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

Skip to content

Commit 6cf185d

Browse files
committed
Issue #18874: _PyObject_Malloc/Realloc/Free() now falls back on
_PyMem_RawMalloc/Realloc/Free, instead of _PyMem_Malloc/Realloc/Free. So it becomes possible to use the fast pymalloc allocator for the PYMEM_DOMAIN_MEM domain (PyMem_Malloc/Realloc/Free functions).
1 parent 7c74de4 commit 6cf185d

2 files changed

Lines changed: 21 additions & 19 deletions

File tree

Doc/c-api/memory.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,8 @@ Customize PyObject Arena Allocator
288288
Python has a *pymalloc* allocator for allocations smaller than 512 bytes. This
289289
allocator is optimized for small objects with a short lifetime. It uses memory
290290
mappings called "arenas" with a fixed size of 256 KB. It falls back to
291-
:c:func:`PyMem_Malloc` and :c:func:`PyMem_Realloc` for allocations larger than
292-
512 bytes. *pymalloc* is the default allocator used by
291+
:c:func:`PyMem_RawMalloc` and :c:func:`PyMem_RawRealloc` for allocations larger
292+
than 512 bytes. *pymalloc* is the default allocator used by
293293
:c:func:`PyObject_Malloc`.
294294
295295
The default arena allocator uses the following functions:

Objects/obmalloc.c

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,11 @@ _PyObject_ArenaFree(void *ctx, void *ptr, size_t size)
125125

126126
#define PYRAW_FUNCS _PyMem_RawMalloc, _PyMem_RawRealloc, _PyMem_RawFree
127127
#ifdef WITH_PYMALLOC
128-
#define PYOBJECT_FUNCS _PyObject_Malloc, _PyObject_Realloc, _PyObject_Free
128+
# define PYOBJ_FUNCS _PyObject_Malloc, _PyObject_Realloc, _PyObject_Free
129129
#else
130-
#define PYOBJECT_FUNCS PYRAW_FUNCS
130+
# define PYOBJ_FUNCS PYRAW_FUNCS
131131
#endif
132+
#define PYMEM_FUNCS PYRAW_FUNCS
132133

133134
#ifdef PYMALLOC_DEBUG
134135
typedef struct {
@@ -142,40 +143,41 @@ static struct {
142143
debug_alloc_api_t obj;
143144
} _PyMem_Debug = {
144145
{'r', {NULL, PYRAW_FUNCS}},
145-
{'m', {NULL, PYRAW_FUNCS}},
146-
{'o', {NULL, PYOBJECT_FUNCS}}
146+
{'m', {NULL, PYMEM_FUNCS}},
147+
{'o', {NULL, PYOBJ_FUNCS}}
147148
};
148149

149-
#define PYDEBUG_FUNCS _PyMem_DebugMalloc, _PyMem_DebugRealloc, _PyMem_DebugFree
150+
#define PYDBG_FUNCS _PyMem_DebugMalloc, _PyMem_DebugRealloc, _PyMem_DebugFree
150151
#endif
151152

152153
static PyMemAllocator _PyMem_Raw = {
153154
#ifdef PYMALLOC_DEBUG
154-
&_PyMem_Debug.raw, PYDEBUG_FUNCS
155+
&_PyMem_Debug.raw, PYDBG_FUNCS
155156
#else
156157
NULL, PYRAW_FUNCS
157158
#endif
158159
};
159160

160161
static PyMemAllocator _PyMem = {
161162
#ifdef PYMALLOC_DEBUG
162-
&_PyMem_Debug.mem, PYDEBUG_FUNCS
163+
&_PyMem_Debug.mem, PYDBG_FUNCS
163164
#else
164-
NULL, PYRAW_FUNCS
165+
NULL, PYMEM_FUNCS
165166
#endif
166167
};
167168

168169
static PyMemAllocator _PyObject = {
169170
#ifdef PYMALLOC_DEBUG
170-
&_PyMem_Debug.obj, PYDEBUG_FUNCS
171+
&_PyMem_Debug.obj, PYDBG_FUNCS
171172
#else
172-
NULL, PYOBJECT_FUNCS
173+
NULL, PYOBJ_FUNCS
173174
#endif
174175
};
175176

176177
#undef PYRAW_FUNCS
177-
#undef PYOBJECT_FUNCS
178-
#undef PYDEBUG_FUNCS
178+
#undef PYMEM_FUNCS
179+
#undef PYOBJ_FUNCS
180+
#undef PYDBG_FUNCS
179181

180182
static PyObjectArenaAllocator _PyObject_Arena = {NULL,
181183
#ifdef MS_WINDOWS
@@ -924,7 +926,7 @@ new_arena(void)
924926
return NULL; /* overflow */
925927
#endif
926928
nbytes = numarenas * sizeof(*arenas);
927-
arenaobj = (struct arena_object *)PyMem_Realloc(arenas, nbytes);
929+
arenaobj = (struct arena_object *)PyMem_RawRealloc(arenas, nbytes);
928930
if (arenaobj == NULL)
929931
return NULL;
930932
arenas = arenaobj;
@@ -1309,7 +1311,7 @@ _PyObject_Malloc(void *ctx, size_t nbytes)
13091311
* has been reached.
13101312
*/
13111313
{
1312-
void *result = PyMem_Malloc(nbytes);
1314+
void *result = PyMem_RawMalloc(nbytes);
13131315
if (!result)
13141316
_Py_AllocatedBlocks--;
13151317
return result;
@@ -1539,7 +1541,7 @@ _PyObject_Free(void *ctx, void *p)
15391541
redirect:
15401542
#endif
15411543
/* We didn't allocate this address. */
1542-
PyMem_Free(p);
1544+
PyMem_RawFree(p);
15431545
}
15441546

15451547
/* realloc. If p is NULL, this acts like malloc(nbytes). Else if nbytes==0,
@@ -1608,14 +1610,14 @@ _PyObject_Realloc(void *ctx, void *p, size_t nbytes)
16081610
* at p. Instead we punt: let C continue to manage this block.
16091611
*/
16101612
if (nbytes)
1611-
return PyMem_Realloc(p, nbytes);
1613+
return PyMem_RawRealloc(p, nbytes);
16121614
/* C doesn't define the result of realloc(p, 0) (it may or may not
16131615
* return NULL then), but Python's docs promise that nbytes==0 never
16141616
* returns NULL. We don't pass 0 to realloc(), to avoid that endcase
16151617
* to begin with. Even then, we can't be sure that realloc() won't
16161618
* return NULL.
16171619
*/
1618-
bp = PyMem_Realloc(p, 1);
1620+
bp = PyMem_RawRealloc(p, 1);
16191621
return bp ? bp : p;
16201622
}
16211623

0 commit comments

Comments
 (0)