1 #ifndef Py_CPYTHON_OBJIMPL_H 2 # error "this header file must not be included directly" 3 #endif 4 5 #define _PyObject_SIZE(typeobj) ( (typeobj)->tp_basicsize ) 6 7 /* _PyObject_VAR_SIZE returns the number of bytes (as size_t) allocated for a 8 vrbl-size object with nitems items, exclusive of gc overhead (if any). The 9 value is rounded up to the closest multiple of sizeof(void *), in order to 10 ensure that pointer fields at the end of the object are correctly aligned 11 for the platform (this is of special importance for subclasses of, e.g., 12 str or int, so that pointers can be stored after the embedded data). 13 14 Note that there's no memory wastage in doing this, as malloc has to 15 return (at worst) pointer-aligned memory anyway. 16 */ 17 #if ((SIZEOF_VOID_P - 1) & SIZEOF_VOID_P) != 0 18 # error "_PyObject_VAR_SIZE requires SIZEOF_VOID_P be a power of 2" 19 #endif 20 21 #define _PyObject_VAR_SIZE(typeobj, nitems) \ 22 _Py_SIZE_ROUND_UP((typeobj)->tp_basicsize + \ 23 (nitems)*(typeobj)->tp_itemsize, \ 24 SIZEOF_VOID_P) 25 26 27 /* This example code implements an object constructor with a custom 28 allocator, where PyObject_New is inlined, and shows the important 29 distinction between two steps (at least): 30 1) the actual allocation of the object storage; 31 2) the initialization of the Python specific fields 32 in this storage with PyObject_{Init, InitVar}. 33 34 PyObject * 35 YourObject_New(...) 36 { 37 PyObject *op; 38 39 op = (PyObject *) Your_Allocator(_PyObject_SIZE(YourTypeStruct)); 40 if (op == NULL) { 41 return PyErr_NoMemory(); 42 } 43 44 PyObject_Init(op, &YourTypeStruct); 45 46 op->ob_field = value; 47 ... 48 return op; 49 } 50 51 Note that in C++, the use of the new operator usually implies that 52 the 1st step is performed automatically for you, so in a C++ class 53 constructor you would start directly with PyObject_Init/InitVar. */ 54 55 /* This function returns the number of allocated memory blocks, regardless of size */ 56 PyAPI_FUNC(Py_ssize_t) _Py_GetAllocatedBlocks(void); 57 58 /* Macros */ 59 #ifdef WITH_PYMALLOC 60 PyAPI_FUNC(int) _PyObject_DebugMallocStats(FILE *out); 61 #endif 62 63 64 typedef struct { 65 /* user context passed as the first argument to the 2 functions */ 66 void *ctx; 67 68 /* allocate an arena of size bytes */ 69 void* (*alloc) (void *ctx, size_t size); 70 71 /* free an arena */ 72 void (*free) (void *ctx, void *ptr, size_t size); 73 } PyObjectArenaAllocator; 74 75 /* Get the arena allocator. */ 76 PyAPI_FUNC(void) PyObject_GetArenaAllocator(PyObjectArenaAllocator *allocator); 77 78 /* Set the arena allocator. */ 79 PyAPI_FUNC(void) PyObject_SetArenaAllocator(PyObjectArenaAllocator *allocator); 80 81 82 /* Test if an object implements the garbage collector protocol */ 83 PyAPI_FUNC(int) PyObject_IS_GC(PyObject *obj); 84 85 86 /* Code built with Py_BUILD_CORE must include pycore_gc.h instead which 87 defines a different _PyGC_FINALIZED() macro. */ 88 #ifndef Py_BUILD_CORE 89 // Kept for backward compatibility with Python 3.8 90 # define _PyGC_FINALIZED(o) PyObject_GC_IsFinalized(o) 91 #endif 92 93 PyAPI_FUNC(PyObject *) _PyObject_GC_Malloc(size_t size); 94 PyAPI_FUNC(PyObject *) _PyObject_GC_Calloc(size_t size); 95 96 97 /* Test if a type supports weak references */ 98 #define PyType_SUPPORTS_WEAKREFS(t) ((t)->tp_weaklistoffset > 0) 99 100 PyAPI_FUNC(PyObject **) PyObject_GET_WEAKREFS_LISTPTR(PyObject *op); 101