codekingpro/portable-devtools
115k
1#ifndef Py_CPYTHON_OBJIMPL_H2# error "this header file must not be included directly"3#endif4 5static inline size_t _PyObject_SIZE(PyTypeObject *type) {6 return _Py_STATIC_CAST(size_t, type->tp_basicsize);7}8 9/* _PyObject_VAR_SIZE returns the number of bytes (as size_t) allocated for a10 vrbl-size object with nitems items, exclusive of gc overhead (if any). The11 value is rounded up to the closest multiple of sizeof(void *), in order to12 ensure that pointer fields at the end of the object are correctly aligned13 for the platform (this is of special importance for subclasses of, e.g.,14 str or int, so that pointers can be stored after the embedded data).15 16 Note that there's no memory wastage in doing this, as malloc has to17 return (at worst) pointer-aligned memory anyway.18*/19#if ((SIZEOF_VOID_P - 1) & SIZEOF_VOID_P) != 020# error "_PyObject_VAR_SIZE requires SIZEOF_VOID_P be a power of 2"21#endif22 23static inline size_t _PyObject_VAR_SIZE(PyTypeObject *type, Py_ssize_t nitems) {24 size_t size = _Py_STATIC_CAST(size_t, type->tp_basicsize);25 size += _Py_STATIC_CAST(size_t, nitems) * _Py_STATIC_CAST(size_t, type->tp_itemsize);26 return _Py_SIZE_ROUND_UP(size, SIZEOF_VOID_P);27}28 29 30/* This example code implements an object constructor with a custom31 allocator, where PyObject_New is inlined, and shows the important32 distinction between two steps (at least):33 1) the actual allocation of the object storage;34 2) the initialization of the Python specific fields35 in this storage with PyObject_{Init, InitVar}.36 37 PyObject *38 YourObject_New(...)39 {40 PyObject *op;41 42 op = (PyObject *) Your_Allocator(_PyObject_SIZE(YourTypeStruct));43 if (op == NULL) {44 return PyErr_NoMemory();45 }46 47 PyObject_Init(op, &YourTypeStruct);48 49 op->ob_field = value;50 ...51 return op;52 }53 54 Note that in C++, the use of the new operator usually implies that55 the 1st step is performed automatically for you, so in a C++ class56 constructor you would start directly with PyObject_Init/InitVar. */57 58 59typedef struct {60 /* user context passed as the first argument to the 2 functions */61 void *ctx;62 63 /* allocate an arena of size bytes */64 void* (*alloc) (void *ctx, size_t size);65 66 /* free an arena */67 void (*free) (void *ctx, void *ptr, size_t size);68} PyObjectArenaAllocator;69 70/* Get the arena allocator. */71PyAPI_FUNC(void) PyObject_GetArenaAllocator(PyObjectArenaAllocator *allocator);72 73/* Set the arena allocator. */74PyAPI_FUNC(void) PyObject_SetArenaAllocator(PyObjectArenaAllocator *allocator);75 76 77/* Test if an object implements the garbage collector protocol */78PyAPI_FUNC(int) PyObject_IS_GC(PyObject *obj);79 80 81// Test if a type supports weak references82PyAPI_FUNC(int) PyType_SUPPORTS_WEAKREFS(PyTypeObject *type);83 84PyAPI_FUNC(PyObject **) PyObject_GET_WEAKREFS_LISTPTR(PyObject *op);85 86PyAPI_FUNC(PyObject *) PyUnstable_Object_GC_NewWithExtraData(PyTypeObject *,87 size_t);88 89 90/* Visit all live GC-capable objects, similar to gc.get_objects(None). The91 * supplied callback is called on every such object with the void* arg set92 * to the supplied arg. Returning 0 from the callback ends iteration, returning93 * 1 allows iteration to continue. Returning any other value may result in94 * undefined behaviour.95 *96 * If new objects are (de)allocated by the callback it is undefined if they97 * will be visited.98 99 * Garbage collection is disabled during operation. Explicitly running a100 * collection in the callback may lead to undefined behaviour e.g. visiting the101 * same objects multiple times or not at all.102 */103typedef int (*gcvisitobjects_t)(PyObject*, void*);104PyAPI_FUNC(void) PyUnstable_GC_VisitObjects(gcvisitobjects_t callback, void* arg);105 