Skip to content

Commit bc450f9

Browse files
authored
Add a paragraph about allocation domains to the C-API docs (GH-24252)
1 parent 916610e commit bc450f9

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

Doc/c-api/memory.rst

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,38 @@ for the I/O buffer escapes completely the Python memory manager.
9292
statistics of the :ref:`pymalloc memory allocator <pymalloc>` every time a
9393
new pymalloc object arena is created, and on shutdown.
9494

95+
Allocator Domains
96+
=================
97+
98+
All allocating functions belong to one of three different "domains" (see also
99+
:c:type`PyMemAllocatorDomain`). These domains represent different allocation
100+
strategies and are optimized for different purposes. The specific details on
101+
how every domain allocates memory or what internal functions each domain calls
102+
is considered an implementation detail, but for debugging purposes a simplified
103+
table can be found at :ref:`here <default-memory-allocators>`. There is no hard
104+
requirement to use the memory returned by the allocation functions belonging to
105+
a given domain for only the purposes hinted by that domain (although this is the
106+
recommended practice). For example, one could use the memory returned by
107+
:c:func:`PyMem_RawMalloc` for allocating Python objects or the memory returned
108+
by :c:func:`PyObject_Malloc` for allocating memory for buffers.
109+
110+
The three allocation domains are:
111+
112+
* Raw domain: intended for allocating memory for general-purpose memory
113+
buffers where the allocation *must* go to the system allocator or where the
114+
allocator can operate without the :term:`GIL`. The memory is requested directly
115+
to the system.
116+
117+
* "Mem" domain: intended for allocating memory for Python buffers and
118+
general-purpose memory buffers where the allocation must be performed with
119+
the :term:`GIL` held. The memory is taken from the Python private heap.
120+
121+
* Object domain: intended for allocating memory belonging to Python objects. The
122+
memory is taken from the Python private heap.
123+
124+
When freeing memory previously allocated by the allocating functions belonging to a
125+
given domain,the matching specific deallocating functions must be used. For example,
126+
:c:func:`PyMem_Free` must be used to free memory allocated using :c:func:`PyMem_Malloc`.
95127

96128
Raw Memory Interface
97129
====================
@@ -601,4 +633,3 @@ heap, objects in Python are allocated and released with :c:func:`PyObject_New`,
601633
602634
These will be explained in the next chapter on defining and implementing new
603635
object types in C.
604-

0 commit comments

Comments
 (0)