blob: 0738eb67fff2e0649fedbe6a005a3e590036820d [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 */
Guido van Rossumbca8c2e2001-10-12 20:52:48 +000035PyGC_Head _PyGC_generation0 = {{&_PyGC_generation0, &_PyGC_generation0, 0}};
36static PyGC_Head generation1 = {{&generation1, &generation1, 0}};
37static PyGC_Head generation2 = {{&generation2, &generation2, 0}};
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000038static 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{
Tim Peters9e4ca102001-10-11 18:31:31 +000080 list->gc.gc_prev = list;
81 list->gc.gc_next = list;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000082}
83
84static void
85gc_list_append(PyGC_Head *node, PyGC_Head *list)
86{
Tim Peters9e4ca102001-10-11 18:31:31 +000087 node->gc.gc_next = list;
88 node->gc.gc_prev = list->gc.gc_prev;
89 node->gc.gc_prev->gc.gc_next = node;
90 list->gc.gc_prev = node;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000091}
92
93static void
94gc_list_remove(PyGC_Head *node)
95{
Tim Peters9e4ca102001-10-11 18:31:31 +000096 node->gc.gc_prev->gc.gc_next = node->gc.gc_next;
97 node->gc.gc_next->gc.gc_prev = node->gc.gc_prev;
98 node->gc.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{
Tim Peters9e4ca102001-10-11 18:31:31 +0000104 if (from->gc.gc_next == from) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000105 /* empty from list */
106 gc_list_init(to);
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000107 }
108 else {
Tim Peters9e4ca102001-10-11 18:31:31 +0000109 to->gc.gc_next = from->gc.gc_next;
110 to->gc.gc_next->gc.gc_prev = to;
111 to->gc.gc_prev = from->gc.gc_prev;
112 to->gc.gc_prev->gc.gc_next = to;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000113 }
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;
Tim Peters9e4ca102001-10-11 18:31:31 +0000122 if (from->gc.gc_next != from) {
123 tail = to->gc.gc_prev;
124 tail->gc.gc_next = from->gc.gc_next;
125 tail->gc.gc_next->gc.gc_prev = tail;
126 to->gc.gc_prev = from->gc.gc_prev;
127 to->gc.gc_prev->gc.gc_next = to;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000128 }
129 gc_list_init(from);
130}
131
132static long
133gc_list_size(PyGC_Head *list)
134{
135 PyGC_Head *gc;
136 long n = 0;
Tim Peters9e4ca102001-10-11 18:31:31 +0000137 for (gc = list->gc.gc_next; gc != list; gc = gc->gc.gc_next) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000138 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{
Tim Peters9e4ca102001-10-11 18:31:31 +0000151 PyGC_Head *gc = containers->gc.gc_next;
152 for (; gc != containers; gc=gc->gc.gc_next) {
153 gc->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);
Tim Peters9e4ca102001-10-11 18:31:31 +0000162 if (gc->gc.gc_next != NULL)
163 AS_GC(op)->gc.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;
Tim Peters9e4ca102001-10-11 18:31:31 +0000173 PyGC_Head *gc = containers->gc.gc_next;
174 for (; gc != containers; gc=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;
Tim Peters9e4ca102001-10-11 18:31:31 +0000187 PyGC_Head *gc = containers->gc.gc_next;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000188 while (gc != containers) {
Tim Peters9e4ca102001-10-11 18:31:31 +0000189 next = gc->gc.gc_next;
190 if (gc->gc.gc_refs > 0) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000191 gc_list_remove(gc);
192 gc_list_append(gc, roots);
Tim Peters9e4ca102001-10-11 18:31:31 +0000193 gc->gc.gc_refs = GC_MOVED;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000194 }
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);
Tim Peters9e4ca102001-10-11 18:31:31 +0000204 if (gc->gc.gc_next != NULL && gc->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);
Tim Peters9e4ca102001-10-11 18:31:31 +0000207 gc->gc.gc_refs = GC_MOVED;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000208 }
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;
Tim Peters9e4ca102001-10-11 18:31:31 +0000218 PyGC_Head *gc = reachable->gc.gc_next;
219 for (; gc != reachable; gc=gc->gc.gc_next) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000220 /* 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 Schemenauera765c122001-11-01 17:35:23 +0000229/* return true of object has a finalization method */
230static int
231has_finalizer(PyObject *op)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000232{
Jeremy Hylton06257772000-08-31 15:10:24 +0000233 static PyObject *delstr = NULL;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000234 if (delstr == NULL) {
235 delstr = PyString_InternFromString("__del__");
Jeremy Hylton06257772000-08-31 15:10:24 +0000236 if (delstr == NULL)
237 Py_FatalError("PyGC: can't initialize __del__ string");
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000238 }
Neil Schemenauera765c122001-11-01 17:35:23 +0000239 if ((PyInstance_Check(op) ||
240 PyType_HasFeature(op->ob_type, Py_TPFLAGS_HEAPTYPE)) &&
241 PyObject_HasAttr(op, delstr)) {
242 return 1;
243 }
244 else {
245 return 0;
246 }
247}
248
249/* Move all objects with finalizers (instances with __del__) */
250static void
251move_finalizers(PyGC_Head *unreachable, PyGC_Head *finalizers)
252{
253 PyGC_Head *next;
254 PyGC_Head *gc = unreachable->gc.gc_next;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000255 for (; gc != unreachable; gc=next) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000256 PyObject *op = FROM_GC(gc);
Tim Peters9e4ca102001-10-11 18:31:31 +0000257 next = gc->gc.gc_next;
Neil Schemenauera765c122001-11-01 17:35:23 +0000258 if (has_finalizer(op)) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000259 gc_list_remove(gc);
260 gc_list_append(gc, finalizers);
261 }
262 }
263}
264
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000265/* Move objects referenced from roots to roots */
266static void
267move_finalizer_reachable(PyGC_Head *finalizers)
268{
269 traverseproc traverse;
Tim Peters9e4ca102001-10-11 18:31:31 +0000270 PyGC_Head *gc = finalizers->gc.gc_next;
271 for (; gc != finalizers; gc=gc->gc.gc_next) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000272 /* careful, finalizers list is growing here */
Neil Schemenauer43411b52001-08-30 00:05:51 +0000273 traverse = FROM_GC(gc)->ob_type->tp_traverse;
274 (void) traverse(FROM_GC(gc),
275 (visitproc)visit_move,
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000276 (void *)finalizers);
277 }
278}
279
280static void
Jeremy Hylton06257772000-08-31 15:10:24 +0000281debug_instance(char *msg, PyInstanceObject *inst)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000282{
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000283 char *cname;
Neil Schemenauera765c122001-11-01 17:35:23 +0000284 /* simple version of instance_repr */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000285 PyObject *classname = inst->in_class->cl_name;
286 if (classname != NULL && PyString_Check(classname))
287 cname = PyString_AsString(classname);
288 else
289 cname = "?";
Jeremy Hylton06257772000-08-31 15:10:24 +0000290 PySys_WriteStderr("gc: %.100s <%.100s instance at %p>\n",
291 msg, cname, inst);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000292}
293
294static void
Jeremy Hylton06257772000-08-31 15:10:24 +0000295debug_cycle(char *msg, PyObject *op)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000296{
297 if ((debug & DEBUG_INSTANCES) && PyInstance_Check(op)) {
Jeremy Hylton06257772000-08-31 15:10:24 +0000298 debug_instance(msg, (PyInstanceObject *)op);
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000299 }
300 else if (debug & DEBUG_OBJECTS) {
Jeremy Hylton06257772000-08-31 15:10:24 +0000301 PySys_WriteStderr("gc: %.100s <%.100s %p>\n",
302 msg, op->ob_type->tp_name, op);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000303 }
304}
305
306/* Handle uncollectable garbage (cycles with finalizers). */
307static void
308handle_finalizers(PyGC_Head *finalizers, PyGC_Head *old)
309{
310 PyGC_Head *gc;
311 if (garbage == NULL) {
312 garbage = PyList_New(0);
313 }
Tim Peters9e4ca102001-10-11 18:31:31 +0000314 for (gc = finalizers->gc.gc_next; gc != finalizers;
315 gc = finalizers->gc.gc_next) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000316 PyObject *op = FROM_GC(gc);
Neil Schemenauera765c122001-11-01 17:35:23 +0000317 if ((debug & DEBUG_SAVEALL) || has_finalizer(op)) {
318 /* If SAVEALL is not set then just append objects with
319 * finalizers to the list of garbage. All objects in
320 * the finalizers list are reachable from those
321 * objects. */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000322 PyList_Append(garbage, op);
323 }
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000324 /* object is now reachable again */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000325 gc_list_remove(gc);
326 gc_list_append(gc, old);
327 }
328}
329
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000330/* Break reference cycles by clearing the containers involved. This is
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000331 * tricky business as the lists can be changing and we don't know which
332 * objects may be freed. It is possible I screwed something up here. */
333static void
334delete_garbage(PyGC_Head *unreachable, PyGC_Head *old)
335{
336 inquiry clear;
337
Tim Peters9e4ca102001-10-11 18:31:31 +0000338 while (unreachable->gc.gc_next != unreachable) {
339 PyGC_Head *gc = unreachable->gc.gc_next;
Neil Schemenauer43411b52001-08-30 00:05:51 +0000340 PyObject *op = FROM_GC(gc);
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000341 if (debug & DEBUG_SAVEALL) {
342 PyList_Append(garbage, op);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000343 }
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000344 else {
345 if ((clear = op->ob_type->tp_clear) != NULL) {
346 Py_INCREF(op);
347 clear((PyObject *)op);
348 Py_DECREF(op);
349 }
350 }
Tim Peters9e4ca102001-10-11 18:31:31 +0000351 if (unreachable->gc.gc_next == gc) {
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000352 /* object is still alive, move it, it may die later */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000353 gc_list_remove(gc);
354 gc_list_append(gc, old);
355 }
356 }
357}
358
359/* This is the main function. Read this to understand how the
360 * collection process works. */
361static long
362collect(PyGC_Head *young, PyGC_Head *old)
363{
364 long n = 0;
365 long m = 0;
366 PyGC_Head reachable;
367 PyGC_Head unreachable;
368 PyGC_Head finalizers;
369 PyGC_Head *gc;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000370
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000371 if (debug & DEBUG_STATS) {
Jeremy Hylton06257772000-08-31 15:10:24 +0000372 PySys_WriteStderr(
373 "gc: collecting generation %d...\n"
374 "gc: objects in each generation: %ld %ld %ld\n",
375 generation,
Neil Schemenauer43411b52001-08-30 00:05:51 +0000376 gc_list_size(&_PyGC_generation0),
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000377 gc_list_size(&generation1),
378 gc_list_size(&generation2));
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000379 }
380
381 /* Using ob_refcnt and gc_refs, calculate which objects in the
382 * container set are reachable from outside the set (ie. have a
383 * refcount greater than 0 when all the references within the
384 * set are taken into account */
385 update_refs(young);
386 subtract_refs(young);
387
388 /* Move everything reachable from outside the set into the
389 * reachable set (ie. gc_refs > 0). Next, move everything
390 * reachable from objects in the reachable set. */
391 gc_list_init(&reachable);
392 move_roots(young, &reachable);
393 move_root_reachable(&reachable);
394
395 /* move unreachable objects to a temporary list, new objects can be
396 * allocated after this point */
397 gc_list_init(&unreachable);
398 gc_list_move(young, &unreachable);
399
400 /* move reachable objects to next generation */
401 gc_list_merge(&reachable, old);
402
403 /* Move objects reachable from finalizers, we can't safely delete
404 * them. Python programmers should take care not to create such
405 * things. For Python finalizers means instance objects with
406 * __del__ methods. */
407 gc_list_init(&finalizers);
408 move_finalizers(&unreachable, &finalizers);
409 move_finalizer_reachable(&finalizers);
410
411 /* Collect statistics on collectable objects found and print
412 * debugging information. */
Tim Peters9e4ca102001-10-11 18:31:31 +0000413 for (gc = unreachable.gc.gc_next; gc != &unreachable;
414 gc = gc->gc.gc_next) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000415 m++;
Jeremy Hylton06257772000-08-31 15:10:24 +0000416 if (debug & DEBUG_COLLECTABLE) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000417 debug_cycle("collectable", FROM_GC(gc));
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000418 }
419 }
420 /* call tp_clear on objects in the collectable set. This will cause
421 * the reference cycles to be broken. It may also cause some objects in
422 * finalizers to be freed */
423 delete_garbage(&unreachable, old);
424
425 /* Collect statistics on uncollectable objects found and print
426 * debugging information. */
Tim Peters9e4ca102001-10-11 18:31:31 +0000427 for (gc = finalizers.gc.gc_next; gc != &finalizers;
428 gc = gc->gc.gc_next) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000429 n++;
Jeremy Hylton06257772000-08-31 15:10:24 +0000430 if (debug & DEBUG_UNCOLLECTABLE) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000431 debug_cycle("uncollectable", FROM_GC(gc));
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000432 }
433 }
Jeremy Hylton06257772000-08-31 15:10:24 +0000434 if (debug & DEBUG_STATS) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000435 if (m == 0 && n == 0) {
Jeremy Hylton06257772000-08-31 15:10:24 +0000436 PySys_WriteStderr("gc: done.\n");
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000437 }
438 else {
Jeremy Hylton06257772000-08-31 15:10:24 +0000439 PySys_WriteStderr(
440 "gc: done, %ld unreachable, %ld uncollectable.\n",
441 n+m, n);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000442 }
443 }
444
445 /* Append instances in the uncollectable set to a Python
446 * reachable list of garbage. The programmer has to deal with
447 * this if they insist on creating this type of structure. */
448 handle_finalizers(&finalizers, old);
449
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000450 if (PyErr_Occurred()) {
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000451 if (gc_str == NULL) {
452 gc_str = PyString_FromString("garbage collection");
453 }
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000454 PyErr_WriteUnraisable(gc_str);
455 Py_FatalError("unexpected exception during garbage collection");
456 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000457 allocated = 0;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000458 return n+m;
459}
460
461static long
462collect_generations(void)
463{
464 static long collections0 = 0;
465 static long collections1 = 0;
Vladimir Marangozovb16714b2000-07-10 05:37:39 +0000466 long n = 0;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000467
468
469 if (collections1 > threshold2) {
470 generation = 2;
Neil Schemenauer43411b52001-08-30 00:05:51 +0000471 gc_list_merge(&_PyGC_generation0, &generation2);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000472 gc_list_merge(&generation1, &generation2);
Tim Peters9e4ca102001-10-11 18:31:31 +0000473 if (generation2.gc.gc_next != &generation2) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000474 n = collect(&generation2, &generation2);
475 }
476 collections1 = 0;
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000477 }
478 else if (collections0 > threshold1) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000479 generation = 1;
480 collections1++;
Neil Schemenauer43411b52001-08-30 00:05:51 +0000481 gc_list_merge(&_PyGC_generation0, &generation1);
Tim Peters9e4ca102001-10-11 18:31:31 +0000482 if (generation1.gc.gc_next != &generation1) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000483 n = collect(&generation1, &generation2);
484 }
485 collections0 = 0;
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000486 }
487 else {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000488 generation = 0;
489 collections0++;
Tim Peters9e4ca102001-10-11 18:31:31 +0000490 if (_PyGC_generation0.gc.gc_next != &_PyGC_generation0) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000491 n = collect(&_PyGC_generation0, &generation1);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000492 }
493 }
494 return n;
495}
496
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000497static char gc_enable__doc__[] =
498"enable() -> None\n"
499"\n"
500"Enable automatic garbage collection.\n"
501;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000502
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000503static PyObject *
504gc_enable(PyObject *self, PyObject *args)
505{
506
507 if (!PyArg_ParseTuple(args, ":enable")) /* check no args */
508 return NULL;
509
510 enabled = 1;
511
512 Py_INCREF(Py_None);
513 return Py_None;
514}
515
516static char gc_disable__doc__[] =
517"disable() -> None\n"
518"\n"
519"Disable automatic garbage collection.\n"
520;
521
522static PyObject *
523gc_disable(PyObject *self, PyObject *args)
524{
525
526 if (!PyArg_ParseTuple(args, ":disable")) /* check no args */
527 return NULL;
528
529 enabled = 0;
530
531 Py_INCREF(Py_None);
532 return Py_None;
533}
534
535static char gc_isenabled__doc__[] =
536"isenabled() -> status\n"
537"\n"
538"Returns true if automatic garbage collection is enabled.\n"
539;
540
541static PyObject *
542gc_isenabled(PyObject *self, PyObject *args)
543{
544
545 if (!PyArg_ParseTuple(args, ":isenabled")) /* check no args */
546 return NULL;
547
548 return Py_BuildValue("i", enabled);
549}
550
551static char gc_collect__doc__[] =
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000552"collect() -> n\n"
553"\n"
554"Run a full collection. The number of unreachable objects is returned.\n"
555;
556
557static PyObject *
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000558gc_collect(PyObject *self, PyObject *args)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000559{
560 long n;
561
Fred Drakecc1be242000-07-12 04:42:23 +0000562 if (!PyArg_ParseTuple(args, ":collect")) /* check no args */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000563 return NULL;
564
Neil Schemenauere8c40cb2001-10-31 23:09:35 +0000565 if (collecting) {
566 n = 0; /* already collecting, don't do anything */
567 }
568 else {
569 collecting = 1;
570 generation = 2;
571 gc_list_merge(&_PyGC_generation0, &generation2);
572 gc_list_merge(&generation1, &generation2);
573 n = collect(&generation2, &generation2);
574 collecting = 0;
575 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000576
Neil Schemenauer7760cff2000-09-22 22:35:36 +0000577 return Py_BuildValue("l", n);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000578}
579
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000580static char gc_set_debug__doc__[] =
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000581"set_debug(flags) -> None\n"
582"\n"
583"Set the garbage collection debugging flags. Debugging information is\n"
584"written to sys.stderr.\n"
585"\n"
586"flags is an integer and can have the following bits turned on:\n"
587"\n"
588" DEBUG_STATS - Print statistics during collection.\n"
589" DEBUG_COLLECTABLE - Print collectable objects found.\n"
590" DEBUG_UNCOLLECTABLE - Print unreachable but uncollectable objects found.\n"
591" DEBUG_INSTANCES - Print instance objects.\n"
592" DEBUG_OBJECTS - Print objects other than instances.\n"
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000593" DEBUG_SAVEALL - Save objects to gc.garbage rather than freeing them.\n"
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000594" DEBUG_LEAK - Debug leaking programs (everything but STATS).\n"
595;
596
597static PyObject *
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000598gc_set_debug(PyObject *self, PyObject *args)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000599{
Neil Schemenauer7760cff2000-09-22 22:35:36 +0000600 if (!PyArg_ParseTuple(args, "i:set_debug", &debug))
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000601 return NULL;
602
603 Py_INCREF(Py_None);
604 return Py_None;
605}
606
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000607static char gc_get_debug__doc__[] =
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000608"get_debug() -> flags\n"
609"\n"
610"Get the garbage collection debugging flags.\n"
611;
612
613static PyObject *
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000614gc_get_debug(PyObject *self, PyObject *args)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000615{
Fred Drakecc1be242000-07-12 04:42:23 +0000616 if (!PyArg_ParseTuple(args, ":get_debug")) /* no args */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000617 return NULL;
618
619 return Py_BuildValue("i", debug);
620}
621
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000622static char gc_set_thresh__doc__[] =
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000623"set_threshold(threshold0, [threhold1, threshold2]) -> None\n"
624"\n"
625"Sets the collection thresholds. Setting threshold0 to zero disables\n"
626"collection.\n"
627;
628
629static PyObject *
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000630gc_set_thresh(PyObject *self, PyObject *args)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000631{
Fred Drakecc1be242000-07-12 04:42:23 +0000632 if (!PyArg_ParseTuple(args, "i|ii:set_threshold", &threshold0,
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000633 &threshold1, &threshold2))
634 return NULL;
635
636 Py_INCREF(Py_None);
637 return Py_None;
638}
639
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000640static char gc_get_thresh__doc__[] =
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000641"get_threshold() -> (threshold0, threshold1, threshold2)\n"
642"\n"
643"Return the current collection thresholds\n"
644;
645
646static PyObject *
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000647gc_get_thresh(PyObject *self, PyObject *args)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000648{
Fred Drakecc1be242000-07-12 04:42:23 +0000649 if (!PyArg_ParseTuple(args, ":get_threshold")) /* no args */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000650 return NULL;
651
652 return Py_BuildValue("(iii)", threshold0, threshold1, threshold2);
653}
654
Neil Schemenauer48c70342001-08-09 15:38:31 +0000655static int
Neil Schemenauer17e7be62001-08-10 14:46:47 +0000656referentsvisit(PyObject* obj, PyObject *objs)
Neil Schemenauer48c70342001-08-09 15:38:31 +0000657{
Neil Schemenauer17e7be62001-08-10 14:46:47 +0000658 if (PySequence_Contains(objs, obj)) {
Neil Schemenauer48c70342001-08-09 15:38:31 +0000659 return 1;
660 }
661 return 0;
662}
663
Neil Schemenauer17e7be62001-08-10 14:46:47 +0000664static int
Neil Schemenauer48c70342001-08-09 15:38:31 +0000665gc_referents_for(PyObject *objs, PyGC_Head *list, PyObject *resultlist)
666{
667 PyGC_Head *gc;
668 PyObject *obj;
669 traverseproc traverse;
Tim Peters9e4ca102001-10-11 18:31:31 +0000670 for (gc = list->gc.gc_next; gc != list; gc = gc->gc.gc_next) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000671 obj = FROM_GC(gc);
Neil Schemenauer48c70342001-08-09 15:38:31 +0000672 traverse = obj->ob_type->tp_traverse;
673 if (obj == objs || obj == resultlist)
674 continue;
Neil Schemenauer17e7be62001-08-10 14:46:47 +0000675 if (traverse(obj, (visitproc)referentsvisit, objs)) {
676 if (PyList_Append(resultlist, obj) < 0)
677 return 0; /* error */
Neil Schemenauer48c70342001-08-09 15:38:31 +0000678 }
679 }
Neil Schemenauer17e7be62001-08-10 14:46:47 +0000680 return 1; /* no error */
Neil Schemenauer48c70342001-08-09 15:38:31 +0000681}
682
683static char gc_get_referents__doc__[]=
684"get_referents(*objs) -> list\n\
685Return the list of objects that directly refer to any of objs.";
686
Neil Schemenauer17e7be62001-08-10 14:46:47 +0000687static PyObject *
688gc_get_referents(PyObject *self, PyObject *args)
Neil Schemenauer48c70342001-08-09 15:38:31 +0000689{
690 PyObject *result = PyList_New(0);
Neil Schemenauer43411b52001-08-30 00:05:51 +0000691 if (!(gc_referents_for(args, &_PyGC_generation0, result) &&
Neil Schemenauer17e7be62001-08-10 14:46:47 +0000692 gc_referents_for(args, &generation1, result) &&
693 gc_referents_for(args, &generation2, result))) {
694 Py_DECREF(result);
695 return NULL;
696 }
Neil Schemenauer48c70342001-08-09 15:38:31 +0000697 return result;
698}
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000699
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000700static char gc_get_objects__doc__[] =
701"get_objects() -> [...]\n"
702"\n"
703"Return a list of objects tracked by the collector (excluding the list\n"
704"returned).\n"
705;
706
707/* appending objects in a GC list to a Python list */
708static void
709append_objects(PyObject *py_list, PyGC_Head *gc_list)
710{
711 PyGC_Head *gc;
Tim Peters9e4ca102001-10-11 18:31:31 +0000712 for (gc = gc_list->gc.gc_next; gc != gc_list; gc = gc->gc.gc_next) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000713 PyObject *op = FROM_GC(gc);
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000714 if (op != py_list) {
715 Py_INCREF(op);
716 PyList_Append(py_list, op);
717 }
718 }
719}
720
721static PyObject *
722gc_get_objects(PyObject *self, PyObject *args)
723{
724 PyObject* result;
725
726 if (!PyArg_ParseTuple(args, ":get_objects")) /* check no args */
727 return NULL;
728 result = PyList_New(0);
Neil Schemenauer43411b52001-08-30 00:05:51 +0000729 append_objects(result, &_PyGC_generation0);
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000730 append_objects(result, &generation1);
731 append_objects(result, &generation2);
732 return result;
733}
734
735
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000736static char gc__doc__ [] =
737"This module provides access to the garbage collector for reference cycles.\n"
738"\n"
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000739"enable() -- Enable automatic garbage collection.\n"
740"disable() -- Disable automatic garbage collection.\n"
741"isenabled() -- Returns true if automatic collection is enabled.\n"
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000742"collect() -- Do a full collection right now.\n"
743"set_debug() -- Set debugging flags.\n"
744"get_debug() -- Get debugging flags.\n"
745"set_threshold() -- Set the collection thresholds.\n"
746"get_threshold() -- Return the current the collection thresholds.\n"
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000747"get_objects() -- Return a list of all objects tracked by the collector.\n"
Neil Schemenauer48c70342001-08-09 15:38:31 +0000748"get_referents() -- Return the list of objects that refer to an object.\n"
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000749;
750
751static PyMethodDef GcMethods[] = {
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000752 {"enable", gc_enable, METH_VARARGS, gc_enable__doc__},
753 {"disable", gc_disable, METH_VARARGS, gc_disable__doc__},
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000754 {"isenabled", gc_isenabled, METH_VARARGS, gc_isenabled__doc__},
755 {"set_debug", gc_set_debug, METH_VARARGS, gc_set_debug__doc__},
756 {"get_debug", gc_get_debug, METH_VARARGS, gc_get_debug__doc__},
757 {"set_threshold", gc_set_thresh, METH_VARARGS, gc_set_thresh__doc__},
758 {"get_threshold", gc_get_thresh, METH_VARARGS, gc_get_thresh__doc__},
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000759 {"collect", gc_collect, METH_VARARGS, gc_collect__doc__},
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000760 {"get_objects", gc_get_objects,METH_VARARGS, gc_get_objects__doc__},
Neil Schemenauer48c70342001-08-09 15:38:31 +0000761 {"get_referents", gc_get_referents, METH_VARARGS,
762 gc_get_referents__doc__},
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000763 {NULL, NULL} /* Sentinel */
764};
765
766void
767initgc(void)
768{
769 PyObject *m;
770 PyObject *d;
771
772 m = Py_InitModule4("gc",
773 GcMethods,
774 gc__doc__,
775 NULL,
776 PYTHON_API_VERSION);
777 d = PyModule_GetDict(m);
778 if (garbage == NULL) {
779 garbage = PyList_New(0);
780 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000781 PyDict_SetItemString(d, "garbage", garbage);
782 PyDict_SetItemString(d, "DEBUG_STATS",
783 PyInt_FromLong(DEBUG_STATS));
784 PyDict_SetItemString(d, "DEBUG_COLLECTABLE",
785 PyInt_FromLong(DEBUG_COLLECTABLE));
786 PyDict_SetItemString(d, "DEBUG_UNCOLLECTABLE",
787 PyInt_FromLong(DEBUG_UNCOLLECTABLE));
788 PyDict_SetItemString(d, "DEBUG_INSTANCES",
789 PyInt_FromLong(DEBUG_INSTANCES));
790 PyDict_SetItemString(d, "DEBUG_OBJECTS",
791 PyInt_FromLong(DEBUG_OBJECTS));
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000792 PyDict_SetItemString(d, "DEBUG_SAVEALL",
793 PyInt_FromLong(DEBUG_SAVEALL));
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000794 PyDict_SetItemString(d, "DEBUG_LEAK",
795 PyInt_FromLong(DEBUG_LEAK));
796}
797
Neil Schemenauer43411b52001-08-30 00:05:51 +0000798/* for debugging */
799void _PyGC_Dump(PyGC_Head *g)
800{
801 _PyObject_Dump(FROM_GC(g));
802}
803
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000804#endif /* WITH_CYCLE_GC */
Neil Schemenauer43411b52001-08-30 00:05:51 +0000805
806/* extension modules might be compiled with GC support so these
807 functions must always be available */
808
809void
810_PyObject_GC_Track(PyObject *op)
811{
812 _PyObject_GC_TRACK(op);
813}
814
815void
816_PyObject_GC_UnTrack(PyObject *op)
817{
818 _PyObject_GC_UNTRACK(op);
819}
820
821PyObject *
Tim Peters6d483d32001-10-06 21:27:34 +0000822_PyObject_GC_Malloc(PyTypeObject *tp, int nitems)
Neil Schemenauer43411b52001-08-30 00:05:51 +0000823{
824 PyObject *op;
Tim Petersf2a67da2001-10-07 03:54:51 +0000825 const size_t basicsize = _PyObject_VAR_SIZE(tp, nitems);
Neil Schemenauer43411b52001-08-30 00:05:51 +0000826#ifdef WITH_CYCLE_GC
Tim Petersf2a67da2001-10-07 03:54:51 +0000827 const size_t nbytes = sizeof(PyGC_Head) + basicsize;
828 PyGC_Head *g = PyObject_MALLOC(nbytes);
Neil Schemenauer43411b52001-08-30 00:05:51 +0000829 if (g == NULL)
830 return (PyObject *)PyErr_NoMemory();
Tim Peters9e4ca102001-10-11 18:31:31 +0000831 g->gc.gc_next = NULL;
Neil Schemenauer43411b52001-08-30 00:05:51 +0000832 allocated++;
833 if (allocated > threshold0 &&
834 enabled &&
835 threshold0 &&
836 !collecting &&
837 !PyErr_Occurred()) {
838 collecting = 1;
839 collect_generations();
840 collecting = 0;
841 }
842 op = FROM_GC(g);
843#else
Tim Peters6d483d32001-10-06 21:27:34 +0000844 op = PyObject_MALLOC(basicsize);
Neil Schemenauer43411b52001-08-30 00:05:51 +0000845 if (op == NULL)
846 return (PyObject *)PyErr_NoMemory();
847
848#endif
849 return op;
850}
851
852PyObject *
853_PyObject_GC_New(PyTypeObject *tp)
854{
Tim Peters6d483d32001-10-06 21:27:34 +0000855 PyObject *op = _PyObject_GC_Malloc(tp, 0);
Neil Schemenauer43411b52001-08-30 00:05:51 +0000856 return PyObject_INIT(op, tp);
857}
858
859PyVarObject *
Tim Peters6d483d32001-10-06 21:27:34 +0000860_PyObject_GC_NewVar(PyTypeObject *tp, int nitems)
Neil Schemenauer43411b52001-08-30 00:05:51 +0000861{
Tim Peters6d483d32001-10-06 21:27:34 +0000862 PyVarObject *op = (PyVarObject *) _PyObject_GC_Malloc(tp, nitems);
863 return PyObject_INIT_VAR(op, tp, nitems);
Neil Schemenauer43411b52001-08-30 00:05:51 +0000864}
865
866PyVarObject *
Tim Peters6d483d32001-10-06 21:27:34 +0000867_PyObject_GC_Resize(PyVarObject *op, int nitems)
Neil Schemenauer43411b52001-08-30 00:05:51 +0000868{
Tim Petersf2a67da2001-10-07 03:54:51 +0000869 const size_t basicsize = _PyObject_VAR_SIZE(op->ob_type, nitems);
Neil Schemenauer43411b52001-08-30 00:05:51 +0000870#ifdef WITH_CYCLE_GC
871 PyGC_Head *g = AS_GC(op);
Tim Peters6d483d32001-10-06 21:27:34 +0000872 g = PyObject_REALLOC(g, sizeof(PyGC_Head) + basicsize);
Neil Schemenauer43411b52001-08-30 00:05:51 +0000873 if (g == NULL)
874 return (PyVarObject *)PyErr_NoMemory();
875 op = (PyVarObject *) FROM_GC(g);
876#else
Tim Peters6d483d32001-10-06 21:27:34 +0000877 op = PyObject_REALLOC(op, basicsize);
Neil Schemenauer43411b52001-08-30 00:05:51 +0000878 if (op == NULL)
879 return (PyVarObject *)PyErr_NoMemory();
880#endif
Tim Peters6d483d32001-10-06 21:27:34 +0000881 op->ob_size = nitems;
Neil Schemenauer43411b52001-08-30 00:05:51 +0000882 return op;
883}
884
885void
886_PyObject_GC_Del(PyObject *op)
887{
888#ifdef WITH_CYCLE_GC
889 PyGC_Head *g = AS_GC(op);
Tim Peters9e4ca102001-10-11 18:31:31 +0000890 if (g->gc.gc_next != NULL)
Neil Schemenauer43411b52001-08-30 00:05:51 +0000891 gc_list_remove(g);
892 if (allocated > 0) {
893 allocated--;
894 }
895 PyObject_FREE(g);
896#else
897 PyObject_FREE(op);
898#endif
899}
900