jpayne@69: #ifndef Py_CPYTHON_OBJIMPL_H jpayne@69: # error "this header file must not be included directly" jpayne@69: #endif jpayne@69: jpayne@69: #ifdef __cplusplus jpayne@69: extern "C" { jpayne@69: #endif jpayne@69: jpayne@69: /* This function returns the number of allocated memory blocks, regardless of size */ jpayne@69: PyAPI_FUNC(Py_ssize_t) _Py_GetAllocatedBlocks(void); jpayne@69: jpayne@69: /* Macros */ jpayne@69: #ifdef WITH_PYMALLOC jpayne@69: PyAPI_FUNC(int) _PyObject_DebugMallocStats(FILE *out); jpayne@69: #endif jpayne@69: jpayne@69: jpayne@69: typedef struct { jpayne@69: /* user context passed as the first argument to the 2 functions */ jpayne@69: void *ctx; jpayne@69: jpayne@69: /* allocate an arena of size bytes */ jpayne@69: void* (*alloc) (void *ctx, size_t size); jpayne@69: jpayne@69: /* free an arena */ jpayne@69: void (*free) (void *ctx, void *ptr, size_t size); jpayne@69: } PyObjectArenaAllocator; jpayne@69: jpayne@69: /* Get the arena allocator. */ jpayne@69: PyAPI_FUNC(void) PyObject_GetArenaAllocator(PyObjectArenaAllocator *allocator); jpayne@69: jpayne@69: /* Set the arena allocator. */ jpayne@69: PyAPI_FUNC(void) PyObject_SetArenaAllocator(PyObjectArenaAllocator *allocator); jpayne@69: jpayne@69: jpayne@69: PyAPI_FUNC(Py_ssize_t) _PyGC_CollectNoFail(void); jpayne@69: PyAPI_FUNC(Py_ssize_t) _PyGC_CollectIfEnabled(void); jpayne@69: jpayne@69: jpayne@69: /* Test if an object has a GC head */ jpayne@69: #define PyObject_IS_GC(o) \ jpayne@69: (PyType_IS_GC(Py_TYPE(o)) \ jpayne@69: && (Py_TYPE(o)->tp_is_gc == NULL || Py_TYPE(o)->tp_is_gc(o))) jpayne@69: jpayne@69: /* GC information is stored BEFORE the object structure. */ jpayne@69: typedef struct { jpayne@69: // Pointer to next object in the list. jpayne@69: // 0 means the object is not tracked jpayne@69: uintptr_t _gc_next; jpayne@69: jpayne@69: // Pointer to previous object in the list. jpayne@69: // Lowest two bits are used for flags documented later. jpayne@69: uintptr_t _gc_prev; jpayne@69: } PyGC_Head; jpayne@69: jpayne@69: #define _Py_AS_GC(o) ((PyGC_Head *)(o)-1) jpayne@69: jpayne@69: /* True if the object is currently tracked by the GC. */ jpayne@69: #define _PyObject_GC_IS_TRACKED(o) (_Py_AS_GC(o)->_gc_next != 0) jpayne@69: jpayne@69: /* True if the object may be tracked by the GC in the future, or already is. jpayne@69: This can be useful to implement some optimizations. */ jpayne@69: #define _PyObject_GC_MAY_BE_TRACKED(obj) \ jpayne@69: (PyObject_IS_GC(obj) && \ jpayne@69: (!PyTuple_CheckExact(obj) || _PyObject_GC_IS_TRACKED(obj))) jpayne@69: jpayne@69: jpayne@69: /* Bit flags for _gc_prev */ jpayne@69: /* Bit 0 is set when tp_finalize is called */ jpayne@69: #define _PyGC_PREV_MASK_FINALIZED (1) jpayne@69: /* Bit 1 is set when the object is in generation which is GCed currently. */ jpayne@69: #define _PyGC_PREV_MASK_COLLECTING (2) jpayne@69: /* The (N-2) most significant bits contain the real address. */ jpayne@69: #define _PyGC_PREV_SHIFT (2) jpayne@69: #define _PyGC_PREV_MASK (((uintptr_t) -1) << _PyGC_PREV_SHIFT) jpayne@69: jpayne@69: // Lowest bit of _gc_next is used for flags only in GC. jpayne@69: // But it is always 0 for normal code. jpayne@69: #define _PyGCHead_NEXT(g) ((PyGC_Head*)(g)->_gc_next) jpayne@69: #define _PyGCHead_SET_NEXT(g, p) ((g)->_gc_next = (uintptr_t)(p)) jpayne@69: jpayne@69: // Lowest two bits of _gc_prev is used for _PyGC_PREV_MASK_* flags. jpayne@69: #define _PyGCHead_PREV(g) ((PyGC_Head*)((g)->_gc_prev & _PyGC_PREV_MASK)) jpayne@69: #define _PyGCHead_SET_PREV(g, p) do { \ jpayne@69: assert(((uintptr_t)p & ~_PyGC_PREV_MASK) == 0); \ jpayne@69: (g)->_gc_prev = ((g)->_gc_prev & ~_PyGC_PREV_MASK) \ jpayne@69: | ((uintptr_t)(p)); \ jpayne@69: } while (0) jpayne@69: jpayne@69: #define _PyGCHead_FINALIZED(g) \ jpayne@69: (((g)->_gc_prev & _PyGC_PREV_MASK_FINALIZED) != 0) jpayne@69: #define _PyGCHead_SET_FINALIZED(g) \ jpayne@69: ((g)->_gc_prev |= _PyGC_PREV_MASK_FINALIZED) jpayne@69: jpayne@69: #define _PyGC_FINALIZED(o) \ jpayne@69: _PyGCHead_FINALIZED(_Py_AS_GC(o)) jpayne@69: #define _PyGC_SET_FINALIZED(o) \ jpayne@69: _PyGCHead_SET_FINALIZED(_Py_AS_GC(o)) jpayne@69: jpayne@69: jpayne@69: PyAPI_FUNC(PyObject *) _PyObject_GC_Malloc(size_t size); jpayne@69: PyAPI_FUNC(PyObject *) _PyObject_GC_Calloc(size_t size); jpayne@69: jpayne@69: jpayne@69: /* Test if a type supports weak references */ jpayne@69: #define PyType_SUPPORTS_WEAKREFS(t) ((t)->tp_weaklistoffset > 0) jpayne@69: jpayne@69: #define PyObject_GET_WEAKREFS_LISTPTR(o) \ jpayne@69: ((PyObject **) (((char *) (o)) + Py_TYPE(o)->tp_weaklistoffset)) jpayne@69: jpayne@69: #ifdef __cplusplus jpayne@69: } jpayne@69: #endif