diff options
author | Victor Stinner <vstinner@python.org> | 2023-10-17 00:41:51 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-17 00:41:51 (GMT) |
commit | cc71cc925662cb089b5c6fe17df00d00045dfd71 (patch) | |
tree | 66629945768e18b3ec3ec33a02f9a78f018847c2 | |
parent | cf9c25c719ba9b0f5bde90fc8b8bba7942d10151 (diff) | |
download | cpython-cc71cc925662cb089b5c6fe17df00d00045dfd71.zip cpython-cc71cc925662cb089b5c6fe17df00d00045dfd71.tar.gz cpython-cc71cc925662cb089b5c6fe17df00d00045dfd71.tar.bz2 |
gh-85283: Add PyMem_RawMalloc() to the limited C API (#108570)
Add PyMem_RawMalloc(), PyMem_RawCalloc(), PyMem_RawRealloc() and
PyMem_RawFree() to the limited C API.
These functions were added by Python 3.4 and are needed to port
stdlib extensions to the limited C API, like grp and pwd.
Co-authored-by: Erlend E. Aasland <erlend@python.org>
-rw-r--r-- | Doc/data/stable_abi.dat | 4 | ||||
-rw-r--r-- | Doc/whatsnew/3.13.rst | 5 | ||||
-rw-r--r-- | Include/cpython/pymem.h | 6 | ||||
-rw-r--r-- | Include/pymem.h | 11 | ||||
-rw-r--r-- | Lib/test/test_stable_abi_ctypes.py | 4 | ||||
-rw-r--r-- | Misc/NEWS.d/next/C API/2023-08-28-17-34-10.gh-issue-85283.f1zXcc.rst | 3 | ||||
-rw-r--r-- | Misc/stable_abi.toml | 8 | ||||
-rwxr-xr-x | PC/python3dll.c | 4 |
8 files changed, 39 insertions, 6 deletions
diff --git a/Doc/data/stable_abi.dat b/Doc/data/stable_abi.dat index 6ec9c90..267d2b8 100644 --- a/Doc/data/stable_abi.dat +++ b/Doc/data/stable_abi.dat @@ -388,6 +388,10 @@ function,PyMapping_Values,3.2,, function,PyMem_Calloc,3.7,, function,PyMem_Free,3.2,, function,PyMem_Malloc,3.2,, +function,PyMem_RawCalloc,3.13,, +function,PyMem_RawFree,3.13,, +function,PyMem_RawMalloc,3.13,, +function,PyMem_RawRealloc,3.13,, function,PyMem_Realloc,3.2,, type,PyMemberDef,3.2,,full-abi var,PyMemberDescr_Type,3.2,, diff --git a/Doc/whatsnew/3.13.rst b/Doc/whatsnew/3.13.rst index 01f8208..ec76659 100644 --- a/Doc/whatsnew/3.13.rst +++ b/Doc/whatsnew/3.13.rst @@ -1053,6 +1053,11 @@ New Features parameter names. (Contributed by Serhiy Storchaka in :gh:`110815`.) +* Add :c:func:`PyMem_RawMalloc`, :c:func:`PyMem_RawCalloc`, + :c:func:`PyMem_RawRealloc` and :c:func:`PyMem_RawFree` to the limited C API + (version 3.13). + (Contributed by Victor Stinner in :gh:`85283`.) + Porting to Python 3.13 ---------------------- diff --git a/Include/cpython/pymem.h b/Include/cpython/pymem.h index b75f1c4..dc4a65c 100644 --- a/Include/cpython/pymem.h +++ b/Include/cpython/pymem.h @@ -2,12 +2,6 @@ # error "this header file must not be included directly" #endif -PyAPI_FUNC(void *) PyMem_RawMalloc(size_t size); -PyAPI_FUNC(void *) PyMem_RawCalloc(size_t nelem, size_t elsize); -PyAPI_FUNC(void *) PyMem_RawRealloc(void *ptr, size_t new_size); -PyAPI_FUNC(void) PyMem_RawFree(void *ptr); - - typedef enum { /* PyMem_RawMalloc(), PyMem_RawRealloc() and PyMem_RawFree() */ PYMEM_DOMAIN_RAW, diff --git a/Include/pymem.h b/Include/pymem.h index 68e33f9..a80da99 100644 --- a/Include/pymem.h +++ b/Include/pymem.h @@ -87,6 +87,17 @@ PyAPI_FUNC(void) PyMem_Free(void *ptr); #define PyMem_DEL(p) PyMem_Free((p)) +#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030d0000 +// Memory allocator which doesn't require the GIL to be held. +// Usually, it's just a thin wrapper to functions of the standard C library: +// malloc(), calloc(), realloc() and free(). The difference is that +// tracemalloc can track these memory allocations. +PyAPI_FUNC(void *) PyMem_RawMalloc(size_t size); +PyAPI_FUNC(void *) PyMem_RawCalloc(size_t nelem, size_t elsize); +PyAPI_FUNC(void *) PyMem_RawRealloc(void *ptr, size_t new_size); +PyAPI_FUNC(void) PyMem_RawFree(void *ptr); +#endif + #ifndef Py_LIMITED_API # define Py_CPYTHON_PYMEM_H # include "cpython/pymem.h" diff --git a/Lib/test/test_stable_abi_ctypes.py b/Lib/test/test_stable_abi_ctypes.py index e06f9ca..0c831c4 100644 --- a/Lib/test/test_stable_abi_ctypes.py +++ b/Lib/test/test_stable_abi_ctypes.py @@ -418,6 +418,10 @@ SYMBOL_NAMES = ( "PyMem_Calloc", "PyMem_Free", "PyMem_Malloc", + "PyMem_RawCalloc", + "PyMem_RawFree", + "PyMem_RawMalloc", + "PyMem_RawRealloc", "PyMem_Realloc", "PyMemberDescr_Type", "PyMember_GetOne", diff --git a/Misc/NEWS.d/next/C API/2023-08-28-17-34-10.gh-issue-85283.f1zXcc.rst b/Misc/NEWS.d/next/C API/2023-08-28-17-34-10.gh-issue-85283.f1zXcc.rst new file mode 100644 index 0000000..1c25fdb --- /dev/null +++ b/Misc/NEWS.d/next/C API/2023-08-28-17-34-10.gh-issue-85283.f1zXcc.rst @@ -0,0 +1,3 @@ +Add :c:func:`PyMem_RawMalloc`, :c:func:`PyMem_RawCalloc`, +:c:func:`PyMem_RawRealloc` and :c:func:`PyMem_RawFree` to the limited C API. +Patch by Victor Stinner. diff --git a/Misc/stable_abi.toml b/Misc/stable_abi.toml index 9d66b92..f8ee973 100644 --- a/Misc/stable_abi.toml +++ b/Misc/stable_abi.toml @@ -2466,3 +2466,11 @@ added = '3.13' [function.PyUnicode_EqualToUTF8AndSize] added = '3.13' +[function.PyMem_RawMalloc] + added = '3.13' +[function.PyMem_RawCalloc] + added = '3.13' +[function.PyMem_RawRealloc] + added = '3.13' +[function.PyMem_RawFree] + added = '3.13' diff --git a/PC/python3dll.c b/PC/python3dll.c index 7ee1174..dd3be3a 100755 --- a/PC/python3dll.c +++ b/PC/python3dll.c @@ -373,6 +373,10 @@ EXPORT_FUNC(PyMarshal_WriteObjectToString) EXPORT_FUNC(PyMem_Calloc) EXPORT_FUNC(PyMem_Free) EXPORT_FUNC(PyMem_Malloc) +EXPORT_FUNC(PyMem_RawCalloc) +EXPORT_FUNC(PyMem_RawFree) +EXPORT_FUNC(PyMem_RawMalloc) +EXPORT_FUNC(PyMem_RawRealloc) EXPORT_FUNC(PyMem_Realloc) EXPORT_FUNC(PyMember_GetOne) EXPORT_FUNC(PyMember_SetOne) |