@@ -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
96128Raw Memory Interface
97129====================
@@ -272,6 +304,12 @@ The following function sets, modeled after the ANSI C standard, but specifying
272304behavior when requesting zero bytes, are available for allocating and releasing
273305memory from the Python heap.
274306
307+ .. note ::
308+ There is no guarantee that the memory returned by these allocators can be
309+ succesfully casted to a Python object when intercepting the allocating
310+ functions in this domain by the methods described in
311+ the :ref: `Customize Memory Allocators <customize-memory-allocators >` section.
312+
275313The :ref: `default object allocator <default-memory-allocators >` uses the
276314:ref: `pymalloc memory allocator <pymalloc >`.
277315
@@ -353,6 +391,7 @@ Legend:
353391* ``pymalloc ``: :ref: `pymalloc memory allocator <pymalloc >`
354392* "+ debug": with debug hooks installed by :c:func: `PyMem_SetupDebugHooks `
355393
394+ .. _customize-memory-allocators :
356395
357396Customize Memory Allocators
358397===========================
@@ -601,4 +640,3 @@ heap, objects in Python are allocated and released with :c:func:`PyObject_New`,
601640
602641These will be explained in the next chapter on defining and implementing new
603642object types in C.
604-
0 commit comments