]> git.proxmox.com Git - mirror_edk2.git/blame - AppPkg/Applications/Python/Python-2.7.10/Modules/gcmodule.c
AppPkg/Applications/Python/Python-2.7.10: Initial Checkin part 2/5.
[mirror_edk2.git] / AppPkg / Applications / Python / Python-2.7.10 / Modules / gcmodule.c
CommitLineData
7eb75bcc
DM
1/*\r
2\r
3 Reference Cycle Garbage Collection\r
4 ==================================\r
5\r
6 Neil Schemenauer <nas@arctrix.com>\r
7\r
8 Based on a post on the python-dev list. Ideas from Guido van Rossum,\r
9 Eric Tiedemann, and various others.\r
10\r
11 http://www.arctrix.com/nas/python/gc/\r
12 http://www.python.org/pipermail/python-dev/2000-March/003869.html\r
13 http://www.python.org/pipermail/python-dev/2000-March/004010.html\r
14 http://www.python.org/pipermail/python-dev/2000-March/004022.html\r
15\r
16 For a highlevel view of the collection process, read the collect\r
17 function.\r
18\r
19*/\r
20\r
21#include "Python.h"\r
22#include "frameobject.h" /* for PyFrame_ClearFreeList */\r
23\r
24/* Get an object's GC head */\r
25#define AS_GC(o) ((PyGC_Head *)(o)-1)\r
26\r
27/* Get the object given the GC head */\r
28#define FROM_GC(g) ((PyObject *)(((PyGC_Head *)g)+1))\r
29\r
30/*** Global GC state ***/\r
31\r
32struct gc_generation {\r
33 PyGC_Head head;\r
34 int threshold; /* collection threshold */\r
35 int count; /* count of allocations or collections of younger\r
36 generations */\r
37};\r
38\r
39#define NUM_GENERATIONS 3\r
40#define GEN_HEAD(n) (&generations[n].head)\r
41\r
42/* linked lists of container objects */\r
43static struct gc_generation generations[NUM_GENERATIONS] = {\r
44 /* PyGC_Head, threshold, count */\r
45 {{{GEN_HEAD(0), GEN_HEAD(0), 0}}, 700, 0},\r
46 {{{GEN_HEAD(1), GEN_HEAD(1), 0}}, 10, 0},\r
47 {{{GEN_HEAD(2), GEN_HEAD(2), 0}}, 10, 0},\r
48};\r
49\r
50PyGC_Head *_PyGC_generation0 = GEN_HEAD(0);\r
51\r
52static int enabled = 1; /* automatic collection enabled? */\r
53\r
54/* true if we are currently running the collector */\r
55static int collecting = 0;\r
56\r
57/* list of uncollectable objects */\r
58static PyObject *garbage = NULL;\r
59\r
60/* Python string to use if unhandled exception occurs */\r
61static PyObject *gc_str = NULL;\r
62\r
63/* Python string used to look for __del__ attribute. */\r
64static PyObject *delstr = NULL;\r
65\r
66/* This is the number of objects who survived the last full collection. It\r
67 approximates the number of long lived objects tracked by the GC.\r
68\r
69 (by "full collection", we mean a collection of the oldest generation).\r
70*/\r
71static Py_ssize_t long_lived_total = 0;\r
72\r
73/* This is the number of objects who survived all "non-full" collections,\r
74 and are awaiting to undergo a full collection for the first time.\r
75\r
76*/\r
77static Py_ssize_t long_lived_pending = 0;\r
78\r
79/*\r
80 NOTE: about the counting of long-lived objects.\r
81\r
82 To limit the cost of garbage collection, there are two strategies;\r
83 - make each collection faster, e.g. by scanning fewer objects\r
84 - do less collections\r
85 This heuristic is about the latter strategy.\r
86\r
87 In addition to the various configurable thresholds, we only trigger a\r
88 full collection if the ratio\r
89 long_lived_pending / long_lived_total\r
90 is above a given value (hardwired to 25%).\r
91\r
92 The reason is that, while "non-full" collections (i.e., collections of\r
93 the young and middle generations) will always examine roughly the same\r
94 number of objects -- determined by the aforementioned thresholds --,\r
95 the cost of a full collection is proportional to the total number of\r
96 long-lived objects, which is virtually unbounded.\r
97\r
98 Indeed, it has been remarked that doing a full collection every\r
99 <constant number> of object creations entails a dramatic performance\r
100 degradation in workloads which consist in creating and storing lots of\r
101 long-lived objects (e.g. building a large list of GC-tracked objects would\r
102 show quadratic performance, instead of linear as expected: see issue #4074).\r
103\r
104 Using the above ratio, instead, yields amortized linear performance in\r
105 the total number of objects (the effect of which can be summarized\r
106 thusly: "each full garbage collection is more and more costly as the\r
107 number of objects grows, but we do fewer and fewer of them").\r
108\r
109 This heuristic was suggested by Martin von Löwis on python-dev in\r
110 June 2008. His original analysis and proposal can be found at:\r
111 http://mail.python.org/pipermail/python-dev/2008-June/080579.html\r
112*/\r
113\r
114/*\r
115 NOTE: about untracking of mutable objects.\r
116\r
117 Certain types of container cannot participate in a reference cycle, and\r
118 so do not need to be tracked by the garbage collector. Untracking these\r
119 objects reduces the cost of garbage collections. However, determining\r
120 which objects may be untracked is not free, and the costs must be\r
121 weighed against the benefits for garbage collection.\r
122\r
123 There are two possible strategies for when to untrack a container:\r
124\r
125 i) When the container is created.\r
126 ii) When the container is examined by the garbage collector.\r
127\r
128 Tuples containing only immutable objects (integers, strings etc, and\r
129 recursively, tuples of immutable objects) do not need to be tracked.\r
130 The interpreter creates a large number of tuples, many of which will\r
131 not survive until garbage collection. It is therefore not worthwhile\r
132 to untrack eligible tuples at creation time.\r
133\r
134 Instead, all tuples except the empty tuple are tracked when created.\r
135 During garbage collection it is determined whether any surviving tuples\r
136 can be untracked. A tuple can be untracked if all of its contents are\r
137 already not tracked. Tuples are examined for untracking in all garbage\r
138 collection cycles. It may take more than one cycle to untrack a tuple.\r
139\r
140 Dictionaries containing only immutable objects also do not need to be\r
141 tracked. Dictionaries are untracked when created. If a tracked item is\r
142 inserted into a dictionary (either as a key or value), the dictionary\r
143 becomes tracked. During a full garbage collection (all generations),\r
144 the collector will untrack any dictionaries whose contents are not\r
145 tracked.\r
146\r
147 The module provides the python function is_tracked(obj), which returns\r
148 the CURRENT tracking status of the object. Subsequent garbage\r
149 collections may change the tracking status of the object.\r
150\r
151 Untracking of certain containers was introduced in issue #4688, and\r
152 the algorithm was refined in response to issue #14775.\r
153*/\r
154\r
155/* set for debugging information */\r
156#define DEBUG_STATS (1<<0) /* print collection statistics */\r
157#define DEBUG_COLLECTABLE (1<<1) /* print collectable objects */\r
158#define DEBUG_UNCOLLECTABLE (1<<2) /* print uncollectable objects */\r
159#define DEBUG_INSTANCES (1<<3) /* print instances */\r
160#define DEBUG_OBJECTS (1<<4) /* print other objects */\r
161#define DEBUG_SAVEALL (1<<5) /* save all garbage in gc.garbage */\r
162#define DEBUG_LEAK DEBUG_COLLECTABLE | \\r
163 DEBUG_UNCOLLECTABLE | \\r
164 DEBUG_INSTANCES | \\r
165 DEBUG_OBJECTS | \\r
166 DEBUG_SAVEALL\r
167static int debug;\r
168static PyObject *tmod = NULL;\r
169\r
170/*--------------------------------------------------------------------------\r
171gc_refs values.\r
172\r
173Between collections, every gc'ed object has one of two gc_refs values:\r
174\r
175GC_UNTRACKED\r
176 The initial state; objects returned by PyObject_GC_Malloc are in this\r
177 state. The object doesn't live in any generation list, and its\r
178 tp_traverse slot must not be called.\r
179\r
180GC_REACHABLE\r
181 The object lives in some generation list, and its tp_traverse is safe to\r
182 call. An object transitions to GC_REACHABLE when PyObject_GC_Track\r
183 is called.\r
184\r
185During a collection, gc_refs can temporarily take on other states:\r
186\r
187>= 0\r
188 At the start of a collection, update_refs() copies the true refcount\r
189 to gc_refs, for each object in the generation being collected.\r
190 subtract_refs() then adjusts gc_refs so that it equals the number of\r
191 times an object is referenced directly from outside the generation\r
192 being collected.\r
193 gc_refs remains >= 0 throughout these steps.\r
194\r
195GC_TENTATIVELY_UNREACHABLE\r
196 move_unreachable() then moves objects not reachable (whether directly or\r
197 indirectly) from outside the generation into an "unreachable" set.\r
198 Objects that are found to be reachable have gc_refs set to GC_REACHABLE\r
199 again. Objects that are found to be unreachable have gc_refs set to\r
200 GC_TENTATIVELY_UNREACHABLE. It's "tentatively" because the pass doing\r
201 this can't be sure until it ends, and GC_TENTATIVELY_UNREACHABLE may\r
202 transition back to GC_REACHABLE.\r
203\r
204 Only objects with GC_TENTATIVELY_UNREACHABLE still set are candidates\r
205 for collection. If it's decided not to collect such an object (e.g.,\r
206 it has a __del__ method), its gc_refs is restored to GC_REACHABLE again.\r
207----------------------------------------------------------------------------\r
208*/\r
209#define GC_UNTRACKED _PyGC_REFS_UNTRACKED\r
210#define GC_REACHABLE _PyGC_REFS_REACHABLE\r
211#define GC_TENTATIVELY_UNREACHABLE _PyGC_REFS_TENTATIVELY_UNREACHABLE\r
212\r
213#define IS_TRACKED(o) ((AS_GC(o))->gc.gc_refs != GC_UNTRACKED)\r
214#define IS_REACHABLE(o) ((AS_GC(o))->gc.gc_refs == GC_REACHABLE)\r
215#define IS_TENTATIVELY_UNREACHABLE(o) ( \\r
216 (AS_GC(o))->gc.gc_refs == GC_TENTATIVELY_UNREACHABLE)\r
217\r
218/*** list functions ***/\r
219\r
220static void\r
221gc_list_init(PyGC_Head *list)\r
222{\r
223 list->gc.gc_prev = list;\r
224 list->gc.gc_next = list;\r
225}\r
226\r
227static int\r
228gc_list_is_empty(PyGC_Head *list)\r
229{\r
230 return (list->gc.gc_next == list);\r
231}\r
232\r
233#if 0\r
234/* This became unused after gc_list_move() was introduced. */\r
235/* Append `node` to `list`. */\r
236static void\r
237gc_list_append(PyGC_Head *node, PyGC_Head *list)\r
238{\r
239 node->gc.gc_next = list;\r
240 node->gc.gc_prev = list->gc.gc_prev;\r
241 node->gc.gc_prev->gc.gc_next = node;\r
242 list->gc.gc_prev = node;\r
243}\r
244#endif\r
245\r
246/* Remove `node` from the gc list it's currently in. */\r
247static void\r
248gc_list_remove(PyGC_Head *node)\r
249{\r
250 node->gc.gc_prev->gc.gc_next = node->gc.gc_next;\r
251 node->gc.gc_next->gc.gc_prev = node->gc.gc_prev;\r
252 node->gc.gc_next = NULL; /* object is not currently tracked */\r
253}\r
254\r
255/* Move `node` from the gc list it's currently in (which is not explicitly\r
256 * named here) to the end of `list`. This is semantically the same as\r
257 * gc_list_remove(node) followed by gc_list_append(node, list).\r
258 */\r
259static void\r
260gc_list_move(PyGC_Head *node, PyGC_Head *list)\r
261{\r
262 PyGC_Head *new_prev;\r
263 PyGC_Head *current_prev = node->gc.gc_prev;\r
264 PyGC_Head *current_next = node->gc.gc_next;\r
265 /* Unlink from current list. */\r
266 current_prev->gc.gc_next = current_next;\r
267 current_next->gc.gc_prev = current_prev;\r
268 /* Relink at end of new list. */\r
269 new_prev = node->gc.gc_prev = list->gc.gc_prev;\r
270 new_prev->gc.gc_next = list->gc.gc_prev = node;\r
271 node->gc.gc_next = list;\r
272}\r
273\r
274/* append list `from` onto list `to`; `from` becomes an empty list */\r
275static void\r
276gc_list_merge(PyGC_Head *from, PyGC_Head *to)\r
277{\r
278 PyGC_Head *tail;\r
279 assert(from != to);\r
280 if (!gc_list_is_empty(from)) {\r
281 tail = to->gc.gc_prev;\r
282 tail->gc.gc_next = from->gc.gc_next;\r
283 tail->gc.gc_next->gc.gc_prev = tail;\r
284 to->gc.gc_prev = from->gc.gc_prev;\r
285 to->gc.gc_prev->gc.gc_next = to;\r
286 }\r
287 gc_list_init(from);\r
288}\r
289\r
290static Py_ssize_t\r
291gc_list_size(PyGC_Head *list)\r
292{\r
293 PyGC_Head *gc;\r
294 Py_ssize_t n = 0;\r
295 for (gc = list->gc.gc_next; gc != list; gc = gc->gc.gc_next) {\r
296 n++;\r
297 }\r
298 return n;\r
299}\r
300\r
301/* Append objects in a GC list to a Python list.\r
302 * Return 0 if all OK, < 0 if error (out of memory for list).\r
303 */\r
304static int\r
305append_objects(PyObject *py_list, PyGC_Head *gc_list)\r
306{\r
307 PyGC_Head *gc;\r
308 for (gc = gc_list->gc.gc_next; gc != gc_list; gc = gc->gc.gc_next) {\r
309 PyObject *op = FROM_GC(gc);\r
310 if (op != py_list) {\r
311 if (PyList_Append(py_list, op)) {\r
312 return -1; /* exception */\r
313 }\r
314 }\r
315 }\r
316 return 0;\r
317}\r
318\r
319/*** end of list stuff ***/\r
320\r
321\r
322/* Set all gc_refs = ob_refcnt. After this, gc_refs is > 0 for all objects\r
323 * in containers, and is GC_REACHABLE for all tracked gc objects not in\r
324 * containers.\r
325 */\r
326static void\r
327update_refs(PyGC_Head *containers)\r
328{\r
329 PyGC_Head *gc = containers->gc.gc_next;\r
330 for (; gc != containers; gc = gc->gc.gc_next) {\r
331 assert(gc->gc.gc_refs == GC_REACHABLE);\r
332 gc->gc.gc_refs = Py_REFCNT(FROM_GC(gc));\r
333 /* Python's cyclic gc should never see an incoming refcount\r
334 * of 0: if something decref'ed to 0, it should have been\r
335 * deallocated immediately at that time.\r
336 * Possible cause (if the assert triggers): a tp_dealloc\r
337 * routine left a gc-aware object tracked during its teardown\r
338 * phase, and did something-- or allowed something to happen --\r
339 * that called back into Python. gc can trigger then, and may\r
340 * see the still-tracked dying object. Before this assert\r
341 * was added, such mistakes went on to allow gc to try to\r
342 * delete the object again. In a debug build, that caused\r
343 * a mysterious segfault, when _Py_ForgetReference tried\r
344 * to remove the object from the doubly-linked list of all\r
345 * objects a second time. In a release build, an actual\r
346 * double deallocation occurred, which leads to corruption\r
347 * of the allocator's internal bookkeeping pointers. That's\r
348 * so serious that maybe this should be a release-build\r
349 * check instead of an assert?\r
350 */\r
351 assert(gc->gc.gc_refs != 0);\r
352 }\r
353}\r
354\r
355/* A traversal callback for subtract_refs. */\r
356static int\r
357visit_decref(PyObject *op, void *data)\r
358{\r
359 assert(op != NULL);\r
360 if (PyObject_IS_GC(op)) {\r
361 PyGC_Head *gc = AS_GC(op);\r
362 /* We're only interested in gc_refs for objects in the\r
363 * generation being collected, which can be recognized\r
364 * because only they have positive gc_refs.\r
365 */\r
366 assert(gc->gc.gc_refs != 0); /* else refcount was too small */\r
367 if (gc->gc.gc_refs > 0)\r
368 gc->gc.gc_refs--;\r
369 }\r
370 return 0;\r
371}\r
372\r
373/* Subtract internal references from gc_refs. After this, gc_refs is >= 0\r
374 * for all objects in containers, and is GC_REACHABLE for all tracked gc\r
375 * objects not in containers. The ones with gc_refs > 0 are directly\r
376 * reachable from outside containers, and so can't be collected.\r
377 */\r
378static void\r
379subtract_refs(PyGC_Head *containers)\r
380{\r
381 traverseproc traverse;\r
382 PyGC_Head *gc = containers->gc.gc_next;\r
383 for (; gc != containers; gc=gc->gc.gc_next) {\r
384 traverse = Py_TYPE(FROM_GC(gc))->tp_traverse;\r
385 (void) traverse(FROM_GC(gc),\r
386 (visitproc)visit_decref,\r
387 NULL);\r
388 }\r
389}\r
390\r
391/* A traversal callback for move_unreachable. */\r
392static int\r
393visit_reachable(PyObject *op, PyGC_Head *reachable)\r
394{\r
395 if (PyObject_IS_GC(op)) {\r
396 PyGC_Head *gc = AS_GC(op);\r
397 const Py_ssize_t gc_refs = gc->gc.gc_refs;\r
398\r
399 if (gc_refs == 0) {\r
400 /* This is in move_unreachable's 'young' list, but\r
401 * the traversal hasn't yet gotten to it. All\r
402 * we need to do is tell move_unreachable that it's\r
403 * reachable.\r
404 */\r
405 gc->gc.gc_refs = 1;\r
406 }\r
407 else if (gc_refs == GC_TENTATIVELY_UNREACHABLE) {\r
408 /* This had gc_refs = 0 when move_unreachable got\r
409 * to it, but turns out it's reachable after all.\r
410 * Move it back to move_unreachable's 'young' list,\r
411 * and move_unreachable will eventually get to it\r
412 * again.\r
413 */\r
414 gc_list_move(gc, reachable);\r
415 gc->gc.gc_refs = 1;\r
416 }\r
417 /* Else there's nothing to do.\r
418 * If gc_refs > 0, it must be in move_unreachable's 'young'\r
419 * list, and move_unreachable will eventually get to it.\r
420 * If gc_refs == GC_REACHABLE, it's either in some other\r
421 * generation so we don't care about it, or move_unreachable\r
422 * already dealt with it.\r
423 * If gc_refs == GC_UNTRACKED, it must be ignored.\r
424 */\r
425 else {\r
426 assert(gc_refs > 0\r
427 || gc_refs == GC_REACHABLE\r
428 || gc_refs == GC_UNTRACKED);\r
429 }\r
430 }\r
431 return 0;\r
432}\r
433\r
434/* Move the unreachable objects from young to unreachable. After this,\r
435 * all objects in young have gc_refs = GC_REACHABLE, and all objects in\r
436 * unreachable have gc_refs = GC_TENTATIVELY_UNREACHABLE. All tracked\r
437 * gc objects not in young or unreachable still have gc_refs = GC_REACHABLE.\r
438 * All objects in young after this are directly or indirectly reachable\r
439 * from outside the original young; and all objects in unreachable are\r
440 * not.\r
441 */\r
442static void\r
443move_unreachable(PyGC_Head *young, PyGC_Head *unreachable)\r
444{\r
445 PyGC_Head *gc = young->gc.gc_next;\r
446\r
447 /* Invariants: all objects "to the left" of us in young have gc_refs\r
448 * = GC_REACHABLE, and are indeed reachable (directly or indirectly)\r
449 * from outside the young list as it was at entry. All other objects\r
450 * from the original young "to the left" of us are in unreachable now,\r
451 * and have gc_refs = GC_TENTATIVELY_UNREACHABLE. All objects to the\r
452 * left of us in 'young' now have been scanned, and no objects here\r
453 * or to the right have been scanned yet.\r
454 */\r
455\r
456 while (gc != young) {\r
457 PyGC_Head *next;\r
458\r
459 if (gc->gc.gc_refs) {\r
460 /* gc is definitely reachable from outside the\r
461 * original 'young'. Mark it as such, and traverse\r
462 * its pointers to find any other objects that may\r
463 * be directly reachable from it. Note that the\r
464 * call to tp_traverse may append objects to young,\r
465 * so we have to wait until it returns to determine\r
466 * the next object to visit.\r
467 */\r
468 PyObject *op = FROM_GC(gc);\r
469 traverseproc traverse = Py_TYPE(op)->tp_traverse;\r
470 assert(gc->gc.gc_refs > 0);\r
471 gc->gc.gc_refs = GC_REACHABLE;\r
472 (void) traverse(op,\r
473 (visitproc)visit_reachable,\r
474 (void *)young);\r
475 next = gc->gc.gc_next;\r
476 if (PyTuple_CheckExact(op)) {\r
477 _PyTuple_MaybeUntrack(op);\r
478 }\r
479 }\r
480 else {\r
481 /* This *may* be unreachable. To make progress,\r
482 * assume it is. gc isn't directly reachable from\r
483 * any object we've already traversed, but may be\r
484 * reachable from an object we haven't gotten to yet.\r
485 * visit_reachable will eventually move gc back into\r
486 * young if that's so, and we'll see it again.\r
487 */\r
488 next = gc->gc.gc_next;\r
489 gc_list_move(gc, unreachable);\r
490 gc->gc.gc_refs = GC_TENTATIVELY_UNREACHABLE;\r
491 }\r
492 gc = next;\r
493 }\r
494}\r
495\r
496/* Return true if object has a finalization method.\r
497 * CAUTION: An instance of an old-style class has to be checked for a\r
498 *__del__ method, and earlier versions of this used to call PyObject_HasAttr,\r
499 * which in turn could call the class's __getattr__ hook (if any). That\r
500 * could invoke arbitrary Python code, mutating the object graph in arbitrary\r
501 * ways, and that was the source of some excruciatingly subtle bugs.\r
502 */\r
503static int\r
504has_finalizer(PyObject *op)\r
505{\r
506 if (PyInstance_Check(op)) {\r
507 assert(delstr != NULL);\r
508 return _PyInstance_Lookup(op, delstr) != NULL;\r
509 }\r
510 else if (PyType_HasFeature(op->ob_type, Py_TPFLAGS_HEAPTYPE))\r
511 return op->ob_type->tp_del != NULL;\r
512 else if (PyGen_CheckExact(op))\r
513 return PyGen_NeedsFinalizing((PyGenObject *)op);\r
514 else\r
515 return 0;\r
516}\r
517\r
518/* Try to untrack all currently tracked dictionaries */\r
519static void\r
520untrack_dicts(PyGC_Head *head)\r
521{\r
522 PyGC_Head *next, *gc = head->gc.gc_next;\r
523 while (gc != head) {\r
524 PyObject *op = FROM_GC(gc);\r
525 next = gc->gc.gc_next;\r
526 if (PyDict_CheckExact(op))\r
527 _PyDict_MaybeUntrack(op);\r
528 gc = next;\r
529 }\r
530}\r
531\r
532/* Move the objects in unreachable with __del__ methods into `finalizers`.\r
533 * Objects moved into `finalizers` have gc_refs set to GC_REACHABLE; the\r
534 * objects remaining in unreachable are left at GC_TENTATIVELY_UNREACHABLE.\r
535 */\r
536static void\r
537move_finalizers(PyGC_Head *unreachable, PyGC_Head *finalizers)\r
538{\r
539 PyGC_Head *gc;\r
540 PyGC_Head *next;\r
541\r
542 /* March over unreachable. Move objects with finalizers into\r
543 * `finalizers`.\r
544 */\r
545 for (gc = unreachable->gc.gc_next; gc != unreachable; gc = next) {\r
546 PyObject *op = FROM_GC(gc);\r
547\r
548 assert(IS_TENTATIVELY_UNREACHABLE(op));\r
549 next = gc->gc.gc_next;\r
550\r
551 if (has_finalizer(op)) {\r
552 gc_list_move(gc, finalizers);\r
553 gc->gc.gc_refs = GC_REACHABLE;\r
554 }\r
555 }\r
556}\r
557\r
558/* A traversal callback for move_finalizer_reachable. */\r
559static int\r
560visit_move(PyObject *op, PyGC_Head *tolist)\r
561{\r
562 if (PyObject_IS_GC(op)) {\r
563 if (IS_TENTATIVELY_UNREACHABLE(op)) {\r
564 PyGC_Head *gc = AS_GC(op);\r
565 gc_list_move(gc, tolist);\r
566 gc->gc.gc_refs = GC_REACHABLE;\r
567 }\r
568 }\r
569 return 0;\r
570}\r
571\r
572/* Move objects that are reachable from finalizers, from the unreachable set\r
573 * into finalizers set.\r
574 */\r
575static void\r
576move_finalizer_reachable(PyGC_Head *finalizers)\r
577{\r
578 traverseproc traverse;\r
579 PyGC_Head *gc = finalizers->gc.gc_next;\r
580 for (; gc != finalizers; gc = gc->gc.gc_next) {\r
581 /* Note that the finalizers list may grow during this. */\r
582 traverse = Py_TYPE(FROM_GC(gc))->tp_traverse;\r
583 (void) traverse(FROM_GC(gc),\r
584 (visitproc)visit_move,\r
585 (void *)finalizers);\r
586 }\r
587}\r
588\r
589/* Clear all weakrefs to unreachable objects, and if such a weakref has a\r
590 * callback, invoke it if necessary. Note that it's possible for such\r
591 * weakrefs to be outside the unreachable set -- indeed, those are precisely\r
592 * the weakrefs whose callbacks must be invoked. See gc_weakref.txt for\r
593 * overview & some details. Some weakrefs with callbacks may be reclaimed\r
594 * directly by this routine; the number reclaimed is the return value. Other\r
595 * weakrefs with callbacks may be moved into the `old` generation. Objects\r
596 * moved into `old` have gc_refs set to GC_REACHABLE; the objects remaining in\r
597 * unreachable are left at GC_TENTATIVELY_UNREACHABLE. When this returns,\r
598 * no object in `unreachable` is weakly referenced anymore.\r
599 */\r
600static int\r
601handle_weakrefs(PyGC_Head *unreachable, PyGC_Head *old)\r
602{\r
603 PyGC_Head *gc;\r
604 PyObject *op; /* generally FROM_GC(gc) */\r
605 PyWeakReference *wr; /* generally a cast of op */\r
606 PyGC_Head wrcb_to_call; /* weakrefs with callbacks to call */\r
607 PyGC_Head *next;\r
608 int num_freed = 0;\r
609\r
610 gc_list_init(&wrcb_to_call);\r
611\r
612 /* Clear all weakrefs to the objects in unreachable. If such a weakref\r
613 * also has a callback, move it into `wrcb_to_call` if the callback\r
614 * needs to be invoked. Note that we cannot invoke any callbacks until\r
615 * all weakrefs to unreachable objects are cleared, lest the callback\r
616 * resurrect an unreachable object via a still-active weakref. We\r
617 * make another pass over wrcb_to_call, invoking callbacks, after this\r
618 * pass completes.\r
619 */\r
620 for (gc = unreachable->gc.gc_next; gc != unreachable; gc = next) {\r
621 PyWeakReference **wrlist;\r
622\r
623 op = FROM_GC(gc);\r
624 assert(IS_TENTATIVELY_UNREACHABLE(op));\r
625 next = gc->gc.gc_next;\r
626\r
627 if (! PyType_SUPPORTS_WEAKREFS(Py_TYPE(op)))\r
628 continue;\r
629\r
630 /* It supports weakrefs. Does it have any? */\r
631 wrlist = (PyWeakReference **)\r
632 PyObject_GET_WEAKREFS_LISTPTR(op);\r
633\r
634 /* `op` may have some weakrefs. March over the list, clear\r
635 * all the weakrefs, and move the weakrefs with callbacks\r
636 * that must be called into wrcb_to_call.\r
637 */\r
638 for (wr = *wrlist; wr != NULL; wr = *wrlist) {\r
639 PyGC_Head *wrasgc; /* AS_GC(wr) */\r
640\r
641 /* _PyWeakref_ClearRef clears the weakref but leaves\r
642 * the callback pointer intact. Obscure: it also\r
643 * changes *wrlist.\r
644 */\r
645 assert(wr->wr_object == op);\r
646 _PyWeakref_ClearRef(wr);\r
647 assert(wr->wr_object == Py_None);\r
648 if (wr->wr_callback == NULL)\r
649 continue; /* no callback */\r
650\r
651 /* Headache time. `op` is going away, and is weakly referenced by\r
652 * `wr`, which has a callback. Should the callback be invoked? If wr\r
653 * is also trash, no:\r
654 *\r
655 * 1. There's no need to call it. The object and the weakref are\r
656 * both going away, so it's legitimate to pretend the weakref is\r
657 * going away first. The user has to ensure a weakref outlives its\r
658 * referent if they want a guarantee that the wr callback will get\r
659 * invoked.\r
660 *\r
661 * 2. It may be catastrophic to call it. If the callback is also in\r
662 * cyclic trash (CT), then although the CT is unreachable from\r
663 * outside the current generation, CT may be reachable from the\r
664 * callback. Then the callback could resurrect insane objects.\r
665 *\r
666 * Since the callback is never needed and may be unsafe in this case,\r
667 * wr is simply left in the unreachable set. Note that because we\r
668 * already called _PyWeakref_ClearRef(wr), its callback will never\r
669 * trigger.\r
670 *\r
671 * OTOH, if wr isn't part of CT, we should invoke the callback: the\r
672 * weakref outlived the trash. Note that since wr isn't CT in this\r
673 * case, its callback can't be CT either -- wr acted as an external\r
674 * root to this generation, and therefore its callback did too. So\r
675 * nothing in CT is reachable from the callback either, so it's hard\r
676 * to imagine how calling it later could create a problem for us. wr\r
677 * is moved to wrcb_to_call in this case.\r
678 */\r
679 if (IS_TENTATIVELY_UNREACHABLE(wr))\r
680 continue;\r
681 assert(IS_REACHABLE(wr));\r
682\r
683 /* Create a new reference so that wr can't go away\r
684 * before we can process it again.\r
685 */\r
686 Py_INCREF(wr);\r
687\r
688 /* Move wr to wrcb_to_call, for the next pass. */\r
689 wrasgc = AS_GC(wr);\r
690 assert(wrasgc != next); /* wrasgc is reachable, but\r
691 next isn't, so they can't\r
692 be the same */\r
693 gc_list_move(wrasgc, &wrcb_to_call);\r
694 }\r
695 }\r
696\r
697 /* Invoke the callbacks we decided to honor. It's safe to invoke them\r
698 * because they can't reference unreachable objects.\r
699 */\r
700 while (! gc_list_is_empty(&wrcb_to_call)) {\r
701 PyObject *temp;\r
702 PyObject *callback;\r
703\r
704 gc = wrcb_to_call.gc.gc_next;\r
705 op = FROM_GC(gc);\r
706 assert(IS_REACHABLE(op));\r
707 assert(PyWeakref_Check(op));\r
708 wr = (PyWeakReference *)op;\r
709 callback = wr->wr_callback;\r
710 assert(callback != NULL);\r
711\r
712 /* copy-paste of weakrefobject.c's handle_callback() */\r
713 temp = PyObject_CallFunctionObjArgs(callback, wr, NULL);\r
714 if (temp == NULL)\r
715 PyErr_WriteUnraisable(callback);\r
716 else\r
717 Py_DECREF(temp);\r
718\r
719 /* Give up the reference we created in the first pass. When\r
720 * op's refcount hits 0 (which it may or may not do right now),\r
721 * op's tp_dealloc will decref op->wr_callback too. Note\r
722 * that the refcount probably will hit 0 now, and because this\r
723 * weakref was reachable to begin with, gc didn't already\r
724 * add it to its count of freed objects. Example: a reachable\r
725 * weak value dict maps some key to this reachable weakref.\r
726 * The callback removes this key->weakref mapping from the\r
727 * dict, leaving no other references to the weakref (excepting\r
728 * ours).\r
729 */\r
730 Py_DECREF(op);\r
731 if (wrcb_to_call.gc.gc_next == gc) {\r
732 /* object is still alive -- move it */\r
733 gc_list_move(gc, old);\r
734 }\r
735 else\r
736 ++num_freed;\r
737 }\r
738\r
739 return num_freed;\r
740}\r
741\r
742static void\r
743debug_instance(char *msg, PyInstanceObject *inst)\r
744{\r
745 char *cname;\r
746 /* simple version of instance_repr */\r
747 PyObject *classname = inst->in_class->cl_name;\r
748 if (classname != NULL && PyString_Check(classname))\r
749 cname = PyString_AsString(classname);\r
750 else\r
751 cname = "?";\r
752 PySys_WriteStderr("gc: %.100s <%.100s instance at %p>\n",\r
753 msg, cname, inst);\r
754}\r
755\r
756static void\r
757debug_cycle(char *msg, PyObject *op)\r
758{\r
759 if ((debug & DEBUG_INSTANCES) && PyInstance_Check(op)) {\r
760 debug_instance(msg, (PyInstanceObject *)op);\r
761 }\r
762 else if (debug & DEBUG_OBJECTS) {\r
763 PySys_WriteStderr("gc: %.100s <%.100s %p>\n",\r
764 msg, Py_TYPE(op)->tp_name, op);\r
765 }\r
766}\r
767\r
768/* Handle uncollectable garbage (cycles with finalizers, and stuff reachable\r
769 * only from such cycles).\r
770 * If DEBUG_SAVEALL, all objects in finalizers are appended to the module\r
771 * garbage list (a Python list), else only the objects in finalizers with\r
772 * __del__ methods are appended to garbage. All objects in finalizers are\r
773 * merged into the old list regardless.\r
774 * Returns 0 if all OK, <0 on error (out of memory to grow the garbage list).\r
775 * The finalizers list is made empty on a successful return.\r
776 */\r
777static int\r
778handle_finalizers(PyGC_Head *finalizers, PyGC_Head *old)\r
779{\r
780 PyGC_Head *gc = finalizers->gc.gc_next;\r
781\r
782 if (garbage == NULL) {\r
783 garbage = PyList_New(0);\r
784 if (garbage == NULL)\r
785 Py_FatalError("gc couldn't create gc.garbage list");\r
786 }\r
787 for (; gc != finalizers; gc = gc->gc.gc_next) {\r
788 PyObject *op = FROM_GC(gc);\r
789\r
790 if ((debug & DEBUG_SAVEALL) || has_finalizer(op)) {\r
791 if (PyList_Append(garbage, op) < 0)\r
792 return -1;\r
793 }\r
794 }\r
795\r
796 gc_list_merge(finalizers, old);\r
797 return 0;\r
798}\r
799\r
800/* Break reference cycles by clearing the containers involved. This is\r
801 * tricky business as the lists can be changing and we don't know which\r
802 * objects may be freed. It is possible I screwed something up here.\r
803 */\r
804static void\r
805delete_garbage(PyGC_Head *collectable, PyGC_Head *old)\r
806{\r
807 inquiry clear;\r
808\r
809 while (!gc_list_is_empty(collectable)) {\r
810 PyGC_Head *gc = collectable->gc.gc_next;\r
811 PyObject *op = FROM_GC(gc);\r
812\r
813 assert(IS_TENTATIVELY_UNREACHABLE(op));\r
814 if (debug & DEBUG_SAVEALL) {\r
815 PyList_Append(garbage, op);\r
816 }\r
817 else {\r
818 if ((clear = Py_TYPE(op)->tp_clear) != NULL) {\r
819 Py_INCREF(op);\r
820 clear(op);\r
821 Py_DECREF(op);\r
822 }\r
823 }\r
824 if (collectable->gc.gc_next == gc) {\r
825 /* object is still alive, move it, it may die later */\r
826 gc_list_move(gc, old);\r
827 gc->gc.gc_refs = GC_REACHABLE;\r
828 }\r
829 }\r
830}\r
831\r
832/* Clear all free lists\r
833 * All free lists are cleared during the collection of the highest generation.\r
834 * Allocated items in the free list may keep a pymalloc arena occupied.\r
835 * Clearing the free lists may give back memory to the OS earlier.\r
836 */\r
837static void\r
838clear_freelists(void)\r
839{\r
840 (void)PyMethod_ClearFreeList();\r
841 (void)PyFrame_ClearFreeList();\r
842 (void)PyCFunction_ClearFreeList();\r
843 (void)PyTuple_ClearFreeList();\r
844#ifdef Py_USING_UNICODE\r
845 (void)PyUnicode_ClearFreeList();\r
846#endif\r
847 (void)PyInt_ClearFreeList();\r
848 (void)PyFloat_ClearFreeList();\r
849}\r
850\r
851static double\r
852get_time(void)\r
853{\r
854 double result = 0;\r
855 if (tmod != NULL) {\r
856 PyObject *f = PyObject_CallMethod(tmod, "time", NULL);\r
857 if (f == NULL) {\r
858 PyErr_Clear();\r
859 }\r
860 else {\r
861 if (PyFloat_Check(f))\r
862 result = PyFloat_AsDouble(f);\r
863 Py_DECREF(f);\r
864 }\r
865 }\r
866 return result;\r
867}\r
868\r
869/* This is the main function. Read this to understand how the\r
870 * collection process works. */\r
871static Py_ssize_t\r
872collect(int generation)\r
873{\r
874 int i;\r
875 Py_ssize_t m = 0; /* # objects collected */\r
876 Py_ssize_t n = 0; /* # unreachable objects that couldn't be collected */\r
877 PyGC_Head *young; /* the generation we are examining */\r
878 PyGC_Head *old; /* next older generation */\r
879 PyGC_Head unreachable; /* non-problematic unreachable trash */\r
880 PyGC_Head finalizers; /* objects with, & reachable from, __del__ */\r
881 PyGC_Head *gc;\r
882 double t1 = 0.0;\r
883\r
884 if (delstr == NULL) {\r
885 delstr = PyString_InternFromString("__del__");\r
886 if (delstr == NULL)\r
887 Py_FatalError("gc couldn't allocate \"__del__\"");\r
888 }\r
889\r
890 if (debug & DEBUG_STATS) {\r
891 PySys_WriteStderr("gc: collecting generation %d...\n",\r
892 generation);\r
893 PySys_WriteStderr("gc: objects in each generation:");\r
894 for (i = 0; i < NUM_GENERATIONS; i++)\r
895 PySys_WriteStderr(" %" PY_FORMAT_SIZE_T "d",\r
896 gc_list_size(GEN_HEAD(i)));\r
897 t1 = get_time();\r
898 PySys_WriteStderr("\n");\r
899 }\r
900\r
901 /* update collection and allocation counters */\r
902 if (generation+1 < NUM_GENERATIONS)\r
903 generations[generation+1].count += 1;\r
904 for (i = 0; i <= generation; i++)\r
905 generations[i].count = 0;\r
906\r
907 /* merge younger generations with one we are currently collecting */\r
908 for (i = 0; i < generation; i++) {\r
909 gc_list_merge(GEN_HEAD(i), GEN_HEAD(generation));\r
910 }\r
911\r
912 /* handy references */\r
913 young = GEN_HEAD(generation);\r
914 if (generation < NUM_GENERATIONS-1)\r
915 old = GEN_HEAD(generation+1);\r
916 else\r
917 old = young;\r
918\r
919 /* Using ob_refcnt and gc_refs, calculate which objects in the\r
920 * container set are reachable from outside the set (i.e., have a\r
921 * refcount greater than 0 when all the references within the\r
922 * set are taken into account).\r
923 */\r
924 update_refs(young);\r
925 subtract_refs(young);\r
926\r
927 /* Leave everything reachable from outside young in young, and move\r
928 * everything else (in young) to unreachable.\r
929 * NOTE: This used to move the reachable objects into a reachable\r
930 * set instead. But most things usually turn out to be reachable,\r
931 * so it's more efficient to move the unreachable things.\r
932 */\r
933 gc_list_init(&unreachable);\r
934 move_unreachable(young, &unreachable);\r
935\r
936 /* Move reachable objects to next generation. */\r
937 if (young != old) {\r
938 if (generation == NUM_GENERATIONS - 2) {\r
939 long_lived_pending += gc_list_size(young);\r
940 }\r
941 gc_list_merge(young, old);\r
942 }\r
943 else {\r
944 /* We only untrack dicts in full collections, to avoid quadratic\r
945 dict build-up. See issue #14775. */\r
946 untrack_dicts(young);\r
947 long_lived_pending = 0;\r
948 long_lived_total = gc_list_size(young);\r
949 }\r
950\r
951 /* All objects in unreachable are trash, but objects reachable from\r
952 * finalizers can't safely be deleted. Python programmers should take\r
953 * care not to create such things. For Python, finalizers means\r
954 * instance objects with __del__ methods. Weakrefs with callbacks\r
955 * can also call arbitrary Python code but they will be dealt with by\r
956 * handle_weakrefs().\r
957 */\r
958 gc_list_init(&finalizers);\r
959 move_finalizers(&unreachable, &finalizers);\r
960 /* finalizers contains the unreachable objects with a finalizer;\r
961 * unreachable objects reachable *from* those are also uncollectable,\r
962 * and we move those into the finalizers list too.\r
963 */\r
964 move_finalizer_reachable(&finalizers);\r
965\r
966 /* Collect statistics on collectable objects found and print\r
967 * debugging information.\r
968 */\r
969 for (gc = unreachable.gc.gc_next; gc != &unreachable;\r
970 gc = gc->gc.gc_next) {\r
971 m++;\r
972 if (debug & DEBUG_COLLECTABLE) {\r
973 debug_cycle("collectable", FROM_GC(gc));\r
974 }\r
975 }\r
976\r
977 /* Clear weakrefs and invoke callbacks as necessary. */\r
978 m += handle_weakrefs(&unreachable, old);\r
979\r
980 /* Call tp_clear on objects in the unreachable set. This will cause\r
981 * the reference cycles to be broken. It may also cause some objects\r
982 * in finalizers to be freed.\r
983 */\r
984 delete_garbage(&unreachable, old);\r
985\r
986 /* Collect statistics on uncollectable objects found and print\r
987 * debugging information. */\r
988 for (gc = finalizers.gc.gc_next;\r
989 gc != &finalizers;\r
990 gc = gc->gc.gc_next) {\r
991 n++;\r
992 if (debug & DEBUG_UNCOLLECTABLE)\r
993 debug_cycle("uncollectable", FROM_GC(gc));\r
994 }\r
995 if (debug & DEBUG_STATS) {\r
996 double t2 = get_time();\r
997 if (m == 0 && n == 0)\r
998 PySys_WriteStderr("gc: done");\r
999 else\r
1000 PySys_WriteStderr(\r
1001 "gc: done, "\r
1002 "%" PY_FORMAT_SIZE_T "d unreachable, "\r
1003 "%" PY_FORMAT_SIZE_T "d uncollectable",\r
1004 n+m, n);\r
1005 if (t1 && t2) {\r
1006 PySys_WriteStderr(", %.4fs elapsed", t2-t1);\r
1007 }\r
1008 PySys_WriteStderr(".\n");\r
1009 }\r
1010\r
1011 /* Append instances in the uncollectable set to a Python\r
1012 * reachable list of garbage. The programmer has to deal with\r
1013 * this if they insist on creating this type of structure.\r
1014 */\r
1015 (void)handle_finalizers(&finalizers, old);\r
1016\r
1017 /* Clear free list only during the collection of the highest\r
1018 * generation */\r
1019 if (generation == NUM_GENERATIONS-1) {\r
1020 clear_freelists();\r
1021 }\r
1022\r
1023 if (PyErr_Occurred()) {\r
1024 if (gc_str == NULL)\r
1025 gc_str = PyString_FromString("garbage collection");\r
1026 PyErr_WriteUnraisable(gc_str);\r
1027 Py_FatalError("unexpected exception during garbage collection");\r
1028 }\r
1029 return n+m;\r
1030}\r
1031\r
1032static Py_ssize_t\r
1033collect_generations(void)\r
1034{\r
1035 int i;\r
1036 Py_ssize_t n = 0;\r
1037\r
1038 /* Find the oldest generation (highest numbered) where the count\r
1039 * exceeds the threshold. Objects in the that generation and\r
1040 * generations younger than it will be collected. */\r
1041 for (i = NUM_GENERATIONS-1; i >= 0; i--) {\r
1042 if (generations[i].count > generations[i].threshold) {\r
1043 /* Avoid quadratic performance degradation in number\r
1044 of tracked objects. See comments at the beginning\r
1045 of this file, and issue #4074.\r
1046 */\r
1047 if (i == NUM_GENERATIONS - 1\r
1048 && long_lived_pending < long_lived_total / 4)\r
1049 continue;\r
1050 n = collect(i);\r
1051 break;\r
1052 }\r
1053 }\r
1054 return n;\r
1055}\r
1056\r
1057PyDoc_STRVAR(gc_enable__doc__,\r
1058"enable() -> None\n"\r
1059"\n"\r
1060"Enable automatic garbage collection.\n");\r
1061\r
1062static PyObject *\r
1063gc_enable(PyObject *self, PyObject *noargs)\r
1064{\r
1065 enabled = 1;\r
1066 Py_INCREF(Py_None);\r
1067 return Py_None;\r
1068}\r
1069\r
1070PyDoc_STRVAR(gc_disable__doc__,\r
1071"disable() -> None\n"\r
1072"\n"\r
1073"Disable automatic garbage collection.\n");\r
1074\r
1075static PyObject *\r
1076gc_disable(PyObject *self, PyObject *noargs)\r
1077{\r
1078 enabled = 0;\r
1079 Py_INCREF(Py_None);\r
1080 return Py_None;\r
1081}\r
1082\r
1083PyDoc_STRVAR(gc_isenabled__doc__,\r
1084"isenabled() -> status\n"\r
1085"\n"\r
1086"Returns true if automatic garbage collection is enabled.\n");\r
1087\r
1088static PyObject *\r
1089gc_isenabled(PyObject *self, PyObject *noargs)\r
1090{\r
1091 return PyBool_FromLong((long)enabled);\r
1092}\r
1093\r
1094PyDoc_STRVAR(gc_collect__doc__,\r
1095"collect([generation]) -> n\n"\r
1096"\n"\r
1097"With no arguments, run a full collection. The optional argument\n"\r
1098"may be an integer specifying which generation to collect. A ValueError\n"\r
1099"is raised if the generation number is invalid.\n\n"\r
1100"The number of unreachable objects is returned.\n");\r
1101\r
1102static PyObject *\r
1103gc_collect(PyObject *self, PyObject *args, PyObject *kws)\r
1104{\r
1105 static char *keywords[] = {"generation", NULL};\r
1106 int genarg = NUM_GENERATIONS - 1;\r
1107 Py_ssize_t n;\r
1108\r
1109 if (!PyArg_ParseTupleAndKeywords(args, kws, "|i", keywords, &genarg))\r
1110 return NULL;\r
1111\r
1112 else if (genarg < 0 || genarg >= NUM_GENERATIONS) {\r
1113 PyErr_SetString(PyExc_ValueError, "invalid generation");\r
1114 return NULL;\r
1115 }\r
1116\r
1117 if (collecting)\r
1118 n = 0; /* already collecting, don't do anything */\r
1119 else {\r
1120 collecting = 1;\r
1121 n = collect(genarg);\r
1122 collecting = 0;\r
1123 }\r
1124\r
1125 return PyInt_FromSsize_t(n);\r
1126}\r
1127\r
1128PyDoc_STRVAR(gc_set_debug__doc__,\r
1129"set_debug(flags) -> None\n"\r
1130"\n"\r
1131"Set the garbage collection debugging flags. Debugging information is\n"\r
1132"written to sys.stderr.\n"\r
1133"\n"\r
1134"flags is an integer and can have the following bits turned on:\n"\r
1135"\n"\r
1136" DEBUG_STATS - Print statistics during collection.\n"\r
1137" DEBUG_COLLECTABLE - Print collectable objects found.\n"\r
1138" DEBUG_UNCOLLECTABLE - Print unreachable but uncollectable objects found.\n"\r
1139" DEBUG_INSTANCES - Print instance objects.\n"\r
1140" DEBUG_OBJECTS - Print objects other than instances.\n"\r
1141" DEBUG_SAVEALL - Save objects to gc.garbage rather than freeing them.\n"\r
1142" DEBUG_LEAK - Debug leaking programs (everything but STATS).\n");\r
1143\r
1144static PyObject *\r
1145gc_set_debug(PyObject *self, PyObject *args)\r
1146{\r
1147 if (!PyArg_ParseTuple(args, "i:set_debug", &debug))\r
1148 return NULL;\r
1149\r
1150 Py_INCREF(Py_None);\r
1151 return Py_None;\r
1152}\r
1153\r
1154PyDoc_STRVAR(gc_get_debug__doc__,\r
1155"get_debug() -> flags\n"\r
1156"\n"\r
1157"Get the garbage collection debugging flags.\n");\r
1158\r
1159static PyObject *\r
1160gc_get_debug(PyObject *self, PyObject *noargs)\r
1161{\r
1162 return Py_BuildValue("i", debug);\r
1163}\r
1164\r
1165PyDoc_STRVAR(gc_set_thresh__doc__,\r
1166"set_threshold(threshold0, [threshold1, threshold2]) -> None\n"\r
1167"\n"\r
1168"Sets the collection thresholds. Setting threshold0 to zero disables\n"\r
1169"collection.\n");\r
1170\r
1171static PyObject *\r
1172gc_set_thresh(PyObject *self, PyObject *args)\r
1173{\r
1174 int i;\r
1175 if (!PyArg_ParseTuple(args, "i|ii:set_threshold",\r
1176 &generations[0].threshold,\r
1177 &generations[1].threshold,\r
1178 &generations[2].threshold))\r
1179 return NULL;\r
1180 for (i = 2; i < NUM_GENERATIONS; i++) {\r
1181 /* generations higher than 2 get the same threshold */\r
1182 generations[i].threshold = generations[2].threshold;\r
1183 }\r
1184\r
1185 Py_INCREF(Py_None);\r
1186 return Py_None;\r
1187}\r
1188\r
1189PyDoc_STRVAR(gc_get_thresh__doc__,\r
1190"get_threshold() -> (threshold0, threshold1, threshold2)\n"\r
1191"\n"\r
1192"Return the current collection thresholds\n");\r
1193\r
1194static PyObject *\r
1195gc_get_thresh(PyObject *self, PyObject *noargs)\r
1196{\r
1197 return Py_BuildValue("(iii)",\r
1198 generations[0].threshold,\r
1199 generations[1].threshold,\r
1200 generations[2].threshold);\r
1201}\r
1202\r
1203PyDoc_STRVAR(gc_get_count__doc__,\r
1204"get_count() -> (count0, count1, count2)\n"\r
1205"\n"\r
1206"Return the current collection counts\n");\r
1207\r
1208static PyObject *\r
1209gc_get_count(PyObject *self, PyObject *noargs)\r
1210{\r
1211 return Py_BuildValue("(iii)",\r
1212 generations[0].count,\r
1213 generations[1].count,\r
1214 generations[2].count);\r
1215}\r
1216\r
1217static int\r
1218referrersvisit(PyObject* obj, PyObject *objs)\r
1219{\r
1220 Py_ssize_t i;\r
1221 for (i = 0; i < PyTuple_GET_SIZE(objs); i++)\r
1222 if (PyTuple_GET_ITEM(objs, i) == obj)\r
1223 return 1;\r
1224 return 0;\r
1225}\r
1226\r
1227static int\r
1228gc_referrers_for(PyObject *objs, PyGC_Head *list, PyObject *resultlist)\r
1229{\r
1230 PyGC_Head *gc;\r
1231 PyObject *obj;\r
1232 traverseproc traverse;\r
1233 for (gc = list->gc.gc_next; gc != list; gc = gc->gc.gc_next) {\r
1234 obj = FROM_GC(gc);\r
1235 traverse = Py_TYPE(obj)->tp_traverse;\r
1236 if (obj == objs || obj == resultlist)\r
1237 continue;\r
1238 if (traverse(obj, (visitproc)referrersvisit, objs)) {\r
1239 if (PyList_Append(resultlist, obj) < 0)\r
1240 return 0; /* error */\r
1241 }\r
1242 }\r
1243 return 1; /* no error */\r
1244}\r
1245\r
1246PyDoc_STRVAR(gc_get_referrers__doc__,\r
1247"get_referrers(*objs) -> list\n\\r
1248Return the list of objects that directly refer to any of objs.");\r
1249\r
1250static PyObject *\r
1251gc_get_referrers(PyObject *self, PyObject *args)\r
1252{\r
1253 int i;\r
1254 PyObject *result = PyList_New(0);\r
1255 if (!result) return NULL;\r
1256\r
1257 for (i = 0; i < NUM_GENERATIONS; i++) {\r
1258 if (!(gc_referrers_for(args, GEN_HEAD(i), result))) {\r
1259 Py_DECREF(result);\r
1260 return NULL;\r
1261 }\r
1262 }\r
1263 return result;\r
1264}\r
1265\r
1266/* Append obj to list; return true if error (out of memory), false if OK. */\r
1267static int\r
1268referentsvisit(PyObject *obj, PyObject *list)\r
1269{\r
1270 return PyList_Append(list, obj) < 0;\r
1271}\r
1272\r
1273PyDoc_STRVAR(gc_get_referents__doc__,\r
1274"get_referents(*objs) -> list\n\\r
1275Return the list of objects that are directly referred to by objs.");\r
1276\r
1277static PyObject *\r
1278gc_get_referents(PyObject *self, PyObject *args)\r
1279{\r
1280 Py_ssize_t i;\r
1281 PyObject *result = PyList_New(0);\r
1282\r
1283 if (result == NULL)\r
1284 return NULL;\r
1285\r
1286 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {\r
1287 traverseproc traverse;\r
1288 PyObject *obj = PyTuple_GET_ITEM(args, i);\r
1289\r
1290 if (! PyObject_IS_GC(obj))\r
1291 continue;\r
1292 traverse = Py_TYPE(obj)->tp_traverse;\r
1293 if (! traverse)\r
1294 continue;\r
1295 if (traverse(obj, (visitproc)referentsvisit, result)) {\r
1296 Py_DECREF(result);\r
1297 return NULL;\r
1298 }\r
1299 }\r
1300 return result;\r
1301}\r
1302\r
1303PyDoc_STRVAR(gc_get_objects__doc__,\r
1304"get_objects() -> [...]\n"\r
1305"\n"\r
1306"Return a list of objects tracked by the collector (excluding the list\n"\r
1307"returned).\n");\r
1308\r
1309static PyObject *\r
1310gc_get_objects(PyObject *self, PyObject *noargs)\r
1311{\r
1312 int i;\r
1313 PyObject* result;\r
1314\r
1315 result = PyList_New(0);\r
1316 if (result == NULL)\r
1317 return NULL;\r
1318 for (i = 0; i < NUM_GENERATIONS; i++) {\r
1319 if (append_objects(result, GEN_HEAD(i))) {\r
1320 Py_DECREF(result);\r
1321 return NULL;\r
1322 }\r
1323 }\r
1324 return result;\r
1325}\r
1326\r
1327PyDoc_STRVAR(gc_is_tracked__doc__,\r
1328"is_tracked(obj) -> bool\n"\r
1329"\n"\r
1330"Returns true if the object is tracked by the garbage collector.\n"\r
1331"Simple atomic objects will return false.\n"\r
1332);\r
1333\r
1334static PyObject *\r
1335gc_is_tracked(PyObject *self, PyObject *obj)\r
1336{\r
1337 PyObject *result;\r
1338\r
1339 if (PyObject_IS_GC(obj) && IS_TRACKED(obj))\r
1340 result = Py_True;\r
1341 else\r
1342 result = Py_False;\r
1343 Py_INCREF(result);\r
1344 return result;\r
1345}\r
1346\r
1347\r
1348PyDoc_STRVAR(gc__doc__,\r
1349"This module provides access to the garbage collector for reference cycles.\n"\r
1350"\n"\r
1351"enable() -- Enable automatic garbage collection.\n"\r
1352"disable() -- Disable automatic garbage collection.\n"\r
1353"isenabled() -- Returns true if automatic collection is enabled.\n"\r
1354"collect() -- Do a full collection right now.\n"\r
1355"get_count() -- Return the current collection counts.\n"\r
1356"set_debug() -- Set debugging flags.\n"\r
1357"get_debug() -- Get debugging flags.\n"\r
1358"set_threshold() -- Set the collection thresholds.\n"\r
1359"get_threshold() -- Return the current the collection thresholds.\n"\r
1360"get_objects() -- Return a list of all objects tracked by the collector.\n"\r
1361"is_tracked() -- Returns true if a given object is tracked.\n"\r
1362"get_referrers() -- Return the list of objects that refer to an object.\n"\r
1363"get_referents() -- Return the list of objects that an object refers to.\n");\r
1364\r
1365static PyMethodDef GcMethods[] = {\r
1366 {"enable", gc_enable, METH_NOARGS, gc_enable__doc__},\r
1367 {"disable", gc_disable, METH_NOARGS, gc_disable__doc__},\r
1368 {"isenabled", gc_isenabled, METH_NOARGS, gc_isenabled__doc__},\r
1369 {"set_debug", gc_set_debug, METH_VARARGS, gc_set_debug__doc__},\r
1370 {"get_debug", gc_get_debug, METH_NOARGS, gc_get_debug__doc__},\r
1371 {"get_count", gc_get_count, METH_NOARGS, gc_get_count__doc__},\r
1372 {"set_threshold", gc_set_thresh, METH_VARARGS, gc_set_thresh__doc__},\r
1373 {"get_threshold", gc_get_thresh, METH_NOARGS, gc_get_thresh__doc__},\r
1374 {"collect", (PyCFunction)gc_collect,\r
1375 METH_VARARGS | METH_KEYWORDS, gc_collect__doc__},\r
1376 {"get_objects", gc_get_objects,METH_NOARGS, gc_get_objects__doc__},\r
1377 {"is_tracked", gc_is_tracked, METH_O, gc_is_tracked__doc__},\r
1378 {"get_referrers", gc_get_referrers, METH_VARARGS,\r
1379 gc_get_referrers__doc__},\r
1380 {"get_referents", gc_get_referents, METH_VARARGS,\r
1381 gc_get_referents__doc__},\r
1382 {NULL, NULL} /* Sentinel */\r
1383};\r
1384\r
1385PyMODINIT_FUNC\r
1386initgc(void)\r
1387{\r
1388 PyObject *m;\r
1389\r
1390 m = Py_InitModule4("gc",\r
1391 GcMethods,\r
1392 gc__doc__,\r
1393 NULL,\r
1394 PYTHON_API_VERSION);\r
1395 if (m == NULL)\r
1396 return;\r
1397\r
1398 if (garbage == NULL) {\r
1399 garbage = PyList_New(0);\r
1400 if (garbage == NULL)\r
1401 return;\r
1402 }\r
1403 Py_INCREF(garbage);\r
1404 if (PyModule_AddObject(m, "garbage", garbage) < 0)\r
1405 return;\r
1406\r
1407 /* Importing can't be done in collect() because collect()\r
1408 * can be called via PyGC_Collect() in Py_Finalize().\r
1409 * This wouldn't be a problem, except that <initialized> is\r
1410 * reset to 0 before calling collect which trips up\r
1411 * the import and triggers an assertion.\r
1412 */\r
1413 if (tmod == NULL) {\r
1414 tmod = PyImport_ImportModuleNoBlock("time");\r
1415 if (tmod == NULL)\r
1416 PyErr_Clear();\r
1417 }\r
1418\r
1419#define ADD_INT(NAME) if (PyModule_AddIntConstant(m, #NAME, NAME) < 0) return\r
1420 ADD_INT(DEBUG_STATS);\r
1421 ADD_INT(DEBUG_COLLECTABLE);\r
1422 ADD_INT(DEBUG_UNCOLLECTABLE);\r
1423 ADD_INT(DEBUG_INSTANCES);\r
1424 ADD_INT(DEBUG_OBJECTS);\r
1425 ADD_INT(DEBUG_SAVEALL);\r
1426 ADD_INT(DEBUG_LEAK);\r
1427#undef ADD_INT\r
1428}\r
1429\r
1430/* API to invoke gc.collect() from C */\r
1431Py_ssize_t\r
1432PyGC_Collect(void)\r
1433{\r
1434 Py_ssize_t n;\r
1435\r
1436 if (collecting)\r
1437 n = 0; /* already collecting, don't do anything */\r
1438 else {\r
1439 collecting = 1;\r
1440 n = collect(NUM_GENERATIONS - 1);\r
1441 collecting = 0;\r
1442 }\r
1443\r
1444 return n;\r
1445}\r
1446\r
1447/* for debugging */\r
1448void\r
1449_PyGC_Dump(PyGC_Head *g)\r
1450{\r
1451 _PyObject_Dump(FROM_GC(g));\r
1452}\r
1453\r
1454/* extension modules might be compiled with GC support so these\r
1455 functions must always be available */\r
1456\r
1457#undef PyObject_GC_Track\r
1458#undef PyObject_GC_UnTrack\r
1459#undef PyObject_GC_Del\r
1460#undef _PyObject_GC_Malloc\r
1461\r
1462void\r
1463PyObject_GC_Track(void *op)\r
1464{\r
1465 _PyObject_GC_TRACK(op);\r
1466}\r
1467\r
1468/* for binary compatibility with 2.2 */\r
1469void\r
1470_PyObject_GC_Track(PyObject *op)\r
1471{\r
1472 PyObject_GC_Track(op);\r
1473}\r
1474\r
1475void\r
1476PyObject_GC_UnTrack(void *op)\r
1477{\r
1478 /* Obscure: the Py_TRASHCAN mechanism requires that we be able to\r
1479 * call PyObject_GC_UnTrack twice on an object.\r
1480 */\r
1481 if (IS_TRACKED(op))\r
1482 _PyObject_GC_UNTRACK(op);\r
1483}\r
1484\r
1485/* for binary compatibility with 2.2 */\r
1486void\r
1487_PyObject_GC_UnTrack(PyObject *op)\r
1488{\r
1489 PyObject_GC_UnTrack(op);\r
1490}\r
1491\r
1492PyObject *\r
1493_PyObject_GC_Malloc(size_t basicsize)\r
1494{\r
1495 PyObject *op;\r
1496 PyGC_Head *g;\r
1497 if (basicsize > PY_SSIZE_T_MAX - sizeof(PyGC_Head))\r
1498 return PyErr_NoMemory();\r
1499 g = (PyGC_Head *)PyObject_MALLOC(\r
1500 sizeof(PyGC_Head) + basicsize);\r
1501 if (g == NULL)\r
1502 return PyErr_NoMemory();\r
1503 g->gc.gc_refs = GC_UNTRACKED;\r
1504 generations[0].count++; /* number of allocated GC objects */\r
1505 if (generations[0].count > generations[0].threshold &&\r
1506 enabled &&\r
1507 generations[0].threshold &&\r
1508 !collecting &&\r
1509 !PyErr_Occurred()) {\r
1510 collecting = 1;\r
1511 collect_generations();\r
1512 collecting = 0;\r
1513 }\r
1514 op = FROM_GC(g);\r
1515 return op;\r
1516}\r
1517\r
1518PyObject *\r
1519_PyObject_GC_New(PyTypeObject *tp)\r
1520{\r
1521 PyObject *op = _PyObject_GC_Malloc(_PyObject_SIZE(tp));\r
1522 if (op != NULL)\r
1523 op = PyObject_INIT(op, tp);\r
1524 return op;\r
1525}\r
1526\r
1527PyVarObject *\r
1528_PyObject_GC_NewVar(PyTypeObject *tp, Py_ssize_t nitems)\r
1529{\r
1530 const size_t size = _PyObject_VAR_SIZE(tp, nitems);\r
1531 PyVarObject *op = (PyVarObject *) _PyObject_GC_Malloc(size);\r
1532 if (op != NULL)\r
1533 op = PyObject_INIT_VAR(op, tp, nitems);\r
1534 return op;\r
1535}\r
1536\r
1537PyVarObject *\r
1538_PyObject_GC_Resize(PyVarObject *op, Py_ssize_t nitems)\r
1539{\r
1540 const size_t basicsize = _PyObject_VAR_SIZE(Py_TYPE(op), nitems);\r
1541 PyGC_Head *g = AS_GC(op);\r
1542 if (basicsize > PY_SSIZE_T_MAX - sizeof(PyGC_Head))\r
1543 return (PyVarObject *)PyErr_NoMemory();\r
1544 g = (PyGC_Head *)PyObject_REALLOC(g, sizeof(PyGC_Head) + basicsize);\r
1545 if (g == NULL)\r
1546 return (PyVarObject *)PyErr_NoMemory();\r
1547 op = (PyVarObject *) FROM_GC(g);\r
1548 Py_SIZE(op) = nitems;\r
1549 return op;\r
1550}\r
1551\r
1552void\r
1553PyObject_GC_Del(void *op)\r
1554{\r
1555 PyGC_Head *g = AS_GC(op);\r
1556 if (IS_TRACKED(op))\r
1557 gc_list_remove(g);\r
1558 if (generations[0].count > 0) {\r
1559 generations[0].count--;\r
1560 }\r
1561 PyObject_FREE(g);\r
1562}\r
1563\r
1564/* for binary compatibility with 2.2 */\r
1565#undef _PyObject_GC_Del\r
1566void\r
1567_PyObject_GC_Del(PyObject *op)\r
1568{\r
1569 PyObject_GC_Del(op);\r
1570}\r