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" |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 27 | #include "frameobject.h" /* for PyFrame_ClearFreeList */ |
Victor Stinner | ae58649 | 2014-09-02 23:18:25 +0200 | [diff] [blame] | 28 | #include "pytime.h" /* for _PyTime_monotonic, _PyTime_INTERVAL */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 29 | |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 30 | /* Get an object's GC head */ |
| 31 | #define AS_GC(o) ((PyGC_Head *)(o)-1) |
| 32 | |
| 33 | /* Get the object given the GC head */ |
| 34 | #define FROM_GC(g) ((PyObject *)(((PyGC_Head *)g)+1)) |
| 35 | |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 36 | /*** Global GC state ***/ |
| 37 | |
Neil Schemenauer | 2880ae5 | 2002-05-04 05:35:20 +0000 | [diff] [blame] | 38 | struct gc_generation { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 39 | PyGC_Head head; |
| 40 | int threshold; /* collection threshold */ |
| 41 | int count; /* count of allocations or collections of younger |
| 42 | generations */ |
Neil Schemenauer | 2880ae5 | 2002-05-04 05:35:20 +0000 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | #define NUM_GENERATIONS 3 |
| 46 | #define GEN_HEAD(n) (&generations[n].head) |
| 47 | |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 48 | /* linked lists of container objects */ |
Neil Schemenauer | 2880ae5 | 2002-05-04 05:35:20 +0000 | [diff] [blame] | 49 | static struct gc_generation generations[NUM_GENERATIONS] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 50 | /* PyGC_Head, threshold, count */ |
| 51 | {{{GEN_HEAD(0), GEN_HEAD(0), 0}}, 700, 0}, |
| 52 | {{{GEN_HEAD(1), GEN_HEAD(1), 0}}, 10, 0}, |
| 53 | {{{GEN_HEAD(2), GEN_HEAD(2), 0}}, 10, 0}, |
Neil Schemenauer | 2880ae5 | 2002-05-04 05:35:20 +0000 | [diff] [blame] | 54 | }; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 55 | |
Neil Schemenauer | 2880ae5 | 2002-05-04 05:35:20 +0000 | [diff] [blame] | 56 | PyGC_Head *_PyGC_generation0 = GEN_HEAD(0); |
| 57 | |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 58 | static int enabled = 1; /* automatic collection enabled? */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 59 | |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 60 | /* true if we are currently running the collector */ |
Tim Peters | bf384c2 | 2003-04-06 00:11:39 +0000 | [diff] [blame] | 61 | static int collecting = 0; |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 62 | |
Tim Peters | 6fc13d9 | 2002-07-02 18:12:35 +0000 | [diff] [blame] | 63 | /* list of uncollectable objects */ |
Tim Peters | bf384c2 | 2003-04-06 00:11:39 +0000 | [diff] [blame] | 64 | static PyObject *garbage = NULL; |
Tim Peters | 6fc13d9 | 2002-07-02 18:12:35 +0000 | [diff] [blame] | 65 | |
| 66 | /* Python string to use if unhandled exception occurs */ |
Tim Peters | bf384c2 | 2003-04-06 00:11:39 +0000 | [diff] [blame] | 67 | static PyObject *gc_str = NULL; |
Tim Peters | 6fc13d9 | 2002-07-02 18:12:35 +0000 | [diff] [blame] | 68 | |
Kristján Valur Jónsson | 69c6352 | 2012-04-15 11:41:32 +0000 | [diff] [blame] | 69 | /* a list of callbacks to be invoked when collection is performed */ |
| 70 | static PyObject *callbacks = NULL; |
| 71 | |
| 72 | /* This is the number of objects that survived the last full collection. It |
Antoine Pitrou | 14b78f5 | 2009-01-09 22:27:08 +0000 | [diff] [blame] | 73 | approximates the number of long lived objects tracked by the GC. |
| 74 | |
| 75 | (by "full collection", we mean a collection of the oldest generation). |
| 76 | */ |
| 77 | static Py_ssize_t long_lived_total = 0; |
| 78 | |
Kristján Valur Jónsson | 69c6352 | 2012-04-15 11:41:32 +0000 | [diff] [blame] | 79 | /* This is the number of objects that survived all "non-full" collections, |
Antoine Pitrou | 14b78f5 | 2009-01-09 22:27:08 +0000 | [diff] [blame] | 80 | and are awaiting to undergo a full collection for the first time. |
| 81 | |
| 82 | */ |
| 83 | static Py_ssize_t long_lived_pending = 0; |
| 84 | |
| 85 | /* |
| 86 | NOTE: about the counting of long-lived objects. |
| 87 | |
| 88 | To limit the cost of garbage collection, there are two strategies; |
| 89 | - make each collection faster, e.g. by scanning fewer objects |
| 90 | - do less collections |
| 91 | This heuristic is about the latter strategy. |
| 92 | |
| 93 | In addition to the various configurable thresholds, we only trigger a |
| 94 | full collection if the ratio |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 95 | long_lived_pending / long_lived_total |
Antoine Pitrou | 14b78f5 | 2009-01-09 22:27:08 +0000 | [diff] [blame] | 96 | is above a given value (hardwired to 25%). |
| 97 | |
| 98 | The reason is that, while "non-full" collections (i.e., collections of |
| 99 | the young and middle generations) will always examine roughly the same |
| 100 | number of objects -- determined by the aforementioned thresholds --, |
| 101 | the cost of a full collection is proportional to the total number of |
| 102 | long-lived objects, which is virtually unbounded. |
| 103 | |
| 104 | Indeed, it has been remarked that doing a full collection every |
| 105 | <constant number> of object creations entails a dramatic performance |
| 106 | degradation in workloads which consist in creating and storing lots of |
| 107 | long-lived objects (e.g. building a large list of GC-tracked objects would |
| 108 | show quadratic performance, instead of linear as expected: see issue #4074). |
| 109 | |
| 110 | Using the above ratio, instead, yields amortized linear performance in |
| 111 | the total number of objects (the effect of which can be summarized |
| 112 | thusly: "each full garbage collection is more and more costly as the |
| 113 | number of objects grows, but we do fewer and fewer of them"). |
| 114 | |
| 115 | This heuristic was suggested by Martin von Löwis on python-dev in |
| 116 | June 2008. His original analysis and proposal can be found at: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 117 | http://mail.python.org/pipermail/python-dev/2008-June/080579.html |
Antoine Pitrou | 14b78f5 | 2009-01-09 22:27:08 +0000 | [diff] [blame] | 118 | */ |
| 119 | |
Antoine Pitrou | e1ad3da | 2012-05-28 22:22:34 +0200 | [diff] [blame] | 120 | /* |
| 121 | NOTE: about untracking of mutable objects. |
Victor Stinner | c1eb26c | 2013-07-08 22:15:05 +0200 | [diff] [blame] | 122 | |
Antoine Pitrou | e1ad3da | 2012-05-28 22:22:34 +0200 | [diff] [blame] | 123 | Certain types of container cannot participate in a reference cycle, and |
| 124 | so do not need to be tracked by the garbage collector. Untracking these |
| 125 | objects reduces the cost of garbage collections. However, determining |
| 126 | which objects may be untracked is not free, and the costs must be |
| 127 | weighed against the benefits for garbage collection. |
| 128 | |
| 129 | There are two possible strategies for when to untrack a container: |
| 130 | |
| 131 | i) When the container is created. |
| 132 | ii) When the container is examined by the garbage collector. |
| 133 | |
| 134 | Tuples containing only immutable objects (integers, strings etc, and |
| 135 | recursively, tuples of immutable objects) do not need to be tracked. |
| 136 | The interpreter creates a large number of tuples, many of which will |
| 137 | not survive until garbage collection. It is therefore not worthwhile |
| 138 | to untrack eligible tuples at creation time. |
| 139 | |
Victor Stinner | c1eb26c | 2013-07-08 22:15:05 +0200 | [diff] [blame] | 140 | Instead, all tuples except the empty tuple are tracked when created. |
| 141 | During garbage collection it is determined whether any surviving tuples |
| 142 | can be untracked. A tuple can be untracked if all of its contents are |
| 143 | already not tracked. Tuples are examined for untracking in all garbage |
Antoine Pitrou | e1ad3da | 2012-05-28 22:22:34 +0200 | [diff] [blame] | 144 | collection cycles. It may take more than one cycle to untrack a tuple. |
| 145 | |
| 146 | Dictionaries containing only immutable objects also do not need to be |
| 147 | tracked. Dictionaries are untracked when created. If a tracked item is |
| 148 | inserted into a dictionary (either as a key or value), the dictionary |
| 149 | becomes tracked. During a full garbage collection (all generations), |
| 150 | the collector will untrack any dictionaries whose contents are not |
| 151 | tracked. |
| 152 | |
| 153 | The module provides the python function is_tracked(obj), which returns |
| 154 | the CURRENT tracking status of the object. Subsequent garbage |
| 155 | collections may change the tracking status of the object. |
Victor Stinner | c1eb26c | 2013-07-08 22:15:05 +0200 | [diff] [blame] | 156 | |
| 157 | Untracking of certain containers was introduced in issue #4688, and |
Antoine Pitrou | e1ad3da | 2012-05-28 22:22:34 +0200 | [diff] [blame] | 158 | the algorithm was refined in response to issue #14775. |
| 159 | */ |
Antoine Pitrou | 14b78f5 | 2009-01-09 22:27:08 +0000 | [diff] [blame] | 160 | |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 161 | /* set for debugging information */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 162 | #define DEBUG_STATS (1<<0) /* print collection statistics */ |
| 163 | #define DEBUG_COLLECTABLE (1<<1) /* print collectable objects */ |
| 164 | #define DEBUG_UNCOLLECTABLE (1<<2) /* print uncollectable objects */ |
| 165 | #define DEBUG_SAVEALL (1<<5) /* save all garbage in gc.garbage */ |
| 166 | #define DEBUG_LEAK DEBUG_COLLECTABLE | \ |
| 167 | DEBUG_UNCOLLECTABLE | \ |
| 168 | DEBUG_SAVEALL |
Jeremy Hylton | b709df3 | 2000-09-01 02:47:25 +0000 | [diff] [blame] | 169 | static int debug; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 170 | |
Antoine Pitrou | d4156c1 | 2012-10-30 22:43:19 +0100 | [diff] [blame] | 171 | /* Running stats per generation */ |
| 172 | struct gc_generation_stats { |
| 173 | /* total number of collections */ |
| 174 | Py_ssize_t collections; |
| 175 | /* total number of collected objects */ |
| 176 | Py_ssize_t collected; |
| 177 | /* total number of uncollectable objects (put into gc.garbage) */ |
| 178 | Py_ssize_t uncollectable; |
| 179 | }; |
| 180 | |
| 181 | static struct gc_generation_stats generation_stats[NUM_GENERATIONS]; |
| 182 | |
Tim Peters | 6fc13d9 | 2002-07-02 18:12:35 +0000 | [diff] [blame] | 183 | /*-------------------------------------------------------------------------- |
| 184 | gc_refs values. |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 185 | |
Tim Peters | 6fc13d9 | 2002-07-02 18:12:35 +0000 | [diff] [blame] | 186 | Between collections, every gc'ed object has one of two gc_refs values: |
| 187 | |
| 188 | GC_UNTRACKED |
| 189 | The initial state; objects returned by PyObject_GC_Malloc are in this |
| 190 | state. The object doesn't live in any generation list, and its |
| 191 | tp_traverse slot must not be called. |
| 192 | |
| 193 | GC_REACHABLE |
| 194 | The object lives in some generation list, and its tp_traverse is safe to |
| 195 | call. An object transitions to GC_REACHABLE when PyObject_GC_Track |
| 196 | is called. |
| 197 | |
| 198 | During a collection, gc_refs can temporarily take on other states: |
| 199 | |
| 200 | >= 0 |
| 201 | At the start of a collection, update_refs() copies the true refcount |
| 202 | to gc_refs, for each object in the generation being collected. |
| 203 | subtract_refs() then adjusts gc_refs so that it equals the number of |
| 204 | times an object is referenced directly from outside the generation |
| 205 | being collected. |
Martin v. Löwis | 774348c | 2002-11-09 19:54:06 +0000 | [diff] [blame] | 206 | gc_refs remains >= 0 throughout these steps. |
Tim Peters | 6fc13d9 | 2002-07-02 18:12:35 +0000 | [diff] [blame] | 207 | |
| 208 | GC_TENTATIVELY_UNREACHABLE |
| 209 | move_unreachable() then moves objects not reachable (whether directly or |
| 210 | indirectly) from outside the generation into an "unreachable" set. |
| 211 | Objects that are found to be reachable have gc_refs set to GC_REACHABLE |
| 212 | again. Objects that are found to be unreachable have gc_refs set to |
| 213 | GC_TENTATIVELY_UNREACHABLE. It's "tentatively" because the pass doing |
| 214 | this can't be sure until it ends, and GC_TENTATIVELY_UNREACHABLE may |
| 215 | transition back to GC_REACHABLE. |
| 216 | |
| 217 | Only objects with GC_TENTATIVELY_UNREACHABLE still set are candidates |
| 218 | for collection. If it's decided not to collect such an object (e.g., |
| 219 | it has a __del__ method), its gc_refs is restored to GC_REACHABLE again. |
| 220 | ---------------------------------------------------------------------------- |
| 221 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 222 | #define GC_UNTRACKED _PyGC_REFS_UNTRACKED |
| 223 | #define GC_REACHABLE _PyGC_REFS_REACHABLE |
| 224 | #define GC_TENTATIVELY_UNREACHABLE _PyGC_REFS_TENTATIVELY_UNREACHABLE |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 225 | |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 226 | #define IS_TRACKED(o) (_PyGC_REFS(o) != GC_UNTRACKED) |
| 227 | #define IS_REACHABLE(o) (_PyGC_REFS(o) == GC_REACHABLE) |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 228 | #define IS_TENTATIVELY_UNREACHABLE(o) ( \ |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 229 | _PyGC_REFS(o) == GC_TENTATIVELY_UNREACHABLE) |
Neil Schemenauer | a2b11ec | 2002-05-21 15:53:24 +0000 | [diff] [blame] | 230 | |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 231 | /*** list functions ***/ |
| 232 | |
| 233 | static void |
| 234 | gc_list_init(PyGC_Head *list) |
| 235 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 236 | list->gc.gc_prev = list; |
| 237 | list->gc.gc_next = list; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Neil Schemenauer | 2880ae5 | 2002-05-04 05:35:20 +0000 | [diff] [blame] | 240 | static int |
| 241 | gc_list_is_empty(PyGC_Head *list) |
| 242 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 243 | return (list->gc.gc_next == list); |
Neil Schemenauer | 2880ae5 | 2002-05-04 05:35:20 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Tim Peters | e2d5918 | 2004-11-01 01:39:08 +0000 | [diff] [blame] | 246 | #if 0 |
| 247 | /* This became unused after gc_list_move() was introduced. */ |
| 248 | /* Append `node` to `list`. */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 249 | static void |
| 250 | gc_list_append(PyGC_Head *node, PyGC_Head *list) |
| 251 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 252 | node->gc.gc_next = list; |
| 253 | node->gc.gc_prev = list->gc.gc_prev; |
| 254 | node->gc.gc_prev->gc.gc_next = node; |
| 255 | list->gc.gc_prev = node; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 256 | } |
Tim Peters | e2d5918 | 2004-11-01 01:39:08 +0000 | [diff] [blame] | 257 | #endif |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 258 | |
Tim Peters | e2d5918 | 2004-11-01 01:39:08 +0000 | [diff] [blame] | 259 | /* Remove `node` from the gc list it's currently in. */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 260 | static void |
| 261 | gc_list_remove(PyGC_Head *node) |
| 262 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 263 | node->gc.gc_prev->gc.gc_next = node->gc.gc_next; |
| 264 | node->gc.gc_next->gc.gc_prev = node->gc.gc_prev; |
| 265 | node->gc.gc_next = NULL; /* object is not currently tracked */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Tim Peters | e2d5918 | 2004-11-01 01:39:08 +0000 | [diff] [blame] | 268 | /* Move `node` from the gc list it's currently in (which is not explicitly |
| 269 | * named here) to the end of `list`. This is semantically the same as |
| 270 | * gc_list_remove(node) followed by gc_list_append(node, list). |
| 271 | */ |
| 272 | static void |
| 273 | gc_list_move(PyGC_Head *node, PyGC_Head *list) |
| 274 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 275 | PyGC_Head *new_prev; |
| 276 | PyGC_Head *current_prev = node->gc.gc_prev; |
| 277 | PyGC_Head *current_next = node->gc.gc_next; |
| 278 | /* Unlink from current list. */ |
| 279 | current_prev->gc.gc_next = current_next; |
| 280 | current_next->gc.gc_prev = current_prev; |
| 281 | /* Relink at end of new list. */ |
| 282 | new_prev = node->gc.gc_prev = list->gc.gc_prev; |
| 283 | new_prev->gc.gc_next = list->gc.gc_prev = node; |
| 284 | node->gc.gc_next = list; |
Tim Peters | e2d5918 | 2004-11-01 01:39:08 +0000 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | /* append list `from` onto list `to`; `from` becomes an empty list */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 288 | static void |
| 289 | gc_list_merge(PyGC_Head *from, PyGC_Head *to) |
| 290 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 291 | PyGC_Head *tail; |
| 292 | assert(from != to); |
| 293 | if (!gc_list_is_empty(from)) { |
| 294 | tail = to->gc.gc_prev; |
| 295 | tail->gc.gc_next = from->gc.gc_next; |
| 296 | tail->gc.gc_next->gc.gc_prev = tail; |
| 297 | to->gc.gc_prev = from->gc.gc_prev; |
| 298 | to->gc.gc_prev->gc.gc_next = to; |
| 299 | } |
| 300 | gc_list_init(from); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Neal Norwitz | 7b216c5 | 2006-03-04 20:01:53 +0000 | [diff] [blame] | 303 | static Py_ssize_t |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 304 | gc_list_size(PyGC_Head *list) |
| 305 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 306 | PyGC_Head *gc; |
| 307 | Py_ssize_t n = 0; |
| 308 | for (gc = list->gc.gc_next; gc != list; gc = gc->gc.gc_next) { |
| 309 | n++; |
| 310 | } |
| 311 | return n; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 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. |
| 315 | * Return 0 if all OK, < 0 if error (out of memory for list). |
| 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; |
| 321 | for (gc = gc_list->gc.gc_next; gc != gc_list; gc = gc->gc.gc_next) { |
| 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 | |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 332 | /*** end of list stuff ***/ |
| 333 | |
| 334 | |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 335 | /* Set all gc_refs = ob_refcnt. After this, gc_refs is > 0 for all objects |
| 336 | * in containers, and is GC_REACHABLE for all tracked gc objects not in |
| 337 | * containers. |
Tim Peters | 8839617 | 2002-06-30 17:56:40 +0000 | [diff] [blame] | 338 | */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 339 | static void |
| 340 | update_refs(PyGC_Head *containers) |
| 341 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 342 | PyGC_Head *gc = containers->gc.gc_next; |
| 343 | for (; gc != containers; gc = gc->gc.gc_next) { |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 344 | assert(_PyGCHead_REFS(gc) == GC_REACHABLE); |
| 345 | _PyGCHead_SET_REFS(gc, Py_REFCNT(FROM_GC(gc))); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 346 | /* Python's cyclic gc should never see an incoming refcount |
| 347 | * of 0: if something decref'ed to 0, it should have been |
| 348 | * deallocated immediately at that time. |
| 349 | * Possible cause (if the assert triggers): a tp_dealloc |
| 350 | * routine left a gc-aware object tracked during its teardown |
| 351 | * phase, and did something-- or allowed something to happen -- |
| 352 | * that called back into Python. gc can trigger then, and may |
| 353 | * see the still-tracked dying object. Before this assert |
| 354 | * was added, such mistakes went on to allow gc to try to |
| 355 | * delete the object again. In a debug build, that caused |
| 356 | * a mysterious segfault, when _Py_ForgetReference tried |
| 357 | * to remove the object from the doubly-linked list of all |
| 358 | * objects a second time. In a release build, an actual |
| 359 | * double deallocation occurred, which leads to corruption |
| 360 | * of the allocator's internal bookkeeping pointers. That's |
| 361 | * so serious that maybe this should be a release-build |
| 362 | * check instead of an assert? |
| 363 | */ |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 364 | assert(_PyGCHead_REFS(gc) != 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 365 | } |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 368 | /* A traversal callback for subtract_refs. */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 369 | static int |
| 370 | visit_decref(PyObject *op, void *data) |
| 371 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 372 | assert(op != NULL); |
| 373 | if (PyObject_IS_GC(op)) { |
| 374 | PyGC_Head *gc = AS_GC(op); |
| 375 | /* We're only interested in gc_refs for objects in the |
| 376 | * generation being collected, which can be recognized |
| 377 | * because only they have positive gc_refs. |
| 378 | */ |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 379 | assert(_PyGCHead_REFS(gc) != 0); /* else refcount was too small */ |
| 380 | if (_PyGCHead_REFS(gc) > 0) |
| 381 | _PyGCHead_DECREF(gc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 382 | } |
| 383 | return 0; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 386 | /* Subtract internal references from gc_refs. After this, gc_refs is >= 0 |
| 387 | * for all objects in containers, and is GC_REACHABLE for all tracked gc |
| 388 | * objects not in containers. The ones with gc_refs > 0 are directly |
| 389 | * reachable from outside containers, and so can't be collected. |
| 390 | */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 391 | static void |
| 392 | subtract_refs(PyGC_Head *containers) |
| 393 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 394 | traverseproc traverse; |
| 395 | PyGC_Head *gc = containers->gc.gc_next; |
| 396 | for (; gc != containers; gc=gc->gc.gc_next) { |
| 397 | traverse = Py_TYPE(FROM_GC(gc))->tp_traverse; |
| 398 | (void) traverse(FROM_GC(gc), |
| 399 | (visitproc)visit_decref, |
| 400 | NULL); |
| 401 | } |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 404 | /* A traversal callback for move_unreachable. */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 405 | static int |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 406 | visit_reachable(PyObject *op, PyGC_Head *reachable) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 407 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 408 | if (PyObject_IS_GC(op)) { |
| 409 | PyGC_Head *gc = AS_GC(op); |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 410 | const Py_ssize_t gc_refs = _PyGCHead_REFS(gc); |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 411 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 412 | if (gc_refs == 0) { |
| 413 | /* This is in move_unreachable's 'young' list, but |
| 414 | * the traversal hasn't yet gotten to it. All |
| 415 | * we need to do is tell move_unreachable that it's |
| 416 | * reachable. |
| 417 | */ |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 418 | _PyGCHead_SET_REFS(gc, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 419 | } |
| 420 | else if (gc_refs == GC_TENTATIVELY_UNREACHABLE) { |
| 421 | /* This had gc_refs = 0 when move_unreachable got |
| 422 | * to it, but turns out it's reachable after all. |
| 423 | * Move it back to move_unreachable's 'young' list, |
| 424 | * and move_unreachable will eventually get to it |
| 425 | * again. |
| 426 | */ |
| 427 | gc_list_move(gc, reachable); |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 428 | _PyGCHead_SET_REFS(gc, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 429 | } |
| 430 | /* Else there's nothing to do. |
| 431 | * If gc_refs > 0, it must be in move_unreachable's 'young' |
| 432 | * list, and move_unreachable will eventually get to it. |
| 433 | * If gc_refs == GC_REACHABLE, it's either in some other |
| 434 | * generation so we don't care about it, or move_unreachable |
| 435 | * already dealt with it. |
| 436 | * If gc_refs == GC_UNTRACKED, it must be ignored. |
| 437 | */ |
| 438 | else { |
| 439 | assert(gc_refs > 0 |
| 440 | || gc_refs == GC_REACHABLE |
| 441 | || gc_refs == GC_UNTRACKED); |
| 442 | } |
| 443 | } |
| 444 | return 0; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 447 | /* Move the unreachable objects from young to unreachable. After this, |
| 448 | * all objects in young have gc_refs = GC_REACHABLE, and all objects in |
| 449 | * unreachable have gc_refs = GC_TENTATIVELY_UNREACHABLE. All tracked |
| 450 | * gc objects not in young or unreachable still have gc_refs = GC_REACHABLE. |
| 451 | * All objects in young after this are directly or indirectly reachable |
| 452 | * from outside the original young; and all objects in unreachable are |
| 453 | * not. |
Tim Peters | 8839617 | 2002-06-30 17:56:40 +0000 | [diff] [blame] | 454 | */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 455 | static void |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 456 | move_unreachable(PyGC_Head *young, PyGC_Head *unreachable) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 457 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 458 | PyGC_Head *gc = young->gc.gc_next; |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 459 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 460 | /* Invariants: all objects "to the left" of us in young have gc_refs |
| 461 | * = GC_REACHABLE, and are indeed reachable (directly or indirectly) |
| 462 | * from outside the young list as it was at entry. All other objects |
| 463 | * from the original young "to the left" of us are in unreachable now, |
| 464 | * and have gc_refs = GC_TENTATIVELY_UNREACHABLE. All objects to the |
| 465 | * left of us in 'young' now have been scanned, and no objects here |
| 466 | * or to the right have been scanned yet. |
| 467 | */ |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 468 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 469 | while (gc != young) { |
| 470 | PyGC_Head *next; |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 471 | |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 472 | if (_PyGCHead_REFS(gc)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 473 | /* gc is definitely reachable from outside the |
| 474 | * original 'young'. Mark it as such, and traverse |
| 475 | * its pointers to find any other objects that may |
| 476 | * be directly reachable from it. Note that the |
| 477 | * call to tp_traverse may append objects to young, |
| 478 | * so we have to wait until it returns to determine |
| 479 | * the next object to visit. |
| 480 | */ |
| 481 | PyObject *op = FROM_GC(gc); |
| 482 | traverseproc traverse = Py_TYPE(op)->tp_traverse; |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 483 | assert(_PyGCHead_REFS(gc) > 0); |
| 484 | _PyGCHead_SET_REFS(gc, GC_REACHABLE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 485 | (void) traverse(op, |
| 486 | (visitproc)visit_reachable, |
| 487 | (void *)young); |
| 488 | next = gc->gc.gc_next; |
| 489 | if (PyTuple_CheckExact(op)) { |
| 490 | _PyTuple_MaybeUntrack(op); |
| 491 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 492 | } |
| 493 | else { |
| 494 | /* This *may* be unreachable. To make progress, |
| 495 | * assume it is. gc isn't directly reachable from |
| 496 | * any object we've already traversed, but may be |
| 497 | * reachable from an object we haven't gotten to yet. |
| 498 | * visit_reachable will eventually move gc back into |
| 499 | * young if that's so, and we'll see it again. |
| 500 | */ |
| 501 | next = gc->gc.gc_next; |
| 502 | gc_list_move(gc, unreachable); |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 503 | _PyGCHead_SET_REFS(gc, GC_TENTATIVELY_UNREACHABLE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 504 | } |
| 505 | gc = next; |
| 506 | } |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 507 | } |
| 508 | |
Antoine Pitrou | e1ad3da | 2012-05-28 22:22:34 +0200 | [diff] [blame] | 509 | /* Try to untrack all currently tracked dictionaries */ |
| 510 | static void |
| 511 | untrack_dicts(PyGC_Head *head) |
| 512 | { |
| 513 | PyGC_Head *next, *gc = head->gc.gc_next; |
| 514 | while (gc != head) { |
| 515 | PyObject *op = FROM_GC(gc); |
| 516 | next = gc->gc.gc_next; |
| 517 | if (PyDict_CheckExact(op)) |
| 518 | _PyDict_MaybeUntrack(op); |
| 519 | gc = next; |
| 520 | } |
| 521 | } |
| 522 | |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 523 | /* Return true if object has a pre-PEP 442 finalization method. */ |
Neil Schemenauer | a765c12 | 2001-11-01 17:35:23 +0000 | [diff] [blame] | 524 | static int |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 525 | has_legacy_finalizer(PyObject *op) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 526 | { |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 527 | return op->ob_type->tp_del != NULL; |
Neil Schemenauer | a765c12 | 2001-11-01 17:35:23 +0000 | [diff] [blame] | 528 | } |
| 529 | |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 530 | /* Move the objects in unreachable with tp_del slots into `finalizers`. |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 531 | * Objects moved into `finalizers` have gc_refs set to GC_REACHABLE; the |
| 532 | * objects remaining in unreachable are left at GC_TENTATIVELY_UNREACHABLE. |
Jeremy Hylton | ce136e9 | 2003-04-04 19:59:06 +0000 | [diff] [blame] | 533 | */ |
Neil Schemenauer | a765c12 | 2001-11-01 17:35:23 +0000 | [diff] [blame] | 534 | static void |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 535 | move_legacy_finalizers(PyGC_Head *unreachable, PyGC_Head *finalizers) |
Neil Schemenauer | a765c12 | 2001-11-01 17:35:23 +0000 | [diff] [blame] | 536 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 537 | PyGC_Head *gc; |
| 538 | PyGC_Head *next; |
Tim Peters | f6b8045 | 2003-04-07 19:21:15 +0000 | [diff] [blame] | 539 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 540 | /* March over unreachable. Move objects with finalizers into |
| 541 | * `finalizers`. |
| 542 | */ |
| 543 | for (gc = unreachable->gc.gc_next; gc != unreachable; gc = next) { |
| 544 | PyObject *op = FROM_GC(gc); |
Jeremy Hylton | ce136e9 | 2003-04-04 19:59:06 +0000 | [diff] [blame] | 545 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 546 | assert(IS_TENTATIVELY_UNREACHABLE(op)); |
| 547 | next = gc->gc.gc_next; |
Tim Peters | f6ae7a4 | 2003-04-05 18:40:50 +0000 | [diff] [blame] | 548 | |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 549 | if (has_legacy_finalizer(op)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 550 | gc_list_move(gc, finalizers); |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 551 | _PyGCHead_SET_REFS(gc, GC_REACHABLE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 552 | } |
| 553 | } |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 554 | } |
| 555 | |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 556 | /* A traversal callback for move_legacy_finalizer_reachable. */ |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 557 | static int |
| 558 | visit_move(PyObject *op, PyGC_Head *tolist) |
| 559 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 560 | if (PyObject_IS_GC(op)) { |
| 561 | if (IS_TENTATIVELY_UNREACHABLE(op)) { |
| 562 | PyGC_Head *gc = AS_GC(op); |
| 563 | gc_list_move(gc, tolist); |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 564 | _PyGCHead_SET_REFS(gc, GC_REACHABLE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 565 | } |
| 566 | } |
| 567 | return 0; |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | /* Move objects that are reachable from finalizers, from the unreachable set |
Tim Peters | f6b8045 | 2003-04-07 19:21:15 +0000 | [diff] [blame] | 571 | * into finalizers set. |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 572 | */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 573 | static void |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 574 | move_legacy_finalizer_reachable(PyGC_Head *finalizers) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 575 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 576 | traverseproc traverse; |
| 577 | PyGC_Head *gc = finalizers->gc.gc_next; |
| 578 | for (; gc != finalizers; gc = gc->gc.gc_next) { |
| 579 | /* Note that the finalizers list may grow during this. */ |
| 580 | traverse = Py_TYPE(FROM_GC(gc))->tp_traverse; |
| 581 | (void) traverse(FROM_GC(gc), |
| 582 | (visitproc)visit_move, |
| 583 | (void *)finalizers); |
| 584 | } |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 585 | } |
| 586 | |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 587 | /* Clear all weakrefs to unreachable objects, and if such a weakref has a |
| 588 | * callback, invoke it if necessary. Note that it's possible for such |
| 589 | * weakrefs to be outside the unreachable set -- indeed, those are precisely |
| 590 | * the weakrefs whose callbacks must be invoked. See gc_weakref.txt for |
| 591 | * overview & some details. Some weakrefs with callbacks may be reclaimed |
| 592 | * directly by this routine; the number reclaimed is the return value. Other |
| 593 | * weakrefs with callbacks may be moved into the `old` generation. Objects |
| 594 | * moved into `old` have gc_refs set to GC_REACHABLE; the objects remaining in |
| 595 | * unreachable are left at GC_TENTATIVELY_UNREACHABLE. When this returns, |
| 596 | * no object in `unreachable` is weakly referenced anymore. |
Tim Peters | 403a203 | 2003-11-20 21:21:46 +0000 | [diff] [blame] | 597 | */ |
| 598 | static int |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 599 | handle_weakrefs(PyGC_Head *unreachable, PyGC_Head *old) |
Tim Peters | 403a203 | 2003-11-20 21:21:46 +0000 | [diff] [blame] | 600 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 601 | PyGC_Head *gc; |
| 602 | PyObject *op; /* generally FROM_GC(gc) */ |
| 603 | PyWeakReference *wr; /* generally a cast of op */ |
| 604 | PyGC_Head wrcb_to_call; /* weakrefs with callbacks to call */ |
| 605 | PyGC_Head *next; |
| 606 | int num_freed = 0; |
Tim Peters | 403a203 | 2003-11-20 21:21:46 +0000 | [diff] [blame] | 607 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 608 | gc_list_init(&wrcb_to_call); |
Tim Peters | 403a203 | 2003-11-20 21:21:46 +0000 | [diff] [blame] | 609 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 610 | /* Clear all weakrefs to the objects in unreachable. If such a weakref |
| 611 | * also has a callback, move it into `wrcb_to_call` if the callback |
| 612 | * needs to be invoked. Note that we cannot invoke any callbacks until |
| 613 | * all weakrefs to unreachable objects are cleared, lest the callback |
| 614 | * resurrect an unreachable object via a still-active weakref. We |
| 615 | * make another pass over wrcb_to_call, invoking callbacks, after this |
| 616 | * pass completes. |
| 617 | */ |
| 618 | for (gc = unreachable->gc.gc_next; gc != unreachable; gc = next) { |
| 619 | PyWeakReference **wrlist; |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 620 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 621 | op = FROM_GC(gc); |
| 622 | assert(IS_TENTATIVELY_UNREACHABLE(op)); |
| 623 | next = gc->gc.gc_next; |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 624 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 625 | if (! PyType_SUPPORTS_WEAKREFS(Py_TYPE(op))) |
| 626 | continue; |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 627 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 628 | /* It supports weakrefs. Does it have any? */ |
| 629 | wrlist = (PyWeakReference **) |
| 630 | PyObject_GET_WEAKREFS_LISTPTR(op); |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 631 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 632 | /* `op` may have some weakrefs. March over the list, clear |
| 633 | * all the weakrefs, and move the weakrefs with callbacks |
| 634 | * that must be called into wrcb_to_call. |
| 635 | */ |
| 636 | for (wr = *wrlist; wr != NULL; wr = *wrlist) { |
| 637 | PyGC_Head *wrasgc; /* AS_GC(wr) */ |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 638 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 639 | /* _PyWeakref_ClearRef clears the weakref but leaves |
| 640 | * the callback pointer intact. Obscure: it also |
| 641 | * changes *wrlist. |
| 642 | */ |
| 643 | assert(wr->wr_object == op); |
| 644 | _PyWeakref_ClearRef(wr); |
| 645 | assert(wr->wr_object == Py_None); |
| 646 | if (wr->wr_callback == NULL) |
| 647 | continue; /* no callback */ |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 648 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 649 | /* Headache time. `op` is going away, and is weakly referenced by |
| 650 | * `wr`, which has a callback. Should the callback be invoked? If wr |
| 651 | * is also trash, no: |
| 652 | * |
| 653 | * 1. There's no need to call it. The object and the weakref are |
| 654 | * both going away, so it's legitimate to pretend the weakref is |
| 655 | * going away first. The user has to ensure a weakref outlives its |
| 656 | * referent if they want a guarantee that the wr callback will get |
| 657 | * invoked. |
| 658 | * |
| 659 | * 2. It may be catastrophic to call it. If the callback is also in |
| 660 | * cyclic trash (CT), then although the CT is unreachable from |
| 661 | * outside the current generation, CT may be reachable from the |
| 662 | * callback. Then the callback could resurrect insane objects. |
| 663 | * |
| 664 | * Since the callback is never needed and may be unsafe in this case, |
| 665 | * wr is simply left in the unreachable set. Note that because we |
| 666 | * already called _PyWeakref_ClearRef(wr), its callback will never |
| 667 | * trigger. |
| 668 | * |
| 669 | * OTOH, if wr isn't part of CT, we should invoke the callback: the |
| 670 | * weakref outlived the trash. Note that since wr isn't CT in this |
| 671 | * case, its callback can't be CT either -- wr acted as an external |
| 672 | * root to this generation, and therefore its callback did too. So |
| 673 | * nothing in CT is reachable from the callback either, so it's hard |
| 674 | * to imagine how calling it later could create a problem for us. wr |
| 675 | * is moved to wrcb_to_call in this case. |
| 676 | */ |
| 677 | if (IS_TENTATIVELY_UNREACHABLE(wr)) |
| 678 | continue; |
| 679 | assert(IS_REACHABLE(wr)); |
Tim Peters | cc2a866 | 2004-10-31 22:12:43 +0000 | [diff] [blame] | 680 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 681 | /* Create a new reference so that wr can't go away |
| 682 | * before we can process it again. |
| 683 | */ |
| 684 | Py_INCREF(wr); |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 685 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 686 | /* Move wr to wrcb_to_call, for the next pass. */ |
| 687 | wrasgc = AS_GC(wr); |
| 688 | assert(wrasgc != next); /* wrasgc is reachable, but |
| 689 | next isn't, so they can't |
| 690 | be the same */ |
| 691 | gc_list_move(wrasgc, &wrcb_to_call); |
| 692 | } |
| 693 | } |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 694 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 695 | /* Invoke the callbacks we decided to honor. It's safe to invoke them |
| 696 | * because they can't reference unreachable objects. |
| 697 | */ |
| 698 | while (! gc_list_is_empty(&wrcb_to_call)) { |
| 699 | PyObject *temp; |
| 700 | PyObject *callback; |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 701 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 702 | gc = wrcb_to_call.gc.gc_next; |
| 703 | op = FROM_GC(gc); |
| 704 | assert(IS_REACHABLE(op)); |
| 705 | assert(PyWeakref_Check(op)); |
| 706 | wr = (PyWeakReference *)op; |
| 707 | callback = wr->wr_callback; |
| 708 | assert(callback != NULL); |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 709 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 710 | /* copy-paste of weakrefobject.c's handle_callback() */ |
| 711 | temp = PyObject_CallFunctionObjArgs(callback, wr, NULL); |
| 712 | if (temp == NULL) |
| 713 | PyErr_WriteUnraisable(callback); |
| 714 | else |
| 715 | Py_DECREF(temp); |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 716 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 717 | /* Give up the reference we created in the first pass. When |
| 718 | * op's refcount hits 0 (which it may or may not do right now), |
| 719 | * op's tp_dealloc will decref op->wr_callback too. Note |
| 720 | * that the refcount probably will hit 0 now, and because this |
| 721 | * weakref was reachable to begin with, gc didn't already |
| 722 | * add it to its count of freed objects. Example: a reachable |
| 723 | * weak value dict maps some key to this reachable weakref. |
| 724 | * The callback removes this key->weakref mapping from the |
| 725 | * dict, leaving no other references to the weakref (excepting |
| 726 | * ours). |
| 727 | */ |
| 728 | Py_DECREF(op); |
| 729 | if (wrcb_to_call.gc.gc_next == gc) { |
| 730 | /* object is still alive -- move it */ |
| 731 | gc_list_move(gc, old); |
| 732 | } |
| 733 | else |
| 734 | ++num_freed; |
| 735 | } |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 736 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 737 | return num_freed; |
Tim Peters | 403a203 | 2003-11-20 21:21:46 +0000 | [diff] [blame] | 738 | } |
| 739 | |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 740 | static void |
Jeremy Hylton | 0625777 | 2000-08-31 15:10:24 +0000 | [diff] [blame] | 741 | debug_cycle(char *msg, PyObject *op) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 742 | { |
Victor Stinner | 499dfcf | 2011-03-21 13:26:24 +0100 | [diff] [blame] | 743 | PySys_FormatStderr("gc: %s <%s %p>\n", |
| 744 | msg, Py_TYPE(op)->tp_name, op); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 745 | } |
| 746 | |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 747 | /* Handle uncollectable garbage (cycles with tp_del slots, and stuff reachable |
Tim Peters | bf384c2 | 2003-04-06 00:11:39 +0000 | [diff] [blame] | 748 | * only from such cycles). |
Tim Peters | f6b8045 | 2003-04-07 19:21:15 +0000 | [diff] [blame] | 749 | * If DEBUG_SAVEALL, all objects in finalizers are appended to the module |
| 750 | * garbage list (a Python list), else only the objects in finalizers with |
| 751 | * __del__ methods are appended to garbage. All objects in finalizers are |
| 752 | * merged into the old list regardless. |
Tim Peters | 259272b | 2003-04-06 19:41:39 +0000 | [diff] [blame] | 753 | * Returns 0 if all OK, <0 on error (out of memory to grow the garbage list). |
| 754 | * The finalizers list is made empty on a successful return. |
Tim Peters | bf384c2 | 2003-04-06 00:11:39 +0000 | [diff] [blame] | 755 | */ |
Tim Peters | 259272b | 2003-04-06 19:41:39 +0000 | [diff] [blame] | 756 | static int |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 757 | handle_legacy_finalizers(PyGC_Head *finalizers, PyGC_Head *old) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 758 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 759 | PyGC_Head *gc = finalizers->gc.gc_next; |
Tim Peters | f6b8045 | 2003-04-07 19:21:15 +0000 | [diff] [blame] | 760 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 761 | if (garbage == NULL) { |
| 762 | garbage = PyList_New(0); |
| 763 | if (garbage == NULL) |
| 764 | Py_FatalError("gc couldn't create gc.garbage list"); |
| 765 | } |
| 766 | for (; gc != finalizers; gc = gc->gc.gc_next) { |
| 767 | PyObject *op = FROM_GC(gc); |
Tim Peters | f6b8045 | 2003-04-07 19:21:15 +0000 | [diff] [blame] | 768 | |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 769 | if ((debug & DEBUG_SAVEALL) || has_legacy_finalizer(op)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 770 | if (PyList_Append(garbage, op) < 0) |
| 771 | return -1; |
| 772 | } |
| 773 | } |
Tim Peters | f6b8045 | 2003-04-07 19:21:15 +0000 | [diff] [blame] | 774 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 775 | gc_list_merge(finalizers, old); |
| 776 | return 0; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 777 | } |
| 778 | |
Tim Peters | 5fbc7b1 | 2014-05-08 17:42:19 -0500 | [diff] [blame] | 779 | /* Run first-time finalizers (if any) on all the objects in collectable. |
| 780 | * Note that this may remove some (or even all) of the objects from the |
| 781 | * list, due to refcounts falling to 0. |
| 782 | */ |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 783 | static void |
Tim Peters | 5fbc7b1 | 2014-05-08 17:42:19 -0500 | [diff] [blame] | 784 | finalize_garbage(PyGC_Head *collectable) |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 785 | { |
| 786 | destructor finalize; |
Tim Peters | 5fbc7b1 | 2014-05-08 17:42:19 -0500 | [diff] [blame] | 787 | PyGC_Head seen; |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 788 | |
Tim Peters | 5fbc7b1 | 2014-05-08 17:42:19 -0500 | [diff] [blame] | 789 | /* While we're going through the loop, `finalize(op)` may cause op, or |
| 790 | * other objects, to be reclaimed via refcounts falling to zero. So |
| 791 | * there's little we can rely on about the structure of the input |
| 792 | * `collectable` list across iterations. For safety, we always take the |
| 793 | * first object in that list and move it to a temporary `seen` list. |
| 794 | * If objects vanish from the `collectable` and `seen` lists we don't |
| 795 | * care. |
| 796 | */ |
| 797 | gc_list_init(&seen); |
| 798 | |
| 799 | while (!gc_list_is_empty(collectable)) { |
| 800 | PyGC_Head *gc = collectable->gc.gc_next; |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 801 | PyObject *op = FROM_GC(gc); |
Tim Peters | 5fbc7b1 | 2014-05-08 17:42:19 -0500 | [diff] [blame] | 802 | gc_list_move(gc, &seen); |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 803 | if (!_PyGCHead_FINALIZED(gc) && |
Tim Peters | 5fbc7b1 | 2014-05-08 17:42:19 -0500 | [diff] [blame] | 804 | PyType_HasFeature(Py_TYPE(op), Py_TPFLAGS_HAVE_FINALIZE) && |
| 805 | (finalize = Py_TYPE(op)->tp_finalize) != NULL) { |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 806 | _PyGCHead_SET_FINALIZED(gc, 1); |
| 807 | Py_INCREF(op); |
| 808 | finalize(op); |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 809 | Py_DECREF(op); |
| 810 | } |
| 811 | } |
Tim Peters | 5fbc7b1 | 2014-05-08 17:42:19 -0500 | [diff] [blame] | 812 | gc_list_merge(&seen, collectable); |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | /* Walk the collectable list and check that they are really unreachable |
| 816 | from the outside (some objects could have been resurrected by a |
| 817 | finalizer). */ |
| 818 | static int |
| 819 | check_garbage(PyGC_Head *collectable) |
| 820 | { |
| 821 | PyGC_Head *gc; |
| 822 | for (gc = collectable->gc.gc_next; gc != collectable; |
| 823 | gc = gc->gc.gc_next) { |
| 824 | _PyGCHead_SET_REFS(gc, Py_REFCNT(FROM_GC(gc))); |
| 825 | assert(_PyGCHead_REFS(gc) != 0); |
| 826 | } |
| 827 | subtract_refs(collectable); |
| 828 | for (gc = collectable->gc.gc_next; gc != collectable; |
| 829 | gc = gc->gc.gc_next) { |
| 830 | assert(_PyGCHead_REFS(gc) >= 0); |
| 831 | if (_PyGCHead_REFS(gc) != 0) |
| 832 | return -1; |
| 833 | } |
| 834 | return 0; |
| 835 | } |
| 836 | |
| 837 | static void |
| 838 | revive_garbage(PyGC_Head *collectable) |
| 839 | { |
| 840 | PyGC_Head *gc; |
| 841 | for (gc = collectable->gc.gc_next; gc != collectable; |
| 842 | gc = gc->gc.gc_next) { |
| 843 | _PyGCHead_SET_REFS(gc, GC_REACHABLE); |
| 844 | } |
| 845 | } |
| 846 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 847 | /* Break reference cycles by clearing the containers involved. This is |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 848 | * 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] | 849 | * objects may be freed. It is possible I screwed something up here. |
| 850 | */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 851 | static void |
Jeremy Hylton | ce136e9 | 2003-04-04 19:59:06 +0000 | [diff] [blame] | 852 | delete_garbage(PyGC_Head *collectable, PyGC_Head *old) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 853 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 854 | inquiry clear; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 855 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 856 | while (!gc_list_is_empty(collectable)) { |
| 857 | PyGC_Head *gc = collectable->gc.gc_next; |
| 858 | PyObject *op = FROM_GC(gc); |
Tim Peters | 8839617 | 2002-06-30 17:56:40 +0000 | [diff] [blame] | 859 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 860 | if (debug & DEBUG_SAVEALL) { |
| 861 | PyList_Append(garbage, op); |
| 862 | } |
| 863 | else { |
| 864 | if ((clear = Py_TYPE(op)->tp_clear) != NULL) { |
| 865 | Py_INCREF(op); |
| 866 | clear(op); |
| 867 | Py_DECREF(op); |
| 868 | } |
| 869 | } |
| 870 | if (collectable->gc.gc_next == gc) { |
| 871 | /* object is still alive, move it, it may die later */ |
| 872 | gc_list_move(gc, old); |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 873 | _PyGCHead_SET_REFS(gc, GC_REACHABLE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 874 | } |
| 875 | } |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 876 | } |
| 877 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 878 | /* Clear all free lists |
| 879 | * All free lists are cleared during the collection of the highest generation. |
| 880 | * Allocated items in the free list may keep a pymalloc arena occupied. |
| 881 | * Clearing the free lists may give back memory to the OS earlier. |
| 882 | */ |
| 883 | static void |
| 884 | clear_freelists(void) |
| 885 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 886 | (void)PyMethod_ClearFreeList(); |
| 887 | (void)PyFrame_ClearFreeList(); |
| 888 | (void)PyCFunction_ClearFreeList(); |
| 889 | (void)PyTuple_ClearFreeList(); |
| 890 | (void)PyUnicode_ClearFreeList(); |
| 891 | (void)PyFloat_ClearFreeList(); |
Antoine Pitrou | 9a812cb | 2011-11-15 00:00:12 +0100 | [diff] [blame] | 892 | (void)PyList_ClearFreeList(); |
| 893 | (void)PyDict_ClearFreeList(); |
Antoine Pitrou | 093ce9c | 2011-12-16 11:24:27 +0100 | [diff] [blame] | 894 | (void)PySet_ClearFreeList(); |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 895 | } |
| 896 | |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 897 | /* This is the main function. Read this to understand how the |
| 898 | * collection process works. */ |
Neal Norwitz | 7b216c5 | 2006-03-04 20:01:53 +0000 | [diff] [blame] | 899 | static Py_ssize_t |
Antoine Pitrou | fef34e3 | 2013-05-19 01:11:58 +0200 | [diff] [blame] | 900 | collect(int generation, Py_ssize_t *n_collected, Py_ssize_t *n_uncollectable, |
| 901 | int nofail) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 902 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 903 | int i; |
| 904 | Py_ssize_t m = 0; /* # objects collected */ |
| 905 | Py_ssize_t n = 0; /* # unreachable objects that couldn't be collected */ |
| 906 | PyGC_Head *young; /* the generation we are examining */ |
| 907 | PyGC_Head *old; /* next older generation */ |
| 908 | PyGC_Head unreachable; /* non-problematic unreachable trash */ |
| 909 | PyGC_Head finalizers; /* objects with, & reachable from, __del__ */ |
| 910 | PyGC_Head *gc; |
Antoine Pitrou | 40f6b12 | 2014-05-24 19:21:53 +0200 | [diff] [blame] | 911 | _PyTime_timeval t1; |
| 912 | |
Antoine Pitrou | d4156c1 | 2012-10-30 22:43:19 +0100 | [diff] [blame] | 913 | struct gc_generation_stats *stats = &generation_stats[generation]; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 914 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 915 | if (debug & DEBUG_STATS) { |
| 916 | PySys_WriteStderr("gc: collecting generation %d...\n", |
| 917 | generation); |
| 918 | PySys_WriteStderr("gc: objects in each generation:"); |
| 919 | for (i = 0; i < NUM_GENERATIONS; i++) |
Antoine Pitrou | ded3c1b | 2014-05-24 19:24:40 +0200 | [diff] [blame] | 920 | PySys_FormatStderr(" %zd", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 921 | gc_list_size(GEN_HEAD(i))); |
Victor Stinner | ae58649 | 2014-09-02 23:18:25 +0200 | [diff] [blame] | 922 | _PyTime_monotonic(&t1); |
Antoine Pitrou | 40f6b12 | 2014-05-24 19:21:53 +0200 | [diff] [blame] | 923 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 924 | PySys_WriteStderr("\n"); |
| 925 | } |
Neil Schemenauer | 2880ae5 | 2002-05-04 05:35:20 +0000 | [diff] [blame] | 926 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 927 | /* update collection and allocation counters */ |
| 928 | if (generation+1 < NUM_GENERATIONS) |
| 929 | generations[generation+1].count += 1; |
| 930 | for (i = 0; i <= generation; i++) |
| 931 | generations[i].count = 0; |
Neil Schemenauer | 2880ae5 | 2002-05-04 05:35:20 +0000 | [diff] [blame] | 932 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 933 | /* merge younger generations with one we are currently collecting */ |
| 934 | for (i = 0; i < generation; i++) { |
| 935 | gc_list_merge(GEN_HEAD(i), GEN_HEAD(generation)); |
| 936 | } |
Neil Schemenauer | 2880ae5 | 2002-05-04 05:35:20 +0000 | [diff] [blame] | 937 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 938 | /* handy references */ |
| 939 | young = GEN_HEAD(generation); |
| 940 | if (generation < NUM_GENERATIONS-1) |
| 941 | old = GEN_HEAD(generation+1); |
| 942 | else |
| 943 | old = young; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 944 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 945 | /* Using ob_refcnt and gc_refs, calculate which objects in the |
| 946 | * container set are reachable from outside the set (i.e., have a |
| 947 | * refcount greater than 0 when all the references within the |
| 948 | * set are taken into account). |
| 949 | */ |
| 950 | update_refs(young); |
| 951 | subtract_refs(young); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 952 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 953 | /* Leave everything reachable from outside young in young, and move |
| 954 | * everything else (in young) to unreachable. |
| 955 | * NOTE: This used to move the reachable objects into a reachable |
| 956 | * set instead. But most things usually turn out to be reachable, |
| 957 | * so it's more efficient to move the unreachable things. |
| 958 | */ |
| 959 | gc_list_init(&unreachable); |
| 960 | move_unreachable(young, &unreachable); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 961 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 962 | /* Move reachable objects to next generation. */ |
| 963 | if (young != old) { |
| 964 | if (generation == NUM_GENERATIONS - 2) { |
| 965 | long_lived_pending += gc_list_size(young); |
| 966 | } |
| 967 | gc_list_merge(young, old); |
| 968 | } |
| 969 | else { |
Antoine Pitrou | e1ad3da | 2012-05-28 22:22:34 +0200 | [diff] [blame] | 970 | /* We only untrack dicts in full collections, to avoid quadratic |
| 971 | dict build-up. See issue #14775. */ |
| 972 | untrack_dicts(young); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 973 | long_lived_pending = 0; |
| 974 | long_lived_total = gc_list_size(young); |
| 975 | } |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 976 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 977 | /* All objects in unreachable are trash, but objects reachable from |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 978 | * legacy finalizers (e.g. tp_del) can't safely be deleted. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 979 | */ |
| 980 | gc_list_init(&finalizers); |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 981 | move_legacy_finalizers(&unreachable, &finalizers); |
| 982 | /* finalizers contains the unreachable objects with a legacy finalizer; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 983 | * unreachable objects reachable *from* those are also uncollectable, |
| 984 | * and we move those into the finalizers list too. |
| 985 | */ |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 986 | move_legacy_finalizer_reachable(&finalizers); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 987 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 988 | /* Collect statistics on collectable objects found and print |
| 989 | * debugging information. |
| 990 | */ |
| 991 | for (gc = unreachable.gc.gc_next; gc != &unreachable; |
| 992 | gc = gc->gc.gc_next) { |
| 993 | m++; |
| 994 | if (debug & DEBUG_COLLECTABLE) { |
| 995 | debug_cycle("collectable", FROM_GC(gc)); |
| 996 | } |
| 997 | } |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 998 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 999 | /* Clear weakrefs and invoke callbacks as necessary. */ |
| 1000 | m += handle_weakrefs(&unreachable, old); |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 1001 | |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 1002 | /* Call tp_finalize on objects which have one. */ |
Tim Peters | 5fbc7b1 | 2014-05-08 17:42:19 -0500 | [diff] [blame] | 1003 | finalize_garbage(&unreachable); |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 1004 | |
| 1005 | if (check_garbage(&unreachable)) { |
| 1006 | revive_garbage(&unreachable); |
| 1007 | gc_list_merge(&unreachable, old); |
| 1008 | } |
| 1009 | else { |
| 1010 | /* Call tp_clear on objects in the unreachable set. This will cause |
| 1011 | * the reference cycles to be broken. It may also cause some objects |
| 1012 | * in finalizers to be freed. |
| 1013 | */ |
| 1014 | delete_garbage(&unreachable, old); |
| 1015 | } |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1016 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1017 | /* Collect statistics on uncollectable objects found and print |
| 1018 | * debugging information. */ |
| 1019 | for (gc = finalizers.gc.gc_next; |
| 1020 | gc != &finalizers; |
| 1021 | gc = gc->gc.gc_next) { |
| 1022 | n++; |
| 1023 | if (debug & DEBUG_UNCOLLECTABLE) |
| 1024 | debug_cycle("uncollectable", FROM_GC(gc)); |
| 1025 | } |
| 1026 | if (debug & DEBUG_STATS) { |
Antoine Pitrou | 40f6b12 | 2014-05-24 19:21:53 +0200 | [diff] [blame] | 1027 | _PyTime_timeval t2; |
Victor Stinner | ae58649 | 2014-09-02 23:18:25 +0200 | [diff] [blame] | 1028 | _PyTime_monotonic(&t2); |
Antoine Pitrou | 40f6b12 | 2014-05-24 19:21:53 +0200 | [diff] [blame] | 1029 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1030 | if (m == 0 && n == 0) |
| 1031 | PySys_WriteStderr("gc: done"); |
| 1032 | else |
Antoine Pitrou | ded3c1b | 2014-05-24 19:24:40 +0200 | [diff] [blame] | 1033 | PySys_FormatStderr( |
| 1034 | "gc: done, %zd unreachable, %zd uncollectable", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1035 | n+m, n); |
Antoine Pitrou | 40f6b12 | 2014-05-24 19:21:53 +0200 | [diff] [blame] | 1036 | PySys_WriteStderr(", %.4fs elapsed\n", _PyTime_INTERVAL(t1, t2)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1037 | } |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1038 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1039 | /* Append instances in the uncollectable set to a Python |
| 1040 | * reachable list of garbage. The programmer has to deal with |
| 1041 | * this if they insist on creating this type of structure. |
| 1042 | */ |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 1043 | (void)handle_legacy_finalizers(&finalizers, old); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1044 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1045 | /* Clear free list only during the collection of the highest |
| 1046 | * generation */ |
| 1047 | if (generation == NUM_GENERATIONS-1) { |
| 1048 | clear_freelists(); |
| 1049 | } |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 1050 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1051 | if (PyErr_Occurred()) { |
Antoine Pitrou | fef34e3 | 2013-05-19 01:11:58 +0200 | [diff] [blame] | 1052 | if (nofail) { |
| 1053 | PyErr_Clear(); |
| 1054 | } |
| 1055 | else { |
| 1056 | if (gc_str == NULL) |
| 1057 | gc_str = PyUnicode_FromString("garbage collection"); |
| 1058 | PyErr_WriteUnraisable(gc_str); |
| 1059 | Py_FatalError("unexpected exception during garbage collection"); |
| 1060 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1061 | } |
Kristján Valur Jónsson | 69c6352 | 2012-04-15 11:41:32 +0000 | [diff] [blame] | 1062 | |
Antoine Pitrou | d4156c1 | 2012-10-30 22:43:19 +0100 | [diff] [blame] | 1063 | /* Update stats */ |
Kristján Valur Jónsson | 69c6352 | 2012-04-15 11:41:32 +0000 | [diff] [blame] | 1064 | if (n_collected) |
| 1065 | *n_collected = m; |
| 1066 | if (n_uncollectable) |
| 1067 | *n_uncollectable = n; |
Antoine Pitrou | d4156c1 | 2012-10-30 22:43:19 +0100 | [diff] [blame] | 1068 | stats->collections++; |
| 1069 | stats->collected += m; |
| 1070 | stats->uncollectable += n; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1071 | return n+m; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1072 | } |
| 1073 | |
Kristján Valur Jónsson | 69c6352 | 2012-04-15 11:41:32 +0000 | [diff] [blame] | 1074 | /* Invoke progress callbacks to notify clients that garbage collection |
| 1075 | * is starting or stopping |
| 1076 | */ |
| 1077 | static void |
| 1078 | invoke_gc_callback(const char *phase, int generation, |
| 1079 | Py_ssize_t collected, Py_ssize_t uncollectable) |
| 1080 | { |
| 1081 | Py_ssize_t i; |
| 1082 | PyObject *info = NULL; |
| 1083 | |
| 1084 | /* we may get called very early */ |
| 1085 | if (callbacks == NULL) |
| 1086 | return; |
| 1087 | /* The local variable cannot be rebound, check it for sanity */ |
| 1088 | assert(callbacks != NULL && PyList_CheckExact(callbacks)); |
| 1089 | if (PyList_GET_SIZE(callbacks) != 0) { |
| 1090 | info = Py_BuildValue("{sisnsn}", |
| 1091 | "generation", generation, |
| 1092 | "collected", collected, |
| 1093 | "uncollectable", uncollectable); |
| 1094 | if (info == NULL) { |
| 1095 | PyErr_WriteUnraisable(NULL); |
| 1096 | return; |
| 1097 | } |
| 1098 | } |
| 1099 | for (i=0; i<PyList_GET_SIZE(callbacks); i++) { |
| 1100 | PyObject *r, *cb = PyList_GET_ITEM(callbacks, i); |
| 1101 | Py_INCREF(cb); /* make sure cb doesn't go away */ |
| 1102 | r = PyObject_CallFunction(cb, "sO", phase, info); |
| 1103 | Py_XDECREF(r); |
| 1104 | if (r == NULL) |
| 1105 | PyErr_WriteUnraisable(cb); |
| 1106 | Py_DECREF(cb); |
| 1107 | } |
| 1108 | Py_XDECREF(info); |
| 1109 | } |
| 1110 | |
| 1111 | /* Perform garbage collection of a generation and invoke |
| 1112 | * progress callbacks. |
| 1113 | */ |
| 1114 | static Py_ssize_t |
| 1115 | collect_with_callback(int generation) |
| 1116 | { |
| 1117 | Py_ssize_t result, collected, uncollectable; |
| 1118 | invoke_gc_callback("start", generation, 0, 0); |
Antoine Pitrou | fef34e3 | 2013-05-19 01:11:58 +0200 | [diff] [blame] | 1119 | result = collect(generation, &collected, &uncollectable, 0); |
Kristján Valur Jónsson | 69c6352 | 2012-04-15 11:41:32 +0000 | [diff] [blame] | 1120 | invoke_gc_callback("stop", generation, collected, uncollectable); |
| 1121 | return result; |
| 1122 | } |
| 1123 | |
Neal Norwitz | 7b216c5 | 2006-03-04 20:01:53 +0000 | [diff] [blame] | 1124 | static Py_ssize_t |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1125 | collect_generations(void) |
| 1126 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1127 | int i; |
| 1128 | Py_ssize_t n = 0; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1129 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1130 | /* Find the oldest generation (highest numbered) where the count |
| 1131 | * exceeds the threshold. Objects in the that generation and |
| 1132 | * generations younger than it will be collected. */ |
| 1133 | for (i = NUM_GENERATIONS-1; i >= 0; i--) { |
| 1134 | if (generations[i].count > generations[i].threshold) { |
| 1135 | /* Avoid quadratic performance degradation in number |
| 1136 | of tracked objects. See comments at the beginning |
| 1137 | of this file, and issue #4074. |
| 1138 | */ |
| 1139 | if (i == NUM_GENERATIONS - 1 |
| 1140 | && long_lived_pending < long_lived_total / 4) |
| 1141 | continue; |
Kristján Valur Jónsson | 69c6352 | 2012-04-15 11:41:32 +0000 | [diff] [blame] | 1142 | n = collect_with_callback(i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1143 | break; |
| 1144 | } |
| 1145 | } |
| 1146 | return n; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1149 | PyDoc_STRVAR(gc_enable__doc__, |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 1150 | "enable() -> None\n" |
| 1151 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1152 | "Enable automatic garbage collection.\n"); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1153 | |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 1154 | static PyObject * |
Tim Peters | 50c61d5 | 2003-04-06 01:50:50 +0000 | [diff] [blame] | 1155 | gc_enable(PyObject *self, PyObject *noargs) |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 1156 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1157 | enabled = 1; |
| 1158 | Py_INCREF(Py_None); |
| 1159 | return Py_None; |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1162 | PyDoc_STRVAR(gc_disable__doc__, |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 1163 | "disable() -> None\n" |
| 1164 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1165 | "Disable automatic garbage collection.\n"); |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 1166 | |
| 1167 | static PyObject * |
Tim Peters | 50c61d5 | 2003-04-06 01:50:50 +0000 | [diff] [blame] | 1168 | gc_disable(PyObject *self, PyObject *noargs) |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 1169 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1170 | enabled = 0; |
| 1171 | Py_INCREF(Py_None); |
| 1172 | return Py_None; |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 1173 | } |
| 1174 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1175 | PyDoc_STRVAR(gc_isenabled__doc__, |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 1176 | "isenabled() -> status\n" |
| 1177 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1178 | "Returns true if automatic garbage collection is enabled.\n"); |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 1179 | |
| 1180 | static PyObject * |
Tim Peters | 50c61d5 | 2003-04-06 01:50:50 +0000 | [diff] [blame] | 1181 | gc_isenabled(PyObject *self, PyObject *noargs) |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 1182 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1183 | return PyBool_FromLong((long)enabled); |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 1184 | } |
| 1185 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1186 | PyDoc_STRVAR(gc_collect__doc__, |
Barry Warsaw | d3c38ff | 2006-03-07 09:46:03 +0000 | [diff] [blame] | 1187 | "collect([generation]) -> n\n" |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1188 | "\n" |
Barry Warsaw | d3c38ff | 2006-03-07 09:46:03 +0000 | [diff] [blame] | 1189 | "With no arguments, run a full collection. The optional argument\n" |
| 1190 | "may be an integer specifying which generation to collect. A ValueError\n" |
| 1191 | "is raised if the generation number is invalid.\n\n" |
| 1192 | "The number of unreachable objects is returned.\n"); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1193 | |
| 1194 | static PyObject * |
Barry Warsaw | d3c38ff | 2006-03-07 09:46:03 +0000 | [diff] [blame] | 1195 | gc_collect(PyObject *self, PyObject *args, PyObject *kws) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1196 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1197 | static char *keywords[] = {"generation", NULL}; |
| 1198 | int genarg = NUM_GENERATIONS - 1; |
| 1199 | Py_ssize_t n; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1200 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1201 | if (!PyArg_ParseTupleAndKeywords(args, kws, "|i", keywords, &genarg)) |
| 1202 | return NULL; |
Barry Warsaw | d3c38ff | 2006-03-07 09:46:03 +0000 | [diff] [blame] | 1203 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1204 | else if (genarg < 0 || genarg >= NUM_GENERATIONS) { |
| 1205 | PyErr_SetString(PyExc_ValueError, "invalid generation"); |
| 1206 | return NULL; |
| 1207 | } |
Barry Warsaw | d3c38ff | 2006-03-07 09:46:03 +0000 | [diff] [blame] | 1208 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1209 | if (collecting) |
| 1210 | n = 0; /* already collecting, don't do anything */ |
| 1211 | else { |
| 1212 | collecting = 1; |
Kristján Valur Jónsson | 69c6352 | 2012-04-15 11:41:32 +0000 | [diff] [blame] | 1213 | n = collect_with_callback(genarg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1214 | collecting = 0; |
| 1215 | } |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1216 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1217 | return PyLong_FromSsize_t(n); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1218 | } |
| 1219 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1220 | PyDoc_STRVAR(gc_set_debug__doc__, |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1221 | "set_debug(flags) -> None\n" |
| 1222 | "\n" |
| 1223 | "Set the garbage collection debugging flags. Debugging information is\n" |
| 1224 | "written to sys.stderr.\n" |
| 1225 | "\n" |
| 1226 | "flags is an integer and can have the following bits turned on:\n" |
| 1227 | "\n" |
| 1228 | " DEBUG_STATS - Print statistics during collection.\n" |
| 1229 | " DEBUG_COLLECTABLE - Print collectable objects found.\n" |
| 1230 | " DEBUG_UNCOLLECTABLE - Print unreachable but uncollectable objects found.\n" |
Neil Schemenauer | 544de1e | 2000-09-22 15:22:38 +0000 | [diff] [blame] | 1231 | " DEBUG_SAVEALL - Save objects to gc.garbage rather than freeing them.\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1232 | " DEBUG_LEAK - Debug leaking programs (everything but STATS).\n"); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1233 | |
| 1234 | static PyObject * |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 1235 | gc_set_debug(PyObject *self, PyObject *args) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1236 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1237 | if (!PyArg_ParseTuple(args, "i:set_debug", &debug)) |
| 1238 | return NULL; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1239 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1240 | Py_INCREF(Py_None); |
| 1241 | return Py_None; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1242 | } |
| 1243 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1244 | PyDoc_STRVAR(gc_get_debug__doc__, |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1245 | "get_debug() -> flags\n" |
| 1246 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1247 | "Get the garbage collection debugging flags.\n"); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1248 | |
| 1249 | static PyObject * |
Tim Peters | 50c61d5 | 2003-04-06 01:50:50 +0000 | [diff] [blame] | 1250 | gc_get_debug(PyObject *self, PyObject *noargs) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1251 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1252 | return Py_BuildValue("i", debug); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1253 | } |
| 1254 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1255 | PyDoc_STRVAR(gc_set_thresh__doc__, |
Neal Norwitz | 2a47c0f | 2002-01-29 00:53:41 +0000 | [diff] [blame] | 1256 | "set_threshold(threshold0, [threshold1, threshold2]) -> None\n" |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1257 | "\n" |
| 1258 | "Sets the collection thresholds. Setting threshold0 to zero disables\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1259 | "collection.\n"); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1260 | |
| 1261 | static PyObject * |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 1262 | gc_set_thresh(PyObject *self, PyObject *args) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1263 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1264 | int i; |
| 1265 | if (!PyArg_ParseTuple(args, "i|ii:set_threshold", |
| 1266 | &generations[0].threshold, |
| 1267 | &generations[1].threshold, |
| 1268 | &generations[2].threshold)) |
| 1269 | return NULL; |
| 1270 | for (i = 2; i < NUM_GENERATIONS; i++) { |
| 1271 | /* generations higher than 2 get the same threshold */ |
| 1272 | generations[i].threshold = generations[2].threshold; |
| 1273 | } |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1274 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1275 | Py_INCREF(Py_None); |
| 1276 | return Py_None; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1277 | } |
| 1278 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1279 | PyDoc_STRVAR(gc_get_thresh__doc__, |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1280 | "get_threshold() -> (threshold0, threshold1, threshold2)\n" |
| 1281 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1282 | "Return the current collection thresholds\n"); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1283 | |
| 1284 | static PyObject * |
Tim Peters | 50c61d5 | 2003-04-06 01:50:50 +0000 | [diff] [blame] | 1285 | gc_get_thresh(PyObject *self, PyObject *noargs) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1286 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1287 | return Py_BuildValue("(iii)", |
| 1288 | generations[0].threshold, |
| 1289 | generations[1].threshold, |
| 1290 | generations[2].threshold); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1291 | } |
| 1292 | |
Barry Warsaw | d3c38ff | 2006-03-07 09:46:03 +0000 | [diff] [blame] | 1293 | PyDoc_STRVAR(gc_get_count__doc__, |
| 1294 | "get_count() -> (count0, count1, count2)\n" |
| 1295 | "\n" |
| 1296 | "Return the current collection counts\n"); |
| 1297 | |
| 1298 | static PyObject * |
| 1299 | gc_get_count(PyObject *self, PyObject *noargs) |
| 1300 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1301 | return Py_BuildValue("(iii)", |
| 1302 | generations[0].count, |
| 1303 | generations[1].count, |
| 1304 | generations[2].count); |
Barry Warsaw | d3c38ff | 2006-03-07 09:46:03 +0000 | [diff] [blame] | 1305 | } |
| 1306 | |
Neil Schemenauer | 48c7034 | 2001-08-09 15:38:31 +0000 | [diff] [blame] | 1307 | static int |
Martin v. Löwis | 560da62 | 2001-11-24 09:24:51 +0000 | [diff] [blame] | 1308 | referrersvisit(PyObject* obj, PyObject *objs) |
Neil Schemenauer | 48c7034 | 2001-08-09 15:38:31 +0000 | [diff] [blame] | 1309 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1310 | Py_ssize_t i; |
| 1311 | for (i = 0; i < PyTuple_GET_SIZE(objs); i++) |
| 1312 | if (PyTuple_GET_ITEM(objs, i) == obj) |
| 1313 | return 1; |
| 1314 | return 0; |
Neil Schemenauer | 48c7034 | 2001-08-09 15:38:31 +0000 | [diff] [blame] | 1315 | } |
| 1316 | |
Neil Schemenauer | 17e7be6 | 2001-08-10 14:46:47 +0000 | [diff] [blame] | 1317 | static int |
Martin v. Löwis | 560da62 | 2001-11-24 09:24:51 +0000 | [diff] [blame] | 1318 | gc_referrers_for(PyObject *objs, PyGC_Head *list, PyObject *resultlist) |
Neil Schemenauer | 48c7034 | 2001-08-09 15:38:31 +0000 | [diff] [blame] | 1319 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1320 | PyGC_Head *gc; |
| 1321 | PyObject *obj; |
| 1322 | traverseproc traverse; |
| 1323 | for (gc = list->gc.gc_next; gc != list; gc = gc->gc.gc_next) { |
| 1324 | obj = FROM_GC(gc); |
| 1325 | traverse = Py_TYPE(obj)->tp_traverse; |
| 1326 | if (obj == objs || obj == resultlist) |
| 1327 | continue; |
| 1328 | if (traverse(obj, (visitproc)referrersvisit, objs)) { |
| 1329 | if (PyList_Append(resultlist, obj) < 0) |
| 1330 | return 0; /* error */ |
| 1331 | } |
| 1332 | } |
| 1333 | return 1; /* no error */ |
Neil Schemenauer | 48c7034 | 2001-08-09 15:38:31 +0000 | [diff] [blame] | 1334 | } |
| 1335 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1336 | PyDoc_STRVAR(gc_get_referrers__doc__, |
Martin v. Löwis | 560da62 | 2001-11-24 09:24:51 +0000 | [diff] [blame] | 1337 | "get_referrers(*objs) -> list\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1338 | Return the list of objects that directly refer to any of objs."); |
Neil Schemenauer | 48c7034 | 2001-08-09 15:38:31 +0000 | [diff] [blame] | 1339 | |
Neil Schemenauer | 17e7be6 | 2001-08-10 14:46:47 +0000 | [diff] [blame] | 1340 | static PyObject * |
Martin v. Löwis | 560da62 | 2001-11-24 09:24:51 +0000 | [diff] [blame] | 1341 | gc_get_referrers(PyObject *self, PyObject *args) |
Neil Schemenauer | 48c7034 | 2001-08-09 15:38:31 +0000 | [diff] [blame] | 1342 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1343 | int i; |
| 1344 | PyObject *result = PyList_New(0); |
| 1345 | if (!result) return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1346 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1347 | for (i = 0; i < NUM_GENERATIONS; i++) { |
| 1348 | if (!(gc_referrers_for(args, GEN_HEAD(i), result))) { |
| 1349 | Py_DECREF(result); |
| 1350 | return NULL; |
| 1351 | } |
| 1352 | } |
| 1353 | return result; |
Neil Schemenauer | 48c7034 | 2001-08-09 15:38:31 +0000 | [diff] [blame] | 1354 | } |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1355 | |
Tim Peters | 0f81ab6 | 2003-04-08 16:39:48 +0000 | [diff] [blame] | 1356 | /* 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] | 1357 | static int |
Tim Peters | 730f553 | 2003-04-08 17:17:17 +0000 | [diff] [blame] | 1358 | referentsvisit(PyObject *obj, PyObject *list) |
Jeremy Hylton | 5bd378b | 2003-04-03 16:28:38 +0000 | [diff] [blame] | 1359 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1360 | return PyList_Append(list, obj) < 0; |
Jeremy Hylton | 5bd378b | 2003-04-03 16:28:38 +0000 | [diff] [blame] | 1361 | } |
| 1362 | |
Tim Peters | 730f553 | 2003-04-08 17:17:17 +0000 | [diff] [blame] | 1363 | PyDoc_STRVAR(gc_get_referents__doc__, |
| 1364 | "get_referents(*objs) -> list\n\ |
Jeremy Hylton | 059b094 | 2003-04-03 16:29:13 +0000 | [diff] [blame] | 1365 | Return the list of objects that are directly referred to by objs."); |
Jeremy Hylton | 5bd378b | 2003-04-03 16:28:38 +0000 | [diff] [blame] | 1366 | |
| 1367 | static PyObject * |
Tim Peters | 730f553 | 2003-04-08 17:17:17 +0000 | [diff] [blame] | 1368 | gc_get_referents(PyObject *self, PyObject *args) |
Jeremy Hylton | 5bd378b | 2003-04-03 16:28:38 +0000 | [diff] [blame] | 1369 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1370 | Py_ssize_t i; |
| 1371 | PyObject *result = PyList_New(0); |
Tim Peters | 0f81ab6 | 2003-04-08 16:39:48 +0000 | [diff] [blame] | 1372 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1373 | if (result == NULL) |
| 1374 | return NULL; |
Tim Peters | 0f81ab6 | 2003-04-08 16:39:48 +0000 | [diff] [blame] | 1375 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1376 | for (i = 0; i < PyTuple_GET_SIZE(args); i++) { |
| 1377 | traverseproc traverse; |
| 1378 | PyObject *obj = PyTuple_GET_ITEM(args, i); |
Tim Peters | 0f81ab6 | 2003-04-08 16:39:48 +0000 | [diff] [blame] | 1379 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1380 | if (! PyObject_IS_GC(obj)) |
| 1381 | continue; |
| 1382 | traverse = Py_TYPE(obj)->tp_traverse; |
| 1383 | if (! traverse) |
| 1384 | continue; |
| 1385 | if (traverse(obj, (visitproc)referentsvisit, result)) { |
| 1386 | Py_DECREF(result); |
| 1387 | return NULL; |
| 1388 | } |
| 1389 | } |
| 1390 | return result; |
Jeremy Hylton | 5bd378b | 2003-04-03 16:28:38 +0000 | [diff] [blame] | 1391 | } |
| 1392 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1393 | PyDoc_STRVAR(gc_get_objects__doc__, |
Neil Schemenauer | c7c8d8e | 2001-08-09 15:58:59 +0000 | [diff] [blame] | 1394 | "get_objects() -> [...]\n" |
| 1395 | "\n" |
| 1396 | "Return a list of objects tracked by the collector (excluding the list\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1397 | "returned).\n"); |
Neil Schemenauer | c7c8d8e | 2001-08-09 15:58:59 +0000 | [diff] [blame] | 1398 | |
Neil Schemenauer | c7c8d8e | 2001-08-09 15:58:59 +0000 | [diff] [blame] | 1399 | static PyObject * |
Tim Peters | 50c61d5 | 2003-04-06 01:50:50 +0000 | [diff] [blame] | 1400 | gc_get_objects(PyObject *self, PyObject *noargs) |
Neil Schemenauer | c7c8d8e | 2001-08-09 15:58:59 +0000 | [diff] [blame] | 1401 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1402 | int i; |
| 1403 | PyObject* result; |
Neil Schemenauer | c7c8d8e | 2001-08-09 15:58:59 +0000 | [diff] [blame] | 1404 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1405 | result = PyList_New(0); |
| 1406 | if (result == NULL) |
| 1407 | return NULL; |
| 1408 | for (i = 0; i < NUM_GENERATIONS; i++) { |
| 1409 | if (append_objects(result, GEN_HEAD(i))) { |
| 1410 | Py_DECREF(result); |
| 1411 | return NULL; |
| 1412 | } |
| 1413 | } |
| 1414 | return result; |
Neil Schemenauer | c7c8d8e | 2001-08-09 15:58:59 +0000 | [diff] [blame] | 1415 | } |
| 1416 | |
Antoine Pitrou | d4156c1 | 2012-10-30 22:43:19 +0100 | [diff] [blame] | 1417 | PyDoc_STRVAR(gc_get_stats__doc__, |
| 1418 | "get_stats() -> [...]\n" |
| 1419 | "\n" |
| 1420 | "Return a list of dictionaries containing per-generation statistics.\n"); |
| 1421 | |
| 1422 | static PyObject * |
| 1423 | gc_get_stats(PyObject *self, PyObject *noargs) |
| 1424 | { |
| 1425 | int i; |
| 1426 | PyObject *result; |
| 1427 | struct gc_generation_stats stats[NUM_GENERATIONS], *st; |
| 1428 | |
| 1429 | /* To get consistent values despite allocations while constructing |
| 1430 | the result list, we use a snapshot of the running stats. */ |
| 1431 | for (i = 0; i < NUM_GENERATIONS; i++) { |
| 1432 | stats[i] = generation_stats[i]; |
| 1433 | } |
| 1434 | |
| 1435 | result = PyList_New(0); |
| 1436 | if (result == NULL) |
| 1437 | return NULL; |
| 1438 | |
| 1439 | for (i = 0; i < NUM_GENERATIONS; i++) { |
| 1440 | PyObject *dict; |
| 1441 | st = &stats[i]; |
| 1442 | dict = Py_BuildValue("{snsnsn}", |
| 1443 | "collections", st->collections, |
| 1444 | "collected", st->collected, |
| 1445 | "uncollectable", st->uncollectable |
| 1446 | ); |
| 1447 | if (dict == NULL) |
| 1448 | goto error; |
| 1449 | if (PyList_Append(result, dict)) { |
| 1450 | Py_DECREF(dict); |
| 1451 | goto error; |
| 1452 | } |
| 1453 | Py_DECREF(dict); |
| 1454 | } |
| 1455 | return result; |
| 1456 | |
| 1457 | error: |
| 1458 | Py_XDECREF(result); |
| 1459 | return NULL; |
| 1460 | } |
| 1461 | |
| 1462 | |
Antoine Pitrou | 3a652b1 | 2009-03-23 18:52:06 +0000 | [diff] [blame] | 1463 | PyDoc_STRVAR(gc_is_tracked__doc__, |
| 1464 | "is_tracked(obj) -> bool\n" |
| 1465 | "\n" |
| 1466 | "Returns true if the object is tracked by the garbage collector.\n" |
| 1467 | "Simple atomic objects will return false.\n" |
| 1468 | ); |
| 1469 | |
| 1470 | static PyObject * |
| 1471 | gc_is_tracked(PyObject *self, PyObject *obj) |
| 1472 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1473 | PyObject *result; |
| 1474 | |
| 1475 | if (PyObject_IS_GC(obj) && IS_TRACKED(obj)) |
| 1476 | result = Py_True; |
| 1477 | else |
| 1478 | result = Py_False; |
| 1479 | Py_INCREF(result); |
| 1480 | return result; |
Antoine Pitrou | 3a652b1 | 2009-03-23 18:52:06 +0000 | [diff] [blame] | 1481 | } |
| 1482 | |
Neil Schemenauer | c7c8d8e | 2001-08-09 15:58:59 +0000 | [diff] [blame] | 1483 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1484 | PyDoc_STRVAR(gc__doc__, |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1485 | "This module provides access to the garbage collector for reference cycles.\n" |
| 1486 | "\n" |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 1487 | "enable() -- Enable automatic garbage collection.\n" |
| 1488 | "disable() -- Disable automatic garbage collection.\n" |
| 1489 | "isenabled() -- Returns true if automatic collection is enabled.\n" |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1490 | "collect() -- Do a full collection right now.\n" |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1491 | "get_count() -- Return the current collection counts.\n" |
R David Murray | 0e81463 | 2013-12-26 15:11:28 -0500 | [diff] [blame] | 1492 | "get_stats() -- Return list of dictionaries containing per-generation stats.\n" |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1493 | "set_debug() -- Set debugging flags.\n" |
| 1494 | "get_debug() -- Get debugging flags.\n" |
| 1495 | "set_threshold() -- Set the collection thresholds.\n" |
| 1496 | "get_threshold() -- Return the current the collection thresholds.\n" |
Neil Schemenauer | c7c8d8e | 2001-08-09 15:58:59 +0000 | [diff] [blame] | 1497 | "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] | 1498 | "is_tracked() -- Returns true if a given object is tracked.\n" |
Jeremy Hylton | 5bd378b | 2003-04-03 16:28:38 +0000 | [diff] [blame] | 1499 | "get_referrers() -- Return the list of objects that refer to an object.\n" |
Tim Peters | 730f553 | 2003-04-08 17:17:17 +0000 | [diff] [blame] | 1500 | "get_referents() -- Return the list of objects that an object refers to.\n"); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1501 | |
| 1502 | static PyMethodDef GcMethods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1503 | {"enable", gc_enable, METH_NOARGS, gc_enable__doc__}, |
| 1504 | {"disable", gc_disable, METH_NOARGS, gc_disable__doc__}, |
| 1505 | {"isenabled", gc_isenabled, METH_NOARGS, gc_isenabled__doc__}, |
| 1506 | {"set_debug", gc_set_debug, METH_VARARGS, gc_set_debug__doc__}, |
| 1507 | {"get_debug", gc_get_debug, METH_NOARGS, gc_get_debug__doc__}, |
| 1508 | {"get_count", gc_get_count, METH_NOARGS, gc_get_count__doc__}, |
| 1509 | {"set_threshold", gc_set_thresh, METH_VARARGS, gc_set_thresh__doc__}, |
| 1510 | {"get_threshold", gc_get_thresh, METH_NOARGS, gc_get_thresh__doc__}, |
| 1511 | {"collect", (PyCFunction)gc_collect, |
| 1512 | METH_VARARGS | METH_KEYWORDS, gc_collect__doc__}, |
| 1513 | {"get_objects", gc_get_objects,METH_NOARGS, gc_get_objects__doc__}, |
Antoine Pitrou | d4156c1 | 2012-10-30 22:43:19 +0100 | [diff] [blame] | 1514 | {"get_stats", gc_get_stats, METH_NOARGS, gc_get_stats__doc__}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1515 | {"is_tracked", gc_is_tracked, METH_O, gc_is_tracked__doc__}, |
| 1516 | {"get_referrers", gc_get_referrers, METH_VARARGS, |
| 1517 | gc_get_referrers__doc__}, |
| 1518 | {"get_referents", gc_get_referents, METH_VARARGS, |
| 1519 | gc_get_referents__doc__}, |
| 1520 | {NULL, NULL} /* Sentinel */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1521 | }; |
| 1522 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1523 | static struct PyModuleDef gcmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1524 | PyModuleDef_HEAD_INIT, |
Antoine Pitrou | 696e035 | 2010-08-08 22:18:46 +0000 | [diff] [blame] | 1525 | "gc", /* m_name */ |
| 1526 | gc__doc__, /* m_doc */ |
| 1527 | -1, /* m_size */ |
| 1528 | GcMethods, /* m_methods */ |
| 1529 | NULL, /* m_reload */ |
| 1530 | NULL, /* m_traverse */ |
| 1531 | NULL, /* m_clear */ |
| 1532 | NULL /* m_free */ |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1533 | }; |
| 1534 | |
Jason Tishler | 6bc06ec | 2003-09-04 11:59:50 +0000 | [diff] [blame] | 1535 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1536 | PyInit_gc(void) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1537 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1538 | PyObject *m; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1539 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1540 | m = PyModule_Create(&gcmodule); |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1541 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1542 | if (m == NULL) |
| 1543 | return NULL; |
Tim Peters | 1155887 | 2003-04-06 23:30:52 +0000 | [diff] [blame] | 1544 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1545 | if (garbage == NULL) { |
| 1546 | garbage = PyList_New(0); |
| 1547 | if (garbage == NULL) |
| 1548 | return NULL; |
| 1549 | } |
| 1550 | Py_INCREF(garbage); |
| 1551 | if (PyModule_AddObject(m, "garbage", garbage) < 0) |
| 1552 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1553 | |
Kristján Valur Jónsson | 69c6352 | 2012-04-15 11:41:32 +0000 | [diff] [blame] | 1554 | if (callbacks == NULL) { |
| 1555 | callbacks = PyList_New(0); |
| 1556 | if (callbacks == NULL) |
| 1557 | return NULL; |
| 1558 | } |
| 1559 | Py_INCREF(callbacks); |
| 1560 | if (PyModule_AddObject(m, "callbacks", callbacks) < 0) |
| 1561 | return NULL; |
| 1562 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1563 | #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] | 1564 | ADD_INT(DEBUG_STATS); |
| 1565 | ADD_INT(DEBUG_COLLECTABLE); |
| 1566 | ADD_INT(DEBUG_UNCOLLECTABLE); |
| 1567 | ADD_INT(DEBUG_SAVEALL); |
| 1568 | ADD_INT(DEBUG_LEAK); |
Tim Peters | 1155887 | 2003-04-06 23:30:52 +0000 | [diff] [blame] | 1569 | #undef ADD_INT |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1570 | return m; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1571 | } |
| 1572 | |
Guido van Rossum | e13ddc9 | 2003-04-17 17:29:22 +0000 | [diff] [blame] | 1573 | /* API to invoke gc.collect() from C */ |
Neal Norwitz | 7b216c5 | 2006-03-04 20:01:53 +0000 | [diff] [blame] | 1574 | Py_ssize_t |
Guido van Rossum | e13ddc9 | 2003-04-17 17:29:22 +0000 | [diff] [blame] | 1575 | PyGC_Collect(void) |
| 1576 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1577 | Py_ssize_t n; |
Guido van Rossum | e13ddc9 | 2003-04-17 17:29:22 +0000 | [diff] [blame] | 1578 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1579 | if (collecting) |
| 1580 | n = 0; /* already collecting, don't do anything */ |
| 1581 | else { |
| 1582 | collecting = 1; |
Kristján Valur Jónsson | 69c6352 | 2012-04-15 11:41:32 +0000 | [diff] [blame] | 1583 | n = collect_with_callback(NUM_GENERATIONS - 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1584 | collecting = 0; |
| 1585 | } |
Guido van Rossum | e13ddc9 | 2003-04-17 17:29:22 +0000 | [diff] [blame] | 1586 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1587 | return n; |
Guido van Rossum | e13ddc9 | 2003-04-17 17:29:22 +0000 | [diff] [blame] | 1588 | } |
| 1589 | |
Antoine Pitrou | fef34e3 | 2013-05-19 01:11:58 +0200 | [diff] [blame] | 1590 | Py_ssize_t |
| 1591 | _PyGC_CollectNoFail(void) |
| 1592 | { |
| 1593 | Py_ssize_t n; |
| 1594 | |
Antoine Pitrou | c69c9bc | 2013-08-15 20:15:15 +0200 | [diff] [blame] | 1595 | /* Ideally, this function is only called on interpreter shutdown, |
| 1596 | and therefore not recursively. Unfortunately, when there are daemon |
| 1597 | threads, a daemon thread can start a cyclic garbage collection |
| 1598 | during interpreter shutdown (and then never finish it). |
| 1599 | See http://bugs.python.org/issue8713#msg195178 for an example. |
| 1600 | */ |
| 1601 | if (collecting) |
| 1602 | n = 0; |
| 1603 | else { |
| 1604 | collecting = 1; |
| 1605 | n = collect(NUM_GENERATIONS - 1, NULL, NULL, 1); |
| 1606 | collecting = 0; |
| 1607 | } |
Antoine Pitrou | fef34e3 | 2013-05-19 01:11:58 +0200 | [diff] [blame] | 1608 | return n; |
| 1609 | } |
Antoine Pitrou | 5f454a0 | 2013-05-06 21:15:57 +0200 | [diff] [blame] | 1610 | |
Antoine Pitrou | 696e035 | 2010-08-08 22:18:46 +0000 | [diff] [blame] | 1611 | void |
Antoine Pitrou | 5f454a0 | 2013-05-06 21:15:57 +0200 | [diff] [blame] | 1612 | _PyGC_DumpShutdownStats(void) |
Antoine Pitrou | 696e035 | 2010-08-08 22:18:46 +0000 | [diff] [blame] | 1613 | { |
Antoine Pitrou | 2ed94eb | 2010-09-14 09:48:39 +0000 | [diff] [blame] | 1614 | if (!(debug & DEBUG_SAVEALL) |
| 1615 | && garbage != NULL && PyList_GET_SIZE(garbage) > 0) { |
Georg Brandl | 08be72d | 2010-10-24 15:11:22 +0000 | [diff] [blame] | 1616 | char *message; |
| 1617 | if (debug & DEBUG_UNCOLLECTABLE) |
Antoine Pitrou | b5d8204 | 2010-11-05 00:05:25 +0000 | [diff] [blame] | 1618 | message = "gc: %zd uncollectable objects at " \ |
Georg Brandl | 08be72d | 2010-10-24 15:11:22 +0000 | [diff] [blame] | 1619 | "shutdown"; |
| 1620 | else |
Antoine Pitrou | b5d8204 | 2010-11-05 00:05:25 +0000 | [diff] [blame] | 1621 | message = "gc: %zd uncollectable objects at " \ |
Georg Brandl | 08be72d | 2010-10-24 15:11:22 +0000 | [diff] [blame] | 1622 | "shutdown; use gc.set_debug(gc.DEBUG_UNCOLLECTABLE) to list them"; |
Antoine Pitrou | 070cb3c | 2013-05-08 13:23:25 +0200 | [diff] [blame] | 1623 | /* PyErr_WarnFormat does too many things and we are at shutdown, |
| 1624 | the warnings module's dependencies (e.g. linecache) may be gone |
| 1625 | already. */ |
| 1626 | if (PyErr_WarnExplicitFormat(PyExc_ResourceWarning, "gc", 0, |
| 1627 | "gc", NULL, message, |
| 1628 | PyList_GET_SIZE(garbage))) |
Georg Brandl | 08be72d | 2010-10-24 15:11:22 +0000 | [diff] [blame] | 1629 | PyErr_WriteUnraisable(NULL); |
Antoine Pitrou | 696e035 | 2010-08-08 22:18:46 +0000 | [diff] [blame] | 1630 | if (debug & DEBUG_UNCOLLECTABLE) { |
| 1631 | PyObject *repr = NULL, *bytes = NULL; |
| 1632 | repr = PyObject_Repr(garbage); |
| 1633 | if (!repr || !(bytes = PyUnicode_EncodeFSDefault(repr))) |
| 1634 | PyErr_WriteUnraisable(garbage); |
| 1635 | else { |
| 1636 | PySys_WriteStderr( |
Antoine Pitrou | 070cb3c | 2013-05-08 13:23:25 +0200 | [diff] [blame] | 1637 | " %s\n", |
Antoine Pitrou | 696e035 | 2010-08-08 22:18:46 +0000 | [diff] [blame] | 1638 | PyBytes_AS_STRING(bytes) |
| 1639 | ); |
| 1640 | } |
| 1641 | Py_XDECREF(repr); |
| 1642 | Py_XDECREF(bytes); |
| 1643 | } |
Antoine Pitrou | 696e035 | 2010-08-08 22:18:46 +0000 | [diff] [blame] | 1644 | } |
Antoine Pitrou | 5f454a0 | 2013-05-06 21:15:57 +0200 | [diff] [blame] | 1645 | } |
| 1646 | |
| 1647 | void |
| 1648 | _PyGC_Fini(void) |
| 1649 | { |
Kristján Valur Jónsson | 69c6352 | 2012-04-15 11:41:32 +0000 | [diff] [blame] | 1650 | Py_CLEAR(callbacks); |
Antoine Pitrou | 696e035 | 2010-08-08 22:18:46 +0000 | [diff] [blame] | 1651 | } |
| 1652 | |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1653 | /* for debugging */ |
Guido van Rossum | e13ddc9 | 2003-04-17 17:29:22 +0000 | [diff] [blame] | 1654 | void |
| 1655 | _PyGC_Dump(PyGC_Head *g) |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1656 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1657 | _PyObject_Dump(FROM_GC(g)); |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1658 | } |
| 1659 | |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1660 | /* extension modules might be compiled with GC support so these |
| 1661 | functions must always be available */ |
| 1662 | |
Neil Schemenauer | fec4eb1 | 2002-04-12 02:41:03 +0000 | [diff] [blame] | 1663 | #undef PyObject_GC_Track |
| 1664 | #undef PyObject_GC_UnTrack |
| 1665 | #undef PyObject_GC_Del |
| 1666 | #undef _PyObject_GC_Malloc |
| 1667 | |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1668 | void |
Neil Schemenauer | fec4eb1 | 2002-04-12 02:41:03 +0000 | [diff] [blame] | 1669 | PyObject_GC_Track(void *op) |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1670 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1671 | _PyObject_GC_TRACK(op); |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1672 | } |
| 1673 | |
Neil Schemenauer | fec4eb1 | 2002-04-12 02:41:03 +0000 | [diff] [blame] | 1674 | void |
| 1675 | PyObject_GC_UnTrack(void *op) |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1676 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1677 | /* Obscure: the Py_TRASHCAN mechanism requires that we be able to |
| 1678 | * call PyObject_GC_UnTrack twice on an object. |
| 1679 | */ |
| 1680 | if (IS_TRACKED(op)) |
| 1681 | _PyObject_GC_UNTRACK(op); |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1682 | } |
| 1683 | |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 1684 | static PyObject * |
| 1685 | _PyObject_GC_Alloc(int use_calloc, size_t basicsize) |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1686 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1687 | PyObject *op; |
| 1688 | PyGC_Head *g; |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 1689 | size_t size; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1690 | if (basicsize > PY_SSIZE_T_MAX - sizeof(PyGC_Head)) |
| 1691 | return PyErr_NoMemory(); |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 1692 | size = sizeof(PyGC_Head) + basicsize; |
| 1693 | if (use_calloc) |
| 1694 | g = (PyGC_Head *)PyObject_Calloc(1, size); |
| 1695 | else |
| 1696 | g = (PyGC_Head *)PyObject_Malloc(size); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1697 | if (g == NULL) |
| 1698 | return PyErr_NoMemory(); |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 1699 | g->gc.gc_refs = 0; |
| 1700 | _PyGCHead_SET_REFS(g, GC_UNTRACKED); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1701 | generations[0].count++; /* number of allocated GC objects */ |
| 1702 | if (generations[0].count > generations[0].threshold && |
| 1703 | enabled && |
| 1704 | generations[0].threshold && |
| 1705 | !collecting && |
| 1706 | !PyErr_Occurred()) { |
| 1707 | collecting = 1; |
| 1708 | collect_generations(); |
| 1709 | collecting = 0; |
| 1710 | } |
| 1711 | op = FROM_GC(g); |
| 1712 | return op; |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1713 | } |
| 1714 | |
| 1715 | PyObject * |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 1716 | _PyObject_GC_Malloc(size_t basicsize) |
| 1717 | { |
| 1718 | return _PyObject_GC_Alloc(0, basicsize); |
| 1719 | } |
| 1720 | |
| 1721 | PyObject * |
| 1722 | _PyObject_GC_Calloc(size_t basicsize) |
| 1723 | { |
| 1724 | return _PyObject_GC_Alloc(1, basicsize); |
| 1725 | } |
| 1726 | |
| 1727 | PyObject * |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1728 | _PyObject_GC_New(PyTypeObject *tp) |
| 1729 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1730 | PyObject *op = _PyObject_GC_Malloc(_PyObject_SIZE(tp)); |
| 1731 | if (op != NULL) |
| 1732 | op = PyObject_INIT(op, tp); |
| 1733 | return op; |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1734 | } |
| 1735 | |
| 1736 | PyVarObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1737 | _PyObject_GC_NewVar(PyTypeObject *tp, Py_ssize_t nitems) |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1738 | { |
Victor Stinner | 5d1866c | 2013-07-08 22:17:52 +0200 | [diff] [blame] | 1739 | size_t size; |
| 1740 | PyVarObject *op; |
| 1741 | |
| 1742 | if (nitems < 0) { |
| 1743 | PyErr_BadInternalCall(); |
| 1744 | return NULL; |
| 1745 | } |
| 1746 | size = _PyObject_VAR_SIZE(tp, nitems); |
| 1747 | op = (PyVarObject *) _PyObject_GC_Malloc(size); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1748 | if (op != NULL) |
| 1749 | op = PyObject_INIT_VAR(op, tp, nitems); |
| 1750 | return op; |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1751 | } |
| 1752 | |
| 1753 | PyVarObject * |
Martin v. Löwis | 4129068 | 2006-02-16 14:56:14 +0000 | [diff] [blame] | 1754 | _PyObject_GC_Resize(PyVarObject *op, Py_ssize_t nitems) |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1755 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1756 | const size_t basicsize = _PyObject_VAR_SIZE(Py_TYPE(op), nitems); |
| 1757 | PyGC_Head *g = AS_GC(op); |
| 1758 | if (basicsize > PY_SSIZE_T_MAX - sizeof(PyGC_Head)) |
| 1759 | return (PyVarObject *)PyErr_NoMemory(); |
| 1760 | g = (PyGC_Head *)PyObject_REALLOC(g, sizeof(PyGC_Head) + basicsize); |
| 1761 | if (g == NULL) |
| 1762 | return (PyVarObject *)PyErr_NoMemory(); |
| 1763 | op = (PyVarObject *) FROM_GC(g); |
| 1764 | Py_SIZE(op) = nitems; |
| 1765 | return op; |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1766 | } |
| 1767 | |
| 1768 | void |
Neil Schemenauer | fec4eb1 | 2002-04-12 02:41:03 +0000 | [diff] [blame] | 1769 | PyObject_GC_Del(void *op) |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1770 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1771 | PyGC_Head *g = AS_GC(op); |
| 1772 | if (IS_TRACKED(op)) |
| 1773 | gc_list_remove(g); |
| 1774 | if (generations[0].count > 0) { |
| 1775 | generations[0].count--; |
| 1776 | } |
| 1777 | PyObject_FREE(g); |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1778 | } |