blob: d8893d1b16aab3773bbcdeec881215500fee9704 [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/
Neil Schemenauera7024e92008-07-15 19:24:01 +000012
13 The following mailing list threads provide a historical perspective on
14 the design of this module. Note that a fair amount of refinement has
15 occurred since those discussions.
16
17 http://mail.python.org/pipermail/python-dev/2000-March/002385.html
18 http://mail.python.org/pipermail/python-dev/2000-March/002434.html
19 http://mail.python.org/pipermail/python-dev/2000-March/002497.html
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000020
21 For a highlevel view of the collection process, read the collect
22 function.
23
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000024*/
25
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000026#include "Python.h"
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000027#include "frameobject.h" /* for PyFrame_ClearFreeList */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000028
Neil Schemenauer43411b52001-08-30 00:05:51 +000029/* Get an object's GC head */
30#define AS_GC(o) ((PyGC_Head *)(o)-1)
31
32/* Get the object given the GC head */
33#define FROM_GC(g) ((PyObject *)(((PyGC_Head *)g)+1))
34
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000035/*** Global GC state ***/
36
Neil Schemenauer2880ae52002-05-04 05:35:20 +000037struct gc_generation {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000038 PyGC_Head head;
39 int threshold; /* collection threshold */
40 int count; /* count of allocations or collections of younger
41 generations */
Neil Schemenauer2880ae52002-05-04 05:35:20 +000042};
43
44#define NUM_GENERATIONS 3
45#define GEN_HEAD(n) (&generations[n].head)
46
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000047/* linked lists of container objects */
Neil Schemenauer2880ae52002-05-04 05:35:20 +000048static struct gc_generation generations[NUM_GENERATIONS] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000049 /* PyGC_Head, threshold, count */
50 {{{GEN_HEAD(0), GEN_HEAD(0), 0}}, 700, 0},
51 {{{GEN_HEAD(1), GEN_HEAD(1), 0}}, 10, 0},
52 {{{GEN_HEAD(2), GEN_HEAD(2), 0}}, 10, 0},
Neil Schemenauer2880ae52002-05-04 05:35:20 +000053};
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000054
Neil Schemenauer2880ae52002-05-04 05:35:20 +000055PyGC_Head *_PyGC_generation0 = GEN_HEAD(0);
56
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +000057static int enabled = 1; /* automatic collection enabled? */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000058
Neil Schemenauer43411b52001-08-30 00:05:51 +000059/* true if we are currently running the collector */
Tim Petersbf384c22003-04-06 00:11:39 +000060static int collecting = 0;
Neil Schemenauer43411b52001-08-30 00:05:51 +000061
Tim Peters6fc13d92002-07-02 18:12:35 +000062/* list of uncollectable objects */
Tim Petersbf384c22003-04-06 00:11:39 +000063static PyObject *garbage = NULL;
Tim Peters6fc13d92002-07-02 18:12:35 +000064
65/* Python string to use if unhandled exception occurs */
Tim Petersbf384c22003-04-06 00:11:39 +000066static PyObject *gc_str = NULL;
Tim Peters6fc13d92002-07-02 18:12:35 +000067
Antoine Pitrou14b78f52009-01-09 22:27:08 +000068/* This is the number of objects who survived the last full collection. It
69 approximates the number of long lived objects tracked by the GC.
70
71 (by "full collection", we mean a collection of the oldest generation).
72*/
73static Py_ssize_t long_lived_total = 0;
74
75/* This is the number of objects who survived all "non-full" collections,
76 and are awaiting to undergo a full collection for the first time.
77
78*/
79static Py_ssize_t long_lived_pending = 0;
80
81/*
82 NOTE: about the counting of long-lived objects.
83
84 To limit the cost of garbage collection, there are two strategies;
85 - make each collection faster, e.g. by scanning fewer objects
86 - do less collections
87 This heuristic is about the latter strategy.
88
89 In addition to the various configurable thresholds, we only trigger a
90 full collection if the ratio
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000091 long_lived_pending / long_lived_total
Antoine Pitrou14b78f52009-01-09 22:27:08 +000092 is above a given value (hardwired to 25%).
93
94 The reason is that, while "non-full" collections (i.e., collections of
95 the young and middle generations) will always examine roughly the same
96 number of objects -- determined by the aforementioned thresholds --,
97 the cost of a full collection is proportional to the total number of
98 long-lived objects, which is virtually unbounded.
99
100 Indeed, it has been remarked that doing a full collection every
101 <constant number> of object creations entails a dramatic performance
102 degradation in workloads which consist in creating and storing lots of
103 long-lived objects (e.g. building a large list of GC-tracked objects would
104 show quadratic performance, instead of linear as expected: see issue #4074).
105
106 Using the above ratio, instead, yields amortized linear performance in
107 the total number of objects (the effect of which can be summarized
108 thusly: "each full garbage collection is more and more costly as the
109 number of objects grows, but we do fewer and fewer of them").
110
111 This heuristic was suggested by Martin von Löwis on python-dev in
112 June 2008. His original analysis and proposal can be found at:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 http://mail.python.org/pipermail/python-dev/2008-June/080579.html
Antoine Pitrou14b78f52009-01-09 22:27:08 +0000114*/
115
116
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000117/* set for debugging information */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000118#define DEBUG_STATS (1<<0) /* print collection statistics */
119#define DEBUG_COLLECTABLE (1<<1) /* print collectable objects */
120#define DEBUG_UNCOLLECTABLE (1<<2) /* print uncollectable objects */
121#define DEBUG_SAVEALL (1<<5) /* save all garbage in gc.garbage */
122#define DEBUG_LEAK DEBUG_COLLECTABLE | \
123 DEBUG_UNCOLLECTABLE | \
124 DEBUG_SAVEALL
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000125static int debug;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000126static PyObject *tmod = NULL;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000127
Tim Peters6fc13d92002-07-02 18:12:35 +0000128/*--------------------------------------------------------------------------
129gc_refs values.
Neil Schemenauer43411b52001-08-30 00:05:51 +0000130
Tim Peters6fc13d92002-07-02 18:12:35 +0000131Between collections, every gc'ed object has one of two gc_refs values:
132
133GC_UNTRACKED
134 The initial state; objects returned by PyObject_GC_Malloc are in this
135 state. The object doesn't live in any generation list, and its
136 tp_traverse slot must not be called.
137
138GC_REACHABLE
139 The object lives in some generation list, and its tp_traverse is safe to
140 call. An object transitions to GC_REACHABLE when PyObject_GC_Track
141 is called.
142
143During a collection, gc_refs can temporarily take on other states:
144
145>= 0
146 At the start of a collection, update_refs() copies the true refcount
147 to gc_refs, for each object in the generation being collected.
148 subtract_refs() then adjusts gc_refs so that it equals the number of
149 times an object is referenced directly from outside the generation
150 being collected.
Martin v. Löwis774348c2002-11-09 19:54:06 +0000151 gc_refs remains >= 0 throughout these steps.
Tim Peters6fc13d92002-07-02 18:12:35 +0000152
153GC_TENTATIVELY_UNREACHABLE
154 move_unreachable() then moves objects not reachable (whether directly or
155 indirectly) from outside the generation into an "unreachable" set.
156 Objects that are found to be reachable have gc_refs set to GC_REACHABLE
157 again. Objects that are found to be unreachable have gc_refs set to
158 GC_TENTATIVELY_UNREACHABLE. It's "tentatively" because the pass doing
159 this can't be sure until it ends, and GC_TENTATIVELY_UNREACHABLE may
160 transition back to GC_REACHABLE.
161
162 Only objects with GC_TENTATIVELY_UNREACHABLE still set are candidates
163 for collection. If it's decided not to collect such an object (e.g.,
164 it has a __del__ method), its gc_refs is restored to GC_REACHABLE again.
165----------------------------------------------------------------------------
166*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000167#define GC_UNTRACKED _PyGC_REFS_UNTRACKED
168#define GC_REACHABLE _PyGC_REFS_REACHABLE
169#define GC_TENTATIVELY_UNREACHABLE _PyGC_REFS_TENTATIVELY_UNREACHABLE
Tim Peters19b74c72002-07-01 03:52:19 +0000170
Tim Peters6fc13d92002-07-02 18:12:35 +0000171#define IS_TRACKED(o) ((AS_GC(o))->gc.gc_refs != GC_UNTRACKED)
Tim Peters19b74c72002-07-01 03:52:19 +0000172#define IS_REACHABLE(o) ((AS_GC(o))->gc.gc_refs == GC_REACHABLE)
173#define IS_TENTATIVELY_UNREACHABLE(o) ( \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000174 (AS_GC(o))->gc.gc_refs == GC_TENTATIVELY_UNREACHABLE)
Neil Schemenauera2b11ec2002-05-21 15:53:24 +0000175
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000176/*** list functions ***/
177
178static void
179gc_list_init(PyGC_Head *list)
180{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000181 list->gc.gc_prev = list;
182 list->gc.gc_next = list;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000183}
184
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000185static int
186gc_list_is_empty(PyGC_Head *list)
187{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000188 return (list->gc.gc_next == list);
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000189}
190
Tim Peterse2d59182004-11-01 01:39:08 +0000191#if 0
192/* This became unused after gc_list_move() was introduced. */
193/* Append `node` to `list`. */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000194static void
195gc_list_append(PyGC_Head *node, PyGC_Head *list)
196{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000197 node->gc.gc_next = list;
198 node->gc.gc_prev = list->gc.gc_prev;
199 node->gc.gc_prev->gc.gc_next = node;
200 list->gc.gc_prev = node;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000201}
Tim Peterse2d59182004-11-01 01:39:08 +0000202#endif
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000203
Tim Peterse2d59182004-11-01 01:39:08 +0000204/* Remove `node` from the gc list it's currently in. */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000205static void
206gc_list_remove(PyGC_Head *node)
207{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000208 node->gc.gc_prev->gc.gc_next = node->gc.gc_next;
209 node->gc.gc_next->gc.gc_prev = node->gc.gc_prev;
210 node->gc.gc_next = NULL; /* object is not currently tracked */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000211}
212
Tim Peterse2d59182004-11-01 01:39:08 +0000213/* Move `node` from the gc list it's currently in (which is not explicitly
214 * named here) to the end of `list`. This is semantically the same as
215 * gc_list_remove(node) followed by gc_list_append(node, list).
216 */
217static void
218gc_list_move(PyGC_Head *node, PyGC_Head *list)
219{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000220 PyGC_Head *new_prev;
221 PyGC_Head *current_prev = node->gc.gc_prev;
222 PyGC_Head *current_next = node->gc.gc_next;
223 /* Unlink from current list. */
224 current_prev->gc.gc_next = current_next;
225 current_next->gc.gc_prev = current_prev;
226 /* Relink at end of new list. */
227 new_prev = node->gc.gc_prev = list->gc.gc_prev;
228 new_prev->gc.gc_next = list->gc.gc_prev = node;
229 node->gc.gc_next = list;
Tim Peterse2d59182004-11-01 01:39:08 +0000230}
231
232/* append list `from` onto list `to`; `from` becomes an empty list */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000233static void
234gc_list_merge(PyGC_Head *from, PyGC_Head *to)
235{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000236 PyGC_Head *tail;
237 assert(from != to);
238 if (!gc_list_is_empty(from)) {
239 tail = to->gc.gc_prev;
240 tail->gc.gc_next = from->gc.gc_next;
241 tail->gc.gc_next->gc.gc_prev = tail;
242 to->gc.gc_prev = from->gc.gc_prev;
243 to->gc.gc_prev->gc.gc_next = to;
244 }
245 gc_list_init(from);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000246}
247
Neal Norwitz7b216c52006-03-04 20:01:53 +0000248static Py_ssize_t
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000249gc_list_size(PyGC_Head *list)
250{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 PyGC_Head *gc;
252 Py_ssize_t n = 0;
253 for (gc = list->gc.gc_next; gc != list; gc = gc->gc.gc_next) {
254 n++;
255 }
256 return n;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000257}
258
Tim Peters259272b2003-04-06 19:41:39 +0000259/* Append objects in a GC list to a Python list.
260 * Return 0 if all OK, < 0 if error (out of memory for list).
261 */
262static int
263append_objects(PyObject *py_list, PyGC_Head *gc_list)
264{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 PyGC_Head *gc;
266 for (gc = gc_list->gc.gc_next; gc != gc_list; gc = gc->gc.gc_next) {
267 PyObject *op = FROM_GC(gc);
268 if (op != py_list) {
269 if (PyList_Append(py_list, op)) {
270 return -1; /* exception */
271 }
272 }
273 }
274 return 0;
Tim Peters259272b2003-04-06 19:41:39 +0000275}
276
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000277/*** end of list stuff ***/
278
279
Tim Peters19b74c72002-07-01 03:52:19 +0000280/* Set all gc_refs = ob_refcnt. After this, gc_refs is > 0 for all objects
281 * in containers, and is GC_REACHABLE for all tracked gc objects not in
282 * containers.
Tim Peters88396172002-06-30 17:56:40 +0000283 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000284static void
285update_refs(PyGC_Head *containers)
286{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 PyGC_Head *gc = containers->gc.gc_next;
288 for (; gc != containers; gc = gc->gc.gc_next) {
289 assert(gc->gc.gc_refs == GC_REACHABLE);
290 gc->gc.gc_refs = Py_REFCNT(FROM_GC(gc));
291 /* Python's cyclic gc should never see an incoming refcount
292 * of 0: if something decref'ed to 0, it should have been
293 * deallocated immediately at that time.
294 * Possible cause (if the assert triggers): a tp_dealloc
295 * routine left a gc-aware object tracked during its teardown
296 * phase, and did something-- or allowed something to happen --
297 * that called back into Python. gc can trigger then, and may
298 * see the still-tracked dying object. Before this assert
299 * was added, such mistakes went on to allow gc to try to
300 * delete the object again. In a debug build, that caused
301 * a mysterious segfault, when _Py_ForgetReference tried
302 * to remove the object from the doubly-linked list of all
303 * objects a second time. In a release build, an actual
304 * double deallocation occurred, which leads to corruption
305 * of the allocator's internal bookkeeping pointers. That's
306 * so serious that maybe this should be a release-build
307 * check instead of an assert?
308 */
309 assert(gc->gc.gc_refs != 0);
310 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000311}
312
Tim Peters19b74c72002-07-01 03:52:19 +0000313/* A traversal callback for subtract_refs. */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000314static int
315visit_decref(PyObject *op, void *data)
316{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000317 assert(op != NULL);
318 if (PyObject_IS_GC(op)) {
319 PyGC_Head *gc = AS_GC(op);
320 /* We're only interested in gc_refs for objects in the
321 * generation being collected, which can be recognized
322 * because only they have positive gc_refs.
323 */
324 assert(gc->gc.gc_refs != 0); /* else refcount was too small */
325 if (gc->gc.gc_refs > 0)
326 gc->gc.gc_refs--;
327 }
328 return 0;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000329}
330
Tim Peters19b74c72002-07-01 03:52:19 +0000331/* Subtract internal references from gc_refs. After this, gc_refs is >= 0
332 * for all objects in containers, and is GC_REACHABLE for all tracked gc
333 * objects not in containers. The ones with gc_refs > 0 are directly
334 * reachable from outside containers, and so can't be collected.
335 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000336static void
337subtract_refs(PyGC_Head *containers)
338{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339 traverseproc traverse;
340 PyGC_Head *gc = containers->gc.gc_next;
341 for (; gc != containers; gc=gc->gc.gc_next) {
342 traverse = Py_TYPE(FROM_GC(gc))->tp_traverse;
343 (void) traverse(FROM_GC(gc),
344 (visitproc)visit_decref,
345 NULL);
346 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000347}
348
Tim Peters19b74c72002-07-01 03:52:19 +0000349/* A traversal callback for move_unreachable. */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000350static int
Tim Peters19b74c72002-07-01 03:52:19 +0000351visit_reachable(PyObject *op, PyGC_Head *reachable)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000352{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 if (PyObject_IS_GC(op)) {
354 PyGC_Head *gc = AS_GC(op);
355 const Py_ssize_t gc_refs = gc->gc.gc_refs;
Tim Peters19b74c72002-07-01 03:52:19 +0000356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000357 if (gc_refs == 0) {
358 /* This is in move_unreachable's 'young' list, but
359 * the traversal hasn't yet gotten to it. All
360 * we need to do is tell move_unreachable that it's
361 * reachable.
362 */
363 gc->gc.gc_refs = 1;
364 }
365 else if (gc_refs == GC_TENTATIVELY_UNREACHABLE) {
366 /* This had gc_refs = 0 when move_unreachable got
367 * to it, but turns out it's reachable after all.
368 * Move it back to move_unreachable's 'young' list,
369 * and move_unreachable will eventually get to it
370 * again.
371 */
372 gc_list_move(gc, reachable);
373 gc->gc.gc_refs = 1;
374 }
375 /* Else there's nothing to do.
376 * If gc_refs > 0, it must be in move_unreachable's 'young'
377 * list, and move_unreachable will eventually get to it.
378 * If gc_refs == GC_REACHABLE, it's either in some other
379 * generation so we don't care about it, or move_unreachable
380 * already dealt with it.
381 * If gc_refs == GC_UNTRACKED, it must be ignored.
382 */
383 else {
384 assert(gc_refs > 0
385 || gc_refs == GC_REACHABLE
386 || gc_refs == GC_UNTRACKED);
387 }
388 }
389 return 0;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000390}
391
Tim Peters19b74c72002-07-01 03:52:19 +0000392/* Move the unreachable objects from young to unreachable. After this,
393 * all objects in young have gc_refs = GC_REACHABLE, and all objects in
394 * unreachable have gc_refs = GC_TENTATIVELY_UNREACHABLE. All tracked
395 * gc objects not in young or unreachable still have gc_refs = GC_REACHABLE.
396 * All objects in young after this are directly or indirectly reachable
397 * from outside the original young; and all objects in unreachable are
398 * not.
Tim Peters88396172002-06-30 17:56:40 +0000399 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000400static void
Tim Peters19b74c72002-07-01 03:52:19 +0000401move_unreachable(PyGC_Head *young, PyGC_Head *unreachable)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000402{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000403 PyGC_Head *gc = young->gc.gc_next;
Tim Peters19b74c72002-07-01 03:52:19 +0000404
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000405 /* Invariants: all objects "to the left" of us in young have gc_refs
406 * = GC_REACHABLE, and are indeed reachable (directly or indirectly)
407 * from outside the young list as it was at entry. All other objects
408 * from the original young "to the left" of us are in unreachable now,
409 * and have gc_refs = GC_TENTATIVELY_UNREACHABLE. All objects to the
410 * left of us in 'young' now have been scanned, and no objects here
411 * or to the right have been scanned yet.
412 */
Tim Peters19b74c72002-07-01 03:52:19 +0000413
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 while (gc != young) {
415 PyGC_Head *next;
Tim Peters19b74c72002-07-01 03:52:19 +0000416
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 if (gc->gc.gc_refs) {
418 /* gc is definitely reachable from outside the
419 * original 'young'. Mark it as such, and traverse
420 * its pointers to find any other objects that may
421 * be directly reachable from it. Note that the
422 * call to tp_traverse may append objects to young,
423 * so we have to wait until it returns to determine
424 * the next object to visit.
425 */
426 PyObject *op = FROM_GC(gc);
427 traverseproc traverse = Py_TYPE(op)->tp_traverse;
428 assert(gc->gc.gc_refs > 0);
429 gc->gc.gc_refs = GC_REACHABLE;
430 (void) traverse(op,
431 (visitproc)visit_reachable,
432 (void *)young);
433 next = gc->gc.gc_next;
434 if (PyTuple_CheckExact(op)) {
435 _PyTuple_MaybeUntrack(op);
436 }
437 else if (PyDict_CheckExact(op)) {
438 _PyDict_MaybeUntrack(op);
439 }
440 }
441 else {
442 /* This *may* be unreachable. To make progress,
443 * assume it is. gc isn't directly reachable from
444 * any object we've already traversed, but may be
445 * reachable from an object we haven't gotten to yet.
446 * visit_reachable will eventually move gc back into
447 * young if that's so, and we'll see it again.
448 */
449 next = gc->gc.gc_next;
450 gc_list_move(gc, unreachable);
451 gc->gc.gc_refs = GC_TENTATIVELY_UNREACHABLE;
452 }
453 gc = next;
454 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000455}
456
Amaury Forgeot d'Arcad8dcd52007-12-10 23:58:35 +0000457/* Return true if object has a finalization method. */
Neil Schemenauera765c122001-11-01 17:35:23 +0000458static int
459has_finalizer(PyObject *op)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000460{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 if (PyGen_CheckExact(op))
462 return PyGen_NeedsFinalizing((PyGenObject *)op);
463 else
464 return op->ob_type->tp_del != NULL;
Neil Schemenauera765c122001-11-01 17:35:23 +0000465}
466
Tim Petersead8b7a2004-10-30 23:09:22 +0000467/* Move the objects in unreachable with __del__ methods into `finalizers`.
468 * Objects moved into `finalizers` have gc_refs set to GC_REACHABLE; the
469 * objects remaining in unreachable are left at GC_TENTATIVELY_UNREACHABLE.
Jeremy Hyltonce136e92003-04-04 19:59:06 +0000470 */
Neil Schemenauera765c122001-11-01 17:35:23 +0000471static void
Tim Petersead8b7a2004-10-30 23:09:22 +0000472move_finalizers(PyGC_Head *unreachable, PyGC_Head *finalizers)
Neil Schemenauera765c122001-11-01 17:35:23 +0000473{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474 PyGC_Head *gc;
475 PyGC_Head *next;
Tim Petersf6b80452003-04-07 19:21:15 +0000476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000477 /* March over unreachable. Move objects with finalizers into
478 * `finalizers`.
479 */
480 for (gc = unreachable->gc.gc_next; gc != unreachable; gc = next) {
481 PyObject *op = FROM_GC(gc);
Jeremy Hyltonce136e92003-04-04 19:59:06 +0000482
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483 assert(IS_TENTATIVELY_UNREACHABLE(op));
484 next = gc->gc.gc_next;
Tim Petersf6ae7a42003-04-05 18:40:50 +0000485
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 if (has_finalizer(op)) {
487 gc_list_move(gc, finalizers);
488 gc->gc.gc_refs = GC_REACHABLE;
489 }
490 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000491}
492
Tim Peters19b74c72002-07-01 03:52:19 +0000493/* A traversal callback for move_finalizer_reachable. */
494static int
495visit_move(PyObject *op, PyGC_Head *tolist)
496{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 if (PyObject_IS_GC(op)) {
498 if (IS_TENTATIVELY_UNREACHABLE(op)) {
499 PyGC_Head *gc = AS_GC(op);
500 gc_list_move(gc, tolist);
501 gc->gc.gc_refs = GC_REACHABLE;
502 }
503 }
504 return 0;
Tim Peters19b74c72002-07-01 03:52:19 +0000505}
506
507/* Move objects that are reachable from finalizers, from the unreachable set
Tim Petersf6b80452003-04-07 19:21:15 +0000508 * into finalizers set.
Tim Peters19b74c72002-07-01 03:52:19 +0000509 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000510static void
Tim Petersf6b80452003-04-07 19:21:15 +0000511move_finalizer_reachable(PyGC_Head *finalizers)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000512{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 traverseproc traverse;
514 PyGC_Head *gc = finalizers->gc.gc_next;
515 for (; gc != finalizers; gc = gc->gc.gc_next) {
516 /* Note that the finalizers list may grow during this. */
517 traverse = Py_TYPE(FROM_GC(gc))->tp_traverse;
518 (void) traverse(FROM_GC(gc),
519 (visitproc)visit_move,
520 (void *)finalizers);
521 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000522}
523
Tim Petersead8b7a2004-10-30 23:09:22 +0000524/* Clear all weakrefs to unreachable objects, and if such a weakref has a
525 * callback, invoke it if necessary. Note that it's possible for such
526 * weakrefs to be outside the unreachable set -- indeed, those are precisely
527 * the weakrefs whose callbacks must be invoked. See gc_weakref.txt for
528 * overview & some details. Some weakrefs with callbacks may be reclaimed
529 * directly by this routine; the number reclaimed is the return value. Other
530 * weakrefs with callbacks may be moved into the `old` generation. Objects
531 * moved into `old` have gc_refs set to GC_REACHABLE; the objects remaining in
532 * unreachable are left at GC_TENTATIVELY_UNREACHABLE. When this returns,
533 * no object in `unreachable` is weakly referenced anymore.
Tim Peters403a2032003-11-20 21:21:46 +0000534 */
535static int
Tim Petersead8b7a2004-10-30 23:09:22 +0000536handle_weakrefs(PyGC_Head *unreachable, PyGC_Head *old)
Tim Peters403a2032003-11-20 21:21:46 +0000537{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 PyGC_Head *gc;
539 PyObject *op; /* generally FROM_GC(gc) */
540 PyWeakReference *wr; /* generally a cast of op */
541 PyGC_Head wrcb_to_call; /* weakrefs with callbacks to call */
542 PyGC_Head *next;
543 int num_freed = 0;
Tim Peters403a2032003-11-20 21:21:46 +0000544
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 gc_list_init(&wrcb_to_call);
Tim Peters403a2032003-11-20 21:21:46 +0000546
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000547 /* Clear all weakrefs to the objects in unreachable. If such a weakref
548 * also has a callback, move it into `wrcb_to_call` if the callback
549 * needs to be invoked. Note that we cannot invoke any callbacks until
550 * all weakrefs to unreachable objects are cleared, lest the callback
551 * resurrect an unreachable object via a still-active weakref. We
552 * make another pass over wrcb_to_call, invoking callbacks, after this
553 * pass completes.
554 */
555 for (gc = unreachable->gc.gc_next; gc != unreachable; gc = next) {
556 PyWeakReference **wrlist;
Tim Petersead8b7a2004-10-30 23:09:22 +0000557
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000558 op = FROM_GC(gc);
559 assert(IS_TENTATIVELY_UNREACHABLE(op));
560 next = gc->gc.gc_next;
Tim Petersead8b7a2004-10-30 23:09:22 +0000561
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 if (! PyType_SUPPORTS_WEAKREFS(Py_TYPE(op)))
563 continue;
Tim Petersead8b7a2004-10-30 23:09:22 +0000564
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 /* It supports weakrefs. Does it have any? */
566 wrlist = (PyWeakReference **)
567 PyObject_GET_WEAKREFS_LISTPTR(op);
Tim Petersead8b7a2004-10-30 23:09:22 +0000568
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 /* `op` may have some weakrefs. March over the list, clear
570 * all the weakrefs, and move the weakrefs with callbacks
571 * that must be called into wrcb_to_call.
572 */
573 for (wr = *wrlist; wr != NULL; wr = *wrlist) {
574 PyGC_Head *wrasgc; /* AS_GC(wr) */
Tim Petersead8b7a2004-10-30 23:09:22 +0000575
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 /* _PyWeakref_ClearRef clears the weakref but leaves
577 * the callback pointer intact. Obscure: it also
578 * changes *wrlist.
579 */
580 assert(wr->wr_object == op);
581 _PyWeakref_ClearRef(wr);
582 assert(wr->wr_object == Py_None);
583 if (wr->wr_callback == NULL)
584 continue; /* no callback */
Tim Petersead8b7a2004-10-30 23:09:22 +0000585
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000586 /* Headache time. `op` is going away, and is weakly referenced by
587 * `wr`, which has a callback. Should the callback be invoked? If wr
588 * is also trash, no:
589 *
590 * 1. There's no need to call it. The object and the weakref are
591 * both going away, so it's legitimate to pretend the weakref is
592 * going away first. The user has to ensure a weakref outlives its
593 * referent if they want a guarantee that the wr callback will get
594 * invoked.
595 *
596 * 2. It may be catastrophic to call it. If the callback is also in
597 * cyclic trash (CT), then although the CT is unreachable from
598 * outside the current generation, CT may be reachable from the
599 * callback. Then the callback could resurrect insane objects.
600 *
601 * Since the callback is never needed and may be unsafe in this case,
602 * wr is simply left in the unreachable set. Note that because we
603 * already called _PyWeakref_ClearRef(wr), its callback will never
604 * trigger.
605 *
606 * OTOH, if wr isn't part of CT, we should invoke the callback: the
607 * weakref outlived the trash. Note that since wr isn't CT in this
608 * case, its callback can't be CT either -- wr acted as an external
609 * root to this generation, and therefore its callback did too. So
610 * nothing in CT is reachable from the callback either, so it's hard
611 * to imagine how calling it later could create a problem for us. wr
612 * is moved to wrcb_to_call in this case.
613 */
614 if (IS_TENTATIVELY_UNREACHABLE(wr))
615 continue;
616 assert(IS_REACHABLE(wr));
Tim Peterscc2a8662004-10-31 22:12:43 +0000617
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 /* Create a new reference so that wr can't go away
619 * before we can process it again.
620 */
621 Py_INCREF(wr);
Tim Petersead8b7a2004-10-30 23:09:22 +0000622
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000623 /* Move wr to wrcb_to_call, for the next pass. */
624 wrasgc = AS_GC(wr);
625 assert(wrasgc != next); /* wrasgc is reachable, but
626 next isn't, so they can't
627 be the same */
628 gc_list_move(wrasgc, &wrcb_to_call);
629 }
630 }
Tim Petersead8b7a2004-10-30 23:09:22 +0000631
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 /* Invoke the callbacks we decided to honor. It's safe to invoke them
633 * because they can't reference unreachable objects.
634 */
635 while (! gc_list_is_empty(&wrcb_to_call)) {
636 PyObject *temp;
637 PyObject *callback;
Tim Petersead8b7a2004-10-30 23:09:22 +0000638
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000639 gc = wrcb_to_call.gc.gc_next;
640 op = FROM_GC(gc);
641 assert(IS_REACHABLE(op));
642 assert(PyWeakref_Check(op));
643 wr = (PyWeakReference *)op;
644 callback = wr->wr_callback;
645 assert(callback != NULL);
Tim Petersead8b7a2004-10-30 23:09:22 +0000646
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000647 /* copy-paste of weakrefobject.c's handle_callback() */
648 temp = PyObject_CallFunctionObjArgs(callback, wr, NULL);
649 if (temp == NULL)
650 PyErr_WriteUnraisable(callback);
651 else
652 Py_DECREF(temp);
Tim Petersead8b7a2004-10-30 23:09:22 +0000653
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000654 /* Give up the reference we created in the first pass. When
655 * op's refcount hits 0 (which it may or may not do right now),
656 * op's tp_dealloc will decref op->wr_callback too. Note
657 * that the refcount probably will hit 0 now, and because this
658 * weakref was reachable to begin with, gc didn't already
659 * add it to its count of freed objects. Example: a reachable
660 * weak value dict maps some key to this reachable weakref.
661 * The callback removes this key->weakref mapping from the
662 * dict, leaving no other references to the weakref (excepting
663 * ours).
664 */
665 Py_DECREF(op);
666 if (wrcb_to_call.gc.gc_next == gc) {
667 /* object is still alive -- move it */
668 gc_list_move(gc, old);
669 }
670 else
671 ++num_freed;
672 }
Tim Petersead8b7a2004-10-30 23:09:22 +0000673
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000674 return num_freed;
Tim Peters403a2032003-11-20 21:21:46 +0000675}
676
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000677static void
Jeremy Hylton06257772000-08-31 15:10:24 +0000678debug_cycle(char *msg, PyObject *op)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000679{
Victor Stinner499dfcf2011-03-21 13:26:24 +0100680 PySys_FormatStderr("gc: %s <%s %p>\n",
681 msg, Py_TYPE(op)->tp_name, op);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000682}
683
Tim Petersbf384c22003-04-06 00:11:39 +0000684/* Handle uncollectable garbage (cycles with finalizers, and stuff reachable
685 * only from such cycles).
Tim Petersf6b80452003-04-07 19:21:15 +0000686 * If DEBUG_SAVEALL, all objects in finalizers are appended to the module
687 * garbage list (a Python list), else only the objects in finalizers with
688 * __del__ methods are appended to garbage. All objects in finalizers are
689 * merged into the old list regardless.
Tim Peters259272b2003-04-06 19:41:39 +0000690 * Returns 0 if all OK, <0 on error (out of memory to grow the garbage list).
691 * The finalizers list is made empty on a successful return.
Tim Petersbf384c22003-04-06 00:11:39 +0000692 */
Tim Peters259272b2003-04-06 19:41:39 +0000693static int
Tim Petersf6b80452003-04-07 19:21:15 +0000694handle_finalizers(PyGC_Head *finalizers, PyGC_Head *old)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000695{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 PyGC_Head *gc = finalizers->gc.gc_next;
Tim Petersf6b80452003-04-07 19:21:15 +0000697
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 if (garbage == NULL) {
699 garbage = PyList_New(0);
700 if (garbage == NULL)
701 Py_FatalError("gc couldn't create gc.garbage list");
702 }
703 for (; gc != finalizers; gc = gc->gc.gc_next) {
704 PyObject *op = FROM_GC(gc);
Tim Petersf6b80452003-04-07 19:21:15 +0000705
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 if ((debug & DEBUG_SAVEALL) || has_finalizer(op)) {
707 if (PyList_Append(garbage, op) < 0)
708 return -1;
709 }
710 }
Tim Petersf6b80452003-04-07 19:21:15 +0000711
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000712 gc_list_merge(finalizers, old);
713 return 0;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000714}
715
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716/* Break reference cycles by clearing the containers involved. This is
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000717 * tricky business as the lists can be changing and we don't know which
Tim Peters19b74c72002-07-01 03:52:19 +0000718 * objects may be freed. It is possible I screwed something up here.
719 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000720static void
Jeremy Hyltonce136e92003-04-04 19:59:06 +0000721delete_garbage(PyGC_Head *collectable, PyGC_Head *old)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000722{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000723 inquiry clear;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000724
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000725 while (!gc_list_is_empty(collectable)) {
726 PyGC_Head *gc = collectable->gc.gc_next;
727 PyObject *op = FROM_GC(gc);
Tim Peters88396172002-06-30 17:56:40 +0000728
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000729 assert(IS_TENTATIVELY_UNREACHABLE(op));
730 if (debug & DEBUG_SAVEALL) {
731 PyList_Append(garbage, op);
732 }
733 else {
734 if ((clear = Py_TYPE(op)->tp_clear) != NULL) {
735 Py_INCREF(op);
736 clear(op);
737 Py_DECREF(op);
738 }
739 }
740 if (collectable->gc.gc_next == gc) {
741 /* object is still alive, move it, it may die later */
742 gc_list_move(gc, old);
743 gc->gc.gc_refs = GC_REACHABLE;
744 }
745 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000746}
747
Christian Heimesa156e092008-02-16 07:38:31 +0000748/* Clear all free lists
749 * All free lists are cleared during the collection of the highest generation.
750 * Allocated items in the free list may keep a pymalloc arena occupied.
751 * Clearing the free lists may give back memory to the OS earlier.
752 */
753static void
754clear_freelists(void)
755{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000756 (void)PyMethod_ClearFreeList();
757 (void)PyFrame_ClearFreeList();
758 (void)PyCFunction_ClearFreeList();
759 (void)PyTuple_ClearFreeList();
760 (void)PyUnicode_ClearFreeList();
761 (void)PyFloat_ClearFreeList();
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100762 (void)PyList_ClearFreeList();
763 (void)PyDict_ClearFreeList();
Antoine Pitrou093ce9c2011-12-16 11:24:27 +0100764 (void)PySet_ClearFreeList();
Christian Heimesa156e092008-02-16 07:38:31 +0000765}
766
Antoine Pitrou621601a2008-12-17 23:18:19 +0000767static double
768get_time(void)
769{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770 double result = 0;
771 if (tmod != NULL) {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200772 _Py_IDENTIFIER(time);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200773
774 PyObject *f = _PyObject_CallMethodId(tmod, &PyId_time, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775 if (f == NULL) {
776 PyErr_Clear();
777 }
778 else {
779 if (PyFloat_Check(f))
780 result = PyFloat_AsDouble(f);
781 Py_DECREF(f);
782 }
783 }
784 return result;
Antoine Pitrou621601a2008-12-17 23:18:19 +0000785}
786
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000787/* This is the main function. Read this to understand how the
788 * collection process works. */
Neal Norwitz7b216c52006-03-04 20:01:53 +0000789static Py_ssize_t
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000790collect(int generation)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000791{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 int i;
793 Py_ssize_t m = 0; /* # objects collected */
794 Py_ssize_t n = 0; /* # unreachable objects that couldn't be collected */
795 PyGC_Head *young; /* the generation we are examining */
796 PyGC_Head *old; /* next older generation */
797 PyGC_Head unreachable; /* non-problematic unreachable trash */
798 PyGC_Head finalizers; /* objects with, & reachable from, __del__ */
799 PyGC_Head *gc;
800 double t1 = 0.0;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000801
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000802 if (debug & DEBUG_STATS) {
803 PySys_WriteStderr("gc: collecting generation %d...\n",
804 generation);
805 PySys_WriteStderr("gc: objects in each generation:");
806 for (i = 0; i < NUM_GENERATIONS; i++)
807 PySys_WriteStderr(" %" PY_FORMAT_SIZE_T "d",
808 gc_list_size(GEN_HEAD(i)));
809 t1 = get_time();
810 PySys_WriteStderr("\n");
811 }
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000812
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 /* update collection and allocation counters */
814 if (generation+1 < NUM_GENERATIONS)
815 generations[generation+1].count += 1;
816 for (i = 0; i <= generation; i++)
817 generations[i].count = 0;
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000818
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000819 /* merge younger generations with one we are currently collecting */
820 for (i = 0; i < generation; i++) {
821 gc_list_merge(GEN_HEAD(i), GEN_HEAD(generation));
822 }
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000823
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000824 /* handy references */
825 young = GEN_HEAD(generation);
826 if (generation < NUM_GENERATIONS-1)
827 old = GEN_HEAD(generation+1);
828 else
829 old = young;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000830
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000831 /* Using ob_refcnt and gc_refs, calculate which objects in the
832 * container set are reachable from outside the set (i.e., have a
833 * refcount greater than 0 when all the references within the
834 * set are taken into account).
835 */
836 update_refs(young);
837 subtract_refs(young);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000838
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000839 /* Leave everything reachable from outside young in young, and move
840 * everything else (in young) to unreachable.
841 * NOTE: This used to move the reachable objects into a reachable
842 * set instead. But most things usually turn out to be reachable,
843 * so it's more efficient to move the unreachable things.
844 */
845 gc_list_init(&unreachable);
846 move_unreachable(young, &unreachable);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000847
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848 /* Move reachable objects to next generation. */
849 if (young != old) {
850 if (generation == NUM_GENERATIONS - 2) {
851 long_lived_pending += gc_list_size(young);
852 }
853 gc_list_merge(young, old);
854 }
855 else {
856 long_lived_pending = 0;
857 long_lived_total = gc_list_size(young);
858 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000859
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 /* All objects in unreachable are trash, but objects reachable from
861 * finalizers can't safely be deleted. Python programmers should take
862 * care not to create such things. For Python, finalizers means
863 * instance objects with __del__ methods. Weakrefs with callbacks
864 * can also call arbitrary Python code but they will be dealt with by
865 * handle_weakrefs().
866 */
867 gc_list_init(&finalizers);
868 move_finalizers(&unreachable, &finalizers);
869 /* finalizers contains the unreachable objects with a finalizer;
870 * unreachable objects reachable *from* those are also uncollectable,
871 * and we move those into the finalizers list too.
872 */
873 move_finalizer_reachable(&finalizers);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000874
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 /* Collect statistics on collectable objects found and print
876 * debugging information.
877 */
878 for (gc = unreachable.gc.gc_next; gc != &unreachable;
879 gc = gc->gc.gc_next) {
880 m++;
881 if (debug & DEBUG_COLLECTABLE) {
882 debug_cycle("collectable", FROM_GC(gc));
883 }
884 }
Tim Petersead8b7a2004-10-30 23:09:22 +0000885
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 /* Clear weakrefs and invoke callbacks as necessary. */
887 m += handle_weakrefs(&unreachable, old);
Tim Petersead8b7a2004-10-30 23:09:22 +0000888
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000889 /* Call tp_clear on objects in the unreachable set. This will cause
890 * the reference cycles to be broken. It may also cause some objects
891 * in finalizers to be freed.
892 */
893 delete_garbage(&unreachable, old);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000894
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 /* Collect statistics on uncollectable objects found and print
896 * debugging information. */
897 for (gc = finalizers.gc.gc_next;
898 gc != &finalizers;
899 gc = gc->gc.gc_next) {
900 n++;
901 if (debug & DEBUG_UNCOLLECTABLE)
902 debug_cycle("uncollectable", FROM_GC(gc));
903 }
904 if (debug & DEBUG_STATS) {
905 double t2 = get_time();
906 if (m == 0 && n == 0)
907 PySys_WriteStderr("gc: done");
908 else
909 PySys_WriteStderr(
910 "gc: done, "
911 "%" PY_FORMAT_SIZE_T "d unreachable, "
912 "%" PY_FORMAT_SIZE_T "d uncollectable",
913 n+m, n);
914 if (t1 && t2) {
915 PySys_WriteStderr(", %.4fs elapsed", t2-t1);
916 }
917 PySys_WriteStderr(".\n");
918 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000919
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 /* Append instances in the uncollectable set to a Python
921 * reachable list of garbage. The programmer has to deal with
922 * this if they insist on creating this type of structure.
923 */
924 (void)handle_finalizers(&finalizers, old);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000925
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 /* Clear free list only during the collection of the highest
927 * generation */
928 if (generation == NUM_GENERATIONS-1) {
929 clear_freelists();
930 }
Christian Heimesa156e092008-02-16 07:38:31 +0000931
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 if (PyErr_Occurred()) {
933 if (gc_str == NULL)
934 gc_str = PyUnicode_FromString("garbage collection");
935 PyErr_WriteUnraisable(gc_str);
936 Py_FatalError("unexpected exception during garbage collection");
937 }
938 return n+m;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000939}
940
Neal Norwitz7b216c52006-03-04 20:01:53 +0000941static Py_ssize_t
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000942collect_generations(void)
943{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 int i;
945 Py_ssize_t n = 0;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000946
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 /* Find the oldest generation (highest numbered) where the count
948 * exceeds the threshold. Objects in the that generation and
949 * generations younger than it will be collected. */
950 for (i = NUM_GENERATIONS-1; i >= 0; i--) {
951 if (generations[i].count > generations[i].threshold) {
952 /* Avoid quadratic performance degradation in number
953 of tracked objects. See comments at the beginning
954 of this file, and issue #4074.
955 */
956 if (i == NUM_GENERATIONS - 1
957 && long_lived_pending < long_lived_total / 4)
958 continue;
959 n = collect(i);
960 break;
961 }
962 }
963 return n;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000964}
965
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000966PyDoc_STRVAR(gc_enable__doc__,
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000967"enable() -> None\n"
968"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000969"Enable automatic garbage collection.\n");
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000970
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000971static PyObject *
Tim Peters50c61d52003-04-06 01:50:50 +0000972gc_enable(PyObject *self, PyObject *noargs)
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000973{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 enabled = 1;
975 Py_INCREF(Py_None);
976 return Py_None;
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000977}
978
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000979PyDoc_STRVAR(gc_disable__doc__,
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000980"disable() -> None\n"
981"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000982"Disable automatic garbage collection.\n");
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000983
984static PyObject *
Tim Peters50c61d52003-04-06 01:50:50 +0000985gc_disable(PyObject *self, PyObject *noargs)
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000986{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 enabled = 0;
988 Py_INCREF(Py_None);
989 return Py_None;
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000990}
991
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000992PyDoc_STRVAR(gc_isenabled__doc__,
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000993"isenabled() -> status\n"
994"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000995"Returns true if automatic garbage collection is enabled.\n");
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000996
997static PyObject *
Tim Peters50c61d52003-04-06 01:50:50 +0000998gc_isenabled(PyObject *self, PyObject *noargs)
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000999{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001000 return PyBool_FromLong((long)enabled);
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001001}
1002
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001003PyDoc_STRVAR(gc_collect__doc__,
Barry Warsawd3c38ff2006-03-07 09:46:03 +00001004"collect([generation]) -> n\n"
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001005"\n"
Barry Warsawd3c38ff2006-03-07 09:46:03 +00001006"With no arguments, run a full collection. The optional argument\n"
1007"may be an integer specifying which generation to collect. A ValueError\n"
1008"is raised if the generation number is invalid.\n\n"
1009"The number of unreachable objects is returned.\n");
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001010
1011static PyObject *
Barry Warsawd3c38ff2006-03-07 09:46:03 +00001012gc_collect(PyObject *self, PyObject *args, PyObject *kws)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001013{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014 static char *keywords[] = {"generation", NULL};
1015 int genarg = NUM_GENERATIONS - 1;
1016 Py_ssize_t n;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001017
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 if (!PyArg_ParseTupleAndKeywords(args, kws, "|i", keywords, &genarg))
1019 return NULL;
Barry Warsawd3c38ff2006-03-07 09:46:03 +00001020
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021 else if (genarg < 0 || genarg >= NUM_GENERATIONS) {
1022 PyErr_SetString(PyExc_ValueError, "invalid generation");
1023 return NULL;
1024 }
Barry Warsawd3c38ff2006-03-07 09:46:03 +00001025
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 if (collecting)
1027 n = 0; /* already collecting, don't do anything */
1028 else {
1029 collecting = 1;
1030 n = collect(genarg);
1031 collecting = 0;
1032 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001033
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034 return PyLong_FromSsize_t(n);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001035}
1036
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001037PyDoc_STRVAR(gc_set_debug__doc__,
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001038"set_debug(flags) -> None\n"
1039"\n"
1040"Set the garbage collection debugging flags. Debugging information is\n"
1041"written to sys.stderr.\n"
1042"\n"
1043"flags is an integer and can have the following bits turned on:\n"
1044"\n"
1045" DEBUG_STATS - Print statistics during collection.\n"
1046" DEBUG_COLLECTABLE - Print collectable objects found.\n"
1047" DEBUG_UNCOLLECTABLE - Print unreachable but uncollectable objects found.\n"
Neil Schemenauer544de1e2000-09-22 15:22:38 +00001048" DEBUG_SAVEALL - Save objects to gc.garbage rather than freeing them.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001049" DEBUG_LEAK - Debug leaking programs (everything but STATS).\n");
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001050
1051static PyObject *
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001052gc_set_debug(PyObject *self, PyObject *args)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001053{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 if (!PyArg_ParseTuple(args, "i:set_debug", &debug))
1055 return NULL;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001056
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001057 Py_INCREF(Py_None);
1058 return Py_None;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001059}
1060
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001061PyDoc_STRVAR(gc_get_debug__doc__,
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001062"get_debug() -> flags\n"
1063"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001064"Get the garbage collection debugging flags.\n");
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001065
1066static PyObject *
Tim Peters50c61d52003-04-06 01:50:50 +00001067gc_get_debug(PyObject *self, PyObject *noargs)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001068{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001069 return Py_BuildValue("i", debug);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001070}
1071
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001072PyDoc_STRVAR(gc_set_thresh__doc__,
Neal Norwitz2a47c0f2002-01-29 00:53:41 +00001073"set_threshold(threshold0, [threshold1, threshold2]) -> None\n"
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001074"\n"
1075"Sets the collection thresholds. Setting threshold0 to zero disables\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001076"collection.\n");
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001077
1078static PyObject *
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001079gc_set_thresh(PyObject *self, PyObject *args)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001080{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001081 int i;
1082 if (!PyArg_ParseTuple(args, "i|ii:set_threshold",
1083 &generations[0].threshold,
1084 &generations[1].threshold,
1085 &generations[2].threshold))
1086 return NULL;
1087 for (i = 2; i < NUM_GENERATIONS; i++) {
1088 /* generations higher than 2 get the same threshold */
1089 generations[i].threshold = generations[2].threshold;
1090 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001091
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001092 Py_INCREF(Py_None);
1093 return Py_None;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001094}
1095
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001096PyDoc_STRVAR(gc_get_thresh__doc__,
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001097"get_threshold() -> (threshold0, threshold1, threshold2)\n"
1098"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001099"Return the current collection thresholds\n");
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001100
1101static PyObject *
Tim Peters50c61d52003-04-06 01:50:50 +00001102gc_get_thresh(PyObject *self, PyObject *noargs)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001103{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001104 return Py_BuildValue("(iii)",
1105 generations[0].threshold,
1106 generations[1].threshold,
1107 generations[2].threshold);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001108}
1109
Barry Warsawd3c38ff2006-03-07 09:46:03 +00001110PyDoc_STRVAR(gc_get_count__doc__,
1111"get_count() -> (count0, count1, count2)\n"
1112"\n"
1113"Return the current collection counts\n");
1114
1115static PyObject *
1116gc_get_count(PyObject *self, PyObject *noargs)
1117{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001118 return Py_BuildValue("(iii)",
1119 generations[0].count,
1120 generations[1].count,
1121 generations[2].count);
Barry Warsawd3c38ff2006-03-07 09:46:03 +00001122}
1123
Neil Schemenauer48c70342001-08-09 15:38:31 +00001124static int
Martin v. Löwis560da622001-11-24 09:24:51 +00001125referrersvisit(PyObject* obj, PyObject *objs)
Neil Schemenauer48c70342001-08-09 15:38:31 +00001126{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001127 Py_ssize_t i;
1128 for (i = 0; i < PyTuple_GET_SIZE(objs); i++)
1129 if (PyTuple_GET_ITEM(objs, i) == obj)
1130 return 1;
1131 return 0;
Neil Schemenauer48c70342001-08-09 15:38:31 +00001132}
1133
Neil Schemenauer17e7be62001-08-10 14:46:47 +00001134static int
Martin v. Löwis560da622001-11-24 09:24:51 +00001135gc_referrers_for(PyObject *objs, PyGC_Head *list, PyObject *resultlist)
Neil Schemenauer48c70342001-08-09 15:38:31 +00001136{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001137 PyGC_Head *gc;
1138 PyObject *obj;
1139 traverseproc traverse;
1140 for (gc = list->gc.gc_next; gc != list; gc = gc->gc.gc_next) {
1141 obj = FROM_GC(gc);
1142 traverse = Py_TYPE(obj)->tp_traverse;
1143 if (obj == objs || obj == resultlist)
1144 continue;
1145 if (traverse(obj, (visitproc)referrersvisit, objs)) {
1146 if (PyList_Append(resultlist, obj) < 0)
1147 return 0; /* error */
1148 }
1149 }
1150 return 1; /* no error */
Neil Schemenauer48c70342001-08-09 15:38:31 +00001151}
1152
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001153PyDoc_STRVAR(gc_get_referrers__doc__,
Martin v. Löwis560da622001-11-24 09:24:51 +00001154"get_referrers(*objs) -> list\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001155Return the list of objects that directly refer to any of objs.");
Neil Schemenauer48c70342001-08-09 15:38:31 +00001156
Neil Schemenauer17e7be62001-08-10 14:46:47 +00001157static PyObject *
Martin v. Löwis560da622001-11-24 09:24:51 +00001158gc_get_referrers(PyObject *self, PyObject *args)
Neil Schemenauer48c70342001-08-09 15:38:31 +00001159{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001160 int i;
1161 PyObject *result = PyList_New(0);
1162 if (!result) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001163
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001164 for (i = 0; i < NUM_GENERATIONS; i++) {
1165 if (!(gc_referrers_for(args, GEN_HEAD(i), result))) {
1166 Py_DECREF(result);
1167 return NULL;
1168 }
1169 }
1170 return result;
Neil Schemenauer48c70342001-08-09 15:38:31 +00001171}
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001172
Tim Peters0f81ab62003-04-08 16:39:48 +00001173/* Append obj to list; return true if error (out of memory), false if OK. */
Jeremy Hylton5bd378b2003-04-03 16:28:38 +00001174static int
Tim Peters730f5532003-04-08 17:17:17 +00001175referentsvisit(PyObject *obj, PyObject *list)
Jeremy Hylton5bd378b2003-04-03 16:28:38 +00001176{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001177 return PyList_Append(list, obj) < 0;
Jeremy Hylton5bd378b2003-04-03 16:28:38 +00001178}
1179
Tim Peters730f5532003-04-08 17:17:17 +00001180PyDoc_STRVAR(gc_get_referents__doc__,
1181"get_referents(*objs) -> list\n\
Jeremy Hylton059b0942003-04-03 16:29:13 +00001182Return the list of objects that are directly referred to by objs.");
Jeremy Hylton5bd378b2003-04-03 16:28:38 +00001183
1184static PyObject *
Tim Peters730f5532003-04-08 17:17:17 +00001185gc_get_referents(PyObject *self, PyObject *args)
Jeremy Hylton5bd378b2003-04-03 16:28:38 +00001186{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 Py_ssize_t i;
1188 PyObject *result = PyList_New(0);
Tim Peters0f81ab62003-04-08 16:39:48 +00001189
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 if (result == NULL)
1191 return NULL;
Tim Peters0f81ab62003-04-08 16:39:48 +00001192
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001193 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
1194 traverseproc traverse;
1195 PyObject *obj = PyTuple_GET_ITEM(args, i);
Tim Peters0f81ab62003-04-08 16:39:48 +00001196
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001197 if (! PyObject_IS_GC(obj))
1198 continue;
1199 traverse = Py_TYPE(obj)->tp_traverse;
1200 if (! traverse)
1201 continue;
1202 if (traverse(obj, (visitproc)referentsvisit, result)) {
1203 Py_DECREF(result);
1204 return NULL;
1205 }
1206 }
1207 return result;
Jeremy Hylton5bd378b2003-04-03 16:28:38 +00001208}
1209
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001210PyDoc_STRVAR(gc_get_objects__doc__,
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +00001211"get_objects() -> [...]\n"
1212"\n"
1213"Return a list of objects tracked by the collector (excluding the list\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001214"returned).\n");
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +00001215
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +00001216static PyObject *
Tim Peters50c61d52003-04-06 01:50:50 +00001217gc_get_objects(PyObject *self, PyObject *noargs)
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +00001218{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 int i;
1220 PyObject* result;
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +00001221
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001222 result = PyList_New(0);
1223 if (result == NULL)
1224 return NULL;
1225 for (i = 0; i < NUM_GENERATIONS; i++) {
1226 if (append_objects(result, GEN_HEAD(i))) {
1227 Py_DECREF(result);
1228 return NULL;
1229 }
1230 }
1231 return result;
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +00001232}
1233
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001234PyDoc_STRVAR(gc_is_tracked__doc__,
1235"is_tracked(obj) -> bool\n"
1236"\n"
1237"Returns true if the object is tracked by the garbage collector.\n"
1238"Simple atomic objects will return false.\n"
1239);
1240
1241static PyObject *
1242gc_is_tracked(PyObject *self, PyObject *obj)
1243{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001244 PyObject *result;
1245
1246 if (PyObject_IS_GC(obj) && IS_TRACKED(obj))
1247 result = Py_True;
1248 else
1249 result = Py_False;
1250 Py_INCREF(result);
1251 return result;
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001252}
1253
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +00001254
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001255PyDoc_STRVAR(gc__doc__,
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001256"This module provides access to the garbage collector for reference cycles.\n"
1257"\n"
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001258"enable() -- Enable automatic garbage collection.\n"
1259"disable() -- Disable automatic garbage collection.\n"
1260"isenabled() -- Returns true if automatic collection is enabled.\n"
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001261"collect() -- Do a full collection right now.\n"
Thomas Wouters89f507f2006-12-13 04:49:30 +00001262"get_count() -- Return the current collection counts.\n"
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001263"set_debug() -- Set debugging flags.\n"
1264"get_debug() -- Get debugging flags.\n"
1265"set_threshold() -- Set the collection thresholds.\n"
1266"get_threshold() -- Return the current the collection thresholds.\n"
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +00001267"get_objects() -- Return a list of all objects tracked by the collector.\n"
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001268"is_tracked() -- Returns true if a given object is tracked.\n"
Jeremy Hylton5bd378b2003-04-03 16:28:38 +00001269"get_referrers() -- Return the list of objects that refer to an object.\n"
Tim Peters730f5532003-04-08 17:17:17 +00001270"get_referents() -- Return the list of objects that an object refers to.\n");
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001271
1272static PyMethodDef GcMethods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001273 {"enable", gc_enable, METH_NOARGS, gc_enable__doc__},
1274 {"disable", gc_disable, METH_NOARGS, gc_disable__doc__},
1275 {"isenabled", gc_isenabled, METH_NOARGS, gc_isenabled__doc__},
1276 {"set_debug", gc_set_debug, METH_VARARGS, gc_set_debug__doc__},
1277 {"get_debug", gc_get_debug, METH_NOARGS, gc_get_debug__doc__},
1278 {"get_count", gc_get_count, METH_NOARGS, gc_get_count__doc__},
1279 {"set_threshold", gc_set_thresh, METH_VARARGS, gc_set_thresh__doc__},
1280 {"get_threshold", gc_get_thresh, METH_NOARGS, gc_get_thresh__doc__},
1281 {"collect", (PyCFunction)gc_collect,
1282 METH_VARARGS | METH_KEYWORDS, gc_collect__doc__},
1283 {"get_objects", gc_get_objects,METH_NOARGS, gc_get_objects__doc__},
1284 {"is_tracked", gc_is_tracked, METH_O, gc_is_tracked__doc__},
1285 {"get_referrers", gc_get_referrers, METH_VARARGS,
1286 gc_get_referrers__doc__},
1287 {"get_referents", gc_get_referents, METH_VARARGS,
1288 gc_get_referents__doc__},
1289 {NULL, NULL} /* Sentinel */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001290};
1291
Martin v. Löwis1a214512008-06-11 05:26:20 +00001292static struct PyModuleDef gcmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 PyModuleDef_HEAD_INIT,
Antoine Pitrou696e0352010-08-08 22:18:46 +00001294 "gc", /* m_name */
1295 gc__doc__, /* m_doc */
1296 -1, /* m_size */
1297 GcMethods, /* m_methods */
1298 NULL, /* m_reload */
1299 NULL, /* m_traverse */
1300 NULL, /* m_clear */
1301 NULL /* m_free */
Martin v. Löwis1a214512008-06-11 05:26:20 +00001302};
1303
Jason Tishler6bc06ec2003-09-04 11:59:50 +00001304PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001305PyInit_gc(void)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001306{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001307 PyObject *m;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001309 m = PyModule_Create(&gcmodule);
Martin v. Löwis1a214512008-06-11 05:26:20 +00001310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001311 if (m == NULL)
1312 return NULL;
Tim Peters11558872003-04-06 23:30:52 +00001313
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001314 if (garbage == NULL) {
1315 garbage = PyList_New(0);
1316 if (garbage == NULL)
1317 return NULL;
1318 }
1319 Py_INCREF(garbage);
1320 if (PyModule_AddObject(m, "garbage", garbage) < 0)
1321 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001322
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001323 /* Importing can't be done in collect() because collect()
1324 * can be called via PyGC_Collect() in Py_Finalize().
1325 * This wouldn't be a problem, except that <initialized> is
1326 * reset to 0 before calling collect which trips up
1327 * the import and triggers an assertion.
1328 */
1329 if (tmod == NULL) {
1330 tmod = PyImport_ImportModuleNoBlock("time");
1331 if (tmod == NULL)
1332 PyErr_Clear();
1333 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001334
Martin v. Löwis1a214512008-06-11 05:26:20 +00001335#define ADD_INT(NAME) if (PyModule_AddIntConstant(m, #NAME, NAME) < 0) return NULL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001336 ADD_INT(DEBUG_STATS);
1337 ADD_INT(DEBUG_COLLECTABLE);
1338 ADD_INT(DEBUG_UNCOLLECTABLE);
1339 ADD_INT(DEBUG_SAVEALL);
1340 ADD_INT(DEBUG_LEAK);
Tim Peters11558872003-04-06 23:30:52 +00001341#undef ADD_INT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342 return m;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001343}
1344
Guido van Rossume13ddc92003-04-17 17:29:22 +00001345/* API to invoke gc.collect() from C */
Neal Norwitz7b216c52006-03-04 20:01:53 +00001346Py_ssize_t
Guido van Rossume13ddc92003-04-17 17:29:22 +00001347PyGC_Collect(void)
1348{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001349 Py_ssize_t n;
Guido van Rossume13ddc92003-04-17 17:29:22 +00001350
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001351 if (collecting)
1352 n = 0; /* already collecting, don't do anything */
1353 else {
1354 collecting = 1;
1355 n = collect(NUM_GENERATIONS - 1);
1356 collecting = 0;
1357 }
Guido van Rossume13ddc92003-04-17 17:29:22 +00001358
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001359 return n;
Guido van Rossume13ddc92003-04-17 17:29:22 +00001360}
1361
Antoine Pitrou696e0352010-08-08 22:18:46 +00001362void
1363_PyGC_Fini(void)
1364{
Antoine Pitrou2ed94eb2010-09-14 09:48:39 +00001365 if (!(debug & DEBUG_SAVEALL)
1366 && garbage != NULL && PyList_GET_SIZE(garbage) > 0) {
Georg Brandl08be72d2010-10-24 15:11:22 +00001367 char *message;
1368 if (debug & DEBUG_UNCOLLECTABLE)
Antoine Pitroub5d82042010-11-05 00:05:25 +00001369 message = "gc: %zd uncollectable objects at " \
Georg Brandl08be72d2010-10-24 15:11:22 +00001370 "shutdown";
1371 else
Antoine Pitroub5d82042010-11-05 00:05:25 +00001372 message = "gc: %zd uncollectable objects at " \
Georg Brandl08be72d2010-10-24 15:11:22 +00001373 "shutdown; use gc.set_debug(gc.DEBUG_UNCOLLECTABLE) to list them";
1374 if (PyErr_WarnFormat(PyExc_ResourceWarning, 0, message,
1375 PyList_GET_SIZE(garbage)) < 0)
1376 PyErr_WriteUnraisable(NULL);
Antoine Pitrou696e0352010-08-08 22:18:46 +00001377 if (debug & DEBUG_UNCOLLECTABLE) {
1378 PyObject *repr = NULL, *bytes = NULL;
1379 repr = PyObject_Repr(garbage);
1380 if (!repr || !(bytes = PyUnicode_EncodeFSDefault(repr)))
1381 PyErr_WriteUnraisable(garbage);
1382 else {
1383 PySys_WriteStderr(
1384 " %s\n",
1385 PyBytes_AS_STRING(bytes)
1386 );
1387 }
1388 Py_XDECREF(repr);
1389 Py_XDECREF(bytes);
1390 }
Antoine Pitrou696e0352010-08-08 22:18:46 +00001391 }
1392}
1393
Neil Schemenauer43411b52001-08-30 00:05:51 +00001394/* for debugging */
Guido van Rossume13ddc92003-04-17 17:29:22 +00001395void
1396_PyGC_Dump(PyGC_Head *g)
Neil Schemenauer43411b52001-08-30 00:05:51 +00001397{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001398 _PyObject_Dump(FROM_GC(g));
Neil Schemenauer43411b52001-08-30 00:05:51 +00001399}
1400
Neil Schemenauer43411b52001-08-30 00:05:51 +00001401/* extension modules might be compiled with GC support so these
1402 functions must always be available */
1403
Neil Schemenauerfec4eb12002-04-12 02:41:03 +00001404#undef PyObject_GC_Track
1405#undef PyObject_GC_UnTrack
1406#undef PyObject_GC_Del
1407#undef _PyObject_GC_Malloc
1408
Neil Schemenauer43411b52001-08-30 00:05:51 +00001409void
Neil Schemenauerfec4eb12002-04-12 02:41:03 +00001410PyObject_GC_Track(void *op)
Neil Schemenauer43411b52001-08-30 00:05:51 +00001411{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001412 _PyObject_GC_TRACK(op);
Neil Schemenauer43411b52001-08-30 00:05:51 +00001413}
1414
Neil Schemenauerfec4eb12002-04-12 02:41:03 +00001415/* for binary compatibility with 2.2 */
Neil Schemenauer43411b52001-08-30 00:05:51 +00001416void
Neil Schemenauerfec4eb12002-04-12 02:41:03 +00001417_PyObject_GC_Track(PyObject *op)
1418{
1419 PyObject_GC_Track(op);
1420}
1421
1422void
1423PyObject_GC_UnTrack(void *op)
Neil Schemenauer43411b52001-08-30 00:05:51 +00001424{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001425 /* Obscure: the Py_TRASHCAN mechanism requires that we be able to
1426 * call PyObject_GC_UnTrack twice on an object.
1427 */
1428 if (IS_TRACKED(op))
1429 _PyObject_GC_UNTRACK(op);
Neil Schemenauer43411b52001-08-30 00:05:51 +00001430}
1431
Neil Schemenauerfec4eb12002-04-12 02:41:03 +00001432/* for binary compatibility with 2.2 */
1433void
1434_PyObject_GC_UnTrack(PyObject *op)
1435{
1436 PyObject_GC_UnTrack(op);
1437}
1438
Neil Schemenauer43411b52001-08-30 00:05:51 +00001439PyObject *
Neil Schemenauerfec4eb12002-04-12 02:41:03 +00001440_PyObject_GC_Malloc(size_t basicsize)
Neil Schemenauer43411b52001-08-30 00:05:51 +00001441{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001442 PyObject *op;
1443 PyGC_Head *g;
1444 if (basicsize > PY_SSIZE_T_MAX - sizeof(PyGC_Head))
1445 return PyErr_NoMemory();
1446 g = (PyGC_Head *)PyObject_MALLOC(
1447 sizeof(PyGC_Head) + basicsize);
1448 if (g == NULL)
1449 return PyErr_NoMemory();
1450 g->gc.gc_refs = GC_UNTRACKED;
1451 generations[0].count++; /* number of allocated GC objects */
1452 if (generations[0].count > generations[0].threshold &&
1453 enabled &&
1454 generations[0].threshold &&
1455 !collecting &&
1456 !PyErr_Occurred()) {
1457 collecting = 1;
1458 collect_generations();
1459 collecting = 0;
1460 }
1461 op = FROM_GC(g);
1462 return op;
Neil Schemenauer43411b52001-08-30 00:05:51 +00001463}
1464
1465PyObject *
1466_PyObject_GC_New(PyTypeObject *tp)
1467{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001468 PyObject *op = _PyObject_GC_Malloc(_PyObject_SIZE(tp));
1469 if (op != NULL)
1470 op = PyObject_INIT(op, tp);
1471 return op;
Neil Schemenauer43411b52001-08-30 00:05:51 +00001472}
1473
1474PyVarObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001475_PyObject_GC_NewVar(PyTypeObject *tp, Py_ssize_t nitems)
Neil Schemenauer43411b52001-08-30 00:05:51 +00001476{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001477 const size_t size = _PyObject_VAR_SIZE(tp, nitems);
1478 PyVarObject *op = (PyVarObject *) _PyObject_GC_Malloc(size);
1479 if (op != NULL)
1480 op = PyObject_INIT_VAR(op, tp, nitems);
1481 return op;
Neil Schemenauer43411b52001-08-30 00:05:51 +00001482}
1483
1484PyVarObject *
Martin v. Löwis41290682006-02-16 14:56:14 +00001485_PyObject_GC_Resize(PyVarObject *op, Py_ssize_t nitems)
Neil Schemenauer43411b52001-08-30 00:05:51 +00001486{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001487 const size_t basicsize = _PyObject_VAR_SIZE(Py_TYPE(op), nitems);
1488 PyGC_Head *g = AS_GC(op);
1489 if (basicsize > PY_SSIZE_T_MAX - sizeof(PyGC_Head))
1490 return (PyVarObject *)PyErr_NoMemory();
1491 g = (PyGC_Head *)PyObject_REALLOC(g, sizeof(PyGC_Head) + basicsize);
1492 if (g == NULL)
1493 return (PyVarObject *)PyErr_NoMemory();
1494 op = (PyVarObject *) FROM_GC(g);
1495 Py_SIZE(op) = nitems;
1496 return op;
Neil Schemenauer43411b52001-08-30 00:05:51 +00001497}
1498
1499void
Neil Schemenauerfec4eb12002-04-12 02:41:03 +00001500PyObject_GC_Del(void *op)
Neil Schemenauer43411b52001-08-30 00:05:51 +00001501{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001502 PyGC_Head *g = AS_GC(op);
1503 if (IS_TRACKED(op))
1504 gc_list_remove(g);
1505 if (generations[0].count > 0) {
1506 generations[0].count--;
1507 }
1508 PyObject_FREE(g);
Neil Schemenauer43411b52001-08-30 00:05:51 +00001509}