8000 gh-81057: Move the Allocators to _PyRuntimeState by ericsnowcurrently · Pull Request #99217 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-81057: Move the Allocators to _PyRuntimeState #99217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
9a44e7a
Look up the current interpreter in PyObject_Malloc().
ericsnowcurrently Oct 5, 2022
16cd128
Move the low-level allocator implementations to a header file.
ericsnowcurrently Oct 6, 2022
737877c
Move the top-level allocators state.
ericsnowcurrently Oct 6, 2022
6dd7ba5
Move the allocators to the runtime state.
ericsnowcurrently Oct 6, 2022
56251cb
Drop debug code.
ericsnowcurrently Oct 6, 2022
007eed4
Move the top-level object allocator to the runtime state.
ericsnowcurrently Oct 6, 2022
38a80d8
Move the arenas state to the runtime state.
ericsnowcurrently Oct 7, 2022
83d397f
Pass the context to initializers explicitly.
ericsnowcurrently Oct 7, 2022
991dfc8
Move the allocator groupings to pycore_allocators.h.
ericsnowcurrently Nov 8, 2022
ac5cfb0
Move the arena allocator to pycore_pymem_init.h.
ericsnowcurrently Nov 8, 2022
9f32218
Add a NEWS entry.
ericsnowcurrently Nov 7, 2022
e5fc139
Move the object allocator definitions to pycore_obmalloc*.h.
ericsnowcurrently Nov 8, 2022
d61b410
pycore_allocators.h -> pycore_pymem_allocators.h
ericsnowcurrently Nov 8, 2022
0efcef7
Move the arena allocator impl to its own file.
ericsnowcurrently Nov 8, 2022
ec01f99
Move _Py_GetAllocatedBlocks() over.
ericsnowcurrently Nov 8, 2022
a1ff9b0
Move _PyObject_DebugMallocStat() over.
ericsnowcurrently Nov 8, 2022
3172cbe
Restore uint for obmalloc.
ericsnowcurrently Nov 8, 2022
adc60e2
Do not define the functions in header files.
ericsnowcurrently Nov 9, 2022
5262207
Drop pycore_*_allocators.h.
ericsnowcurrently Nov 9, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Move the object allocator definitions to pycore_obmalloc*.h.
  • Loading branch information
ericsnowcurrently committed Nov 8, 2022
commit e5fc1394449dc1eed7c126d204817a2d14837367
10 changes: 9 additions & 1 deletion Include/internal/pycore_obmalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,10 @@ on that C doesn't insert any padding anywhere in a pool_header at or before
the prevpool member.
**************************************************************************** */

#define OBMALLOC_USED_POOLS_SIZE (2 * ((NB_SMALL_SIZE_CLASSES + 7) / 8) * 8)

struct _obmalloc_pools {
poolp used[2 * ((NB_SMALL_SIZE_CLASSES + 7) / 8) * 8];
poolp used[OBMALLOC_USED_POOLS_SIZE];
};


Expand Down Expand Up @@ -654,6 +656,12 @@ struct _obmalloc_state {
struct _obmalloc_usage usage;
};


/* Allocate memory directly from the O/S virtual memory system,
* where supported. Otherwise fallback on malloc */
void *_PyObject_VirtualAlloc(size_t size);
void _PyObject_VirtualFree(void *, size_t size);

#ifdef __cplusplus
}
#endif
Expand Down
78 changes: 78 additions & 0 deletions Include/internal/pycore_obmalloc_init.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,84 @@ extern "C" {
}


/***************************************/
/* the low-level 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 object allocator's initializer */

#ifdef MS_WINDOWS
# define _pymem_allocators_obj_arena_INIT \
{ NULL, _PyObject_ArenaVirtualAlloc, _PyObject_ArenaVirtualFree }
#elif defined(ARENAS_USE_MMAP)
# define _pymem_allocators_obj_arena_INIT \
{ NULL, _PyObject_ArenaMmap, _PyObject_ArenaMunmap }
#else
# define _pymem_allocators_obj_arena_INIT \
{ NULL, _PyObject_ArenaMalloc, _PyObject_ArenaFree }
#endif


#ifdef __cplusplus
}
#endif
Expand Down
5 changes: 0 additions & 5 deletions Include/internal/pycore_pymem.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,6 @@ struct _PyTraceMalloc_Config {

PyAPI_DATA(struct _PyTraceMalloc_Config) _Py_tracemalloc_config;

/* Allocate memory directly from the O/S virtual memory system,
* where supported. Otherwise fallback on malloc */
void *_PyObject_VirtualAlloc(size_t size);
void _PyObject_VirtualFree(void *, size_t size);

/* This function returns the number of allocated memory blocks, regardless of size */
PyAPI_FUNC(Py_ssize_t) _Py_GetAllocatedBlocks(void);

Expand Down
74 changes: 0 additions & 74 deletions Include/internal/pycore_pymem_init.h
8000
Original file line number Diff line number Diff line change
Expand Up @@ -11,69 +11,6 @@ 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 All @@ -100,17 +37,6 @@ _PyObject_ArenaFree(void *Py_UNUSED(ctx), void *ptr, size_t Py_UNUSED(size))
{'o', PYOBJ_ALLOC}, \
}

#ifdef MS_WINDOWS
# define _pymem_allocators_obj_arena_INIT \
{ NULL, _PyObject_ArenaVirtualAlloc, _PyObject_ArenaVirtualFree }
#elif defined(ARENAS_USE_MMAP)
# define _pymem_allocators_obj_arena_INIT \
{ NULL, _PyObject_ArenaMmap, _PyObject_ArenaMunmap }
#else
# define _pymem_allocators_obj_arena_INIT \
{ NULL, _PyObject_ArenaMalloc, _PyObject_ArenaFree }
#endif


#ifdef __cplusplus
}
Expand Down
0