8000 gh-96997: Clarify the contract of PyMem_SetAllocator() (#98977) · python/cpython@c053284 · GitHub
[go: up one dir, main page]

Skip to content

Commit c053284

Browse files
authored
gh-96997: Clarify the contract of PyMem_SetAllocator() (#98977)
1 parent 908e81f commit c053284

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

Doc/c-api/memory.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ for the I/O buffer escapes completely the Python memory manager.
9595
Allocator Domains
9696
=================
9797

98+
.. _allocator-domains:
99+
98100
All allocating functions belong to one of three different "domains" (see also
99101
:c:type:`PyMemAllocatorDomain`). These domains represent different allocation
100102
strategies and are optimized for different purposes. The specific details on
@@ -479,6 +481,25 @@ Customize Memory Allocators
479481
See also :c:member:`PyPreConfig.allocator` and :ref:`Preinitialize Python
480482
with PyPreConfig <c-preinit>`.
481483
484+
.. warning::
485+
486+
:c:func:`PyMem_SetAllocator` does have the following contract:
487+
488+
* It can be called after :c:func:`Py_PreInitialize` and before
489+
:c:func:`Py_InitializeFromConfig` to install a custom memory
490+
allocator. There are no restrictions over the installed allocator
491+
other than the ones imposed by the domain (for instance, the Raw
492+
Domain allows the allocator to be called without the GIL held). See
493+
:ref:`the section on allocator domains <allocator-domains>` for more
494+
information.
495+
496+
* If called after Python has finish initializing (after
497+
:c:func:`Py_InitializeFromConfig` has been called) the allocator
498+
**must** wrap the existing allocator. Substituting the current
499+
allocator for some other arbitrary one is **not supported**.
500+
501+
502+
482503
.. c:function:: void PyMem_SetupDebugHooks(void)
483504
484505
Setup :ref:`debug hooks in the Python memory allocators <pymem-debug-hooks>`

0 commit comments

Comments
 (0)
0