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