Skip to content

Commit d515a09

Browse files
Merge branch 'main' of https://github.com/python/cpython into critical-ctypes
2 parents a05195e + f1967e7 commit d515a09

2 files changed

Lines changed: 19 additions & 3 deletions

File tree

Modules/_ctypes/_ctypes.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5120,7 +5120,6 @@ PyCArrayType_from_ctype(ctypes_state *st, PyObject *itemtype, Py_ssize_t length)
51205120
PyObject *len;
51215121

51225122
assert(st->array_cache != NULL);
5123-
51245123
len = PyLong_FromSsize_t(length);
51255124
if (len == NULL)
51265125
return NULL;

Modules/_ctypes/malloc_closure.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@
2727

2828
/******************************************************************/
2929

30+
31+
#ifdef Py_GIL_DISABLED
32+
static PyMutex malloc_closure_lock;
33+
# define MALLOC_CLOSURE_LOCK() PyMutex_Lock(&malloc_closure_lock)
34+
# define MALLOC_CLOSURE_UNLOCK() PyMutex_Unlock(&malloc_closure_lock)
35+
#else
36+
# define MALLOC_CLOSURE_LOCK() ((void)0)
37+
# define MALLOC_CLOSURE_UNLOCK() ((void)0)
38+
#endif
39+
3040
typedef union _tagITEM {
3141
ffi_closure closure;
3242
union _tagITEM *next;
@@ -110,9 +120,11 @@ void Py_ffi_closure_free(void *p)
110120
}
111121
#endif
112122
#endif
123+
MALLOC_CLOSURE_LOCK();
113124
ITEM *item = (ITEM *)p;
114125
item->next = free_list;
115126
free_list = item;
127+
MALLOC_CLOSURE_UNLOCK();
116128
}
117129

118130
/* return one item from the free list, allocating more if needed */
@@ -131,11 +143,15 @@ void *Py_ffi_closure_alloc(size_t size, void** codeloc)
131143
}
132144
#endif
133145
#endif
146+
MALLOC_CLOSURE_LOCK();
134147
ITEM *item;
135-
if (!free_list)
148+
if (!free_list) {
136149
more_core();
137-
if (!free_list)
150+
}
151+
if (!free_list) {
152+
MALLOC_CLOSURE_UNLOCK();
138153
return NULL;
154+
}
139155
item = free_list;
140156
free_list = item->next;
141157
#ifdef _M_ARM
@@ -144,5 +160,6 @@ void *Py_ffi_closure_alloc(size_t size, void** codeloc)
144160
#else
145161
*codeloc = (void *)item;
146162
#endif
163+
MALLOC_CLOSURE_UNLOCK();
147164
return (void *)item;
148165
}

0 commit comments

Comments
 (0)