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" |
| 22 | |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 23 | /* Get an object's GC head */ |
| 24 | #define AS_GC(o) ((PyGC_Head *)(o)-1) |
| 25 | |
| 26 | /* Get the object given the GC head */ |
| 27 | #define FROM_GC(g) ((PyObject *)(((PyGC_Head *)g)+1)) |
| 28 | |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 29 | /*** Global GC state ***/ |
| 30 | |
Neil Schemenauer | 2880ae5 | 2002-05-04 05:35:20 +0000 | [diff] [blame] | 31 | struct gc_generation { |
| 32 | PyGC_Head head; |
| 33 | int threshold; /* collection threshold */ |
| 34 | int count; /* count of allocations or collections of younger |
| 35 | generations */ |
| 36 | }; |
| 37 | |
| 38 | #define NUM_GENERATIONS 3 |
| 39 | #define GEN_HEAD(n) (&generations[n].head) |
| 40 | |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 41 | /* linked lists of container objects */ |
Neil Schemenauer | 2880ae5 | 2002-05-04 05:35:20 +0000 | [diff] [blame] | 42 | static struct gc_generation generations[NUM_GENERATIONS] = { |
| 43 | /* PyGC_Head, threshold, count */ |
| 44 | {{{GEN_HEAD(0), GEN_HEAD(0), 0}}, 700, 0}, |
| 45 | {{{GEN_HEAD(1), GEN_HEAD(1), 0}}, 10, 0}, |
| 46 | {{{GEN_HEAD(2), GEN_HEAD(2), 0}}, 10, 0}, |
| 47 | }; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 48 | |
Neil Schemenauer | 2880ae5 | 2002-05-04 05:35:20 +0000 | [diff] [blame] | 49 | PyGC_Head *_PyGC_generation0 = GEN_HEAD(0); |
| 50 | |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 51 | static int enabled = 1; /* automatic collection enabled? */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 52 | |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 53 | /* true if we are currently running the collector */ |
| 54 | static int collecting; |
| 55 | |
Tim Peters | 6fc13d9 | 2002-07-02 18:12:35 +0000 | [diff] [blame] | 56 | /* list of uncollectable objects */ |
| 57 | static PyObject *garbage; |
| 58 | |
| 59 | /* Python string to use if unhandled exception occurs */ |
| 60 | static PyObject *gc_str; |
| 61 | |
Tim Peters | 93ad66d | 2003-04-05 17:15:44 +0000 | [diff] [blame] | 62 | /* Python string used to look for __del__ attribute. */ |
| 63 | static PyObject *delstr = NULL; |
Jeremy Hylton | ce136e9 | 2003-04-04 19:59:06 +0000 | [diff] [blame] | 64 | |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 65 | /* set for debugging information */ |
| 66 | #define DEBUG_STATS (1<<0) /* print collection statistics */ |
| 67 | #define DEBUG_COLLECTABLE (1<<1) /* print collectable objects */ |
| 68 | #define DEBUG_UNCOLLECTABLE (1<<2) /* print uncollectable objects */ |
| 69 | #define DEBUG_INSTANCES (1<<3) /* print instances */ |
| 70 | #define DEBUG_OBJECTS (1<<4) /* print other objects */ |
Neil Schemenauer | 544de1e | 2000-09-22 15:22:38 +0000 | [diff] [blame] | 71 | #define DEBUG_SAVEALL (1<<5) /* save all garbage in gc.garbage */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 72 | #define DEBUG_LEAK DEBUG_COLLECTABLE | \ |
| 73 | DEBUG_UNCOLLECTABLE | \ |
| 74 | DEBUG_INSTANCES | \ |
Neil Schemenauer | 544de1e | 2000-09-22 15:22:38 +0000 | [diff] [blame] | 75 | DEBUG_OBJECTS | \ |
| 76 | DEBUG_SAVEALL |
Jeremy Hylton | b709df3 | 2000-09-01 02:47:25 +0000 | [diff] [blame] | 77 | static int debug; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 78 | |
Tim Peters | 6fc13d9 | 2002-07-02 18:12:35 +0000 | [diff] [blame] | 79 | /*-------------------------------------------------------------------------- |
| 80 | gc_refs values. |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 81 | |
Tim Peters | 6fc13d9 | 2002-07-02 18:12:35 +0000 | [diff] [blame] | 82 | Between collections, every gc'ed object has one of two gc_refs values: |
| 83 | |
| 84 | GC_UNTRACKED |
| 85 | The initial state; objects returned by PyObject_GC_Malloc are in this |
| 86 | state. The object doesn't live in any generation list, and its |
| 87 | tp_traverse slot must not be called. |
| 88 | |
| 89 | GC_REACHABLE |
| 90 | The object lives in some generation list, and its tp_traverse is safe to |
| 91 | call. An object transitions to GC_REACHABLE when PyObject_GC_Track |
| 92 | is called. |
| 93 | |
| 94 | During a collection, gc_refs can temporarily take on other states: |
| 95 | |
| 96 | >= 0 |
| 97 | At the start of a collection, update_refs() copies the true refcount |
| 98 | to gc_refs, for each object in the generation being collected. |
| 99 | subtract_refs() then adjusts gc_refs so that it equals the number of |
| 100 | times an object is referenced directly from outside the generation |
| 101 | being collected. |
Martin v. Löwis | 774348c | 2002-11-09 19:54:06 +0000 | [diff] [blame] | 102 | gc_refs remains >= 0 throughout these steps. |
Tim Peters | 6fc13d9 | 2002-07-02 18:12:35 +0000 | [diff] [blame] | 103 | |
| 104 | GC_TENTATIVELY_UNREACHABLE |
| 105 | move_unreachable() then moves objects not reachable (whether directly or |
| 106 | indirectly) from outside the generation into an "unreachable" set. |
| 107 | Objects that are found to be reachable have gc_refs set to GC_REACHABLE |
| 108 | again. Objects that are found to be unreachable have gc_refs set to |
| 109 | GC_TENTATIVELY_UNREACHABLE. It's "tentatively" because the pass doing |
| 110 | this can't be sure until it ends, and GC_TENTATIVELY_UNREACHABLE may |
| 111 | transition back to GC_REACHABLE. |
| 112 | |
| 113 | Only objects with GC_TENTATIVELY_UNREACHABLE still set are candidates |
| 114 | for collection. If it's decided not to collect such an object (e.g., |
| 115 | it has a __del__ method), its gc_refs is restored to GC_REACHABLE again. |
| 116 | ---------------------------------------------------------------------------- |
| 117 | */ |
Tim Peters | ea40563 | 2002-07-02 00:52:30 +0000 | [diff] [blame] | 118 | #define GC_UNTRACKED _PyGC_REFS_UNTRACKED |
| 119 | #define GC_REACHABLE _PyGC_REFS_REACHABLE |
| 120 | #define GC_TENTATIVELY_UNREACHABLE _PyGC_REFS_TENTATIVELY_UNREACHABLE |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 121 | |
Tim Peters | 6fc13d9 | 2002-07-02 18:12:35 +0000 | [diff] [blame] | 122 | #define IS_TRACKED(o) ((AS_GC(o))->gc.gc_refs != GC_UNTRACKED) |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 123 | #define IS_REACHABLE(o) ((AS_GC(o))->gc.gc_refs == GC_REACHABLE) |
| 124 | #define IS_TENTATIVELY_UNREACHABLE(o) ( \ |
| 125 | (AS_GC(o))->gc.gc_refs == GC_TENTATIVELY_UNREACHABLE) |
Neil Schemenauer | a2b11ec | 2002-05-21 15:53:24 +0000 | [diff] [blame] | 126 | |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 127 | /*** list functions ***/ |
| 128 | |
| 129 | static void |
| 130 | gc_list_init(PyGC_Head *list) |
| 131 | { |
Tim Peters | 9e4ca10 | 2001-10-11 18:31:31 +0000 | [diff] [blame] | 132 | list->gc.gc_prev = list; |
| 133 | list->gc.gc_next = list; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Neil Schemenauer | 2880ae5 | 2002-05-04 05:35:20 +0000 | [diff] [blame] | 136 | static int |
| 137 | gc_list_is_empty(PyGC_Head *list) |
| 138 | { |
| 139 | return (list->gc.gc_next == list); |
| 140 | } |
| 141 | |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 142 | static void |
| 143 | gc_list_append(PyGC_Head *node, PyGC_Head *list) |
| 144 | { |
Tim Peters | 9e4ca10 | 2001-10-11 18:31:31 +0000 | [diff] [blame] | 145 | node->gc.gc_next = list; |
| 146 | node->gc.gc_prev = list->gc.gc_prev; |
| 147 | node->gc.gc_prev->gc.gc_next = node; |
| 148 | list->gc.gc_prev = node; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | static void |
| 152 | gc_list_remove(PyGC_Head *node) |
| 153 | { |
Tim Peters | 9e4ca10 | 2001-10-11 18:31:31 +0000 | [diff] [blame] | 154 | node->gc.gc_prev->gc.gc_next = node->gc.gc_next; |
| 155 | node->gc.gc_next->gc.gc_prev = node->gc.gc_prev; |
| 156 | node->gc.gc_next = NULL; /* object is not currently tracked */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 159 | /* append a list onto another list, from becomes an empty list */ |
| 160 | static void |
| 161 | gc_list_merge(PyGC_Head *from, PyGC_Head *to) |
| 162 | { |
| 163 | PyGC_Head *tail; |
Neil Schemenauer | 2880ae5 | 2002-05-04 05:35:20 +0000 | [diff] [blame] | 164 | if (!gc_list_is_empty(from)) { |
Tim Peters | 9e4ca10 | 2001-10-11 18:31:31 +0000 | [diff] [blame] | 165 | tail = to->gc.gc_prev; |
| 166 | tail->gc.gc_next = from->gc.gc_next; |
| 167 | tail->gc.gc_next->gc.gc_prev = tail; |
| 168 | to->gc.gc_prev = from->gc.gc_prev; |
| 169 | to->gc.gc_prev->gc.gc_next = to; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 170 | } |
| 171 | gc_list_init(from); |
| 172 | } |
| 173 | |
| 174 | static long |
| 175 | gc_list_size(PyGC_Head *list) |
| 176 | { |
| 177 | PyGC_Head *gc; |
| 178 | long n = 0; |
Tim Peters | 9e4ca10 | 2001-10-11 18:31:31 +0000 | [diff] [blame] | 179 | for (gc = list->gc.gc_next; gc != list; gc = gc->gc.gc_next) { |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 180 | n++; |
| 181 | } |
| 182 | return n; |
| 183 | } |
| 184 | |
| 185 | /*** end of list stuff ***/ |
| 186 | |
| 187 | |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 188 | /* Set all gc_refs = ob_refcnt. After this, gc_refs is > 0 for all objects |
| 189 | * in containers, and is GC_REACHABLE for all tracked gc objects not in |
| 190 | * containers. |
Tim Peters | 8839617 | 2002-06-30 17:56:40 +0000 | [diff] [blame] | 191 | */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 192 | static void |
| 193 | update_refs(PyGC_Head *containers) |
| 194 | { |
Tim Peters | 9e4ca10 | 2001-10-11 18:31:31 +0000 | [diff] [blame] | 195 | PyGC_Head *gc = containers->gc.gc_next; |
Tim Peters | ea40563 | 2002-07-02 00:52:30 +0000 | [diff] [blame] | 196 | for (; gc != containers; gc = gc->gc.gc_next) { |
| 197 | assert(gc->gc.gc_refs == GC_REACHABLE); |
Tim Peters | 9e4ca10 | 2001-10-11 18:31:31 +0000 | [diff] [blame] | 198 | gc->gc.gc_refs = FROM_GC(gc)->ob_refcnt; |
Tim Peters | ea40563 | 2002-07-02 00:52:30 +0000 | [diff] [blame] | 199 | } |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 202 | /* A traversal callback for subtract_refs. */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 203 | static int |
| 204 | visit_decref(PyObject *op, void *data) |
| 205 | { |
Tim Peters | 93cd83e | 2002-06-30 21:31:03 +0000 | [diff] [blame] | 206 | assert(op != NULL); |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 207 | if (PyObject_IS_GC(op)) { |
| 208 | PyGC_Head *gc = AS_GC(op); |
| 209 | /* We're only interested in gc_refs for objects in the |
| 210 | * generation being collected, which can be recognized |
| 211 | * because only they have positive gc_refs. |
| 212 | */ |
Tim Peters | aab713b | 2002-07-02 22:15:28 +0000 | [diff] [blame] | 213 | assert(gc->gc.gc_refs != 0); /* else refcount was too small */ |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 214 | if (gc->gc.gc_refs > 0) |
| 215 | gc->gc.gc_refs--; |
| 216 | } |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 217 | return 0; |
| 218 | } |
| 219 | |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 220 | /* Subtract internal references from gc_refs. After this, gc_refs is >= 0 |
| 221 | * for all objects in containers, and is GC_REACHABLE for all tracked gc |
| 222 | * objects not in containers. The ones with gc_refs > 0 are directly |
| 223 | * reachable from outside containers, and so can't be collected. |
| 224 | */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 225 | static void |
| 226 | subtract_refs(PyGC_Head *containers) |
| 227 | { |
| 228 | traverseproc traverse; |
Tim Peters | 9e4ca10 | 2001-10-11 18:31:31 +0000 | [diff] [blame] | 229 | PyGC_Head *gc = containers->gc.gc_next; |
| 230 | for (; gc != containers; gc=gc->gc.gc_next) { |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 231 | traverse = FROM_GC(gc)->ob_type->tp_traverse; |
| 232 | (void) traverse(FROM_GC(gc), |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 233 | (visitproc)visit_decref, |
| 234 | NULL); |
| 235 | } |
| 236 | } |
| 237 | |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 238 | /* A traversal callback for move_unreachable. */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 239 | static int |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 240 | visit_reachable(PyObject *op, PyGC_Head *reachable) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 241 | { |
Tim Peters | ea40563 | 2002-07-02 00:52:30 +0000 | [diff] [blame] | 242 | if (PyObject_IS_GC(op)) { |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 243 | PyGC_Head *gc = AS_GC(op); |
| 244 | const int gc_refs = gc->gc.gc_refs; |
| 245 | |
| 246 | if (gc_refs == 0) { |
| 247 | /* This is in move_unreachable's 'young' list, but |
| 248 | * the traversal hasn't yet gotten to it. All |
| 249 | * we need to do is tell move_unreachable that it's |
| 250 | * reachable. |
| 251 | */ |
| 252 | gc->gc.gc_refs = 1; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 253 | } |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 254 | else if (gc_refs == GC_TENTATIVELY_UNREACHABLE) { |
| 255 | /* This had gc_refs = 0 when move_unreachable got |
| 256 | * to it, but turns out it's reachable after all. |
| 257 | * Move it back to move_unreachable's 'young' list, |
| 258 | * and move_unreachable will eventually get to it |
| 259 | * again. |
| 260 | */ |
| 261 | gc_list_remove(gc); |
| 262 | gc_list_append(gc, reachable); |
| 263 | gc->gc.gc_refs = 1; |
| 264 | } |
| 265 | /* Else there's nothing to do. |
| 266 | * If gc_refs > 0, it must be in move_unreachable's 'young' |
| 267 | * list, and move_unreachable will eventually get to it. |
| 268 | * If gc_refs == GC_REACHABLE, it's either in some other |
| 269 | * generation so we don't care about it, or move_unreachable |
Tim Peters | 6fc13d9 | 2002-07-02 18:12:35 +0000 | [diff] [blame] | 270 | * already dealt with it. |
Tim Peters | ea40563 | 2002-07-02 00:52:30 +0000 | [diff] [blame] | 271 | * If gc_refs == GC_UNTRACKED, it must be ignored. |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 272 | */ |
Tim Peters | ea40563 | 2002-07-02 00:52:30 +0000 | [diff] [blame] | 273 | else { |
| 274 | assert(gc_refs > 0 |
| 275 | || gc_refs == GC_REACHABLE |
| 276 | || gc_refs == GC_UNTRACKED); |
| 277 | } |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 278 | } |
| 279 | return 0; |
| 280 | } |
| 281 | |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 282 | /* Move the unreachable objects from young to unreachable. After this, |
| 283 | * all objects in young have gc_refs = GC_REACHABLE, and all objects in |
| 284 | * unreachable have gc_refs = GC_TENTATIVELY_UNREACHABLE. All tracked |
| 285 | * gc objects not in young or unreachable still have gc_refs = GC_REACHABLE. |
| 286 | * All objects in young after this are directly or indirectly reachable |
| 287 | * from outside the original young; and all objects in unreachable are |
| 288 | * not. |
Tim Peters | 8839617 | 2002-06-30 17:56:40 +0000 | [diff] [blame] | 289 | */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 290 | static void |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 291 | move_unreachable(PyGC_Head *young, PyGC_Head *unreachable) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 292 | { |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 293 | PyGC_Head *gc = young->gc.gc_next; |
| 294 | |
| 295 | /* Invariants: all objects "to the left" of us in young have gc_refs |
| 296 | * = GC_REACHABLE, and are indeed reachable (directly or indirectly) |
| 297 | * from outside the young list as it was at entry. All other objects |
| 298 | * from the original young "to the left" of us are in unreachable now, |
| 299 | * and have gc_refs = GC_TENTATIVELY_UNREACHABLE. All objects to the |
| 300 | * left of us in 'young' now have been scanned, and no objects here |
| 301 | * or to the right have been scanned yet. |
| 302 | */ |
| 303 | |
| 304 | while (gc != young) { |
| 305 | PyGC_Head *next; |
| 306 | |
Tim Peters | 6fc13d9 | 2002-07-02 18:12:35 +0000 | [diff] [blame] | 307 | if (gc->gc.gc_refs) { |
| 308 | /* gc is definitely reachable from outside the |
| 309 | * original 'young'. Mark it as such, and traverse |
| 310 | * its pointers to find any other objects that may |
| 311 | * be directly reachable from it. Note that the |
| 312 | * call to tp_traverse may append objects to young, |
| 313 | * so we have to wait until it returns to determine |
| 314 | * the next object to visit. |
| 315 | */ |
| 316 | PyObject *op = FROM_GC(gc); |
| 317 | traverseproc traverse = op->ob_type->tp_traverse; |
| 318 | assert(gc->gc.gc_refs > 0); |
| 319 | gc->gc.gc_refs = GC_REACHABLE; |
| 320 | (void) traverse(op, |
| 321 | (visitproc)visit_reachable, |
| 322 | (void *)young); |
| 323 | next = gc->gc.gc_next; |
| 324 | } |
| 325 | else { |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 326 | /* This *may* be unreachable. To make progress, |
| 327 | * assume it is. gc isn't directly reachable from |
| 328 | * any object we've already traversed, but may be |
| 329 | * reachable from an object we haven't gotten to yet. |
| 330 | * visit_reachable will eventually move gc back into |
| 331 | * young if that's so, and we'll see it again. |
| 332 | */ |
| 333 | next = gc->gc.gc_next; |
| 334 | gc_list_remove(gc); |
| 335 | gc_list_append(gc, unreachable); |
| 336 | gc->gc.gc_refs = GC_TENTATIVELY_UNREACHABLE; |
| 337 | } |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 338 | gc = next; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | |
Tim Peters | 86b993b | 2003-04-05 17:35:54 +0000 | [diff] [blame] | 342 | /* Return true if object has a finalization method. |
| 343 | * CAUTION: An instance of an old-style class has to be checked for a |
| 344 | *__del__ method, and that can cause arbitrary Python code to get executed |
| 345 | * via the class's __getattr__ hook (if any). This function can therefore |
| 346 | * mutate the object graph, and that's been the source of subtle bugs. |
| 347 | */ |
Neil Schemenauer | a765c12 | 2001-11-01 17:35:23 +0000 | [diff] [blame] | 348 | static int |
| 349 | has_finalizer(PyObject *op) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 350 | { |
Tim Peters | 86b993b | 2003-04-05 17:35:54 +0000 | [diff] [blame] | 351 | if (PyInstance_Check(op)) { |
| 352 | /* This is the dangerous path: hasattr can invoke |
| 353 | * the class __getattr__(), and that can do anything. |
| 354 | */ |
| 355 | assert(delstr != NULL); |
| 356 | return PyObject_HasAttr(op, delstr); |
| 357 | } |
| 358 | else if (PyType_HasFeature(op->ob_type, Py_TPFLAGS_HEAPTYPE)) |
| 359 | return op->ob_type->tp_del != NULL; |
| 360 | else |
| 361 | return 0; |
Neil Schemenauer | a765c12 | 2001-11-01 17:35:23 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Tim Peters | f6ae7a4 | 2003-04-05 18:40:50 +0000 | [diff] [blame^] | 364 | /* Move all objects out of unreachable, into collectable or finalizers. |
| 365 | * It's possible that some objects will get collected (via refcount falling |
| 366 | * to 0), or resurrected, as a side effect of checking for __del__ methods. |
| 367 | * After, finalizers contains all the objects from unreachable that haven't |
| 368 | * been collected by magic, and that have a finalizer. gc_refs is |
| 369 | * GC_REACHABLE for all of those. collectable contains all the remaining |
| 370 | * objects from unreachable, and gc_refs remains GC_TENTATIVELY_UNREACHABLE |
| 371 | * for those (we're still not sure they're reclaimable after this! Some |
| 372 | * may yet by reachable *from* the objects in finalizers). |
Jeremy Hylton | ce136e9 | 2003-04-04 19:59:06 +0000 | [diff] [blame] | 373 | */ |
Neil Schemenauer | a765c12 | 2001-11-01 17:35:23 +0000 | [diff] [blame] | 374 | static void |
Jeremy Hylton | ce136e9 | 2003-04-04 19:59:06 +0000 | [diff] [blame] | 375 | move_finalizers(PyGC_Head *unreachable, PyGC_Head *collectable, |
| 376 | PyGC_Head *finalizers) |
Neil Schemenauer | a765c12 | 2001-11-01 17:35:23 +0000 | [diff] [blame] | 377 | { |
Jeremy Hylton | ce136e9 | 2003-04-04 19:59:06 +0000 | [diff] [blame] | 378 | while (!gc_list_is_empty(unreachable)) { |
| 379 | PyGC_Head *gc = unreachable->gc.gc_next; |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 380 | PyObject *op = FROM_GC(gc); |
Jeremy Hylton | ce136e9 | 2003-04-04 19:59:06 +0000 | [diff] [blame] | 381 | int finalizer; |
| 382 | |
Tim Peters | f6ae7a4 | 2003-04-05 18:40:50 +0000 | [diff] [blame^] | 383 | assert(IS_TENTATIVELY_UNREACHABLE(op)); |
| 384 | |
| 385 | finalizer = has_finalizer(op); |
| 386 | if (unreachable->gc.gc_next == gc) { |
| 387 | gc_list_remove(gc); |
| 388 | if (finalizer) { |
| 389 | gc_list_append(gc, finalizers); |
| 390 | gc->gc.gc_refs = GC_REACHABLE; |
Jeremy Hylton | ce136e9 | 2003-04-04 19:59:06 +0000 | [diff] [blame] | 391 | } |
Tim Peters | f6ae7a4 | 2003-04-05 18:40:50 +0000 | [diff] [blame^] | 392 | else |
| 393 | gc_list_append(gc, collectable); |
Jeremy Hylton | ce136e9 | 2003-04-04 19:59:06 +0000 | [diff] [blame] | 394 | } |
Tim Peters | f6ae7a4 | 2003-04-05 18:40:50 +0000 | [diff] [blame^] | 395 | /* else has_finalizer() deleted op via side effect */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 396 | } |
| 397 | } |
| 398 | |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 399 | /* A traversal callback for move_finalizer_reachable. */ |
| 400 | static int |
| 401 | visit_move(PyObject *op, PyGC_Head *tolist) |
| 402 | { |
| 403 | if (PyObject_IS_GC(op)) { |
Tim Peters | ea40563 | 2002-07-02 00:52:30 +0000 | [diff] [blame] | 404 | if (IS_TENTATIVELY_UNREACHABLE(op)) { |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 405 | PyGC_Head *gc = AS_GC(op); |
| 406 | gc_list_remove(gc); |
| 407 | gc_list_append(gc, tolist); |
| 408 | gc->gc.gc_refs = GC_REACHABLE; |
| 409 | } |
| 410 | } |
| 411 | return 0; |
| 412 | } |
| 413 | |
| 414 | /* Move objects that are reachable from finalizers, from the unreachable set |
| 415 | * into the finalizers set. |
| 416 | */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 417 | static void |
| 418 | move_finalizer_reachable(PyGC_Head *finalizers) |
| 419 | { |
| 420 | traverseproc traverse; |
Tim Peters | 9e4ca10 | 2001-10-11 18:31:31 +0000 | [diff] [blame] | 421 | PyGC_Head *gc = finalizers->gc.gc_next; |
| 422 | for (; gc != finalizers; gc=gc->gc.gc_next) { |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 423 | /* careful, finalizers list is growing here */ |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 424 | traverse = FROM_GC(gc)->ob_type->tp_traverse; |
Tim Peters | 8839617 | 2002-06-30 17:56:40 +0000 | [diff] [blame] | 425 | (void) traverse(FROM_GC(gc), |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 426 | (visitproc)visit_move, |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 427 | (void *)finalizers); |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | static void |
Jeremy Hylton | 0625777 | 2000-08-31 15:10:24 +0000 | [diff] [blame] | 432 | debug_instance(char *msg, PyInstanceObject *inst) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 433 | { |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 434 | char *cname; |
Neil Schemenauer | a765c12 | 2001-11-01 17:35:23 +0000 | [diff] [blame] | 435 | /* simple version of instance_repr */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 436 | PyObject *classname = inst->in_class->cl_name; |
| 437 | if (classname != NULL && PyString_Check(classname)) |
| 438 | cname = PyString_AsString(classname); |
| 439 | else |
| 440 | cname = "?"; |
Jeremy Hylton | 0625777 | 2000-08-31 15:10:24 +0000 | [diff] [blame] | 441 | PySys_WriteStderr("gc: %.100s <%.100s instance at %p>\n", |
| 442 | msg, cname, inst); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | static void |
Jeremy Hylton | 0625777 | 2000-08-31 15:10:24 +0000 | [diff] [blame] | 446 | debug_cycle(char *msg, PyObject *op) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 447 | { |
| 448 | if ((debug & DEBUG_INSTANCES) && PyInstance_Check(op)) { |
Jeremy Hylton | 0625777 | 2000-08-31 15:10:24 +0000 | [diff] [blame] | 449 | debug_instance(msg, (PyInstanceObject *)op); |
Neil Schemenauer | 544de1e | 2000-09-22 15:22:38 +0000 | [diff] [blame] | 450 | } |
| 451 | else if (debug & DEBUG_OBJECTS) { |
Jeremy Hylton | 0625777 | 2000-08-31 15:10:24 +0000 | [diff] [blame] | 452 | PySys_WriteStderr("gc: %.100s <%.100s %p>\n", |
| 453 | msg, op->ob_type->tp_name, op); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 454 | } |
| 455 | } |
| 456 | |
| 457 | /* Handle uncollectable garbage (cycles with finalizers). */ |
| 458 | static void |
| 459 | handle_finalizers(PyGC_Head *finalizers, PyGC_Head *old) |
| 460 | { |
| 461 | PyGC_Head *gc; |
| 462 | if (garbage == NULL) { |
| 463 | garbage = PyList_New(0); |
| 464 | } |
Tim Peters | 9e4ca10 | 2001-10-11 18:31:31 +0000 | [diff] [blame] | 465 | for (gc = finalizers->gc.gc_next; gc != finalizers; |
| 466 | gc = finalizers->gc.gc_next) { |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 467 | PyObject *op = FROM_GC(gc); |
Jeremy Hylton | ce136e9 | 2003-04-04 19:59:06 +0000 | [diff] [blame] | 468 | /* XXX has_finalizer() is not safe here. */ |
Neil Schemenauer | a765c12 | 2001-11-01 17:35:23 +0000 | [diff] [blame] | 469 | if ((debug & DEBUG_SAVEALL) || has_finalizer(op)) { |
| 470 | /* If SAVEALL is not set then just append objects with |
| 471 | * finalizers to the list of garbage. All objects in |
| 472 | * the finalizers list are reachable from those |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 473 | * objects. |
| 474 | */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 475 | PyList_Append(garbage, op); |
| 476 | } |
Tim Peters | 8839617 | 2002-06-30 17:56:40 +0000 | [diff] [blame] | 477 | /* object is now reachable again */ |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 478 | assert(IS_REACHABLE(op)); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 479 | gc_list_remove(gc); |
| 480 | gc_list_append(gc, old); |
| 481 | } |
| 482 | } |
| 483 | |
Neil Schemenauer | 544de1e | 2000-09-22 15:22:38 +0000 | [diff] [blame] | 484 | /* Break reference cycles by clearing the containers involved. This is |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 485 | * 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] | 486 | * objects may be freed. It is possible I screwed something up here. |
| 487 | */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 488 | static void |
Jeremy Hylton | ce136e9 | 2003-04-04 19:59:06 +0000 | [diff] [blame] | 489 | delete_garbage(PyGC_Head *collectable, PyGC_Head *old) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 490 | { |
| 491 | inquiry clear; |
| 492 | |
Jeremy Hylton | ce136e9 | 2003-04-04 19:59:06 +0000 | [diff] [blame] | 493 | while (!gc_list_is_empty(collectable)) { |
| 494 | PyGC_Head *gc = collectable->gc.gc_next; |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 495 | PyObject *op = FROM_GC(gc); |
Tim Peters | 8839617 | 2002-06-30 17:56:40 +0000 | [diff] [blame] | 496 | |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 497 | assert(IS_TENTATIVELY_UNREACHABLE(op)); |
Neil Schemenauer | 544de1e | 2000-09-22 15:22:38 +0000 | [diff] [blame] | 498 | if (debug & DEBUG_SAVEALL) { |
| 499 | PyList_Append(garbage, op); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 500 | } |
Neil Schemenauer | 544de1e | 2000-09-22 15:22:38 +0000 | [diff] [blame] | 501 | else { |
| 502 | if ((clear = op->ob_type->tp_clear) != NULL) { |
| 503 | Py_INCREF(op); |
Jeremy Hylton | 8a13518 | 2002-06-06 23:23:55 +0000 | [diff] [blame] | 504 | clear(op); |
Neil Schemenauer | 544de1e | 2000-09-22 15:22:38 +0000 | [diff] [blame] | 505 | Py_DECREF(op); |
| 506 | } |
| 507 | } |
Jeremy Hylton | ce136e9 | 2003-04-04 19:59:06 +0000 | [diff] [blame] | 508 | if (collectable->gc.gc_next == gc) { |
Neil Schemenauer | 544de1e | 2000-09-22 15:22:38 +0000 | [diff] [blame] | 509 | /* object is still alive, move it, it may die later */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 510 | gc_list_remove(gc); |
| 511 | gc_list_append(gc, old); |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 512 | gc->gc.gc_refs = GC_REACHABLE; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 513 | } |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | /* This is the main function. Read this to understand how the |
| 518 | * collection process works. */ |
| 519 | static long |
Neil Schemenauer | 2880ae5 | 2002-05-04 05:35:20 +0000 | [diff] [blame] | 520 | collect(int generation) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 521 | { |
Neil Schemenauer | 2880ae5 | 2002-05-04 05:35:20 +0000 | [diff] [blame] | 522 | int i; |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 523 | long m = 0; /* # objects collected */ |
| 524 | long n = 0; /* # unreachable objects that couldn't be collected */ |
Neil Schemenauer | 2880ae5 | 2002-05-04 05:35:20 +0000 | [diff] [blame] | 525 | PyGC_Head *young; /* the generation we are examining */ |
| 526 | PyGC_Head *old; /* next older generation */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 527 | PyGC_Head unreachable; |
Jeremy Hylton | ce136e9 | 2003-04-04 19:59:06 +0000 | [diff] [blame] | 528 | PyGC_Head collectable; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 529 | PyGC_Head finalizers; |
| 530 | PyGC_Head *gc; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 531 | |
Tim Peters | 93ad66d | 2003-04-05 17:15:44 +0000 | [diff] [blame] | 532 | if (delstr == NULL) { |
| 533 | delstr = PyString_InternFromString("__del__"); |
| 534 | if (delstr == NULL) |
| 535 | Py_FatalError("gc couldn't allocate \"__del__\""); |
| 536 | } |
| 537 | |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 538 | if (debug & DEBUG_STATS) { |
Neil Schemenauer | 2880ae5 | 2002-05-04 05:35:20 +0000 | [diff] [blame] | 539 | PySys_WriteStderr("gc: collecting generation %d...\n", |
| 540 | generation); |
| 541 | PySys_WriteStderr("gc: objects in each generation:"); |
| 542 | for (i = 0; i < NUM_GENERATIONS; i++) { |
| 543 | PySys_WriteStderr(" %ld", gc_list_size(GEN_HEAD(i))); |
| 544 | } |
| 545 | PySys_WriteStderr("\n"); |
| 546 | } |
| 547 | |
| 548 | /* update collection and allocation counters */ |
| 549 | if (generation+1 < NUM_GENERATIONS) |
| 550 | generations[generation+1].count += 1; |
| 551 | for (i = 0; i <= generation; i++) |
Neil Schemenauer | c905164 | 2002-06-28 19:16:04 +0000 | [diff] [blame] | 552 | generations[i].count = 0; |
Neil Schemenauer | 2880ae5 | 2002-05-04 05:35:20 +0000 | [diff] [blame] | 553 | |
| 554 | /* merge younger generations with one we are currently collecting */ |
| 555 | for (i = 0; i < generation; i++) { |
| 556 | gc_list_merge(GEN_HEAD(i), GEN_HEAD(generation)); |
| 557 | } |
| 558 | |
| 559 | /* handy references */ |
| 560 | young = GEN_HEAD(generation); |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 561 | if (generation < NUM_GENERATIONS-1) |
Neil Schemenauer | 2880ae5 | 2002-05-04 05:35:20 +0000 | [diff] [blame] | 562 | old = GEN_HEAD(generation+1); |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 563 | else |
| 564 | old = young; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 565 | |
| 566 | /* Using ob_refcnt and gc_refs, calculate which objects in the |
| 567 | * container set are reachable from outside the set (ie. have a |
| 568 | * refcount greater than 0 when all the references within the |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 569 | * set are taken into account |
| 570 | */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 571 | update_refs(young); |
| 572 | subtract_refs(young); |
| 573 | |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 574 | /* Leave everything reachable from outside young in young, and move |
| 575 | * everything else (in young) to unreachable. |
| 576 | * NOTE: This used to move the reachable objects into a reachable |
| 577 | * set instead. But most things usually turn out to be reachable, |
| 578 | * so it's more efficient to move the unreachable things. |
| 579 | */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 580 | gc_list_init(&unreachable); |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 581 | move_unreachable(young, &unreachable); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 582 | |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 583 | /* Move reachable objects to next generation. */ |
| 584 | if (young != old) |
| 585 | gc_list_merge(young, old); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 586 | |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 587 | /* All objects in unreachable are trash, but objects reachable from |
| 588 | * finalizers can't safely be deleted. Python programmers should take |
| 589 | * care not to create such things. For Python, finalizers means |
| 590 | * instance objects with __del__ methods. |
Tim Peters | 93ad66d | 2003-04-05 17:15:44 +0000 | [diff] [blame] | 591 | * |
Jeremy Hylton | ce136e9 | 2003-04-04 19:59:06 +0000 | [diff] [blame] | 592 | * Move each object into the collectable set or the finalizers set. |
Tim Peters | 86b993b | 2003-04-05 17:35:54 +0000 | [diff] [blame] | 593 | * Because we need to check for __del__ methods on instances of |
| 594 | * classic classes, arbitrary Python code may get executed by |
| 595 | * getattr hooks: that may resurrect or deallocate (via refcount |
| 596 | * falling to 0) unreachable objects, so this is very delicate. |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 597 | */ |
Jeremy Hylton | ce136e9 | 2003-04-04 19:59:06 +0000 | [diff] [blame] | 598 | gc_list_init(&collectable); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 599 | gc_list_init(&finalizers); |
Jeremy Hylton | ce136e9 | 2003-04-04 19:59:06 +0000 | [diff] [blame] | 600 | move_finalizers(&unreachable, &collectable, &finalizers); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 601 | move_finalizer_reachable(&finalizers); |
| 602 | |
| 603 | /* Collect statistics on collectable objects found and print |
| 604 | * debugging information. */ |
Jeremy Hylton | ce136e9 | 2003-04-04 19:59:06 +0000 | [diff] [blame] | 605 | for (gc = collectable.gc.gc_next; gc != &collectable; |
Tim Peters | 9e4ca10 | 2001-10-11 18:31:31 +0000 | [diff] [blame] | 606 | gc = gc->gc.gc_next) { |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 607 | m++; |
Jeremy Hylton | 0625777 | 2000-08-31 15:10:24 +0000 | [diff] [blame] | 608 | if (debug & DEBUG_COLLECTABLE) { |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 609 | debug_cycle("collectable", FROM_GC(gc)); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 610 | } |
| 611 | } |
Tim Peters | 19b74c7 | 2002-07-01 03:52:19 +0000 | [diff] [blame] | 612 | /* Call tp_clear on objects in the collectable set. This will cause |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 613 | * the reference cycles to be broken. It may also cause some objects in |
| 614 | * finalizers to be freed */ |
Jeremy Hylton | ce136e9 | 2003-04-04 19:59:06 +0000 | [diff] [blame] | 615 | delete_garbage(&collectable, old); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 616 | |
| 617 | /* Collect statistics on uncollectable objects found and print |
| 618 | * debugging information. */ |
Tim Peters | 9e4ca10 | 2001-10-11 18:31:31 +0000 | [diff] [blame] | 619 | for (gc = finalizers.gc.gc_next; gc != &finalizers; |
| 620 | gc = gc->gc.gc_next) { |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 621 | n++; |
Jeremy Hylton | 0625777 | 2000-08-31 15:10:24 +0000 | [diff] [blame] | 622 | if (debug & DEBUG_UNCOLLECTABLE) { |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 623 | debug_cycle("uncollectable", FROM_GC(gc)); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 624 | } |
| 625 | } |
Jeremy Hylton | 0625777 | 2000-08-31 15:10:24 +0000 | [diff] [blame] | 626 | if (debug & DEBUG_STATS) { |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 627 | if (m == 0 && n == 0) { |
Jeremy Hylton | 0625777 | 2000-08-31 15:10:24 +0000 | [diff] [blame] | 628 | PySys_WriteStderr("gc: done.\n"); |
Neil Schemenauer | 544de1e | 2000-09-22 15:22:38 +0000 | [diff] [blame] | 629 | } |
| 630 | else { |
Jeremy Hylton | 0625777 | 2000-08-31 15:10:24 +0000 | [diff] [blame] | 631 | PySys_WriteStderr( |
| 632 | "gc: done, %ld unreachable, %ld uncollectable.\n", |
| 633 | n+m, n); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 634 | } |
| 635 | } |
| 636 | |
| 637 | /* Append instances in the uncollectable set to a Python |
| 638 | * reachable list of garbage. The programmer has to deal with |
| 639 | * this if they insist on creating this type of structure. */ |
| 640 | handle_finalizers(&finalizers, old); |
| 641 | |
Jeremy Hylton | b709df3 | 2000-09-01 02:47:25 +0000 | [diff] [blame] | 642 | if (PyErr_Occurred()) { |
Neil Schemenauer | 544de1e | 2000-09-22 15:22:38 +0000 | [diff] [blame] | 643 | if (gc_str == NULL) { |
| 644 | gc_str = PyString_FromString("garbage collection"); |
| 645 | } |
Jeremy Hylton | b709df3 | 2000-09-01 02:47:25 +0000 | [diff] [blame] | 646 | PyErr_WriteUnraisable(gc_str); |
| 647 | Py_FatalError("unexpected exception during garbage collection"); |
| 648 | } |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 649 | return n+m; |
| 650 | } |
| 651 | |
| 652 | static long |
| 653 | collect_generations(void) |
| 654 | { |
Neil Schemenauer | 2880ae5 | 2002-05-04 05:35:20 +0000 | [diff] [blame] | 655 | int i; |
Vladimir Marangozov | b16714b | 2000-07-10 05:37:39 +0000 | [diff] [blame] | 656 | long n = 0; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 657 | |
Neil Schemenauer | 2880ae5 | 2002-05-04 05:35:20 +0000 | [diff] [blame] | 658 | /* Find the oldest generation (higest numbered) where the count |
| 659 | * exceeds the threshold. Objects in the that generation and |
| 660 | * generations younger than it will be collected. */ |
| 661 | for (i = NUM_GENERATIONS-1; i >= 0; i--) { |
| 662 | if (generations[i].count > generations[i].threshold) { |
| 663 | n = collect(i); |
| 664 | break; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 665 | } |
| 666 | } |
| 667 | return n; |
| 668 | } |
| 669 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 670 | PyDoc_STRVAR(gc_enable__doc__, |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 671 | "enable() -> None\n" |
| 672 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 673 | "Enable automatic garbage collection.\n"); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 674 | |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 675 | static PyObject * |
| 676 | gc_enable(PyObject *self, PyObject *args) |
| 677 | { |
| 678 | |
| 679 | if (!PyArg_ParseTuple(args, ":enable")) /* check no args */ |
| 680 | return NULL; |
| 681 | |
| 682 | enabled = 1; |
| 683 | |
| 684 | Py_INCREF(Py_None); |
| 685 | return Py_None; |
| 686 | } |
| 687 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 688 | PyDoc_STRVAR(gc_disable__doc__, |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 689 | "disable() -> None\n" |
| 690 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 691 | "Disable automatic garbage collection.\n"); |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 692 | |
| 693 | static PyObject * |
| 694 | gc_disable(PyObject *self, PyObject *args) |
| 695 | { |
| 696 | |
| 697 | if (!PyArg_ParseTuple(args, ":disable")) /* check no args */ |
| 698 | return NULL; |
| 699 | |
| 700 | enabled = 0; |
| 701 | |
| 702 | Py_INCREF(Py_None); |
| 703 | return Py_None; |
| 704 | } |
| 705 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 706 | PyDoc_STRVAR(gc_isenabled__doc__, |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 707 | "isenabled() -> status\n" |
| 708 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 709 | "Returns true if automatic garbage collection is enabled.\n"); |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 710 | |
| 711 | static PyObject * |
| 712 | gc_isenabled(PyObject *self, PyObject *args) |
| 713 | { |
| 714 | |
| 715 | if (!PyArg_ParseTuple(args, ":isenabled")) /* check no args */ |
| 716 | return NULL; |
| 717 | |
| 718 | return Py_BuildValue("i", enabled); |
| 719 | } |
| 720 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 721 | PyDoc_STRVAR(gc_collect__doc__, |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 722 | "collect() -> n\n" |
| 723 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 724 | "Run a full collection. The number of unreachable objects is returned.\n"); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 725 | |
| 726 | static PyObject * |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 727 | gc_collect(PyObject *self, PyObject *args) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 728 | { |
| 729 | long n; |
| 730 | |
Fred Drake | cc1be24 | 2000-07-12 04:42:23 +0000 | [diff] [blame] | 731 | if (!PyArg_ParseTuple(args, ":collect")) /* check no args */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 732 | return NULL; |
| 733 | |
Neil Schemenauer | e8c40cb | 2001-10-31 23:09:35 +0000 | [diff] [blame] | 734 | if (collecting) { |
| 735 | n = 0; /* already collecting, don't do anything */ |
| 736 | } |
| 737 | else { |
| 738 | collecting = 1; |
Neil Schemenauer | 2880ae5 | 2002-05-04 05:35:20 +0000 | [diff] [blame] | 739 | n = collect(NUM_GENERATIONS - 1); |
Neil Schemenauer | e8c40cb | 2001-10-31 23:09:35 +0000 | [diff] [blame] | 740 | collecting = 0; |
| 741 | } |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 742 | |
Neil Schemenauer | 7760cff | 2000-09-22 22:35:36 +0000 | [diff] [blame] | 743 | return Py_BuildValue("l", n); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 744 | } |
| 745 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 746 | PyDoc_STRVAR(gc_set_debug__doc__, |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 747 | "set_debug(flags) -> None\n" |
| 748 | "\n" |
| 749 | "Set the garbage collection debugging flags. Debugging information is\n" |
| 750 | "written to sys.stderr.\n" |
| 751 | "\n" |
| 752 | "flags is an integer and can have the following bits turned on:\n" |
| 753 | "\n" |
| 754 | " DEBUG_STATS - Print statistics during collection.\n" |
| 755 | " DEBUG_COLLECTABLE - Print collectable objects found.\n" |
| 756 | " DEBUG_UNCOLLECTABLE - Print unreachable but uncollectable objects found.\n" |
| 757 | " DEBUG_INSTANCES - Print instance objects.\n" |
| 758 | " DEBUG_OBJECTS - Print objects other than instances.\n" |
Neil Schemenauer | 544de1e | 2000-09-22 15:22:38 +0000 | [diff] [blame] | 759 | " 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] | 760 | " DEBUG_LEAK - Debug leaking programs (everything but STATS).\n"); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 761 | |
| 762 | static PyObject * |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 763 | gc_set_debug(PyObject *self, PyObject *args) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 764 | { |
Neil Schemenauer | 7760cff | 2000-09-22 22:35:36 +0000 | [diff] [blame] | 765 | if (!PyArg_ParseTuple(args, "i:set_debug", &debug)) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 766 | return NULL; |
| 767 | |
| 768 | Py_INCREF(Py_None); |
| 769 | return Py_None; |
| 770 | } |
| 771 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 772 | PyDoc_STRVAR(gc_get_debug__doc__, |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 773 | "get_debug() -> flags\n" |
| 774 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 775 | "Get the garbage collection debugging flags.\n"); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 776 | |
| 777 | static PyObject * |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 778 | gc_get_debug(PyObject *self, PyObject *args) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 779 | { |
Fred Drake | cc1be24 | 2000-07-12 04:42:23 +0000 | [diff] [blame] | 780 | if (!PyArg_ParseTuple(args, ":get_debug")) /* no args */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 781 | return NULL; |
| 782 | |
| 783 | return Py_BuildValue("i", debug); |
| 784 | } |
| 785 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 786 | PyDoc_STRVAR(gc_set_thresh__doc__, |
Neal Norwitz | 2a47c0f | 2002-01-29 00:53:41 +0000 | [diff] [blame] | 787 | "set_threshold(threshold0, [threshold1, threshold2]) -> None\n" |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 788 | "\n" |
| 789 | "Sets the collection thresholds. Setting threshold0 to zero disables\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 790 | "collection.\n"); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 791 | |
| 792 | static PyObject * |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 793 | gc_set_thresh(PyObject *self, PyObject *args) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 794 | { |
Neil Schemenauer | 2880ae5 | 2002-05-04 05:35:20 +0000 | [diff] [blame] | 795 | int i; |
| 796 | if (!PyArg_ParseTuple(args, "i|ii:set_threshold", |
| 797 | &generations[0].threshold, |
| 798 | &generations[1].threshold, |
| 799 | &generations[2].threshold)) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 800 | return NULL; |
Neil Schemenauer | 2880ae5 | 2002-05-04 05:35:20 +0000 | [diff] [blame] | 801 | for (i = 2; i < NUM_GENERATIONS; i++) { |
| 802 | /* generations higher than 2 get the same threshold */ |
| 803 | generations[i].threshold = generations[2].threshold; |
| 804 | } |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 805 | |
| 806 | Py_INCREF(Py_None); |
| 807 | return Py_None; |
| 808 | } |
| 809 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 810 | PyDoc_STRVAR(gc_get_thresh__doc__, |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 811 | "get_threshold() -> (threshold0, threshold1, threshold2)\n" |
| 812 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 813 | "Return the current collection thresholds\n"); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 814 | |
| 815 | static PyObject * |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 816 | gc_get_thresh(PyObject *self, PyObject *args) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 817 | { |
Fred Drake | cc1be24 | 2000-07-12 04:42:23 +0000 | [diff] [blame] | 818 | if (!PyArg_ParseTuple(args, ":get_threshold")) /* no args */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 819 | return NULL; |
| 820 | |
Neil Schemenauer | 2880ae5 | 2002-05-04 05:35:20 +0000 | [diff] [blame] | 821 | return Py_BuildValue("(iii)", |
| 822 | generations[0].threshold, |
| 823 | generations[1].threshold, |
| 824 | generations[2].threshold); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 825 | } |
| 826 | |
Neil Schemenauer | 48c7034 | 2001-08-09 15:38:31 +0000 | [diff] [blame] | 827 | static int |
Martin v. Löwis | 560da62 | 2001-11-24 09:24:51 +0000 | [diff] [blame] | 828 | referrersvisit(PyObject* obj, PyObject *objs) |
Neil Schemenauer | 48c7034 | 2001-08-09 15:38:31 +0000 | [diff] [blame] | 829 | { |
Martin v. Löwis | c8fe77b | 2001-11-29 18:08:31 +0000 | [diff] [blame] | 830 | int i; |
| 831 | for (i = 0; i < PyTuple_GET_SIZE(objs); i++) |
| 832 | if (PyTuple_GET_ITEM(objs, i) == obj) |
| 833 | return 1; |
Neil Schemenauer | 48c7034 | 2001-08-09 15:38:31 +0000 | [diff] [blame] | 834 | return 0; |
| 835 | } |
| 836 | |
Neil Schemenauer | 17e7be6 | 2001-08-10 14:46:47 +0000 | [diff] [blame] | 837 | static int |
Martin v. Löwis | 560da62 | 2001-11-24 09:24:51 +0000 | [diff] [blame] | 838 | gc_referrers_for(PyObject *objs, PyGC_Head *list, PyObject *resultlist) |
Neil Schemenauer | 48c7034 | 2001-08-09 15:38:31 +0000 | [diff] [blame] | 839 | { |
| 840 | PyGC_Head *gc; |
| 841 | PyObject *obj; |
| 842 | traverseproc traverse; |
Tim Peters | 9e4ca10 | 2001-10-11 18:31:31 +0000 | [diff] [blame] | 843 | for (gc = list->gc.gc_next; gc != list; gc = gc->gc.gc_next) { |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 844 | obj = FROM_GC(gc); |
Neil Schemenauer | 48c7034 | 2001-08-09 15:38:31 +0000 | [diff] [blame] | 845 | traverse = obj->ob_type->tp_traverse; |
| 846 | if (obj == objs || obj == resultlist) |
| 847 | continue; |
Martin v. Löwis | 560da62 | 2001-11-24 09:24:51 +0000 | [diff] [blame] | 848 | if (traverse(obj, (visitproc)referrersvisit, objs)) { |
Neil Schemenauer | 17e7be6 | 2001-08-10 14:46:47 +0000 | [diff] [blame] | 849 | if (PyList_Append(resultlist, obj) < 0) |
| 850 | return 0; /* error */ |
Neil Schemenauer | 48c7034 | 2001-08-09 15:38:31 +0000 | [diff] [blame] | 851 | } |
| 852 | } |
Neil Schemenauer | 17e7be6 | 2001-08-10 14:46:47 +0000 | [diff] [blame] | 853 | return 1; /* no error */ |
Neil Schemenauer | 48c7034 | 2001-08-09 15:38:31 +0000 | [diff] [blame] | 854 | } |
| 855 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 856 | PyDoc_STRVAR(gc_get_referrers__doc__, |
Martin v. Löwis | 560da62 | 2001-11-24 09:24:51 +0000 | [diff] [blame] | 857 | "get_referrers(*objs) -> list\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 858 | Return the list of objects that directly refer to any of objs."); |
Neil Schemenauer | 48c7034 | 2001-08-09 15:38:31 +0000 | [diff] [blame] | 859 | |
Neil Schemenauer | 17e7be6 | 2001-08-10 14:46:47 +0000 | [diff] [blame] | 860 | static PyObject * |
Martin v. Löwis | 560da62 | 2001-11-24 09:24:51 +0000 | [diff] [blame] | 861 | gc_get_referrers(PyObject *self, PyObject *args) |
Neil Schemenauer | 48c7034 | 2001-08-09 15:38:31 +0000 | [diff] [blame] | 862 | { |
Neil Schemenauer | 2880ae5 | 2002-05-04 05:35:20 +0000 | [diff] [blame] | 863 | int i; |
Neil Schemenauer | 48c7034 | 2001-08-09 15:38:31 +0000 | [diff] [blame] | 864 | PyObject *result = PyList_New(0); |
Neil Schemenauer | 2880ae5 | 2002-05-04 05:35:20 +0000 | [diff] [blame] | 865 | for (i = 0; i < NUM_GENERATIONS; i++) { |
| 866 | if (!(gc_referrers_for(args, GEN_HEAD(i), result))) { |
| 867 | Py_DECREF(result); |
| 868 | return NULL; |
| 869 | } |
Neil Schemenauer | 17e7be6 | 2001-08-10 14:46:47 +0000 | [diff] [blame] | 870 | } |
Neil Schemenauer | 48c7034 | 2001-08-09 15:38:31 +0000 | [diff] [blame] | 871 | return result; |
| 872 | } |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 873 | |
Jeremy Hylton | 5bd378b | 2003-04-03 16:28:38 +0000 | [diff] [blame] | 874 | static int |
| 875 | referrentsvisit(PyObject *obj, PyObject *list) |
| 876 | { |
| 877 | if (PyList_Append(list, obj) < 0) |
| 878 | return 1; |
| 879 | return 0; |
| 880 | } |
| 881 | |
| 882 | PyDoc_STRVAR(gc_get_referrents__doc__, |
| 883 | "get_referrents(*objs) -> list\n\ |
Jeremy Hylton | 059b094 | 2003-04-03 16:29:13 +0000 | [diff] [blame] | 884 | Return the list of objects that are directly referred to by objs."); |
Jeremy Hylton | 5bd378b | 2003-04-03 16:28:38 +0000 | [diff] [blame] | 885 | |
| 886 | static PyObject * |
| 887 | gc_get_referrents(PyObject *self, PyObject *args) |
| 888 | { |
| 889 | int i; |
| 890 | PyObject *result = PyList_New(0); |
| 891 | for (i = 0; i < PyTuple_GET_SIZE(args); i++) { |
Tim Peters | 93ad66d | 2003-04-05 17:15:44 +0000 | [diff] [blame] | 892 | PyObject *obj = PyTuple_GET_ITEM(args, i); |
Jeremy Hylton | 5bd378b | 2003-04-03 16:28:38 +0000 | [diff] [blame] | 893 | traverseproc traverse = obj->ob_type->tp_traverse; |
| 894 | if (!traverse) |
| 895 | continue; |
| 896 | if (traverse(obj, (visitproc)referrentsvisit, result)) |
| 897 | return NULL; |
| 898 | } |
| 899 | return result; |
| 900 | } |
| 901 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 902 | PyDoc_STRVAR(gc_get_objects__doc__, |
Neil Schemenauer | c7c8d8e | 2001-08-09 15:58:59 +0000 | [diff] [blame] | 903 | "get_objects() -> [...]\n" |
| 904 | "\n" |
| 905 | "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] | 906 | "returned).\n"); |
Neil Schemenauer | c7c8d8e | 2001-08-09 15:58:59 +0000 | [diff] [blame] | 907 | |
| 908 | /* appending objects in a GC list to a Python list */ |
Martin v. Löwis | 155aad1 | 2001-12-02 12:21:34 +0000 | [diff] [blame] | 909 | static int |
Neil Schemenauer | c7c8d8e | 2001-08-09 15:58:59 +0000 | [diff] [blame] | 910 | append_objects(PyObject *py_list, PyGC_Head *gc_list) |
| 911 | { |
| 912 | PyGC_Head *gc; |
Tim Peters | 9e4ca10 | 2001-10-11 18:31:31 +0000 | [diff] [blame] | 913 | for (gc = gc_list->gc.gc_next; gc != gc_list; gc = gc->gc.gc_next) { |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 914 | PyObject *op = FROM_GC(gc); |
Neil Schemenauer | c7c8d8e | 2001-08-09 15:58:59 +0000 | [diff] [blame] | 915 | if (op != py_list) { |
Martin v. Löwis | 155aad1 | 2001-12-02 12:21:34 +0000 | [diff] [blame] | 916 | if (PyList_Append(py_list, op)) { |
| 917 | return -1; /* exception */ |
| 918 | } |
Neil Schemenauer | c7c8d8e | 2001-08-09 15:58:59 +0000 | [diff] [blame] | 919 | } |
| 920 | } |
Martin v. Löwis | 155aad1 | 2001-12-02 12:21:34 +0000 | [diff] [blame] | 921 | return 0; |
Neil Schemenauer | c7c8d8e | 2001-08-09 15:58:59 +0000 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | static PyObject * |
| 925 | gc_get_objects(PyObject *self, PyObject *args) |
| 926 | { |
Neil Schemenauer | 2880ae5 | 2002-05-04 05:35:20 +0000 | [diff] [blame] | 927 | int i; |
Neil Schemenauer | c7c8d8e | 2001-08-09 15:58:59 +0000 | [diff] [blame] | 928 | PyObject* result; |
| 929 | |
| 930 | if (!PyArg_ParseTuple(args, ":get_objects")) /* check no args */ |
| 931 | return NULL; |
| 932 | result = PyList_New(0); |
Martin v. Löwis | f8a6f24 | 2001-12-02 18:31:02 +0000 | [diff] [blame] | 933 | if (result == NULL) { |
| 934 | return NULL; |
| 935 | } |
Neil Schemenauer | 2880ae5 | 2002-05-04 05:35:20 +0000 | [diff] [blame] | 936 | for (i = 0; i < NUM_GENERATIONS; i++) { |
| 937 | if (append_objects(result, GEN_HEAD(i))) { |
| 938 | Py_DECREF(result); |
| 939 | return NULL; |
| 940 | } |
Martin v. Löwis | 155aad1 | 2001-12-02 12:21:34 +0000 | [diff] [blame] | 941 | } |
Neil Schemenauer | c7c8d8e | 2001-08-09 15:58:59 +0000 | [diff] [blame] | 942 | return result; |
| 943 | } |
| 944 | |
| 945 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 946 | PyDoc_STRVAR(gc__doc__, |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 947 | "This module provides access to the garbage collector for reference cycles.\n" |
| 948 | "\n" |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 949 | "enable() -- Enable automatic garbage collection.\n" |
| 950 | "disable() -- Disable automatic garbage collection.\n" |
| 951 | "isenabled() -- Returns true if automatic collection is enabled.\n" |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 952 | "collect() -- Do a full collection right now.\n" |
| 953 | "set_debug() -- Set debugging flags.\n" |
| 954 | "get_debug() -- Get debugging flags.\n" |
| 955 | "set_threshold() -- Set the collection thresholds.\n" |
| 956 | "get_threshold() -- Return the current the collection thresholds.\n" |
Neil Schemenauer | c7c8d8e | 2001-08-09 15:58:59 +0000 | [diff] [blame] | 957 | "get_objects() -- Return a list of all objects tracked by the collector.\n" |
Jeremy Hylton | 5bd378b | 2003-04-03 16:28:38 +0000 | [diff] [blame] | 958 | "get_referrers() -- Return the list of objects that refer to an object.\n" |
| 959 | "get_referrents() -- Return the list of objects that an object refers to.\n"); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 960 | |
| 961 | static PyMethodDef GcMethods[] = { |
Neil Schemenauer | 544de1e | 2000-09-22 15:22:38 +0000 | [diff] [blame] | 962 | {"enable", gc_enable, METH_VARARGS, gc_enable__doc__}, |
| 963 | {"disable", gc_disable, METH_VARARGS, gc_disable__doc__}, |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 964 | {"isenabled", gc_isenabled, METH_VARARGS, gc_isenabled__doc__}, |
| 965 | {"set_debug", gc_set_debug, METH_VARARGS, gc_set_debug__doc__}, |
| 966 | {"get_debug", gc_get_debug, METH_VARARGS, gc_get_debug__doc__}, |
| 967 | {"set_threshold", gc_set_thresh, METH_VARARGS, gc_set_thresh__doc__}, |
| 968 | {"get_threshold", gc_get_thresh, METH_VARARGS, gc_get_thresh__doc__}, |
Neil Schemenauer | 544de1e | 2000-09-22 15:22:38 +0000 | [diff] [blame] | 969 | {"collect", gc_collect, METH_VARARGS, gc_collect__doc__}, |
Neil Schemenauer | c7c8d8e | 2001-08-09 15:58:59 +0000 | [diff] [blame] | 970 | {"get_objects", gc_get_objects,METH_VARARGS, gc_get_objects__doc__}, |
Martin v. Löwis | 560da62 | 2001-11-24 09:24:51 +0000 | [diff] [blame] | 971 | {"get_referrers", gc_get_referrers, METH_VARARGS, |
| 972 | gc_get_referrers__doc__}, |
Jeremy Hylton | 5bd378b | 2003-04-03 16:28:38 +0000 | [diff] [blame] | 973 | {"get_referrents", gc_get_referrents, METH_VARARGS, |
| 974 | gc_get_referrents__doc__}, |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 975 | {NULL, NULL} /* Sentinel */ |
| 976 | }; |
| 977 | |
| 978 | void |
| 979 | initgc(void) |
| 980 | { |
| 981 | PyObject *m; |
| 982 | PyObject *d; |
| 983 | |
| 984 | m = Py_InitModule4("gc", |
| 985 | GcMethods, |
| 986 | gc__doc__, |
| 987 | NULL, |
| 988 | PYTHON_API_VERSION); |
| 989 | d = PyModule_GetDict(m); |
| 990 | if (garbage == NULL) { |
| 991 | garbage = PyList_New(0); |
| 992 | } |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 993 | PyDict_SetItemString(d, "garbage", garbage); |
| 994 | PyDict_SetItemString(d, "DEBUG_STATS", |
| 995 | PyInt_FromLong(DEBUG_STATS)); |
| 996 | PyDict_SetItemString(d, "DEBUG_COLLECTABLE", |
| 997 | PyInt_FromLong(DEBUG_COLLECTABLE)); |
| 998 | PyDict_SetItemString(d, "DEBUG_UNCOLLECTABLE", |
| 999 | PyInt_FromLong(DEBUG_UNCOLLECTABLE)); |
| 1000 | PyDict_SetItemString(d, "DEBUG_INSTANCES", |
| 1001 | PyInt_FromLong(DEBUG_INSTANCES)); |
| 1002 | PyDict_SetItemString(d, "DEBUG_OBJECTS", |
| 1003 | PyInt_FromLong(DEBUG_OBJECTS)); |
Neil Schemenauer | 544de1e | 2000-09-22 15:22:38 +0000 | [diff] [blame] | 1004 | PyDict_SetItemString(d, "DEBUG_SAVEALL", |
| 1005 | PyInt_FromLong(DEBUG_SAVEALL)); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1006 | PyDict_SetItemString(d, "DEBUG_LEAK", |
| 1007 | PyInt_FromLong(DEBUG_LEAK)); |
| 1008 | } |
| 1009 | |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1010 | /* for debugging */ |
| 1011 | void _PyGC_Dump(PyGC_Head *g) |
| 1012 | { |
| 1013 | _PyObject_Dump(FROM_GC(g)); |
| 1014 | } |
| 1015 | |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1016 | /* extension modules might be compiled with GC support so these |
| 1017 | functions must always be available */ |
| 1018 | |
Neil Schemenauer | fec4eb1 | 2002-04-12 02:41:03 +0000 | [diff] [blame] | 1019 | #undef PyObject_GC_Track |
| 1020 | #undef PyObject_GC_UnTrack |
| 1021 | #undef PyObject_GC_Del |
| 1022 | #undef _PyObject_GC_Malloc |
| 1023 | |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1024 | void |
Neil Schemenauer | fec4eb1 | 2002-04-12 02:41:03 +0000 | [diff] [blame] | 1025 | PyObject_GC_Track(void *op) |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1026 | { |
| 1027 | _PyObject_GC_TRACK(op); |
| 1028 | } |
| 1029 | |
Neil Schemenauer | fec4eb1 | 2002-04-12 02:41:03 +0000 | [diff] [blame] | 1030 | /* for binary compatibility with 2.2 */ |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1031 | void |
Neil Schemenauer | fec4eb1 | 2002-04-12 02:41:03 +0000 | [diff] [blame] | 1032 | _PyObject_GC_Track(PyObject *op) |
| 1033 | { |
| 1034 | PyObject_GC_Track(op); |
| 1035 | } |
| 1036 | |
| 1037 | void |
| 1038 | PyObject_GC_UnTrack(void *op) |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1039 | { |
Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 1040 | /* Obscure: the Py_TRASHCAN mechanism requires that we be able to |
| 1041 | * call PyObject_GC_UnTrack twice on an object. |
| 1042 | */ |
Neil Schemenauer | a2b11ec | 2002-05-21 15:53:24 +0000 | [diff] [blame] | 1043 | if (IS_TRACKED(op)) |
Guido van Rossum | ff413af | 2002-03-28 20:34:59 +0000 | [diff] [blame] | 1044 | _PyObject_GC_UNTRACK(op); |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1045 | } |
| 1046 | |
Neil Schemenauer | fec4eb1 | 2002-04-12 02:41:03 +0000 | [diff] [blame] | 1047 | /* for binary compatibility with 2.2 */ |
| 1048 | void |
| 1049 | _PyObject_GC_UnTrack(PyObject *op) |
| 1050 | { |
| 1051 | PyObject_GC_UnTrack(op); |
| 1052 | } |
| 1053 | |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1054 | PyObject * |
Neil Schemenauer | fec4eb1 | 2002-04-12 02:41:03 +0000 | [diff] [blame] | 1055 | _PyObject_GC_Malloc(size_t basicsize) |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1056 | { |
| 1057 | PyObject *op; |
Neil Schemenauer | fec4eb1 | 2002-04-12 02:41:03 +0000 | [diff] [blame] | 1058 | PyGC_Head *g = PyObject_MALLOC(sizeof(PyGC_Head) + basicsize); |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1059 | if (g == NULL) |
Jeremy Hylton | 8a13518 | 2002-06-06 23:23:55 +0000 | [diff] [blame] | 1060 | return PyErr_NoMemory(); |
Tim Peters | ea40563 | 2002-07-02 00:52:30 +0000 | [diff] [blame] | 1061 | g->gc.gc_refs = GC_UNTRACKED; |
Neil Schemenauer | 2880ae5 | 2002-05-04 05:35:20 +0000 | [diff] [blame] | 1062 | generations[0].count++; /* number of allocated GC objects */ |
| 1063 | if (generations[0].count > generations[0].threshold && |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1064 | enabled && |
Neil Schemenauer | 2880ae5 | 2002-05-04 05:35:20 +0000 | [diff] [blame] | 1065 | generations[0].threshold && |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1066 | !collecting && |
| 1067 | !PyErr_Occurred()) { |
| 1068 | collecting = 1; |
Neil Schemenauer | 2880ae5 | 2002-05-04 05:35:20 +0000 | [diff] [blame] | 1069 | collect_generations(); |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1070 | collecting = 0; |
| 1071 | } |
| 1072 | op = FROM_GC(g); |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1073 | return op; |
| 1074 | } |
| 1075 | |
| 1076 | PyObject * |
| 1077 | _PyObject_GC_New(PyTypeObject *tp) |
| 1078 | { |
Neil Schemenauer | fec4eb1 | 2002-04-12 02:41:03 +0000 | [diff] [blame] | 1079 | PyObject *op = _PyObject_GC_Malloc(_PyObject_SIZE(tp)); |
Tim Peters | fa8efab | 2002-04-28 01:57:25 +0000 | [diff] [blame] | 1080 | if (op != NULL) |
| 1081 | op = PyObject_INIT(op, tp); |
| 1082 | return op; |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1083 | } |
| 1084 | |
| 1085 | PyVarObject * |
Tim Peters | 6d483d3 | 2001-10-06 21:27:34 +0000 | [diff] [blame] | 1086 | _PyObject_GC_NewVar(PyTypeObject *tp, int nitems) |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1087 | { |
Neil Schemenauer | fec4eb1 | 2002-04-12 02:41:03 +0000 | [diff] [blame] | 1088 | const size_t size = _PyObject_VAR_SIZE(tp, nitems); |
| 1089 | PyVarObject *op = (PyVarObject *) _PyObject_GC_Malloc(size); |
Tim Peters | fa8efab | 2002-04-28 01:57:25 +0000 | [diff] [blame] | 1090 | if (op != NULL) |
| 1091 | op = PyObject_INIT_VAR(op, tp, nitems); |
| 1092 | return op; |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | PyVarObject * |
Tim Peters | 6d483d3 | 2001-10-06 21:27:34 +0000 | [diff] [blame] | 1096 | _PyObject_GC_Resize(PyVarObject *op, int nitems) |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1097 | { |
Tim Peters | f2a67da | 2001-10-07 03:54:51 +0000 | [diff] [blame] | 1098 | const size_t basicsize = _PyObject_VAR_SIZE(op->ob_type, nitems); |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1099 | PyGC_Head *g = AS_GC(op); |
Neil Schemenauer | fec4eb1 | 2002-04-12 02:41:03 +0000 | [diff] [blame] | 1100 | g = PyObject_REALLOC(g, sizeof(PyGC_Head) + basicsize); |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1101 | if (g == NULL) |
| 1102 | return (PyVarObject *)PyErr_NoMemory(); |
| 1103 | op = (PyVarObject *) FROM_GC(g); |
Tim Peters | 6d483d3 | 2001-10-06 21:27:34 +0000 | [diff] [blame] | 1104 | op->ob_size = nitems; |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1105 | return op; |
| 1106 | } |
| 1107 | |
| 1108 | void |
Neil Schemenauer | fec4eb1 | 2002-04-12 02:41:03 +0000 | [diff] [blame] | 1109 | PyObject_GC_Del(void *op) |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1110 | { |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1111 | PyGC_Head *g = AS_GC(op); |
Neil Schemenauer | a2b11ec | 2002-05-21 15:53:24 +0000 | [diff] [blame] | 1112 | if (IS_TRACKED(op)) |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1113 | gc_list_remove(g); |
Neil Schemenauer | 2880ae5 | 2002-05-04 05:35:20 +0000 | [diff] [blame] | 1114 | if (generations[0].count > 0) { |
| 1115 | generations[0].count--; |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1116 | } |
Neil Schemenauer | fec4eb1 | 2002-04-12 02:41:03 +0000 | [diff] [blame] | 1117 | PyObject_FREE(g); |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 1118 | } |
| 1119 | |
Neil Schemenauer | fec4eb1 | 2002-04-12 02:41:03 +0000 | [diff] [blame] | 1120 | /* for binary compatibility with 2.2 */ |
| 1121 | #undef _PyObject_GC_Del |
| 1122 | void |
| 1123 | _PyObject_GC_Del(PyObject *op) |
| 1124 | { |
| 1125 | PyObject_GC_Del(op); |
| 1126 | } |