Debian Codegraph @

Modules/_ctypes/malloc_closure.c

raw
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 typedef union _tagITEM {
31 ffi_closure closure;
32 union _tagITEM *next;
33 } ITEM;
34
35 static ITEM *free_list;
36 static int _pagesize;
37
38 static void more_core(void)
39 {
40 ITEM *item;
41 int count, i;
42
43 /* determine the pagesize */
44 #ifdef MS_WIN32
45 if (!_pagesize) {
46 SYSTEM_INFO systeminfo;
47 GetSystemInfo(&systeminfo);
48 _pagesize = systeminfo.dwPageSize;
49 }
50 #else
51 if (!_pagesize) {
52 #ifdef _SC_PAGESIZE
53 _pagesize = sysconf(_SC_PAGESIZE);
54 #else
55 _pagesize = getpagesize();
56 #endif
57 }
58 #endif
59
60 /* calculate the number of nodes to allocate */
61 count = BLOCKSIZE / sizeof(ITEM);
62
63 /* allocate a memory block */
64 #ifdef MS_WIN32
65 item = (ITEM *)VirtualAlloc(NULL,
66 count * sizeof(ITEM),
67 MEM_COMMIT,
68 PAGE_EXECUTE_READWRITE);
69 if (item == NULL)
70 return;
71 #else
72 item = (ITEM *)mmap(NULL,
73 count * sizeof(ITEM),
74 PROT_READ | PROT_WRITE | PROT_EXEC,
75 MAP_PRIVATE | MAP_ANONYMOUS,
76 -1,
77 0);
78 if (item == (void *)MAP_FAILED)
79 return;
80 #endif
81
82 #ifdef MALLOC_CLOSURE_DEBUG
83 printf("block at %p allocated (%d bytes), %d ITEMs\n",
84 item, count * (int)sizeof(ITEM), count);
85 #endif
86 /* put them into the free list */
87 for (i = 0; i < count; ++i) {
88 item->next = free_list;
89 free_list = item;
90 ++item;
91 }
92 }
93
94 /******************************************************************/
95
96 /* put the item back into the free list */
97 void Py_ffi_closure_free(void *p)
98 {
99 #ifdef HAVE_FFI_CLOSURE_ALLOC
100 #ifdef USING_APPLE_OS_LIBFFI
101 # ifdef HAVE_BUILTIN_AVAILABLE
102 if (__builtin_available(macos 10.15, ios 13, watchos 6, tvos 13, *)) {
103 # else
104 if (ffi_closure_free != NULL) {
105 # endif
106 #endif
107 ffi_closure_free(p);
108 return;
109 #ifdef USING_APPLE_OS_LIBFFI
110 }
111 #endif
112 #endif
113 ITEM *item = (ITEM *)p;
114 item->next = free_list;
115 free_list = item;
116 }
117
118 /* return one item from the free list, allocating more if needed */
119 void *Py_ffi_closure_alloc(size_t size, void** codeloc)
120 {
121 #ifdef HAVE_FFI_CLOSURE_ALLOC
122 #ifdef USING_APPLE_OS_LIBFFI
123 # ifdef HAVE_BUILTIN_AVAILABLE
124 if (__builtin_available(macos 10.15, ios 13, watchos 6, tvos 13, *)) {
125 # else
126 if (ffi_closure_alloc != NULL) {
127 # endif
128 #endif
129 return ffi_closure_alloc(size, codeloc);
130 #ifdef USING_APPLE_OS_LIBFFI
131 }
132 #endif
133 #endif
134 ITEM *item;
135 if (!free_list)
136 more_core();
137 if (!free_list)
138 return NULL;
139 item = free_list;
140 free_list = item->next;
141 #ifdef _M_ARM
142 // set Thumb bit so that blx is called correctly
143 *codeloc = (ITEM*)((uintptr_t)item | 1);
144 #else
145 *codeloc = (void *)item;
146 #endif
147 return (void *)item;
148 }