bpo-36763: Add PyMemAllocatorName (GH-13387)
* Add PyMemAllocatorName enum
* _PyPreConfig.allocator type becomes PyMemAllocatorName, instead of
char*
* Remove _PyPreConfig_Clear()
* Add _PyMem_GetAllocatorName()
* Rename _PyMem_GetAllocatorsName() to
_PyMem_GetCurrentAllocatorName()
* Remove _PyPreConfig_SetAllocator(): just call
_PyMem_SetupAllocators() directly, we don't have do reallocate the
configuration with the new allocator anymore!
* _PyPreConfig_Write() parameter becomes const, as it should be in
the first place!
diff --git a/Include/cpython/coreconfig.h b/Include/cpython/coreconfig.h
index 1ba2663..dca4134 100644
--- a/Include/cpython/coreconfig.h
+++ b/Include/cpython/coreconfig.h
@@ -120,7 +120,9 @@
int utf8_mode;
int dev_mode; /* Development mode. PYTHONDEVMODE, -X dev */
- char *allocator; /* Memory allocator: PYTHONMALLOC */
+
+ /* Memory allocator: PYTHONMALLOC env var */
+ PyMemAllocatorName allocator;
} _PyPreConfig;
#ifdef MS_WINDOWS
@@ -137,7 +139,7 @@
.isolated = -1, \
.use_environment = -1, \
.dev_mode = -1, \
- .allocator = NULL}
+ .allocator = PYMEM_ALLOCATOR_NOT_SET}
/* --- _PyCoreConfig ---------------------------------------------- */
diff --git a/Include/cpython/pymem.h b/Include/cpython/pymem.h
index bd66506..79f063b 100644
--- a/Include/cpython/pymem.h
+++ b/Include/cpython/pymem.h
@@ -11,12 +11,8 @@
PyAPI_FUNC(void *) PyMem_RawRealloc(void *ptr, size_t new_size);
PyAPI_FUNC(void) PyMem_RawFree(void *ptr);
-/* Configure the Python memory allocators. Pass NULL to use default
- allocators. */
-PyAPI_FUNC(int) _PyMem_SetupAllocators(const char *opt);
-
/* Try to get the allocators name set by _PyMem_SetupAllocators(). */
-PyAPI_FUNC(const char*) _PyMem_GetAllocatorsName(void);
+PyAPI_FUNC(const char*) _PyMem_GetCurrentAllocatorName(void);
PyAPI_FUNC(void *) PyMem_Calloc(size_t nelem, size_t elsize);
@@ -41,6 +37,19 @@
PYMEM_DOMAIN_OBJ
} PyMemAllocatorDomain;
+typedef enum {
+ PYMEM_ALLOCATOR_NOT_SET = 0,
+ PYMEM_ALLOCATOR_DEFAULT = 1,
+ PYMEM_ALLOCATOR_DEBUG = 2,
+ PYMEM_ALLOCATOR_MALLOC = 3,
+ PYMEM_ALLOCATOR_MALLOC_DEBUG = 4,
+#ifdef WITH_PYMALLOC
+ PYMEM_ALLOCATOR_PYMALLOC = 5,
+ PYMEM_ALLOCATOR_PYMALLOC_DEBUG = 6,
+#endif
+} PyMemAllocatorName;
+
+
typedef struct {
/* user context passed as the first argument to the 4 functions */
void *ctx;
diff --git a/Include/internal/pycore_coreconfig.h b/Include/internal/pycore_coreconfig.h
index d48904e..ccb7948 100644
--- a/Include/internal/pycore_coreconfig.h
+++ b/Include/internal/pycore_coreconfig.h
@@ -88,7 +88,6 @@
/* --- _PyPreConfig ----------------------------------------------- */
-PyAPI_FUNC(void) _PyPreConfig_Clear(_PyPreConfig *config);
PyAPI_FUNC(int) _PyPreConfig_Copy(_PyPreConfig *config,
const _PyPreConfig *config2);
PyAPI_FUNC(PyObject*) _PyPreConfig_AsDict(const _PyPreConfig *config);
@@ -96,7 +95,7 @@
const _PyCoreConfig *core_config);
PyAPI_FUNC(_PyInitError) _PyPreConfig_Read(_PyPreConfig *config,
const _PyArgv *args);
-PyAPI_FUNC(_PyInitError) _PyPreConfig_Write(_PyPreConfig *config);
+PyAPI_FUNC(_PyInitError) _PyPreConfig_Write(const _PyPreConfig *config);
/* --- _PyCoreConfig ---------------------------------------------- */
diff --git a/Include/internal/pycore_pymem.h b/Include/internal/pycore_pymem.h
index 20f3b5e..dcc492a 100644
--- a/Include/internal/pycore_pymem.h
+++ b/Include/internal/pycore_pymem.h
@@ -179,6 +179,15 @@
#endif
}
+PyAPI_FUNC(int) _PyMem_GetAllocatorName(
+ const char *name,
+ PyMemAllocatorName *allocator);
+
+/* Configure the Python memory allocators.
+ Pass PYMEM_ALLOCATOR_DEFAULT to use default allocators.
+ PYMEM_ALLOCATOR_NOT_SET does nothing. */
+PyAPI_FUNC(int) _PyMem_SetupAllocators(PyMemAllocatorName allocator);
+
#ifdef __cplusplus
}
#endif