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 */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 28 | |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 29 | /* Get an object's GC head */ |
| 30 | #define AS_GC(o) ((PyGC_Head *)(o)-1) |
| 31 | |
| 32 | /* Get the object given the GC head */ |
| 33 | #define FROM_GC(g) ((PyObject *)(((PyGC_Head *)g)+1)) |
| 34 | |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 35 | /*** Global GC state ***/ |
| 36 | |
Neil Schemenauer | 2880ae5 | 2002-05-04 05:35:20 +0000 | [diff] [blame] | 37 | struct gc_generation { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 38 | PyGC_Head head; |
| 39 | int threshold; /* collection threshold */ |
| 40 | int count; /* count of allocations or collections of younger |
| 41 | generations */ |
Neil Schemenauer | 2880ae5 | 2002-05-04 05:35:20 +0000 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | #define NUM_GENERATIONS 3 |
| 45 | #define GEN_HEAD(n) (&generations[n].head) |
| 46 | |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 47 | /* linked lists of container objects */ |
Neil Schemenauer | 2880ae5 | 2002-05-04 05:35:20 +0000 | [diff] [blame] | 48 | static struct gc_generation generations[NUM_GENERATIONS] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 49 | /* PyGC_Head, threshold, count */ |
| 50 | {{{GEN_HEAD(0), GEN_HEAD(0), 0}}, 700, 0}, |
| 51 | {{{GEN_HEAD(1), GEN_HEAD(1), 0}}, 10, 0}, |
| 52 | {{{GEN_HEAD(2), GEN_HEAD(2), 0}}, 10, 0}, |
Neil Schemenauer | 2880ae5 | 2002-05-04 05:35:20 +0000 | [diff] [blame] | 53 | }; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 54 | |
Neil Schemenauer | 2880ae5 | 2002-05-04 05:35:20 +0000 | [diff] [blame] | 55 | PyGC_Head *_PyGC_generation0 = GEN_HEAD(0); |
| 56 | |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 57 | static int enabled = 1; /* automatic collection enabled? */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 58 | |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 59 | /* true if we are currently running the collector */ |
Tim Peters | bf384c2 | 2003-04-06 00:11:39 +0000 | [diff] [blame] | 60 | static int collecting = 0; |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 61 | |
Tim Peters | 6fc13d9 | 2002-07-02 18:12:35 +0000 | [diff] [blame] | 62 | /* list of uncollectable objects */ |
Tim Peters | bf384c2 | 2003-04-06 00:11:39 +0000 | [diff] [blame] | 63 | static PyObject *garbage = NULL; |
Tim Peters | 6fc13d9 | 2002-07-02 18:12:35 +0000 | [diff] [blame] | 64 | |
| 65 | /* Python string to use if unhandled exception occurs */ |
Tim Peters | bf384c2 | 2003-04-06 00:11:39 +0000 | [diff] [blame] | 66 | static PyObject *gc_str = NULL; |
Tim Peters | 6fc13d9 | 2002-07-02 18:12:35 +0000 | [diff] [blame] | 67 | |
Kristján Valur Jónsson | 69c6352 | 2012-04-15 11:41:32 +0000 | [diff] [blame] | 68 | /* a list of callbacks to be invoked when collection is performed */ |
| 69 | static PyObject *callbacks = NULL; |
| 70 | |
| 71 | /* 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] | 72 | approximates the number of long lived objects tracked by the GC. |
| 73 | |
| 74 | (by "full collection", we mean a collection of the oldest generation). |
| 75 | */ |
| 76 | static Py_ssize_t long_lived_total = 0; |
| 77 | |
Kristján Valur Jónsson | 69c6352 | 2012-04-15 11:41:32 +0000 | [diff] [blame] | 78 | /* This is the number of objects that survived all "non-full" collections, |
Antoine Pitrou | 14b78f5 | 2009-01-09 22:27:08 +0000 | [diff] [blame] | 79 | and are awaiting to undergo a full collection for the first time. |
| 80 | |
| 81 | */ |
| 82 | static Py_ssize_t long_lived_pending = 0; |
| 83 | |
| 84 | /* |
| 85 | NOTE: about the counting of long-lived objects. |
| 86 | |
| 87 | To limit the cost of garbage collection, there are two strategies; |
| 88 | - make each collection faster, e.g. by scanning fewer objects |
| 89 | - do less collections |
| 90 | This heuristic is about the latter strategy. |
| 91 | |
| 92 | In addition to the various configurable thresholds, we only trigger a |
| 93 | full collection if the ratio |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 94 | long_lived_pending / long_lived_total |
Antoine Pitrou | 14b78f5 | 2009-01-09 22:27:08 +0000 | [diff] [blame] | 95 | is above a given value (hardwired to 25%). |
| 96 | |
| 97 | The reason is that, while "non-full" collections (i.e., collections of |
| 98 | the young and middle generations) will always examine roughly the same |
| 99 | number of objects -- determined by the aforementioned thresholds --, |
| 100 | the cost of a full collection is proportional to the total number of |
| 101 | long-lived objects, which is virtually unbounded. |
| 102 | |
| 103 | Indeed, it has been remarked that doing a full collection every |
| 104 | <constant number> of object creations entails a dramatic performance |
| 105 | degradation in workloads which consist in creating and storing lots of |
| 106 | long-lived objects (e.g. building a large list of GC-tracked objects would |
| 107 | show quadratic performance, instead of linear as expected: see issue #4074). |
| 108 | |
| 109 | Using the above ratio, instead, yields amortized linear performance in |
| 110 | the total number of objects (the effect of which can be summarized |
| 111 | thusly: "each full garbage collection is more and more costly as the |
| 112 | number of objects grows, but we do fewer and fewer of them"). |
| 113 | |
| 114 | This heuristic was suggested by Martin von Löwis on python-dev in |
| 115 | June 2008. His original analysis and proposal can be found at: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 116 | http://mail.python.org/pipermail/python-dev/2008-June/080579.html |
Antoine Pitrou | 14b78f5 | 2009-01-09 22:27:08 +0000 | [diff] [blame] | 117 | */ |
| 118 | |
Antoine Pitrou | e1ad3da | 2012-05-28 22:22:34 +0200 | [diff] [blame] | 119 | /* |
| 120 | NOTE: about untracking of mutable objects. |
| 121 | |
| 122 | Certain types of container cannot participate in a reference cycle, and |
| 123 | so do not need to be tracked by the garbage collector. Untracking these |
| 124 | objects reduces the cost of garbage collections. However, determining |
| 125 | which objects may be untracked is not free, and the costs must be |
| 126 | weighed against the benefits for garbage collection. |
| 127 | |
| 128 | There are two possible strategies for when to untrack a container: |
| 129 | |
| 130 | i) When the container is created. |
| 131 | ii) When the container is examined by the garbage collector. |
| 132 | |
| 133 | Tuples containing only immutable objects (integers, strings etc, and |
| 134 | recursively, tuples of immutable objects) do not need to be tracked. |
| 135 | The interpreter creates a large number of tuples, many of which will |
| 136 | not survive until garbage collection. It is therefore not worthwhile |
| 137 | to untrack eligible tuples at creation time. |
| 138 | |
| 139 | Instead, all tuples except the empty tuple are tracked when created. |
| 140 | During garbage collection it is determined whether any surviving tuples |
| 141 | can be untracked. A tuple can be untracked if all of its contents are |
| 142 | already not tracked. Tuples are examined for untracking in all garbage |
| 143 | collection cycles. It may take more than one cycle to untrack a tuple. |
| 144 | |
| 145 | Dictionaries containing only immutable objects also do not need to be |
| 146 | tracked. Dictionaries are untracked when created. If a tracked item is |
| 147 | inserted into a dictionary (either as a key or value), the dictionary |
| 148 | becomes tracked. During a full garbage collection (all generations), |
| 149 | the collector will untrack any dictionaries whose contents are not |
| 150 | tracked. |
| 151 | |
| 152 | The module provides the python function is_tracked(obj), which returns |
| 153 | the CURRENT tracking status of the object. Subsequent garbage |
| 154 | collections may change the tracking status of the object. |
| 155 | |
| 156 | Untracking of certain containers was introduced in issue #4688, and |
| 157 | the algorithm was refined in response to issue #14775. |
| 158 | */ |
Antoine Pitrou | 14b78f5 | 2009-01-09 22:27:08 +0000 | [diff] [blame] | 159 | |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 160 | /* set for debugging information */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 161 | #define DEBUG_STATS (1<<0) /* print collection statistics */ |
| 162 | #define DEBUG_COLLECTABLE (1<<1) /* print collectable objects */ |
| 163 | #define DEBUG_UNCOLLECTABLE (1<<2) /* print uncollectable objects */ |
| 164 | #define DEBUG_SAVEALL (1<<5) /* save all garbage in gc.garbage */ |
| 165 | #define DEBUG_LEAK DEBUG_COLLECTABLE | \ |
| 166 | DEBUG_UNCOLLECTABLE | \ |
| 167 | DEBUG_SAVEALL |
Jeremy Hylton | b709df3 | 2000-09-01 02:47:25 +0000 | [diff] [blame] | 168 | static int debug; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 169 | static PyObject *tmod = NULL; |
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 | |
Tim Peters | 6fc13d9 | 2002-07-02 18:12:35 +0000 | [diff] [blame] | 226 | #define IS_TRACKED(o) ((AS_GC(o))->gc.gc_refs != GC_UNTRACKED) |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 227 | #define IS_REACHABLE(o) ((AS_GC(o))->gc.gc_refs == GC_REACHABLE) |
| 228 | #define IS_TENTATIVELY_UNREACHABLE(o) ( \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 229 | (AS_GC(o))->gc.gc_refs == 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) { |
| 344 | assert(gc->gc.gc_refs == GC_REACHABLE); |
| 345 | gc->gc.gc_refs = Py_REFCNT(FROM_GC(gc)); |
| 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 | */ |
| 364 | assert(gc->gc.gc_refs != 0); |
| 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 | */ |
| 379 | assert(gc->gc.gc_refs != 0); /* else refcount was too small */ |
| 380 | if (gc->gc.gc_refs > 0) |
| 381 | gc->gc.gc_refs--; |
| 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); |
| 410 | const Py_ssize_t gc_refs = gc->gc.gc_refs; |
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 | */ |
| 418 | gc->gc.gc_refs = 1; |
| 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); |
| 428 | gc->gc.gc_refs = 1; |
| 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 | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 472 | if (gc->gc.gc_refs) { |
| 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; |
| 483 | assert(gc->gc.gc_refs > 0); |
| 484 | gc->gc.gc_refs = GC_REACHABLE; |
| 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); |
| 503 | gc->gc.gc_refs = GC_TENTATIVELY_UNREACHABLE; |
| 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 | |
Amaury Forgeot d'Arc | ad8dcd5 | 2007-12-10 23:58:35 +0000 | [diff] [blame] | 523 | /* Return true if object has a finalization method. */ |
Neil Schemenauer | a765c12 | 2001-11-01 17:35:23 +0000 | [diff] [blame] | 524 | static int |
| 525 | has_finalizer(PyObject *op) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 526 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 527 | if (PyGen_CheckExact(op)) |
| 528 | return PyGen_NeedsFinalizing((PyGenObject *)op); |
| 529 | else |
| 530 | return op->ob_type->tp_del != NULL; |
Neil Schemenauer | a765c12 | 2001-11-01 17:35:23 +0000 | [diff] [blame] | 531 | } |
| 532 | |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 533 | /* Move the objects in unreachable with __del__ methods into `finalizers`. |
| 534 | * Objects moved into `finalizers` have gc_refs set to GC_REACHABLE; the |
| 535 | * objects remaining in unreachable are left at GC_TENTATIVELY_UNREACHABLE. |
Jeremy Hylton | ce136e9 | 2003-04-04 19:59:06 +0000 | [diff] [blame] | 536 | */ |
Neil Schemenauer | a765c12 | 2001-11-01 17:35:23 +0000 | [diff] [blame] | 537 | static void |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 538 | move_finalizers(PyGC_Head *unreachable, PyGC_Head *finalizers) |
Neil Schemenauer | a765c12 | 2001-11-01 17:35:23 +0000 | [diff] [blame] | 539 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 540 | PyGC_Head *gc; |
| 541 | PyGC_Head *next; |
Tim Peters | f6b8045 | 2003-04-07 19:21:15 +0000 | [diff] [blame] | 542 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 543 | /* March over unreachable. Move objects with finalizers into |
| 544 | * `finalizers`. |
| 545 | */ |
| 546 | for (gc = unreachable->gc.gc_next; gc != unreachable; gc = next) { |
| 547 | PyObject *op = FROM_GC(gc); |
Jeremy Hylton | ce136e9 | 2003-04-04 19:59:06 +0000 | [diff] [blame] | 548 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 549 | assert(IS_TENTATIVELY_UNREACHABLE(op)); |
| 550 | next = gc->gc.gc_next; |
Tim Peters | f6ae7a4 | 2003-04-05 18:40:50 +0000 | [diff] [blame] | 551 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 552 | if (has_finalizer(op)) { |
| 553 | gc_list_move(gc, finalizers); |
| 554 | gc->gc.gc_refs = GC_REACHABLE; |
| 555 | } |
| 556 | } |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 557 | } |
| 558 | |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 559 | /* A traversal callback for move_finalizer_reachable. */ |
| 560 | static int |
| 561 | visit_move(PyObject *op, PyGC_Head *tolist) |
| 562 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 563 | if (PyObject_IS_GC(op)) { |
| 564 | if (IS_TENTATIVELY_UNREACHABLE(op)) { |
| 565 | PyGC_Head *gc = AS_GC(op); |
| 566 | gc_list_move(gc, tolist); |
| 567 | gc->gc.gc_refs = GC_REACHABLE; |
| 568 | } |
| 569 | } |
| 570 | return 0; |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | /* Move objects that are reachable from finalizers, from the unreachable set |
Tim Peters | f6b8045 | 2003-04-07 19:21:15 +0000 | [diff] [blame] | 574 | * into finalizers set. |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 575 | */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 576 | static void |
Tim Peters | f6b8045 | 2003-04-07 19:21:15 +0000 | [diff] [blame] | 577 | move_finalizer_reachable(PyGC_Head *finalizers) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 578 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 579 | traverseproc traverse; |
| 580 | PyGC_Head *gc = finalizers->gc.gc_next; |
| 581 | for (; gc != finalizers; gc = gc->gc.gc_next) { |
| 582 | /* Note that the finalizers list may grow during this. */ |
| 583 | traverse = Py_TYPE(FROM_GC(gc))->tp_traverse; |
| 584 | (void) traverse(FROM_GC(gc), |
| 585 | (visitproc)visit_move, |
| 586 | (void *)finalizers); |
| 587 | } |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 588 | } |
| 589 | |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 590 | /* Clear all weakrefs to unreachable objects, and if such a weakref has a |
| 591 | * callback, invoke it if necessary. Note that it's possible for such |
| 592 | * weakrefs to be outside the unreachable set -- indeed, those are precisely |
| 593 | * the weakrefs whose callbacks must be invoked. See gc_weakref.txt for |
| 594 | * overview & some details. Some weakrefs with callbacks may be reclaimed |
| 595 | * directly by this routine; the number reclaimed is the return value. Other |
| 596 | * weakrefs with callbacks may be moved into the `old` generation. Objects |
| 597 | * moved into `old` have gc_refs set to GC_REACHABLE; the objects remaining in |
| 598 | * unreachable are left at GC_TENTATIVELY_UNREACHABLE. When this returns, |
| 599 | * no object in `unreachable` is weakly referenced anymore. |
Tim Peters | 403a203 | 2003-11-20 21:21:46 +0000 | [diff] [blame] | 600 | */ |
| 601 | static int |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 602 | handle_weakrefs(PyGC_Head *unreachable, PyGC_Head *old) |
Tim Peters | 403a203 | 2003-11-20 21:21:46 +0000 | [diff] [blame] | 603 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 604 | PyGC_Head *gc; |
| 605 | PyObject *op; /* generally FROM_GC(gc) */ |
| 606 | PyWeakReference *wr; /* generally a cast of op */ |
| 607 | PyGC_Head wrcb_to_call; /* weakrefs with callbacks to call */ |
| 608 | PyGC_Head *next; |
| 609 | int num_freed = 0; |
Tim Peters | 403a203 | 2003-11-20 21:21:46 +0000 | [diff] [blame] | 610 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 611 | gc_list_init(&wrcb_to_call); |
Tim Peters | 403a203 | 2003-11-20 21:21:46 +0000 | [diff] [blame] | 612 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 613 | /* Clear all weakrefs to the objects in unreachable. If such a weakref |
| 614 | * also has a callback, move it into `wrcb_to_call` if the callback |
| 615 | * needs to be invoked. Note that we cannot invoke any callbacks until |
| 616 | * all weakrefs to unreachable objects are cleared, lest the callback |
| 617 | * resurrect an unreachable object via a still-active weakref. We |
| 618 | * make another pass over wrcb_to_call, invoking callbacks, after this |
| 619 | * pass completes. |
| 620 | */ |
| 621 | for (gc = unreachable->gc.gc_next; gc != unreachable; gc = next) { |
| 622 | PyWeakReference **wrlist; |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 623 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 624 | op = FROM_GC(gc); |
| 625 | assert(IS_TENTATIVELY_UNREACHABLE(op)); |
| 626 | next = gc->gc.gc_next; |
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 | if (! PyType_SUPPORTS_WEAKREFS(Py_TYPE(op))) |
| 629 | continue; |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 630 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 631 | /* It supports weakrefs. Does it have any? */ |
| 632 | wrlist = (PyWeakReference **) |
| 633 | PyObject_GET_WEAKREFS_LISTPTR(op); |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 634 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 635 | /* `op` may have some weakrefs. March over the list, clear |
| 636 | * all the weakrefs, and move the weakrefs with callbacks |
| 637 | * that must be called into wrcb_to_call. |
| 638 | */ |
| 639 | for (wr = *wrlist; wr != NULL; wr = *wrlist) { |
| 640 | PyGC_Head *wrasgc; /* AS_GC(wr) */ |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 641 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 642 | /* _PyWeakref_ClearRef clears the weakref but leaves |
| 643 | * the callback pointer intact. Obscure: it also |
| 644 | * changes *wrlist. |
| 645 | */ |
| 646 | assert(wr->wr_object == op); |
| 647 | _PyWeakref_ClearRef(wr); |
| 648 | assert(wr->wr_object == Py_None); |
| 649 | if (wr->wr_callback == NULL) |
| 650 | continue; /* no callback */ |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 651 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 652 | /* Headache time. `op` is going away, and is weakly referenced by |
| 653 | * `wr`, which has a callback. Should the callback be invoked? If wr |
| 654 | * is also trash, no: |
| 655 | * |
| 656 | * 1. There's no need to call it. The object and the weakref are |
| 657 | * both going away, so it's legitimate to pretend the weakref is |
| 658 | * going away first. The user has to ensure a weakref outlives its |
| 659 | * referent if they want a guarantee that the wr callback will get |
| 660 | * invoked. |
| 661 | * |
| 662 | * 2. It may be catastrophic to call it. If the callback is also in |
| 663 | * cyclic trash (CT), then although the CT is unreachable from |
| 664 | * outside the current generation, CT may be reachable from the |
| 665 | * callback. Then the callback could resurrect insane objects. |
| 666 | * |
| 667 | * Since the callback is never needed and may be unsafe in this case, |
| 668 | * wr is simply left in the unreachable set. Note that because we |
| 669 | * already called _PyWeakref_ClearRef(wr), its callback will never |
| 670 | * trigger. |
| 671 | * |
| 672 | * OTOH, if wr isn't part of CT, we should invoke the callback: the |
| 673 | * weakref outlived the trash. Note that since wr isn't CT in this |
| 674 | * case, its callback can't be CT either -- wr acted as an external |
| 675 | * root to this generation, and therefore its callback did too. So |
| 676 | * nothing in CT is reachable from the callback either, so it's hard |
| 677 | * to imagine how calling it later could create a problem for us. wr |
| 678 | * is moved to wrcb_to_call in this case. |
| 679 | */ |
| 680 | if (IS_TENTATIVELY_UNREACHABLE(wr)) |
| 681 | continue; |
| 682 | assert(IS_REACHABLE(wr)); |
Tim Peters | cc2a866 | 2004-10-31 22:12:43 +0000 | [diff] [blame] | 683 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 684 | /* Create a new reference so that wr can't go away |
| 685 | * before we can process it again. |
| 686 | */ |
| 687 | Py_INCREF(wr); |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 688 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 689 | /* Move wr to wrcb_to_call, for the next pass. */ |
| 690 | wrasgc = AS_GC(wr); |
| 691 | assert(wrasgc != next); /* wrasgc is reachable, but |
| 692 | next isn't, so they can't |
| 693 | be the same */ |
| 694 | gc_list_move(wrasgc, &wrcb_to_call); |
| 695 | } |
| 696 | } |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 697 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 698 | /* Invoke the callbacks we decided to honor. It's safe to invoke them |
| 699 | * because they can't reference unreachable objects. |
| 700 | */ |
| 701 | while (! gc_list_is_empty(&wrcb_to_call)) { |
| 702 | PyObject *temp; |
| 703 | PyObject *callback; |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 704 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 705 | gc = wrcb_to_call.gc.gc_next; |
| 706 | op = FROM_GC(gc); |
| 707 | assert(IS_REACHABLE(op)); |
| 708 | assert(PyWeakref_Check(op)); |
| 709 | wr = (PyWeakReference *)op; |
| 710 | callback = wr->wr_callback; |
| 711 | assert(callback != NULL); |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 712 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 713 | /* copy-paste of weakrefobject.c's handle_callback() */ |
| 714 | temp = PyObject_CallFunctionObjArgs(callback, wr, NULL); |
| 715 | if (temp == NULL) |
| 716 | PyErr_WriteUnraisable(callback); |
| 717 | else |
| 718 | Py_DECREF(temp); |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 719 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 720 | /* Give up the reference we created in the first pass. When |
| 721 | * op's refcount hits 0 (which it may or may not do right now), |
| 722 | * op's tp_dealloc will decref op->wr_callback too. Note |
| 723 | * that the refcount probably will hit 0 now, and because this |
| 724 | * weakref was reachable to begin with, gc didn't already |
| 725 | * add it to its count of freed objects. Example: a reachable |
| 726 | * weak value dict maps some key to this reachable weakref. |
| 727 | * The callback removes this key->weakref mapping from the |
| 728 | * dict, leaving no other references to the weakref (excepting |
| 729 | * ours). |
| 730 | */ |
| 731 | Py_DECREF(op); |
| 732 | if (wrcb_to_call.gc.gc_next == gc) { |
| 733 | /* object is still alive -- move it */ |
| 734 | gc_list_move(gc, old); |
| 735 | } |
| 736 | else |
| 737 | ++num_freed; |
| 738 | } |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 739 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 740 | return num_freed; |
Tim Peters | 403a203 | 2003-11-20 21:21:46 +0000 | [diff] [blame] | 741 | } |
| 742 | |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 743 | static void |
Jeremy Hylton | 0625777 | 2000-08-31 15:10:24 +0000 | [diff] [blame] | 744 | debug_cycle(char *msg, PyObject *op) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 745 | { |
Victor Stinner | 499dfcf | 2011-03-21 13:26:24 +0100 | [diff] [blame] | 746 | PySys_FormatStderr("gc: %s <%s %p>\n", |
| 747 | msg, Py_TYPE(op)->tp_name, op); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 748 | } |
| 749 | |
Tim Peters | bf384c2 | 2003-04-06 00:11:39 +0000 | [diff] [blame] | 750 | /* Handle uncollectable garbage (cycles with finalizers, and stuff reachable |
| 751 | * only from such cycles). |
Tim Peters | f6b8045 | 2003-04-07 19:21:15 +0000 | [diff] [blame] | 752 | * If DEBUG_SAVEALL, all objects in finalizers are appended to the module |
| 753 | * garbage list (a Python list), else only the objects in finalizers with |
| 754 | * __del__ methods are appended to garbage. All objects in finalizers are |
| 755 | * merged into the old list regardless. |
Tim Peters | 259272b | 2003-04-06 19:41:39 +0000 | [diff] [blame] | 756 | * Returns 0 if all OK, <0 on error (out of memory to grow the garbage list). |
| 757 | * The finalizers list is made empty on a successful return. |
Tim Peters | bf384c2 | 2003-04-06 00:11:39 +0000 | [diff] [blame] | 758 | */ |
Tim Peters | 259272b | 2003-04-06 19:41:39 +0000 | [diff] [blame] | 759 | static int |
Tim Peters | f6b8045 | 2003-04-07 19:21:15 +0000 | [diff] [blame] | 760 | handle_finalizers(PyGC_Head *finalizers, PyGC_Head *old) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 761 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 762 | PyGC_Head *gc = finalizers->gc.gc_next; |
Tim Peters | f6b8045 | 2003-04-07 19:21:15 +0000 | [diff] [blame] | 763 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 764 | if (garbage == NULL) { |
| 765 | garbage = PyList_New(0); |
| 766 | if (garbage == NULL) |
| 767 | Py_FatalError("gc couldn't create gc.garbage list"); |
| 768 | } |
| 769 | for (; gc != finalizers; gc = gc->gc.gc_next) { |
| 770 | PyObject *op = FROM_GC(gc); |
Tim Peters | f6b8045 | 2003-04-07 19:21:15 +0000 | [diff] [blame] | 771 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 772 | if ((debug & DEBUG_SAVEALL) || has_finalizer(op)) { |
| 773 | if (PyList_Append(garbage, op) < 0) |
| 774 | return -1; |
| 775 | } |
| 776 | } |
Tim Peters | f6b8045 | 2003-04-07 19:21:15 +0000 | [diff] [blame] | 777 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 778 | gc_list_merge(finalizers, old); |
| 779 | return 0; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 780 | } |
| 781 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 782 | /* Break reference cycles by clearing the containers involved. This is |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 783 | * 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] | 784 | * objects may be freed. It is possible I screwed something up here. |
| 785 | */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 786 | static void |
Jeremy Hylton | ce136e9 | 2003-04-04 19:59:06 +0000 | [diff] [blame] | 787 | delete_garbage(PyGC_Head *collectable, PyGC_Head *old) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 788 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 789 | inquiry clear; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 790 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 791 | while (!gc_list_is_empty(collectable)) { |
| 792 | PyGC_Head *gc = collectable->gc.gc_next; |
| 793 | PyObject *op = FROM_GC(gc); |
Tim Peters | 8839617 | 2002-06-30 17:56:40 +0000 | [diff] [blame] | 794 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 795 | assert(IS_TENTATIVELY_UNREACHABLE(op)); |
| 796 | if (debug & DEBUG_SAVEALL) { |
| 797 | PyList_Append(garbage, op); |
| 798 | } |
| 799 | else { |
| 800 | if ((clear = Py_TYPE(op)->tp_clear) != NULL) { |
| 801 | Py_INCREF(op); |
| 802 | clear(op); |
| 803 | Py_DECREF(op); |
| 804 | } |
| 805 | } |
| 806 | if (collectable->gc.gc_next == gc) { |
| 807 | /* object is still alive, move it, it may die later */ |
| 808 | gc_list_move(gc, old); |
| 809 | gc->gc.gc_refs = GC_REACHABLE; |
| 810 | } |
| 811 | } |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 812 | } |
| 813 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 814 | /* Clear all free lists |
| 815 | * All free lists are cleared during the collection of the highest generation. |
| 816 | * Allocated items in the free list may keep a pymalloc arena occupied. |
| 817 | * Clearing the free lists may give back memory to the OS earlier. |
| 818 | */ |
| 819 | static void |
| 820 | clear_freelists(void) |
| 821 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 822 | (void)PyMethod_ClearFreeList(); |
| 823 | (void)PyFrame_ClearFreeList(); |
| 824 | (void)PyCFunction_ClearFreeList(); |
| 825 | (void)PyTuple_ClearFreeList(); |
| 826 | (void)PyUnicode_ClearFreeList(); |
| 827 | (void)PyFloat_ClearFreeList(); |
Antoine Pitrou | 9a812cb | 2011-11-15 00:00:12 +0100 | [diff] [blame] | 828 | (void)PyList_ClearFreeList(); |
| 829 | (void)PyDict_ClearFreeList(); |
Antoine Pitrou | 093ce9c | 2011-12-16 11:24:27 +0100 | [diff] [blame] | 830 | (void)PySet_ClearFreeList(); |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 831 | } |
| 832 | |
Antoine Pitrou | 621601a | 2008-12-17 23:18:19 +0000 | [diff] [blame] | 833 | static double |
| 834 | get_time(void) |
| 835 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 836 | double result = 0; |
| 837 | if (tmod != NULL) { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 838 | _Py_IDENTIFIER(time); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 839 | |
| 840 | PyObject *f = _PyObject_CallMethodId(tmod, &PyId_time, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 841 | if (f == NULL) { |
| 842 | PyErr_Clear(); |
| 843 | } |
| 844 | else { |
| 845 | if (PyFloat_Check(f)) |
| 846 | result = PyFloat_AsDouble(f); |
| 847 | Py_DECREF(f); |
| 848 | } |
| 849 | } |
| 850 | return result; |
Antoine Pitrou | 621601a | 2008-12-17 23:18:19 +0000 | [diff] [blame] | 851 | } |
| 852 | |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 853 | /* This is the main function. Read this to understand how the |
| 854 | * collection process works. */ |
Neal Norwitz | 7b216c5 | 2006-03-04 20:01:53 +0000 | [diff] [blame] | 855 | static Py_ssize_t |
Kristján Valur Jónsson | 69c6352 | 2012-04-15 11:41:32 +0000 | [diff] [blame] | 856 | collect(int generation, Py_ssize_t *n_collected, Py_ssize_t *n_uncollectable) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 857 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 858 | int i; |
| 859 | Py_ssize_t m = 0; /* # objects collected */ |
| 860 | Py_ssize_t n = 0; /* # unreachable objects that couldn't be collected */ |
| 861 | PyGC_Head *young; /* the generation we are examining */ |
| 862 | PyGC_Head *old; /* next older generation */ |
| 863 | PyGC_Head unreachable; /* non-problematic unreachable trash */ |
| 864 | PyGC_Head finalizers; /* objects with, & reachable from, __del__ */ |
| 865 | PyGC_Head *gc; |
| 866 | double t1 = 0.0; |
Antoine Pitrou | d4156c1 | 2012-10-30 22:43:19 +0100 | [diff] [blame] | 867 | struct gc_generation_stats *stats = &generation_stats[generation]; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 868 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 869 | if (debug & DEBUG_STATS) { |
| 870 | PySys_WriteStderr("gc: collecting generation %d...\n", |
| 871 | generation); |
| 872 | PySys_WriteStderr("gc: objects in each generation:"); |
| 873 | for (i = 0; i < NUM_GENERATIONS; i++) |
| 874 | PySys_WriteStderr(" %" PY_FORMAT_SIZE_T "d", |
| 875 | gc_list_size(GEN_HEAD(i))); |
| 876 | t1 = get_time(); |
| 877 | PySys_WriteStderr("\n"); |
| 878 | } |
Neil Schemenauer | 2880ae5 | 2002-05-04 05:35:20 +0000 | [diff] [blame] | 879 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 880 | /* update collection and allocation counters */ |
| 881 | if (generation+1 < NUM_GENERATIONS) |
| 882 | generations[generation+1].count += 1; |
| 883 | for (i = 0; i <= generation; i++) |
| 884 | generations[i].count = 0; |
Neil Schemenauer | 2880ae5 | 2002-05-04 05:35:20 +0000 | [diff] [blame] | 885 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 886 | /* merge younger generations with one we are currently collecting */ |
| 887 | for (i = 0; i < generation; i++) { |
| 888 | gc_list_merge(GEN_HEAD(i), GEN_HEAD(generation)); |
| 889 | } |
Neil Schemenauer | 2880ae5 | 2002-05-04 05:35:20 +0000 | [diff] [blame] | 890 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 891 | /* handy references */ |
| 892 | young = GEN_HEAD(generation); |
| 893 | if (generation < NUM_GENERATIONS-1) |
| 894 | old = GEN_HEAD(generation+1); |
| 895 | else |
| 896 | old = young; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 897 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 898 | /* Using ob_refcnt and gc_refs, calculate which objects in the |
| 899 | * container set are reachable from outside the set (i.e., have a |
| 900 | * refcount greater than 0 when all the references within the |
| 901 | * set are taken into account). |
| 902 | */ |
| 903 | update_refs(young); |
| 904 | subtract_refs(young); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 905 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 906 | /* Leave everything reachable from outside young in young, and move |
| 907 | * everything else (in young) to unreachable. |
| 908 | * NOTE: This used to move the reachable objects into a reachable |
| 909 | * set instead. But most things usually turn out to be reachable, |
| 910 | * so it's more efficient to move the unreachable things. |
| 911 | */ |
| 912 | gc_list_init(&unreachable); |
| 913 | move_unreachable(young, &unreachable); |
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 | /* Move reachable objects to next generation. */ |
| 916 | if (young != old) { |
| 917 | if (generation == NUM_GENERATIONS - 2) { |
| 918 | long_lived_pending += gc_list_size(young); |
| 919 | } |
| 920 | gc_list_merge(young, old); |
| 921 | } |
| 922 | else { |
Antoine Pitrou | e1ad3da | 2012-05-28 22:22:34 +0200 | [diff] [blame] | 923 | /* We only untrack dicts in full collections, to avoid quadratic |
| 924 | dict build-up. See issue #14775. */ |
| 925 | untrack_dicts(young); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 926 | long_lived_pending = 0; |
| 927 | long_lived_total = gc_list_size(young); |
| 928 | } |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 929 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 930 | /* All objects in unreachable are trash, but objects reachable from |
| 931 | * finalizers can't safely be deleted. Python programmers should take |
| 932 | * care not to create such things. For Python, finalizers means |
| 933 | * instance objects with __del__ methods. Weakrefs with callbacks |
| 934 | * can also call arbitrary Python code but they will be dealt with by |
| 935 | * handle_weakrefs(). |
| 936 | */ |
| 937 | gc_list_init(&finalizers); |
| 938 | move_finalizers(&unreachable, &finalizers); |
| 939 | /* finalizers contains the unreachable objects with a finalizer; |
| 940 | * unreachable objects reachable *from* those are also uncollectable, |
| 941 | * and we move those into the finalizers list too. |
| 942 | */ |
| 943 | move_finalizer_reachable(&finalizers); |
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 | /* Collect statistics on collectable objects found and print |
| 946 | * debugging information. |
| 947 | */ |
| 948 | for (gc = unreachable.gc.gc_next; gc != &unreachable; |
| 949 | gc = gc->gc.gc_next) { |
| 950 | m++; |
| 951 | if (debug & DEBUG_COLLECTABLE) { |
| 952 | debug_cycle("collectable", FROM_GC(gc)); |
| 953 | } |
| 954 | } |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 955 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 956 | /* Clear weakrefs and invoke callbacks as necessary. */ |
| 957 | m += handle_weakrefs(&unreachable, old); |
Tim Peters | ead8b7a | 2004-10-30 23:09:22 +0000 | [diff] [blame] | 958 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 959 | /* Call tp_clear on objects in the unreachable set. This will cause |
| 960 | * the reference cycles to be broken. It may also cause some objects |
| 961 | * in finalizers to be freed. |
| 962 | */ |
| 963 | delete_garbage(&unreachable, old); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 964 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 965 | /* Collect statistics on uncollectable objects found and print |
| 966 | * debugging information. */ |
| 967 | for (gc = finalizers.gc.gc_next; |
| 968 | gc != &finalizers; |
| 969 | gc = gc->gc.gc_next) { |
| 970 | n++; |
| 971 | if (debug & DEBUG_UNCOLLECTABLE) |
| 972 | debug_cycle("uncollectable", FROM_GC(gc)); |
| 973 | } |
| 974 | if (debug & DEBUG_STATS) { |
| 975 | double t2 = get_time(); |
| 976 | if (m == 0 && n == 0) |
| 977 | PySys_WriteStderr("gc: done"); |
| 978 | else |
| 979 | PySys_WriteStderr( |
| 980 | "gc: done, " |
| 981 | "%" PY_FORMAT_SIZE_T "d unreachable, " |
| 982 | "%" PY_FORMAT_SIZE_T "d uncollectable", |
| 983 | n+m, n); |
| 984 | if (t1 && t2) { |
| 985 | PySys_WriteStderr(", %.4fs elapsed", t2-t1); |
| 986 | } |
| 987 | PySys_WriteStderr(".\n"); |
| 988 | } |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 989 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 990 | /* Append instances in the uncollectable set to a Python |
| 991 | * reachable list of garbage. The programmer has to deal with |
| 992 | * this if they insist on creating this type of structure. |
| 993 | */ |
| 994 | (void)handle_finalizers(&finalizers, old); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 995 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 996 | /* Clear free list only during the collection of the highest |
| 997 | * generation */ |
| 998 | if (generation == NUM_GENERATIONS-1) { |
| 999 | clear_freelists(); |
| 1000 | } |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 1001 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1002 | if (PyErr_Occurred()) { |
| 1003 | if (gc_str == NULL) |
| 1004 | gc_str = PyUnicode_FromString("garbage collection"); |
| 1005 | PyErr_WriteUnraisable(gc_str); |
| 1006 | Py_FatalError("unexpected exception during garbage collection"); |
| 1007 | } |
Kristján Valur Jónsson | 69c6352 | 2012-04-15 11:41:32 +0000 | [diff] [blame] | 1008 | |
Antoine Pitrou | d4156c1 | 2012-10-30 22:43:19 +0100 | [diff] [blame] | 1009 | /* Update stats */ |
Kristján Valur Jónsson | 69c6352 | 2012-04-15 11:41:32 +0000 | [diff] [blame] | 1010 | if (n_collected) |
| 1011 | *n_collected = m; |
| 1012 | if (n_uncollectable) |
| 1013 | *n_uncollectable = n; |
Antoine Pitrou | d4156c1 | 2012-10-30 22:43:19 +0100 | [diff] [blame] | 1014 | stats->collections++; |
| 1015 | stats->collected += m; |
| 1016 | stats->uncollectable += n; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1017 | return n+m; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
Kristján Valur Jónsson | 69c6352 | 2012-04-15 11:41:32 +0000 | [diff] [blame] | 1020 | /* Invoke progress callbacks to notify clients that garbage collection |
| 1021 | * is starting or stopping |
| 1022 | */ |
| 1023 | static void |
| 1024 | invoke_gc_callback(const char *phase, int generation, |
| 1025 | Py_ssize_t collected, Py_ssize_t uncollectable) |
| 1026 | { |
| 1027 | Py_ssize_t i; |
| 1028 | PyObject *info = NULL; |
| 1029 | |
| 1030 | /* we may get called very early */ |
| 1031 | if (callbacks == NULL) |
| 1032 | return; |
| 1033 | /* The local variable cannot be rebound, check it for sanity */ |
| 1034 | assert(callbacks != NULL && PyList_CheckExact(callbacks)); |
| 1035 | if (PyList_GET_SIZE(callbacks) != 0) { |
| 1036 | info = Py_BuildValue("{sisnsn}", |
| 1037 | "generation", generation, |
| 1038 | "collected", collected, |
| 1039 | "uncollectable", uncollectable); |
| 1040 | if (info == NULL) { |
| 1041 | PyErr_WriteUnraisable(NULL); |
| 1042 | return; |
| 1043 | } |
| 1044 | } |
| 1045 | for (i=0; i<PyList_GET_SIZE(callbacks); i++) { |
| 1046 | PyObject *r, *cb = PyList_GET_ITEM(callbacks, i); |
| 1047 | Py_INCREF(cb); /* make sure cb doesn't go away */ |
| 1048 | r = PyObject_CallFunction(cb, "sO", phase, info); |
| 1049 | Py_XDECREF(r); |
| 1050 | if (r == NULL) |
| 1051 | PyErr_WriteUnraisable(cb); |
| 1052 | Py_DECREF(cb); |
| 1053 | } |
| 1054 | Py_XDECREF(info); |
| 1055 | } |
| 1056 | |
| 1057 | /* Perform garbage collection of a generation and invoke |
| 1058 | * progress callbacks. |
| 1059 | */ |
| 1060 | static Py_ssize_t |
| 1061 | collect_with_callback(int generation) |
| 1062 | { |
| 1063 | Py_ssize_t result, collected, uncollectable; |
| 1064 | invoke_gc_callback("start", generation, 0, 0); |
| 1065 | result = collect(generation, &collected, &uncollectable); |
| 1066 | invoke_gc_callback("stop", generation, collected, uncollectable); |
| 1067 | return result; |
| 1068 | } |
| 1069 | |
Neal Norwitz | 7b216c5 | 2006-03-04 20:01:53 +0000 | [diff] [blame] | 1070 | static Py_ssize_t |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1071 | collect_generations(void) |
| 1072 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1073 | int i; |
| 1074 | Py_ssize_t n = 0; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1075 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1076 | /* Find the oldest generation (highest numbered) where the count |
| 1077 | * exceeds the threshold. Objects in the that generation and |
| 1078 | * generations younger than it will be collected. */ |
| 1079 | for (i = NUM_GENERATIONS-1; i >= 0; i--) { |
| 1080 | if (generations[i].count > generations[i].threshold) { |
| 1081 | /* Avoid quadratic performance degradation in number |
| 1082 | of tracked objects. See comments at the beginning |
| 1083 | of this file, and issue #4074. |
| 1084 | */ |
| 1085 | if (i == NUM_GENERATIONS - 1 |
| 1086 | && long_lived_pending < long_lived_total / 4) |
| 1087 | continue; |
Kristján Valur Jónsson | 69c6352 | 2012-04-15 11:41:32 +0000 | [diff] [blame] | 1088 | n = collect_with_callback(i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1089 | break; |
| 1090 | } |
| 1091 | } |
| 1092 | return n; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1093 | } |
| 1094 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1095 | PyDoc_STRVAR(gc_enable__doc__, |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 1096 | "enable() -> None\n" |
| 1097 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1098 | "Enable automatic garbage collection.\n"); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1099 | |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 1100 | static PyObject * |
Tim Peters | 50c61d5 | 2003-04-06 01:50:50 +0000 | [diff] [blame] | 1101 | gc_enable(PyObject *self, PyObject *noargs) |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 1102 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1103 | enabled = 1; |
| 1104 | Py_INCREF(Py_None); |
| 1105 | return Py_None; |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 1106 | } |
| 1107 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1108 | PyDoc_STRVAR(gc_disable__doc__, |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 1109 | "disable() -> None\n" |
| 1110 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1111 | "Disable automatic garbage collection.\n"); |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 1112 | |
| 1113 | static PyObject * |
Tim Peters | 50c61d5 | 2003-04-06 01:50:50 +0000 | [diff] [blame] | 1114 | gc_disable(PyObject *self, PyObject *noargs) |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 1115 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1116 | enabled = 0; |
| 1117 | Py_INCREF(Py_None); |
| 1118 | return Py_None; |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 1119 | } |
| 1120 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1121 | PyDoc_STRVAR(gc_isenabled__doc__, |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 1122 | "isenabled() -> status\n" |
| 1123 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1124 | "Returns true if automatic garbage collection is enabled.\n"); |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 1125 | |
| 1126 | static PyObject * |
Tim Peters | 50c61d5 | 2003-04-06 01:50:50 +0000 | [diff] [blame] | 1127 | gc_isenabled(PyObject *self, PyObject *noargs) |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 1128 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1129 | return PyBool_FromLong((long)enabled); |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 1130 | } |
| 1131 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1132 | PyDoc_STRVAR(gc_collect__doc__, |
Barry Warsaw | d3c38ff | 2006-03-07 09:46:03 +0000 | [diff] [blame] | 1133 | "collect([generation]) -> n\n" |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1134 | "\n" |
Barry Warsaw | d3c38ff | 2006-03-07 09:46:03 +0000 | [diff] [blame] | 1135 | "With no arguments, run a full collection. The optional argument\n" |
| 1136 | "may be an integer specifying which generation to collect. A ValueError\n" |
| 1137 | "is raised if the generation number is invalid.\n\n" |
| 1138 | "The number of unreachable objects is returned.\n"); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1139 | |
| 1140 | static PyObject * |
Barry Warsaw | d3c38ff | 2006-03-07 09:46:03 +0000 | [diff] [blame] | 1141 | gc_collect(PyObject *self, PyObject *args, PyObject *kws) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1142 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1143 | static char *keywords[] = {"generation", NULL}; |
| 1144 | int genarg = NUM_GENERATIONS - 1; |
| 1145 | Py_ssize_t n; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1146 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1147 | if (!PyArg_ParseTupleAndKeywords(args, kws, "|i", keywords, &genarg)) |
| 1148 | return NULL; |
Barry Warsaw | d3c38ff | 2006-03-07 09:46:03 +0000 | [diff] [blame] | 1149 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1150 | else if (genarg < 0 || genarg >= NUM_GENERATIONS) { |
| 1151 | PyErr_SetString(PyExc_ValueError, "invalid generation"); |
| 1152 | return NULL; |
| 1153 | } |
Barry Warsaw | d3c38ff | 2006-03-07 09:46:03 +0000 | [diff] [blame] | 1154 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1155 | if (collecting) |
| 1156 | n = 0; /* already collecting, don't do anything */ |
| 1157 | else { |
| 1158 | collecting = 1; |
Kristján Valur Jónsson | 69c6352 | 2012-04-15 11:41:32 +0000 | [diff] [blame] | 1159 | n = collect_with_callback(genarg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1160 | collecting = 0; |
| 1161 | } |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1162 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1163 | return PyLong_FromSsize_t(n); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1166 | PyDoc_STRVAR(gc_set_debug__doc__, |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1167 | "set_debug(flags) -> None\n" |
| 1168 | "\n" |
| 1169 | "Set the garbage collection debugging flags. Debugging information is\n" |
| 1170 | "written to sys.stderr.\n" |
| 1171 | "\n" |
| 1172 | "flags is an integer and can have the following bits turned on:\n" |
| 1173 | "\n" |
| 1174 | " DEBUG_STATS - Print statistics during collection.\n" |
| 1175 | " DEBUG_COLLECTABLE - Print collectable objects found.\n" |
| 1176 | " DEBUG_UNCOLLECTABLE - Print unreachable but uncollectable objects found.\n" |
Neil Schemenauer | 544de1e | 2000-09-22 15:22:38 +0000 | [diff] [blame] | 1177 | " 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] | 1178 | " DEBUG_LEAK - Debug leaking programs (everything but STATS).\n"); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1179 | |
| 1180 | static PyObject * |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 1181 | gc_set_debug(PyObject *self, PyObject *args) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1182 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1183 | if (!PyArg_ParseTuple(args, "i:set_debug", &debug)) |
| 1184 | return NULL; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1185 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1186 | Py_INCREF(Py_None); |
| 1187 | return Py_None; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1188 | } |
| 1189 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1190 | PyDoc_STRVAR(gc_get_debug__doc__, |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1191 | "get_debug() -> flags\n" |
| 1192 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1193 | "Get the garbage collection debugging flags.\n"); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1194 | |
| 1195 | static PyObject * |
Tim Peters | 50c61d5 | 2003-04-06 01:50:50 +0000 | [diff] [blame] | 1196 | gc_get_debug(PyObject *self, PyObject *noargs) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1197 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1198 | return Py_BuildValue("i", debug); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1199 | } |
| 1200 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1201 | PyDoc_STRVAR(gc_set_thresh__doc__, |
Neal Norwitz | 2a47c0f | 2002-01-29 00:53:41 +0000 | [diff] [blame] | 1202 | "set_threshold(threshold0, [threshold1, threshold2]) -> None\n" |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1203 | "\n" |
| 1204 | "Sets the collection thresholds. Setting threshold0 to zero disables\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1205 | "collection.\n"); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1206 | |
| 1207 | static PyObject * |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 1208 | gc_set_thresh(PyObject *self, PyObject *args) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1209 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1210 | int i; |
| 1211 | if (!PyArg_ParseTuple(args, "i|ii:set_threshold", |
| 1212 | &generations[0].threshold, |
| 1213 | &generations[1].threshold, |
| 1214 | &generations[2].threshold)) |
| 1215 | return NULL; |
| 1216 | for (i = 2; i < NUM_GENERATIONS; i++) { |
| 1217 | /* generations higher than 2 get the same threshold */ |
| 1218 | generations[i].threshold = generations[2].threshold; |
| 1219 | } |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1220 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1221 | Py_INCREF(Py_None); |
| 1222 | return Py_None; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1223 | } |
| 1224 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1225 | PyDoc_STRVAR(gc_get_thresh__doc__, |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1226 | "get_threshold() -> (threshold0, threshold1, threshold2)\n" |
| 1227 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1228 | "Return the current collection thresholds\n"); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1229 | |
| 1230 | static PyObject * |
Tim Peters | 50c61d5 | 2003-04-06 01:50:50 +0000 | [diff] [blame] | 1231 | gc_get_thresh(PyObject *self, PyObject *noargs) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1232 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1233 | return Py_BuildValue("(iii)", |
| 1234 | generations[0].threshold, |
| 1235 | generations[1].threshold, |
| 1236 | generations[2].threshold); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1237 | } |
| 1238 | |
Barry Warsaw | d3c38ff | 2006-03-07 09:46:03 +0000 | [diff] [blame] | 1239 | PyDoc_STRVAR(gc_get_count__doc__, |
| 1240 | "get_count() -> (count0, count1, count2)\n" |
| 1241 | "\n" |
| 1242 | "Return the current collection counts\n"); |
| 1243 | |
| 1244 | static PyObject * |
| 1245 | gc_get_count(PyObject *self, PyObject *noargs) |
| 1246 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1247 | return Py_BuildValue("(iii)", |
| 1248 | generations[0].count, |
| 1249 | generations[1].count, |
| 1250 | generations[2].count); |
Barry Warsaw | d3c38ff | 2006-03-07 09:46:03 +0000 | [diff] [blame] | 1251 | } |
| 1252 | |
Neil Schemenauer | 48c7034 | 2001-08-09 15:38:31 +0000 | [diff] [blame] | 1253 | static int |
Martin v. Löwis | 560da62 | 2001-11-24 09:24:51 +0000 | [diff] [blame] | 1254 | referrersvisit(PyObject* obj, PyObject *objs) |
Neil Schemenauer | 48c7034 | 2001-08-09 15:38:31 +0000 | [diff] [blame] | 1255 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1256 | Py_ssize_t i; |
| 1257 | for (i = 0; i < PyTuple_GET_SIZE(objs); i++) |
| 1258 | if (PyTuple_GET_ITEM(objs, i) == obj) |
| 1259 | return 1; |
| 1260 | return 0; |
Neil Schemenauer | 48c7034 | 2001-08-09 15:38:31 +0000 | [diff] [blame] | 1261 | } |
| 1262 | |
Neil Schemenauer | 17e7be6 | 2001-08-10 14:46:47 +0000 | [diff] [blame] | 1263 | static int |
Martin v. Löwis | 560da62 | 2001-11-24 09:24:51 +0000 | [diff] [blame] | 1264 | gc_referrers_for(PyObject *objs, PyGC_Head *list, PyObject *resultlist) |
Neil Schemenauer | 48c7034 | 2001-08-09 15:38:31 +0000 | [diff] [blame] | 1265 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1266 | PyGC_Head *gc; |
| 1267 | PyObject *obj; |
| 1268 | traverseproc traverse; |
| 1269 | for (gc = list->gc.gc_next; gc != list; gc = gc->gc.gc_next) { |
| 1270 | obj = FROM_GC(gc); |
| 1271 | traverse = Py_TYPE(obj)->tp_traverse; |
| 1272 | if (obj == objs || obj == resultlist) |
| 1273 | continue; |
| 1274 | if (traverse(obj, (visitproc)referrersvisit, objs)) { |
| 1275 | if (PyList_Append(resultlist, obj) < 0) |
| 1276 | return 0; /* error */ |
| 1277 | } |
| 1278 | } |
| 1279 | return 1; /* no error */ |
Neil Schemenauer | 48c7034 | 2001-08-09 15:38:31 +0000 | [diff] [blame] | 1280 | } |
| 1281 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1282 | PyDoc_STRVAR(gc_get_referrers__doc__, |
Martin v. Löwis | 560da62 | 2001-11-24 09:24:51 +0000 | [diff] [blame] | 1283 | "get_referrers(*objs) -> list\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1284 | Return the list of objects that directly refer to any of objs."); |
Neil Schemenauer | 48c7034 | 2001-08-09 15:38:31 +0000 | [diff] [blame] | 1285 | |
Neil Schemenauer | 17e7be6 | 2001-08-10 14:46:47 +0000 | [diff] [blame] | 1286 | static PyObject * |
Martin v. Löwis | 560da62 | 2001-11-24 09:24:51 +0000 | [diff] [blame] | 1287 | gc_get_referrers(PyObject *self, PyObject *args) |
Neil Schemenauer | 48c7034 | 2001-08-09 15:38:31 +0000 | [diff] [blame] | 1288 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1289 | int i; |
| 1290 | PyObject *result = PyList_New(0); |
| 1291 | if (!result) return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1292 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1293 | for (i = 0; i < NUM_GENERATIONS; i++) { |
| 1294 | if (!(gc_referrers_for(args, GEN_HEAD(i), result))) { |
| 1295 | Py_DECREF(result); |
| 1296 | return NULL; |
| 1297 | } |
| 1298 | } |
| 1299 | return result; |
Neil Schemenauer | 48c7034 | 2001-08-09 15:38:31 +0000 | [diff] [blame] | 1300 | } |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1301 | |
Tim Peters | 0f81ab6 | 2003-04-08 16:39:48 +0000 | [diff] [blame] | 1302 | /* 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] | 1303 | static int |
Tim Peters | 730f553 | 2003-04-08 17:17:17 +0000 | [diff] [blame] | 1304 | referentsvisit(PyObject *obj, PyObject *list) |
Jeremy Hylton | 5bd378b | 2003-04-03 16:28:38 +0000 | [diff] [blame] | 1305 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1306 | return PyList_Append(list, obj) < 0; |
Jeremy Hylton | 5bd378b | 2003-04-03 16:28:38 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
Tim Peters | 730f553 | 2003-04-08 17:17:17 +0000 | [diff] [blame] | 1309 | PyDoc_STRVAR(gc_get_referents__doc__, |
| 1310 | "get_referents(*objs) -> list\n\ |
Jeremy Hylton | 059b094 | 2003-04-03 16:29:13 +0000 | [diff] [blame] | 1311 | Return the list of objects that are directly referred to by objs."); |
Jeremy Hylton | 5bd378b | 2003-04-03 16:28:38 +0000 | [diff] [blame] | 1312 | |
| 1313 | static PyObject * |
Tim Peters | 730f553 | 2003-04-08 17:17:17 +0000 | [diff] [blame] | 1314 | gc_get_referents(PyObject *self, PyObject *args) |
Jeremy Hylton | 5bd378b | 2003-04-03 16:28:38 +0000 | [diff] [blame] | 1315 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1316 | Py_ssize_t i; |
| 1317 | PyObject *result = PyList_New(0); |
Tim Peters | 0f81ab6 | 2003-04-08 16:39:48 +0000 | [diff] [blame] | 1318 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1319 | if (result == NULL) |
| 1320 | return NULL; |
Tim Peters | 0f81ab6 | 2003-04-08 16:39:48 +0000 | [diff] [blame] | 1321 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1322 | for (i = 0; i < PyTuple_GET_SIZE(args); i++) { |
| 1323 | traverseproc traverse; |
| 1324 | PyObject *obj = PyTuple_GET_ITEM(args, i); |
Tim Peters | 0f81ab6 | 2003-04-08 16:39:48 +0000 | [diff] [blame] | 1325 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1326 | if (! PyObject_IS_GC(obj)) |
| 1327 | continue; |
| 1328 | traverse = Py_TYPE(obj)->tp_traverse; |
| 1329 | if (! traverse) |
| 1330 | continue; |
| 1331 | if (traverse(obj, (visitproc)referentsvisit, result)) { |
| 1332 | Py_DECREF(result); |
| 1333 | return NULL; |
| 1334 | } |
| 1335 | } |
| 1336 | return result; |
Jeremy Hylton | 5bd378b | 2003-04-03 16:28:38 +0000 | [diff] [blame] | 1337 | } |
| 1338 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1339 | PyDoc_STRVAR(gc_get_objects__doc__, |
Neil Schemenauer | c7c8d8e | 2001-08-09 15:58:59 +0000 | [diff] [blame] | 1340 | "get_objects() -> [...]\n" |
| 1341 | "\n" |
| 1342 | "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] | 1343 | "returned).\n"); |
Neil Schemenauer | c7c8d8e | 2001-08-09 15:58:59 +0000 | [diff] [blame] | 1344 | |
Neil Schemenauer | c7c8d8e | 2001-08-09 15:58:59 +0000 | [diff] [blame] | 1345 | static PyObject * |
Tim Peters | 50c61d5 | 2003-04-06 01:50:50 +0000 | [diff] [blame] | 1346 | gc_get_objects(PyObject *self, PyObject *noargs) |
Neil Schemenauer | c7c8d8e | 2001-08-09 15:58:59 +0000 | [diff] [blame] | 1347 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1348 | int i; |
| 1349 | PyObject* result; |
Neil Schemenauer | c7c8d8e | 2001-08-09 15:58:59 +0000 | [diff] [blame] | 1350 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1351 | result = PyList_New(0); |
| 1352 | if (result == NULL) |
| 1353 | return NULL; |
| 1354 | for (i = 0; i < NUM_GENERATIONS; i++) { |
| 1355 | if (append_objects(result, GEN_HEAD(i))) { |
| 1356 | Py_DECREF(result); |
| 1357 | return NULL; |
| 1358 | } |
| 1359 | } |
| 1360 | return result; |
Neil Schemenauer | c7c8d8e | 2001-08-09 15:58:59 +0000 | [diff] [blame] | 1361 | } |
| 1362 | |
Antoine Pitrou | d4156c1 | 2012-10-30 22:43:19 +0100 | [diff] [blame] | 1363 | PyDoc_STRVAR(gc_get_stats__doc__, |
| 1364 | "get_stats() -> [...]\n" |
| 1365 | "\n" |
| 1366 | "Return a list of dictionaries containing per-generation statistics.\n"); |
| 1367 | |
| 1368 | static PyObject * |
| 1369 | gc_get_stats(PyObject *self, PyObject *noargs) |
| 1370 | { |
| 1371 | int i; |
| 1372 | PyObject *result; |
| 1373 | struct gc_generation_stats stats[NUM_GENERATIONS], *st; |
| 1374 | |
| 1375 | /* To get consistent values despite allocations while constructing |
| 1376 | the result list, we use a snapshot of the running stats. */ |
| 1377 | for (i = 0; i < NUM_GENERATIONS; i++) { |
| 1378 | stats[i] = generation_stats[i]; |
| 1379 | } |
| 1380 | |
| 1381 | result = PyList_New(0); |
| 1382 | if (result == NULL) |
| 1383 | return NULL; |
| 1384 | |
| 1385 | for (i = 0; i < NUM_GENERATIONS; i++) { |
| 1386 | PyObject *dict; |
| 1387 | st = &stats[i]; |
| 1388 | dict = Py_BuildValue("{snsnsn}", |
| 1389 | "collections", st->collections, |
| 1390 | "collected", st->collected, |
| 1391 | "uncollectable", st->uncollectable |
| 1392 | ); |
| 1393 | if (dict == NULL) |
| 1394 | goto error; |
| 1395 | if (PyList_Append(result, dict)) { |
| 1396 | Py_DECREF(dict); |
| 1397 | goto error; |
| 1398 | } |
| 1399 | Py_DECREF(dict); |
| 1400 | } |
| 1401 | return result; |
| 1402 | |
| 1403 | error: |
| 1404 | Py_XDECREF(result); |
| 1405 | return NULL; |
| 1406 | } |
| 1407 | |
| 1408 | |
Antoine Pitrou | 3a652b1 | 2009-03-23 18:52:06 +0000 | [diff] [blame] | 1409 | PyDoc_STRVAR(gc_is_tracked__doc__, |
| 1410 | "is_tracked(obj) -> bool\n" |
| 1411 | "\n" |
| 1412 | "Returns true if the object is tracked by the garbage collector.\n" |
| 1413 | "Simple atomic objects will return false.\n" |
| 1414 | ); |
| 1415 | |
| 1416 | static PyObject * |
| 1417 | gc_is_tracked(PyObject *self, PyObject *obj) |
| 1418 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1419 | PyObject *result; |
| 1420 | |
| 1421 | if (PyObject_IS_GC(obj) && IS_TRACKED(obj)) |
| 1422 | result = Py_True; |
| 1423 | else |
| 1424 | result = Py_False; |
| 1425 | Py_INCREF(result); |
| 1426 | return result; |
Antoine Pitrou | 3a652b1 | 2009-03-23 18:52:06 +0000 | [diff] [blame] | 1427 | } |
| 1428 | |
Neil Schemenauer | c7c8d8e | 2001-08-09 15:58:59 +0000 | [diff] [blame] | 1429 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1430 | PyDoc_STRVAR(gc__doc__, |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1431 | "This module provides access to the garbage collector for reference cycles.\n" |
| 1432 | "\n" |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 1433 | "enable() -- Enable automatic garbage collection.\n" |
| 1434 | "disable() -- Disable automatic garbage collection.\n" |
| 1435 | "isenabled() -- Returns true if automatic collection is enabled.\n" |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1436 | "collect() -- Do a full collection right now.\n" |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1437 | "get_count() -- Return the current collection counts.\n" |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1438 | "set_debug() -- Set debugging flags.\n" |
| 1439 | "get_debug() -- Get debugging flags.\n" |
| 1440 | "set_threshold() -- Set the collection thresholds.\n" |
| 1441 | "get_threshold() -- Return the current the collection thresholds.\n" |
Neil Schemenauer | c7c8d8e | 2001-08-09 15:58:59 +0000 | [diff] [blame] | 1442 | "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] | 1443 | "is_tracked() -- Returns true if a given object is tracked.\n" |
Jeremy Hylton | 5bd378b | 2003-04-03 16:28:38 +0000 | [diff] [blame] | 1444 | "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] | 1445 | "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] | 1446 | |
| 1447 | static PyMethodDef GcMethods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1448 | {"enable", gc_enable, METH_NOARGS, gc_enable__doc__}, |
| 1449 | {"disable", gc_disable, METH_NOARGS, gc_disable__doc__}, |
| 1450 | {"isenabled", gc_isenabled, METH_NOARGS, gc_isenabled__doc__}, |
| 1451 | {"set_debug", gc_set_debug, METH_VARARGS, gc_set_debug__doc__}, |
| 1452 | {"get_debug", gc_get_debug, METH_NOARGS, gc_get_debug__doc__}, |
| 1453 | {"get_count", gc_get_count, METH_NOARGS, gc_get_count__doc__}, |
| 1454 | {"set_threshold", gc_set_thresh, METH_VARARGS, gc_set_thresh__doc__}, |
| 1455 | {"get_threshold", gc_get_thresh, METH_NOARGS, gc_get_thresh__doc__}, |
| 1456 | {"collect", (PyCFunction)gc_collect, |
| 1457 | METH_VARARGS | METH_KEYWORDS, gc_collect__doc__}, |
| 1458 | {"get_objects", gc_get_objects,METH_NOARGS, gc_get_objects__doc__}, |
Antoine Pitrou | d4156c1 | 2012-10-30 22:43:19 +0100 | [diff] [blame] | 1459 | {"get_stats", gc_get_stats, METH_NOARGS, gc_get_stats__doc__}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1460 | {"is_tracked", gc_is_tracked, METH_O, gc_is_tracked__doc__}, |
| 1461 | {"get_referrers", gc_get_referrers, METH_VARARGS, |
| 1462 | gc_get_referrers__doc__}, |
| 1463 | {"get_referents", gc_get_referents, METH_VARARGS, |
| 1464 | gc_get_referents__doc__}, |
| 1465 | {NULL, NULL} /* Sentinel */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1466 | }; |
| 1467 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1468 | static struct PyModuleDef gcmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1469 | PyModuleDef_HEAD_INIT, |
Antoine Pitrou | 696e035 | 2010-08-08 22:18:46 +0000 | [diff] [blame] | 1470 | "gc", /* m_name */ |
| 1471 | gc__doc__, /* m_doc */ |
| 1472 | -1, /* m_size */ |
| 1473 | GcMethods, /* m_methods */ |
| 1474 | NULL, /* m_reload */ |
| 1475 | NULL, /* m_traverse */ |
| 1476 | NULL, /* m_clear */ |
| 1477 | NULL /* m_free */ |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1478 | }; |
| 1479 | |
Jason Tishler | 6bc06ec | 2003-09-04 11:59:50 +0000 | [diff] [blame] | 1480 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1481 | PyInit_gc(void) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1482 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1483 | PyObject *m; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1484 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1485 | m = PyModule_Create(&gcmodule); |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1486 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1487 | if (m == NULL) |
| 1488 | return NULL; |
Tim Peters | 1155887 | 2003-04-06 23:30:52 +0000 | [diff] [blame] | 1489 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1490 | if (garbage == NULL) { |
| 1491 | garbage = PyList_New(0); |
| 1492 | if (garbage == NULL) |
| 1493 | return NULL; |
| 1494 | } |
| 1495 | Py_INCREF(garbage); |
| 1496 | if (PyModule_AddObject(m, "garbage", garbage) < 0) |
| 1497 | return NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1498 | |
Kristján Valur Jónsson | 69c6352 | 2012-04-15 11:41:32 +0000 | [diff] [blame] | 1499 | if (callbacks == NULL) { |
| 1500 | callbacks = PyList_New(0); |
| 1501 | if (callbacks == NULL) |
| 1502 | return NULL; |
| 1503 | } |
| 1504 | Py_INCREF(callbacks); |
| 1505 | if (PyModule_AddObject(m, "callbacks", callbacks) < 0) |
| 1506 | return NULL; |
| 1507 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1508 | /* Importing can't be done in collect() because collect() |
| 1509 | * can be called via PyGC_Collect() in Py_Finalize(). |
| 1510 | * This wouldn't be a problem, except that <initialized> is |
| 1511 | * reset to 0 before calling collect which trips up |
| 1512 | * the import and triggers an assertion. |
| 1513 | */ |
| 1514 | if (tmod == NULL) { |
| 1515 | tmod = PyImport_ImportModuleNoBlock("time"); |
| 1516 | if (tmod == NULL) |
| 1517 | PyErr_Clear(); |
| 1518 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1519 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1520 | #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] | 1521 | ADD_INT(DEBUG_STATS); |
| 1522 | ADD_INT(DEBUG_COLLECTABLE); |
| 1523 | ADD_INT(DEBUG_UNCOLLECTABLE); |
| 1524 | ADD_INT(DEBUG_SAVEALL); |
| 1525 | ADD_INT(DEBUG_LEAK); |
Tim Peters | 1155887 | 2003-04-06 23:30:52 +0000 | [diff] [blame] | 1526 | #undef ADD_INT |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1527 | return m; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1528 | } |
| 1529 | |
Guido van Rossum | e13ddc9 | 2003-04-17 17:29:22 +0000 | [diff] [blame] | 1530 | /* API to invoke gc.collect() from C */ |
Neal Norwitz | 7b216c5 | 2006-03-04 20:01:53 +0000 | [diff] [blame] | 1531 | Py_ssize_t |
Guido van Rossum | e13ddc9 | 2003-04-17 17:29:22 +0000 | [diff] [blame] | 1532 | PyGC_Collect(void) |
| 1533 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1534 | Py_ssize_t n; |
Guido van Rossum | e13ddc9 | 2003-04-17 17:29:22 +0000 | [diff] [blame] | 1535 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1536 | if (collecting) |
| 1537 | n = 0; /* already collecting, don't do anything */ |
| 1538 | else { |
| 1539 | collecting = 1; |
Kristján Valur Jónsson | 69c6352 | 2012-04-15 11:41:32 +0000 | [diff] [blame] | 1540 | n = collect_with_callback(NUM_GENERATIONS - 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1541 | collecting = 0; |
| 1542 | } |
Guido van Rossum | e13ddc9 | 2003-04-17 17:29:22 +0000 | [diff] [blame] | 1543 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1544 | return n; |
Guido van Rossum | e13ddc9 | 2003-04-17 17:29:22 +0000 | [diff] [blame] | 1545 | } |
| 1546 | |
Antoine Pitrou | 696e035 | 2010-08-08 22:18:46 +0000 | [diff] [blame] | 1547 | void |
| 1548 | _PyGC_Fini(void) |
| 1549 | { |
Antoine Pitrou | 2ed94eb | 2010-09-14 09:48:39 +0000 | [diff] [blame] | 1550 | if (!(debug & DEBUG_SAVEALL) |
| 1551 | && garbage != NULL && PyList_GET_SIZE(garbage) > 0) { |
Georg Brandl | 08be72d | 2010-10-24 15:11:22 +0000 | [diff] [blame] | 1552 | char *message; |
| 1553 | if (debug & DEBUG_UNCOLLECTABLE) |
Antoine Pitrou | b5d8204 | 2010-11-05 00:05:25 +0000 | [diff] [blame] | 1554 | message = "gc: %zd uncollectable objects at " \ |
Georg Brandl | 08be72d | 2010-10-24 15:11:22 +0000 | [diff] [blame] | 1555 | "shutdown"; |
| 1556 | else |
Antoine Pitrou | b5d8204 | 2010-11-05 00:05:25 +0000 | [diff] [blame] | 1557 | message = "gc: %zd uncollectable objects at " \ |
Georg Brandl | 08be72d | 2010-10-24 15:11:22 +0000 | [diff] [blame] | 1558 | "shutdown; use gc.set_debug(gc.DEBUG_UNCOLLECTABLE) to list them"; |
| 1559 | if (PyErr_WarnFormat(PyExc_ResourceWarning, 0, message, |
| 1560 | PyList_GET_SIZE(garbage)) < 0) |
| 1561 | PyErr_WriteUnraisable(NULL); |
Antoine Pitrou | 696e035 | 2010-08-08 22:18:46 +0000 | [diff] [blame] | 1562 | if (debug & DEBUG_UNCOLLECTABLE) { |
| 1563 | PyObject *repr = NULL, *bytes = NULL; |
| 1564 | repr = PyObject_Repr(garbage); |
| 1565 | if (!repr || !(bytes = PyUnicode_EncodeFSDefault(repr))) |
| 1566 | PyErr_WriteUnraisable(garbage); |
| 1567 | else { |
| 1568 | PySys_WriteStderr( |
| 1569 | " %s\n", |
| 1570 | PyBytes_AS_STRING(bytes) |
| 1571 | ); |
| 1572 | } |
| 1573 | Py_XDECREF(repr); |
| 1574 | Py_XDECREF(bytes); |
| 1575 | } |
Antoine Pitrou | 696e035 | 2010-08-08 22:18:46 +0000 | [diff] [blame] | 1576 | } |
Kristján Valur Jónsson | 69c6352 | 2012-04-15 11:41:32 +0000 | [diff] [blame] | 1577 | Py_CLEAR(callbacks); |
Antoine Pitrou | 696e035 | 2010-08-08 22:18:46 +0000 | [diff] [blame] | 1578 | } |
| 1579 | |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1580 | /* for debugging */ |
Guido van Rossum | e13ddc9 | 2003-04-17 17:29:22 +0000 | [diff] [blame] | 1581 | void |
| 1582 | _PyGC_Dump(PyGC_Head *g) |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1583 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1584 | _PyObject_Dump(FROM_GC(g)); |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1585 | } |
| 1586 | |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1587 | /* extension modules might be compiled with GC support so these |
| 1588 | functions must always be available */ |
| 1589 | |
Neil Schemenauer | fec4eb1 | 2002-04-12 02:41:03 +0000 | [diff] [blame] | 1590 | #undef PyObject_GC_Track |
| 1591 | #undef PyObject_GC_UnTrack |
| 1592 | #undef PyObject_GC_Del |
| 1593 | #undef _PyObject_GC_Malloc |
| 1594 | |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1595 | void |
Neil Schemenauer | fec4eb1 | 2002-04-12 02:41:03 +0000 | [diff] [blame] | 1596 | PyObject_GC_Track(void *op) |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1597 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1598 | _PyObject_GC_TRACK(op); |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1599 | } |
| 1600 | |
Neil Schemenauer | fec4eb1 | 2002-04-12 02:41:03 +0000 | [diff] [blame] | 1601 | /* for binary compatibility with 2.2 */ |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1602 | void |
Neil Schemenauer | fec4eb1 | 2002-04-12 02:41:03 +0000 | [diff] [blame] | 1603 | _PyObject_GC_Track(PyObject *op) |
| 1604 | { |
| 1605 | PyObject_GC_Track(op); |
| 1606 | } |
| 1607 | |
| 1608 | void |
| 1609 | PyObject_GC_UnTrack(void *op) |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1610 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1611 | /* Obscure: the Py_TRASHCAN mechanism requires that we be able to |
| 1612 | * call PyObject_GC_UnTrack twice on an object. |
| 1613 | */ |
| 1614 | if (IS_TRACKED(op)) |
| 1615 | _PyObject_GC_UNTRACK(op); |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1616 | } |
| 1617 | |
Neil Schemenauer | fec4eb1 | 2002-04-12 02:41:03 +0000 | [diff] [blame] | 1618 | /* for binary compatibility with 2.2 */ |
| 1619 | void |
| 1620 | _PyObject_GC_UnTrack(PyObject *op) |
| 1621 | { |
| 1622 | PyObject_GC_UnTrack(op); |
| 1623 | } |
| 1624 | |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1625 | PyObject * |
Neil Schemenauer | fec4eb1 | 2002-04-12 02:41:03 +0000 | [diff] [blame] | 1626 | _PyObject_GC_Malloc(size_t basicsize) |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1627 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1628 | PyObject *op; |
| 1629 | PyGC_Head *g; |
| 1630 | if (basicsize > PY_SSIZE_T_MAX - sizeof(PyGC_Head)) |
| 1631 | return PyErr_NoMemory(); |
| 1632 | g = (PyGC_Head *)PyObject_MALLOC( |
| 1633 | sizeof(PyGC_Head) + basicsize); |
| 1634 | if (g == NULL) |
| 1635 | return PyErr_NoMemory(); |
| 1636 | g->gc.gc_refs = GC_UNTRACKED; |
| 1637 | generations[0].count++; /* number of allocated GC objects */ |
| 1638 | if (generations[0].count > generations[0].threshold && |
| 1639 | enabled && |
| 1640 | generations[0].threshold && |
| 1641 | !collecting && |
| 1642 | !PyErr_Occurred()) { |
| 1643 | collecting = 1; |
| 1644 | collect_generations(); |
| 1645 | collecting = 0; |
| 1646 | } |
| 1647 | op = FROM_GC(g); |
| 1648 | return op; |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1649 | } |
| 1650 | |
| 1651 | PyObject * |
| 1652 | _PyObject_GC_New(PyTypeObject *tp) |
| 1653 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1654 | PyObject *op = _PyObject_GC_Malloc(_PyObject_SIZE(tp)); |
| 1655 | if (op != NULL) |
| 1656 | op = PyObject_INIT(op, tp); |
| 1657 | return op; |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1658 | } |
| 1659 | |
| 1660 | PyVarObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1661 | _PyObject_GC_NewVar(PyTypeObject *tp, Py_ssize_t nitems) |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1662 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1663 | const size_t size = _PyObject_VAR_SIZE(tp, nitems); |
| 1664 | PyVarObject *op = (PyVarObject *) _PyObject_GC_Malloc(size); |
| 1665 | if (op != NULL) |
| 1666 | op = PyObject_INIT_VAR(op, tp, nitems); |
| 1667 | return op; |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1668 | } |
| 1669 | |
| 1670 | PyVarObject * |
Martin v. Löwis | 4129068 | 2006-02-16 14:56:14 +0000 | [diff] [blame] | 1671 | _PyObject_GC_Resize(PyVarObject *op, Py_ssize_t nitems) |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1672 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1673 | const size_t basicsize = _PyObject_VAR_SIZE(Py_TYPE(op), nitems); |
| 1674 | PyGC_Head *g = AS_GC(op); |
| 1675 | if (basicsize > PY_SSIZE_T_MAX - sizeof(PyGC_Head)) |
| 1676 | return (PyVarObject *)PyErr_NoMemory(); |
| 1677 | g = (PyGC_Head *)PyObject_REALLOC(g, sizeof(PyGC_Head) + basicsize); |
| 1678 | if (g == NULL) |
| 1679 | return (PyVarObject *)PyErr_NoMemory(); |
| 1680 | op = (PyVarObject *) FROM_GC(g); |
| 1681 | Py_SIZE(op) = nitems; |
| 1682 | return op; |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1683 | } |
| 1684 | |
| 1685 | void |
Neil Schemenauer | fec4eb1 | 2002-04-12 02:41:03 +0000 | [diff] [blame] | 1686 | PyObject_GC_Del(void *op) |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1687 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1688 | PyGC_Head *g = AS_GC(op); |
| 1689 | if (IS_TRACKED(op)) |
| 1690 | gc_list_remove(g); |
| 1691 | if (generations[0].count > 0) { |
| 1692 | generations[0].count--; |
| 1693 | } |
| 1694 | PyObject_FREE(g); |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1695 | } |