10000 Move the arena allocator to pycore_pymem_init.h. · python/cpython@2b97c3c · GitHub
[go: up one dir, main page]

Skip to content

Commit 2b97c3c

Browse files
Move the arena allocator to pycore_pymem_init.h.
1 parent 73f8991 commit 2b97c3c

File tree

2 files changed

+65
-58
lines changed

2 files changed

+65
-58
lines changed

Include/internal/pycore_allocators.h

Lines changed: 2 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -95,64 +95,8 @@ void _PyMem_DebugFree(void *ctx, void *p);
9595
/***************************************/
9696
/* the object allocator implementation */
9797

98-
#ifdef WITH_PYMALLOC
99-
# ifdef MS_WINDOWS
100-
# include <windows.h>
101-
# elif defined(HAVE_MMAP)
102-
# include <sys/mman.h>
103-
# ifdef MAP_ANONYMOUS
104-
# define ARENAS_USE_MMAP
105-
# endif
106-
# endif
107-
#endif
108-
109-
#ifdef MS_WINDOWS
110-
static void *
111-
_PyObject_ArenaVirtualAlloc(void *Py_UNUSED(ctx), size_t size)
112-
{
113-
return VirtualAlloc(NULL, size,
114-
MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
115-
}
116-
117-
static void
118-
_PyObject_ArenaVirtualFree(void *Py_UNUSED(ctx), void *ptr,
119-
size_t Py_UNUSED(size))
120-
{
121-
VirtualFree(ptr, 0, MEM_RELEASE);
122-
}
123-
124-
#elif defined(ARENAS_USE_MMAP)
125-
static void *
126-
_PyObject_ArenaMmap(void *Py_UNUSED(ctx), size_t size)
127-
{
128-
void *ptr;
129-
ptr = mmap(NULL, size, PROT_READ|PROT_WRITE,
130-
MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
131-
if (ptr == MAP_FAILED)
132-
return NULL;
133-
assert(ptr != NULL);
134-
return ptr;
135-
}
136-
137-
static void
138-
_PyObject_ArenaMunmap(void *Py_UNUSED(ctx), void *ptr, size_t size)
139-
{
140-
munmap(ptr, size);
141-
}
142-
143-
#else
144-
static void *
145-
_PyObject_ArenaMalloc(void *Py_UNUSED(ctx), size_t size)
146-
{
147-
return malloc(size);
148-
}
149-
150-
static void
151-
_PyObject_ArenaFree(void *Py_UNUSED(ctx), 10000 void *ptr, size_t Py_UNUSED(size))
152-
{
153-
free(ptr);
154-
}
155-
#endif
98+
// It is only used to initialize the runtime,
99+
// so it lives in pycore_pymem_init.h.
156100

157101

158102
#ifdef __cplusplus

Include/internal/pycore_pymem_init.h

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,69 @@ extern "C" {
1111
#include "pycore_allocators.h"
1212

1313

14+
/***************************************/
15+
/* the object allocator implementation */
16+
17+
#ifdef WITH_PYMALLOC
18+
# ifdef MS_WINDOWS
19+
# include <windows.h>
20+
# elif defined(HAVE_MMAP)
21+
# include <sys/mman.h>
22+
# ifdef MAP_ANONYMOUS
23+
# define ARENAS_USE_MMAP
24+
# endif
25+
# endif
26+
#endif
27+
28+
#ifdef MS_WINDOWS
29+
static void *
30+
_PyObject_ArenaVirtualAlloc(void *Py_UNUSED(ctx), size_t size)
31+
{
32+
return VirtualAlloc(NULL, size,
33+
MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
34+
}
35+
36+
static void
37+
_PyObject_ArenaVirtualFree(void *Py_UNUSED(ctx), void *ptr,
38+
size_t Py_UNUSED(size))
39+
{
40+
VirtualFree(ptr, 0, MEM_RELEASE);
41+
}
42+
43+
#elif defined(ARENAS_USE_MMAP)
44+
static void *
45+
_PyObject_ArenaMmap(void *Py_UNUSED(ctx), size_t size)
46+
{
47+
void *ptr;
48+
ptr = mmap(NULL, size, PROT_READ|PROT_WRITE,
49+
MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
50+
if (ptr == MAP_FAILED)
51+
return NULL;
52+
assert(ptr != NULL);
53+
return ptr;
54+
}
55+
56+
static void
57+
_PyObject_ArenaMunmap(void *Py_UNUSED(ctx), void *ptr, size_t size)
58+
{
59+
munmap(ptr, size);
60+
}
61+
62+
#else
63+
static void *
64+
_PyObject_ArenaMalloc(void *Py_UNUSED(ctx), size_t size)
65+
{
66+
return malloc(size);
67+
}
68+
69+
static void
70+
_PyObject_ArenaFree(void *Py_UNUSED(ctx), void *ptr, size_t Py_UNUSED(size))
71+
{
72+
free(ptr);
73+
}
74+
#endif
75+
76+
1477
/********************************/
1578
/* the allocators' initializers */
1679

0 commit comments

Comments
 (0)
0