1+ /* The PyObject_ memory family: high-level object memory interfaces.
2+ See pymem.h for the low-level PyMem_ family.
3+ */
14
25#ifndef Py_OBJIMPL_H
36#define Py_OBJIMPL_H
811extern "C" {
912#endif
1013
14+ /* BEWARE:
15+
16+ Each interface exports both functions and macros. Extension modules should
17+ use the functions, to ensure binary compatibility across Python versions.
18+ Because the Python implementation is free to change internal details, and
19+ the macros may (or may not) expose details for speed, if you do use the
20+ macros you must recompile your extensions with each Python release.
21+
22+ Never mix calls to PyObject_ memory functions with calls to the platform
23+ malloc/realloc/ calloc/free, or with calls to PyMem_.
24+ */
25+
1126/*
1227Functions and macros for modules that implement new object types.
13- You must first include "object.h".
14-
15- - PyObject_New(type, typeobj) allocates memory for a new object of
16- the given type; here 'type' must be the C structure type used to
17- represent the object and 'typeobj' the address of the corresponding
18- type object. Reference count and type pointer are filled in; the
19- rest of the bytes of the object are *undefined*! The resulting
20- expression type is 'type *'. The size of the object is determined
21- by the tp_basicsize field of the type object.
22-
23- - PyObject_NewVar(type, typeobj, n) is similar but allocates a
24- variable-size object with n extra items. The size is computed as
25- tp_basicsize plus n * tp_itemsize. This fills in the ob_size field
26- as well.
27-
28- - PyObject_Del(op) releases the memory allocated for an object. It
29- does not run a destructor -- it only frees the memory. PyObject_Free
30- is identical.
31-
32- - PyObject_Init(op, typeobj) and PyObject_InitVar(op, typeobj, n) are
33- similar to PyObject_{New, NewVar} except that they don't allocate
34- the memory needed for an object. Instead of the 'type' parameter,
35- they accept the pointer of a new object (allocated by an arbitrary
36- allocator) and initialize its object header fields.
37-
38- Note that objects created with PyObject_{New, NewVar} are allocated
39- using the specialized Python allocator (implemented in obmalloc.c), if
40- WITH_PYMALLOC is enabled. In addition, a special debugging allocator
41- is used if PYMALLOC_DEBUG is also #defined.
42-
43- In case a specific form of memory management is needed, implying that
44- the objects would not reside in the Python heap (for example standard
45- malloc heap(s) are mandatory, use of shared memory, C++ local storage
46- or operator new), you must first allocate the object with your custom
47- allocator, then pass its pointer to PyObject_{Init, InitVar} for
48- filling in its Python-specific fields: reference count, type pointer,
49- possibly others. You should be aware that Python has very limited
50- control over these objects because they don't cooperate with the
51- Python memory manager. Such objects may not be eligible for automatic
52- garbage collection and you have to make sure that they are released
53- accordingly whenever their destructor gets called (cf. the specific
28+
29+ - PyObject_New(type, typeobj) allocates memory for a new object of the given
30+ type, and initializes part of it. 'type' must be the C structure type used
31+ to represent the object, and 'typeobj' the address of the corresponding
32+ type object. Reference count and type pointer are filled in; the rest of
33+ the bytes of the object are *undefined*! The resulting expression type is
34+ 'type *'. The size of the object is determined by the tp_basicsize field
35+ of the type object.
36+
37+ - PyObject_NewVar(type, typeobj, n) is similar but allocates a variable-size
38+ object with room for n items. In addition to the refcount and type pointer
39+ fields, this also fills in the ob_size field.
40+
41+ - PyObject_Del(op) releases the memory allocated for an object. It does not
42+ run a destructor -- it only frees the memory. PyObject_Free is identical.
43+
44+ - PyObject_Init(op, typeobj) and PyObject_InitVar(op, typeobj, n) don't
45+ allocate memory. Instead of a 'type' parameter, they take a pointer to a
46+ new object (allocated by an arbitrary allocator), and initialize its object
47+ header fields.
48+
49+ Note that objects created with PyObject_{New, NewVar} are allocated using the
50+ specialized Python allocator (implemented in obmalloc.c), if WITH_PYMALLOC is
51+ enabled. In addition, a special debugging allocator is used if PYMALLOC_DEBUG
52+ is also #defined.
53+
54+ In case a specific form of memory management is needed (for example, if you
55+ must use the platform malloc heap(s), or shared memory, or C++ local storage or
56+ operator new), you must first allocate the object with your custom allocator,
57+ then pass its pointer to PyObject_{Init, InitVar} for filling in its Python-
58+ specific fields: reference count, type pointer, possibly others. You should
59+ be aware that Python no control over these objects because they don't
60+ cooperate with the Python memory manager. Such objects may not be eligible
61+ for automatic garbage collection and you have to make sure that they are
62+ released accordingly whenever their destructor gets called (cf. the specific
5463form of memory management you're using).
5564
56- Unless you have specific memory management requirements, it is
57- recommended to use PyObject_{New, NewVar, Del}. */
65+ Unless you have specific memory management requirements, use
66+ PyObject_{New, NewVar, Del}.
67+ */
5868
5969/*
6070 * Raw object memory interface
6171 * ===========================
6272 */
6373
64- /* The use of this API should be avoided, unless a builtin object
65- constructor inlines PyObject_{New, NewVar}, either because the
66- latter functions cannot allocate the exact amount of needed memory,
67- either for speed. This situation is exceptional, but occurs for
68- some object constructors (PyBuffer_New, PyList_New...). Inlining
69- PyObject_{New, NewVar} for objects that are supposed to belong to
70- the Python heap is discouraged. If you really have to, make sure
71- the object is initialized with PyObject_{Init, InitVar}. Do *not*
72- inline PyObject_{Init, InitVar} for user-extension types or you
73- might seriously interfere with Python's memory management. */
74-
75- /* Functions */
76-
7774/* Functions to call the same malloc/realloc/free as used by Python's
7875 object allocator. If WITH_PYMALLOC is enabled, these may differ from
7976 the platform malloc/realloc/free. The Python object allocator is
8077 designed for fast, cache-conscious allocation of many "small" objects,
81- with low hidden memory overhead. PyObject_Malloc(0) returns a unique
82- non-NULL pointer if possible. PyObject_Realloc(NULL, n) acts like
83- PyObject_Malloc(n). PyObject_Realloc(p != NULL, 0) does not return
84- NULL or free the memory at p. Returned pointers must be checked for
85- NULL explicitly; no action is performed on failure other than to return
86- NULL. */
78+ and with low hidden memory overhead.
79+
80+ PyObject_Malloc(0) returns a unique non-NULL pointer if possible.
81+
82+ PyObject_Realloc(NULL, n) acts like PyObject_Malloc(n).
83+ PyObject_Realloc(p != NULL, 0) does not return NULL, or free the memory
84+ at p.
85+
86+ Returned pointers must be checked for NULL explicitly; no action is
87+ performed on failure other than to return NULL (no warning it printed, no
88+ exception is set, etc).
89+
90+ For allocating objects, use PyObject_{New, NewVar} instead whenever
91+ possible. The PyObject_{Malloc, Realloc, Free} family is exposed
92+ so that you can exploit Python's small-block allocator for non-object
93+ uses. If you must use these routines to allocate object memory, make sure
94+ the object gets initialized via PyObject_{Init, InitVar} after obtaining
95+ the raw memory.
96+ */
8797extern DL_IMPORT (void * ) PyObject_Malloc (size_t );
8898extern DL_IMPORT (void * ) PyObject_Realloc (void * , size_t );
8999extern DL_IMPORT (void ) PyObject_Free (void * );
@@ -114,14 +124,19 @@ DL_IMPORT(void) _PyObject_DebugMallocStats(void);
114124#else /* ! WITH_PYMALLOC */
115125#define PyObject_MALLOC PyMem_MALLOC
116126#define PyObject_REALLOC PyMem_REALLOC
117- #define PyObject_FREE PyMem_FREE
127+ /* This is an odd one! For backward compatability with old extensions, the
128+ PyMem "release memory" functions have to invoke the object allocator's
129+ free() function. When pymalloc isn't enabled, that leaves us using
130+ the platform free(). */
131+ #define PyObject_FREE free
132+
118133#endif /* WITH_PYMALLOC */
119134
120- #define PyObject_Del PyObject_Free
121- #define PyObject_DEL PyObject_FREE
135+ #define PyObject_Del PyObject_Free
136+ #define PyObject_DEL PyObject_FREE
122137
123138/* for source compatibility with 2.2 */
124- #define _PyObject_Del PyObject_Free
139+ #define _PyObject_Del PyObject_Free
125140
126141/*
127142 * Generic object allocator interface
@@ -196,9 +211,7 @@ extern DL_IMPORT(PyVarObject *) _PyObject_NewVar(PyTypeObject *, int);
196211 if (op == NULL)
197212 return PyErr_NoMemory();
198213
199- op = PyObject_Init(op, &YourTypeStruct);
200- if (op == NULL)
201- return NULL;
214+ PyObject_Init(op, &YourTypeStruct);
202215
203216 op->ob_field = value;
204217 ...
@@ -207,7 +220,8 @@ extern DL_IMPORT(PyVarObject *) _PyObject_NewVar(PyTypeObject *, int);
207220
208221 Note that in C++, the use of the new operator usually implies that
209222 the 1st step is performed automatically for you, so in a C++ class
210- constructor you would start directly with PyObject_Init/InitVar. */
223+ constructor you would start directly with PyObject_Init/InitVar
224+ */
211225
212226/*
213227 * Garbage Collection Support
@@ -234,7 +248,7 @@ extern DL_IMPORT(PyVarObject *) _PyObject_GC_Resize(PyVarObject *, int);
234248
235249#ifdef WITH_CYCLE_GC
236250
237- /* GC information is stored BEFORE the object structure */
251+ /* GC information is stored BEFORE the object structure. */
238252typedef union _gc_head {
239253 struct {
240254 union _gc_head * gc_next ; /* not NULL if object is tracked */
@@ -283,10 +297,10 @@ extern DL_IMPORT(void) PyObject_GC_Del(void *);
283297
284298#else /* !WITH_CYCLE_GC */
285299
286- #define _PyObject_GC_Malloc PyObject_Malloc
287- #define PyObject_GC_New PyObject_New
288- #define PyObject_GC_NewVar PyObject_NewVar
289- #define PyObject_GC_Del PyObject_Del
300+ #define _PyObject_GC_Malloc PyObject_Malloc
301+ #define PyObject_GC_New PyObject_New
302+ #define PyObject_GC_NewVar PyObject_NewVar
303+ #define PyObject_GC_Del PyObject_Del
290304#define _PyObject_GC_TRACK (op )
291305#define _PyObject_GC_UNTRACK (op )
292306#define PyObject_GC_Track (op )
0 commit comments