python (3.11.7)

(root)/
include/
python3.11/
internal/
pycore_gc.h
       1  #ifndef Py_INTERNAL_GC_H
       2  #define Py_INTERNAL_GC_H
       3  #ifdef __cplusplus
       4  extern "C" {
       5  #endif
       6  
       7  #ifndef Py_BUILD_CORE
       8  #  error "this header requires Py_BUILD_CORE define"
       9  #endif
      10  
      11  /* GC information is stored BEFORE the object structure. */
      12  typedef struct {
      13      // Pointer to next object in the list.
      14      // 0 means the object is not tracked
      15      uintptr_t _gc_next;
      16  
      17      // Pointer to previous object in the list.
      18      // Lowest two bits are used for flags documented later.
      19      uintptr_t _gc_prev;
      20  } PyGC_Head;
      21  
      22  #define _Py_AS_GC(o) ((PyGC_Head *)(o)-1)
      23  #define _PyGC_Head_UNUSED PyGC_Head
      24  
      25  /* True if the object is currently tracked by the GC. */
      26  #define _PyObject_GC_IS_TRACKED(o) (_Py_AS_GC(o)->_gc_next != 0)
      27  
      28  /* True if the object may be tracked by the GC in the future, or already is.
      29     This can be useful to implement some optimizations. */
      30  #define _PyObject_GC_MAY_BE_TRACKED(obj) \
      31      (PyObject_IS_GC(obj) && \
      32          (!PyTuple_CheckExact(obj) || _PyObject_GC_IS_TRACKED(obj)))
      33  
      34  
      35  /* Bit flags for _gc_prev */
      36  /* Bit 0 is set when tp_finalize is called */
      37  #define _PyGC_PREV_MASK_FINALIZED  (1)
      38  /* Bit 1 is set when the object is in generation which is GCed currently. */
      39  #define _PyGC_PREV_MASK_COLLECTING (2)
      40  /* The (N-2) most significant bits contain the real address. */
      41  #define _PyGC_PREV_SHIFT           (2)
      42  #define _PyGC_PREV_MASK            (((uintptr_t) -1) << _PyGC_PREV_SHIFT)
      43  
      44  // Lowest bit of _gc_next is used for flags only in GC.
      45  // But it is always 0 for normal code.
      46  #define _PyGCHead_NEXT(g)        ((PyGC_Head*)(g)->_gc_next)
      47  #define _PyGCHead_SET_NEXT(g, p) _Py_RVALUE((g)->_gc_next = (uintptr_t)(p))
      48  
      49  // Lowest two bits of _gc_prev is used for _PyGC_PREV_MASK_* flags.
      50  #define _PyGCHead_PREV(g) ((PyGC_Head*)((g)->_gc_prev & _PyGC_PREV_MASK))
      51  #define _PyGCHead_SET_PREV(g, p) do { \
      52      assert(((uintptr_t)p & ~_PyGC_PREV_MASK) == 0); \
      53      (g)->_gc_prev = ((g)->_gc_prev & ~_PyGC_PREV_MASK) \
      54          | ((uintptr_t)(p)); \
      55      } while (0)
      56  
      57  #define _PyGCHead_FINALIZED(g) \
      58      (((g)->_gc_prev & _PyGC_PREV_MASK_FINALIZED) != 0)
      59  #define _PyGCHead_SET_FINALIZED(g) \
      60      _Py_RVALUE((g)->_gc_prev |= _PyGC_PREV_MASK_FINALIZED)
      61  
      62  #define _PyGC_FINALIZED(o) \
      63      _PyGCHead_FINALIZED(_Py_AS_GC(o))
      64  #define _PyGC_SET_FINALIZED(o) \
      65      _PyGCHead_SET_FINALIZED(_Py_AS_GC(o))
      66  
      67  
      68  /* GC runtime state */
      69  
      70  /* If we change this, we need to change the default value in the
      71     signature of gc.collect. */
      72  #define NUM_GENERATIONS 3
      73  /*
      74     NOTE: about untracking of mutable objects.
      75  
      76     Certain types of container cannot participate in a reference cycle, and
      77     so do not need to be tracked by the garbage collector. Untracking these
      78     objects reduces the cost of garbage collections. However, determining
      79     which objects may be untracked is not free, and the costs must be
      80     weighed against the benefits for garbage collection.
      81  
      82     There are two possible strategies for when to untrack a container:
      83  
      84     i) When the container is created.
      85     ii) When the container is examined by the garbage collector.
      86  
      87     Tuples containing only immutable objects (integers, strings etc, and
      88     recursively, tuples of immutable objects) do not need to be tracked.
      89     The interpreter creates a large number of tuples, many of which will
      90     not survive until garbage collection. It is therefore not worthwhile
      91     to untrack eligible tuples at creation time.
      92  
      93     Instead, all tuples except the empty tuple are tracked when created.
      94     During garbage collection it is determined whether any surviving tuples
      95     can be untracked. A tuple can be untracked if all of its contents are
      96     already not tracked. Tuples are examined for untracking in all garbage
      97     collection cycles. It may take more than one cycle to untrack a tuple.
      98  
      99     Dictionaries containing only immutable objects also do not need to be
     100     tracked. Dictionaries are untracked when created. If a tracked item is
     101     inserted into a dictionary (either as a key or value), the dictionary
     102     becomes tracked. During a full garbage collection (all generations),
     103     the collector will untrack any dictionaries whose contents are not
     104     tracked.
     105  
     106     The module provides the python function is_tracked(obj), which returns
     107     the CURRENT tracking status of the object. Subsequent garbage
     108     collections may change the tracking status of the object.
     109  
     110     Untracking of certain containers was introduced in issue #4688, and
     111     the algorithm was refined in response to issue #14775.
     112  */
     113  
     114  struct gc_generation {
     115      PyGC_Head head;
     116      int threshold; /* collection threshold */
     117      int count; /* count of allocations or collections of younger
     118                    generations */
     119  };
     120  
     121  /* Running stats per generation */
     122  struct gc_generation_stats {
     123      /* total number of collections */
     124      Py_ssize_t collections;
     125      /* total number of collected objects */
     126      Py_ssize_t collected;
     127      /* total number of uncollectable objects (put into gc.garbage) */
     128      Py_ssize_t uncollectable;
     129  };
     130  
     131  struct _gc_runtime_state {
     132      /* List of objects that still need to be cleaned up, singly linked
     133       * via their gc headers' gc_prev pointers.  */
     134      PyObject *trash_delete_later;
     135      /* Current call-stack depth of tp_dealloc calls. */
     136      int trash_delete_nesting;
     137  
     138      /* Is automatic collection enabled? */
     139      int enabled;
     140      int debug;
     141      /* linked lists of container objects */
     142      struct gc_generation generations[NUM_GENERATIONS];
     143      PyGC_Head *generation0;
     144      /* a permanent generation which won't be collected */
     145      struct gc_generation permanent_generation;
     146      struct gc_generation_stats generation_stats[NUM_GENERATIONS];
     147      /* true if we are currently running the collector */
     148      int collecting;
     149      /* list of uncollectable objects */
     150      PyObject *garbage;
     151      /* a list of callbacks to be invoked when collection is performed */
     152      PyObject *callbacks;
     153      /* This is the number of objects that survived the last full
     154         collection. It approximates the number of long lived objects
     155         tracked by the GC.
     156  
     157         (by "full collection", we mean a collection of the oldest
     158         generation). */
     159      Py_ssize_t long_lived_total;
     160      /* This is the number of objects that survived all "non-full"
     161         collections, and are awaiting to undergo a full collection for
     162         the first time. */
     163      Py_ssize_t long_lived_pending;
     164  };
     165  
     166  
     167  extern void _PyGC_InitState(struct _gc_runtime_state *);
     168  
     169  extern Py_ssize_t _PyGC_CollectNoFail(PyThreadState *tstate);
     170  
     171  
     172  // Functions to clear types free lists
     173  extern void _PyTuple_ClearFreeList(PyInterpreterState *interp);
     174  extern void _PyFloat_ClearFreeList(PyInterpreterState *interp);
     175  extern void _PyList_ClearFreeList(PyInterpreterState *interp);
     176  extern void _PyDict_ClearFreeList(PyInterpreterState *interp);
     177  extern void _PyAsyncGen_ClearFreeLists(PyInterpreterState *interp);
     178  extern void _PyContext_ClearFreeList(PyInterpreterState *interp);
     179  
     180  #ifdef __cplusplus
     181  }
     182  #endif
     183  #endif /* !Py_INTERNAL_GC_H */