blob: f44963c7b3368ae622081984dc88b803dbdbf7aa [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00002Copyright (c) 2000, BeOpen.com.
3Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
5All rights reserved.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00006
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00007See the file "Misc/COPYRIGHT" for information on usage and
8redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009******************************************************************/
10
Fred Drake3cf4d2b2000-07-09 00:55:06 +000011#ifndef Py_OBJIMPL_H
12#define Py_OBJIMPL_H
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000013
14#include "pymem.h"
15
Fred Drake3cf4d2b2000-07-09 00:55:06 +000016#ifdef __cplusplus
17extern "C" {
18#endif
19
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000020/*
Guido van Rossumb18618d2000-05-03 23:44:39 +000021Functions and macros for modules that implement new object types.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000022You must first include "object.h".
23
Guido van Rossumb18618d2000-05-03 23:44:39 +000024 - PyObject_New(type, typeobj) allocates memory for a new object of
25 the given type; here 'type' must be the C structure type used to
26 represent the object and 'typeobj' the address of the corresponding
27 type object. Reference count and type pointer are filled in; the
28 rest of the bytes of the object are *undefined*! The resulting
29 expression type is 'type *'. The size of the object is actually
30 determined by the tp_basicsize field of the type object.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000031
Guido van Rossumb18618d2000-05-03 23:44:39 +000032 - PyObject_NewVar(type, typeobj, n) is similar but allocates a
33 variable-size object with n extra items. The size is computed as
34 tp_basicsize plus n * tp_itemsize. This fills in the ob_size field
35 as well.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000036
Guido van Rossumb18618d2000-05-03 23:44:39 +000037 - PyObject_Del(op) releases the memory allocated for an object.
38
39 - PyObject_Init(op, typeobj) and PyObject_InitVar(op, typeobj, n) are
40 similar to PyObject_{New, NewVar} except that they don't allocate
41 the memory needed for an object. Instead of the 'type' parameter,
42 they accept the pointer of a new object (allocated by an arbitrary
43 allocator) and initialize its object header fields.
44
45Note that objects created with PyObject_{New, NewVar} are allocated
46within the Python heap by an object allocator, the latter being
47implemented (by default) on top of the Python raw memory
48allocator. This ensures that Python keeps control on the user's
49objects regarding their memory management; for instance, they may be
50subject to automatic garbage collection.
51
52In case a specific form of memory management is needed, implying that
53the objects would not reside in the Python heap (for example standard
54malloc heap(s) are mandatory, use of shared memory, C++ local storage
55or operator new), you must first allocate the object with your custom
56allocator, then pass its pointer to PyObject_{Init, InitVar} for
57filling in its Python-specific fields: reference count, type pointer,
58possibly others. You should be aware that Python has very limited
59control over these objects because they don't cooperate with the
60Python memory manager. Such objects may not be eligible for automatic
61garbage collection and you have to make sure that they are released
62accordingly whenever their destructor gets called (cf. the specific
63form of memory management you're using).
64
65Unless you have specific memory management requirements, it is
66recommended to use PyObject_{New, NewVar, Del}. */
67
68/*
69 * Core object memory allocator
70 * ============================
71 */
72
Vladimir Marangozovd8a93322000-07-10 04:30:56 +000073/* The purpose of the object allocator is to make the distinction
Guido van Rossumb18618d2000-05-03 23:44:39 +000074 between "object memory" and the rest within the Python heap.
75
76 Object memory is the one allocated by PyObject_{New, NewVar}, i.e.
77 the one that holds the object's representation defined by its C
78 type structure, *excluding* any object-specific memory buffers that
79 might be referenced by the structure (for type structures that have
80 pointer fields). By default, the object memory allocator is
81 implemented on top of the raw memory allocator.
82
83 The PyCore_* macros can be defined to make the interpreter use a
84 custom object memory allocator. They are reserved for internal
85 memory management purposes exclusively. Both the core and extension
86 modules should use the PyObject_* API. */
87
88#ifndef PyCore_OBJECT_MALLOC_FUNC
89#undef PyCore_OBJECT_REALLOC_FUNC
90#undef PyCore_OBJECT_FREE_FUNC
91#define PyCore_OBJECT_MALLOC_FUNC PyCore_MALLOC_FUNC
92#define PyCore_OBJECT_REALLOC_FUNC PyCore_REALLOC_FUNC
93#define PyCore_OBJECT_FREE_FUNC PyCore_FREE_FUNC
94#endif
95
96#ifndef PyCore_OBJECT_MALLOC_PROTO
97#undef PyCore_OBJECT_REALLOC_PROTO
98#undef PyCore_OBJECT_FREE_PROTO
99#define PyCore_OBJECT_MALLOC_PROTO PyCore_MALLOC_PROTO
100#define PyCore_OBJECT_REALLOC_PROTO PyCore_REALLOC_PROTO
101#define PyCore_OBJECT_FREE_PROTO PyCore_FREE_PROTO
102#endif
103
104#ifdef NEED_TO_DECLARE_OBJECT_MALLOC_AND_FRIEND
Thomas Wouters334fb892000-07-25 12:56:38 +0000105extern void *PyCore_OBJECT_MALLOC_FUNC PyCore_OBJECT_MALLOC_PROTO;
106extern void *PyCore_OBJECT_REALLOC_FUNC PyCore_OBJECT_REALLOC_PROTO;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000107extern void PyCore_OBJECT_FREE_FUNC PyCore_OBJECT_FREE_PROTO;
108#endif
109
110#ifndef PyCore_OBJECT_MALLOC
111#undef PyCore_OBJECT_REALLOC
112#undef PyCore_OBJECT_FREE
113#define PyCore_OBJECT_MALLOC(n) PyCore_OBJECT_MALLOC_FUNC(n)
114#define PyCore_OBJECT_REALLOC(p, n) PyCore_OBJECT_REALLOC_FUNC((p), (n))
115#define PyCore_OBJECT_FREE(p) PyCore_OBJECT_FREE_FUNC(p)
116#endif
117
118/*
119 * Raw object memory interface
120 * ===========================
121 */
122
123/* The use of this API should be avoided, unless a builtin object
124 constructor inlines PyObject_{New, NewVar}, either because the
125 latter functions cannot allocate the exact amount of needed memory,
126 either for speed. This situation is exceptional, but occurs for
127 some object constructors (PyBuffer_New, PyList_New...). Inlining
128 PyObject_{New, NewVar} for objects that are supposed to belong to
129 the Python heap is discouraged. If you really have to, make sure
130 the object is initialized with PyObject_{Init, InitVar}. Do *not*
131 inline PyObject_{Init, InitVar} for user-extension types or you
132 might seriously interfere with Python's memory management. */
133
134/* Functions */
135
136/* Wrappers around PyCore_OBJECT_MALLOC and friends; useful if you
137 need to be sure that you are using the same object memory allocator
138 as Python. These wrappers *do not* make sure that allocating 0
139 bytes returns a non-NULL pointer. Returned pointers must be checked
140 for NULL explicitly; no action is performed on failure. */
Thomas Wouters334fb892000-07-25 12:56:38 +0000141extern DL_IMPORT(void *) PyObject_Malloc(size_t);
142extern DL_IMPORT(void *) PyObject_Realloc(void *, size_t);
143extern DL_IMPORT(void) PyObject_Free(void *);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000144
145/* Macros */
146#define PyObject_MALLOC(n) PyCore_OBJECT_MALLOC(n)
Thomas Wouters334fb892000-07-25 12:56:38 +0000147#define PyObject_REALLOC(op, n) PyCore_OBJECT_REALLOC((void *)(op), (n))
148#define PyObject_FREE(op) PyCore_OBJECT_FREE((void *)(op))
Guido van Rossumb18618d2000-05-03 23:44:39 +0000149
150/*
151 * Generic object allocator interface
152 * ==================================
153 */
154
155/* Functions */
Fred Drake3cf4d2b2000-07-09 00:55:06 +0000156extern DL_IMPORT(PyObject *) PyObject_Init(PyObject *, PyTypeObject *);
157extern DL_IMPORT(PyVarObject *) PyObject_InitVar(PyVarObject *,
158 PyTypeObject *, int);
159extern DL_IMPORT(PyObject *) _PyObject_New(PyTypeObject *);
160extern DL_IMPORT(PyVarObject *) _PyObject_NewVar(PyTypeObject *, int);
161extern DL_IMPORT(void) _PyObject_Del(PyObject *);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000162
Guido van Rossumb18618d2000-05-03 23:44:39 +0000163#define PyObject_New(type, typeobj) \
164 ( (type *) _PyObject_New(typeobj) )
165#define PyObject_NewVar(type, typeobj, n) \
166 ( (type *) _PyObject_NewVar((typeobj), (n)) )
167#define PyObject_Del(op) _PyObject_Del((PyObject *)(op))
Guido van Rossuma3309961993-07-28 09:05:47 +0000168
Andrew M. Kuchling1582a3a2000-08-16 12:27:23 +0000169/* Macros trading binary compatibility for speed. See also pymem.h.
Guido van Rossumb18618d2000-05-03 23:44:39 +0000170 Note that these macros expect non-NULL object pointers.*/
171#define PyObject_INIT(op, typeobj) \
172 ( (op)->ob_type = (typeobj), _Py_NewReference((PyObject *)(op)), (op) )
173#define PyObject_INIT_VAR(op, typeobj, size) \
174 ( (op)->ob_size = (size), PyObject_INIT((op), (typeobj)) )
Guido van Rossum5a849141996-07-21 02:23:54 +0000175
Guido van Rossumb18618d2000-05-03 23:44:39 +0000176#define _PyObject_SIZE(typeobj) ( (typeobj)->tp_basicsize )
177#define _PyObject_VAR_SIZE(typeobj, n) \
178 ( (typeobj)->tp_basicsize + (n) * (typeobj)->tp_itemsize )
Guido van Rossum5a849141996-07-21 02:23:54 +0000179
Guido van Rossumb18618d2000-05-03 23:44:39 +0000180#define PyObject_NEW(type, typeobj) \
181( (type *) PyObject_Init( \
182 (PyObject *) PyObject_MALLOC( _PyObject_SIZE(typeobj) ), (typeobj)) )
183#define PyObject_NEW_VAR(type, typeobj, n) \
184( (type *) PyObject_InitVar( \
185 (PyVarObject *) PyObject_MALLOC( _PyObject_VAR_SIZE((typeobj),(n)) ),\
186 (typeobj), (n)) )
Guido van Rossumb18618d2000-05-03 23:44:39 +0000187
Guido van Rossum4cc6ac72000-07-01 01:00:38 +0000188#define PyObject_DEL(op) PyObject_FREE(op)
189
Guido van Rossumb18618d2000-05-03 23:44:39 +0000190/* This example code implements an object constructor with a custom
191 allocator, where PyObject_New is inlined, and shows the important
192 distinction between two steps (at least):
193 1) the actual allocation of the object storage;
194 2) the initialization of the Python specific fields
195 in this storage with PyObject_{Init, InitVar}.
196
197 PyObject *
198 YourObject_New(...)
199 {
200 PyObject *op;
201
202 op = (PyObject *) Your_Allocator(_PyObject_SIZE(YourTypeStruct));
203 if (op == NULL)
204 return PyErr_NoMemory();
205
206 op = PyObject_Init(op, &YourTypeStruct);
207 if (op == NULL)
208 return NULL;
209
210 op->ob_field = value;
211 ...
212 return op;
213 }
214
215 Note that in C++, the use of the new operator usually implies that
216 the 1st step is performed automatically for you, so in a C++ class
217 constructor you would start directly with PyObject_Init/InitVar. */
Guido van Rossum5a849141996-07-21 02:23:54 +0000218
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000219/*
220 * Garbage Collection Support
221 * ==========================
222 */
Jeremy Hyltond08b4c42000-06-23 19:37:02 +0000223
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000224/* To make a new object participate in garbage collection use
225 PyObject_{New, VarNew, Del} to manage the memory. Set the type flag
226 Py_TPFLAGS_GC and define the type method tp_recurse. You should also
227 add the method tp_clear if your object is mutable. Include
Guido van Rossum4cc6ac72000-07-01 01:00:38 +0000228 PyGC_HEAD_SIZE in the calculation of tp_basicsize. Call
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000229 PyObject_GC_Init after the pointers followed by tp_recurse become
230 valid (usually just before returning the object from the allocation
231 method. Call PyObject_GC_Fini before those pointers become invalid
232 (usually at the top of the deallocation method). */
Jeremy Hyltond08b4c42000-06-23 19:37:02 +0000233
234#ifndef WITH_CYCLE_GC
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000235
236#define PyGC_HEAD_SIZE 0
237#define PyObject_GC_Init(op)
238#define PyObject_GC_Fini(op)
239#define PyObject_AS_GC(op) (op)
240#define PyObject_FROM_GC(op) (op)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000241
242#else
243
244/* Add the object into the container set */
Fred Drake3cf4d2b2000-07-09 00:55:06 +0000245extern DL_IMPORT(void) _PyGC_Insert(PyObject *);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000246
247/* Remove the object from the container set */
Fred Drake3cf4d2b2000-07-09 00:55:06 +0000248extern DL_IMPORT(void) _PyGC_Remove(PyObject *);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000249
250#define PyObject_GC_Init(op) _PyGC_Insert((PyObject *)op)
251#define PyObject_GC_Fini(op) _PyGC_Remove((PyObject *)op)
252
253/* Structure *prefixed* to container objects participating in GC */
254typedef struct _gc_head {
255 struct _gc_head *gc_next;
256 struct _gc_head *gc_prev;
257 int gc_refs;
258} PyGC_Head;
259
260#define PyGC_HEAD_SIZE sizeof(PyGC_Head)
261
262/* Test if a type has a GC head */
263#define PyType_IS_GC(t) PyType_HasFeature((t), Py_TPFLAGS_GC)
264
265/* Test if an object has a GC head */
266#define PyObject_IS_GC(o) PyType_IS_GC((o)->ob_type)
267
268/* Get an object's GC head */
269#define PyObject_AS_GC(o) ((PyGC_Head *)(o)-1)
270
271/* Get the object given the PyGC_Head */
272#define PyObject_FROM_GC(g) ((PyObject *)(((PyGC_Head *)g)+1))
273
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000274#endif /* WITH_CYCLE_GC */
Jeremy Hyltond08b4c42000-06-23 19:37:02 +0000275
Guido van Rossuma3309961993-07-28 09:05:47 +0000276#ifdef __cplusplus
277}
278#endif
279#endif /* !Py_OBJIMPL_H */