blob: b258a0f6e11806be3553b848c405e171b40680d8 [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"
Victor Stinner27e2d1f2018-11-01 00:52:28 +010027#include "pycore_context.h"
Victor Stinnerbcda8f12018-11-21 22:27:47 +010028#include "pycore_object.h"
Victor Stinner621cebe2018-11-12 16:53:38 +010029#include "pycore_pymem.h"
30#include "pycore_pystate.h"
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000031#include "frameobject.h" /* for PyFrame_ClearFreeList */
Łukasz Langaa785c872016-09-09 17:37:37 -070032#include "pydtrace.h"
Victor Stinner7181dec2015-03-27 17:47:53 +010033#include "pytime.h" /* for _PyTime_GetMonotonicClock() */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000034
Serhiy Storchaka93260282017-02-04 11:19:59 +020035/*[clinic input]
36module gc
37[clinic start generated code]*/
38/*[clinic end generated code: output=da39a3ee5e6b4b0d input=b5c9690ecc842d79]*/
39
Pablo Galindo320dd502019-10-10 22:45:17 +010040
41#ifdef Py_DEBUG
42# define GC_DEBUG
43#endif
INADA Naoki5ac9e6e2018-07-10 17:19:53 +090044
45#define GC_NEXT _PyGCHead_NEXT
46#define GC_PREV _PyGCHead_PREV
47
48// update_refs() set this bit for all objects in current generation.
49// subtract_refs() and move_unreachable() uses this to distinguish
50// visited object is in GCing or not.
51//
52// move_unreachable() removes this flag from reachable objects.
53// Only unreachable objects have this flag.
54//
55// No objects in interpreter have this flag after GC ends.
56#define PREV_MASK_COLLECTING _PyGC_PREV_MASK_COLLECTING
57
58// Lowest bit of _gc_next is used for UNREACHABLE flag.
59//
60// This flag represents the object is in unreachable list in move_unreachable()
61//
62// Although this flag is used only in move_unreachable(), move_unreachable()
63// doesn't clear this flag to skip unnecessary iteration.
64// move_legacy_finalizers() removes this flag instead.
65// Between them, unreachable list is not normal list and we can not use
66// most gc_list_* functions for it.
67#define NEXT_MASK_UNREACHABLE (1)
68
Victor Stinner626bff82018-10-25 17:31:10 +020069/* Get an object's GC head */
70#define AS_GC(o) ((PyGC_Head *)(o)-1)
71
72/* Get the object given the GC head */
73#define FROM_GC(g) ((PyObject *)(((PyGC_Head *)g)+1))
74
INADA Naoki5ac9e6e2018-07-10 17:19:53 +090075static inline int
76gc_is_collecting(PyGC_Head *g)
77{
78 return (g->_gc_prev & PREV_MASK_COLLECTING) != 0;
79}
80
81static inline void
82gc_clear_collecting(PyGC_Head *g)
83{
84 g->_gc_prev &= ~PREV_MASK_COLLECTING;
85}
86
87static inline Py_ssize_t
88gc_get_refs(PyGC_Head *g)
89{
90 return (Py_ssize_t)(g->_gc_prev >> _PyGC_PREV_SHIFT);
91}
92
93static inline void
94gc_set_refs(PyGC_Head *g, Py_ssize_t refs)
95{
96 g->_gc_prev = (g->_gc_prev & ~_PyGC_PREV_MASK)
97 | ((uintptr_t)(refs) << _PyGC_PREV_SHIFT);
98}
99
100static inline void
101gc_reset_refs(PyGC_Head *g, Py_ssize_t refs)
102{
103 g->_gc_prev = (g->_gc_prev & _PyGC_PREV_MASK_FINALIZED)
104 | PREV_MASK_COLLECTING
105 | ((uintptr_t)(refs) << _PyGC_PREV_SHIFT);
106}
107
108static inline void
109gc_decref(PyGC_Head *g)
110{
Victor Stinner626bff82018-10-25 17:31:10 +0200111 _PyObject_ASSERT_WITH_MSG(FROM_GC(g),
112 gc_get_refs(g) > 0,
113 "refcount is too small");
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900114 g->_gc_prev -= 1 << _PyGC_PREV_SHIFT;
115}
116
Tim Peters6fc13d92002-07-02 18:12:35 +0000117/* Python string to use if unhandled exception occurs */
Tim Petersbf384c22003-04-06 00:11:39 +0000118static PyObject *gc_str = NULL;
Tim Peters6fc13d92002-07-02 18:12:35 +0000119
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000120/* set for debugging information */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121#define DEBUG_STATS (1<<0) /* print collection statistics */
122#define DEBUG_COLLECTABLE (1<<1) /* print collectable objects */
123#define DEBUG_UNCOLLECTABLE (1<<2) /* print uncollectable objects */
124#define DEBUG_SAVEALL (1<<5) /* save all garbage in gc.garbage */
125#define DEBUG_LEAK DEBUG_COLLECTABLE | \
126 DEBUG_UNCOLLECTABLE | \
127 DEBUG_SAVEALL
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000128
Victor Stinner9db03242019-04-26 02:32:01 +0200129#define GEN_HEAD(state, n) (&(state)->generations[n].head)
Antoine Pitroud4156c12012-10-30 22:43:19 +0100130
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600131void
132_PyGC_Initialize(struct _gc_runtime_state *state)
133{
134 state->enabled = 1; /* automatic collection enabled? */
135
Victor Stinner9db03242019-04-26 02:32:01 +0200136#define _GEN_HEAD(n) GEN_HEAD(state, n)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600137 struct gc_generation generations[NUM_GENERATIONS] = {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900138 /* PyGC_Head, threshold, count */
139 {{(uintptr_t)_GEN_HEAD(0), (uintptr_t)_GEN_HEAD(0)}, 700, 0},
140 {{(uintptr_t)_GEN_HEAD(1), (uintptr_t)_GEN_HEAD(1)}, 10, 0},
141 {{(uintptr_t)_GEN_HEAD(2), (uintptr_t)_GEN_HEAD(2)}, 10, 0},
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600142 };
143 for (int i = 0; i < NUM_GENERATIONS; i++) {
144 state->generations[i] = generations[i];
145 };
Victor Stinner9db03242019-04-26 02:32:01 +0200146 state->generation0 = GEN_HEAD(state, 0);
brainfvckc75edab2017-10-16 12:49:41 -0700147 struct gc_generation permanent_generation = {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900148 {(uintptr_t)&state->permanent_generation.head,
149 (uintptr_t)&state->permanent_generation.head}, 0, 0
brainfvckc75edab2017-10-16 12:49:41 -0700150 };
151 state->permanent_generation = permanent_generation;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600152}
Antoine Pitroud4156c12012-10-30 22:43:19 +0100153
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900154/*
155_gc_prev values
156---------------
Neil Schemenauer43411b52001-08-30 00:05:51 +0000157
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900158Between collections, _gc_prev is used for doubly linked list.
Tim Peters6fc13d92002-07-02 18:12:35 +0000159
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900160Lowest two bits of _gc_prev are used for flags.
161PREV_MASK_COLLECTING is used only while collecting and cleared before GC ends
162or _PyObject_GC_UNTRACK() is called.
Tim Peters6fc13d92002-07-02 18:12:35 +0000163
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900164During a collection, _gc_prev is temporary used for gc_refs, and the gc list
165is singly linked until _gc_prev is restored.
Tim Peters6fc13d92002-07-02 18:12:35 +0000166
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900167gc_refs
Tim Peters6fc13d92002-07-02 18:12:35 +0000168 At the start of a collection, update_refs() copies the true refcount
169 to gc_refs, for each object in the generation being collected.
170 subtract_refs() then adjusts gc_refs so that it equals the number of
171 times an object is referenced directly from outside the generation
172 being collected.
Tim Peters6fc13d92002-07-02 18:12:35 +0000173
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900174PREV_MASK_COLLECTING
175 Objects in generation being collected are marked PREV_MASK_COLLECTING in
176 update_refs().
177
178
179_gc_next values
180---------------
181
182_gc_next takes these values:
183
1840
185 The object is not tracked
186
187!= 0
188 Pointer to the next object in the GC list.
189 Additionally, lowest bit is used temporary for
190 NEXT_MASK_UNREACHABLE flag described below.
191
192NEXT_MASK_UNREACHABLE
Tim Peters6fc13d92002-07-02 18:12:35 +0000193 move_unreachable() then moves objects not reachable (whether directly or
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900194 indirectly) from outside the generation into an "unreachable" set and
195 set this flag.
Tim Peters6fc13d92002-07-02 18:12:35 +0000196
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900197 Objects that are found to be reachable have gc_refs set to 1.
198 When this flag is set for the reachable object, the object must be in
199 "unreachable" set.
200 The flag is unset and the object is moved back to "reachable" set.
201
202 move_legacy_finalizers() will remove this flag from "unreachable" set.
Tim Peters6fc13d92002-07-02 18:12:35 +0000203*/
Neil Schemenauera2b11ec2002-05-21 15:53:24 +0000204
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000205/*** list functions ***/
206
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900207static inline void
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000208gc_list_init(PyGC_Head *list)
209{
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900210 // List header must not have flags.
211 // We can assign pointer by simple cast.
212 list->_gc_prev = (uintptr_t)list;
213 list->_gc_next = (uintptr_t)list;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000214}
215
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900216static inline int
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000217gc_list_is_empty(PyGC_Head *list)
218{
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900219 return (list->_gc_next == (uintptr_t)list);
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000220}
221
Tim Peterse2d59182004-11-01 01:39:08 +0000222/* Append `node` to `list`. */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900223static inline void
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000224gc_list_append(PyGC_Head *node, PyGC_Head *list)
225{
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900226 PyGC_Head *last = (PyGC_Head *)list->_gc_prev;
227
228 // last <-> node
229 _PyGCHead_SET_PREV(node, last);
230 _PyGCHead_SET_NEXT(last, node);
231
232 // node <-> list
233 _PyGCHead_SET_NEXT(node, list);
234 list->_gc_prev = (uintptr_t)node;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000235}
236
Tim Peterse2d59182004-11-01 01:39:08 +0000237/* Remove `node` from the gc list it's currently in. */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900238static inline void
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000239gc_list_remove(PyGC_Head *node)
240{
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900241 PyGC_Head *prev = GC_PREV(node);
242 PyGC_Head *next = GC_NEXT(node);
243
244 _PyGCHead_SET_NEXT(prev, next);
245 _PyGCHead_SET_PREV(next, prev);
246
247 node->_gc_next = 0; /* object is not currently tracked */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000248}
249
Tim Peterse2d59182004-11-01 01:39:08 +0000250/* Move `node` from the gc list it's currently in (which is not explicitly
251 * named here) to the end of `list`. This is semantically the same as
252 * gc_list_remove(node) followed by gc_list_append(node, list).
253 */
254static void
255gc_list_move(PyGC_Head *node, PyGC_Head *list)
256{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 /* Unlink from current list. */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900258 PyGC_Head *from_prev = GC_PREV(node);
259 PyGC_Head *from_next = GC_NEXT(node);
260 _PyGCHead_SET_NEXT(from_prev, from_next);
261 _PyGCHead_SET_PREV(from_next, from_prev);
262
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 /* Relink at end of new list. */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900264 // list must not have flags. So we can skip macros.
265 PyGC_Head *to_prev = (PyGC_Head*)list->_gc_prev;
266 _PyGCHead_SET_PREV(node, to_prev);
267 _PyGCHead_SET_NEXT(to_prev, node);
268 list->_gc_prev = (uintptr_t)node;
269 _PyGCHead_SET_NEXT(node, list);
Tim Peterse2d59182004-11-01 01:39:08 +0000270}
271
272/* append list `from` onto list `to`; `from` becomes an empty list */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000273static void
274gc_list_merge(PyGC_Head *from, PyGC_Head *to)
275{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000276 assert(from != to);
277 if (!gc_list_is_empty(from)) {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900278 PyGC_Head *to_tail = GC_PREV(to);
279 PyGC_Head *from_head = GC_NEXT(from);
280 PyGC_Head *from_tail = GC_PREV(from);
281 assert(from_head != from);
282 assert(from_tail != from);
283
284 _PyGCHead_SET_NEXT(to_tail, from_head);
285 _PyGCHead_SET_PREV(from_head, to_tail);
286
287 _PyGCHead_SET_NEXT(from_tail, to);
288 _PyGCHead_SET_PREV(to, from_tail);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000289 }
290 gc_list_init(from);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000291}
292
Neal Norwitz7b216c52006-03-04 20:01:53 +0000293static Py_ssize_t
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000294gc_list_size(PyGC_Head *list)
295{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000296 PyGC_Head *gc;
297 Py_ssize_t n = 0;
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900298 for (gc = GC_NEXT(list); gc != list; gc = GC_NEXT(gc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 n++;
300 }
301 return n;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000302}
303
Pablo Galindo466326d2019-10-13 16:48:59 +0100304/* Walk the list and mark all objects as non-collecting */
305static inline void
306gc_list_clear_collecting(PyGC_Head *collectable)
307{
308 PyGC_Head *gc;
309 for (gc = GC_NEXT(collectable); gc != collectable; gc = GC_NEXT(gc)) {
310 gc_clear_collecting(gc);
311 }
312}
313
Tim Peters259272b2003-04-06 19:41:39 +0000314/* Append objects in a GC list to a Python list.
Pablo Galindo466326d2019-10-13 16:48:59 +0100315 * Return 0 if all OK, < 0 if error (out of memory for list)
Tim Peters259272b2003-04-06 19:41:39 +0000316 */
317static int
318append_objects(PyObject *py_list, PyGC_Head *gc_list)
319{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000320 PyGC_Head *gc;
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900321 for (gc = GC_NEXT(gc_list); gc != gc_list; gc = GC_NEXT(gc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000322 PyObject *op = FROM_GC(gc);
323 if (op != py_list) {
324 if (PyList_Append(py_list, op)) {
325 return -1; /* exception */
326 }
327 }
328 }
329 return 0;
Tim Peters259272b2003-04-06 19:41:39 +0000330}
331
Tim Petersea55c512019-10-18 20:59:14 -0500332// Constants for validate_list's flags argument.
333enum flagstates {collecting_clear_unreachable_clear,
334 collecting_clear_unreachable_set,
335 collecting_set_unreachable_clear,
336 collecting_set_unreachable_set};
337
Pablo Galindo320dd502019-10-10 22:45:17 +0100338#ifdef GC_DEBUG
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900339// validate_list checks list consistency. And it works as document
Tim Peters95bfc8a2019-10-13 16:47:04 -0500340// describing when flags are expected to be set / unset.
341// `head` must be a doubly-linked gc list, although it's fine (expected!) if
342// the prev and next pointers are "polluted" with flags.
343// What's checked:
344// - The `head` pointers are not polluted.
Tim Petersea55c512019-10-18 20:59:14 -0500345// - The objects' PREV_MASK_COLLECTING and NEXT_MASK_UNREACHABLE flags are all
346// `set or clear, as specified by the 'flags' argument.
Tim Peters95bfc8a2019-10-13 16:47:04 -0500347// - The prev and next pointers are mutually consistent.
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900348static void
Tim Petersea55c512019-10-18 20:59:14 -0500349validate_list(PyGC_Head *head, enum flagstates flags)
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900350{
Tim Peters95bfc8a2019-10-13 16:47:04 -0500351 assert((head->_gc_prev & PREV_MASK_COLLECTING) == 0);
352 assert((head->_gc_next & NEXT_MASK_UNREACHABLE) == 0);
Tim Petersea55c512019-10-18 20:59:14 -0500353 uintptr_t prev_value = 0, next_value = 0;
354 switch (flags) {
355 case collecting_clear_unreachable_clear:
356 break;
357 case collecting_set_unreachable_clear:
358 prev_value = PREV_MASK_COLLECTING;
359 break;
360 case collecting_clear_unreachable_set:
361 next_value = NEXT_MASK_UNREACHABLE;
362 break;
363 case collecting_set_unreachable_set:
364 prev_value = PREV_MASK_COLLECTING;
365 next_value = NEXT_MASK_UNREACHABLE;
366 break;
367 default:
368 assert(! "bad internal flags argument");
369 }
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900370 PyGC_Head *prev = head;
371 PyGC_Head *gc = GC_NEXT(head);
372 while (gc != head) {
Tim Peters95bfc8a2019-10-13 16:47:04 -0500373 PyGC_Head *trueprev = GC_PREV(gc);
374 PyGC_Head *truenext = (PyGC_Head *)(gc->_gc_next & ~NEXT_MASK_UNREACHABLE);
375 assert(truenext != NULL);
376 assert(trueprev == prev);
377 assert((gc->_gc_prev & PREV_MASK_COLLECTING) == prev_value);
378 assert((gc->_gc_next & NEXT_MASK_UNREACHABLE) == next_value);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900379 prev = gc;
Tim Peters95bfc8a2019-10-13 16:47:04 -0500380 gc = truenext;
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900381 }
382 assert(prev == GC_PREV(head));
383}
384#else
Tim Petersea55c512019-10-18 20:59:14 -0500385#define validate_list(x, y) do{}while(0)
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900386#endif
387
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000388/*** end of list stuff ***/
389
390
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900391/* Set all gc_refs = ob_refcnt. After this, gc_refs is > 0 and
392 * PREV_MASK_COLLECTING bit is set for all objects in containers.
Tim Peters88396172002-06-30 17:56:40 +0000393 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000394static void
395update_refs(PyGC_Head *containers)
396{
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900397 PyGC_Head *gc = GC_NEXT(containers);
398 for (; gc != containers; gc = GC_NEXT(gc)) {
399 gc_reset_refs(gc, Py_REFCNT(FROM_GC(gc)));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 /* Python's cyclic gc should never see an incoming refcount
401 * of 0: if something decref'ed to 0, it should have been
402 * deallocated immediately at that time.
403 * Possible cause (if the assert triggers): a tp_dealloc
404 * routine left a gc-aware object tracked during its teardown
405 * phase, and did something-- or allowed something to happen --
406 * that called back into Python. gc can trigger then, and may
407 * see the still-tracked dying object. Before this assert
408 * was added, such mistakes went on to allow gc to try to
409 * delete the object again. In a debug build, that caused
410 * a mysterious segfault, when _Py_ForgetReference tried
411 * to remove the object from the doubly-linked list of all
412 * objects a second time. In a release build, an actual
413 * double deallocation occurred, which leads to corruption
414 * of the allocator's internal bookkeeping pointers. That's
415 * so serious that maybe this should be a release-build
416 * check instead of an assert?
417 */
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200418 _PyObject_ASSERT(FROM_GC(gc), gc_get_refs(gc) != 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000419 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000420}
421
Tim Peters19b74c72002-07-01 03:52:19 +0000422/* A traversal callback for subtract_refs. */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000423static int
Victor Stinner4d5f94b2019-10-08 02:37:38 +0200424visit_decref(PyObject *op, void *parent)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000425{
Victor Stinner4d5f94b2019-10-08 02:37:38 +0200426 _PyObject_ASSERT(_PyObject_CAST(parent), !_PyObject_IsFreed(op));
Victor Stinnerd91d4de2019-09-09 17:44:59 +0200427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 if (PyObject_IS_GC(op)) {
429 PyGC_Head *gc = AS_GC(op);
430 /* We're only interested in gc_refs for objects in the
431 * generation being collected, which can be recognized
432 * because only they have positive gc_refs.
433 */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900434 if (gc_is_collecting(gc)) {
435 gc_decref(gc);
436 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000437 }
438 return 0;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000439}
440
Tim Peters19b74c72002-07-01 03:52:19 +0000441/* Subtract internal references from gc_refs. After this, gc_refs is >= 0
442 * for all objects in containers, and is GC_REACHABLE for all tracked gc
443 * objects not in containers. The ones with gc_refs > 0 are directly
444 * reachable from outside containers, and so can't be collected.
445 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000446static void
447subtract_refs(PyGC_Head *containers)
448{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 traverseproc traverse;
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900450 PyGC_Head *gc = GC_NEXT(containers);
451 for (; gc != containers; gc = GC_NEXT(gc)) {
Victor Stinner4d5f94b2019-10-08 02:37:38 +0200452 PyObject *op = FROM_GC(gc);
453 traverse = Py_TYPE(op)->tp_traverse;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 (void) traverse(FROM_GC(gc),
455 (visitproc)visit_decref,
Victor Stinner4d5f94b2019-10-08 02:37:38 +0200456 op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000458}
459
Tim Peters19b74c72002-07-01 03:52:19 +0000460/* A traversal callback for move_unreachable. */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000461static int
Tim Peters19b74c72002-07-01 03:52:19 +0000462visit_reachable(PyObject *op, PyGC_Head *reachable)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000463{
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900464 if (!PyObject_IS_GC(op)) {
465 return 0;
466 }
Tim Peters19b74c72002-07-01 03:52:19 +0000467
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900468 PyGC_Head *gc = AS_GC(op);
469 const Py_ssize_t gc_refs = gc_get_refs(gc);
470
Tim Peters1e739452019-10-21 11:21:35 -0500471 // Ignore objects in other generation.
Tim Peters95bfc8a2019-10-13 16:47:04 -0500472 // This also skips objects "to the left" of the current position in
473 // move_unreachable's scan of the 'young' list - they've already been
474 // traversed, and no longer have the PREV_MASK_COLLECTING flag.
Tim Peters1e739452019-10-21 11:21:35 -0500475 if (! gc_is_collecting(gc)) {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900476 return 0;
477 }
Tim Peters1e739452019-10-21 11:21:35 -0500478 // It would be a logic error elsewhere if the collecting flag were set on
479 // an untracked object.
480 assert(gc->_gc_next != 0);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900481
482 if (gc->_gc_next & NEXT_MASK_UNREACHABLE) {
483 /* This had gc_refs = 0 when move_unreachable got
484 * to it, but turns out it's reachable after all.
485 * Move it back to move_unreachable's 'young' list,
486 * and move_unreachable will eventually get to it
487 * again.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 */
Tim Peters95bfc8a2019-10-13 16:47:04 -0500489 // Manually unlink gc from unreachable list because the list functions
490 // don't work right in the presence of NEXT_MASK_UNREACHABLE flags.
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900491 PyGC_Head *prev = GC_PREV(gc);
492 PyGC_Head *next = (PyGC_Head*)(gc->_gc_next & ~NEXT_MASK_UNREACHABLE);
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200493 _PyObject_ASSERT(FROM_GC(prev),
494 prev->_gc_next & NEXT_MASK_UNREACHABLE);
495 _PyObject_ASSERT(FROM_GC(next),
496 next->_gc_next & NEXT_MASK_UNREACHABLE);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900497 prev->_gc_next = gc->_gc_next; // copy NEXT_MASK_UNREACHABLE
498 _PyGCHead_SET_PREV(next, prev);
499
500 gc_list_append(gc, reachable);
501 gc_set_refs(gc, 1);
502 }
503 else if (gc_refs == 0) {
504 /* This is in move_unreachable's 'young' list, but
505 * the traversal hasn't yet gotten to it. All
506 * we need to do is tell move_unreachable that it's
507 * reachable.
508 */
509 gc_set_refs(gc, 1);
510 }
511 /* Else there's nothing to do.
512 * If gc_refs > 0, it must be in move_unreachable's 'young'
513 * list, and move_unreachable will eventually get to it.
514 */
515 else {
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200516 _PyObject_ASSERT_WITH_MSG(op, gc_refs > 0, "refcount is too small");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000517 }
518 return 0;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000519}
520
Tim Peters19b74c72002-07-01 03:52:19 +0000521/* Move the unreachable objects from young to unreachable. After this,
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900522 * all objects in young don't have PREV_MASK_COLLECTING flag and
523 * unreachable have the flag.
Tim Peters19b74c72002-07-01 03:52:19 +0000524 * All objects in young after this are directly or indirectly reachable
525 * from outside the original young; and all objects in unreachable are
526 * not.
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900527 *
528 * This function restores _gc_prev pointer. young and unreachable are
529 * doubly linked list after this function.
530 * But _gc_next in unreachable list has NEXT_MASK_UNREACHABLE flag.
531 * So we can not gc_list_* functions for unreachable until we remove the flag.
Tim Peters88396172002-06-30 17:56:40 +0000532 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000533static void
Tim Peters19b74c72002-07-01 03:52:19 +0000534move_unreachable(PyGC_Head *young, PyGC_Head *unreachable)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000535{
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900536 // previous elem in the young list, used for restore gc_prev.
537 PyGC_Head *prev = young;
538 PyGC_Head *gc = GC_NEXT(young);
Tim Peters19b74c72002-07-01 03:52:19 +0000539
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900540 /* Invariants: all objects "to the left" of us in young are reachable
541 * (directly or indirectly) from outside the young list as it was at entry.
542 *
543 * All other objects from the original young "to the left" of us are in
544 * unreachable now, and have NEXT_MASK_UNREACHABLE. All objects to the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 * left of us in 'young' now have been scanned, and no objects here
546 * or to the right have been scanned yet.
547 */
Tim Peters19b74c72002-07-01 03:52:19 +0000548
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 while (gc != young) {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900550 if (gc_get_refs(gc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 /* gc is definitely reachable from outside the
552 * original 'young'. Mark it as such, and traverse
553 * its pointers to find any other objects that may
554 * be directly reachable from it. Note that the
555 * call to tp_traverse may append objects to young,
556 * so we have to wait until it returns to determine
557 * the next object to visit.
558 */
559 PyObject *op = FROM_GC(gc);
560 traverseproc traverse = Py_TYPE(op)->tp_traverse;
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200561 _PyObject_ASSERT_WITH_MSG(op, gc_get_refs(gc) > 0,
562 "refcount is too small");
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900563 // NOTE: visit_reachable may change gc->_gc_next when
564 // young->_gc_prev == gc. Don't do gc = GC_NEXT(gc) before!
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 (void) traverse(op,
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900566 (visitproc)visit_reachable,
567 (void *)young);
568 // relink gc_prev to prev element.
569 _PyGCHead_SET_PREV(gc, prev);
Quan Tian3bd0d622018-10-20 05:30:03 +0800570 // gc is not COLLECTING state after here.
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900571 gc_clear_collecting(gc);
572 prev = gc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 }
574 else {
575 /* This *may* be unreachable. To make progress,
576 * assume it is. gc isn't directly reachable from
577 * any object we've already traversed, but may be
578 * reachable from an object we haven't gotten to yet.
579 * visit_reachable will eventually move gc back into
580 * young if that's so, and we'll see it again.
581 */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900582 // Move gc to unreachable.
583 // No need to gc->next->prev = prev because it is single linked.
584 prev->_gc_next = gc->_gc_next;
585
586 // We can't use gc_list_append() here because we use
587 // NEXT_MASK_UNREACHABLE here.
588 PyGC_Head *last = GC_PREV(unreachable);
589 // NOTE: Since all objects in unreachable set has
590 // NEXT_MASK_UNREACHABLE flag, we set it unconditionally.
Tim Peters95bfc8a2019-10-13 16:47:04 -0500591 // But this may pollute the unreachable list head's 'next' pointer
592 // too. That's semantically senseless but expedient here - the
593 // damage is repaired when this fumction ends.
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900594 last->_gc_next = (NEXT_MASK_UNREACHABLE | (uintptr_t)gc);
595 _PyGCHead_SET_PREV(gc, last);
596 gc->_gc_next = (NEXT_MASK_UNREACHABLE | (uintptr_t)unreachable);
597 unreachable->_gc_prev = (uintptr_t)gc;
598 }
599 gc = (PyGC_Head*)prev->_gc_next;
600 }
601 // young->_gc_prev must be last element remained in the list.
602 young->_gc_prev = (uintptr_t)prev;
Tim Peters95bfc8a2019-10-13 16:47:04 -0500603 // don't let the pollution of the list head's next pointer leak
604 unreachable->_gc_next &= ~NEXT_MASK_UNREACHABLE;
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900605}
606
607static void
608untrack_tuples(PyGC_Head *head)
609{
610 PyGC_Head *next, *gc = GC_NEXT(head);
611 while (gc != head) {
612 PyObject *op = FROM_GC(gc);
613 next = GC_NEXT(gc);
614 if (PyTuple_CheckExact(op)) {
615 _PyTuple_MaybeUntrack(op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616 }
617 gc = next;
618 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000619}
620
Antoine Pitroue1ad3da2012-05-28 22:22:34 +0200621/* Try to untrack all currently tracked dictionaries */
622static void
623untrack_dicts(PyGC_Head *head)
624{
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900625 PyGC_Head *next, *gc = GC_NEXT(head);
Antoine Pitroue1ad3da2012-05-28 22:22:34 +0200626 while (gc != head) {
627 PyObject *op = FROM_GC(gc);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900628 next = GC_NEXT(gc);
629 if (PyDict_CheckExact(op)) {
Antoine Pitroue1ad3da2012-05-28 22:22:34 +0200630 _PyDict_MaybeUntrack(op);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900631 }
Antoine Pitroue1ad3da2012-05-28 22:22:34 +0200632 gc = next;
633 }
634}
635
Antoine Pitrou796564c2013-07-30 19:59:21 +0200636/* Return true if object has a pre-PEP 442 finalization method. */
Neil Schemenauera765c122001-11-01 17:35:23 +0000637static int
Antoine Pitrou796564c2013-07-30 19:59:21 +0200638has_legacy_finalizer(PyObject *op)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000639{
Antoine Pitrou796564c2013-07-30 19:59:21 +0200640 return op->ob_type->tp_del != NULL;
Neil Schemenauera765c122001-11-01 17:35:23 +0000641}
642
Antoine Pitrou796564c2013-07-30 19:59:21 +0200643/* Move the objects in unreachable with tp_del slots into `finalizers`.
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900644 *
645 * This function also removes NEXT_MASK_UNREACHABLE flag
646 * from _gc_next in unreachable.
Jeremy Hyltonce136e92003-04-04 19:59:06 +0000647 */
Neil Schemenauera765c122001-11-01 17:35:23 +0000648static void
Antoine Pitrou796564c2013-07-30 19:59:21 +0200649move_legacy_finalizers(PyGC_Head *unreachable, PyGC_Head *finalizers)
Neil Schemenauera765c122001-11-01 17:35:23 +0000650{
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900651 PyGC_Head *gc, *next;
Tim Peters95bfc8a2019-10-13 16:47:04 -0500652 assert((unreachable->_gc_next & NEXT_MASK_UNREACHABLE) == 0);
Tim Petersf6b80452003-04-07 19:21:15 +0000653
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000654 /* March over unreachable. Move objects with finalizers into
655 * `finalizers`.
656 */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900657 for (gc = GC_NEXT(unreachable); gc != unreachable; gc = next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 PyObject *op = FROM_GC(gc);
Jeremy Hyltonce136e92003-04-04 19:59:06 +0000659
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200660 _PyObject_ASSERT(op, gc->_gc_next & NEXT_MASK_UNREACHABLE);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900661 gc->_gc_next &= ~NEXT_MASK_UNREACHABLE;
662 next = (PyGC_Head*)gc->_gc_next;
Tim Petersf6ae7a42003-04-05 18:40:50 +0000663
Antoine Pitrou796564c2013-07-30 19:59:21 +0200664 if (has_legacy_finalizer(op)) {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900665 gc_clear_collecting(gc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000666 gc_list_move(gc, finalizers);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000667 }
668 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000669}
670
Pablo Galindo466326d2019-10-13 16:48:59 +0100671static inline void
672clear_unreachable_mask(PyGC_Head *unreachable)
673{
674 /* Check that the list head does not have the unreachable bit set */
675 assert(((uintptr_t)unreachable & NEXT_MASK_UNREACHABLE) == 0);
676
677 PyGC_Head *gc, *next;
Tim Peters95bfc8a2019-10-13 16:47:04 -0500678 assert((unreachable->_gc_next & NEXT_MASK_UNREACHABLE) == 0);
Pablo Galindo466326d2019-10-13 16:48:59 +0100679 for (gc = GC_NEXT(unreachable); gc != unreachable; gc = next) {
680 _PyObject_ASSERT((PyObject*)FROM_GC(gc), gc->_gc_next & NEXT_MASK_UNREACHABLE);
681 gc->_gc_next &= ~NEXT_MASK_UNREACHABLE;
682 next = (PyGC_Head*)gc->_gc_next;
683 }
Tim Petersea55c512019-10-18 20:59:14 -0500684 validate_list(unreachable, collecting_set_unreachable_clear);
Pablo Galindo466326d2019-10-13 16:48:59 +0100685}
686
Antoine Pitrou796564c2013-07-30 19:59:21 +0200687/* A traversal callback for move_legacy_finalizer_reachable. */
Tim Peters19b74c72002-07-01 03:52:19 +0000688static int
689visit_move(PyObject *op, PyGC_Head *tolist)
690{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000691 if (PyObject_IS_GC(op)) {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900692 PyGC_Head *gc = AS_GC(op);
693 if (gc_is_collecting(gc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000694 gc_list_move(gc, tolist);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900695 gc_clear_collecting(gc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 }
697 }
698 return 0;
Tim Peters19b74c72002-07-01 03:52:19 +0000699}
700
701/* Move objects that are reachable from finalizers, from the unreachable set
Tim Petersf6b80452003-04-07 19:21:15 +0000702 * into finalizers set.
Tim Peters19b74c72002-07-01 03:52:19 +0000703 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000704static void
Antoine Pitrou796564c2013-07-30 19:59:21 +0200705move_legacy_finalizer_reachable(PyGC_Head *finalizers)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000706{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000707 traverseproc traverse;
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900708 PyGC_Head *gc = GC_NEXT(finalizers);
709 for (; gc != finalizers; gc = GC_NEXT(gc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000710 /* Note that the finalizers list may grow during this. */
711 traverse = Py_TYPE(FROM_GC(gc))->tp_traverse;
712 (void) traverse(FROM_GC(gc),
713 (visitproc)visit_move,
714 (void *)finalizers);
715 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000716}
717
Tim Petersead8b7a2004-10-30 23:09:22 +0000718/* Clear all weakrefs to unreachable objects, and if such a weakref has a
719 * callback, invoke it if necessary. Note that it's possible for such
720 * weakrefs to be outside the unreachable set -- indeed, those are precisely
721 * the weakrefs whose callbacks must be invoked. See gc_weakref.txt for
722 * overview & some details. Some weakrefs with callbacks may be reclaimed
723 * directly by this routine; the number reclaimed is the return value. Other
724 * weakrefs with callbacks may be moved into the `old` generation. Objects
725 * moved into `old` have gc_refs set to GC_REACHABLE; the objects remaining in
726 * unreachable are left at GC_TENTATIVELY_UNREACHABLE. When this returns,
727 * no object in `unreachable` is weakly referenced anymore.
Tim Peters403a2032003-11-20 21:21:46 +0000728 */
729static int
Tim Petersead8b7a2004-10-30 23:09:22 +0000730handle_weakrefs(PyGC_Head *unreachable, PyGC_Head *old)
Tim Peters403a2032003-11-20 21:21:46 +0000731{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000732 PyGC_Head *gc;
733 PyObject *op; /* generally FROM_GC(gc) */
734 PyWeakReference *wr; /* generally a cast of op */
735 PyGC_Head wrcb_to_call; /* weakrefs with callbacks to call */
736 PyGC_Head *next;
737 int num_freed = 0;
Tim Peters403a2032003-11-20 21:21:46 +0000738
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000739 gc_list_init(&wrcb_to_call);
Tim Peters403a2032003-11-20 21:21:46 +0000740
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 /* Clear all weakrefs to the objects in unreachable. If such a weakref
742 * also has a callback, move it into `wrcb_to_call` if the callback
743 * needs to be invoked. Note that we cannot invoke any callbacks until
744 * all weakrefs to unreachable objects are cleared, lest the callback
745 * resurrect an unreachable object via a still-active weakref. We
746 * make another pass over wrcb_to_call, invoking callbacks, after this
747 * pass completes.
748 */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900749 for (gc = GC_NEXT(unreachable); gc != unreachable; gc = next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000750 PyWeakReference **wrlist;
Tim Petersead8b7a2004-10-30 23:09:22 +0000751
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 op = FROM_GC(gc);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900753 next = GC_NEXT(gc);
Tim Petersead8b7a2004-10-30 23:09:22 +0000754
Neil Schemenauerbcda4602019-09-30 10:06:45 -0700755 if (PyWeakref_Check(op)) {
756 /* A weakref inside the unreachable set must be cleared. If we
757 * allow its callback to execute inside delete_garbage(), it
758 * could expose objects that have tp_clear already called on
759 * them. Or, it could resurrect unreachable objects. One way
760 * this can happen is if some container objects do not implement
761 * tp_traverse. Then, wr_object can be outside the unreachable
762 * set but can be deallocated as a result of breaking the
763 * reference cycle. If we don't clear the weakref, the callback
764 * will run and potentially cause a crash. See bpo-38006 for
765 * one example.
766 */
767 _PyWeakref_ClearRef((PyWeakReference *)op);
768 }
769
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770 if (! PyType_SUPPORTS_WEAKREFS(Py_TYPE(op)))
771 continue;
Tim Petersead8b7a2004-10-30 23:09:22 +0000772
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000773 /* It supports weakrefs. Does it have any? */
774 wrlist = (PyWeakReference **)
775 PyObject_GET_WEAKREFS_LISTPTR(op);
Tim Petersead8b7a2004-10-30 23:09:22 +0000776
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000777 /* `op` may have some weakrefs. March over the list, clear
778 * all the weakrefs, and move the weakrefs with callbacks
779 * that must be called into wrcb_to_call.
780 */
781 for (wr = *wrlist; wr != NULL; wr = *wrlist) {
782 PyGC_Head *wrasgc; /* AS_GC(wr) */
Tim Petersead8b7a2004-10-30 23:09:22 +0000783
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 /* _PyWeakref_ClearRef clears the weakref but leaves
785 * the callback pointer intact. Obscure: it also
786 * changes *wrlist.
787 */
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200788 _PyObject_ASSERT((PyObject *)wr, wr->wr_object == op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000789 _PyWeakref_ClearRef(wr);
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200790 _PyObject_ASSERT((PyObject *)wr, wr->wr_object == Py_None);
791 if (wr->wr_callback == NULL) {
792 /* no callback */
793 continue;
794 }
Tim Petersead8b7a2004-10-30 23:09:22 +0000795
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200796 /* Headache time. `op` is going away, and is weakly referenced by
797 * `wr`, which has a callback. Should the callback be invoked? If wr
798 * is also trash, no:
799 *
800 * 1. There's no need to call it. The object and the weakref are
801 * both going away, so it's legitimate to pretend the weakref is
802 * going away first. The user has to ensure a weakref outlives its
803 * referent if they want a guarantee that the wr callback will get
804 * invoked.
805 *
806 * 2. It may be catastrophic to call it. If the callback is also in
807 * cyclic trash (CT), then although the CT is unreachable from
808 * outside the current generation, CT may be reachable from the
809 * callback. Then the callback could resurrect insane objects.
810 *
811 * Since the callback is never needed and may be unsafe in this case,
812 * wr is simply left in the unreachable set. Note that because we
813 * already called _PyWeakref_ClearRef(wr), its callback will never
814 * trigger.
815 *
816 * OTOH, if wr isn't part of CT, we should invoke the callback: the
817 * weakref outlived the trash. Note that since wr isn't CT in this
818 * case, its callback can't be CT either -- wr acted as an external
819 * root to this generation, and therefore its callback did too. So
820 * nothing in CT is reachable from the callback either, so it's hard
821 * to imagine how calling it later could create a problem for us. wr
822 * is moved to wrcb_to_call in this case.
823 */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900824 if (gc_is_collecting(AS_GC(wr))) {
Neil Schemenauerbcda4602019-09-30 10:06:45 -0700825 /* it should already have been cleared above */
826 assert(wr->wr_object == Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000827 continue;
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900828 }
Tim Peterscc2a8662004-10-31 22:12:43 +0000829
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 /* Create a new reference so that wr can't go away
831 * before we can process it again.
832 */
833 Py_INCREF(wr);
Tim Petersead8b7a2004-10-30 23:09:22 +0000834
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000835 /* Move wr to wrcb_to_call, for the next pass. */
836 wrasgc = AS_GC(wr);
837 assert(wrasgc != next); /* wrasgc is reachable, but
838 next isn't, so they can't
839 be the same */
840 gc_list_move(wrasgc, &wrcb_to_call);
841 }
842 }
Tim Petersead8b7a2004-10-30 23:09:22 +0000843
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 /* Invoke the callbacks we decided to honor. It's safe to invoke them
845 * because they can't reference unreachable objects.
846 */
847 while (! gc_list_is_empty(&wrcb_to_call)) {
848 PyObject *temp;
849 PyObject *callback;
Tim Petersead8b7a2004-10-30 23:09:22 +0000850
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900851 gc = (PyGC_Head*)wrcb_to_call._gc_next;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 op = FROM_GC(gc);
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200853 _PyObject_ASSERT(op, PyWeakref_Check(op));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 wr = (PyWeakReference *)op;
855 callback = wr->wr_callback;
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200856 _PyObject_ASSERT(op, callback != NULL);
Tim Petersead8b7a2004-10-30 23:09:22 +0000857
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858 /* copy-paste of weakrefobject.c's handle_callback() */
Jeroen Demeyer196a5302019-07-04 12:31:34 +0200859 temp = _PyObject_CallOneArg(callback, (PyObject *)wr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 if (temp == NULL)
861 PyErr_WriteUnraisable(callback);
862 else
863 Py_DECREF(temp);
Tim Petersead8b7a2004-10-30 23:09:22 +0000864
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 /* Give up the reference we created in the first pass. When
866 * op's refcount hits 0 (which it may or may not do right now),
867 * op's tp_dealloc will decref op->wr_callback too. Note
868 * that the refcount probably will hit 0 now, and because this
869 * weakref was reachable to begin with, gc didn't already
870 * add it to its count of freed objects. Example: a reachable
871 * weak value dict maps some key to this reachable weakref.
872 * The callback removes this key->weakref mapping from the
873 * dict, leaving no other references to the weakref (excepting
874 * ours).
875 */
876 Py_DECREF(op);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900877 if (wrcb_to_call._gc_next == (uintptr_t)gc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 /* object is still alive -- move it */
879 gc_list_move(gc, old);
880 }
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900881 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 ++num_freed;
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900883 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000884 }
Tim Petersead8b7a2004-10-30 23:09:22 +0000885
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 return num_freed;
Tim Peters403a2032003-11-20 21:21:46 +0000887}
888
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000889static void
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200890debug_cycle(const char *msg, PyObject *op)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000891{
Victor Stinner499dfcf2011-03-21 13:26:24 +0100892 PySys_FormatStderr("gc: %s <%s %p>\n",
893 msg, Py_TYPE(op)->tp_name, op);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000894}
895
Antoine Pitrou796564c2013-07-30 19:59:21 +0200896/* Handle uncollectable garbage (cycles with tp_del slots, and stuff reachable
Tim Petersbf384c22003-04-06 00:11:39 +0000897 * only from such cycles).
Tim Petersf6b80452003-04-07 19:21:15 +0000898 * If DEBUG_SAVEALL, all objects in finalizers are appended to the module
899 * garbage list (a Python list), else only the objects in finalizers with
900 * __del__ methods are appended to garbage. All objects in finalizers are
901 * merged into the old list regardless.
Tim Petersbf384c22003-04-06 00:11:39 +0000902 */
Serhiy Storchaka301e3cc2018-05-24 15:19:29 +0300903static void
Victor Stinner9db03242019-04-26 02:32:01 +0200904handle_legacy_finalizers(struct _gc_runtime_state *state,
905 PyGC_Head *finalizers, PyGC_Head *old)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000906{
Serhiy Storchakac4653c92018-05-29 18:50:10 +0300907 assert(!PyErr_Occurred());
Victor Stinner9db03242019-04-26 02:32:01 +0200908
909 PyGC_Head *gc = GC_NEXT(finalizers);
910 if (state->garbage == NULL) {
911 state->garbage = PyList_New(0);
912 if (state->garbage == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 Py_FatalError("gc couldn't create gc.garbage list");
914 }
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900915 for (; gc != finalizers; gc = GC_NEXT(gc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 PyObject *op = FROM_GC(gc);
Tim Petersf6b80452003-04-07 19:21:15 +0000917
Victor Stinner9db03242019-04-26 02:32:01 +0200918 if ((state->debug & DEBUG_SAVEALL) || has_legacy_finalizer(op)) {
919 if (PyList_Append(state->garbage, op) < 0) {
Serhiy Storchakac4653c92018-05-29 18:50:10 +0300920 PyErr_Clear();
Serhiy Storchaka301e3cc2018-05-24 15:19:29 +0300921 break;
Serhiy Storchakac4653c92018-05-29 18:50:10 +0300922 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 }
924 }
Tim Petersf6b80452003-04-07 19:21:15 +0000925
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 gc_list_merge(finalizers, old);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000927}
928
Tim Peters5fbc7b12014-05-08 17:42:19 -0500929/* Run first-time finalizers (if any) on all the objects in collectable.
930 * Note that this may remove some (or even all) of the objects from the
931 * list, due to refcounts falling to 0.
932 */
Antoine Pitrou796564c2013-07-30 19:59:21 +0200933static void
Tim Peters5fbc7b12014-05-08 17:42:19 -0500934finalize_garbage(PyGC_Head *collectable)
Antoine Pitrou796564c2013-07-30 19:59:21 +0200935{
936 destructor finalize;
Tim Peters5fbc7b12014-05-08 17:42:19 -0500937 PyGC_Head seen;
Antoine Pitrou796564c2013-07-30 19:59:21 +0200938
Tim Peters5fbc7b12014-05-08 17:42:19 -0500939 /* While we're going through the loop, `finalize(op)` may cause op, or
940 * other objects, to be reclaimed via refcounts falling to zero. So
941 * there's little we can rely on about the structure of the input
942 * `collectable` list across iterations. For safety, we always take the
943 * first object in that list and move it to a temporary `seen` list.
944 * If objects vanish from the `collectable` and `seen` lists we don't
945 * care.
946 */
947 gc_list_init(&seen);
948
949 while (!gc_list_is_empty(collectable)) {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900950 PyGC_Head *gc = GC_NEXT(collectable);
Antoine Pitrou796564c2013-07-30 19:59:21 +0200951 PyObject *op = FROM_GC(gc);
Tim Peters5fbc7b12014-05-08 17:42:19 -0500952 gc_list_move(gc, &seen);
Antoine Pitrou796564c2013-07-30 19:59:21 +0200953 if (!_PyGCHead_FINALIZED(gc) &&
Tim Peters5fbc7b12014-05-08 17:42:19 -0500954 (finalize = Py_TYPE(op)->tp_finalize) != NULL) {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900955 _PyGCHead_SET_FINALIZED(gc);
Antoine Pitrou796564c2013-07-30 19:59:21 +0200956 Py_INCREF(op);
957 finalize(op);
Serhiy Storchakac4653c92018-05-29 18:50:10 +0300958 assert(!PyErr_Occurred());
Antoine Pitrou796564c2013-07-30 19:59:21 +0200959 Py_DECREF(op);
960 }
961 }
Tim Peters5fbc7b12014-05-08 17:42:19 -0500962 gc_list_merge(&seen, collectable);
Antoine Pitrou796564c2013-07-30 19:59:21 +0200963}
964
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965/* Break reference cycles by clearing the containers involved. This is
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000966 * tricky business as the lists can be changing and we don't know which
Tim Peters19b74c72002-07-01 03:52:19 +0000967 * objects may be freed. It is possible I screwed something up here.
968 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000969static void
Victor Stinner9db03242019-04-26 02:32:01 +0200970delete_garbage(struct _gc_runtime_state *state,
971 PyGC_Head *collectable, PyGC_Head *old)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000972{
Serhiy Storchakac4653c92018-05-29 18:50:10 +0300973 assert(!PyErr_Occurred());
Victor Stinner9db03242019-04-26 02:32:01 +0200974
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000975 while (!gc_list_is_empty(collectable)) {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900976 PyGC_Head *gc = GC_NEXT(collectable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 PyObject *op = FROM_GC(gc);
Tim Peters88396172002-06-30 17:56:40 +0000978
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200979 _PyObject_ASSERT_WITH_MSG(op, Py_REFCNT(op) > 0,
980 "refcount is too small");
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900981
Victor Stinner9db03242019-04-26 02:32:01 +0200982 if (state->debug & DEBUG_SAVEALL) {
983 assert(state->garbage != NULL);
984 if (PyList_Append(state->garbage, op) < 0) {
Serhiy Storchakac4653c92018-05-29 18:50:10 +0300985 PyErr_Clear();
986 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 }
988 else {
Victor Stinner9db03242019-04-26 02:32:01 +0200989 inquiry clear;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 if ((clear = Py_TYPE(op)->tp_clear) != NULL) {
991 Py_INCREF(op);
Serhiy Storchakac4653c92018-05-29 18:50:10 +0300992 (void) clear(op);
993 if (PyErr_Occurred()) {
Victor Stinner71c52e32019-05-27 08:57:14 +0200994 _PyErr_WriteUnraisableMsg("in tp_clear of",
995 (PyObject*)Py_TYPE(op));
Serhiy Storchakac4653c92018-05-29 18:50:10 +0300996 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 Py_DECREF(op);
998 }
999 }
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09001000 if (GC_NEXT(collectable) == gc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 /* object is still alive, move it, it may die later */
Pablo Galindo466326d2019-10-13 16:48:59 +01001002 gc_clear_collecting(gc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001003 gc_list_move(gc, old);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001004 }
1005 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001006}
1007
Christian Heimesa156e092008-02-16 07:38:31 +00001008/* Clear all free lists
1009 * All free lists are cleared during the collection of the highest generation.
1010 * Allocated items in the free list may keep a pymalloc arena occupied.
1011 * Clearing the free lists may give back memory to the OS earlier.
1012 */
1013static void
1014clear_freelists(void)
1015{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 (void)PyMethod_ClearFreeList();
1017 (void)PyFrame_ClearFreeList();
1018 (void)PyCFunction_ClearFreeList();
1019 (void)PyTuple_ClearFreeList();
1020 (void)PyUnicode_ClearFreeList();
1021 (void)PyFloat_ClearFreeList();
Antoine Pitrou9a812cb2011-11-15 00:00:12 +01001022 (void)PyList_ClearFreeList();
1023 (void)PyDict_ClearFreeList();
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001024 (void)PySet_ClearFreeList();
Yury Selivanoveb636452016-09-08 22:01:51 -07001025 (void)PyAsyncGen_ClearFreeLists();
Yury Selivanovf23746a2018-01-22 19:11:18 -05001026 (void)PyContext_ClearFreeList();
Christian Heimesa156e092008-02-16 07:38:31 +00001027}
1028
Inada Naokibf8162c2019-08-02 16:25:29 +09001029// Show stats for objects in each gennerations.
1030static void
1031show_stats_each_generations(struct _gc_runtime_state *state)
1032{
1033 char buf[100];
1034 size_t pos = 0;
1035
1036 for (int i = 0; i < NUM_GENERATIONS && pos < sizeof(buf); i++) {
1037 pos += PyOS_snprintf(buf+pos, sizeof(buf)-pos,
1038 " %"PY_FORMAT_SIZE_T"d",
1039 gc_list_size(GEN_HEAD(state, i)));
1040 }
1041
1042 PySys_FormatStderr(
1043 "gc: objects in each generation:%s\n"
1044 "gc: objects in permanent generation: %zd\n",
1045 buf, gc_list_size(&state->permanent_generation.head));
1046}
1047
Pablo Galindo466326d2019-10-13 16:48:59 +01001048/* Deduce wich objects among "base" are unreachable from outside the list
1049 and move them to 'unreachable'. The process consist in the following steps:
1050
10511. Copy all reference counts to a different field (gc_prev is used to hold
1052 this copy to save memory).
10532. Traverse all objects in "base" and visit all referred objects using
1054 "tp_traverse" and for every visited object, substract 1 to the reference
1055 count (the one that we copied in the previous step). After this step, all
1056 objects that can be reached directly from outside must have strictly positive
1057 reference count, while all unreachable objects must have a count of exactly 0.
10583. Indentify all unreachable objects (the ones with 0 reference count) and move
1059 them to the "unreachable" list. This step also needs to move back to "base" all
1060 objects that were initially marked as unreachable but are referred transitively
1061 by the reachable objects (the ones with strictly positive reference count).
1062
1063Contracts:
1064
1065 * The "base" has to be a valid list with no mask set.
1066
1067 * The "unreachable" list must be uninitialized (this function calls
1068 gc_list_init over 'unreachable').
Tim Peters95bfc8a2019-10-13 16:47:04 -05001069
Pablo Galindo466326d2019-10-13 16:48:59 +01001070IMPORTANT: This function leaves 'unreachable' with the NEXT_MASK_UNREACHABLE
1071flag set but it does not clear it to skip unnecessary iteration. Before the
1072flag is cleared (for example, by using 'clear_unreachable_mask' function or
1073by a call to 'move_legacy_finalizers'), the 'unreachable' list is not a normal
1074list and we can not use most gc_list_* functions for it. */
1075static inline void
1076deduce_unreachable(PyGC_Head *base, PyGC_Head *unreachable) {
Tim Petersea55c512019-10-18 20:59:14 -05001077 validate_list(base, collecting_clear_unreachable_clear);
Pablo Galindo466326d2019-10-13 16:48:59 +01001078 /* Using ob_refcnt and gc_refs, calculate which objects in the
1079 * container set are reachable from outside the set (i.e., have a
1080 * refcount greater than 0 when all the references within the
1081 * set are taken into account).
1082 */
1083 update_refs(base); // gc_prev is used for gc_refs
1084 subtract_refs(base);
1085
1086 /* Leave everything reachable from outside base in base, and move
1087 * everything else (in base) to unreachable.
1088 * NOTE: This used to move the reachable objects into a reachable
1089 * set instead. But most things usually turn out to be reachable,
Tim Petersd9d39932019-11-02 12:06:31 -05001090 * so it's more efficient to move the unreachable things. See note
1091 ^ [REACHABLE OR UNREACHABLE?} at the file end.
Pablo Galindo466326d2019-10-13 16:48:59 +01001092 */
1093 gc_list_init(unreachable);
1094 move_unreachable(base, unreachable); // gc_prev is pointer again
Tim Petersea55c512019-10-18 20:59:14 -05001095 validate_list(base, collecting_clear_unreachable_clear);
1096 validate_list(unreachable, collecting_set_unreachable_set);
Pablo Galindo466326d2019-10-13 16:48:59 +01001097}
1098
1099/* Handle objects that may have resurrected after a call to 'finalize_garbage', moving
1100 them to 'old_generation' and placing the rest on 'still_unreachable'.
1101
1102 Contracts:
1103 * After this function 'unreachable' must not be used anymore and 'still_unreachable'
1104 will contain the objects that did not resurrect.
1105
1106 * The "still_unreachable" list must be uninitialized (this function calls
1107 gc_list_init over 'still_unreachable').
1108
1109IMPORTANT: After a call to this function, the 'still_unreachable' set will have the
1110PREV_MARK_COLLECTING set, but the objects in this set are going to be removed so
1111we can skip the expense of clearing the flag to avoid extra iteration. */
1112static inline void
1113handle_resurrected_objects(PyGC_Head *unreachable, PyGC_Head* still_unreachable,
1114 PyGC_Head *old_generation)
1115{
1116 // Remove the PREV_MASK_COLLECTING from unreachable
1117 // to prepare it for a new call to 'deduce_unreachable'
1118 gc_list_clear_collecting(unreachable);
1119
1120 // After the call to deduce_unreachable, the 'still_unreachable' set will
1121 // have the PREV_MARK_COLLECTING set, but the objects are going to be
1122 // removed so we can skip the expense of clearing the flag.
1123 PyGC_Head* resurrected = unreachable;
1124 deduce_unreachable(resurrected, still_unreachable);
1125 clear_unreachable_mask(still_unreachable);
1126
1127 // Move the resurrected objects to the old generation for future collection.
1128 gc_list_merge(resurrected, old_generation);
1129}
1130
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001131/* This is the main function. Read this to understand how the
1132 * collection process works. */
Neal Norwitz7b216c52006-03-04 20:01:53 +00001133static Py_ssize_t
Victor Stinner9db03242019-04-26 02:32:01 +02001134collect(struct _gc_runtime_state *state, int generation,
1135 Py_ssize_t *n_collected, Py_ssize_t *n_uncollectable, int nofail)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001136{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001137 int i;
1138 Py_ssize_t m = 0; /* # objects collected */
1139 Py_ssize_t n = 0; /* # unreachable objects that couldn't be collected */
1140 PyGC_Head *young; /* the generation we are examining */
1141 PyGC_Head *old; /* next older generation */
1142 PyGC_Head unreachable; /* non-problematic unreachable trash */
1143 PyGC_Head finalizers; /* objects with, & reachable from, __del__ */
1144 PyGC_Head *gc;
Victor Stinner7181dec2015-03-27 17:47:53 +01001145 _PyTime_t t1 = 0; /* initialize to prevent a compiler warning */
Antoine Pitrou40f6b122014-05-24 19:21:53 +02001146
Victor Stinner9db03242019-04-26 02:32:01 +02001147 if (state->debug & DEBUG_STATS) {
Inada Naokibf8162c2019-08-02 16:25:29 +09001148 PySys_WriteStderr("gc: collecting generation %d...\n", generation);
1149 show_stats_each_generations(state);
Victor Stinner7181dec2015-03-27 17:47:53 +01001150 t1 = _PyTime_GetMonotonicClock();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001151 }
Neil Schemenauer2880ae52002-05-04 05:35:20 +00001152
Łukasz Langaa785c872016-09-09 17:37:37 -07001153 if (PyDTrace_GC_START_ENABLED())
1154 PyDTrace_GC_START(generation);
1155
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001156 /* update collection and allocation counters */
1157 if (generation+1 < NUM_GENERATIONS)
Victor Stinner9db03242019-04-26 02:32:01 +02001158 state->generations[generation+1].count += 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001159 for (i = 0; i <= generation; i++)
Victor Stinner9db03242019-04-26 02:32:01 +02001160 state->generations[i].count = 0;
Neil Schemenauer2880ae52002-05-04 05:35:20 +00001161
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001162 /* merge younger generations with one we are currently collecting */
1163 for (i = 0; i < generation; i++) {
Victor Stinner9db03242019-04-26 02:32:01 +02001164 gc_list_merge(GEN_HEAD(state, i), GEN_HEAD(state, generation));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001165 }
Neil Schemenauer2880ae52002-05-04 05:35:20 +00001166
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 /* handy references */
Victor Stinner9db03242019-04-26 02:32:01 +02001168 young = GEN_HEAD(state, generation);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001169 if (generation < NUM_GENERATIONS-1)
Victor Stinner9db03242019-04-26 02:32:01 +02001170 old = GEN_HEAD(state, generation+1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001171 else
1172 old = young;
Tim Petersea55c512019-10-18 20:59:14 -05001173 validate_list(old, collecting_clear_unreachable_clear);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001174
Pablo Galindo466326d2019-10-13 16:48:59 +01001175 deduce_unreachable(young, &unreachable);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001176
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09001177 untrack_tuples(young);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001178 /* Move reachable objects to next generation. */
1179 if (young != old) {
1180 if (generation == NUM_GENERATIONS - 2) {
Victor Stinner9db03242019-04-26 02:32:01 +02001181 state->long_lived_pending += gc_list_size(young);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001182 }
1183 gc_list_merge(young, old);
1184 }
1185 else {
Antoine Pitroue1ad3da2012-05-28 22:22:34 +02001186 /* We only untrack dicts in full collections, to avoid quadratic
1187 dict build-up. See issue #14775. */
1188 untrack_dicts(young);
Victor Stinner9db03242019-04-26 02:32:01 +02001189 state->long_lived_pending = 0;
1190 state->long_lived_total = gc_list_size(young);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001191 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001192
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001193 /* All objects in unreachable are trash, but objects reachable from
Antoine Pitrou796564c2013-07-30 19:59:21 +02001194 * legacy finalizers (e.g. tp_del) can't safely be deleted.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001195 */
1196 gc_list_init(&finalizers);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09001197 // NEXT_MASK_UNREACHABLE is cleared here.
1198 // After move_legacy_finalizers(), unreachable is normal list.
Antoine Pitrou796564c2013-07-30 19:59:21 +02001199 move_legacy_finalizers(&unreachable, &finalizers);
1200 /* finalizers contains the unreachable objects with a legacy finalizer;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001201 * unreachable objects reachable *from* those are also uncollectable,
1202 * and we move those into the finalizers list too.
1203 */
Antoine Pitrou796564c2013-07-30 19:59:21 +02001204 move_legacy_finalizer_reachable(&finalizers);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001205
Tim Petersea55c512019-10-18 20:59:14 -05001206 validate_list(&finalizers, collecting_clear_unreachable_clear);
1207 validate_list(&unreachable, collecting_set_unreachable_clear);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09001208
Tim Petersecbf35f2019-10-09 12:37:30 -05001209 /* Print debugging information. */
1210 if (state->debug & DEBUG_COLLECTABLE) {
1211 for (gc = GC_NEXT(&unreachable); gc != &unreachable; gc = GC_NEXT(gc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001212 debug_cycle("collectable", FROM_GC(gc));
1213 }
1214 }
Tim Petersead8b7a2004-10-30 23:09:22 +00001215
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 /* Clear weakrefs and invoke callbacks as necessary. */
1217 m += handle_weakrefs(&unreachable, old);
Tim Petersead8b7a2004-10-30 23:09:22 +00001218
Tim Petersea55c512019-10-18 20:59:14 -05001219 validate_list(old, collecting_clear_unreachable_clear);
1220 validate_list(&unreachable, collecting_set_unreachable_clear);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09001221
Antoine Pitrou796564c2013-07-30 19:59:21 +02001222 /* Call tp_finalize on objects which have one. */
Tim Peters5fbc7b12014-05-08 17:42:19 -05001223 finalize_garbage(&unreachable);
Antoine Pitrou796564c2013-07-30 19:59:21 +02001224
Pablo Galindo466326d2019-10-13 16:48:59 +01001225 /* Handle any objects that may have resurrected after the call
1226 * to 'finalize_garbage' and continue the collection with the
1227 * objects that are still unreachable */
1228 PyGC_Head final_unreachable;
1229 handle_resurrected_objects(&unreachable, &final_unreachable, old);
1230
1231 /* Call tp_clear on objects in the final_unreachable set. This will cause
1232 * the reference cycles to be broken. It may also cause some objects
1233 * in finalizers to be freed.
1234 */
1235 m += gc_list_size(&final_unreachable);
1236 delete_garbage(state, &final_unreachable, old);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001237
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001238 /* Collect statistics on uncollectable objects found and print
1239 * debugging information. */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09001240 for (gc = GC_NEXT(&finalizers); gc != &finalizers; gc = GC_NEXT(gc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 n++;
Victor Stinner9db03242019-04-26 02:32:01 +02001242 if (state->debug & DEBUG_UNCOLLECTABLE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001243 debug_cycle("uncollectable", FROM_GC(gc));
1244 }
Victor Stinner9db03242019-04-26 02:32:01 +02001245 if (state->debug & DEBUG_STATS) {
Inada Naokibf8162c2019-08-02 16:25:29 +09001246 double d = _PyTime_AsSecondsDouble(_PyTime_GetMonotonicClock() - t1);
Inada Naoki013e52f2019-08-31 09:13:42 +09001247 PySys_WriteStderr(
1248 "gc: done, %" PY_FORMAT_SIZE_T "d unreachable, "
1249 "%" PY_FORMAT_SIZE_T "d uncollectable, %.4fs elapsed\n",
Inada Naokibf8162c2019-08-02 16:25:29 +09001250 n+m, n, d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001252
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 /* Append instances in the uncollectable set to a Python
1254 * reachable list of garbage. The programmer has to deal with
1255 * this if they insist on creating this type of structure.
1256 */
Victor Stinner9db03242019-04-26 02:32:01 +02001257 handle_legacy_finalizers(state, &finalizers, old);
Tim Petersea55c512019-10-18 20:59:14 -05001258 validate_list(old, collecting_clear_unreachable_clear);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001259
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001260 /* Clear free list only during the collection of the highest
1261 * generation */
1262 if (generation == NUM_GENERATIONS-1) {
1263 clear_freelists();
1264 }
Christian Heimesa156e092008-02-16 07:38:31 +00001265
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001266 if (PyErr_Occurred()) {
Antoine Pitroufef34e32013-05-19 01:11:58 +02001267 if (nofail) {
1268 PyErr_Clear();
1269 }
1270 else {
1271 if (gc_str == NULL)
1272 gc_str = PyUnicode_FromString("garbage collection");
1273 PyErr_WriteUnraisable(gc_str);
1274 Py_FatalError("unexpected exception during garbage collection");
1275 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001276 }
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001277
Antoine Pitroud4156c12012-10-30 22:43:19 +01001278 /* Update stats */
Victor Stinner9db03242019-04-26 02:32:01 +02001279 if (n_collected) {
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001280 *n_collected = m;
Victor Stinner9db03242019-04-26 02:32:01 +02001281 }
1282 if (n_uncollectable) {
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001283 *n_uncollectable = n;
Victor Stinner9db03242019-04-26 02:32:01 +02001284 }
1285
1286 struct gc_generation_stats *stats = &state->generation_stats[generation];
Antoine Pitroud4156c12012-10-30 22:43:19 +01001287 stats->collections++;
1288 stats->collected += m;
1289 stats->uncollectable += n;
Łukasz Langaa785c872016-09-09 17:37:37 -07001290
Victor Stinner9db03242019-04-26 02:32:01 +02001291 if (PyDTrace_GC_DONE_ENABLED()) {
Łukasz Langaa785c872016-09-09 17:37:37 -07001292 PyDTrace_GC_DONE(n+m);
Victor Stinner9db03242019-04-26 02:32:01 +02001293 }
Łukasz Langaa785c872016-09-09 17:37:37 -07001294
Serhiy Storchakac4653c92018-05-29 18:50:10 +03001295 assert(!PyErr_Occurred());
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001296 return n+m;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001297}
1298
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001299/* Invoke progress callbacks to notify clients that garbage collection
1300 * is starting or stopping
1301 */
1302static void
Victor Stinner9db03242019-04-26 02:32:01 +02001303invoke_gc_callback(struct _gc_runtime_state *state, const char *phase,
1304 int generation, Py_ssize_t collected,
1305 Py_ssize_t uncollectable)
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001306{
Serhiy Storchakac4653c92018-05-29 18:50:10 +03001307 assert(!PyErr_Occurred());
Victor Stinner9db03242019-04-26 02:32:01 +02001308
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001309 /* we may get called very early */
Victor Stinner9db03242019-04-26 02:32:01 +02001310 if (state->callbacks == NULL) {
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001311 return;
Victor Stinner9db03242019-04-26 02:32:01 +02001312 }
1313
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001314 /* The local variable cannot be rebound, check it for sanity */
Victor Stinner9db03242019-04-26 02:32:01 +02001315 assert(PyList_CheckExact(state->callbacks));
1316 PyObject *info = NULL;
1317 if (PyList_GET_SIZE(state->callbacks) != 0) {
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001318 info = Py_BuildValue("{sisnsn}",
1319 "generation", generation,
1320 "collected", collected,
1321 "uncollectable", uncollectable);
1322 if (info == NULL) {
1323 PyErr_WriteUnraisable(NULL);
1324 return;
1325 }
1326 }
Victor Stinner9db03242019-04-26 02:32:01 +02001327 for (Py_ssize_t i=0; i<PyList_GET_SIZE(state->callbacks); i++) {
1328 PyObject *r, *cb = PyList_GET_ITEM(state->callbacks, i);
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001329 Py_INCREF(cb); /* make sure cb doesn't go away */
1330 r = PyObject_CallFunction(cb, "sO", phase, info);
Serhiy Storchaka301e3cc2018-05-24 15:19:29 +03001331 if (r == NULL) {
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001332 PyErr_WriteUnraisable(cb);
Serhiy Storchaka301e3cc2018-05-24 15:19:29 +03001333 }
1334 else {
1335 Py_DECREF(r);
1336 }
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001337 Py_DECREF(cb);
1338 }
1339 Py_XDECREF(info);
Serhiy Storchakac4653c92018-05-29 18:50:10 +03001340 assert(!PyErr_Occurred());
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001341}
1342
1343/* Perform garbage collection of a generation and invoke
1344 * progress callbacks.
1345 */
1346static Py_ssize_t
Victor Stinner9db03242019-04-26 02:32:01 +02001347collect_with_callback(struct _gc_runtime_state *state, int generation)
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001348{
Serhiy Storchakac4653c92018-05-29 18:50:10 +03001349 assert(!PyErr_Occurred());
Victor Stinner9db03242019-04-26 02:32:01 +02001350 Py_ssize_t result, collected, uncollectable;
1351 invoke_gc_callback(state, "start", generation, 0, 0);
1352 result = collect(state, generation, &collected, &uncollectable, 0);
1353 invoke_gc_callback(state, "stop", generation, collected, uncollectable);
Serhiy Storchakac4653c92018-05-29 18:50:10 +03001354 assert(!PyErr_Occurred());
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001355 return result;
1356}
1357
Neal Norwitz7b216c52006-03-04 20:01:53 +00001358static Py_ssize_t
Victor Stinner9db03242019-04-26 02:32:01 +02001359collect_generations(struct _gc_runtime_state *state)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001360{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001361 /* Find the oldest generation (highest numbered) where the count
1362 * exceeds the threshold. Objects in the that generation and
1363 * generations younger than it will be collected. */
Victor Stinner9db03242019-04-26 02:32:01 +02001364 Py_ssize_t n = 0;
1365 for (int i = NUM_GENERATIONS-1; i >= 0; i--) {
1366 if (state->generations[i].count > state->generations[i].threshold) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001367 /* Avoid quadratic performance degradation in number
1368 of tracked objects. See comments at the beginning
1369 of this file, and issue #4074.
1370 */
1371 if (i == NUM_GENERATIONS - 1
Victor Stinner9db03242019-04-26 02:32:01 +02001372 && state->long_lived_pending < state->long_lived_total / 4)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001373 continue;
Victor Stinner9db03242019-04-26 02:32:01 +02001374 n = collect_with_callback(state, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001375 break;
1376 }
1377 }
1378 return n;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001379}
1380
Serhiy Storchaka93260282017-02-04 11:19:59 +02001381#include "clinic/gcmodule.c.h"
1382
1383/*[clinic input]
1384gc.enable
1385
1386Enable automatic garbage collection.
1387[clinic start generated code]*/
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001388
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001389static PyObject *
Serhiy Storchaka93260282017-02-04 11:19:59 +02001390gc_enable_impl(PyObject *module)
1391/*[clinic end generated code: output=45a427e9dce9155c input=81ac4940ca579707]*/
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001392{
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001393 _PyRuntime.gc.enabled = 1;
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001394 Py_RETURN_NONE;
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001395}
1396
Serhiy Storchaka93260282017-02-04 11:19:59 +02001397/*[clinic input]
1398gc.disable
1399
1400Disable automatic garbage collection.
1401[clinic start generated code]*/
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001402
1403static PyObject *
Serhiy Storchaka93260282017-02-04 11:19:59 +02001404gc_disable_impl(PyObject *module)
1405/*[clinic end generated code: output=97d1030f7aa9d279 input=8c2e5a14e800d83b]*/
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001406{
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001407 _PyRuntime.gc.enabled = 0;
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001408 Py_RETURN_NONE;
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001409}
1410
Serhiy Storchaka93260282017-02-04 11:19:59 +02001411/*[clinic input]
1412gc.isenabled -> bool
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001413
Serhiy Storchaka93260282017-02-04 11:19:59 +02001414Returns true if automatic garbage collection is enabled.
1415[clinic start generated code]*/
1416
1417static int
1418gc_isenabled_impl(PyObject *module)
1419/*[clinic end generated code: output=1874298331c49130 input=30005e0422373b31]*/
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001420{
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001421 return _PyRuntime.gc.enabled;
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001422}
1423
Serhiy Storchaka93260282017-02-04 11:19:59 +02001424/*[clinic input]
1425gc.collect -> Py_ssize_t
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001426
Serhiy Storchaka93260282017-02-04 11:19:59 +02001427 generation: int(c_default="NUM_GENERATIONS - 1") = 2
1428
1429Run the garbage collector.
1430
1431With no arguments, run a full collection. The optional argument
1432may be an integer specifying which generation to collect. A ValueError
1433is raised if the generation number is invalid.
1434
1435The number of unreachable objects is returned.
1436[clinic start generated code]*/
1437
1438static Py_ssize_t
1439gc_collect_impl(PyObject *module, int generation)
1440/*[clinic end generated code: output=b697e633043233c7 input=40720128b682d879]*/
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001441{
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001442
Serhiy Storchaka93260282017-02-04 11:19:59 +02001443 if (generation < 0 || generation >= NUM_GENERATIONS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001444 PyErr_SetString(PyExc_ValueError, "invalid generation");
Serhiy Storchaka93260282017-02-04 11:19:59 +02001445 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001446 }
Barry Warsawd3c38ff2006-03-07 09:46:03 +00001447
Victor Stinner9db03242019-04-26 02:32:01 +02001448 struct _gc_runtime_state *state = &_PyRuntime.gc;
1449 Py_ssize_t n;
1450 if (state->collecting) {
1451 /* already collecting, don't do anything */
1452 n = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001453 }
Victor Stinner9db03242019-04-26 02:32:01 +02001454 else {
1455 state->collecting = 1;
1456 n = collect_with_callback(state, generation);
1457 state->collecting = 0;
1458 }
Serhiy Storchaka93260282017-02-04 11:19:59 +02001459 return n;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001460}
1461
Serhiy Storchaka93260282017-02-04 11:19:59 +02001462/*[clinic input]
1463gc.set_debug
1464
1465 flags: int
1466 An integer that can have the following bits turned on:
1467 DEBUG_STATS - Print statistics during collection.
1468 DEBUG_COLLECTABLE - Print collectable objects found.
1469 DEBUG_UNCOLLECTABLE - Print unreachable but uncollectable objects
1470 found.
1471 DEBUG_SAVEALL - Save objects to gc.garbage rather than freeing them.
1472 DEBUG_LEAK - Debug leaking programs (everything but STATS).
1473 /
1474
1475Set the garbage collection debugging flags.
1476
1477Debugging information is written to sys.stderr.
1478[clinic start generated code]*/
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001479
1480static PyObject *
Serhiy Storchaka93260282017-02-04 11:19:59 +02001481gc_set_debug_impl(PyObject *module, int flags)
1482/*[clinic end generated code: output=7c8366575486b228 input=5e5ce15e84fbed15]*/
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001483{
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001484 _PyRuntime.gc.debug = flags;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001485
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001486 Py_RETURN_NONE;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001487}
1488
Serhiy Storchaka93260282017-02-04 11:19:59 +02001489/*[clinic input]
1490gc.get_debug -> int
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001491
Serhiy Storchaka93260282017-02-04 11:19:59 +02001492Get the garbage collection debugging flags.
1493[clinic start generated code]*/
1494
1495static int
1496gc_get_debug_impl(PyObject *module)
1497/*[clinic end generated code: output=91242f3506cd1e50 input=91a101e1c3b98366]*/
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001498{
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001499 return _PyRuntime.gc.debug;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001500}
1501
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001502PyDoc_STRVAR(gc_set_thresh__doc__,
Neal Norwitz2a47c0f2002-01-29 00:53:41 +00001503"set_threshold(threshold0, [threshold1, threshold2]) -> None\n"
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001504"\n"
1505"Sets the collection thresholds. Setting threshold0 to zero disables\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001506"collection.\n");
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001507
1508static PyObject *
Victor Stinner9db03242019-04-26 02:32:01 +02001509gc_set_threshold(PyObject *self, PyObject *args)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001510{
Victor Stinner9db03242019-04-26 02:32:01 +02001511 struct _gc_runtime_state *state = &_PyRuntime.gc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001512 if (!PyArg_ParseTuple(args, "i|ii:set_threshold",
Victor Stinner9db03242019-04-26 02:32:01 +02001513 &state->generations[0].threshold,
1514 &state->generations[1].threshold,
1515 &state->generations[2].threshold))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001516 return NULL;
Victor Stinner9db03242019-04-26 02:32:01 +02001517 for (int i = 3; i < NUM_GENERATIONS; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001518 /* generations higher than 2 get the same threshold */
Victor Stinner9db03242019-04-26 02:32:01 +02001519 state->generations[i].threshold = state->generations[2].threshold;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001520 }
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001521 Py_RETURN_NONE;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001522}
1523
Serhiy Storchaka93260282017-02-04 11:19:59 +02001524/*[clinic input]
1525gc.get_threshold
1526
1527Return the current collection thresholds.
1528[clinic start generated code]*/
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001529
1530static PyObject *
Serhiy Storchaka93260282017-02-04 11:19:59 +02001531gc_get_threshold_impl(PyObject *module)
1532/*[clinic end generated code: output=7902bc9f41ecbbd8 input=286d79918034d6e6]*/
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001533{
Victor Stinner9db03242019-04-26 02:32:01 +02001534 struct _gc_runtime_state *state = &_PyRuntime.gc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001535 return Py_BuildValue("(iii)",
Victor Stinner9db03242019-04-26 02:32:01 +02001536 state->generations[0].threshold,
1537 state->generations[1].threshold,
1538 state->generations[2].threshold);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001539}
1540
Serhiy Storchaka93260282017-02-04 11:19:59 +02001541/*[clinic input]
1542gc.get_count
1543
1544Return a three-tuple of the current collection counts.
1545[clinic start generated code]*/
Barry Warsawd3c38ff2006-03-07 09:46:03 +00001546
1547static PyObject *
Serhiy Storchaka93260282017-02-04 11:19:59 +02001548gc_get_count_impl(PyObject *module)
1549/*[clinic end generated code: output=354012e67b16398f input=a392794a08251751]*/
Barry Warsawd3c38ff2006-03-07 09:46:03 +00001550{
Victor Stinner9db03242019-04-26 02:32:01 +02001551 struct _gc_runtime_state *state = &_PyRuntime.gc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001552 return Py_BuildValue("(iii)",
Victor Stinner9db03242019-04-26 02:32:01 +02001553 state->generations[0].count,
1554 state->generations[1].count,
1555 state->generations[2].count);
Barry Warsawd3c38ff2006-03-07 09:46:03 +00001556}
1557
Neil Schemenauer48c70342001-08-09 15:38:31 +00001558static int
Martin v. Löwis560da622001-11-24 09:24:51 +00001559referrersvisit(PyObject* obj, PyObject *objs)
Neil Schemenauer48c70342001-08-09 15:38:31 +00001560{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001561 Py_ssize_t i;
1562 for (i = 0; i < PyTuple_GET_SIZE(objs); i++)
1563 if (PyTuple_GET_ITEM(objs, i) == obj)
1564 return 1;
1565 return 0;
Neil Schemenauer48c70342001-08-09 15:38:31 +00001566}
1567
Neil Schemenauer17e7be62001-08-10 14:46:47 +00001568static int
Martin v. Löwis560da622001-11-24 09:24:51 +00001569gc_referrers_for(PyObject *objs, PyGC_Head *list, PyObject *resultlist)
Neil Schemenauer48c70342001-08-09 15:38:31 +00001570{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001571 PyGC_Head *gc;
1572 PyObject *obj;
1573 traverseproc traverse;
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09001574 for (gc = GC_NEXT(list); gc != list; gc = GC_NEXT(gc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001575 obj = FROM_GC(gc);
1576 traverse = Py_TYPE(obj)->tp_traverse;
1577 if (obj == objs || obj == resultlist)
1578 continue;
1579 if (traverse(obj, (visitproc)referrersvisit, objs)) {
1580 if (PyList_Append(resultlist, obj) < 0)
1581 return 0; /* error */
1582 }
1583 }
1584 return 1; /* no error */
Neil Schemenauer48c70342001-08-09 15:38:31 +00001585}
1586
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001587PyDoc_STRVAR(gc_get_referrers__doc__,
Martin v. Löwis560da622001-11-24 09:24:51 +00001588"get_referrers(*objs) -> list\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001589Return the list of objects that directly refer to any of objs.");
Neil Schemenauer48c70342001-08-09 15:38:31 +00001590
Neil Schemenauer17e7be62001-08-10 14:46:47 +00001591static PyObject *
Martin v. Löwis560da622001-11-24 09:24:51 +00001592gc_get_referrers(PyObject *self, PyObject *args)
Neil Schemenauer48c70342001-08-09 15:38:31 +00001593{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001594 int i;
1595 PyObject *result = PyList_New(0);
1596 if (!result) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001597
Victor Stinner9db03242019-04-26 02:32:01 +02001598 struct _gc_runtime_state *state = &_PyRuntime.gc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001599 for (i = 0; i < NUM_GENERATIONS; i++) {
Victor Stinner9db03242019-04-26 02:32:01 +02001600 if (!(gc_referrers_for(args, GEN_HEAD(state, i), result))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001601 Py_DECREF(result);
1602 return NULL;
1603 }
1604 }
1605 return result;
Neil Schemenauer48c70342001-08-09 15:38:31 +00001606}
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001607
Tim Peters0f81ab62003-04-08 16:39:48 +00001608/* Append obj to list; return true if error (out of memory), false if OK. */
Jeremy Hylton5bd378b2003-04-03 16:28:38 +00001609static int
Tim Peters730f5532003-04-08 17:17:17 +00001610referentsvisit(PyObject *obj, PyObject *list)
Jeremy Hylton5bd378b2003-04-03 16:28:38 +00001611{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001612 return PyList_Append(list, obj) < 0;
Jeremy Hylton5bd378b2003-04-03 16:28:38 +00001613}
1614
Tim Peters730f5532003-04-08 17:17:17 +00001615PyDoc_STRVAR(gc_get_referents__doc__,
1616"get_referents(*objs) -> list\n\
Jeremy Hylton059b0942003-04-03 16:29:13 +00001617Return the list of objects that are directly referred to by objs.");
Jeremy Hylton5bd378b2003-04-03 16:28:38 +00001618
1619static PyObject *
Tim Peters730f5532003-04-08 17:17:17 +00001620gc_get_referents(PyObject *self, PyObject *args)
Jeremy Hylton5bd378b2003-04-03 16:28:38 +00001621{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001622 Py_ssize_t i;
1623 PyObject *result = PyList_New(0);
Tim Peters0f81ab62003-04-08 16:39:48 +00001624
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001625 if (result == NULL)
1626 return NULL;
Tim Peters0f81ab62003-04-08 16:39:48 +00001627
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001628 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
1629 traverseproc traverse;
1630 PyObject *obj = PyTuple_GET_ITEM(args, i);
Tim Peters0f81ab62003-04-08 16:39:48 +00001631
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001632 if (! PyObject_IS_GC(obj))
1633 continue;
1634 traverse = Py_TYPE(obj)->tp_traverse;
1635 if (! traverse)
1636 continue;
1637 if (traverse(obj, (visitproc)referentsvisit, result)) {
1638 Py_DECREF(result);
1639 return NULL;
1640 }
1641 }
1642 return result;
Jeremy Hylton5bd378b2003-04-03 16:28:38 +00001643}
1644
Serhiy Storchaka93260282017-02-04 11:19:59 +02001645/*[clinic input]
1646gc.get_objects
Pablo Galindo175421b2019-02-23 03:02:06 +00001647 generation: Py_ssize_t(accept={int, NoneType}, c_default="-1") = None
1648 Generation to extract the objects from.
Serhiy Storchaka93260282017-02-04 11:19:59 +02001649
1650Return a list of objects tracked by the collector (excluding the list returned).
Pablo Galindo175421b2019-02-23 03:02:06 +00001651
1652If generation is not None, return only the objects tracked by the collector
1653that are in that generation.
Serhiy Storchaka93260282017-02-04 11:19:59 +02001654[clinic start generated code]*/
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +00001655
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +00001656static PyObject *
Pablo Galindo175421b2019-02-23 03:02:06 +00001657gc_get_objects_impl(PyObject *module, Py_ssize_t generation)
1658/*[clinic end generated code: output=48b35fea4ba6cb0e input=ef7da9df9806754c]*/
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +00001659{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001660 int i;
1661 PyObject* result;
Victor Stinner9db03242019-04-26 02:32:01 +02001662 struct _gc_runtime_state *state = &_PyRuntime.gc;
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +00001663
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001664 result = PyList_New(0);
Pablo Galindo175421b2019-02-23 03:02:06 +00001665 if (result == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001666 return NULL;
Pablo Galindo175421b2019-02-23 03:02:06 +00001667 }
1668
1669 /* If generation is passed, we extract only that generation */
Victor Stinner0810fa72019-04-15 17:54:09 +02001670 if (generation != -1) {
Pablo Galindo175421b2019-02-23 03:02:06 +00001671 if (generation >= NUM_GENERATIONS) {
1672 PyErr_Format(PyExc_ValueError,
1673 "generation parameter must be less than the number of "
1674 "available generations (%i)",
1675 NUM_GENERATIONS);
1676 goto error;
1677 }
1678
1679 if (generation < 0) {
1680 PyErr_SetString(PyExc_ValueError,
1681 "generation parameter cannot be negative");
1682 goto error;
1683 }
1684
Victor Stinner9db03242019-04-26 02:32:01 +02001685 if (append_objects(result, GEN_HEAD(state, generation))) {
Pablo Galindo175421b2019-02-23 03:02:06 +00001686 goto error;
1687 }
1688
1689 return result;
1690 }
1691
1692 /* If generation is not passed or None, get all objects from all generations */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001693 for (i = 0; i < NUM_GENERATIONS; i++) {
Victor Stinner9db03242019-04-26 02:32:01 +02001694 if (append_objects(result, GEN_HEAD(state, i))) {
Pablo Galindo175421b2019-02-23 03:02:06 +00001695 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001696 }
1697 }
1698 return result;
Pablo Galindo175421b2019-02-23 03:02:06 +00001699
1700error:
1701 Py_DECREF(result);
1702 return NULL;
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +00001703}
1704
Serhiy Storchaka93260282017-02-04 11:19:59 +02001705/*[clinic input]
1706gc.get_stats
1707
1708Return a list of dictionaries containing per-generation statistics.
1709[clinic start generated code]*/
Antoine Pitroud4156c12012-10-30 22:43:19 +01001710
1711static PyObject *
Serhiy Storchaka93260282017-02-04 11:19:59 +02001712gc_get_stats_impl(PyObject *module)
1713/*[clinic end generated code: output=a8ab1d8a5d26f3ab input=1ef4ed9d17b1a470]*/
Antoine Pitroud4156c12012-10-30 22:43:19 +01001714{
1715 int i;
Antoine Pitroud4156c12012-10-30 22:43:19 +01001716 struct gc_generation_stats stats[NUM_GENERATIONS], *st;
1717
1718 /* To get consistent values despite allocations while constructing
1719 the result list, we use a snapshot of the running stats. */
Victor Stinner9db03242019-04-26 02:32:01 +02001720 struct _gc_runtime_state *state = &_PyRuntime.gc;
Antoine Pitroud4156c12012-10-30 22:43:19 +01001721 for (i = 0; i < NUM_GENERATIONS; i++) {
Victor Stinner9db03242019-04-26 02:32:01 +02001722 stats[i] = state->generation_stats[i];
Antoine Pitroud4156c12012-10-30 22:43:19 +01001723 }
1724
Victor Stinner9db03242019-04-26 02:32:01 +02001725 PyObject *result = PyList_New(0);
Antoine Pitroud4156c12012-10-30 22:43:19 +01001726 if (result == NULL)
1727 return NULL;
1728
1729 for (i = 0; i < NUM_GENERATIONS; i++) {
1730 PyObject *dict;
1731 st = &stats[i];
1732 dict = Py_BuildValue("{snsnsn}",
1733 "collections", st->collections,
1734 "collected", st->collected,
1735 "uncollectable", st->uncollectable
1736 );
1737 if (dict == NULL)
1738 goto error;
1739 if (PyList_Append(result, dict)) {
1740 Py_DECREF(dict);
1741 goto error;
1742 }
1743 Py_DECREF(dict);
1744 }
1745 return result;
1746
1747error:
1748 Py_XDECREF(result);
1749 return NULL;
1750}
1751
1752
Serhiy Storchaka93260282017-02-04 11:19:59 +02001753/*[clinic input]
1754gc.is_tracked
1755
1756 obj: object
1757 /
1758
1759Returns true if the object is tracked by the garbage collector.
1760
1761Simple atomic objects will return false.
1762[clinic start generated code]*/
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001763
1764static PyObject *
Serhiy Storchaka93260282017-02-04 11:19:59 +02001765gc_is_tracked(PyObject *module, PyObject *obj)
1766/*[clinic end generated code: output=14f0103423b28e31 input=d83057f170ea2723]*/
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001767{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001768 PyObject *result;
1769
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09001770 if (PyObject_IS_GC(obj) && _PyObject_GC_IS_TRACKED(obj))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001771 result = Py_True;
1772 else
1773 result = Py_False;
1774 Py_INCREF(result);
1775 return result;
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001776}
1777
brainfvckc75edab2017-10-16 12:49:41 -07001778/*[clinic input]
1779gc.freeze
1780
1781Freeze all current tracked objects and ignore them for future collections.
1782
1783This can be used before a POSIX fork() call to make the gc copy-on-write friendly.
1784Note: collection before a POSIX fork() call may free pages for future allocation
1785which can cause copy-on-write.
1786[clinic start generated code]*/
1787
1788static PyObject *
1789gc_freeze_impl(PyObject *module)
1790/*[clinic end generated code: output=502159d9cdc4c139 input=b602b16ac5febbe5]*/
1791{
Victor Stinner9db03242019-04-26 02:32:01 +02001792 struct _gc_runtime_state *state = &_PyRuntime.gc;
brainfvckc75edab2017-10-16 12:49:41 -07001793 for (int i = 0; i < NUM_GENERATIONS; ++i) {
Victor Stinner9db03242019-04-26 02:32:01 +02001794 gc_list_merge(GEN_HEAD(state, i), &state->permanent_generation.head);
1795 state->generations[i].count = 0;
brainfvckc75edab2017-10-16 12:49:41 -07001796 }
1797 Py_RETURN_NONE;
1798}
1799
1800/*[clinic input]
1801gc.unfreeze
1802
1803Unfreeze all objects in the permanent generation.
1804
1805Put all objects in the permanent generation back into oldest generation.
1806[clinic start generated code]*/
1807
1808static PyObject *
1809gc_unfreeze_impl(PyObject *module)
1810/*[clinic end generated code: output=1c15f2043b25e169 input=2dd52b170f4cef6c]*/
1811{
Victor Stinner9db03242019-04-26 02:32:01 +02001812 struct _gc_runtime_state *state = &_PyRuntime.gc;
1813 gc_list_merge(&state->permanent_generation.head, GEN_HEAD(state, NUM_GENERATIONS-1));
brainfvckc75edab2017-10-16 12:49:41 -07001814 Py_RETURN_NONE;
1815}
1816
1817/*[clinic input]
Victor Stinner05d68a82018-01-18 11:15:25 +01001818gc.get_freeze_count -> Py_ssize_t
brainfvckc75edab2017-10-16 12:49:41 -07001819
1820Return the number of objects in the permanent generation.
1821[clinic start generated code]*/
1822
Victor Stinner05d68a82018-01-18 11:15:25 +01001823static Py_ssize_t
brainfvckc75edab2017-10-16 12:49:41 -07001824gc_get_freeze_count_impl(PyObject *module)
Victor Stinner05d68a82018-01-18 11:15:25 +01001825/*[clinic end generated code: output=61cbd9f43aa032e1 input=45ffbc65cfe2a6ed]*/
brainfvckc75edab2017-10-16 12:49:41 -07001826{
1827 return gc_list_size(&_PyRuntime.gc.permanent_generation.head);
1828}
1829
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +00001830
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001831PyDoc_STRVAR(gc__doc__,
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001832"This module provides access to the garbage collector for reference cycles.\n"
1833"\n"
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001834"enable() -- Enable automatic garbage collection.\n"
1835"disable() -- Disable automatic garbage collection.\n"
1836"isenabled() -- Returns true if automatic collection is enabled.\n"
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001837"collect() -- Do a full collection right now.\n"
Thomas Wouters89f507f2006-12-13 04:49:30 +00001838"get_count() -- Return the current collection counts.\n"
R David Murray0e814632013-12-26 15:11:28 -05001839"get_stats() -- Return list of dictionaries containing per-generation stats.\n"
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001840"set_debug() -- Set debugging flags.\n"
1841"get_debug() -- Get debugging flags.\n"
1842"set_threshold() -- Set the collection thresholds.\n"
1843"get_threshold() -- Return the current the collection thresholds.\n"
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +00001844"get_objects() -- Return a list of all objects tracked by the collector.\n"
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001845"is_tracked() -- Returns true if a given object is tracked.\n"
Jeremy Hylton5bd378b2003-04-03 16:28:38 +00001846"get_referrers() -- Return the list of objects that refer to an object.\n"
brainfvckc75edab2017-10-16 12:49:41 -07001847"get_referents() -- Return the list of objects that an object refers to.\n"
1848"freeze() -- Freeze all tracked objects and ignore them for future collections.\n"
1849"unfreeze() -- Unfreeze all objects in the permanent generation.\n"
1850"get_freeze_count() -- Return the number of objects in the permanent generation.\n");
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001851
1852static PyMethodDef GcMethods[] = {
Serhiy Storchaka93260282017-02-04 11:19:59 +02001853 GC_ENABLE_METHODDEF
1854 GC_DISABLE_METHODDEF
1855 GC_ISENABLED_METHODDEF
1856 GC_SET_DEBUG_METHODDEF
1857 GC_GET_DEBUG_METHODDEF
1858 GC_GET_COUNT_METHODDEF
Victor Stinner9db03242019-04-26 02:32:01 +02001859 {"set_threshold", gc_set_threshold, METH_VARARGS, gc_set_thresh__doc__},
Serhiy Storchaka93260282017-02-04 11:19:59 +02001860 GC_GET_THRESHOLD_METHODDEF
1861 GC_COLLECT_METHODDEF
1862 GC_GET_OBJECTS_METHODDEF
1863 GC_GET_STATS_METHODDEF
1864 GC_IS_TRACKED_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001865 {"get_referrers", gc_get_referrers, METH_VARARGS,
1866 gc_get_referrers__doc__},
1867 {"get_referents", gc_get_referents, METH_VARARGS,
1868 gc_get_referents__doc__},
brainfvckc75edab2017-10-16 12:49:41 -07001869 GC_FREEZE_METHODDEF
1870 GC_UNFREEZE_METHODDEF
1871 GC_GET_FREEZE_COUNT_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001872 {NULL, NULL} /* Sentinel */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001873};
1874
Martin v. Löwis1a214512008-06-11 05:26:20 +00001875static struct PyModuleDef gcmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001876 PyModuleDef_HEAD_INIT,
Antoine Pitrou696e0352010-08-08 22:18:46 +00001877 "gc", /* m_name */
1878 gc__doc__, /* m_doc */
1879 -1, /* m_size */
1880 GcMethods, /* m_methods */
1881 NULL, /* m_reload */
1882 NULL, /* m_traverse */
1883 NULL, /* m_clear */
1884 NULL /* m_free */
Martin v. Löwis1a214512008-06-11 05:26:20 +00001885};
1886
Jason Tishler6bc06ec2003-09-04 11:59:50 +00001887PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001888PyInit_gc(void)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001889{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001890 PyObject *m;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001891
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001892 m = PyModule_Create(&gcmodule);
Martin v. Löwis1a214512008-06-11 05:26:20 +00001893
Victor Stinner9db03242019-04-26 02:32:01 +02001894 if (m == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001895 return NULL;
Victor Stinner9db03242019-04-26 02:32:01 +02001896 }
Tim Peters11558872003-04-06 23:30:52 +00001897
Victor Stinner9db03242019-04-26 02:32:01 +02001898 struct _gc_runtime_state *state = &_PyRuntime.gc;
1899 if (state->garbage == NULL) {
1900 state->garbage = PyList_New(0);
1901 if (state->garbage == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001902 return NULL;
1903 }
Victor Stinner9db03242019-04-26 02:32:01 +02001904 Py_INCREF(state->garbage);
1905 if (PyModule_AddObject(m, "garbage", state->garbage) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001906 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001907
Victor Stinner9db03242019-04-26 02:32:01 +02001908 if (state->callbacks == NULL) {
1909 state->callbacks = PyList_New(0);
1910 if (state->callbacks == NULL)
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001911 return NULL;
1912 }
Victor Stinner9db03242019-04-26 02:32:01 +02001913 Py_INCREF(state->callbacks);
1914 if (PyModule_AddObject(m, "callbacks", state->callbacks) < 0)
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001915 return NULL;
1916
Martin v. Löwis1a214512008-06-11 05:26:20 +00001917#define ADD_INT(NAME) if (PyModule_AddIntConstant(m, #NAME, NAME) < 0) return NULL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001918 ADD_INT(DEBUG_STATS);
1919 ADD_INT(DEBUG_COLLECTABLE);
1920 ADD_INT(DEBUG_UNCOLLECTABLE);
1921 ADD_INT(DEBUG_SAVEALL);
1922 ADD_INT(DEBUG_LEAK);
Tim Peters11558872003-04-06 23:30:52 +00001923#undef ADD_INT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001924 return m;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001925}
1926
Guido van Rossume13ddc92003-04-17 17:29:22 +00001927/* API to invoke gc.collect() from C */
Neal Norwitz7b216c52006-03-04 20:01:53 +00001928Py_ssize_t
Guido van Rossume13ddc92003-04-17 17:29:22 +00001929PyGC_Collect(void)
1930{
Victor Stinner9db03242019-04-26 02:32:01 +02001931 struct _gc_runtime_state *state = &_PyRuntime.gc;
1932 if (!state->enabled) {
1933 return 0;
1934 }
Guido van Rossume13ddc92003-04-17 17:29:22 +00001935
Victor Stinner9db03242019-04-26 02:32:01 +02001936 Py_ssize_t n;
1937 if (state->collecting) {
1938 /* already collecting, don't do anything */
1939 n = 0;
1940 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001941 else {
Serhiy Storchaka301e3cc2018-05-24 15:19:29 +03001942 PyObject *exc, *value, *tb;
Victor Stinner9db03242019-04-26 02:32:01 +02001943 state->collecting = 1;
Serhiy Storchaka301e3cc2018-05-24 15:19:29 +03001944 PyErr_Fetch(&exc, &value, &tb);
Victor Stinner9db03242019-04-26 02:32:01 +02001945 n = collect_with_callback(state, NUM_GENERATIONS - 1);
Serhiy Storchaka301e3cc2018-05-24 15:19:29 +03001946 PyErr_Restore(exc, value, tb);
Victor Stinner9db03242019-04-26 02:32:01 +02001947 state->collecting = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001948 }
Guido van Rossume13ddc92003-04-17 17:29:22 +00001949
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001950 return n;
Guido van Rossume13ddc92003-04-17 17:29:22 +00001951}
1952
Antoine Pitroufef34e32013-05-19 01:11:58 +02001953Py_ssize_t
Łukasz Langafef7e942016-09-09 21:47:46 -07001954_PyGC_CollectIfEnabled(void)
1955{
Łukasz Langafef7e942016-09-09 21:47:46 -07001956 return PyGC_Collect();
1957}
1958
1959Py_ssize_t
Antoine Pitroufef34e32013-05-19 01:11:58 +02001960_PyGC_CollectNoFail(void)
1961{
Victor Stinner9db03242019-04-26 02:32:01 +02001962 assert(!PyErr_Occurred());
1963
1964 struct _gc_runtime_state *state = &_PyRuntime.gc;
Antoine Pitroufef34e32013-05-19 01:11:58 +02001965 Py_ssize_t n;
1966
Antoine Pitrouc69c9bc2013-08-15 20:15:15 +02001967 /* Ideally, this function is only called on interpreter shutdown,
1968 and therefore not recursively. Unfortunately, when there are daemon
1969 threads, a daemon thread can start a cyclic garbage collection
1970 during interpreter shutdown (and then never finish it).
1971 See http://bugs.python.org/issue8713#msg195178 for an example.
1972 */
Victor Stinner9db03242019-04-26 02:32:01 +02001973 if (state->collecting) {
Antoine Pitrouc69c9bc2013-08-15 20:15:15 +02001974 n = 0;
Victor Stinner9db03242019-04-26 02:32:01 +02001975 }
Antoine Pitrouc69c9bc2013-08-15 20:15:15 +02001976 else {
Victor Stinner9db03242019-04-26 02:32:01 +02001977 state->collecting = 1;
1978 n = collect(state, NUM_GENERATIONS - 1, NULL, NULL, 1);
1979 state->collecting = 0;
Antoine Pitrouc69c9bc2013-08-15 20:15:15 +02001980 }
Antoine Pitroufef34e32013-05-19 01:11:58 +02001981 return n;
1982}
Antoine Pitrou5f454a02013-05-06 21:15:57 +02001983
Antoine Pitrou696e0352010-08-08 22:18:46 +00001984void
Victor Stinner9db03242019-04-26 02:32:01 +02001985_PyGC_DumpShutdownStats(_PyRuntimeState *runtime)
Antoine Pitrou696e0352010-08-08 22:18:46 +00001986{
Victor Stinner9db03242019-04-26 02:32:01 +02001987 struct _gc_runtime_state *state = &runtime->gc;
1988 if (!(state->debug & DEBUG_SAVEALL)
1989 && state->garbage != NULL && PyList_GET_SIZE(state->garbage) > 0) {
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001990 const char *message;
Victor Stinner9db03242019-04-26 02:32:01 +02001991 if (state->debug & DEBUG_UNCOLLECTABLE)
Antoine Pitroub5d82042010-11-05 00:05:25 +00001992 message = "gc: %zd uncollectable objects at " \
Georg Brandl08be72d2010-10-24 15:11:22 +00001993 "shutdown";
1994 else
Antoine Pitroub5d82042010-11-05 00:05:25 +00001995 message = "gc: %zd uncollectable objects at " \
Georg Brandl08be72d2010-10-24 15:11:22 +00001996 "shutdown; use gc.set_debug(gc.DEBUG_UNCOLLECTABLE) to list them";
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001997 /* PyErr_WarnFormat does too many things and we are at shutdown,
1998 the warnings module's dependencies (e.g. linecache) may be gone
1999 already. */
2000 if (PyErr_WarnExplicitFormat(PyExc_ResourceWarning, "gc", 0,
2001 "gc", NULL, message,
Victor Stinner9db03242019-04-26 02:32:01 +02002002 PyList_GET_SIZE(state->garbage)))
Georg Brandl08be72d2010-10-24 15:11:22 +00002003 PyErr_WriteUnraisable(NULL);
Victor Stinner9db03242019-04-26 02:32:01 +02002004 if (state->debug & DEBUG_UNCOLLECTABLE) {
Antoine Pitrou696e0352010-08-08 22:18:46 +00002005 PyObject *repr = NULL, *bytes = NULL;
Victor Stinner9db03242019-04-26 02:32:01 +02002006 repr = PyObject_Repr(state->garbage);
Antoine Pitrou696e0352010-08-08 22:18:46 +00002007 if (!repr || !(bytes = PyUnicode_EncodeFSDefault(repr)))
Victor Stinner9db03242019-04-26 02:32:01 +02002008 PyErr_WriteUnraisable(state->garbage);
Antoine Pitrou696e0352010-08-08 22:18:46 +00002009 else {
2010 PySys_WriteStderr(
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02002011 " %s\n",
Antoine Pitrou696e0352010-08-08 22:18:46 +00002012 PyBytes_AS_STRING(bytes)
2013 );
2014 }
2015 Py_XDECREF(repr);
2016 Py_XDECREF(bytes);
2017 }
Antoine Pitrou696e0352010-08-08 22:18:46 +00002018 }
Antoine Pitrou5f454a02013-05-06 21:15:57 +02002019}
2020
2021void
Victor Stinner8e91c242019-04-24 17:24:01 +02002022_PyGC_Fini(_PyRuntimeState *runtime)
Antoine Pitrou5f454a02013-05-06 21:15:57 +02002023{
Victor Stinner9db03242019-04-26 02:32:01 +02002024 struct _gc_runtime_state *state = &runtime->gc;
2025 Py_CLEAR(state->garbage);
2026 Py_CLEAR(state->callbacks);
Antoine Pitrou696e0352010-08-08 22:18:46 +00002027}
2028
Neil Schemenauer43411b52001-08-30 00:05:51 +00002029/* for debugging */
Guido van Rossume13ddc92003-04-17 17:29:22 +00002030void
2031_PyGC_Dump(PyGC_Head *g)
Neil Schemenauer43411b52001-08-30 00:05:51 +00002032{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002033 _PyObject_Dump(FROM_GC(g));
Neil Schemenauer43411b52001-08-30 00:05:51 +00002034}
2035
Victor Stinnera5447732019-10-10 09:32:13 +02002036
2037#ifdef Py_DEBUG
Victor Stinner1b184552019-10-08 00:09:31 +02002038static int
2039visit_validate(PyObject *op, void *parent_raw)
2040{
2041 PyObject *parent = _PyObject_CAST(parent_raw);
2042 if (_PyObject_IsFreed(op)) {
2043 _PyObject_ASSERT_FAILED_MSG(parent,
2044 "PyObject_GC_Track() object is not valid");
2045 }
2046 return 0;
2047}
Victor Stinnera5447732019-10-10 09:32:13 +02002048#endif
Victor Stinner1b184552019-10-08 00:09:31 +02002049
2050
Neil Schemenauer43411b52001-08-30 00:05:51 +00002051/* extension modules might be compiled with GC support so these
2052 functions must always be available */
2053
2054void
Victor Stinnera42de742018-11-22 10:25:22 +01002055PyObject_GC_Track(void *op_raw)
Neil Schemenauer43411b52001-08-30 00:05:51 +00002056{
Victor Stinnera42de742018-11-22 10:25:22 +01002057 PyObject *op = _PyObject_CAST(op_raw);
Victor Stinner271753a2018-11-22 01:02:54 +01002058 if (_PyObject_GC_IS_TRACKED(op)) {
2059 _PyObject_ASSERT_FAILED_MSG(op,
2060 "object already tracked "
2061 "by the garbage collector");
2062 }
Victor Stinnera42de742018-11-22 10:25:22 +01002063 _PyObject_GC_TRACK(op);
Victor Stinner1b184552019-10-08 00:09:31 +02002064
2065#ifdef Py_DEBUG
2066 /* Check that the object is valid: validate objects traversed
2067 by tp_traverse() */
2068 traverseproc traverse = Py_TYPE(op)->tp_traverse;
2069 (void)traverse(op, visit_validate, op);
2070#endif
Neil Schemenauer43411b52001-08-30 00:05:51 +00002071}
2072
Neil Schemenauerfec4eb12002-04-12 02:41:03 +00002073void
Victor Stinnera42de742018-11-22 10:25:22 +01002074PyObject_GC_UnTrack(void *op_raw)
Neil Schemenauer43411b52001-08-30 00:05:51 +00002075{
Victor Stinnera42de742018-11-22 10:25:22 +01002076 PyObject *op = _PyObject_CAST(op_raw);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002077 /* Obscure: the Py_TRASHCAN mechanism requires that we be able to
2078 * call PyObject_GC_UnTrack twice on an object.
2079 */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002080 if (_PyObject_GC_IS_TRACKED(op)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002081 _PyObject_GC_UNTRACK(op);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002082 }
Neil Schemenauer43411b52001-08-30 00:05:51 +00002083}
2084
Victor Stinnerdb067af2014-05-02 22:31:14 +02002085static PyObject *
2086_PyObject_GC_Alloc(int use_calloc, size_t basicsize)
Neil Schemenauer43411b52001-08-30 00:05:51 +00002087{
Victor Stinner9db03242019-04-26 02:32:01 +02002088 struct _gc_runtime_state *state = &_PyRuntime.gc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002089 PyObject *op;
2090 PyGC_Head *g;
Victor Stinnerdb067af2014-05-02 22:31:14 +02002091 size_t size;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002092 if (basicsize > PY_SSIZE_T_MAX - sizeof(PyGC_Head))
2093 return PyErr_NoMemory();
Victor Stinnerdb067af2014-05-02 22:31:14 +02002094 size = sizeof(PyGC_Head) + basicsize;
2095 if (use_calloc)
2096 g = (PyGC_Head *)PyObject_Calloc(1, size);
2097 else
2098 g = (PyGC_Head *)PyObject_Malloc(size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002099 if (g == NULL)
2100 return PyErr_NoMemory();
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002101 assert(((uintptr_t)g & 3) == 0); // g must be aligned 4bytes boundary
2102 g->_gc_next = 0;
2103 g->_gc_prev = 0;
Victor Stinner9db03242019-04-26 02:32:01 +02002104 state->generations[0].count++; /* number of allocated GC objects */
2105 if (state->generations[0].count > state->generations[0].threshold &&
2106 state->enabled &&
2107 state->generations[0].threshold &&
2108 !state->collecting &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002109 !PyErr_Occurred()) {
Victor Stinner9db03242019-04-26 02:32:01 +02002110 state->collecting = 1;
2111 collect_generations(state);
2112 state->collecting = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002113 }
2114 op = FROM_GC(g);
2115 return op;
Neil Schemenauer43411b52001-08-30 00:05:51 +00002116}
2117
2118PyObject *
Victor Stinnerdb067af2014-05-02 22:31:14 +02002119_PyObject_GC_Malloc(size_t basicsize)
2120{
2121 return _PyObject_GC_Alloc(0, basicsize);
2122}
2123
2124PyObject *
2125_PyObject_GC_Calloc(size_t basicsize)
2126{
2127 return _PyObject_GC_Alloc(1, basicsize);
2128}
2129
2130PyObject *
Neil Schemenauer43411b52001-08-30 00:05:51 +00002131_PyObject_GC_New(PyTypeObject *tp)
2132{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002133 PyObject *op = _PyObject_GC_Malloc(_PyObject_SIZE(tp));
2134 if (op != NULL)
2135 op = PyObject_INIT(op, tp);
2136 return op;
Neil Schemenauer43411b52001-08-30 00:05:51 +00002137}
2138
2139PyVarObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00002140_PyObject_GC_NewVar(PyTypeObject *tp, Py_ssize_t nitems)
Neil Schemenauer43411b52001-08-30 00:05:51 +00002141{
Victor Stinner5d1866c2013-07-08 22:17:52 +02002142 size_t size;
2143 PyVarObject *op;
2144
2145 if (nitems < 0) {
2146 PyErr_BadInternalCall();
2147 return NULL;
2148 }
2149 size = _PyObject_VAR_SIZE(tp, nitems);
2150 op = (PyVarObject *) _PyObject_GC_Malloc(size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002151 if (op != NULL)
2152 op = PyObject_INIT_VAR(op, tp, nitems);
2153 return op;
Neil Schemenauer43411b52001-08-30 00:05:51 +00002154}
2155
2156PyVarObject *
Martin v. Löwis41290682006-02-16 14:56:14 +00002157_PyObject_GC_Resize(PyVarObject *op, Py_ssize_t nitems)
Neil Schemenauer43411b52001-08-30 00:05:51 +00002158{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002159 const size_t basicsize = _PyObject_VAR_SIZE(Py_TYPE(op), nitems);
Victor Stinnera4b2bc72018-10-26 18:00:13 +02002160 _PyObject_ASSERT((PyObject *)op, !_PyObject_GC_IS_TRACKED(op));
2161 if (basicsize > PY_SSIZE_T_MAX - sizeof(PyGC_Head)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002162 return (PyVarObject *)PyErr_NoMemory();
Victor Stinnera4b2bc72018-10-26 18:00:13 +02002163 }
2164
2165 PyGC_Head *g = AS_GC(op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002166 g = (PyGC_Head *)PyObject_REALLOC(g, sizeof(PyGC_Head) + basicsize);
2167 if (g == NULL)
2168 return (PyVarObject *)PyErr_NoMemory();
2169 op = (PyVarObject *) FROM_GC(g);
2170 Py_SIZE(op) = nitems;
2171 return op;
Neil Schemenauer43411b52001-08-30 00:05:51 +00002172}
2173
2174void
Neil Schemenauerfec4eb12002-04-12 02:41:03 +00002175PyObject_GC_Del(void *op)
Neil Schemenauer43411b52001-08-30 00:05:51 +00002176{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002177 PyGC_Head *g = AS_GC(op);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002178 if (_PyObject_GC_IS_TRACKED(op)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002179 gc_list_remove(g);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002180 }
Victor Stinner9db03242019-04-26 02:32:01 +02002181 struct _gc_runtime_state *state = &_PyRuntime.gc;
2182 if (state->generations[0].count > 0) {
2183 state->generations[0].count--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002184 }
2185 PyObject_FREE(g);
Neil Schemenauer43411b52001-08-30 00:05:51 +00002186}
Tim Petersd9d39932019-11-02 12:06:31 -05002187
2188/* ------------------------------------------------------------------------
2189Notes
2190
2191[REACHABLE OR UNREACHABLE?}
2192
2193It "sounds slick" to move the unreachable objects, until you think about
2194it - the reason it pays isn't actually obvious.
2195
2196Suppose we create objects A, B, C in that order. They appear in the young
2197generation in the same order. If B points to A, and C to B, and C is
2198reachable from outside, then the adjusted refcounts will be 0, 0, and 1
2199respectively.
2200
2201When move_unreachable finds A, A is moved to the unreachable list. The
2202same for B when it's first encountered. Then C is traversed, B is moved
2203_back_ to the reachable list. B is eventually traversed, and then A is
2204moved back to the reachable list.
2205
2206So instead of not moving at all, the reachable objects B and A are moved
2207twice each. Why is this a win? A straightforward algorithm to move the
2208reachable objects instead would move A, B, and C once each.
2209
2210The key is that this dance leaves the objects in order C, B, A - it's
2211reversed from the original order. On all _subsequent_ scans, none of
2212them will move. Since most objects aren't in cycles, this can save an
2213unbounded number of moves across an unbounded number of later collections.
2214It can cost more only the first time the chain is scanned.
2215
2216Drawback: move_unreachable is also used to find out what's still trash
2217after finalizers may resurrect objects. In _that_ case most unreachable
2218objects will remain unreachable, so it would be more efficient to move
2219the reachable objects instead. But this is a one-time cost, probably not
2220worth complicating the code to speed just a little.
2221------------------------------------------------------------------------ */
2222