blob: a623e8b0c34bd3d71e680e4aeb9ca9d5c64df5ac [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
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000031/*** Global GC state ***/
32
Neil Schemenauer2880ae52002-05-04 05:35:20 +000033struct gc_generation {
34 PyGC_Head head;
35 int threshold; /* collection threshold */
36 int count; /* count of allocations or collections of younger
37 generations */
38};
39
40#define NUM_GENERATIONS 3
41#define GEN_HEAD(n) (&generations[n].head)
42
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000043/* linked lists of container objects */
Neil Schemenauer2880ae52002-05-04 05:35:20 +000044static struct gc_generation generations[NUM_GENERATIONS] = {
45 /* PyGC_Head, threshold, count */
46 {{{GEN_HEAD(0), GEN_HEAD(0), 0}}, 700, 0},
47 {{{GEN_HEAD(1), GEN_HEAD(1), 0}}, 10, 0},
48 {{{GEN_HEAD(2), GEN_HEAD(2), 0}}, 10, 0},
49};
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000050
Neil Schemenauer2880ae52002-05-04 05:35:20 +000051PyGC_Head *_PyGC_generation0 = GEN_HEAD(0);
52
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +000053static int enabled = 1; /* automatic collection enabled? */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000054
Neil Schemenauer43411b52001-08-30 00:05:51 +000055/* true if we are currently running the collector */
56static int collecting;
57
Tim Peters6fc13d92002-07-02 18:12:35 +000058/* list of uncollectable objects */
59static PyObject *garbage;
60
61/* Python string to use if unhandled exception occurs */
62static PyObject *gc_str;
63
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000064/* set for debugging information */
65#define DEBUG_STATS (1<<0) /* print collection statistics */
66#define DEBUG_COLLECTABLE (1<<1) /* print collectable objects */
67#define DEBUG_UNCOLLECTABLE (1<<2) /* print uncollectable objects */
68#define DEBUG_INSTANCES (1<<3) /* print instances */
69#define DEBUG_OBJECTS (1<<4) /* print other objects */
Neil Schemenauer544de1e2000-09-22 15:22:38 +000070#define DEBUG_SAVEALL (1<<5) /* save all garbage in gc.garbage */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000071#define DEBUG_LEAK DEBUG_COLLECTABLE | \
72 DEBUG_UNCOLLECTABLE | \
73 DEBUG_INSTANCES | \
Neil Schemenauer544de1e2000-09-22 15:22:38 +000074 DEBUG_OBJECTS | \
75 DEBUG_SAVEALL
Jeremy Hyltonb709df32000-09-01 02:47:25 +000076static int debug;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000077
Tim Peters6fc13d92002-07-02 18:12:35 +000078/*--------------------------------------------------------------------------
79gc_refs values.
Neil Schemenauer43411b52001-08-30 00:05:51 +000080
Tim Peters6fc13d92002-07-02 18:12:35 +000081Between collections, every gc'ed object has one of two gc_refs values:
82
83GC_UNTRACKED
84 The initial state; objects returned by PyObject_GC_Malloc are in this
85 state. The object doesn't live in any generation list, and its
86 tp_traverse slot must not be called.
87
88GC_REACHABLE
89 The object lives in some generation list, and its tp_traverse is safe to
90 call. An object transitions to GC_REACHABLE when PyObject_GC_Track
91 is called.
92
93During a collection, gc_refs can temporarily take on other states:
94
95>= 0
96 At the start of a collection, update_refs() copies the true refcount
97 to gc_refs, for each object in the generation being collected.
98 subtract_refs() then adjusts gc_refs so that it equals the number of
99 times an object is referenced directly from outside the generation
100 being collected.
101 gc_refs reamins >= 0 throughout these steps.
102
103GC_TENTATIVELY_UNREACHABLE
104 move_unreachable() then moves objects not reachable (whether directly or
105 indirectly) from outside the generation into an "unreachable" set.
106 Objects that are found to be reachable have gc_refs set to GC_REACHABLE
107 again. Objects that are found to be unreachable have gc_refs set to
108 GC_TENTATIVELY_UNREACHABLE. It's "tentatively" because the pass doing
109 this can't be sure until it ends, and GC_TENTATIVELY_UNREACHABLE may
110 transition back to GC_REACHABLE.
111
112 Only objects with GC_TENTATIVELY_UNREACHABLE still set are candidates
113 for collection. If it's decided not to collect such an object (e.g.,
114 it has a __del__ method), its gc_refs is restored to GC_REACHABLE again.
115----------------------------------------------------------------------------
116*/
Tim Petersea405632002-07-02 00:52:30 +0000117#define GC_UNTRACKED _PyGC_REFS_UNTRACKED
118#define GC_REACHABLE _PyGC_REFS_REACHABLE
119#define GC_TENTATIVELY_UNREACHABLE _PyGC_REFS_TENTATIVELY_UNREACHABLE
Tim Peters19b74c72002-07-01 03:52:19 +0000120
Tim Peters6fc13d92002-07-02 18:12:35 +0000121#define IS_TRACKED(o) ((AS_GC(o))->gc.gc_refs != GC_UNTRACKED)
Tim Peters19b74c72002-07-01 03:52:19 +0000122#define IS_REACHABLE(o) ((AS_GC(o))->gc.gc_refs == GC_REACHABLE)
123#define IS_TENTATIVELY_UNREACHABLE(o) ( \
124 (AS_GC(o))->gc.gc_refs == GC_TENTATIVELY_UNREACHABLE)
Neil Schemenauera2b11ec2002-05-21 15:53:24 +0000125
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000126/*** list functions ***/
127
128static void
129gc_list_init(PyGC_Head *list)
130{
Tim Peters9e4ca102001-10-11 18:31:31 +0000131 list->gc.gc_prev = list;
132 list->gc.gc_next = list;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000133}
134
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000135static int
136gc_list_is_empty(PyGC_Head *list)
137{
138 return (list->gc.gc_next == list);
139}
140
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000141static void
142gc_list_append(PyGC_Head *node, PyGC_Head *list)
143{
Tim Peters9e4ca102001-10-11 18:31:31 +0000144 node->gc.gc_next = list;
145 node->gc.gc_prev = list->gc.gc_prev;
146 node->gc.gc_prev->gc.gc_next = node;
147 list->gc.gc_prev = node;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000148}
149
150static void
151gc_list_remove(PyGC_Head *node)
152{
Tim Peters9e4ca102001-10-11 18:31:31 +0000153 node->gc.gc_prev->gc.gc_next = node->gc.gc_next;
154 node->gc.gc_next->gc.gc_prev = node->gc.gc_prev;
155 node->gc.gc_next = NULL; /* object is not currently tracked */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000156}
157
Tim Peters88396172002-06-30 17:56:40 +0000158static void
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000159gc_list_move(PyGC_Head *from, PyGC_Head *to)
160{
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000161 if (gc_list_is_empty(from)) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000162 gc_list_init(to);
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000163 }
164 else {
Tim Peters9e4ca102001-10-11 18:31:31 +0000165 to->gc.gc_next = from->gc.gc_next;
166 to->gc.gc_next->gc.gc_prev = to;
167 to->gc.gc_prev = from->gc.gc_prev;
168 to->gc.gc_prev->gc.gc_next = to;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000169 }
170 gc_list_init(from);
171}
172
173/* append a list onto another list, from becomes an empty list */
174static void
175gc_list_merge(PyGC_Head *from, PyGC_Head *to)
176{
177 PyGC_Head *tail;
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000178 if (!gc_list_is_empty(from)) {
Tim Peters9e4ca102001-10-11 18:31:31 +0000179 tail = to->gc.gc_prev;
180 tail->gc.gc_next = from->gc.gc_next;
181 tail->gc.gc_next->gc.gc_prev = tail;
182 to->gc.gc_prev = from->gc.gc_prev;
183 to->gc.gc_prev->gc.gc_next = to;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000184 }
185 gc_list_init(from);
186}
187
188static long
189gc_list_size(PyGC_Head *list)
190{
191 PyGC_Head *gc;
192 long n = 0;
Tim Peters9e4ca102001-10-11 18:31:31 +0000193 for (gc = list->gc.gc_next; gc != list; gc = gc->gc.gc_next) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000194 n++;
195 }
196 return n;
197}
198
199/*** end of list stuff ***/
200
201
Tim Peters19b74c72002-07-01 03:52:19 +0000202/* Set all gc_refs = ob_refcnt. After this, gc_refs is > 0 for all objects
203 * in containers, and is GC_REACHABLE for all tracked gc objects not in
204 * containers.
Tim Peters88396172002-06-30 17:56:40 +0000205 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000206static void
207update_refs(PyGC_Head *containers)
208{
Tim Peters9e4ca102001-10-11 18:31:31 +0000209 PyGC_Head *gc = containers->gc.gc_next;
Tim Petersea405632002-07-02 00:52:30 +0000210 for (; gc != containers; gc = gc->gc.gc_next) {
211 assert(gc->gc.gc_refs == GC_REACHABLE);
Tim Peters9e4ca102001-10-11 18:31:31 +0000212 gc->gc.gc_refs = FROM_GC(gc)->ob_refcnt;
Tim Petersea405632002-07-02 00:52:30 +0000213 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000214}
215
Tim Peters19b74c72002-07-01 03:52:19 +0000216/* A traversal callback for subtract_refs. */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000217static int
218visit_decref(PyObject *op, void *data)
219{
Tim Peters93cd83e2002-06-30 21:31:03 +0000220 assert(op != NULL);
Tim Peters19b74c72002-07-01 03:52:19 +0000221 if (PyObject_IS_GC(op)) {
222 PyGC_Head *gc = AS_GC(op);
223 /* We're only interested in gc_refs for objects in the
224 * generation being collected, which can be recognized
225 * because only they have positive gc_refs.
226 */
227 if (gc->gc.gc_refs > 0)
228 gc->gc.gc_refs--;
229 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000230 return 0;
231}
232
Tim Peters19b74c72002-07-01 03:52:19 +0000233/* Subtract internal references from gc_refs. After this, gc_refs is >= 0
234 * for all objects in containers, and is GC_REACHABLE for all tracked gc
235 * objects not in containers. The ones with gc_refs > 0 are directly
236 * reachable from outside containers, and so can't be collected.
237 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000238static void
239subtract_refs(PyGC_Head *containers)
240{
241 traverseproc traverse;
Tim Peters9e4ca102001-10-11 18:31:31 +0000242 PyGC_Head *gc = containers->gc.gc_next;
243 for (; gc != containers; gc=gc->gc.gc_next) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000244 traverse = FROM_GC(gc)->ob_type->tp_traverse;
245 (void) traverse(FROM_GC(gc),
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000246 (visitproc)visit_decref,
247 NULL);
248 }
249}
250
Tim Peters19b74c72002-07-01 03:52:19 +0000251/* A traversal callback for move_unreachable. */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000252static int
Tim Peters19b74c72002-07-01 03:52:19 +0000253visit_reachable(PyObject *op, PyGC_Head *reachable)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000254{
Tim Petersea405632002-07-02 00:52:30 +0000255 if (PyObject_IS_GC(op)) {
Tim Peters19b74c72002-07-01 03:52:19 +0000256 PyGC_Head *gc = AS_GC(op);
257 const int gc_refs = gc->gc.gc_refs;
258
259 if (gc_refs == 0) {
260 /* This is in move_unreachable's 'young' list, but
261 * the traversal hasn't yet gotten to it. All
262 * we need to do is tell move_unreachable that it's
263 * reachable.
264 */
265 gc->gc.gc_refs = 1;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000266 }
Tim Peters19b74c72002-07-01 03:52:19 +0000267 else if (gc_refs == GC_TENTATIVELY_UNREACHABLE) {
268 /* This had gc_refs = 0 when move_unreachable got
269 * to it, but turns out it's reachable after all.
270 * Move it back to move_unreachable's 'young' list,
271 * and move_unreachable will eventually get to it
272 * again.
273 */
274 gc_list_remove(gc);
275 gc_list_append(gc, reachable);
276 gc->gc.gc_refs = 1;
277 }
278 /* Else there's nothing to do.
279 * If gc_refs > 0, it must be in move_unreachable's 'young'
280 * list, and move_unreachable will eventually get to it.
281 * If gc_refs == GC_REACHABLE, it's either in some other
282 * generation so we don't care about it, or move_unreachable
Tim Peters6fc13d92002-07-02 18:12:35 +0000283 * already dealt with it.
Tim Petersea405632002-07-02 00:52:30 +0000284 * If gc_refs == GC_UNTRACKED, it must be ignored.
Tim Peters19b74c72002-07-01 03:52:19 +0000285 */
Tim Petersea405632002-07-02 00:52:30 +0000286 else {
287 assert(gc_refs > 0
288 || gc_refs == GC_REACHABLE
289 || gc_refs == GC_UNTRACKED);
290 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000291 }
292 return 0;
293}
294
Tim Peters19b74c72002-07-01 03:52:19 +0000295/* Move the unreachable objects from young to unreachable. After this,
296 * all objects in young have gc_refs = GC_REACHABLE, and all objects in
297 * unreachable have gc_refs = GC_TENTATIVELY_UNREACHABLE. All tracked
298 * gc objects not in young or unreachable still have gc_refs = GC_REACHABLE.
299 * All objects in young after this are directly or indirectly reachable
300 * from outside the original young; and all objects in unreachable are
301 * not.
Tim Peters88396172002-06-30 17:56:40 +0000302 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000303static void
Tim Peters19b74c72002-07-01 03:52:19 +0000304move_unreachable(PyGC_Head *young, PyGC_Head *unreachable)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000305{
Tim Peters19b74c72002-07-01 03:52:19 +0000306 PyGC_Head *gc = young->gc.gc_next;
307
308 /* Invariants: all objects "to the left" of us in young have gc_refs
309 * = GC_REACHABLE, and are indeed reachable (directly or indirectly)
310 * from outside the young list as it was at entry. All other objects
311 * from the original young "to the left" of us are in unreachable now,
312 * and have gc_refs = GC_TENTATIVELY_UNREACHABLE. All objects to the
313 * left of us in 'young' now have been scanned, and no objects here
314 * or to the right have been scanned yet.
315 */
316
317 while (gc != young) {
318 PyGC_Head *next;
319
Tim Peters6fc13d92002-07-02 18:12:35 +0000320 if (gc->gc.gc_refs) {
321 /* gc is definitely reachable from outside the
322 * original 'young'. Mark it as such, and traverse
323 * its pointers to find any other objects that may
324 * be directly reachable from it. Note that the
325 * call to tp_traverse may append objects to young,
326 * so we have to wait until it returns to determine
327 * the next object to visit.
328 */
329 PyObject *op = FROM_GC(gc);
330 traverseproc traverse = op->ob_type->tp_traverse;
331 assert(gc->gc.gc_refs > 0);
332 gc->gc.gc_refs = GC_REACHABLE;
333 (void) traverse(op,
334 (visitproc)visit_reachable,
335 (void *)young);
336 next = gc->gc.gc_next;
337 }
338 else {
Tim Peters19b74c72002-07-01 03:52:19 +0000339 /* This *may* be unreachable. To make progress,
340 * assume it is. gc isn't directly reachable from
341 * any object we've already traversed, but may be
342 * reachable from an object we haven't gotten to yet.
343 * visit_reachable will eventually move gc back into
344 * young if that's so, and we'll see it again.
345 */
346 next = gc->gc.gc_next;
347 gc_list_remove(gc);
348 gc_list_append(gc, unreachable);
349 gc->gc.gc_refs = GC_TENTATIVELY_UNREACHABLE;
350 }
Tim Peters19b74c72002-07-01 03:52:19 +0000351 gc = next;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000352 }
353}
354
Tim Peters88396172002-06-30 17:56:40 +0000355/* return true if object has a finalization method */
Neil Schemenauera765c122001-11-01 17:35:23 +0000356static int
357has_finalizer(PyObject *op)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000358{
Jeremy Hylton06257772000-08-31 15:10:24 +0000359 static PyObject *delstr = NULL;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000360 if (delstr == NULL) {
361 delstr = PyString_InternFromString("__del__");
Jeremy Hylton06257772000-08-31 15:10:24 +0000362 if (delstr == NULL)
363 Py_FatalError("PyGC: can't initialize __del__ string");
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000364 }
Tim Petersdb865612001-11-01 19:35:45 +0000365 return (PyInstance_Check(op) ||
366 PyType_HasFeature(op->ob_type, Py_TPFLAGS_HEAPTYPE))
367 && PyObject_HasAttr(op, delstr);
Neil Schemenauera765c122001-11-01 17:35:23 +0000368}
369
370/* Move all objects with finalizers (instances with __del__) */
371static void
372move_finalizers(PyGC_Head *unreachable, PyGC_Head *finalizers)
373{
374 PyGC_Head *next;
375 PyGC_Head *gc = unreachable->gc.gc_next;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000376 for (; gc != unreachable; gc=next) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000377 PyObject *op = FROM_GC(gc);
Tim Peters9e4ca102001-10-11 18:31:31 +0000378 next = gc->gc.gc_next;
Neil Schemenauera765c122001-11-01 17:35:23 +0000379 if (has_finalizer(op)) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000380 gc_list_remove(gc);
381 gc_list_append(gc, finalizers);
Tim Peters19b74c72002-07-01 03:52:19 +0000382 gc->gc.gc_refs = GC_REACHABLE;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000383 }
384 }
385}
386
Tim Peters19b74c72002-07-01 03:52:19 +0000387/* A traversal callback for move_finalizer_reachable. */
388static int
389visit_move(PyObject *op, PyGC_Head *tolist)
390{
391 if (PyObject_IS_GC(op)) {
Tim Petersea405632002-07-02 00:52:30 +0000392 if (IS_TENTATIVELY_UNREACHABLE(op)) {
Tim Peters19b74c72002-07-01 03:52:19 +0000393 PyGC_Head *gc = AS_GC(op);
394 gc_list_remove(gc);
395 gc_list_append(gc, tolist);
396 gc->gc.gc_refs = GC_REACHABLE;
397 }
398 }
399 return 0;
400}
401
402/* Move objects that are reachable from finalizers, from the unreachable set
403 * into the finalizers set.
404 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000405static void
406move_finalizer_reachable(PyGC_Head *finalizers)
407{
408 traverseproc traverse;
Tim Peters9e4ca102001-10-11 18:31:31 +0000409 PyGC_Head *gc = finalizers->gc.gc_next;
410 for (; gc != finalizers; gc=gc->gc.gc_next) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000411 /* careful, finalizers list is growing here */
Neil Schemenauer43411b52001-08-30 00:05:51 +0000412 traverse = FROM_GC(gc)->ob_type->tp_traverse;
Tim Peters88396172002-06-30 17:56:40 +0000413 (void) traverse(FROM_GC(gc),
Neil Schemenauer43411b52001-08-30 00:05:51 +0000414 (visitproc)visit_move,
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000415 (void *)finalizers);
416 }
417}
418
419static void
Jeremy Hylton06257772000-08-31 15:10:24 +0000420debug_instance(char *msg, PyInstanceObject *inst)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000421{
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000422 char *cname;
Neil Schemenauera765c122001-11-01 17:35:23 +0000423 /* simple version of instance_repr */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000424 PyObject *classname = inst->in_class->cl_name;
425 if (classname != NULL && PyString_Check(classname))
426 cname = PyString_AsString(classname);
427 else
428 cname = "?";
Jeremy Hylton06257772000-08-31 15:10:24 +0000429 PySys_WriteStderr("gc: %.100s <%.100s instance at %p>\n",
430 msg, cname, inst);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000431}
432
433static void
Jeremy Hylton06257772000-08-31 15:10:24 +0000434debug_cycle(char *msg, PyObject *op)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000435{
436 if ((debug & DEBUG_INSTANCES) && PyInstance_Check(op)) {
Jeremy Hylton06257772000-08-31 15:10:24 +0000437 debug_instance(msg, (PyInstanceObject *)op);
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000438 }
439 else if (debug & DEBUG_OBJECTS) {
Jeremy Hylton06257772000-08-31 15:10:24 +0000440 PySys_WriteStderr("gc: %.100s <%.100s %p>\n",
441 msg, op->ob_type->tp_name, op);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000442 }
443}
444
445/* Handle uncollectable garbage (cycles with finalizers). */
446static void
447handle_finalizers(PyGC_Head *finalizers, PyGC_Head *old)
448{
449 PyGC_Head *gc;
450 if (garbage == NULL) {
451 garbage = PyList_New(0);
452 }
Tim Peters9e4ca102001-10-11 18:31:31 +0000453 for (gc = finalizers->gc.gc_next; gc != finalizers;
454 gc = finalizers->gc.gc_next) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000455 PyObject *op = FROM_GC(gc);
Neil Schemenauera765c122001-11-01 17:35:23 +0000456 if ((debug & DEBUG_SAVEALL) || has_finalizer(op)) {
457 /* If SAVEALL is not set then just append objects with
458 * finalizers to the list of garbage. All objects in
459 * the finalizers list are reachable from those
Tim Peters19b74c72002-07-01 03:52:19 +0000460 * objects.
461 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000462 PyList_Append(garbage, op);
463 }
Tim Peters88396172002-06-30 17:56:40 +0000464 /* object is now reachable again */
Tim Peters19b74c72002-07-01 03:52:19 +0000465 assert(IS_REACHABLE(op));
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000466 gc_list_remove(gc);
467 gc_list_append(gc, old);
468 }
469}
470
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000471/* Break reference cycles by clearing the containers involved. This is
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000472 * tricky business as the lists can be changing and we don't know which
Tim Peters19b74c72002-07-01 03:52:19 +0000473 * objects may be freed. It is possible I screwed something up here.
474 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000475static void
476delete_garbage(PyGC_Head *unreachable, PyGC_Head *old)
477{
478 inquiry clear;
479
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000480 while (!gc_list_is_empty(unreachable)) {
Tim Peters9e4ca102001-10-11 18:31:31 +0000481 PyGC_Head *gc = unreachable->gc.gc_next;
Neil Schemenauer43411b52001-08-30 00:05:51 +0000482 PyObject *op = FROM_GC(gc);
Tim Peters88396172002-06-30 17:56:40 +0000483
Tim Peters19b74c72002-07-01 03:52:19 +0000484 assert(IS_TENTATIVELY_UNREACHABLE(op));
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000485 if (debug & DEBUG_SAVEALL) {
486 PyList_Append(garbage, op);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000487 }
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000488 else {
489 if ((clear = op->ob_type->tp_clear) != NULL) {
490 Py_INCREF(op);
Jeremy Hylton8a135182002-06-06 23:23:55 +0000491 clear(op);
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000492 Py_DECREF(op);
493 }
494 }
Tim Peters9e4ca102001-10-11 18:31:31 +0000495 if (unreachable->gc.gc_next == gc) {
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000496 /* object is still alive, move it, it may die later */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000497 gc_list_remove(gc);
498 gc_list_append(gc, old);
Tim Peters19b74c72002-07-01 03:52:19 +0000499 gc->gc.gc_refs = GC_REACHABLE;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000500 }
501 }
502}
503
504/* This is the main function. Read this to understand how the
505 * collection process works. */
506static long
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000507collect(int generation)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000508{
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000509 int i;
Tim Peters19b74c72002-07-01 03:52:19 +0000510 long m = 0; /* # objects collected */
511 long n = 0; /* # unreachable objects that couldn't be collected */
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000512 PyGC_Head *young; /* the generation we are examining */
513 PyGC_Head *old; /* next older generation */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000514 PyGC_Head unreachable;
515 PyGC_Head finalizers;
516 PyGC_Head *gc;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000517
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000518 if (debug & DEBUG_STATS) {
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000519 PySys_WriteStderr("gc: collecting generation %d...\n",
520 generation);
521 PySys_WriteStderr("gc: objects in each generation:");
522 for (i = 0; i < NUM_GENERATIONS; i++) {
523 PySys_WriteStderr(" %ld", gc_list_size(GEN_HEAD(i)));
524 }
525 PySys_WriteStderr("\n");
526 }
527
528 /* update collection and allocation counters */
529 if (generation+1 < NUM_GENERATIONS)
530 generations[generation+1].count += 1;
531 for (i = 0; i <= generation; i++)
Neil Schemenauerc9051642002-06-28 19:16:04 +0000532 generations[i].count = 0;
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000533
534 /* merge younger generations with one we are currently collecting */
535 for (i = 0; i < generation; i++) {
536 gc_list_merge(GEN_HEAD(i), GEN_HEAD(generation));
537 }
538
539 /* handy references */
540 young = GEN_HEAD(generation);
Tim Peters19b74c72002-07-01 03:52:19 +0000541 if (generation < NUM_GENERATIONS-1)
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000542 old = GEN_HEAD(generation+1);
Tim Peters19b74c72002-07-01 03:52:19 +0000543 else
544 old = young;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000545
546 /* Using ob_refcnt and gc_refs, calculate which objects in the
547 * container set are reachable from outside the set (ie. have a
548 * refcount greater than 0 when all the references within the
Tim Peters19b74c72002-07-01 03:52:19 +0000549 * set are taken into account
550 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000551 update_refs(young);
552 subtract_refs(young);
553
Tim Peters19b74c72002-07-01 03:52:19 +0000554 /* Leave everything reachable from outside young in young, and move
555 * everything else (in young) to unreachable.
556 * NOTE: This used to move the reachable objects into a reachable
557 * set instead. But most things usually turn out to be reachable,
558 * so it's more efficient to move the unreachable things.
559 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000560 gc_list_init(&unreachable);
Tim Peters19b74c72002-07-01 03:52:19 +0000561 move_unreachable(young, &unreachable);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000562
Tim Peters19b74c72002-07-01 03:52:19 +0000563 /* Move reachable objects to next generation. */
564 if (young != old)
565 gc_list_merge(young, old);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000566
Tim Peters19b74c72002-07-01 03:52:19 +0000567 /* All objects in unreachable are trash, but objects reachable from
568 * finalizers can't safely be deleted. Python programmers should take
569 * care not to create such things. For Python, finalizers means
570 * instance objects with __del__ methods.
571 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000572 gc_list_init(&finalizers);
573 move_finalizers(&unreachable, &finalizers);
574 move_finalizer_reachable(&finalizers);
575
576 /* Collect statistics on collectable objects found and print
577 * debugging information. */
Tim Peters9e4ca102001-10-11 18:31:31 +0000578 for (gc = unreachable.gc.gc_next; gc != &unreachable;
579 gc = gc->gc.gc_next) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000580 m++;
Jeremy Hylton06257772000-08-31 15:10:24 +0000581 if (debug & DEBUG_COLLECTABLE) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000582 debug_cycle("collectable", FROM_GC(gc));
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000583 }
584 }
Tim Peters19b74c72002-07-01 03:52:19 +0000585 /* Call tp_clear on objects in the collectable set. This will cause
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000586 * the reference cycles to be broken. It may also cause some objects in
587 * finalizers to be freed */
588 delete_garbage(&unreachable, old);
589
590 /* Collect statistics on uncollectable objects found and print
591 * debugging information. */
Tim Peters9e4ca102001-10-11 18:31:31 +0000592 for (gc = finalizers.gc.gc_next; gc != &finalizers;
593 gc = gc->gc.gc_next) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000594 n++;
Jeremy Hylton06257772000-08-31 15:10:24 +0000595 if (debug & DEBUG_UNCOLLECTABLE) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000596 debug_cycle("uncollectable", FROM_GC(gc));
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000597 }
598 }
Jeremy Hylton06257772000-08-31 15:10:24 +0000599 if (debug & DEBUG_STATS) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000600 if (m == 0 && n == 0) {
Jeremy Hylton06257772000-08-31 15:10:24 +0000601 PySys_WriteStderr("gc: done.\n");
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000602 }
603 else {
Jeremy Hylton06257772000-08-31 15:10:24 +0000604 PySys_WriteStderr(
605 "gc: done, %ld unreachable, %ld uncollectable.\n",
606 n+m, n);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000607 }
608 }
609
610 /* Append instances in the uncollectable set to a Python
611 * reachable list of garbage. The programmer has to deal with
612 * this if they insist on creating this type of structure. */
613 handle_finalizers(&finalizers, old);
614
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000615 if (PyErr_Occurred()) {
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000616 if (gc_str == NULL) {
617 gc_str = PyString_FromString("garbage collection");
618 }
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000619 PyErr_WriteUnraisable(gc_str);
620 Py_FatalError("unexpected exception during garbage collection");
621 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000622 return n+m;
623}
624
625static long
626collect_generations(void)
627{
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000628 int i;
Vladimir Marangozovb16714b2000-07-10 05:37:39 +0000629 long n = 0;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000630
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000631 /* Find the oldest generation (higest numbered) where the count
632 * exceeds the threshold. Objects in the that generation and
633 * generations younger than it will be collected. */
634 for (i = NUM_GENERATIONS-1; i >= 0; i--) {
635 if (generations[i].count > generations[i].threshold) {
636 n = collect(i);
637 break;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000638 }
639 }
640 return n;
641}
642
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000643PyDoc_STRVAR(gc_enable__doc__,
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000644"enable() -> None\n"
645"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000646"Enable automatic garbage collection.\n");
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000647
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000648static PyObject *
649gc_enable(PyObject *self, PyObject *args)
650{
651
652 if (!PyArg_ParseTuple(args, ":enable")) /* check no args */
653 return NULL;
654
655 enabled = 1;
656
657 Py_INCREF(Py_None);
658 return Py_None;
659}
660
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000661PyDoc_STRVAR(gc_disable__doc__,
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000662"disable() -> None\n"
663"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000664"Disable automatic garbage collection.\n");
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000665
666static PyObject *
667gc_disable(PyObject *self, PyObject *args)
668{
669
670 if (!PyArg_ParseTuple(args, ":disable")) /* check no args */
671 return NULL;
672
673 enabled = 0;
674
675 Py_INCREF(Py_None);
676 return Py_None;
677}
678
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000679PyDoc_STRVAR(gc_isenabled__doc__,
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000680"isenabled() -> status\n"
681"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000682"Returns true if automatic garbage collection is enabled.\n");
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000683
684static PyObject *
685gc_isenabled(PyObject *self, PyObject *args)
686{
687
688 if (!PyArg_ParseTuple(args, ":isenabled")) /* check no args */
689 return NULL;
690
691 return Py_BuildValue("i", enabled);
692}
693
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000694PyDoc_STRVAR(gc_collect__doc__,
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000695"collect() -> n\n"
696"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000697"Run a full collection. The number of unreachable objects is returned.\n");
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000698
699static PyObject *
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000700gc_collect(PyObject *self, PyObject *args)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000701{
702 long n;
703
Fred Drakecc1be242000-07-12 04:42:23 +0000704 if (!PyArg_ParseTuple(args, ":collect")) /* check no args */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000705 return NULL;
706
Neil Schemenauere8c40cb2001-10-31 23:09:35 +0000707 if (collecting) {
708 n = 0; /* already collecting, don't do anything */
709 }
710 else {
711 collecting = 1;
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000712 n = collect(NUM_GENERATIONS - 1);
Neil Schemenauere8c40cb2001-10-31 23:09:35 +0000713 collecting = 0;
714 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000715
Neil Schemenauer7760cff2000-09-22 22:35:36 +0000716 return Py_BuildValue("l", n);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000717}
718
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000719PyDoc_STRVAR(gc_set_debug__doc__,
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000720"set_debug(flags) -> None\n"
721"\n"
722"Set the garbage collection debugging flags. Debugging information is\n"
723"written to sys.stderr.\n"
724"\n"
725"flags is an integer and can have the following bits turned on:\n"
726"\n"
727" DEBUG_STATS - Print statistics during collection.\n"
728" DEBUG_COLLECTABLE - Print collectable objects found.\n"
729" DEBUG_UNCOLLECTABLE - Print unreachable but uncollectable objects found.\n"
730" DEBUG_INSTANCES - Print instance objects.\n"
731" DEBUG_OBJECTS - Print objects other than instances.\n"
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000732" DEBUG_SAVEALL - Save objects to gc.garbage rather than freeing them.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000733" DEBUG_LEAK - Debug leaking programs (everything but STATS).\n");
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000734
735static PyObject *
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000736gc_set_debug(PyObject *self, PyObject *args)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000737{
Neil Schemenauer7760cff2000-09-22 22:35:36 +0000738 if (!PyArg_ParseTuple(args, "i:set_debug", &debug))
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000739 return NULL;
740
741 Py_INCREF(Py_None);
742 return Py_None;
743}
744
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000745PyDoc_STRVAR(gc_get_debug__doc__,
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000746"get_debug() -> flags\n"
747"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000748"Get the garbage collection debugging flags.\n");
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000749
750static PyObject *
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000751gc_get_debug(PyObject *self, PyObject *args)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000752{
Fred Drakecc1be242000-07-12 04:42:23 +0000753 if (!PyArg_ParseTuple(args, ":get_debug")) /* no args */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000754 return NULL;
755
756 return Py_BuildValue("i", debug);
757}
758
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000759PyDoc_STRVAR(gc_set_thresh__doc__,
Neal Norwitz2a47c0f2002-01-29 00:53:41 +0000760"set_threshold(threshold0, [threshold1, threshold2]) -> None\n"
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000761"\n"
762"Sets the collection thresholds. Setting threshold0 to zero disables\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000763"collection.\n");
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000764
765static PyObject *
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000766gc_set_thresh(PyObject *self, PyObject *args)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000767{
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000768 int i;
769 if (!PyArg_ParseTuple(args, "i|ii:set_threshold",
770 &generations[0].threshold,
771 &generations[1].threshold,
772 &generations[2].threshold))
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000773 return NULL;
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000774 for (i = 2; i < NUM_GENERATIONS; i++) {
775 /* generations higher than 2 get the same threshold */
776 generations[i].threshold = generations[2].threshold;
777 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000778
779 Py_INCREF(Py_None);
780 return Py_None;
781}
782
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000783PyDoc_STRVAR(gc_get_thresh__doc__,
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000784"get_threshold() -> (threshold0, threshold1, threshold2)\n"
785"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000786"Return the current collection thresholds\n");
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000787
788static PyObject *
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000789gc_get_thresh(PyObject *self, PyObject *args)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000790{
Fred Drakecc1be242000-07-12 04:42:23 +0000791 if (!PyArg_ParseTuple(args, ":get_threshold")) /* no args */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000792 return NULL;
793
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000794 return Py_BuildValue("(iii)",
795 generations[0].threshold,
796 generations[1].threshold,
797 generations[2].threshold);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000798}
799
Neil Schemenauer48c70342001-08-09 15:38:31 +0000800static int
Martin v. Löwis560da622001-11-24 09:24:51 +0000801referrersvisit(PyObject* obj, PyObject *objs)
Neil Schemenauer48c70342001-08-09 15:38:31 +0000802{
Martin v. Löwisc8fe77b2001-11-29 18:08:31 +0000803 int i;
804 for (i = 0; i < PyTuple_GET_SIZE(objs); i++)
805 if (PyTuple_GET_ITEM(objs, i) == obj)
806 return 1;
Neil Schemenauer48c70342001-08-09 15:38:31 +0000807 return 0;
808}
809
Neil Schemenauer17e7be62001-08-10 14:46:47 +0000810static int
Martin v. Löwis560da622001-11-24 09:24:51 +0000811gc_referrers_for(PyObject *objs, PyGC_Head *list, PyObject *resultlist)
Neil Schemenauer48c70342001-08-09 15:38:31 +0000812{
813 PyGC_Head *gc;
814 PyObject *obj;
815 traverseproc traverse;
Tim Peters9e4ca102001-10-11 18:31:31 +0000816 for (gc = list->gc.gc_next; gc != list; gc = gc->gc.gc_next) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000817 obj = FROM_GC(gc);
Neil Schemenauer48c70342001-08-09 15:38:31 +0000818 traverse = obj->ob_type->tp_traverse;
819 if (obj == objs || obj == resultlist)
820 continue;
Martin v. Löwis560da622001-11-24 09:24:51 +0000821 if (traverse(obj, (visitproc)referrersvisit, objs)) {
Neil Schemenauer17e7be62001-08-10 14:46:47 +0000822 if (PyList_Append(resultlist, obj) < 0)
823 return 0; /* error */
Neil Schemenauer48c70342001-08-09 15:38:31 +0000824 }
825 }
Neil Schemenauer17e7be62001-08-10 14:46:47 +0000826 return 1; /* no error */
Neil Schemenauer48c70342001-08-09 15:38:31 +0000827}
828
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000829PyDoc_STRVAR(gc_get_referrers__doc__,
Martin v. Löwis560da622001-11-24 09:24:51 +0000830"get_referrers(*objs) -> list\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000831Return the list of objects that directly refer to any of objs.");
Neil Schemenauer48c70342001-08-09 15:38:31 +0000832
Neil Schemenauer17e7be62001-08-10 14:46:47 +0000833static PyObject *
Martin v. Löwis560da622001-11-24 09:24:51 +0000834gc_get_referrers(PyObject *self, PyObject *args)
Neil Schemenauer48c70342001-08-09 15:38:31 +0000835{
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000836 int i;
Neil Schemenauer48c70342001-08-09 15:38:31 +0000837 PyObject *result = PyList_New(0);
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000838 for (i = 0; i < NUM_GENERATIONS; i++) {
839 if (!(gc_referrers_for(args, GEN_HEAD(i), result))) {
840 Py_DECREF(result);
841 return NULL;
842 }
Neil Schemenauer17e7be62001-08-10 14:46:47 +0000843 }
Neil Schemenauer48c70342001-08-09 15:38:31 +0000844 return result;
845}
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000846
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000847PyDoc_STRVAR(gc_get_objects__doc__,
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000848"get_objects() -> [...]\n"
849"\n"
850"Return a list of objects tracked by the collector (excluding the list\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000851"returned).\n");
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000852
853/* appending objects in a GC list to a Python list */
Martin v. Löwis155aad12001-12-02 12:21:34 +0000854static int
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000855append_objects(PyObject *py_list, PyGC_Head *gc_list)
856{
857 PyGC_Head *gc;
Tim Peters9e4ca102001-10-11 18:31:31 +0000858 for (gc = gc_list->gc.gc_next; gc != gc_list; gc = gc->gc.gc_next) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000859 PyObject *op = FROM_GC(gc);
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000860 if (op != py_list) {
Martin v. Löwis155aad12001-12-02 12:21:34 +0000861 if (PyList_Append(py_list, op)) {
862 return -1; /* exception */
863 }
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000864 }
865 }
Martin v. Löwis155aad12001-12-02 12:21:34 +0000866 return 0;
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000867}
868
869static PyObject *
870gc_get_objects(PyObject *self, PyObject *args)
871{
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000872 int i;
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000873 PyObject* result;
874
875 if (!PyArg_ParseTuple(args, ":get_objects")) /* check no args */
876 return NULL;
877 result = PyList_New(0);
Martin v. Löwisf8a6f242001-12-02 18:31:02 +0000878 if (result == NULL) {
879 return NULL;
880 }
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000881 for (i = 0; i < NUM_GENERATIONS; i++) {
882 if (append_objects(result, GEN_HEAD(i))) {
883 Py_DECREF(result);
884 return NULL;
885 }
Martin v. Löwis155aad12001-12-02 12:21:34 +0000886 }
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000887 return result;
888}
889
890
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000891PyDoc_STRVAR(gc__doc__,
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000892"This module provides access to the garbage collector for reference cycles.\n"
893"\n"
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000894"enable() -- Enable automatic garbage collection.\n"
895"disable() -- Disable automatic garbage collection.\n"
896"isenabled() -- Returns true if automatic collection is enabled.\n"
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000897"collect() -- Do a full collection right now.\n"
898"set_debug() -- Set debugging flags.\n"
899"get_debug() -- Get debugging flags.\n"
900"set_threshold() -- Set the collection thresholds.\n"
901"get_threshold() -- Return the current the collection thresholds.\n"
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000902"get_objects() -- Return a list of all objects tracked by the collector.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000903"get_referrers() -- Return the list of objects that refer to an object.\n");
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000904
905static PyMethodDef GcMethods[] = {
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000906 {"enable", gc_enable, METH_VARARGS, gc_enable__doc__},
907 {"disable", gc_disable, METH_VARARGS, gc_disable__doc__},
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000908 {"isenabled", gc_isenabled, METH_VARARGS, gc_isenabled__doc__},
909 {"set_debug", gc_set_debug, METH_VARARGS, gc_set_debug__doc__},
910 {"get_debug", gc_get_debug, METH_VARARGS, gc_get_debug__doc__},
911 {"set_threshold", gc_set_thresh, METH_VARARGS, gc_set_thresh__doc__},
912 {"get_threshold", gc_get_thresh, METH_VARARGS, gc_get_thresh__doc__},
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000913 {"collect", gc_collect, METH_VARARGS, gc_collect__doc__},
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000914 {"get_objects", gc_get_objects,METH_VARARGS, gc_get_objects__doc__},
Martin v. Löwis560da622001-11-24 09:24:51 +0000915 {"get_referrers", gc_get_referrers, METH_VARARGS,
916 gc_get_referrers__doc__},
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000917 {NULL, NULL} /* Sentinel */
918};
919
920void
921initgc(void)
922{
923 PyObject *m;
924 PyObject *d;
925
926 m = Py_InitModule4("gc",
927 GcMethods,
928 gc__doc__,
929 NULL,
930 PYTHON_API_VERSION);
931 d = PyModule_GetDict(m);
932 if (garbage == NULL) {
933 garbage = PyList_New(0);
934 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000935 PyDict_SetItemString(d, "garbage", garbage);
936 PyDict_SetItemString(d, "DEBUG_STATS",
937 PyInt_FromLong(DEBUG_STATS));
938 PyDict_SetItemString(d, "DEBUG_COLLECTABLE",
939 PyInt_FromLong(DEBUG_COLLECTABLE));
940 PyDict_SetItemString(d, "DEBUG_UNCOLLECTABLE",
941 PyInt_FromLong(DEBUG_UNCOLLECTABLE));
942 PyDict_SetItemString(d, "DEBUG_INSTANCES",
943 PyInt_FromLong(DEBUG_INSTANCES));
944 PyDict_SetItemString(d, "DEBUG_OBJECTS",
945 PyInt_FromLong(DEBUG_OBJECTS));
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000946 PyDict_SetItemString(d, "DEBUG_SAVEALL",
947 PyInt_FromLong(DEBUG_SAVEALL));
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000948 PyDict_SetItemString(d, "DEBUG_LEAK",
949 PyInt_FromLong(DEBUG_LEAK));
950}
951
Neil Schemenauer43411b52001-08-30 00:05:51 +0000952/* for debugging */
953void _PyGC_Dump(PyGC_Head *g)
954{
955 _PyObject_Dump(FROM_GC(g));
956}
957
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000958#endif /* WITH_CYCLE_GC */
Neil Schemenauer43411b52001-08-30 00:05:51 +0000959
960/* extension modules might be compiled with GC support so these
961 functions must always be available */
962
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000963#undef PyObject_GC_Track
964#undef PyObject_GC_UnTrack
965#undef PyObject_GC_Del
966#undef _PyObject_GC_Malloc
967
Neil Schemenauer43411b52001-08-30 00:05:51 +0000968void
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000969PyObject_GC_Track(void *op)
Neil Schemenauer43411b52001-08-30 00:05:51 +0000970{
971 _PyObject_GC_TRACK(op);
972}
973
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000974/* for binary compatibility with 2.2 */
Neil Schemenauer43411b52001-08-30 00:05:51 +0000975void
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000976_PyObject_GC_Track(PyObject *op)
977{
978 PyObject_GC_Track(op);
979}
980
981void
982PyObject_GC_UnTrack(void *op)
Neil Schemenauer43411b52001-08-30 00:05:51 +0000983{
Neil Schemenauerb8833102002-03-29 03:04:25 +0000984#ifdef WITH_CYCLE_GC
Neil Schemenauera2b11ec2002-05-21 15:53:24 +0000985 if (IS_TRACKED(op))
Guido van Rossumff413af2002-03-28 20:34:59 +0000986 _PyObject_GC_UNTRACK(op);
Neil Schemenauerb8833102002-03-29 03:04:25 +0000987#endif
Neil Schemenauer43411b52001-08-30 00:05:51 +0000988}
989
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000990/* for binary compatibility with 2.2 */
991void
992_PyObject_GC_UnTrack(PyObject *op)
993{
994 PyObject_GC_UnTrack(op);
995}
996
Neil Schemenauer43411b52001-08-30 00:05:51 +0000997PyObject *
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000998_PyObject_GC_Malloc(size_t basicsize)
Neil Schemenauer43411b52001-08-30 00:05:51 +0000999{
1000 PyObject *op;
1001#ifdef WITH_CYCLE_GC
Neil Schemenauerfec4eb12002-04-12 02:41:03 +00001002 PyGC_Head *g = PyObject_MALLOC(sizeof(PyGC_Head) + basicsize);
Neil Schemenauer43411b52001-08-30 00:05:51 +00001003 if (g == NULL)
Jeremy Hylton8a135182002-06-06 23:23:55 +00001004 return PyErr_NoMemory();
Tim Petersea405632002-07-02 00:52:30 +00001005 g->gc.gc_refs = GC_UNTRACKED;
Neil Schemenauer2880ae52002-05-04 05:35:20 +00001006 generations[0].count++; /* number of allocated GC objects */
1007 if (generations[0].count > generations[0].threshold &&
Neil Schemenauer43411b52001-08-30 00:05:51 +00001008 enabled &&
Neil Schemenauer2880ae52002-05-04 05:35:20 +00001009 generations[0].threshold &&
Neil Schemenauer43411b52001-08-30 00:05:51 +00001010 !collecting &&
1011 !PyErr_Occurred()) {
1012 collecting = 1;
Neil Schemenauer2880ae52002-05-04 05:35:20 +00001013 collect_generations();
Neil Schemenauer43411b52001-08-30 00:05:51 +00001014 collecting = 0;
1015 }
1016 op = FROM_GC(g);
1017#else
Neil Schemenauerfec4eb12002-04-12 02:41:03 +00001018 op = PyObject_MALLOC(basicsize);
Neil Schemenauer43411b52001-08-30 00:05:51 +00001019 if (op == NULL)
Jeremy Hylton8a135182002-06-06 23:23:55 +00001020 return PyErr_NoMemory();
Neil Schemenauer43411b52001-08-30 00:05:51 +00001021
1022#endif
1023 return op;
1024}
1025
1026PyObject *
1027_PyObject_GC_New(PyTypeObject *tp)
1028{
Neil Schemenauerfec4eb12002-04-12 02:41:03 +00001029 PyObject *op = _PyObject_GC_Malloc(_PyObject_SIZE(tp));
Tim Petersfa8efab2002-04-28 01:57:25 +00001030 if (op != NULL)
1031 op = PyObject_INIT(op, tp);
1032 return op;
Neil Schemenauer43411b52001-08-30 00:05:51 +00001033}
1034
1035PyVarObject *
Tim Peters6d483d32001-10-06 21:27:34 +00001036_PyObject_GC_NewVar(PyTypeObject *tp, int nitems)
Neil Schemenauer43411b52001-08-30 00:05:51 +00001037{
Neil Schemenauerfec4eb12002-04-12 02:41:03 +00001038 const size_t size = _PyObject_VAR_SIZE(tp, nitems);
1039 PyVarObject *op = (PyVarObject *) _PyObject_GC_Malloc(size);
Tim Petersfa8efab2002-04-28 01:57:25 +00001040 if (op != NULL)
1041 op = PyObject_INIT_VAR(op, tp, nitems);
1042 return op;
Neil Schemenauer43411b52001-08-30 00:05:51 +00001043}
1044
1045PyVarObject *
Tim Peters6d483d32001-10-06 21:27:34 +00001046_PyObject_GC_Resize(PyVarObject *op, int nitems)
Neil Schemenauer43411b52001-08-30 00:05:51 +00001047{
Tim Petersf2a67da2001-10-07 03:54:51 +00001048 const size_t basicsize = _PyObject_VAR_SIZE(op->ob_type, nitems);
Neil Schemenauer43411b52001-08-30 00:05:51 +00001049#ifdef WITH_CYCLE_GC
1050 PyGC_Head *g = AS_GC(op);
Neil Schemenauerfec4eb12002-04-12 02:41:03 +00001051 g = PyObject_REALLOC(g, sizeof(PyGC_Head) + basicsize);
Neil Schemenauer43411b52001-08-30 00:05:51 +00001052 if (g == NULL)
1053 return (PyVarObject *)PyErr_NoMemory();
1054 op = (PyVarObject *) FROM_GC(g);
1055#else
Neil Schemenauerfec4eb12002-04-12 02:41:03 +00001056 op = PyObject_REALLOC(op, basicsize);
Neil Schemenauer43411b52001-08-30 00:05:51 +00001057 if (op == NULL)
1058 return (PyVarObject *)PyErr_NoMemory();
1059#endif
Tim Peters6d483d32001-10-06 21:27:34 +00001060 op->ob_size = nitems;
Neil Schemenauer43411b52001-08-30 00:05:51 +00001061 return op;
1062}
1063
1064void
Neil Schemenauerfec4eb12002-04-12 02:41:03 +00001065PyObject_GC_Del(void *op)
Neil Schemenauer43411b52001-08-30 00:05:51 +00001066{
1067#ifdef WITH_CYCLE_GC
1068 PyGC_Head *g = AS_GC(op);
Neil Schemenauera2b11ec2002-05-21 15:53:24 +00001069 if (IS_TRACKED(op))
Neil Schemenauer43411b52001-08-30 00:05:51 +00001070 gc_list_remove(g);
Neil Schemenauer2880ae52002-05-04 05:35:20 +00001071 if (generations[0].count > 0) {
1072 generations[0].count--;
Neil Schemenauer43411b52001-08-30 00:05:51 +00001073 }
Neil Schemenauerfec4eb12002-04-12 02:41:03 +00001074 PyObject_FREE(g);
Neil Schemenauer43411b52001-08-30 00:05:51 +00001075#else
Neil Schemenauerfec4eb12002-04-12 02:41:03 +00001076 PyObject_FREE(op);
Neil Schemenauer43411b52001-08-30 00:05:51 +00001077#endif
1078}
1079
Neil Schemenauerfec4eb12002-04-12 02:41:03 +00001080/* for binary compatibility with 2.2 */
1081#undef _PyObject_GC_Del
1082void
1083_PyObject_GC_Del(PyObject *op)
1084{
1085 PyObject_GC_Del(op);
1086}