Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1 | /* |
| 2 | |
| 3 | Reference Cycle Garbage Collection |
| 4 | ================================== |
| 5 | |
Neil Schemenauer | b2c2c9e | 2000-10-04 16:34:09 +0000 | [diff] [blame] | 6 | Neil Schemenauer <nas@arctrix.com> |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 7 | |
| 8 | Based on a post on the python-dev list. Ideas from Guido van Rossum, |
| 9 | Eric Tiedemann, and various others. |
| 10 | |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 11 | http://www.arctrix.com/nas/python/gc/ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 12 | http://www.python.org/pipermail/python-dev/2000-March/003869.html |
| 13 | http://www.python.org/pipermail/python-dev/2000-March/004010.html |
| 14 | http://www.python.org/pipermail/python-dev/2000-March/004022.html |
| 15 | |
| 16 | For a highlevel view of the collection process, read the collect |
| 17 | function. |
| 18 | |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 21 | #include "Python.h" |
| 22 | |
| 23 | #ifdef WITH_CYCLE_GC |
| 24 | |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 25 | /* Get an object's GC head */ |
| 26 | #define AS_GC(o) ((PyGC_Head *)(o)-1) |
| 27 | |
| 28 | /* Get the object given the GC head */ |
| 29 | #define FROM_GC(g) ((PyObject *)(((PyGC_Head *)g)+1)) |
| 30 | |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 31 | |
| 32 | /*** Global GC state ***/ |
| 33 | |
| 34 | /* linked lists of container objects */ |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 35 | PyGC_Head _PyGC_generation0 = {&_PyGC_generation0, &_PyGC_generation0, 0}; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 36 | static PyGC_Head generation1 = {&generation1, &generation1, 0}; |
| 37 | static PyGC_Head generation2 = {&generation2, &generation2, 0}; |
| 38 | static int generation = 0; /* current generation being collected */ |
| 39 | |
| 40 | /* collection frequencies, XXX tune these */ |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 41 | static int enabled = 1; /* automatic collection enabled? */ |
Jeremy Hylton | 3263dc2b | 2000-09-05 15:44:50 +0000 | [diff] [blame] | 42 | static int threshold0 = 700; /* net new containers before collection */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 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 | |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 49 | /* true if we are currently running the collector */ |
| 50 | static int collecting; |
| 51 | |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 52 | /* set for debugging information */ |
| 53 | #define DEBUG_STATS (1<<0) /* print collection statistics */ |
| 54 | #define DEBUG_COLLECTABLE (1<<1) /* print collectable objects */ |
| 55 | #define DEBUG_UNCOLLECTABLE (1<<2) /* print uncollectable objects */ |
| 56 | #define DEBUG_INSTANCES (1<<3) /* print instances */ |
| 57 | #define DEBUG_OBJECTS (1<<4) /* print other objects */ |
Neil Schemenauer | 544de1e | 2000-09-22 15:22:38 +0000 | [diff] [blame] | 58 | #define DEBUG_SAVEALL (1<<5) /* save all garbage in gc.garbage */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 59 | #define DEBUG_LEAK DEBUG_COLLECTABLE | \ |
| 60 | DEBUG_UNCOLLECTABLE | \ |
| 61 | DEBUG_INSTANCES | \ |
Neil Schemenauer | 544de1e | 2000-09-22 15:22:38 +0000 | [diff] [blame] | 62 | DEBUG_OBJECTS | \ |
| 63 | DEBUG_SAVEALL |
Jeremy Hylton | b709df3 | 2000-09-01 02:47:25 +0000 | [diff] [blame] | 64 | static int debug; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 65 | |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 66 | /* Special gc_refs value */ |
| 67 | #define GC_MOVED -123 |
| 68 | |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 69 | /* list of uncollectable objects */ |
| 70 | static PyObject *garbage; |
| 71 | |
Jeremy Hylton | b709df3 | 2000-09-01 02:47:25 +0000 | [diff] [blame] | 72 | /* Python string to use if unhandled exception occurs */ |
| 73 | static PyObject *gc_str; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 74 | |
| 75 | /*** list functions ***/ |
| 76 | |
| 77 | static void |
| 78 | gc_list_init(PyGC_Head *list) |
| 79 | { |
| 80 | list->gc_prev = list; |
| 81 | list->gc_next = list; |
| 82 | } |
| 83 | |
| 84 | static void |
| 85 | gc_list_append(PyGC_Head *node, PyGC_Head *list) |
| 86 | { |
| 87 | node->gc_next = list; |
| 88 | node->gc_prev = list->gc_prev; |
| 89 | node->gc_prev->gc_next = node; |
| 90 | list->gc_prev = node; |
| 91 | } |
| 92 | |
| 93 | static void |
| 94 | gc_list_remove(PyGC_Head *node) |
| 95 | { |
| 96 | node->gc_prev->gc_next = node->gc_next; |
| 97 | node->gc_next->gc_prev = node->gc_prev; |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 98 | node->gc_next = NULL; /* object is not currently tracked */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | static void |
| 102 | gc_list_move(PyGC_Head *from, PyGC_Head *to) |
| 103 | { |
| 104 | if (from->gc_next == from) { |
| 105 | /* empty from list */ |
| 106 | gc_list_init(to); |
Neil Schemenauer | 544de1e | 2000-09-22 15:22:38 +0000 | [diff] [blame] | 107 | } |
| 108 | else { |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 109 | to->gc_next = from->gc_next; |
| 110 | to->gc_next->gc_prev = to; |
| 111 | to->gc_prev = from->gc_prev; |
| 112 | to->gc_prev->gc_next = to; |
| 113 | } |
| 114 | gc_list_init(from); |
| 115 | } |
| 116 | |
| 117 | /* append a list onto another list, from becomes an empty list */ |
| 118 | static void |
| 119 | gc_list_merge(PyGC_Head *from, PyGC_Head *to) |
| 120 | { |
| 121 | PyGC_Head *tail; |
| 122 | if (from->gc_next != from) { |
| 123 | tail = to->gc_prev; |
| 124 | tail->gc_next = from->gc_next; |
| 125 | tail->gc_next->gc_prev = tail; |
| 126 | to->gc_prev = from->gc_prev; |
| 127 | to->gc_prev->gc_next = to; |
| 128 | } |
| 129 | gc_list_init(from); |
| 130 | } |
| 131 | |
| 132 | static long |
| 133 | gc_list_size(PyGC_Head *list) |
| 134 | { |
| 135 | PyGC_Head *gc; |
| 136 | long n = 0; |
| 137 | for (gc = list->gc_next; gc != list; gc = gc->gc_next) { |
| 138 | n++; |
| 139 | } |
| 140 | return n; |
| 141 | } |
| 142 | |
| 143 | /*** end of list stuff ***/ |
| 144 | |
| 145 | |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 146 | |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 147 | /* Set all gc_refs = ob_refcnt */ |
| 148 | static void |
| 149 | update_refs(PyGC_Head *containers) |
| 150 | { |
| 151 | PyGC_Head *gc = containers->gc_next; |
| 152 | for (; gc != containers; gc=gc->gc_next) { |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 153 | gc->gc_refs = FROM_GC(gc)->ob_refcnt; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
| 157 | static int |
| 158 | visit_decref(PyObject *op, void *data) |
| 159 | { |
| 160 | if (op && PyObject_IS_GC(op)) { |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 161 | PyGC_Head *gc = AS_GC(op); |
| 162 | if (gc->gc_next != NULL) |
| 163 | AS_GC(op)->gc_refs--; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 164 | } |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | /* Subtract internal references from gc_refs */ |
| 169 | static void |
| 170 | subtract_refs(PyGC_Head *containers) |
| 171 | { |
| 172 | traverseproc traverse; |
| 173 | PyGC_Head *gc = containers->gc_next; |
| 174 | for (; gc != containers; gc=gc->gc_next) { |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 175 | traverse = FROM_GC(gc)->ob_type->tp_traverse; |
| 176 | (void) traverse(FROM_GC(gc), |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 177 | (visitproc)visit_decref, |
| 178 | NULL); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | /* Append objects with gc_refs > 0 to roots list */ |
| 183 | static void |
| 184 | move_roots(PyGC_Head *containers, PyGC_Head *roots) |
| 185 | { |
| 186 | PyGC_Head *next; |
| 187 | PyGC_Head *gc = containers->gc_next; |
| 188 | while (gc != containers) { |
| 189 | next = gc->gc_next; |
| 190 | if (gc->gc_refs > 0) { |
| 191 | gc_list_remove(gc); |
| 192 | gc_list_append(gc, roots); |
| 193 | gc->gc_refs = GC_MOVED; |
| 194 | } |
| 195 | gc = next; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | static int |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 200 | visit_move(PyObject *op, PyGC_Head *tolist) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 201 | { |
| 202 | if (PyObject_IS_GC(op)) { |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 203 | PyGC_Head *gc = AS_GC(op); |
| 204 | if (gc->gc_next != NULL && gc->gc_refs != GC_MOVED) { |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 205 | gc_list_remove(gc); |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 206 | gc_list_append(gc, tolist); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 207 | gc->gc_refs = GC_MOVED; |
| 208 | } |
| 209 | } |
| 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | /* Move objects referenced from reachable to reachable set. */ |
| 214 | static void |
| 215 | move_root_reachable(PyGC_Head *reachable) |
| 216 | { |
| 217 | traverseproc traverse; |
| 218 | PyGC_Head *gc = reachable->gc_next; |
| 219 | for (; gc != reachable; gc=gc->gc_next) { |
| 220 | /* careful, reachable list is growing here */ |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 221 | PyObject *op = FROM_GC(gc); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 222 | traverse = op->ob_type->tp_traverse; |
| 223 | (void) traverse(op, |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 224 | (visitproc)visit_move, |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 225 | (void *)reachable); |
| 226 | } |
| 227 | } |
| 228 | |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 229 | /* Move all objects with finalizers (instances with __del__) */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 230 | static void |
| 231 | move_finalizers(PyGC_Head *unreachable, PyGC_Head *finalizers) |
| 232 | { |
| 233 | PyGC_Head *next; |
| 234 | PyGC_Head *gc = unreachable->gc_next; |
Jeremy Hylton | 0625777 | 2000-08-31 15:10:24 +0000 | [diff] [blame] | 235 | static PyObject *delstr = NULL; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 236 | if (delstr == NULL) { |
| 237 | delstr = PyString_InternFromString("__del__"); |
Jeremy Hylton | 0625777 | 2000-08-31 15:10:24 +0000 | [diff] [blame] | 238 | if (delstr == NULL) |
| 239 | Py_FatalError("PyGC: can't initialize __del__ string"); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 240 | } |
| 241 | for (; gc != unreachable; gc=next) { |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 242 | PyObject *op = FROM_GC(gc); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 243 | next = gc->gc_next; |
| 244 | if (PyInstance_Check(op) && PyObject_HasAttr(op, delstr)) { |
| 245 | gc_list_remove(gc); |
| 246 | gc_list_append(gc, finalizers); |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 251 | /* Move objects referenced from roots to roots */ |
| 252 | static void |
| 253 | move_finalizer_reachable(PyGC_Head *finalizers) |
| 254 | { |
| 255 | traverseproc traverse; |
| 256 | PyGC_Head *gc = finalizers->gc_next; |
| 257 | for (; gc != finalizers; gc=gc->gc_next) { |
| 258 | /* careful, finalizers list is growing here */ |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 259 | traverse = FROM_GC(gc)->ob_type->tp_traverse; |
| 260 | (void) traverse(FROM_GC(gc), |
| 261 | (visitproc)visit_move, |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 262 | (void *)finalizers); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | static void |
Jeremy Hylton | 0625777 | 2000-08-31 15:10:24 +0000 | [diff] [blame] | 267 | debug_instance(char *msg, PyInstanceObject *inst) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 268 | { |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 269 | char *cname; |
| 270 | /* be careful not to create new dictionaries */ |
| 271 | PyObject *classname = inst->in_class->cl_name; |
| 272 | if (classname != NULL && PyString_Check(classname)) |
| 273 | cname = PyString_AsString(classname); |
| 274 | else |
| 275 | cname = "?"; |
Jeremy Hylton | 0625777 | 2000-08-31 15:10:24 +0000 | [diff] [blame] | 276 | PySys_WriteStderr("gc: %.100s <%.100s instance at %p>\n", |
| 277 | msg, cname, inst); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | static void |
Jeremy Hylton | 0625777 | 2000-08-31 15:10:24 +0000 | [diff] [blame] | 281 | debug_cycle(char *msg, PyObject *op) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 282 | { |
| 283 | if ((debug & DEBUG_INSTANCES) && PyInstance_Check(op)) { |
Jeremy Hylton | 0625777 | 2000-08-31 15:10:24 +0000 | [diff] [blame] | 284 | debug_instance(msg, (PyInstanceObject *)op); |
Neil Schemenauer | 544de1e | 2000-09-22 15:22:38 +0000 | [diff] [blame] | 285 | } |
| 286 | else if (debug & DEBUG_OBJECTS) { |
Jeremy Hylton | 0625777 | 2000-08-31 15:10:24 +0000 | [diff] [blame] | 287 | PySys_WriteStderr("gc: %.100s <%.100s %p>\n", |
| 288 | msg, op->ob_type->tp_name, op); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | |
| 292 | /* Handle uncollectable garbage (cycles with finalizers). */ |
| 293 | static void |
| 294 | handle_finalizers(PyGC_Head *finalizers, PyGC_Head *old) |
| 295 | { |
| 296 | PyGC_Head *gc; |
| 297 | if (garbage == NULL) { |
| 298 | garbage = PyList_New(0); |
| 299 | } |
| 300 | for (gc = finalizers->gc_next; gc != finalizers; |
| 301 | gc = finalizers->gc_next) { |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 302 | PyObject *op = FROM_GC(gc); |
Neil Schemenauer | 544de1e | 2000-09-22 15:22:38 +0000 | [diff] [blame] | 303 | if ((debug & DEBUG_SAVEALL) || PyInstance_Check(op)) { |
| 304 | /* If SAVEALL is not set then just append |
| 305 | * instances to the list of garbage. We assume |
| 306 | * that all objects in the finalizers list are |
| 307 | * reachable from instances. */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 308 | PyList_Append(garbage, op); |
| 309 | } |
Neil Schemenauer | 544de1e | 2000-09-22 15:22:38 +0000 | [diff] [blame] | 310 | /* object is now reachable again */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 311 | gc_list_remove(gc); |
| 312 | gc_list_append(gc, old); |
| 313 | } |
| 314 | } |
| 315 | |
Neil Schemenauer | 544de1e | 2000-09-22 15:22:38 +0000 | [diff] [blame] | 316 | /* Break reference cycles by clearing the containers involved. This is |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 317 | * tricky business as the lists can be changing and we don't know which |
| 318 | * objects may be freed. It is possible I screwed something up here. */ |
| 319 | static void |
| 320 | delete_garbage(PyGC_Head *unreachable, PyGC_Head *old) |
| 321 | { |
| 322 | inquiry clear; |
| 323 | |
| 324 | while (unreachable->gc_next != unreachable) { |
| 325 | PyGC_Head *gc = unreachable->gc_next; |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 326 | PyObject *op = FROM_GC(gc); |
Neil Schemenauer | 544de1e | 2000-09-22 15:22:38 +0000 | [diff] [blame] | 327 | if (debug & DEBUG_SAVEALL) { |
| 328 | PyList_Append(garbage, op); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 329 | } |
Neil Schemenauer | 544de1e | 2000-09-22 15:22:38 +0000 | [diff] [blame] | 330 | else { |
| 331 | if ((clear = op->ob_type->tp_clear) != NULL) { |
| 332 | Py_INCREF(op); |
| 333 | clear((PyObject *)op); |
| 334 | Py_DECREF(op); |
| 335 | } |
| 336 | } |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 337 | if (unreachable->gc_next == gc) { |
Neil Schemenauer | 544de1e | 2000-09-22 15:22:38 +0000 | [diff] [blame] | 338 | /* object is still alive, move it, it may die later */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 339 | gc_list_remove(gc); |
| 340 | gc_list_append(gc, old); |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | /* This is the main function. Read this to understand how the |
| 346 | * collection process works. */ |
| 347 | static long |
| 348 | collect(PyGC_Head *young, PyGC_Head *old) |
| 349 | { |
| 350 | long n = 0; |
| 351 | long m = 0; |
| 352 | PyGC_Head reachable; |
| 353 | PyGC_Head unreachable; |
| 354 | PyGC_Head finalizers; |
| 355 | PyGC_Head *gc; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 356 | |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 357 | if (debug & DEBUG_STATS) { |
Jeremy Hylton | 0625777 | 2000-08-31 15:10:24 +0000 | [diff] [blame] | 358 | PySys_WriteStderr( |
| 359 | "gc: collecting generation %d...\n" |
| 360 | "gc: objects in each generation: %ld %ld %ld\n", |
| 361 | generation, |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 362 | gc_list_size(&_PyGC_generation0), |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 363 | gc_list_size(&generation1), |
| 364 | gc_list_size(&generation2)); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | /* Using ob_refcnt and gc_refs, calculate which objects in the |
| 368 | * container set are reachable from outside the set (ie. have a |
| 369 | * refcount greater than 0 when all the references within the |
| 370 | * set are taken into account */ |
| 371 | update_refs(young); |
| 372 | subtract_refs(young); |
| 373 | |
| 374 | /* Move everything reachable from outside the set into the |
| 375 | * reachable set (ie. gc_refs > 0). Next, move everything |
| 376 | * reachable from objects in the reachable set. */ |
| 377 | gc_list_init(&reachable); |
| 378 | move_roots(young, &reachable); |
| 379 | move_root_reachable(&reachable); |
| 380 | |
| 381 | /* move unreachable objects to a temporary list, new objects can be |
| 382 | * allocated after this point */ |
| 383 | gc_list_init(&unreachable); |
| 384 | gc_list_move(young, &unreachable); |
| 385 | |
| 386 | /* move reachable objects to next generation */ |
| 387 | gc_list_merge(&reachable, old); |
| 388 | |
| 389 | /* Move objects reachable from finalizers, we can't safely delete |
| 390 | * them. Python programmers should take care not to create such |
| 391 | * things. For Python finalizers means instance objects with |
| 392 | * __del__ methods. */ |
| 393 | gc_list_init(&finalizers); |
| 394 | move_finalizers(&unreachable, &finalizers); |
| 395 | move_finalizer_reachable(&finalizers); |
| 396 | |
| 397 | /* Collect statistics on collectable objects found and print |
| 398 | * debugging information. */ |
| 399 | for (gc = unreachable.gc_next; gc != &unreachable; |
| 400 | gc = gc->gc_next) { |
| 401 | m++; |
Jeremy Hylton | 0625777 | 2000-08-31 15:10:24 +0000 | [diff] [blame] | 402 | if (debug & DEBUG_COLLECTABLE) { |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 403 | debug_cycle("collectable", FROM_GC(gc)); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 404 | } |
| 405 | } |
| 406 | /* call tp_clear on objects in the collectable set. This will cause |
| 407 | * the reference cycles to be broken. It may also cause some objects in |
| 408 | * finalizers to be freed */ |
| 409 | delete_garbage(&unreachable, old); |
| 410 | |
| 411 | /* Collect statistics on uncollectable objects found and print |
| 412 | * debugging information. */ |
| 413 | for (gc = finalizers.gc_next; gc != &finalizers; |
| 414 | gc = gc->gc_next) { |
| 415 | n++; |
Jeremy Hylton | 0625777 | 2000-08-31 15:10:24 +0000 | [diff] [blame] | 416 | if (debug & DEBUG_UNCOLLECTABLE) { |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 417 | debug_cycle("uncollectable", FROM_GC(gc)); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 418 | } |
| 419 | } |
Jeremy Hylton | 0625777 | 2000-08-31 15:10:24 +0000 | [diff] [blame] | 420 | if (debug & DEBUG_STATS) { |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 421 | if (m == 0 && n == 0) { |
Jeremy Hylton | 0625777 | 2000-08-31 15:10:24 +0000 | [diff] [blame] | 422 | PySys_WriteStderr("gc: done.\n"); |
Neil Schemenauer | 544de1e | 2000-09-22 15:22:38 +0000 | [diff] [blame] | 423 | } |
| 424 | else { |
Jeremy Hylton | 0625777 | 2000-08-31 15:10:24 +0000 | [diff] [blame] | 425 | PySys_WriteStderr( |
| 426 | "gc: done, %ld unreachable, %ld uncollectable.\n", |
| 427 | n+m, n); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 428 | } |
| 429 | } |
| 430 | |
| 431 | /* Append instances in the uncollectable set to a Python |
| 432 | * reachable list of garbage. The programmer has to deal with |
| 433 | * this if they insist on creating this type of structure. */ |
| 434 | handle_finalizers(&finalizers, old); |
| 435 | |
Jeremy Hylton | b709df3 | 2000-09-01 02:47:25 +0000 | [diff] [blame] | 436 | if (PyErr_Occurred()) { |
Neil Schemenauer | 544de1e | 2000-09-22 15:22:38 +0000 | [diff] [blame] | 437 | if (gc_str == NULL) { |
| 438 | gc_str = PyString_FromString("garbage collection"); |
| 439 | } |
Jeremy Hylton | b709df3 | 2000-09-01 02:47:25 +0000 | [diff] [blame] | 440 | PyErr_WriteUnraisable(gc_str); |
| 441 | Py_FatalError("unexpected exception during garbage collection"); |
| 442 | } |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 443 | allocated = 0; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 444 | return n+m; |
| 445 | } |
| 446 | |
| 447 | static long |
| 448 | collect_generations(void) |
| 449 | { |
| 450 | static long collections0 = 0; |
| 451 | static long collections1 = 0; |
Vladimir Marangozov | b16714b | 2000-07-10 05:37:39 +0000 | [diff] [blame] | 452 | long n = 0; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 453 | |
| 454 | |
| 455 | if (collections1 > threshold2) { |
| 456 | generation = 2; |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 457 | gc_list_merge(&_PyGC_generation0, &generation2); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 458 | gc_list_merge(&generation1, &generation2); |
| 459 | if (generation2.gc_next != &generation2) { |
| 460 | n = collect(&generation2, &generation2); |
| 461 | } |
| 462 | collections1 = 0; |
Neil Schemenauer | 544de1e | 2000-09-22 15:22:38 +0000 | [diff] [blame] | 463 | } |
| 464 | else if (collections0 > threshold1) { |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 465 | generation = 1; |
| 466 | collections1++; |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 467 | gc_list_merge(&_PyGC_generation0, &generation1); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 468 | if (generation1.gc_next != &generation1) { |
| 469 | n = collect(&generation1, &generation2); |
| 470 | } |
| 471 | collections0 = 0; |
Neil Schemenauer | 544de1e | 2000-09-22 15:22:38 +0000 | [diff] [blame] | 472 | } |
| 473 | else { |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 474 | generation = 0; |
| 475 | collections0++; |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 476 | if (_PyGC_generation0.gc_next != &_PyGC_generation0) { |
| 477 | n = collect(&_PyGC_generation0, &generation1); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 478 | } |
| 479 | } |
| 480 | return n; |
| 481 | } |
| 482 | |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 483 | static char gc_enable__doc__[] = |
| 484 | "enable() -> None\n" |
| 485 | "\n" |
| 486 | "Enable automatic garbage collection.\n" |
| 487 | ; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 488 | |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 489 | static PyObject * |
| 490 | gc_enable(PyObject *self, PyObject *args) |
| 491 | { |
| 492 | |
| 493 | if (!PyArg_ParseTuple(args, ":enable")) /* check no args */ |
| 494 | return NULL; |
| 495 | |
| 496 | enabled = 1; |
| 497 | |
| 498 | Py_INCREF(Py_None); |
| 499 | return Py_None; |
| 500 | } |
| 501 | |
| 502 | static char gc_disable__doc__[] = |
| 503 | "disable() -> None\n" |
| 504 | "\n" |
| 505 | "Disable automatic garbage collection.\n" |
| 506 | ; |
| 507 | |
| 508 | static PyObject * |
| 509 | gc_disable(PyObject *self, PyObject *args) |
| 510 | { |
| 511 | |
| 512 | if (!PyArg_ParseTuple(args, ":disable")) /* check no args */ |
| 513 | return NULL; |
| 514 | |
| 515 | enabled = 0; |
| 516 | |
| 517 | Py_INCREF(Py_None); |
| 518 | return Py_None; |
| 519 | } |
| 520 | |
| 521 | static char gc_isenabled__doc__[] = |
| 522 | "isenabled() -> status\n" |
| 523 | "\n" |
| 524 | "Returns true if automatic garbage collection is enabled.\n" |
| 525 | ; |
| 526 | |
| 527 | static PyObject * |
| 528 | gc_isenabled(PyObject *self, PyObject *args) |
| 529 | { |
| 530 | |
| 531 | if (!PyArg_ParseTuple(args, ":isenabled")) /* check no args */ |
| 532 | return NULL; |
| 533 | |
| 534 | return Py_BuildValue("i", enabled); |
| 535 | } |
| 536 | |
| 537 | static char gc_collect__doc__[] = |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 538 | "collect() -> n\n" |
| 539 | "\n" |
| 540 | "Run a full collection. The number of unreachable objects is returned.\n" |
| 541 | ; |
| 542 | |
| 543 | static PyObject * |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 544 | gc_collect(PyObject *self, PyObject *args) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 545 | { |
| 546 | long n; |
| 547 | |
Fred Drake | cc1be24 | 2000-07-12 04:42:23 +0000 | [diff] [blame] | 548 | if (!PyArg_ParseTuple(args, ":collect")) /* check no args */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 549 | return NULL; |
| 550 | |
| 551 | generation = 2; |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 552 | gc_list_merge(&_PyGC_generation0, &generation2); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 553 | gc_list_merge(&generation1, &generation2); |
| 554 | n = collect(&generation2, &generation2); |
| 555 | |
Neil Schemenauer | 7760cff | 2000-09-22 22:35:36 +0000 | [diff] [blame] | 556 | return Py_BuildValue("l", n); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 557 | } |
| 558 | |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 559 | static char gc_set_debug__doc__[] = |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 560 | "set_debug(flags) -> None\n" |
| 561 | "\n" |
| 562 | "Set the garbage collection debugging flags. Debugging information is\n" |
| 563 | "written to sys.stderr.\n" |
| 564 | "\n" |
| 565 | "flags is an integer and can have the following bits turned on:\n" |
| 566 | "\n" |
| 567 | " DEBUG_STATS - Print statistics during collection.\n" |
| 568 | " DEBUG_COLLECTABLE - Print collectable objects found.\n" |
| 569 | " DEBUG_UNCOLLECTABLE - Print unreachable but uncollectable objects found.\n" |
| 570 | " DEBUG_INSTANCES - Print instance objects.\n" |
| 571 | " DEBUG_OBJECTS - Print objects other than instances.\n" |
Neil Schemenauer | 544de1e | 2000-09-22 15:22:38 +0000 | [diff] [blame] | 572 | " DEBUG_SAVEALL - Save objects to gc.garbage rather than freeing them.\n" |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 573 | " DEBUG_LEAK - Debug leaking programs (everything but STATS).\n" |
| 574 | ; |
| 575 | |
| 576 | static PyObject * |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 577 | gc_set_debug(PyObject *self, PyObject *args) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 578 | { |
Neil Schemenauer | 7760cff | 2000-09-22 22:35:36 +0000 | [diff] [blame] | 579 | if (!PyArg_ParseTuple(args, "i:set_debug", &debug)) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 580 | return NULL; |
| 581 | |
| 582 | Py_INCREF(Py_None); |
| 583 | return Py_None; |
| 584 | } |
| 585 | |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 586 | static char gc_get_debug__doc__[] = |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 587 | "get_debug() -> flags\n" |
| 588 | "\n" |
| 589 | "Get the garbage collection debugging flags.\n" |
| 590 | ; |
| 591 | |
| 592 | static PyObject * |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 593 | gc_get_debug(PyObject *self, PyObject *args) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 594 | { |
Fred Drake | cc1be24 | 2000-07-12 04:42:23 +0000 | [diff] [blame] | 595 | if (!PyArg_ParseTuple(args, ":get_debug")) /* no args */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 596 | return NULL; |
| 597 | |
| 598 | return Py_BuildValue("i", debug); |
| 599 | } |
| 600 | |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 601 | static char gc_set_thresh__doc__[] = |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 602 | "set_threshold(threshold0, [threhold1, threshold2]) -> None\n" |
| 603 | "\n" |
| 604 | "Sets the collection thresholds. Setting threshold0 to zero disables\n" |
| 605 | "collection.\n" |
| 606 | ; |
| 607 | |
| 608 | static PyObject * |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 609 | gc_set_thresh(PyObject *self, PyObject *args) |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 610 | { |
Fred Drake | cc1be24 | 2000-07-12 04:42:23 +0000 | [diff] [blame] | 611 | if (!PyArg_ParseTuple(args, "i|ii:set_threshold", &threshold0, |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 612 | &threshold1, &threshold2)) |
| 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_thresh__doc__[] = |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 620 | "get_threshold() -> (threshold0, threshold1, threshold2)\n" |
| 621 | "\n" |
| 622 | "Return the current collection thresholds\n" |
| 623 | ; |
| 624 | |
| 625 | static PyObject * |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 626 | gc_get_thresh(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_threshold")) /* no args */ |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 629 | return NULL; |
| 630 | |
| 631 | return Py_BuildValue("(iii)", threshold0, threshold1, threshold2); |
| 632 | } |
| 633 | |
Neil Schemenauer | 48c7034 | 2001-08-09 15:38:31 +0000 | [diff] [blame] | 634 | static int |
Neil Schemenauer | 17e7be6 | 2001-08-10 14:46:47 +0000 | [diff] [blame] | 635 | referentsvisit(PyObject* obj, PyObject *objs) |
Neil Schemenauer | 48c7034 | 2001-08-09 15:38:31 +0000 | [diff] [blame] | 636 | { |
Neil Schemenauer | 17e7be6 | 2001-08-10 14:46:47 +0000 | [diff] [blame] | 637 | if (PySequence_Contains(objs, obj)) { |
Neil Schemenauer | 48c7034 | 2001-08-09 15:38:31 +0000 | [diff] [blame] | 638 | return 1; |
| 639 | } |
| 640 | return 0; |
| 641 | } |
| 642 | |
Neil Schemenauer | 17e7be6 | 2001-08-10 14:46:47 +0000 | [diff] [blame] | 643 | static int |
Neil Schemenauer | 48c7034 | 2001-08-09 15:38:31 +0000 | [diff] [blame] | 644 | gc_referents_for(PyObject *objs, PyGC_Head *list, PyObject *resultlist) |
| 645 | { |
| 646 | PyGC_Head *gc; |
| 647 | PyObject *obj; |
| 648 | traverseproc traverse; |
Neil Schemenauer | 17e7be6 | 2001-08-10 14:46:47 +0000 | [diff] [blame] | 649 | for (gc = list->gc_next; gc != list; gc = gc->gc_next) { |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 650 | obj = FROM_GC(gc); |
Neil Schemenauer | 48c7034 | 2001-08-09 15:38:31 +0000 | [diff] [blame] | 651 | traverse = obj->ob_type->tp_traverse; |
| 652 | if (obj == objs || obj == resultlist) |
| 653 | continue; |
Neil Schemenauer | 17e7be6 | 2001-08-10 14:46:47 +0000 | [diff] [blame] | 654 | if (traverse(obj, (visitproc)referentsvisit, objs)) { |
| 655 | if (PyList_Append(resultlist, obj) < 0) |
| 656 | return 0; /* error */ |
Neil Schemenauer | 48c7034 | 2001-08-09 15:38:31 +0000 | [diff] [blame] | 657 | } |
| 658 | } |
Neil Schemenauer | 17e7be6 | 2001-08-10 14:46:47 +0000 | [diff] [blame] | 659 | return 1; /* no error */ |
Neil Schemenauer | 48c7034 | 2001-08-09 15:38:31 +0000 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | static char gc_get_referents__doc__[]= |
| 663 | "get_referents(*objs) -> list\n\ |
| 664 | Return the list of objects that directly refer to any of objs."; |
| 665 | |
Neil Schemenauer | 17e7be6 | 2001-08-10 14:46:47 +0000 | [diff] [blame] | 666 | static PyObject * |
| 667 | gc_get_referents(PyObject *self, PyObject *args) |
Neil Schemenauer | 48c7034 | 2001-08-09 15:38:31 +0000 | [diff] [blame] | 668 | { |
| 669 | PyObject *result = PyList_New(0); |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 670 | if (!(gc_referents_for(args, &_PyGC_generation0, result) && |
Neil Schemenauer | 17e7be6 | 2001-08-10 14:46:47 +0000 | [diff] [blame] | 671 | gc_referents_for(args, &generation1, result) && |
| 672 | gc_referents_for(args, &generation2, result))) { |
| 673 | Py_DECREF(result); |
| 674 | return NULL; |
| 675 | } |
Neil Schemenauer | 48c7034 | 2001-08-09 15:38:31 +0000 | [diff] [blame] | 676 | return result; |
| 677 | } |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 678 | |
Neil Schemenauer | c7c8d8e | 2001-08-09 15:58:59 +0000 | [diff] [blame] | 679 | static char gc_get_objects__doc__[] = |
| 680 | "get_objects() -> [...]\n" |
| 681 | "\n" |
| 682 | "Return a list of objects tracked by the collector (excluding the list\n" |
| 683 | "returned).\n" |
| 684 | ; |
| 685 | |
| 686 | /* appending objects in a GC list to a Python list */ |
| 687 | static void |
| 688 | append_objects(PyObject *py_list, PyGC_Head *gc_list) |
| 689 | { |
| 690 | PyGC_Head *gc; |
| 691 | for (gc = gc_list->gc_next; gc != gc_list; gc = gc->gc_next) { |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 692 | PyObject *op = FROM_GC(gc); |
Neil Schemenauer | c7c8d8e | 2001-08-09 15:58:59 +0000 | [diff] [blame] | 693 | if (op != py_list) { |
| 694 | Py_INCREF(op); |
| 695 | PyList_Append(py_list, op); |
| 696 | } |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | static PyObject * |
| 701 | gc_get_objects(PyObject *self, PyObject *args) |
| 702 | { |
| 703 | PyObject* result; |
| 704 | |
| 705 | if (!PyArg_ParseTuple(args, ":get_objects")) /* check no args */ |
| 706 | return NULL; |
| 707 | result = PyList_New(0); |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 708 | append_objects(result, &_PyGC_generation0); |
Neil Schemenauer | c7c8d8e | 2001-08-09 15:58:59 +0000 | [diff] [blame] | 709 | append_objects(result, &generation1); |
| 710 | append_objects(result, &generation2); |
| 711 | return result; |
| 712 | } |
| 713 | |
| 714 | |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 715 | static char gc__doc__ [] = |
| 716 | "This module provides access to the garbage collector for reference cycles.\n" |
| 717 | "\n" |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 718 | "enable() -- Enable automatic garbage collection.\n" |
| 719 | "disable() -- Disable automatic garbage collection.\n" |
| 720 | "isenabled() -- Returns true if automatic collection is enabled.\n" |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 721 | "collect() -- Do a full collection right now.\n" |
| 722 | "set_debug() -- Set debugging flags.\n" |
| 723 | "get_debug() -- Get debugging flags.\n" |
| 724 | "set_threshold() -- Set the collection thresholds.\n" |
| 725 | "get_threshold() -- Return the current the collection thresholds.\n" |
Neil Schemenauer | c7c8d8e | 2001-08-09 15:58:59 +0000 | [diff] [blame] | 726 | "get_objects() -- Return a list of all objects tracked by the collector.\n" |
Neil Schemenauer | 48c7034 | 2001-08-09 15:38:31 +0000 | [diff] [blame] | 727 | "get_referents() -- Return the list of objects that refer to an object.\n" |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 728 | ; |
| 729 | |
| 730 | static PyMethodDef GcMethods[] = { |
Neil Schemenauer | 544de1e | 2000-09-22 15:22:38 +0000 | [diff] [blame] | 731 | {"enable", gc_enable, METH_VARARGS, gc_enable__doc__}, |
| 732 | {"disable", gc_disable, METH_VARARGS, gc_disable__doc__}, |
Vladimir Marangozov | f9d20c3 | 2000-08-06 22:45:31 +0000 | [diff] [blame] | 733 | {"isenabled", gc_isenabled, METH_VARARGS, gc_isenabled__doc__}, |
| 734 | {"set_debug", gc_set_debug, METH_VARARGS, gc_set_debug__doc__}, |
| 735 | {"get_debug", gc_get_debug, METH_VARARGS, gc_get_debug__doc__}, |
| 736 | {"set_threshold", gc_set_thresh, METH_VARARGS, gc_set_thresh__doc__}, |
| 737 | {"get_threshold", gc_get_thresh, METH_VARARGS, gc_get_thresh__doc__}, |
Neil Schemenauer | 544de1e | 2000-09-22 15:22:38 +0000 | [diff] [blame] | 738 | {"collect", gc_collect, METH_VARARGS, gc_collect__doc__}, |
Neil Schemenauer | c7c8d8e | 2001-08-09 15:58:59 +0000 | [diff] [blame] | 739 | {"get_objects", gc_get_objects,METH_VARARGS, gc_get_objects__doc__}, |
Neil Schemenauer | 48c7034 | 2001-08-09 15:38:31 +0000 | [diff] [blame] | 740 | {"get_referents", gc_get_referents, METH_VARARGS, |
| 741 | gc_get_referents__doc__}, |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 742 | {NULL, NULL} /* Sentinel */ |
| 743 | }; |
| 744 | |
| 745 | void |
| 746 | initgc(void) |
| 747 | { |
| 748 | PyObject *m; |
| 749 | PyObject *d; |
| 750 | |
| 751 | m = Py_InitModule4("gc", |
| 752 | GcMethods, |
| 753 | gc__doc__, |
| 754 | NULL, |
| 755 | PYTHON_API_VERSION); |
| 756 | d = PyModule_GetDict(m); |
| 757 | if (garbage == NULL) { |
| 758 | garbage = PyList_New(0); |
| 759 | } |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 760 | PyDict_SetItemString(d, "garbage", garbage); |
| 761 | PyDict_SetItemString(d, "DEBUG_STATS", |
| 762 | PyInt_FromLong(DEBUG_STATS)); |
| 763 | PyDict_SetItemString(d, "DEBUG_COLLECTABLE", |
| 764 | PyInt_FromLong(DEBUG_COLLECTABLE)); |
| 765 | PyDict_SetItemString(d, "DEBUG_UNCOLLECTABLE", |
| 766 | PyInt_FromLong(DEBUG_UNCOLLECTABLE)); |
| 767 | PyDict_SetItemString(d, "DEBUG_INSTANCES", |
| 768 | PyInt_FromLong(DEBUG_INSTANCES)); |
| 769 | PyDict_SetItemString(d, "DEBUG_OBJECTS", |
| 770 | PyInt_FromLong(DEBUG_OBJECTS)); |
Neil Schemenauer | 544de1e | 2000-09-22 15:22:38 +0000 | [diff] [blame] | 771 | PyDict_SetItemString(d, "DEBUG_SAVEALL", |
| 772 | PyInt_FromLong(DEBUG_SAVEALL)); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 773 | PyDict_SetItemString(d, "DEBUG_LEAK", |
| 774 | PyInt_FromLong(DEBUG_LEAK)); |
| 775 | } |
| 776 | |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 777 | /* for debugging */ |
| 778 | void _PyGC_Dump(PyGC_Head *g) |
| 779 | { |
| 780 | _PyObject_Dump(FROM_GC(g)); |
| 781 | } |
| 782 | |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 783 | #endif /* WITH_CYCLE_GC */ |
Neil Schemenauer | 43411b5 | 2001-08-30 00:05:51 +0000 | [diff] [blame] | 784 | |
| 785 | /* extension modules might be compiled with GC support so these |
| 786 | functions must always be available */ |
| 787 | |
| 788 | void |
| 789 | _PyObject_GC_Track(PyObject *op) |
| 790 | { |
| 791 | _PyObject_GC_TRACK(op); |
| 792 | } |
| 793 | |
| 794 | void |
| 795 | _PyObject_GC_UnTrack(PyObject *op) |
| 796 | { |
| 797 | _PyObject_GC_UNTRACK(op); |
| 798 | } |
| 799 | |
| 800 | PyObject * |
| 801 | _PyObject_GC_Malloc(PyTypeObject *tp, int size) |
| 802 | { |
| 803 | PyObject *op; |
| 804 | #ifdef WITH_CYCLE_GC |
| 805 | PyGC_Head *g = PyObject_MALLOC(_PyObject_VAR_SIZE(tp, size) + |
| 806 | sizeof(PyGC_Head)); |
| 807 | if (g == NULL) |
| 808 | return (PyObject *)PyErr_NoMemory(); |
| 809 | g->gc_next = NULL; |
| 810 | allocated++; |
| 811 | if (allocated > threshold0 && |
| 812 | enabled && |
| 813 | threshold0 && |
| 814 | !collecting && |
| 815 | !PyErr_Occurred()) { |
| 816 | collecting = 1; |
| 817 | collect_generations(); |
| 818 | collecting = 0; |
| 819 | } |
| 820 | op = FROM_GC(g); |
| 821 | #else |
| 822 | op = PyObject_MALLOC(_PyObject_VAR_SIZE(tp, size)); |
| 823 | if (op == NULL) |
| 824 | return (PyObject *)PyErr_NoMemory(); |
| 825 | |
| 826 | #endif |
| 827 | return op; |
| 828 | } |
| 829 | |
| 830 | PyObject * |
| 831 | _PyObject_GC_New(PyTypeObject *tp) |
| 832 | { |
| 833 | PyObject *op = _PyObject_GC_Malloc(tp, 0); |
| 834 | return PyObject_INIT(op, tp); |
| 835 | } |
| 836 | |
| 837 | PyVarObject * |
| 838 | _PyObject_GC_NewVar(PyTypeObject *tp, int size) |
| 839 | { |
| 840 | PyVarObject *op = (PyVarObject *) _PyObject_GC_Malloc(tp, size); |
| 841 | return PyObject_INIT_VAR(op, tp, size); |
| 842 | } |
| 843 | |
| 844 | PyVarObject * |
| 845 | _PyObject_GC_Resize(PyVarObject *op, int size) |
| 846 | { |
| 847 | #ifdef WITH_CYCLE_GC |
| 848 | PyGC_Head *g = AS_GC(op); |
| 849 | g = PyObject_REALLOC(g, _PyObject_VAR_SIZE(op->ob_type, size) + |
| 850 | sizeof(PyGC_Head)); |
| 851 | if (g == NULL) |
| 852 | return (PyVarObject *)PyErr_NoMemory(); |
| 853 | op = (PyVarObject *) FROM_GC(g); |
| 854 | #else |
| 855 | op = PyObject_REALLOC(op, _PyObject_VAR_SIZE(op->ob_type, size)); |
| 856 | if (op == NULL) |
| 857 | return (PyVarObject *)PyErr_NoMemory(); |
| 858 | #endif |
| 859 | op->ob_size = size; |
| 860 | return op; |
| 861 | } |
| 862 | |
| 863 | void |
| 864 | _PyObject_GC_Del(PyObject *op) |
| 865 | { |
| 866 | #ifdef WITH_CYCLE_GC |
| 867 | PyGC_Head *g = AS_GC(op); |
| 868 | if (g->gc_next != NULL) |
| 869 | gc_list_remove(g); |
| 870 | if (allocated > 0) { |
| 871 | allocated--; |
| 872 | } |
| 873 | PyObject_FREE(g); |
| 874 | #else |
| 875 | PyObject_FREE(op); |
| 876 | #endif |
| 877 | } |
| 878 | |