blob: 396c23be9b50e04b8fbfb06e9c35f7ec568d8e49 [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
Tim Peters6d483d32001-10-06 21:27:34 +000059/*
Guido van Rossumb18618d2000-05-03 23:44:39 +000060 * Raw object memory interface
61 * ===========================
62 */
63
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
Neil Schemenauer25f3dc22002-03-18 21:06:21 +000077/* Wrappers that useful if you need to be sure that you are using the
78 same object memory allocator as Python. These wrappers *do not* make
79 sure that allocating 0 bytes returns a non-NULL pointer. Returned
80 pointers must be checked for NULL explicitly; no action is performed
81 on failure. */
Thomas Wouters334fb892000-07-25 12:56:38 +000082extern DL_IMPORT(void *) PyObject_Malloc(size_t);
83extern DL_IMPORT(void *) PyObject_Realloc(void *, size_t);
84extern DL_IMPORT(void) PyObject_Free(void *);
Guido van Rossumb18618d2000-05-03 23:44:39 +000085
86/* Macros */
Neil Schemenauer25f3dc22002-03-18 21:06:21 +000087#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))
Guido van Rossumb18618d2000-05-03 23:44:39 +000090
91/*
92 * Generic object allocator interface
93 * ==================================
94 */
95
96/* Functions */
Fred Drake3cf4d2b2000-07-09 00:55:06 +000097extern DL_IMPORT(PyObject *) PyObject_Init(PyObject *, PyTypeObject *);
98extern DL_IMPORT(PyVarObject *) PyObject_InitVar(PyVarObject *,
99 PyTypeObject *, int);
100extern DL_IMPORT(PyObject *) _PyObject_New(PyTypeObject *);
101extern DL_IMPORT(PyVarObject *) _PyObject_NewVar(PyTypeObject *, int);
102extern DL_IMPORT(void) _PyObject_Del(PyObject *);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000103
Guido van Rossumb18618d2000-05-03 23:44:39 +0000104#define PyObject_New(type, typeobj) \
105 ( (type *) _PyObject_New(typeobj) )
106#define PyObject_NewVar(type, typeobj, n) \
107 ( (type *) _PyObject_NewVar((typeobj), (n)) )
108#define PyObject_Del(op) _PyObject_Del((PyObject *)(op))
Guido van Rossuma3309961993-07-28 09:05:47 +0000109
Andrew M. Kuchling1582a3a2000-08-16 12:27:23 +0000110/* Macros trading binary compatibility for speed. See also pymem.h.
Guido van Rossumb18618d2000-05-03 23:44:39 +0000111 Note that these macros expect non-NULL object pointers.*/
112#define PyObject_INIT(op, typeobj) \
Fred Drake4e262a92001-03-22 18:26:47 +0000113 ( (op)->ob_type = (typeobj), _Py_NewReference((PyObject *)(op)), (op) )
Guido van Rossumb18618d2000-05-03 23:44:39 +0000114#define PyObject_INIT_VAR(op, typeobj, size) \
115 ( (op)->ob_size = (size), PyObject_INIT((op), (typeobj)) )
Guido van Rossum5a849141996-07-21 02:23:54 +0000116
Guido van Rossumb18618d2000-05-03 23:44:39 +0000117#define _PyObject_SIZE(typeobj) ( (typeobj)->tp_basicsize )
Tim Peters6d483d32001-10-06 21:27:34 +0000118
Tim Petersf2a67da2001-10-07 03:54:51 +0000119/* _PyObject_VAR_SIZE returns the number of bytes (as size_t) allocated for a
120 vrbl-size object with nitems items, exclusive of gc overhead (if any). The
121 value is rounded up to the closest multiple of sizeof(void *), in order to
122 ensure that pointer fields at the end of the object are correctly aligned
123 for the platform (this is of special importance for subclasses of, e.g.,
124 str or long, so that pointers can be stored after the embedded data).
Tim Peters6d483d32001-10-06 21:27:34 +0000125
Tim Petersf2a67da2001-10-07 03:54:51 +0000126 Note that there's no memory wastage in doing this, as malloc has to
127 return (at worst) pointer-aligned memory anyway.
Tim Peters6d483d32001-10-06 21:27:34 +0000128*/
Tim Petersf2a67da2001-10-07 03:54:51 +0000129#if ((SIZEOF_VOID_P - 1) & SIZEOF_VOID_P) != 0
130# error "_PyObject_VAR_SIZE requires SIZEOF_VOID_P be a power of 2"
131#endif
132
133#define _PyObject_VAR_SIZE(typeobj, nitems) \
134 (size_t) \
135 ( ( (typeobj)->tp_basicsize + \
136 (nitems)*(typeobj)->tp_itemsize + \
137 (SIZEOF_VOID_P - 1) \
138 ) & ~(SIZEOF_VOID_P - 1) \
139 )
Guido van Rossum5a849141996-07-21 02:23:54 +0000140
Guido van Rossumb18618d2000-05-03 23:44:39 +0000141#define PyObject_NEW(type, typeobj) \
142( (type *) PyObject_Init( \
143 (PyObject *) PyObject_MALLOC( _PyObject_SIZE(typeobj) ), (typeobj)) )
Tim Peters6d483d32001-10-06 21:27:34 +0000144
Tim Petersf2a67da2001-10-07 03:54:51 +0000145#define PyObject_NEW_VAR(type, typeobj, n) \
146( (type *) PyObject_InitVar( \
147 (PyVarObject *) PyObject_MALLOC(_PyObject_VAR_SIZE((typeobj),(n)) ),\
148 (typeobj), (n)) )
Guido van Rossumb18618d2000-05-03 23:44:39 +0000149
Guido van Rossum4cc6ac72000-07-01 01:00:38 +0000150#define PyObject_DEL(op) PyObject_FREE(op)
151
Guido van Rossumb18618d2000-05-03 23:44:39 +0000152/* This example code implements an object constructor with a custom
153 allocator, where PyObject_New is inlined, and shows the important
154 distinction between two steps (at least):
155 1) the actual allocation of the object storage;
156 2) the initialization of the Python specific fields
157 in this storage with PyObject_{Init, InitVar}.
158
159 PyObject *
160 YourObject_New(...)
161 {
162 PyObject *op;
163
164 op = (PyObject *) Your_Allocator(_PyObject_SIZE(YourTypeStruct));
165 if (op == NULL)
166 return PyErr_NoMemory();
167
168 op = PyObject_Init(op, &YourTypeStruct);
169 if (op == NULL)
170 return NULL;
171
172 op->ob_field = value;
173 ...
174 return op;
175 }
176
177 Note that in C++, the use of the new operator usually implies that
178 the 1st step is performed automatically for you, so in a C++ class
179 constructor you would start directly with PyObject_Init/InitVar. */
Guido van Rossum5a849141996-07-21 02:23:54 +0000180
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000181/*
182 * Garbage Collection Support
183 * ==========================
Guido van Rossum048eb752001-10-02 21:24:57 +0000184 *
185 * Some of the functions and macros below are always defined; when
186 * WITH_CYCLE_GC is undefined, they simply don't do anything different
187 * than their non-GC counterparts.
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000188 */
Jeremy Hyltond08b4c42000-06-23 19:37:02 +0000189
Neil Schemenauer74b5ade2001-08-29 23:49:28 +0000190/* Test if a type has a GC head */
191#define PyType_IS_GC(t) PyType_HasFeature((t), Py_TPFLAGS_HAVE_GC)
Jeremy Hyltond08b4c42000-06-23 19:37:02 +0000192
Neil Schemenauer74b5ade2001-08-29 23:49:28 +0000193/* Test if an object has a GC head */
Guido van Rossum048eb752001-10-02 21:24:57 +0000194#define PyObject_IS_GC(o) (PyType_IS_GC((o)->ob_type) && \
195 ((o)->ob_type->tp_is_gc == NULL || (o)->ob_type->tp_is_gc(o)))
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000196
Tim Peters6d483d32001-10-06 21:27:34 +0000197extern DL_IMPORT(PyObject *) _PyObject_GC_Malloc(PyTypeObject *, int);
Neil Schemenauer74b5ade2001-08-29 23:49:28 +0000198extern DL_IMPORT(PyVarObject *) _PyObject_GC_Resize(PyVarObject *, int);
199
200#define PyObject_GC_Resize(type, op, n) \
201 ( (type *) _PyObject_GC_Resize((PyVarObject *)(op), (n)) )
202
Neil Schemenauer74b5ade2001-08-29 23:49:28 +0000203extern DL_IMPORT(PyObject *) _PyObject_GC_New(PyTypeObject *);
204extern DL_IMPORT(PyVarObject *) _PyObject_GC_NewVar(PyTypeObject *, int);
205extern DL_IMPORT(void) _PyObject_GC_Del(PyObject *);
206extern DL_IMPORT(void) _PyObject_GC_Track(PyObject *);
207extern DL_IMPORT(void) _PyObject_GC_UnTrack(PyObject *);
208
Guido van Rossum048eb752001-10-02 21:24:57 +0000209#ifdef WITH_CYCLE_GC
210
Neil Schemenauer74b5ade2001-08-29 23:49:28 +0000211/* GC information is stored BEFORE the object structure */
Tim Peters9e4ca102001-10-11 18:31:31 +0000212typedef union _gc_head {
213 struct {
214 union _gc_head *gc_next; /* not NULL if object is tracked */
215 union _gc_head *gc_prev;
216 int gc_refs;
217 } gc;
Tim Peters5e67cde2002-02-28 19:38:51 +0000218 long double dummy; /* force worst-case alignment */
Neil Schemenauer74b5ade2001-08-29 23:49:28 +0000219} PyGC_Head;
220
221extern PyGC_Head _PyGC_generation0;
222
223/* Tell the GC to track this object. NB: While the object is tracked the
224 * collector it must be safe to call the ob_traverse method. */
225#define _PyObject_GC_TRACK(o) do { \
226 PyGC_Head *g = (PyGC_Head *)(o)-1; \
Tim Peters9e4ca102001-10-11 18:31:31 +0000227 if (g->gc.gc_next != NULL) \
Neil Schemenauer74b5ade2001-08-29 23:49:28 +0000228 Py_FatalError("GC object already in linked list"); \
Tim Peters9e4ca102001-10-11 18:31:31 +0000229 g->gc.gc_next = &_PyGC_generation0; \
230 g->gc.gc_prev = _PyGC_generation0.gc.gc_prev; \
231 g->gc.gc_prev->gc.gc_next = g; \
232 _PyGC_generation0.gc.gc_prev = g; \
Neil Schemenauer74b5ade2001-08-29 23:49:28 +0000233 } while (0);
234
235/* Tell the GC to stop tracking this object. */
236#define _PyObject_GC_UNTRACK(o) do { \
237 PyGC_Head *g = (PyGC_Head *)(o)-1; \
Tim Peters9e4ca102001-10-11 18:31:31 +0000238 g->gc.gc_prev->gc.gc_next = g->gc.gc_next; \
239 g->gc.gc_next->gc.gc_prev = g->gc.gc_prev; \
240 g->gc.gc_next = NULL; \
Neil Schemenauer74b5ade2001-08-29 23:49:28 +0000241 } while (0);
242
243#define PyObject_GC_Track(op) _PyObject_GC_Track((PyObject *)op)
244#define PyObject_GC_UnTrack(op) _PyObject_GC_UnTrack((PyObject *)op)
Tim Peters6d483d32001-10-06 21:27:34 +0000245
Neil Schemenauer74b5ade2001-08-29 23:49:28 +0000246
247#define PyObject_GC_New(type, typeobj) \
248 ( (type *) _PyObject_GC_New(typeobj) )
249#define PyObject_GC_NewVar(type, typeobj, n) \
250 ( (type *) _PyObject_GC_NewVar((typeobj), (n)) )
251#define PyObject_GC_Del(op) _PyObject_GC_Del((PyObject *)(op))
252
253#else /* !WITH_CYCLE_GC */
254
255#define PyObject_GC_New PyObject_New
256#define PyObject_GC_NewVar PyObject_NewVar
257#define PyObject_GC_Del PyObject_Del
Neil Schemenauer49417e72001-09-03 15:44:48 +0000258#define _PyObject_GC_TRACK(op)
259#define _PyObject_GC_UNTRACK(op)
Neil Schemenauer74b5ade2001-08-29 23:49:28 +0000260#define PyObject_GC_Track(op)
261#define PyObject_GC_UnTrack(op)
262
263#endif
264
265/* This is here for the sake of backwards compatibility. Extensions that
266 * use the old GC API will still compile but the objects will not be
267 * tracked by the GC. */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000268#define PyGC_HEAD_SIZE 0
269#define PyObject_GC_Init(op)
270#define PyObject_GC_Fini(op)
271#define PyObject_AS_GC(op) (op)
272#define PyObject_FROM_GC(op) (op)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000273
Jeremy Hyltond08b4c42000-06-23 19:37:02 +0000274
Fred Drake41deb1e2001-02-01 05:27:45 +0000275/* Test if a type supports weak references */
Fred Drake033f3122001-02-02 18:17:30 +0000276#define PyType_SUPPORTS_WEAKREFS(t) \
277 (PyType_HasFeature((t), Py_TPFLAGS_HAVE_WEAKREFS) \
278 && ((t)->tp_weaklistoffset > 0))
Fred Drake41deb1e2001-02-01 05:27:45 +0000279
280#define PyObject_GET_WEAKREFS_LISTPTR(o) \
281 ((PyObject **) (((char *) (o)) + (o)->ob_type->tp_weaklistoffset))
282
Guido van Rossuma3309961993-07-28 09:05:47 +0000283#ifdef __cplusplus
284}
285#endif
286#endif /* !Py_OBJIMPL_H */