blob: 71dbb937871f790531977430345740d40e13020d [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)) )
206#define PyObject_DEL(op) PyObject_FREE(op)
207
208/* This example code implements an object constructor with a custom
209 allocator, where PyObject_New is inlined, and shows the important
210 distinction between two steps (at least):
211 1) the actual allocation of the object storage;
212 2) the initialization of the Python specific fields
213 in this storage with PyObject_{Init, InitVar}.
214
215 PyObject *
216 YourObject_New(...)
217 {
218 PyObject *op;
219
220 op = (PyObject *) Your_Allocator(_PyObject_SIZE(YourTypeStruct));
221 if (op == NULL)
222 return PyErr_NoMemory();
223
224 op = PyObject_Init(op, &YourTypeStruct);
225 if (op == NULL)
226 return NULL;
227
228 op->ob_field = value;
229 ...
230 return op;
231 }
232
233 Note that in C++, the use of the new operator usually implies that
234 the 1st step is performed automatically for you, so in a C++ class
235 constructor you would start directly with PyObject_Init/InitVar. */
Guido van Rossum5a849141996-07-21 02:23:54 +0000236
Guido van Rossuma3309961993-07-28 09:05:47 +0000237#ifdef __cplusplus
238}
239#endif
240#endif /* !Py_OBJIMPL_H */