[Python-checkins] peps: PEP 445
victor.stinner
python-checkins at python.org
Tue Jun 18 01:52:26 CEST 2013
http://hg.python.org/peps/rev/0883803524a0
changeset: 4937:0883803524a0
user: Victor Stinner <victor.stinner at gmail.com>
date: Tue Jun 18 01:52:14 2013 +0200
summary:
PEP 445
files:
pep-0445.txt | 45 +++++++++++++++++++++++++++++++++++----
1 files changed, 40 insertions(+), 5 deletions(-)
diff --git a/pep-0445.txt b/pep-0445.txt
--- a/pep-0445.txt
+++ b/pep-0445.txt
@@ -67,6 +67,10 @@
- ``void PyMem_SetupDebugHooks(void)``
+
+Use these new APIs
+------------------
+
* ``PyMem_Malloc()`` and ``PyMem_Realloc()`` now always call ``malloc()`` and
``realloc()``, instead of calling ``PyObject_Malloc()`` and
``PyObject_Realloc()`` in debug mode
@@ -76,6 +80,12 @@
``PyObject_Realloc()`` falls back on ``PyMem_Realloc()`` instead of
``realloc()``
+* Replace direct calls to ``malloc()`` with ``PyMem_Malloc()``, or
+ ``PyMem_RawMalloc()`` if the GIL is not held
+
+* Configure external libraries like zlib or OpenSSL to use
+ ``PyMem_RawMalloc()``
+
Examples
========
@@ -137,7 +147,7 @@
}
.. warning::
- Remove the call ``PyMem_SetRawAllocators(&alloc);`` if the new allocators
+ Remove the call ``PyMem_SetRawAllocators(&alloc)`` if the new allocators
are not thread-safe.
@@ -185,7 +195,7 @@
}
.. warning::
- Remove the call ``PyMem_SetRawAllocators(&alloc);`` if the new allocators
+ Remove the call ``PyMem_SetRawAllocators(&alloc)`` if the new allocators
are not thread-safe.
@@ -255,9 +265,13 @@
PyObject_SetAllocators(&alloc);
}
+.. warning::
+ Remove the call ``PyMem_SetRawAllocators(&alloc)`` if hooks are not
+ thread-safe.
+
.. note::
- No need to call ``PyMem_SetupDebugHooks()``: it is already installed by
- default.
+ ``PyMem_SetupDebugHooks()`` does not need to be called: Python debug hooks
+ are installed automatically at startup.
Performances
@@ -339,7 +353,28 @@
PyMem_Malloc() GIL-free
-----------------------
-There is no real reason to require the GIL when calling PyMem_Malloc().
+There is no real reason to require the GIL when calling ``PyMem_Malloc()``.
+
+Allowing to call ``PyMem_Malloc()`` without holding the GIL might break
+applications which setup their own allocator or their allocator hooks. Holding
+the GIL is very convinient to develop a custom allocator or a hook (no need to
+care of other threads, no need to handle mutexes, etc.).
+
+
+Don't add PyMem_RawMalloc()
+---------------------------
+
+Replace ``malloc()`` with ``PyMem_Malloc()``, but if the GIL is not held: keep
+``malloc()`` unchanged.
+
+The ``PyMem_Malloc()`` is sometimes already misused. For example, the
+``main()`` and ``Py_Main()`` functions of Python call ``PyMem_Malloc()``
+whereas the GIL do not exist yet. In this case, ``PyMem_Malloc()`` should
+be replaced with ``malloc()``.
+
+If an hook is used to the track memory usage, the ``malloc()`` memory will not
+be seen. Remaining ``malloc()`` may allocate a lot of memory and so would be
+missed in reports.
CCP API
--
Repository URL: http://hg.python.org/peps
More information about the Python-checkins
mailing list