blob: 7148e4c8e936b31605d74238a472258883904fe7 [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. */
Tim Petersea405632002-07-02 00:52:30 +000085#define GC_UNTRACKED _PyGC_REFS_UNTRACKED
86#define GC_REACHABLE _PyGC_REFS_REACHABLE
87#define GC_TENTATIVELY_UNREACHABLE _PyGC_REFS_TENTATIVELY_UNREACHABLE
Tim Peters19b74c72002-07-01 03:52:19 +000088
89#define IS_REACHABLE(o) ((AS_GC(o))->gc.gc_refs == GC_REACHABLE)
90#define IS_TENTATIVELY_UNREACHABLE(o) ( \
91 (AS_GC(o))->gc.gc_refs == GC_TENTATIVELY_UNREACHABLE)
Neil Schemenauera2b11ec2002-05-21 15:53:24 +000092
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000093/* list of uncollectable objects */
94static PyObject *garbage;
95
Jeremy Hyltonb709df32000-09-01 02:47:25 +000096/* Python string to use if unhandled exception occurs */
97static PyObject *gc_str;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000098
99/*** list functions ***/
100
101static void
102gc_list_init(PyGC_Head *list)
103{
Tim Peters9e4ca102001-10-11 18:31:31 +0000104 list->gc.gc_prev = list;
105 list->gc.gc_next = list;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000106}
107
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000108static int
109gc_list_is_empty(PyGC_Head *list)
110{
111 return (list->gc.gc_next == list);
112}
113
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000114static void
115gc_list_append(PyGC_Head *node, PyGC_Head *list)
116{
Tim Peters9e4ca102001-10-11 18:31:31 +0000117 node->gc.gc_next = list;
118 node->gc.gc_prev = list->gc.gc_prev;
119 node->gc.gc_prev->gc.gc_next = node;
120 list->gc.gc_prev = node;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000121}
122
123static void
124gc_list_remove(PyGC_Head *node)
125{
Tim Peters9e4ca102001-10-11 18:31:31 +0000126 node->gc.gc_prev->gc.gc_next = node->gc.gc_next;
127 node->gc.gc_next->gc.gc_prev = node->gc.gc_prev;
128 node->gc.gc_next = NULL; /* object is not currently tracked */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000129}
130
Tim Peters88396172002-06-30 17:56:40 +0000131static void
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000132gc_list_move(PyGC_Head *from, PyGC_Head *to)
133{
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000134 if (gc_list_is_empty(from)) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000135 gc_list_init(to);
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000136 }
137 else {
Tim Peters9e4ca102001-10-11 18:31:31 +0000138 to->gc.gc_next = from->gc.gc_next;
139 to->gc.gc_next->gc.gc_prev = to;
140 to->gc.gc_prev = from->gc.gc_prev;
141 to->gc.gc_prev->gc.gc_next = to;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000142 }
143 gc_list_init(from);
144}
145
146/* append a list onto another list, from becomes an empty list */
147static void
148gc_list_merge(PyGC_Head *from, PyGC_Head *to)
149{
150 PyGC_Head *tail;
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000151 if (!gc_list_is_empty(from)) {
Tim Peters9e4ca102001-10-11 18:31:31 +0000152 tail = to->gc.gc_prev;
153 tail->gc.gc_next = from->gc.gc_next;
154 tail->gc.gc_next->gc.gc_prev = tail;
155 to->gc.gc_prev = from->gc.gc_prev;
156 to->gc.gc_prev->gc.gc_next = to;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000157 }
158 gc_list_init(from);
159}
160
161static long
162gc_list_size(PyGC_Head *list)
163{
164 PyGC_Head *gc;
165 long n = 0;
Tim Peters9e4ca102001-10-11 18:31:31 +0000166 for (gc = list->gc.gc_next; gc != list; gc = gc->gc.gc_next) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000167 n++;
168 }
169 return n;
170}
171
172/*** end of list stuff ***/
173
174
Tim Peters19b74c72002-07-01 03:52:19 +0000175/* Set all gc_refs = ob_refcnt. After this, gc_refs is > 0 for all objects
176 * in containers, and is GC_REACHABLE for all tracked gc objects not in
177 * containers.
Tim Peters88396172002-06-30 17:56:40 +0000178 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000179static void
180update_refs(PyGC_Head *containers)
181{
Tim Peters9e4ca102001-10-11 18:31:31 +0000182 PyGC_Head *gc = containers->gc.gc_next;
Tim Petersea405632002-07-02 00:52:30 +0000183 for (; gc != containers; gc = gc->gc.gc_next) {
184 assert(gc->gc.gc_refs == GC_REACHABLE);
Tim Peters9e4ca102001-10-11 18:31:31 +0000185 gc->gc.gc_refs = FROM_GC(gc)->ob_refcnt;
Tim Petersea405632002-07-02 00:52:30 +0000186 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000187}
188
Tim Peters19b74c72002-07-01 03:52:19 +0000189/* A traversal callback for subtract_refs. */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000190static int
191visit_decref(PyObject *op, void *data)
192{
Tim Peters93cd83e2002-06-30 21:31:03 +0000193 assert(op != NULL);
Tim Peters19b74c72002-07-01 03:52:19 +0000194 if (PyObject_IS_GC(op)) {
195 PyGC_Head *gc = AS_GC(op);
196 /* We're only interested in gc_refs for objects in the
197 * generation being collected, which can be recognized
198 * because only they have positive gc_refs.
199 */
200 if (gc->gc.gc_refs > 0)
201 gc->gc.gc_refs--;
202 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000203 return 0;
204}
205
Tim Peters19b74c72002-07-01 03:52:19 +0000206/* Subtract internal references from gc_refs. After this, gc_refs is >= 0
207 * for all objects in containers, and is GC_REACHABLE for all tracked gc
208 * objects not in containers. The ones with gc_refs > 0 are directly
209 * reachable from outside containers, and so can't be collected.
210 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000211static void
212subtract_refs(PyGC_Head *containers)
213{
214 traverseproc traverse;
Tim Peters9e4ca102001-10-11 18:31:31 +0000215 PyGC_Head *gc = containers->gc.gc_next;
216 for (; gc != containers; gc=gc->gc.gc_next) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000217 traverse = FROM_GC(gc)->ob_type->tp_traverse;
218 (void) traverse(FROM_GC(gc),
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000219 (visitproc)visit_decref,
220 NULL);
221 }
222}
223
Tim Peters19b74c72002-07-01 03:52:19 +0000224/* A traversal callback for move_unreachable. */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000225static int
Tim Peters19b74c72002-07-01 03:52:19 +0000226visit_reachable(PyObject *op, PyGC_Head *reachable)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000227{
Tim Petersea405632002-07-02 00:52:30 +0000228 if (PyObject_IS_GC(op)) {
Tim Peters19b74c72002-07-01 03:52:19 +0000229 PyGC_Head *gc = AS_GC(op);
230 const int gc_refs = gc->gc.gc_refs;
231
232 if (gc_refs == 0) {
233 /* This is in move_unreachable's 'young' list, but
234 * the traversal hasn't yet gotten to it. All
235 * we need to do is tell move_unreachable that it's
236 * reachable.
237 */
238 gc->gc.gc_refs = 1;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000239 }
Tim Peters19b74c72002-07-01 03:52:19 +0000240 else if (gc_refs == GC_TENTATIVELY_UNREACHABLE) {
241 /* This had gc_refs = 0 when move_unreachable got
242 * to it, but turns out it's reachable after all.
243 * Move it back to move_unreachable's 'young' list,
244 * and move_unreachable will eventually get to it
245 * again.
246 */
247 gc_list_remove(gc);
248 gc_list_append(gc, reachable);
249 gc->gc.gc_refs = 1;
250 }
251 /* Else there's nothing to do.
252 * If gc_refs > 0, it must be in move_unreachable's 'young'
253 * list, and move_unreachable will eventually get to it.
254 * If gc_refs == GC_REACHABLE, it's either in some other
255 * generation so we don't care about it, or move_unreachable
Tim Petersea405632002-07-02 00:52:30 +0000256 * already deat with it.
257 * If gc_refs == GC_UNTRACKED, it must be ignored.
Tim Peters19b74c72002-07-01 03:52:19 +0000258 */
Tim Petersea405632002-07-02 00:52:30 +0000259 else {
260 assert(gc_refs > 0
261 || gc_refs == GC_REACHABLE
262 || gc_refs == GC_UNTRACKED);
263 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000264 }
265 return 0;
266}
267
Tim Peters19b74c72002-07-01 03:52:19 +0000268/* Move the unreachable objects from young to unreachable. After this,
269 * all objects in young have gc_refs = GC_REACHABLE, and all objects in
270 * unreachable have gc_refs = GC_TENTATIVELY_UNREACHABLE. All tracked
271 * gc objects not in young or unreachable still have gc_refs = GC_REACHABLE.
272 * All objects in young after this are directly or indirectly reachable
273 * from outside the original young; and all objects in unreachable are
274 * not.
Tim Peters88396172002-06-30 17:56:40 +0000275 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000276static void
Tim Peters19b74c72002-07-01 03:52:19 +0000277move_unreachable(PyGC_Head *young, PyGC_Head *unreachable)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000278{
Tim Peters19b74c72002-07-01 03:52:19 +0000279 PyGC_Head *gc = young->gc.gc_next;
280
281 /* Invariants: all objects "to the left" of us in young have gc_refs
282 * = GC_REACHABLE, and are indeed reachable (directly or indirectly)
283 * from outside the young list as it was at entry. All other objects
284 * from the original young "to the left" of us are in unreachable now,
285 * and have gc_refs = GC_TENTATIVELY_UNREACHABLE. All objects to the
286 * left of us in 'young' now have been scanned, and no objects here
287 * or to the right have been scanned yet.
288 */
289
290 while (gc != young) {
291 PyGC_Head *next;
292
293 if (gc->gc.gc_refs == 0) {
294 /* This *may* be unreachable. To make progress,
295 * assume it is. gc isn't directly reachable from
296 * any object we've already traversed, but may be
297 * reachable from an object we haven't gotten to yet.
298 * visit_reachable will eventually move gc back into
299 * young if that's so, and we'll see it again.
300 */
301 next = gc->gc.gc_next;
302 gc_list_remove(gc);
303 gc_list_append(gc, unreachable);
304 gc->gc.gc_refs = GC_TENTATIVELY_UNREACHABLE;
305 }
306 else {
307 /* gc is definitely reachable from outside the
308 * original 'young'. Mark it as such, and traverse
309 * its pointers to find any other objects that may
310 * be directly reachable from it. Note that the
311 * call to tp_traverse may append objects to young,
312 * so we have to wait until it returns to determine
313 * the next object to visit.
314 */
315 PyObject *op = FROM_GC(gc);
316 traverseproc traverse = op->ob_type->tp_traverse;
317 gc->gc.gc_refs = GC_REACHABLE;
318 (void) traverse(op,
319 (visitproc)visit_reachable,
320 (void *)young);
321 next = gc->gc.gc_next;
322 }
323 gc = next;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000324 }
325}
326
Tim Peters88396172002-06-30 17:56:40 +0000327/* return true if object has a finalization method */
Neil Schemenauera765c122001-11-01 17:35:23 +0000328static int
329has_finalizer(PyObject *op)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000330{
Jeremy Hylton06257772000-08-31 15:10:24 +0000331 static PyObject *delstr = NULL;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000332 if (delstr == NULL) {
333 delstr = PyString_InternFromString("__del__");
Jeremy Hylton06257772000-08-31 15:10:24 +0000334 if (delstr == NULL)
335 Py_FatalError("PyGC: can't initialize __del__ string");
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000336 }
Tim Petersdb865612001-11-01 19:35:45 +0000337 return (PyInstance_Check(op) ||
338 PyType_HasFeature(op->ob_type, Py_TPFLAGS_HEAPTYPE))
339 && PyObject_HasAttr(op, delstr);
Neil Schemenauera765c122001-11-01 17:35:23 +0000340}
341
342/* Move all objects with finalizers (instances with __del__) */
343static void
344move_finalizers(PyGC_Head *unreachable, PyGC_Head *finalizers)
345{
346 PyGC_Head *next;
347 PyGC_Head *gc = unreachable->gc.gc_next;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000348 for (; gc != unreachable; gc=next) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000349 PyObject *op = FROM_GC(gc);
Tim Peters9e4ca102001-10-11 18:31:31 +0000350 next = gc->gc.gc_next;
Neil Schemenauera765c122001-11-01 17:35:23 +0000351 if (has_finalizer(op)) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000352 gc_list_remove(gc);
353 gc_list_append(gc, finalizers);
Tim Peters19b74c72002-07-01 03:52:19 +0000354 gc->gc.gc_refs = GC_REACHABLE;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000355 }
356 }
357}
358
Tim Peters19b74c72002-07-01 03:52:19 +0000359/* A traversal callback for move_finalizer_reachable. */
360static int
361visit_move(PyObject *op, PyGC_Head *tolist)
362{
363 if (PyObject_IS_GC(op)) {
Tim Petersea405632002-07-02 00:52:30 +0000364 if (IS_TENTATIVELY_UNREACHABLE(op)) {
Tim Peters19b74c72002-07-01 03:52:19 +0000365 PyGC_Head *gc = AS_GC(op);
366 gc_list_remove(gc);
367 gc_list_append(gc, tolist);
368 gc->gc.gc_refs = GC_REACHABLE;
369 }
370 }
371 return 0;
372}
373
374/* Move objects that are reachable from finalizers, from the unreachable set
375 * into the finalizers set.
376 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000377static void
378move_finalizer_reachable(PyGC_Head *finalizers)
379{
380 traverseproc traverse;
Tim Peters9e4ca102001-10-11 18:31:31 +0000381 PyGC_Head *gc = finalizers->gc.gc_next;
382 for (; gc != finalizers; gc=gc->gc.gc_next) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000383 /* careful, finalizers list is growing here */
Neil Schemenauer43411b52001-08-30 00:05:51 +0000384 traverse = FROM_GC(gc)->ob_type->tp_traverse;
Tim Peters88396172002-06-30 17:56:40 +0000385 (void) traverse(FROM_GC(gc),
Neil Schemenauer43411b52001-08-30 00:05:51 +0000386 (visitproc)visit_move,
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000387 (void *)finalizers);
388 }
389}
390
391static void
Jeremy Hylton06257772000-08-31 15:10:24 +0000392debug_instance(char *msg, PyInstanceObject *inst)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000393{
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000394 char *cname;
Neil Schemenauera765c122001-11-01 17:35:23 +0000395 /* simple version of instance_repr */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000396 PyObject *classname = inst->in_class->cl_name;
397 if (classname != NULL && PyString_Check(classname))
398 cname = PyString_AsString(classname);
399 else
400 cname = "?";
Jeremy Hylton06257772000-08-31 15:10:24 +0000401 PySys_WriteStderr("gc: %.100s <%.100s instance at %p>\n",
402 msg, cname, inst);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000403}
404
405static void
Jeremy Hylton06257772000-08-31 15:10:24 +0000406debug_cycle(char *msg, PyObject *op)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000407{
408 if ((debug & DEBUG_INSTANCES) && PyInstance_Check(op)) {
Jeremy Hylton06257772000-08-31 15:10:24 +0000409 debug_instance(msg, (PyInstanceObject *)op);
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000410 }
411 else if (debug & DEBUG_OBJECTS) {
Jeremy Hylton06257772000-08-31 15:10:24 +0000412 PySys_WriteStderr("gc: %.100s <%.100s %p>\n",
413 msg, op->ob_type->tp_name, op);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000414 }
415}
416
417/* Handle uncollectable garbage (cycles with finalizers). */
418static void
419handle_finalizers(PyGC_Head *finalizers, PyGC_Head *old)
420{
421 PyGC_Head *gc;
422 if (garbage == NULL) {
423 garbage = PyList_New(0);
424 }
Tim Peters9e4ca102001-10-11 18:31:31 +0000425 for (gc = finalizers->gc.gc_next; gc != finalizers;
426 gc = finalizers->gc.gc_next) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000427 PyObject *op = FROM_GC(gc);
Neil Schemenauera765c122001-11-01 17:35:23 +0000428 if ((debug & DEBUG_SAVEALL) || has_finalizer(op)) {
429 /* If SAVEALL is not set then just append objects with
430 * finalizers to the list of garbage. All objects in
431 * the finalizers list are reachable from those
Tim Peters19b74c72002-07-01 03:52:19 +0000432 * objects.
433 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000434 PyList_Append(garbage, op);
435 }
Tim Peters88396172002-06-30 17:56:40 +0000436 /* object is now reachable again */
Tim Peters19b74c72002-07-01 03:52:19 +0000437 assert(IS_REACHABLE(op));
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000438 gc_list_remove(gc);
439 gc_list_append(gc, old);
440 }
441}
442
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000443/* Break reference cycles by clearing the containers involved. This is
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000444 * tricky business as the lists can be changing and we don't know which
Tim Peters19b74c72002-07-01 03:52:19 +0000445 * objects may be freed. It is possible I screwed something up here.
446 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000447static void
448delete_garbage(PyGC_Head *unreachable, PyGC_Head *old)
449{
450 inquiry clear;
451
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000452 while (!gc_list_is_empty(unreachable)) {
Tim Peters9e4ca102001-10-11 18:31:31 +0000453 PyGC_Head *gc = unreachable->gc.gc_next;
Neil Schemenauer43411b52001-08-30 00:05:51 +0000454 PyObject *op = FROM_GC(gc);
Tim Peters88396172002-06-30 17:56:40 +0000455
Tim Peters19b74c72002-07-01 03:52:19 +0000456 assert(IS_TENTATIVELY_UNREACHABLE(op));
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000457 if (debug & DEBUG_SAVEALL) {
458 PyList_Append(garbage, op);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000459 }
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000460 else {
461 if ((clear = op->ob_type->tp_clear) != NULL) {
462 Py_INCREF(op);
Jeremy Hylton8a135182002-06-06 23:23:55 +0000463 clear(op);
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000464 Py_DECREF(op);
465 }
466 }
Tim Peters9e4ca102001-10-11 18:31:31 +0000467 if (unreachable->gc.gc_next == gc) {
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000468 /* object is still alive, move it, it may die later */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000469 gc_list_remove(gc);
470 gc_list_append(gc, old);
Tim Peters19b74c72002-07-01 03:52:19 +0000471 gc->gc.gc_refs = GC_REACHABLE;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000472 }
473 }
474}
475
476/* This is the main function. Read this to understand how the
477 * collection process works. */
478static long
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000479collect(int generation)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000480{
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000481 int i;
Tim Peters19b74c72002-07-01 03:52:19 +0000482 long m = 0; /* # objects collected */
483 long n = 0; /* # unreachable objects that couldn't be collected */
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000484 PyGC_Head *young; /* the generation we are examining */
485 PyGC_Head *old; /* next older generation */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000486 PyGC_Head unreachable;
487 PyGC_Head finalizers;
488 PyGC_Head *gc;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000489
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000490 if (debug & DEBUG_STATS) {
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000491 PySys_WriteStderr("gc: collecting generation %d...\n",
492 generation);
493 PySys_WriteStderr("gc: objects in each generation:");
494 for (i = 0; i < NUM_GENERATIONS; i++) {
495 PySys_WriteStderr(" %ld", gc_list_size(GEN_HEAD(i)));
496 }
497 PySys_WriteStderr("\n");
498 }
499
500 /* update collection and allocation counters */
501 if (generation+1 < NUM_GENERATIONS)
502 generations[generation+1].count += 1;
503 for (i = 0; i <= generation; i++)
Neil Schemenauerc9051642002-06-28 19:16:04 +0000504 generations[i].count = 0;
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000505
506 /* merge younger generations with one we are currently collecting */
507 for (i = 0; i < generation; i++) {
508 gc_list_merge(GEN_HEAD(i), GEN_HEAD(generation));
509 }
510
511 /* handy references */
512 young = GEN_HEAD(generation);
Tim Peters19b74c72002-07-01 03:52:19 +0000513 if (generation < NUM_GENERATIONS-1)
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000514 old = GEN_HEAD(generation+1);
Tim Peters19b74c72002-07-01 03:52:19 +0000515 else
516 old = young;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000517
518 /* Using ob_refcnt and gc_refs, calculate which objects in the
519 * container set are reachable from outside the set (ie. have a
520 * refcount greater than 0 when all the references within the
Tim Peters19b74c72002-07-01 03:52:19 +0000521 * set are taken into account
522 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000523 update_refs(young);
524 subtract_refs(young);
525
Tim Peters19b74c72002-07-01 03:52:19 +0000526 /* Leave everything reachable from outside young in young, and move
527 * everything else (in young) to unreachable.
528 * NOTE: This used to move the reachable objects into a reachable
529 * set instead. But most things usually turn out to be reachable,
530 * so it's more efficient to move the unreachable things.
531 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000532 gc_list_init(&unreachable);
Tim Peters19b74c72002-07-01 03:52:19 +0000533 move_unreachable(young, &unreachable);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000534
Tim Peters19b74c72002-07-01 03:52:19 +0000535 /* Move reachable objects to next generation. */
536 if (young != old)
537 gc_list_merge(young, old);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000538
Tim Peters19b74c72002-07-01 03:52:19 +0000539 /* All objects in unreachable are trash, but objects reachable from
540 * finalizers can't safely be deleted. Python programmers should take
541 * care not to create such things. For Python, finalizers means
542 * instance objects with __del__ methods.
543 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000544 gc_list_init(&finalizers);
545 move_finalizers(&unreachable, &finalizers);
546 move_finalizer_reachable(&finalizers);
547
548 /* Collect statistics on collectable objects found and print
549 * debugging information. */
Tim Peters9e4ca102001-10-11 18:31:31 +0000550 for (gc = unreachable.gc.gc_next; gc != &unreachable;
551 gc = gc->gc.gc_next) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000552 m++;
Jeremy Hylton06257772000-08-31 15:10:24 +0000553 if (debug & DEBUG_COLLECTABLE) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000554 debug_cycle("collectable", FROM_GC(gc));
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000555 }
556 }
Tim Peters19b74c72002-07-01 03:52:19 +0000557 /* Call tp_clear on objects in the collectable set. This will cause
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000558 * the reference cycles to be broken. It may also cause some objects in
559 * finalizers to be freed */
560 delete_garbage(&unreachable, old);
561
562 /* Collect statistics on uncollectable objects found and print
563 * debugging information. */
Tim Peters9e4ca102001-10-11 18:31:31 +0000564 for (gc = finalizers.gc.gc_next; gc != &finalizers;
565 gc = gc->gc.gc_next) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000566 n++;
Jeremy Hylton06257772000-08-31 15:10:24 +0000567 if (debug & DEBUG_UNCOLLECTABLE) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000568 debug_cycle("uncollectable", FROM_GC(gc));
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000569 }
570 }
Jeremy Hylton06257772000-08-31 15:10:24 +0000571 if (debug & DEBUG_STATS) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000572 if (m == 0 && n == 0) {
Jeremy Hylton06257772000-08-31 15:10:24 +0000573 PySys_WriteStderr("gc: done.\n");
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000574 }
575 else {
Jeremy Hylton06257772000-08-31 15:10:24 +0000576 PySys_WriteStderr(
577 "gc: done, %ld unreachable, %ld uncollectable.\n",
578 n+m, n);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000579 }
580 }
581
582 /* Append instances in the uncollectable set to a Python
583 * reachable list of garbage. The programmer has to deal with
584 * this if they insist on creating this type of structure. */
585 handle_finalizers(&finalizers, old);
586
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000587 if (PyErr_Occurred()) {
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000588 if (gc_str == NULL) {
589 gc_str = PyString_FromString("garbage collection");
590 }
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000591 PyErr_WriteUnraisable(gc_str);
592 Py_FatalError("unexpected exception during garbage collection");
593 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000594 return n+m;
595}
596
597static long
598collect_generations(void)
599{
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000600 int i;
Vladimir Marangozovb16714b2000-07-10 05:37:39 +0000601 long n = 0;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000602
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000603 /* Find the oldest generation (higest numbered) where the count
604 * exceeds the threshold. Objects in the that generation and
605 * generations younger than it will be collected. */
606 for (i = NUM_GENERATIONS-1; i >= 0; i--) {
607 if (generations[i].count > generations[i].threshold) {
608 n = collect(i);
609 break;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000610 }
611 }
612 return n;
613}
614
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000615PyDoc_STRVAR(gc_enable__doc__,
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000616"enable() -> None\n"
617"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000618"Enable automatic garbage collection.\n");
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000619
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000620static PyObject *
621gc_enable(PyObject *self, PyObject *args)
622{
623
624 if (!PyArg_ParseTuple(args, ":enable")) /* check no args */
625 return NULL;
626
627 enabled = 1;
628
629 Py_INCREF(Py_None);
630 return Py_None;
631}
632
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000633PyDoc_STRVAR(gc_disable__doc__,
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000634"disable() -> None\n"
635"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000636"Disable automatic garbage collection.\n");
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000637
638static PyObject *
639gc_disable(PyObject *self, PyObject *args)
640{
641
642 if (!PyArg_ParseTuple(args, ":disable")) /* check no args */
643 return NULL;
644
645 enabled = 0;
646
647 Py_INCREF(Py_None);
648 return Py_None;
649}
650
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000651PyDoc_STRVAR(gc_isenabled__doc__,
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000652"isenabled() -> status\n"
653"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000654"Returns true if automatic garbage collection is enabled.\n");
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000655
656static PyObject *
657gc_isenabled(PyObject *self, PyObject *args)
658{
659
660 if (!PyArg_ParseTuple(args, ":isenabled")) /* check no args */
661 return NULL;
662
663 return Py_BuildValue("i", enabled);
664}
665
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000666PyDoc_STRVAR(gc_collect__doc__,
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000667"collect() -> n\n"
668"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000669"Run a full collection. The number of unreachable objects is returned.\n");
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000670
671static PyObject *
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000672gc_collect(PyObject *self, PyObject *args)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000673{
674 long n;
675
Fred Drakecc1be242000-07-12 04:42:23 +0000676 if (!PyArg_ParseTuple(args, ":collect")) /* check no args */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000677 return NULL;
678
Neil Schemenauere8c40cb2001-10-31 23:09:35 +0000679 if (collecting) {
680 n = 0; /* already collecting, don't do anything */
681 }
682 else {
683 collecting = 1;
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000684 n = collect(NUM_GENERATIONS - 1);
Neil Schemenauere8c40cb2001-10-31 23:09:35 +0000685 collecting = 0;
686 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000687
Neil Schemenauer7760cff2000-09-22 22:35:36 +0000688 return Py_BuildValue("l", n);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000689}
690
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000691PyDoc_STRVAR(gc_set_debug__doc__,
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000692"set_debug(flags) -> None\n"
693"\n"
694"Set the garbage collection debugging flags. Debugging information is\n"
695"written to sys.stderr.\n"
696"\n"
697"flags is an integer and can have the following bits turned on:\n"
698"\n"
699" DEBUG_STATS - Print statistics during collection.\n"
700" DEBUG_COLLECTABLE - Print collectable objects found.\n"
701" DEBUG_UNCOLLECTABLE - Print unreachable but uncollectable objects found.\n"
702" DEBUG_INSTANCES - Print instance objects.\n"
703" DEBUG_OBJECTS - Print objects other than instances.\n"
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000704" DEBUG_SAVEALL - Save objects to gc.garbage rather than freeing them.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000705" DEBUG_LEAK - Debug leaking programs (everything but STATS).\n");
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000706
707static PyObject *
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000708gc_set_debug(PyObject *self, PyObject *args)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000709{
Neil Schemenauer7760cff2000-09-22 22:35:36 +0000710 if (!PyArg_ParseTuple(args, "i:set_debug", &debug))
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000711 return NULL;
712
713 Py_INCREF(Py_None);
714 return Py_None;
715}
716
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000717PyDoc_STRVAR(gc_get_debug__doc__,
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000718"get_debug() -> flags\n"
719"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000720"Get the garbage collection debugging flags.\n");
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000721
722static PyObject *
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000723gc_get_debug(PyObject *self, PyObject *args)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000724{
Fred Drakecc1be242000-07-12 04:42:23 +0000725 if (!PyArg_ParseTuple(args, ":get_debug")) /* no args */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000726 return NULL;
727
728 return Py_BuildValue("i", debug);
729}
730
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000731PyDoc_STRVAR(gc_set_thresh__doc__,
Neal Norwitz2a47c0f2002-01-29 00:53:41 +0000732"set_threshold(threshold0, [threshold1, threshold2]) -> None\n"
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000733"\n"
734"Sets the collection thresholds. Setting threshold0 to zero disables\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000735"collection.\n");
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000736
737static PyObject *
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000738gc_set_thresh(PyObject *self, PyObject *args)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000739{
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000740 int i;
741 if (!PyArg_ParseTuple(args, "i|ii:set_threshold",
742 &generations[0].threshold,
743 &generations[1].threshold,
744 &generations[2].threshold))
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000745 return NULL;
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000746 for (i = 2; i < NUM_GENERATIONS; i++) {
747 /* generations higher than 2 get the same threshold */
748 generations[i].threshold = generations[2].threshold;
749 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000750
751 Py_INCREF(Py_None);
752 return Py_None;
753}
754
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000755PyDoc_STRVAR(gc_get_thresh__doc__,
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000756"get_threshold() -> (threshold0, threshold1, threshold2)\n"
757"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000758"Return the current collection thresholds\n");
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000759
760static PyObject *
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000761gc_get_thresh(PyObject *self, PyObject *args)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000762{
Fred Drakecc1be242000-07-12 04:42:23 +0000763 if (!PyArg_ParseTuple(args, ":get_threshold")) /* no args */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000764 return NULL;
765
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000766 return Py_BuildValue("(iii)",
767 generations[0].threshold,
768 generations[1].threshold,
769 generations[2].threshold);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000770}
771
Neil Schemenauer48c70342001-08-09 15:38:31 +0000772static int
Martin v. Löwis560da622001-11-24 09:24:51 +0000773referrersvisit(PyObject* obj, PyObject *objs)
Neil Schemenauer48c70342001-08-09 15:38:31 +0000774{
Martin v. Löwisc8fe77b2001-11-29 18:08:31 +0000775 int i;
776 for (i = 0; i < PyTuple_GET_SIZE(objs); i++)
777 if (PyTuple_GET_ITEM(objs, i) == obj)
778 return 1;
Neil Schemenauer48c70342001-08-09 15:38:31 +0000779 return 0;
780}
781
Neil Schemenauer17e7be62001-08-10 14:46:47 +0000782static int
Martin v. Löwis560da622001-11-24 09:24:51 +0000783gc_referrers_for(PyObject *objs, PyGC_Head *list, PyObject *resultlist)
Neil Schemenauer48c70342001-08-09 15:38:31 +0000784{
785 PyGC_Head *gc;
786 PyObject *obj;
787 traverseproc traverse;
Tim Peters9e4ca102001-10-11 18:31:31 +0000788 for (gc = list->gc.gc_next; gc != list; gc = gc->gc.gc_next) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000789 obj = FROM_GC(gc);
Neil Schemenauer48c70342001-08-09 15:38:31 +0000790 traverse = obj->ob_type->tp_traverse;
791 if (obj == objs || obj == resultlist)
792 continue;
Martin v. Löwis560da622001-11-24 09:24:51 +0000793 if (traverse(obj, (visitproc)referrersvisit, objs)) {
Neil Schemenauer17e7be62001-08-10 14:46:47 +0000794 if (PyList_Append(resultlist, obj) < 0)
795 return 0; /* error */
Neil Schemenauer48c70342001-08-09 15:38:31 +0000796 }
797 }
Neil Schemenauer17e7be62001-08-10 14:46:47 +0000798 return 1; /* no error */
Neil Schemenauer48c70342001-08-09 15:38:31 +0000799}
800
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000801PyDoc_STRVAR(gc_get_referrers__doc__,
Martin v. Löwis560da622001-11-24 09:24:51 +0000802"get_referrers(*objs) -> list\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000803Return the list of objects that directly refer to any of objs.");
Neil Schemenauer48c70342001-08-09 15:38:31 +0000804
Neil Schemenauer17e7be62001-08-10 14:46:47 +0000805static PyObject *
Martin v. Löwis560da622001-11-24 09:24:51 +0000806gc_get_referrers(PyObject *self, PyObject *args)
Neil Schemenauer48c70342001-08-09 15:38:31 +0000807{
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000808 int i;
Neil Schemenauer48c70342001-08-09 15:38:31 +0000809 PyObject *result = PyList_New(0);
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000810 for (i = 0; i < NUM_GENERATIONS; i++) {
811 if (!(gc_referrers_for(args, GEN_HEAD(i), result))) {
812 Py_DECREF(result);
813 return NULL;
814 }
Neil Schemenauer17e7be62001-08-10 14:46:47 +0000815 }
Neil Schemenauer48c70342001-08-09 15:38:31 +0000816 return result;
817}
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000818
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000819PyDoc_STRVAR(gc_get_objects__doc__,
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000820"get_objects() -> [...]\n"
821"\n"
822"Return a list of objects tracked by the collector (excluding the list\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000823"returned).\n");
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000824
825/* appending objects in a GC list to a Python list */
Martin v. Löwis155aad12001-12-02 12:21:34 +0000826static int
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000827append_objects(PyObject *py_list, PyGC_Head *gc_list)
828{
829 PyGC_Head *gc;
Tim Peters9e4ca102001-10-11 18:31:31 +0000830 for (gc = gc_list->gc.gc_next; gc != gc_list; gc = gc->gc.gc_next) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000831 PyObject *op = FROM_GC(gc);
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000832 if (op != py_list) {
Martin v. Löwis155aad12001-12-02 12:21:34 +0000833 if (PyList_Append(py_list, op)) {
834 return -1; /* exception */
835 }
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000836 }
837 }
Martin v. Löwis155aad12001-12-02 12:21:34 +0000838 return 0;
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000839}
840
841static PyObject *
842gc_get_objects(PyObject *self, PyObject *args)
843{
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000844 int i;
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000845 PyObject* result;
846
847 if (!PyArg_ParseTuple(args, ":get_objects")) /* check no args */
848 return NULL;
849 result = PyList_New(0);
Martin v. Löwisf8a6f242001-12-02 18:31:02 +0000850 if (result == NULL) {
851 return NULL;
852 }
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000853 for (i = 0; i < NUM_GENERATIONS; i++) {
854 if (append_objects(result, GEN_HEAD(i))) {
855 Py_DECREF(result);
856 return NULL;
857 }
Martin v. Löwis155aad12001-12-02 12:21:34 +0000858 }
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000859 return result;
860}
861
862
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000863PyDoc_STRVAR(gc__doc__,
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000864"This module provides access to the garbage collector for reference cycles.\n"
865"\n"
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000866"enable() -- Enable automatic garbage collection.\n"
867"disable() -- Disable automatic garbage collection.\n"
868"isenabled() -- Returns true if automatic collection is enabled.\n"
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000869"collect() -- Do a full collection right now.\n"
870"set_debug() -- Set debugging flags.\n"
871"get_debug() -- Get debugging flags.\n"
872"set_threshold() -- Set the collection thresholds.\n"
873"get_threshold() -- Return the current the collection thresholds.\n"
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000874"get_objects() -- Return a list of all objects tracked by the collector.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000875"get_referrers() -- Return the list of objects that refer to an object.\n");
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000876
877static PyMethodDef GcMethods[] = {
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000878 {"enable", gc_enable, METH_VARARGS, gc_enable__doc__},
879 {"disable", gc_disable, METH_VARARGS, gc_disable__doc__},
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000880 {"isenabled", gc_isenabled, METH_VARARGS, gc_isenabled__doc__},
881 {"set_debug", gc_set_debug, METH_VARARGS, gc_set_debug__doc__},
882 {"get_debug", gc_get_debug, METH_VARARGS, gc_get_debug__doc__},
883 {"set_threshold", gc_set_thresh, METH_VARARGS, gc_set_thresh__doc__},
884 {"get_threshold", gc_get_thresh, METH_VARARGS, gc_get_thresh__doc__},
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000885 {"collect", gc_collect, METH_VARARGS, gc_collect__doc__},
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000886 {"get_objects", gc_get_objects,METH_VARARGS, gc_get_objects__doc__},
Martin v. Löwis560da622001-11-24 09:24:51 +0000887 {"get_referrers", gc_get_referrers, METH_VARARGS,
888 gc_get_referrers__doc__},
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000889 {NULL, NULL} /* Sentinel */
890};
891
892void
893initgc(void)
894{
895 PyObject *m;
896 PyObject *d;
897
898 m = Py_InitModule4("gc",
899 GcMethods,
900 gc__doc__,
901 NULL,
902 PYTHON_API_VERSION);
903 d = PyModule_GetDict(m);
904 if (garbage == NULL) {
905 garbage = PyList_New(0);
906 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000907 PyDict_SetItemString(d, "garbage", garbage);
908 PyDict_SetItemString(d, "DEBUG_STATS",
909 PyInt_FromLong(DEBUG_STATS));
910 PyDict_SetItemString(d, "DEBUG_COLLECTABLE",
911 PyInt_FromLong(DEBUG_COLLECTABLE));
912 PyDict_SetItemString(d, "DEBUG_UNCOLLECTABLE",
913 PyInt_FromLong(DEBUG_UNCOLLECTABLE));
914 PyDict_SetItemString(d, "DEBUG_INSTANCES",
915 PyInt_FromLong(DEBUG_INSTANCES));
916 PyDict_SetItemString(d, "DEBUG_OBJECTS",
917 PyInt_FromLong(DEBUG_OBJECTS));
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000918 PyDict_SetItemString(d, "DEBUG_SAVEALL",
919 PyInt_FromLong(DEBUG_SAVEALL));
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000920 PyDict_SetItemString(d, "DEBUG_LEAK",
921 PyInt_FromLong(DEBUG_LEAK));
922}
923
Neil Schemenauer43411b52001-08-30 00:05:51 +0000924/* for debugging */
925void _PyGC_Dump(PyGC_Head *g)
926{
927 _PyObject_Dump(FROM_GC(g));
928}
929
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000930#endif /* WITH_CYCLE_GC */
Neil Schemenauer43411b52001-08-30 00:05:51 +0000931
932/* extension modules might be compiled with GC support so these
933 functions must always be available */
934
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000935#undef PyObject_GC_Track
936#undef PyObject_GC_UnTrack
937#undef PyObject_GC_Del
938#undef _PyObject_GC_Malloc
939
Neil Schemenauer43411b52001-08-30 00:05:51 +0000940void
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000941PyObject_GC_Track(void *op)
Neil Schemenauer43411b52001-08-30 00:05:51 +0000942{
943 _PyObject_GC_TRACK(op);
944}
945
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000946/* for binary compatibility with 2.2 */
Neil Schemenauer43411b52001-08-30 00:05:51 +0000947void
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000948_PyObject_GC_Track(PyObject *op)
949{
950 PyObject_GC_Track(op);
951}
952
953void
954PyObject_GC_UnTrack(void *op)
Neil Schemenauer43411b52001-08-30 00:05:51 +0000955{
Neil Schemenauerb8833102002-03-29 03:04:25 +0000956#ifdef WITH_CYCLE_GC
Neil Schemenauera2b11ec2002-05-21 15:53:24 +0000957 if (IS_TRACKED(op))
Guido van Rossumff413af2002-03-28 20:34:59 +0000958 _PyObject_GC_UNTRACK(op);
Neil Schemenauerb8833102002-03-29 03:04:25 +0000959#endif
Neil Schemenauer43411b52001-08-30 00:05:51 +0000960}
961
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000962/* for binary compatibility with 2.2 */
963void
964_PyObject_GC_UnTrack(PyObject *op)
965{
966 PyObject_GC_UnTrack(op);
967}
968
Neil Schemenauer43411b52001-08-30 00:05:51 +0000969PyObject *
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000970_PyObject_GC_Malloc(size_t basicsize)
Neil Schemenauer43411b52001-08-30 00:05:51 +0000971{
972 PyObject *op;
973#ifdef WITH_CYCLE_GC
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000974 PyGC_Head *g = PyObject_MALLOC(sizeof(PyGC_Head) + basicsize);
Neil Schemenauer43411b52001-08-30 00:05:51 +0000975 if (g == NULL)
Jeremy Hylton8a135182002-06-06 23:23:55 +0000976 return PyErr_NoMemory();
Tim Peters9e4ca102001-10-11 18:31:31 +0000977 g->gc.gc_next = NULL;
Tim Petersea405632002-07-02 00:52:30 +0000978 g->gc.gc_refs = GC_UNTRACKED;
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000979 generations[0].count++; /* number of allocated GC objects */
980 if (generations[0].count > generations[0].threshold &&
Neil Schemenauer43411b52001-08-30 00:05:51 +0000981 enabled &&
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000982 generations[0].threshold &&
Neil Schemenauer43411b52001-08-30 00:05:51 +0000983 !collecting &&
984 !PyErr_Occurred()) {
985 collecting = 1;
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000986 collect_generations();
Neil Schemenauer43411b52001-08-30 00:05:51 +0000987 collecting = 0;
988 }
989 op = FROM_GC(g);
990#else
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000991 op = PyObject_MALLOC(basicsize);
Neil Schemenauer43411b52001-08-30 00:05:51 +0000992 if (op == NULL)
Jeremy Hylton8a135182002-06-06 23:23:55 +0000993 return PyErr_NoMemory();
Neil Schemenauer43411b52001-08-30 00:05:51 +0000994
995#endif
996 return op;
997}
998
999PyObject *
1000_PyObject_GC_New(PyTypeObject *tp)
1001{
Neil Schemenauerfec4eb12002-04-12 02:41:03 +00001002 PyObject *op = _PyObject_GC_Malloc(_PyObject_SIZE(tp));
Tim Petersfa8efab2002-04-28 01:57:25 +00001003 if (op != NULL)
1004 op = PyObject_INIT(op, tp);
1005 return op;
Neil Schemenauer43411b52001-08-30 00:05:51 +00001006}
1007
1008PyVarObject *
Tim Peters6d483d32001-10-06 21:27:34 +00001009_PyObject_GC_NewVar(PyTypeObject *tp, int nitems)
Neil Schemenauer43411b52001-08-30 00:05:51 +00001010{
Neil Schemenauerfec4eb12002-04-12 02:41:03 +00001011 const size_t size = _PyObject_VAR_SIZE(tp, nitems);
1012 PyVarObject *op = (PyVarObject *) _PyObject_GC_Malloc(size);
Tim Petersfa8efab2002-04-28 01:57:25 +00001013 if (op != NULL)
1014 op = PyObject_INIT_VAR(op, tp, nitems);
1015 return op;
Neil Schemenauer43411b52001-08-30 00:05:51 +00001016}
1017
1018PyVarObject *
Tim Peters6d483d32001-10-06 21:27:34 +00001019_PyObject_GC_Resize(PyVarObject *op, int nitems)
Neil Schemenauer43411b52001-08-30 00:05:51 +00001020{
Tim Petersf2a67da2001-10-07 03:54:51 +00001021 const size_t basicsize = _PyObject_VAR_SIZE(op->ob_type, nitems);
Neil Schemenauer43411b52001-08-30 00:05:51 +00001022#ifdef WITH_CYCLE_GC
1023 PyGC_Head *g = AS_GC(op);
Neil Schemenauerfec4eb12002-04-12 02:41:03 +00001024 g = PyObject_REALLOC(g, sizeof(PyGC_Head) + basicsize);
Neil Schemenauer43411b52001-08-30 00:05:51 +00001025 if (g == NULL)
1026 return (PyVarObject *)PyErr_NoMemory();
1027 op = (PyVarObject *) FROM_GC(g);
1028#else
Neil Schemenauerfec4eb12002-04-12 02:41:03 +00001029 op = PyObject_REALLOC(op, basicsize);
Neil Schemenauer43411b52001-08-30 00:05:51 +00001030 if (op == NULL)
1031 return (PyVarObject *)PyErr_NoMemory();
1032#endif
Tim Peters6d483d32001-10-06 21:27:34 +00001033 op->ob_size = nitems;
Neil Schemenauer43411b52001-08-30 00:05:51 +00001034 return op;
1035}
1036
1037void
Neil Schemenauerfec4eb12002-04-12 02:41:03 +00001038PyObject_GC_Del(void *op)
Neil Schemenauer43411b52001-08-30 00:05:51 +00001039{
1040#ifdef WITH_CYCLE_GC
1041 PyGC_Head *g = AS_GC(op);
Neil Schemenauera2b11ec2002-05-21 15:53:24 +00001042 if (IS_TRACKED(op))
Neil Schemenauer43411b52001-08-30 00:05:51 +00001043 gc_list_remove(g);
Neil Schemenauer2880ae52002-05-04 05:35:20 +00001044 if (generations[0].count > 0) {
1045 generations[0].count--;
Neil Schemenauer43411b52001-08-30 00:05:51 +00001046 }
Neil Schemenauerfec4eb12002-04-12 02:41:03 +00001047 PyObject_FREE(g);
Neil Schemenauer43411b52001-08-30 00:05:51 +00001048#else
Neil Schemenauerfec4eb12002-04-12 02:41:03 +00001049 PyObject_FREE(op);
Neil Schemenauer43411b52001-08-30 00:05:51 +00001050#endif
1051}
1052
Neil Schemenauerfec4eb12002-04-12 02:41:03 +00001053/* for binary compatibility with 2.2 */
1054#undef _PyObject_GC_Del
1055void
1056_PyObject_GC_Del(PyObject *op)
1057{
1058 PyObject_GC_Del(op);
1059}