Commit 9fbbf216 authored by John Kirkham's avatar John Kirkham

Add Python Raw memory helper functions

In Python 3.4+, Raw memory helper functions were added to wrap, track,
and check `malloc` and `free` calls for C memory outside of the GIL.
These have the same API as the existing `PyMem_*` calls except they use
Raw and never require the GIL. For Python 2/3 compatibility, export the
existing `PyMem_` functions on Python 2 as `PyMem_Raw` where they act
basically equivalently. These are especially important as Python 3.6
changed the existing `PyMem_` functions to allocate from pymalloc
instead of the C allocator meaning the GIL must now be held with them.
So these are Python 2/3 alternatives that can be relied on to not
require the GIL.
parent 63a81578
from cpython.version cimport PY_VERSION_HEX
cdef extern from "Python.h":
#####################################################################
......@@ -22,6 +24,15 @@ cdef extern from "Python.h":
# for the I/O buffer escapes completely the Python memory
# manager."
IF PY_VERSION_HEX >= 0x03040000:
void* PyMem_RawMalloc(size_t n) nogil
void* PyMem_RawRealloc(void *p, size_t n) nogil
void PyMem_RawFree(void *p) nogil
ELSE:
void* PyMem_RawMalloc "PyMem_Malloc" (size_t n) nogil
void* PyMem_RawRealloc "PyMem_Realloc" (void *p, size_t n) nogil
void PyMem_RawFree "PyMem_Free" (void *p) nogil
# 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
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment