1 /*****************************************************************
2 This file should be kept compatible with Python 2.3, see PEP 291.
3 *****************************************************************/
12 # if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
13 # define MAP_ANONYMOUS MAP_ANON
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.
23 #define BLOCKSIZE _pagesize
25 /* #define MALLOC_CLOSURE_DEBUG */ /* enable for some debugging output */
27 /******************************************************************/
29 typedef union _tagITEM
{
34 static ITEM
*free_list
;
37 static void more_core(void)
42 /* determine the pagesize */
45 SYSTEM_INFO systeminfo
;
46 GetSystemInfo(&systeminfo
);
47 _pagesize
= systeminfo
.dwPageSize
;
52 _pagesize
= sysconf(_SC_PAGESIZE
);
54 _pagesize
= getpagesize();
59 /* calculate the number of nodes to allocate */
60 count
= BLOCKSIZE
/ sizeof(ITEM
);
62 /* allocate a memory block */
64 item
= (ITEM
*)VirtualAlloc(NULL
,
67 PAGE_EXECUTE_READWRITE
);
71 item
= (ITEM
*)mmap(NULL
,
73 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
74 MAP_PRIVATE
| MAP_ANONYMOUS
,
77 if (item
== (void *)MAP_FAILED
)
81 #ifdef MALLOC_CLOSURE_DEBUG
82 printf("block at %p allocated (%d bytes), %d ITEMs\n",
83 item
, count
* sizeof(ITEM
), count
);
85 /* put them into the free list */
86 for (i
= 0; i
< count
; ++i
) {
87 item
->next
= free_list
;
93 /******************************************************************/
95 /* put the item back into the free list */
96 void _ctypes_free_closure(void *p
)
98 ITEM
*item
= (ITEM
*)p
;
99 item
->next
= free_list
;
103 /* return one item from the free list, allocating more if needed */
104 void *_ctypes_alloc_closure(void)
112 free_list
= item
->next
;