Skip to content

Commit 92484f4

Browse files
erlend-aaslandvstinner
authored andcommitted
gh-85283: Add PyMem_RawMalloc() to the limited C API
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.
1 parent 4116592 commit 92484f4

File tree

4 files changed

+16
-6
lines changed

4 files changed

+16
-6
lines changed

Doc/whatsnew/3.13.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,10 @@ New Features
886886
(with an underscore prefix).
887887
(Contributed by Victor Stinner in :gh:`108014`.)
888888

889+
* Add :c:func:`PyMem_RawMalloc`, :c:func:`PyMem_RawCalloc`,
890+
:c:func:`PyMem_RawRealloc` and :c:func:`PyMem_RawFree` to the limited C API.
891+
(Contributed by Victor Stinner in :gh:`85283`.)
892+
889893
Porting to Python 3.13
890894
----------------------
891895

Include/cpython/pymem.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@
22
# error "this header file must not be included directly"
33
#endif
44

5-
PyAPI_FUNC(void *) PyMem_RawMalloc(size_t size);
6-
PyAPI_FUNC(void *) PyMem_RawCalloc(size_t nelem, size_t elsize);
7-
PyAPI_FUNC(void *) PyMem_RawRealloc(void *ptr, size_t new_size);
8-
PyAPI_FUNC(void) PyMem_RawFree(void *ptr);
9-
10-
115
typedef enum {
126
/* PyMem_RawMalloc(), PyMem_RawRealloc() and PyMem_RawFree() */
137
PYMEM_DOMAIN_RAW,

Include/pymem.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,15 @@ PyAPI_FUNC(void) PyMem_Free(void *ptr);
9191
#define PyMem_DEL(p) PyMem_Free((p))
9292

9393

94+
// Memory allocator which doesn't require the GIL to be held.
95+
// Usually, it's just a thin wrapper to functions of the standard C library:
96+
// malloc(), calloc(), realloc() and free(). The difference is that
97+
// tracemalloc can track these memory allocations.
98+
PyAPI_FUNC(void *) PyMem_RawMalloc(size_t size);
99+
PyAPI_FUNC(void *) PyMem_RawCalloc(size_t nelem, size_t elsize);
100+
PyAPI_FUNC(void *) PyMem_RawRealloc(void *ptr, size_t new_size);
101+
PyAPI_FUNC(void) PyMem_RawFree(void *ptr);
102+
94103
#ifndef Py_LIMITED_API
95104
# define Py_CPYTHON_PYMEM_H
96105
# include "cpython/pymem.h"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Add :c:func:`PyMem_RawMalloc`, :c:func:`PyMem_RawCalloc`,
2+
:c:func:`PyMem_RawRealloc` and :c:func:`PyMem_RawFree` to the limited C API.
3+
Patch by Victor Stinner.

0 commit comments

Comments
 (0)