From 5d39e0429029324cae90bba2f19fb689b007c7d6 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 29 Nov 2017 17:20:38 +0100 Subject: bpo-32030: Rework memory allocators (#4625) * Fix _PyMem_SetupAllocators("debug"): always restore allocators to the defaults, rather than only caling _PyMem_SetupDebugHooks(). * Add _PyMem_SetDefaultAllocator() helper to set the "default" allocator. * Add _PyMem_GetAllocatorsName(): get the name of the allocators * main() now uses debug hooks on memory allocators if Py_DEBUG is defined, rather than calling directly malloc() * Document default memory allocators in C API documentation * _Py_InitializeCore() now fails with a fatal user error if PYTHONMALLOC value is an unknown memory allocator, instead of failing with a fatal internal error. * Add new tests on the PYTHONMALLOC environment variable * Add support.with_pymalloc() * Add the _testcapi.WITH_PYMALLOC constant and expose it as support.with_pymalloc(). * sysconfig.get_config_var('WITH_PYMALLOC') doesn't work on Windows, so replace it with support.with_pymalloc(). * pythoninfo: add _testcapi collector for pymem --- Programs/python.c | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) (limited to 'Programs/python.c') diff --git a/Programs/python.c b/Programs/python.c index 707e38f257..22d55bbc4c 100644 --- a/Programs/python.c +++ b/Programs/python.c @@ -33,12 +33,9 @@ main(int argc, char **argv) exit(1); } - /* Force malloc() allocator to bootstrap Python */ -#ifdef Py_DEBUG - (void)_PyMem_SetupAllocators("malloc_debug"); -# else - (void)_PyMem_SetupAllocators("malloc"); -# endif + /* Force default allocator, to be able to release memory above + with a known allocator. */ + _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, NULL); argv_copy = (wchar_t **)PyMem_RawMalloc(sizeof(wchar_t*) * (argc+1)); argv_copy2 = (wchar_t **)PyMem_RawMalloc(sizeof(wchar_t*) * (argc+1)); @@ -98,13 +95,9 @@ main(int argc, char **argv) status = Py_Main(argc, argv_copy); - /* Force again malloc() allocator to release memory blocks allocated - before Py_Main() */ -#ifdef Py_DEBUG - (void)_PyMem_SetupAllocators("malloc_debug"); -# else - (void)_PyMem_SetupAllocators("malloc"); -# endif + /* Py_Main() can change PyMem_RawMalloc() allocator, so restore the default + to release memory blocks allocated before Py_Main() */ + _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, NULL); for (i = 0; i < argc; i++) { PyMem_RawFree(argv_copy2[i]); -- cgit v1.2.1