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 | |
| 9 | /* This function returns the number of allocated memory blocks, regardless of size */ |
| 10 | PyAPI_FUNC(Py_ssize_t) _Py_GetAllocatedBlocks(void); |
| 11 | |
| 12 | /* Macros */ |
| 13 | #ifdef WITH_PYMALLOC |
| 14 | PyAPI_FUNC(int) _PyObject_DebugMallocStats(FILE *out); |
| 15 | #endif |
| 16 | |
| 17 | |
| 18 | typedef struct { |
| 19 | /* user context passed as the first argument to the 2 functions */ |
| 20 | void *ctx; |
| 21 | |
| 22 | /* allocate an arena of size bytes */ |
| 23 | void* (*alloc) (void *ctx, size_t size); |
| 24 | |
| 25 | /* free an arena */ |
| 26 | void (*free) (void *ctx, void *ptr, size_t size); |
| 27 | } PyObjectArenaAllocator; |
| 28 | |
| 29 | /* Get the arena allocator. */ |
| 30 | PyAPI_FUNC(void) PyObject_GetArenaAllocator(PyObjectArenaAllocator *allocator); |
| 31 | |
| 32 | /* Set the arena allocator. */ |
| 33 | PyAPI_FUNC(void) PyObject_SetArenaAllocator(PyObjectArenaAllocator *allocator); |
| 34 | |
| 35 | |
| 36 | PyAPI_FUNC(Py_ssize_t) _PyGC_CollectNoFail(void); |
| 37 | PyAPI_FUNC(Py_ssize_t) _PyGC_CollectIfEnabled(void); |
| 38 | |
| 39 | |
| 40 | /* Test if an object has a GC head */ |
| 41 | #define PyObject_IS_GC(o) \ |
| 42 | (PyType_IS_GC(Py_TYPE(o)) \ |
| 43 | && (Py_TYPE(o)->tp_is_gc == NULL || Py_TYPE(o)->tp_is_gc(o))) |
| 44 | |
| 45 | /* GC information is stored BEFORE the object structure. */ |
| 46 | typedef struct { |
| 47 | // Pointer to next object in the list. |
| 48 | // 0 means the object is not tracked |
| 49 | uintptr_t _gc_next; |
| 50 | |
| 51 | // Pointer to previous object in the list. |
| 52 | // Lowest two bits are used for flags documented later. |
| 53 | uintptr_t _gc_prev; |
| 54 | } PyGC_Head; |
| 55 | |
| 56 | #define _Py_AS_GC(o) ((PyGC_Head *)(o)-1) |
| 57 | |
| 58 | /* True if the object is currently tracked by the GC. */ |
| 59 | #define _PyObject_GC_IS_TRACKED(o) (_Py_AS_GC(o)->_gc_next != 0) |
| 60 | |
| 61 | /* True if the object may be tracked by the GC in the future, or already is. |
| 62 | This can be useful to implement some optimizations. */ |
| 63 | #define _PyObject_GC_MAY_BE_TRACKED(obj) \ |
| 64 | (PyObject_IS_GC(obj) && \ |
| 65 | (!PyTuple_CheckExact(obj) || _PyObject_GC_IS_TRACKED(obj))) |
| 66 | |
| 67 | |
| 68 | /* Bit flags for _gc_prev */ |
| 69 | /* Bit 0 is set when tp_finalize is called */ |
| 70 | #define _PyGC_PREV_MASK_FINALIZED (1) |
| 71 | /* Bit 1 is set when the object is in generation which is GCed currently. */ |
| 72 | #define _PyGC_PREV_MASK_COLLECTING (2) |
| 73 | /* The (N-2) most significant bits contain the real address. */ |
| 74 | #define _PyGC_PREV_SHIFT (2) |
| 75 | #define _PyGC_PREV_MASK (((uintptr_t) -1) << _PyGC_PREV_SHIFT) |
| 76 | |
| 77 | // Lowest bit of _gc_next is used for flags only in GC. |
| 78 | // But it is always 0 for normal code. |
| 79 | #define _PyGCHead_NEXT(g) ((PyGC_Head*)(g)->_gc_next) |
| 80 | #define _PyGCHead_SET_NEXT(g, p) ((g)->_gc_next = (uintptr_t)(p)) |
| 81 | |
| 82 | // Lowest two bits of _gc_prev is used for _PyGC_PREV_MASK_* flags. |
| 83 | #define _PyGCHead_PREV(g) ((PyGC_Head*)((g)->_gc_prev & _PyGC_PREV_MASK)) |
| 84 | #define _PyGCHead_SET_PREV(g, p) do { \ |
| 85 | assert(((uintptr_t)p & ~_PyGC_PREV_MASK) == 0); \ |
| 86 | (g)->_gc_prev = ((g)->_gc_prev & ~_PyGC_PREV_MASK) \ |
| 87 | | ((uintptr_t)(p)); \ |
| 88 | } while (0) |
| 89 | |
| 90 | #define _PyGCHead_FINALIZED(g) \ |
| 91 | (((g)->_gc_prev & _PyGC_PREV_MASK_FINALIZED) != 0) |
| 92 | #define _PyGCHead_SET_FINALIZED(g) \ |
| 93 | ((g)->_gc_prev |= _PyGC_PREV_MASK_FINALIZED) |
| 94 | |
| 95 | #define _PyGC_FINALIZED(o) \ |
| 96 | _PyGCHead_FINALIZED(_Py_AS_GC(o)) |
| 97 | #define _PyGC_SET_FINALIZED(o) \ |
| 98 | _PyGCHead_SET_FINALIZED(_Py_AS_GC(o)) |
| 99 | |
| 100 | |
| 101 | PyAPI_FUNC(PyObject *) _PyObject_GC_Malloc(size_t size); |
| 102 | PyAPI_FUNC(PyObject *) _PyObject_GC_Calloc(size_t size); |
| 103 | |
| 104 | |
| 105 | /* Test if a type supports weak references */ |
| 106 | #define PyType_SUPPORTS_WEAKREFS(t) ((t)->tp_weaklistoffset > 0) |
| 107 | |
| 108 | #define PyObject_GET_WEAKREFS_LISTPTR(o) \ |
| 109 | ((PyObject **) (((char *) (o)) + Py_TYPE(o)->tp_weaklistoffset)) |
| 110 | |
| 111 | #ifdef __cplusplus |
| 112 | } |
| 113 | #endif |