Skip to content

Commit ffd5399

Browse files
committed
Make PyObject_{NEW,New,Del,DEL} always use the standard malloc (PyMem_*)
and not pymalloc. Add the functions PyMalloc_New, PyMalloc_NewVar, and PyMalloc_Del that will use pymalloc if it's enabled. If pymalloc is not enabled then they use the standard malloc (PyMem_*).
1 parent 150ed61 commit ffd5399

1 file changed

Lines changed: 23 additions & 8 deletions

File tree

Include/objimpl.h

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,10 @@ You must first include "object.h".
3434
allocator) and initialize its object header fields.
3535
3636
Note that objects created with PyObject_{New, NewVar} are allocated
37-
within the Python heap by an object allocator, the latter being
38-
implemented (by default) on top of the Python raw memory
39-
allocator. This ensures that Python keeps control on the user's
40-
objects regarding their memory management; for instance, they may be
41-
subject to automatic garbage collection.
37+
within the Python heap by the raw memory allocator (usually the system
38+
malloc). If you want to use the specialized Python allocator use
39+
PyMalloc_New and PyMalloc_NewVar to allocate the objects and
40+
PyMalloc_Del to free them.
4241
4342
In case a specific form of memory management is needed, implying that
4443
the objects would not reside in the Python heap (for example standard
@@ -84,9 +83,9 @@ extern DL_IMPORT(void *) PyObject_Realloc(void *, size_t);
8483
extern DL_IMPORT(void) PyObject_Free(void *);
8584

8685
/* Macros */
87-
#define PyObject_MALLOC(n) _PyMalloc_MALLOC(n)
88-
#define PyObject_REALLOC(op, n) _PyMalloc_REALLOC((void *)(op), (n))
89-
#define PyObject_FREE(op) _PyMalloc_FREE((void *)(op))
86+
#define PyObject_MALLOC(n) PyMem_MALLOC(n)
87+
#define PyObject_REALLOC(op, n) PyMem_REALLOC((void *)(op), (n))
88+
#define PyObject_FREE(op) PyMem_FREE((void *)(op))
9089

9190
/*
9291
* Generic object allocator interface
@@ -178,6 +177,22 @@ extern DL_IMPORT(void) _PyObject_Del(PyObject *);
178177
the 1st step is performed automatically for you, so in a C++ class
179178
constructor you would start directly with PyObject_Init/InitVar. */
180179

180+
/*
181+
* The PyMalloc Object Allocator
182+
* =============================
183+
*/
184+
185+
extern DL_IMPORT(PyObject *) _PyMalloc_New(PyTypeObject *);
186+
extern DL_IMPORT(PyVarObject *) _PyMalloc_NewVar(PyTypeObject *, int);
187+
extern DL_IMPORT(void) _PyMalloc_Del(PyObject *);
188+
189+
#define PyMalloc_New(type, typeobj) \
190+
( (type *) _PyMalloc_New(typeobj) )
191+
#define PyMalloc_NewVar(type, typeobj, n) \
192+
( (type *) _PyMalloc_NewVar((typeobj), (n)) )
193+
#define PyMalloc_Del(op) _PyMalloc_Del((PyObject *)(op))
194+
195+
181196
/*
182197
* Garbage Collection Support
183198
* ==========================

0 commit comments

Comments
 (0)