Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1 | /* |
| 2 | |
| 3 | Reference Cycle Garbage Collection |
| 4 | ================================== |
| 5 | |
| 6 | Neil Schemenauer <nascheme@enme.ucalgary.ca> |
| 7 | |
| 8 | Based on a post on the python-dev list. Ideas from Guido van Rossum, |
| 9 | Eric Tiedemann, and various others. |
| 10 | |
| 11 | http://www.enme.calgary.ca/~nascheme/python/gc.html |
| 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 | |
| 19 | TODO: |
| 20 | use a different interface for set_debug() (keywords)? |
| 21 | tune parameters |
| 22 | |
| 23 | */ |
| 24 | |
| 25 | |
| 26 | #include "Python.h" |
| 27 | |
| 28 | #ifdef WITH_CYCLE_GC |
| 29 | |
| 30 | /* magic gc_refs value */ |
| 31 | #define GC_MOVED -1 |
| 32 | |
| 33 | /*** Global GC state ***/ |
| 34 | |
| 35 | /* linked lists of container objects */ |
| 36 | static PyGC_Head generation0 = {&generation0, &generation0, 0}; |
| 37 | static PyGC_Head generation1 = {&generation1, &generation1, 0}; |
| 38 | static PyGC_Head generation2 = {&generation2, &generation2, 0}; |
| 39 | static int generation = 0; /* current generation being collected */ |
| 40 | |
| 41 | /* collection frequencies, XXX tune these */ |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 42 | static int enabled = 1; /* automatic collection enabled? */ |
Jeremy Hylton | 045946d | 2000-09-01 04:01:55 +0000 | [diff] [blame] | 43 | static int threshold0 = 5000; /* net new containers before collection */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 44 | static int threshold1 = 10; /* generation0 collections before collecting 1 */ |
| 45 | static int threshold2 = 10; /* generation1 collections before collecting 2 */ |
| 46 | |
| 47 | /* net new objects allocated since last collection */ |
| 48 | static int allocated; |
| 49 | |
| 50 | /* set for debugging information */ |
| 51 | #define DEBUG_STATS (1<<0) /* print collection statistics */ |
| 52 | #define DEBUG_COLLECTABLE (1<<1) /* print collectable objects */ |
| 53 | #define DEBUG_UNCOLLECTABLE (1<<2) /* print uncollectable objects */ |
| 54 | #define DEBUG_INSTANCES (1<<3) /* print instances */ |
| 55 | #define DEBUG_OBJECTS (1<<4) /* print other objects */ |
| 56 | #define DEBUG_LEAK DEBUG_COLLECTABLE | \ |
| 57 | DEBUG_UNCOLLECTABLE | \ |
| 58 | DEBUG_INSTANCES | \ |
| 59 | DEBUG_OBJECTS |
Jeremy Hylton | b709df3 | 2000-09-01 02:47:25 +0000 | [diff] [blame] | 60 | static int debug; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 61 | |
| 62 | /* list of uncollectable objects */ |
| 63 | static PyObject *garbage; |
| 64 | |
Jeremy Hylton | b709df3 | 2000-09-01 02:47:25 +0000 | [diff] [blame] | 65 | /* Python string to use if unhandled exception occurs */ |
| 66 | static PyObject *gc_str; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 67 | |
| 68 | /*** list functions ***/ |
| 69 | |
| 70 | static void |
| 71 | gc_list_init(PyGC_Head *list) |
| 72 | { |
| 73 | list->gc_prev = list; |
| 74 | list->gc_next = list; |
| 75 | } |
| 76 | |
| 77 | static void |
| 78 | gc_list_append(PyGC_Head *node, PyGC_Head *list) |
| 79 | { |
| 80 | node->gc_next = list; |
| 81 | node->gc_prev = list->gc_prev; |
| 82 | node->gc_prev->gc_next = node; |
| 83 | list->gc_prev = node; |
| 84 | } |
| 85 | |
| 86 | static void |
| 87 | gc_list_remove(PyGC_Head *node) |
| 88 | { |
| 89 | node->gc_prev->gc_next = node->gc_next; |
| 90 | node->gc_next->gc_prev = node->gc_prev; |
| 91 | #ifdef Py_DEBUG |
| 92 | node->gc_prev = NULL; |
| 93 | node->gc_next = NULL; |
| 94 | #endif |
| 95 | } |
| 96 | |
| 97 | static void |
| 98 | gc_list_move(PyGC_Head *from, PyGC_Head *to) |
| 99 | { |
| 100 | if (from->gc_next == from) { |
| 101 | /* empty from list */ |
| 102 | gc_list_init(to); |
| 103 | } else { |
| 104 | to->gc_next = from->gc_next; |
| 105 | to->gc_next->gc_prev = to; |
| 106 | to->gc_prev = from->gc_prev; |
| 107 | to->gc_prev->gc_next = to; |
| 108 | } |
| 109 | gc_list_init(from); |
| 110 | } |
| 111 | |
| 112 | /* append a list onto another list, from becomes an empty list */ |
| 113 | static void |
| 114 | gc_list_merge(PyGC_Head *from, PyGC_Head *to) |
| 115 | { |
| 116 | PyGC_Head *tail; |
| 117 | if (from->gc_next != from) { |
| 118 | tail = to->gc_prev; |
| 119 | tail->gc_next = from->gc_next; |
| 120 | tail->gc_next->gc_prev = tail; |
| 121 | to->gc_prev = from->gc_prev; |
| 122 | to->gc_prev->gc_next = to; |
| 123 | } |
| 124 | gc_list_init(from); |
| 125 | } |
| 126 | |
| 127 | static long |
| 128 | gc_list_size(PyGC_Head *list) |
| 129 | { |
| 130 | PyGC_Head *gc; |
| 131 | long n = 0; |
| 132 | for (gc = list->gc_next; gc != list; gc = gc->gc_next) { |
| 133 | n++; |
| 134 | } |
| 135 | return n; |
| 136 | } |
| 137 | |
| 138 | /*** end of list stuff ***/ |
| 139 | |
| 140 | |
| 141 | /* Set all gc_refs = ob_refcnt */ |
| 142 | static void |
| 143 | update_refs(PyGC_Head *containers) |
| 144 | { |
| 145 | PyGC_Head *gc = containers->gc_next; |
| 146 | for (; gc != containers; gc=gc->gc_next) { |
| 147 | gc->gc_refs = PyObject_FROM_GC(gc)->ob_refcnt; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | static int |
| 152 | visit_decref(PyObject *op, void *data) |
| 153 | { |
| 154 | if (op && PyObject_IS_GC(op)) { |
| 155 | PyObject_AS_GC(op)->gc_refs--; |
| 156 | } |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | /* Subtract internal references from gc_refs */ |
| 161 | static void |
| 162 | subtract_refs(PyGC_Head *containers) |
| 163 | { |
| 164 | traverseproc traverse; |
| 165 | PyGC_Head *gc = containers->gc_next; |
| 166 | for (; gc != containers; gc=gc->gc_next) { |
| 167 | traverse = PyObject_FROM_GC(gc)->ob_type->tp_traverse; |
| 168 | (void) traverse(PyObject_FROM_GC(gc), |
| 169 | (visitproc)visit_decref, |
| 170 | NULL); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | /* Append objects with gc_refs > 0 to roots list */ |
| 175 | static void |
| 176 | move_roots(PyGC_Head *containers, PyGC_Head *roots) |
| 177 | { |
| 178 | PyGC_Head *next; |
| 179 | PyGC_Head *gc = containers->gc_next; |
| 180 | while (gc != containers) { |
| 181 | next = gc->gc_next; |
| 182 | if (gc->gc_refs > 0) { |
| 183 | gc_list_remove(gc); |
| 184 | gc_list_append(gc, roots); |
| 185 | gc->gc_refs = GC_MOVED; |
| 186 | } |
| 187 | gc = next; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | static int |
| 192 | visit_reachable(PyObject *op, PyGC_Head *roots) |
| 193 | { |
| 194 | if (PyObject_IS_GC(op)) { |
| 195 | PyGC_Head *gc = PyObject_AS_GC(op); |
| 196 | if (gc && gc->gc_refs != GC_MOVED) { |
| 197 | gc_list_remove(gc); |
| 198 | gc_list_append(gc, roots); |
| 199 | gc->gc_refs = GC_MOVED; |
| 200 | } |
| 201 | } |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | /* Move objects referenced from reachable to reachable set. */ |
| 206 | static void |
| 207 | move_root_reachable(PyGC_Head *reachable) |
| 208 | { |
| 209 | traverseproc traverse; |
| 210 | PyGC_Head *gc = reachable->gc_next; |
| 211 | for (; gc != reachable; gc=gc->gc_next) { |
| 212 | /* careful, reachable list is growing here */ |
| 213 | PyObject *op = PyObject_FROM_GC(gc); |
| 214 | traverse = op->ob_type->tp_traverse; |
| 215 | (void) traverse(op, |
| 216 | (visitproc)visit_reachable, |
| 217 | (void *)reachable); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | /* move all objects with finalizers (instances with __del__) */ |
| 222 | static void |
| 223 | move_finalizers(PyGC_Head *unreachable, PyGC_Head *finalizers) |
| 224 | { |
| 225 | PyGC_Head *next; |
| 226 | PyGC_Head *gc = unreachable->gc_next; |
Jeremy Hylton | 0625777 | 2000-08-31 15:10:24 +0000 | [diff] [blame] | 227 | static PyObject *delstr = NULL; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 228 | if (delstr == NULL) { |
| 229 | delstr = PyString_InternFromString("__del__"); |
Jeremy Hylton | 0625777 | 2000-08-31 15:10:24 +0000 | [diff] [blame] | 230 | if (delstr == NULL) |
| 231 | Py_FatalError("PyGC: can't initialize __del__ string"); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 232 | } |
| 233 | for (; gc != unreachable; gc=next) { |
| 234 | PyObject *op = PyObject_FROM_GC(gc); |
| 235 | next = gc->gc_next; |
| 236 | if (PyInstance_Check(op) && PyObject_HasAttr(op, delstr)) { |
| 237 | gc_list_remove(gc); |
| 238 | gc_list_append(gc, finalizers); |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | |
| 244 | /* called by tp_traverse */ |
| 245 | static int |
| 246 | visit_finalizer_reachable(PyObject *op, PyGC_Head *finalizers) |
| 247 | { |
| 248 | if (PyObject_IS_GC(op)) { |
| 249 | PyGC_Head *gc = PyObject_AS_GC(op); |
| 250 | if (gc && gc->gc_refs != GC_MOVED) { |
| 251 | gc_list_remove(gc); |
| 252 | gc_list_append(gc, finalizers); |
| 253 | gc->gc_refs = GC_MOVED; |
| 254 | } |
| 255 | } |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | /* Move objects referenced from roots to roots */ |
| 260 | static void |
| 261 | move_finalizer_reachable(PyGC_Head *finalizers) |
| 262 | { |
| 263 | traverseproc traverse; |
| 264 | PyGC_Head *gc = finalizers->gc_next; |
| 265 | for (; gc != finalizers; gc=gc->gc_next) { |
| 266 | /* careful, finalizers list is growing here */ |
| 267 | traverse = PyObject_FROM_GC(gc)->ob_type->tp_traverse; |
| 268 | (void) traverse(PyObject_FROM_GC(gc), |
| 269 | (visitproc)visit_finalizer_reachable, |
| 270 | (void *)finalizers); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | static void |
Jeremy Hylton | 0625777 | 2000-08-31 15:10:24 +0000 | [diff] [blame] | 275 | debug_instance(char *msg, PyInstanceObject *inst) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 276 | { |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 277 | char *cname; |
| 278 | /* be careful not to create new dictionaries */ |
| 279 | PyObject *classname = inst->in_class->cl_name; |
| 280 | if (classname != NULL && PyString_Check(classname)) |
| 281 | cname = PyString_AsString(classname); |
| 282 | else |
| 283 | cname = "?"; |
Jeremy Hylton | 0625777 | 2000-08-31 15:10:24 +0000 | [diff] [blame] | 284 | PySys_WriteStderr("gc: %.100s <%.100s instance at %p>\n", |
| 285 | msg, cname, inst); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | static void |
Jeremy Hylton | 0625777 | 2000-08-31 15:10:24 +0000 | [diff] [blame] | 289 | debug_cycle(char *msg, PyObject *op) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 290 | { |
| 291 | if ((debug & DEBUG_INSTANCES) && PyInstance_Check(op)) { |
Jeremy Hylton | 0625777 | 2000-08-31 15:10:24 +0000 | [diff] [blame] | 292 | debug_instance(msg, (PyInstanceObject *)op); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 293 | } else if (debug & DEBUG_OBJECTS) { |
Jeremy Hylton | 0625777 | 2000-08-31 15:10:24 +0000 | [diff] [blame] | 294 | PySys_WriteStderr("gc: %.100s <%.100s %p>\n", |
| 295 | msg, op->ob_type->tp_name, op); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | |
| 299 | /* Handle uncollectable garbage (cycles with finalizers). */ |
| 300 | static void |
| 301 | handle_finalizers(PyGC_Head *finalizers, PyGC_Head *old) |
| 302 | { |
| 303 | PyGC_Head *gc; |
| 304 | if (garbage == NULL) { |
| 305 | garbage = PyList_New(0); |
| 306 | } |
| 307 | for (gc = finalizers->gc_next; gc != finalizers; |
| 308 | gc = finalizers->gc_next) { |
| 309 | PyObject *op = PyObject_FROM_GC(gc); |
| 310 | /* Add all instances to a Python accessible list of garbage */ |
| 311 | if (PyInstance_Check(op)) { |
| 312 | PyList_Append(garbage, op); |
| 313 | } |
| 314 | /* We assume that all objects in finalizers are reachable from |
| 315 | * instances. Once we add the instances to the garbage list |
| 316 | * everything is reachable from Python again. */ |
| 317 | gc_list_remove(gc); |
| 318 | gc_list_append(gc, old); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | /* Break reference cycles by clearing the containers involved. This is |
| 323 | * tricky business as the lists can be changing and we don't know which |
| 324 | * objects may be freed. It is possible I screwed something up here. */ |
| 325 | static void |
| 326 | delete_garbage(PyGC_Head *unreachable, PyGC_Head *old) |
| 327 | { |
| 328 | inquiry clear; |
| 329 | |
| 330 | while (unreachable->gc_next != unreachable) { |
| 331 | PyGC_Head *gc = unreachable->gc_next; |
| 332 | PyObject *op = PyObject_FROM_GC(gc); |
| 333 | /* |
| 334 | PyList_Append(garbage, op); |
| 335 | */ |
| 336 | if ((clear = op->ob_type->tp_clear) != NULL) { |
| 337 | Py_INCREF(op); |
| 338 | clear((PyObject *)op); |
| 339 | Py_DECREF(op); |
| 340 | } |
| 341 | /* only try to call tp_clear once for each object */ |
| 342 | if (unreachable->gc_next == gc) { |
| 343 | /* still alive, move it, it may die later */ |
| 344 | gc_list_remove(gc); |
| 345 | gc_list_append(gc, old); |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | /* This is the main function. Read this to understand how the |
| 351 | * collection process works. */ |
| 352 | static long |
| 353 | collect(PyGC_Head *young, PyGC_Head *old) |
| 354 | { |
| 355 | long n = 0; |
| 356 | long m = 0; |
| 357 | PyGC_Head reachable; |
| 358 | PyGC_Head unreachable; |
| 359 | PyGC_Head finalizers; |
| 360 | PyGC_Head *gc; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 361 | |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 362 | if (debug & DEBUG_STATS) { |
Jeremy Hylton | 0625777 | 2000-08-31 15:10:24 +0000 | [diff] [blame] | 363 | PySys_WriteStderr( |
| 364 | "gc: collecting generation %d...\n" |
| 365 | "gc: objects in each generation: %ld %ld %ld\n", |
| 366 | generation, |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 367 | gc_list_size(&generation0), |
| 368 | gc_list_size(&generation1), |
| 369 | gc_list_size(&generation2)); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | /* Using ob_refcnt and gc_refs, calculate which objects in the |
| 373 | * container set are reachable from outside the set (ie. have a |
| 374 | * refcount greater than 0 when all the references within the |
| 375 | * set are taken into account */ |
| 376 | update_refs(young); |
| 377 | subtract_refs(young); |
| 378 | |
| 379 | /* Move everything reachable from outside the set into the |
| 380 | * reachable set (ie. gc_refs > 0). Next, move everything |
| 381 | * reachable from objects in the reachable set. */ |
| 382 | gc_list_init(&reachable); |
| 383 | move_roots(young, &reachable); |
| 384 | move_root_reachable(&reachable); |
| 385 | |
| 386 | /* move unreachable objects to a temporary list, new objects can be |
| 387 | * allocated after this point */ |
| 388 | gc_list_init(&unreachable); |
| 389 | gc_list_move(young, &unreachable); |
| 390 | |
| 391 | /* move reachable objects to next generation */ |
| 392 | gc_list_merge(&reachable, old); |
| 393 | |
| 394 | /* Move objects reachable from finalizers, we can't safely delete |
| 395 | * them. Python programmers should take care not to create such |
| 396 | * things. For Python finalizers means instance objects with |
| 397 | * __del__ methods. */ |
| 398 | gc_list_init(&finalizers); |
| 399 | move_finalizers(&unreachable, &finalizers); |
| 400 | move_finalizer_reachable(&finalizers); |
| 401 | |
| 402 | /* Collect statistics on collectable objects found and print |
| 403 | * debugging information. */ |
| 404 | for (gc = unreachable.gc_next; gc != &unreachable; |
| 405 | gc = gc->gc_next) { |
| 406 | m++; |
Jeremy Hylton | 0625777 | 2000-08-31 15:10:24 +0000 | [diff] [blame] | 407 | if (debug & DEBUG_COLLECTABLE) { |
| 408 | debug_cycle("collectable", PyObject_FROM_GC(gc)); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | /* call tp_clear on objects in the collectable set. This will cause |
| 412 | * the reference cycles to be broken. It may also cause some objects in |
| 413 | * finalizers to be freed */ |
| 414 | delete_garbage(&unreachable, old); |
| 415 | |
| 416 | /* Collect statistics on uncollectable objects found and print |
| 417 | * debugging information. */ |
| 418 | for (gc = finalizers.gc_next; gc != &finalizers; |
| 419 | gc = gc->gc_next) { |
| 420 | n++; |
Jeremy Hylton | 0625777 | 2000-08-31 15:10:24 +0000 | [diff] [blame] | 421 | if (debug & DEBUG_UNCOLLECTABLE) { |
| 422 | debug_cycle("uncollectable", PyObject_FROM_GC(gc)); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 423 | } |
| 424 | } |
Jeremy Hylton | 0625777 | 2000-08-31 15:10:24 +0000 | [diff] [blame] | 425 | if (debug & DEBUG_STATS) { |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 426 | if (m == 0 && n == 0) { |
Jeremy Hylton | 0625777 | 2000-08-31 15:10:24 +0000 | [diff] [blame] | 427 | PySys_WriteStderr("gc: done.\n"); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 428 | } else { |
Jeremy Hylton | 0625777 | 2000-08-31 15:10:24 +0000 | [diff] [blame] | 429 | PySys_WriteStderr( |
| 430 | "gc: done, %ld unreachable, %ld uncollectable.\n", |
| 431 | n+m, n); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 432 | } |
| 433 | } |
| 434 | |
| 435 | /* Append instances in the uncollectable set to a Python |
| 436 | * reachable list of garbage. The programmer has to deal with |
| 437 | * this if they insist on creating this type of structure. */ |
| 438 | handle_finalizers(&finalizers, old); |
| 439 | |
Jeremy Hylton | b709df3 | 2000-09-01 02:47:25 +0000 | [diff] [blame] | 440 | if (PyErr_Occurred()) { |
| 441 | PyErr_WriteUnraisable(gc_str); |
| 442 | Py_FatalError("unexpected exception during garbage collection"); |
| 443 | } |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 444 | allocated = 0; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 445 | return n+m; |
| 446 | } |
| 447 | |
| 448 | static long |
| 449 | collect_generations(void) |
| 450 | { |
| 451 | static long collections0 = 0; |
| 452 | static long collections1 = 0; |
Vladimir Marangozov | b16714b | 2000-07-10 05:37:39 +0000 | [diff] [blame] | 453 | long n = 0; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 454 | |
| 455 | |
| 456 | if (collections1 > threshold2) { |
| 457 | generation = 2; |
| 458 | gc_list_merge(&generation0, &generation2); |
| 459 | gc_list_merge(&generation1, &generation2); |
| 460 | if (generation2.gc_next != &generation2) { |
| 461 | n = collect(&generation2, &generation2); |
| 462 | } |
| 463 | collections1 = 0; |
| 464 | } else if (collections0 > threshold1) { |
| 465 | generation = 1; |
| 466 | collections1++; |
| 467 | gc_list_merge(&generation0, &generation1); |
| 468 | if (generation1.gc_next != &generation1) { |
| 469 | n = collect(&generation1, &generation2); |
| 470 | } |
| 471 | collections0 = 0; |
| 472 | } else { |
| 473 | generation = 0; |
| 474 | collections0++; |
| 475 | if (generation0.gc_next != &generation0) { |
| 476 | n = collect(&generation0, &generation1); |
| 477 | } |
| 478 | } |
| 479 | return n; |
| 480 | } |
| 481 | |
| 482 | void |
| 483 | _PyGC_Insert(PyObject *op) |
| 484 | { |
| 485 | /* collection lock since collecting may cause allocations */ |
| 486 | static int collecting = 0; |
| 487 | |
| 488 | #ifdef Py_DEBUG |
| 489 | if (!PyObject_IS_GC(op)) { |
| 490 | abort(); |
| 491 | } |
| 492 | #endif |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 493 | if (allocated > threshold0 && enabled && threshold0 && !collecting) { |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 494 | collecting++; |
| 495 | collect_generations(); |
| 496 | collecting--; |
| 497 | } |
| 498 | allocated++; |
| 499 | gc_list_append(PyObject_AS_GC(op), &generation0); |
| 500 | } |
| 501 | |
| 502 | void |
| 503 | _PyGC_Remove(PyObject *op) |
| 504 | { |
| 505 | PyGC_Head *g = PyObject_AS_GC(op); |
| 506 | #ifdef Py_DEBUG |
| 507 | if (!PyObject_IS_GC(op)) { |
| 508 | abort(); |
| 509 | } |
| 510 | #endif |
| 511 | gc_list_remove(g); |
| 512 | if (allocated > 0) { |
| 513 | allocated--; |
| 514 | } |
| 515 | } |
| 516 | |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 517 | static char gc_enable__doc__[] = |
| 518 | "enable() -> None\n" |
| 519 | "\n" |
| 520 | "Enable automatic garbage collection.\n" |
| 521 | ; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 522 | |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 523 | static PyObject * |
| 524 | gc_enable(PyObject *self, PyObject *args) |
| 525 | { |
| 526 | |
| 527 | if (!PyArg_ParseTuple(args, ":enable")) /* check no args */ |
| 528 | return NULL; |
| 529 | |
| 530 | enabled = 1; |
| 531 | |
| 532 | Py_INCREF(Py_None); |
| 533 | return Py_None; |
| 534 | } |
| 535 | |
| 536 | static char gc_disable__doc__[] = |
| 537 | "disable() -> None\n" |
| 538 | "\n" |
| 539 | "Disable automatic garbage collection.\n" |
| 540 | ; |
| 541 | |
| 542 | static PyObject * |
| 543 | gc_disable(PyObject *self, PyObject *args) |
| 544 | { |
| 545 | |
| 546 | if (!PyArg_ParseTuple(args, ":disable")) /* check no args */ |
| 547 | return NULL; |
| 548 | |
| 549 | enabled = 0; |
| 550 | |
| 551 | Py_INCREF(Py_None); |
| 552 | return Py_None; |
| 553 | } |
| 554 | |
| 555 | static char gc_isenabled__doc__[] = |
| 556 | "isenabled() -> status\n" |
| 557 | "\n" |
| 558 | "Returns true if automatic garbage collection is enabled.\n" |
| 559 | ; |
| 560 | |
| 561 | static PyObject * |
| 562 | gc_isenabled(PyObject *self, PyObject *args) |
| 563 | { |
| 564 | |
| 565 | if (!PyArg_ParseTuple(args, ":isenabled")) /* check no args */ |
| 566 | return NULL; |
| 567 | |
| 568 | return Py_BuildValue("i", enabled); |
| 569 | } |
| 570 | |
| 571 | static char gc_collect__doc__[] = |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 572 | "collect() -> n\n" |
| 573 | "\n" |
| 574 | "Run a full collection. The number of unreachable objects is returned.\n" |
| 575 | ; |
| 576 | |
| 577 | static PyObject * |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 578 | gc_collect(PyObject *self, PyObject *args) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 579 | { |
| 580 | long n; |
| 581 | |
Fred Drake | cc1be24 | 2000-07-12 04:42:23 +0000 | [diff] [blame] | 582 | if (!PyArg_ParseTuple(args, ":collect")) /* check no args */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 583 | return NULL; |
| 584 | |
| 585 | generation = 2; |
| 586 | gc_list_merge(&generation0, &generation2); |
| 587 | gc_list_merge(&generation1, &generation2); |
| 588 | n = collect(&generation2, &generation2); |
| 589 | |
| 590 | return Py_BuildValue("i", n); |
| 591 | } |
| 592 | |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 593 | static char gc_set_debug__doc__[] = |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 594 | "set_debug(flags) -> None\n" |
| 595 | "\n" |
| 596 | "Set the garbage collection debugging flags. Debugging information is\n" |
| 597 | "written to sys.stderr.\n" |
| 598 | "\n" |
| 599 | "flags is an integer and can have the following bits turned on:\n" |
| 600 | "\n" |
| 601 | " DEBUG_STATS - Print statistics during collection.\n" |
| 602 | " DEBUG_COLLECTABLE - Print collectable objects found.\n" |
| 603 | " DEBUG_UNCOLLECTABLE - Print unreachable but uncollectable objects found.\n" |
| 604 | " DEBUG_INSTANCES - Print instance objects.\n" |
| 605 | " DEBUG_OBJECTS - Print objects other than instances.\n" |
| 606 | " DEBUG_LEAK - Debug leaking programs (everything but STATS).\n" |
| 607 | ; |
| 608 | |
| 609 | static PyObject * |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 610 | gc_set_debug(PyObject *self, PyObject *args) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 611 | { |
Fred Drake | cc1be24 | 2000-07-12 04:42:23 +0000 | [diff] [blame] | 612 | if (!PyArg_ParseTuple(args, "l:get_debug", &debug)) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 613 | return NULL; |
| 614 | |
| 615 | Py_INCREF(Py_None); |
| 616 | return Py_None; |
| 617 | } |
| 618 | |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 619 | static char gc_get_debug__doc__[] = |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 620 | "get_debug() -> flags\n" |
| 621 | "\n" |
| 622 | "Get the garbage collection debugging flags.\n" |
| 623 | ; |
| 624 | |
| 625 | static PyObject * |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 626 | gc_get_debug(PyObject *self, PyObject *args) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 627 | { |
Fred Drake | cc1be24 | 2000-07-12 04:42:23 +0000 | [diff] [blame] | 628 | if (!PyArg_ParseTuple(args, ":get_debug")) /* no args */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 629 | return NULL; |
| 630 | |
| 631 | return Py_BuildValue("i", debug); |
| 632 | } |
| 633 | |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 634 | static char gc_set_thresh__doc__[] = |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 635 | "set_threshold(threshold0, [threhold1, threshold2]) -> None\n" |
| 636 | "\n" |
| 637 | "Sets the collection thresholds. Setting threshold0 to zero disables\n" |
| 638 | "collection.\n" |
| 639 | ; |
| 640 | |
| 641 | static PyObject * |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 642 | gc_set_thresh(PyObject *self, PyObject *args) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 643 | { |
Fred Drake | cc1be24 | 2000-07-12 04:42:23 +0000 | [diff] [blame] | 644 | if (!PyArg_ParseTuple(args, "i|ii:set_threshold", &threshold0, |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 645 | &threshold1, &threshold2)) |
| 646 | return NULL; |
| 647 | |
| 648 | Py_INCREF(Py_None); |
| 649 | return Py_None; |
| 650 | } |
| 651 | |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 652 | static char gc_get_thresh__doc__[] = |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 653 | "get_threshold() -> (threshold0, threshold1, threshold2)\n" |
| 654 | "\n" |
| 655 | "Return the current collection thresholds\n" |
| 656 | ; |
| 657 | |
| 658 | static PyObject * |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 659 | gc_get_thresh(PyObject *self, PyObject *args) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 660 | { |
Fred Drake | cc1be24 | 2000-07-12 04:42:23 +0000 | [diff] [blame] | 661 | if (!PyArg_ParseTuple(args, ":get_threshold")) /* no args */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 662 | return NULL; |
| 663 | |
| 664 | return Py_BuildValue("(iii)", threshold0, threshold1, threshold2); |
| 665 | } |
| 666 | |
| 667 | |
| 668 | static char gc__doc__ [] = |
| 669 | "This module provides access to the garbage collector for reference cycles.\n" |
| 670 | "\n" |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 671 | "enable() -- Enable automatic garbage collection.\n" |
| 672 | "disable() -- Disable automatic garbage collection.\n" |
| 673 | "isenabled() -- Returns true if automatic collection is enabled.\n" |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 674 | "collect() -- Do a full collection right now.\n" |
| 675 | "set_debug() -- Set debugging flags.\n" |
| 676 | "get_debug() -- Get debugging flags.\n" |
| 677 | "set_threshold() -- Set the collection thresholds.\n" |
| 678 | "get_threshold() -- Return the current the collection thresholds.\n" |
| 679 | ; |
| 680 | |
| 681 | static PyMethodDef GcMethods[] = { |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 682 | {"enable", gc_enable, METH_VARARGS, gc_enable__doc__}, |
| 683 | {"disable", gc_disable, METH_VARARGS, gc_disable__doc__}, |
| 684 | {"isenabled", gc_isenabled, METH_VARARGS, gc_isenabled__doc__}, |
| 685 | {"set_debug", gc_set_debug, METH_VARARGS, gc_set_debug__doc__}, |
| 686 | {"get_debug", gc_get_debug, METH_VARARGS, gc_get_debug__doc__}, |
| 687 | {"set_threshold", gc_set_thresh, METH_VARARGS, gc_set_thresh__doc__}, |
| 688 | {"get_threshold", gc_get_thresh, METH_VARARGS, gc_get_thresh__doc__}, |
| 689 | {"collect", gc_collect, METH_VARARGS, gc_collect__doc__}, |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 690 | {NULL, NULL} /* Sentinel */ |
| 691 | }; |
| 692 | |
| 693 | void |
| 694 | initgc(void) |
| 695 | { |
| 696 | PyObject *m; |
| 697 | PyObject *d; |
| 698 | |
| 699 | m = Py_InitModule4("gc", |
| 700 | GcMethods, |
| 701 | gc__doc__, |
| 702 | NULL, |
| 703 | PYTHON_API_VERSION); |
| 704 | d = PyModule_GetDict(m); |
| 705 | if (garbage == NULL) { |
| 706 | garbage = PyList_New(0); |
| 707 | } |
Jeremy Hylton | b709df3 | 2000-09-01 02:47:25 +0000 | [diff] [blame] | 708 | if (gc_str == NULL) { |
| 709 | gc_str = PyString_FromString("garbage collection"); |
| 710 | } |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 711 | PyDict_SetItemString(d, "garbage", garbage); |
| 712 | PyDict_SetItemString(d, "DEBUG_STATS", |
| 713 | PyInt_FromLong(DEBUG_STATS)); |
| 714 | PyDict_SetItemString(d, "DEBUG_COLLECTABLE", |
| 715 | PyInt_FromLong(DEBUG_COLLECTABLE)); |
| 716 | PyDict_SetItemString(d, "DEBUG_UNCOLLECTABLE", |
| 717 | PyInt_FromLong(DEBUG_UNCOLLECTABLE)); |
| 718 | PyDict_SetItemString(d, "DEBUG_INSTANCES", |
| 719 | PyInt_FromLong(DEBUG_INSTANCES)); |
| 720 | PyDict_SetItemString(d, "DEBUG_OBJECTS", |
| 721 | PyInt_FromLong(DEBUG_OBJECTS)); |
| 722 | PyDict_SetItemString(d, "DEBUG_LEAK", |
| 723 | PyInt_FromLong(DEBUG_LEAK)); |
| 724 | } |
| 725 | |
| 726 | #endif /* WITH_CYCLE_GC */ |