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 --- Python/pystate.c | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) (limited to 'Python/pystate.c') diff --git a/Python/pystate.c b/Python/pystate.c index ecf921d0c2..0fb8ed0719 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -35,8 +35,8 @@ to avoid the expense of doing their own locking). extern "C" { #endif -_PyInitError -_PyRuntimeState_Init(_PyRuntimeState *runtime) +static _PyInitError +_PyRuntimeState_Init_impl(_PyRuntimeState *runtime) { memset(runtime, 0, sizeof(*runtime)); @@ -59,14 +59,26 @@ _PyRuntimeState_Init(_PyRuntimeState *runtime) return _Py_INIT_OK(); } +_PyInitError +_PyRuntimeState_Init(_PyRuntimeState *runtime) +{ + /* Force default allocator, since _PyRuntimeState_Fini() must + use the same allocator than this function. */ + PyMemAllocatorEx old_alloc; + _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); + + _PyInitError err = _PyRuntimeState_Init_impl(runtime); + + PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); + return err; +} + void _PyRuntimeState_Fini(_PyRuntimeState *runtime) { - /* Use the same memory allocator than _PyRuntimeState_Init() */ - PyMemAllocatorEx old_alloc, raw_alloc; - PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); - _PyMem_GetDefaultRawAllocator(&raw_alloc); - PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &raw_alloc); + /* Force the allocator used by _PyRuntimeState_Init(). */ + PyMemAllocatorEx old_alloc; + _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); if (runtime->interpreters.mutex != NULL) { PyThread_free_lock(runtime->interpreters.mutex); -- cgit v1.2.1