Victor Stinner | e421106 | 2018-11-23 17:00:00 +0100 | [diff] [blame] | 1 | #ifndef Py_CPYTHON_OBJIMPL_H |
| 2 | # error "this header file must not be included directly" |
| 3 | #endif |
| 4 | |
| 5 | #ifdef __cplusplus |
| 6 | extern "C" { |
| 7 | #endif |
| 8 | |
Victor Stinner | 9205520 | 2020-04-08 00:38:15 +0200 | [diff] [blame^] | 9 | #define _PyObject_SIZE(typeobj) ( (typeobj)->tp_basicsize ) |
| 10 | |
| 11 | /* _PyObject_VAR_SIZE returns the number of bytes (as size_t) allocated for a |
| 12 | vrbl-size object with nitems items, exclusive of gc overhead (if any). The |
| 13 | value is rounded up to the closest multiple of sizeof(void *), in order to |
| 14 | ensure that pointer fields at the end of the object are correctly aligned |
| 15 | for the platform (this is of special importance for subclasses of, e.g., |
| 16 | str or int, so that pointers can be stored after the embedded data). |
| 17 | |
| 18 | Note that there's no memory wastage in doing this, as malloc has to |
| 19 | return (at worst) pointer-aligned memory anyway. |
| 20 | */ |
| 21 | #if ((SIZEOF_VOID_P - 1) & SIZEOF_VOID_P) != 0 |
| 22 | # error "_PyObject_VAR_SIZE requires SIZEOF_VOID_P be a power of 2" |
| 23 | #endif |
| 24 | |
| 25 | #define _PyObject_VAR_SIZE(typeobj, nitems) \ |
| 26 | _Py_SIZE_ROUND_UP((typeobj)->tp_basicsize + \ |
| 27 | (nitems)*(typeobj)->tp_itemsize, \ |
| 28 | SIZEOF_VOID_P) |
| 29 | |
| 30 | |
| 31 | /* This example code implements an object constructor with a custom |
| 32 | allocator, where PyObject_New is inlined, and shows the important |
| 33 | distinction between two steps (at least): |
| 34 | 1) the actual allocation of the object storage; |
| 35 | 2) the initialization of the Python specific fields |
| 36 | in this storage with PyObject_{Init, InitVar}. |
| 37 | |
| 38 | PyObject * |
| 39 | YourObject_New(...) |
| 40 | { |
| 41 | PyObject *op; |
| 42 | |
| 43 | op = (PyObject *) Your_Allocator(_PyObject_SIZE(YourTypeStruct)); |
| 44 | if (op == NULL) |
| 45 | return PyErr_NoMemory(); |
| 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 that |
| 55 | the 1st step is performed automatically for you, so in a C++ class |
| 56 | constructor you would start directly with PyObject_Init/InitVar. */ |
| 57 | |
| 58 | |
Victor Stinner | f58bd7c | 2020-02-05 13:12:19 +0100 | [diff] [blame] | 59 | /* Inline functions trading binary compatibility for speed: |
| 60 | PyObject_INIT() is the fast version of PyObject_Init(), and |
| 61 | PyObject_INIT_VAR() is the fast version of PyObject_InitVar(). |
| 62 | |
| 63 | These inline functions must not be called with op=NULL. */ |
| 64 | static inline PyObject* |
| 65 | _PyObject_INIT(PyObject *op, PyTypeObject *typeobj) |
| 66 | { |
| 67 | assert(op != NULL); |
Victor Stinner | d2ec81a | 2020-02-07 09:17:07 +0100 | [diff] [blame] | 68 | Py_SET_TYPE(op, typeobj); |
Victor Stinner | f58bd7c | 2020-02-05 13:12:19 +0100 | [diff] [blame] | 69 | if (PyType_GetFlags(typeobj) & Py_TPFLAGS_HEAPTYPE) { |
| 70 | Py_INCREF(typeobj); |
| 71 | } |
| 72 | _Py_NewReference(op); |
| 73 | return op; |
| 74 | } |
| 75 | |
| 76 | #define PyObject_INIT(op, typeobj) \ |
| 77 | _PyObject_INIT(_PyObject_CAST(op), (typeobj)) |
| 78 | |
| 79 | static inline PyVarObject* |
| 80 | _PyObject_INIT_VAR(PyVarObject *op, PyTypeObject *typeobj, Py_ssize_t size) |
| 81 | { |
| 82 | assert(op != NULL); |
Victor Stinner | b10dc3e | 2020-02-07 12:05:12 +0100 | [diff] [blame] | 83 | Py_SET_SIZE(op, size); |
Victor Stinner | f58bd7c | 2020-02-05 13:12:19 +0100 | [diff] [blame] | 84 | PyObject_INIT((PyObject *)op, typeobj); |
| 85 | return op; |
| 86 | } |
| 87 | |
| 88 | #define PyObject_INIT_VAR(op, typeobj, size) \ |
| 89 | _PyObject_INIT_VAR(_PyVarObject_CAST(op), (typeobj), (size)) |
| 90 | |
| 91 | |
Victor Stinner | e421106 | 2018-11-23 17:00:00 +0100 | [diff] [blame] | 92 | /* This function returns the number of allocated memory blocks, regardless of size */ |
| 93 | PyAPI_FUNC(Py_ssize_t) _Py_GetAllocatedBlocks(void); |
| 94 | |
| 95 | /* Macros */ |
| 96 | #ifdef WITH_PYMALLOC |
| 97 | PyAPI_FUNC(int) _PyObject_DebugMallocStats(FILE *out); |
| 98 | #endif |
| 99 | |
| 100 | |
| 101 | typedef struct { |
| 102 | /* user context passed as the first argument to the 2 functions */ |
| 103 | void *ctx; |
| 104 | |
| 105 | /* allocate an arena of size bytes */ |
| 106 | void* (*alloc) (void *ctx, size_t size); |
| 107 | |
| 108 | /* free an arena */ |
| 109 | void (*free) (void *ctx, void *ptr, size_t size); |
| 110 | } PyObjectArenaAllocator; |
| 111 | |
| 112 | /* Get the arena allocator. */ |
| 113 | PyAPI_FUNC(void) PyObject_GetArenaAllocator(PyObjectArenaAllocator *allocator); |
| 114 | |
| 115 | /* Set the arena allocator. */ |
| 116 | PyAPI_FUNC(void) PyObject_SetArenaAllocator(PyObjectArenaAllocator *allocator); |
| 117 | |
| 118 | |
| 119 | PyAPI_FUNC(Py_ssize_t) _PyGC_CollectNoFail(void); |
| 120 | PyAPI_FUNC(Py_ssize_t) _PyGC_CollectIfEnabled(void); |
| 121 | |
| 122 | |
| 123 | /* Test if an object has a GC head */ |
| 124 | #define PyObject_IS_GC(o) \ |
| 125 | (PyType_IS_GC(Py_TYPE(o)) \ |
| 126 | && (Py_TYPE(o)->tp_is_gc == NULL || Py_TYPE(o)->tp_is_gc(o))) |
| 127 | |
| 128 | /* GC information is stored BEFORE the object structure. */ |
| 129 | typedef struct { |
| 130 | // Pointer to next object in the list. |
| 131 | // 0 means the object is not tracked |
| 132 | uintptr_t _gc_next; |
| 133 | |
| 134 | // Pointer to previous object in the list. |
| 135 | // Lowest two bits are used for flags documented later. |
| 136 | uintptr_t _gc_prev; |
| 137 | } PyGC_Head; |
| 138 | |
| 139 | #define _Py_AS_GC(o) ((PyGC_Head *)(o)-1) |
| 140 | |
| 141 | /* True if the object is currently tracked by the GC. */ |
| 142 | #define _PyObject_GC_IS_TRACKED(o) (_Py_AS_GC(o)->_gc_next != 0) |
| 143 | |
| 144 | /* True if the object may be tracked by the GC in the future, or already is. |
| 145 | This can be useful to implement some optimizations. */ |
| 146 | #define _PyObject_GC_MAY_BE_TRACKED(obj) \ |
| 147 | (PyObject_IS_GC(obj) && \ |
| 148 | (!PyTuple_CheckExact(obj) || _PyObject_GC_IS_TRACKED(obj))) |
| 149 | |
| 150 | |
| 151 | /* Bit flags for _gc_prev */ |
| 152 | /* Bit 0 is set when tp_finalize is called */ |
| 153 | #define _PyGC_PREV_MASK_FINALIZED (1) |
| 154 | /* Bit 1 is set when the object is in generation which is GCed currently. */ |
| 155 | #define _PyGC_PREV_MASK_COLLECTING (2) |
| 156 | /* The (N-2) most significant bits contain the real address. */ |
| 157 | #define _PyGC_PREV_SHIFT (2) |
| 158 | #define _PyGC_PREV_MASK (((uintptr_t) -1) << _PyGC_PREV_SHIFT) |
| 159 | |
| 160 | // Lowest bit of _gc_next is used for flags only in GC. |
| 161 | // But it is always 0 for normal code. |
| 162 | #define _PyGCHead_NEXT(g) ((PyGC_Head*)(g)->_gc_next) |
| 163 | #define _PyGCHead_SET_NEXT(g, p) ((g)->_gc_next = (uintptr_t)(p)) |
| 164 | |
| 165 | // Lowest two bits of _gc_prev is used for _PyGC_PREV_MASK_* flags. |
| 166 | #define _PyGCHead_PREV(g) ((PyGC_Head*)((g)->_gc_prev & _PyGC_PREV_MASK)) |
| 167 | #define _PyGCHead_SET_PREV(g, p) do { \ |
| 168 | assert(((uintptr_t)p & ~_PyGC_PREV_MASK) == 0); \ |
| 169 | (g)->_gc_prev = ((g)->_gc_prev & ~_PyGC_PREV_MASK) \ |
| 170 | | ((uintptr_t)(p)); \ |
| 171 | } while (0) |
| 172 | |
| 173 | #define _PyGCHead_FINALIZED(g) \ |
| 174 | (((g)->_gc_prev & _PyGC_PREV_MASK_FINALIZED) != 0) |
| 175 | #define _PyGCHead_SET_FINALIZED(g) \ |
| 176 | ((g)->_gc_prev |= _PyGC_PREV_MASK_FINALIZED) |
| 177 | |
| 178 | #define _PyGC_FINALIZED(o) \ |
| 179 | _PyGCHead_FINALIZED(_Py_AS_GC(o)) |
| 180 | #define _PyGC_SET_FINALIZED(o) \ |
| 181 | _PyGCHead_SET_FINALIZED(_Py_AS_GC(o)) |
| 182 | |
| 183 | |
| 184 | PyAPI_FUNC(PyObject *) _PyObject_GC_Malloc(size_t size); |
| 185 | PyAPI_FUNC(PyObject *) _PyObject_GC_Calloc(size_t size); |
| 186 | |
| 187 | |
| 188 | /* Test if a type supports weak references */ |
| 189 | #define PyType_SUPPORTS_WEAKREFS(t) ((t)->tp_weaklistoffset > 0) |
| 190 | |
Victor Stinner | 38aefc5 | 2020-04-06 14:07:02 +0200 | [diff] [blame] | 191 | PyAPI_FUNC(PyObject **) PyObject_GET_WEAKREFS_LISTPTR(PyObject *op); |
Victor Stinner | e421106 | 2018-11-23 17:00:00 +0100 | [diff] [blame] | 192 | |
| 193 | #ifdef __cplusplus |
| 194 | } |
| 195 | #endif |