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 8000
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 top-level allocators state.
  • Loading branch information
ericsnowcurrently committed Nov 8, 2022
commit 737877ca6bbcdf0ec7acecc587c194b01132536d
76 changes: 72 additions & 4 deletions Include/internal/pycore_allocators.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ extern "C" {
#include <stdlib.h> // malloc()


/*************************************/
/* the (raw) malloc allocator */
/*********************************************/
/* the (raw) malloc allocator implementation */

static void *
_PyMem_RawMalloc(void *Py_UNUSED(ctx), size_t size)
Expand Down Expand Up @@ -55,8 +55,8 @@ _PyMem_RawFree(void *Py_UNUSED(ctx), void *ptr)
}


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

#ifdef WITH_PYMALLOC
# ifdef MS_WINDOWS
Expand Down Expand Up @@ -118,6 +118,74 @@ _PyObject_ArenaFree(void *Py_UNUSED(ctx), void *ptr, size_t Py_UNUSED(size))
#endif


/******************/
/* the allocators */

#ifdef WITH_PYMALLOC
/* Forward declaration */
static void* _PyObject_Malloc(void *ctx, size_t size);
static void* _PyObject_Calloc(void *ctx, size_t nelem, size_t elsize);
static void _PyObject_Free(void *ctx, void *p);
static void* _PyObject_Realloc(void *ctx, void *ptr, size_t size);
#endif

#define MALLOC_ALLOC {NULL, _PyMem_RawMalloc, _PyMem_RawCalloc, _PyMem_RawRealloc, _PyMem_RawFree}
#ifdef WITH_PYMALLOC
# define PYMALLOC_ALLOC {NULL, _PyObject_Malloc, _PyObject_Calloc, _PyObject_Realloc, _PyObject_Free}
#endif

#define PYRAW_ALLOC MALLOC_ALLOC
#ifdef WITH_PYMALLOC
# define PYOBJ_ALLOC PYMALLOC_ALLOC
#else
# define PYOBJ_ALLOC MALLOC_ALLOC
#endif
#define PYMEM_ALLOC PYOBJ_ALLOC

typedef struct {
/* We tag each block with an API ID in order to tag API violations */
char api_id;
PyMemAllocatorEx alloc;
} debug_alloc_api_t;
static struct {
debug_alloc_api_t raw;
debug_alloc_api_t mem;
debug_alloc_api_t obj;
} _PyMem_Debug = {
{'r', PYRAW_ALLOC},
{'m', PYMEM_ALLOC},
{'o', PYOBJ_ALLOC}
};

/* Forward declaration */
static void* _PyMem_DebugRawMalloc(void *ctx, size_t size);
static void* _PyMem_DebugRawCalloc(void *ctx, size_t nelem, size_t elsize);
static void* _PyMem_DebugRawRealloc(void *ctx, void *ptr, size_t size);
static void _PyMem_DebugRawFree(void *ctx, void *ptr);

static void* _PyMem_DebugMalloc(void *ctx, size_t size);
static void* _PyMem_DebugCalloc(void *ctx, size_t nelem, size_t elsize);
static void* _PyMem_DebugRealloc(void *ctx, void *ptr, size_t size);
static void _PyMem_DebugFree(void *ctx, void *p);

#define PYDBGRAW_ALLOC \
{&_PyMem_Debug.raw, _PyMem_DebugRawMalloc, _PyMem_DebugRawCalloc, _PyMem_DebugRawRealloc, _PyMem_DebugRawFree}
#define PYDBGMEM_ALLOC \
{&_PyMem_Debug.mem, _PyMem_DebugMalloc, _PyMem_DebugCalloc, _PyMem_DebugRealloc, _PyMem_DebugFree}
#define PYDBGOBJ_ALLOC \
{&_PyMem_Debug.obj, _PyMem_DebugMalloc, _PyMem_DebugCalloc, _PyMem_DebugRealloc, _PyMem_DebugFree}

#ifdef Py_DEBUG
static PyMemAllocatorEx _PyMem_Raw = PYDBGRAW_ALLOC;
static PyMemAllocatorEx _PyMem = PYDBGMEM_ALLOC;
static PyMemAllocatorEx _PyObject = PYDBGOBJ_ALLOC;
#else
static PyMemAllocatorEx _PyMem_Raw = PYRAW_ALLOC;
static PyMemAllocatorEx _PyMem = PYMEM_ALLOC;
static PyMemAllocatorEx _PyObject = PYOBJ_ALLOC;
#endif


#ifdef __cplusplus
}
#endif
Expand Down
66 changes: 0 additions & 66 deletions Objects/obmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "pycore_allocators.h"

#include <stdbool.h>
#include <stdlib.h> // malloc()


/* Defined in tracemalloc.c */
Expand All @@ -17,17 +16,6 @@ extern void _PyMem_DumpTraceback(int fd, const void *ptr);
#undef uint
#define uint unsigned int /* assuming >= 16 bits */

/* Forward declaration */
static void* _PyMem_DebugRawMalloc(void *ctx, size_t size);
static void* _PyMem_DebugRawCalloc(void *ctx, size_t nelem, size_t elsize);
static void* _PyMem_DebugRawRealloc(void *ctx, void *ptr, size_t size);
static void _PyMem_DebugRawFree(void *ctx, void *ptr);

static void* _PyMem_DebugMalloc(void *ctx, size_t size);
static void* _PyMem_DebugCalloc(void *ctx, size_t nelem, size_t elsize);
static void* _PyMem_DebugRealloc(void *ctx, void *ptr, size_t size);
static void _PyMem_DebugFree(void *ctx, void *p);

static void _PyObject_DebugDumpAddress(const void *p);
static void _PyMem_DebugCheckAddress(const char *func, char api_id, const void *p);

Expand Down Expand Up @@ -66,67 +54,13 @@ static void _PyMem_SetupDebugHooksDomain(PyMemAllocatorDomain domain);
# define _Py_NO_SANITIZE_MEMORY
#endif

#ifdef WITH_PYMALLOC
/* Forward declaration */
static void* _PyObject_Malloc(void *ctx, size_t size);
static void* _PyObject_Calloc(void *ctx, size_t nelem, size_t elsize);
static void _PyObject_Free(void *ctx, void *p);
static void* _PyObject_Realloc(void *ctx, void *ptr, size_t size);
#endif


/* bpo-35053: Declare tracemalloc configuration here rather than
Modules/_tracemalloc.c because _tracemalloc can be compiled as dynamic
library, whereas _Py_NewReference() requires it. */
struct _PyTraceMalloc_Config _Py_tracemalloc_config = _PyTraceMalloc_Config_INIT;


#define MALLOC_ALLOC {NULL, _PyMem_RawMalloc, _PyMem_RawCalloc, _PyMem_RawRealloc, _PyMem_RawFree}
#ifdef WITH_PYMALLOC
# define PYMALLOC_ALLOC {NULL, _PyObject_Malloc, _PyObject_Calloc, _PyObject_Realloc, _PyObject_Free}
#endif

#define PYRAW_ALLOC MALLOC_ALLOC
#ifdef WITH_PYMALLOC
# define PYOBJ_ALLOC PYMALLOC_ALLOC
#else
# define PYOBJ_ALLOC MALLOC_ALLOC
#endif
#define PYMEM_ALLOC PYOBJ_ALLOC

typedef struct {
/* We tag each block with an API ID in order to tag API violations */
char api_id;
PyMemAllocatorEx alloc;
} debug_alloc_api_t;
static struct {
debug_alloc_api_t raw;
debug_alloc_api_t mem;
debug_alloc_api_t obj;
} _PyMem_Debug = {
{'r', PYRAW_ALLOC},
{'m', PYMEM_ALLOC},
{'o', PYOBJ_ALLOC}
};

#define PYDBGRAW_ALLOC \
{&_PyMem_Debug.raw, _PyMem_DebugRawMalloc, _PyMem_DebugRawCalloc, _PyMem_DebugRawRealloc, _PyMem_DebugRawFree}
#define PYDBGMEM_ALLOC \
{&_PyMem_Debug.mem, _PyMem_DebugMalloc, _PyMem_DebugCalloc, _PyMem_DebugRealloc, _PyMem_DebugFree}
#define PYDBGOBJ_ALLOC \
{&_PyMem_Debug.obj, _PyMem_DebugMalloc, _PyMem_DebugCalloc, _PyMem_DebugRealloc, _PyMem_DebugFree}

#ifdef Py_DEBUG
static PyMemAllocatorEx _PyMem_Raw = PYDBGRAW_ALLOC;
static PyMemAllocatorEx _PyMem = PYDBGMEM_ALLOC;
static PyMemAllocatorEx _PyObject = PYDBGOBJ_ALLOC;
#else
static PyMemAllocatorEx _PyMem_Raw = PYRAW_ALLOC;
static PyMemAllocatorEx _PyMem = PYMEM_ALLOC;
static PyMemAllocatorEx _PyObject = PYOBJ_ALLOC;
#endif


static int
pymem_set_default_allocator(PyMemAllocatorDomain domain, int debug,
PyMemAllocatorEx *old_alloc)
Expand Down
0