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

Skip to content

Commit 3e7b893

Browse files
committed
Remove PyMalloc_* symbols. PyObject_Malloc now uses pymalloc if
it's enabled. Allow PyObject_Del, PyObject_Free, and PyObject_GC_Del to be used as function designators. Provide source compatibility macros. Make PyObject_GC_Track and PyObject_GC_UnTrack functions instead of trivial macros wrapping functions.
1 parent f6d1ea1 commit 3e7b893

1 file changed

Lines changed: 44 additions & 38 deletions

File tree

Include/objimpl.h

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,7 @@ 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 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.
37+
using the specialized Python allocator (implemented in obmalloc.c).
4138
4239
In case a specific form of memory management is needed, implying that
4340
the objects would not reside in the Python heap (for example standard
@@ -82,10 +79,40 @@ extern DL_IMPORT(void *) PyObject_Malloc(size_t);
8279
extern DL_IMPORT(void *) PyObject_Realloc(void *, size_t);
8380
extern DL_IMPORT(void) PyObject_Free(void *);
8481

82+
8583
/* Macros */
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))
84+
#ifdef WITH_PYMALLOC
85+
#ifdef PYMALLOC_DEBUG
86+
DL_IMPORT(void *) _PyObject_DebugMalloc(size_t nbytes);
87+
DL_IMPORT(void *) _PyObject_DebugRealloc(void *p, size_t nbytes);
88+
DL_IMPORT(void) _PyObject_DebugFree(void *p);
89+
DL_IMPORT(void) _PyObject_DebugDumpAddress(const void *p);
90+
DL_IMPORT(void) _PyObject_DebugCheckAddress(const void *p);
91+
DL_IMPORT(void) _PyObject_DebugDumpStats(void);
92+
#define PyObject_MALLOC _PyObject_DebugMalloc
93+
#define PyObject_Malloc _PyObject_DebugMalloc
94+
#define PyObject_REALLOC _PyObject_DebugRealloc
95+
#define PyObject_Realloc _PyObject_DebugRealloc
96+
#define PyObject_FREE _PyObject_DebugFree
97+
#define PyObject_Free _PyObject_DebugFree
98+
99+
#else /* WITH_PYMALLOC && ! PYMALLOC_DEBUG */
100+
#define PyObject_MALLOC PyObject_Malloc
101+
#define PyObject_REALLOC PyObject_Realloc
102+
#define PyObject_FREE PyObject_Free
103+
#endif
104+
105+
#else /* ! WITH_PYMALLOC */
106+
#define PyObject_MALLOC PyMem_MALLOC
107+
#define PyObject_REALLOC PyMem_REALLOC
108+
#define PyObject_FREE PyMem_FREE
109+
#endif /* WITH_PYMALLOC */
110+
111+
#define PyObject_Del PyObject_Free
112+
#define PyObject_DEL PyObject_FREE
113+
114+
/* for source compatibility with 2.2 */
115+
#define _PyObject_Del PyObject_Free
89116

90117
/*
91118
* Generic object allocator interface
@@ -98,13 +125,11 @@ extern DL_IMPORT(PyVarObject *) PyObject_InitVar(PyVarObject *,
98125
PyTypeObject *, int);
99126
extern DL_IMPORT(PyObject *) _PyObject_New(PyTypeObject *);
100127
extern DL_IMPORT(PyVarObject *) _PyObject_NewVar(PyTypeObject *, int);
101-
extern DL_IMPORT(void) _PyObject_Del(PyObject *);
102128

103129
#define PyObject_New(type, typeobj) \
104130
( (type *) _PyObject_New(typeobj) )
105131
#define PyObject_NewVar(type, typeobj, n) \
106132
( (type *) _PyObject_NewVar((typeobj), (n)) )
107-
#define PyObject_Del(op) _PyObject_Del((PyObject *)(op))
108133

109134
/* Macros trading binary compatibility for speed. See also pymem.h.
110135
Note that these macros expect non-NULL object pointers.*/
@@ -146,8 +171,6 @@ extern DL_IMPORT(void) _PyObject_Del(PyObject *);
146171
(PyVarObject *) PyObject_MALLOC(_PyObject_VAR_SIZE((typeobj),(n)) ),\
147172
(typeobj), (n)) )
148173

149-
#define PyObject_DEL(op) PyObject_FREE(op)
150-
151174
/* This example code implements an object constructor with a custom
152175
allocator, where PyObject_New is inlined, and shows the important
153176
distinction between two steps (at least):
@@ -177,22 +200,6 @@ extern DL_IMPORT(void) _PyObject_Del(PyObject *);
177200
the 1st step is performed automatically for you, so in a C++ class
178201
constructor you would start directly with PyObject_Init/InitVar. */
179202

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-
196203
/*
197204
* Garbage Collection Support
198205
* ==========================
@@ -209,17 +216,12 @@ extern DL_IMPORT(void) _PyMalloc_Del(PyObject *);
209216
#define PyObject_IS_GC(o) (PyType_IS_GC((o)->ob_type) && \
210217
((o)->ob_type->tp_is_gc == NULL || (o)->ob_type->tp_is_gc(o)))
211218

212-
extern DL_IMPORT(PyObject *) _PyObject_GC_Malloc(PyTypeObject *, int);
213219
extern DL_IMPORT(PyVarObject *) _PyObject_GC_Resize(PyVarObject *, int);
214-
215220
#define PyObject_GC_Resize(type, op, n) \
216221
( (type *) _PyObject_GC_Resize((PyVarObject *)(op), (n)) )
217222

218-
extern DL_IMPORT(PyObject *) _PyObject_GC_New(PyTypeObject *);
219-
extern DL_IMPORT(PyVarObject *) _PyObject_GC_NewVar(PyTypeObject *, int);
220-
extern DL_IMPORT(void) _PyObject_GC_Del(PyObject *);
221-
extern DL_IMPORT(void) _PyObject_GC_Track(PyObject *);
222-
extern DL_IMPORT(void) _PyObject_GC_UnTrack(PyObject *);
223+
/* for source compatibility with 2.2 */
224+
#define _PyObject_GC_Del PyObject_GC_Del
223225

224226
#ifdef WITH_CYCLE_GC
225227

@@ -257,18 +259,22 @@ extern PyGC_Head _PyGC_generation0;
257259
g->gc.gc_next = NULL; \
258260
} while (0);
259261

260-
#define PyObject_GC_Track(op) _PyObject_GC_Track((PyObject *)op)
261-
#define PyObject_GC_UnTrack(op) _PyObject_GC_UnTrack((PyObject *)op)
262-
262+
extern DL_IMPORT(PyObject *) _PyObject_GC_Malloc(size_t);
263+
extern DL_IMPORT(PyObject *) _PyObject_GC_New(PyTypeObject *);
264+
extern DL_IMPORT(PyVarObject *) _PyObject_GC_NewVar(PyTypeObject *, int);
265+
extern DL_IMPORT(void) PyObject_GC_Track(void *);
266+
extern DL_IMPORT(void) PyObject_GC_UnTrack(void *);
267+
extern DL_IMPORT(void) PyObject_GC_Del(void *);
263268

264269
#define PyObject_GC_New(type, typeobj) \
265270
( (type *) _PyObject_GC_New(typeobj) )
266271
#define PyObject_GC_NewVar(type, typeobj, n) \
267272
( (type *) _PyObject_GC_NewVar((typeobj), (n)) )
268-
#define PyObject_GC_Del(op) _PyObject_GC_Del((PyObject *)(op))
273+
269274

270275
#else /* !WITH_CYCLE_GC */
271276

277+
#define _PyObject_GC_Malloc PyObject_Malloc
272278
#define PyObject_GC_New PyObject_New
273279
#define PyObject_GC_NewVar PyObject_NewVar
274280
#define PyObject_GC_Del PyObject_Del

0 commit comments

Comments
 (0)