Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d8f0d92

Browse files
committed
Issue #21233: Rename the C structure "PyMemAllocator" to "PyMemAllocatorEx" to
make sure that the code using it will be adapted for the new "calloc" field (instead of crashing).
1 parent aa0e7af commit d8f0d92

6 files changed

Lines changed: 32 additions & 29 deletions

File tree

Doc/c-api/memory.rst

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ Customize Memory Allocators
232232
233233
.. versionadded:: 3.4
234234
235-
.. c:type:: PyMemAllocator
235+
.. c:type:: PyMemAllocatorEx
236236
237237
Structure used to describe a memory block allocator. The structure has
238238
four fields:
@@ -253,7 +253,9 @@ Customize Memory Allocators
253253
+----------------------------------------------------------+---------------------------------------+
254254
255255
.. versionchanged:: 3.5
256-
Add a new field ``calloc``.
256+
The :c:type:`PyMemAllocator` structure was renamed to
257+
:c:type:`PyMemAllocatorEx` and a new ``calloc`` field was added.
258+
257259
258260
.. c:type:: PyMemAllocatorDomain
259261
@@ -267,12 +269,12 @@ Customize Memory Allocators
267269
:c:func:`PyObject_Realloc` and :c:func:`PyObject_Free`
268270
269271
270-
.. c:function:: void PyMem_GetAllocator(PyMemAllocatorDomain domain, PyMemAllocator *allocator)
272+
.. c:function:: void PyMem_GetAllocator(PyMemAllocatorDomain domain, PyMemAllocatorEx *allocator)
271273
272274
Get the memory block allocator of the specified domain.
273275
274276
275-
.. c:function:: void PyMem_SetAllocator(PyMemAllocatorDomain domain, PyMemAllocator *allocator)
277+
.. c:function:: void PyMem_SetAllocator(PyMemAllocatorDomain domain, PyMemAllocatorEx *allocator)
276278
277279
Set the memory block allocator of the specified domain.
278280

Doc/whatsnew/3.5.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,4 +271,5 @@ Changes in the Python API
271271
Changes in the C API
272272
--------------------
273273

274-
* The :c:type:`PyMemAllocator` structure has a new ``calloc`` field.
274+
* The :c:type:`PyMemAllocator` structure was renamed to
275+
:c:type:`PyMemAllocatorEx` and a new ``calloc`` field was added.

Include/pymem.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ typedef enum {
128128
} PyMemAllocatorDomain;
129129

130130
typedef struct {
131-
/* user context passed as the first argument to the 3 functions */
131+
/* user context passed as the first argument to the 4 functions */
132132
void *ctx;
133133

134134
/* allocate a memory block */
@@ -142,11 +142,11 @@ typedef struct {
142142

143143
/* release a memory block */
144144
void (*free) (void *ctx, void *ptr);
145-
} PyMemAllocator;
145+
} PyMemAllocatorEx;
146146

147147
/* Get the memory block allocator of the specified domain. */
148148
PyAPI_FUNC(void) PyMem_GetAllocator(PyMemAllocatorDomain domain,
149-
PyMemAllocator *allocator);
149+
PyMemAllocatorEx *allocator);
150150

151151
/* Set the memory block allocator of the specified domain.
152152
@@ -160,7 +160,7 @@ PyAPI_FUNC(void) PyMem_GetAllocator(PyMemAllocatorDomain domain,
160160
PyMem_SetupDebugHooks() function must be called to reinstall the debug hooks
161161
on top on the new allocator. */
162162
PyAPI_FUNC(void) PyMem_SetAllocator(PyMemAllocatorDomain domain,
163-
PyMemAllocator *allocator);
163+
PyMemAllocatorEx *allocator);
164164

165165
/* Setup hooks to detect bugs in the following Python memory allocator
166166
functions:

Modules/_testcapimodule.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2756,7 +2756,7 @@ test_pymem_alloc0(PyObject *self)
27562756
}
27572757

27582758
typedef struct {
2759-
PyMemAllocator alloc;
2759+
PyMemAllocatorEx alloc;
27602760

27612761
size_t malloc_size;
27622762
size_t calloc_nelem;
@@ -2802,7 +2802,7 @@ test_setallocators(PyMemAllocatorDomain domain)
28022802
PyObject *res = NULL;
28032803
const char *error_msg;
28042804
alloc_hook_t hook;
2805-
PyMemAllocator alloc;
2805+
PyMemAllocatorEx alloc;
28062806
size_t size, size2, nelem, elsize;
28072807
void *ptr, *ptr2;
28082808

Modules/_tracemalloc.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ static void raw_free(void *ptr);
1818

1919
/* Protected by the GIL */
2020
static struct {
21-
PyMemAllocator mem;
22-
PyMemAllocator raw;
23-
PyMemAllocator obj;
21+
PyMemAllocatorEx mem;
22+
PyMemAllocatorEx raw;
23+
PyMemAllocatorEx obj;
2424
} allocators;
2525

2626
static struct {
@@ -475,7 +475,7 @@ tracemalloc_remove_trace(void *ptr)
475475
static void*
476476
tracemalloc_alloc(int use_calloc, void *ctx, size_t nelem, size_t elsize)
477477
{
478-
PyMemAllocator *alloc = (PyMemAllocator *)ctx;
478+
PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx;
479479
void *ptr;
480480

481481
assert(elsize == 0 || nelem <= PY_SIZE_MAX / elsize);
@@ -501,7 +501,7 @@ tracemalloc_alloc(int use_calloc, void *ctx, size_t nelem, size_t elsize)
501501
static void*
502502
tracemalloc_realloc(void *ctx, void *ptr, size_t new_size)
503503
{
504-
PyMemAllocator *alloc = (PyMemAllocator *)ctx;
504+
PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx;
505505
void *ptr2;
506506

507507
ptr2 = alloc->realloc(alloc->ctx, ptr, new_size);
@@ -546,7 +546,7 @@ tracemalloc_realloc(void *ctx, void *ptr, size_t new_size)
546546
static void
547547
tracemalloc_free(void *ctx, void *ptr)
548548
{
549-
PyMemAllocator *alloc = (PyMemAllocator *)ctx;
549+
PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx;
550550

551551
if (ptr == NULL)
552552
return;
@@ -567,7 +567,7 @@ tracemalloc_alloc_gil(int use_calloc, void *ctx, size_t nelem, size_t elsize)
567567
void *ptr;
568568

569569
if (get_reentrant()) {
570-
PyMemAllocator *alloc = (PyMemAllocator *)ctx;
570+
PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx;
571571
if (use_calloc)
572572
return alloc->calloc(alloc->ctx, nelem, elsize);
573573
else
@@ -607,7 +607,7 @@ tracemalloc_realloc_gil(void *ctx, void *ptr, size_t new_size)
607607
Example: PyMem_RawRealloc() is called internally by pymalloc
608608
(_PyObject_Malloc() and _PyObject_Realloc()) to allocate a new
609609
arena (new_arena()). */
610-
PyMemAllocator *alloc = (PyMemAllocator *)ctx;
610+
PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx;
611611

612612
ptr2 = alloc->realloc(alloc->ctx, ptr, new_size);
613613
if (ptr2 != NULL && ptr != NULL) {
@@ -639,7 +639,7 @@ tracemalloc_raw_alloc(int use_calloc, void *ctx, size_t nelem, size_t elsize)
639639
void *ptr;
640640

641641
if (get_reentrant()) {
642-
PyMemAllocator *alloc = (PyMemAllocator *)ctx;
642+
PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx;
643643
if (use_calloc)
644644
return alloc->calloc(alloc->ctx, nelem, elsize);
645645
else
@@ -685,7 +685,7 @@ tracemalloc_raw_realloc(void *ctx, void *ptr, size_t new_size)
685685

686686
if (get_reentrant()) {
687687
/* Reentrant call to PyMem_RawRealloc(). */
688-
PyMemAllocator *alloc = (PyMemAllocator *)ctx;
688+
PyMemAllocatorEx *alloc = (PyMemAllocatorEx *)ctx;
689689

690690
ptr2 = alloc->realloc(alloc->ctx, ptr, new_size);
691691

@@ -863,7 +863,7 @@ tracemalloc_deinit(void)
863863
static int
864864
tracemalloc_start(int max_nframe)
865865
{
866-
PyMemAllocator alloc;
866+
PyMemAllocatorEx alloc;
867867
size_t size;
868868

869869
if (tracemalloc_init() < 0)

Objects/obmalloc.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ _PyObject_ArenaFree(void *ctx, void *ptr, size_t size)
151151
typedef struct {
152152
/* We tag each block with an API ID in order to tag API violations */
153153
char api_id;
154-
PyMemAllocator alloc;
154+
PyMemAllocatorEx alloc;
155155
} debug_alloc_api_t;
156156
static struct {
157157
debug_alloc_api_t raw;
@@ -166,23 +166,23 @@ static struct {
166166
#define PYDBG_FUNCS _PyMem_DebugMalloc, _PyMem_DebugCalloc, _PyMem_DebugRealloc, _PyMem_DebugFree
167167
#endif
168168

169-
static PyMemAllocator _PyMem_Raw = {
169+
static PyMemAllocatorEx _PyMem_Raw = {
170170
#ifdef PYMALLOC_DEBUG
171171
&_PyMem_Debug.raw, PYDBG_FUNCS
172172
#else
173173
NULL, PYRAW_FUNCS
174174
#endif
175175
};
176176

177-
static PyMemAllocator _PyMem = {
177+
static PyMemAllocatorEx _PyMem = {
178178
#ifdef PYMALLOC_DEBUG
179179
&_PyMem_Debug.mem, PYDBG_FUNCS
180180
#else
181181
NULL, PYMEM_FUNCS
182182
#endif
183183
};
184184

185-
static PyMemAllocator _PyObject = {
185+
static PyMemAllocatorEx _PyObject = {
186186
#ifdef PYMALLOC_DEBUG
187187
&_PyMem_Debug.obj, PYDBG_FUNCS
188188
#else
@@ -209,7 +209,7 @@ void
209209
PyMem_SetupDebugHooks(void)
210210
{
211211
#ifdef PYMALLOC_DEBUG
212-
PyMemAllocator alloc;
212+
PyMemAllocatorEx alloc;
213213

214214
alloc.malloc = _PyMem_DebugMalloc;
215215
alloc.calloc = _PyMem_DebugCalloc;
@@ -237,7 +237,7 @@ PyMem_SetupDebugHooks(void)
237237
}
238238

239239
void
240-
PyMem_GetAllocator(PyMemAllocatorDomain domain, PyMemAllocator *allocator)
240+
PyMem_GetAllocator(PyMemAllocatorDomain domain, PyMemAllocatorEx *allocator)
241241
{
242242
switch(domain)
243243
{
@@ -255,7 +255,7 @@ PyMem_GetAllocator(PyMemAllocatorDomain domain, PyMemAllocator *allocator)
255255
}
256256

257257
void
258-
PyMem_SetAllocator(PyMemAllocatorDomain domain, PyMemAllocator *allocator)
258+
PyMem_SetAllocator(PyMemAllocatorDomain domain, PyMemAllocatorEx *allocator)
259259
{
260260
switch(domain)
261261
{

0 commit comments

Comments
 (0)