blob: c708910bd88d86752e66b53cec7c0fd608fddd70 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Fred Drake3cf4d2b2000-07-09 00:55:06 +00002#ifndef Py_OBJIMPL_H
3#define Py_OBJIMPL_H
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +00004
5#include "pymem.h"
6
Fred Drake3cf4d2b2000-07-09 00:55:06 +00007#ifdef __cplusplus
8extern "C" {
9#endif
10
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000011/*
Guido van Rossumb18618d2000-05-03 23:44:39 +000012Functions and macros for modules that implement new object types.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000013You must first include "object.h".
14
Guido van Rossumb18618d2000-05-03 23:44:39 +000015 - 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 actually
21 determined by the tp_basicsize field of the type object.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000022
Guido van Rossumb18618d2000-05-03 23:44:39 +000023 - 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.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000027
Guido van Rossumb18618d2000-05-03 23:44:39 +000028 - PyObject_Del(op) releases the memory allocated for an object.
29
30 - PyObject_Init(op, typeobj) and PyObject_InitVar(op, typeobj, n) are
31 similar to PyObject_{New, NewVar} except that they don't allocate
32 the memory needed for an object. Instead of the 'type' parameter,
33 they accept the pointer of a new object (allocated by an arbitrary
34 allocator) and initialize its object header fields.
35
36Note that objects created with PyObject_{New, NewVar} are allocated
37within the Python heap by an object allocator, the latter being
38implemented (by default) on top of the Python raw memory
39allocator. This ensures that Python keeps control on the user's
40objects regarding their memory management; for instance, they may be
41subject to automatic garbage collection.
42
43In case a specific form of memory management is needed, implying that
44the objects would not reside in the Python heap (for example standard
45malloc heap(s) are mandatory, use of shared memory, C++ local storage
46or operator new), you must first allocate the object with your custom
47allocator, then pass its pointer to PyObject_{Init, InitVar} for
48filling in its Python-specific fields: reference count, type pointer,
49possibly others. You should be aware that Python has very limited
50control over these objects because they don't cooperate with the
51Python memory manager. Such objects may not be eligible for automatic
52garbage collection and you have to make sure that they are released
53accordingly whenever their destructor gets called (cf. the specific
54form of memory management you're using).
55
56Unless you have specific memory management requirements, it is
57recommended to use PyObject_{New, NewVar, Del}. */
58
59/*
60 * Core object memory allocator
61 * ============================
62 */
63
Vladimir Marangozovd8a93322000-07-10 04:30:56 +000064/* The purpose of the object allocator is to make the distinction
Guido van Rossumb18618d2000-05-03 23:44:39 +000065 between "object memory" and the rest within the Python heap.
66
67 Object memory is the one allocated by PyObject_{New, NewVar}, i.e.
68 the one that holds the object's representation defined by its C
69 type structure, *excluding* any object-specific memory buffers that
70 might be referenced by the structure (for type structures that have
71 pointer fields). By default, the object memory allocator is
72 implemented on top of the raw memory allocator.
73
74 The PyCore_* macros can be defined to make the interpreter use a
75 custom object memory allocator. They are reserved for internal
76 memory management purposes exclusively. Both the core and extension
77 modules should use the PyObject_* API. */
78
Neil Schemenauera35c6882001-02-27 04:45:05 +000079#ifdef WITH_PYMALLOC
80#define PyCore_OBJECT_MALLOC_FUNC _PyCore_ObjectMalloc
81#define PyCore_OBJECT_REALLOC_FUNC _PyCore_ObjectRealloc
82#define PyCore_OBJECT_FREE_FUNC _PyCore_ObjectFree
83#define NEED_TO_DECLARE_OBJECT_MALLOC_AND_FRIEND
84#endif /* !WITH_PYMALLOC */
85
Guido van Rossumb18618d2000-05-03 23:44:39 +000086#ifndef PyCore_OBJECT_MALLOC_FUNC
87#undef PyCore_OBJECT_REALLOC_FUNC
88#undef PyCore_OBJECT_FREE_FUNC
89#define PyCore_OBJECT_MALLOC_FUNC PyCore_MALLOC_FUNC
90#define PyCore_OBJECT_REALLOC_FUNC PyCore_REALLOC_FUNC
91#define PyCore_OBJECT_FREE_FUNC PyCore_FREE_FUNC
92#endif
93
94#ifndef PyCore_OBJECT_MALLOC_PROTO
95#undef PyCore_OBJECT_REALLOC_PROTO
96#undef PyCore_OBJECT_FREE_PROTO
97#define PyCore_OBJECT_MALLOC_PROTO PyCore_MALLOC_PROTO
98#define PyCore_OBJECT_REALLOC_PROTO PyCore_REALLOC_PROTO
99#define PyCore_OBJECT_FREE_PROTO PyCore_FREE_PROTO
100#endif
101
102#ifdef NEED_TO_DECLARE_OBJECT_MALLOC_AND_FRIEND
Thomas Wouters334fb892000-07-25 12:56:38 +0000103extern void *PyCore_OBJECT_MALLOC_FUNC PyCore_OBJECT_MALLOC_PROTO;
104extern void *PyCore_OBJECT_REALLOC_FUNC PyCore_OBJECT_REALLOC_PROTO;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000105extern void PyCore_OBJECT_FREE_FUNC PyCore_OBJECT_FREE_PROTO;
106#endif
107
108#ifndef PyCore_OBJECT_MALLOC
109#undef PyCore_OBJECT_REALLOC
110#undef PyCore_OBJECT_FREE
111#define PyCore_OBJECT_MALLOC(n) PyCore_OBJECT_MALLOC_FUNC(n)
112#define PyCore_OBJECT_REALLOC(p, n) PyCore_OBJECT_REALLOC_FUNC((p), (n))
113#define PyCore_OBJECT_FREE(p) PyCore_OBJECT_FREE_FUNC(p)
114#endif
115
116/*
117 * Raw object memory interface
118 * ===========================
119 */
120
121/* The use of this API should be avoided, unless a builtin object
122 constructor inlines PyObject_{New, NewVar}, either because the
123 latter functions cannot allocate the exact amount of needed memory,
124 either for speed. This situation is exceptional, but occurs for
125 some object constructors (PyBuffer_New, PyList_New...). Inlining
126 PyObject_{New, NewVar} for objects that are supposed to belong to
127 the Python heap is discouraged. If you really have to, make sure
128 the object is initialized with PyObject_{Init, InitVar}. Do *not*
129 inline PyObject_{Init, InitVar} for user-extension types or you
130 might seriously interfere with Python's memory management. */
131
132/* Functions */
133
134/* Wrappers around PyCore_OBJECT_MALLOC and friends; useful if you
135 need to be sure that you are using the same object memory allocator
136 as Python. These wrappers *do not* make sure that allocating 0
137 bytes returns a non-NULL pointer. Returned pointers must be checked
138 for NULL explicitly; no action is performed on failure. */
Thomas Wouters334fb892000-07-25 12:56:38 +0000139extern DL_IMPORT(void *) PyObject_Malloc(size_t);
140extern DL_IMPORT(void *) PyObject_Realloc(void *, size_t);
141extern DL_IMPORT(void) PyObject_Free(void *);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000142
143/* Macros */
144#define PyObject_MALLOC(n) PyCore_OBJECT_MALLOC(n)
Thomas Wouters334fb892000-07-25 12:56:38 +0000145#define PyObject_REALLOC(op, n) PyCore_OBJECT_REALLOC((void *)(op), (n))
146#define PyObject_FREE(op) PyCore_OBJECT_FREE((void *)(op))
Guido van Rossumb18618d2000-05-03 23:44:39 +0000147
148/*
149 * Generic object allocator interface
150 * ==================================
151 */
152
153/* Functions */
Fred Drake3cf4d2b2000-07-09 00:55:06 +0000154extern DL_IMPORT(PyObject *) PyObject_Init(PyObject *, PyTypeObject *);
155extern DL_IMPORT(PyVarObject *) PyObject_InitVar(PyVarObject *,
156 PyTypeObject *, int);
157extern DL_IMPORT(PyObject *) _PyObject_New(PyTypeObject *);
158extern DL_IMPORT(PyVarObject *) _PyObject_NewVar(PyTypeObject *, int);
159extern DL_IMPORT(void) _PyObject_Del(PyObject *);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000160
Guido van Rossumb18618d2000-05-03 23:44:39 +0000161#define PyObject_New(type, typeobj) \
162 ( (type *) _PyObject_New(typeobj) )
163#define PyObject_NewVar(type, typeobj, n) \
164 ( (type *) _PyObject_NewVar((typeobj), (n)) )
165#define PyObject_Del(op) _PyObject_Del((PyObject *)(op))
Guido van Rossuma3309961993-07-28 09:05:47 +0000166
Andrew M. Kuchling1582a3a2000-08-16 12:27:23 +0000167/* Macros trading binary compatibility for speed. See also pymem.h.
Guido van Rossumb18618d2000-05-03 23:44:39 +0000168 Note that these macros expect non-NULL object pointers.*/
169#define PyObject_INIT(op, typeobj) \
Fred Drake4e262a92001-03-22 18:26:47 +0000170 ( (op)->ob_type = (typeobj), _Py_NewReference((PyObject *)(op)), (op) )
Guido van Rossumb18618d2000-05-03 23:44:39 +0000171#define PyObject_INIT_VAR(op, typeobj, size) \
172 ( (op)->ob_size = (size), PyObject_INIT((op), (typeobj)) )
Guido van Rossum5a849141996-07-21 02:23:54 +0000173
Guido van Rossumb18618d2000-05-03 23:44:39 +0000174#define _PyObject_SIZE(typeobj) ( (typeobj)->tp_basicsize )
175#define _PyObject_VAR_SIZE(typeobj, n) \
176 ( (typeobj)->tp_basicsize + (n) * (typeobj)->tp_itemsize )
Guido van Rossum5a849141996-07-21 02:23:54 +0000177
Guido van Rossumb18618d2000-05-03 23:44:39 +0000178#define PyObject_NEW(type, typeobj) \
179( (type *) PyObject_Init( \
180 (PyObject *) PyObject_MALLOC( _PyObject_SIZE(typeobj) ), (typeobj)) )
181#define PyObject_NEW_VAR(type, typeobj, n) \
182( (type *) PyObject_InitVar( \
183 (PyVarObject *) PyObject_MALLOC( _PyObject_VAR_SIZE((typeobj),(n)) ),\
184 (typeobj), (n)) )
Guido van Rossumb18618d2000-05-03 23:44:39 +0000185
Guido van Rossum4cc6ac72000-07-01 01:00:38 +0000186#define PyObject_DEL(op) PyObject_FREE(op)
187
Guido van Rossumb18618d2000-05-03 23:44:39 +0000188/* This example code implements an object constructor with a custom
189 allocator, where PyObject_New is inlined, and shows the important
190 distinction between two steps (at least):
191 1) the actual allocation of the object storage;
192 2) the initialization of the Python specific fields
193 in this storage with PyObject_{Init, InitVar}.
194
195 PyObject *
196 YourObject_New(...)
197 {
198 PyObject *op;
199
200 op = (PyObject *) Your_Allocator(_PyObject_SIZE(YourTypeStruct));
201 if (op == NULL)
202 return PyErr_NoMemory();
203
204 op = PyObject_Init(op, &YourTypeStruct);
205 if (op == NULL)
206 return NULL;
207
208 op->ob_field = value;
209 ...
210 return op;
211 }
212
213 Note that in C++, the use of the new operator usually implies that
214 the 1st step is performed automatically for you, so in a C++ class
215 constructor you would start directly with PyObject_Init/InitVar. */
Guido van Rossum5a849141996-07-21 02:23:54 +0000216
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000217/*
218 * Garbage Collection Support
219 * ==========================
220 */
Jeremy Hyltond08b4c42000-06-23 19:37:02 +0000221
Neil Schemenauer74b5ade2001-08-29 23:49:28 +0000222/* Test if a type has a GC head */
223#define PyType_IS_GC(t) PyType_HasFeature((t), Py_TPFLAGS_HAVE_GC)
Jeremy Hyltond08b4c42000-06-23 19:37:02 +0000224
Neil Schemenauer74b5ade2001-08-29 23:49:28 +0000225/* Test if an object has a GC head */
226#define PyObject_IS_GC(o) PyType_IS_GC((o)->ob_type)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000227
Neil Schemenauer74b5ade2001-08-29 23:49:28 +0000228extern DL_IMPORT(PyObject *) _PyObject_GC_Malloc(PyTypeObject *, int);
229extern DL_IMPORT(PyVarObject *) _PyObject_GC_Resize(PyVarObject *, int);
230
231#define PyObject_GC_Resize(type, op, n) \
232 ( (type *) _PyObject_GC_Resize((PyVarObject *)(op), (n)) )
233
234#ifdef WITH_CYCLE_GC
235
236extern DL_IMPORT(PyObject *) _PyObject_GC_New(PyTypeObject *);
237extern DL_IMPORT(PyVarObject *) _PyObject_GC_NewVar(PyTypeObject *, int);
238extern DL_IMPORT(void) _PyObject_GC_Del(PyObject *);
239extern DL_IMPORT(void) _PyObject_GC_Track(PyObject *);
240extern DL_IMPORT(void) _PyObject_GC_UnTrack(PyObject *);
241
242/* GC information is stored BEFORE the object structure */
243typedef struct _gc_head {
244 struct _gc_head *gc_next; /* not NULL if object is tracked */
245 struct _gc_head *gc_prev;
246 int gc_refs;
247} PyGC_Head;
248
249extern PyGC_Head _PyGC_generation0;
250
251/* Tell the GC to track this object. NB: While the object is tracked the
252 * collector it must be safe to call the ob_traverse method. */
253#define _PyObject_GC_TRACK(o) do { \
254 PyGC_Head *g = (PyGC_Head *)(o)-1; \
255 if (g->gc_next != NULL) \
256 Py_FatalError("GC object already in linked list"); \
257 g->gc_next = &_PyGC_generation0; \
258 g->gc_prev = _PyGC_generation0.gc_prev; \
259 g->gc_prev->gc_next = g; \
260 _PyGC_generation0.gc_prev = g; \
261 } while (0);
262
263/* Tell the GC to stop tracking this object. */
264#define _PyObject_GC_UNTRACK(o) do { \
265 PyGC_Head *g = (PyGC_Head *)(o)-1; \
266 g->gc_prev->gc_next = g->gc_next; \
267 g->gc_next->gc_prev = g->gc_prev; \
268 g->gc_next = NULL; \
269 } while (0);
270
271#define PyObject_GC_Track(op) _PyObject_GC_Track((PyObject *)op)
272#define PyObject_GC_UnTrack(op) _PyObject_GC_UnTrack((PyObject *)op)
273
274
275#define PyObject_GC_New(type, typeobj) \
276 ( (type *) _PyObject_GC_New(typeobj) )
277#define PyObject_GC_NewVar(type, typeobj, n) \
278 ( (type *) _PyObject_GC_NewVar((typeobj), (n)) )
279#define PyObject_GC_Del(op) _PyObject_GC_Del((PyObject *)(op))
280
281#else /* !WITH_CYCLE_GC */
282
283#define PyObject_GC_New PyObject_New
284#define PyObject_GC_NewVar PyObject_NewVar
285#define PyObject_GC_Del PyObject_Del
286#define PyObject_GC_TRACK(op)
287#define PyObject_GC_UNTRACK(op)
288#define PyObject_GC_Track(op)
289#define PyObject_GC_UnTrack(op)
290
291#endif
292
293/* This is here for the sake of backwards compatibility. Extensions that
294 * use the old GC API will still compile but the objects will not be
295 * tracked by the GC. */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000296#define PyGC_HEAD_SIZE 0
297#define PyObject_GC_Init(op)
298#define PyObject_GC_Fini(op)
299#define PyObject_AS_GC(op) (op)
300#define PyObject_FROM_GC(op) (op)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000301
Jeremy Hyltond08b4c42000-06-23 19:37:02 +0000302
Fred Drake41deb1e2001-02-01 05:27:45 +0000303/* Test if a type supports weak references */
Fred Drake033f3122001-02-02 18:17:30 +0000304#define PyType_SUPPORTS_WEAKREFS(t) \
305 (PyType_HasFeature((t), Py_TPFLAGS_HAVE_WEAKREFS) \
306 && ((t)->tp_weaklistoffset > 0))
Fred Drake41deb1e2001-02-01 05:27:45 +0000307
308#define PyObject_GET_WEAKREFS_LISTPTR(o) \
309 ((PyObject **) (((char *) (o)) + (o)->ob_type->tp_weaklistoffset))
310
Guido van Rossuma3309961993-07-28 09:05:47 +0000311#ifdef __cplusplus
312}
313#endif
314#endif /* !Py_OBJIMPL_H */