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