#ifndef Py_INTERNAL_GC_H1#define Py_INTERNAL_GC_H2#ifdef __cplusplus3extern "C" {4#endif56#ifndef Py_BUILD_CORE7# error "this header requires Py_BUILD_CORE define"8#endif910/* GC information is stored BEFORE the object structure. */11typedef struct {12// Pointer to next object in the list.13// 0 means the object is not tracked14uintptr_t _gc_next;1516// Pointer to previous object in the list.17// Lowest two bits are used for flags documented later.18uintptr_t _gc_prev;19} PyGC_Head;2021#define _PyGC_Head_UNUSED PyGC_Head222324/* Get an object's GC head */25static inline PyGC_Head* _Py_AS_GC(PyObject *op) {26char *gc = ((char*)op) - sizeof(PyGC_Head);27return (PyGC_Head*)gc;28}2930/* Get the object given the GC head */31static inline PyObject* _Py_FROM_GC(PyGC_Head *gc) {32char *op = ((char *)gc) + sizeof(PyGC_Head);33return (PyObject *)op;34}353637/* True if the object is currently tracked by the GC. */38static inline int _PyObject_GC_IS_TRACKED(PyObject *op) {39PyGC_Head *gc = _Py_AS_GC(op);40return (gc->_gc_next != 0);41}42#define _PyObject_GC_IS_TRACKED(op) _PyObject_GC_IS_TRACKED(_Py_CAST(PyObject*, op))4344/* True if the object may be tracked by the GC in the future, or already is.45This can be useful to implement some optimizations. */46static inline int _PyObject_GC_MAY_BE_TRACKED(PyObject *obj) {47if (!PyObject_IS_GC(obj)) {48return 0;49}50if (PyTuple_CheckExact(obj)) {51return _PyObject_GC_IS_TRACKED(obj);52}53return 1;54}555657/* Bit flags for _gc_prev */58/* Bit 0 is set when tp_finalize is called */59#define _PyGC_PREV_MASK_FINALIZED (1)60/* Bit 1 is set when the object is in generation which is GCed currently. */61#define _PyGC_PREV_MASK_COLLECTING (2)62/* The (N-2) most significant bits contain the real address. */63#define _PyGC_PREV_SHIFT (2)64#define _PyGC_PREV_MASK (((uintptr_t) -1) << _PyGC_PREV_SHIFT)6566// Lowest bit of _gc_next is used for flags only in GC.67// But it is always 0 for normal code.68static inline PyGC_Head* _PyGCHead_NEXT(PyGC_Head *gc) {69uintptr_t next = gc->_gc_next;70return (PyGC_Head*)next;71}72static inline void _PyGCHead_SET_NEXT(PyGC_Head *gc, PyGC_Head *next) {73gc->_gc_next = (uintptr_t)next;74}7576// Lowest two bits of _gc_prev is used for _PyGC_PREV_MASK_* flags.77static inline PyGC_Head* _PyGCHead_PREV(PyGC_Head *gc) {78uintptr_t prev = (gc->_gc_prev & _PyGC_PREV_MASK);79return (PyGC_Head*)prev;80}81static inline void _PyGCHead_SET_PREV(PyGC_Head *gc, PyGC_Head *prev) {82uintptr_t uprev = (uintptr_t)prev;83assert((uprev & ~_PyGC_PREV_MASK) == 0);84gc->_gc_prev = ((gc->_gc_prev & ~_PyGC_PREV_MASK) | uprev);85}8687static inline int _PyGCHead_FINALIZED(PyGC_Head *gc) {88return ((gc->_gc_prev & _PyGC_PREV_MASK_FINALIZED) != 0);89}90static inline void _PyGCHead_SET_FINALIZED(PyGC_Head *gc) {91gc->_gc_prev |= _PyGC_PREV_MASK_FINALIZED;92}9394static inline int _PyGC_FINALIZED(PyObject *op) {95PyGC_Head *gc = _Py_AS_GC(op);96return _PyGCHead_FINALIZED(gc);97}98static inline void _PyGC_SET_FINALIZED(PyObject *op) {99PyGC_Head *gc = _Py_AS_GC(op);100_PyGCHead_SET_FINALIZED(gc);101}102103104/* GC runtime state */105106/* If we change this, we need to change the default value in the107signature of gc.collect. */108#define NUM_GENERATIONS 3109/*110NOTE: about untracking of mutable objects.111112Certain types of container cannot participate in a reference cycle, and113so do not need to be tracked by the garbage collector. Untracking these114objects reduces the cost of garbage collections. However, determining115which objects may be untracked is not free, and the costs must be116weighed against the benefits for garbage collection.117118There are two possible strategies for when to untrack a container:119120i) When the container is created.121ii) When the container is examined by the garbage collector.122123Tuples containing only immutable objects (integers, strings etc, and124recursively, tuples of immutable objects) do not need to be tracked.125The interpreter creates a large number of tuples, many of which will126not survive until garbage collection. It is therefore not worthwhile127to untrack eligible tuples at creation time.128129Instead, all tuples except the empty tuple are tracked when created.130During garbage collection it is determined whether any surviving tuples131can be untracked. A tuple can be untracked if all of its contents are132already not tracked. Tuples are examined for untracking in all garbage133collection cycles. It may take more than one cycle to untrack a tuple.134135Dictionaries containing only immutable objects also do not need to be136tracked. Dictionaries are untracked when created. If a tracked item is137inserted into a dictionary (either as a key or value), the dictionary138becomes tracked. During a full garbage collection (all generations),139the collector will untrack any dictionaries whose contents are not140tracked.141142The module provides the python function is_tracked(obj), which returns143the CURRENT tracking status of the object. Subsequent garbage144collections may change the tracking status of the object.145146Untracking of certain containers was introduced in issue #4688, and147the algorithm was refined in response to issue #14775.148*/149150struct gc_generation {151PyGC_Head head;152int threshold; /* collection threshold */153int count; /* count of allocations or collections of younger154generations */155};156157/* Running stats per generation */158struct gc_generation_stats {159/* total number of collections */160Py_ssize_t collections;161/* total number of collected objects */162Py_ssize_t collected;163/* total number of uncollectable objects (put into gc.garbage) */164Py_ssize_t uncollectable;165};166167struct _gc_runtime_state {168/* List of objects that still need to be cleaned up, singly linked169* via their gc headers' gc_prev pointers. */170PyObject *trash_delete_later;171/* Current call-stack depth of tp_dealloc calls. */172int trash_delete_nesting;173174/* Is automatic collection enabled? */175int enabled;176int debug;177/* linked lists of container objects */178struct gc_generation generations[NUM_GENERATIONS];179PyGC_Head *generation0;180/* a permanent generation which won't be collected */181struct gc_generation permanent_generation;182struct gc_generation_stats generation_stats[NUM_GENERATIONS];183/* true if we are currently running the collector */184int collecting;185/* list of uncollectable objects */186PyObject *garbage;187/* a list of callbacks to be invoked when collection is performed */188PyObject *callbacks;189/* This is the number of objects that survived the last full190collection. It approximates the number of long lived objects191tracked by the GC.192193(by "full collection", we mean a collection of the oldest194generation). */195Py_ssize_t long_lived_total;196/* This is the number of objects that survived all "non-full"197collections, and are awaiting to undergo a full collection for198the first time. */199Py_ssize_t long_lived_pending;200};201202203extern void _PyGC_InitState(struct _gc_runtime_state *);204205extern Py_ssize_t _PyGC_CollectNoFail(PyThreadState *tstate);206207208// Functions to clear types free lists209extern void _PyTuple_ClearFreeList(PyInterpreterState *interp);210extern void _PyFloat_ClearFreeList(PyInterpreterState *interp);211extern void _PyList_ClearFreeList(PyInterpreterState *interp);212extern void _PyDict_ClearFreeList(PyInterpreterState *interp);213extern void _PyAsyncGen_ClearFreeLists(PyInterpreterState *interp);214extern void _PyContext_ClearFreeList(PyInterpreterState *interp);215extern void _Py_ScheduleGC(PyInterpreterState *interp);216extern void _Py_RunGC(PyThreadState *tstate);217218#ifdef __cplusplus219}220#endif221#endif /* !Py_INTERNAL_GC_H */222223224