Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1 | /* |
Tim Peters | 8839617 | 2002-06-30 17:56:40 +0000 | [diff] [blame] | 2 | |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 3 | Reference Cycle Garbage Collection |
| 4 | ================================== |
| 5 | |
Neil Schemenauer | b2c2c9e | 2000-10-04 16:34:09 +0000 | [diff] [blame] | 6 | Neil Schemenauer <nas@arctrix.com> |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 7 | |
| 8 | Based on a post on the python-dev list. Ideas from Guido van Rossum, |
| 9 | Eric Tiedemann, and various others. |
| 10 | |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 11 | http://www.arctrix.com/nas/python/gc/ |
Neil Schemenauer | a7024e9 | 2008-07-15 19:24:01 +0000 | [diff] [blame] | 12 | |
| 13 | The following mailing list threads provide a historical perspective on |
| 14 | the design of this module. Note that a fair amount of refinement has |
| 15 | occurred since those discussions. |
| 16 | |
| 17 | http://mail.python.org/pipermail/python-dev/2000-March/002385.html |
| 18 | http://mail.python.org/pipermail/python-dev/2000-March/002434.html |
| 19 | http://mail.python.org/pipermail/python-dev/2000-March/002497.html |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 20 | |
| 21 | For a highlevel view of the collection process, read the collect |
| 22 | function. |
| 23 | |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 24 | */ |
| 25 | |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 26 | #include "Python.h" |
Victor Stinner | 27e2d1f | 2018-11-01 00:52:28 +0100 | [diff] [blame] | 27 | #include "pycore_context.h" |
Victor Stinner | 444b39b | 2019-11-20 01:18:11 +0100 | [diff] [blame] | 28 | #include "pycore_initconfig.h" |
Victor Stinner | e5014be | 2020-04-14 17:52:15 +0200 | [diff] [blame] | 29 | #include "pycore_interp.h" // PyInterpreterState.gc |
Victor Stinner | bcda8f1 | 2018-11-21 22:27:47 +0100 | [diff] [blame] | 30 | #include "pycore_object.h" |
Victor Stinner | 2e96906 | 2019-11-20 01:49:32 +0100 | [diff] [blame] | 31 | #include "pycore_pyerrors.h" |
Victor Stinner | e5014be | 2020-04-14 17:52:15 +0200 | [diff] [blame] | 32 | #include "pycore_pystate.h" // _PyThreadState_GET() |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 33 | #include "pydtrace.h" |
Victor Stinner | e5014be | 2020-04-14 17:52:15 +0200 | [diff] [blame] | 34 | #include "pytime.h" // _PyTime_GetMonotonicClock() |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 35 | |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 36 | typedef struct _gc_runtime_state GCState; |
| 37 | |
Serhiy Storchaka | 9326028 | 2017-02-04 11:19:59 +0200 | [diff] [blame] | 38 | /*[clinic input] |
| 39 | module gc |
| 40 | [clinic start generated code]*/ |
| 41 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=b5c9690ecc842d79]*/ |
| 42 | |
Pablo Galindo | 320dd50 | 2019-10-10 22:45:17 +0100 | [diff] [blame] | 43 | |
| 44 | #ifdef Py_DEBUG |
| 45 | # define GC_DEBUG |
| 46 | #endif |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 47 | |
| 48 | #define GC_NEXT _PyGCHead_NEXT |
| 49 | #define GC_PREV _PyGCHead_PREV |
| 50 | |
| 51 | // update_refs() set this bit for all objects in current generation. |
| 52 | // subtract_refs() and move_unreachable() uses this to distinguish |
| 53 | // visited object is in GCing or not. |
| 54 | // |
| 55 | // move_unreachable() removes this flag from reachable objects. |
| 56 | // Only unreachable objects have this flag. |
| 57 | // |
| 58 | // No objects in interpreter have this flag after GC ends. |
| 59 | #define PREV_MASK_COLLECTING _PyGC_PREV_MASK_COLLECTING |
| 60 | |
| 61 | // Lowest bit of _gc_next is used for UNREACHABLE flag. |
| 62 | // |
| 63 | // This flag represents the object is in unreachable list in move_unreachable() |
| 64 | // |
| 65 | // Although this flag is used only in move_unreachable(), move_unreachable() |
| 66 | // doesn't clear this flag to skip unnecessary iteration. |
| 67 | // move_legacy_finalizers() removes this flag instead. |
| 68 | // Between them, unreachable list is not normal list and we can not use |
| 69 | // most gc_list_* functions for it. |
| 70 | #define NEXT_MASK_UNREACHABLE (1) |
| 71 | |
Victor Stinner | 626bff8 | 2018-10-25 17:31:10 +0200 | [diff] [blame] | 72 | /* Get an object's GC head */ |
| 73 | #define AS_GC(o) ((PyGC_Head *)(o)-1) |
| 74 | |
| 75 | /* Get the object given the GC head */ |
| 76 | #define FROM_GC(g) ((PyObject *)(((PyGC_Head *)g)+1)) |
| 77 | |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 78 | static inline int |
| 79 | gc_is_collecting(PyGC_Head *g) |
| 80 | { |
| 81 | return (g->_gc_prev & PREV_MASK_COLLECTING) != 0; |
| 82 | } |
| 83 | |
| 84 | static inline void |
| 85 | gc_clear_collecting(PyGC_Head *g) |
| 86 | { |
| 87 | g->_gc_prev &= ~PREV_MASK_COLLECTING; |
| 88 | } |
| 89 | |
| 90 | static inline Py_ssize_t |
| 91 | gc_get_refs(PyGC_Head *g) |
| 92 | { |
| 93 | return (Py_ssize_t)(g->_gc_prev >> _PyGC_PREV_SHIFT); |
| 94 | } |
| 95 | |
| 96 | static inline void |
| 97 | gc_set_refs(PyGC_Head *g, Py_ssize_t refs) |
| 98 | { |
| 99 | g->_gc_prev = (g->_gc_prev & ~_PyGC_PREV_MASK) |
| 100 | | ((uintptr_t)(refs) << _PyGC_PREV_SHIFT); |
| 101 | } |
| 102 | |
| 103 | static inline void |
| 104 | gc_reset_refs(PyGC_Head *g, Py_ssize_t refs) |
| 105 | { |
| 106 | g->_gc_prev = (g->_gc_prev & _PyGC_PREV_MASK_FINALIZED) |
| 107 | | PREV_MASK_COLLECTING |
| 108 | | ((uintptr_t)(refs) << _PyGC_PREV_SHIFT); |
| 109 | } |
| 110 | |
| 111 | static inline void |
| 112 | gc_decref(PyGC_Head *g) |
| 113 | { |
Victor Stinner | 626bff8 | 2018-10-25 17:31:10 +0200 | [diff] [blame] | 114 | _PyObject_ASSERT_WITH_MSG(FROM_GC(g), |
| 115 | gc_get_refs(g) > 0, |
| 116 | "refcount is too small"); |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 117 | g->_gc_prev -= 1 << _PyGC_PREV_SHIFT; |
| 118 | } |
| 119 | |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 120 | /* set for debugging information */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 121 | #define DEBUG_STATS (1<<0) /* print collection statistics */ |
| 122 | #define DEBUG_COLLECTABLE (1<<1) /* print collectable objects */ |
| 123 | #define DEBUG_UNCOLLECTABLE (1<<2) /* print uncollectable objects */ |
| 124 | #define DEBUG_SAVEALL (1<<5) /* save all garbage in gc.garbage */ |
| 125 | #define DEBUG_LEAK DEBUG_COLLECTABLE | \ |
| 126 | DEBUG_UNCOLLECTABLE | \ |
| 127 | DEBUG_SAVEALL |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 128 | |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 129 | #define GEN_HEAD(gcstate, n) (&(gcstate)->generations[n].head) |
Antoine Pitrou | d4156c1 | 2012-10-30 22:43:19 +0100 | [diff] [blame] | 130 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 131 | void |
Victor Stinner | 7247407 | 2019-11-20 12:25:50 +0100 | [diff] [blame] | 132 | _PyGC_InitState(GCState *gcstate) |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 133 | { |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 134 | gcstate->enabled = 1; /* automatic collection enabled? */ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 135 | |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 136 | #define _GEN_HEAD(n) GEN_HEAD(gcstate, n) |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 137 | struct gc_generation generations[NUM_GENERATIONS] = { |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 138 | /* PyGC_Head, threshold, count */ |
| 139 | {{(uintptr_t)_GEN_HEAD(0), (uintptr_t)_GEN_HEAD(0)}, 700, 0}, |
| 140 | {{(uintptr_t)_GEN_HEAD(1), (uintptr_t)_GEN_HEAD(1)}, 10, 0}, |
| 141 | {{(uintptr_t)_GEN_HEAD(2), (uintptr_t)_GEN_HEAD(2)}, 10, 0}, |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 142 | }; |
| 143 | for (int i = 0; i < NUM_GENERATIONS; i++) { |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 144 | gcstate->generations[i] = generations[i]; |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 145 | }; |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 146 | gcstate->generation0 = GEN_HEAD(gcstate, 0); |
brainfvck | c75edab | 2017-10-16 12:49:41 -0700 | [diff] [blame] | 147 | struct gc_generation permanent_generation = { |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 148 | {(uintptr_t)&gcstate->permanent_generation.head, |
| 149 | (uintptr_t)&gcstate->permanent_generation.head}, 0, 0 |
brainfvck | c75edab | 2017-10-16 12:49:41 -0700 | [diff] [blame] | 150 | }; |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 151 | gcstate->permanent_generation = permanent_generation; |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 152 | } |
Antoine Pitrou | d4156c1 | 2012-10-30 22:43:19 +0100 | [diff] [blame] | 153 | |
Victor Stinner | 444b39b | 2019-11-20 01:18:11 +0100 | [diff] [blame] | 154 | |
| 155 | PyStatus |
Victor Stinner | 01b1cc1 | 2019-11-20 02:27:56 +0100 | [diff] [blame] | 156 | _PyGC_Init(PyThreadState *tstate) |
Victor Stinner | 444b39b | 2019-11-20 01:18:11 +0100 | [diff] [blame] | 157 | { |
Victor Stinner | 7247407 | 2019-11-20 12:25:50 +0100 | [diff] [blame] | 158 | GCState *gcstate = &tstate->interp->gc; |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 159 | if (gcstate->garbage == NULL) { |
| 160 | gcstate->garbage = PyList_New(0); |
| 161 | if (gcstate->garbage == NULL) { |
Victor Stinner | 444b39b | 2019-11-20 01:18:11 +0100 | [diff] [blame] | 162 | return _PyStatus_NO_MEMORY(); |
| 163 | } |
| 164 | } |
| 165 | return _PyStatus_OK(); |
| 166 | } |
| 167 | |
| 168 | |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 169 | /* |
| 170 | _gc_prev values |
| 171 | --------------- |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 172 | |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 173 | Between collections, _gc_prev is used for doubly linked list. |
Tim Peters | 6fc13d9 | 2002-07-02 18:12:35 +0000 | [diff] [blame] | 174 | |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 175 | Lowest two bits of _gc_prev are used for flags. |
| 176 | PREV_MASK_COLLECTING is used only while collecting and cleared before GC ends |
| 177 | or _PyObject_GC_UNTRACK() is called. |
Tim Peters | 6fc13d9 | 2002-07-02 18:12:35 +0000 | [diff] [blame] | 178 | |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 179 | During a collection, _gc_prev is temporary used for gc_refs, and the gc list |
| 180 | is singly linked until _gc_prev is restored. |
Tim Peters | 6fc13d9 | 2002-07-02 18:12:35 +0000 | [diff] [blame] | 181 | |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 182 | gc_refs |
Tim Peters | 6fc13d9 | 2002-07-02 18:12:35 +0000 | [diff] [blame] | 183 | At the start of a collection, update_refs() copies the true refcount |
| 184 | to gc_refs, for each object in the generation being collected. |
| 185 | subtract_refs() then adjusts gc_refs so that it equals the number of |
| 186 | times an object is referenced directly from outside the generation |
| 187 | being collected. |
Tim Peters | 6fc13d9 | 2002-07-02 18:12:35 +0000 | [diff] [blame] | 188 | |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 189 | PREV_MASK_COLLECTING |
| 190 | Objects in generation being collected are marked PREV_MASK_COLLECTING in |
| 191 | update_refs(). |
| 192 | |
| 193 | |
| 194 | _gc_next values |
| 195 | --------------- |
| 196 | |
| 197 | _gc_next takes these values: |
| 198 | |
| 199 | 0 |
| 200 | The object is not tracked |
| 201 | |
| 202 | != 0 |
| 203 | Pointer to the next object in the GC list. |
| 204 | Additionally, lowest bit is used temporary for |
| 205 | NEXT_MASK_UNREACHABLE flag described below. |
| 206 | |
| 207 | NEXT_MASK_UNREACHABLE |
Tim Peters | 6fc13d9 | 2002-07-02 18:12:35 +0000 | [diff] [blame] | 208 | move_unreachable() then moves objects not reachable (whether directly or |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 209 | indirectly) from outside the generation into an "unreachable" set and |
| 210 | set this flag. |
Tim Peters | 6fc13d9 | 2002-07-02 18:12:35 +0000 | [diff] [blame] | 211 | |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 212 | Objects that are found to be reachable have gc_refs set to 1. |
| 213 | When this flag is set for the reachable object, the object must be in |
| 214 | "unreachable" set. |
| 215 | The flag is unset and the object is moved back to "reachable" set. |
| 216 | |
| 217 | move_legacy_finalizers() will remove this flag from "unreachable" set. |
Tim Peters | 6fc13d9 | 2002-07-02 18:12:35 +0000 | [diff] [blame] | 218 | */ |
Neil Schemenauer | a2b11ec | 2002-05-21 15:53:24 +0000 | [diff] [blame] | 219 | |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 220 | /*** list functions ***/ |
| 221 | |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 222 | static inline void |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 223 | gc_list_init(PyGC_Head *list) |
| 224 | { |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 225 | // List header must not have flags. |
| 226 | // We can assign pointer by simple cast. |
| 227 | list->_gc_prev = (uintptr_t)list; |
| 228 | list->_gc_next = (uintptr_t)list; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 229 | } |
| 230 | |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 231 | static inline int |
Neil Schemenauer | 2880ae5 | 2002-05-04 05:35:20 +0000 | [diff] [blame] | 232 | gc_list_is_empty(PyGC_Head *list) |
| 233 | { |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 234 | return (list->_gc_next == (uintptr_t)list); |
Neil Schemenauer | 2880ae5 | 2002-05-04 05:35:20 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Tim Peters | e2d5918 | 2004-11-01 01:39:08 +0000 | [diff] [blame] | 237 | /* Append `node` to `list`. */ |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 238 | static inline void |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 239 | gc_list_append(PyGC_Head *node, PyGC_Head *list) |
| 240 | { |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 241 | PyGC_Head *last = (PyGC_Head *)list->_gc_prev; |
| 242 | |
| 243 | // last <-> node |
| 244 | _PyGCHead_SET_PREV(node, last); |
| 245 | _PyGCHead_SET_NEXT(last, node); |
| 246 | |
| 247 | // node <-> list |
| 248 | _PyGCHead_SET_NEXT(node, list); |
| 249 | list->_gc_prev = (uintptr_t)node; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Tim Peters | e2d5918 | 2004-11-01 01:39:08 +0000 | [diff] [blame] | 252 | /* Remove `node` from the gc list it's currently in. */ |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 253 | static inline void |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 254 | gc_list_remove(PyGC_Head *node) |
| 255 | { |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 256 | PyGC_Head *prev = GC_PREV(node); |
| 257 | PyGC_Head *next = GC_NEXT(node); |
| 258 | |
| 259 | _PyGCHead_SET_NEXT(prev, next); |
| 260 | _PyGCHead_SET_PREV(next, prev); |
| 261 | |
| 262 | node->_gc_next = 0; /* object is not currently tracked */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Tim Peters | e2d5918 | 2004-11-01 01:39:08 +0000 | [diff] [blame] | 265 | /* Move `node` from the gc list it's currently in (which is not explicitly |
| 266 | * named here) to the end of `list`. This is semantically the same as |
| 267 | * gc_list_remove(node) followed by gc_list_append(node, list). |
| 268 | */ |
| 269 | static void |
| 270 | gc_list_move(PyGC_Head *node, PyGC_Head *list) |
| 271 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 272 | /* Unlink from current list. */ |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 273 | PyGC_Head *from_prev = GC_PREV(node); |
| 274 | PyGC_Head *from_next = GC_NEXT(node); |
| 275 | _PyGCHead_SET_NEXT(from_prev, from_next); |
| 276 | _PyGCHead_SET_PREV(from_next, from_prev); |
| 277 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 278 | /* Relink at end of new list. */ |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 279 | // list must not have flags. So we can skip macros. |
| 280 | PyGC_Head *to_prev = (PyGC_Head*)list->_gc_prev; |
| 281 | _PyGCHead_SET_PREV(node, to_prev); |
| 282 | _PyGCHead_SET_NEXT(to_prev, node); |
| 283 | list->_gc_prev = (uintptr_t)node; |
| 284 | _PyGCHead_SET_NEXT(node, list); |
Tim Peters | e2d5918 | 2004-11-01 01:39:08 +0000 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | /* append list `from` onto list `to`; `from` becomes an empty list */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 288 | static void |
| 289 | gc_list_merge(PyGC_Head *from, PyGC_Head *to) |
| 290 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 291 | assert(from != to); |
| 292 | if (!gc_list_is_empty(from)) { |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 293 | PyGC_Head *to_tail = GC_PREV(to); |
| 294 | PyGC_Head *from_head = GC_NEXT(from); |
| 295 | PyGC_Head *from_tail = GC_PREV(from); |
| 296 | assert(from_head != from); |
| 297 | assert(from_tail != from); |
| 298 | |
| 299 | _PyGCHead_SET_NEXT(to_tail, from_head); |
| 300 | _PyGCHead_SET_PREV(from_head, to_tail); |
| 301 | |
| 302 | _PyGCHead_SET_NEXT(from_tail, to); |
| 303 | _PyGCHead_SET_PREV(to, from_tail); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 304 | } |
| 305 | gc_list_init(from); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Neal Norwitz | 7b216c5 | 2006-03-04 20:01:53 +0000 | [diff] [blame] | 308 | static Py_ssize_t |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 309 | gc_list_size(PyGC_Head *list) |
| 310 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 311 | PyGC_Head *gc; |
| 312 | Py_ssize_t n = 0; |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 313 | for (gc = GC_NEXT(list); gc != list; gc = GC_NEXT(gc)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 314 | n++; |
| 315 | } |
| 316 | return n; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Pablo Galindo | 466326d | 2019-10-13 16:48:59 +0100 | [diff] [blame] | 319 | /* Walk the list and mark all objects as non-collecting */ |
| 320 | static inline void |
| 321 | gc_list_clear_collecting(PyGC_Head *collectable) |
| 322 | { |
| 323 | PyGC_Head *gc; |
| 324 | for (gc = GC_NEXT(collectable); gc != collectable; gc = GC_NEXT(gc)) { |
| 325 | gc_clear_collecting(gc); |
| 326 | } |
| 327 | } |
| 328 | |
Tim Peters | 259272b | 2003-04-06 19:41:39 +0000 | [diff] [blame] | 329 | /* Append objects in a GC list to a Python list. |
Pablo Galindo | 466326d | 2019-10-13 16:48:59 +0100 | [diff] [blame] | 330 | * Return 0 if all OK, < 0 if error (out of memory for list) |
Tim Peters | 259272b | 2003-04-06 19:41:39 +0000 | [diff] [blame] | 331 | */ |
| 332 | static int |
| 333 | append_objects(PyObject *py_list, PyGC_Head *gc_list) |
| 334 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 335 | PyGC_Head *gc; |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 336 | for (gc = GC_NEXT(gc_list); gc != gc_list; gc = GC_NEXT(gc)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 337 | PyObject *op = FROM_GC(gc); |
| 338 | if (op != py_list) { |
| 339 | if (PyList_Append(py_list, op)) { |
| 340 | return -1; /* exception */ |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | return 0; |
Tim Peters | 259272b | 2003-04-06 19:41:39 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Tim Peters | ea55c51 | 2019-10-18 20:59:14 -0500 | [diff] [blame] | 347 | // Constants for validate_list's flags argument. |
| 348 | enum flagstates {collecting_clear_unreachable_clear, |
| 349 | collecting_clear_unreachable_set, |
| 350 | collecting_set_unreachable_clear, |
| 351 | collecting_set_unreachable_set}; |
| 352 | |
Pablo Galindo | 320dd50 | 2019-10-10 22:45:17 +0100 | [diff] [blame] | 353 | #ifdef GC_DEBUG |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 354 | // validate_list checks list consistency. And it works as document |
Tim Peters | 95bfc8a | 2019-10-13 16:47:04 -0500 | [diff] [blame] | 355 | // describing when flags are expected to be set / unset. |
| 356 | // `head` must be a doubly-linked gc list, although it's fine (expected!) if |
| 357 | // the prev and next pointers are "polluted" with flags. |
| 358 | // What's checked: |
| 359 | // - The `head` pointers are not polluted. |
Tim Peters | ea55c51 | 2019-10-18 20:59:14 -0500 | [diff] [blame] | 360 | // - The objects' PREV_MASK_COLLECTING and NEXT_MASK_UNREACHABLE flags are all |
| 361 | // `set or clear, as specified by the 'flags' argument. |
Tim Peters | 95bfc8a | 2019-10-13 16:47:04 -0500 | [diff] [blame] | 362 | // - The prev and next pointers are mutually consistent. |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 363 | static void |
Tim Peters | ea55c51 | 2019-10-18 20:59:14 -0500 | [diff] [blame] | 364 | validate_list(PyGC_Head *head, enum flagstates flags) |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 365 | { |
Tim Peters | 95bfc8a | 2019-10-13 16:47:04 -0500 | [diff] [blame] | 366 | assert((head->_gc_prev & PREV_MASK_COLLECTING) == 0); |
| 367 | assert((head->_gc_next & NEXT_MASK_UNREACHABLE) == 0); |
Tim Peters | ea55c51 | 2019-10-18 20:59:14 -0500 | [diff] [blame] | 368 | uintptr_t prev_value = 0, next_value = 0; |
| 369 | switch (flags) { |
| 370 | case collecting_clear_unreachable_clear: |
| 371 | break; |
| 372 | case collecting_set_unreachable_clear: |
| 373 | prev_value = PREV_MASK_COLLECTING; |
| 374 | break; |
| 375 | case collecting_clear_unreachable_set: |
| 376 | next_value = NEXT_MASK_UNREACHABLE; |
| 377 | break; |
| 378 | case collecting_set_unreachable_set: |
| 379 | prev_value = PREV_MASK_COLLECTING; |
| 380 | next_value = NEXT_MASK_UNREACHABLE; |
| 381 | break; |
| 382 | default: |
| 383 | assert(! "bad internal flags argument"); |
| 384 | } |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 385 | PyGC_Head *prev = head; |
| 386 | PyGC_Head *gc = GC_NEXT(head); |
| 387 | while (gc != head) { |
Tim Peters | 95bfc8a | 2019-10-13 16:47:04 -0500 | [diff] [blame] | 388 | PyGC_Head *trueprev = GC_PREV(gc); |
| 389 | PyGC_Head *truenext = (PyGC_Head *)(gc->_gc_next & ~NEXT_MASK_UNREACHABLE); |
| 390 | assert(truenext != NULL); |
| 391 | assert(trueprev == prev); |
| 392 | assert((gc->_gc_prev & PREV_MASK_COLLECTING) == prev_value); |
| 393 | assert((gc->_gc_next & NEXT_MASK_UNREACHABLE) == next_value); |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 394 | prev = gc; |
Tim Peters | 95bfc8a | 2019-10-13 16:47:04 -0500 | [diff] [blame] | 395 | gc = truenext; |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 396 | } |
| 397 | assert(prev == GC_PREV(head)); |
| 398 | } |
| 399 | #else |
Tim Peters | ea55c51 | 2019-10-18 20:59:14 -0500 | [diff] [blame] | 400 | #define validate_list(x, y) do{}while(0) |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 401 | #endif |
| 402 | |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 403 | /*** end of list stuff ***/ |
| 404 | |
| 405 | |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 406 | /* Set all gc_refs = ob_refcnt. After this, gc_refs is > 0 and |
| 407 | * PREV_MASK_COLLECTING bit is set for all objects in containers. |
Tim Peters | 8839617 | 2002-06-30 17:56:40 +0000 | [diff] [blame] | 408 | */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 409 | static void |
| 410 | update_refs(PyGC_Head *containers) |
| 411 | { |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 412 | PyGC_Head *gc = GC_NEXT(containers); |
| 413 | for (; gc != containers; gc = GC_NEXT(gc)) { |
| 414 | gc_reset_refs(gc, Py_REFCNT(FROM_GC(gc))); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 415 | /* Python's cyclic gc should never see an incoming refcount |
| 416 | * of 0: if something decref'ed to 0, it should have been |
| 417 | * deallocated immediately at that time. |
| 418 | * Possible cause (if the assert triggers): a tp_dealloc |
| 419 | * routine left a gc-aware object tracked during its teardown |
| 420 | * phase, and did something-- or allowed something to happen -- |
| 421 | * that called back into Python. gc can trigger then, and may |
| 422 | * see the still-tracked dying object. Before this assert |
| 423 | * was added, such mistakes went on to allow gc to try to |
| 424 | * delete the object again. In a debug build, that caused |
| 425 | * a mysterious segfault, when _Py_ForgetReference tried |
| 426 | * to remove the object from the doubly-linked list of all |
| 427 | * objects a second time. In a release build, an actual |
| 428 | * double deallocation occurred, which leads to corruption |
| 429 | * of the allocator's internal bookkeeping pointers. That's |
| 430 | * so serious that maybe this should be a release-build |
| 431 | * check instead of an assert? |
| 432 | */ |
Victor Stinner | a4b2bc7 | 2018-10-26 18:00:13 +0200 | [diff] [blame] | 433 | _PyObject_ASSERT(FROM_GC(gc), gc_get_refs(gc) != 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 434 | } |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 435 | } |
| 436 | |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 437 | /* A traversal callback for subtract_refs. */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 438 | static int |
Victor Stinner | 4d5f94b | 2019-10-08 02:37:38 +0200 | [diff] [blame] | 439 | visit_decref(PyObject *op, void *parent) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 440 | { |
Victor Stinner | 4d5f94b | 2019-10-08 02:37:38 +0200 | [diff] [blame] | 441 | _PyObject_ASSERT(_PyObject_CAST(parent), !_PyObject_IsFreed(op)); |
Victor Stinner | d91d4de | 2019-09-09 17:44:59 +0200 | [diff] [blame] | 442 | |
Hai Shi | 675d9a3 | 2020-04-15 02:11:20 +0800 | [diff] [blame] | 443 | if (_PyObject_IS_GC(op)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 444 | PyGC_Head *gc = AS_GC(op); |
| 445 | /* We're only interested in gc_refs for objects in the |
| 446 | * generation being collected, which can be recognized |
| 447 | * because only they have positive gc_refs. |
| 448 | */ |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 449 | if (gc_is_collecting(gc)) { |
| 450 | gc_decref(gc); |
| 451 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 452 | } |
| 453 | return 0; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 454 | } |
| 455 | |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 456 | /* Subtract internal references from gc_refs. After this, gc_refs is >= 0 |
| 457 | * for all objects in containers, and is GC_REACHABLE for all tracked gc |
| 458 | * objects not in containers. The ones with gc_refs > 0 are directly |
| 459 | * reachable from outside containers, and so can't be collected. |
| 460 | */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 461 | static void |
| 462 | subtract_refs(PyGC_Head *containers) |
| 463 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 464 | traverseproc traverse; |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 465 | PyGC_Head *gc = GC_NEXT(containers); |
| 466 | for (; gc != containers; gc = GC_NEXT(gc)) { |
Victor Stinner | 4d5f94b | 2019-10-08 02:37:38 +0200 | [diff] [blame] | 467 | PyObject *op = FROM_GC(gc); |
| 468 | traverse = Py_TYPE(op)->tp_traverse; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 469 | (void) traverse(FROM_GC(gc), |
| 470 | (visitproc)visit_decref, |
Victor Stinner | 4d5f94b | 2019-10-08 02:37:38 +0200 | [diff] [blame] | 471 | op); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 472 | } |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 473 | } |
| 474 | |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 475 | /* A traversal callback for move_unreachable. */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 476 | static int |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 477 | visit_reachable(PyObject *op, PyGC_Head *reachable) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 478 | { |
Hai Shi | 675d9a3 | 2020-04-15 02:11:20 +0800 | [diff] [blame] | 479 | if (!_PyObject_IS_GC(op)) { |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 480 | return 0; |
| 481 | } |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 482 | |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 483 | PyGC_Head *gc = AS_GC(op); |
| 484 | const Py_ssize_t gc_refs = gc_get_refs(gc); |
| 485 | |
Tim Peters | 1e73945 | 2019-10-21 11:21:35 -0500 | [diff] [blame] | 486 | // Ignore objects in other generation. |
Tim Peters | 95bfc8a | 2019-10-13 16:47:04 -0500 | [diff] [blame] | 487 | // This also skips objects "to the left" of the current position in |
| 488 | // move_unreachable's scan of the 'young' list - they've already been |
| 489 | // traversed, and no longer have the PREV_MASK_COLLECTING flag. |
Tim Peters | 1e73945 | 2019-10-21 11:21:35 -0500 | [diff] [blame] | 490 | if (! gc_is_collecting(gc)) { |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 491 | return 0; |
| 492 | } |
Tim Peters | 1e73945 | 2019-10-21 11:21:35 -0500 | [diff] [blame] | 493 | // It would be a logic error elsewhere if the collecting flag were set on |
| 494 | // an untracked object. |
| 495 | assert(gc->_gc_next != 0); |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 496 | |
| 497 | if (gc->_gc_next & NEXT_MASK_UNREACHABLE) { |
| 498 | /* This had gc_refs = 0 when move_unreachable got |
| 499 | * to it, but turns out it's reachable after all. |
| 500 | * Move it back to move_unreachable's 'young' list, |
| 501 | * and move_unreachable will eventually get to it |
| 502 | * again. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 503 | */ |
Tim Peters | 95bfc8a | 2019-10-13 16:47:04 -0500 | [diff] [blame] | 504 | // Manually unlink gc from unreachable list because the list functions |
| 505 | // don't work right in the presence of NEXT_MASK_UNREACHABLE flags. |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 506 | PyGC_Head *prev = GC_PREV(gc); |
| 507 | PyGC_Head *next = (PyGC_Head*)(gc->_gc_next & ~NEXT_MASK_UNREACHABLE); |
Victor Stinner | a4b2bc7 | 2018-10-26 18:00:13 +0200 | [diff] [blame] | 508 | _PyObject_ASSERT(FROM_GC(prev), |
| 509 | prev->_gc_next & NEXT_MASK_UNREACHABLE); |
| 510 | _PyObject_ASSERT(FROM_GC(next), |
| 511 | next->_gc_next & NEXT_MASK_UNREACHABLE); |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 512 | prev->_gc_next = gc->_gc_next; // copy NEXT_MASK_UNREACHABLE |
| 513 | _PyGCHead_SET_PREV(next, prev); |
| 514 | |
| 515 | gc_list_append(gc, reachable); |
| 516 | gc_set_refs(gc, 1); |
| 517 | } |
| 518 | else if (gc_refs == 0) { |
| 519 | /* This is in move_unreachable's 'young' list, but |
| 520 | * the traversal hasn't yet gotten to it. All |
| 521 | * we need to do is tell move_unreachable that it's |
| 522 | * reachable. |
| 523 | */ |
| 524 | gc_set_refs(gc, 1); |
| 525 | } |
| 526 | /* Else there's nothing to do. |
| 527 | * If gc_refs > 0, it must be in move_unreachable's 'young' |
| 528 | * list, and move_unreachable will eventually get to it. |
| 529 | */ |
| 530 | else { |
Victor Stinner | a4b2bc7 | 2018-10-26 18:00:13 +0200 | [diff] [blame] | 531 | _PyObject_ASSERT_WITH_MSG(op, gc_refs > 0, "refcount is too small"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 532 | } |
| 533 | return 0; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 534 | } |
| 535 | |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 536 | /* Move the unreachable objects from young to unreachable. After this, |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 537 | * all objects in young don't have PREV_MASK_COLLECTING flag and |
| 538 | * unreachable have the flag. |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 539 | * All objects in young after this are directly or indirectly reachable |
| 540 | * from outside the original young; and all objects in unreachable are |
| 541 | * not. |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 542 | * |
| 543 | * This function restores _gc_prev pointer. young and unreachable are |
| 544 | * doubly linked list after this function. |
| 545 | * But _gc_next in unreachable list has NEXT_MASK_UNREACHABLE flag. |
| 546 | * So we can not gc_list_* functions for unreachable until we remove the flag. |
Tim Peters | 8839617 | 2002-06-30 17:56:40 +0000 | [diff] [blame] | 547 | */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 548 | static void |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 549 | move_unreachable(PyGC_Head *young, PyGC_Head *unreachable) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 550 | { |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 551 | // previous elem in the young list, used for restore gc_prev. |
| 552 | PyGC_Head *prev = young; |
| 553 | PyGC_Head *gc = GC_NEXT(young); |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 554 | |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 555 | /* Invariants: all objects "to the left" of us in young are reachable |
| 556 | * (directly or indirectly) from outside the young list as it was at entry. |
| 557 | * |
| 558 | * All other objects from the original young "to the left" of us are in |
| 559 | * unreachable now, and have NEXT_MASK_UNREACHABLE. All objects to the |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 560 | * left of us in 'young' now have been scanned, and no objects here |
| 561 | * or to the right have been scanned yet. |
| 562 | */ |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 563 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 564 | while (gc != young) { |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 565 | if (gc_get_refs(gc)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 566 | /* gc is definitely reachable from outside the |
| 567 | * original 'young'. Mark it as such, and traverse |
| 568 | * its pointers to find any other objects that may |
| 569 | * be directly reachable from it. Note that the |
| 570 | * call to tp_traverse may append objects to young, |
| 571 | * so we have to wait until it returns to determine |
| 572 | * the next object to visit. |
| 573 | */ |
| 574 | PyObject *op = FROM_GC(gc); |
| 575 | traverseproc traverse = Py_TYPE(op)->tp_traverse; |
Victor Stinner | a4b2bc7 | 2018-10-26 18:00:13 +0200 | [diff] [blame] | 576 | _PyObject_ASSERT_WITH_MSG(op, gc_get_refs(gc) > 0, |
| 577 | "refcount is too small"); |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 578 | // NOTE: visit_reachable may change gc->_gc_next when |
| 579 | // young->_gc_prev == gc. Don't do gc = GC_NEXT(gc) before! |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 580 | (void) traverse(op, |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 581 | (visitproc)visit_reachable, |
| 582 | (void *)young); |
| 583 | // relink gc_prev to prev element. |
| 584 | _PyGCHead_SET_PREV(gc, prev); |
Quan Tian | 3bd0d62 | 2018-10-20 05:30:03 +0800 | [diff] [blame] | 585 | // gc is not COLLECTING state after here. |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 586 | gc_clear_collecting(gc); |
| 587 | prev = gc; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 588 | } |
| 589 | else { |
| 590 | /* This *may* be unreachable. To make progress, |
| 591 | * assume it is. gc isn't directly reachable from |
| 592 | * any object we've already traversed, but may be |
| 593 | * reachable from an object we haven't gotten to yet. |
| 594 | * visit_reachable will eventually move gc back into |
| 595 | * young if that's so, and we'll see it again. |
| 596 | */ |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 597 | // Move gc to unreachable. |
| 598 | // No need to gc->next->prev = prev because it is single linked. |
| 599 | prev->_gc_next = gc->_gc_next; |
| 600 | |
| 601 | // We can't use gc_list_append() here because we use |
| 602 | // NEXT_MASK_UNREACHABLE here. |
| 603 | PyGC_Head *last = GC_PREV(unreachable); |
| 604 | // NOTE: Since all objects in unreachable set has |
| 605 | // NEXT_MASK_UNREACHABLE flag, we set it unconditionally. |
Tim Peters | 95bfc8a | 2019-10-13 16:47:04 -0500 | [diff] [blame] | 606 | // But this may pollute the unreachable list head's 'next' pointer |
| 607 | // too. That's semantically senseless but expedient here - the |
Pablo Galindo | 97f1267 | 2020-01-13 12:25:05 +0000 | [diff] [blame] | 608 | // damage is repaired when this function ends. |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 609 | last->_gc_next = (NEXT_MASK_UNREACHABLE | (uintptr_t)gc); |
| 610 | _PyGCHead_SET_PREV(gc, last); |
| 611 | gc->_gc_next = (NEXT_MASK_UNREACHABLE | (uintptr_t)unreachable); |
| 612 | unreachable->_gc_prev = (uintptr_t)gc; |
| 613 | } |
| 614 | gc = (PyGC_Head*)prev->_gc_next; |
| 615 | } |
| 616 | // young->_gc_prev must be last element remained in the list. |
| 617 | young->_gc_prev = (uintptr_t)prev; |
Tim Peters | 95bfc8a | 2019-10-13 16:47:04 -0500 | [diff] [blame] | 618 | // don't let the pollution of the list head's next pointer leak |
| 619 | unreachable->_gc_next &= ~NEXT_MASK_UNREACHABLE; |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | static void |
| 623 | untrack_tuples(PyGC_Head *head) |
| 624 | { |
| 625 | PyGC_Head *next, *gc = GC_NEXT(head); |
| 626 | while (gc != head) { |
| 627 | PyObject *op = FROM_GC(gc); |
| 628 | next = GC_NEXT(gc); |
| 629 | if (PyTuple_CheckExact(op)) { |
| 630 | _PyTuple_MaybeUntrack(op); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 631 | } |
| 632 | gc = next; |
| 633 | } |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 634 | } |
| 635 | |
Antoine Pitrou | e1ad3da | 2012-05-28 22:22:34 +0200 | [diff] [blame] | 636 | /* Try to untrack all currently tracked dictionaries */ |
| 637 | static void |
| 638 | untrack_dicts(PyGC_Head *head) |
| 639 | { |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 640 | PyGC_Head *next, *gc = GC_NEXT(head); |
Antoine Pitrou | e1ad3da | 2012-05-28 22:22:34 +0200 | [diff] [blame] | 641 | while (gc != head) { |
| 642 | PyObject *op = FROM_GC(gc); |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 643 | next = GC_NEXT(gc); |
| 644 | if (PyDict_CheckExact(op)) { |
Antoine Pitrou | e1ad3da | 2012-05-28 22:22:34 +0200 | [diff] [blame] | 645 | _PyDict_MaybeUntrack(op); |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 646 | } |
Antoine Pitrou | e1ad3da | 2012-05-28 22:22:34 +0200 | [diff] [blame] | 647 | gc = next; |
| 648 | } |
| 649 | } |
| 650 | |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 651 | /* Return true if object has a pre-PEP 442 finalization method. */ |
Neil Schemenauer | a765c12 | 2001-11-01 17:35:23 +0000 | [diff] [blame] | 652 | static int |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 653 | has_legacy_finalizer(PyObject *op) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 654 | { |
Victor Stinner | daa9756 | 2020-02-07 03:37:06 +0100 | [diff] [blame] | 655 | return Py_TYPE(op)->tp_del != NULL; |
Neil Schemenauer | a765c12 | 2001-11-01 17:35:23 +0000 | [diff] [blame] | 656 | } |
| 657 | |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 658 | /* Move the objects in unreachable with tp_del slots into `finalizers`. |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 659 | * |
| 660 | * This function also removes NEXT_MASK_UNREACHABLE flag |
| 661 | * from _gc_next in unreachable. |
Jeremy Hylton | ce136e9 | 2003-04-04 19:59:06 +0000 | [diff] [blame] | 662 | */ |
Neil Schemenauer | a765c12 | 2001-11-01 17:35:23 +0000 | [diff] [blame] | 663 | static void |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 664 | move_legacy_finalizers(PyGC_Head *unreachable, PyGC_Head *finalizers) |
Neil Schemenauer | a765c12 | 2001-11-01 17:35:23 +0000 | [diff] [blame] | 665 | { |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 666 | PyGC_Head *gc, *next; |
Tim Peters | 95bfc8a | 2019-10-13 16:47:04 -0500 | [diff] [blame] | 667 | assert((unreachable->_gc_next & NEXT_MASK_UNREACHABLE) == 0); |
Tim Peters | f6b8045 | 2003-04-07 19:21:15 +0000 | [diff] [blame] | 668 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 669 | /* March over unreachable. Move objects with finalizers into |
| 670 | * `finalizers`. |
| 671 | */ |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 672 | for (gc = GC_NEXT(unreachable); gc != unreachable; gc = next) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 673 | PyObject *op = FROM_GC(gc); |
Jeremy Hylton | ce136e9 | 2003-04-04 19:59:06 +0000 | [diff] [blame] | 674 | |
Victor Stinner | a4b2bc7 | 2018-10-26 18:00:13 +0200 | [diff] [blame] | 675 | _PyObject_ASSERT(op, gc->_gc_next & NEXT_MASK_UNREACHABLE); |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 676 | gc->_gc_next &= ~NEXT_MASK_UNREACHABLE; |
| 677 | next = (PyGC_Head*)gc->_gc_next; |
Tim Peters | f6ae7a4 | 2003-04-05 18:40:50 +0000 | [diff] [blame] | 678 | |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 679 | if (has_legacy_finalizer(op)) { |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 680 | gc_clear_collecting(gc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 681 | gc_list_move(gc, finalizers); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 682 | } |
| 683 | } |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 684 | } |
| 685 | |
Pablo Galindo | 466326d | 2019-10-13 16:48:59 +0100 | [diff] [blame] | 686 | static inline void |
| 687 | clear_unreachable_mask(PyGC_Head *unreachable) |
| 688 | { |
| 689 | /* Check that the list head does not have the unreachable bit set */ |
| 690 | assert(((uintptr_t)unreachable & NEXT_MASK_UNREACHABLE) == 0); |
| 691 | |
| 692 | PyGC_Head *gc, *next; |
Tim Peters | 95bfc8a | 2019-10-13 16:47:04 -0500 | [diff] [blame] | 693 | assert((unreachable->_gc_next & NEXT_MASK_UNREACHABLE) == 0); |
Pablo Galindo | 466326d | 2019-10-13 16:48:59 +0100 | [diff] [blame] | 694 | for (gc = GC_NEXT(unreachable); gc != unreachable; gc = next) { |
| 695 | _PyObject_ASSERT((PyObject*)FROM_GC(gc), gc->_gc_next & NEXT_MASK_UNREACHABLE); |
| 696 | gc->_gc_next &= ~NEXT_MASK_UNREACHABLE; |
| 697 | next = (PyGC_Head*)gc->_gc_next; |
| 698 | } |
Tim Peters | ea55c51 | 2019-10-18 20:59:14 -0500 | [diff] [blame] | 699 | validate_list(unreachable, collecting_set_unreachable_clear); |
Pablo Galindo | 466326d | 2019-10-13 16:48:59 +0100 | [diff] [blame] | 700 | } |
| 701 | |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 702 | /* A traversal callback for move_legacy_finalizer_reachable. */ |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 703 | static int |
| 704 | visit_move(PyObject *op, PyGC_Head *tolist) |
| 705 | { |
Hai Shi | 675d9a3 | 2020-04-15 02:11:20 +0800 | [diff] [blame] | 706 | if (_PyObject_IS_GC(op)) { |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 707 | PyGC_Head *gc = AS_GC(op); |
| 708 | if (gc_is_collecting(gc)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 709 | gc_list_move(gc, tolist); |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 710 | gc_clear_collecting(gc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 711 | } |
| 712 | } |
| 713 | return 0; |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | /* Move objects that are reachable from finalizers, from the unreachable set |
Tim Peters | f6b8045 | 2003-04-07 19:21:15 +0000 | [diff] [blame] | 717 | * into finalizers set. |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 718 | */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 719 | static void |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 720 | move_legacy_finalizer_reachable(PyGC_Head *finalizers) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 721 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 722 | traverseproc traverse; |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 723 | PyGC_Head *gc = GC_NEXT(finalizers); |
| 724 | for (; gc != finalizers; gc = GC_NEXT(gc)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 725 | /* Note that the finalizers list may grow during this. */ |
| 726 | traverse = Py_TYPE(FROM_GC(gc))->tp_traverse; |
| 727 | (void) traverse(FROM_GC(gc), |
| 728 | (visitproc)visit_move, |
| 729 | (void *)finalizers); |
| 730 | } |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 731 | } |
| 732 | |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 733 | /* Clear all weakrefs to unreachable objects, and if such a weakref has a |
| 734 | * callback, invoke it if necessary. Note that it's possible for such |
| 735 | * weakrefs to be outside the unreachable set -- indeed, those are precisely |
| 736 | * the weakrefs whose callbacks must be invoked. See gc_weakref.txt for |
| 737 | * overview & some details. Some weakrefs with callbacks may be reclaimed |
| 738 | * directly by this routine; the number reclaimed is the return value. Other |
| 739 | * weakrefs with callbacks may be moved into the `old` generation. Objects |
| 740 | * moved into `old` have gc_refs set to GC_REACHABLE; the objects remaining in |
| 741 | * unreachable are left at GC_TENTATIVELY_UNREACHABLE. When this returns, |
| 742 | * no object in `unreachable` is weakly referenced anymore. |
Tim Peters | 403a203 | 2003-11-20 21:21:46 +0000 | [diff] [blame] | 743 | */ |
| 744 | static int |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 745 | handle_weakrefs(PyGC_Head *unreachable, PyGC_Head *old) |
Tim Peters | 403a203 | 2003-11-20 21:21:46 +0000 | [diff] [blame] | 746 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 747 | PyGC_Head *gc; |
| 748 | PyObject *op; /* generally FROM_GC(gc) */ |
| 749 | PyWeakReference *wr; /* generally a cast of op */ |
| 750 | PyGC_Head wrcb_to_call; /* weakrefs with callbacks to call */ |
| 751 | PyGC_Head *next; |
| 752 | int num_freed = 0; |
Tim Peters | 403a203 | 2003-11-20 21:21:46 +0000 | [diff] [blame] | 753 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 754 | gc_list_init(&wrcb_to_call); |
Tim Peters | 403a203 | 2003-11-20 21:21:46 +0000 | [diff] [blame] | 755 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 756 | /* Clear all weakrefs to the objects in unreachable. If such a weakref |
| 757 | * also has a callback, move it into `wrcb_to_call` if the callback |
| 758 | * needs to be invoked. Note that we cannot invoke any callbacks until |
| 759 | * all weakrefs to unreachable objects are cleared, lest the callback |
| 760 | * resurrect an unreachable object via a still-active weakref. We |
| 761 | * make another pass over wrcb_to_call, invoking callbacks, after this |
| 762 | * pass completes. |
| 763 | */ |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 764 | for (gc = GC_NEXT(unreachable); gc != unreachable; gc = next) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 765 | PyWeakReference **wrlist; |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 766 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 767 | op = FROM_GC(gc); |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 768 | next = GC_NEXT(gc); |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 769 | |
Neil Schemenauer | bcda460 | 2019-09-30 10:06:45 -0700 | [diff] [blame] | 770 | if (PyWeakref_Check(op)) { |
| 771 | /* A weakref inside the unreachable set must be cleared. If we |
| 772 | * allow its callback to execute inside delete_garbage(), it |
| 773 | * could expose objects that have tp_clear already called on |
| 774 | * them. Or, it could resurrect unreachable objects. One way |
| 775 | * this can happen is if some container objects do not implement |
| 776 | * tp_traverse. Then, wr_object can be outside the unreachable |
| 777 | * set but can be deallocated as a result of breaking the |
| 778 | * reference cycle. If we don't clear the weakref, the callback |
| 779 | * will run and potentially cause a crash. See bpo-38006 for |
| 780 | * one example. |
| 781 | */ |
| 782 | _PyWeakref_ClearRef((PyWeakReference *)op); |
| 783 | } |
| 784 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 785 | if (! PyType_SUPPORTS_WEAKREFS(Py_TYPE(op))) |
| 786 | continue; |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 787 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 788 | /* It supports weakrefs. Does it have any? */ |
| 789 | wrlist = (PyWeakReference **) |
Victor Stinner | 38aefc5 | 2020-04-06 14:07:02 +0200 | [diff] [blame] | 790 | _PyObject_GET_WEAKREFS_LISTPTR(op); |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 791 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 792 | /* `op` may have some weakrefs. March over the list, clear |
| 793 | * all the weakrefs, and move the weakrefs with callbacks |
| 794 | * that must be called into wrcb_to_call. |
| 795 | */ |
| 796 | for (wr = *wrlist; wr != NULL; wr = *wrlist) { |
| 797 | PyGC_Head *wrasgc; /* AS_GC(wr) */ |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 798 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 799 | /* _PyWeakref_ClearRef clears the weakref but leaves |
| 800 | * the callback pointer intact. Obscure: it also |
| 801 | * changes *wrlist. |
| 802 | */ |
Victor Stinner | a4b2bc7 | 2018-10-26 18:00:13 +0200 | [diff] [blame] | 803 | _PyObject_ASSERT((PyObject *)wr, wr->wr_object == op); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 804 | _PyWeakref_ClearRef(wr); |
Victor Stinner | a4b2bc7 | 2018-10-26 18:00:13 +0200 | [diff] [blame] | 805 | _PyObject_ASSERT((PyObject *)wr, wr->wr_object == Py_None); |
| 806 | if (wr->wr_callback == NULL) { |
| 807 | /* no callback */ |
| 808 | continue; |
| 809 | } |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 810 | |
Victor Stinner | a4b2bc7 | 2018-10-26 18:00:13 +0200 | [diff] [blame] | 811 | /* Headache time. `op` is going away, and is weakly referenced by |
| 812 | * `wr`, which has a callback. Should the callback be invoked? If wr |
| 813 | * is also trash, no: |
| 814 | * |
| 815 | * 1. There's no need to call it. The object and the weakref are |
| 816 | * both going away, so it's legitimate to pretend the weakref is |
| 817 | * going away first. The user has to ensure a weakref outlives its |
| 818 | * referent if they want a guarantee that the wr callback will get |
| 819 | * invoked. |
| 820 | * |
| 821 | * 2. It may be catastrophic to call it. If the callback is also in |
| 822 | * cyclic trash (CT), then although the CT is unreachable from |
| 823 | * outside the current generation, CT may be reachable from the |
| 824 | * callback. Then the callback could resurrect insane objects. |
| 825 | * |
| 826 | * Since the callback is never needed and may be unsafe in this case, |
| 827 | * wr is simply left in the unreachable set. Note that because we |
| 828 | * already called _PyWeakref_ClearRef(wr), its callback will never |
| 829 | * trigger. |
| 830 | * |
| 831 | * OTOH, if wr isn't part of CT, we should invoke the callback: the |
| 832 | * weakref outlived the trash. Note that since wr isn't CT in this |
| 833 | * case, its callback can't be CT either -- wr acted as an external |
| 834 | * root to this generation, and therefore its callback did too. So |
| 835 | * nothing in CT is reachable from the callback either, so it's hard |
| 836 | * to imagine how calling it later could create a problem for us. wr |
| 837 | * is moved to wrcb_to_call in this case. |
| 838 | */ |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 839 | if (gc_is_collecting(AS_GC(wr))) { |
Neil Schemenauer | bcda460 | 2019-09-30 10:06:45 -0700 | [diff] [blame] | 840 | /* it should already have been cleared above */ |
| 841 | assert(wr->wr_object == Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 842 | continue; |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 843 | } |
Tim Peters | cc2a866 | 2004-10-31 22:12:43 +0000 | [diff] [blame] | 844 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 845 | /* Create a new reference so that wr can't go away |
| 846 | * before we can process it again. |
| 847 | */ |
| 848 | Py_INCREF(wr); |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 849 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 850 | /* Move wr to wrcb_to_call, for the next pass. */ |
| 851 | wrasgc = AS_GC(wr); |
| 852 | assert(wrasgc != next); /* wrasgc is reachable, but |
| 853 | next isn't, so they can't |
| 854 | be the same */ |
| 855 | gc_list_move(wrasgc, &wrcb_to_call); |
| 856 | } |
| 857 | } |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 858 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 859 | /* Invoke the callbacks we decided to honor. It's safe to invoke them |
| 860 | * because they can't reference unreachable objects. |
| 861 | */ |
| 862 | while (! gc_list_is_empty(&wrcb_to_call)) { |
| 863 | PyObject *temp; |
| 864 | PyObject *callback; |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 865 | |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 866 | gc = (PyGC_Head*)wrcb_to_call._gc_next; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 867 | op = FROM_GC(gc); |
Victor Stinner | a4b2bc7 | 2018-10-26 18:00:13 +0200 | [diff] [blame] | 868 | _PyObject_ASSERT(op, PyWeakref_Check(op)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 869 | wr = (PyWeakReference *)op; |
| 870 | callback = wr->wr_callback; |
Victor Stinner | a4b2bc7 | 2018-10-26 18:00:13 +0200 | [diff] [blame] | 871 | _PyObject_ASSERT(op, callback != NULL); |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 872 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 873 | /* copy-paste of weakrefobject.c's handle_callback() */ |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 874 | temp = PyObject_CallOneArg(callback, (PyObject *)wr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 875 | if (temp == NULL) |
| 876 | PyErr_WriteUnraisable(callback); |
| 877 | else |
| 878 | Py_DECREF(temp); |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 879 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 880 | /* Give up the reference we created in the first pass. When |
| 881 | * op's refcount hits 0 (which it may or may not do right now), |
| 882 | * op's tp_dealloc will decref op->wr_callback too. Note |
| 883 | * that the refcount probably will hit 0 now, and because this |
| 884 | * weakref was reachable to begin with, gc didn't already |
| 885 | * add it to its count of freed objects. Example: a reachable |
| 886 | * weak value dict maps some key to this reachable weakref. |
| 887 | * The callback removes this key->weakref mapping from the |
| 888 | * dict, leaving no other references to the weakref (excepting |
| 889 | * ours). |
| 890 | */ |
| 891 | Py_DECREF(op); |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 892 | if (wrcb_to_call._gc_next == (uintptr_t)gc) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 893 | /* object is still alive -- move it */ |
| 894 | gc_list_move(gc, old); |
| 895 | } |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 896 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 897 | ++num_freed; |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 898 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 899 | } |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 900 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 901 | return num_freed; |
Tim Peters | 403a203 | 2003-11-20 21:21:46 +0000 | [diff] [blame] | 902 | } |
| 903 | |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 904 | static void |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 905 | debug_cycle(const char *msg, PyObject *op) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 906 | { |
Victor Stinner | 499dfcf | 2011-03-21 13:26:24 +0100 | [diff] [blame] | 907 | PySys_FormatStderr("gc: %s <%s %p>\n", |
| 908 | msg, Py_TYPE(op)->tp_name, op); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 909 | } |
| 910 | |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 911 | /* Handle uncollectable garbage (cycles with tp_del slots, and stuff reachable |
Tim Peters | bf384c2 | 2003-04-06 00:11:39 +0000 | [diff] [blame] | 912 | * only from such cycles). |
Tim Peters | f6b8045 | 2003-04-07 19:21:15 +0000 | [diff] [blame] | 913 | * If DEBUG_SAVEALL, all objects in finalizers are appended to the module |
| 914 | * garbage list (a Python list), else only the objects in finalizers with |
| 915 | * __del__ methods are appended to garbage. All objects in finalizers are |
| 916 | * merged into the old list regardless. |
Tim Peters | bf384c2 | 2003-04-06 00:11:39 +0000 | [diff] [blame] | 917 | */ |
Serhiy Storchaka | 301e3cc | 2018-05-24 15:19:29 +0300 | [diff] [blame] | 918 | static void |
Victor Stinner | 2e96906 | 2019-11-20 01:49:32 +0100 | [diff] [blame] | 919 | handle_legacy_finalizers(PyThreadState *tstate, |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 920 | GCState *gcstate, |
Victor Stinner | 9db0324 | 2019-04-26 02:32:01 +0200 | [diff] [blame] | 921 | PyGC_Head *finalizers, PyGC_Head *old) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 922 | { |
Victor Stinner | 2e96906 | 2019-11-20 01:49:32 +0100 | [diff] [blame] | 923 | assert(!_PyErr_Occurred(tstate)); |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 924 | assert(gcstate->garbage != NULL); |
Victor Stinner | 9db0324 | 2019-04-26 02:32:01 +0200 | [diff] [blame] | 925 | |
| 926 | PyGC_Head *gc = GC_NEXT(finalizers); |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 927 | for (; gc != finalizers; gc = GC_NEXT(gc)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 928 | PyObject *op = FROM_GC(gc); |
Tim Peters | f6b8045 | 2003-04-07 19:21:15 +0000 | [diff] [blame] | 929 | |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 930 | if ((gcstate->debug & DEBUG_SAVEALL) || has_legacy_finalizer(op)) { |
| 931 | if (PyList_Append(gcstate->garbage, op) < 0) { |
Victor Stinner | 2e96906 | 2019-11-20 01:49:32 +0100 | [diff] [blame] | 932 | _PyErr_Clear(tstate); |
Serhiy Storchaka | 301e3cc | 2018-05-24 15:19:29 +0300 | [diff] [blame] | 933 | break; |
Serhiy Storchaka | c4653c9 | 2018-05-29 18:50:10 +0300 | [diff] [blame] | 934 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 935 | } |
| 936 | } |
Tim Peters | f6b8045 | 2003-04-07 19:21:15 +0000 | [diff] [blame] | 937 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 938 | gc_list_merge(finalizers, old); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 939 | } |
| 940 | |
Tim Peters | 5fbc7b1 | 2014-05-08 17:42:19 -0500 | [diff] [blame] | 941 | /* Run first-time finalizers (if any) on all the objects in collectable. |
| 942 | * Note that this may remove some (or even all) of the objects from the |
| 943 | * list, due to refcounts falling to 0. |
| 944 | */ |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 945 | static void |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 946 | finalize_garbage(PyThreadState *tstate, PyGC_Head *collectable) |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 947 | { |
| 948 | destructor finalize; |
Tim Peters | 5fbc7b1 | 2014-05-08 17:42:19 -0500 | [diff] [blame] | 949 | PyGC_Head seen; |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 950 | |
Tim Peters | 5fbc7b1 | 2014-05-08 17:42:19 -0500 | [diff] [blame] | 951 | /* While we're going through the loop, `finalize(op)` may cause op, or |
| 952 | * other objects, to be reclaimed via refcounts falling to zero. So |
| 953 | * there's little we can rely on about the structure of the input |
| 954 | * `collectable` list across iterations. For safety, we always take the |
| 955 | * first object in that list and move it to a temporary `seen` list. |
| 956 | * If objects vanish from the `collectable` and `seen` lists we don't |
| 957 | * care. |
| 958 | */ |
| 959 | gc_list_init(&seen); |
| 960 | |
| 961 | while (!gc_list_is_empty(collectable)) { |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 962 | PyGC_Head *gc = GC_NEXT(collectable); |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 963 | PyObject *op = FROM_GC(gc); |
Tim Peters | 5fbc7b1 | 2014-05-08 17:42:19 -0500 | [diff] [blame] | 964 | gc_list_move(gc, &seen); |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 965 | if (!_PyGCHead_FINALIZED(gc) && |
Tim Peters | 5fbc7b1 | 2014-05-08 17:42:19 -0500 | [diff] [blame] | 966 | (finalize = Py_TYPE(op)->tp_finalize) != NULL) { |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 967 | _PyGCHead_SET_FINALIZED(gc); |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 968 | Py_INCREF(op); |
| 969 | finalize(op); |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 970 | assert(!_PyErr_Occurred(tstate)); |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 971 | Py_DECREF(op); |
| 972 | } |
| 973 | } |
Tim Peters | 5fbc7b1 | 2014-05-08 17:42:19 -0500 | [diff] [blame] | 974 | gc_list_merge(&seen, collectable); |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 975 | } |
| 976 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 977 | /* Break reference cycles by clearing the containers involved. This is |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 978 | * tricky business as the lists can be changing and we don't know which |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 979 | * objects may be freed. It is possible I screwed something up here. |
| 980 | */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 981 | static void |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 982 | delete_garbage(PyThreadState *tstate, GCState *gcstate, |
Victor Stinner | 9db0324 | 2019-04-26 02:32:01 +0200 | [diff] [blame] | 983 | PyGC_Head *collectable, PyGC_Head *old) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 984 | { |
Victor Stinner | 2e96906 | 2019-11-20 01:49:32 +0100 | [diff] [blame] | 985 | assert(!_PyErr_Occurred(tstate)); |
Victor Stinner | 9db0324 | 2019-04-26 02:32:01 +0200 | [diff] [blame] | 986 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 987 | while (!gc_list_is_empty(collectable)) { |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 988 | PyGC_Head *gc = GC_NEXT(collectable); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 989 | PyObject *op = FROM_GC(gc); |
Tim Peters | 8839617 | 2002-06-30 17:56:40 +0000 | [diff] [blame] | 990 | |
Victor Stinner | a4b2bc7 | 2018-10-26 18:00:13 +0200 | [diff] [blame] | 991 | _PyObject_ASSERT_WITH_MSG(op, Py_REFCNT(op) > 0, |
| 992 | "refcount is too small"); |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 993 | |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 994 | if (gcstate->debug & DEBUG_SAVEALL) { |
| 995 | assert(gcstate->garbage != NULL); |
| 996 | if (PyList_Append(gcstate->garbage, op) < 0) { |
Victor Stinner | 2e96906 | 2019-11-20 01:49:32 +0100 | [diff] [blame] | 997 | _PyErr_Clear(tstate); |
Serhiy Storchaka | c4653c9 | 2018-05-29 18:50:10 +0300 | [diff] [blame] | 998 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 999 | } |
| 1000 | else { |
Victor Stinner | 9db0324 | 2019-04-26 02:32:01 +0200 | [diff] [blame] | 1001 | inquiry clear; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1002 | if ((clear = Py_TYPE(op)->tp_clear) != NULL) { |
| 1003 | Py_INCREF(op); |
Serhiy Storchaka | c4653c9 | 2018-05-29 18:50:10 +0300 | [diff] [blame] | 1004 | (void) clear(op); |
Victor Stinner | 2e96906 | 2019-11-20 01:49:32 +0100 | [diff] [blame] | 1005 | if (_PyErr_Occurred(tstate)) { |
Victor Stinner | 71c52e3 | 2019-05-27 08:57:14 +0200 | [diff] [blame] | 1006 | _PyErr_WriteUnraisableMsg("in tp_clear of", |
| 1007 | (PyObject*)Py_TYPE(op)); |
Serhiy Storchaka | c4653c9 | 2018-05-29 18:50:10 +0300 | [diff] [blame] | 1008 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1009 | Py_DECREF(op); |
| 1010 | } |
| 1011 | } |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 1012 | if (GC_NEXT(collectable) == gc) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1013 | /* object is still alive, move it, it may die later */ |
Pablo Galindo | 466326d | 2019-10-13 16:48:59 +0100 | [diff] [blame] | 1014 | gc_clear_collecting(gc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1015 | gc_list_move(gc, old); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1016 | } |
| 1017 | } |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 1020 | /* Clear all free lists |
| 1021 | * All free lists are cleared during the collection of the highest generation. |
| 1022 | * Allocated items in the free list may keep a pymalloc arena occupied. |
| 1023 | * Clearing the free lists may give back memory to the OS earlier. |
| 1024 | */ |
| 1025 | static void |
| 1026 | clear_freelists(void) |
| 1027 | { |
Victor Stinner | 69ac6e5 | 2020-06-04 23:38:36 +0200 | [diff] [blame] | 1028 | PyThreadState *tstate = _PyThreadState_GET(); |
Victor Stinner | 3744ed2 | 2020-06-05 01:39:24 +0200 | [diff] [blame] | 1029 | _PyFrame_ClearFreeList(tstate); |
Victor Stinner | 69ac6e5 | 2020-06-04 23:38:36 +0200 | [diff] [blame] | 1030 | _PyTuple_ClearFreeList(tstate); |
Victor Stinner | 2ba5937 | 2020-06-05 00:50:05 +0200 | [diff] [blame] | 1031 | _PyFloat_ClearFreeList(tstate); |
Victor Stinner | 88ec919 | 2020-06-05 02:05:41 +0200 | [diff] [blame] | 1032 | _PyList_ClearFreeList(tstate); |
Victor Stinner | ae00a5a | 2020-04-29 02:29:20 +0200 | [diff] [blame] | 1033 | _PyDict_ClearFreeList(); |
Victor Stinner | 78a02c2 | 2020-06-05 02:34:14 +0200 | [diff] [blame^] | 1034 | _PyAsyncGen_ClearFreeLists(tstate); |
Victor Stinner | ae00a5a | 2020-04-29 02:29:20 +0200 | [diff] [blame] | 1035 | _PyContext_ClearFreeList(); |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 1036 | } |
| 1037 | |
Pablo Galindo | 97f1267 | 2020-01-13 12:25:05 +0000 | [diff] [blame] | 1038 | // Show stats for objects in each generations |
Inada Naoki | bf8162c | 2019-08-02 16:25:29 +0900 | [diff] [blame] | 1039 | static void |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1040 | show_stats_each_generations(GCState *gcstate) |
Inada Naoki | bf8162c | 2019-08-02 16:25:29 +0900 | [diff] [blame] | 1041 | { |
| 1042 | char buf[100]; |
| 1043 | size_t pos = 0; |
| 1044 | |
| 1045 | for (int i = 0; i < NUM_GENERATIONS && pos < sizeof(buf); i++) { |
| 1046 | pos += PyOS_snprintf(buf+pos, sizeof(buf)-pos, |
| 1047 | " %"PY_FORMAT_SIZE_T"d", |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1048 | gc_list_size(GEN_HEAD(gcstate, i))); |
Inada Naoki | bf8162c | 2019-08-02 16:25:29 +0900 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | PySys_FormatStderr( |
| 1052 | "gc: objects in each generation:%s\n" |
| 1053 | "gc: objects in permanent generation: %zd\n", |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1054 | buf, gc_list_size(&gcstate->permanent_generation.head)); |
Inada Naoki | bf8162c | 2019-08-02 16:25:29 +0900 | [diff] [blame] | 1055 | } |
| 1056 | |
Pablo Galindo | 97f1267 | 2020-01-13 12:25:05 +0000 | [diff] [blame] | 1057 | /* Deduce which objects among "base" are unreachable from outside the list |
Pablo Galindo | 466326d | 2019-10-13 16:48:59 +0100 | [diff] [blame] | 1058 | and move them to 'unreachable'. The process consist in the following steps: |
| 1059 | |
| 1060 | 1. Copy all reference counts to a different field (gc_prev is used to hold |
| 1061 | this copy to save memory). |
| 1062 | 2. Traverse all objects in "base" and visit all referred objects using |
Pablo Galindo | 97f1267 | 2020-01-13 12:25:05 +0000 | [diff] [blame] | 1063 | "tp_traverse" and for every visited object, subtract 1 to the reference |
Pablo Galindo | 466326d | 2019-10-13 16:48:59 +0100 | [diff] [blame] | 1064 | count (the one that we copied in the previous step). After this step, all |
| 1065 | objects that can be reached directly from outside must have strictly positive |
| 1066 | reference count, while all unreachable objects must have a count of exactly 0. |
Pablo Galindo | 97f1267 | 2020-01-13 12:25:05 +0000 | [diff] [blame] | 1067 | 3. Identify all unreachable objects (the ones with 0 reference count) and move |
Pablo Galindo | 466326d | 2019-10-13 16:48:59 +0100 | [diff] [blame] | 1068 | them to the "unreachable" list. This step also needs to move back to "base" all |
| 1069 | objects that were initially marked as unreachable but are referred transitively |
| 1070 | by the reachable objects (the ones with strictly positive reference count). |
| 1071 | |
| 1072 | Contracts: |
| 1073 | |
| 1074 | * The "base" has to be a valid list with no mask set. |
| 1075 | |
| 1076 | * The "unreachable" list must be uninitialized (this function calls |
| 1077 | gc_list_init over 'unreachable'). |
Tim Peters | 95bfc8a | 2019-10-13 16:47:04 -0500 | [diff] [blame] | 1078 | |
Pablo Galindo | 466326d | 2019-10-13 16:48:59 +0100 | [diff] [blame] | 1079 | IMPORTANT: This function leaves 'unreachable' with the NEXT_MASK_UNREACHABLE |
| 1080 | flag set but it does not clear it to skip unnecessary iteration. Before the |
| 1081 | flag is cleared (for example, by using 'clear_unreachable_mask' function or |
| 1082 | by a call to 'move_legacy_finalizers'), the 'unreachable' list is not a normal |
| 1083 | list and we can not use most gc_list_* functions for it. */ |
| 1084 | static inline void |
| 1085 | deduce_unreachable(PyGC_Head *base, PyGC_Head *unreachable) { |
Tim Peters | ea55c51 | 2019-10-18 20:59:14 -0500 | [diff] [blame] | 1086 | validate_list(base, collecting_clear_unreachable_clear); |
Pablo Galindo | 466326d | 2019-10-13 16:48:59 +0100 | [diff] [blame] | 1087 | /* Using ob_refcnt and gc_refs, calculate which objects in the |
| 1088 | * container set are reachable from outside the set (i.e., have a |
| 1089 | * refcount greater than 0 when all the references within the |
| 1090 | * set are taken into account). |
| 1091 | */ |
| 1092 | update_refs(base); // gc_prev is used for gc_refs |
| 1093 | subtract_refs(base); |
| 1094 | |
| 1095 | /* Leave everything reachable from outside base in base, and move |
| 1096 | * everything else (in base) to unreachable. |
Pablo Galindo | 97f1267 | 2020-01-13 12:25:05 +0000 | [diff] [blame] | 1097 | * |
Pablo Galindo | 466326d | 2019-10-13 16:48:59 +0100 | [diff] [blame] | 1098 | * NOTE: This used to move the reachable objects into a reachable |
| 1099 | * set instead. But most things usually turn out to be reachable, |
Pablo Galindo | 97f1267 | 2020-01-13 12:25:05 +0000 | [diff] [blame] | 1100 | * so it's more efficient to move the unreachable things. It "sounds slick" |
| 1101 | * to move the unreachable objects, until you think about it - the reason it |
| 1102 | * pays isn't actually obvious. |
| 1103 | * |
| 1104 | * Suppose we create objects A, B, C in that order. They appear in the young |
| 1105 | * generation in the same order. If B points to A, and C to B, and C is |
| 1106 | * reachable from outside, then the adjusted refcounts will be 0, 0, and 1 |
| 1107 | * respectively. |
| 1108 | * |
| 1109 | * When move_unreachable finds A, A is moved to the unreachable list. The |
| 1110 | * same for B when it's first encountered. Then C is traversed, B is moved |
| 1111 | * _back_ to the reachable list. B is eventually traversed, and then A is |
| 1112 | * moved back to the reachable list. |
| 1113 | * |
| 1114 | * So instead of not moving at all, the reachable objects B and A are moved |
| 1115 | * twice each. Why is this a win? A straightforward algorithm to move the |
| 1116 | * reachable objects instead would move A, B, and C once each. |
| 1117 | * |
| 1118 | * The key is that this dance leaves the objects in order C, B, A - it's |
| 1119 | * reversed from the original order. On all _subsequent_ scans, none of |
| 1120 | * them will move. Since most objects aren't in cycles, this can save an |
| 1121 | * unbounded number of moves across an unbounded number of later collections. |
| 1122 | * It can cost more only the first time the chain is scanned. |
| 1123 | * |
| 1124 | * Drawback: move_unreachable is also used to find out what's still trash |
| 1125 | * after finalizers may resurrect objects. In _that_ case most unreachable |
| 1126 | * objects will remain unreachable, so it would be more efficient to move |
| 1127 | * the reachable objects instead. But this is a one-time cost, probably not |
| 1128 | * worth complicating the code to speed just a little. |
Pablo Galindo | 466326d | 2019-10-13 16:48:59 +0100 | [diff] [blame] | 1129 | */ |
| 1130 | gc_list_init(unreachable); |
| 1131 | move_unreachable(base, unreachable); // gc_prev is pointer again |
Tim Peters | ea55c51 | 2019-10-18 20:59:14 -0500 | [diff] [blame] | 1132 | validate_list(base, collecting_clear_unreachable_clear); |
| 1133 | validate_list(unreachable, collecting_set_unreachable_set); |
Pablo Galindo | 466326d | 2019-10-13 16:48:59 +0100 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | /* Handle objects that may have resurrected after a call to 'finalize_garbage', moving |
| 1137 | them to 'old_generation' and placing the rest on 'still_unreachable'. |
| 1138 | |
| 1139 | Contracts: |
| 1140 | * After this function 'unreachable' must not be used anymore and 'still_unreachable' |
| 1141 | will contain the objects that did not resurrect. |
| 1142 | |
| 1143 | * The "still_unreachable" list must be uninitialized (this function calls |
| 1144 | gc_list_init over 'still_unreachable'). |
| 1145 | |
| 1146 | IMPORTANT: After a call to this function, the 'still_unreachable' set will have the |
| 1147 | PREV_MARK_COLLECTING set, but the objects in this set are going to be removed so |
| 1148 | we can skip the expense of clearing the flag to avoid extra iteration. */ |
| 1149 | static inline void |
| 1150 | handle_resurrected_objects(PyGC_Head *unreachable, PyGC_Head* still_unreachable, |
| 1151 | PyGC_Head *old_generation) |
| 1152 | { |
| 1153 | // Remove the PREV_MASK_COLLECTING from unreachable |
| 1154 | // to prepare it for a new call to 'deduce_unreachable' |
| 1155 | gc_list_clear_collecting(unreachable); |
| 1156 | |
| 1157 | // After the call to deduce_unreachable, the 'still_unreachable' set will |
| 1158 | // have the PREV_MARK_COLLECTING set, but the objects are going to be |
| 1159 | // removed so we can skip the expense of clearing the flag. |
| 1160 | PyGC_Head* resurrected = unreachable; |
| 1161 | deduce_unreachable(resurrected, still_unreachable); |
| 1162 | clear_unreachable_mask(still_unreachable); |
| 1163 | |
| 1164 | // Move the resurrected objects to the old generation for future collection. |
| 1165 | gc_list_merge(resurrected, old_generation); |
| 1166 | } |
| 1167 | |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1168 | /* This is the main function. Read this to understand how the |
| 1169 | * collection process works. */ |
Neal Norwitz | 7b216c5 | 2006-03-04 20:01:53 +0000 | [diff] [blame] | 1170 | static Py_ssize_t |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1171 | collect(PyThreadState *tstate, int generation, |
Victor Stinner | 9db0324 | 2019-04-26 02:32:01 +0200 | [diff] [blame] | 1172 | Py_ssize_t *n_collected, Py_ssize_t *n_uncollectable, int nofail) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1173 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1174 | int i; |
| 1175 | Py_ssize_t m = 0; /* # objects collected */ |
| 1176 | Py_ssize_t n = 0; /* # unreachable objects that couldn't be collected */ |
| 1177 | PyGC_Head *young; /* the generation we are examining */ |
| 1178 | PyGC_Head *old; /* next older generation */ |
| 1179 | PyGC_Head unreachable; /* non-problematic unreachable trash */ |
| 1180 | PyGC_Head finalizers; /* objects with, & reachable from, __del__ */ |
| 1181 | PyGC_Head *gc; |
Victor Stinner | 7181dec | 2015-03-27 17:47:53 +0100 | [diff] [blame] | 1182 | _PyTime_t t1 = 0; /* initialize to prevent a compiler warning */ |
Victor Stinner | 7247407 | 2019-11-20 12:25:50 +0100 | [diff] [blame] | 1183 | GCState *gcstate = &tstate->interp->gc; |
Antoine Pitrou | 40f6b12 | 2014-05-24 19:21:53 +0200 | [diff] [blame] | 1184 | |
Victor Stinner | d8135e9 | 2020-05-06 18:25:06 +0200 | [diff] [blame] | 1185 | #ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS |
| 1186 | if (tstate->interp->config._isolated_interpreter) { |
| 1187 | // bpo-40533: The garbage collector must not be run on parallel on |
| 1188 | // Python objects shared by multiple interpreters. |
| 1189 | return 0; |
| 1190 | } |
| 1191 | #endif |
| 1192 | |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1193 | if (gcstate->debug & DEBUG_STATS) { |
Inada Naoki | bf8162c | 2019-08-02 16:25:29 +0900 | [diff] [blame] | 1194 | PySys_WriteStderr("gc: collecting generation %d...\n", generation); |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1195 | show_stats_each_generations(gcstate); |
Victor Stinner | 7181dec | 2015-03-27 17:47:53 +0100 | [diff] [blame] | 1196 | t1 = _PyTime_GetMonotonicClock(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1197 | } |
Neil Schemenauer | 2880ae5 | 2002-05-04 05:35:20 +0000 | [diff] [blame] | 1198 | |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 1199 | if (PyDTrace_GC_START_ENABLED()) |
| 1200 | PyDTrace_GC_START(generation); |
| 1201 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1202 | /* update collection and allocation counters */ |
| 1203 | if (generation+1 < NUM_GENERATIONS) |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1204 | gcstate->generations[generation+1].count += 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1205 | for (i = 0; i <= generation; i++) |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1206 | gcstate->generations[i].count = 0; |
Neil Schemenauer | 2880ae5 | 2002-05-04 05:35:20 +0000 | [diff] [blame] | 1207 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1208 | /* merge younger generations with one we are currently collecting */ |
| 1209 | for (i = 0; i < generation; i++) { |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1210 | gc_list_merge(GEN_HEAD(gcstate, i), GEN_HEAD(gcstate, generation)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1211 | } |
Neil Schemenauer | 2880ae5 | 2002-05-04 05:35:20 +0000 | [diff] [blame] | 1212 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1213 | /* handy references */ |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1214 | young = GEN_HEAD(gcstate, generation); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1215 | if (generation < NUM_GENERATIONS-1) |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1216 | old = GEN_HEAD(gcstate, generation+1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1217 | else |
| 1218 | old = young; |
Tim Peters | ea55c51 | 2019-10-18 20:59:14 -0500 | [diff] [blame] | 1219 | validate_list(old, collecting_clear_unreachable_clear); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1220 | |
Pablo Galindo | 466326d | 2019-10-13 16:48:59 +0100 | [diff] [blame] | 1221 | deduce_unreachable(young, &unreachable); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1222 | |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 1223 | untrack_tuples(young); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1224 | /* Move reachable objects to next generation. */ |
| 1225 | if (young != old) { |
| 1226 | if (generation == NUM_GENERATIONS - 2) { |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1227 | gcstate->long_lived_pending += gc_list_size(young); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1228 | } |
| 1229 | gc_list_merge(young, old); |
| 1230 | } |
| 1231 | else { |
Pablo Galindo | 97f1267 | 2020-01-13 12:25:05 +0000 | [diff] [blame] | 1232 | /* We only un-track dicts in full collections, to avoid quadratic |
Antoine Pitrou | e1ad3da | 2012-05-28 22:22:34 +0200 | [diff] [blame] | 1233 | dict build-up. See issue #14775. */ |
| 1234 | untrack_dicts(young); |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1235 | gcstate->long_lived_pending = 0; |
| 1236 | gcstate->long_lived_total = gc_list_size(young); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1237 | } |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1238 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1239 | /* All objects in unreachable are trash, but objects reachable from |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 1240 | * legacy finalizers (e.g. tp_del) can't safely be deleted. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1241 | */ |
| 1242 | gc_list_init(&finalizers); |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 1243 | // NEXT_MASK_UNREACHABLE is cleared here. |
| 1244 | // After move_legacy_finalizers(), unreachable is normal list. |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 1245 | move_legacy_finalizers(&unreachable, &finalizers); |
| 1246 | /* finalizers contains the unreachable objects with a legacy finalizer; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1247 | * unreachable objects reachable *from* those are also uncollectable, |
| 1248 | * and we move those into the finalizers list too. |
| 1249 | */ |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 1250 | move_legacy_finalizer_reachable(&finalizers); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1251 | |
Tim Peters | ea55c51 | 2019-10-18 20:59:14 -0500 | [diff] [blame] | 1252 | validate_list(&finalizers, collecting_clear_unreachable_clear); |
| 1253 | validate_list(&unreachable, collecting_set_unreachable_clear); |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 1254 | |
Tim Peters | ecbf35f | 2019-10-09 12:37:30 -0500 | [diff] [blame] | 1255 | /* Print debugging information. */ |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1256 | if (gcstate->debug & DEBUG_COLLECTABLE) { |
Tim Peters | ecbf35f | 2019-10-09 12:37:30 -0500 | [diff] [blame] | 1257 | for (gc = GC_NEXT(&unreachable); gc != &unreachable; gc = GC_NEXT(gc)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1258 | debug_cycle("collectable", FROM_GC(gc)); |
| 1259 | } |
| 1260 | } |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 1261 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1262 | /* Clear weakrefs and invoke callbacks as necessary. */ |
| 1263 | m += handle_weakrefs(&unreachable, old); |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 1264 | |
Tim Peters | ea55c51 | 2019-10-18 20:59:14 -0500 | [diff] [blame] | 1265 | validate_list(old, collecting_clear_unreachable_clear); |
| 1266 | validate_list(&unreachable, collecting_set_unreachable_clear); |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 1267 | |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 1268 | /* Call tp_finalize on objects which have one. */ |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1269 | finalize_garbage(tstate, &unreachable); |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 1270 | |
Pablo Galindo | 466326d | 2019-10-13 16:48:59 +0100 | [diff] [blame] | 1271 | /* Handle any objects that may have resurrected after the call |
| 1272 | * to 'finalize_garbage' and continue the collection with the |
| 1273 | * objects that are still unreachable */ |
| 1274 | PyGC_Head final_unreachable; |
| 1275 | handle_resurrected_objects(&unreachable, &final_unreachable, old); |
| 1276 | |
| 1277 | /* Call tp_clear on objects in the final_unreachable set. This will cause |
| 1278 | * the reference cycles to be broken. It may also cause some objects |
| 1279 | * in finalizers to be freed. |
| 1280 | */ |
| 1281 | m += gc_list_size(&final_unreachable); |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1282 | delete_garbage(tstate, gcstate, &final_unreachable, old); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1283 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1284 | /* Collect statistics on uncollectable objects found and print |
| 1285 | * debugging information. */ |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 1286 | for (gc = GC_NEXT(&finalizers); gc != &finalizers; gc = GC_NEXT(gc)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1287 | n++; |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1288 | if (gcstate->debug & DEBUG_UNCOLLECTABLE) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1289 | debug_cycle("uncollectable", FROM_GC(gc)); |
| 1290 | } |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1291 | if (gcstate->debug & DEBUG_STATS) { |
Inada Naoki | bf8162c | 2019-08-02 16:25:29 +0900 | [diff] [blame] | 1292 | double d = _PyTime_AsSecondsDouble(_PyTime_GetMonotonicClock() - t1); |
Inada Naoki | 013e52f | 2019-08-31 09:13:42 +0900 | [diff] [blame] | 1293 | PySys_WriteStderr( |
| 1294 | "gc: done, %" PY_FORMAT_SIZE_T "d unreachable, " |
| 1295 | "%" PY_FORMAT_SIZE_T "d uncollectable, %.4fs elapsed\n", |
Inada Naoki | bf8162c | 2019-08-02 16:25:29 +0900 | [diff] [blame] | 1296 | n+m, n, d); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1297 | } |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1298 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1299 | /* Append instances in the uncollectable set to a Python |
| 1300 | * reachable list of garbage. The programmer has to deal with |
| 1301 | * this if they insist on creating this type of structure. |
| 1302 | */ |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1303 | handle_legacy_finalizers(tstate, gcstate, &finalizers, old); |
Tim Peters | ea55c51 | 2019-10-18 20:59:14 -0500 | [diff] [blame] | 1304 | validate_list(old, collecting_clear_unreachable_clear); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1305 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1306 | /* Clear free list only during the collection of the highest |
| 1307 | * generation */ |
| 1308 | if (generation == NUM_GENERATIONS-1) { |
| 1309 | clear_freelists(); |
| 1310 | } |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 1311 | |
Victor Stinner | 2e96906 | 2019-11-20 01:49:32 +0100 | [diff] [blame] | 1312 | if (_PyErr_Occurred(tstate)) { |
Antoine Pitrou | fef34e3 | 2013-05-19 01:11:58 +0200 | [diff] [blame] | 1313 | if (nofail) { |
Victor Stinner | 2e96906 | 2019-11-20 01:49:32 +0100 | [diff] [blame] | 1314 | _PyErr_Clear(tstate); |
Antoine Pitrou | fef34e3 | 2013-05-19 01:11:58 +0200 | [diff] [blame] | 1315 | } |
| 1316 | else { |
Victor Stinner | 656c45e | 2020-01-24 18:05:24 +0100 | [diff] [blame] | 1317 | _PyErr_WriteUnraisableMsg("in garbage collection", NULL); |
Antoine Pitrou | fef34e3 | 2013-05-19 01:11:58 +0200 | [diff] [blame] | 1318 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1319 | } |
Kristján Valur Jónsson | 69c6352 | 2012-04-15 11:41:32 +0000 | [diff] [blame] | 1320 | |
Antoine Pitrou | d4156c1 | 2012-10-30 22:43:19 +0100 | [diff] [blame] | 1321 | /* Update stats */ |
Victor Stinner | 9db0324 | 2019-04-26 02:32:01 +0200 | [diff] [blame] | 1322 | if (n_collected) { |
Kristján Valur Jónsson | 69c6352 | 2012-04-15 11:41:32 +0000 | [diff] [blame] | 1323 | *n_collected = m; |
Victor Stinner | 9db0324 | 2019-04-26 02:32:01 +0200 | [diff] [blame] | 1324 | } |
| 1325 | if (n_uncollectable) { |
Kristján Valur Jónsson | 69c6352 | 2012-04-15 11:41:32 +0000 | [diff] [blame] | 1326 | *n_uncollectable = n; |
Victor Stinner | 9db0324 | 2019-04-26 02:32:01 +0200 | [diff] [blame] | 1327 | } |
| 1328 | |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1329 | struct gc_generation_stats *stats = &gcstate->generation_stats[generation]; |
Antoine Pitrou | d4156c1 | 2012-10-30 22:43:19 +0100 | [diff] [blame] | 1330 | stats->collections++; |
| 1331 | stats->collected += m; |
| 1332 | stats->uncollectable += n; |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 1333 | |
Victor Stinner | 9db0324 | 2019-04-26 02:32:01 +0200 | [diff] [blame] | 1334 | if (PyDTrace_GC_DONE_ENABLED()) { |
Victor Stinner | 2e96906 | 2019-11-20 01:49:32 +0100 | [diff] [blame] | 1335 | PyDTrace_GC_DONE(n + m); |
Victor Stinner | 9db0324 | 2019-04-26 02:32:01 +0200 | [diff] [blame] | 1336 | } |
Łukasz Langa | a785c87 | 2016-09-09 17:37:37 -0700 | [diff] [blame] | 1337 | |
Victor Stinner | 2e96906 | 2019-11-20 01:49:32 +0100 | [diff] [blame] | 1338 | assert(!_PyErr_Occurred(tstate)); |
| 1339 | return n + m; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1340 | } |
| 1341 | |
Kristján Valur Jónsson | 69c6352 | 2012-04-15 11:41:32 +0000 | [diff] [blame] | 1342 | /* Invoke progress callbacks to notify clients that garbage collection |
| 1343 | * is starting or stopping |
| 1344 | */ |
| 1345 | static void |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1346 | invoke_gc_callback(PyThreadState *tstate, const char *phase, |
Victor Stinner | 9db0324 | 2019-04-26 02:32:01 +0200 | [diff] [blame] | 1347 | int generation, Py_ssize_t collected, |
| 1348 | Py_ssize_t uncollectable) |
Kristján Valur Jónsson | 69c6352 | 2012-04-15 11:41:32 +0000 | [diff] [blame] | 1349 | { |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1350 | assert(!_PyErr_Occurred(tstate)); |
Victor Stinner | 9db0324 | 2019-04-26 02:32:01 +0200 | [diff] [blame] | 1351 | |
Kristján Valur Jónsson | 69c6352 | 2012-04-15 11:41:32 +0000 | [diff] [blame] | 1352 | /* we may get called very early */ |
Victor Stinner | 7247407 | 2019-11-20 12:25:50 +0100 | [diff] [blame] | 1353 | GCState *gcstate = &tstate->interp->gc; |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1354 | if (gcstate->callbacks == NULL) { |
Kristján Valur Jónsson | 69c6352 | 2012-04-15 11:41:32 +0000 | [diff] [blame] | 1355 | return; |
Victor Stinner | 9db0324 | 2019-04-26 02:32:01 +0200 | [diff] [blame] | 1356 | } |
| 1357 | |
Kristján Valur Jónsson | 69c6352 | 2012-04-15 11:41:32 +0000 | [diff] [blame] | 1358 | /* The local variable cannot be rebound, check it for sanity */ |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1359 | assert(PyList_CheckExact(gcstate->callbacks)); |
Victor Stinner | 9db0324 | 2019-04-26 02:32:01 +0200 | [diff] [blame] | 1360 | PyObject *info = NULL; |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1361 | if (PyList_GET_SIZE(gcstate->callbacks) != 0) { |
Kristján Valur Jónsson | 69c6352 | 2012-04-15 11:41:32 +0000 | [diff] [blame] | 1362 | info = Py_BuildValue("{sisnsn}", |
| 1363 | "generation", generation, |
| 1364 | "collected", collected, |
| 1365 | "uncollectable", uncollectable); |
| 1366 | if (info == NULL) { |
| 1367 | PyErr_WriteUnraisable(NULL); |
| 1368 | return; |
| 1369 | } |
| 1370 | } |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1371 | for (Py_ssize_t i=0; i<PyList_GET_SIZE(gcstate->callbacks); i++) { |
| 1372 | PyObject *r, *cb = PyList_GET_ITEM(gcstate->callbacks, i); |
Kristján Valur Jónsson | 69c6352 | 2012-04-15 11:41:32 +0000 | [diff] [blame] | 1373 | Py_INCREF(cb); /* make sure cb doesn't go away */ |
| 1374 | r = PyObject_CallFunction(cb, "sO", phase, info); |
Serhiy Storchaka | 301e3cc | 2018-05-24 15:19:29 +0300 | [diff] [blame] | 1375 | if (r == NULL) { |
Kristján Valur Jónsson | 69c6352 | 2012-04-15 11:41:32 +0000 | [diff] [blame] | 1376 | PyErr_WriteUnraisable(cb); |
Serhiy Storchaka | 301e3cc | 2018-05-24 15:19:29 +0300 | [diff] [blame] | 1377 | } |
| 1378 | else { |
| 1379 | Py_DECREF(r); |
| 1380 | } |
Kristján Valur Jónsson | 69c6352 | 2012-04-15 11:41:32 +0000 | [diff] [blame] | 1381 | Py_DECREF(cb); |
| 1382 | } |
| 1383 | Py_XDECREF(info); |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1384 | assert(!_PyErr_Occurred(tstate)); |
Kristján Valur Jónsson | 69c6352 | 2012-04-15 11:41:32 +0000 | [diff] [blame] | 1385 | } |
| 1386 | |
| 1387 | /* Perform garbage collection of a generation and invoke |
| 1388 | * progress callbacks. |
| 1389 | */ |
| 1390 | static Py_ssize_t |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1391 | collect_with_callback(PyThreadState *tstate, int generation) |
Kristján Valur Jónsson | 69c6352 | 2012-04-15 11:41:32 +0000 | [diff] [blame] | 1392 | { |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1393 | assert(!_PyErr_Occurred(tstate)); |
Victor Stinner | 9db0324 | 2019-04-26 02:32:01 +0200 | [diff] [blame] | 1394 | Py_ssize_t result, collected, uncollectable; |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1395 | invoke_gc_callback(tstate, "start", generation, 0, 0); |
| 1396 | result = collect(tstate, generation, &collected, &uncollectable, 0); |
| 1397 | invoke_gc_callback(tstate, "stop", generation, collected, uncollectable); |
| 1398 | assert(!_PyErr_Occurred(tstate)); |
Kristján Valur Jónsson | 69c6352 | 2012-04-15 11:41:32 +0000 | [diff] [blame] | 1399 | return result; |
| 1400 | } |
| 1401 | |
Neal Norwitz | 7b216c5 | 2006-03-04 20:01:53 +0000 | [diff] [blame] | 1402 | static Py_ssize_t |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1403 | collect_generations(PyThreadState *tstate) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1404 | { |
Victor Stinner | 7247407 | 2019-11-20 12:25:50 +0100 | [diff] [blame] | 1405 | GCState *gcstate = &tstate->interp->gc; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1406 | /* Find the oldest generation (highest numbered) where the count |
| 1407 | * exceeds the threshold. Objects in the that generation and |
| 1408 | * generations younger than it will be collected. */ |
Victor Stinner | 9db0324 | 2019-04-26 02:32:01 +0200 | [diff] [blame] | 1409 | Py_ssize_t n = 0; |
| 1410 | for (int i = NUM_GENERATIONS-1; i >= 0; i--) { |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1411 | if (gcstate->generations[i].count > gcstate->generations[i].threshold) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1412 | /* Avoid quadratic performance degradation in number |
Pablo Galindo | 9091398 | 2019-12-27 21:55:56 +0000 | [diff] [blame] | 1413 | of tracked objects (see also issue #4074): |
| 1414 | |
| 1415 | To limit the cost of garbage collection, there are two strategies; |
| 1416 | - make each collection faster, e.g. by scanning fewer objects |
| 1417 | - do less collections |
| 1418 | This heuristic is about the latter strategy. |
| 1419 | |
| 1420 | In addition to the various configurable thresholds, we only trigger a |
| 1421 | full collection if the ratio |
| 1422 | |
| 1423 | long_lived_pending / long_lived_total |
| 1424 | |
| 1425 | is above a given value (hardwired to 25%). |
| 1426 | |
| 1427 | The reason is that, while "non-full" collections (i.e., collections of |
| 1428 | the young and middle generations) will always examine roughly the same |
| 1429 | number of objects -- determined by the aforementioned thresholds --, |
| 1430 | the cost of a full collection is proportional to the total number of |
| 1431 | long-lived objects, which is virtually unbounded. |
| 1432 | |
| 1433 | Indeed, it has been remarked that doing a full collection every |
| 1434 | <constant number> of object creations entails a dramatic performance |
| 1435 | degradation in workloads which consist in creating and storing lots of |
| 1436 | long-lived objects (e.g. building a large list of GC-tracked objects would |
| 1437 | show quadratic performance, instead of linear as expected: see issue #4074). |
| 1438 | |
| 1439 | Using the above ratio, instead, yields amortized linear performance in |
| 1440 | the total number of objects (the effect of which can be summarized |
| 1441 | thusly: "each full garbage collection is more and more costly as the |
| 1442 | number of objects grows, but we do fewer and fewer of them"). |
| 1443 | |
| 1444 | This heuristic was suggested by Martin von Löwis on python-dev in |
| 1445 | June 2008. His original analysis and proposal can be found at: |
| 1446 | http://mail.python.org/pipermail/python-dev/2008-June/080579.html |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1447 | */ |
| 1448 | if (i == NUM_GENERATIONS - 1 |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1449 | && gcstate->long_lived_pending < gcstate->long_lived_total / 4) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1450 | continue; |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1451 | n = collect_with_callback(tstate, i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1452 | break; |
| 1453 | } |
| 1454 | } |
| 1455 | return n; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1456 | } |
| 1457 | |
Serhiy Storchaka | 9326028 | 2017-02-04 11:19:59 +0200 | [diff] [blame] | 1458 | #include "clinic/gcmodule.c.h" |
| 1459 | |
| 1460 | /*[clinic input] |
| 1461 | gc.enable |
| 1462 | |
| 1463 | Enable automatic garbage collection. |
| 1464 | [clinic start generated code]*/ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1465 | |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 1466 | static PyObject * |
Serhiy Storchaka | 9326028 | 2017-02-04 11:19:59 +0200 | [diff] [blame] | 1467 | gc_enable_impl(PyObject *module) |
| 1468 | /*[clinic end generated code: output=45a427e9dce9155c input=81ac4940ca579707]*/ |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 1469 | { |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1470 | PyThreadState *tstate = _PyThreadState_GET(); |
Victor Stinner | 7247407 | 2019-11-20 12:25:50 +0100 | [diff] [blame] | 1471 | GCState *gcstate = &tstate->interp->gc; |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1472 | gcstate->enabled = 1; |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 1473 | Py_RETURN_NONE; |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 1474 | } |
| 1475 | |
Serhiy Storchaka | 9326028 | 2017-02-04 11:19:59 +0200 | [diff] [blame] | 1476 | /*[clinic input] |
| 1477 | gc.disable |
| 1478 | |
| 1479 | Disable automatic garbage collection. |
| 1480 | [clinic start generated code]*/ |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 1481 | |
| 1482 | static PyObject * |
Serhiy Storchaka | 9326028 | 2017-02-04 11:19:59 +0200 | [diff] [blame] | 1483 | gc_disable_impl(PyObject *module) |
| 1484 | /*[clinic end generated code: output=97d1030f7aa9d279 input=8c2e5a14e800d83b]*/ |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 1485 | { |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1486 | PyThreadState *tstate = _PyThreadState_GET(); |
Victor Stinner | 7247407 | 2019-11-20 12:25:50 +0100 | [diff] [blame] | 1487 | GCState *gcstate = &tstate->interp->gc; |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1488 | gcstate->enabled = 0; |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 1489 | Py_RETURN_NONE; |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 1490 | } |
| 1491 | |
Serhiy Storchaka | 9326028 | 2017-02-04 11:19:59 +0200 | [diff] [blame] | 1492 | /*[clinic input] |
| 1493 | gc.isenabled -> bool |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 1494 | |
Serhiy Storchaka | 9326028 | 2017-02-04 11:19:59 +0200 | [diff] [blame] | 1495 | Returns true if automatic garbage collection is enabled. |
| 1496 | [clinic start generated code]*/ |
| 1497 | |
| 1498 | static int |
| 1499 | gc_isenabled_impl(PyObject *module) |
| 1500 | /*[clinic end generated code: output=1874298331c49130 input=30005e0422373b31]*/ |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 1501 | { |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1502 | PyThreadState *tstate = _PyThreadState_GET(); |
Victor Stinner | 7247407 | 2019-11-20 12:25:50 +0100 | [diff] [blame] | 1503 | GCState *gcstate = &tstate->interp->gc; |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1504 | return gcstate->enabled; |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 1505 | } |
| 1506 | |
Serhiy Storchaka | 9326028 | 2017-02-04 11:19:59 +0200 | [diff] [blame] | 1507 | /*[clinic input] |
| 1508 | gc.collect -> Py_ssize_t |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1509 | |
Serhiy Storchaka | 9326028 | 2017-02-04 11:19:59 +0200 | [diff] [blame] | 1510 | generation: int(c_default="NUM_GENERATIONS - 1") = 2 |
| 1511 | |
| 1512 | Run the garbage collector. |
| 1513 | |
| 1514 | With no arguments, run a full collection. The optional argument |
| 1515 | may be an integer specifying which generation to collect. A ValueError |
| 1516 | is raised if the generation number is invalid. |
| 1517 | |
| 1518 | The number of unreachable objects is returned. |
| 1519 | [clinic start generated code]*/ |
| 1520 | |
| 1521 | static Py_ssize_t |
| 1522 | gc_collect_impl(PyObject *module, int generation) |
| 1523 | /*[clinic end generated code: output=b697e633043233c7 input=40720128b682d879]*/ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1524 | { |
Victor Stinner | 2e96906 | 2019-11-20 01:49:32 +0100 | [diff] [blame] | 1525 | PyThreadState *tstate = _PyThreadState_GET(); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1526 | |
Serhiy Storchaka | 9326028 | 2017-02-04 11:19:59 +0200 | [diff] [blame] | 1527 | if (generation < 0 || generation >= NUM_GENERATIONS) { |
Victor Stinner | 2e96906 | 2019-11-20 01:49:32 +0100 | [diff] [blame] | 1528 | _PyErr_SetString(tstate, PyExc_ValueError, "invalid generation"); |
Serhiy Storchaka | 9326028 | 2017-02-04 11:19:59 +0200 | [diff] [blame] | 1529 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1530 | } |
Barry Warsaw | d3c38ff | 2006-03-07 09:46:03 +0000 | [diff] [blame] | 1531 | |
Victor Stinner | 7247407 | 2019-11-20 12:25:50 +0100 | [diff] [blame] | 1532 | GCState *gcstate = &tstate->interp->gc; |
Victor Stinner | 9db0324 | 2019-04-26 02:32:01 +0200 | [diff] [blame] | 1533 | Py_ssize_t n; |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1534 | if (gcstate->collecting) { |
Victor Stinner | 9db0324 | 2019-04-26 02:32:01 +0200 | [diff] [blame] | 1535 | /* already collecting, don't do anything */ |
| 1536 | n = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1537 | } |
Victor Stinner | 9db0324 | 2019-04-26 02:32:01 +0200 | [diff] [blame] | 1538 | else { |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1539 | gcstate->collecting = 1; |
| 1540 | n = collect_with_callback(tstate, generation); |
| 1541 | gcstate->collecting = 0; |
Victor Stinner | 9db0324 | 2019-04-26 02:32:01 +0200 | [diff] [blame] | 1542 | } |
Serhiy Storchaka | 9326028 | 2017-02-04 11:19:59 +0200 | [diff] [blame] | 1543 | return n; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1544 | } |
| 1545 | |
Serhiy Storchaka | 9326028 | 2017-02-04 11:19:59 +0200 | [diff] [blame] | 1546 | /*[clinic input] |
| 1547 | gc.set_debug |
| 1548 | |
| 1549 | flags: int |
| 1550 | An integer that can have the following bits turned on: |
| 1551 | DEBUG_STATS - Print statistics during collection. |
| 1552 | DEBUG_COLLECTABLE - Print collectable objects found. |
| 1553 | DEBUG_UNCOLLECTABLE - Print unreachable but uncollectable objects |
| 1554 | found. |
| 1555 | DEBUG_SAVEALL - Save objects to gc.garbage rather than freeing them. |
| 1556 | DEBUG_LEAK - Debug leaking programs (everything but STATS). |
| 1557 | / |
| 1558 | |
| 1559 | Set the garbage collection debugging flags. |
| 1560 | |
| 1561 | Debugging information is written to sys.stderr. |
| 1562 | [clinic start generated code]*/ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1563 | |
| 1564 | static PyObject * |
Serhiy Storchaka | 9326028 | 2017-02-04 11:19:59 +0200 | [diff] [blame] | 1565 | gc_set_debug_impl(PyObject *module, int flags) |
| 1566 | /*[clinic end generated code: output=7c8366575486b228 input=5e5ce15e84fbed15]*/ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1567 | { |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1568 | PyThreadState *tstate = _PyThreadState_GET(); |
Victor Stinner | 7247407 | 2019-11-20 12:25:50 +0100 | [diff] [blame] | 1569 | GCState *gcstate = &tstate->interp->gc; |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1570 | gcstate->debug = flags; |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 1571 | Py_RETURN_NONE; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1572 | } |
| 1573 | |
Serhiy Storchaka | 9326028 | 2017-02-04 11:19:59 +0200 | [diff] [blame] | 1574 | /*[clinic input] |
| 1575 | gc.get_debug -> int |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1576 | |
Serhiy Storchaka | 9326028 | 2017-02-04 11:19:59 +0200 | [diff] [blame] | 1577 | Get the garbage collection debugging flags. |
| 1578 | [clinic start generated code]*/ |
| 1579 | |
| 1580 | static int |
| 1581 | gc_get_debug_impl(PyObject *module) |
| 1582 | /*[clinic end generated code: output=91242f3506cd1e50 input=91a101e1c3b98366]*/ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1583 | { |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1584 | PyThreadState *tstate = _PyThreadState_GET(); |
Victor Stinner | 7247407 | 2019-11-20 12:25:50 +0100 | [diff] [blame] | 1585 | GCState *gcstate = &tstate->interp->gc; |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1586 | return gcstate->debug; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1587 | } |
| 1588 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1589 | PyDoc_STRVAR(gc_set_thresh__doc__, |
Neal Norwitz | 2a47c0f | 2002-01-29 00:53:41 +0000 | [diff] [blame] | 1590 | "set_threshold(threshold0, [threshold1, threshold2]) -> None\n" |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1591 | "\n" |
| 1592 | "Sets the collection thresholds. Setting threshold0 to zero disables\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1593 | "collection.\n"); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1594 | |
| 1595 | static PyObject * |
Victor Stinner | 9db0324 | 2019-04-26 02:32:01 +0200 | [diff] [blame] | 1596 | gc_set_threshold(PyObject *self, PyObject *args) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1597 | { |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1598 | PyThreadState *tstate = _PyThreadState_GET(); |
Victor Stinner | 7247407 | 2019-11-20 12:25:50 +0100 | [diff] [blame] | 1599 | GCState *gcstate = &tstate->interp->gc; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1600 | if (!PyArg_ParseTuple(args, "i|ii:set_threshold", |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1601 | &gcstate->generations[0].threshold, |
| 1602 | &gcstate->generations[1].threshold, |
| 1603 | &gcstate->generations[2].threshold)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1604 | return NULL; |
Victor Stinner | 9db0324 | 2019-04-26 02:32:01 +0200 | [diff] [blame] | 1605 | for (int i = 3; i < NUM_GENERATIONS; i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1606 | /* generations higher than 2 get the same threshold */ |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1607 | gcstate->generations[i].threshold = gcstate->generations[2].threshold; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1608 | } |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 1609 | Py_RETURN_NONE; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1610 | } |
| 1611 | |
Serhiy Storchaka | 9326028 | 2017-02-04 11:19:59 +0200 | [diff] [blame] | 1612 | /*[clinic input] |
| 1613 | gc.get_threshold |
| 1614 | |
| 1615 | Return the current collection thresholds. |
| 1616 | [clinic start generated code]*/ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1617 | |
| 1618 | static PyObject * |
Serhiy Storchaka | 9326028 | 2017-02-04 11:19:59 +0200 | [diff] [blame] | 1619 | gc_get_threshold_impl(PyObject *module) |
| 1620 | /*[clinic end generated code: output=7902bc9f41ecbbd8 input=286d79918034d6e6]*/ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1621 | { |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1622 | PyThreadState *tstate = _PyThreadState_GET(); |
Victor Stinner | 7247407 | 2019-11-20 12:25:50 +0100 | [diff] [blame] | 1623 | GCState *gcstate = &tstate->interp->gc; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1624 | return Py_BuildValue("(iii)", |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1625 | gcstate->generations[0].threshold, |
| 1626 | gcstate->generations[1].threshold, |
| 1627 | gcstate->generations[2].threshold); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1628 | } |
| 1629 | |
Serhiy Storchaka | 9326028 | 2017-02-04 11:19:59 +0200 | [diff] [blame] | 1630 | /*[clinic input] |
| 1631 | gc.get_count |
| 1632 | |
| 1633 | Return a three-tuple of the current collection counts. |
| 1634 | [clinic start generated code]*/ |
Barry Warsaw | d3c38ff | 2006-03-07 09:46:03 +0000 | [diff] [blame] | 1635 | |
| 1636 | static PyObject * |
Serhiy Storchaka | 9326028 | 2017-02-04 11:19:59 +0200 | [diff] [blame] | 1637 | gc_get_count_impl(PyObject *module) |
| 1638 | /*[clinic end generated code: output=354012e67b16398f input=a392794a08251751]*/ |
Barry Warsaw | d3c38ff | 2006-03-07 09:46:03 +0000 | [diff] [blame] | 1639 | { |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1640 | PyThreadState *tstate = _PyThreadState_GET(); |
Victor Stinner | 7247407 | 2019-11-20 12:25:50 +0100 | [diff] [blame] | 1641 | GCState *gcstate = &tstate->interp->gc; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1642 | return Py_BuildValue("(iii)", |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1643 | gcstate->generations[0].count, |
| 1644 | gcstate->generations[1].count, |
| 1645 | gcstate->generations[2].count); |
Barry Warsaw | d3c38ff | 2006-03-07 09:46:03 +0000 | [diff] [blame] | 1646 | } |
| 1647 | |
Neil Schemenauer | 48c7034 | 2001-08-09 15:38:31 +0000 | [diff] [blame] | 1648 | static int |
Martin v. Löwis | 560da62 | 2001-11-24 09:24:51 +0000 | [diff] [blame] | 1649 | referrersvisit(PyObject* obj, PyObject *objs) |
Neil Schemenauer | 48c7034 | 2001-08-09 15:38:31 +0000 | [diff] [blame] | 1650 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1651 | Py_ssize_t i; |
| 1652 | for (i = 0; i < PyTuple_GET_SIZE(objs); i++) |
| 1653 | if (PyTuple_GET_ITEM(objs, i) == obj) |
| 1654 | return 1; |
| 1655 | return 0; |
Neil Schemenauer | 48c7034 | 2001-08-09 15:38:31 +0000 | [diff] [blame] | 1656 | } |
| 1657 | |
Neil Schemenauer | 17e7be6 | 2001-08-10 14:46:47 +0000 | [diff] [blame] | 1658 | static int |
Martin v. Löwis | 560da62 | 2001-11-24 09:24:51 +0000 | [diff] [blame] | 1659 | gc_referrers_for(PyObject *objs, PyGC_Head *list, PyObject *resultlist) |
Neil Schemenauer | 48c7034 | 2001-08-09 15:38:31 +0000 | [diff] [blame] | 1660 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1661 | PyGC_Head *gc; |
| 1662 | PyObject *obj; |
| 1663 | traverseproc traverse; |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 1664 | for (gc = GC_NEXT(list); gc != list; gc = GC_NEXT(gc)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1665 | obj = FROM_GC(gc); |
| 1666 | traverse = Py_TYPE(obj)->tp_traverse; |
| 1667 | if (obj == objs || obj == resultlist) |
| 1668 | continue; |
| 1669 | if (traverse(obj, (visitproc)referrersvisit, objs)) { |
| 1670 | if (PyList_Append(resultlist, obj) < 0) |
| 1671 | return 0; /* error */ |
| 1672 | } |
| 1673 | } |
| 1674 | return 1; /* no error */ |
Neil Schemenauer | 48c7034 | 2001-08-09 15:38:31 +0000 | [diff] [blame] | 1675 | } |
| 1676 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1677 | PyDoc_STRVAR(gc_get_referrers__doc__, |
Martin v. Löwis | 560da62 | 2001-11-24 09:24:51 +0000 | [diff] [blame] | 1678 | "get_referrers(*objs) -> list\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1679 | Return the list of objects that directly refer to any of objs."); |
Neil Schemenauer | 48c7034 | 2001-08-09 15:38:31 +0000 | [diff] [blame] | 1680 | |
Neil Schemenauer | 17e7be6 | 2001-08-10 14:46:47 +0000 | [diff] [blame] | 1681 | static PyObject * |
Martin v. Löwis | 560da62 | 2001-11-24 09:24:51 +0000 | [diff] [blame] | 1682 | gc_get_referrers(PyObject *self, PyObject *args) |
Neil Schemenauer | 48c7034 | 2001-08-09 15:38:31 +0000 | [diff] [blame] | 1683 | { |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1684 | PyThreadState *tstate = _PyThreadState_GET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1685 | int i; |
| 1686 | PyObject *result = PyList_New(0); |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1687 | if (!result) { |
| 1688 | return NULL; |
| 1689 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1690 | |
Victor Stinner | 7247407 | 2019-11-20 12:25:50 +0100 | [diff] [blame] | 1691 | GCState *gcstate = &tstate->interp->gc; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1692 | for (i = 0; i < NUM_GENERATIONS; i++) { |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1693 | if (!(gc_referrers_for(args, GEN_HEAD(gcstate, i), result))) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1694 | Py_DECREF(result); |
| 1695 | return NULL; |
| 1696 | } |
| 1697 | } |
| 1698 | return result; |
Neil Schemenauer | 48c7034 | 2001-08-09 15:38:31 +0000 | [diff] [blame] | 1699 | } |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1700 | |
Tim Peters | 0f81ab6 | 2003-04-08 16:39:48 +0000 | [diff] [blame] | 1701 | /* Append obj to list; return true if error (out of memory), false if OK. */ |
Jeremy Hylton | 5bd378b | 2003-04-03 16:28:38 +0000 | [diff] [blame] | 1702 | static int |
Tim Peters | 730f553 | 2003-04-08 17:17:17 +0000 | [diff] [blame] | 1703 | referentsvisit(PyObject *obj, PyObject *list) |
Jeremy Hylton | 5bd378b | 2003-04-03 16:28:38 +0000 | [diff] [blame] | 1704 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1705 | return PyList_Append(list, obj) < 0; |
Jeremy Hylton | 5bd378b | 2003-04-03 16:28:38 +0000 | [diff] [blame] | 1706 | } |
| 1707 | |
Tim Peters | 730f553 | 2003-04-08 17:17:17 +0000 | [diff] [blame] | 1708 | PyDoc_STRVAR(gc_get_referents__doc__, |
| 1709 | "get_referents(*objs) -> list\n\ |
Jeremy Hylton | 059b094 | 2003-04-03 16:29:13 +0000 | [diff] [blame] | 1710 | Return the list of objects that are directly referred to by objs."); |
Jeremy Hylton | 5bd378b | 2003-04-03 16:28:38 +0000 | [diff] [blame] | 1711 | |
| 1712 | static PyObject * |
Tim Peters | 730f553 | 2003-04-08 17:17:17 +0000 | [diff] [blame] | 1713 | gc_get_referents(PyObject *self, PyObject *args) |
Jeremy Hylton | 5bd378b | 2003-04-03 16:28:38 +0000 | [diff] [blame] | 1714 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1715 | Py_ssize_t i; |
| 1716 | PyObject *result = PyList_New(0); |
Tim Peters | 0f81ab6 | 2003-04-08 16:39:48 +0000 | [diff] [blame] | 1717 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1718 | if (result == NULL) |
| 1719 | return NULL; |
Tim Peters | 0f81ab6 | 2003-04-08 16:39:48 +0000 | [diff] [blame] | 1720 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1721 | for (i = 0; i < PyTuple_GET_SIZE(args); i++) { |
| 1722 | traverseproc traverse; |
| 1723 | PyObject *obj = PyTuple_GET_ITEM(args, i); |
Tim Peters | 0f81ab6 | 2003-04-08 16:39:48 +0000 | [diff] [blame] | 1724 | |
Hai Shi | 675d9a3 | 2020-04-15 02:11:20 +0800 | [diff] [blame] | 1725 | if (!_PyObject_IS_GC(obj)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1726 | continue; |
| 1727 | traverse = Py_TYPE(obj)->tp_traverse; |
| 1728 | if (! traverse) |
| 1729 | continue; |
| 1730 | if (traverse(obj, (visitproc)referentsvisit, result)) { |
| 1731 | Py_DECREF(result); |
| 1732 | return NULL; |
| 1733 | } |
| 1734 | } |
| 1735 | return result; |
Jeremy Hylton | 5bd378b | 2003-04-03 16:28:38 +0000 | [diff] [blame] | 1736 | } |
| 1737 | |
Serhiy Storchaka | 9326028 | 2017-02-04 11:19:59 +0200 | [diff] [blame] | 1738 | /*[clinic input] |
| 1739 | gc.get_objects |
Pablo Galindo | 175421b | 2019-02-23 03:02:06 +0000 | [diff] [blame] | 1740 | generation: Py_ssize_t(accept={int, NoneType}, c_default="-1") = None |
| 1741 | Generation to extract the objects from. |
Serhiy Storchaka | 9326028 | 2017-02-04 11:19:59 +0200 | [diff] [blame] | 1742 | |
| 1743 | Return a list of objects tracked by the collector (excluding the list returned). |
Pablo Galindo | 175421b | 2019-02-23 03:02:06 +0000 | [diff] [blame] | 1744 | |
| 1745 | If generation is not None, return only the objects tracked by the collector |
| 1746 | that are in that generation. |
Serhiy Storchaka | 9326028 | 2017-02-04 11:19:59 +0200 | [diff] [blame] | 1747 | [clinic start generated code]*/ |
Neil Schemenauer | c7c8d8e | 2001-08-09 15:58:59 +0000 | [diff] [blame] | 1748 | |
Neil Schemenauer | c7c8d8e | 2001-08-09 15:58:59 +0000 | [diff] [blame] | 1749 | static PyObject * |
Pablo Galindo | 175421b | 2019-02-23 03:02:06 +0000 | [diff] [blame] | 1750 | gc_get_objects_impl(PyObject *module, Py_ssize_t generation) |
| 1751 | /*[clinic end generated code: output=48b35fea4ba6cb0e input=ef7da9df9806754c]*/ |
Neil Schemenauer | c7c8d8e | 2001-08-09 15:58:59 +0000 | [diff] [blame] | 1752 | { |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1753 | PyThreadState *tstate = _PyThreadState_GET(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1754 | int i; |
| 1755 | PyObject* result; |
Victor Stinner | 7247407 | 2019-11-20 12:25:50 +0100 | [diff] [blame] | 1756 | GCState *gcstate = &tstate->interp->gc; |
Neil Schemenauer | c7c8d8e | 2001-08-09 15:58:59 +0000 | [diff] [blame] | 1757 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1758 | result = PyList_New(0); |
Pablo Galindo | 175421b | 2019-02-23 03:02:06 +0000 | [diff] [blame] | 1759 | if (result == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1760 | return NULL; |
Pablo Galindo | 175421b | 2019-02-23 03:02:06 +0000 | [diff] [blame] | 1761 | } |
| 1762 | |
| 1763 | /* If generation is passed, we extract only that generation */ |
Victor Stinner | 0810fa7 | 2019-04-15 17:54:09 +0200 | [diff] [blame] | 1764 | if (generation != -1) { |
Pablo Galindo | 175421b | 2019-02-23 03:02:06 +0000 | [diff] [blame] | 1765 | if (generation >= NUM_GENERATIONS) { |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1766 | _PyErr_Format(tstate, PyExc_ValueError, |
| 1767 | "generation parameter must be less than the number of " |
| 1768 | "available generations (%i)", |
| 1769 | NUM_GENERATIONS); |
Pablo Galindo | 175421b | 2019-02-23 03:02:06 +0000 | [diff] [blame] | 1770 | goto error; |
| 1771 | } |
| 1772 | |
| 1773 | if (generation < 0) { |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1774 | _PyErr_SetString(tstate, PyExc_ValueError, |
| 1775 | "generation parameter cannot be negative"); |
Pablo Galindo | 175421b | 2019-02-23 03:02:06 +0000 | [diff] [blame] | 1776 | goto error; |
| 1777 | } |
| 1778 | |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1779 | if (append_objects(result, GEN_HEAD(gcstate, generation))) { |
Pablo Galindo | 175421b | 2019-02-23 03:02:06 +0000 | [diff] [blame] | 1780 | goto error; |
| 1781 | } |
| 1782 | |
| 1783 | return result; |
| 1784 | } |
| 1785 | |
| 1786 | /* If generation is not passed or None, get all objects from all generations */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1787 | for (i = 0; i < NUM_GENERATIONS; i++) { |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1788 | if (append_objects(result, GEN_HEAD(gcstate, i))) { |
Pablo Galindo | 175421b | 2019-02-23 03:02:06 +0000 | [diff] [blame] | 1789 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1790 | } |
| 1791 | } |
| 1792 | return result; |
Pablo Galindo | 175421b | 2019-02-23 03:02:06 +0000 | [diff] [blame] | 1793 | |
| 1794 | error: |
| 1795 | Py_DECREF(result); |
| 1796 | return NULL; |
Neil Schemenauer | c7c8d8e | 2001-08-09 15:58:59 +0000 | [diff] [blame] | 1797 | } |
| 1798 | |
Serhiy Storchaka | 9326028 | 2017-02-04 11:19:59 +0200 | [diff] [blame] | 1799 | /*[clinic input] |
| 1800 | gc.get_stats |
| 1801 | |
| 1802 | Return a list of dictionaries containing per-generation statistics. |
| 1803 | [clinic start generated code]*/ |
Antoine Pitrou | d4156c1 | 2012-10-30 22:43:19 +0100 | [diff] [blame] | 1804 | |
| 1805 | static PyObject * |
Serhiy Storchaka | 9326028 | 2017-02-04 11:19:59 +0200 | [diff] [blame] | 1806 | gc_get_stats_impl(PyObject *module) |
| 1807 | /*[clinic end generated code: output=a8ab1d8a5d26f3ab input=1ef4ed9d17b1a470]*/ |
Antoine Pitrou | d4156c1 | 2012-10-30 22:43:19 +0100 | [diff] [blame] | 1808 | { |
| 1809 | int i; |
Antoine Pitrou | d4156c1 | 2012-10-30 22:43:19 +0100 | [diff] [blame] | 1810 | struct gc_generation_stats stats[NUM_GENERATIONS], *st; |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1811 | PyThreadState *tstate = _PyThreadState_GET(); |
Antoine Pitrou | d4156c1 | 2012-10-30 22:43:19 +0100 | [diff] [blame] | 1812 | |
| 1813 | /* To get consistent values despite allocations while constructing |
| 1814 | the result list, we use a snapshot of the running stats. */ |
Victor Stinner | 7247407 | 2019-11-20 12:25:50 +0100 | [diff] [blame] | 1815 | GCState *gcstate = &tstate->interp->gc; |
Antoine Pitrou | d4156c1 | 2012-10-30 22:43:19 +0100 | [diff] [blame] | 1816 | for (i = 0; i < NUM_GENERATIONS; i++) { |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1817 | stats[i] = gcstate->generation_stats[i]; |
Antoine Pitrou | d4156c1 | 2012-10-30 22:43:19 +0100 | [diff] [blame] | 1818 | } |
| 1819 | |
Victor Stinner | 9db0324 | 2019-04-26 02:32:01 +0200 | [diff] [blame] | 1820 | PyObject *result = PyList_New(0); |
Antoine Pitrou | d4156c1 | 2012-10-30 22:43:19 +0100 | [diff] [blame] | 1821 | if (result == NULL) |
| 1822 | return NULL; |
| 1823 | |
| 1824 | for (i = 0; i < NUM_GENERATIONS; i++) { |
| 1825 | PyObject *dict; |
| 1826 | st = &stats[i]; |
| 1827 | dict = Py_BuildValue("{snsnsn}", |
| 1828 | "collections", st->collections, |
| 1829 | "collected", st->collected, |
| 1830 | "uncollectable", st->uncollectable |
| 1831 | ); |
| 1832 | if (dict == NULL) |
| 1833 | goto error; |
| 1834 | if (PyList_Append(result, dict)) { |
| 1835 | Py_DECREF(dict); |
| 1836 | goto error; |
| 1837 | } |
| 1838 | Py_DECREF(dict); |
| 1839 | } |
| 1840 | return result; |
| 1841 | |
| 1842 | error: |
| 1843 | Py_XDECREF(result); |
| 1844 | return NULL; |
| 1845 | } |
| 1846 | |
| 1847 | |
Serhiy Storchaka | 9326028 | 2017-02-04 11:19:59 +0200 | [diff] [blame] | 1848 | /*[clinic input] |
| 1849 | gc.is_tracked |
| 1850 | |
| 1851 | obj: object |
| 1852 | / |
| 1853 | |
| 1854 | Returns true if the object is tracked by the garbage collector. |
| 1855 | |
| 1856 | Simple atomic objects will return false. |
| 1857 | [clinic start generated code]*/ |
Antoine Pitrou | 3a652b1 | 2009-03-23 18:52:06 +0000 | [diff] [blame] | 1858 | |
| 1859 | static PyObject * |
Serhiy Storchaka | 9326028 | 2017-02-04 11:19:59 +0200 | [diff] [blame] | 1860 | gc_is_tracked(PyObject *module, PyObject *obj) |
| 1861 | /*[clinic end generated code: output=14f0103423b28e31 input=d83057f170ea2723]*/ |
Antoine Pitrou | 3a652b1 | 2009-03-23 18:52:06 +0000 | [diff] [blame] | 1862 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1863 | PyObject *result; |
| 1864 | |
Hai Shi | 675d9a3 | 2020-04-15 02:11:20 +0800 | [diff] [blame] | 1865 | if (_PyObject_IS_GC(obj) && _PyObject_GC_IS_TRACKED(obj)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1866 | result = Py_True; |
| 1867 | else |
| 1868 | result = Py_False; |
| 1869 | Py_INCREF(result); |
| 1870 | return result; |
Antoine Pitrou | 3a652b1 | 2009-03-23 18:52:06 +0000 | [diff] [blame] | 1871 | } |
| 1872 | |
brainfvck | c75edab | 2017-10-16 12:49:41 -0700 | [diff] [blame] | 1873 | /*[clinic input] |
Pablo Galindo | a2ec3f0 | 2020-01-14 12:06:45 +0000 | [diff] [blame] | 1874 | gc.is_finalized |
| 1875 | |
| 1876 | obj: object |
| 1877 | / |
| 1878 | |
| 1879 | Returns true if the object has been already finalized by the GC. |
| 1880 | [clinic start generated code]*/ |
| 1881 | |
| 1882 | static PyObject * |
| 1883 | gc_is_finalized(PyObject *module, PyObject *obj) |
| 1884 | /*[clinic end generated code: output=e1516ac119a918ed input=201d0c58f69ae390]*/ |
| 1885 | { |
Hai Shi | 675d9a3 | 2020-04-15 02:11:20 +0800 | [diff] [blame] | 1886 | if (_PyObject_IS_GC(obj) && _PyGCHead_FINALIZED(AS_GC(obj))) { |
Pablo Galindo | a2ec3f0 | 2020-01-14 12:06:45 +0000 | [diff] [blame] | 1887 | Py_RETURN_TRUE; |
| 1888 | } |
| 1889 | Py_RETURN_FALSE; |
| 1890 | } |
| 1891 | |
| 1892 | /*[clinic input] |
brainfvck | c75edab | 2017-10-16 12:49:41 -0700 | [diff] [blame] | 1893 | gc.freeze |
| 1894 | |
| 1895 | Freeze all current tracked objects and ignore them for future collections. |
| 1896 | |
| 1897 | This can be used before a POSIX fork() call to make the gc copy-on-write friendly. |
| 1898 | Note: collection before a POSIX fork() call may free pages for future allocation |
| 1899 | which can cause copy-on-write. |
| 1900 | [clinic start generated code]*/ |
| 1901 | |
| 1902 | static PyObject * |
| 1903 | gc_freeze_impl(PyObject *module) |
| 1904 | /*[clinic end generated code: output=502159d9cdc4c139 input=b602b16ac5febbe5]*/ |
| 1905 | { |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1906 | PyThreadState *tstate = _PyThreadState_GET(); |
Victor Stinner | 7247407 | 2019-11-20 12:25:50 +0100 | [diff] [blame] | 1907 | GCState *gcstate = &tstate->interp->gc; |
brainfvck | c75edab | 2017-10-16 12:49:41 -0700 | [diff] [blame] | 1908 | for (int i = 0; i < NUM_GENERATIONS; ++i) { |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1909 | gc_list_merge(GEN_HEAD(gcstate, i), &gcstate->permanent_generation.head); |
| 1910 | gcstate->generations[i].count = 0; |
brainfvck | c75edab | 2017-10-16 12:49:41 -0700 | [diff] [blame] | 1911 | } |
| 1912 | Py_RETURN_NONE; |
| 1913 | } |
| 1914 | |
| 1915 | /*[clinic input] |
| 1916 | gc.unfreeze |
| 1917 | |
| 1918 | Unfreeze all objects in the permanent generation. |
| 1919 | |
| 1920 | Put all objects in the permanent generation back into oldest generation. |
| 1921 | [clinic start generated code]*/ |
| 1922 | |
| 1923 | static PyObject * |
| 1924 | gc_unfreeze_impl(PyObject *module) |
| 1925 | /*[clinic end generated code: output=1c15f2043b25e169 input=2dd52b170f4cef6c]*/ |
| 1926 | { |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1927 | PyThreadState *tstate = _PyThreadState_GET(); |
Victor Stinner | 7247407 | 2019-11-20 12:25:50 +0100 | [diff] [blame] | 1928 | GCState *gcstate = &tstate->interp->gc; |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1929 | gc_list_merge(&gcstate->permanent_generation.head, |
| 1930 | GEN_HEAD(gcstate, NUM_GENERATIONS-1)); |
brainfvck | c75edab | 2017-10-16 12:49:41 -0700 | [diff] [blame] | 1931 | Py_RETURN_NONE; |
| 1932 | } |
| 1933 | |
| 1934 | /*[clinic input] |
Victor Stinner | 05d68a8 | 2018-01-18 11:15:25 +0100 | [diff] [blame] | 1935 | gc.get_freeze_count -> Py_ssize_t |
brainfvck | c75edab | 2017-10-16 12:49:41 -0700 | [diff] [blame] | 1936 | |
| 1937 | Return the number of objects in the permanent generation. |
| 1938 | [clinic start generated code]*/ |
| 1939 | |
Victor Stinner | 05d68a8 | 2018-01-18 11:15:25 +0100 | [diff] [blame] | 1940 | static Py_ssize_t |
brainfvck | c75edab | 2017-10-16 12:49:41 -0700 | [diff] [blame] | 1941 | gc_get_freeze_count_impl(PyObject *module) |
Victor Stinner | 05d68a8 | 2018-01-18 11:15:25 +0100 | [diff] [blame] | 1942 | /*[clinic end generated code: output=61cbd9f43aa032e1 input=45ffbc65cfe2a6ed]*/ |
brainfvck | c75edab | 2017-10-16 12:49:41 -0700 | [diff] [blame] | 1943 | { |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1944 | PyThreadState *tstate = _PyThreadState_GET(); |
Victor Stinner | 7247407 | 2019-11-20 12:25:50 +0100 | [diff] [blame] | 1945 | GCState *gcstate = &tstate->interp->gc; |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 1946 | return gc_list_size(&gcstate->permanent_generation.head); |
brainfvck | c75edab | 2017-10-16 12:49:41 -0700 | [diff] [blame] | 1947 | } |
| 1948 | |
Neil Schemenauer | c7c8d8e | 2001-08-09 15:58:59 +0000 | [diff] [blame] | 1949 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1950 | PyDoc_STRVAR(gc__doc__, |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1951 | "This module provides access to the garbage collector for reference cycles.\n" |
| 1952 | "\n" |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 1953 | "enable() -- Enable automatic garbage collection.\n" |
| 1954 | "disable() -- Disable automatic garbage collection.\n" |
| 1955 | "isenabled() -- Returns true if automatic collection is enabled.\n" |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1956 | "collect() -- Do a full collection right now.\n" |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1957 | "get_count() -- Return the current collection counts.\n" |
R David Murray | 0e81463 | 2013-12-26 15:11:28 -0500 | [diff] [blame] | 1958 | "get_stats() -- Return list of dictionaries containing per-generation stats.\n" |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1959 | "set_debug() -- Set debugging flags.\n" |
| 1960 | "get_debug() -- Get debugging flags.\n" |
| 1961 | "set_threshold() -- Set the collection thresholds.\n" |
| 1962 | "get_threshold() -- Return the current the collection thresholds.\n" |
Neil Schemenauer | c7c8d8e | 2001-08-09 15:58:59 +0000 | [diff] [blame] | 1963 | "get_objects() -- Return a list of all objects tracked by the collector.\n" |
Antoine Pitrou | 3a652b1 | 2009-03-23 18:52:06 +0000 | [diff] [blame] | 1964 | "is_tracked() -- Returns true if a given object is tracked.\n" |
Pablo Galindo | b679137 | 2020-01-14 17:38:15 +0000 | [diff] [blame] | 1965 | "is_finalized() -- Returns true if a given object has been already finalized.\n" |
Jeremy Hylton | 5bd378b | 2003-04-03 16:28:38 +0000 | [diff] [blame] | 1966 | "get_referrers() -- Return the list of objects that refer to an object.\n" |
brainfvck | c75edab | 2017-10-16 12:49:41 -0700 | [diff] [blame] | 1967 | "get_referents() -- Return the list of objects that an object refers to.\n" |
| 1968 | "freeze() -- Freeze all tracked objects and ignore them for future collections.\n" |
| 1969 | "unfreeze() -- Unfreeze all objects in the permanent generation.\n" |
| 1970 | "get_freeze_count() -- Return the number of objects in the permanent generation.\n"); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1971 | |
| 1972 | static PyMethodDef GcMethods[] = { |
Serhiy Storchaka | 9326028 | 2017-02-04 11:19:59 +0200 | [diff] [blame] | 1973 | GC_ENABLE_METHODDEF |
| 1974 | GC_DISABLE_METHODDEF |
| 1975 | GC_ISENABLED_METHODDEF |
| 1976 | GC_SET_DEBUG_METHODDEF |
| 1977 | GC_GET_DEBUG_METHODDEF |
| 1978 | GC_GET_COUNT_METHODDEF |
Victor Stinner | 9db0324 | 2019-04-26 02:32:01 +0200 | [diff] [blame] | 1979 | {"set_threshold", gc_set_threshold, METH_VARARGS, gc_set_thresh__doc__}, |
Serhiy Storchaka | 9326028 | 2017-02-04 11:19:59 +0200 | [diff] [blame] | 1980 | GC_GET_THRESHOLD_METHODDEF |
| 1981 | GC_COLLECT_METHODDEF |
| 1982 | GC_GET_OBJECTS_METHODDEF |
| 1983 | GC_GET_STATS_METHODDEF |
| 1984 | GC_IS_TRACKED_METHODDEF |
Pablo Galindo | a2ec3f0 | 2020-01-14 12:06:45 +0000 | [diff] [blame] | 1985 | GC_IS_FINALIZED_METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1986 | {"get_referrers", gc_get_referrers, METH_VARARGS, |
| 1987 | gc_get_referrers__doc__}, |
| 1988 | {"get_referents", gc_get_referents, METH_VARARGS, |
| 1989 | gc_get_referents__doc__}, |
brainfvck | c75edab | 2017-10-16 12:49:41 -0700 | [diff] [blame] | 1990 | GC_FREEZE_METHODDEF |
| 1991 | GC_UNFREEZE_METHODDEF |
| 1992 | GC_GET_FREEZE_COUNT_METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1993 | {NULL, NULL} /* Sentinel */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1994 | }; |
| 1995 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1996 | static struct PyModuleDef gcmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1997 | PyModuleDef_HEAD_INIT, |
Antoine Pitrou | 696e035 | 2010-08-08 22:18:46 +0000 | [diff] [blame] | 1998 | "gc", /* m_name */ |
| 1999 | gc__doc__, /* m_doc */ |
| 2000 | -1, /* m_size */ |
| 2001 | GcMethods, /* m_methods */ |
| 2002 | NULL, /* m_reload */ |
| 2003 | NULL, /* m_traverse */ |
| 2004 | NULL, /* m_clear */ |
| 2005 | NULL /* m_free */ |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 2006 | }; |
| 2007 | |
Jason Tishler | 6bc06ec | 2003-09-04 11:59:50 +0000 | [diff] [blame] | 2008 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 2009 | PyInit_gc(void) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 2010 | { |
Victor Stinner | 7247407 | 2019-11-20 12:25:50 +0100 | [diff] [blame] | 2011 | PyThreadState *tstate = _PyThreadState_GET(); |
| 2012 | GCState *gcstate = &tstate->interp->gc; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 2013 | |
Victor Stinner | 7247407 | 2019-11-20 12:25:50 +0100 | [diff] [blame] | 2014 | PyObject *m = PyModule_Create(&gcmodule); |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 2015 | |
Victor Stinner | 9db0324 | 2019-04-26 02:32:01 +0200 | [diff] [blame] | 2016 | if (m == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2017 | return NULL; |
Victor Stinner | 9db0324 | 2019-04-26 02:32:01 +0200 | [diff] [blame] | 2018 | } |
Tim Peters | 1155887 | 2003-04-06 23:30:52 +0000 | [diff] [blame] | 2019 | |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 2020 | if (gcstate->garbage == NULL) { |
| 2021 | gcstate->garbage = PyList_New(0); |
Victor Stinner | 7247407 | 2019-11-20 12:25:50 +0100 | [diff] [blame] | 2022 | if (gcstate->garbage == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2023 | return NULL; |
Victor Stinner | 7247407 | 2019-11-20 12:25:50 +0100 | [diff] [blame] | 2024 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2025 | } |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 2026 | Py_INCREF(gcstate->garbage); |
Victor Stinner | 7247407 | 2019-11-20 12:25:50 +0100 | [diff] [blame] | 2027 | if (PyModule_AddObject(m, "garbage", gcstate->garbage) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2028 | return NULL; |
Victor Stinner | 7247407 | 2019-11-20 12:25:50 +0100 | [diff] [blame] | 2029 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2030 | |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 2031 | if (gcstate->callbacks == NULL) { |
| 2032 | gcstate->callbacks = PyList_New(0); |
Victor Stinner | 7247407 | 2019-11-20 12:25:50 +0100 | [diff] [blame] | 2033 | if (gcstate->callbacks == NULL) { |
Kristján Valur Jónsson | 69c6352 | 2012-04-15 11:41:32 +0000 | [diff] [blame] | 2034 | return NULL; |
Victor Stinner | 7247407 | 2019-11-20 12:25:50 +0100 | [diff] [blame] | 2035 | } |
Kristján Valur Jónsson | 69c6352 | 2012-04-15 11:41:32 +0000 | [diff] [blame] | 2036 | } |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 2037 | Py_INCREF(gcstate->callbacks); |
Victor Stinner | 7247407 | 2019-11-20 12:25:50 +0100 | [diff] [blame] | 2038 | if (PyModule_AddObject(m, "callbacks", gcstate->callbacks) < 0) { |
Kristján Valur Jónsson | 69c6352 | 2012-04-15 11:41:32 +0000 | [diff] [blame] | 2039 | return NULL; |
Victor Stinner | 7247407 | 2019-11-20 12:25:50 +0100 | [diff] [blame] | 2040 | } |
Kristján Valur Jónsson | 69c6352 | 2012-04-15 11:41:32 +0000 | [diff] [blame] | 2041 | |
Victor Stinner | 7247407 | 2019-11-20 12:25:50 +0100 | [diff] [blame] | 2042 | #define ADD_INT(NAME) if (PyModule_AddIntConstant(m, #NAME, NAME) < 0) { return NULL; } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2043 | ADD_INT(DEBUG_STATS); |
| 2044 | ADD_INT(DEBUG_COLLECTABLE); |
| 2045 | ADD_INT(DEBUG_UNCOLLECTABLE); |
| 2046 | ADD_INT(DEBUG_SAVEALL); |
| 2047 | ADD_INT(DEBUG_LEAK); |
Tim Peters | 1155887 | 2003-04-06 23:30:52 +0000 | [diff] [blame] | 2048 | #undef ADD_INT |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2049 | return m; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 2050 | } |
| 2051 | |
Guido van Rossum | e13ddc9 | 2003-04-17 17:29:22 +0000 | [diff] [blame] | 2052 | /* API to invoke gc.collect() from C */ |
Neal Norwitz | 7b216c5 | 2006-03-04 20:01:53 +0000 | [diff] [blame] | 2053 | Py_ssize_t |
Guido van Rossum | e13ddc9 | 2003-04-17 17:29:22 +0000 | [diff] [blame] | 2054 | PyGC_Collect(void) |
| 2055 | { |
Victor Stinner | 2e96906 | 2019-11-20 01:49:32 +0100 | [diff] [blame] | 2056 | PyThreadState *tstate = _PyThreadState_GET(); |
Victor Stinner | 7247407 | 2019-11-20 12:25:50 +0100 | [diff] [blame] | 2057 | GCState *gcstate = &tstate->interp->gc; |
Victor Stinner | 2e96906 | 2019-11-20 01:49:32 +0100 | [diff] [blame] | 2058 | |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 2059 | if (!gcstate->enabled) { |
Victor Stinner | 9db0324 | 2019-04-26 02:32:01 +0200 | [diff] [blame] | 2060 | return 0; |
| 2061 | } |
Guido van Rossum | e13ddc9 | 2003-04-17 17:29:22 +0000 | [diff] [blame] | 2062 | |
Victor Stinner | 9db0324 | 2019-04-26 02:32:01 +0200 | [diff] [blame] | 2063 | Py_ssize_t n; |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 2064 | if (gcstate->collecting) { |
Victor Stinner | 9db0324 | 2019-04-26 02:32:01 +0200 | [diff] [blame] | 2065 | /* already collecting, don't do anything */ |
| 2066 | n = 0; |
| 2067 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2068 | else { |
Serhiy Storchaka | 301e3cc | 2018-05-24 15:19:29 +0300 | [diff] [blame] | 2069 | PyObject *exc, *value, *tb; |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 2070 | gcstate->collecting = 1; |
Victor Stinner | 2e96906 | 2019-11-20 01:49:32 +0100 | [diff] [blame] | 2071 | _PyErr_Fetch(tstate, &exc, &value, &tb); |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 2072 | n = collect_with_callback(tstate, NUM_GENERATIONS - 1); |
Victor Stinner | 2e96906 | 2019-11-20 01:49:32 +0100 | [diff] [blame] | 2073 | _PyErr_Restore(tstate, exc, value, tb); |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 2074 | gcstate->collecting = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2075 | } |
Guido van Rossum | e13ddc9 | 2003-04-17 17:29:22 +0000 | [diff] [blame] | 2076 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2077 | return n; |
Guido van Rossum | e13ddc9 | 2003-04-17 17:29:22 +0000 | [diff] [blame] | 2078 | } |
| 2079 | |
Antoine Pitrou | fef34e3 | 2013-05-19 01:11:58 +0200 | [diff] [blame] | 2080 | Py_ssize_t |
Łukasz Langa | fef7e94 | 2016-09-09 21:47:46 -0700 | [diff] [blame] | 2081 | _PyGC_CollectIfEnabled(void) |
| 2082 | { |
Łukasz Langa | fef7e94 | 2016-09-09 21:47:46 -0700 | [diff] [blame] | 2083 | return PyGC_Collect(); |
| 2084 | } |
| 2085 | |
| 2086 | Py_ssize_t |
Antoine Pitrou | fef34e3 | 2013-05-19 01:11:58 +0200 | [diff] [blame] | 2087 | _PyGC_CollectNoFail(void) |
| 2088 | { |
Victor Stinner | 2e96906 | 2019-11-20 01:49:32 +0100 | [diff] [blame] | 2089 | PyThreadState *tstate = _PyThreadState_GET(); |
| 2090 | assert(!_PyErr_Occurred(tstate)); |
Victor Stinner | 9db0324 | 2019-04-26 02:32:01 +0200 | [diff] [blame] | 2091 | |
Victor Stinner | 7247407 | 2019-11-20 12:25:50 +0100 | [diff] [blame] | 2092 | GCState *gcstate = &tstate->interp->gc; |
Antoine Pitrou | fef34e3 | 2013-05-19 01:11:58 +0200 | [diff] [blame] | 2093 | Py_ssize_t n; |
| 2094 | |
Antoine Pitrou | c69c9bc | 2013-08-15 20:15:15 +0200 | [diff] [blame] | 2095 | /* Ideally, this function is only called on interpreter shutdown, |
| 2096 | and therefore not recursively. Unfortunately, when there are daemon |
| 2097 | threads, a daemon thread can start a cyclic garbage collection |
| 2098 | during interpreter shutdown (and then never finish it). |
| 2099 | See http://bugs.python.org/issue8713#msg195178 for an example. |
| 2100 | */ |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 2101 | if (gcstate->collecting) { |
Antoine Pitrou | c69c9bc | 2013-08-15 20:15:15 +0200 | [diff] [blame] | 2102 | n = 0; |
Victor Stinner | 9db0324 | 2019-04-26 02:32:01 +0200 | [diff] [blame] | 2103 | } |
Antoine Pitrou | c69c9bc | 2013-08-15 20:15:15 +0200 | [diff] [blame] | 2104 | else { |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 2105 | gcstate->collecting = 1; |
| 2106 | n = collect(tstate, NUM_GENERATIONS - 1, NULL, NULL, 1); |
| 2107 | gcstate->collecting = 0; |
Antoine Pitrou | c69c9bc | 2013-08-15 20:15:15 +0200 | [diff] [blame] | 2108 | } |
Antoine Pitrou | fef34e3 | 2013-05-19 01:11:58 +0200 | [diff] [blame] | 2109 | return n; |
| 2110 | } |
Antoine Pitrou | 5f454a0 | 2013-05-06 21:15:57 +0200 | [diff] [blame] | 2111 | |
Antoine Pitrou | 696e035 | 2010-08-08 22:18:46 +0000 | [diff] [blame] | 2112 | void |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 2113 | _PyGC_DumpShutdownStats(PyThreadState *tstate) |
Antoine Pitrou | 696e035 | 2010-08-08 22:18:46 +0000 | [diff] [blame] | 2114 | { |
Victor Stinner | 7247407 | 2019-11-20 12:25:50 +0100 | [diff] [blame] | 2115 | GCState *gcstate = &tstate->interp->gc; |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 2116 | if (!(gcstate->debug & DEBUG_SAVEALL) |
| 2117 | && gcstate->garbage != NULL && PyList_GET_SIZE(gcstate->garbage) > 0) { |
Serhiy Storchaka | e2f92de | 2017-11-11 13:06:26 +0200 | [diff] [blame] | 2118 | const char *message; |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 2119 | if (gcstate->debug & DEBUG_UNCOLLECTABLE) |
Antoine Pitrou | b5d8204 | 2010-11-05 00:05:25 +0000 | [diff] [blame] | 2120 | message = "gc: %zd uncollectable objects at " \ |
Georg Brandl | 08be72d | 2010-10-24 15:11:22 +0000 | [diff] [blame] | 2121 | "shutdown"; |
| 2122 | else |
Antoine Pitrou | b5d8204 | 2010-11-05 00:05:25 +0000 | [diff] [blame] | 2123 | message = "gc: %zd uncollectable objects at " \ |
Georg Brandl | 08be72d | 2010-10-24 15:11:22 +0000 | [diff] [blame] | 2124 | "shutdown; use gc.set_debug(gc.DEBUG_UNCOLLECTABLE) to list them"; |
Antoine Pitrou | 070cb3c | 2013-05-08 13:23:25 +0200 | [diff] [blame] | 2125 | /* PyErr_WarnFormat does too many things and we are at shutdown, |
| 2126 | the warnings module's dependencies (e.g. linecache) may be gone |
| 2127 | already. */ |
| 2128 | if (PyErr_WarnExplicitFormat(PyExc_ResourceWarning, "gc", 0, |
| 2129 | "gc", NULL, message, |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 2130 | PyList_GET_SIZE(gcstate->garbage))) |
Georg Brandl | 08be72d | 2010-10-24 15:11:22 +0000 | [diff] [blame] | 2131 | PyErr_WriteUnraisable(NULL); |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 2132 | if (gcstate->debug & DEBUG_UNCOLLECTABLE) { |
Antoine Pitrou | 696e035 | 2010-08-08 22:18:46 +0000 | [diff] [blame] | 2133 | PyObject *repr = NULL, *bytes = NULL; |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 2134 | repr = PyObject_Repr(gcstate->garbage); |
Antoine Pitrou | 696e035 | 2010-08-08 22:18:46 +0000 | [diff] [blame] | 2135 | if (!repr || !(bytes = PyUnicode_EncodeFSDefault(repr))) |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 2136 | PyErr_WriteUnraisable(gcstate->garbage); |
Antoine Pitrou | 696e035 | 2010-08-08 22:18:46 +0000 | [diff] [blame] | 2137 | else { |
| 2138 | PySys_WriteStderr( |
Antoine Pitrou | 070cb3c | 2013-05-08 13:23:25 +0200 | [diff] [blame] | 2139 | " %s\n", |
Antoine Pitrou | 696e035 | 2010-08-08 22:18:46 +0000 | [diff] [blame] | 2140 | PyBytes_AS_STRING(bytes) |
| 2141 | ); |
| 2142 | } |
| 2143 | Py_XDECREF(repr); |
| 2144 | Py_XDECREF(bytes); |
| 2145 | } |
Antoine Pitrou | 696e035 | 2010-08-08 22:18:46 +0000 | [diff] [blame] | 2146 | } |
Antoine Pitrou | 5f454a0 | 2013-05-06 21:15:57 +0200 | [diff] [blame] | 2147 | } |
| 2148 | |
| 2149 | void |
Victor Stinner | 7eee5be | 2019-11-20 10:38:34 +0100 | [diff] [blame] | 2150 | _PyGC_Fini(PyThreadState *tstate) |
Antoine Pitrou | 5f454a0 | 2013-05-06 21:15:57 +0200 | [diff] [blame] | 2151 | { |
Victor Stinner | 7247407 | 2019-11-20 12:25:50 +0100 | [diff] [blame] | 2152 | GCState *gcstate = &tstate->interp->gc; |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 2153 | Py_CLEAR(gcstate->garbage); |
| 2154 | Py_CLEAR(gcstate->callbacks); |
Antoine Pitrou | 696e035 | 2010-08-08 22:18:46 +0000 | [diff] [blame] | 2155 | } |
| 2156 | |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 2157 | /* for debugging */ |
Guido van Rossum | e13ddc9 | 2003-04-17 17:29:22 +0000 | [diff] [blame] | 2158 | void |
| 2159 | _PyGC_Dump(PyGC_Head *g) |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 2160 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2161 | _PyObject_Dump(FROM_GC(g)); |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 2162 | } |
| 2163 | |
Victor Stinner | a544773 | 2019-10-10 09:32:13 +0200 | [diff] [blame] | 2164 | |
| 2165 | #ifdef Py_DEBUG |
Victor Stinner | 1b18455 | 2019-10-08 00:09:31 +0200 | [diff] [blame] | 2166 | static int |
| 2167 | visit_validate(PyObject *op, void *parent_raw) |
| 2168 | { |
| 2169 | PyObject *parent = _PyObject_CAST(parent_raw); |
| 2170 | if (_PyObject_IsFreed(op)) { |
| 2171 | _PyObject_ASSERT_FAILED_MSG(parent, |
| 2172 | "PyObject_GC_Track() object is not valid"); |
| 2173 | } |
| 2174 | return 0; |
| 2175 | } |
Victor Stinner | a544773 | 2019-10-10 09:32:13 +0200 | [diff] [blame] | 2176 | #endif |
Victor Stinner | 1b18455 | 2019-10-08 00:09:31 +0200 | [diff] [blame] | 2177 | |
| 2178 | |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 2179 | /* extension modules might be compiled with GC support so these |
| 2180 | functions must always be available */ |
| 2181 | |
| 2182 | void |
Victor Stinner | a42de74 | 2018-11-22 10:25:22 +0100 | [diff] [blame] | 2183 | PyObject_GC_Track(void *op_raw) |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 2184 | { |
Victor Stinner | a42de74 | 2018-11-22 10:25:22 +0100 | [diff] [blame] | 2185 | PyObject *op = _PyObject_CAST(op_raw); |
Victor Stinner | 271753a | 2018-11-22 01:02:54 +0100 | [diff] [blame] | 2186 | if (_PyObject_GC_IS_TRACKED(op)) { |
| 2187 | _PyObject_ASSERT_FAILED_MSG(op, |
| 2188 | "object already tracked " |
| 2189 | "by the garbage collector"); |
| 2190 | } |
Victor Stinner | a42de74 | 2018-11-22 10:25:22 +0100 | [diff] [blame] | 2191 | _PyObject_GC_TRACK(op); |
Victor Stinner | 1b18455 | 2019-10-08 00:09:31 +0200 | [diff] [blame] | 2192 | |
| 2193 | #ifdef Py_DEBUG |
| 2194 | /* Check that the object is valid: validate objects traversed |
| 2195 | by tp_traverse() */ |
| 2196 | traverseproc traverse = Py_TYPE(op)->tp_traverse; |
| 2197 | (void)traverse(op, visit_validate, op); |
| 2198 | #endif |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 2199 | } |
| 2200 | |
Neil Schemenauer | fec4eb1 | 2002-04-12 02:41:03 +0000 | [diff] [blame] | 2201 | void |
Victor Stinner | a42de74 | 2018-11-22 10:25:22 +0100 | [diff] [blame] | 2202 | PyObject_GC_UnTrack(void *op_raw) |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 2203 | { |
Victor Stinner | a42de74 | 2018-11-22 10:25:22 +0100 | [diff] [blame] | 2204 | PyObject *op = _PyObject_CAST(op_raw); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2205 | /* Obscure: the Py_TRASHCAN mechanism requires that we be able to |
| 2206 | * call PyObject_GC_UnTrack twice on an object. |
| 2207 | */ |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 2208 | if (_PyObject_GC_IS_TRACKED(op)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2209 | _PyObject_GC_UNTRACK(op); |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 2210 | } |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 2211 | } |
| 2212 | |
Hai Shi | 675d9a3 | 2020-04-15 02:11:20 +0800 | [diff] [blame] | 2213 | int |
| 2214 | PyObject_IS_GC(PyObject *obj) |
| 2215 | { |
| 2216 | return _PyObject_IS_GC(obj); |
| 2217 | } |
| 2218 | |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 2219 | static PyObject * |
| 2220 | _PyObject_GC_Alloc(int use_calloc, size_t basicsize) |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 2221 | { |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 2222 | PyThreadState *tstate = _PyThreadState_GET(); |
Victor Stinner | 7247407 | 2019-11-20 12:25:50 +0100 | [diff] [blame] | 2223 | GCState *gcstate = &tstate->interp->gc; |
Victor Stinner | 2e96906 | 2019-11-20 01:49:32 +0100 | [diff] [blame] | 2224 | if (basicsize > PY_SSIZE_T_MAX - sizeof(PyGC_Head)) { |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 2225 | return _PyErr_NoMemory(tstate); |
Victor Stinner | 2e96906 | 2019-11-20 01:49:32 +0100 | [diff] [blame] | 2226 | } |
| 2227 | size_t size = sizeof(PyGC_Head) + basicsize; |
| 2228 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2229 | PyGC_Head *g; |
Victor Stinner | 2e96906 | 2019-11-20 01:49:32 +0100 | [diff] [blame] | 2230 | if (use_calloc) { |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 2231 | g = (PyGC_Head *)PyObject_Calloc(1, size); |
Victor Stinner | 2e96906 | 2019-11-20 01:49:32 +0100 | [diff] [blame] | 2232 | } |
| 2233 | else { |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 2234 | g = (PyGC_Head *)PyObject_Malloc(size); |
Victor Stinner | 2e96906 | 2019-11-20 01:49:32 +0100 | [diff] [blame] | 2235 | } |
| 2236 | if (g == NULL) { |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 2237 | return _PyErr_NoMemory(tstate); |
Victor Stinner | 2e96906 | 2019-11-20 01:49:32 +0100 | [diff] [blame] | 2238 | } |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 2239 | assert(((uintptr_t)g & 3) == 0); // g must be aligned 4bytes boundary |
Victor Stinner | 2e96906 | 2019-11-20 01:49:32 +0100 | [diff] [blame] | 2240 | |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 2241 | g->_gc_next = 0; |
| 2242 | g->_gc_prev = 0; |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 2243 | gcstate->generations[0].count++; /* number of allocated GC objects */ |
| 2244 | if (gcstate->generations[0].count > gcstate->generations[0].threshold && |
| 2245 | gcstate->enabled && |
| 2246 | gcstate->generations[0].threshold && |
| 2247 | !gcstate->collecting && |
| 2248 | !_PyErr_Occurred(tstate)) |
Victor Stinner | 2e96906 | 2019-11-20 01:49:32 +0100 | [diff] [blame] | 2249 | { |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 2250 | gcstate->collecting = 1; |
| 2251 | collect_generations(tstate); |
| 2252 | gcstate->collecting = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2253 | } |
Victor Stinner | 2e96906 | 2019-11-20 01:49:32 +0100 | [diff] [blame] | 2254 | PyObject *op = FROM_GC(g); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2255 | return op; |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 2256 | } |
| 2257 | |
| 2258 | PyObject * |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 2259 | _PyObject_GC_Malloc(size_t basicsize) |
| 2260 | { |
| 2261 | return _PyObject_GC_Alloc(0, basicsize); |
| 2262 | } |
| 2263 | |
| 2264 | PyObject * |
| 2265 | _PyObject_GC_Calloc(size_t basicsize) |
| 2266 | { |
| 2267 | return _PyObject_GC_Alloc(1, basicsize); |
| 2268 | } |
| 2269 | |
| 2270 | PyObject * |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 2271 | _PyObject_GC_New(PyTypeObject *tp) |
| 2272 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2273 | PyObject *op = _PyObject_GC_Malloc(_PyObject_SIZE(tp)); |
| 2274 | if (op != NULL) |
| 2275 | op = PyObject_INIT(op, tp); |
| 2276 | return op; |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 2277 | } |
| 2278 | |
| 2279 | PyVarObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2280 | _PyObject_GC_NewVar(PyTypeObject *tp, Py_ssize_t nitems) |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 2281 | { |
Victor Stinner | 5d1866c | 2013-07-08 22:17:52 +0200 | [diff] [blame] | 2282 | size_t size; |
| 2283 | PyVarObject *op; |
| 2284 | |
| 2285 | if (nitems < 0) { |
| 2286 | PyErr_BadInternalCall(); |
| 2287 | return NULL; |
| 2288 | } |
| 2289 | size = _PyObject_VAR_SIZE(tp, nitems); |
| 2290 | op = (PyVarObject *) _PyObject_GC_Malloc(size); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2291 | if (op != NULL) |
| 2292 | op = PyObject_INIT_VAR(op, tp, nitems); |
| 2293 | return op; |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 2294 | } |
| 2295 | |
| 2296 | PyVarObject * |
Martin v. Löwis | 4129068 | 2006-02-16 14:56:14 +0000 | [diff] [blame] | 2297 | _PyObject_GC_Resize(PyVarObject *op, Py_ssize_t nitems) |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 2298 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2299 | const size_t basicsize = _PyObject_VAR_SIZE(Py_TYPE(op), nitems); |
Victor Stinner | a4b2bc7 | 2018-10-26 18:00:13 +0200 | [diff] [blame] | 2300 | _PyObject_ASSERT((PyObject *)op, !_PyObject_GC_IS_TRACKED(op)); |
| 2301 | if (basicsize > PY_SSIZE_T_MAX - sizeof(PyGC_Head)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2302 | return (PyVarObject *)PyErr_NoMemory(); |
Victor Stinner | a4b2bc7 | 2018-10-26 18:00:13 +0200 | [diff] [blame] | 2303 | } |
| 2304 | |
| 2305 | PyGC_Head *g = AS_GC(op); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2306 | g = (PyGC_Head *)PyObject_REALLOC(g, sizeof(PyGC_Head) + basicsize); |
| 2307 | if (g == NULL) |
| 2308 | return (PyVarObject *)PyErr_NoMemory(); |
| 2309 | op = (PyVarObject *) FROM_GC(g); |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 2310 | Py_SET_SIZE(op, nitems); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2311 | return op; |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 2312 | } |
| 2313 | |
| 2314 | void |
Neil Schemenauer | fec4eb1 | 2002-04-12 02:41:03 +0000 | [diff] [blame] | 2315 | PyObject_GC_Del(void *op) |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 2316 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2317 | PyGC_Head *g = AS_GC(op); |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 2318 | if (_PyObject_GC_IS_TRACKED(op)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2319 | gc_list_remove(g); |
INADA Naoki | 5ac9e6e | 2018-07-10 17:19:53 +0900 | [diff] [blame] | 2320 | } |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 2321 | PyThreadState *tstate = _PyThreadState_GET(); |
Victor Stinner | 7247407 | 2019-11-20 12:25:50 +0100 | [diff] [blame] | 2322 | GCState *gcstate = &tstate->interp->gc; |
Victor Stinner | 67e0de6 | 2019-11-20 11:48:18 +0100 | [diff] [blame] | 2323 | if (gcstate->generations[0].count > 0) { |
| 2324 | gcstate->generations[0].count--; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2325 | } |
| 2326 | PyObject_FREE(g); |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 2327 | } |
Pablo Galindo | f13072b | 2020-04-11 01:21:54 +0100 | [diff] [blame] | 2328 | |
| 2329 | int |
| 2330 | PyObject_GC_IsTracked(PyObject* obj) |
| 2331 | { |
Hai Shi | 675d9a3 | 2020-04-15 02:11:20 +0800 | [diff] [blame] | 2332 | if (_PyObject_IS_GC(obj) && _PyObject_GC_IS_TRACKED(obj)) { |
Pablo Galindo | f13072b | 2020-04-11 01:21:54 +0100 | [diff] [blame] | 2333 | return 1; |
| 2334 | } |
| 2335 | return 0; |
| 2336 | } |
| 2337 | |
| 2338 | int |
| 2339 | PyObject_GC_IsFinalized(PyObject *obj) |
| 2340 | { |
Hai Shi | 675d9a3 | 2020-04-15 02:11:20 +0800 | [diff] [blame] | 2341 | if (_PyObject_IS_GC(obj) && _PyGCHead_FINALIZED(AS_GC(obj))) { |
Pablo Galindo | f13072b | 2020-04-11 01:21:54 +0100 | [diff] [blame] | 2342 | return 1; |
| 2343 | } |
| 2344 | return 0; |
| 2345 | } |