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 | f58bd7c | 2020-02-05 13:12:19 +0100 | [diff] [blame] | 9 | /* Inline functions trading binary compatibility for speed: |
| 10 | PyObject_INIT() is the fast version of PyObject_Init(), and |
| 11 | PyObject_INIT_VAR() is the fast version of PyObject_InitVar(). |
| 12 | |
| 13 | These inline functions must not be called with op=NULL. */ |
| 14 | static inline PyObject* |
| 15 | _PyObject_INIT(PyObject *op, PyTypeObject *typeobj) |
| 16 | { |
| 17 | assert(op != NULL); |
Victor Stinner | d2ec81a | 2020-02-07 09:17:07 +0100 | [diff] [blame] | 18 | Py_SET_TYPE(op, typeobj); |
Victor Stinner | f58bd7c | 2020-02-05 13:12:19 +0100 | [diff] [blame] | 19 | if (PyType_GetFlags(typeobj) & Py_TPFLAGS_HEAPTYPE) { |
| 20 | Py_INCREF(typeobj); |
| 21 | } |
| 22 | _Py_NewReference(op); |
| 23 | return op; |
| 24 | } |
| 25 | |
| 26 | #define PyObject_INIT(op, typeobj) \ |
| 27 | _PyObject_INIT(_PyObject_CAST(op), (typeobj)) |
| 28 | |
| 29 | static inline PyVarObject* |
| 30 | _PyObject_INIT_VAR(PyVarObject *op, PyTypeObject *typeobj, Py_ssize_t size) |
| 31 | { |
| 32 | assert(op != NULL); |
Victor Stinner | b10dc3e | 2020-02-07 12:05:12 +0100 | [diff] [blame] | 33 | Py_SET_SIZE(op, size); |
Victor Stinner | f58bd7c | 2020-02-05 13:12:19 +0100 | [diff] [blame] | 34 | PyObject_INIT((PyObject *)op, typeobj); |
| 35 | return op; |
| 36 | } |
| 37 | |
| 38 | #define PyObject_INIT_VAR(op, typeobj, size) \ |
| 39 | _PyObject_INIT_VAR(_PyVarObject_CAST(op), (typeobj), (size)) |
| 40 | |
| 41 | |
Victor Stinner | e421106 | 2018-11-23 17:00:00 +0100 | [diff] [blame] | 42 | /* This function returns the number of allocated memory blocks, regardless of size */ |
| 43 | PyAPI_FUNC(Py_ssize_t) _Py_GetAllocatedBlocks(void); |
| 44 | |
| 45 | /* Macros */ |
| 46 | #ifdef WITH_PYMALLOC |
| 47 | PyAPI_FUNC(int) _PyObject_DebugMallocStats(FILE *out); |
| 48 | #endif |
| 49 | |
| 50 | |
| 51 | typedef struct { |
| 52 | /* user context passed as the first argument to the 2 functions */ |
| 53 | void *ctx; |
| 54 | |
| 55 | /* allocate an arena of size bytes */ |
| 56 | void* (*alloc) (void *ctx, size_t size); |
| 57 | |
| 58 | /* free an arena */ |
| 59 | void (*free) (void *ctx, void *ptr, size_t size); |
| 60 | } PyObjectArenaAllocator; |
| 61 | |
| 62 | /* Get the arena allocator. */ |
| 63 | PyAPI_FUNC(void) PyObject_GetArenaAllocator(PyObjectArenaAllocator *allocator); |
| 64 | |
| 65 | /* Set the arena allocator. */ |
| 66 | PyAPI_FUNC(void) PyObject_SetArenaAllocator(PyObjectArenaAllocator *allocator); |
| 67 | |
| 68 | |
| 69 | PyAPI_FUNC(Py_ssize_t) _PyGC_CollectNoFail(void); |
| 70 | PyAPI_FUNC(Py_ssize_t) _PyGC_CollectIfEnabled(void); |
| 71 | |
| 72 | |
| 73 | /* Test if an object has a GC head */ |
| 74 | #define PyObject_IS_GC(o) \ |
| 75 | (PyType_IS_GC(Py_TYPE(o)) \ |
| 76 | && (Py_TYPE(o)->tp_is_gc == NULL || Py_TYPE(o)->tp_is_gc(o))) |
| 77 | |
| 78 | /* GC information is stored BEFORE the object structure. */ |
| 79 | typedef struct { |
| 80 | // Pointer to next object in the list. |
| 81 | // 0 means the object is not tracked |
| 82 | uintptr_t _gc_next; |
| 83 | |
| 84 | // Pointer to previous object in the list. |
| 85 | // Lowest two bits are used for flags documented later. |
| 86 | uintptr_t _gc_prev; |
| 87 | } PyGC_Head; |
| 88 | |
| 89 | #define _Py_AS_GC(o) ((PyGC_Head *)(o)-1) |
| 90 | |
| 91 | /* True if the object is currently tracked by the GC. */ |
| 92 | #define _PyObject_GC_IS_TRACKED(o) (_Py_AS_GC(o)->_gc_next != 0) |
| 93 | |
| 94 | /* True if the object may be tracked by the GC in the future, or already is. |
| 95 | This can be useful to implement some optimizations. */ |
| 96 | #define _PyObject_GC_MAY_BE_TRACKED(obj) \ |
| 97 | (PyObject_IS_GC(obj) && \ |
| 98 | (!PyTuple_CheckExact(obj) || _PyObject_GC_IS_TRACKED(obj))) |
| 99 | |
| 100 | |
| 101 | /* Bit flags for _gc_prev */ |
| 102 | /* Bit 0 is set when tp_finalize is called */ |
| 103 | #define _PyGC_PREV_MASK_FINALIZED (1) |
| 104 | /* Bit 1 is set when the object is in generation which is GCed currently. */ |
| 105 | #define _PyGC_PREV_MASK_COLLECTING (2) |
| 106 | /* The (N-2) most significant bits contain the real address. */ |
| 107 | #define _PyGC_PREV_SHIFT (2) |
| 108 | #define _PyGC_PREV_MASK (((uintptr_t) -1) << _PyGC_PREV_SHIFT) |
| 109 | |
| 110 | // Lowest bit of _gc_next is used for flags only in GC. |
| 111 | // But it is always 0 for normal code. |
| 112 | #define _PyGCHead_NEXT(g) ((PyGC_Head*)(g)->_gc_next) |
| 113 | #define _PyGCHead_SET_NEXT(g, p) ((g)->_gc_next = (uintptr_t)(p)) |
| 114 | |
| 115 | // Lowest two bits of _gc_prev is used for _PyGC_PREV_MASK_* flags. |
| 116 | #define _PyGCHead_PREV(g) ((PyGC_Head*)((g)->_gc_prev & _PyGC_PREV_MASK)) |
| 117 | #define _PyGCHead_SET_PREV(g, p) do { \ |
| 118 | assert(((uintptr_t)p & ~_PyGC_PREV_MASK) == 0); \ |
| 119 | (g)->_gc_prev = ((g)->_gc_prev & ~_PyGC_PREV_MASK) \ |
| 120 | | ((uintptr_t)(p)); \ |
| 121 | } while (0) |
| 122 | |
| 123 | #define _PyGCHead_FINALIZED(g) \ |
| 124 | (((g)->_gc_prev & _PyGC_PREV_MASK_FINALIZED) != 0) |
| 125 | #define _PyGCHead_SET_FINALIZED(g) \ |
| 126 | ((g)->_gc_prev |= _PyGC_PREV_MASK_FINALIZED) |
| 127 | |
| 128 | #define _PyGC_FINALIZED(o) \ |
| 129 | _PyGCHead_FINALIZED(_Py_AS_GC(o)) |
| 130 | #define _PyGC_SET_FINALIZED(o) \ |
| 131 | _PyGCHead_SET_FINALIZED(_Py_AS_GC(o)) |
| 132 | |
| 133 | |
| 134 | PyAPI_FUNC(PyObject *) _PyObject_GC_Malloc(size_t size); |
| 135 | PyAPI_FUNC(PyObject *) _PyObject_GC_Calloc(size_t size); |
| 136 | |
| 137 | |
| 138 | /* Test if a type supports weak references */ |
| 139 | #define PyType_SUPPORTS_WEAKREFS(t) ((t)->tp_weaklistoffset > 0) |
| 140 | |
Victor Stinner | 38aefc5 | 2020-04-06 14:07:02 +0200 | [diff] [blame^] | 141 | PyAPI_FUNC(PyObject **) PyObject_GET_WEAKREFS_LISTPTR(PyObject *op); |
Victor Stinner | e421106 | 2018-11-23 17:00:00 +0100 | [diff] [blame] | 142 | |
| 143 | #ifdef __cplusplus |
| 144 | } |
| 145 | #endif |