Skip to content

Commit 44074db

Browse files
Add an explanation to the custom allocators section.
1 parent 2e155a0 commit 44074db

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

‎Doc/c-api/memory.rst‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,35 @@ Legend:
448448
Customize Memory Allocators
449449
===========================
450450
451+
The Python runtime may be configured, with :c:func:`PyMem_SetAllocator`,
452+
to use custom allocators for any of the
453+
:ref:`supported allocator domains <allocator-domains>`.
454+
There are two kinds of custom allocator:
455+
456+
* actual allocator implementations
457+
* wrappers around other allocators (AKA "hooks")
458+
459+
Applications that embed Python may set either kind before the runtime
460+
is initialized (e.g. with :c:func:`Py_InitializeFromConfig`). However,
461+
from that point on only wrappers around the current allocator
462+
(see :c:func:`PyMem_GetAllocator`) may be set. Thus, extension modules
463+
may only set wrappers.
464+
465+
An actual allocator is responsible for managing its own state and memory
466+
pool. For the "mem" and "object" domains, the allocator is responsible
467+
for maintaining its state and allocations relative to the
468+
:c:func:`current interpreter <PyInterpreterState_Get>`.
469+
The ``PyMemAllocatorEx.ctx`` field may be ``NULL`` for
470+
this kind of allocator.
471+
472+
A wrapper can be useful for tracking allocations, adjusting the behavior
473+
of another allocator (e.g. handling failures differently), or debugging.
474+
For example, debug builds of CPython use a wrapper to track memory usage
475+
and find memory leaks, and the :mod:`tracemalloc` module sets a wrapper
476+
to identify where allocations happen. Typically, the wrapped allocator
477+
is stored in (or within) the ``PyMemAllocatorEx.ctx`` field
478+
of the wrapper.
479+
451480
.. versionadded:: 3.4
452481
453482
.. c:type:: PyMemAllocatorEx

0 commit comments

Comments
 (0)