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