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