blob: 003dfdfa5ec3ef08491c0df9b01fd9775c757784 [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 Rossumd266eb41996-10-25 14:44:06 +000013Permission to use, copy, modify, and distribute this software and its
14documentation for any purpose and without fee is hereby granted,
Guido van Rossumf70e43a1991-02-19 12:39:46 +000015provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000016both that copyright notice and this permission notice appear in
Guido van Rossumf70e43a1991-02-19 12:39:46 +000017supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000018Centrum or CWI or Corporation for National Research Initiatives or
19CNRI not be used in advertising or publicity pertaining to
20distribution of the software without specific, written prior
21permission.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000022
Guido van Rossumd266eb41996-10-25 14:44:06 +000023While CWI is the initial source for this software, a modified version
24is made available by the Corporation for National Research Initiatives
25(CNRI) at the Internet address ftp://ftp.python.org.
26
27STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
28REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
29MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
30CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
31DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
32PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
33TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
34PERFORMANCE OF THIS SOFTWARE.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000035
36******************************************************************/
37
Guido van Rossumb18618d2000-05-03 23:44:39 +000038#include "mymalloc.h"
39
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000040/*
Guido van Rossumb18618d2000-05-03 23:44:39 +000041Functions and macros for modules that implement new object types.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000042You must first include "object.h".
43
Guido van Rossumb18618d2000-05-03 23:44:39 +000044 - PyObject_New(type, typeobj) allocates memory for a new object of
45 the given type; here 'type' must be the C structure type used to
46 represent the object and 'typeobj' the address of the corresponding
47 type object. Reference count and type pointer are filled in; the
48 rest of the bytes of the object are *undefined*! The resulting
49 expression type is 'type *'. The size of the object is actually
50 determined by the tp_basicsize field of the type object.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000051
Guido van Rossumb18618d2000-05-03 23:44:39 +000052 - PyObject_NewVar(type, typeobj, n) is similar but allocates a
53 variable-size object with n extra items. The size is computed as
54 tp_basicsize plus n * tp_itemsize. This fills in the ob_size field
55 as well.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000056
Guido van Rossumb18618d2000-05-03 23:44:39 +000057 - PyObject_Del(op) releases the memory allocated for an object.
58
59 - PyObject_Init(op, typeobj) and PyObject_InitVar(op, typeobj, n) are
60 similar to PyObject_{New, NewVar} except that they don't allocate
61 the memory needed for an object. Instead of the 'type' parameter,
62 they accept the pointer of a new object (allocated by an arbitrary
63 allocator) and initialize its object header fields.
64
65Note that objects created with PyObject_{New, NewVar} are allocated
66within the Python heap by an object allocator, the latter being
67implemented (by default) on top of the Python raw memory
68allocator. This ensures that Python keeps control on the user's
69objects regarding their memory management; for instance, they may be
70subject to automatic garbage collection.
71
72In case a specific form of memory management is needed, implying that
73the objects would not reside in the Python heap (for example standard
74malloc heap(s) are mandatory, use of shared memory, C++ local storage
75or operator new), you must first allocate the object with your custom
76allocator, then pass its pointer to PyObject_{Init, InitVar} for
77filling in its Python-specific fields: reference count, type pointer,
78possibly others. You should be aware that Python has very limited
79control over these objects because they don't cooperate with the
80Python memory manager. Such objects may not be eligible for automatic
81garbage collection and you have to make sure that they are released
82accordingly whenever their destructor gets called (cf. the specific
83form of memory management you're using).
84
85Unless you have specific memory management requirements, it is
86recommended to use PyObject_{New, NewVar, Del}. */
87
88/*
89 * Core object memory allocator
90 * ============================
91 */
92
93/* The purpose of the object allocator is to make make the distinction
94 between "object memory" and the rest within the Python heap.
95
96 Object memory is the one allocated by PyObject_{New, NewVar}, i.e.
97 the one that holds the object's representation defined by its C
98 type structure, *excluding* any object-specific memory buffers that
99 might be referenced by the structure (for type structures that have
100 pointer fields). By default, the object memory allocator is
101 implemented on top of the raw memory allocator.
102
103 The PyCore_* macros can be defined to make the interpreter use a
104 custom object memory allocator. They are reserved for internal
105 memory management purposes exclusively. Both the core and extension
106 modules should use the PyObject_* API. */
107
108#ifndef PyCore_OBJECT_MALLOC_FUNC
109#undef PyCore_OBJECT_REALLOC_FUNC
110#undef PyCore_OBJECT_FREE_FUNC
111#define PyCore_OBJECT_MALLOC_FUNC PyCore_MALLOC_FUNC
112#define PyCore_OBJECT_REALLOC_FUNC PyCore_REALLOC_FUNC
113#define PyCore_OBJECT_FREE_FUNC PyCore_FREE_FUNC
114#endif
115
116#ifndef PyCore_OBJECT_MALLOC_PROTO
117#undef PyCore_OBJECT_REALLOC_PROTO
118#undef PyCore_OBJECT_FREE_PROTO
119#define PyCore_OBJECT_MALLOC_PROTO PyCore_MALLOC_PROTO
120#define PyCore_OBJECT_REALLOC_PROTO PyCore_REALLOC_PROTO
121#define PyCore_OBJECT_FREE_PROTO PyCore_FREE_PROTO
122#endif
123
124#ifdef NEED_TO_DECLARE_OBJECT_MALLOC_AND_FRIEND
125extern ANY *PyCore_OBJECT_MALLOC_FUNC PyCore_OBJECT_MALLOC_PROTO;
126extern ANY *PyCore_OBJECT_REALLOC_FUNC PyCore_OBJECT_REALLOC_PROTO;
127extern void PyCore_OBJECT_FREE_FUNC PyCore_OBJECT_FREE_PROTO;
128#endif
129
130#ifndef PyCore_OBJECT_MALLOC
131#undef PyCore_OBJECT_REALLOC
132#undef PyCore_OBJECT_FREE
133#define PyCore_OBJECT_MALLOC(n) PyCore_OBJECT_MALLOC_FUNC(n)
134#define PyCore_OBJECT_REALLOC(p, n) PyCore_OBJECT_REALLOC_FUNC((p), (n))
135#define PyCore_OBJECT_FREE(p) PyCore_OBJECT_FREE_FUNC(p)
136#endif
137
138/*
139 * Raw object memory interface
140 * ===========================
141 */
142
143/* The use of this API should be avoided, unless a builtin object
144 constructor inlines PyObject_{New, NewVar}, either because the
145 latter functions cannot allocate the exact amount of needed memory,
146 either for speed. This situation is exceptional, but occurs for
147 some object constructors (PyBuffer_New, PyList_New...). Inlining
148 PyObject_{New, NewVar} for objects that are supposed to belong to
149 the Python heap is discouraged. If you really have to, make sure
150 the object is initialized with PyObject_{Init, InitVar}. Do *not*
151 inline PyObject_{Init, InitVar} for user-extension types or you
152 might seriously interfere with Python's memory management. */
153
154/* Functions */
155
156/* Wrappers around PyCore_OBJECT_MALLOC and friends; useful if you
157 need to be sure that you are using the same object memory allocator
158 as Python. These wrappers *do not* make sure that allocating 0
159 bytes returns a non-NULL pointer. Returned pointers must be checked
160 for NULL explicitly; no action is performed on failure. */
161extern DL_IMPORT(ANY *) PyObject_Malloc Py_PROTO((size_t));
162extern DL_IMPORT(ANY *) PyObject_Realloc Py_PROTO((ANY *, size_t));
163extern DL_IMPORT(void) PyObject_Free Py_PROTO((ANY *));
164
165/* Macros */
166#define PyObject_MALLOC(n) PyCore_OBJECT_MALLOC(n)
167#define PyObject_REALLOC(op, n) PyCore_OBJECT_REALLOC((ANY *)(op), (n))
168#define PyObject_FREE(op) PyCore_OBJECT_FREE((ANY *)(op))
169
170/*
171 * Generic object allocator interface
172 * ==================================
173 */
174
175/* Functions */
176extern DL_IMPORT(PyObject *) PyObject_Init Py_PROTO((PyObject *, PyTypeObject *));
177extern DL_IMPORT(PyVarObject *) PyObject_InitVar Py_PROTO((PyVarObject *, PyTypeObject *, int));
Guido van Rossum43466ec1998-12-04 18:48:25 +0000178extern DL_IMPORT(PyObject *) _PyObject_New Py_PROTO((PyTypeObject *));
179extern DL_IMPORT(PyVarObject *) _PyObject_NewVar Py_PROTO((PyTypeObject *, int));
Guido van Rossumb18618d2000-05-03 23:44:39 +0000180extern DL_IMPORT(void) _PyObject_Del Py_PROTO((PyObject *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000181
Guido van Rossumb18618d2000-05-03 23:44:39 +0000182#define PyObject_New(type, typeobj) \
183 ( (type *) _PyObject_New(typeobj) )
184#define PyObject_NewVar(type, typeobj, n) \
185 ( (type *) _PyObject_NewVar((typeobj), (n)) )
186#define PyObject_Del(op) _PyObject_Del((PyObject *)(op))
Guido van Rossuma3309961993-07-28 09:05:47 +0000187
Guido van Rossumb18618d2000-05-03 23:44:39 +0000188/* Macros trading binary compatibility for speed. See also mymalloc.h.
189 Note that these macros expect non-NULL object pointers.*/
190#define PyObject_INIT(op, typeobj) \
191 ( (op)->ob_type = (typeobj), _Py_NewReference((PyObject *)(op)), (op) )
192#define PyObject_INIT_VAR(op, typeobj, size) \
193 ( (op)->ob_size = (size), PyObject_INIT((op), (typeobj)) )
Guido van Rossum5a849141996-07-21 02:23:54 +0000194
Guido van Rossumb18618d2000-05-03 23:44:39 +0000195#define _PyObject_SIZE(typeobj) ( (typeobj)->tp_basicsize )
196#define _PyObject_VAR_SIZE(typeobj, n) \
197 ( (typeobj)->tp_basicsize + (n) * (typeobj)->tp_itemsize )
Guido van Rossum5a849141996-07-21 02:23:54 +0000198
Guido van Rossumb18618d2000-05-03 23:44:39 +0000199#define PyObject_NEW(type, typeobj) \
200( (type *) PyObject_Init( \
201 (PyObject *) PyObject_MALLOC( _PyObject_SIZE(typeobj) ), (typeobj)) )
202#define PyObject_NEW_VAR(type, typeobj, n) \
203( (type *) PyObject_InitVar( \
204 (PyVarObject *) PyObject_MALLOC( _PyObject_VAR_SIZE((typeobj),(n)) ),\
205 (typeobj), (n)) )
Guido van Rossumb18618d2000-05-03 23:44:39 +0000206
207/* This example code implements an object constructor with a custom
208 allocator, where PyObject_New is inlined, and shows the important
209 distinction between two steps (at least):
210 1) the actual allocation of the object storage;
211 2) the initialization of the Python specific fields
212 in this storage with PyObject_{Init, InitVar}.
213
214 PyObject *
215 YourObject_New(...)
216 {
217 PyObject *op;
218
219 op = (PyObject *) Your_Allocator(_PyObject_SIZE(YourTypeStruct));
220 if (op == NULL)
221 return PyErr_NoMemory();
222
223 op = PyObject_Init(op, &YourTypeStruct);
224 if (op == NULL)
225 return NULL;
226
227 op->ob_field = value;
228 ...
229 return op;
230 }
231
232 Note that in C++, the use of the new operator usually implies that
233 the 1st step is performed automatically for you, so in a C++ class
234 constructor you would start directly with PyObject_Init/InitVar. */
Guido van Rossum5a849141996-07-21 02:23:54 +0000235
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000236/*
237 * Garbage Collection Support
238 * ==========================
239 */
Jeremy Hyltond08b4c42000-06-23 19:37:02 +0000240
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000241/* To make a new object participate in garbage collection use
242 PyObject_{New, VarNew, Del} to manage the memory. Set the type flag
243 Py_TPFLAGS_GC and define the type method tp_recurse. You should also
244 add the method tp_clear if your object is mutable. Include
245 PyGC_INFO_SIZE in the calculation of tp_basicsize. Call
246 PyObject_GC_Init after the pointers followed by tp_recurse become
247 valid (usually just before returning the object from the allocation
248 method. Call PyObject_GC_Fini before those pointers become invalid
249 (usually at the top of the deallocation method). */
Jeremy Hyltond08b4c42000-06-23 19:37:02 +0000250
251#ifndef WITH_CYCLE_GC
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000252
253#define PyGC_HEAD_SIZE 0
254#define PyObject_GC_Init(op)
255#define PyObject_GC_Fini(op)
256#define PyObject_AS_GC(op) (op)
257#define PyObject_FROM_GC(op) (op)
258#define PyObject_DEL(op) PyObject_FREE(op)
259
260#else
261
262/* Add the object into the container set */
263extern DL_IMPORT(void) _PyGC_Insert Py_PROTO((PyObject *));
264
265/* Remove the object from the container set */
266extern DL_IMPORT(void) _PyGC_Remove Py_PROTO((PyObject *));
267
268#define PyObject_GC_Init(op) _PyGC_Insert((PyObject *)op)
269#define PyObject_GC_Fini(op) _PyGC_Remove((PyObject *)op)
270
271/* Structure *prefixed* to container objects participating in GC */
272typedef struct _gc_head {
273 struct _gc_head *gc_next;
274 struct _gc_head *gc_prev;
275 int gc_refs;
276} PyGC_Head;
277
278#define PyGC_HEAD_SIZE sizeof(PyGC_Head)
279
280/* Test if a type has a GC head */
281#define PyType_IS_GC(t) PyType_HasFeature((t), Py_TPFLAGS_GC)
282
283/* Test if an object has a GC head */
284#define PyObject_IS_GC(o) PyType_IS_GC((o)->ob_type)
285
286/* Get an object's GC head */
287#define PyObject_AS_GC(o) ((PyGC_Head *)(o)-1)
288
289/* Get the object given the PyGC_Head */
290#define PyObject_FROM_GC(g) ((PyObject *)(((PyGC_Head *)g)+1))
291
292#define PyObject_DEL(op) PyObject_FREE( PyObject_IS_GC(op) ? \
293 (ANY *)PyObject_AS_GC(op) : \
294 (ANY *)(op) )
295
296#endif /* WITH_CYCLE_GC */
Jeremy Hyltond08b4c42000-06-23 19:37:02 +0000297
Guido van Rossuma3309961993-07-28 09:05:47 +0000298#ifdef __cplusplus
299}
300#endif
301#endif /* !Py_OBJIMPL_H */