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