blob: cb56253bcfff4390c8ff7a2c39ae3746d80de046 [file] [log] [blame]
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001/*
Tim Peters88396172002-06-30 17:56:40 +00002
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00003 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
Neil Schemenauera2b11ec2002-05-21 15:53:24 +000031/* True if an object is tracked by the GC */
32#define IS_TRACKED(o) ((AS_GC(o))->gc.gc_next != NULL)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000033
34/*** Global GC state ***/
35
Neil Schemenauer2880ae52002-05-04 05:35:20 +000036struct gc_generation {
37 PyGC_Head head;
38 int threshold; /* collection threshold */
39 int count; /* count of allocations or collections of younger
40 generations */
41};
42
43#define NUM_GENERATIONS 3
44#define GEN_HEAD(n) (&generations[n].head)
45
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000046/* linked lists of container objects */
Neil Schemenauer2880ae52002-05-04 05:35:20 +000047static struct gc_generation generations[NUM_GENERATIONS] = {
48 /* PyGC_Head, threshold, count */
49 {{{GEN_HEAD(0), GEN_HEAD(0), 0}}, 700, 0},
50 {{{GEN_HEAD(1), GEN_HEAD(1), 0}}, 10, 0},
51 {{{GEN_HEAD(2), GEN_HEAD(2), 0}}, 10, 0},
52};
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000053
Neil Schemenauer2880ae52002-05-04 05:35:20 +000054PyGC_Head *_PyGC_generation0 = GEN_HEAD(0);
55
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +000056static int enabled = 1; /* automatic collection enabled? */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000057
Neil Schemenauer43411b52001-08-30 00:05:51 +000058/* true if we are currently running the collector */
59static int collecting;
60
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000061/* set for debugging information */
62#define DEBUG_STATS (1<<0) /* print collection statistics */
63#define DEBUG_COLLECTABLE (1<<1) /* print collectable objects */
64#define DEBUG_UNCOLLECTABLE (1<<2) /* print uncollectable objects */
65#define DEBUG_INSTANCES (1<<3) /* print instances */
66#define DEBUG_OBJECTS (1<<4) /* print other objects */
Neil Schemenauer544de1e2000-09-22 15:22:38 +000067#define DEBUG_SAVEALL (1<<5) /* save all garbage in gc.garbage */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000068#define DEBUG_LEAK DEBUG_COLLECTABLE | \
69 DEBUG_UNCOLLECTABLE | \
70 DEBUG_INSTANCES | \
Neil Schemenauer544de1e2000-09-22 15:22:38 +000071 DEBUG_OBJECTS | \
72 DEBUG_SAVEALL
Jeremy Hyltonb709df32000-09-01 02:47:25 +000073static int debug;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000074
Tim Peters88396172002-06-30 17:56:40 +000075/* When a collection begins, gc_refs is set to ob_refcnt for, and only for,
76 * the objects in the generation being collected, called the "young"
Tim Peters19b74c72002-07-01 03:52:19 +000077 * generation at that point. As collection proceeds, the gc_refs members
78 * of young objects are set to GC_REACHABLE when it becomes known that they're
79 * uncollectable, and to GC_TENTATIVELY_UNREACHABLE when the evidence
80 * suggests they are collectable (this can't be known for certain until all
81 * of the young generation is scanned).
Tim Peters88396172002-06-30 17:56:40 +000082 */
Neil Schemenauer43411b52001-08-30 00:05:51 +000083
Tim Peters19b74c72002-07-01 03:52:19 +000084/* Special gc_refs values. */
85#define GC_REACHABLE -123
86#define GC_TENTATIVELY_UNREACHABLE -42
87
88#define IS_REACHABLE(o) ((AS_GC(o))->gc.gc_refs == GC_REACHABLE)
89#define IS_TENTATIVELY_UNREACHABLE(o) ( \
90 (AS_GC(o))->gc.gc_refs == GC_TENTATIVELY_UNREACHABLE)
Neil Schemenauera2b11ec2002-05-21 15:53:24 +000091
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000092/* list of uncollectable objects */
93static PyObject *garbage;
94
Jeremy Hyltonb709df32000-09-01 02:47:25 +000095/* Python string to use if unhandled exception occurs */
96static PyObject *gc_str;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000097
98/*** list functions ***/
99
100static void
101gc_list_init(PyGC_Head *list)
102{
Tim Peters9e4ca102001-10-11 18:31:31 +0000103 list->gc.gc_prev = list;
104 list->gc.gc_next = list;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000105}
106
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000107static int
108gc_list_is_empty(PyGC_Head *list)
109{
110 return (list->gc.gc_next == list);
111}
112
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000113static void
114gc_list_append(PyGC_Head *node, PyGC_Head *list)
115{
Tim Peters9e4ca102001-10-11 18:31:31 +0000116 node->gc.gc_next = list;
117 node->gc.gc_prev = list->gc.gc_prev;
118 node->gc.gc_prev->gc.gc_next = node;
119 list->gc.gc_prev = node;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000120}
121
122static void
123gc_list_remove(PyGC_Head *node)
124{
Tim Peters9e4ca102001-10-11 18:31:31 +0000125 node->gc.gc_prev->gc.gc_next = node->gc.gc_next;
126 node->gc.gc_next->gc.gc_prev = node->gc.gc_prev;
127 node->gc.gc_next = NULL; /* object is not currently tracked */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000128}
129
Tim Peters88396172002-06-30 17:56:40 +0000130static void
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000131gc_list_move(PyGC_Head *from, PyGC_Head *to)
132{
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000133 if (gc_list_is_empty(from)) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000134 gc_list_init(to);
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000135 }
136 else {
Tim Peters9e4ca102001-10-11 18:31:31 +0000137 to->gc.gc_next = from->gc.gc_next;
138 to->gc.gc_next->gc.gc_prev = to;
139 to->gc.gc_prev = from->gc.gc_prev;
140 to->gc.gc_prev->gc.gc_next = to;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000141 }
142 gc_list_init(from);
143}
144
145/* append a list onto another list, from becomes an empty list */
146static void
147gc_list_merge(PyGC_Head *from, PyGC_Head *to)
148{
149 PyGC_Head *tail;
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000150 if (!gc_list_is_empty(from)) {
Tim Peters9e4ca102001-10-11 18:31:31 +0000151 tail = to->gc.gc_prev;
152 tail->gc.gc_next = from->gc.gc_next;
153 tail->gc.gc_next->gc.gc_prev = tail;
154 to->gc.gc_prev = from->gc.gc_prev;
155 to->gc.gc_prev->gc.gc_next = to;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000156 }
157 gc_list_init(from);
158}
159
160static long
161gc_list_size(PyGC_Head *list)
162{
163 PyGC_Head *gc;
164 long n = 0;
Tim Peters9e4ca102001-10-11 18:31:31 +0000165 for (gc = list->gc.gc_next; gc != list; gc = gc->gc.gc_next) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000166 n++;
167 }
168 return n;
169}
170
171/*** end of list stuff ***/
172
173
Tim Peters19b74c72002-07-01 03:52:19 +0000174/* Set all gc_refs = ob_refcnt. After this, gc_refs is > 0 for all objects
175 * in containers, and is GC_REACHABLE for all tracked gc objects not in
176 * containers.
Tim Peters88396172002-06-30 17:56:40 +0000177 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000178static void
179update_refs(PyGC_Head *containers)
180{
Tim Peters9e4ca102001-10-11 18:31:31 +0000181 PyGC_Head *gc = containers->gc.gc_next;
Tim Peters19b74c72002-07-01 03:52:19 +0000182 for (; gc != containers; gc = gc->gc.gc_next)
Tim Peters9e4ca102001-10-11 18:31:31 +0000183 gc->gc.gc_refs = FROM_GC(gc)->ob_refcnt;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000184}
185
Tim Peters19b74c72002-07-01 03:52:19 +0000186/* A traversal callback for subtract_refs. */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000187static int
188visit_decref(PyObject *op, void *data)
189{
Tim Peters93cd83e2002-06-30 21:31:03 +0000190 assert(op != NULL);
Tim Peters19b74c72002-07-01 03:52:19 +0000191 if (PyObject_IS_GC(op)) {
192 PyGC_Head *gc = AS_GC(op);
193 /* We're only interested in gc_refs for objects in the
194 * generation being collected, which can be recognized
195 * because only they have positive gc_refs.
196 */
197 if (gc->gc.gc_refs > 0)
198 gc->gc.gc_refs--;
199 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000200 return 0;
201}
202
Tim Peters19b74c72002-07-01 03:52:19 +0000203/* Subtract internal references from gc_refs. After this, gc_refs is >= 0
204 * for all objects in containers, and is GC_REACHABLE for all tracked gc
205 * objects not in containers. The ones with gc_refs > 0 are directly
206 * reachable from outside containers, and so can't be collected.
207 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000208static void
209subtract_refs(PyGC_Head *containers)
210{
211 traverseproc traverse;
Tim Peters9e4ca102001-10-11 18:31:31 +0000212 PyGC_Head *gc = containers->gc.gc_next;
213 for (; gc != containers; gc=gc->gc.gc_next) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000214 traverse = FROM_GC(gc)->ob_type->tp_traverse;
215 (void) traverse(FROM_GC(gc),
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000216 (visitproc)visit_decref,
217 NULL);
218 }
219}
220
Tim Peters19b74c72002-07-01 03:52:19 +0000221/* A traversal callback for move_unreachable. */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000222static int
Tim Peters19b74c72002-07-01 03:52:19 +0000223visit_reachable(PyObject *op, PyGC_Head *reachable)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000224{
Tim Peters19b74c72002-07-01 03:52:19 +0000225 if (PyObject_IS_GC(op) && IS_TRACKED(op)) {
226 PyGC_Head *gc = AS_GC(op);
227 const int gc_refs = gc->gc.gc_refs;
228
229 if (gc_refs == 0) {
230 /* This is in move_unreachable's 'young' list, but
231 * the traversal hasn't yet gotten to it. All
232 * we need to do is tell move_unreachable that it's
233 * reachable.
234 */
235 gc->gc.gc_refs = 1;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000236 }
Tim Peters19b74c72002-07-01 03:52:19 +0000237 else if (gc_refs == GC_TENTATIVELY_UNREACHABLE) {
238 /* This had gc_refs = 0 when move_unreachable got
239 * to it, but turns out it's reachable after all.
240 * Move it back to move_unreachable's 'young' list,
241 * and move_unreachable will eventually get to it
242 * again.
243 */
244 gc_list_remove(gc);
245 gc_list_append(gc, reachable);
246 gc->gc.gc_refs = 1;
247 }
248 /* Else there's nothing to do.
249 * If gc_refs > 0, it must be in move_unreachable's 'young'
250 * list, and move_unreachable will eventually get to it.
251 * If gc_refs == GC_REACHABLE, it's either in some other
252 * generation so we don't care about it, or move_unreachable
253 * already dealt with it.
254 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000255 }
256 return 0;
257}
258
Tim Peters19b74c72002-07-01 03:52:19 +0000259/* Move the unreachable objects from young to unreachable. After this,
260 * all objects in young have gc_refs = GC_REACHABLE, and all objects in
261 * unreachable have gc_refs = GC_TENTATIVELY_UNREACHABLE. All tracked
262 * gc objects not in young or unreachable still have gc_refs = GC_REACHABLE.
263 * All objects in young after this are directly or indirectly reachable
264 * from outside the original young; and all objects in unreachable are
265 * not.
Tim Peters88396172002-06-30 17:56:40 +0000266 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000267static void
Tim Peters19b74c72002-07-01 03:52:19 +0000268move_unreachable(PyGC_Head *young, PyGC_Head *unreachable)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000269{
Tim Peters19b74c72002-07-01 03:52:19 +0000270 PyGC_Head *gc = young->gc.gc_next;
271
272 /* Invariants: all objects "to the left" of us in young have gc_refs
273 * = GC_REACHABLE, and are indeed reachable (directly or indirectly)
274 * from outside the young list as it was at entry. All other objects
275 * from the original young "to the left" of us are in unreachable now,
276 * and have gc_refs = GC_TENTATIVELY_UNREACHABLE. All objects to the
277 * left of us in 'young' now have been scanned, and no objects here
278 * or to the right have been scanned yet.
279 */
280
281 while (gc != young) {
282 PyGC_Head *next;
283
284 if (gc->gc.gc_refs == 0) {
285 /* This *may* be unreachable. To make progress,
286 * assume it is. gc isn't directly reachable from
287 * any object we've already traversed, but may be
288 * reachable from an object we haven't gotten to yet.
289 * visit_reachable will eventually move gc back into
290 * young if that's so, and we'll see it again.
291 */
292 next = gc->gc.gc_next;
293 gc_list_remove(gc);
294 gc_list_append(gc, unreachable);
295 gc->gc.gc_refs = GC_TENTATIVELY_UNREACHABLE;
296 }
297 else {
298 /* gc is definitely reachable from outside the
299 * original 'young'. Mark it as such, and traverse
300 * its pointers to find any other objects that may
301 * be directly reachable from it. Note that the
302 * call to tp_traverse may append objects to young,
303 * so we have to wait until it returns to determine
304 * the next object to visit.
305 */
306 PyObject *op = FROM_GC(gc);
307 traverseproc traverse = op->ob_type->tp_traverse;
308 gc->gc.gc_refs = GC_REACHABLE;
309 (void) traverse(op,
310 (visitproc)visit_reachable,
311 (void *)young);
312 next = gc->gc.gc_next;
313 }
314 gc = next;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000315 }
316}
317
Tim Peters88396172002-06-30 17:56:40 +0000318/* return true if object has a finalization method */
Neil Schemenauera765c122001-11-01 17:35:23 +0000319static int
320has_finalizer(PyObject *op)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000321{
Jeremy Hylton06257772000-08-31 15:10:24 +0000322 static PyObject *delstr = NULL;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000323 if (delstr == NULL) {
324 delstr = PyString_InternFromString("__del__");
Jeremy Hylton06257772000-08-31 15:10:24 +0000325 if (delstr == NULL)
326 Py_FatalError("PyGC: can't initialize __del__ string");
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000327 }
Tim Petersdb865612001-11-01 19:35:45 +0000328 return (PyInstance_Check(op) ||
329 PyType_HasFeature(op->ob_type, Py_TPFLAGS_HEAPTYPE))
330 && PyObject_HasAttr(op, delstr);
Neil Schemenauera765c122001-11-01 17:35:23 +0000331}
332
333/* Move all objects with finalizers (instances with __del__) */
334static void
335move_finalizers(PyGC_Head *unreachable, PyGC_Head *finalizers)
336{
337 PyGC_Head *next;
338 PyGC_Head *gc = unreachable->gc.gc_next;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000339 for (; gc != unreachable; gc=next) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000340 PyObject *op = FROM_GC(gc);
Tim Peters9e4ca102001-10-11 18:31:31 +0000341 next = gc->gc.gc_next;
Neil Schemenauera765c122001-11-01 17:35:23 +0000342 if (has_finalizer(op)) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000343 gc_list_remove(gc);
344 gc_list_append(gc, finalizers);
Tim Peters19b74c72002-07-01 03:52:19 +0000345 gc->gc.gc_refs = GC_REACHABLE;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000346 }
347 }
348}
349
Tim Peters19b74c72002-07-01 03:52:19 +0000350/* A traversal callback for move_finalizer_reachable. */
351static int
352visit_move(PyObject *op, PyGC_Head *tolist)
353{
354 if (PyObject_IS_GC(op)) {
355 if (IS_TRACKED(op) && IS_TENTATIVELY_UNREACHABLE(op)) {
356 PyGC_Head *gc = AS_GC(op);
357 gc_list_remove(gc);
358 gc_list_append(gc, tolist);
359 gc->gc.gc_refs = GC_REACHABLE;
360 }
361 }
362 return 0;
363}
364
365/* Move objects that are reachable from finalizers, from the unreachable set
366 * into the finalizers set.
367 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000368static void
369move_finalizer_reachable(PyGC_Head *finalizers)
370{
371 traverseproc traverse;
Tim Peters9e4ca102001-10-11 18:31:31 +0000372 PyGC_Head *gc = finalizers->gc.gc_next;
373 for (; gc != finalizers; gc=gc->gc.gc_next) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000374 /* careful, finalizers list is growing here */
Neil Schemenauer43411b52001-08-30 00:05:51 +0000375 traverse = FROM_GC(gc)->ob_type->tp_traverse;
Tim Peters88396172002-06-30 17:56:40 +0000376 (void) traverse(FROM_GC(gc),
Neil Schemenauer43411b52001-08-30 00:05:51 +0000377 (visitproc)visit_move,
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000378 (void *)finalizers);
379 }
380}
381
382static void
Jeremy Hylton06257772000-08-31 15:10:24 +0000383debug_instance(char *msg, PyInstanceObject *inst)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000384{
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000385 char *cname;
Neil Schemenauera765c122001-11-01 17:35:23 +0000386 /* simple version of instance_repr */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000387 PyObject *classname = inst->in_class->cl_name;
388 if (classname != NULL && PyString_Check(classname))
389 cname = PyString_AsString(classname);
390 else
391 cname = "?";
Jeremy Hylton06257772000-08-31 15:10:24 +0000392 PySys_WriteStderr("gc: %.100s <%.100s instance at %p>\n",
393 msg, cname, inst);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000394}
395
396static void
Jeremy Hylton06257772000-08-31 15:10:24 +0000397debug_cycle(char *msg, PyObject *op)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000398{
399 if ((debug & DEBUG_INSTANCES) && PyInstance_Check(op)) {
Jeremy Hylton06257772000-08-31 15:10:24 +0000400 debug_instance(msg, (PyInstanceObject *)op);
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000401 }
402 else if (debug & DEBUG_OBJECTS) {
Jeremy Hylton06257772000-08-31 15:10:24 +0000403 PySys_WriteStderr("gc: %.100s <%.100s %p>\n",
404 msg, op->ob_type->tp_name, op);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000405 }
406}
407
408/* Handle uncollectable garbage (cycles with finalizers). */
409static void
410handle_finalizers(PyGC_Head *finalizers, PyGC_Head *old)
411{
412 PyGC_Head *gc;
413 if (garbage == NULL) {
414 garbage = PyList_New(0);
415 }
Tim Peters9e4ca102001-10-11 18:31:31 +0000416 for (gc = finalizers->gc.gc_next; gc != finalizers;
417 gc = finalizers->gc.gc_next) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000418 PyObject *op = FROM_GC(gc);
Neil Schemenauera765c122001-11-01 17:35:23 +0000419 if ((debug & DEBUG_SAVEALL) || has_finalizer(op)) {
420 /* If SAVEALL is not set then just append objects with
421 * finalizers to the list of garbage. All objects in
422 * the finalizers list are reachable from those
Tim Peters19b74c72002-07-01 03:52:19 +0000423 * objects.
424 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000425 PyList_Append(garbage, op);
426 }
Tim Peters88396172002-06-30 17:56:40 +0000427 /* object is now reachable again */
Tim Peters19b74c72002-07-01 03:52:19 +0000428 assert(IS_REACHABLE(op));
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000429 gc_list_remove(gc);
430 gc_list_append(gc, old);
431 }
432}
433
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000434/* Break reference cycles by clearing the containers involved. This is
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000435 * tricky business as the lists can be changing and we don't know which
Tim Peters19b74c72002-07-01 03:52:19 +0000436 * objects may be freed. It is possible I screwed something up here.
437 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000438static void
439delete_garbage(PyGC_Head *unreachable, PyGC_Head *old)
440{
441 inquiry clear;
442
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000443 while (!gc_list_is_empty(unreachable)) {
Tim Peters9e4ca102001-10-11 18:31:31 +0000444 PyGC_Head *gc = unreachable->gc.gc_next;
Neil Schemenauer43411b52001-08-30 00:05:51 +0000445 PyObject *op = FROM_GC(gc);
Tim Peters88396172002-06-30 17:56:40 +0000446
Tim Peters19b74c72002-07-01 03:52:19 +0000447 assert(IS_TENTATIVELY_UNREACHABLE(op));
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000448 if (debug & DEBUG_SAVEALL) {
449 PyList_Append(garbage, op);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000450 }
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000451 else {
452 if ((clear = op->ob_type->tp_clear) != NULL) {
453 Py_INCREF(op);
Jeremy Hylton8a135182002-06-06 23:23:55 +0000454 clear(op);
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000455 Py_DECREF(op);
456 }
457 }
Tim Peters9e4ca102001-10-11 18:31:31 +0000458 if (unreachable->gc.gc_next == gc) {
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000459 /* object is still alive, move it, it may die later */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000460 gc_list_remove(gc);
461 gc_list_append(gc, old);
Tim Peters19b74c72002-07-01 03:52:19 +0000462 gc->gc.gc_refs = GC_REACHABLE;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000463 }
464 }
465}
466
467/* This is the main function. Read this to understand how the
468 * collection process works. */
469static long
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000470collect(int generation)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000471{
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000472 int i;
Tim Peters19b74c72002-07-01 03:52:19 +0000473 long m = 0; /* # objects collected */
474 long n = 0; /* # unreachable objects that couldn't be collected */
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000475 PyGC_Head *young; /* the generation we are examining */
476 PyGC_Head *old; /* next older generation */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000477 PyGC_Head unreachable;
478 PyGC_Head finalizers;
479 PyGC_Head *gc;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000480
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000481 if (debug & DEBUG_STATS) {
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000482 PySys_WriteStderr("gc: collecting generation %d...\n",
483 generation);
484 PySys_WriteStderr("gc: objects in each generation:");
485 for (i = 0; i < NUM_GENERATIONS; i++) {
486 PySys_WriteStderr(" %ld", gc_list_size(GEN_HEAD(i)));
487 }
488 PySys_WriteStderr("\n");
489 }
490
491 /* update collection and allocation counters */
492 if (generation+1 < NUM_GENERATIONS)
493 generations[generation+1].count += 1;
494 for (i = 0; i <= generation; i++)
Neil Schemenauerc9051642002-06-28 19:16:04 +0000495 generations[i].count = 0;
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000496
497 /* merge younger generations with one we are currently collecting */
498 for (i = 0; i < generation; i++) {
499 gc_list_merge(GEN_HEAD(i), GEN_HEAD(generation));
500 }
501
502 /* handy references */
503 young = GEN_HEAD(generation);
Tim Peters19b74c72002-07-01 03:52:19 +0000504 if (generation < NUM_GENERATIONS-1)
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000505 old = GEN_HEAD(generation+1);
Tim Peters19b74c72002-07-01 03:52:19 +0000506 else
507 old = young;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000508
509 /* Using ob_refcnt and gc_refs, calculate which objects in the
510 * container set are reachable from outside the set (ie. have a
511 * refcount greater than 0 when all the references within the
Tim Peters19b74c72002-07-01 03:52:19 +0000512 * set are taken into account
513 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000514 update_refs(young);
515 subtract_refs(young);
516
Tim Peters19b74c72002-07-01 03:52:19 +0000517 /* Leave everything reachable from outside young in young, and move
518 * everything else (in young) to unreachable.
519 * NOTE: This used to move the reachable objects into a reachable
520 * set instead. But most things usually turn out to be reachable,
521 * so it's more efficient to move the unreachable things.
522 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000523 gc_list_init(&unreachable);
Tim Peters19b74c72002-07-01 03:52:19 +0000524 move_unreachable(young, &unreachable);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000525
Tim Peters19b74c72002-07-01 03:52:19 +0000526 /* Move reachable objects to next generation. */
527 if (young != old)
528 gc_list_merge(young, old);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000529
Tim Peters19b74c72002-07-01 03:52:19 +0000530 /* All objects in unreachable are trash, but objects reachable from
531 * finalizers can't safely be deleted. Python programmers should take
532 * care not to create such things. For Python, finalizers means
533 * instance objects with __del__ methods.
534 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000535 gc_list_init(&finalizers);
536 move_finalizers(&unreachable, &finalizers);
537 move_finalizer_reachable(&finalizers);
538
539 /* Collect statistics on collectable objects found and print
540 * debugging information. */
Tim Peters9e4ca102001-10-11 18:31:31 +0000541 for (gc = unreachable.gc.gc_next; gc != &unreachable;
542 gc = gc->gc.gc_next) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000543 m++;
Jeremy Hylton06257772000-08-31 15:10:24 +0000544 if (debug & DEBUG_COLLECTABLE) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000545 debug_cycle("collectable", FROM_GC(gc));
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000546 }
547 }
Tim Peters19b74c72002-07-01 03:52:19 +0000548 /* Call tp_clear on objects in the collectable set. This will cause
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000549 * the reference cycles to be broken. It may also cause some objects in
550 * finalizers to be freed */
551 delete_garbage(&unreachable, old);
552
553 /* Collect statistics on uncollectable objects found and print
554 * debugging information. */
Tim Peters9e4ca102001-10-11 18:31:31 +0000555 for (gc = finalizers.gc.gc_next; gc != &finalizers;
556 gc = gc->gc.gc_next) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000557 n++;
Jeremy Hylton06257772000-08-31 15:10:24 +0000558 if (debug & DEBUG_UNCOLLECTABLE) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000559 debug_cycle("uncollectable", FROM_GC(gc));
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000560 }
561 }
Jeremy Hylton06257772000-08-31 15:10:24 +0000562 if (debug & DEBUG_STATS) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000563 if (m == 0 && n == 0) {
Jeremy Hylton06257772000-08-31 15:10:24 +0000564 PySys_WriteStderr("gc: done.\n");
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000565 }
566 else {
Jeremy Hylton06257772000-08-31 15:10:24 +0000567 PySys_WriteStderr(
568 "gc: done, %ld unreachable, %ld uncollectable.\n",
569 n+m, n);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000570 }
571 }
572
573 /* Append instances in the uncollectable set to a Python
574 * reachable list of garbage. The programmer has to deal with
575 * this if they insist on creating this type of structure. */
576 handle_finalizers(&finalizers, old);
577
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000578 if (PyErr_Occurred()) {
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000579 if (gc_str == NULL) {
580 gc_str = PyString_FromString("garbage collection");
581 }
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000582 PyErr_WriteUnraisable(gc_str);
583 Py_FatalError("unexpected exception during garbage collection");
584 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000585 return n+m;
586}
587
588static long
589collect_generations(void)
590{
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000591 int i;
Vladimir Marangozovb16714b2000-07-10 05:37:39 +0000592 long n = 0;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000593
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000594 /* Find the oldest generation (higest numbered) where the count
595 * exceeds the threshold. Objects in the that generation and
596 * generations younger than it will be collected. */
597 for (i = NUM_GENERATIONS-1; i >= 0; i--) {
598 if (generations[i].count > generations[i].threshold) {
599 n = collect(i);
600 break;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000601 }
602 }
603 return n;
604}
605
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000606PyDoc_STRVAR(gc_enable__doc__,
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000607"enable() -> None\n"
608"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000609"Enable automatic garbage collection.\n");
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000610
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000611static PyObject *
612gc_enable(PyObject *self, PyObject *args)
613{
614
615 if (!PyArg_ParseTuple(args, ":enable")) /* check no args */
616 return NULL;
617
618 enabled = 1;
619
620 Py_INCREF(Py_None);
621 return Py_None;
622}
623
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000624PyDoc_STRVAR(gc_disable__doc__,
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000625"disable() -> None\n"
626"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000627"Disable automatic garbage collection.\n");
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000628
629static PyObject *
630gc_disable(PyObject *self, PyObject *args)
631{
632
633 if (!PyArg_ParseTuple(args, ":disable")) /* check no args */
634 return NULL;
635
636 enabled = 0;
637
638 Py_INCREF(Py_None);
639 return Py_None;
640}
641
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000642PyDoc_STRVAR(gc_isenabled__doc__,
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000643"isenabled() -> status\n"
644"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000645"Returns true if automatic garbage collection is enabled.\n");
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000646
647static PyObject *
648gc_isenabled(PyObject *self, PyObject *args)
649{
650
651 if (!PyArg_ParseTuple(args, ":isenabled")) /* check no args */
652 return NULL;
653
654 return Py_BuildValue("i", enabled);
655}
656
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000657PyDoc_STRVAR(gc_collect__doc__,
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000658"collect() -> n\n"
659"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000660"Run a full collection. The number of unreachable objects is returned.\n");
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000661
662static PyObject *
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000663gc_collect(PyObject *self, PyObject *args)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000664{
665 long n;
666
Fred Drakecc1be242000-07-12 04:42:23 +0000667 if (!PyArg_ParseTuple(args, ":collect")) /* check no args */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000668 return NULL;
669
Neil Schemenauere8c40cb2001-10-31 23:09:35 +0000670 if (collecting) {
671 n = 0; /* already collecting, don't do anything */
672 }
673 else {
674 collecting = 1;
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000675 n = collect(NUM_GENERATIONS - 1);
Neil Schemenauere8c40cb2001-10-31 23:09:35 +0000676 collecting = 0;
677 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000678
Neil Schemenauer7760cff2000-09-22 22:35:36 +0000679 return Py_BuildValue("l", n);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000680}
681
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000682PyDoc_STRVAR(gc_set_debug__doc__,
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000683"set_debug(flags) -> None\n"
684"\n"
685"Set the garbage collection debugging flags. Debugging information is\n"
686"written to sys.stderr.\n"
687"\n"
688"flags is an integer and can have the following bits turned on:\n"
689"\n"
690" DEBUG_STATS - Print statistics during collection.\n"
691" DEBUG_COLLECTABLE - Print collectable objects found.\n"
692" DEBUG_UNCOLLECTABLE - Print unreachable but uncollectable objects found.\n"
693" DEBUG_INSTANCES - Print instance objects.\n"
694" DEBUG_OBJECTS - Print objects other than instances.\n"
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000695" DEBUG_SAVEALL - Save objects to gc.garbage rather than freeing them.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000696" DEBUG_LEAK - Debug leaking programs (everything but STATS).\n");
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000697
698static PyObject *
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000699gc_set_debug(PyObject *self, PyObject *args)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000700{
Neil Schemenauer7760cff2000-09-22 22:35:36 +0000701 if (!PyArg_ParseTuple(args, "i:set_debug", &debug))
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000702 return NULL;
703
704 Py_INCREF(Py_None);
705 return Py_None;
706}
707
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000708PyDoc_STRVAR(gc_get_debug__doc__,
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000709"get_debug() -> flags\n"
710"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000711"Get the garbage collection debugging flags.\n");
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000712
713static PyObject *
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000714gc_get_debug(PyObject *self, PyObject *args)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000715{
Fred Drakecc1be242000-07-12 04:42:23 +0000716 if (!PyArg_ParseTuple(args, ":get_debug")) /* no args */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000717 return NULL;
718
719 return Py_BuildValue("i", debug);
720}
721
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000722PyDoc_STRVAR(gc_set_thresh__doc__,
Neal Norwitz2a47c0f2002-01-29 00:53:41 +0000723"set_threshold(threshold0, [threshold1, threshold2]) -> None\n"
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000724"\n"
725"Sets the collection thresholds. Setting threshold0 to zero disables\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000726"collection.\n");
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000727
728static PyObject *
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000729gc_set_thresh(PyObject *self, PyObject *args)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000730{
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000731 int i;
732 if (!PyArg_ParseTuple(args, "i|ii:set_threshold",
733 &generations[0].threshold,
734 &generations[1].threshold,
735 &generations[2].threshold))
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000736 return NULL;
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000737 for (i = 2; i < NUM_GENERATIONS; i++) {
738 /* generations higher than 2 get the same threshold */
739 generations[i].threshold = generations[2].threshold;
740 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000741
742 Py_INCREF(Py_None);
743 return Py_None;
744}
745
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000746PyDoc_STRVAR(gc_get_thresh__doc__,
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000747"get_threshold() -> (threshold0, threshold1, threshold2)\n"
748"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000749"Return the current collection thresholds\n");
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000750
751static PyObject *
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000752gc_get_thresh(PyObject *self, PyObject *args)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000753{
Fred Drakecc1be242000-07-12 04:42:23 +0000754 if (!PyArg_ParseTuple(args, ":get_threshold")) /* no args */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000755 return NULL;
756
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000757 return Py_BuildValue("(iii)",
758 generations[0].threshold,
759 generations[1].threshold,
760 generations[2].threshold);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000761}
762
Neil Schemenauer48c70342001-08-09 15:38:31 +0000763static int
Martin v. Löwis560da622001-11-24 09:24:51 +0000764referrersvisit(PyObject* obj, PyObject *objs)
Neil Schemenauer48c70342001-08-09 15:38:31 +0000765{
Martin v. Löwisc8fe77b2001-11-29 18:08:31 +0000766 int i;
767 for (i = 0; i < PyTuple_GET_SIZE(objs); i++)
768 if (PyTuple_GET_ITEM(objs, i) == obj)
769 return 1;
Neil Schemenauer48c70342001-08-09 15:38:31 +0000770 return 0;
771}
772
Neil Schemenauer17e7be62001-08-10 14:46:47 +0000773static int
Martin v. Löwis560da622001-11-24 09:24:51 +0000774gc_referrers_for(PyObject *objs, PyGC_Head *list, PyObject *resultlist)
Neil Schemenauer48c70342001-08-09 15:38:31 +0000775{
776 PyGC_Head *gc;
777 PyObject *obj;
778 traverseproc traverse;
Tim Peters9e4ca102001-10-11 18:31:31 +0000779 for (gc = list->gc.gc_next; gc != list; gc = gc->gc.gc_next) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000780 obj = FROM_GC(gc);
Neil Schemenauer48c70342001-08-09 15:38:31 +0000781 traverse = obj->ob_type->tp_traverse;
782 if (obj == objs || obj == resultlist)
783 continue;
Martin v. Löwis560da622001-11-24 09:24:51 +0000784 if (traverse(obj, (visitproc)referrersvisit, objs)) {
Neil Schemenauer17e7be62001-08-10 14:46:47 +0000785 if (PyList_Append(resultlist, obj) < 0)
786 return 0; /* error */
Neil Schemenauer48c70342001-08-09 15:38:31 +0000787 }
788 }
Neil Schemenauer17e7be62001-08-10 14:46:47 +0000789 return 1; /* no error */
Neil Schemenauer48c70342001-08-09 15:38:31 +0000790}
791
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000792PyDoc_STRVAR(gc_get_referrers__doc__,
Martin v. Löwis560da622001-11-24 09:24:51 +0000793"get_referrers(*objs) -> list\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000794Return the list of objects that directly refer to any of objs.");
Neil Schemenauer48c70342001-08-09 15:38:31 +0000795
Neil Schemenauer17e7be62001-08-10 14:46:47 +0000796static PyObject *
Martin v. Löwis560da622001-11-24 09:24:51 +0000797gc_get_referrers(PyObject *self, PyObject *args)
Neil Schemenauer48c70342001-08-09 15:38:31 +0000798{
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000799 int i;
Neil Schemenauer48c70342001-08-09 15:38:31 +0000800 PyObject *result = PyList_New(0);
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000801 for (i = 0; i < NUM_GENERATIONS; i++) {
802 if (!(gc_referrers_for(args, GEN_HEAD(i), result))) {
803 Py_DECREF(result);
804 return NULL;
805 }
Neil Schemenauer17e7be62001-08-10 14:46:47 +0000806 }
Neil Schemenauer48c70342001-08-09 15:38:31 +0000807 return result;
808}
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000809
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000810PyDoc_STRVAR(gc_get_objects__doc__,
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000811"get_objects() -> [...]\n"
812"\n"
813"Return a list of objects tracked by the collector (excluding the list\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000814"returned).\n");
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000815
816/* appending objects in a GC list to a Python list */
Martin v. Löwis155aad12001-12-02 12:21:34 +0000817static int
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000818append_objects(PyObject *py_list, PyGC_Head *gc_list)
819{
820 PyGC_Head *gc;
Tim Peters9e4ca102001-10-11 18:31:31 +0000821 for (gc = gc_list->gc.gc_next; gc != gc_list; gc = gc->gc.gc_next) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000822 PyObject *op = FROM_GC(gc);
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000823 if (op != py_list) {
Martin v. Löwis155aad12001-12-02 12:21:34 +0000824 if (PyList_Append(py_list, op)) {
825 return -1; /* exception */
826 }
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000827 }
828 }
Martin v. Löwis155aad12001-12-02 12:21:34 +0000829 return 0;
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000830}
831
832static PyObject *
833gc_get_objects(PyObject *self, PyObject *args)
834{
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000835 int i;
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000836 PyObject* result;
837
838 if (!PyArg_ParseTuple(args, ":get_objects")) /* check no args */
839 return NULL;
840 result = PyList_New(0);
Martin v. Löwisf8a6f242001-12-02 18:31:02 +0000841 if (result == NULL) {
842 return NULL;
843 }
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000844 for (i = 0; i < NUM_GENERATIONS; i++) {
845 if (append_objects(result, GEN_HEAD(i))) {
846 Py_DECREF(result);
847 return NULL;
848 }
Martin v. Löwis155aad12001-12-02 12:21:34 +0000849 }
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000850 return result;
851}
852
853
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000854PyDoc_STRVAR(gc__doc__,
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000855"This module provides access to the garbage collector for reference cycles.\n"
856"\n"
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000857"enable() -- Enable automatic garbage collection.\n"
858"disable() -- Disable automatic garbage collection.\n"
859"isenabled() -- Returns true if automatic collection is enabled.\n"
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000860"collect() -- Do a full collection right now.\n"
861"set_debug() -- Set debugging flags.\n"
862"get_debug() -- Get debugging flags.\n"
863"set_threshold() -- Set the collection thresholds.\n"
864"get_threshold() -- Return the current the collection thresholds.\n"
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000865"get_objects() -- Return a list of all objects tracked by the collector.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000866"get_referrers() -- Return the list of objects that refer to an object.\n");
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000867
868static PyMethodDef GcMethods[] = {
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000869 {"enable", gc_enable, METH_VARARGS, gc_enable__doc__},
870 {"disable", gc_disable, METH_VARARGS, gc_disable__doc__},
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000871 {"isenabled", gc_isenabled, METH_VARARGS, gc_isenabled__doc__},
872 {"set_debug", gc_set_debug, METH_VARARGS, gc_set_debug__doc__},
873 {"get_debug", gc_get_debug, METH_VARARGS, gc_get_debug__doc__},
874 {"set_threshold", gc_set_thresh, METH_VARARGS, gc_set_thresh__doc__},
875 {"get_threshold", gc_get_thresh, METH_VARARGS, gc_get_thresh__doc__},
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000876 {"collect", gc_collect, METH_VARARGS, gc_collect__doc__},
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000877 {"get_objects", gc_get_objects,METH_VARARGS, gc_get_objects__doc__},
Martin v. Löwis560da622001-11-24 09:24:51 +0000878 {"get_referrers", gc_get_referrers, METH_VARARGS,
879 gc_get_referrers__doc__},
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000880 {NULL, NULL} /* Sentinel */
881};
882
883void
884initgc(void)
885{
886 PyObject *m;
887 PyObject *d;
888
889 m = Py_InitModule4("gc",
890 GcMethods,
891 gc__doc__,
892 NULL,
893 PYTHON_API_VERSION);
894 d = PyModule_GetDict(m);
895 if (garbage == NULL) {
896 garbage = PyList_New(0);
897 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000898 PyDict_SetItemString(d, "garbage", garbage);
899 PyDict_SetItemString(d, "DEBUG_STATS",
900 PyInt_FromLong(DEBUG_STATS));
901 PyDict_SetItemString(d, "DEBUG_COLLECTABLE",
902 PyInt_FromLong(DEBUG_COLLECTABLE));
903 PyDict_SetItemString(d, "DEBUG_UNCOLLECTABLE",
904 PyInt_FromLong(DEBUG_UNCOLLECTABLE));
905 PyDict_SetItemString(d, "DEBUG_INSTANCES",
906 PyInt_FromLong(DEBUG_INSTANCES));
907 PyDict_SetItemString(d, "DEBUG_OBJECTS",
908 PyInt_FromLong(DEBUG_OBJECTS));
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000909 PyDict_SetItemString(d, "DEBUG_SAVEALL",
910 PyInt_FromLong(DEBUG_SAVEALL));
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000911 PyDict_SetItemString(d, "DEBUG_LEAK",
912 PyInt_FromLong(DEBUG_LEAK));
913}
914
Neil Schemenauer43411b52001-08-30 00:05:51 +0000915/* for debugging */
916void _PyGC_Dump(PyGC_Head *g)
917{
918 _PyObject_Dump(FROM_GC(g));
919}
920
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000921#endif /* WITH_CYCLE_GC */
Neil Schemenauer43411b52001-08-30 00:05:51 +0000922
923/* extension modules might be compiled with GC support so these
924 functions must always be available */
925
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000926#undef PyObject_GC_Track
927#undef PyObject_GC_UnTrack
928#undef PyObject_GC_Del
929#undef _PyObject_GC_Malloc
930
Neil Schemenauer43411b52001-08-30 00:05:51 +0000931void
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000932PyObject_GC_Track(void *op)
Neil Schemenauer43411b52001-08-30 00:05:51 +0000933{
934 _PyObject_GC_TRACK(op);
935}
936
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000937/* for binary compatibility with 2.2 */
Neil Schemenauer43411b52001-08-30 00:05:51 +0000938void
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000939_PyObject_GC_Track(PyObject *op)
940{
941 PyObject_GC_Track(op);
942}
943
944void
945PyObject_GC_UnTrack(void *op)
Neil Schemenauer43411b52001-08-30 00:05:51 +0000946{
Neil Schemenauerb8833102002-03-29 03:04:25 +0000947#ifdef WITH_CYCLE_GC
Neil Schemenauera2b11ec2002-05-21 15:53:24 +0000948 if (IS_TRACKED(op))
Guido van Rossumff413af2002-03-28 20:34:59 +0000949 _PyObject_GC_UNTRACK(op);
Neil Schemenauerb8833102002-03-29 03:04:25 +0000950#endif
Neil Schemenauer43411b52001-08-30 00:05:51 +0000951}
952
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000953/* for binary compatibility with 2.2 */
954void
955_PyObject_GC_UnTrack(PyObject *op)
956{
957 PyObject_GC_UnTrack(op);
958}
959
Neil Schemenauer43411b52001-08-30 00:05:51 +0000960PyObject *
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000961_PyObject_GC_Malloc(size_t basicsize)
Neil Schemenauer43411b52001-08-30 00:05:51 +0000962{
963 PyObject *op;
964#ifdef WITH_CYCLE_GC
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000965 PyGC_Head *g = PyObject_MALLOC(sizeof(PyGC_Head) + basicsize);
Neil Schemenauer43411b52001-08-30 00:05:51 +0000966 if (g == NULL)
Jeremy Hylton8a135182002-06-06 23:23:55 +0000967 return PyErr_NoMemory();
Tim Peters9e4ca102001-10-11 18:31:31 +0000968 g->gc.gc_next = NULL;
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000969 generations[0].count++; /* number of allocated GC objects */
970 if (generations[0].count > generations[0].threshold &&
Neil Schemenauer43411b52001-08-30 00:05:51 +0000971 enabled &&
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000972 generations[0].threshold &&
Neil Schemenauer43411b52001-08-30 00:05:51 +0000973 !collecting &&
974 !PyErr_Occurred()) {
975 collecting = 1;
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000976 collect_generations();
Neil Schemenauer43411b52001-08-30 00:05:51 +0000977 collecting = 0;
978 }
979 op = FROM_GC(g);
980#else
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000981 op = PyObject_MALLOC(basicsize);
Neil Schemenauer43411b52001-08-30 00:05:51 +0000982 if (op == NULL)
Jeremy Hylton8a135182002-06-06 23:23:55 +0000983 return PyErr_NoMemory();
Neil Schemenauer43411b52001-08-30 00:05:51 +0000984
985#endif
986 return op;
987}
988
989PyObject *
990_PyObject_GC_New(PyTypeObject *tp)
991{
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000992 PyObject *op = _PyObject_GC_Malloc(_PyObject_SIZE(tp));
Tim Petersfa8efab2002-04-28 01:57:25 +0000993 if (op != NULL)
994 op = PyObject_INIT(op, tp);
995 return op;
Neil Schemenauer43411b52001-08-30 00:05:51 +0000996}
997
998PyVarObject *
Tim Peters6d483d32001-10-06 21:27:34 +0000999_PyObject_GC_NewVar(PyTypeObject *tp, int nitems)
Neil Schemenauer43411b52001-08-30 00:05:51 +00001000{
Neil Schemenauerfec4eb12002-04-12 02:41:03 +00001001 const size_t size = _PyObject_VAR_SIZE(tp, nitems);
1002 PyVarObject *op = (PyVarObject *) _PyObject_GC_Malloc(size);
Tim Petersfa8efab2002-04-28 01:57:25 +00001003 if (op != NULL)
1004 op = PyObject_INIT_VAR(op, tp, nitems);
1005 return op;
Neil Schemenauer43411b52001-08-30 00:05:51 +00001006}
1007
1008PyVarObject *
Tim Peters6d483d32001-10-06 21:27:34 +00001009_PyObject_GC_Resize(PyVarObject *op, int nitems)
Neil Schemenauer43411b52001-08-30 00:05:51 +00001010{
Tim Petersf2a67da2001-10-07 03:54:51 +00001011 const size_t basicsize = _PyObject_VAR_SIZE(op->ob_type, nitems);
Neil Schemenauer43411b52001-08-30 00:05:51 +00001012#ifdef WITH_CYCLE_GC
1013 PyGC_Head *g = AS_GC(op);
Neil Schemenauerfec4eb12002-04-12 02:41:03 +00001014 g = PyObject_REALLOC(g, sizeof(PyGC_Head) + basicsize);
Neil Schemenauer43411b52001-08-30 00:05:51 +00001015 if (g == NULL)
1016 return (PyVarObject *)PyErr_NoMemory();
1017 op = (PyVarObject *) FROM_GC(g);
1018#else
Neil Schemenauerfec4eb12002-04-12 02:41:03 +00001019 op = PyObject_REALLOC(op, basicsize);
Neil Schemenauer43411b52001-08-30 00:05:51 +00001020 if (op == NULL)
1021 return (PyVarObject *)PyErr_NoMemory();
1022#endif
Tim Peters6d483d32001-10-06 21:27:34 +00001023 op->ob_size = nitems;
Neil Schemenauer43411b52001-08-30 00:05:51 +00001024 return op;
1025}
1026
1027void
Neil Schemenauerfec4eb12002-04-12 02:41:03 +00001028PyObject_GC_Del(void *op)
Neil Schemenauer43411b52001-08-30 00:05:51 +00001029{
1030#ifdef WITH_CYCLE_GC
1031 PyGC_Head *g = AS_GC(op);
Neil Schemenauera2b11ec2002-05-21 15:53:24 +00001032 if (IS_TRACKED(op))
Neil Schemenauer43411b52001-08-30 00:05:51 +00001033 gc_list_remove(g);
Neil Schemenauer2880ae52002-05-04 05:35:20 +00001034 if (generations[0].count > 0) {
1035 generations[0].count--;
Neil Schemenauer43411b52001-08-30 00:05:51 +00001036 }
Neil Schemenauerfec4eb12002-04-12 02:41:03 +00001037 PyObject_FREE(g);
Neil Schemenauer43411b52001-08-30 00:05:51 +00001038#else
Neil Schemenauerfec4eb12002-04-12 02:41:03 +00001039 PyObject_FREE(op);
Neil Schemenauer43411b52001-08-30 00:05:51 +00001040#endif
1041}
1042
Neil Schemenauerfec4eb12002-04-12 02:41:03 +00001043/* for binary compatibility with 2.2 */
1044#undef _PyObject_GC_Del
1045void
1046_PyObject_GC_Del(PyObject *op)
1047{
1048 PyObject_GC_Del(op);
1049}