1 /* The PyObject_ memory family: high-level object memory interfaces. 2 See pymem.h for the low-level PyMem_ family. 3 */ 4 5 #ifndef Py_OBJIMPL_H 6 #define Py_OBJIMPL_H 7 8 #include "pymem.h" 9 10 #ifdef __cplusplus 11 extern "C" { 12 #endif 13 14 /* BEWARE: 15 16 Each interface exports both functions and macros. Extension modules should 17 use the functions, to ensure binary compatibility across Python versions. 18 Because the Python implementation is free to change internal details, and 19 the macros may (or may not) expose details for speed, if you do use the 20 macros you must recompile your extensions with each Python release. 21 22 Never mix calls to PyObject_ memory functions with calls to the platform 23 malloc/realloc/ calloc/free, or with calls to PyMem_. 24 */ 25 26 /* 27 Functions and macros for modules that implement new object types. 28 29 - PyObject_New(type, typeobj) allocates memory for a new object of the given 30 type, and initializes part of it. 'type' must be the C structure type used 31 to represent the object, and 'typeobj' the address of the corresponding 32 type object. Reference count and type pointer are filled in; the rest of 33 the bytes of the object are *undefined*! The resulting expression type is 34 'type *'. The size of the object is determined by the tp_basicsize field 35 of the type object. 36 37 - PyObject_NewVar(type, typeobj, n) is similar but allocates a variable-size 38 object with room for n items. In addition to the refcount and type pointer 39 fields, this also fills in the ob_size field. 40 41 - PyObject_Del(op) releases the memory allocated for an object. It does not 42 run a destructor -- it only frees the memory. PyObject_Free is identical. 43 44 - PyObject_Init(op, typeobj) and PyObject_InitVar(op, typeobj, n) don't 45 allocate memory. Instead of a 'type' parameter, they take a pointer to a 46 new object (allocated by an arbitrary allocator), and initialize its object 47 header fields. 48 49 Note that objects created with PyObject_{New, NewVar} are allocated using the 50 specialized Python allocator (implemented in obmalloc.c), if WITH_PYMALLOC is 51 enabled. In addition, a special debugging allocator is used if PYMALLOC_DEBUG 52 is also #defined. 53 54 In case a specific form of memory management is needed (for example, if you 55 must use the platform malloc heap(s), or shared memory, or C++ local storage or 56 operator new), you must first allocate the object with your custom allocator, 57 then pass its pointer to PyObject_{Init, InitVar} for filling in its Python- 58 specific fields: reference count, type pointer, possibly others. You should 59 be aware that Python has no control over these objects because they don't 60 cooperate with the Python memory manager. Such objects may not be eligible 61 for automatic garbage collection and you have to make sure that they are 62 released accordingly whenever their destructor gets called (cf. the specific 63 form of memory management you're using). 64 65 Unless you have specific memory management requirements, use 66 PyObject_{New, NewVar, Del}. 67 */ 68 69 /* 70 * Raw object memory interface 71 * =========================== 72 */ 73 74 /* Functions to call the same malloc/realloc/free as used by Python's 75 object allocator. If WITH_PYMALLOC is enabled, these may differ from 76 the platform malloc/realloc/free. The Python object allocator is 77 designed for fast, cache-conscious allocation of many "small" objects, 78 and with low hidden memory overhead. 79 80 PyObject_Malloc(0) returns a unique non-NULL pointer if possible. 81 82 PyObject_Realloc(NULL, n) acts like PyObject_Malloc(n). 83 PyObject_Realloc(p != NULL, 0) does not return NULL, or free the memory 84 at p. 85 86 Returned pointers must be checked for NULL explicitly; no action is 87 performed on failure other than to return NULL (no warning it printed, no 88 exception is set, etc). 89 90 For allocating objects, use PyObject_{New, NewVar} instead whenever 91 possible. The PyObject_{Malloc, Realloc, Free} family is exposed 92 so that you can exploit Python's small-block allocator for non-object 93 uses. If you must use these routines to allocate object memory, make sure 94 the object gets initialized via PyObject_{Init, InitVar} after obtaining 95 the raw memory. 96 */ 97 PyAPI_FUNC(void *) PyObject_Malloc(size_t); 98 PyAPI_FUNC(void *) PyObject_Realloc(void *, size_t); 99 PyAPI_FUNC(void) PyObject_Free(void *); 100 101 102 /* Macros */ 103 #ifdef WITH_PYMALLOC 104 #ifdef PYMALLOC_DEBUG /* WITH_PYMALLOC && PYMALLOC_DEBUG */ 105 PyAPI_FUNC(void *) _PyObject_DebugMalloc(size_t nbytes); 106 PyAPI_FUNC(void *) _PyObject_DebugRealloc(void *p, size_t nbytes); 107 PyAPI_FUNC(void) _PyObject_DebugFree(void *p); 108 PyAPI_FUNC(void) _PyObject_DebugDumpAddress(const void *p); 109 PyAPI_FUNC(void) _PyObject_DebugCheckAddress(const void *p); 110 PyAPI_FUNC(void) _PyObject_DebugMallocStats(void); 111 PyAPI_FUNC(void *) _PyObject_DebugMallocApi(char api, size_t nbytes); 112 PyAPI_FUNC(void *) _PyObject_DebugReallocApi(char api, void *p, size_t nbytes); 113 PyAPI_FUNC(void) _PyObject_DebugFreeApi(char api, void *p); 114 PyAPI_FUNC(void) _PyObject_DebugCheckAddressApi(char api, const void *p); 115 PyAPI_FUNC(void *) _PyMem_DebugMalloc(size_t nbytes); 116 PyAPI_FUNC(void *) _PyMem_DebugRealloc(void *p, size_t nbytes); 117 PyAPI_FUNC(void) _PyMem_DebugFree(void *p); 118 #define PyObject_MALLOC _PyObject_DebugMalloc 119 #define PyObject_Malloc _PyObject_DebugMalloc 120 #define PyObject_REALLOC _PyObject_DebugRealloc 121 #define PyObject_Realloc _PyObject_DebugRealloc 122 #define PyObject_FREE _PyObject_DebugFree 123 #define PyObject_Free _PyObject_DebugFree 124 125 #else /* WITH_PYMALLOC && ! PYMALLOC_DEBUG */ 126 #define PyObject_MALLOC PyObject_Malloc 127 #define PyObject_REALLOC PyObject_Realloc 128 #define PyObject_FREE PyObject_Free 129 #endif 130 131 #else /* ! WITH_PYMALLOC */ 132 #define PyObject_MALLOC PyMem_MALLOC 133 #define PyObject_REALLOC PyMem_REALLOC 134 #define PyObject_FREE PyMem_FREE 135 136 #endif /* WITH_PYMALLOC */ 137 138 #define PyObject_Del PyObject_Free 139 #define PyObject_DEL PyObject_FREE 140 141 /* for source compatibility with 2.2 */ 142 #define _PyObject_Del PyObject_Free 143 144 /* 145 * Generic object allocator interface 146 * ================================== 147 */ 148 149 /* Functions */ 150 PyAPI_FUNC(PyObject *) PyObject_Init(PyObject *, PyTypeObject *); 151 PyAPI_FUNC(PyVarObject *) PyObject_InitVar(PyVarObject *, 152 PyTypeObject *, Py_ssize_t); 153 PyAPI_FUNC(PyObject *) _PyObject_New(PyTypeObject *); 154 PyAPI_FUNC(PyVarObject *) _PyObject_NewVar(PyTypeObject *, Py_ssize_t); 155 156 #define PyObject_New(type, typeobj) \ 157 ( (type *) _PyObject_New(typeobj) ) 158 #define PyObject_NewVar(type, typeobj, n) \ 159 ( (type *) _PyObject_NewVar((typeobj), (n)) ) 160 161 /* Macros trading binary compatibility for speed. See also pymem.h. 162 Note that these macros expect non-NULL object pointers.*/ 163 #define PyObject_INIT(op, typeobj) \ 164 ( Py_TYPE(op) = (typeobj), _Py_NewReference((PyObject *)(op)), (op) ) 165 #define PyObject_INIT_VAR(op, typeobj, size) \ 166 ( Py_SIZE(op) = (size), PyObject_INIT((op), (typeobj)) ) 167 168 #define _PyObject_SIZE(typeobj) ( (typeobj)->tp_basicsize ) 169 170 /* _PyObject_VAR_SIZE returns the number of bytes (as size_t) allocated for a 171 vrbl-size object with nitems items, exclusive of gc overhead (if any). The 172 value is rounded up to the closest multiple of sizeof(void *), in order to 173 ensure that pointer fields at the end of the object are correctly aligned 174 for the platform (this is of special importance for subclasses of, e.g., 175 str or long, so that pointers can be stored after the embedded data). 176 177 Note that there's no memory wastage in doing this, as malloc has to 178 return (at worst) pointer-aligned memory anyway. 179 */ 180 #if ((SIZEOF_VOID_P - 1) & SIZEOF_VOID_P) != 0 181 # error "_PyObject_VAR_SIZE requires SIZEOF_VOID_P be a power of 2" 182 #endif 183 184 #define _PyObject_VAR_SIZE(typeobj, nitems) \ 185 (size_t) \ 186 ( ( (typeobj)->tp_basicsize + \ 187 (nitems)*(typeobj)->tp_itemsize + \ 188 (SIZEOF_VOID_P - 1) \ 189 ) & ~(SIZEOF_VOID_P - 1) \ 190 ) 191 192 #define PyObject_NEW(type, typeobj) \ 193 ( (type *) PyObject_Init( \ 194 (PyObject *) PyObject_MALLOC( _PyObject_SIZE(typeobj) ), (typeobj)) ) 195 196 #define PyObject_NEW_VAR(type, typeobj, n) \ 197 ( (type *) PyObject_InitVar( \ 198 (PyVarObject *) PyObject_MALLOC(_PyObject_VAR_SIZE((typeobj),(n)) ),\ 199 (typeobj), (n)) ) 200 201 /* This example code implements an object constructor with a custom 202 allocator, where PyObject_New is inlined, and shows the important 203 distinction between two steps (at least): 204 1) the actual allocation of the object storage; 205 2) the initialization of the Python specific fields 206 in this storage with PyObject_{Init, InitVar}. 207 208 PyObject * 209 YourObject_New(...) 210 { 211 PyObject *op; 212 213 op = (PyObject *) Your_Allocator(_PyObject_SIZE(YourTypeStruct)); 214 if (op == NULL) 215 return PyErr_NoMemory(); 216 217 PyObject_Init(op, &YourTypeStruct); 218 219 op->ob_field = value; 220 ... 221 return op; 222 } 223 224 Note that in C++, the use of the new operator usually implies that 225 the 1st step is performed automatically for you, so in a C++ class 226 constructor you would start directly with PyObject_Init/InitVar 227 */ 228 229 /* 230 * Garbage Collection Support 231 * ========================== 232 */ 233 234 /* C equivalent of gc.collect(). */ 235 PyAPI_FUNC(Py_ssize_t) PyGC_Collect(void); 236 237 /* Test if a type has a GC head */ 238 #define PyType_IS_GC(t) PyType_HasFeature((t), Py_TPFLAGS_HAVE_GC) 239 240 /* Test if an object has a GC head */ 241 #define PyObject_IS_GC(o) (PyType_IS_GC(Py_TYPE(o)) && \ 242 (Py_TYPE(o)->tp_is_gc == NULL || Py_TYPE(o)->tp_is_gc(o))) 243 244 PyAPI_FUNC(PyVarObject *) _PyObject_GC_Resize(PyVarObject *, Py_ssize_t); 245 #define PyObject_GC_Resize(type, op, n) \ 246 ( (type *) _PyObject_GC_Resize((PyVarObject *)(op), (n)) ) 247 248 /* for source compatibility with 2.2 */ 249 #define _PyObject_GC_Del PyObject_GC_Del 250 251 /* 252 * Former over-aligned definition of PyGC_Head, used to compute the size of the 253 * padding for the new version below. 254 */ 255 union _gc_head; 256 union _gc_head_old { 257 struct { 258 union _gc_head_old *gc_next; 259 union _gc_head_old *gc_prev; 260 Py_ssize_t gc_refs; 261 } gc; 262 long double dummy; 263 }; 264 265 /* GC information is stored BEFORE the object structure. */ 266 typedef union _gc_head { 267 struct { 268 union _gc_head *gc_next; 269 union _gc_head *gc_prev; 270 Py_ssize_t gc_refs; 271 } gc; 272 double dummy; /* Force at least 8-byte alignment. */ 273 char dummy_padding[sizeof(union _gc_head_old)]; 274 } PyGC_Head; 275 276 extern PyGC_Head *_PyGC_generation0; 277 278 #define _Py_AS_GC(o) ((PyGC_Head *)(o)-1) 279 280 #define _PyGC_REFS_UNTRACKED (-2) 281 #define _PyGC_REFS_REACHABLE (-3) 282 #define _PyGC_REFS_TENTATIVELY_UNREACHABLE (-4) 283 284 /* Tell the GC to track this object. NB: While the object is tracked the 285 * collector it must be safe to call the ob_traverse method. */ 286 #define _PyObject_GC_TRACK(o) do { \ 287 PyGC_Head *g = _Py_AS_GC(o); \ 288 if (g->gc.gc_refs != _PyGC_REFS_UNTRACKED) \ 289 Py_FatalError("GC object already tracked"); \ 290 g->gc.gc_refs = _PyGC_REFS_REACHABLE; \ 291 g->gc.gc_next = _PyGC_generation0; \ 292 g->gc.gc_prev = _PyGC_generation0->gc.gc_prev; \ 293 g->gc.gc_prev->gc.gc_next = g; \ 294 _PyGC_generation0->gc.gc_prev = g; \ 295 } while (0); 296 297 /* Tell the GC to stop tracking this object. 298 * gc_next doesn't need to be set to NULL, but doing so is a good 299 * way to provoke memory errors if calling code is confused. 300 */ 301 #define _PyObject_GC_UNTRACK(o) do { \ 302 PyGC_Head *g = _Py_AS_GC(o); \ 303 assert(g->gc.gc_refs != _PyGC_REFS_UNTRACKED); \ 304 g->gc.gc_refs = _PyGC_REFS_UNTRACKED; \ 305 g->gc.gc_prev->gc.gc_next = g->gc.gc_next; \ 306 g->gc.gc_next->gc.gc_prev = g->gc.gc_prev; \ 307 g->gc.gc_next = NULL; \ 308 } while (0); 309 310 /* True if the object is currently tracked by the GC. */ 311 #define _PyObject_GC_IS_TRACKED(o) \ 312 ((_Py_AS_GC(o))->gc.gc_refs != _PyGC_REFS_UNTRACKED) 313 314 /* True if the object may be tracked by the GC in the future, or already is. 315 This can be useful to implement some optimizations. */ 316 #define _PyObject_GC_MAY_BE_TRACKED(obj) \ 317 (PyObject_IS_GC(obj) && \ 318 (!PyTuple_CheckExact(obj) || _PyObject_GC_IS_TRACKED(obj))) 319 320 321 PyAPI_FUNC(PyObject *) _PyObject_GC_Malloc(size_t); 322 PyAPI_FUNC(PyObject *) _PyObject_GC_New(PyTypeObject *); 323 PyAPI_FUNC(PyVarObject *) _PyObject_GC_NewVar(PyTypeObject *, Py_ssize_t); 324 PyAPI_FUNC(void) PyObject_GC_Track(void *); 325 PyAPI_FUNC(void) PyObject_GC_UnTrack(void *); 326 PyAPI_FUNC(void) PyObject_GC_Del(void *); 327 328 #define PyObject_GC_New(type, typeobj) \ 329 ( (type *) _PyObject_GC_New(typeobj) ) 330 #define PyObject_GC_NewVar(type, typeobj, n) \ 331 ( (type *) _PyObject_GC_NewVar((typeobj), (n)) ) 332 333 334 /* Utility macro to help write tp_traverse functions. 335 * To use this macro, the tp_traverse function must name its arguments 336 * "visit" and "arg". This is intended to keep tp_traverse functions 337 * looking as much alike as possible. 338 */ 339 #define Py_VISIT(op) \ 340 do { \ 341 if (op) { \ 342 int vret = visit((PyObject *)(op), arg); \ 343 if (vret) \ 344 return vret; \ 345 } \ 346 } while (0) 347 348 /* This is here for the sake of backwards compatibility. Extensions that 349 * use the old GC API will still compile but the objects will not be 350 * tracked by the GC. */ 351 #define PyGC_HEAD_SIZE 0 352 #define PyObject_GC_Init(op) 353 #define PyObject_GC_Fini(op) 354 #define PyObject_AS_GC(op) (op) 355 #define PyObject_FROM_GC(op) (op) 356 357 358 /* Test if a type supports weak references */ 359 #define PyType_SUPPORTS_WEAKREFS(t) \ 360 (PyType_HasFeature((t), Py_TPFLAGS_HAVE_WEAKREFS) \ 361 && ((t)->tp_weaklistoffset > 0)) 362 363 #define PyObject_GET_WEAKREFS_LISTPTR(o) \ 364 ((PyObject **) (((char *) (o)) + Py_TYPE(o)->tp_weaklistoffset)) 365 366 #ifdef __cplusplus 367 } 368 #endif 369 #endif /* !Py_OBJIMPL_H */ 370