blob: 34503e43a3e8d0ce4461f292d1169a7302ca69e3 [file] [log] [blame]
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001/*
2
3 Reference Cycle Garbage Collection
4 ==================================
5
Neil Schemenauerb2c2c9e2000-10-04 16:34:09 +00006 Neil Schemenauer <nas@arctrix.com>
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00007
8 Based on a post on the python-dev list. Ideas from Guido van Rossum,
9 Eric Tiedemann, and various others.
10
Neil Schemenauer43411b52001-08-30 00:05:51 +000011 http://www.arctrix.com/nas/python/gc/
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000012 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 Hyltonc5007aa2000-06-30 05:02:53 +000019*/
20
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000021#include "Python.h"
22
23#ifdef WITH_CYCLE_GC
24
Neil Schemenauer43411b52001-08-30 00:05:51 +000025/* 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 Hyltonc5007aa2000-06-30 05:02:53 +000031
32/*** Global GC state ***/
33
34/* linked lists of container objects */
Neil Schemenauer43411b52001-08-30 00:05:51 +000035PyGC_Head _PyGC_generation0 = {&_PyGC_generation0, &_PyGC_generation0, 0};
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000036static PyGC_Head generation1 = {&generation1, &generation1, 0};
37static PyGC_Head generation2 = {&generation2, &generation2, 0};
38static int generation = 0; /* current generation being collected */
39
40/* collection frequencies, XXX tune these */
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +000041static int enabled = 1; /* automatic collection enabled? */
Jeremy Hylton3263dc2b2000-09-05 15:44:50 +000042static int threshold0 = 700; /* net new containers before collection */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000043static int threshold1 = 10; /* generation0 collections before collecting 1 */
44static int threshold2 = 10; /* generation1 collections before collecting 2 */
45
46/* net new objects allocated since last collection */
47static int allocated;
48
Neil Schemenauer43411b52001-08-30 00:05:51 +000049/* true if we are currently running the collector */
50static int collecting;
51
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000052/* 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 Schemenauer544de1e2000-09-22 15:22:38 +000058#define DEBUG_SAVEALL (1<<5) /* save all garbage in gc.garbage */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000059#define DEBUG_LEAK DEBUG_COLLECTABLE | \
60 DEBUG_UNCOLLECTABLE | \
61 DEBUG_INSTANCES | \
Neil Schemenauer544de1e2000-09-22 15:22:38 +000062 DEBUG_OBJECTS | \
63 DEBUG_SAVEALL
Jeremy Hyltonb709df32000-09-01 02:47:25 +000064static int debug;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000065
Neil Schemenauer43411b52001-08-30 00:05:51 +000066/* Special gc_refs value */
67#define GC_MOVED -123
68
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000069/* list of uncollectable objects */
70static PyObject *garbage;
71
Jeremy Hyltonb709df32000-09-01 02:47:25 +000072/* Python string to use if unhandled exception occurs */
73static PyObject *gc_str;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000074
75/*** list functions ***/
76
77static void
78gc_list_init(PyGC_Head *list)
79{
80 list->gc_prev = list;
81 list->gc_next = list;
82}
83
84static void
85gc_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
93static void
94gc_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 Schemenauer43411b52001-08-30 00:05:51 +000098 node->gc_next = NULL; /* object is not currently tracked */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000099}
100
101static void
102gc_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 Schemenauer544de1e2000-09-22 15:22:38 +0000107 }
108 else {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000109 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 */
118static void
119gc_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
132static long
133gc_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 Schemenauer43411b52001-08-30 00:05:51 +0000146
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000147/* Set all gc_refs = ob_refcnt */
148static void
149update_refs(PyGC_Head *containers)
150{
151 PyGC_Head *gc = containers->gc_next;
152 for (; gc != containers; gc=gc->gc_next) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000153 gc->gc_refs = FROM_GC(gc)->ob_refcnt;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000154 }
155}
156
157static int
158visit_decref(PyObject *op, void *data)
159{
160 if (op && PyObject_IS_GC(op)) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000161 PyGC_Head *gc = AS_GC(op);
162 if (gc->gc_next != NULL)
163 AS_GC(op)->gc_refs--;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000164 }
165 return 0;
166}
167
168/* Subtract internal references from gc_refs */
169static void
170subtract_refs(PyGC_Head *containers)
171{
172 traverseproc traverse;
173 PyGC_Head *gc = containers->gc_next;
174 for (; gc != containers; gc=gc->gc_next) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000175 traverse = FROM_GC(gc)->ob_type->tp_traverse;
176 (void) traverse(FROM_GC(gc),
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000177 (visitproc)visit_decref,
178 NULL);
179 }
180}
181
182/* Append objects with gc_refs > 0 to roots list */
183static void
184move_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
199static int
Neil Schemenauer43411b52001-08-30 00:05:51 +0000200visit_move(PyObject *op, PyGC_Head *tolist)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000201{
202 if (PyObject_IS_GC(op)) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000203 PyGC_Head *gc = AS_GC(op);
204 if (gc->gc_next != NULL && gc->gc_refs != GC_MOVED) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000205 gc_list_remove(gc);
Neil Schemenauer43411b52001-08-30 00:05:51 +0000206 gc_list_append(gc, tolist);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000207 gc->gc_refs = GC_MOVED;
208 }
209 }
210 return 0;
211}
212
213/* Move objects referenced from reachable to reachable set. */
214static void
215move_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 Schemenauer43411b52001-08-30 00:05:51 +0000221 PyObject *op = FROM_GC(gc);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000222 traverse = op->ob_type->tp_traverse;
223 (void) traverse(op,
Neil Schemenauer43411b52001-08-30 00:05:51 +0000224 (visitproc)visit_move,
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000225 (void *)reachable);
226 }
227}
228
Neil Schemenauer43411b52001-08-30 00:05:51 +0000229/* Move all objects with finalizers (instances with __del__) */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000230static void
231move_finalizers(PyGC_Head *unreachable, PyGC_Head *finalizers)
232{
233 PyGC_Head *next;
234 PyGC_Head *gc = unreachable->gc_next;
Jeremy Hylton06257772000-08-31 15:10:24 +0000235 static PyObject *delstr = NULL;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000236 if (delstr == NULL) {
237 delstr = PyString_InternFromString("__del__");
Jeremy Hylton06257772000-08-31 15:10:24 +0000238 if (delstr == NULL)
239 Py_FatalError("PyGC: can't initialize __del__ string");
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000240 }
241 for (; gc != unreachable; gc=next) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000242 PyObject *op = FROM_GC(gc);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000243 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 Hyltonc5007aa2000-06-30 05:02:53 +0000251/* Move objects referenced from roots to roots */
252static void
253move_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 Schemenauer43411b52001-08-30 00:05:51 +0000259 traverse = FROM_GC(gc)->ob_type->tp_traverse;
260 (void) traverse(FROM_GC(gc),
261 (visitproc)visit_move,
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000262 (void *)finalizers);
263 }
264}
265
266static void
Jeremy Hylton06257772000-08-31 15:10:24 +0000267debug_instance(char *msg, PyInstanceObject *inst)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000268{
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000269 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 Hylton06257772000-08-31 15:10:24 +0000276 PySys_WriteStderr("gc: %.100s <%.100s instance at %p>\n",
277 msg, cname, inst);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000278}
279
280static void
Jeremy Hylton06257772000-08-31 15:10:24 +0000281debug_cycle(char *msg, PyObject *op)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000282{
283 if ((debug & DEBUG_INSTANCES) && PyInstance_Check(op)) {
Jeremy Hylton06257772000-08-31 15:10:24 +0000284 debug_instance(msg, (PyInstanceObject *)op);
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000285 }
286 else if (debug & DEBUG_OBJECTS) {
Jeremy Hylton06257772000-08-31 15:10:24 +0000287 PySys_WriteStderr("gc: %.100s <%.100s %p>\n",
288 msg, op->ob_type->tp_name, op);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000289 }
290}
291
292/* Handle uncollectable garbage (cycles with finalizers). */
293static void
294handle_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 Schemenauer43411b52001-08-30 00:05:51 +0000302 PyObject *op = FROM_GC(gc);
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000303 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 Hyltonc5007aa2000-06-30 05:02:53 +0000308 PyList_Append(garbage, op);
309 }
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000310 /* object is now reachable again */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000311 gc_list_remove(gc);
312 gc_list_append(gc, old);
313 }
314}
315
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000316/* Break reference cycles by clearing the containers involved. This is
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000317 * 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. */
319static void
320delete_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 Schemenauer43411b52001-08-30 00:05:51 +0000326 PyObject *op = FROM_GC(gc);
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000327 if (debug & DEBUG_SAVEALL) {
328 PyList_Append(garbage, op);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000329 }
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000330 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 Hyltonc5007aa2000-06-30 05:02:53 +0000337 if (unreachable->gc_next == gc) {
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000338 /* object is still alive, move it, it may die later */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000339 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. */
347static long
348collect(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 Hyltonc5007aa2000-06-30 05:02:53 +0000356
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000357 if (debug & DEBUG_STATS) {
Jeremy Hylton06257772000-08-31 15:10:24 +0000358 PySys_WriteStderr(
359 "gc: collecting generation %d...\n"
360 "gc: objects in each generation: %ld %ld %ld\n",
361 generation,
Neil Schemenauer43411b52001-08-30 00:05:51 +0000362 gc_list_size(&_PyGC_generation0),
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000363 gc_list_size(&generation1),
364 gc_list_size(&generation2));
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000365 }
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 Hylton06257772000-08-31 15:10:24 +0000402 if (debug & DEBUG_COLLECTABLE) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000403 debug_cycle("collectable", FROM_GC(gc));
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000404 }
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 Hylton06257772000-08-31 15:10:24 +0000416 if (debug & DEBUG_UNCOLLECTABLE) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000417 debug_cycle("uncollectable", FROM_GC(gc));
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000418 }
419 }
Jeremy Hylton06257772000-08-31 15:10:24 +0000420 if (debug & DEBUG_STATS) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000421 if (m == 0 && n == 0) {
Jeremy Hylton06257772000-08-31 15:10:24 +0000422 PySys_WriteStderr("gc: done.\n");
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000423 }
424 else {
Jeremy Hylton06257772000-08-31 15:10:24 +0000425 PySys_WriteStderr(
426 "gc: done, %ld unreachable, %ld uncollectable.\n",
427 n+m, n);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000428 }
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 Hyltonb709df32000-09-01 02:47:25 +0000436 if (PyErr_Occurred()) {
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000437 if (gc_str == NULL) {
438 gc_str = PyString_FromString("garbage collection");
439 }
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000440 PyErr_WriteUnraisable(gc_str);
441 Py_FatalError("unexpected exception during garbage collection");
442 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000443 allocated = 0;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000444 return n+m;
445}
446
447static long
448collect_generations(void)
449{
450 static long collections0 = 0;
451 static long collections1 = 0;
Vladimir Marangozovb16714b2000-07-10 05:37:39 +0000452 long n = 0;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000453
454
455 if (collections1 > threshold2) {
456 generation = 2;
Neil Schemenauer43411b52001-08-30 00:05:51 +0000457 gc_list_merge(&_PyGC_generation0, &generation2);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000458 gc_list_merge(&generation1, &generation2);
459 if (generation2.gc_next != &generation2) {
460 n = collect(&generation2, &generation2);
461 }
462 collections1 = 0;
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000463 }
464 else if (collections0 > threshold1) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000465 generation = 1;
466 collections1++;
Neil Schemenauer43411b52001-08-30 00:05:51 +0000467 gc_list_merge(&_PyGC_generation0, &generation1);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000468 if (generation1.gc_next != &generation1) {
469 n = collect(&generation1, &generation2);
470 }
471 collections0 = 0;
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000472 }
473 else {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000474 generation = 0;
475 collections0++;
Neil Schemenauer43411b52001-08-30 00:05:51 +0000476 if (_PyGC_generation0.gc_next != &_PyGC_generation0) {
477 n = collect(&_PyGC_generation0, &generation1);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000478 }
479 }
480 return n;
481}
482
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000483static char gc_enable__doc__[] =
484"enable() -> None\n"
485"\n"
486"Enable automatic garbage collection.\n"
487;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000488
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000489static PyObject *
490gc_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
502static char gc_disable__doc__[] =
503"disable() -> None\n"
504"\n"
505"Disable automatic garbage collection.\n"
506;
507
508static PyObject *
509gc_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
521static char gc_isenabled__doc__[] =
522"isenabled() -> status\n"
523"\n"
524"Returns true if automatic garbage collection is enabled.\n"
525;
526
527static PyObject *
528gc_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
537static char gc_collect__doc__[] =
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000538"collect() -> n\n"
539"\n"
540"Run a full collection. The number of unreachable objects is returned.\n"
541;
542
543static PyObject *
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000544gc_collect(PyObject *self, PyObject *args)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000545{
546 long n;
547
Fred Drakecc1be242000-07-12 04:42:23 +0000548 if (!PyArg_ParseTuple(args, ":collect")) /* check no args */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000549 return NULL;
550
551 generation = 2;
Neil Schemenauer43411b52001-08-30 00:05:51 +0000552 gc_list_merge(&_PyGC_generation0, &generation2);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000553 gc_list_merge(&generation1, &generation2);
554 n = collect(&generation2, &generation2);
555
Neil Schemenauer7760cff2000-09-22 22:35:36 +0000556 return Py_BuildValue("l", n);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000557}
558
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000559static char gc_set_debug__doc__[] =
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000560"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 Schemenauer544de1e2000-09-22 15:22:38 +0000572" DEBUG_SAVEALL - Save objects to gc.garbage rather than freeing them.\n"
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000573" DEBUG_LEAK - Debug leaking programs (everything but STATS).\n"
574;
575
576static PyObject *
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000577gc_set_debug(PyObject *self, PyObject *args)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000578{
Neil Schemenauer7760cff2000-09-22 22:35:36 +0000579 if (!PyArg_ParseTuple(args, "i:set_debug", &debug))
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000580 return NULL;
581
582 Py_INCREF(Py_None);
583 return Py_None;
584}
585
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000586static char gc_get_debug__doc__[] =
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000587"get_debug() -> flags\n"
588"\n"
589"Get the garbage collection debugging flags.\n"
590;
591
592static PyObject *
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000593gc_get_debug(PyObject *self, PyObject *args)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000594{
Fred Drakecc1be242000-07-12 04:42:23 +0000595 if (!PyArg_ParseTuple(args, ":get_debug")) /* no args */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000596 return NULL;
597
598 return Py_BuildValue("i", debug);
599}
600
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000601static char gc_set_thresh__doc__[] =
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000602"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
608static PyObject *
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000609gc_set_thresh(PyObject *self, PyObject *args)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000610{
Fred Drakecc1be242000-07-12 04:42:23 +0000611 if (!PyArg_ParseTuple(args, "i|ii:set_threshold", &threshold0,
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000612 &threshold1, &threshold2))
613 return NULL;
614
615 Py_INCREF(Py_None);
616 return Py_None;
617}
618
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000619static char gc_get_thresh__doc__[] =
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000620"get_threshold() -> (threshold0, threshold1, threshold2)\n"
621"\n"
622"Return the current collection thresholds\n"
623;
624
625static PyObject *
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000626gc_get_thresh(PyObject *self, PyObject *args)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000627{
Fred Drakecc1be242000-07-12 04:42:23 +0000628 if (!PyArg_ParseTuple(args, ":get_threshold")) /* no args */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000629 return NULL;
630
631 return Py_BuildValue("(iii)", threshold0, threshold1, threshold2);
632}
633
Neil Schemenauer48c70342001-08-09 15:38:31 +0000634static int
Neil Schemenauer17e7be62001-08-10 14:46:47 +0000635referentsvisit(PyObject* obj, PyObject *objs)
Neil Schemenauer48c70342001-08-09 15:38:31 +0000636{
Neil Schemenauer17e7be62001-08-10 14:46:47 +0000637 if (PySequence_Contains(objs, obj)) {
Neil Schemenauer48c70342001-08-09 15:38:31 +0000638 return 1;
639 }
640 return 0;
641}
642
Neil Schemenauer17e7be62001-08-10 14:46:47 +0000643static int
Neil Schemenauer48c70342001-08-09 15:38:31 +0000644gc_referents_for(PyObject *objs, PyGC_Head *list, PyObject *resultlist)
645{
646 PyGC_Head *gc;
647 PyObject *obj;
648 traverseproc traverse;
Neil Schemenauer17e7be62001-08-10 14:46:47 +0000649 for (gc = list->gc_next; gc != list; gc = gc->gc_next) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000650 obj = FROM_GC(gc);
Neil Schemenauer48c70342001-08-09 15:38:31 +0000651 traverse = obj->ob_type->tp_traverse;
652 if (obj == objs || obj == resultlist)
653 continue;
Neil Schemenauer17e7be62001-08-10 14:46:47 +0000654 if (traverse(obj, (visitproc)referentsvisit, objs)) {
655 if (PyList_Append(resultlist, obj) < 0)
656 return 0; /* error */
Neil Schemenauer48c70342001-08-09 15:38:31 +0000657 }
658 }
Neil Schemenauer17e7be62001-08-10 14:46:47 +0000659 return 1; /* no error */
Neil Schemenauer48c70342001-08-09 15:38:31 +0000660}
661
662static char gc_get_referents__doc__[]=
663"get_referents(*objs) -> list\n\
664Return the list of objects that directly refer to any of objs.";
665
Neil Schemenauer17e7be62001-08-10 14:46:47 +0000666static PyObject *
667gc_get_referents(PyObject *self, PyObject *args)
Neil Schemenauer48c70342001-08-09 15:38:31 +0000668{
669 PyObject *result = PyList_New(0);
Neil Schemenauer43411b52001-08-30 00:05:51 +0000670 if (!(gc_referents_for(args, &_PyGC_generation0, result) &&
Neil Schemenauer17e7be62001-08-10 14:46:47 +0000671 gc_referents_for(args, &generation1, result) &&
672 gc_referents_for(args, &generation2, result))) {
673 Py_DECREF(result);
674 return NULL;
675 }
Neil Schemenauer48c70342001-08-09 15:38:31 +0000676 return result;
677}
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000678
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000679static 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 */
687static void
688append_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 Schemenauer43411b52001-08-30 00:05:51 +0000692 PyObject *op = FROM_GC(gc);
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000693 if (op != py_list) {
694 Py_INCREF(op);
695 PyList_Append(py_list, op);
696 }
697 }
698}
699
700static PyObject *
701gc_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 Schemenauer43411b52001-08-30 00:05:51 +0000708 append_objects(result, &_PyGC_generation0);
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000709 append_objects(result, &generation1);
710 append_objects(result, &generation2);
711 return result;
712}
713
714
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000715static char gc__doc__ [] =
716"This module provides access to the garbage collector for reference cycles.\n"
717"\n"
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000718"enable() -- Enable automatic garbage collection.\n"
719"disable() -- Disable automatic garbage collection.\n"
720"isenabled() -- Returns true if automatic collection is enabled.\n"
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000721"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 Schemenauerc7c8d8e2001-08-09 15:58:59 +0000726"get_objects() -- Return a list of all objects tracked by the collector.\n"
Neil Schemenauer48c70342001-08-09 15:38:31 +0000727"get_referents() -- Return the list of objects that refer to an object.\n"
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000728;
729
730static PyMethodDef GcMethods[] = {
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000731 {"enable", gc_enable, METH_VARARGS, gc_enable__doc__},
732 {"disable", gc_disable, METH_VARARGS, gc_disable__doc__},
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000733 {"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 Schemenauer544de1e2000-09-22 15:22:38 +0000738 {"collect", gc_collect, METH_VARARGS, gc_collect__doc__},
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000739 {"get_objects", gc_get_objects,METH_VARARGS, gc_get_objects__doc__},
Neil Schemenauer48c70342001-08-09 15:38:31 +0000740 {"get_referents", gc_get_referents, METH_VARARGS,
741 gc_get_referents__doc__},
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000742 {NULL, NULL} /* Sentinel */
743};
744
745void
746initgc(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 Hyltonc5007aa2000-06-30 05:02:53 +0000760 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 Schemenauer544de1e2000-09-22 15:22:38 +0000771 PyDict_SetItemString(d, "DEBUG_SAVEALL",
772 PyInt_FromLong(DEBUG_SAVEALL));
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000773 PyDict_SetItemString(d, "DEBUG_LEAK",
774 PyInt_FromLong(DEBUG_LEAK));
775}
776
Neil Schemenauer43411b52001-08-30 00:05:51 +0000777/* for debugging */
778void _PyGC_Dump(PyGC_Head *g)
779{
780 _PyObject_Dump(FROM_GC(g));
781}
782
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000783#endif /* WITH_CYCLE_GC */
Neil Schemenauer43411b52001-08-30 00:05:51 +0000784
785/* extension modules might be compiled with GC support so these
786 functions must always be available */
787
788void
789_PyObject_GC_Track(PyObject *op)
790{
791 _PyObject_GC_TRACK(op);
792}
793
794void
795_PyObject_GC_UnTrack(PyObject *op)
796{
797 _PyObject_GC_UNTRACK(op);
798}
799
800PyObject *
Tim Peters6d483d32001-10-06 21:27:34 +0000801_PyObject_GC_Malloc(PyTypeObject *tp, int nitems)
Neil Schemenauer43411b52001-08-30 00:05:51 +0000802{
803 PyObject *op;
Tim Petersf2a67da2001-10-07 03:54:51 +0000804 const size_t basicsize = _PyObject_VAR_SIZE(tp, nitems);
Neil Schemenauer43411b52001-08-30 00:05:51 +0000805#ifdef WITH_CYCLE_GC
Tim Petersf2a67da2001-10-07 03:54:51 +0000806 const size_t nbytes = sizeof(PyGC_Head) + basicsize;
807 PyGC_Head *g = PyObject_MALLOC(nbytes);
Neil Schemenauer43411b52001-08-30 00:05:51 +0000808 if (g == NULL)
809 return (PyObject *)PyErr_NoMemory();
810 g->gc_next = NULL;
811 allocated++;
812 if (allocated > threshold0 &&
813 enabled &&
814 threshold0 &&
815 !collecting &&
816 !PyErr_Occurred()) {
817 collecting = 1;
818 collect_generations();
819 collecting = 0;
820 }
821 op = FROM_GC(g);
822#else
Tim Peters6d483d32001-10-06 21:27:34 +0000823 op = PyObject_MALLOC(basicsize);
Neil Schemenauer43411b52001-08-30 00:05:51 +0000824 if (op == NULL)
825 return (PyObject *)PyErr_NoMemory();
826
827#endif
828 return op;
829}
830
831PyObject *
832_PyObject_GC_New(PyTypeObject *tp)
833{
Tim Peters6d483d32001-10-06 21:27:34 +0000834 PyObject *op = _PyObject_GC_Malloc(tp, 0);
Neil Schemenauer43411b52001-08-30 00:05:51 +0000835 return PyObject_INIT(op, tp);
836}
837
838PyVarObject *
Tim Peters6d483d32001-10-06 21:27:34 +0000839_PyObject_GC_NewVar(PyTypeObject *tp, int nitems)
Neil Schemenauer43411b52001-08-30 00:05:51 +0000840{
Tim Peters6d483d32001-10-06 21:27:34 +0000841 PyVarObject *op = (PyVarObject *) _PyObject_GC_Malloc(tp, nitems);
842 return PyObject_INIT_VAR(op, tp, nitems);
Neil Schemenauer43411b52001-08-30 00:05:51 +0000843}
844
845PyVarObject *
Tim Peters6d483d32001-10-06 21:27:34 +0000846_PyObject_GC_Resize(PyVarObject *op, int nitems)
Neil Schemenauer43411b52001-08-30 00:05:51 +0000847{
Tim Petersf2a67da2001-10-07 03:54:51 +0000848 const size_t basicsize = _PyObject_VAR_SIZE(op->ob_type, nitems);
Neil Schemenauer43411b52001-08-30 00:05:51 +0000849#ifdef WITH_CYCLE_GC
850 PyGC_Head *g = AS_GC(op);
Tim Peters6d483d32001-10-06 21:27:34 +0000851 g = PyObject_REALLOC(g, sizeof(PyGC_Head) + basicsize);
Neil Schemenauer43411b52001-08-30 00:05:51 +0000852 if (g == NULL)
853 return (PyVarObject *)PyErr_NoMemory();
854 op = (PyVarObject *) FROM_GC(g);
855#else
Tim Peters6d483d32001-10-06 21:27:34 +0000856 op = PyObject_REALLOC(op, basicsize);
Neil Schemenauer43411b52001-08-30 00:05:51 +0000857 if (op == NULL)
858 return (PyVarObject *)PyErr_NoMemory();
859#endif
Tim Peters6d483d32001-10-06 21:27:34 +0000860 op->ob_size = nitems;
Neil Schemenauer43411b52001-08-30 00:05:51 +0000861 return op;
862}
863
864void
865_PyObject_GC_Del(PyObject *op)
866{
867#ifdef WITH_CYCLE_GC
868 PyGC_Head *g = AS_GC(op);
869 if (g->gc_next != NULL)
870 gc_list_remove(g);
871 if (allocated > 0) {
872 allocated--;
873 }
874 PyObject_FREE(g);
875#else
876 PyObject_FREE(op);
877#endif
878}
879