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