Skip to content

Commit

Permalink
gh-119802: Update memory management docs for free-threaded build (gh-…
Browse files Browse the repository at this point in the history
…124006)

* gh-119802: Update memory management docs for free-threaded build

* nit

* nit

* Address code review

* nit

* Update Doc/c-api/memory.rst

Co-authored-by: Sam Gross <colesbury@gmail.com>

---------

Co-authored-by: Sam Gross <colesbury@gmail.com>
  • Loading branch information
corona10 and colesbury authored Sep 13, 2024
1 parent bb904e0 commit e6bb1a2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
34 changes: 22 additions & 12 deletions Doc/c-api/memory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -102,30 +102,38 @@ All allocating functions belong to one of three different "domains" (see also
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.
table can be found at :ref:`here <default-memory-allocators>`.
The APIs used to allocate and free a block of memory must be from the same domain.
For example, :c:func:`PyMem_Free` must be used to free memory allocated using :c:func:`PyMem_Malloc`.

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.
from the system. See :ref:`Raw Memory Interface <raw-memoryinterface>`.

* "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.
See :ref:`Memory Interface <memoryinterface>`.

* Object domain: intended for allocating memory belonging to Python objects. The
memory is taken from the Python private heap.
* Object domain: intended for allocating memory for Python objects. The
memory is taken from the Python private heap. See :ref:`Object allocators <objectinterface>`.

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`.
.. note::

The :term:`free-threaded <free threading>` build requires that only Python objects are allocated using the "object" domain
and that all Python objects are allocated using that domain. This differs from the prior Python versions,
where this was only a best practice and not a hard requirement.

For example, buffers (non-Python objects) should be allocated using :c:func:`PyMem_Malloc`,
:c:func:`PyMem_RawMalloc`, or :c:func:`malloc`, but not :c:func:`PyObject_Malloc`.

See :ref:`Memory Allocation APIs <free-threaded-memory-allocation>`.


.. _raw-memoryinterface:

Raw Memory Interface
====================
Expand Down Expand Up @@ -299,6 +307,8 @@ versions and is therefore deprecated in extension modules.
* ``PyMem_DEL(ptr)``
.. _objectinterface:
Object allocators
=================
Expand Down
2 changes: 2 additions & 0 deletions Doc/howto/free-threading-extensions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ Some of these functions were added in Python 3.13. You can use the
to provide implementations of these functions for older Python versions.


.. _free-threaded-memory-allocation:

Memory Allocation APIs
======================

Expand Down

0 comments on commit e6bb1a2

Please sign in to comment.