blob: 110a48d8cd76fb8c1b8df237b095b283767c2763 [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 Stinner444b39b2019-11-20 01:18:11 +010028#include "pycore_initconfig.h"
Victor Stinnere5014be2020-04-14 17:52:15 +020029#include "pycore_interp.h" // PyInterpreterState.gc
Victor Stinnerbcda8f12018-11-21 22:27:47 +010030#include "pycore_object.h"
Victor Stinner2e969062019-11-20 01:49:32 +010031#include "pycore_pyerrors.h"
Victor Stinnere5014be2020-04-14 17:52:15 +020032#include "pycore_pystate.h" // _PyThreadState_GET()
Łukasz Langaa785c872016-09-09 17:37:37 -070033#include "pydtrace.h"
Victor Stinnere5014be2020-04-14 17:52:15 +020034#include "pytime.h" // _PyTime_GetMonotonicClock()
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000035
Victor Stinner67e0de62019-11-20 11:48:18 +010036typedef struct _gc_runtime_state GCState;
37
Serhiy Storchaka93260282017-02-04 11:19:59 +020038/*[clinic input]
39module gc
40[clinic start generated code]*/
41/*[clinic end generated code: output=da39a3ee5e6b4b0d input=b5c9690ecc842d79]*/
42
Pablo Galindo320dd502019-10-10 22:45:17 +010043
44#ifdef Py_DEBUG
45# define GC_DEBUG
46#endif
INADA Naoki5ac9e6e2018-07-10 17:19:53 +090047
48#define GC_NEXT _PyGCHead_NEXT
49#define GC_PREV _PyGCHead_PREV
50
51// update_refs() set this bit for all objects in current generation.
52// subtract_refs() and move_unreachable() uses this to distinguish
53// visited object is in GCing or not.
54//
55// move_unreachable() removes this flag from reachable objects.
56// Only unreachable objects have this flag.
57//
58// No objects in interpreter have this flag after GC ends.
59#define PREV_MASK_COLLECTING _PyGC_PREV_MASK_COLLECTING
60
61// Lowest bit of _gc_next is used for UNREACHABLE flag.
62//
63// This flag represents the object is in unreachable list in move_unreachable()
64//
65// Although this flag is used only in move_unreachable(), move_unreachable()
66// doesn't clear this flag to skip unnecessary iteration.
67// move_legacy_finalizers() removes this flag instead.
68// Between them, unreachable list is not normal list and we can not use
69// most gc_list_* functions for it.
70#define NEXT_MASK_UNREACHABLE (1)
71
Victor Stinner626bff82018-10-25 17:31:10 +020072/* Get an object's GC head */
73#define AS_GC(o) ((PyGC_Head *)(o)-1)
74
75/* Get the object given the GC head */
76#define FROM_GC(g) ((PyObject *)(((PyGC_Head *)g)+1))
77
INADA Naoki5ac9e6e2018-07-10 17:19:53 +090078static inline int
79gc_is_collecting(PyGC_Head *g)
80{
81 return (g->_gc_prev & PREV_MASK_COLLECTING) != 0;
82}
83
84static inline void
85gc_clear_collecting(PyGC_Head *g)
86{
87 g->_gc_prev &= ~PREV_MASK_COLLECTING;
88}
89
90static inline Py_ssize_t
91gc_get_refs(PyGC_Head *g)
92{
93 return (Py_ssize_t)(g->_gc_prev >> _PyGC_PREV_SHIFT);
94}
95
96static inline void
97gc_set_refs(PyGC_Head *g, Py_ssize_t refs)
98{
99 g->_gc_prev = (g->_gc_prev & ~_PyGC_PREV_MASK)
100 | ((uintptr_t)(refs) << _PyGC_PREV_SHIFT);
101}
102
103static inline void
104gc_reset_refs(PyGC_Head *g, Py_ssize_t refs)
105{
106 g->_gc_prev = (g->_gc_prev & _PyGC_PREV_MASK_FINALIZED)
107 | PREV_MASK_COLLECTING
108 | ((uintptr_t)(refs) << _PyGC_PREV_SHIFT);
109}
110
111static inline void
112gc_decref(PyGC_Head *g)
113{
Victor Stinner626bff82018-10-25 17:31:10 +0200114 _PyObject_ASSERT_WITH_MSG(FROM_GC(g),
115 gc_get_refs(g) > 0,
116 "refcount is too small");
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900117 g->_gc_prev -= 1 << _PyGC_PREV_SHIFT;
118}
119
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 Stinner67e0de62019-11-20 11:48:18 +0100129#define GEN_HEAD(gcstate, n) (&(gcstate)->generations[n].head)
Antoine Pitroud4156c12012-10-30 22:43:19 +0100130
Victor Stinner1bcc32f2020-06-10 20:08:26 +0200131
132static GCState *
133get_gc_state(void)
134{
135 PyInterpreterState *interp = _PyInterpreterState_GET();
136 return &interp->gc;
137}
138
139
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600140void
Victor Stinner72474072019-11-20 12:25:50 +0100141_PyGC_InitState(GCState *gcstate)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600142{
Victor Stinner67e0de62019-11-20 11:48:18 +0100143 gcstate->enabled = 1; /* automatic collection enabled? */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600144
Victor Stinner67e0de62019-11-20 11:48:18 +0100145#define _GEN_HEAD(n) GEN_HEAD(gcstate, n)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600146 struct gc_generation generations[NUM_GENERATIONS] = {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900147 /* PyGC_Head, threshold, count */
148 {{(uintptr_t)_GEN_HEAD(0), (uintptr_t)_GEN_HEAD(0)}, 700, 0},
149 {{(uintptr_t)_GEN_HEAD(1), (uintptr_t)_GEN_HEAD(1)}, 10, 0},
150 {{(uintptr_t)_GEN_HEAD(2), (uintptr_t)_GEN_HEAD(2)}, 10, 0},
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600151 };
152 for (int i = 0; i < NUM_GENERATIONS; i++) {
Victor Stinner67e0de62019-11-20 11:48:18 +0100153 gcstate->generations[i] = generations[i];
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600154 };
Victor Stinner67e0de62019-11-20 11:48:18 +0100155 gcstate->generation0 = GEN_HEAD(gcstate, 0);
brainfvckc75edab2017-10-16 12:49:41 -0700156 struct gc_generation permanent_generation = {
Victor Stinner67e0de62019-11-20 11:48:18 +0100157 {(uintptr_t)&gcstate->permanent_generation.head,
158 (uintptr_t)&gcstate->permanent_generation.head}, 0, 0
brainfvckc75edab2017-10-16 12:49:41 -0700159 };
Victor Stinner67e0de62019-11-20 11:48:18 +0100160 gcstate->permanent_generation = permanent_generation;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600161}
Antoine Pitroud4156c12012-10-30 22:43:19 +0100162
Victor Stinner444b39b2019-11-20 01:18:11 +0100163
164PyStatus
Victor Stinner01b1cc12019-11-20 02:27:56 +0100165_PyGC_Init(PyThreadState *tstate)
Victor Stinner444b39b2019-11-20 01:18:11 +0100166{
Victor Stinner72474072019-11-20 12:25:50 +0100167 GCState *gcstate = &tstate->interp->gc;
Victor Stinner67e0de62019-11-20 11:48:18 +0100168 if (gcstate->garbage == NULL) {
169 gcstate->garbage = PyList_New(0);
170 if (gcstate->garbage == NULL) {
Victor Stinner444b39b2019-11-20 01:18:11 +0100171 return _PyStatus_NO_MEMORY();
172 }
173 }
174 return _PyStatus_OK();
175}
176
177
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900178/*
179_gc_prev values
180---------------
Neil Schemenauer43411b52001-08-30 00:05:51 +0000181
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900182Between collections, _gc_prev is used for doubly linked list.
Tim Peters6fc13d92002-07-02 18:12:35 +0000183
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900184Lowest two bits of _gc_prev are used for flags.
185PREV_MASK_COLLECTING is used only while collecting and cleared before GC ends
186or _PyObject_GC_UNTRACK() is called.
Tim Peters6fc13d92002-07-02 18:12:35 +0000187
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900188During a collection, _gc_prev is temporary used for gc_refs, and the gc list
189is singly linked until _gc_prev is restored.
Tim Peters6fc13d92002-07-02 18:12:35 +0000190
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900191gc_refs
Tim Peters6fc13d92002-07-02 18:12:35 +0000192 At the start of a collection, update_refs() copies the true refcount
193 to gc_refs, for each object in the generation being collected.
194 subtract_refs() then adjusts gc_refs so that it equals the number of
195 times an object is referenced directly from outside the generation
196 being collected.
Tim Peters6fc13d92002-07-02 18:12:35 +0000197
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900198PREV_MASK_COLLECTING
199 Objects in generation being collected are marked PREV_MASK_COLLECTING in
200 update_refs().
201
202
203_gc_next values
204---------------
205
206_gc_next takes these values:
207
2080
209 The object is not tracked
210
211!= 0
212 Pointer to the next object in the GC list.
213 Additionally, lowest bit is used temporary for
214 NEXT_MASK_UNREACHABLE flag described below.
215
216NEXT_MASK_UNREACHABLE
Tim Peters6fc13d92002-07-02 18:12:35 +0000217 move_unreachable() then moves objects not reachable (whether directly or
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900218 indirectly) from outside the generation into an "unreachable" set and
219 set this flag.
Tim Peters6fc13d92002-07-02 18:12:35 +0000220
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900221 Objects that are found to be reachable have gc_refs set to 1.
222 When this flag is set for the reachable object, the object must be in
223 "unreachable" set.
224 The flag is unset and the object is moved back to "reachable" set.
225
226 move_legacy_finalizers() will remove this flag from "unreachable" set.
Tim Peters6fc13d92002-07-02 18:12:35 +0000227*/
Neil Schemenauera2b11ec2002-05-21 15:53:24 +0000228
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000229/*** list functions ***/
230
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900231static inline void
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000232gc_list_init(PyGC_Head *list)
233{
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900234 // List header must not have flags.
235 // We can assign pointer by simple cast.
236 list->_gc_prev = (uintptr_t)list;
237 list->_gc_next = (uintptr_t)list;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000238}
239
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900240static inline int
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000241gc_list_is_empty(PyGC_Head *list)
242{
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900243 return (list->_gc_next == (uintptr_t)list);
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000244}
245
Tim Peterse2d59182004-11-01 01:39:08 +0000246/* Append `node` to `list`. */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900247static inline void
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000248gc_list_append(PyGC_Head *node, PyGC_Head *list)
249{
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900250 PyGC_Head *last = (PyGC_Head *)list->_gc_prev;
251
252 // last <-> node
253 _PyGCHead_SET_PREV(node, last);
254 _PyGCHead_SET_NEXT(last, node);
255
256 // node <-> list
257 _PyGCHead_SET_NEXT(node, list);
258 list->_gc_prev = (uintptr_t)node;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000259}
260
Tim Peterse2d59182004-11-01 01:39:08 +0000261/* Remove `node` from the gc list it's currently in. */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900262static inline void
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000263gc_list_remove(PyGC_Head *node)
264{
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900265 PyGC_Head *prev = GC_PREV(node);
266 PyGC_Head *next = GC_NEXT(node);
267
268 _PyGCHead_SET_NEXT(prev, next);
269 _PyGCHead_SET_PREV(next, prev);
270
271 node->_gc_next = 0; /* object is not currently tracked */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000272}
273
Tim Peterse2d59182004-11-01 01:39:08 +0000274/* Move `node` from the gc list it's currently in (which is not explicitly
275 * named here) to the end of `list`. This is semantically the same as
276 * gc_list_remove(node) followed by gc_list_append(node, list).
277 */
278static void
279gc_list_move(PyGC_Head *node, PyGC_Head *list)
280{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000281 /* Unlink from current list. */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900282 PyGC_Head *from_prev = GC_PREV(node);
283 PyGC_Head *from_next = GC_NEXT(node);
284 _PyGCHead_SET_NEXT(from_prev, from_next);
285 _PyGCHead_SET_PREV(from_next, from_prev);
286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 /* Relink at end of new list. */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900288 // list must not have flags. So we can skip macros.
289 PyGC_Head *to_prev = (PyGC_Head*)list->_gc_prev;
290 _PyGCHead_SET_PREV(node, to_prev);
291 _PyGCHead_SET_NEXT(to_prev, node);
292 list->_gc_prev = (uintptr_t)node;
293 _PyGCHead_SET_NEXT(node, list);
Tim Peterse2d59182004-11-01 01:39:08 +0000294}
295
296/* append list `from` onto list `to`; `from` becomes an empty list */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000297static void
298gc_list_merge(PyGC_Head *from, PyGC_Head *to)
299{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 assert(from != to);
301 if (!gc_list_is_empty(from)) {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900302 PyGC_Head *to_tail = GC_PREV(to);
303 PyGC_Head *from_head = GC_NEXT(from);
304 PyGC_Head *from_tail = GC_PREV(from);
305 assert(from_head != from);
306 assert(from_tail != from);
307
308 _PyGCHead_SET_NEXT(to_tail, from_head);
309 _PyGCHead_SET_PREV(from_head, to_tail);
310
311 _PyGCHead_SET_NEXT(from_tail, to);
312 _PyGCHead_SET_PREV(to, from_tail);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313 }
314 gc_list_init(from);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000315}
316
Neal Norwitz7b216c52006-03-04 20:01:53 +0000317static Py_ssize_t
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000318gc_list_size(PyGC_Head *list)
319{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000320 PyGC_Head *gc;
321 Py_ssize_t n = 0;
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900322 for (gc = GC_NEXT(list); gc != list; gc = GC_NEXT(gc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323 n++;
324 }
325 return n;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000326}
327
Pablo Galindo466326d2019-10-13 16:48:59 +0100328/* Walk the list and mark all objects as non-collecting */
329static inline void
330gc_list_clear_collecting(PyGC_Head *collectable)
331{
332 PyGC_Head *gc;
333 for (gc = GC_NEXT(collectable); gc != collectable; gc = GC_NEXT(gc)) {
334 gc_clear_collecting(gc);
335 }
336}
337
Tim Peters259272b2003-04-06 19:41:39 +0000338/* Append objects in a GC list to a Python list.
Pablo Galindo466326d2019-10-13 16:48:59 +0100339 * Return 0 if all OK, < 0 if error (out of memory for list)
Tim Peters259272b2003-04-06 19:41:39 +0000340 */
341static int
342append_objects(PyObject *py_list, PyGC_Head *gc_list)
343{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000344 PyGC_Head *gc;
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900345 for (gc = GC_NEXT(gc_list); gc != gc_list; gc = GC_NEXT(gc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 PyObject *op = FROM_GC(gc);
347 if (op != py_list) {
348 if (PyList_Append(py_list, op)) {
349 return -1; /* exception */
350 }
351 }
352 }
353 return 0;
Tim Peters259272b2003-04-06 19:41:39 +0000354}
355
Tim Petersea55c512019-10-18 20:59:14 -0500356// Constants for validate_list's flags argument.
357enum flagstates {collecting_clear_unreachable_clear,
358 collecting_clear_unreachable_set,
359 collecting_set_unreachable_clear,
360 collecting_set_unreachable_set};
361
Pablo Galindo320dd502019-10-10 22:45:17 +0100362#ifdef GC_DEBUG
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900363// validate_list checks list consistency. And it works as document
Tim Peters95bfc8a2019-10-13 16:47:04 -0500364// describing when flags are expected to be set / unset.
365// `head` must be a doubly-linked gc list, although it's fine (expected!) if
366// the prev and next pointers are "polluted" with flags.
367// What's checked:
368// - The `head` pointers are not polluted.
Tim Petersea55c512019-10-18 20:59:14 -0500369// - The objects' PREV_MASK_COLLECTING and NEXT_MASK_UNREACHABLE flags are all
370// `set or clear, as specified by the 'flags' argument.
Tim Peters95bfc8a2019-10-13 16:47:04 -0500371// - The prev and next pointers are mutually consistent.
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900372static void
Tim Petersea55c512019-10-18 20:59:14 -0500373validate_list(PyGC_Head *head, enum flagstates flags)
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900374{
Tim Peters95bfc8a2019-10-13 16:47:04 -0500375 assert((head->_gc_prev & PREV_MASK_COLLECTING) == 0);
376 assert((head->_gc_next & NEXT_MASK_UNREACHABLE) == 0);
Tim Petersea55c512019-10-18 20:59:14 -0500377 uintptr_t prev_value = 0, next_value = 0;
378 switch (flags) {
379 case collecting_clear_unreachable_clear:
380 break;
381 case collecting_set_unreachable_clear:
382 prev_value = PREV_MASK_COLLECTING;
383 break;
384 case collecting_clear_unreachable_set:
385 next_value = NEXT_MASK_UNREACHABLE;
386 break;
387 case collecting_set_unreachable_set:
388 prev_value = PREV_MASK_COLLECTING;
389 next_value = NEXT_MASK_UNREACHABLE;
390 break;
391 default:
392 assert(! "bad internal flags argument");
393 }
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900394 PyGC_Head *prev = head;
395 PyGC_Head *gc = GC_NEXT(head);
396 while (gc != head) {
Tim Peters95bfc8a2019-10-13 16:47:04 -0500397 PyGC_Head *trueprev = GC_PREV(gc);
398 PyGC_Head *truenext = (PyGC_Head *)(gc->_gc_next & ~NEXT_MASK_UNREACHABLE);
399 assert(truenext != NULL);
400 assert(trueprev == prev);
401 assert((gc->_gc_prev & PREV_MASK_COLLECTING) == prev_value);
402 assert((gc->_gc_next & NEXT_MASK_UNREACHABLE) == next_value);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900403 prev = gc;
Tim Peters95bfc8a2019-10-13 16:47:04 -0500404 gc = truenext;
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900405 }
406 assert(prev == GC_PREV(head));
407}
408#else
Tim Petersea55c512019-10-18 20:59:14 -0500409#define validate_list(x, y) do{}while(0)
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900410#endif
411
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000412/*** end of list stuff ***/
413
414
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900415/* Set all gc_refs = ob_refcnt. After this, gc_refs is > 0 and
416 * PREV_MASK_COLLECTING bit is set for all objects in containers.
Tim Peters88396172002-06-30 17:56:40 +0000417 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000418static void
419update_refs(PyGC_Head *containers)
420{
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900421 PyGC_Head *gc = GC_NEXT(containers);
422 for (; gc != containers; gc = GC_NEXT(gc)) {
423 gc_reset_refs(gc, Py_REFCNT(FROM_GC(gc)));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 /* Python's cyclic gc should never see an incoming refcount
425 * of 0: if something decref'ed to 0, it should have been
426 * deallocated immediately at that time.
427 * Possible cause (if the assert triggers): a tp_dealloc
428 * routine left a gc-aware object tracked during its teardown
429 * phase, and did something-- or allowed something to happen --
430 * that called back into Python. gc can trigger then, and may
431 * see the still-tracked dying object. Before this assert
432 * was added, such mistakes went on to allow gc to try to
433 * delete the object again. In a debug build, that caused
434 * a mysterious segfault, when _Py_ForgetReference tried
435 * to remove the object from the doubly-linked list of all
436 * objects a second time. In a release build, an actual
437 * double deallocation occurred, which leads to corruption
438 * of the allocator's internal bookkeeping pointers. That's
439 * so serious that maybe this should be a release-build
440 * check instead of an assert?
441 */
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200442 _PyObject_ASSERT(FROM_GC(gc), gc_get_refs(gc) != 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000444}
445
Tim Peters19b74c72002-07-01 03:52:19 +0000446/* A traversal callback for subtract_refs. */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000447static int
Victor Stinner4d5f94b2019-10-08 02:37:38 +0200448visit_decref(PyObject *op, void *parent)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000449{
Victor Stinner4d5f94b2019-10-08 02:37:38 +0200450 _PyObject_ASSERT(_PyObject_CAST(parent), !_PyObject_IsFreed(op));
Victor Stinnerd91d4de2019-09-09 17:44:59 +0200451
Hai Shi675d9a32020-04-15 02:11:20 +0800452 if (_PyObject_IS_GC(op)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 PyGC_Head *gc = AS_GC(op);
454 /* We're only interested in gc_refs for objects in the
455 * generation being collected, which can be recognized
456 * because only they have positive gc_refs.
457 */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900458 if (gc_is_collecting(gc)) {
459 gc_decref(gc);
460 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 }
462 return 0;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000463}
464
Tim Peters19b74c72002-07-01 03:52:19 +0000465/* Subtract internal references from gc_refs. After this, gc_refs is >= 0
466 * for all objects in containers, and is GC_REACHABLE for all tracked gc
467 * objects not in containers. The ones with gc_refs > 0 are directly
468 * reachable from outside containers, and so can't be collected.
469 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000470static void
471subtract_refs(PyGC_Head *containers)
472{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000473 traverseproc traverse;
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900474 PyGC_Head *gc = GC_NEXT(containers);
475 for (; gc != containers; gc = GC_NEXT(gc)) {
Victor Stinner4d5f94b2019-10-08 02:37:38 +0200476 PyObject *op = FROM_GC(gc);
477 traverse = Py_TYPE(op)->tp_traverse;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 (void) traverse(FROM_GC(gc),
479 (visitproc)visit_decref,
Victor Stinner4d5f94b2019-10-08 02:37:38 +0200480 op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000481 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000482}
483
Tim Peters19b74c72002-07-01 03:52:19 +0000484/* A traversal callback for move_unreachable. */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000485static int
Tim Peters19b74c72002-07-01 03:52:19 +0000486visit_reachable(PyObject *op, PyGC_Head *reachable)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000487{
Hai Shi675d9a32020-04-15 02:11:20 +0800488 if (!_PyObject_IS_GC(op)) {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900489 return 0;
490 }
Tim Peters19b74c72002-07-01 03:52:19 +0000491
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900492 PyGC_Head *gc = AS_GC(op);
493 const Py_ssize_t gc_refs = gc_get_refs(gc);
494
Tim Peters1e739452019-10-21 11:21:35 -0500495 // Ignore objects in other generation.
Tim Peters95bfc8a2019-10-13 16:47:04 -0500496 // This also skips objects "to the left" of the current position in
497 // move_unreachable's scan of the 'young' list - they've already been
498 // traversed, and no longer have the PREV_MASK_COLLECTING flag.
Tim Peters1e739452019-10-21 11:21:35 -0500499 if (! gc_is_collecting(gc)) {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900500 return 0;
501 }
Tim Peters1e739452019-10-21 11:21:35 -0500502 // It would be a logic error elsewhere if the collecting flag were set on
503 // an untracked object.
504 assert(gc->_gc_next != 0);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900505
506 if (gc->_gc_next & NEXT_MASK_UNREACHABLE) {
507 /* This had gc_refs = 0 when move_unreachable got
508 * to it, but turns out it's reachable after all.
509 * Move it back to move_unreachable's 'young' list,
510 * and move_unreachable will eventually get to it
511 * again.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 */
Tim Peters95bfc8a2019-10-13 16:47:04 -0500513 // Manually unlink gc from unreachable list because the list functions
514 // don't work right in the presence of NEXT_MASK_UNREACHABLE flags.
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900515 PyGC_Head *prev = GC_PREV(gc);
516 PyGC_Head *next = (PyGC_Head*)(gc->_gc_next & ~NEXT_MASK_UNREACHABLE);
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200517 _PyObject_ASSERT(FROM_GC(prev),
518 prev->_gc_next & NEXT_MASK_UNREACHABLE);
519 _PyObject_ASSERT(FROM_GC(next),
520 next->_gc_next & NEXT_MASK_UNREACHABLE);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900521 prev->_gc_next = gc->_gc_next; // copy NEXT_MASK_UNREACHABLE
522 _PyGCHead_SET_PREV(next, prev);
523
524 gc_list_append(gc, reachable);
525 gc_set_refs(gc, 1);
526 }
527 else if (gc_refs == 0) {
528 /* This is in move_unreachable's 'young' list, but
529 * the traversal hasn't yet gotten to it. All
530 * we need to do is tell move_unreachable that it's
531 * reachable.
532 */
533 gc_set_refs(gc, 1);
534 }
535 /* Else there's nothing to do.
536 * If gc_refs > 0, it must be in move_unreachable's 'young'
537 * list, and move_unreachable will eventually get to it.
538 */
539 else {
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200540 _PyObject_ASSERT_WITH_MSG(op, gc_refs > 0, "refcount is too small");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 }
542 return 0;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000543}
544
Tim Peters19b74c72002-07-01 03:52:19 +0000545/* Move the unreachable objects from young to unreachable. After this,
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900546 * all objects in young don't have PREV_MASK_COLLECTING flag and
547 * unreachable have the flag.
Tim Peters19b74c72002-07-01 03:52:19 +0000548 * All objects in young after this are directly or indirectly reachable
549 * from outside the original young; and all objects in unreachable are
550 * not.
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900551 *
552 * This function restores _gc_prev pointer. young and unreachable are
553 * doubly linked list after this function.
554 * But _gc_next in unreachable list has NEXT_MASK_UNREACHABLE flag.
555 * So we can not gc_list_* functions for unreachable until we remove the flag.
Tim Peters88396172002-06-30 17:56:40 +0000556 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000557static void
Tim Peters19b74c72002-07-01 03:52:19 +0000558move_unreachable(PyGC_Head *young, PyGC_Head *unreachable)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000559{
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900560 // previous elem in the young list, used for restore gc_prev.
561 PyGC_Head *prev = young;
562 PyGC_Head *gc = GC_NEXT(young);
Tim Peters19b74c72002-07-01 03:52:19 +0000563
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900564 /* Invariants: all objects "to the left" of us in young are reachable
565 * (directly or indirectly) from outside the young list as it was at entry.
566 *
567 * All other objects from the original young "to the left" of us are in
568 * unreachable now, and have NEXT_MASK_UNREACHABLE. All objects to the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 * left of us in 'young' now have been scanned, and no objects here
570 * or to the right have been scanned yet.
571 */
Tim Peters19b74c72002-07-01 03:52:19 +0000572
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 while (gc != young) {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900574 if (gc_get_refs(gc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 /* gc is definitely reachable from outside the
576 * original 'young'. Mark it as such, and traverse
577 * its pointers to find any other objects that may
578 * be directly reachable from it. Note that the
579 * call to tp_traverse may append objects to young,
580 * so we have to wait until it returns to determine
581 * the next object to visit.
582 */
583 PyObject *op = FROM_GC(gc);
584 traverseproc traverse = Py_TYPE(op)->tp_traverse;
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200585 _PyObject_ASSERT_WITH_MSG(op, gc_get_refs(gc) > 0,
586 "refcount is too small");
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900587 // NOTE: visit_reachable may change gc->_gc_next when
588 // young->_gc_prev == gc. Don't do gc = GC_NEXT(gc) before!
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 (void) traverse(op,
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900590 (visitproc)visit_reachable,
591 (void *)young);
592 // relink gc_prev to prev element.
593 _PyGCHead_SET_PREV(gc, prev);
Quan Tian3bd0d622018-10-20 05:30:03 +0800594 // gc is not COLLECTING state after here.
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900595 gc_clear_collecting(gc);
596 prev = gc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 }
598 else {
599 /* This *may* be unreachable. To make progress,
600 * assume it is. gc isn't directly reachable from
601 * any object we've already traversed, but may be
602 * reachable from an object we haven't gotten to yet.
603 * visit_reachable will eventually move gc back into
604 * young if that's so, and we'll see it again.
605 */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900606 // Move gc to unreachable.
607 // No need to gc->next->prev = prev because it is single linked.
608 prev->_gc_next = gc->_gc_next;
609
610 // We can't use gc_list_append() here because we use
611 // NEXT_MASK_UNREACHABLE here.
612 PyGC_Head *last = GC_PREV(unreachable);
613 // NOTE: Since all objects in unreachable set has
614 // NEXT_MASK_UNREACHABLE flag, we set it unconditionally.
Tim Peters95bfc8a2019-10-13 16:47:04 -0500615 // But this may pollute the unreachable list head's 'next' pointer
616 // too. That's semantically senseless but expedient here - the
Pablo Galindo97f12672020-01-13 12:25:05 +0000617 // damage is repaired when this function ends.
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900618 last->_gc_next = (NEXT_MASK_UNREACHABLE | (uintptr_t)gc);
619 _PyGCHead_SET_PREV(gc, last);
620 gc->_gc_next = (NEXT_MASK_UNREACHABLE | (uintptr_t)unreachable);
621 unreachable->_gc_prev = (uintptr_t)gc;
622 }
623 gc = (PyGC_Head*)prev->_gc_next;
624 }
625 // young->_gc_prev must be last element remained in the list.
626 young->_gc_prev = (uintptr_t)prev;
Tim Peters95bfc8a2019-10-13 16:47:04 -0500627 // don't let the pollution of the list head's next pointer leak
628 unreachable->_gc_next &= ~NEXT_MASK_UNREACHABLE;
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900629}
630
631static void
632untrack_tuples(PyGC_Head *head)
633{
634 PyGC_Head *next, *gc = GC_NEXT(head);
635 while (gc != head) {
636 PyObject *op = FROM_GC(gc);
637 next = GC_NEXT(gc);
638 if (PyTuple_CheckExact(op)) {
639 _PyTuple_MaybeUntrack(op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000640 }
641 gc = next;
642 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000643}
644
Antoine Pitroue1ad3da2012-05-28 22:22:34 +0200645/* Try to untrack all currently tracked dictionaries */
646static void
647untrack_dicts(PyGC_Head *head)
648{
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900649 PyGC_Head *next, *gc = GC_NEXT(head);
Antoine Pitroue1ad3da2012-05-28 22:22:34 +0200650 while (gc != head) {
651 PyObject *op = FROM_GC(gc);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900652 next = GC_NEXT(gc);
653 if (PyDict_CheckExact(op)) {
Antoine Pitroue1ad3da2012-05-28 22:22:34 +0200654 _PyDict_MaybeUntrack(op);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900655 }
Antoine Pitroue1ad3da2012-05-28 22:22:34 +0200656 gc = next;
657 }
658}
659
Antoine Pitrou796564c2013-07-30 19:59:21 +0200660/* Return true if object has a pre-PEP 442 finalization method. */
Neil Schemenauera765c122001-11-01 17:35:23 +0000661static int
Antoine Pitrou796564c2013-07-30 19:59:21 +0200662has_legacy_finalizer(PyObject *op)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000663{
Victor Stinnerdaa97562020-02-07 03:37:06 +0100664 return Py_TYPE(op)->tp_del != NULL;
Neil Schemenauera765c122001-11-01 17:35:23 +0000665}
666
Antoine Pitrou796564c2013-07-30 19:59:21 +0200667/* Move the objects in unreachable with tp_del slots into `finalizers`.
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900668 *
669 * This function also removes NEXT_MASK_UNREACHABLE flag
670 * from _gc_next in unreachable.
Jeremy Hyltonce136e92003-04-04 19:59:06 +0000671 */
Neil Schemenauera765c122001-11-01 17:35:23 +0000672static void
Antoine Pitrou796564c2013-07-30 19:59:21 +0200673move_legacy_finalizers(PyGC_Head *unreachable, PyGC_Head *finalizers)
Neil Schemenauera765c122001-11-01 17:35:23 +0000674{
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900675 PyGC_Head *gc, *next;
Tim Peters95bfc8a2019-10-13 16:47:04 -0500676 assert((unreachable->_gc_next & NEXT_MASK_UNREACHABLE) == 0);
Tim Petersf6b80452003-04-07 19:21:15 +0000677
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 /* March over unreachable. Move objects with finalizers into
679 * `finalizers`.
680 */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900681 for (gc = GC_NEXT(unreachable); gc != unreachable; gc = next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 PyObject *op = FROM_GC(gc);
Jeremy Hyltonce136e92003-04-04 19:59:06 +0000683
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200684 _PyObject_ASSERT(op, gc->_gc_next & NEXT_MASK_UNREACHABLE);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900685 gc->_gc_next &= ~NEXT_MASK_UNREACHABLE;
686 next = (PyGC_Head*)gc->_gc_next;
Tim Petersf6ae7a42003-04-05 18:40:50 +0000687
Antoine Pitrou796564c2013-07-30 19:59:21 +0200688 if (has_legacy_finalizer(op)) {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900689 gc_clear_collecting(gc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000690 gc_list_move(gc, finalizers);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000691 }
692 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000693}
694
Pablo Galindo466326d2019-10-13 16:48:59 +0100695static inline void
696clear_unreachable_mask(PyGC_Head *unreachable)
697{
698 /* Check that the list head does not have the unreachable bit set */
699 assert(((uintptr_t)unreachable & NEXT_MASK_UNREACHABLE) == 0);
700
701 PyGC_Head *gc, *next;
Tim Peters95bfc8a2019-10-13 16:47:04 -0500702 assert((unreachable->_gc_next & NEXT_MASK_UNREACHABLE) == 0);
Pablo Galindo466326d2019-10-13 16:48:59 +0100703 for (gc = GC_NEXT(unreachable); gc != unreachable; gc = next) {
704 _PyObject_ASSERT((PyObject*)FROM_GC(gc), gc->_gc_next & NEXT_MASK_UNREACHABLE);
705 gc->_gc_next &= ~NEXT_MASK_UNREACHABLE;
706 next = (PyGC_Head*)gc->_gc_next;
707 }
Tim Petersea55c512019-10-18 20:59:14 -0500708 validate_list(unreachable, collecting_set_unreachable_clear);
Pablo Galindo466326d2019-10-13 16:48:59 +0100709}
710
Antoine Pitrou796564c2013-07-30 19:59:21 +0200711/* A traversal callback for move_legacy_finalizer_reachable. */
Tim Peters19b74c72002-07-01 03:52:19 +0000712static int
713visit_move(PyObject *op, PyGC_Head *tolist)
714{
Hai Shi675d9a32020-04-15 02:11:20 +0800715 if (_PyObject_IS_GC(op)) {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900716 PyGC_Head *gc = AS_GC(op);
717 if (gc_is_collecting(gc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 gc_list_move(gc, tolist);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900719 gc_clear_collecting(gc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000720 }
721 }
722 return 0;
Tim Peters19b74c72002-07-01 03:52:19 +0000723}
724
725/* Move objects that are reachable from finalizers, from the unreachable set
Tim Petersf6b80452003-04-07 19:21:15 +0000726 * into finalizers set.
Tim Peters19b74c72002-07-01 03:52:19 +0000727 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000728static void
Antoine Pitrou796564c2013-07-30 19:59:21 +0200729move_legacy_finalizer_reachable(PyGC_Head *finalizers)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000730{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000731 traverseproc traverse;
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900732 PyGC_Head *gc = GC_NEXT(finalizers);
733 for (; gc != finalizers; gc = GC_NEXT(gc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000734 /* Note that the finalizers list may grow during this. */
735 traverse = Py_TYPE(FROM_GC(gc))->tp_traverse;
736 (void) traverse(FROM_GC(gc),
737 (visitproc)visit_move,
738 (void *)finalizers);
739 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000740}
741
Tim Petersead8b7a2004-10-30 23:09:22 +0000742/* Clear all weakrefs to unreachable objects, and if such a weakref has a
743 * callback, invoke it if necessary. Note that it's possible for such
744 * weakrefs to be outside the unreachable set -- indeed, those are precisely
745 * the weakrefs whose callbacks must be invoked. See gc_weakref.txt for
746 * overview & some details. Some weakrefs with callbacks may be reclaimed
747 * directly by this routine; the number reclaimed is the return value. Other
748 * weakrefs with callbacks may be moved into the `old` generation. Objects
749 * moved into `old` have gc_refs set to GC_REACHABLE; the objects remaining in
750 * unreachable are left at GC_TENTATIVELY_UNREACHABLE. When this returns,
751 * no object in `unreachable` is weakly referenced anymore.
Tim Peters403a2032003-11-20 21:21:46 +0000752 */
753static int
Tim Petersead8b7a2004-10-30 23:09:22 +0000754handle_weakrefs(PyGC_Head *unreachable, PyGC_Head *old)
Tim Peters403a2032003-11-20 21:21:46 +0000755{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000756 PyGC_Head *gc;
757 PyObject *op; /* generally FROM_GC(gc) */
758 PyWeakReference *wr; /* generally a cast of op */
759 PyGC_Head wrcb_to_call; /* weakrefs with callbacks to call */
760 PyGC_Head *next;
761 int num_freed = 0;
Tim Peters403a2032003-11-20 21:21:46 +0000762
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000763 gc_list_init(&wrcb_to_call);
Tim Peters403a2032003-11-20 21:21:46 +0000764
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 /* Clear all weakrefs to the objects in unreachable. If such a weakref
766 * also has a callback, move it into `wrcb_to_call` if the callback
767 * needs to be invoked. Note that we cannot invoke any callbacks until
768 * all weakrefs to unreachable objects are cleared, lest the callback
769 * resurrect an unreachable object via a still-active weakref. We
770 * make another pass over wrcb_to_call, invoking callbacks, after this
771 * pass completes.
772 */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900773 for (gc = GC_NEXT(unreachable); gc != unreachable; gc = next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000774 PyWeakReference **wrlist;
Tim Petersead8b7a2004-10-30 23:09:22 +0000775
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000776 op = FROM_GC(gc);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900777 next = GC_NEXT(gc);
Tim Petersead8b7a2004-10-30 23:09:22 +0000778
Neil Schemenauerbcda4602019-09-30 10:06:45 -0700779 if (PyWeakref_Check(op)) {
780 /* A weakref inside the unreachable set must be cleared. If we
781 * allow its callback to execute inside delete_garbage(), it
782 * could expose objects that have tp_clear already called on
783 * them. Or, it could resurrect unreachable objects. One way
784 * this can happen is if some container objects do not implement
785 * tp_traverse. Then, wr_object can be outside the unreachable
786 * set but can be deallocated as a result of breaking the
787 * reference cycle. If we don't clear the weakref, the callback
788 * will run and potentially cause a crash. See bpo-38006 for
789 * one example.
790 */
791 _PyWeakref_ClearRef((PyWeakReference *)op);
792 }
793
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 if (! PyType_SUPPORTS_WEAKREFS(Py_TYPE(op)))
795 continue;
Tim Petersead8b7a2004-10-30 23:09:22 +0000796
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 /* It supports weakrefs. Does it have any? */
798 wrlist = (PyWeakReference **)
Victor Stinner38aefc52020-04-06 14:07:02 +0200799 _PyObject_GET_WEAKREFS_LISTPTR(op);
Tim Petersead8b7a2004-10-30 23:09:22 +0000800
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 /* `op` may have some weakrefs. March over the list, clear
802 * all the weakrefs, and move the weakrefs with callbacks
803 * that must be called into wrcb_to_call.
804 */
805 for (wr = *wrlist; wr != NULL; wr = *wrlist) {
806 PyGC_Head *wrasgc; /* AS_GC(wr) */
Tim Petersead8b7a2004-10-30 23:09:22 +0000807
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 /* _PyWeakref_ClearRef clears the weakref but leaves
809 * the callback pointer intact. Obscure: it also
810 * changes *wrlist.
811 */
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200812 _PyObject_ASSERT((PyObject *)wr, wr->wr_object == op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 _PyWeakref_ClearRef(wr);
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200814 _PyObject_ASSERT((PyObject *)wr, wr->wr_object == Py_None);
815 if (wr->wr_callback == NULL) {
816 /* no callback */
817 continue;
818 }
Tim Petersead8b7a2004-10-30 23:09:22 +0000819
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200820 /* Headache time. `op` is going away, and is weakly referenced by
821 * `wr`, which has a callback. Should the callback be invoked? If wr
822 * is also trash, no:
823 *
824 * 1. There's no need to call it. The object and the weakref are
825 * both going away, so it's legitimate to pretend the weakref is
826 * going away first. The user has to ensure a weakref outlives its
827 * referent if they want a guarantee that the wr callback will get
828 * invoked.
829 *
830 * 2. It may be catastrophic to call it. If the callback is also in
831 * cyclic trash (CT), then although the CT is unreachable from
832 * outside the current generation, CT may be reachable from the
833 * callback. Then the callback could resurrect insane objects.
834 *
835 * Since the callback is never needed and may be unsafe in this case,
836 * wr is simply left in the unreachable set. Note that because we
837 * already called _PyWeakref_ClearRef(wr), its callback will never
838 * trigger.
839 *
840 * OTOH, if wr isn't part of CT, we should invoke the callback: the
841 * weakref outlived the trash. Note that since wr isn't CT in this
842 * case, its callback can't be CT either -- wr acted as an external
843 * root to this generation, and therefore its callback did too. So
844 * nothing in CT is reachable from the callback either, so it's hard
845 * to imagine how calling it later could create a problem for us. wr
846 * is moved to wrcb_to_call in this case.
847 */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900848 if (gc_is_collecting(AS_GC(wr))) {
Neil Schemenauerbcda4602019-09-30 10:06:45 -0700849 /* it should already have been cleared above */
850 assert(wr->wr_object == Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851 continue;
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900852 }
Tim Peterscc2a8662004-10-31 22:12:43 +0000853
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 /* Create a new reference so that wr can't go away
855 * before we can process it again.
856 */
857 Py_INCREF(wr);
Tim Petersead8b7a2004-10-30 23:09:22 +0000858
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000859 /* Move wr to wrcb_to_call, for the next pass. */
860 wrasgc = AS_GC(wr);
861 assert(wrasgc != next); /* wrasgc is reachable, but
862 next isn't, so they can't
863 be the same */
864 gc_list_move(wrasgc, &wrcb_to_call);
865 }
866 }
Tim Petersead8b7a2004-10-30 23:09:22 +0000867
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 /* Invoke the callbacks we decided to honor. It's safe to invoke them
869 * because they can't reference unreachable objects.
870 */
871 while (! gc_list_is_empty(&wrcb_to_call)) {
872 PyObject *temp;
873 PyObject *callback;
Tim Petersead8b7a2004-10-30 23:09:22 +0000874
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900875 gc = (PyGC_Head*)wrcb_to_call._gc_next;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876 op = FROM_GC(gc);
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200877 _PyObject_ASSERT(op, PyWeakref_Check(op));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 wr = (PyWeakReference *)op;
879 callback = wr->wr_callback;
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200880 _PyObject_ASSERT(op, callback != NULL);
Tim Petersead8b7a2004-10-30 23:09:22 +0000881
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 /* copy-paste of weakrefobject.c's handle_callback() */
Petr Viktorinffd97532020-02-11 17:46:57 +0100883 temp = PyObject_CallOneArg(callback, (PyObject *)wr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000884 if (temp == NULL)
885 PyErr_WriteUnraisable(callback);
886 else
887 Py_DECREF(temp);
Tim Petersead8b7a2004-10-30 23:09:22 +0000888
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000889 /* Give up the reference we created in the first pass. When
890 * op's refcount hits 0 (which it may or may not do right now),
891 * op's tp_dealloc will decref op->wr_callback too. Note
892 * that the refcount probably will hit 0 now, and because this
893 * weakref was reachable to begin with, gc didn't already
894 * add it to its count of freed objects. Example: a reachable
895 * weak value dict maps some key to this reachable weakref.
896 * The callback removes this key->weakref mapping from the
897 * dict, leaving no other references to the weakref (excepting
898 * ours).
899 */
900 Py_DECREF(op);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900901 if (wrcb_to_call._gc_next == (uintptr_t)gc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 /* object is still alive -- move it */
903 gc_list_move(gc, old);
904 }
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900905 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 ++num_freed;
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900907 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908 }
Tim Petersead8b7a2004-10-30 23:09:22 +0000909
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 return num_freed;
Tim Peters403a2032003-11-20 21:21:46 +0000911}
912
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000913static void
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200914debug_cycle(const char *msg, PyObject *op)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000915{
Victor Stinner499dfcf2011-03-21 13:26:24 +0100916 PySys_FormatStderr("gc: %s <%s %p>\n",
917 msg, Py_TYPE(op)->tp_name, op);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000918}
919
Antoine Pitrou796564c2013-07-30 19:59:21 +0200920/* Handle uncollectable garbage (cycles with tp_del slots, and stuff reachable
Tim Petersbf384c22003-04-06 00:11:39 +0000921 * only from such cycles).
Tim Petersf6b80452003-04-07 19:21:15 +0000922 * If DEBUG_SAVEALL, all objects in finalizers are appended to the module
923 * garbage list (a Python list), else only the objects in finalizers with
924 * __del__ methods are appended to garbage. All objects in finalizers are
925 * merged into the old list regardless.
Tim Petersbf384c22003-04-06 00:11:39 +0000926 */
Serhiy Storchaka301e3cc2018-05-24 15:19:29 +0300927static void
Victor Stinner2e969062019-11-20 01:49:32 +0100928handle_legacy_finalizers(PyThreadState *tstate,
Victor Stinner67e0de62019-11-20 11:48:18 +0100929 GCState *gcstate,
Victor Stinner9db03242019-04-26 02:32:01 +0200930 PyGC_Head *finalizers, PyGC_Head *old)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000931{
Victor Stinner2e969062019-11-20 01:49:32 +0100932 assert(!_PyErr_Occurred(tstate));
Victor Stinner67e0de62019-11-20 11:48:18 +0100933 assert(gcstate->garbage != NULL);
Victor Stinner9db03242019-04-26 02:32:01 +0200934
935 PyGC_Head *gc = GC_NEXT(finalizers);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900936 for (; gc != finalizers; gc = GC_NEXT(gc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 PyObject *op = FROM_GC(gc);
Tim Petersf6b80452003-04-07 19:21:15 +0000938
Victor Stinner67e0de62019-11-20 11:48:18 +0100939 if ((gcstate->debug & DEBUG_SAVEALL) || has_legacy_finalizer(op)) {
940 if (PyList_Append(gcstate->garbage, op) < 0) {
Victor Stinner2e969062019-11-20 01:49:32 +0100941 _PyErr_Clear(tstate);
Serhiy Storchaka301e3cc2018-05-24 15:19:29 +0300942 break;
Serhiy Storchakac4653c92018-05-29 18:50:10 +0300943 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 }
945 }
Tim Petersf6b80452003-04-07 19:21:15 +0000946
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 gc_list_merge(finalizers, old);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000948}
949
Tim Peters5fbc7b12014-05-08 17:42:19 -0500950/* Run first-time finalizers (if any) on all the objects in collectable.
951 * Note that this may remove some (or even all) of the objects from the
952 * list, due to refcounts falling to 0.
953 */
Antoine Pitrou796564c2013-07-30 19:59:21 +0200954static void
Victor Stinner67e0de62019-11-20 11:48:18 +0100955finalize_garbage(PyThreadState *tstate, PyGC_Head *collectable)
Antoine Pitrou796564c2013-07-30 19:59:21 +0200956{
957 destructor finalize;
Tim Peters5fbc7b12014-05-08 17:42:19 -0500958 PyGC_Head seen;
Antoine Pitrou796564c2013-07-30 19:59:21 +0200959
Tim Peters5fbc7b12014-05-08 17:42:19 -0500960 /* While we're going through the loop, `finalize(op)` may cause op, or
961 * other objects, to be reclaimed via refcounts falling to zero. So
962 * there's little we can rely on about the structure of the input
963 * `collectable` list across iterations. For safety, we always take the
964 * first object in that list and move it to a temporary `seen` list.
965 * If objects vanish from the `collectable` and `seen` lists we don't
966 * care.
967 */
968 gc_list_init(&seen);
969
970 while (!gc_list_is_empty(collectable)) {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900971 PyGC_Head *gc = GC_NEXT(collectable);
Antoine Pitrou796564c2013-07-30 19:59:21 +0200972 PyObject *op = FROM_GC(gc);
Tim Peters5fbc7b12014-05-08 17:42:19 -0500973 gc_list_move(gc, &seen);
Antoine Pitrou796564c2013-07-30 19:59:21 +0200974 if (!_PyGCHead_FINALIZED(gc) &&
Tim Peters5fbc7b12014-05-08 17:42:19 -0500975 (finalize = Py_TYPE(op)->tp_finalize) != NULL) {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900976 _PyGCHead_SET_FINALIZED(gc);
Antoine Pitrou796564c2013-07-30 19:59:21 +0200977 Py_INCREF(op);
978 finalize(op);
Victor Stinner67e0de62019-11-20 11:48:18 +0100979 assert(!_PyErr_Occurred(tstate));
Antoine Pitrou796564c2013-07-30 19:59:21 +0200980 Py_DECREF(op);
981 }
982 }
Tim Peters5fbc7b12014-05-08 17:42:19 -0500983 gc_list_merge(&seen, collectable);
Antoine Pitrou796564c2013-07-30 19:59:21 +0200984}
985
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986/* Break reference cycles by clearing the containers involved. This is
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000987 * tricky business as the lists can be changing and we don't know which
Tim Peters19b74c72002-07-01 03:52:19 +0000988 * objects may be freed. It is possible I screwed something up here.
989 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000990static void
Victor Stinner67e0de62019-11-20 11:48:18 +0100991delete_garbage(PyThreadState *tstate, GCState *gcstate,
Victor Stinner9db03242019-04-26 02:32:01 +0200992 PyGC_Head *collectable, PyGC_Head *old)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000993{
Victor Stinner2e969062019-11-20 01:49:32 +0100994 assert(!_PyErr_Occurred(tstate));
Victor Stinner9db03242019-04-26 02:32:01 +0200995
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996 while (!gc_list_is_empty(collectable)) {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900997 PyGC_Head *gc = GC_NEXT(collectable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000998 PyObject *op = FROM_GC(gc);
Tim Peters88396172002-06-30 17:56:40 +0000999
Victor Stinnera4b2bc72018-10-26 18:00:13 +02001000 _PyObject_ASSERT_WITH_MSG(op, Py_REFCNT(op) > 0,
1001 "refcount is too small");
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09001002
Victor Stinner67e0de62019-11-20 11:48:18 +01001003 if (gcstate->debug & DEBUG_SAVEALL) {
1004 assert(gcstate->garbage != NULL);
1005 if (PyList_Append(gcstate->garbage, op) < 0) {
Victor Stinner2e969062019-11-20 01:49:32 +01001006 _PyErr_Clear(tstate);
Serhiy Storchakac4653c92018-05-29 18:50:10 +03001007 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001008 }
1009 else {
Victor Stinner9db03242019-04-26 02:32:01 +02001010 inquiry clear;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 if ((clear = Py_TYPE(op)->tp_clear) != NULL) {
1012 Py_INCREF(op);
Serhiy Storchakac4653c92018-05-29 18:50:10 +03001013 (void) clear(op);
Victor Stinner2e969062019-11-20 01:49:32 +01001014 if (_PyErr_Occurred(tstate)) {
Victor Stinner71c52e32019-05-27 08:57:14 +02001015 _PyErr_WriteUnraisableMsg("in tp_clear of",
1016 (PyObject*)Py_TYPE(op));
Serhiy Storchakac4653c92018-05-29 18:50:10 +03001017 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 Py_DECREF(op);
1019 }
1020 }
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09001021 if (GC_NEXT(collectable) == gc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 /* object is still alive, move it, it may die later */
Pablo Galindo466326d2019-10-13 16:48:59 +01001023 gc_clear_collecting(gc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 gc_list_move(gc, old);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001025 }
1026 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001027}
1028
Christian Heimesa156e092008-02-16 07:38:31 +00001029/* Clear all free lists
1030 * All free lists are cleared during the collection of the highest generation.
1031 * Allocated items in the free list may keep a pymalloc arena occupied.
1032 * Clearing the free lists may give back memory to the OS earlier.
1033 */
1034static void
Victor Stinnere005ead2020-06-05 02:56:37 +02001035clear_freelists(PyThreadState *tstate)
Christian Heimesa156e092008-02-16 07:38:31 +00001036{
Victor Stinner3744ed22020-06-05 01:39:24 +02001037 _PyFrame_ClearFreeList(tstate);
Victor Stinner69ac6e52020-06-04 23:38:36 +02001038 _PyTuple_ClearFreeList(tstate);
Victor Stinner2ba59372020-06-05 00:50:05 +02001039 _PyFloat_ClearFreeList(tstate);
Victor Stinner88ec9192020-06-05 02:05:41 +02001040 _PyList_ClearFreeList(tstate);
Victor Stinnerae00a5a2020-04-29 02:29:20 +02001041 _PyDict_ClearFreeList();
Victor Stinner78a02c22020-06-05 02:34:14 +02001042 _PyAsyncGen_ClearFreeLists(tstate);
Victor Stinnere005ead2020-06-05 02:56:37 +02001043 _PyContext_ClearFreeList(tstate);
Christian Heimesa156e092008-02-16 07:38:31 +00001044}
1045
Pablo Galindo97f12672020-01-13 12:25:05 +00001046// Show stats for objects in each generations
Inada Naokibf8162c2019-08-02 16:25:29 +09001047static void
Victor Stinner67e0de62019-11-20 11:48:18 +01001048show_stats_each_generations(GCState *gcstate)
Inada Naokibf8162c2019-08-02 16:25:29 +09001049{
1050 char buf[100];
1051 size_t pos = 0;
1052
1053 for (int i = 0; i < NUM_GENERATIONS && pos < sizeof(buf); i++) {
1054 pos += PyOS_snprintf(buf+pos, sizeof(buf)-pos,
Victor Stinnerd36cf5f2020-06-10 18:38:05 +02001055 " %zd",
Victor Stinner67e0de62019-11-20 11:48:18 +01001056 gc_list_size(GEN_HEAD(gcstate, i)));
Inada Naokibf8162c2019-08-02 16:25:29 +09001057 }
1058
1059 PySys_FormatStderr(
1060 "gc: objects in each generation:%s\n"
1061 "gc: objects in permanent generation: %zd\n",
Victor Stinner67e0de62019-11-20 11:48:18 +01001062 buf, gc_list_size(&gcstate->permanent_generation.head));
Inada Naokibf8162c2019-08-02 16:25:29 +09001063}
1064
Pablo Galindo97f12672020-01-13 12:25:05 +00001065/* Deduce which objects among "base" are unreachable from outside the list
Pablo Galindo466326d2019-10-13 16:48:59 +01001066 and move them to 'unreachable'. The process consist in the following steps:
1067
10681. Copy all reference counts to a different field (gc_prev is used to hold
1069 this copy to save memory).
10702. Traverse all objects in "base" and visit all referred objects using
Pablo Galindo97f12672020-01-13 12:25:05 +00001071 "tp_traverse" and for every visited object, subtract 1 to the reference
Pablo Galindo466326d2019-10-13 16:48:59 +01001072 count (the one that we copied in the previous step). After this step, all
1073 objects that can be reached directly from outside must have strictly positive
1074 reference count, while all unreachable objects must have a count of exactly 0.
Pablo Galindo97f12672020-01-13 12:25:05 +000010753. Identify all unreachable objects (the ones with 0 reference count) and move
Pablo Galindo466326d2019-10-13 16:48:59 +01001076 them to the "unreachable" list. This step also needs to move back to "base" all
1077 objects that were initially marked as unreachable but are referred transitively
1078 by the reachable objects (the ones with strictly positive reference count).
1079
1080Contracts:
1081
1082 * The "base" has to be a valid list with no mask set.
1083
1084 * The "unreachable" list must be uninitialized (this function calls
1085 gc_list_init over 'unreachable').
Tim Peters95bfc8a2019-10-13 16:47:04 -05001086
Pablo Galindo466326d2019-10-13 16:48:59 +01001087IMPORTANT: This function leaves 'unreachable' with the NEXT_MASK_UNREACHABLE
1088flag set but it does not clear it to skip unnecessary iteration. Before the
1089flag is cleared (for example, by using 'clear_unreachable_mask' function or
1090by a call to 'move_legacy_finalizers'), the 'unreachable' list is not a normal
1091list and we can not use most gc_list_* functions for it. */
1092static inline void
1093deduce_unreachable(PyGC_Head *base, PyGC_Head *unreachable) {
Tim Petersea55c512019-10-18 20:59:14 -05001094 validate_list(base, collecting_clear_unreachable_clear);
Pablo Galindo466326d2019-10-13 16:48:59 +01001095 /* Using ob_refcnt and gc_refs, calculate which objects in the
1096 * container set are reachable from outside the set (i.e., have a
1097 * refcount greater than 0 when all the references within the
1098 * set are taken into account).
1099 */
1100 update_refs(base); // gc_prev is used for gc_refs
1101 subtract_refs(base);
1102
1103 /* Leave everything reachable from outside base in base, and move
1104 * everything else (in base) to unreachable.
Pablo Galindo97f12672020-01-13 12:25:05 +00001105 *
Pablo Galindo466326d2019-10-13 16:48:59 +01001106 * NOTE: This used to move the reachable objects into a reachable
1107 * set instead. But most things usually turn out to be reachable,
Pablo Galindo97f12672020-01-13 12:25:05 +00001108 * so it's more efficient to move the unreachable things. It "sounds slick"
1109 * to move the unreachable objects, until you think about it - the reason it
1110 * pays isn't actually obvious.
1111 *
1112 * Suppose we create objects A, B, C in that order. They appear in the young
1113 * generation in the same order. If B points to A, and C to B, and C is
1114 * reachable from outside, then the adjusted refcounts will be 0, 0, and 1
1115 * respectively.
1116 *
1117 * When move_unreachable finds A, A is moved to the unreachable list. The
1118 * same for B when it's first encountered. Then C is traversed, B is moved
1119 * _back_ to the reachable list. B is eventually traversed, and then A is
1120 * moved back to the reachable list.
1121 *
1122 * So instead of not moving at all, the reachable objects B and A are moved
1123 * twice each. Why is this a win? A straightforward algorithm to move the
1124 * reachable objects instead would move A, B, and C once each.
1125 *
1126 * The key is that this dance leaves the objects in order C, B, A - it's
1127 * reversed from the original order. On all _subsequent_ scans, none of
1128 * them will move. Since most objects aren't in cycles, this can save an
1129 * unbounded number of moves across an unbounded number of later collections.
1130 * It can cost more only the first time the chain is scanned.
1131 *
1132 * Drawback: move_unreachable is also used to find out what's still trash
1133 * after finalizers may resurrect objects. In _that_ case most unreachable
1134 * objects will remain unreachable, so it would be more efficient to move
1135 * the reachable objects instead. But this is a one-time cost, probably not
1136 * worth complicating the code to speed just a little.
Pablo Galindo466326d2019-10-13 16:48:59 +01001137 */
1138 gc_list_init(unreachable);
1139 move_unreachable(base, unreachable); // gc_prev is pointer again
Tim Petersea55c512019-10-18 20:59:14 -05001140 validate_list(base, collecting_clear_unreachable_clear);
1141 validate_list(unreachable, collecting_set_unreachable_set);
Pablo Galindo466326d2019-10-13 16:48:59 +01001142}
1143
1144/* Handle objects that may have resurrected after a call to 'finalize_garbage', moving
1145 them to 'old_generation' and placing the rest on 'still_unreachable'.
1146
1147 Contracts:
1148 * After this function 'unreachable' must not be used anymore and 'still_unreachable'
1149 will contain the objects that did not resurrect.
1150
1151 * The "still_unreachable" list must be uninitialized (this function calls
1152 gc_list_init over 'still_unreachable').
1153
1154IMPORTANT: After a call to this function, the 'still_unreachable' set will have the
1155PREV_MARK_COLLECTING set, but the objects in this set are going to be removed so
1156we can skip the expense of clearing the flag to avoid extra iteration. */
1157static inline void
1158handle_resurrected_objects(PyGC_Head *unreachable, PyGC_Head* still_unreachable,
1159 PyGC_Head *old_generation)
1160{
1161 // Remove the PREV_MASK_COLLECTING from unreachable
1162 // to prepare it for a new call to 'deduce_unreachable'
1163 gc_list_clear_collecting(unreachable);
1164
1165 // After the call to deduce_unreachable, the 'still_unreachable' set will
1166 // have the PREV_MARK_COLLECTING set, but the objects are going to be
1167 // removed so we can skip the expense of clearing the flag.
1168 PyGC_Head* resurrected = unreachable;
1169 deduce_unreachable(resurrected, still_unreachable);
1170 clear_unreachable_mask(still_unreachable);
1171
1172 // Move the resurrected objects to the old generation for future collection.
1173 gc_list_merge(resurrected, old_generation);
1174}
1175
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001176/* This is the main function. Read this to understand how the
1177 * collection process works. */
Neal Norwitz7b216c52006-03-04 20:01:53 +00001178static Py_ssize_t
Victor Stinner67e0de62019-11-20 11:48:18 +01001179collect(PyThreadState *tstate, int generation,
Victor Stinner9db03242019-04-26 02:32:01 +02001180 Py_ssize_t *n_collected, Py_ssize_t *n_uncollectable, int nofail)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001181{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001182 int i;
1183 Py_ssize_t m = 0; /* # objects collected */
1184 Py_ssize_t n = 0; /* # unreachable objects that couldn't be collected */
1185 PyGC_Head *young; /* the generation we are examining */
1186 PyGC_Head *old; /* next older generation */
1187 PyGC_Head unreachable; /* non-problematic unreachable trash */
1188 PyGC_Head finalizers; /* objects with, & reachable from, __del__ */
1189 PyGC_Head *gc;
Victor Stinner7181dec2015-03-27 17:47:53 +01001190 _PyTime_t t1 = 0; /* initialize to prevent a compiler warning */
Victor Stinner72474072019-11-20 12:25:50 +01001191 GCState *gcstate = &tstate->interp->gc;
Antoine Pitrou40f6b122014-05-24 19:21:53 +02001192
Victor Stinnerd8135e92020-05-06 18:25:06 +02001193#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
1194 if (tstate->interp->config._isolated_interpreter) {
1195 // bpo-40533: The garbage collector must not be run on parallel on
1196 // Python objects shared by multiple interpreters.
1197 return 0;
1198 }
1199#endif
1200
Victor Stinner67e0de62019-11-20 11:48:18 +01001201 if (gcstate->debug & DEBUG_STATS) {
Inada Naokibf8162c2019-08-02 16:25:29 +09001202 PySys_WriteStderr("gc: collecting generation %d...\n", generation);
Victor Stinner67e0de62019-11-20 11:48:18 +01001203 show_stats_each_generations(gcstate);
Victor Stinner7181dec2015-03-27 17:47:53 +01001204 t1 = _PyTime_GetMonotonicClock();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001205 }
Neil Schemenauer2880ae52002-05-04 05:35:20 +00001206
Łukasz Langaa785c872016-09-09 17:37:37 -07001207 if (PyDTrace_GC_START_ENABLED())
1208 PyDTrace_GC_START(generation);
1209
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 /* update collection and allocation counters */
1211 if (generation+1 < NUM_GENERATIONS)
Victor Stinner67e0de62019-11-20 11:48:18 +01001212 gcstate->generations[generation+1].count += 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001213 for (i = 0; i <= generation; i++)
Victor Stinner67e0de62019-11-20 11:48:18 +01001214 gcstate->generations[i].count = 0;
Neil Schemenauer2880ae52002-05-04 05:35:20 +00001215
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 /* merge younger generations with one we are currently collecting */
1217 for (i = 0; i < generation; i++) {
Victor Stinner67e0de62019-11-20 11:48:18 +01001218 gc_list_merge(GEN_HEAD(gcstate, i), GEN_HEAD(gcstate, generation));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 }
Neil Schemenauer2880ae52002-05-04 05:35:20 +00001220
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001221 /* handy references */
Victor Stinner67e0de62019-11-20 11:48:18 +01001222 young = GEN_HEAD(gcstate, generation);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001223 if (generation < NUM_GENERATIONS-1)
Victor Stinner67e0de62019-11-20 11:48:18 +01001224 old = GEN_HEAD(gcstate, generation+1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001225 else
1226 old = young;
Tim Petersea55c512019-10-18 20:59:14 -05001227 validate_list(old, collecting_clear_unreachable_clear);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001228
Pablo Galindo466326d2019-10-13 16:48:59 +01001229 deduce_unreachable(young, &unreachable);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001230
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09001231 untrack_tuples(young);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001232 /* Move reachable objects to next generation. */
1233 if (young != old) {
1234 if (generation == NUM_GENERATIONS - 2) {
Victor Stinner67e0de62019-11-20 11:48:18 +01001235 gcstate->long_lived_pending += gc_list_size(young);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001236 }
1237 gc_list_merge(young, old);
1238 }
1239 else {
Pablo Galindo97f12672020-01-13 12:25:05 +00001240 /* We only un-track dicts in full collections, to avoid quadratic
Antoine Pitroue1ad3da2012-05-28 22:22:34 +02001241 dict build-up. See issue #14775. */
1242 untrack_dicts(young);
Victor Stinner67e0de62019-11-20 11:48:18 +01001243 gcstate->long_lived_pending = 0;
1244 gcstate->long_lived_total = gc_list_size(young);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001246
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 /* All objects in unreachable are trash, but objects reachable from
Antoine Pitrou796564c2013-07-30 19:59:21 +02001248 * legacy finalizers (e.g. tp_del) can't safely be deleted.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 */
1250 gc_list_init(&finalizers);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09001251 // NEXT_MASK_UNREACHABLE is cleared here.
1252 // After move_legacy_finalizers(), unreachable is normal list.
Antoine Pitrou796564c2013-07-30 19:59:21 +02001253 move_legacy_finalizers(&unreachable, &finalizers);
1254 /* finalizers contains the unreachable objects with a legacy finalizer;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 * unreachable objects reachable *from* those are also uncollectable,
1256 * and we move those into the finalizers list too.
1257 */
Antoine Pitrou796564c2013-07-30 19:59:21 +02001258 move_legacy_finalizer_reachable(&finalizers);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001259
Tim Petersea55c512019-10-18 20:59:14 -05001260 validate_list(&finalizers, collecting_clear_unreachable_clear);
1261 validate_list(&unreachable, collecting_set_unreachable_clear);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09001262
Tim Petersecbf35f2019-10-09 12:37:30 -05001263 /* Print debugging information. */
Victor Stinner67e0de62019-11-20 11:48:18 +01001264 if (gcstate->debug & DEBUG_COLLECTABLE) {
Tim Petersecbf35f2019-10-09 12:37:30 -05001265 for (gc = GC_NEXT(&unreachable); gc != &unreachable; gc = GC_NEXT(gc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001266 debug_cycle("collectable", FROM_GC(gc));
1267 }
1268 }
Tim Petersead8b7a2004-10-30 23:09:22 +00001269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001270 /* Clear weakrefs and invoke callbacks as necessary. */
1271 m += handle_weakrefs(&unreachable, old);
Tim Petersead8b7a2004-10-30 23:09:22 +00001272
Tim Petersea55c512019-10-18 20:59:14 -05001273 validate_list(old, collecting_clear_unreachable_clear);
1274 validate_list(&unreachable, collecting_set_unreachable_clear);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09001275
Antoine Pitrou796564c2013-07-30 19:59:21 +02001276 /* Call tp_finalize on objects which have one. */
Victor Stinner67e0de62019-11-20 11:48:18 +01001277 finalize_garbage(tstate, &unreachable);
Antoine Pitrou796564c2013-07-30 19:59:21 +02001278
Pablo Galindo466326d2019-10-13 16:48:59 +01001279 /* Handle any objects that may have resurrected after the call
1280 * to 'finalize_garbage' and continue the collection with the
1281 * objects that are still unreachable */
1282 PyGC_Head final_unreachable;
1283 handle_resurrected_objects(&unreachable, &final_unreachable, old);
1284
1285 /* Call tp_clear on objects in the final_unreachable set. This will cause
1286 * the reference cycles to be broken. It may also cause some objects
1287 * in finalizers to be freed.
1288 */
1289 m += gc_list_size(&final_unreachable);
Victor Stinner67e0de62019-11-20 11:48:18 +01001290 delete_garbage(tstate, gcstate, &final_unreachable, old);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001291
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 /* Collect statistics on uncollectable objects found and print
1293 * debugging information. */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09001294 for (gc = GC_NEXT(&finalizers); gc != &finalizers; gc = GC_NEXT(gc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001295 n++;
Victor Stinner67e0de62019-11-20 11:48:18 +01001296 if (gcstate->debug & DEBUG_UNCOLLECTABLE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001297 debug_cycle("uncollectable", FROM_GC(gc));
1298 }
Victor Stinner67e0de62019-11-20 11:48:18 +01001299 if (gcstate->debug & DEBUG_STATS) {
Inada Naokibf8162c2019-08-02 16:25:29 +09001300 double d = _PyTime_AsSecondsDouble(_PyTime_GetMonotonicClock() - t1);
Inada Naoki013e52f2019-08-31 09:13:42 +09001301 PySys_WriteStderr(
Victor Stinnerd36cf5f2020-06-10 18:38:05 +02001302 "gc: done, %zd unreachable, %zd uncollectable, %.4fs elapsed\n",
Inada Naokibf8162c2019-08-02 16:25:29 +09001303 n+m, n, d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001304 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 /* Append instances in the uncollectable set to a Python
1307 * reachable list of garbage. The programmer has to deal with
1308 * this if they insist on creating this type of structure.
1309 */
Victor Stinner67e0de62019-11-20 11:48:18 +01001310 handle_legacy_finalizers(tstate, gcstate, &finalizers, old);
Tim Petersea55c512019-10-18 20:59:14 -05001311 validate_list(old, collecting_clear_unreachable_clear);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001313 /* Clear free list only during the collection of the highest
1314 * generation */
1315 if (generation == NUM_GENERATIONS-1) {
Victor Stinnere005ead2020-06-05 02:56:37 +02001316 clear_freelists(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317 }
Christian Heimesa156e092008-02-16 07:38:31 +00001318
Victor Stinner2e969062019-11-20 01:49:32 +01001319 if (_PyErr_Occurred(tstate)) {
Antoine Pitroufef34e32013-05-19 01:11:58 +02001320 if (nofail) {
Victor Stinner2e969062019-11-20 01:49:32 +01001321 _PyErr_Clear(tstate);
Antoine Pitroufef34e32013-05-19 01:11:58 +02001322 }
1323 else {
Victor Stinner656c45e2020-01-24 18:05:24 +01001324 _PyErr_WriteUnraisableMsg("in garbage collection", NULL);
Antoine Pitroufef34e32013-05-19 01:11:58 +02001325 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001326 }
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001327
Antoine Pitroud4156c12012-10-30 22:43:19 +01001328 /* Update stats */
Victor Stinner9db03242019-04-26 02:32:01 +02001329 if (n_collected) {
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001330 *n_collected = m;
Victor Stinner9db03242019-04-26 02:32:01 +02001331 }
1332 if (n_uncollectable) {
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001333 *n_uncollectable = n;
Victor Stinner9db03242019-04-26 02:32:01 +02001334 }
1335
Victor Stinner67e0de62019-11-20 11:48:18 +01001336 struct gc_generation_stats *stats = &gcstate->generation_stats[generation];
Antoine Pitroud4156c12012-10-30 22:43:19 +01001337 stats->collections++;
1338 stats->collected += m;
1339 stats->uncollectable += n;
Łukasz Langaa785c872016-09-09 17:37:37 -07001340
Victor Stinner9db03242019-04-26 02:32:01 +02001341 if (PyDTrace_GC_DONE_ENABLED()) {
Victor Stinner2e969062019-11-20 01:49:32 +01001342 PyDTrace_GC_DONE(n + m);
Victor Stinner9db03242019-04-26 02:32:01 +02001343 }
Łukasz Langaa785c872016-09-09 17:37:37 -07001344
Victor Stinner2e969062019-11-20 01:49:32 +01001345 assert(!_PyErr_Occurred(tstate));
1346 return n + m;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001347}
1348
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001349/* Invoke progress callbacks to notify clients that garbage collection
1350 * is starting or stopping
1351 */
1352static void
Victor Stinner67e0de62019-11-20 11:48:18 +01001353invoke_gc_callback(PyThreadState *tstate, const char *phase,
Victor Stinner9db03242019-04-26 02:32:01 +02001354 int generation, Py_ssize_t collected,
1355 Py_ssize_t uncollectable)
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001356{
Victor Stinner67e0de62019-11-20 11:48:18 +01001357 assert(!_PyErr_Occurred(tstate));
Victor Stinner9db03242019-04-26 02:32:01 +02001358
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001359 /* we may get called very early */
Victor Stinner72474072019-11-20 12:25:50 +01001360 GCState *gcstate = &tstate->interp->gc;
Victor Stinner67e0de62019-11-20 11:48:18 +01001361 if (gcstate->callbacks == NULL) {
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001362 return;
Victor Stinner9db03242019-04-26 02:32:01 +02001363 }
1364
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001365 /* The local variable cannot be rebound, check it for sanity */
Victor Stinner67e0de62019-11-20 11:48:18 +01001366 assert(PyList_CheckExact(gcstate->callbacks));
Victor Stinner9db03242019-04-26 02:32:01 +02001367 PyObject *info = NULL;
Victor Stinner67e0de62019-11-20 11:48:18 +01001368 if (PyList_GET_SIZE(gcstate->callbacks) != 0) {
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001369 info = Py_BuildValue("{sisnsn}",
1370 "generation", generation,
1371 "collected", collected,
1372 "uncollectable", uncollectable);
1373 if (info == NULL) {
1374 PyErr_WriteUnraisable(NULL);
1375 return;
1376 }
1377 }
Victor Stinner67e0de62019-11-20 11:48:18 +01001378 for (Py_ssize_t i=0; i<PyList_GET_SIZE(gcstate->callbacks); i++) {
1379 PyObject *r, *cb = PyList_GET_ITEM(gcstate->callbacks, i);
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001380 Py_INCREF(cb); /* make sure cb doesn't go away */
1381 r = PyObject_CallFunction(cb, "sO", phase, info);
Serhiy Storchaka301e3cc2018-05-24 15:19:29 +03001382 if (r == NULL) {
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001383 PyErr_WriteUnraisable(cb);
Serhiy Storchaka301e3cc2018-05-24 15:19:29 +03001384 }
1385 else {
1386 Py_DECREF(r);
1387 }
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001388 Py_DECREF(cb);
1389 }
1390 Py_XDECREF(info);
Victor Stinner67e0de62019-11-20 11:48:18 +01001391 assert(!_PyErr_Occurred(tstate));
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001392}
1393
1394/* Perform garbage collection of a generation and invoke
1395 * progress callbacks.
1396 */
1397static Py_ssize_t
Victor Stinner67e0de62019-11-20 11:48:18 +01001398collect_with_callback(PyThreadState *tstate, int generation)
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001399{
Victor Stinner67e0de62019-11-20 11:48:18 +01001400 assert(!_PyErr_Occurred(tstate));
Victor Stinner9db03242019-04-26 02:32:01 +02001401 Py_ssize_t result, collected, uncollectable;
Victor Stinner67e0de62019-11-20 11:48:18 +01001402 invoke_gc_callback(tstate, "start", generation, 0, 0);
1403 result = collect(tstate, generation, &collected, &uncollectable, 0);
1404 invoke_gc_callback(tstate, "stop", generation, collected, uncollectable);
1405 assert(!_PyErr_Occurred(tstate));
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001406 return result;
1407}
1408
Neal Norwitz7b216c52006-03-04 20:01:53 +00001409static Py_ssize_t
Victor Stinner67e0de62019-11-20 11:48:18 +01001410collect_generations(PyThreadState *tstate)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001411{
Victor Stinner72474072019-11-20 12:25:50 +01001412 GCState *gcstate = &tstate->interp->gc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001413 /* Find the oldest generation (highest numbered) where the count
1414 * exceeds the threshold. Objects in the that generation and
1415 * generations younger than it will be collected. */
Victor Stinner9db03242019-04-26 02:32:01 +02001416 Py_ssize_t n = 0;
1417 for (int i = NUM_GENERATIONS-1; i >= 0; i--) {
Victor Stinner67e0de62019-11-20 11:48:18 +01001418 if (gcstate->generations[i].count > gcstate->generations[i].threshold) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001419 /* Avoid quadratic performance degradation in number
Pablo Galindo90913982019-12-27 21:55:56 +00001420 of tracked objects (see also issue #4074):
1421
1422 To limit the cost of garbage collection, there are two strategies;
1423 - make each collection faster, e.g. by scanning fewer objects
1424 - do less collections
1425 This heuristic is about the latter strategy.
1426
1427 In addition to the various configurable thresholds, we only trigger a
1428 full collection if the ratio
1429
1430 long_lived_pending / long_lived_total
1431
1432 is above a given value (hardwired to 25%).
1433
1434 The reason is that, while "non-full" collections (i.e., collections of
1435 the young and middle generations) will always examine roughly the same
1436 number of objects -- determined by the aforementioned thresholds --,
1437 the cost of a full collection is proportional to the total number of
1438 long-lived objects, which is virtually unbounded.
1439
1440 Indeed, it has been remarked that doing a full collection every
1441 <constant number> of object creations entails a dramatic performance
1442 degradation in workloads which consist in creating and storing lots of
1443 long-lived objects (e.g. building a large list of GC-tracked objects would
1444 show quadratic performance, instead of linear as expected: see issue #4074).
1445
1446 Using the above ratio, instead, yields amortized linear performance in
1447 the total number of objects (the effect of which can be summarized
1448 thusly: "each full garbage collection is more and more costly as the
1449 number of objects grows, but we do fewer and fewer of them").
1450
1451 This heuristic was suggested by Martin von Löwis on python-dev in
1452 June 2008. His original analysis and proposal can be found at:
1453 http://mail.python.org/pipermail/python-dev/2008-June/080579.html
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001454 */
1455 if (i == NUM_GENERATIONS - 1
Victor Stinner67e0de62019-11-20 11:48:18 +01001456 && gcstate->long_lived_pending < gcstate->long_lived_total / 4)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001457 continue;
Victor Stinner67e0de62019-11-20 11:48:18 +01001458 n = collect_with_callback(tstate, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001459 break;
1460 }
1461 }
1462 return n;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001463}
1464
Serhiy Storchaka93260282017-02-04 11:19:59 +02001465#include "clinic/gcmodule.c.h"
1466
1467/*[clinic input]
1468gc.enable
1469
1470Enable automatic garbage collection.
1471[clinic start generated code]*/
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001472
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001473static PyObject *
Serhiy Storchaka93260282017-02-04 11:19:59 +02001474gc_enable_impl(PyObject *module)
1475/*[clinic end generated code: output=45a427e9dce9155c input=81ac4940ca579707]*/
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001476{
Victor Stinner1bcc32f2020-06-10 20:08:26 +02001477 GCState *gcstate = get_gc_state();
Victor Stinner67e0de62019-11-20 11:48:18 +01001478 gcstate->enabled = 1;
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001479 Py_RETURN_NONE;
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001480}
1481
Serhiy Storchaka93260282017-02-04 11:19:59 +02001482/*[clinic input]
1483gc.disable
1484
1485Disable automatic garbage collection.
1486[clinic start generated code]*/
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001487
1488static PyObject *
Serhiy Storchaka93260282017-02-04 11:19:59 +02001489gc_disable_impl(PyObject *module)
1490/*[clinic end generated code: output=97d1030f7aa9d279 input=8c2e5a14e800d83b]*/
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001491{
Victor Stinner1bcc32f2020-06-10 20:08:26 +02001492 GCState *gcstate = get_gc_state();
Victor Stinner67e0de62019-11-20 11:48:18 +01001493 gcstate->enabled = 0;
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001494 Py_RETURN_NONE;
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001495}
1496
Serhiy Storchaka93260282017-02-04 11:19:59 +02001497/*[clinic input]
1498gc.isenabled -> bool
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001499
Serhiy Storchaka93260282017-02-04 11:19:59 +02001500Returns true if automatic garbage collection is enabled.
1501[clinic start generated code]*/
1502
1503static int
1504gc_isenabled_impl(PyObject *module)
1505/*[clinic end generated code: output=1874298331c49130 input=30005e0422373b31]*/
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001506{
Victor Stinner1bcc32f2020-06-10 20:08:26 +02001507 GCState *gcstate = get_gc_state();
Victor Stinner67e0de62019-11-20 11:48:18 +01001508 return gcstate->enabled;
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001509}
1510
Serhiy Storchaka93260282017-02-04 11:19:59 +02001511/*[clinic input]
1512gc.collect -> Py_ssize_t
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001513
Serhiy Storchaka93260282017-02-04 11:19:59 +02001514 generation: int(c_default="NUM_GENERATIONS - 1") = 2
1515
1516Run the garbage collector.
1517
1518With no arguments, run a full collection. The optional argument
1519may be an integer specifying which generation to collect. A ValueError
1520is raised if the generation number is invalid.
1521
1522The number of unreachable objects is returned.
1523[clinic start generated code]*/
1524
1525static Py_ssize_t
1526gc_collect_impl(PyObject *module, int generation)
1527/*[clinic end generated code: output=b697e633043233c7 input=40720128b682d879]*/
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001528{
Victor Stinner2e969062019-11-20 01:49:32 +01001529 PyThreadState *tstate = _PyThreadState_GET();
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001530
Serhiy Storchaka93260282017-02-04 11:19:59 +02001531 if (generation < 0 || generation >= NUM_GENERATIONS) {
Victor Stinner2e969062019-11-20 01:49:32 +01001532 _PyErr_SetString(tstate, PyExc_ValueError, "invalid generation");
Serhiy Storchaka93260282017-02-04 11:19:59 +02001533 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001534 }
Barry Warsawd3c38ff2006-03-07 09:46:03 +00001535
Victor Stinner72474072019-11-20 12:25:50 +01001536 GCState *gcstate = &tstate->interp->gc;
Victor Stinner9db03242019-04-26 02:32:01 +02001537 Py_ssize_t n;
Victor Stinner67e0de62019-11-20 11:48:18 +01001538 if (gcstate->collecting) {
Victor Stinner9db03242019-04-26 02:32:01 +02001539 /* already collecting, don't do anything */
1540 n = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001541 }
Victor Stinner9db03242019-04-26 02:32:01 +02001542 else {
Victor Stinner67e0de62019-11-20 11:48:18 +01001543 gcstate->collecting = 1;
1544 n = collect_with_callback(tstate, generation);
1545 gcstate->collecting = 0;
Victor Stinner9db03242019-04-26 02:32:01 +02001546 }
Serhiy Storchaka93260282017-02-04 11:19:59 +02001547 return n;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001548}
1549
Serhiy Storchaka93260282017-02-04 11:19:59 +02001550/*[clinic input]
1551gc.set_debug
1552
1553 flags: int
1554 An integer that can have the following bits turned on:
1555 DEBUG_STATS - Print statistics during collection.
1556 DEBUG_COLLECTABLE - Print collectable objects found.
1557 DEBUG_UNCOLLECTABLE - Print unreachable but uncollectable objects
1558 found.
1559 DEBUG_SAVEALL - Save objects to gc.garbage rather than freeing them.
1560 DEBUG_LEAK - Debug leaking programs (everything but STATS).
1561 /
1562
1563Set the garbage collection debugging flags.
1564
1565Debugging information is written to sys.stderr.
1566[clinic start generated code]*/
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001567
1568static PyObject *
Serhiy Storchaka93260282017-02-04 11:19:59 +02001569gc_set_debug_impl(PyObject *module, int flags)
1570/*[clinic end generated code: output=7c8366575486b228 input=5e5ce15e84fbed15]*/
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001571{
Victor Stinner1bcc32f2020-06-10 20:08:26 +02001572 GCState *gcstate = get_gc_state();
Victor Stinner67e0de62019-11-20 11:48:18 +01001573 gcstate->debug = flags;
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001574 Py_RETURN_NONE;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001575}
1576
Serhiy Storchaka93260282017-02-04 11:19:59 +02001577/*[clinic input]
1578gc.get_debug -> int
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001579
Serhiy Storchaka93260282017-02-04 11:19:59 +02001580Get the garbage collection debugging flags.
1581[clinic start generated code]*/
1582
1583static int
1584gc_get_debug_impl(PyObject *module)
1585/*[clinic end generated code: output=91242f3506cd1e50 input=91a101e1c3b98366]*/
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001586{
Victor Stinner1bcc32f2020-06-10 20:08:26 +02001587 GCState *gcstate = get_gc_state();
Victor Stinner67e0de62019-11-20 11:48:18 +01001588 return gcstate->debug;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001589}
1590
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001591PyDoc_STRVAR(gc_set_thresh__doc__,
Neal Norwitz2a47c0f2002-01-29 00:53:41 +00001592"set_threshold(threshold0, [threshold1, threshold2]) -> None\n"
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001593"\n"
1594"Sets the collection thresholds. Setting threshold0 to zero disables\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001595"collection.\n");
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001596
1597static PyObject *
Victor Stinner9db03242019-04-26 02:32:01 +02001598gc_set_threshold(PyObject *self, PyObject *args)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001599{
Victor Stinner1bcc32f2020-06-10 20:08:26 +02001600 GCState *gcstate = get_gc_state();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001601 if (!PyArg_ParseTuple(args, "i|ii:set_threshold",
Victor Stinner67e0de62019-11-20 11:48:18 +01001602 &gcstate->generations[0].threshold,
1603 &gcstate->generations[1].threshold,
1604 &gcstate->generations[2].threshold))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001605 return NULL;
Victor Stinner9db03242019-04-26 02:32:01 +02001606 for (int i = 3; i < NUM_GENERATIONS; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001607 /* generations higher than 2 get the same threshold */
Victor Stinner67e0de62019-11-20 11:48:18 +01001608 gcstate->generations[i].threshold = gcstate->generations[2].threshold;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001609 }
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001610 Py_RETURN_NONE;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001611}
1612
Serhiy Storchaka93260282017-02-04 11:19:59 +02001613/*[clinic input]
1614gc.get_threshold
1615
1616Return the current collection thresholds.
1617[clinic start generated code]*/
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001618
1619static PyObject *
Serhiy Storchaka93260282017-02-04 11:19:59 +02001620gc_get_threshold_impl(PyObject *module)
1621/*[clinic end generated code: output=7902bc9f41ecbbd8 input=286d79918034d6e6]*/
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001622{
Victor Stinner1bcc32f2020-06-10 20:08:26 +02001623 GCState *gcstate = get_gc_state();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001624 return Py_BuildValue("(iii)",
Victor Stinner67e0de62019-11-20 11:48:18 +01001625 gcstate->generations[0].threshold,
1626 gcstate->generations[1].threshold,
1627 gcstate->generations[2].threshold);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001628}
1629
Serhiy Storchaka93260282017-02-04 11:19:59 +02001630/*[clinic input]
1631gc.get_count
1632
1633Return a three-tuple of the current collection counts.
1634[clinic start generated code]*/
Barry Warsawd3c38ff2006-03-07 09:46:03 +00001635
1636static PyObject *
Serhiy Storchaka93260282017-02-04 11:19:59 +02001637gc_get_count_impl(PyObject *module)
1638/*[clinic end generated code: output=354012e67b16398f input=a392794a08251751]*/
Barry Warsawd3c38ff2006-03-07 09:46:03 +00001639{
Victor Stinner1bcc32f2020-06-10 20:08:26 +02001640 GCState *gcstate = get_gc_state();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001641 return Py_BuildValue("(iii)",
Victor Stinner67e0de62019-11-20 11:48:18 +01001642 gcstate->generations[0].count,
1643 gcstate->generations[1].count,
1644 gcstate->generations[2].count);
Barry Warsawd3c38ff2006-03-07 09:46:03 +00001645}
1646
Neil Schemenauer48c70342001-08-09 15:38:31 +00001647static int
Martin v. Löwis560da622001-11-24 09:24:51 +00001648referrersvisit(PyObject* obj, PyObject *objs)
Neil Schemenauer48c70342001-08-09 15:38:31 +00001649{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001650 Py_ssize_t i;
1651 for (i = 0; i < PyTuple_GET_SIZE(objs); i++)
1652 if (PyTuple_GET_ITEM(objs, i) == obj)
1653 return 1;
1654 return 0;
Neil Schemenauer48c70342001-08-09 15:38:31 +00001655}
1656
Neil Schemenauer17e7be62001-08-10 14:46:47 +00001657static int
Martin v. Löwis560da622001-11-24 09:24:51 +00001658gc_referrers_for(PyObject *objs, PyGC_Head *list, PyObject *resultlist)
Neil Schemenauer48c70342001-08-09 15:38:31 +00001659{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001660 PyGC_Head *gc;
1661 PyObject *obj;
1662 traverseproc traverse;
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09001663 for (gc = GC_NEXT(list); gc != list; gc = GC_NEXT(gc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001664 obj = FROM_GC(gc);
1665 traverse = Py_TYPE(obj)->tp_traverse;
1666 if (obj == objs || obj == resultlist)
1667 continue;
1668 if (traverse(obj, (visitproc)referrersvisit, objs)) {
1669 if (PyList_Append(resultlist, obj) < 0)
1670 return 0; /* error */
1671 }
1672 }
1673 return 1; /* no error */
Neil Schemenauer48c70342001-08-09 15:38:31 +00001674}
1675
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001676PyDoc_STRVAR(gc_get_referrers__doc__,
Martin v. Löwis560da622001-11-24 09:24:51 +00001677"get_referrers(*objs) -> list\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001678Return the list of objects that directly refer to any of objs.");
Neil Schemenauer48c70342001-08-09 15:38:31 +00001679
Neil Schemenauer17e7be62001-08-10 14:46:47 +00001680static PyObject *
Martin v. Löwis560da622001-11-24 09:24:51 +00001681gc_get_referrers(PyObject *self, PyObject *args)
Neil Schemenauer48c70342001-08-09 15:38:31 +00001682{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001683 PyObject *result = PyList_New(0);
Victor Stinner67e0de62019-11-20 11:48:18 +01001684 if (!result) {
1685 return NULL;
1686 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001687
Victor Stinner1bcc32f2020-06-10 20:08:26 +02001688 GCState *gcstate = get_gc_state();
1689 for (int i = 0; i < NUM_GENERATIONS; i++) {
Victor Stinner67e0de62019-11-20 11:48:18 +01001690 if (!(gc_referrers_for(args, GEN_HEAD(gcstate, i), result))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001691 Py_DECREF(result);
1692 return NULL;
1693 }
1694 }
1695 return result;
Neil Schemenauer48c70342001-08-09 15:38:31 +00001696}
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001697
Tim Peters0f81ab62003-04-08 16:39:48 +00001698/* Append obj to list; return true if error (out of memory), false if OK. */
Jeremy Hylton5bd378b2003-04-03 16:28:38 +00001699static int
Tim Peters730f5532003-04-08 17:17:17 +00001700referentsvisit(PyObject *obj, PyObject *list)
Jeremy Hylton5bd378b2003-04-03 16:28:38 +00001701{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001702 return PyList_Append(list, obj) < 0;
Jeremy Hylton5bd378b2003-04-03 16:28:38 +00001703}
1704
Tim Peters730f5532003-04-08 17:17:17 +00001705PyDoc_STRVAR(gc_get_referents__doc__,
1706"get_referents(*objs) -> list\n\
Jeremy Hylton059b0942003-04-03 16:29:13 +00001707Return the list of objects that are directly referred to by objs.");
Jeremy Hylton5bd378b2003-04-03 16:28:38 +00001708
1709static PyObject *
Tim Peters730f5532003-04-08 17:17:17 +00001710gc_get_referents(PyObject *self, PyObject *args)
Jeremy Hylton5bd378b2003-04-03 16:28:38 +00001711{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001712 Py_ssize_t i;
1713 PyObject *result = PyList_New(0);
Tim Peters0f81ab62003-04-08 16:39:48 +00001714
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001715 if (result == NULL)
1716 return NULL;
Tim Peters0f81ab62003-04-08 16:39:48 +00001717
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001718 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
1719 traverseproc traverse;
1720 PyObject *obj = PyTuple_GET_ITEM(args, i);
Tim Peters0f81ab62003-04-08 16:39:48 +00001721
Hai Shi675d9a32020-04-15 02:11:20 +08001722 if (!_PyObject_IS_GC(obj))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001723 continue;
1724 traverse = Py_TYPE(obj)->tp_traverse;
1725 if (! traverse)
1726 continue;
1727 if (traverse(obj, (visitproc)referentsvisit, result)) {
1728 Py_DECREF(result);
1729 return NULL;
1730 }
1731 }
1732 return result;
Jeremy Hylton5bd378b2003-04-03 16:28:38 +00001733}
1734
Serhiy Storchaka93260282017-02-04 11:19:59 +02001735/*[clinic input]
1736gc.get_objects
Pablo Galindo175421b2019-02-23 03:02:06 +00001737 generation: Py_ssize_t(accept={int, NoneType}, c_default="-1") = None
1738 Generation to extract the objects from.
Serhiy Storchaka93260282017-02-04 11:19:59 +02001739
1740Return a list of objects tracked by the collector (excluding the list returned).
Pablo Galindo175421b2019-02-23 03:02:06 +00001741
1742If generation is not None, return only the objects tracked by the collector
1743that are in that generation.
Serhiy Storchaka93260282017-02-04 11:19:59 +02001744[clinic start generated code]*/
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +00001745
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +00001746static PyObject *
Pablo Galindo175421b2019-02-23 03:02:06 +00001747gc_get_objects_impl(PyObject *module, Py_ssize_t generation)
1748/*[clinic end generated code: output=48b35fea4ba6cb0e input=ef7da9df9806754c]*/
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +00001749{
Victor Stinner67e0de62019-11-20 11:48:18 +01001750 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001751 int i;
1752 PyObject* result;
Victor Stinner72474072019-11-20 12:25:50 +01001753 GCState *gcstate = &tstate->interp->gc;
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +00001754
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001755 result = PyList_New(0);
Pablo Galindo175421b2019-02-23 03:02:06 +00001756 if (result == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001757 return NULL;
Pablo Galindo175421b2019-02-23 03:02:06 +00001758 }
1759
1760 /* If generation is passed, we extract only that generation */
Victor Stinner0810fa72019-04-15 17:54:09 +02001761 if (generation != -1) {
Pablo Galindo175421b2019-02-23 03:02:06 +00001762 if (generation >= NUM_GENERATIONS) {
Victor Stinner67e0de62019-11-20 11:48:18 +01001763 _PyErr_Format(tstate, PyExc_ValueError,
1764 "generation parameter must be less than the number of "
1765 "available generations (%i)",
1766 NUM_GENERATIONS);
Pablo Galindo175421b2019-02-23 03:02:06 +00001767 goto error;
1768 }
1769
1770 if (generation < 0) {
Victor Stinner67e0de62019-11-20 11:48:18 +01001771 _PyErr_SetString(tstate, PyExc_ValueError,
1772 "generation parameter cannot be negative");
Pablo Galindo175421b2019-02-23 03:02:06 +00001773 goto error;
1774 }
1775
Victor Stinner67e0de62019-11-20 11:48:18 +01001776 if (append_objects(result, GEN_HEAD(gcstate, generation))) {
Pablo Galindo175421b2019-02-23 03:02:06 +00001777 goto error;
1778 }
1779
1780 return result;
1781 }
1782
1783 /* If generation is not passed or None, get all objects from all generations */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001784 for (i = 0; i < NUM_GENERATIONS; i++) {
Victor Stinner67e0de62019-11-20 11:48:18 +01001785 if (append_objects(result, GEN_HEAD(gcstate, i))) {
Pablo Galindo175421b2019-02-23 03:02:06 +00001786 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001787 }
1788 }
1789 return result;
Pablo Galindo175421b2019-02-23 03:02:06 +00001790
1791error:
1792 Py_DECREF(result);
1793 return NULL;
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +00001794}
1795
Serhiy Storchaka93260282017-02-04 11:19:59 +02001796/*[clinic input]
1797gc.get_stats
1798
1799Return a list of dictionaries containing per-generation statistics.
1800[clinic start generated code]*/
Antoine Pitroud4156c12012-10-30 22:43:19 +01001801
1802static PyObject *
Serhiy Storchaka93260282017-02-04 11:19:59 +02001803gc_get_stats_impl(PyObject *module)
1804/*[clinic end generated code: output=a8ab1d8a5d26f3ab input=1ef4ed9d17b1a470]*/
Antoine Pitroud4156c12012-10-30 22:43:19 +01001805{
1806 int i;
Antoine Pitroud4156c12012-10-30 22:43:19 +01001807 struct gc_generation_stats stats[NUM_GENERATIONS], *st;
1808
1809 /* To get consistent values despite allocations while constructing
1810 the result list, we use a snapshot of the running stats. */
Victor Stinner1bcc32f2020-06-10 20:08:26 +02001811 GCState *gcstate = get_gc_state();
Antoine Pitroud4156c12012-10-30 22:43:19 +01001812 for (i = 0; i < NUM_GENERATIONS; i++) {
Victor Stinner67e0de62019-11-20 11:48:18 +01001813 stats[i] = gcstate->generation_stats[i];
Antoine Pitroud4156c12012-10-30 22:43:19 +01001814 }
1815
Victor Stinner9db03242019-04-26 02:32:01 +02001816 PyObject *result = PyList_New(0);
Antoine Pitroud4156c12012-10-30 22:43:19 +01001817 if (result == NULL)
1818 return NULL;
1819
1820 for (i = 0; i < NUM_GENERATIONS; i++) {
1821 PyObject *dict;
1822 st = &stats[i];
1823 dict = Py_BuildValue("{snsnsn}",
1824 "collections", st->collections,
1825 "collected", st->collected,
1826 "uncollectable", st->uncollectable
1827 );
1828 if (dict == NULL)
1829 goto error;
1830 if (PyList_Append(result, dict)) {
1831 Py_DECREF(dict);
1832 goto error;
1833 }
1834 Py_DECREF(dict);
1835 }
1836 return result;
1837
1838error:
1839 Py_XDECREF(result);
1840 return NULL;
1841}
1842
1843
Serhiy Storchaka93260282017-02-04 11:19:59 +02001844/*[clinic input]
1845gc.is_tracked
1846
1847 obj: object
1848 /
1849
1850Returns true if the object is tracked by the garbage collector.
1851
1852Simple atomic objects will return false.
1853[clinic start generated code]*/
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001854
1855static PyObject *
Serhiy Storchaka93260282017-02-04 11:19:59 +02001856gc_is_tracked(PyObject *module, PyObject *obj)
1857/*[clinic end generated code: output=14f0103423b28e31 input=d83057f170ea2723]*/
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001858{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001859 PyObject *result;
1860
Hai Shi675d9a32020-04-15 02:11:20 +08001861 if (_PyObject_IS_GC(obj) && _PyObject_GC_IS_TRACKED(obj))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001862 result = Py_True;
1863 else
1864 result = Py_False;
1865 Py_INCREF(result);
1866 return result;
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001867}
1868
brainfvckc75edab2017-10-16 12:49:41 -07001869/*[clinic input]
Pablo Galindoa2ec3f02020-01-14 12:06:45 +00001870gc.is_finalized
1871
1872 obj: object
1873 /
1874
1875Returns true if the object has been already finalized by the GC.
1876[clinic start generated code]*/
1877
1878static PyObject *
1879gc_is_finalized(PyObject *module, PyObject *obj)
1880/*[clinic end generated code: output=e1516ac119a918ed input=201d0c58f69ae390]*/
1881{
Hai Shi675d9a32020-04-15 02:11:20 +08001882 if (_PyObject_IS_GC(obj) && _PyGCHead_FINALIZED(AS_GC(obj))) {
Pablo Galindoa2ec3f02020-01-14 12:06:45 +00001883 Py_RETURN_TRUE;
1884 }
1885 Py_RETURN_FALSE;
1886}
1887
1888/*[clinic input]
brainfvckc75edab2017-10-16 12:49:41 -07001889gc.freeze
1890
1891Freeze all current tracked objects and ignore them for future collections.
1892
1893This can be used before a POSIX fork() call to make the gc copy-on-write friendly.
1894Note: collection before a POSIX fork() call may free pages for future allocation
1895which can cause copy-on-write.
1896[clinic start generated code]*/
1897
1898static PyObject *
1899gc_freeze_impl(PyObject *module)
1900/*[clinic end generated code: output=502159d9cdc4c139 input=b602b16ac5febbe5]*/
1901{
Victor Stinner1bcc32f2020-06-10 20:08:26 +02001902 GCState *gcstate = get_gc_state();
brainfvckc75edab2017-10-16 12:49:41 -07001903 for (int i = 0; i < NUM_GENERATIONS; ++i) {
Victor Stinner67e0de62019-11-20 11:48:18 +01001904 gc_list_merge(GEN_HEAD(gcstate, i), &gcstate->permanent_generation.head);
1905 gcstate->generations[i].count = 0;
brainfvckc75edab2017-10-16 12:49:41 -07001906 }
1907 Py_RETURN_NONE;
1908}
1909
1910/*[clinic input]
1911gc.unfreeze
1912
1913Unfreeze all objects in the permanent generation.
1914
1915Put all objects in the permanent generation back into oldest generation.
1916[clinic start generated code]*/
1917
1918static PyObject *
1919gc_unfreeze_impl(PyObject *module)
1920/*[clinic end generated code: output=1c15f2043b25e169 input=2dd52b170f4cef6c]*/
1921{
Victor Stinner1bcc32f2020-06-10 20:08:26 +02001922 GCState *gcstate = get_gc_state();
Victor Stinner67e0de62019-11-20 11:48:18 +01001923 gc_list_merge(&gcstate->permanent_generation.head,
1924 GEN_HEAD(gcstate, NUM_GENERATIONS-1));
brainfvckc75edab2017-10-16 12:49:41 -07001925 Py_RETURN_NONE;
1926}
1927
1928/*[clinic input]
Victor Stinner05d68a82018-01-18 11:15:25 +01001929gc.get_freeze_count -> Py_ssize_t
brainfvckc75edab2017-10-16 12:49:41 -07001930
1931Return the number of objects in the permanent generation.
1932[clinic start generated code]*/
1933
Victor Stinner05d68a82018-01-18 11:15:25 +01001934static Py_ssize_t
brainfvckc75edab2017-10-16 12:49:41 -07001935gc_get_freeze_count_impl(PyObject *module)
Victor Stinner05d68a82018-01-18 11:15:25 +01001936/*[clinic end generated code: output=61cbd9f43aa032e1 input=45ffbc65cfe2a6ed]*/
brainfvckc75edab2017-10-16 12:49:41 -07001937{
Victor Stinner1bcc32f2020-06-10 20:08:26 +02001938 GCState *gcstate = get_gc_state();
Victor Stinner67e0de62019-11-20 11:48:18 +01001939 return gc_list_size(&gcstate->permanent_generation.head);
brainfvckc75edab2017-10-16 12:49:41 -07001940}
1941
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +00001942
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001943PyDoc_STRVAR(gc__doc__,
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001944"This module provides access to the garbage collector for reference cycles.\n"
1945"\n"
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001946"enable() -- Enable automatic garbage collection.\n"
1947"disable() -- Disable automatic garbage collection.\n"
1948"isenabled() -- Returns true if automatic collection is enabled.\n"
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001949"collect() -- Do a full collection right now.\n"
Thomas Wouters89f507f2006-12-13 04:49:30 +00001950"get_count() -- Return the current collection counts.\n"
R David Murray0e814632013-12-26 15:11:28 -05001951"get_stats() -- Return list of dictionaries containing per-generation stats.\n"
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001952"set_debug() -- Set debugging flags.\n"
1953"get_debug() -- Get debugging flags.\n"
1954"set_threshold() -- Set the collection thresholds.\n"
1955"get_threshold() -- Return the current the collection thresholds.\n"
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +00001956"get_objects() -- Return a list of all objects tracked by the collector.\n"
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001957"is_tracked() -- Returns true if a given object is tracked.\n"
Pablo Galindob6791372020-01-14 17:38:15 +00001958"is_finalized() -- Returns true if a given object has been already finalized.\n"
Jeremy Hylton5bd378b2003-04-03 16:28:38 +00001959"get_referrers() -- Return the list of objects that refer to an object.\n"
brainfvckc75edab2017-10-16 12:49:41 -07001960"get_referents() -- Return the list of objects that an object refers to.\n"
1961"freeze() -- Freeze all tracked objects and ignore them for future collections.\n"
1962"unfreeze() -- Unfreeze all objects in the permanent generation.\n"
1963"get_freeze_count() -- Return the number of objects in the permanent generation.\n");
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001964
1965static PyMethodDef GcMethods[] = {
Serhiy Storchaka93260282017-02-04 11:19:59 +02001966 GC_ENABLE_METHODDEF
1967 GC_DISABLE_METHODDEF
1968 GC_ISENABLED_METHODDEF
1969 GC_SET_DEBUG_METHODDEF
1970 GC_GET_DEBUG_METHODDEF
1971 GC_GET_COUNT_METHODDEF
Victor Stinner9db03242019-04-26 02:32:01 +02001972 {"set_threshold", gc_set_threshold, METH_VARARGS, gc_set_thresh__doc__},
Serhiy Storchaka93260282017-02-04 11:19:59 +02001973 GC_GET_THRESHOLD_METHODDEF
1974 GC_COLLECT_METHODDEF
1975 GC_GET_OBJECTS_METHODDEF
1976 GC_GET_STATS_METHODDEF
1977 GC_IS_TRACKED_METHODDEF
Pablo Galindoa2ec3f02020-01-14 12:06:45 +00001978 GC_IS_FINALIZED_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001979 {"get_referrers", gc_get_referrers, METH_VARARGS,
1980 gc_get_referrers__doc__},
1981 {"get_referents", gc_get_referents, METH_VARARGS,
1982 gc_get_referents__doc__},
brainfvckc75edab2017-10-16 12:49:41 -07001983 GC_FREEZE_METHODDEF
1984 GC_UNFREEZE_METHODDEF
1985 GC_GET_FREEZE_COUNT_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001986 {NULL, NULL} /* Sentinel */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001987};
1988
Martin v. Löwis1a214512008-06-11 05:26:20 +00001989static struct PyModuleDef gcmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001990 PyModuleDef_HEAD_INIT,
Antoine Pitrou696e0352010-08-08 22:18:46 +00001991 "gc", /* m_name */
1992 gc__doc__, /* m_doc */
1993 -1, /* m_size */
1994 GcMethods, /* m_methods */
1995 NULL, /* m_reload */
1996 NULL, /* m_traverse */
1997 NULL, /* m_clear */
1998 NULL /* m_free */
Martin v. Löwis1a214512008-06-11 05:26:20 +00001999};
2000
Jason Tishler6bc06ec2003-09-04 11:59:50 +00002001PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00002002PyInit_gc(void)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00002003{
Victor Stinner1bcc32f2020-06-10 20:08:26 +02002004 GCState *gcstate = get_gc_state();
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00002005
Victor Stinner72474072019-11-20 12:25:50 +01002006 PyObject *m = PyModule_Create(&gcmodule);
Martin v. Löwis1a214512008-06-11 05:26:20 +00002007
Victor Stinner9db03242019-04-26 02:32:01 +02002008 if (m == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002009 return NULL;
Victor Stinner9db03242019-04-26 02:32:01 +02002010 }
Tim Peters11558872003-04-06 23:30:52 +00002011
Victor Stinner67e0de62019-11-20 11:48:18 +01002012 if (gcstate->garbage == NULL) {
2013 gcstate->garbage = PyList_New(0);
Victor Stinner72474072019-11-20 12:25:50 +01002014 if (gcstate->garbage == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002015 return NULL;
Victor Stinner72474072019-11-20 12:25:50 +01002016 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002017 }
Victor Stinner67e0de62019-11-20 11:48:18 +01002018 Py_INCREF(gcstate->garbage);
Victor Stinner72474072019-11-20 12:25:50 +01002019 if (PyModule_AddObject(m, "garbage", gcstate->garbage) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002020 return NULL;
Victor Stinner72474072019-11-20 12:25:50 +01002021 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002022
Victor Stinner67e0de62019-11-20 11:48:18 +01002023 if (gcstate->callbacks == NULL) {
2024 gcstate->callbacks = PyList_New(0);
Victor Stinner72474072019-11-20 12:25:50 +01002025 if (gcstate->callbacks == NULL) {
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00002026 return NULL;
Victor Stinner72474072019-11-20 12:25:50 +01002027 }
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00002028 }
Victor Stinner67e0de62019-11-20 11:48:18 +01002029 Py_INCREF(gcstate->callbacks);
Victor Stinner72474072019-11-20 12:25:50 +01002030 if (PyModule_AddObject(m, "callbacks", gcstate->callbacks) < 0) {
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00002031 return NULL;
Victor Stinner72474072019-11-20 12:25:50 +01002032 }
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00002033
Victor Stinner72474072019-11-20 12:25:50 +01002034#define ADD_INT(NAME) if (PyModule_AddIntConstant(m, #NAME, NAME) < 0) { return NULL; }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002035 ADD_INT(DEBUG_STATS);
2036 ADD_INT(DEBUG_COLLECTABLE);
2037 ADD_INT(DEBUG_UNCOLLECTABLE);
2038 ADD_INT(DEBUG_SAVEALL);
2039 ADD_INT(DEBUG_LEAK);
Tim Peters11558872003-04-06 23:30:52 +00002040#undef ADD_INT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002041 return m;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00002042}
2043
Guido van Rossume13ddc92003-04-17 17:29:22 +00002044/* API to invoke gc.collect() from C */
Neal Norwitz7b216c52006-03-04 20:01:53 +00002045Py_ssize_t
Guido van Rossume13ddc92003-04-17 17:29:22 +00002046PyGC_Collect(void)
2047{
Victor Stinner2e969062019-11-20 01:49:32 +01002048 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner72474072019-11-20 12:25:50 +01002049 GCState *gcstate = &tstate->interp->gc;
Victor Stinner2e969062019-11-20 01:49:32 +01002050
Victor Stinner67e0de62019-11-20 11:48:18 +01002051 if (!gcstate->enabled) {
Victor Stinner9db03242019-04-26 02:32:01 +02002052 return 0;
2053 }
Guido van Rossume13ddc92003-04-17 17:29:22 +00002054
Victor Stinner9db03242019-04-26 02:32:01 +02002055 Py_ssize_t n;
Victor Stinner67e0de62019-11-20 11:48:18 +01002056 if (gcstate->collecting) {
Victor Stinner9db03242019-04-26 02:32:01 +02002057 /* already collecting, don't do anything */
2058 n = 0;
2059 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002060 else {
Serhiy Storchaka301e3cc2018-05-24 15:19:29 +03002061 PyObject *exc, *value, *tb;
Victor Stinner67e0de62019-11-20 11:48:18 +01002062 gcstate->collecting = 1;
Victor Stinner2e969062019-11-20 01:49:32 +01002063 _PyErr_Fetch(tstate, &exc, &value, &tb);
Victor Stinner67e0de62019-11-20 11:48:18 +01002064 n = collect_with_callback(tstate, NUM_GENERATIONS - 1);
Victor Stinner2e969062019-11-20 01:49:32 +01002065 _PyErr_Restore(tstate, exc, value, tb);
Victor Stinner67e0de62019-11-20 11:48:18 +01002066 gcstate->collecting = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002067 }
Guido van Rossume13ddc92003-04-17 17:29:22 +00002068
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002069 return n;
Guido van Rossume13ddc92003-04-17 17:29:22 +00002070}
2071
Antoine Pitroufef34e32013-05-19 01:11:58 +02002072Py_ssize_t
Łukasz Langafef7e942016-09-09 21:47:46 -07002073_PyGC_CollectIfEnabled(void)
2074{
Łukasz Langafef7e942016-09-09 21:47:46 -07002075 return PyGC_Collect();
2076}
2077
2078Py_ssize_t
Antoine Pitroufef34e32013-05-19 01:11:58 +02002079_PyGC_CollectNoFail(void)
2080{
Victor Stinner2e969062019-11-20 01:49:32 +01002081 PyThreadState *tstate = _PyThreadState_GET();
2082 assert(!_PyErr_Occurred(tstate));
Victor Stinner9db03242019-04-26 02:32:01 +02002083
Victor Stinner72474072019-11-20 12:25:50 +01002084 GCState *gcstate = &tstate->interp->gc;
Antoine Pitroufef34e32013-05-19 01:11:58 +02002085 Py_ssize_t n;
2086
Antoine Pitrouc69c9bc2013-08-15 20:15:15 +02002087 /* Ideally, this function is only called on interpreter shutdown,
2088 and therefore not recursively. Unfortunately, when there are daemon
2089 threads, a daemon thread can start a cyclic garbage collection
2090 during interpreter shutdown (and then never finish it).
2091 See http://bugs.python.org/issue8713#msg195178 for an example.
2092 */
Victor Stinner67e0de62019-11-20 11:48:18 +01002093 if (gcstate->collecting) {
Antoine Pitrouc69c9bc2013-08-15 20:15:15 +02002094 n = 0;
Victor Stinner9db03242019-04-26 02:32:01 +02002095 }
Antoine Pitrouc69c9bc2013-08-15 20:15:15 +02002096 else {
Victor Stinner67e0de62019-11-20 11:48:18 +01002097 gcstate->collecting = 1;
2098 n = collect(tstate, NUM_GENERATIONS - 1, NULL, NULL, 1);
2099 gcstate->collecting = 0;
Antoine Pitrouc69c9bc2013-08-15 20:15:15 +02002100 }
Antoine Pitroufef34e32013-05-19 01:11:58 +02002101 return n;
2102}
Antoine Pitrou5f454a02013-05-06 21:15:57 +02002103
Antoine Pitrou696e0352010-08-08 22:18:46 +00002104void
Victor Stinner67e0de62019-11-20 11:48:18 +01002105_PyGC_DumpShutdownStats(PyThreadState *tstate)
Antoine Pitrou696e0352010-08-08 22:18:46 +00002106{
Victor Stinner72474072019-11-20 12:25:50 +01002107 GCState *gcstate = &tstate->interp->gc;
Victor Stinner67e0de62019-11-20 11:48:18 +01002108 if (!(gcstate->debug & DEBUG_SAVEALL)
2109 && gcstate->garbage != NULL && PyList_GET_SIZE(gcstate->garbage) > 0) {
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02002110 const char *message;
Victor Stinner67e0de62019-11-20 11:48:18 +01002111 if (gcstate->debug & DEBUG_UNCOLLECTABLE)
Antoine Pitroub5d82042010-11-05 00:05:25 +00002112 message = "gc: %zd uncollectable objects at " \
Georg Brandl08be72d2010-10-24 15:11:22 +00002113 "shutdown";
2114 else
Antoine Pitroub5d82042010-11-05 00:05:25 +00002115 message = "gc: %zd uncollectable objects at " \
Georg Brandl08be72d2010-10-24 15:11:22 +00002116 "shutdown; use gc.set_debug(gc.DEBUG_UNCOLLECTABLE) to list them";
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02002117 /* PyErr_WarnFormat does too many things and we are at shutdown,
2118 the warnings module's dependencies (e.g. linecache) may be gone
2119 already. */
2120 if (PyErr_WarnExplicitFormat(PyExc_ResourceWarning, "gc", 0,
2121 "gc", NULL, message,
Victor Stinner67e0de62019-11-20 11:48:18 +01002122 PyList_GET_SIZE(gcstate->garbage)))
Georg Brandl08be72d2010-10-24 15:11:22 +00002123 PyErr_WriteUnraisable(NULL);
Victor Stinner67e0de62019-11-20 11:48:18 +01002124 if (gcstate->debug & DEBUG_UNCOLLECTABLE) {
Antoine Pitrou696e0352010-08-08 22:18:46 +00002125 PyObject *repr = NULL, *bytes = NULL;
Victor Stinner67e0de62019-11-20 11:48:18 +01002126 repr = PyObject_Repr(gcstate->garbage);
Antoine Pitrou696e0352010-08-08 22:18:46 +00002127 if (!repr || !(bytes = PyUnicode_EncodeFSDefault(repr)))
Victor Stinner67e0de62019-11-20 11:48:18 +01002128 PyErr_WriteUnraisable(gcstate->garbage);
Antoine Pitrou696e0352010-08-08 22:18:46 +00002129 else {
2130 PySys_WriteStderr(
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02002131 " %s\n",
Antoine Pitrou696e0352010-08-08 22:18:46 +00002132 PyBytes_AS_STRING(bytes)
2133 );
2134 }
2135 Py_XDECREF(repr);
2136 Py_XDECREF(bytes);
2137 }
Antoine Pitrou696e0352010-08-08 22:18:46 +00002138 }
Antoine Pitrou5f454a02013-05-06 21:15:57 +02002139}
2140
2141void
Victor Stinner7eee5be2019-11-20 10:38:34 +01002142_PyGC_Fini(PyThreadState *tstate)
Antoine Pitrou5f454a02013-05-06 21:15:57 +02002143{
Victor Stinner72474072019-11-20 12:25:50 +01002144 GCState *gcstate = &tstate->interp->gc;
Victor Stinner67e0de62019-11-20 11:48:18 +01002145 Py_CLEAR(gcstate->garbage);
2146 Py_CLEAR(gcstate->callbacks);
Antoine Pitrou696e0352010-08-08 22:18:46 +00002147}
2148
Neil Schemenauer43411b52001-08-30 00:05:51 +00002149/* for debugging */
Guido van Rossume13ddc92003-04-17 17:29:22 +00002150void
2151_PyGC_Dump(PyGC_Head *g)
Neil Schemenauer43411b52001-08-30 00:05:51 +00002152{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002153 _PyObject_Dump(FROM_GC(g));
Neil Schemenauer43411b52001-08-30 00:05:51 +00002154}
2155
Victor Stinnera5447732019-10-10 09:32:13 +02002156
2157#ifdef Py_DEBUG
Victor Stinner1b184552019-10-08 00:09:31 +02002158static int
2159visit_validate(PyObject *op, void *parent_raw)
2160{
2161 PyObject *parent = _PyObject_CAST(parent_raw);
2162 if (_PyObject_IsFreed(op)) {
2163 _PyObject_ASSERT_FAILED_MSG(parent,
2164 "PyObject_GC_Track() object is not valid");
2165 }
2166 return 0;
2167}
Victor Stinnera5447732019-10-10 09:32:13 +02002168#endif
Victor Stinner1b184552019-10-08 00:09:31 +02002169
2170
Neil Schemenauer43411b52001-08-30 00:05:51 +00002171/* extension modules might be compiled with GC support so these
2172 functions must always be available */
2173
2174void
Victor Stinnera42de742018-11-22 10:25:22 +01002175PyObject_GC_Track(void *op_raw)
Neil Schemenauer43411b52001-08-30 00:05:51 +00002176{
Victor Stinnera42de742018-11-22 10:25:22 +01002177 PyObject *op = _PyObject_CAST(op_raw);
Victor Stinner271753a2018-11-22 01:02:54 +01002178 if (_PyObject_GC_IS_TRACKED(op)) {
2179 _PyObject_ASSERT_FAILED_MSG(op,
2180 "object already tracked "
2181 "by the garbage collector");
2182 }
Victor Stinnera42de742018-11-22 10:25:22 +01002183 _PyObject_GC_TRACK(op);
Victor Stinner1b184552019-10-08 00:09:31 +02002184
2185#ifdef Py_DEBUG
2186 /* Check that the object is valid: validate objects traversed
2187 by tp_traverse() */
2188 traverseproc traverse = Py_TYPE(op)->tp_traverse;
2189 (void)traverse(op, visit_validate, op);
2190#endif
Neil Schemenauer43411b52001-08-30 00:05:51 +00002191}
2192
Neil Schemenauerfec4eb12002-04-12 02:41:03 +00002193void
Victor Stinnera42de742018-11-22 10:25:22 +01002194PyObject_GC_UnTrack(void *op_raw)
Neil Schemenauer43411b52001-08-30 00:05:51 +00002195{
Victor Stinnera42de742018-11-22 10:25:22 +01002196 PyObject *op = _PyObject_CAST(op_raw);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002197 /* Obscure: the Py_TRASHCAN mechanism requires that we be able to
2198 * call PyObject_GC_UnTrack twice on an object.
2199 */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002200 if (_PyObject_GC_IS_TRACKED(op)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002201 _PyObject_GC_UNTRACK(op);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002202 }
Neil Schemenauer43411b52001-08-30 00:05:51 +00002203}
2204
Hai Shi675d9a32020-04-15 02:11:20 +08002205int
2206PyObject_IS_GC(PyObject *obj)
2207{
2208 return _PyObject_IS_GC(obj);
2209}
2210
Victor Stinnerdb067af2014-05-02 22:31:14 +02002211static PyObject *
2212_PyObject_GC_Alloc(int use_calloc, size_t basicsize)
Neil Schemenauer43411b52001-08-30 00:05:51 +00002213{
Victor Stinner67e0de62019-11-20 11:48:18 +01002214 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner72474072019-11-20 12:25:50 +01002215 GCState *gcstate = &tstate->interp->gc;
Victor Stinner2e969062019-11-20 01:49:32 +01002216 if (basicsize > PY_SSIZE_T_MAX - sizeof(PyGC_Head)) {
Victor Stinner67e0de62019-11-20 11:48:18 +01002217 return _PyErr_NoMemory(tstate);
Victor Stinner2e969062019-11-20 01:49:32 +01002218 }
2219 size_t size = sizeof(PyGC_Head) + basicsize;
2220
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002221 PyGC_Head *g;
Victor Stinner2e969062019-11-20 01:49:32 +01002222 if (use_calloc) {
Victor Stinnerdb067af2014-05-02 22:31:14 +02002223 g = (PyGC_Head *)PyObject_Calloc(1, size);
Victor Stinner2e969062019-11-20 01:49:32 +01002224 }
2225 else {
Victor Stinnerdb067af2014-05-02 22:31:14 +02002226 g = (PyGC_Head *)PyObject_Malloc(size);
Victor Stinner2e969062019-11-20 01:49:32 +01002227 }
2228 if (g == NULL) {
Victor Stinner67e0de62019-11-20 11:48:18 +01002229 return _PyErr_NoMemory(tstate);
Victor Stinner2e969062019-11-20 01:49:32 +01002230 }
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002231 assert(((uintptr_t)g & 3) == 0); // g must be aligned 4bytes boundary
Victor Stinner2e969062019-11-20 01:49:32 +01002232
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002233 g->_gc_next = 0;
2234 g->_gc_prev = 0;
Victor Stinner67e0de62019-11-20 11:48:18 +01002235 gcstate->generations[0].count++; /* number of allocated GC objects */
2236 if (gcstate->generations[0].count > gcstate->generations[0].threshold &&
2237 gcstate->enabled &&
2238 gcstate->generations[0].threshold &&
2239 !gcstate->collecting &&
2240 !_PyErr_Occurred(tstate))
Victor Stinner2e969062019-11-20 01:49:32 +01002241 {
Victor Stinner67e0de62019-11-20 11:48:18 +01002242 gcstate->collecting = 1;
2243 collect_generations(tstate);
2244 gcstate->collecting = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002245 }
Victor Stinner2e969062019-11-20 01:49:32 +01002246 PyObject *op = FROM_GC(g);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002247 return op;
Neil Schemenauer43411b52001-08-30 00:05:51 +00002248}
2249
2250PyObject *
Victor Stinnerdb067af2014-05-02 22:31:14 +02002251_PyObject_GC_Malloc(size_t basicsize)
2252{
2253 return _PyObject_GC_Alloc(0, basicsize);
2254}
2255
2256PyObject *
2257_PyObject_GC_Calloc(size_t basicsize)
2258{
2259 return _PyObject_GC_Alloc(1, basicsize);
2260}
2261
2262PyObject *
Neil Schemenauer43411b52001-08-30 00:05:51 +00002263_PyObject_GC_New(PyTypeObject *tp)
2264{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002265 PyObject *op = _PyObject_GC_Malloc(_PyObject_SIZE(tp));
Victor Stinner04fc4f22020-06-16 01:28:07 +02002266 if (op == NULL) {
2267 return NULL;
2268 }
2269 _PyObject_Init(op, tp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002270 return op;
Neil Schemenauer43411b52001-08-30 00:05:51 +00002271}
2272
2273PyVarObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00002274_PyObject_GC_NewVar(PyTypeObject *tp, Py_ssize_t nitems)
Neil Schemenauer43411b52001-08-30 00:05:51 +00002275{
Victor Stinner5d1866c2013-07-08 22:17:52 +02002276 size_t size;
2277 PyVarObject *op;
2278
2279 if (nitems < 0) {
2280 PyErr_BadInternalCall();
2281 return NULL;
2282 }
2283 size = _PyObject_VAR_SIZE(tp, nitems);
2284 op = (PyVarObject *) _PyObject_GC_Malloc(size);
Victor Stinner04fc4f22020-06-16 01:28:07 +02002285 if (op == NULL) {
2286 return NULL;
2287 }
2288 _PyObject_InitVar(op, tp, nitems);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002289 return op;
Neil Schemenauer43411b52001-08-30 00:05:51 +00002290}
2291
2292PyVarObject *
Martin v. Löwis41290682006-02-16 14:56:14 +00002293_PyObject_GC_Resize(PyVarObject *op, Py_ssize_t nitems)
Neil Schemenauer43411b52001-08-30 00:05:51 +00002294{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002295 const size_t basicsize = _PyObject_VAR_SIZE(Py_TYPE(op), nitems);
Victor Stinnera4b2bc72018-10-26 18:00:13 +02002296 _PyObject_ASSERT((PyObject *)op, !_PyObject_GC_IS_TRACKED(op));
2297 if (basicsize > PY_SSIZE_T_MAX - sizeof(PyGC_Head)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002298 return (PyVarObject *)PyErr_NoMemory();
Victor Stinnera4b2bc72018-10-26 18:00:13 +02002299 }
2300
2301 PyGC_Head *g = AS_GC(op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002302 g = (PyGC_Head *)PyObject_REALLOC(g, sizeof(PyGC_Head) + basicsize);
2303 if (g == NULL)
2304 return (PyVarObject *)PyErr_NoMemory();
2305 op = (PyVarObject *) FROM_GC(g);
Victor Stinner60ac6ed2020-02-07 23:18:08 +01002306 Py_SET_SIZE(op, nitems);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002307 return op;
Neil Schemenauer43411b52001-08-30 00:05:51 +00002308}
2309
2310void
Neil Schemenauerfec4eb12002-04-12 02:41:03 +00002311PyObject_GC_Del(void *op)
Neil Schemenauer43411b52001-08-30 00:05:51 +00002312{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002313 PyGC_Head *g = AS_GC(op);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002314 if (_PyObject_GC_IS_TRACKED(op)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002315 gc_list_remove(g);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002316 }
Victor Stinner1bcc32f2020-06-10 20:08:26 +02002317 GCState *gcstate = get_gc_state();
Victor Stinner67e0de62019-11-20 11:48:18 +01002318 if (gcstate->generations[0].count > 0) {
2319 gcstate->generations[0].count--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002320 }
2321 PyObject_FREE(g);
Neil Schemenauer43411b52001-08-30 00:05:51 +00002322}
Pablo Galindof13072b2020-04-11 01:21:54 +01002323
2324int
2325PyObject_GC_IsTracked(PyObject* obj)
2326{
Hai Shi675d9a32020-04-15 02:11:20 +08002327 if (_PyObject_IS_GC(obj) && _PyObject_GC_IS_TRACKED(obj)) {
Pablo Galindof13072b2020-04-11 01:21:54 +01002328 return 1;
2329 }
2330 return 0;
2331}
2332
2333int
2334PyObject_GC_IsFinalized(PyObject *obj)
2335{
Hai Shi675d9a32020-04-15 02:11:20 +08002336 if (_PyObject_IS_GC(obj) && _PyGCHead_FINALIZED(AS_GC(obj))) {
Pablo Galindof13072b2020-04-11 01:21:54 +01002337 return 1;
2338 }
2339 return 0;
2340}