| 1 |
#ifndef Py_BUILD_CORE_BUILTIN |
| 2 |
# define Py_BUILD_CORE_MODULE 1 |
| 3 |
#endif |
| 4 |
|
| 5 |
#include <Python.h> |
| 6 |
#include <ffi.h> |
| 7 |
#ifdef MS_WIN32 |
| 8 |
# include <windows.h> |
| 9 |
#else |
| 10 |
# include <sys/mman.h> |
| 11 |
# include <unistd.h> // sysconf() |
| 12 |
# if !defined(MAP_ANONYMOUS) && defined(MAP_ANON) |
| 13 |
# define MAP_ANONYMOUS MAP_ANON |
| 14 |
# endif |
| 15 |
#endif |
| 16 |
#include "ctypes.h" |
| 17 |
|
| 18 |
/* BLOCKSIZE can be adjusted. Larger blocksize will take a larger memory |
| 19 |
overhead, but allocate less blocks from the system. It may be that some |
| 20 |
systems have a limit of how many mmap'd blocks can be open. |
| 21 |
*/ |
| 22 |
|
| 23 |
#define BLOCKSIZE _pagesize |
| 24 |
|
| 25 |
/* #define MALLOC_CLOSURE_DEBUG */ /* enable for some debugging output */ |
| 26 |
|
| 27 |
|
| 28 |
/******************************************************************/ |
| 29 |
|
| 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 |
|
| 40 |
typedef union _tagITEM { |
| 41 |
ffi_closure closure; |
| 42 |
union _tagITEM *next; |
| 43 |
} ITEM; |
| 44 |
|
| 45 |
static ITEM *free_list; |
| 46 |
static int _pagesize; |
| 47 |
|
| 48 |
static void more_core(void) |
| 49 |
{ |
| 50 |
ITEM *item; |
| 51 |
int count, i; |
| 52 |
|
| 53 |
/* determine the pagesize */ |
| 54 |
#ifdef MS_WIN32 |
| 55 |
if (!_pagesize) { |
| 56 |
SYSTEM_INFO systeminfo; |
| 57 |
GetSystemInfo(&systeminfo); |
| 58 |
_pagesize = systeminfo.dwPageSize; |
| 59 |
} |
| 60 |
#else |
| 61 |
if (!_pagesize) { |
| 62 |
#ifdef _SC_PAGESIZE |
| 63 |
_pagesize = sysconf(_SC_PAGESIZE); |
| 64 |
#else |
| 65 |
_pagesize = getpagesize(); |
| 66 |
#endif |
| 67 |
} |
| 68 |
#endif |
| 69 |
|
| 70 |
/* calculate the number of nodes to allocate */ |
| 71 |
count = BLOCKSIZE / sizeof(ITEM); |
| 72 |
|
| 73 |
/* allocate a memory block */ |
| 74 |
#ifdef MS_WIN32 |
| 75 |
item = (ITEM *)VirtualAlloc(NULL, |
| 76 |
count * sizeof(ITEM), |
| 77 |
MEM_COMMIT, |
| 78 |
PAGE_EXECUTE_READWRITE); |
| 79 |
if (item == NULL) |
| 80 |
return; |
| 81 |
#else |
| 82 |
item = (ITEM *)mmap(NULL, |
| 83 |
count * sizeof(ITEM), |
| 84 |
PROT_READ | PROT_WRITE | PROT_EXEC, |
| 85 |
MAP_PRIVATE | MAP_ANONYMOUS, |
| 86 |
-1, |
| 87 |
0); |
| 88 |
if (item == (void *)MAP_FAILED) |
| 89 |
return; |
| 90 |
#endif |
| 91 |
|
| 92 |
#ifdef MALLOC_CLOSURE_DEBUG |
| 93 |
printf("block at %p allocated (%d bytes), %d ITEMs\n", |
| 94 |
item, count * (int)sizeof(ITEM), count); |
| 95 |
#endif |
| 96 |
/* put them into the free list */ |
| 97 |
for (i = 0; i < count; ++i) { |
| 98 |
item->next = free_list; |
| 99 |
free_list = item; |
| 100 |
++item; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
/******************************************************************/ |
| 105 |
|
| 106 |
/* put the item back into the free list */ |
| 107 |
void Py_ffi_closure_free(void *p) |
| 108 |
{ |
| 109 |
#ifdef HAVE_FFI_CLOSURE_ALLOC |
| 110 |
#ifdef USING_APPLE_OS_LIBFFI |
| 111 |
# ifdef HAVE_BUILTIN_AVAILABLE |
| 112 |
if (__builtin_available(macos 10.15, ios 13, watchos 6, tvos 13, *)) { |
| 113 |
# else |
| 114 |
if (ffi_closure_free != NULL) { |
| 115 |
# endif |
| 116 |
#endif |
| 117 |
ffi_closure_free(p); |
| 118 |
return; |
| 119 |
#ifdef USING_APPLE_OS_LIBFFI |
| 120 |
} |
| 121 |
#endif |
| 122 |
#endif |
| 123 |
MALLOC_CLOSURE_LOCK(); |
| 124 |
ITEM *item = (ITEM *)p; |
| 125 |
item->next = free_list; |
| 126 |
free_list = item; |
| 127 |
MALLOC_CLOSURE_UNLOCK(); |
| 128 |
} |
| 129 |
|
| 130 |
/* return one item from the free list, allocating more if needed */ |
| 131 |
void *Py_ffi_closure_alloc(size_t size, void** codeloc) |
| 132 |
{ |
| 133 |
#ifdef HAVE_FFI_CLOSURE_ALLOC |
| 134 |
#ifdef USING_APPLE_OS_LIBFFI |
| 135 |
# ifdef HAVE_BUILTIN_AVAILABLE |
| 136 |
if (__builtin_available(macos 10.15, ios 13, watchos 6, tvos 13, *)) { |
| 137 |
# else |
| 138 |
if (ffi_closure_alloc != NULL) { |
| 139 |
# endif |
| 140 |
#endif |
| 141 |
return ffi_closure_alloc(size, codeloc); |
| 142 |
#ifdef USING_APPLE_OS_LIBFFI |
| 143 |
} |
| 144 |
#endif |
| 145 |
#endif |
| 146 |
MALLOC_CLOSURE_LOCK(); |
| 147 |
ITEM *item; |
| 148 |
if (!free_list) { |
| 149 |
more_core(); |
| 150 |
} |
| 151 |
if (!free_list) { |
| 152 |
MALLOC_CLOSURE_UNLOCK(); |
| 153 |
return NULL; |
| 154 |
} |
| 155 |
item = free_list; |
| 156 |
free_list = item->next; |
| 157 |
#ifdef _M_ARM |
| 158 |
// set Thumb bit so that blx is called correctly |
| 159 |
*codeloc = (ITEM*)((uintptr_t)item | 1); |
| 160 |
#else |
| 161 |
*codeloc = (void *)item; |
| 162 |
#endif |
| 163 |
MALLOC_CLOSURE_UNLOCK(); |
| 164 |
return (void *)item; |
| 165 |
} |