Guido van Rossum | a330996 | 1993-07-28 09:05:47 +0000 | [diff] [blame] | 1 | #ifndef Py_OBJIMPL_H |
| 2 | #define Py_OBJIMPL_H |
| 3 | #ifdef __cplusplus |
| 4 | extern "C" { |
| 5 | #endif |
| 6 | |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 7 | /*********************************************************** |
Guido van Rossum | 5799b52 | 1995-01-04 19:06:22 +0000 | [diff] [blame] | 8 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 9 | The Netherlands. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 10 | |
| 11 | All Rights Reserved |
| 12 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 13 | Permission to use, copy, modify, and distribute this software and its |
| 14 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 15 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 16 | both that copyright notice and this permission notice appear in |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 17 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 18 | Centrum or CWI or Corporation for National Research Initiatives or |
| 19 | CNRI not be used in advertising or publicity pertaining to |
| 20 | distribution of the software without specific, written prior |
| 21 | permission. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 22 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 23 | While CWI is the initial source for this software, a modified version |
| 24 | is made available by the Corporation for National Research Initiatives |
| 25 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 26 | |
| 27 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 28 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 29 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 30 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 31 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 32 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 33 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 34 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 35 | |
| 36 | ******************************************************************/ |
| 37 | |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 38 | #include "mymalloc.h" |
| 39 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 40 | /* |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 41 | Functions and macros for modules that implement new object types. |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 42 | You must first include "object.h". |
| 43 | |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 44 | - 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 Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 51 | |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 52 | - 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 Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 56 | |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 57 | - 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 | |
| 65 | Note that objects created with PyObject_{New, NewVar} are allocated |
| 66 | within the Python heap by an object allocator, the latter being |
| 67 | implemented (by default) on top of the Python raw memory |
| 68 | allocator. This ensures that Python keeps control on the user's |
| 69 | objects regarding their memory management; for instance, they may be |
| 70 | subject to automatic garbage collection. |
| 71 | |
| 72 | In case a specific form of memory management is needed, implying that |
| 73 | the objects would not reside in the Python heap (for example standard |
| 74 | malloc heap(s) are mandatory, use of shared memory, C++ local storage |
| 75 | or operator new), you must first allocate the object with your custom |
| 76 | allocator, then pass its pointer to PyObject_{Init, InitVar} for |
| 77 | filling in its Python-specific fields: reference count, type pointer, |
| 78 | possibly others. You should be aware that Python has very limited |
| 79 | control over these objects because they don't cooperate with the |
| 80 | Python memory manager. Such objects may not be eligible for automatic |
| 81 | garbage collection and you have to make sure that they are released |
| 82 | accordingly whenever their destructor gets called (cf. the specific |
| 83 | form of memory management you're using). |
| 84 | |
| 85 | Unless you have specific memory management requirements, it is |
| 86 | recommended 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 |
| 125 | extern ANY *PyCore_OBJECT_MALLOC_FUNC PyCore_OBJECT_MALLOC_PROTO; |
| 126 | extern ANY *PyCore_OBJECT_REALLOC_FUNC PyCore_OBJECT_REALLOC_PROTO; |
| 127 | extern 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. */ |
| 161 | extern DL_IMPORT(ANY *) PyObject_Malloc Py_PROTO((size_t)); |
| 162 | extern DL_IMPORT(ANY *) PyObject_Realloc Py_PROTO((ANY *, size_t)); |
| 163 | extern 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 */ |
| 176 | extern DL_IMPORT(PyObject *) PyObject_Init Py_PROTO((PyObject *, PyTypeObject *)); |
| 177 | extern DL_IMPORT(PyVarObject *) PyObject_InitVar Py_PROTO((PyVarObject *, PyTypeObject *, int)); |
Guido van Rossum | 43466ec | 1998-12-04 18:48:25 +0000 | [diff] [blame] | 178 | extern DL_IMPORT(PyObject *) _PyObject_New Py_PROTO((PyTypeObject *)); |
| 179 | extern DL_IMPORT(PyVarObject *) _PyObject_NewVar Py_PROTO((PyTypeObject *, int)); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 180 | extern DL_IMPORT(void) _PyObject_Del Py_PROTO((PyObject *)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 181 | |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 182 | #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 Rossum | a330996 | 1993-07-28 09:05:47 +0000 | [diff] [blame] | 187 | |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 188 | /* 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 Rossum | 5a84914 | 1996-07-21 02:23:54 +0000 | [diff] [blame] | 194 | |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 195 | #define _PyObject_SIZE(typeobj) ( (typeobj)->tp_basicsize ) |
| 196 | #define _PyObject_VAR_SIZE(typeobj, n) \ |
| 197 | ( (typeobj)->tp_basicsize + (n) * (typeobj)->tp_itemsize ) |
Guido van Rossum | 5a84914 | 1996-07-21 02:23:54 +0000 | [diff] [blame] | 198 | |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 199 | #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 Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 206 | |
| 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 Rossum | 5a84914 | 1996-07-21 02:23:54 +0000 | [diff] [blame] | 235 | |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 236 | /* |
| 237 | * Garbage Collection Support |
| 238 | * ========================== |
| 239 | */ |
Jeremy Hylton | d08b4c4 | 2000-06-23 19:37:02 +0000 | [diff] [blame] | 240 | |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 241 | /* 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 Hylton | d08b4c4 | 2000-06-23 19:37:02 +0000 | [diff] [blame] | 250 | |
| 251 | #ifndef WITH_CYCLE_GC |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 252 | |
| 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 */ |
| 263 | extern DL_IMPORT(void) _PyGC_Insert Py_PROTO((PyObject *)); |
| 264 | |
| 265 | /* Remove the object from the container set */ |
| 266 | extern 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 */ |
| 272 | typedef 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 Hylton | d08b4c4 | 2000-06-23 19:37:02 +0000 | [diff] [blame] | 297 | |
Guido van Rossum | a330996 | 1993-07-28 09:05:47 +0000 | [diff] [blame] | 298 | #ifdef __cplusplus |
| 299 | } |
| 300 | #endif |
| 301 | #endif /* !Py_OBJIMPL_H */ |