From 12b37b02d39e51be9b989f6a8990b90e6c79365a Mon Sep 17 00:00:00 2001 From: Pablo Galindo Date: Tue, 1 Nov 2022 21:35:45 +0000 Subject: [PATCH 1/2] gh-96997: Clarify the contract of PyMem_SetAllocator() --- Doc/c-api/memory.rst | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Doc/c-api/memory.rst b/Doc/c-api/memory.rst index f726cd48663b1c..1c6de3ba42afe5 100644 --- a/Doc/c-api/memory.rst +++ b/Doc/c-api/memory.rst @@ -95,6 +95,8 @@ for the I/O buffer escapes completely the Python memory manager. Allocator Domains ================= +.. _allocator-domains: + All allocating functions belong to one of three different "domains" (see also :c:type:`PyMemAllocatorDomain`). These domains represent different allocation strategies and are optimized for different purposes. The specific details on @@ -479,6 +481,25 @@ Customize Memory Allocators See also :c:member:`PyPreConfig.allocator` and :ref:`Preinitialize Python with PyPreConfig `. + .. warning:: + + :c:func:`PyMem_SetAllocator` does have the following contract: + + * It can be called after :c:func:`Py_PreInitialize` and before + :c:func:`Py_InitializeFromConfig` to install a custom memory + allocator. There are no restrictions over the installed allocator + other than the ones imposed by the domain (for instance, the Raw + Domain allows to be called without the GIL held). See + :ref:`the section on allocator domains ` for more + information. + + * If called after Python has finish initializing (after + :c:func:`Py_InitializeFromConfig` has been called) the allocator + **must** wrap the existing allocator. Substituting the current + allocator for some other arbitrary one is **not supported**. + + + .. c:function:: void PyMem_SetupDebugHooks(void) Setup :ref:`debug hooks in the Python memory allocators ` From f93f7f8fff65282718e8cfa23032cb78a9798a87 Mon Sep 17 00:00:00 2001 From: Pablo Galindo Salgado Date: Wed, 2 Nov 2022 16:40:42 +0000 Subject: [PATCH 2/2] Update Doc/c-api/memory.rst Co-authored-by: Eric Snow --- Doc/c-api/memory.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/c-api/memory.rst b/Doc/c-api/memory.rst index 1c6de3ba42afe5..7041c15d23fb83 100644 --- a/Doc/c-api/memory.rst +++ b/Doc/c-api/memory.rst @@ -489,7 +489,7 @@ Customize Memory Allocators :c:func:`Py_InitializeFromConfig` to install a custom memory allocator. There are no restrictions over the installed allocator other than the ones imposed by the domain (for instance, the Raw - Domain allows to be called without the GIL held). See + Domain allows the allocator to be called without the GIL held). See :ref:`the section on allocator domains ` for more information.