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