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
;
51 _pagesize
= getpagesize();
55 /* calculate the number of nodes to allocate */
56 count
= BLOCKSIZE
/ sizeof(ITEM
);
58 /* allocate a memory block */
60 item
= (ITEM
*)VirtualAlloc(NULL
,
63 PAGE_EXECUTE_READWRITE
);
67 item
= (ITEM
*)mmap(NULL
,
69 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
70 MAP_PRIVATE
| MAP_ANONYMOUS
,
73 if (item
== (void *)MAP_FAILED
)
77 #ifdef MALLOC_CLOSURE_DEBUG
78 printf("block at %p allocated (%d bytes), %d ITEMs\n",
79 item
, count
* sizeof(ITEM
), count
);
81 /* put them into the free list */
82 for (i
= 0; i
< count
; ++i
) {
83 item
->next
= free_list
;
89 /******************************************************************/
91 /* put the item back into the free list */
92 void FreeClosure(void *p
)
94 ITEM
*item
= (ITEM
*)p
;
95 item
->next
= free_list
;
99 /* return one item from the free list, allocating more if needed */
100 void *MallocClosure(void)
108 free_list
= item
->next
;