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