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