summaryrefslogtreecommitdiffstats
path: root/Doc/c-api/memory.rst
diff options
context:
space:
mode:
Diffstat (limited to 'Doc/c-api/memory.rst')
-rw-r--r--Doc/c-api/memory.rst113
1 files changed, 77 insertions, 36 deletions
diff --git a/Doc/c-api/memory.rst b/Doc/c-api/memory.rst
index 290ef09..3ff5452 100644
--- a/Doc/c-api/memory.rst
+++ b/Doc/c-api/memory.rst
@@ -85,9 +85,12 @@ for the I/O buffer escapes completely the Python memory manager.
.. seealso::
+ The :envvar:`PYTHONMALLOC` environment variable can be used to configure
+ the memory allocators used by Python.
+
The :envvar:`PYTHONMALLOCSTATS` environment variable can be used to print
- memory allocation statistics every time a new object arena is created, and
- on shutdown.
+ statistics of the :ref:`pymalloc memory allocator <pymalloc>` every time a
+ new pymalloc object arena is created, and on shutdown.
Raw Memory Interface
@@ -162,15 +165,17 @@ The following function sets, modeled after the ANSI C standard, but specifying
behavior when requesting zero bytes, are available for allocating and releasing
memory from the Python heap.
-The default memory block allocator uses the following functions:
-:c:func:`malloc`, :c:func:`calloc`, :c:func:`realloc` and :c:func:`free`; call
-``malloc(1)`` (or ``calloc(1, 1)``) when requesting zero bytes.
+By default, these functions use :ref:`pymalloc memory allocator <pymalloc>`.
.. warning::
The :term:`GIL <global interpreter lock>` must be held when using these
functions.
+.. versionchanged:: 3.6
+
+ The default allocator is now pymalloc instead of system :c:func:`malloc`.
+
.. c:function:: void* PyMem_Malloc(size_t n)
Allocates *n* bytes and returns a pointer of type :c:type:`void\*` to the
@@ -292,15 +297,32 @@ Customize Memory Allocators
Enum used to identify an allocator domain. Domains:
- * :c:data:`PYMEM_DOMAIN_RAW`: functions :c:func:`PyMem_RawMalloc`,
- :c:func:`PyMem_RawRealloc`, :c:func:`PyMem_RawCalloc` and
- :c:func:`PyMem_RawFree`
- * :c:data:`PYMEM_DOMAIN_MEM`: functions :c:func:`PyMem_Malloc`,
- :c:func:`PyMem_Realloc`, :c:func:`PyMem_Calloc` and :c:func:`PyMem_Free`
- * :c:data:`PYMEM_DOMAIN_OBJ`: functions :c:func:`PyObject_Malloc`,
- :c:func:`PyObject_Realloc`, :c:func:`PyObject_Calloc` and
- :c:func:`PyObject_Free`
+ .. c:var:: PYMEM_DOMAIN_RAW
+
+ Functions:
+
+ * :c:func:`PyMem_RawMalloc`
+ * :c:func:`PyMem_RawRealloc`
+ * :c:func:`PyMem_RawCalloc`
+ * :c:func:`PyMem_RawFree`
+
+ .. c:var:: PYMEM_DOMAIN_MEM
+
+ Functions:
+
+ * :c:func:`PyMem_Malloc`,
+ * :c:func:`PyMem_Realloc`
+ * :c:func:`PyMem_Calloc`
+ * :c:func:`PyMem_Free`
+ .. c:var:: PYMEM_DOMAIN_OBJ
+
+ Functions:
+
+ * :c:func:`PyObject_Malloc`
+ * :c:func:`PyObject_Realloc`
+ * :c:func:`PyObject_Calloc`
+ * :c:func:`PyObject_Free`
.. c:function:: void PyMem_GetAllocator(PyMemAllocatorDomain domain, PyMemAllocatorEx *allocator)
@@ -325,43 +347,62 @@ Customize Memory Allocators
.. c:function:: void PyMem_SetupDebugHooks(void)
- Setup hooks to detect bugs in the following Python memory allocator
- functions:
-
- - :c:func:`PyMem_RawMalloc`, :c:func:`PyMem_RawRealloc`,
- :c:func:`PyMem_RawCalloc`, :c:func:`PyMem_RawFree`
- - :c:func:`PyMem_Malloc`, :c:func:`PyMem_Realloc`, :c:func:`PyMem_Calloc`,
- :c:func:`PyMem_Free`
- - :c:func:`PyObject_Malloc`, :c:func:`PyObject_Realloc`,
- :c:func:`PyObject_Calloc`, :c:func:`PyObject_Free`
+ Setup hooks to detect bugs in the Python memory allocator functions.
Newly allocated memory is filled with the byte ``0xCB``, freed memory is
- filled with the byte ``0xDB``. Additional checks:
+ filled with the byte ``0xDB``.
- - detect API violations, ex: :c:func:`PyObject_Free` called on a buffer
+ Runtime checks:
+
+ - Detect API violations, ex: :c:func:`PyObject_Free` called on a buffer
allocated by :c:func:`PyMem_Malloc`
- - detect write before the start of the buffer (buffer underflow)
- - detect write after the end of the buffer (buffer overflow)
+ - Detect write before the start of the buffer (buffer underflow)
+ - Detect write after the end of the buffer (buffer overflow)
+ - Check that the :term:`GIL <global interpreter lock>` is held when
+ allocator functions of :c:data:`PYMEM_DOMAIN_OBJ` (ex:
+ :c:func:`PyObject_Malloc`) and :c:data:`PYMEM_DOMAIN_MEM` (ex:
+ :c:func:`PyMem_Malloc`) domains are called
+
+ On error, the debug hooks use the :mod:`tracemalloc` module to get the
+ traceback where a memory block was allocated. The traceback is only
+ displayed if :mod:`tracemalloc` is tracing Python memory allocations and the
+ memory block was traced.
- The function does nothing if Python is not compiled is debug mode.
+ These hooks are installed by default if Python is compiled in debug
+ mode. The :envvar:`PYTHONMALLOC` environment variable can be used to install
+ debug hooks on a Python compiled in release mode.
+ .. versionchanged:: 3.6
+ This function now also works on Python compiled in release mode.
+ On error, the debug hooks now use :mod:`tracemalloc` to get the traceback
+ where a memory block was allocated. The debug hooks now also check
+ if the GIL is held when functions of :c:data:`PYMEM_DOMAIN_OBJ` and
+ :c:data:`PYMEM_DOMAIN_MEM` domains are called.
-Customize PyObject Arena Allocator
-==================================
-Python has a *pymalloc* allocator for allocations smaller than 512 bytes. This
-allocator is optimized for small objects with a short lifetime. It uses memory
-mappings called "arenas" with a fixed size of 256 KB. It falls back to
-:c:func:`PyMem_RawMalloc` and :c:func:`PyMem_RawRealloc` for allocations larger
-than 512 bytes. *pymalloc* is the default allocator used by
-:c:func:`PyObject_Malloc`.
+.. _pymalloc:
-The default arena allocator uses the following functions:
+The pymalloc allocator
+======================
+
+Python has a *pymalloc* allocator optimized for small objects (smaller or equal
+to 512 bytes) with a short lifetime. It uses memory mappings called "arenas"
+with a fixed size of 256 KB. It falls back to :c:func:`PyMem_RawMalloc` and
+:c:func:`PyMem_RawRealloc` for allocations larger than 512 bytes.
+
+*pymalloc* is the default allocator of the :c:data:`PYMEM_DOMAIN_MEM` (ex:
+:c:func:`PyObject_Malloc`) and :c:data:`PYMEM_DOMAIN_OBJ` (ex:
+:c:func:`PyObject_Malloc`) domains.
+
+The arena allocator uses the following functions:
* :c:func:`VirtualAlloc` and :c:func:`VirtualFree` on Windows,
* :c:func:`mmap` and :c:func:`munmap` if available,
* :c:func:`malloc` and :c:func:`free` otherwise.
+Customize pymalloc Arena Allocator
+----------------------------------
+
.. versionadded:: 3.4
.. c:type:: PyObjectArenaAllocator