diff options
author | Pablo Galindo <Pablogsal@gmail.com> | 2021-01-18 22:20:57 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-18 22:20:57 (GMT) |
commit | bc450f9694fb8aa7a7a3d2f9d46f8b200e8aa4b3 (patch) | |
tree | ed5945e61c974a3bcc80f34b5252f36bbabd40b7 /Doc/c-api/memory.rst | |
parent | 916610ef90a0d0761f08747f7b0905541f0977c7 (diff) | |
download | cpython-bc450f9694fb8aa7a7a3d2f9d46f8b200e8aa4b3.zip cpython-bc450f9694fb8aa7a7a3d2f9d46f8b200e8aa4b3.tar.gz cpython-bc450f9694fb8aa7a7a3d2f9d46f8b200e8aa4b3.tar.bz2 |
Add a paragraph about allocation domains to the C-API docs (GH-24252)
Diffstat (limited to 'Doc/c-api/memory.rst')
-rw-r--r-- | Doc/c-api/memory.rst | 33 |
1 files changed, 32 insertions, 1 deletions
diff --git a/Doc/c-api/memory.rst b/Doc/c-api/memory.rst index 87425bc..bd73780 100644 --- a/Doc/c-api/memory.rst +++ b/Doc/c-api/memory.rst @@ -92,6 +92,38 @@ for the I/O buffer escapes completely the Python memory manager. statistics of the :ref:`pymalloc memory allocator <pymalloc>` every time a new pymalloc object arena is created, and on shutdown. +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 +how every domain allocates memory or what internal functions each domain calls +is considered an implementation detail, but for debugging purposes a simplified +table can be found at :ref:`here <default-memory-allocators>`. There is no hard +requirement to use the memory returned by the allocation functions belonging to +a given domain for only the purposes hinted by that domain (although this is the +recommended practice). For example, one could use the memory returned by +:c:func:`PyMem_RawMalloc` for allocating Python objects or the memory returned +by :c:func:`PyObject_Malloc` for allocating memory for buffers. + +The three allocation domains are: + +* Raw domain: intended for allocating memory for general-purpose memory + buffers where the allocation *must* go to the system allocator or where the + allocator can operate without the :term:`GIL`. The memory is requested directly + to the system. + +* "Mem" domain: intended for allocating memory for Python buffers and + general-purpose memory buffers where the allocation must be performed with + the :term:`GIL` held. The memory is taken from the Python private heap. + +* Object domain: intended for allocating memory belonging to Python objects. The + memory is taken from the Python private heap. + +When freeing memory previously allocated by the allocating functions belonging to a +given domain,the matching specific deallocating functions must be used. For example, +:c:func:`PyMem_Free` must be used to free memory allocated using :c:func:`PyMem_Malloc`. Raw Memory Interface ==================== @@ -601,4 +633,3 @@ heap, objects in Python are allocated and released with :c:func:`PyObject_New`, These will be explained in the next chapter on defining and implementing new object types in C. - |