blob: 6df2485bd66eeca003785d90b44c921e681f49d0 [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 */
Tim Petersaab713b2002-07-02 22:15:28 +0000227 assert(gc->gc.gc_refs != 0); /* else refcount was too small */
Tim Peters19b74c72002-07-01 03:52:19 +0000228 if (gc->gc.gc_refs > 0)
229 gc->gc.gc_refs--;
230 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000231 return 0;
232}
233
Tim Peters19b74c72002-07-01 03:52:19 +0000234/* Subtract internal references from gc_refs. After this, gc_refs is >= 0
235 * for all objects in containers, and is GC_REACHABLE for all tracked gc
236 * objects not in containers. The ones with gc_refs > 0 are directly
237 * reachable from outside containers, and so can't be collected.
238 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000239static void
240subtract_refs(PyGC_Head *containers)
241{
242 traverseproc traverse;
Tim Peters9e4ca102001-10-11 18:31:31 +0000243 PyGC_Head *gc = containers->gc.gc_next;
244 for (; gc != containers; gc=gc->gc.gc_next) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000245 traverse = FROM_GC(gc)->ob_type->tp_traverse;
246 (void) traverse(FROM_GC(gc),
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000247 (visitproc)visit_decref,
248 NULL);
249 }
250}
251
Tim Peters19b74c72002-07-01 03:52:19 +0000252/* A traversal callback for move_unreachable. */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000253static int
Tim Peters19b74c72002-07-01 03:52:19 +0000254visit_reachable(PyObject *op, PyGC_Head *reachable)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000255{
Tim Petersea405632002-07-02 00:52:30 +0000256 if (PyObject_IS_GC(op)) {
Tim Peters19b74c72002-07-01 03:52:19 +0000257 PyGC_Head *gc = AS_GC(op);
258 const int gc_refs = gc->gc.gc_refs;
259
260 if (gc_refs == 0) {
261 /* This is in move_unreachable's 'young' list, but
262 * the traversal hasn't yet gotten to it. All
263 * we need to do is tell move_unreachable that it's
264 * reachable.
265 */
266 gc->gc.gc_refs = 1;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000267 }
Tim Peters19b74c72002-07-01 03:52:19 +0000268 else if (gc_refs == GC_TENTATIVELY_UNREACHABLE) {
269 /* This had gc_refs = 0 when move_unreachable got
270 * to it, but turns out it's reachable after all.
271 * Move it back to move_unreachable's 'young' list,
272 * and move_unreachable will eventually get to it
273 * again.
274 */
275 gc_list_remove(gc);
276 gc_list_append(gc, reachable);
277 gc->gc.gc_refs = 1;
278 }
279 /* Else there's nothing to do.
280 * If gc_refs > 0, it must be in move_unreachable's 'young'
281 * list, and move_unreachable will eventually get to it.
282 * If gc_refs == GC_REACHABLE, it's either in some other
283 * generation so we don't care about it, or move_unreachable
Tim Peters6fc13d92002-07-02 18:12:35 +0000284 * already dealt with it.
Tim Petersea405632002-07-02 00:52:30 +0000285 * If gc_refs == GC_UNTRACKED, it must be ignored.
Tim Peters19b74c72002-07-01 03:52:19 +0000286 */
Tim Petersea405632002-07-02 00:52:30 +0000287 else {
288 assert(gc_refs > 0
289 || gc_refs == GC_REACHABLE
290 || gc_refs == GC_UNTRACKED);
291 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000292 }
293 return 0;
294}
295
Tim Peters19b74c72002-07-01 03:52:19 +0000296/* Move the unreachable objects from young to unreachable. After this,
297 * all objects in young have gc_refs = GC_REACHABLE, and all objects in
298 * unreachable have gc_refs = GC_TENTATIVELY_UNREACHABLE. All tracked
299 * gc objects not in young or unreachable still have gc_refs = GC_REACHABLE.
300 * All objects in young after this are directly or indirectly reachable
301 * from outside the original young; and all objects in unreachable are
302 * not.
Tim Peters88396172002-06-30 17:56:40 +0000303 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000304static void
Tim Peters19b74c72002-07-01 03:52:19 +0000305move_unreachable(PyGC_Head *young, PyGC_Head *unreachable)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000306{
Tim Peters19b74c72002-07-01 03:52:19 +0000307 PyGC_Head *gc = young->gc.gc_next;
308
309 /* Invariants: all objects "to the left" of us in young have gc_refs
310 * = GC_REACHABLE, and are indeed reachable (directly or indirectly)
311 * from outside the young list as it was at entry. All other objects
312 * from the original young "to the left" of us are in unreachable now,
313 * and have gc_refs = GC_TENTATIVELY_UNREACHABLE. All objects to the
314 * left of us in 'young' now have been scanned, and no objects here
315 * or to the right have been scanned yet.
316 */
317
318 while (gc != young) {
319 PyGC_Head *next;
320
Tim Peters6fc13d92002-07-02 18:12:35 +0000321 if (gc->gc.gc_refs) {
322 /* gc is definitely reachable from outside the
323 * original 'young'. Mark it as such, and traverse
324 * its pointers to find any other objects that may
325 * be directly reachable from it. Note that the
326 * call to tp_traverse may append objects to young,
327 * so we have to wait until it returns to determine
328 * the next object to visit.
329 */
330 PyObject *op = FROM_GC(gc);
331 traverseproc traverse = op->ob_type->tp_traverse;
332 assert(gc->gc.gc_refs > 0);
333 gc->gc.gc_refs = GC_REACHABLE;
334 (void) traverse(op,
335 (visitproc)visit_reachable,
336 (void *)young);
337 next = gc->gc.gc_next;
338 }
339 else {
Tim Peters19b74c72002-07-01 03:52:19 +0000340 /* This *may* be unreachable. To make progress,
341 * assume it is. gc isn't directly reachable from
342 * any object we've already traversed, but may be
343 * reachable from an object we haven't gotten to yet.
344 * visit_reachable will eventually move gc back into
345 * young if that's so, and we'll see it again.
346 */
347 next = gc->gc.gc_next;
348 gc_list_remove(gc);
349 gc_list_append(gc, unreachable);
350 gc->gc.gc_refs = GC_TENTATIVELY_UNREACHABLE;
351 }
Tim Peters19b74c72002-07-01 03:52:19 +0000352 gc = next;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000353 }
354}
355
Tim Peters88396172002-06-30 17:56:40 +0000356/* return true if object has a finalization method */
Neil Schemenauera765c122001-11-01 17:35:23 +0000357static int
358has_finalizer(PyObject *op)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000359{
Jeremy Hylton06257772000-08-31 15:10:24 +0000360 static PyObject *delstr = NULL;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000361 if (delstr == NULL) {
362 delstr = PyString_InternFromString("__del__");
Jeremy Hylton06257772000-08-31 15:10:24 +0000363 if (delstr == NULL)
364 Py_FatalError("PyGC: can't initialize __del__ string");
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000365 }
Tim Petersdb865612001-11-01 19:35:45 +0000366 return (PyInstance_Check(op) ||
367 PyType_HasFeature(op->ob_type, Py_TPFLAGS_HEAPTYPE))
368 && PyObject_HasAttr(op, delstr);
Neil Schemenauera765c122001-11-01 17:35:23 +0000369}
370
371/* Move all objects with finalizers (instances with __del__) */
372static void
373move_finalizers(PyGC_Head *unreachable, PyGC_Head *finalizers)
374{
375 PyGC_Head *next;
376 PyGC_Head *gc = unreachable->gc.gc_next;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000377 for (; gc != unreachable; gc=next) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000378 PyObject *op = FROM_GC(gc);
Tim Peters9e4ca102001-10-11 18:31:31 +0000379 next = gc->gc.gc_next;
Neil Schemenauera765c122001-11-01 17:35:23 +0000380 if (has_finalizer(op)) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000381 gc_list_remove(gc);
382 gc_list_append(gc, finalizers);
Tim Peters19b74c72002-07-01 03:52:19 +0000383 gc->gc.gc_refs = GC_REACHABLE;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000384 }
385 }
386}
387
Tim Peters19b74c72002-07-01 03:52:19 +0000388/* A traversal callback for move_finalizer_reachable. */
389static int
390visit_move(PyObject *op, PyGC_Head *tolist)
391{
392 if (PyObject_IS_GC(op)) {
Tim Petersea405632002-07-02 00:52:30 +0000393 if (IS_TENTATIVELY_UNREACHABLE(op)) {
Tim Peters19b74c72002-07-01 03:52:19 +0000394 PyGC_Head *gc = AS_GC(op);
395 gc_list_remove(gc);
396 gc_list_append(gc, tolist);
397 gc->gc.gc_refs = GC_REACHABLE;
398 }
399 }
400 return 0;
401}
402
403/* Move objects that are reachable from finalizers, from the unreachable set
404 * into the finalizers set.
405 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000406static void
407move_finalizer_reachable(PyGC_Head *finalizers)
408{
409 traverseproc traverse;
Tim Peters9e4ca102001-10-11 18:31:31 +0000410 PyGC_Head *gc = finalizers->gc.gc_next;
411 for (; gc != finalizers; gc=gc->gc.gc_next) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000412 /* careful, finalizers list is growing here */
Neil Schemenauer43411b52001-08-30 00:05:51 +0000413 traverse = FROM_GC(gc)->ob_type->tp_traverse;
Tim Peters88396172002-06-30 17:56:40 +0000414 (void) traverse(FROM_GC(gc),
Neil Schemenauer43411b52001-08-30 00:05:51 +0000415 (visitproc)visit_move,
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000416 (void *)finalizers);
417 }
418}
419
420static void
Jeremy Hylton06257772000-08-31 15:10:24 +0000421debug_instance(char *msg, PyInstanceObject *inst)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000422{
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000423 char *cname;
Neil Schemenauera765c122001-11-01 17:35:23 +0000424 /* simple version of instance_repr */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000425 PyObject *classname = inst->in_class->cl_name;
426 if (classname != NULL && PyString_Check(classname))
427 cname = PyString_AsString(classname);
428 else
429 cname = "?";
Jeremy Hylton06257772000-08-31 15:10:24 +0000430 PySys_WriteStderr("gc: %.100s <%.100s instance at %p>\n",
431 msg, cname, inst);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000432}
433
434static void
Jeremy Hylton06257772000-08-31 15:10:24 +0000435debug_cycle(char *msg, PyObject *op)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000436{
437 if ((debug & DEBUG_INSTANCES) && PyInstance_Check(op)) {
Jeremy Hylton06257772000-08-31 15:10:24 +0000438 debug_instance(msg, (PyInstanceObject *)op);
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000439 }
440 else if (debug & DEBUG_OBJECTS) {
Jeremy Hylton06257772000-08-31 15:10:24 +0000441 PySys_WriteStderr("gc: %.100s <%.100s %p>\n",
442 msg, op->ob_type->tp_name, op);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000443 }
444}
445
446/* Handle uncollectable garbage (cycles with finalizers). */
447static void
448handle_finalizers(PyGC_Head *finalizers, PyGC_Head *old)
449{
450 PyGC_Head *gc;
451 if (garbage == NULL) {
452 garbage = PyList_New(0);
453 }
Tim Peters9e4ca102001-10-11 18:31:31 +0000454 for (gc = finalizers->gc.gc_next; gc != finalizers;
455 gc = finalizers->gc.gc_next) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000456 PyObject *op = FROM_GC(gc);
Neil Schemenauera765c122001-11-01 17:35:23 +0000457 if ((debug & DEBUG_SAVEALL) || has_finalizer(op)) {
458 /* If SAVEALL is not set then just append objects with
459 * finalizers to the list of garbage. All objects in
460 * the finalizers list are reachable from those
Tim Peters19b74c72002-07-01 03:52:19 +0000461 * objects.
462 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000463 PyList_Append(garbage, op);
464 }
Tim Peters88396172002-06-30 17:56:40 +0000465 /* object is now reachable again */
Tim Peters19b74c72002-07-01 03:52:19 +0000466 assert(IS_REACHABLE(op));
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000467 gc_list_remove(gc);
468 gc_list_append(gc, old);
469 }
470}
471
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000472/* Break reference cycles by clearing the containers involved. This is
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000473 * tricky business as the lists can be changing and we don't know which
Tim Peters19b74c72002-07-01 03:52:19 +0000474 * objects may be freed. It is possible I screwed something up here.
475 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000476static void
477delete_garbage(PyGC_Head *unreachable, PyGC_Head *old)
478{
479 inquiry clear;
480
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000481 while (!gc_list_is_empty(unreachable)) {
Tim Peters9e4ca102001-10-11 18:31:31 +0000482 PyGC_Head *gc = unreachable->gc.gc_next;
Neil Schemenauer43411b52001-08-30 00:05:51 +0000483 PyObject *op = FROM_GC(gc);
Tim Peters88396172002-06-30 17:56:40 +0000484
Tim Peters19b74c72002-07-01 03:52:19 +0000485 assert(IS_TENTATIVELY_UNREACHABLE(op));
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000486 if (debug & DEBUG_SAVEALL) {
487 PyList_Append(garbage, op);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000488 }
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000489 else {
490 if ((clear = op->ob_type->tp_clear) != NULL) {
491 Py_INCREF(op);
Jeremy Hylton8a135182002-06-06 23:23:55 +0000492 clear(op);
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000493 Py_DECREF(op);
494 }
495 }
Tim Peters9e4ca102001-10-11 18:31:31 +0000496 if (unreachable->gc.gc_next == gc) {
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000497 /* object is still alive, move it, it may die later */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000498 gc_list_remove(gc);
499 gc_list_append(gc, old);
Tim Peters19b74c72002-07-01 03:52:19 +0000500 gc->gc.gc_refs = GC_REACHABLE;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000501 }
502 }
503}
504
505/* This is the main function. Read this to understand how the
506 * collection process works. */
507static long
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000508collect(int generation)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000509{
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000510 int i;
Tim Peters19b74c72002-07-01 03:52:19 +0000511 long m = 0; /* # objects collected */
512 long n = 0; /* # unreachable objects that couldn't be collected */
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000513 PyGC_Head *young; /* the generation we are examining */
514 PyGC_Head *old; /* next older generation */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000515 PyGC_Head unreachable;
516 PyGC_Head finalizers;
517 PyGC_Head *gc;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000518
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000519 if (debug & DEBUG_STATS) {
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000520 PySys_WriteStderr("gc: collecting generation %d...\n",
521 generation);
522 PySys_WriteStderr("gc: objects in each generation:");
523 for (i = 0; i < NUM_GENERATIONS; i++) {
524 PySys_WriteStderr(" %ld", gc_list_size(GEN_HEAD(i)));
525 }
526 PySys_WriteStderr("\n");
527 }
528
529 /* update collection and allocation counters */
530 if (generation+1 < NUM_GENERATIONS)
531 generations[generation+1].count += 1;
532 for (i = 0; i <= generation; i++)
Neil Schemenauerc9051642002-06-28 19:16:04 +0000533 generations[i].count = 0;
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000534
535 /* merge younger generations with one we are currently collecting */
536 for (i = 0; i < generation; i++) {
537 gc_list_merge(GEN_HEAD(i), GEN_HEAD(generation));
538 }
539
540 /* handy references */
541 young = GEN_HEAD(generation);
Tim Peters19b74c72002-07-01 03:52:19 +0000542 if (generation < NUM_GENERATIONS-1)
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000543 old = GEN_HEAD(generation+1);
Tim Peters19b74c72002-07-01 03:52:19 +0000544 else
545 old = young;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000546
547 /* Using ob_refcnt and gc_refs, calculate which objects in the
548 * container set are reachable from outside the set (ie. have a
549 * refcount greater than 0 when all the references within the
Tim Peters19b74c72002-07-01 03:52:19 +0000550 * set are taken into account
551 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000552 update_refs(young);
553 subtract_refs(young);
554
Tim Peters19b74c72002-07-01 03:52:19 +0000555 /* Leave everything reachable from outside young in young, and move
556 * everything else (in young) to unreachable.
557 * NOTE: This used to move the reachable objects into a reachable
558 * set instead. But most things usually turn out to be reachable,
559 * so it's more efficient to move the unreachable things.
560 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000561 gc_list_init(&unreachable);
Tim Peters19b74c72002-07-01 03:52:19 +0000562 move_unreachable(young, &unreachable);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000563
Tim Peters19b74c72002-07-01 03:52:19 +0000564 /* Move reachable objects to next generation. */
565 if (young != old)
566 gc_list_merge(young, old);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000567
Tim Peters19b74c72002-07-01 03:52:19 +0000568 /* All objects in unreachable are trash, but objects reachable from
569 * finalizers can't safely be deleted. Python programmers should take
570 * care not to create such things. For Python, finalizers means
571 * instance objects with __del__ methods.
572 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000573 gc_list_init(&finalizers);
574 move_finalizers(&unreachable, &finalizers);
575 move_finalizer_reachable(&finalizers);
576
577 /* Collect statistics on collectable objects found and print
578 * debugging information. */
Tim Peters9e4ca102001-10-11 18:31:31 +0000579 for (gc = unreachable.gc.gc_next; gc != &unreachable;
580 gc = gc->gc.gc_next) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000581 m++;
Jeremy Hylton06257772000-08-31 15:10:24 +0000582 if (debug & DEBUG_COLLECTABLE) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000583 debug_cycle("collectable", FROM_GC(gc));
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000584 }
585 }
Tim Peters19b74c72002-07-01 03:52:19 +0000586 /* Call tp_clear on objects in the collectable set. This will cause
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000587 * the reference cycles to be broken. It may also cause some objects in
588 * finalizers to be freed */
589 delete_garbage(&unreachable, old);
590
591 /* Collect statistics on uncollectable objects found and print
592 * debugging information. */
Tim Peters9e4ca102001-10-11 18:31:31 +0000593 for (gc = finalizers.gc.gc_next; gc != &finalizers;
594 gc = gc->gc.gc_next) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000595 n++;
Jeremy Hylton06257772000-08-31 15:10:24 +0000596 if (debug & DEBUG_UNCOLLECTABLE) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000597 debug_cycle("uncollectable", FROM_GC(gc));
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000598 }
599 }
Jeremy Hylton06257772000-08-31 15:10:24 +0000600 if (debug & DEBUG_STATS) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000601 if (m == 0 && n == 0) {
Jeremy Hylton06257772000-08-31 15:10:24 +0000602 PySys_WriteStderr("gc: done.\n");
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000603 }
604 else {
Jeremy Hylton06257772000-08-31 15:10:24 +0000605 PySys_WriteStderr(
606 "gc: done, %ld unreachable, %ld uncollectable.\n",
607 n+m, n);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000608 }
609 }
610
611 /* Append instances in the uncollectable set to a Python
612 * reachable list of garbage. The programmer has to deal with
613 * this if they insist on creating this type of structure. */
614 handle_finalizers(&finalizers, old);
615
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000616 if (PyErr_Occurred()) {
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000617 if (gc_str == NULL) {
618 gc_str = PyString_FromString("garbage collection");
619 }
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000620 PyErr_WriteUnraisable(gc_str);
621 Py_FatalError("unexpected exception during garbage collection");
622 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000623 return n+m;
624}
625
626static long
627collect_generations(void)
628{
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000629 int i;
Vladimir Marangozovb16714b2000-07-10 05:37:39 +0000630 long n = 0;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000631
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000632 /* Find the oldest generation (higest numbered) where the count
633 * exceeds the threshold. Objects in the that generation and
634 * generations younger than it will be collected. */
635 for (i = NUM_GENERATIONS-1; i >= 0; i--) {
636 if (generations[i].count > generations[i].threshold) {
637 n = collect(i);
638 break;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000639 }
640 }
641 return n;
642}
643
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000644PyDoc_STRVAR(gc_enable__doc__,
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000645"enable() -> None\n"
646"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000647"Enable automatic garbage collection.\n");
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000648
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000649static PyObject *
650gc_enable(PyObject *self, PyObject *args)
651{
652
653 if (!PyArg_ParseTuple(args, ":enable")) /* check no args */
654 return NULL;
655
656 enabled = 1;
657
658 Py_INCREF(Py_None);
659 return Py_None;
660}
661
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000662PyDoc_STRVAR(gc_disable__doc__,
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000663"disable() -> None\n"
664"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000665"Disable automatic garbage collection.\n");
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000666
667static PyObject *
668gc_disable(PyObject *self, PyObject *args)
669{
670
671 if (!PyArg_ParseTuple(args, ":disable")) /* check no args */
672 return NULL;
673
674 enabled = 0;
675
676 Py_INCREF(Py_None);
677 return Py_None;
678}
679
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000680PyDoc_STRVAR(gc_isenabled__doc__,
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000681"isenabled() -> status\n"
682"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000683"Returns true if automatic garbage collection is enabled.\n");
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000684
685static PyObject *
686gc_isenabled(PyObject *self, PyObject *args)
687{
688
689 if (!PyArg_ParseTuple(args, ":isenabled")) /* check no args */
690 return NULL;
691
692 return Py_BuildValue("i", enabled);
693}
694
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000695PyDoc_STRVAR(gc_collect__doc__,
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000696"collect() -> n\n"
697"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000698"Run a full collection. The number of unreachable objects is returned.\n");
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000699
700static PyObject *
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000701gc_collect(PyObject *self, PyObject *args)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000702{
703 long n;
704
Fred Drakecc1be242000-07-12 04:42:23 +0000705 if (!PyArg_ParseTuple(args, ":collect")) /* check no args */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000706 return NULL;
707
Neil Schemenauere8c40cb2001-10-31 23:09:35 +0000708 if (collecting) {
709 n = 0; /* already collecting, don't do anything */
710 }
711 else {
712 collecting = 1;
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000713 n = collect(NUM_GENERATIONS - 1);
Neil Schemenauere8c40cb2001-10-31 23:09:35 +0000714 collecting = 0;
715 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000716
Neil Schemenauer7760cff2000-09-22 22:35:36 +0000717 return Py_BuildValue("l", n);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000718}
719
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000720PyDoc_STRVAR(gc_set_debug__doc__,
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000721"set_debug(flags) -> None\n"
722"\n"
723"Set the garbage collection debugging flags. Debugging information is\n"
724"written to sys.stderr.\n"
725"\n"
726"flags is an integer and can have the following bits turned on:\n"
727"\n"
728" DEBUG_STATS - Print statistics during collection.\n"
729" DEBUG_COLLECTABLE - Print collectable objects found.\n"
730" DEBUG_UNCOLLECTABLE - Print unreachable but uncollectable objects found.\n"
731" DEBUG_INSTANCES - Print instance objects.\n"
732" DEBUG_OBJECTS - Print objects other than instances.\n"
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000733" DEBUG_SAVEALL - Save objects to gc.garbage rather than freeing them.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000734" DEBUG_LEAK - Debug leaking programs (everything but STATS).\n");
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000735
736static PyObject *
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000737gc_set_debug(PyObject *self, PyObject *args)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000738{
Neil Schemenauer7760cff2000-09-22 22:35:36 +0000739 if (!PyArg_ParseTuple(args, "i:set_debug", &debug))
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000740 return NULL;
741
742 Py_INCREF(Py_None);
743 return Py_None;
744}
745
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000746PyDoc_STRVAR(gc_get_debug__doc__,
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000747"get_debug() -> flags\n"
748"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000749"Get the garbage collection debugging flags.\n");
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000750
751static PyObject *
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000752gc_get_debug(PyObject *self, PyObject *args)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000753{
Fred Drakecc1be242000-07-12 04:42:23 +0000754 if (!PyArg_ParseTuple(args, ":get_debug")) /* no args */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000755 return NULL;
756
757 return Py_BuildValue("i", debug);
758}
759
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000760PyDoc_STRVAR(gc_set_thresh__doc__,
Neal Norwitz2a47c0f2002-01-29 00:53:41 +0000761"set_threshold(threshold0, [threshold1, threshold2]) -> None\n"
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000762"\n"
763"Sets the collection thresholds. Setting threshold0 to zero disables\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000764"collection.\n");
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000765
766static PyObject *
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000767gc_set_thresh(PyObject *self, PyObject *args)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000768{
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000769 int i;
770 if (!PyArg_ParseTuple(args, "i|ii:set_threshold",
771 &generations[0].threshold,
772 &generations[1].threshold,
773 &generations[2].threshold))
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000774 return NULL;
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000775 for (i = 2; i < NUM_GENERATIONS; i++) {
776 /* generations higher than 2 get the same threshold */
777 generations[i].threshold = generations[2].threshold;
778 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000779
780 Py_INCREF(Py_None);
781 return Py_None;
782}
783
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000784PyDoc_STRVAR(gc_get_thresh__doc__,
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000785"get_threshold() -> (threshold0, threshold1, threshold2)\n"
786"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000787"Return the current collection thresholds\n");
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000788
789static PyObject *
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000790gc_get_thresh(PyObject *self, PyObject *args)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000791{
Fred Drakecc1be242000-07-12 04:42:23 +0000792 if (!PyArg_ParseTuple(args, ":get_threshold")) /* no args */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000793 return NULL;
794
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000795 return Py_BuildValue("(iii)",
796 generations[0].threshold,
797 generations[1].threshold,
798 generations[2].threshold);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000799}
800
Neil Schemenauer48c70342001-08-09 15:38:31 +0000801static int
Martin v. Löwis560da622001-11-24 09:24:51 +0000802referrersvisit(PyObject* obj, PyObject *objs)
Neil Schemenauer48c70342001-08-09 15:38:31 +0000803{
Martin v. Löwisc8fe77b2001-11-29 18:08:31 +0000804 int i;
805 for (i = 0; i < PyTuple_GET_SIZE(objs); i++)
806 if (PyTuple_GET_ITEM(objs, i) == obj)
807 return 1;
Neil Schemenauer48c70342001-08-09 15:38:31 +0000808 return 0;
809}
810
Neil Schemenauer17e7be62001-08-10 14:46:47 +0000811static int
Martin v. Löwis560da622001-11-24 09:24:51 +0000812gc_referrers_for(PyObject *objs, PyGC_Head *list, PyObject *resultlist)
Neil Schemenauer48c70342001-08-09 15:38:31 +0000813{
814 PyGC_Head *gc;
815 PyObject *obj;
816 traverseproc traverse;
Tim Peters9e4ca102001-10-11 18:31:31 +0000817 for (gc = list->gc.gc_next; gc != list; gc = gc->gc.gc_next) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000818 obj = FROM_GC(gc);
Neil Schemenauer48c70342001-08-09 15:38:31 +0000819 traverse = obj->ob_type->tp_traverse;
820 if (obj == objs || obj == resultlist)
821 continue;
Martin v. Löwis560da622001-11-24 09:24:51 +0000822 if (traverse(obj, (visitproc)referrersvisit, objs)) {
Neil Schemenauer17e7be62001-08-10 14:46:47 +0000823 if (PyList_Append(resultlist, obj) < 0)
824 return 0; /* error */
Neil Schemenauer48c70342001-08-09 15:38:31 +0000825 }
826 }
Neil Schemenauer17e7be62001-08-10 14:46:47 +0000827 return 1; /* no error */
Neil Schemenauer48c70342001-08-09 15:38:31 +0000828}
829
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000830PyDoc_STRVAR(gc_get_referrers__doc__,
Martin v. Löwis560da622001-11-24 09:24:51 +0000831"get_referrers(*objs) -> list\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000832Return the list of objects that directly refer to any of objs.");
Neil Schemenauer48c70342001-08-09 15:38:31 +0000833
Neil Schemenauer17e7be62001-08-10 14:46:47 +0000834static PyObject *
Martin v. Löwis560da622001-11-24 09:24:51 +0000835gc_get_referrers(PyObject *self, PyObject *args)
Neil Schemenauer48c70342001-08-09 15:38:31 +0000836{
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000837 int i;
Neil Schemenauer48c70342001-08-09 15:38:31 +0000838 PyObject *result = PyList_New(0);
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000839 for (i = 0; i < NUM_GENERATIONS; i++) {
840 if (!(gc_referrers_for(args, GEN_HEAD(i), result))) {
841 Py_DECREF(result);
842 return NULL;
843 }
Neil Schemenauer17e7be62001-08-10 14:46:47 +0000844 }
Neil Schemenauer48c70342001-08-09 15:38:31 +0000845 return result;
846}
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000847
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000848PyDoc_STRVAR(gc_get_objects__doc__,
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000849"get_objects() -> [...]\n"
850"\n"
851"Return a list of objects tracked by the collector (excluding the list\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000852"returned).\n");
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000853
854/* appending objects in a GC list to a Python list */
Martin v. Löwis155aad12001-12-02 12:21:34 +0000855static int
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000856append_objects(PyObject *py_list, PyGC_Head *gc_list)
857{
858 PyGC_Head *gc;
Tim Peters9e4ca102001-10-11 18:31:31 +0000859 for (gc = gc_list->gc.gc_next; gc != gc_list; gc = gc->gc.gc_next) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000860 PyObject *op = FROM_GC(gc);
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000861 if (op != py_list) {
Martin v. Löwis155aad12001-12-02 12:21:34 +0000862 if (PyList_Append(py_list, op)) {
863 return -1; /* exception */
864 }
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000865 }
866 }
Martin v. Löwis155aad12001-12-02 12:21:34 +0000867 return 0;
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000868}
869
870static PyObject *
871gc_get_objects(PyObject *self, PyObject *args)
872{
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000873 int i;
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000874 PyObject* result;
875
876 if (!PyArg_ParseTuple(args, ":get_objects")) /* check no args */
877 return NULL;
878 result = PyList_New(0);
Martin v. Löwisf8a6f242001-12-02 18:31:02 +0000879 if (result == NULL) {
880 return NULL;
881 }
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000882 for (i = 0; i < NUM_GENERATIONS; i++) {
883 if (append_objects(result, GEN_HEAD(i))) {
884 Py_DECREF(result);
885 return NULL;
886 }
Martin v. Löwis155aad12001-12-02 12:21:34 +0000887 }
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000888 return result;
889}
890
891
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000892PyDoc_STRVAR(gc__doc__,
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000893"This module provides access to the garbage collector for reference cycles.\n"
894"\n"
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000895"enable() -- Enable automatic garbage collection.\n"
896"disable() -- Disable automatic garbage collection.\n"
897"isenabled() -- Returns true if automatic collection is enabled.\n"
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000898"collect() -- Do a full collection right now.\n"
899"set_debug() -- Set debugging flags.\n"
900"get_debug() -- Get debugging flags.\n"
901"set_threshold() -- Set the collection thresholds.\n"
902"get_threshold() -- Return the current the collection thresholds.\n"
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000903"get_objects() -- Return a list of all objects tracked by the collector.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000904"get_referrers() -- Return the list of objects that refer to an object.\n");
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000905
906static PyMethodDef GcMethods[] = {
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000907 {"enable", gc_enable, METH_VARARGS, gc_enable__doc__},
908 {"disable", gc_disable, METH_VARARGS, gc_disable__doc__},
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000909 {"isenabled", gc_isenabled, METH_VARARGS, gc_isenabled__doc__},
910 {"set_debug", gc_set_debug, METH_VARARGS, gc_set_debug__doc__},
911 {"get_debug", gc_get_debug, METH_VARARGS, gc_get_debug__doc__},
912 {"set_threshold", gc_set_thresh, METH_VARARGS, gc_set_thresh__doc__},
913 {"get_threshold", gc_get_thresh, METH_VARARGS, gc_get_thresh__doc__},
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000914 {"collect", gc_collect, METH_VARARGS, gc_collect__doc__},
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000915 {"get_objects", gc_get_objects,METH_VARARGS, gc_get_objects__doc__},
Martin v. Löwis560da622001-11-24 09:24:51 +0000916 {"get_referrers", gc_get_referrers, METH_VARARGS,
917 gc_get_referrers__doc__},
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000918 {NULL, NULL} /* Sentinel */
919};
920
921void
922initgc(void)
923{
924 PyObject *m;
925 PyObject *d;
926
927 m = Py_InitModule4("gc",
928 GcMethods,
929 gc__doc__,
930 NULL,
931 PYTHON_API_VERSION);
932 d = PyModule_GetDict(m);
933 if (garbage == NULL) {
934 garbage = PyList_New(0);
935 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000936 PyDict_SetItemString(d, "garbage", garbage);
937 PyDict_SetItemString(d, "DEBUG_STATS",
938 PyInt_FromLong(DEBUG_STATS));
939 PyDict_SetItemString(d, "DEBUG_COLLECTABLE",
940 PyInt_FromLong(DEBUG_COLLECTABLE));
941 PyDict_SetItemString(d, "DEBUG_UNCOLLECTABLE",
942 PyInt_FromLong(DEBUG_UNCOLLECTABLE));
943 PyDict_SetItemString(d, "DEBUG_INSTANCES",
944 PyInt_FromLong(DEBUG_INSTANCES));
945 PyDict_SetItemString(d, "DEBUG_OBJECTS",
946 PyInt_FromLong(DEBUG_OBJECTS));
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000947 PyDict_SetItemString(d, "DEBUG_SAVEALL",
948 PyInt_FromLong(DEBUG_SAVEALL));
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000949 PyDict_SetItemString(d, "DEBUG_LEAK",
950 PyInt_FromLong(DEBUG_LEAK));
951}
952
Neil Schemenauer43411b52001-08-30 00:05:51 +0000953/* for debugging */
954void _PyGC_Dump(PyGC_Head *g)
955{
956 _PyObject_Dump(FROM_GC(g));
957}
958
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000959#endif /* WITH_CYCLE_GC */
Neil Schemenauer43411b52001-08-30 00:05:51 +0000960
961/* extension modules might be compiled with GC support so these
962 functions must always be available */
963
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000964#undef PyObject_GC_Track
965#undef PyObject_GC_UnTrack
966#undef PyObject_GC_Del
967#undef _PyObject_GC_Malloc
968
Neil Schemenauer43411b52001-08-30 00:05:51 +0000969void
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000970PyObject_GC_Track(void *op)
Neil Schemenauer43411b52001-08-30 00:05:51 +0000971{
972 _PyObject_GC_TRACK(op);
973}
974
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000975/* for binary compatibility with 2.2 */
Neil Schemenauer43411b52001-08-30 00:05:51 +0000976void
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000977_PyObject_GC_Track(PyObject *op)
978{
979 PyObject_GC_Track(op);
980}
981
982void
983PyObject_GC_UnTrack(void *op)
Neil Schemenauer43411b52001-08-30 00:05:51 +0000984{
Neil Schemenauerb8833102002-03-29 03:04:25 +0000985#ifdef WITH_CYCLE_GC
Neil Schemenauera2b11ec2002-05-21 15:53:24 +0000986 if (IS_TRACKED(op))
Guido van Rossumff413af2002-03-28 20:34:59 +0000987 _PyObject_GC_UNTRACK(op);
Neil Schemenauerb8833102002-03-29 03:04:25 +0000988#endif
Neil Schemenauer43411b52001-08-30 00:05:51 +0000989}
990
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000991/* for binary compatibility with 2.2 */
992void
993_PyObject_GC_UnTrack(PyObject *op)
994{
995 PyObject_GC_UnTrack(op);
996}
997
Neil Schemenauer43411b52001-08-30 00:05:51 +0000998PyObject *
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000999_PyObject_GC_Malloc(size_t basicsize)
Neil Schemenauer43411b52001-08-30 00:05:51 +00001000{
1001 PyObject *op;
1002#ifdef WITH_CYCLE_GC
Neil Schemenauerfec4eb12002-04-12 02:41:03 +00001003 PyGC_Head *g = PyObject_MALLOC(sizeof(PyGC_Head) + basicsize);
Neil Schemenauer43411b52001-08-30 00:05:51 +00001004 if (g == NULL)
Jeremy Hylton8a135182002-06-06 23:23:55 +00001005 return PyErr_NoMemory();
Tim Petersea405632002-07-02 00:52:30 +00001006 g->gc.gc_refs = GC_UNTRACKED;
Neil Schemenauer2880ae52002-05-04 05:35:20 +00001007 generations[0].count++; /* number of allocated GC objects */
1008 if (generations[0].count > generations[0].threshold &&
Neil Schemenauer43411b52001-08-30 00:05:51 +00001009 enabled &&
Neil Schemenauer2880ae52002-05-04 05:35:20 +00001010 generations[0].threshold &&
Neil Schemenauer43411b52001-08-30 00:05:51 +00001011 !collecting &&
1012 !PyErr_Occurred()) {
1013 collecting = 1;
Neil Schemenauer2880ae52002-05-04 05:35:20 +00001014 collect_generations();
Neil Schemenauer43411b52001-08-30 00:05:51 +00001015 collecting = 0;
1016 }
1017 op = FROM_GC(g);
1018#else
Neil Schemenauerfec4eb12002-04-12 02:41:03 +00001019 op = PyObject_MALLOC(basicsize);
Neil Schemenauer43411b52001-08-30 00:05:51 +00001020 if (op == NULL)
Jeremy Hylton8a135182002-06-06 23:23:55 +00001021 return PyErr_NoMemory();
Neil Schemenauer43411b52001-08-30 00:05:51 +00001022
1023#endif
1024 return op;
1025}
1026
1027PyObject *
1028_PyObject_GC_New(PyTypeObject *tp)
1029{
Neil Schemenauerfec4eb12002-04-12 02:41:03 +00001030 PyObject *op = _PyObject_GC_Malloc(_PyObject_SIZE(tp));
Tim Petersfa8efab2002-04-28 01:57:25 +00001031 if (op != NULL)
1032 op = PyObject_INIT(op, tp);
1033 return op;
Neil Schemenauer43411b52001-08-30 00:05:51 +00001034}
1035
1036PyVarObject *
Tim Peters6d483d32001-10-06 21:27:34 +00001037_PyObject_GC_NewVar(PyTypeObject *tp, int nitems)
Neil Schemenauer43411b52001-08-30 00:05:51 +00001038{
Neil Schemenauerfec4eb12002-04-12 02:41:03 +00001039 const size_t size = _PyObject_VAR_SIZE(tp, nitems);
1040 PyVarObject *op = (PyVarObject *) _PyObject_GC_Malloc(size);
Tim Petersfa8efab2002-04-28 01:57:25 +00001041 if (op != NULL)
1042 op = PyObject_INIT_VAR(op, tp, nitems);
1043 return op;
Neil Schemenauer43411b52001-08-30 00:05:51 +00001044}
1045
1046PyVarObject *
Tim Peters6d483d32001-10-06 21:27:34 +00001047_PyObject_GC_Resize(PyVarObject *op, int nitems)
Neil Schemenauer43411b52001-08-30 00:05:51 +00001048{
Tim Petersf2a67da2001-10-07 03:54:51 +00001049 const size_t basicsize = _PyObject_VAR_SIZE(op->ob_type, nitems);
Neil Schemenauer43411b52001-08-30 00:05:51 +00001050#ifdef WITH_CYCLE_GC
1051 PyGC_Head *g = AS_GC(op);
Neil Schemenauerfec4eb12002-04-12 02:41:03 +00001052 g = PyObject_REALLOC(g, sizeof(PyGC_Head) + basicsize);
Neil Schemenauer43411b52001-08-30 00:05:51 +00001053 if (g == NULL)
1054 return (PyVarObject *)PyErr_NoMemory();
1055 op = (PyVarObject *) FROM_GC(g);
1056#else
Neil Schemenauerfec4eb12002-04-12 02:41:03 +00001057 op = PyObject_REALLOC(op, basicsize);
Neil Schemenauer43411b52001-08-30 00:05:51 +00001058 if (op == NULL)
1059 return (PyVarObject *)PyErr_NoMemory();
1060#endif
Tim Peters6d483d32001-10-06 21:27:34 +00001061 op->ob_size = nitems;
Neil Schemenauer43411b52001-08-30 00:05:51 +00001062 return op;
1063}
1064
1065void
Neil Schemenauerfec4eb12002-04-12 02:41:03 +00001066PyObject_GC_Del(void *op)
Neil Schemenauer43411b52001-08-30 00:05:51 +00001067{
1068#ifdef WITH_CYCLE_GC
1069 PyGC_Head *g = AS_GC(op);
Neil Schemenauera2b11ec2002-05-21 15:53:24 +00001070 if (IS_TRACKED(op))
Neil Schemenauer43411b52001-08-30 00:05:51 +00001071 gc_list_remove(g);
Neil Schemenauer2880ae52002-05-04 05:35:20 +00001072 if (generations[0].count > 0) {
1073 generations[0].count--;
Neil Schemenauer43411b52001-08-30 00:05:51 +00001074 }
Neil Schemenauerfec4eb12002-04-12 02:41:03 +00001075 PyObject_FREE(g);
Neil Schemenauer43411b52001-08-30 00:05:51 +00001076#else
Neil Schemenauerfec4eb12002-04-12 02:41:03 +00001077 PyObject_FREE(op);
Neil Schemenauer43411b52001-08-30 00:05:51 +00001078#endif
1079}
1080
Neil Schemenauerfec4eb12002-04-12 02:41:03 +00001081/* for binary compatibility with 2.2 */
1082#undef _PyObject_GC_Del
1083void
1084_PyObject_GC_Del(PyObject *op)
1085{
1086 PyObject_GC_Del(op);
1087}