Skip to content

Commit

Permalink
Move the arena allocator to pycore_pymem_init.h.
Browse files Browse the repository at this point in the history
  • Loading branch information
ericsnowcurrently committed Nov 8, 2022
1 parent 73f8991 commit 2b97c3c
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 58 deletions.
60 changes: 2 additions & 58 deletions Include/internal/pycore_allocators.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,64 +95,8 @@ void _PyMem_DebugFree(void *ctx, void *p);
/***************************************/
/* the object allocator implementation */

#ifdef WITH_PYMALLOC
# ifdef MS_WINDOWS
# include <windows.h>
# elif defined(HAVE_MMAP)
# include <sys/mman.h>
# ifdef MAP_ANONYMOUS
# define ARENAS_USE_MMAP
# endif
# endif
#endif

#ifdef MS_WINDOWS
static void *
_PyObject_ArenaVirtualAlloc(void *Py_UNUSED(ctx), size_t size)
{
return VirtualAlloc(NULL, size,
MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
}

static void
_PyObject_ArenaVirtualFree(void *Py_UNUSED(ctx), void *ptr,
size_t Py_UNUSED(size))
{
VirtualFree(ptr, 0, MEM_RELEASE);
}

#elif defined(ARENAS_USE_MMAP)
static void *
_PyObject_ArenaMmap(void *Py_UNUSED(ctx), size_t size)
{
void *ptr;
ptr = mmap(NULL, size, PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
if (ptr == MAP_FAILED)
return NULL;
assert(ptr != NULL);
return ptr;
}

static void
_PyObject_ArenaMunmap(void *Py_UNUSED(ctx), void *ptr, size_t size)
{
munmap(ptr, size);
}

#else
static void *
_PyObject_ArenaMalloc(void *Py_UNUSED(ctx), size_t size)
{
return malloc(size);
}

static void
_PyObject_ArenaFree(void *Py_UNUSED(ctx), void *ptr, size_t Py_UNUSED(size))
{
free(ptr);
}
#endif
// It is only used to initialize the runtime,
// so it lives in pycore_pymem_init.h.


#ifdef __cplusplus
Expand Down
63 changes: 63 additions & 0 deletions Include/internal/pycore_pymem_init.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,69 @@ extern "C" {
#include "pycore_allocators.h"


/***************************************/
/* the object allocator implementation */

#ifdef WITH_PYMALLOC
# ifdef MS_WINDOWS
# include <windows.h>
# elif defined(HAVE_MMAP)
# include <sys/mman.h>
# ifdef MAP_ANONYMOUS
# define ARENAS_USE_MMAP
# endif
# endif
#endif

#ifdef MS_WINDOWS
static void *
_PyObject_ArenaVirtualAlloc(void *Py_UNUSED(ctx), size_t size)
{
return VirtualAlloc(NULL, size,
MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
}

static void
_PyObject_ArenaVirtualFree(void *Py_UNUSED(ctx), void *ptr,
size_t Py_UNUSED(size))
{
VirtualFree(ptr, 0, MEM_RELEASE);
}

#elif defined(ARENAS_USE_MMAP)
static void *
_PyObject_ArenaMmap(void *Py_UNUSED(ctx), size_t size)
{
void *ptr;
ptr = mmap(NULL, size, PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
if (ptr == MAP_FAILED)
return NULL;
assert(ptr != NULL);
return ptr;
}

static void
_PyObject_ArenaMunmap(void *Py_UNUSED(ctx), void *ptr, size_t size)
{
munmap(ptr, size);
}

#else
static void *
_PyObject_ArenaMalloc(void *Py_UNUSED(ctx), size_t size)
{
return malloc(size);
}

static void
_PyObject_ArenaFree(void *Py_UNUSED(ctx), void *ptr, size_t Py_UNUSED(size))
{
free(ptr);
}
#endif


/********************************/
/* the allocators' initializers */

Expand Down

0 comments on commit 2b97c3c

Please sign in to comment.