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