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