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