Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
add locking to malloc closure
  • Loading branch information
kumaraditya303 committed Mar 24, 2025
commit 931c4d80ff8359791dca99cd86920678a8f0d1cc
21 changes: 19 additions & 2 deletions Modules/_ctypes/malloc_closure.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@

/******************************************************************/


#ifdef Py_GIL_DISABLED
static PyMutex malloc_closure_lock;
# define MALLOC_CLOSURE_LOCK() PyMutex_Lock(&malloc_closure_lock)
# define MALLOC_CLOSURE_UNLOCK() PyMutex_Unlock(&malloc_closure_lock)
#else
# define MALLOC_CLOSURE_LOCK() ((void)0)
# define MALLOC_CLOSURE_UNLOCK() ((void)0)
#endif

typedef union _tagITEM {
ffi_closure closure;
union _tagITEM *next;
Expand Down Expand Up @@ -110,9 +120,11 @@ void Py_ffi_closure_free(void *p)
}
#endif
#endif
MALLOC_CLOSURE_LOCK();
ITEM *item = (ITEM *)p;
item->next = free_list;
free_list = item;
MALLOC_CLOSURE_UNLOCK();
}

/* return one item from the free list, allocating more if needed */
Expand All @@ -131,11 +143,15 @@ void *Py_ffi_closure_alloc(size_t size, void** codeloc)
}
#endif
#endif
MALLOC_CLOSURE_LOCK();
ITEM *item;
if (!free_list)
if (!free_list) {
more_core();
if (!free_list)
}
if (!free_list) {
MALLOC_CLOSURE_UNLOCK();
return NULL;
}
item = free_list;
free_list = item->next;
#ifdef _M_ARM
Expand All @@ -144,5 +160,6 @@ void *Py_ffi_closure_alloc(size_t size, void** codeloc)
#else
*codeloc = (void *)item;
#endif
MALLOC_CLOSURE_UNLOCK();
return (void *)item;
}
Loading