blob: f0d569949082331801af97aa269ce637916becdc [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"
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000034
Victor Stinner67e0de62019-11-20 11:48:18 +010035typedef struct _gc_runtime_state GCState;
36
Serhiy Storchaka93260282017-02-04 11:19:59 +020037/*[clinic input]
38module gc
39[clinic start generated code]*/
40/*[clinic end generated code: output=da39a3ee5e6b4b0d input=b5c9690ecc842d79]*/
41
Pablo Galindo320dd502019-10-10 22:45:17 +010042
43#ifdef Py_DEBUG
44# define GC_DEBUG
45#endif
INADA Naoki5ac9e6e2018-07-10 17:19:53 +090046
47#define GC_NEXT _PyGCHead_NEXT
48#define GC_PREV _PyGCHead_PREV
49
50// update_refs() set this bit for all objects in current generation.
51// subtract_refs() and move_unreachable() uses this to distinguish
52// visited object is in GCing or not.
53//
54// move_unreachable() removes this flag from reachable objects.
55// Only unreachable objects have this flag.
56//
57// No objects in interpreter have this flag after GC ends.
58#define PREV_MASK_COLLECTING _PyGC_PREV_MASK_COLLECTING
59
60// Lowest bit of _gc_next is used for UNREACHABLE flag.
61//
62// This flag represents the object is in unreachable list in move_unreachable()
63//
64// Although this flag is used only in move_unreachable(), move_unreachable()
65// doesn't clear this flag to skip unnecessary iteration.
66// move_legacy_finalizers() removes this flag instead.
67// Between them, unreachable list is not normal list and we can not use
68// most gc_list_* functions for it.
69#define NEXT_MASK_UNREACHABLE (1)
70
Victor Stinner626bff82018-10-25 17:31:10 +020071/* Get an object's GC head */
72#define AS_GC(o) ((PyGC_Head *)(o)-1)
73
74/* Get the object given the GC head */
75#define FROM_GC(g) ((PyObject *)(((PyGC_Head *)g)+1))
76
INADA Naoki5ac9e6e2018-07-10 17:19:53 +090077static inline int
78gc_is_collecting(PyGC_Head *g)
79{
80 return (g->_gc_prev & PREV_MASK_COLLECTING) != 0;
81}
82
83static inline void
84gc_clear_collecting(PyGC_Head *g)
85{
86 g->_gc_prev &= ~PREV_MASK_COLLECTING;
87}
88
89static inline Py_ssize_t
90gc_get_refs(PyGC_Head *g)
91{
92 return (Py_ssize_t)(g->_gc_prev >> _PyGC_PREV_SHIFT);
93}
94
95static inline void
96gc_set_refs(PyGC_Head *g, Py_ssize_t refs)
97{
98 g->_gc_prev = (g->_gc_prev & ~_PyGC_PREV_MASK)
99 | ((uintptr_t)(refs) << _PyGC_PREV_SHIFT);
100}
101
102static inline void
103gc_reset_refs(PyGC_Head *g, Py_ssize_t refs)
104{
105 g->_gc_prev = (g->_gc_prev & _PyGC_PREV_MASK_FINALIZED)
106 | PREV_MASK_COLLECTING
107 | ((uintptr_t)(refs) << _PyGC_PREV_SHIFT);
108}
109
110static inline void
111gc_decref(PyGC_Head *g)
112{
Victor Stinner626bff82018-10-25 17:31:10 +0200113 _PyObject_ASSERT_WITH_MSG(FROM_GC(g),
114 gc_get_refs(g) > 0,
115 "refcount is too small");
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900116 g->_gc_prev -= 1 << _PyGC_PREV_SHIFT;
117}
118
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000119/* set for debugging information */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000120#define DEBUG_STATS (1<<0) /* print collection statistics */
121#define DEBUG_COLLECTABLE (1<<1) /* print collectable objects */
122#define DEBUG_UNCOLLECTABLE (1<<2) /* print uncollectable objects */
123#define DEBUG_SAVEALL (1<<5) /* save all garbage in gc.garbage */
124#define DEBUG_LEAK DEBUG_COLLECTABLE | \
125 DEBUG_UNCOLLECTABLE | \
126 DEBUG_SAVEALL
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000127
Victor Stinner67e0de62019-11-20 11:48:18 +0100128#define GEN_HEAD(gcstate, n) (&(gcstate)->generations[n].head)
Antoine Pitroud4156c12012-10-30 22:43:19 +0100129
Victor Stinner1bcc32f2020-06-10 20:08:26 +0200130
131static GCState *
132get_gc_state(void)
133{
134 PyInterpreterState *interp = _PyInterpreterState_GET();
135 return &interp->gc;
136}
137
138
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600139void
Victor Stinner72474072019-11-20 12:25:50 +0100140_PyGC_InitState(GCState *gcstate)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600141{
Victor Stinner67e0de62019-11-20 11:48:18 +0100142 gcstate->enabled = 1; /* automatic collection enabled? */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600143
Victor Stinner67e0de62019-11-20 11:48:18 +0100144#define _GEN_HEAD(n) GEN_HEAD(gcstate, n)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600145 struct gc_generation generations[NUM_GENERATIONS] = {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900146 /* PyGC_Head, threshold, count */
147 {{(uintptr_t)_GEN_HEAD(0), (uintptr_t)_GEN_HEAD(0)}, 700, 0},
148 {{(uintptr_t)_GEN_HEAD(1), (uintptr_t)_GEN_HEAD(1)}, 10, 0},
149 {{(uintptr_t)_GEN_HEAD(2), (uintptr_t)_GEN_HEAD(2)}, 10, 0},
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600150 };
151 for (int i = 0; i < NUM_GENERATIONS; i++) {
Victor Stinner67e0de62019-11-20 11:48:18 +0100152 gcstate->generations[i] = generations[i];
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600153 };
Victor Stinner67e0de62019-11-20 11:48:18 +0100154 gcstate->generation0 = GEN_HEAD(gcstate, 0);
brainfvckc75edab2017-10-16 12:49:41 -0700155 struct gc_generation permanent_generation = {
Victor Stinner67e0de62019-11-20 11:48:18 +0100156 {(uintptr_t)&gcstate->permanent_generation.head,
157 (uintptr_t)&gcstate->permanent_generation.head}, 0, 0
brainfvckc75edab2017-10-16 12:49:41 -0700158 };
Victor Stinner67e0de62019-11-20 11:48:18 +0100159 gcstate->permanent_generation = permanent_generation;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600160}
Antoine Pitroud4156c12012-10-30 22:43:19 +0100161
Victor Stinner444b39b2019-11-20 01:18:11 +0100162
163PyStatus
Victor Stinner01b1cc12019-11-20 02:27:56 +0100164_PyGC_Init(PyThreadState *tstate)
Victor Stinner444b39b2019-11-20 01:18:11 +0100165{
Victor Stinner72474072019-11-20 12:25:50 +0100166 GCState *gcstate = &tstate->interp->gc;
Christian Heimes646d7fd2020-11-19 15:08:34 +0100167
168 gcstate->garbage = PyList_New(0);
Victor Stinner67e0de62019-11-20 11:48:18 +0100169 if (gcstate->garbage == NULL) {
Christian Heimes646d7fd2020-11-19 15:08:34 +0100170 return _PyStatus_NO_MEMORY();
Victor Stinner444b39b2019-11-20 01:18:11 +0100171 }
Christian Heimes646d7fd2020-11-19 15:08:34 +0100172
173 gcstate->callbacks = PyList_New(0);
174 if (gcstate->callbacks == NULL) {
175 return _PyStatus_NO_MEMORY();
176 }
177
Victor Stinner444b39b2019-11-20 01:18:11 +0100178 return _PyStatus_OK();
179}
180
181
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900182/*
183_gc_prev values
184---------------
Neil Schemenauer43411b52001-08-30 00:05:51 +0000185
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900186Between collections, _gc_prev is used for doubly linked list.
Tim Peters6fc13d92002-07-02 18:12:35 +0000187
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900188Lowest two bits of _gc_prev are used for flags.
189PREV_MASK_COLLECTING is used only while collecting and cleared before GC ends
190or _PyObject_GC_UNTRACK() is called.
Tim Peters6fc13d92002-07-02 18:12:35 +0000191
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900192During a collection, _gc_prev is temporary used for gc_refs, and the gc list
193is singly linked until _gc_prev is restored.
Tim Peters6fc13d92002-07-02 18:12:35 +0000194
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900195gc_refs
Tim Peters6fc13d92002-07-02 18:12:35 +0000196 At the start of a collection, update_refs() copies the true refcount
197 to gc_refs, for each object in the generation being collected.
198 subtract_refs() then adjusts gc_refs so that it equals the number of
199 times an object is referenced directly from outside the generation
200 being collected.
Tim Peters6fc13d92002-07-02 18:12:35 +0000201
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900202PREV_MASK_COLLECTING
203 Objects in generation being collected are marked PREV_MASK_COLLECTING in
204 update_refs().
205
206
207_gc_next values
208---------------
209
210_gc_next takes these values:
211
2120
213 The object is not tracked
214
215!= 0
216 Pointer to the next object in the GC list.
217 Additionally, lowest bit is used temporary for
218 NEXT_MASK_UNREACHABLE flag described below.
219
220NEXT_MASK_UNREACHABLE
Tim Peters6fc13d92002-07-02 18:12:35 +0000221 move_unreachable() then moves objects not reachable (whether directly or
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900222 indirectly) from outside the generation into an "unreachable" set and
223 set this flag.
Tim Peters6fc13d92002-07-02 18:12:35 +0000224
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900225 Objects that are found to be reachable have gc_refs set to 1.
226 When this flag is set for the reachable object, the object must be in
227 "unreachable" set.
228 The flag is unset and the object is moved back to "reachable" set.
229
230 move_legacy_finalizers() will remove this flag from "unreachable" set.
Tim Peters6fc13d92002-07-02 18:12:35 +0000231*/
Neil Schemenauera2b11ec2002-05-21 15:53:24 +0000232
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000233/*** list functions ***/
234
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900235static inline void
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000236gc_list_init(PyGC_Head *list)
237{
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900238 // List header must not have flags.
239 // We can assign pointer by simple cast.
240 list->_gc_prev = (uintptr_t)list;
241 list->_gc_next = (uintptr_t)list;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000242}
243
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900244static inline int
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000245gc_list_is_empty(PyGC_Head *list)
246{
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900247 return (list->_gc_next == (uintptr_t)list);
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000248}
249
Tim Peterse2d59182004-11-01 01:39:08 +0000250/* Append `node` to `list`. */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900251static inline void
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000252gc_list_append(PyGC_Head *node, PyGC_Head *list)
253{
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900254 PyGC_Head *last = (PyGC_Head *)list->_gc_prev;
255
256 // last <-> node
257 _PyGCHead_SET_PREV(node, last);
258 _PyGCHead_SET_NEXT(last, node);
259
260 // node <-> list
261 _PyGCHead_SET_NEXT(node, list);
262 list->_gc_prev = (uintptr_t)node;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000263}
264
Tim Peterse2d59182004-11-01 01:39:08 +0000265/* Remove `node` from the gc list it's currently in. */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900266static inline void
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000267gc_list_remove(PyGC_Head *node)
268{
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900269 PyGC_Head *prev = GC_PREV(node);
270 PyGC_Head *next = GC_NEXT(node);
271
272 _PyGCHead_SET_NEXT(prev, next);
273 _PyGCHead_SET_PREV(next, prev);
274
275 node->_gc_next = 0; /* object is not currently tracked */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000276}
277
Tim Peterse2d59182004-11-01 01:39:08 +0000278/* Move `node` from the gc list it's currently in (which is not explicitly
279 * named here) to the end of `list`. This is semantically the same as
280 * gc_list_remove(node) followed by gc_list_append(node, list).
281 */
282static void
283gc_list_move(PyGC_Head *node, PyGC_Head *list)
284{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000285 /* Unlink from current list. */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900286 PyGC_Head *from_prev = GC_PREV(node);
287 PyGC_Head *from_next = GC_NEXT(node);
288 _PyGCHead_SET_NEXT(from_prev, from_next);
289 _PyGCHead_SET_PREV(from_next, from_prev);
290
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000291 /* Relink at end of new list. */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900292 // list must not have flags. So we can skip macros.
293 PyGC_Head *to_prev = (PyGC_Head*)list->_gc_prev;
294 _PyGCHead_SET_PREV(node, to_prev);
295 _PyGCHead_SET_NEXT(to_prev, node);
296 list->_gc_prev = (uintptr_t)node;
297 _PyGCHead_SET_NEXT(node, list);
Tim Peterse2d59182004-11-01 01:39:08 +0000298}
299
300/* append list `from` onto list `to`; `from` becomes an empty list */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000301static void
302gc_list_merge(PyGC_Head *from, PyGC_Head *to)
303{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 assert(from != to);
305 if (!gc_list_is_empty(from)) {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900306 PyGC_Head *to_tail = GC_PREV(to);
307 PyGC_Head *from_head = GC_NEXT(from);
308 PyGC_Head *from_tail = GC_PREV(from);
309 assert(from_head != from);
310 assert(from_tail != from);
311
312 _PyGCHead_SET_NEXT(to_tail, from_head);
313 _PyGCHead_SET_PREV(from_head, to_tail);
314
315 _PyGCHead_SET_NEXT(from_tail, to);
316 _PyGCHead_SET_PREV(to, from_tail);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000317 }
318 gc_list_init(from);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000319}
320
Neal Norwitz7b216c52006-03-04 20:01:53 +0000321static Py_ssize_t
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000322gc_list_size(PyGC_Head *list)
323{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324 PyGC_Head *gc;
325 Py_ssize_t n = 0;
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900326 for (gc = GC_NEXT(list); gc != list; gc = GC_NEXT(gc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 n++;
328 }
329 return n;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000330}
331
Pablo Galindo466326d2019-10-13 16:48:59 +0100332/* Walk the list and mark all objects as non-collecting */
333static inline void
334gc_list_clear_collecting(PyGC_Head *collectable)
335{
336 PyGC_Head *gc;
337 for (gc = GC_NEXT(collectable); gc != collectable; gc = GC_NEXT(gc)) {
338 gc_clear_collecting(gc);
339 }
340}
341
Tim Peters259272b2003-04-06 19:41:39 +0000342/* Append objects in a GC list to a Python list.
Pablo Galindo466326d2019-10-13 16:48:59 +0100343 * Return 0 if all OK, < 0 if error (out of memory for list)
Tim Peters259272b2003-04-06 19:41:39 +0000344 */
345static int
346append_objects(PyObject *py_list, PyGC_Head *gc_list)
347{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 PyGC_Head *gc;
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900349 for (gc = GC_NEXT(gc_list); gc != gc_list; gc = GC_NEXT(gc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000350 PyObject *op = FROM_GC(gc);
351 if (op != py_list) {
352 if (PyList_Append(py_list, op)) {
353 return -1; /* exception */
354 }
355 }
356 }
357 return 0;
Tim Peters259272b2003-04-06 19:41:39 +0000358}
359
Tim Petersea55c512019-10-18 20:59:14 -0500360// Constants for validate_list's flags argument.
361enum flagstates {collecting_clear_unreachable_clear,
362 collecting_clear_unreachable_set,
363 collecting_set_unreachable_clear,
364 collecting_set_unreachable_set};
365
Pablo Galindo320dd502019-10-10 22:45:17 +0100366#ifdef GC_DEBUG
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900367// validate_list checks list consistency. And it works as document
Tim Peters95bfc8a2019-10-13 16:47:04 -0500368// describing when flags are expected to be set / unset.
369// `head` must be a doubly-linked gc list, although it's fine (expected!) if
370// the prev and next pointers are "polluted" with flags.
371// What's checked:
372// - The `head` pointers are not polluted.
Tim Petersea55c512019-10-18 20:59:14 -0500373// - The objects' PREV_MASK_COLLECTING and NEXT_MASK_UNREACHABLE flags are all
374// `set or clear, as specified by the 'flags' argument.
Tim Peters95bfc8a2019-10-13 16:47:04 -0500375// - The prev and next pointers are mutually consistent.
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900376static void
Tim Petersea55c512019-10-18 20:59:14 -0500377validate_list(PyGC_Head *head, enum flagstates flags)
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900378{
Tim Peters95bfc8a2019-10-13 16:47:04 -0500379 assert((head->_gc_prev & PREV_MASK_COLLECTING) == 0);
380 assert((head->_gc_next & NEXT_MASK_UNREACHABLE) == 0);
Tim Petersea55c512019-10-18 20:59:14 -0500381 uintptr_t prev_value = 0, next_value = 0;
382 switch (flags) {
383 case collecting_clear_unreachable_clear:
384 break;
385 case collecting_set_unreachable_clear:
386 prev_value = PREV_MASK_COLLECTING;
387 break;
388 case collecting_clear_unreachable_set:
389 next_value = NEXT_MASK_UNREACHABLE;
390 break;
391 case collecting_set_unreachable_set:
392 prev_value = PREV_MASK_COLLECTING;
393 next_value = NEXT_MASK_UNREACHABLE;
394 break;
395 default:
396 assert(! "bad internal flags argument");
397 }
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900398 PyGC_Head *prev = head;
399 PyGC_Head *gc = GC_NEXT(head);
400 while (gc != head) {
Tim Peters95bfc8a2019-10-13 16:47:04 -0500401 PyGC_Head *trueprev = GC_PREV(gc);
402 PyGC_Head *truenext = (PyGC_Head *)(gc->_gc_next & ~NEXT_MASK_UNREACHABLE);
403 assert(truenext != NULL);
404 assert(trueprev == prev);
405 assert((gc->_gc_prev & PREV_MASK_COLLECTING) == prev_value);
406 assert((gc->_gc_next & NEXT_MASK_UNREACHABLE) == next_value);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900407 prev = gc;
Tim Peters95bfc8a2019-10-13 16:47:04 -0500408 gc = truenext;
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900409 }
410 assert(prev == GC_PREV(head));
411}
412#else
Tim Petersea55c512019-10-18 20:59:14 -0500413#define validate_list(x, y) do{}while(0)
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900414#endif
415
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000416/*** end of list stuff ***/
417
418
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900419/* Set all gc_refs = ob_refcnt. After this, gc_refs is > 0 and
420 * PREV_MASK_COLLECTING bit is set for all objects in containers.
Tim Peters88396172002-06-30 17:56:40 +0000421 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000422static void
423update_refs(PyGC_Head *containers)
424{
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900425 PyGC_Head *gc = GC_NEXT(containers);
426 for (; gc != containers; gc = GC_NEXT(gc)) {
427 gc_reset_refs(gc, Py_REFCNT(FROM_GC(gc)));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 /* Python's cyclic gc should never see an incoming refcount
429 * of 0: if something decref'ed to 0, it should have been
430 * deallocated immediately at that time.
431 * Possible cause (if the assert triggers): a tp_dealloc
432 * routine left a gc-aware object tracked during its teardown
433 * phase, and did something-- or allowed something to happen --
434 * that called back into Python. gc can trigger then, and may
435 * see the still-tracked dying object. Before this assert
436 * was added, such mistakes went on to allow gc to try to
437 * delete the object again. In a debug build, that caused
438 * a mysterious segfault, when _Py_ForgetReference tried
439 * to remove the object from the doubly-linked list of all
440 * objects a second time. In a release build, an actual
441 * double deallocation occurred, which leads to corruption
442 * of the allocator's internal bookkeeping pointers. That's
443 * so serious that maybe this should be a release-build
444 * check instead of an assert?
445 */
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200446 _PyObject_ASSERT(FROM_GC(gc), gc_get_refs(gc) != 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000448}
449
Tim Peters19b74c72002-07-01 03:52:19 +0000450/* A traversal callback for subtract_refs. */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000451static int
Victor Stinner4d5f94b2019-10-08 02:37:38 +0200452visit_decref(PyObject *op, void *parent)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000453{
Victor Stinner4d5f94b2019-10-08 02:37:38 +0200454 _PyObject_ASSERT(_PyObject_CAST(parent), !_PyObject_IsFreed(op));
Victor Stinnerd91d4de2019-09-09 17:44:59 +0200455
Hai Shi675d9a32020-04-15 02:11:20 +0800456 if (_PyObject_IS_GC(op)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 PyGC_Head *gc = AS_GC(op);
458 /* We're only interested in gc_refs for objects in the
459 * generation being collected, which can be recognized
460 * because only they have positive gc_refs.
461 */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900462 if (gc_is_collecting(gc)) {
463 gc_decref(gc);
464 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465 }
466 return 0;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000467}
468
Tim Peters19b74c72002-07-01 03:52:19 +0000469/* Subtract internal references from gc_refs. After this, gc_refs is >= 0
470 * for all objects in containers, and is GC_REACHABLE for all tracked gc
471 * objects not in containers. The ones with gc_refs > 0 are directly
472 * reachable from outside containers, and so can't be collected.
473 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000474static void
475subtract_refs(PyGC_Head *containers)
476{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000477 traverseproc traverse;
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900478 PyGC_Head *gc = GC_NEXT(containers);
479 for (; gc != containers; gc = GC_NEXT(gc)) {
Victor Stinner4d5f94b2019-10-08 02:37:38 +0200480 PyObject *op = FROM_GC(gc);
481 traverse = Py_TYPE(op)->tp_traverse;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 (void) traverse(FROM_GC(gc),
483 (visitproc)visit_decref,
Victor Stinner4d5f94b2019-10-08 02:37:38 +0200484 op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000486}
487
Tim Peters19b74c72002-07-01 03:52:19 +0000488/* A traversal callback for move_unreachable. */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000489static int
Tim Peters19b74c72002-07-01 03:52:19 +0000490visit_reachable(PyObject *op, PyGC_Head *reachable)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000491{
Hai Shi675d9a32020-04-15 02:11:20 +0800492 if (!_PyObject_IS_GC(op)) {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900493 return 0;
494 }
Tim Peters19b74c72002-07-01 03:52:19 +0000495
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900496 PyGC_Head *gc = AS_GC(op);
497 const Py_ssize_t gc_refs = gc_get_refs(gc);
498
Tim Peters1e739452019-10-21 11:21:35 -0500499 // Ignore objects in other generation.
Tim Peters95bfc8a2019-10-13 16:47:04 -0500500 // This also skips objects "to the left" of the current position in
501 // move_unreachable's scan of the 'young' list - they've already been
502 // traversed, and no longer have the PREV_MASK_COLLECTING flag.
Tim Peters1e739452019-10-21 11:21:35 -0500503 if (! gc_is_collecting(gc)) {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900504 return 0;
505 }
Tim Peters1e739452019-10-21 11:21:35 -0500506 // It would be a logic error elsewhere if the collecting flag were set on
507 // an untracked object.
508 assert(gc->_gc_next != 0);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900509
510 if (gc->_gc_next & NEXT_MASK_UNREACHABLE) {
511 /* This had gc_refs = 0 when move_unreachable got
512 * to it, but turns out it's reachable after all.
513 * Move it back to move_unreachable's 'young' list,
514 * and move_unreachable will eventually get to it
515 * again.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000516 */
Tim Peters95bfc8a2019-10-13 16:47:04 -0500517 // Manually unlink gc from unreachable list because the list functions
518 // don't work right in the presence of NEXT_MASK_UNREACHABLE flags.
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900519 PyGC_Head *prev = GC_PREV(gc);
520 PyGC_Head *next = (PyGC_Head*)(gc->_gc_next & ~NEXT_MASK_UNREACHABLE);
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200521 _PyObject_ASSERT(FROM_GC(prev),
522 prev->_gc_next & NEXT_MASK_UNREACHABLE);
523 _PyObject_ASSERT(FROM_GC(next),
524 next->_gc_next & NEXT_MASK_UNREACHABLE);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900525 prev->_gc_next = gc->_gc_next; // copy NEXT_MASK_UNREACHABLE
526 _PyGCHead_SET_PREV(next, prev);
527
528 gc_list_append(gc, reachable);
529 gc_set_refs(gc, 1);
530 }
531 else if (gc_refs == 0) {
532 /* This is in move_unreachable's 'young' list, but
533 * the traversal hasn't yet gotten to it. All
534 * we need to do is tell move_unreachable that it's
535 * reachable.
536 */
537 gc_set_refs(gc, 1);
538 }
539 /* Else there's nothing to do.
540 * If gc_refs > 0, it must be in move_unreachable's 'young'
541 * list, and move_unreachable will eventually get to it.
542 */
543 else {
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200544 _PyObject_ASSERT_WITH_MSG(op, gc_refs > 0, "refcount is too small");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 }
546 return 0;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000547}
548
Tim Peters19b74c72002-07-01 03:52:19 +0000549/* Move the unreachable objects from young to unreachable. After this,
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900550 * all objects in young don't have PREV_MASK_COLLECTING flag and
551 * unreachable have the flag.
Tim Peters19b74c72002-07-01 03:52:19 +0000552 * All objects in young after this are directly or indirectly reachable
553 * from outside the original young; and all objects in unreachable are
554 * not.
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900555 *
556 * This function restores _gc_prev pointer. young and unreachable are
557 * doubly linked list after this function.
558 * But _gc_next in unreachable list has NEXT_MASK_UNREACHABLE flag.
559 * So we can not gc_list_* functions for unreachable until we remove the flag.
Tim Peters88396172002-06-30 17:56:40 +0000560 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000561static void
Tim Peters19b74c72002-07-01 03:52:19 +0000562move_unreachable(PyGC_Head *young, PyGC_Head *unreachable)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000563{
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900564 // previous elem in the young list, used for restore gc_prev.
565 PyGC_Head *prev = young;
566 PyGC_Head *gc = GC_NEXT(young);
Tim Peters19b74c72002-07-01 03:52:19 +0000567
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900568 /* Invariants: all objects "to the left" of us in young are reachable
569 * (directly or indirectly) from outside the young list as it was at entry.
570 *
571 * All other objects from the original young "to the left" of us are in
572 * unreachable now, and have NEXT_MASK_UNREACHABLE. All objects to the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 * left of us in 'young' now have been scanned, and no objects here
574 * or to the right have been scanned yet.
575 */
Tim Peters19b74c72002-07-01 03:52:19 +0000576
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577 while (gc != young) {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900578 if (gc_get_refs(gc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 /* gc is definitely reachable from outside the
580 * original 'young'. Mark it as such, and traverse
581 * its pointers to find any other objects that may
582 * be directly reachable from it. Note that the
583 * call to tp_traverse may append objects to young,
584 * so we have to wait until it returns to determine
585 * the next object to visit.
586 */
587 PyObject *op = FROM_GC(gc);
588 traverseproc traverse = Py_TYPE(op)->tp_traverse;
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200589 _PyObject_ASSERT_WITH_MSG(op, gc_get_refs(gc) > 0,
590 "refcount is too small");
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900591 // NOTE: visit_reachable may change gc->_gc_next when
592 // young->_gc_prev == gc. Don't do gc = GC_NEXT(gc) before!
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 (void) traverse(op,
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900594 (visitproc)visit_reachable,
595 (void *)young);
596 // relink gc_prev to prev element.
597 _PyGCHead_SET_PREV(gc, prev);
Quan Tian3bd0d622018-10-20 05:30:03 +0800598 // gc is not COLLECTING state after here.
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900599 gc_clear_collecting(gc);
600 prev = gc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000601 }
602 else {
603 /* This *may* be unreachable. To make progress,
604 * assume it is. gc isn't directly reachable from
605 * any object we've already traversed, but may be
606 * reachable from an object we haven't gotten to yet.
607 * visit_reachable will eventually move gc back into
608 * young if that's so, and we'll see it again.
609 */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900610 // Move gc to unreachable.
611 // No need to gc->next->prev = prev because it is single linked.
612 prev->_gc_next = gc->_gc_next;
613
614 // We can't use gc_list_append() here because we use
615 // NEXT_MASK_UNREACHABLE here.
616 PyGC_Head *last = GC_PREV(unreachable);
617 // NOTE: Since all objects in unreachable set has
618 // NEXT_MASK_UNREACHABLE flag, we set it unconditionally.
Tim Peters95bfc8a2019-10-13 16:47:04 -0500619 // But this may pollute the unreachable list head's 'next' pointer
620 // too. That's semantically senseless but expedient here - the
Pablo Galindo97f12672020-01-13 12:25:05 +0000621 // damage is repaired when this function ends.
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900622 last->_gc_next = (NEXT_MASK_UNREACHABLE | (uintptr_t)gc);
623 _PyGCHead_SET_PREV(gc, last);
624 gc->_gc_next = (NEXT_MASK_UNREACHABLE | (uintptr_t)unreachable);
625 unreachable->_gc_prev = (uintptr_t)gc;
626 }
627 gc = (PyGC_Head*)prev->_gc_next;
628 }
629 // young->_gc_prev must be last element remained in the list.
630 young->_gc_prev = (uintptr_t)prev;
Tim Peters95bfc8a2019-10-13 16:47:04 -0500631 // don't let the pollution of the list head's next pointer leak
632 unreachable->_gc_next &= ~NEXT_MASK_UNREACHABLE;
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900633}
634
635static void
636untrack_tuples(PyGC_Head *head)
637{
638 PyGC_Head *next, *gc = GC_NEXT(head);
639 while (gc != head) {
640 PyObject *op = FROM_GC(gc);
641 next = GC_NEXT(gc);
642 if (PyTuple_CheckExact(op)) {
643 _PyTuple_MaybeUntrack(op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644 }
645 gc = next;
646 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000647}
648
Antoine Pitroue1ad3da2012-05-28 22:22:34 +0200649/* Try to untrack all currently tracked dictionaries */
650static void
651untrack_dicts(PyGC_Head *head)
652{
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900653 PyGC_Head *next, *gc = GC_NEXT(head);
Antoine Pitroue1ad3da2012-05-28 22:22:34 +0200654 while (gc != head) {
655 PyObject *op = FROM_GC(gc);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900656 next = GC_NEXT(gc);
657 if (PyDict_CheckExact(op)) {
Antoine Pitroue1ad3da2012-05-28 22:22:34 +0200658 _PyDict_MaybeUntrack(op);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900659 }
Antoine Pitroue1ad3da2012-05-28 22:22:34 +0200660 gc = next;
661 }
662}
663
Antoine Pitrou796564c2013-07-30 19:59:21 +0200664/* Return true if object has a pre-PEP 442 finalization method. */
Neil Schemenauera765c122001-11-01 17:35:23 +0000665static int
Antoine Pitrou796564c2013-07-30 19:59:21 +0200666has_legacy_finalizer(PyObject *op)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000667{
Victor Stinnerdaa97562020-02-07 03:37:06 +0100668 return Py_TYPE(op)->tp_del != NULL;
Neil Schemenauera765c122001-11-01 17:35:23 +0000669}
670
Antoine Pitrou796564c2013-07-30 19:59:21 +0200671/* Move the objects in unreachable with tp_del slots into `finalizers`.
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900672 *
673 * This function also removes NEXT_MASK_UNREACHABLE flag
674 * from _gc_next in unreachable.
Jeremy Hyltonce136e92003-04-04 19:59:06 +0000675 */
Neil Schemenauera765c122001-11-01 17:35:23 +0000676static void
Antoine Pitrou796564c2013-07-30 19:59:21 +0200677move_legacy_finalizers(PyGC_Head *unreachable, PyGC_Head *finalizers)
Neil Schemenauera765c122001-11-01 17:35:23 +0000678{
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900679 PyGC_Head *gc, *next;
Tim Peters95bfc8a2019-10-13 16:47:04 -0500680 assert((unreachable->_gc_next & NEXT_MASK_UNREACHABLE) == 0);
Tim Petersf6b80452003-04-07 19:21:15 +0000681
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 /* March over unreachable. Move objects with finalizers into
683 * `finalizers`.
684 */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900685 for (gc = GC_NEXT(unreachable); gc != unreachable; gc = next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 PyObject *op = FROM_GC(gc);
Jeremy Hyltonce136e92003-04-04 19:59:06 +0000687
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200688 _PyObject_ASSERT(op, gc->_gc_next & NEXT_MASK_UNREACHABLE);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900689 gc->_gc_next &= ~NEXT_MASK_UNREACHABLE;
690 next = (PyGC_Head*)gc->_gc_next;
Tim Petersf6ae7a42003-04-05 18:40:50 +0000691
Antoine Pitrou796564c2013-07-30 19:59:21 +0200692 if (has_legacy_finalizer(op)) {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900693 gc_clear_collecting(gc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000694 gc_list_move(gc, finalizers);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000695 }
696 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000697}
698
Pablo Galindo466326d2019-10-13 16:48:59 +0100699static inline void
700clear_unreachable_mask(PyGC_Head *unreachable)
701{
702 /* Check that the list head does not have the unreachable bit set */
703 assert(((uintptr_t)unreachable & NEXT_MASK_UNREACHABLE) == 0);
704
705 PyGC_Head *gc, *next;
Tim Peters95bfc8a2019-10-13 16:47:04 -0500706 assert((unreachable->_gc_next & NEXT_MASK_UNREACHABLE) == 0);
Pablo Galindo466326d2019-10-13 16:48:59 +0100707 for (gc = GC_NEXT(unreachable); gc != unreachable; gc = next) {
708 _PyObject_ASSERT((PyObject*)FROM_GC(gc), gc->_gc_next & NEXT_MASK_UNREACHABLE);
709 gc->_gc_next &= ~NEXT_MASK_UNREACHABLE;
710 next = (PyGC_Head*)gc->_gc_next;
711 }
Tim Petersea55c512019-10-18 20:59:14 -0500712 validate_list(unreachable, collecting_set_unreachable_clear);
Pablo Galindo466326d2019-10-13 16:48:59 +0100713}
714
Antoine Pitrou796564c2013-07-30 19:59:21 +0200715/* A traversal callback for move_legacy_finalizer_reachable. */
Tim Peters19b74c72002-07-01 03:52:19 +0000716static int
717visit_move(PyObject *op, PyGC_Head *tolist)
718{
Hai Shi675d9a32020-04-15 02:11:20 +0800719 if (_PyObject_IS_GC(op)) {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900720 PyGC_Head *gc = AS_GC(op);
721 if (gc_is_collecting(gc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722 gc_list_move(gc, tolist);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900723 gc_clear_collecting(gc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 }
725 }
726 return 0;
Tim Peters19b74c72002-07-01 03:52:19 +0000727}
728
729/* Move objects that are reachable from finalizers, from the unreachable set
Tim Petersf6b80452003-04-07 19:21:15 +0000730 * into finalizers set.
Tim Peters19b74c72002-07-01 03:52:19 +0000731 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000732static void
Antoine Pitrou796564c2013-07-30 19:59:21 +0200733move_legacy_finalizer_reachable(PyGC_Head *finalizers)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000734{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 traverseproc traverse;
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900736 PyGC_Head *gc = GC_NEXT(finalizers);
737 for (; gc != finalizers; gc = GC_NEXT(gc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000738 /* Note that the finalizers list may grow during this. */
739 traverse = Py_TYPE(FROM_GC(gc))->tp_traverse;
740 (void) traverse(FROM_GC(gc),
741 (visitproc)visit_move,
742 (void *)finalizers);
743 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000744}
745
Tim Petersead8b7a2004-10-30 23:09:22 +0000746/* Clear all weakrefs to unreachable objects, and if such a weakref has a
747 * callback, invoke it if necessary. Note that it's possible for such
748 * weakrefs to be outside the unreachable set -- indeed, those are precisely
749 * the weakrefs whose callbacks must be invoked. See gc_weakref.txt for
750 * overview & some details. Some weakrefs with callbacks may be reclaimed
751 * directly by this routine; the number reclaimed is the return value. Other
752 * weakrefs with callbacks may be moved into the `old` generation. Objects
753 * moved into `old` have gc_refs set to GC_REACHABLE; the objects remaining in
754 * unreachable are left at GC_TENTATIVELY_UNREACHABLE. When this returns,
755 * no object in `unreachable` is weakly referenced anymore.
Tim Peters403a2032003-11-20 21:21:46 +0000756 */
757static int
Tim Petersead8b7a2004-10-30 23:09:22 +0000758handle_weakrefs(PyGC_Head *unreachable, PyGC_Head *old)
Tim Peters403a2032003-11-20 21:21:46 +0000759{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000760 PyGC_Head *gc;
761 PyObject *op; /* generally FROM_GC(gc) */
762 PyWeakReference *wr; /* generally a cast of op */
763 PyGC_Head wrcb_to_call; /* weakrefs with callbacks to call */
764 PyGC_Head *next;
765 int num_freed = 0;
Tim Peters403a2032003-11-20 21:21:46 +0000766
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 gc_list_init(&wrcb_to_call);
Tim Peters403a2032003-11-20 21:21:46 +0000768
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 /* Clear all weakrefs to the objects in unreachable. If such a weakref
770 * also has a callback, move it into `wrcb_to_call` if the callback
771 * needs to be invoked. Note that we cannot invoke any callbacks until
772 * all weakrefs to unreachable objects are cleared, lest the callback
773 * resurrect an unreachable object via a still-active weakref. We
774 * make another pass over wrcb_to_call, invoking callbacks, after this
775 * pass completes.
776 */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900777 for (gc = GC_NEXT(unreachable); gc != unreachable; gc = next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000778 PyWeakReference **wrlist;
Tim Petersead8b7a2004-10-30 23:09:22 +0000779
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 op = FROM_GC(gc);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900781 next = GC_NEXT(gc);
Tim Petersead8b7a2004-10-30 23:09:22 +0000782
Neil Schemenauerbcda4602019-09-30 10:06:45 -0700783 if (PyWeakref_Check(op)) {
784 /* A weakref inside the unreachable set must be cleared. If we
785 * allow its callback to execute inside delete_garbage(), it
786 * could expose objects that have tp_clear already called on
787 * them. Or, it could resurrect unreachable objects. One way
788 * this can happen is if some container objects do not implement
789 * tp_traverse. Then, wr_object can be outside the unreachable
790 * set but can be deallocated as a result of breaking the
791 * reference cycle. If we don't clear the weakref, the callback
792 * will run and potentially cause a crash. See bpo-38006 for
793 * one example.
794 */
795 _PyWeakref_ClearRef((PyWeakReference *)op);
796 }
797
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798 if (! PyType_SUPPORTS_WEAKREFS(Py_TYPE(op)))
799 continue;
Tim Petersead8b7a2004-10-30 23:09:22 +0000800
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 /* It supports weakrefs. Does it have any? */
802 wrlist = (PyWeakReference **)
Victor Stinner38aefc52020-04-06 14:07:02 +0200803 _PyObject_GET_WEAKREFS_LISTPTR(op);
Tim Petersead8b7a2004-10-30 23:09:22 +0000804
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000805 /* `op` may have some weakrefs. March over the list, clear
806 * all the weakrefs, and move the weakrefs with callbacks
807 * that must be called into wrcb_to_call.
808 */
809 for (wr = *wrlist; wr != NULL; wr = *wrlist) {
810 PyGC_Head *wrasgc; /* AS_GC(wr) */
Tim Petersead8b7a2004-10-30 23:09:22 +0000811
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812 /* _PyWeakref_ClearRef clears the weakref but leaves
813 * the callback pointer intact. Obscure: it also
814 * changes *wrlist.
815 */
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200816 _PyObject_ASSERT((PyObject *)wr, wr->wr_object == op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 _PyWeakref_ClearRef(wr);
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200818 _PyObject_ASSERT((PyObject *)wr, wr->wr_object == Py_None);
819 if (wr->wr_callback == NULL) {
820 /* no callback */
821 continue;
822 }
Tim Petersead8b7a2004-10-30 23:09:22 +0000823
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200824 /* Headache time. `op` is going away, and is weakly referenced by
825 * `wr`, which has a callback. Should the callback be invoked? If wr
826 * is also trash, no:
827 *
828 * 1. There's no need to call it. The object and the weakref are
829 * both going away, so it's legitimate to pretend the weakref is
830 * going away first. The user has to ensure a weakref outlives its
831 * referent if they want a guarantee that the wr callback will get
832 * invoked.
833 *
834 * 2. It may be catastrophic to call it. If the callback is also in
835 * cyclic trash (CT), then although the CT is unreachable from
836 * outside the current generation, CT may be reachable from the
837 * callback. Then the callback could resurrect insane objects.
838 *
839 * Since the callback is never needed and may be unsafe in this case,
840 * wr is simply left in the unreachable set. Note that because we
841 * already called _PyWeakref_ClearRef(wr), its callback will never
842 * trigger.
843 *
844 * OTOH, if wr isn't part of CT, we should invoke the callback: the
845 * weakref outlived the trash. Note that since wr isn't CT in this
846 * case, its callback can't be CT either -- wr acted as an external
847 * root to this generation, and therefore its callback did too. So
848 * nothing in CT is reachable from the callback either, so it's hard
849 * to imagine how calling it later could create a problem for us. wr
850 * is moved to wrcb_to_call in this case.
851 */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900852 if (gc_is_collecting(AS_GC(wr))) {
Neil Schemenauerbcda4602019-09-30 10:06:45 -0700853 /* it should already have been cleared above */
854 assert(wr->wr_object == Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 continue;
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900856 }
Tim Peterscc2a8662004-10-31 22:12:43 +0000857
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858 /* Create a new reference so that wr can't go away
859 * before we can process it again.
860 */
861 Py_INCREF(wr);
Tim Petersead8b7a2004-10-30 23:09:22 +0000862
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000863 /* Move wr to wrcb_to_call, for the next pass. */
864 wrasgc = AS_GC(wr);
865 assert(wrasgc != next); /* wrasgc is reachable, but
866 next isn't, so they can't
867 be the same */
868 gc_list_move(wrasgc, &wrcb_to_call);
869 }
870 }
Tim Petersead8b7a2004-10-30 23:09:22 +0000871
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000872 /* Invoke the callbacks we decided to honor. It's safe to invoke them
873 * because they can't reference unreachable objects.
874 */
875 while (! gc_list_is_empty(&wrcb_to_call)) {
876 PyObject *temp;
877 PyObject *callback;
Tim Petersead8b7a2004-10-30 23:09:22 +0000878
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900879 gc = (PyGC_Head*)wrcb_to_call._gc_next;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000880 op = FROM_GC(gc);
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200881 _PyObject_ASSERT(op, PyWeakref_Check(op));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 wr = (PyWeakReference *)op;
883 callback = wr->wr_callback;
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200884 _PyObject_ASSERT(op, callback != NULL);
Tim Petersead8b7a2004-10-30 23:09:22 +0000885
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 /* copy-paste of weakrefobject.c's handle_callback() */
Petr Viktorinffd97532020-02-11 17:46:57 +0100887 temp = PyObject_CallOneArg(callback, (PyObject *)wr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 if (temp == NULL)
889 PyErr_WriteUnraisable(callback);
890 else
891 Py_DECREF(temp);
Tim Petersead8b7a2004-10-30 23:09:22 +0000892
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 /* Give up the reference we created in the first pass. When
894 * op's refcount hits 0 (which it may or may not do right now),
895 * op's tp_dealloc will decref op->wr_callback too. Note
896 * that the refcount probably will hit 0 now, and because this
897 * weakref was reachable to begin with, gc didn't already
898 * add it to its count of freed objects. Example: a reachable
899 * weak value dict maps some key to this reachable weakref.
900 * The callback removes this key->weakref mapping from the
901 * dict, leaving no other references to the weakref (excepting
902 * ours).
903 */
904 Py_DECREF(op);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900905 if (wrcb_to_call._gc_next == (uintptr_t)gc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 /* object is still alive -- move it */
907 gc_list_move(gc, old);
908 }
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900909 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 ++num_freed;
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900911 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 }
Tim Petersead8b7a2004-10-30 23:09:22 +0000913
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 return num_freed;
Tim Peters403a2032003-11-20 21:21:46 +0000915}
916
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000917static void
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200918debug_cycle(const char *msg, PyObject *op)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000919{
Victor Stinner499dfcf2011-03-21 13:26:24 +0100920 PySys_FormatStderr("gc: %s <%s %p>\n",
921 msg, Py_TYPE(op)->tp_name, op);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000922}
923
Antoine Pitrou796564c2013-07-30 19:59:21 +0200924/* Handle uncollectable garbage (cycles with tp_del slots, and stuff reachable
Tim Petersbf384c22003-04-06 00:11:39 +0000925 * only from such cycles).
Tim Petersf6b80452003-04-07 19:21:15 +0000926 * If DEBUG_SAVEALL, all objects in finalizers are appended to the module
927 * garbage list (a Python list), else only the objects in finalizers with
928 * __del__ methods are appended to garbage. All objects in finalizers are
929 * merged into the old list regardless.
Tim Petersbf384c22003-04-06 00:11:39 +0000930 */
Serhiy Storchaka301e3cc2018-05-24 15:19:29 +0300931static void
Victor Stinner2e969062019-11-20 01:49:32 +0100932handle_legacy_finalizers(PyThreadState *tstate,
Victor Stinner67e0de62019-11-20 11:48:18 +0100933 GCState *gcstate,
Victor Stinner9db03242019-04-26 02:32:01 +0200934 PyGC_Head *finalizers, PyGC_Head *old)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000935{
Victor Stinner2e969062019-11-20 01:49:32 +0100936 assert(!_PyErr_Occurred(tstate));
Victor Stinner67e0de62019-11-20 11:48:18 +0100937 assert(gcstate->garbage != NULL);
Victor Stinner9db03242019-04-26 02:32:01 +0200938
939 PyGC_Head *gc = GC_NEXT(finalizers);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900940 for (; gc != finalizers; gc = GC_NEXT(gc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000941 PyObject *op = FROM_GC(gc);
Tim Petersf6b80452003-04-07 19:21:15 +0000942
Victor Stinner67e0de62019-11-20 11:48:18 +0100943 if ((gcstate->debug & DEBUG_SAVEALL) || has_legacy_finalizer(op)) {
944 if (PyList_Append(gcstate->garbage, op) < 0) {
Victor Stinner2e969062019-11-20 01:49:32 +0100945 _PyErr_Clear(tstate);
Serhiy Storchaka301e3cc2018-05-24 15:19:29 +0300946 break;
Serhiy Storchakac4653c92018-05-29 18:50:10 +0300947 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948 }
949 }
Tim Petersf6b80452003-04-07 19:21:15 +0000950
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 gc_list_merge(finalizers, old);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000952}
953
Tim Peters5fbc7b12014-05-08 17:42:19 -0500954/* Run first-time finalizers (if any) on all the objects in collectable.
955 * Note that this may remove some (or even all) of the objects from the
956 * list, due to refcounts falling to 0.
957 */
Antoine Pitrou796564c2013-07-30 19:59:21 +0200958static void
Victor Stinner67e0de62019-11-20 11:48:18 +0100959finalize_garbage(PyThreadState *tstate, PyGC_Head *collectable)
Antoine Pitrou796564c2013-07-30 19:59:21 +0200960{
961 destructor finalize;
Tim Peters5fbc7b12014-05-08 17:42:19 -0500962 PyGC_Head seen;
Antoine Pitrou796564c2013-07-30 19:59:21 +0200963
Tim Peters5fbc7b12014-05-08 17:42:19 -0500964 /* While we're going through the loop, `finalize(op)` may cause op, or
965 * other objects, to be reclaimed via refcounts falling to zero. So
966 * there's little we can rely on about the structure of the input
967 * `collectable` list across iterations. For safety, we always take the
968 * first object in that list and move it to a temporary `seen` list.
969 * If objects vanish from the `collectable` and `seen` lists we don't
970 * care.
971 */
972 gc_list_init(&seen);
973
974 while (!gc_list_is_empty(collectable)) {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900975 PyGC_Head *gc = GC_NEXT(collectable);
Antoine Pitrou796564c2013-07-30 19:59:21 +0200976 PyObject *op = FROM_GC(gc);
Tim Peters5fbc7b12014-05-08 17:42:19 -0500977 gc_list_move(gc, &seen);
Antoine Pitrou796564c2013-07-30 19:59:21 +0200978 if (!_PyGCHead_FINALIZED(gc) &&
Tim Peters5fbc7b12014-05-08 17:42:19 -0500979 (finalize = Py_TYPE(op)->tp_finalize) != NULL) {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900980 _PyGCHead_SET_FINALIZED(gc);
Antoine Pitrou796564c2013-07-30 19:59:21 +0200981 Py_INCREF(op);
982 finalize(op);
Victor Stinner67e0de62019-11-20 11:48:18 +0100983 assert(!_PyErr_Occurred(tstate));
Antoine Pitrou796564c2013-07-30 19:59:21 +0200984 Py_DECREF(op);
985 }
986 }
Tim Peters5fbc7b12014-05-08 17:42:19 -0500987 gc_list_merge(&seen, collectable);
Antoine Pitrou796564c2013-07-30 19:59:21 +0200988}
989
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990/* Break reference cycles by clearing the containers involved. This is
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000991 * tricky business as the lists can be changing and we don't know which
Tim Peters19b74c72002-07-01 03:52:19 +0000992 * objects may be freed. It is possible I screwed something up here.
993 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000994static void
Victor Stinner67e0de62019-11-20 11:48:18 +0100995delete_garbage(PyThreadState *tstate, GCState *gcstate,
Victor Stinner9db03242019-04-26 02:32:01 +0200996 PyGC_Head *collectable, PyGC_Head *old)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000997{
Victor Stinner2e969062019-11-20 01:49:32 +0100998 assert(!_PyErr_Occurred(tstate));
Victor Stinner9db03242019-04-26 02:32:01 +0200999
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001000 while (!gc_list_is_empty(collectable)) {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09001001 PyGC_Head *gc = GC_NEXT(collectable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 PyObject *op = FROM_GC(gc);
Tim Peters88396172002-06-30 17:56:40 +00001003
Victor Stinnera4b2bc72018-10-26 18:00:13 +02001004 _PyObject_ASSERT_WITH_MSG(op, Py_REFCNT(op) > 0,
1005 "refcount is too small");
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09001006
Victor Stinner67e0de62019-11-20 11:48:18 +01001007 if (gcstate->debug & DEBUG_SAVEALL) {
1008 assert(gcstate->garbage != NULL);
1009 if (PyList_Append(gcstate->garbage, op) < 0) {
Victor Stinner2e969062019-11-20 01:49:32 +01001010 _PyErr_Clear(tstate);
Serhiy Storchakac4653c92018-05-29 18:50:10 +03001011 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 }
1013 else {
Victor Stinner9db03242019-04-26 02:32:01 +02001014 inquiry clear;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 if ((clear = Py_TYPE(op)->tp_clear) != NULL) {
1016 Py_INCREF(op);
Serhiy Storchakac4653c92018-05-29 18:50:10 +03001017 (void) clear(op);
Victor Stinner2e969062019-11-20 01:49:32 +01001018 if (_PyErr_Occurred(tstate)) {
Victor Stinner71c52e32019-05-27 08:57:14 +02001019 _PyErr_WriteUnraisableMsg("in tp_clear of",
1020 (PyObject*)Py_TYPE(op));
Serhiy Storchakac4653c92018-05-29 18:50:10 +03001021 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 Py_DECREF(op);
1023 }
1024 }
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09001025 if (GC_NEXT(collectable) == gc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 /* object is still alive, move it, it may die later */
Pablo Galindo466326d2019-10-13 16:48:59 +01001027 gc_clear_collecting(gc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 gc_list_move(gc, old);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 }
1030 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001031}
1032
Christian Heimesa156e092008-02-16 07:38:31 +00001033/* Clear all free lists
1034 * All free lists are cleared during the collection of the highest generation.
1035 * Allocated items in the free list may keep a pymalloc arena occupied.
1036 * Clearing the free lists may give back memory to the OS earlier.
1037 */
1038static void
Victor Stinnere005ead2020-06-05 02:56:37 +02001039clear_freelists(PyThreadState *tstate)
Christian Heimesa156e092008-02-16 07:38:31 +00001040{
Victor Stinner3744ed22020-06-05 01:39:24 +02001041 _PyFrame_ClearFreeList(tstate);
Victor Stinner69ac6e52020-06-04 23:38:36 +02001042 _PyTuple_ClearFreeList(tstate);
Victor Stinner2ba59372020-06-05 00:50:05 +02001043 _PyFloat_ClearFreeList(tstate);
Victor Stinner88ec9192020-06-05 02:05:41 +02001044 _PyList_ClearFreeList(tstate);
Victor Stinnerb4e85ca2020-06-23 11:33:18 +02001045 _PyDict_ClearFreeList(tstate);
Victor Stinner78a02c22020-06-05 02:34:14 +02001046 _PyAsyncGen_ClearFreeLists(tstate);
Victor Stinnere005ead2020-06-05 02:56:37 +02001047 _PyContext_ClearFreeList(tstate);
Christian Heimesa156e092008-02-16 07:38:31 +00001048}
1049
Pablo Galindo97f12672020-01-13 12:25:05 +00001050// Show stats for objects in each generations
Inada Naokibf8162c2019-08-02 16:25:29 +09001051static void
Victor Stinner67e0de62019-11-20 11:48:18 +01001052show_stats_each_generations(GCState *gcstate)
Inada Naokibf8162c2019-08-02 16:25:29 +09001053{
1054 char buf[100];
1055 size_t pos = 0;
1056
1057 for (int i = 0; i < NUM_GENERATIONS && pos < sizeof(buf); i++) {
1058 pos += PyOS_snprintf(buf+pos, sizeof(buf)-pos,
Victor Stinnerd36cf5f2020-06-10 18:38:05 +02001059 " %zd",
Victor Stinner67e0de62019-11-20 11:48:18 +01001060 gc_list_size(GEN_HEAD(gcstate, i)));
Inada Naokibf8162c2019-08-02 16:25:29 +09001061 }
1062
1063 PySys_FormatStderr(
1064 "gc: objects in each generation:%s\n"
1065 "gc: objects in permanent generation: %zd\n",
Victor Stinner67e0de62019-11-20 11:48:18 +01001066 buf, gc_list_size(&gcstate->permanent_generation.head));
Inada Naokibf8162c2019-08-02 16:25:29 +09001067}
1068
Pablo Galindo97f12672020-01-13 12:25:05 +00001069/* Deduce which objects among "base" are unreachable from outside the list
Pablo Galindo466326d2019-10-13 16:48:59 +01001070 and move them to 'unreachable'. The process consist in the following steps:
1071
10721. Copy all reference counts to a different field (gc_prev is used to hold
1073 this copy to save memory).
10742. Traverse all objects in "base" and visit all referred objects using
Pablo Galindo97f12672020-01-13 12:25:05 +00001075 "tp_traverse" and for every visited object, subtract 1 to the reference
Pablo Galindo466326d2019-10-13 16:48:59 +01001076 count (the one that we copied in the previous step). After this step, all
1077 objects that can be reached directly from outside must have strictly positive
1078 reference count, while all unreachable objects must have a count of exactly 0.
Pablo Galindo97f12672020-01-13 12:25:05 +000010793. Identify all unreachable objects (the ones with 0 reference count) and move
Pablo Galindo466326d2019-10-13 16:48:59 +01001080 them to the "unreachable" list. This step also needs to move back to "base" all
1081 objects that were initially marked as unreachable but are referred transitively
1082 by the reachable objects (the ones with strictly positive reference count).
1083
1084Contracts:
1085
1086 * The "base" has to be a valid list with no mask set.
1087
1088 * The "unreachable" list must be uninitialized (this function calls
1089 gc_list_init over 'unreachable').
Tim Peters95bfc8a2019-10-13 16:47:04 -05001090
Pablo Galindo466326d2019-10-13 16:48:59 +01001091IMPORTANT: This function leaves 'unreachable' with the NEXT_MASK_UNREACHABLE
1092flag set but it does not clear it to skip unnecessary iteration. Before the
1093flag is cleared (for example, by using 'clear_unreachable_mask' function or
1094by a call to 'move_legacy_finalizers'), the 'unreachable' list is not a normal
1095list and we can not use most gc_list_* functions for it. */
1096static inline void
1097deduce_unreachable(PyGC_Head *base, PyGC_Head *unreachable) {
Tim Petersea55c512019-10-18 20:59:14 -05001098 validate_list(base, collecting_clear_unreachable_clear);
Pablo Galindo466326d2019-10-13 16:48:59 +01001099 /* Using ob_refcnt and gc_refs, calculate which objects in the
1100 * container set are reachable from outside the set (i.e., have a
1101 * refcount greater than 0 when all the references within the
1102 * set are taken into account).
1103 */
1104 update_refs(base); // gc_prev is used for gc_refs
1105 subtract_refs(base);
1106
1107 /* Leave everything reachable from outside base in base, and move
1108 * everything else (in base) to unreachable.
Pablo Galindo97f12672020-01-13 12:25:05 +00001109 *
Pablo Galindo466326d2019-10-13 16:48:59 +01001110 * NOTE: This used to move the reachable objects into a reachable
1111 * set instead. But most things usually turn out to be reachable,
Pablo Galindo97f12672020-01-13 12:25:05 +00001112 * so it's more efficient to move the unreachable things. It "sounds slick"
1113 * to move the unreachable objects, until you think about it - the reason it
1114 * pays isn't actually obvious.
1115 *
1116 * Suppose we create objects A, B, C in that order. They appear in the young
1117 * generation in the same order. If B points to A, and C to B, and C is
1118 * reachable from outside, then the adjusted refcounts will be 0, 0, and 1
1119 * respectively.
1120 *
1121 * When move_unreachable finds A, A is moved to the unreachable list. The
1122 * same for B when it's first encountered. Then C is traversed, B is moved
1123 * _back_ to the reachable list. B is eventually traversed, and then A is
1124 * moved back to the reachable list.
1125 *
1126 * So instead of not moving at all, the reachable objects B and A are moved
1127 * twice each. Why is this a win? A straightforward algorithm to move the
1128 * reachable objects instead would move A, B, and C once each.
1129 *
1130 * The key is that this dance leaves the objects in order C, B, A - it's
1131 * reversed from the original order. On all _subsequent_ scans, none of
1132 * them will move. Since most objects aren't in cycles, this can save an
1133 * unbounded number of moves across an unbounded number of later collections.
1134 * It can cost more only the first time the chain is scanned.
1135 *
1136 * Drawback: move_unreachable is also used to find out what's still trash
1137 * after finalizers may resurrect objects. In _that_ case most unreachable
1138 * objects will remain unreachable, so it would be more efficient to move
1139 * the reachable objects instead. But this is a one-time cost, probably not
1140 * worth complicating the code to speed just a little.
Pablo Galindo466326d2019-10-13 16:48:59 +01001141 */
1142 gc_list_init(unreachable);
1143 move_unreachable(base, unreachable); // gc_prev is pointer again
Tim Petersea55c512019-10-18 20:59:14 -05001144 validate_list(base, collecting_clear_unreachable_clear);
1145 validate_list(unreachable, collecting_set_unreachable_set);
Pablo Galindo466326d2019-10-13 16:48:59 +01001146}
1147
1148/* Handle objects that may have resurrected after a call to 'finalize_garbage', moving
1149 them to 'old_generation' and placing the rest on 'still_unreachable'.
1150
1151 Contracts:
1152 * After this function 'unreachable' must not be used anymore and 'still_unreachable'
1153 will contain the objects that did not resurrect.
1154
1155 * The "still_unreachable" list must be uninitialized (this function calls
1156 gc_list_init over 'still_unreachable').
1157
1158IMPORTANT: After a call to this function, the 'still_unreachable' set will have the
1159PREV_MARK_COLLECTING set, but the objects in this set are going to be removed so
1160we can skip the expense of clearing the flag to avoid extra iteration. */
1161static inline void
1162handle_resurrected_objects(PyGC_Head *unreachable, PyGC_Head* still_unreachable,
1163 PyGC_Head *old_generation)
1164{
1165 // Remove the PREV_MASK_COLLECTING from unreachable
1166 // to prepare it for a new call to 'deduce_unreachable'
1167 gc_list_clear_collecting(unreachable);
1168
1169 // After the call to deduce_unreachable, the 'still_unreachable' set will
1170 // have the PREV_MARK_COLLECTING set, but the objects are going to be
1171 // removed so we can skip the expense of clearing the flag.
1172 PyGC_Head* resurrected = unreachable;
1173 deduce_unreachable(resurrected, still_unreachable);
1174 clear_unreachable_mask(still_unreachable);
1175
1176 // Move the resurrected objects to the old generation for future collection.
1177 gc_list_merge(resurrected, old_generation);
1178}
1179
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001180/* This is the main function. Read this to understand how the
1181 * collection process works. */
Neal Norwitz7b216c52006-03-04 20:01:53 +00001182static Py_ssize_t
Victor Stinner8b341482020-10-30 17:00:00 +01001183gc_collect_main(PyThreadState *tstate, int generation,
1184 Py_ssize_t *n_collected, Py_ssize_t *n_uncollectable,
1185 int nofail)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001186{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 int i;
1188 Py_ssize_t m = 0; /* # objects collected */
1189 Py_ssize_t n = 0; /* # unreachable objects that couldn't be collected */
1190 PyGC_Head *young; /* the generation we are examining */
1191 PyGC_Head *old; /* next older generation */
1192 PyGC_Head unreachable; /* non-problematic unreachable trash */
1193 PyGC_Head finalizers; /* objects with, & reachable from, __del__ */
1194 PyGC_Head *gc;
Victor Stinner7181dec2015-03-27 17:47:53 +01001195 _PyTime_t t1 = 0; /* initialize to prevent a compiler warning */
Victor Stinner72474072019-11-20 12:25:50 +01001196 GCState *gcstate = &tstate->interp->gc;
Antoine Pitrou40f6b122014-05-24 19:21:53 +02001197
Victor Stinnereba5bf22020-10-30 22:51:02 +01001198 // gc_collect_main() must not be called before _PyGC_Init
1199 // or after _PyGC_Fini()
1200 assert(gcstate->garbage != NULL);
1201 assert(!_PyErr_Occurred(tstate));
1202
Victor Stinnerd8135e92020-05-06 18:25:06 +02001203#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
1204 if (tstate->interp->config._isolated_interpreter) {
1205 // bpo-40533: The garbage collector must not be run on parallel on
1206 // Python objects shared by multiple interpreters.
1207 return 0;
1208 }
1209#endif
1210
Victor Stinner67e0de62019-11-20 11:48:18 +01001211 if (gcstate->debug & DEBUG_STATS) {
Inada Naokibf8162c2019-08-02 16:25:29 +09001212 PySys_WriteStderr("gc: collecting generation %d...\n", generation);
Victor Stinner67e0de62019-11-20 11:48:18 +01001213 show_stats_each_generations(gcstate);
Victor Stinner7181dec2015-03-27 17:47:53 +01001214 t1 = _PyTime_GetMonotonicClock();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001215 }
Neil Schemenauer2880ae52002-05-04 05:35:20 +00001216
Łukasz Langaa785c872016-09-09 17:37:37 -07001217 if (PyDTrace_GC_START_ENABLED())
1218 PyDTrace_GC_START(generation);
1219
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001220 /* update collection and allocation counters */
1221 if (generation+1 < NUM_GENERATIONS)
Victor Stinner67e0de62019-11-20 11:48:18 +01001222 gcstate->generations[generation+1].count += 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001223 for (i = 0; i <= generation; i++)
Victor Stinner67e0de62019-11-20 11:48:18 +01001224 gcstate->generations[i].count = 0;
Neil Schemenauer2880ae52002-05-04 05:35:20 +00001225
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001226 /* merge younger generations with one we are currently collecting */
1227 for (i = 0; i < generation; i++) {
Victor Stinner67e0de62019-11-20 11:48:18 +01001228 gc_list_merge(GEN_HEAD(gcstate, i), GEN_HEAD(gcstate, generation));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001229 }
Neil Schemenauer2880ae52002-05-04 05:35:20 +00001230
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001231 /* handy references */
Victor Stinner67e0de62019-11-20 11:48:18 +01001232 young = GEN_HEAD(gcstate, generation);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001233 if (generation < NUM_GENERATIONS-1)
Victor Stinner67e0de62019-11-20 11:48:18 +01001234 old = GEN_HEAD(gcstate, generation+1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001235 else
1236 old = young;
Tim Petersea55c512019-10-18 20:59:14 -05001237 validate_list(old, collecting_clear_unreachable_clear);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001238
Pablo Galindo466326d2019-10-13 16:48:59 +01001239 deduce_unreachable(young, &unreachable);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001240
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09001241 untrack_tuples(young);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001242 /* Move reachable objects to next generation. */
1243 if (young != old) {
1244 if (generation == NUM_GENERATIONS - 2) {
Victor Stinner67e0de62019-11-20 11:48:18 +01001245 gcstate->long_lived_pending += gc_list_size(young);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001246 }
1247 gc_list_merge(young, old);
1248 }
1249 else {
Pablo Galindo97f12672020-01-13 12:25:05 +00001250 /* We only un-track dicts in full collections, to avoid quadratic
Antoine Pitroue1ad3da2012-05-28 22:22:34 +02001251 dict build-up. See issue #14775. */
1252 untrack_dicts(young);
Victor Stinner67e0de62019-11-20 11:48:18 +01001253 gcstate->long_lived_pending = 0;
1254 gcstate->long_lived_total = gc_list_size(young);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001256
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001257 /* All objects in unreachable are trash, but objects reachable from
Antoine Pitrou796564c2013-07-30 19:59:21 +02001258 * legacy finalizers (e.g. tp_del) can't safely be deleted.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001259 */
1260 gc_list_init(&finalizers);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09001261 // NEXT_MASK_UNREACHABLE is cleared here.
1262 // After move_legacy_finalizers(), unreachable is normal list.
Antoine Pitrou796564c2013-07-30 19:59:21 +02001263 move_legacy_finalizers(&unreachable, &finalizers);
1264 /* finalizers contains the unreachable objects with a legacy finalizer;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001265 * unreachable objects reachable *from* those are also uncollectable,
1266 * and we move those into the finalizers list too.
1267 */
Antoine Pitrou796564c2013-07-30 19:59:21 +02001268 move_legacy_finalizer_reachable(&finalizers);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001269
Tim Petersea55c512019-10-18 20:59:14 -05001270 validate_list(&finalizers, collecting_clear_unreachable_clear);
1271 validate_list(&unreachable, collecting_set_unreachable_clear);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09001272
Tim Petersecbf35f2019-10-09 12:37:30 -05001273 /* Print debugging information. */
Victor Stinner67e0de62019-11-20 11:48:18 +01001274 if (gcstate->debug & DEBUG_COLLECTABLE) {
Tim Petersecbf35f2019-10-09 12:37:30 -05001275 for (gc = GC_NEXT(&unreachable); gc != &unreachable; gc = GC_NEXT(gc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001276 debug_cycle("collectable", FROM_GC(gc));
1277 }
1278 }
Tim Petersead8b7a2004-10-30 23:09:22 +00001279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001280 /* Clear weakrefs and invoke callbacks as necessary. */
1281 m += handle_weakrefs(&unreachable, old);
Tim Petersead8b7a2004-10-30 23:09:22 +00001282
Tim Petersea55c512019-10-18 20:59:14 -05001283 validate_list(old, collecting_clear_unreachable_clear);
1284 validate_list(&unreachable, collecting_set_unreachable_clear);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09001285
Antoine Pitrou796564c2013-07-30 19:59:21 +02001286 /* Call tp_finalize on objects which have one. */
Victor Stinner67e0de62019-11-20 11:48:18 +01001287 finalize_garbage(tstate, &unreachable);
Antoine Pitrou796564c2013-07-30 19:59:21 +02001288
Pablo Galindo466326d2019-10-13 16:48:59 +01001289 /* Handle any objects that may have resurrected after the call
1290 * to 'finalize_garbage' and continue the collection with the
1291 * objects that are still unreachable */
1292 PyGC_Head final_unreachable;
1293 handle_resurrected_objects(&unreachable, &final_unreachable, old);
1294
1295 /* Call tp_clear on objects in the final_unreachable set. This will cause
1296 * the reference cycles to be broken. It may also cause some objects
1297 * in finalizers to be freed.
1298 */
1299 m += gc_list_size(&final_unreachable);
Victor Stinner67e0de62019-11-20 11:48:18 +01001300 delete_garbage(tstate, gcstate, &final_unreachable, old);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001302 /* Collect statistics on uncollectable objects found and print
1303 * debugging information. */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09001304 for (gc = GC_NEXT(&finalizers); gc != &finalizers; gc = GC_NEXT(gc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001305 n++;
Victor Stinner67e0de62019-11-20 11:48:18 +01001306 if (gcstate->debug & DEBUG_UNCOLLECTABLE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001307 debug_cycle("uncollectable", FROM_GC(gc));
1308 }
Victor Stinner67e0de62019-11-20 11:48:18 +01001309 if (gcstate->debug & DEBUG_STATS) {
Inada Naokibf8162c2019-08-02 16:25:29 +09001310 double d = _PyTime_AsSecondsDouble(_PyTime_GetMonotonicClock() - t1);
Inada Naoki013e52f2019-08-31 09:13:42 +09001311 PySys_WriteStderr(
Victor Stinnerd36cf5f2020-06-10 18:38:05 +02001312 "gc: done, %zd unreachable, %zd uncollectable, %.4fs elapsed\n",
Inada Naokibf8162c2019-08-02 16:25:29 +09001313 n+m, n, d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001314 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001315
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001316 /* Append instances in the uncollectable set to a Python
1317 * reachable list of garbage. The programmer has to deal with
1318 * this if they insist on creating this type of structure.
1319 */
Victor Stinner67e0de62019-11-20 11:48:18 +01001320 handle_legacy_finalizers(tstate, gcstate, &finalizers, old);
Tim Petersea55c512019-10-18 20:59:14 -05001321 validate_list(old, collecting_clear_unreachable_clear);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001322
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001323 /* Clear free list only during the collection of the highest
1324 * generation */
1325 if (generation == NUM_GENERATIONS-1) {
Victor Stinnere005ead2020-06-05 02:56:37 +02001326 clear_freelists(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001327 }
Christian Heimesa156e092008-02-16 07:38:31 +00001328
Victor Stinner2e969062019-11-20 01:49:32 +01001329 if (_PyErr_Occurred(tstate)) {
Antoine Pitroufef34e32013-05-19 01:11:58 +02001330 if (nofail) {
Victor Stinner2e969062019-11-20 01:49:32 +01001331 _PyErr_Clear(tstate);
Antoine Pitroufef34e32013-05-19 01:11:58 +02001332 }
1333 else {
Victor Stinner656c45e2020-01-24 18:05:24 +01001334 _PyErr_WriteUnraisableMsg("in garbage collection", NULL);
Antoine Pitroufef34e32013-05-19 01:11:58 +02001335 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001336 }
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001337
Antoine Pitroud4156c12012-10-30 22:43:19 +01001338 /* Update stats */
Victor Stinner9db03242019-04-26 02:32:01 +02001339 if (n_collected) {
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001340 *n_collected = m;
Victor Stinner9db03242019-04-26 02:32:01 +02001341 }
1342 if (n_uncollectable) {
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001343 *n_uncollectable = n;
Victor Stinner9db03242019-04-26 02:32:01 +02001344 }
1345
Victor Stinner67e0de62019-11-20 11:48:18 +01001346 struct gc_generation_stats *stats = &gcstate->generation_stats[generation];
Antoine Pitroud4156c12012-10-30 22:43:19 +01001347 stats->collections++;
1348 stats->collected += m;
1349 stats->uncollectable += n;
Łukasz Langaa785c872016-09-09 17:37:37 -07001350
Victor Stinner9db03242019-04-26 02:32:01 +02001351 if (PyDTrace_GC_DONE_ENABLED()) {
Victor Stinner2e969062019-11-20 01:49:32 +01001352 PyDTrace_GC_DONE(n + m);
Victor Stinner9db03242019-04-26 02:32:01 +02001353 }
Łukasz Langaa785c872016-09-09 17:37:37 -07001354
Victor Stinner2e969062019-11-20 01:49:32 +01001355 assert(!_PyErr_Occurred(tstate));
1356 return n + m;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001357}
1358
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001359/* Invoke progress callbacks to notify clients that garbage collection
1360 * is starting or stopping
1361 */
1362static void
Victor Stinner67e0de62019-11-20 11:48:18 +01001363invoke_gc_callback(PyThreadState *tstate, const char *phase,
Victor Stinner9db03242019-04-26 02:32:01 +02001364 int generation, Py_ssize_t collected,
1365 Py_ssize_t uncollectable)
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001366{
Victor Stinner67e0de62019-11-20 11:48:18 +01001367 assert(!_PyErr_Occurred(tstate));
Victor Stinner9db03242019-04-26 02:32:01 +02001368
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001369 /* we may get called very early */
Victor Stinner72474072019-11-20 12:25:50 +01001370 GCState *gcstate = &tstate->interp->gc;
Victor Stinner67e0de62019-11-20 11:48:18 +01001371 if (gcstate->callbacks == NULL) {
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001372 return;
Victor Stinner9db03242019-04-26 02:32:01 +02001373 }
1374
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001375 /* The local variable cannot be rebound, check it for sanity */
Victor Stinner67e0de62019-11-20 11:48:18 +01001376 assert(PyList_CheckExact(gcstate->callbacks));
Victor Stinner9db03242019-04-26 02:32:01 +02001377 PyObject *info = NULL;
Victor Stinner67e0de62019-11-20 11:48:18 +01001378 if (PyList_GET_SIZE(gcstate->callbacks) != 0) {
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001379 info = Py_BuildValue("{sisnsn}",
1380 "generation", generation,
1381 "collected", collected,
1382 "uncollectable", uncollectable);
1383 if (info == NULL) {
1384 PyErr_WriteUnraisable(NULL);
1385 return;
1386 }
1387 }
Victor Stinner67e0de62019-11-20 11:48:18 +01001388 for (Py_ssize_t i=0; i<PyList_GET_SIZE(gcstate->callbacks); i++) {
1389 PyObject *r, *cb = PyList_GET_ITEM(gcstate->callbacks, i);
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001390 Py_INCREF(cb); /* make sure cb doesn't go away */
1391 r = PyObject_CallFunction(cb, "sO", phase, info);
Serhiy Storchaka301e3cc2018-05-24 15:19:29 +03001392 if (r == NULL) {
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001393 PyErr_WriteUnraisable(cb);
Serhiy Storchaka301e3cc2018-05-24 15:19:29 +03001394 }
1395 else {
1396 Py_DECREF(r);
1397 }
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001398 Py_DECREF(cb);
1399 }
1400 Py_XDECREF(info);
Victor Stinner67e0de62019-11-20 11:48:18 +01001401 assert(!_PyErr_Occurred(tstate));
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001402}
1403
1404/* Perform garbage collection of a generation and invoke
1405 * progress callbacks.
1406 */
1407static Py_ssize_t
Victor Stinner8b341482020-10-30 17:00:00 +01001408gc_collect_with_callback(PyThreadState *tstate, int generation)
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001409{
Victor Stinner67e0de62019-11-20 11:48:18 +01001410 assert(!_PyErr_Occurred(tstate));
Victor Stinner9db03242019-04-26 02:32:01 +02001411 Py_ssize_t result, collected, uncollectable;
Victor Stinner67e0de62019-11-20 11:48:18 +01001412 invoke_gc_callback(tstate, "start", generation, 0, 0);
Victor Stinner8b341482020-10-30 17:00:00 +01001413 result = gc_collect_main(tstate, generation, &collected, &uncollectable, 0);
Victor Stinner67e0de62019-11-20 11:48:18 +01001414 invoke_gc_callback(tstate, "stop", generation, collected, uncollectable);
1415 assert(!_PyErr_Occurred(tstate));
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001416 return result;
1417}
1418
Neal Norwitz7b216c52006-03-04 20:01:53 +00001419static Py_ssize_t
Victor Stinner8b341482020-10-30 17:00:00 +01001420gc_collect_generations(PyThreadState *tstate)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001421{
Victor Stinner72474072019-11-20 12:25:50 +01001422 GCState *gcstate = &tstate->interp->gc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001423 /* Find the oldest generation (highest numbered) where the count
1424 * exceeds the threshold. Objects in the that generation and
1425 * generations younger than it will be collected. */
Victor Stinner9db03242019-04-26 02:32:01 +02001426 Py_ssize_t n = 0;
1427 for (int i = NUM_GENERATIONS-1; i >= 0; i--) {
Victor Stinner67e0de62019-11-20 11:48:18 +01001428 if (gcstate->generations[i].count > gcstate->generations[i].threshold) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001429 /* Avoid quadratic performance degradation in number
Pablo Galindo90913982019-12-27 21:55:56 +00001430 of tracked objects (see also issue #4074):
1431
1432 To limit the cost of garbage collection, there are two strategies;
1433 - make each collection faster, e.g. by scanning fewer objects
1434 - do less collections
1435 This heuristic is about the latter strategy.
1436
1437 In addition to the various configurable thresholds, we only trigger a
1438 full collection if the ratio
1439
1440 long_lived_pending / long_lived_total
1441
1442 is above a given value (hardwired to 25%).
1443
1444 The reason is that, while "non-full" collections (i.e., collections of
1445 the young and middle generations) will always examine roughly the same
1446 number of objects -- determined by the aforementioned thresholds --,
1447 the cost of a full collection is proportional to the total number of
1448 long-lived objects, which is virtually unbounded.
1449
1450 Indeed, it has been remarked that doing a full collection every
1451 <constant number> of object creations entails a dramatic performance
1452 degradation in workloads which consist in creating and storing lots of
1453 long-lived objects (e.g. building a large list of GC-tracked objects would
1454 show quadratic performance, instead of linear as expected: see issue #4074).
1455
1456 Using the above ratio, instead, yields amortized linear performance in
1457 the total number of objects (the effect of which can be summarized
1458 thusly: "each full garbage collection is more and more costly as the
1459 number of objects grows, but we do fewer and fewer of them").
1460
1461 This heuristic was suggested by Martin von Löwis on python-dev in
1462 June 2008. His original analysis and proposal can be found at:
1463 http://mail.python.org/pipermail/python-dev/2008-June/080579.html
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001464 */
1465 if (i == NUM_GENERATIONS - 1
Victor Stinner67e0de62019-11-20 11:48:18 +01001466 && gcstate->long_lived_pending < gcstate->long_lived_total / 4)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001467 continue;
Victor Stinner8b341482020-10-30 17:00:00 +01001468 n = gc_collect_with_callback(tstate, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001469 break;
1470 }
1471 }
1472 return n;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001473}
1474
Serhiy Storchaka93260282017-02-04 11:19:59 +02001475#include "clinic/gcmodule.c.h"
1476
1477/*[clinic input]
1478gc.enable
1479
1480Enable automatic garbage collection.
1481[clinic start generated code]*/
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001482
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001483static PyObject *
Serhiy Storchaka93260282017-02-04 11:19:59 +02001484gc_enable_impl(PyObject *module)
1485/*[clinic end generated code: output=45a427e9dce9155c input=81ac4940ca579707]*/
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001486{
Victor Stinner1bcc32f2020-06-10 20:08:26 +02001487 GCState *gcstate = get_gc_state();
Victor Stinner67e0de62019-11-20 11:48:18 +01001488 gcstate->enabled = 1;
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001489 Py_RETURN_NONE;
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001490}
1491
Serhiy Storchaka93260282017-02-04 11:19:59 +02001492/*[clinic input]
1493gc.disable
1494
1495Disable automatic garbage collection.
1496[clinic start generated code]*/
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001497
1498static PyObject *
Serhiy Storchaka93260282017-02-04 11:19:59 +02001499gc_disable_impl(PyObject *module)
1500/*[clinic end generated code: output=97d1030f7aa9d279 input=8c2e5a14e800d83b]*/
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001501{
Victor Stinner1bcc32f2020-06-10 20:08:26 +02001502 GCState *gcstate = get_gc_state();
Victor Stinner67e0de62019-11-20 11:48:18 +01001503 gcstate->enabled = 0;
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001504 Py_RETURN_NONE;
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001505}
1506
Serhiy Storchaka93260282017-02-04 11:19:59 +02001507/*[clinic input]
1508gc.isenabled -> bool
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001509
Serhiy Storchaka93260282017-02-04 11:19:59 +02001510Returns true if automatic garbage collection is enabled.
1511[clinic start generated code]*/
1512
1513static int
1514gc_isenabled_impl(PyObject *module)
1515/*[clinic end generated code: output=1874298331c49130 input=30005e0422373b31]*/
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001516{
Victor Stinner1bcc32f2020-06-10 20:08:26 +02001517 GCState *gcstate = get_gc_state();
Victor Stinner67e0de62019-11-20 11:48:18 +01001518 return gcstate->enabled;
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001519}
1520
Serhiy Storchaka93260282017-02-04 11:19:59 +02001521/*[clinic input]
1522gc.collect -> Py_ssize_t
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001523
Serhiy Storchaka93260282017-02-04 11:19:59 +02001524 generation: int(c_default="NUM_GENERATIONS - 1") = 2
1525
1526Run the garbage collector.
1527
1528With no arguments, run a full collection. The optional argument
1529may be an integer specifying which generation to collect. A ValueError
1530is raised if the generation number is invalid.
1531
1532The number of unreachable objects is returned.
1533[clinic start generated code]*/
1534
1535static Py_ssize_t
1536gc_collect_impl(PyObject *module, int generation)
1537/*[clinic end generated code: output=b697e633043233c7 input=40720128b682d879]*/
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001538{
Victor Stinner2e969062019-11-20 01:49:32 +01001539 PyThreadState *tstate = _PyThreadState_GET();
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001540
Serhiy Storchaka93260282017-02-04 11:19:59 +02001541 if (generation < 0 || generation >= NUM_GENERATIONS) {
Victor Stinner2e969062019-11-20 01:49:32 +01001542 _PyErr_SetString(tstate, PyExc_ValueError, "invalid generation");
Serhiy Storchaka93260282017-02-04 11:19:59 +02001543 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001544 }
Barry Warsawd3c38ff2006-03-07 09:46:03 +00001545
Victor Stinner72474072019-11-20 12:25:50 +01001546 GCState *gcstate = &tstate->interp->gc;
Victor Stinner9db03242019-04-26 02:32:01 +02001547 Py_ssize_t n;
Victor Stinner67e0de62019-11-20 11:48:18 +01001548 if (gcstate->collecting) {
Victor Stinner9db03242019-04-26 02:32:01 +02001549 /* already collecting, don't do anything */
1550 n = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001551 }
Victor Stinner9db03242019-04-26 02:32:01 +02001552 else {
Victor Stinner67e0de62019-11-20 11:48:18 +01001553 gcstate->collecting = 1;
Victor Stinner8b341482020-10-30 17:00:00 +01001554 n = gc_collect_with_callback(tstate, generation);
Victor Stinner67e0de62019-11-20 11:48:18 +01001555 gcstate->collecting = 0;
Victor Stinner9db03242019-04-26 02:32:01 +02001556 }
Serhiy Storchaka93260282017-02-04 11:19:59 +02001557 return n;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001558}
1559
Serhiy Storchaka93260282017-02-04 11:19:59 +02001560/*[clinic input]
1561gc.set_debug
1562
1563 flags: int
1564 An integer that can have the following bits turned on:
1565 DEBUG_STATS - Print statistics during collection.
1566 DEBUG_COLLECTABLE - Print collectable objects found.
1567 DEBUG_UNCOLLECTABLE - Print unreachable but uncollectable objects
1568 found.
1569 DEBUG_SAVEALL - Save objects to gc.garbage rather than freeing them.
1570 DEBUG_LEAK - Debug leaking programs (everything but STATS).
1571 /
1572
1573Set the garbage collection debugging flags.
1574
1575Debugging information is written to sys.stderr.
1576[clinic start generated code]*/
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001577
1578static PyObject *
Serhiy Storchaka93260282017-02-04 11:19:59 +02001579gc_set_debug_impl(PyObject *module, int flags)
1580/*[clinic end generated code: output=7c8366575486b228 input=5e5ce15e84fbed15]*/
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001581{
Victor Stinner1bcc32f2020-06-10 20:08:26 +02001582 GCState *gcstate = get_gc_state();
Victor Stinner67e0de62019-11-20 11:48:18 +01001583 gcstate->debug = flags;
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001584 Py_RETURN_NONE;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001585}
1586
Serhiy Storchaka93260282017-02-04 11:19:59 +02001587/*[clinic input]
1588gc.get_debug -> int
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001589
Serhiy Storchaka93260282017-02-04 11:19:59 +02001590Get the garbage collection debugging flags.
1591[clinic start generated code]*/
1592
1593static int
1594gc_get_debug_impl(PyObject *module)
1595/*[clinic end generated code: output=91242f3506cd1e50 input=91a101e1c3b98366]*/
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001596{
Victor Stinner1bcc32f2020-06-10 20:08:26 +02001597 GCState *gcstate = get_gc_state();
Victor Stinner67e0de62019-11-20 11:48:18 +01001598 return gcstate->debug;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001599}
1600
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001601PyDoc_STRVAR(gc_set_thresh__doc__,
Neal Norwitz2a47c0f2002-01-29 00:53:41 +00001602"set_threshold(threshold0, [threshold1, threshold2]) -> None\n"
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001603"\n"
1604"Sets the collection thresholds. Setting threshold0 to zero disables\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001605"collection.\n");
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001606
1607static PyObject *
Victor Stinner9db03242019-04-26 02:32:01 +02001608gc_set_threshold(PyObject *self, PyObject *args)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001609{
Victor Stinner1bcc32f2020-06-10 20:08:26 +02001610 GCState *gcstate = get_gc_state();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001611 if (!PyArg_ParseTuple(args, "i|ii:set_threshold",
Victor Stinner67e0de62019-11-20 11:48:18 +01001612 &gcstate->generations[0].threshold,
1613 &gcstate->generations[1].threshold,
1614 &gcstate->generations[2].threshold))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001615 return NULL;
Victor Stinner9db03242019-04-26 02:32:01 +02001616 for (int i = 3; i < NUM_GENERATIONS; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 /* generations higher than 2 get the same threshold */
Victor Stinner67e0de62019-11-20 11:48:18 +01001618 gcstate->generations[i].threshold = gcstate->generations[2].threshold;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001619 }
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001620 Py_RETURN_NONE;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001621}
1622
Serhiy Storchaka93260282017-02-04 11:19:59 +02001623/*[clinic input]
1624gc.get_threshold
1625
1626Return the current collection thresholds.
1627[clinic start generated code]*/
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001628
1629static PyObject *
Serhiy Storchaka93260282017-02-04 11:19:59 +02001630gc_get_threshold_impl(PyObject *module)
1631/*[clinic end generated code: output=7902bc9f41ecbbd8 input=286d79918034d6e6]*/
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001632{
Victor Stinner1bcc32f2020-06-10 20:08:26 +02001633 GCState *gcstate = get_gc_state();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001634 return Py_BuildValue("(iii)",
Victor Stinner67e0de62019-11-20 11:48:18 +01001635 gcstate->generations[0].threshold,
1636 gcstate->generations[1].threshold,
1637 gcstate->generations[2].threshold);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001638}
1639
Serhiy Storchaka93260282017-02-04 11:19:59 +02001640/*[clinic input]
1641gc.get_count
1642
1643Return a three-tuple of the current collection counts.
1644[clinic start generated code]*/
Barry Warsawd3c38ff2006-03-07 09:46:03 +00001645
1646static PyObject *
Serhiy Storchaka93260282017-02-04 11:19:59 +02001647gc_get_count_impl(PyObject *module)
1648/*[clinic end generated code: output=354012e67b16398f input=a392794a08251751]*/
Barry Warsawd3c38ff2006-03-07 09:46:03 +00001649{
Victor Stinner1bcc32f2020-06-10 20:08:26 +02001650 GCState *gcstate = get_gc_state();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001651 return Py_BuildValue("(iii)",
Victor Stinner67e0de62019-11-20 11:48:18 +01001652 gcstate->generations[0].count,
1653 gcstate->generations[1].count,
1654 gcstate->generations[2].count);
Barry Warsawd3c38ff2006-03-07 09:46:03 +00001655}
1656
Neil Schemenauer48c70342001-08-09 15:38:31 +00001657static int
Martin v. Löwis560da622001-11-24 09:24:51 +00001658referrersvisit(PyObject* obj, PyObject *objs)
Neil Schemenauer48c70342001-08-09 15:38:31 +00001659{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001660 Py_ssize_t i;
1661 for (i = 0; i < PyTuple_GET_SIZE(objs); i++)
1662 if (PyTuple_GET_ITEM(objs, i) == obj)
1663 return 1;
1664 return 0;
Neil Schemenauer48c70342001-08-09 15:38:31 +00001665}
1666
Neil Schemenauer17e7be62001-08-10 14:46:47 +00001667static int
Martin v. Löwis560da622001-11-24 09:24:51 +00001668gc_referrers_for(PyObject *objs, PyGC_Head *list, PyObject *resultlist)
Neil Schemenauer48c70342001-08-09 15:38:31 +00001669{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001670 PyGC_Head *gc;
1671 PyObject *obj;
1672 traverseproc traverse;
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09001673 for (gc = GC_NEXT(list); gc != list; gc = GC_NEXT(gc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001674 obj = FROM_GC(gc);
1675 traverse = Py_TYPE(obj)->tp_traverse;
1676 if (obj == objs || obj == resultlist)
1677 continue;
1678 if (traverse(obj, (visitproc)referrersvisit, objs)) {
1679 if (PyList_Append(resultlist, obj) < 0)
1680 return 0; /* error */
1681 }
1682 }
1683 return 1; /* no error */
Neil Schemenauer48c70342001-08-09 15:38:31 +00001684}
1685
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001686PyDoc_STRVAR(gc_get_referrers__doc__,
Martin v. Löwis560da622001-11-24 09:24:51 +00001687"get_referrers(*objs) -> list\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001688Return the list of objects that directly refer to any of objs.");
Neil Schemenauer48c70342001-08-09 15:38:31 +00001689
Neil Schemenauer17e7be62001-08-10 14:46:47 +00001690static PyObject *
Martin v. Löwis560da622001-11-24 09:24:51 +00001691gc_get_referrers(PyObject *self, PyObject *args)
Neil Schemenauer48c70342001-08-09 15:38:31 +00001692{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001693 PyObject *result = PyList_New(0);
Victor Stinner67e0de62019-11-20 11:48:18 +01001694 if (!result) {
1695 return NULL;
1696 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001697
Victor Stinner1bcc32f2020-06-10 20:08:26 +02001698 GCState *gcstate = get_gc_state();
1699 for (int i = 0; i < NUM_GENERATIONS; i++) {
Victor Stinner67e0de62019-11-20 11:48:18 +01001700 if (!(gc_referrers_for(args, GEN_HEAD(gcstate, i), result))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001701 Py_DECREF(result);
1702 return NULL;
1703 }
1704 }
1705 return result;
Neil Schemenauer48c70342001-08-09 15:38:31 +00001706}
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001707
Tim Peters0f81ab62003-04-08 16:39:48 +00001708/* Append obj to list; return true if error (out of memory), false if OK. */
Jeremy Hylton5bd378b2003-04-03 16:28:38 +00001709static int
Tim Peters730f5532003-04-08 17:17:17 +00001710referentsvisit(PyObject *obj, PyObject *list)
Jeremy Hylton5bd378b2003-04-03 16:28:38 +00001711{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001712 return PyList_Append(list, obj) < 0;
Jeremy Hylton5bd378b2003-04-03 16:28:38 +00001713}
1714
Tim Peters730f5532003-04-08 17:17:17 +00001715PyDoc_STRVAR(gc_get_referents__doc__,
1716"get_referents(*objs) -> list\n\
Jeremy Hylton059b0942003-04-03 16:29:13 +00001717Return the list of objects that are directly referred to by objs.");
Jeremy Hylton5bd378b2003-04-03 16:28:38 +00001718
1719static PyObject *
Tim Peters730f5532003-04-08 17:17:17 +00001720gc_get_referents(PyObject *self, PyObject *args)
Jeremy Hylton5bd378b2003-04-03 16:28:38 +00001721{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001722 Py_ssize_t i;
1723 PyObject *result = PyList_New(0);
Tim Peters0f81ab62003-04-08 16:39:48 +00001724
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001725 if (result == NULL)
1726 return NULL;
Tim Peters0f81ab62003-04-08 16:39:48 +00001727
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001728 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
1729 traverseproc traverse;
1730 PyObject *obj = PyTuple_GET_ITEM(args, i);
Tim Peters0f81ab62003-04-08 16:39:48 +00001731
Hai Shi675d9a32020-04-15 02:11:20 +08001732 if (!_PyObject_IS_GC(obj))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001733 continue;
1734 traverse = Py_TYPE(obj)->tp_traverse;
1735 if (! traverse)
1736 continue;
1737 if (traverse(obj, (visitproc)referentsvisit, result)) {
1738 Py_DECREF(result);
1739 return NULL;
1740 }
1741 }
1742 return result;
Jeremy Hylton5bd378b2003-04-03 16:28:38 +00001743}
1744
Serhiy Storchaka93260282017-02-04 11:19:59 +02001745/*[clinic input]
1746gc.get_objects
Pablo Galindo175421b2019-02-23 03:02:06 +00001747 generation: Py_ssize_t(accept={int, NoneType}, c_default="-1") = None
1748 Generation to extract the objects from.
Serhiy Storchaka93260282017-02-04 11:19:59 +02001749
1750Return a list of objects tracked by the collector (excluding the list returned).
Pablo Galindo175421b2019-02-23 03:02:06 +00001751
1752If generation is not None, return only the objects tracked by the collector
1753that are in that generation.
Serhiy Storchaka93260282017-02-04 11:19:59 +02001754[clinic start generated code]*/
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +00001755
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +00001756static PyObject *
Pablo Galindo175421b2019-02-23 03:02:06 +00001757gc_get_objects_impl(PyObject *module, Py_ssize_t generation)
1758/*[clinic end generated code: output=48b35fea4ba6cb0e input=ef7da9df9806754c]*/
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +00001759{
Victor Stinner67e0de62019-11-20 11:48:18 +01001760 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001761 int i;
1762 PyObject* result;
Victor Stinner72474072019-11-20 12:25:50 +01001763 GCState *gcstate = &tstate->interp->gc;
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +00001764
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001765 result = PyList_New(0);
Pablo Galindo175421b2019-02-23 03:02:06 +00001766 if (result == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001767 return NULL;
Pablo Galindo175421b2019-02-23 03:02:06 +00001768 }
1769
1770 /* If generation is passed, we extract only that generation */
Victor Stinner0810fa72019-04-15 17:54:09 +02001771 if (generation != -1) {
Pablo Galindo175421b2019-02-23 03:02:06 +00001772 if (generation >= NUM_GENERATIONS) {
Victor Stinner67e0de62019-11-20 11:48:18 +01001773 _PyErr_Format(tstate, PyExc_ValueError,
1774 "generation parameter must be less than the number of "
1775 "available generations (%i)",
1776 NUM_GENERATIONS);
Pablo Galindo175421b2019-02-23 03:02:06 +00001777 goto error;
1778 }
1779
1780 if (generation < 0) {
Victor Stinner67e0de62019-11-20 11:48:18 +01001781 _PyErr_SetString(tstate, PyExc_ValueError,
1782 "generation parameter cannot be negative");
Pablo Galindo175421b2019-02-23 03:02:06 +00001783 goto error;
1784 }
1785
Victor Stinner67e0de62019-11-20 11:48:18 +01001786 if (append_objects(result, GEN_HEAD(gcstate, generation))) {
Pablo Galindo175421b2019-02-23 03:02:06 +00001787 goto error;
1788 }
1789
1790 return result;
1791 }
1792
1793 /* If generation is not passed or None, get all objects from all generations */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001794 for (i = 0; i < NUM_GENERATIONS; i++) {
Victor Stinner67e0de62019-11-20 11:48:18 +01001795 if (append_objects(result, GEN_HEAD(gcstate, i))) {
Pablo Galindo175421b2019-02-23 03:02:06 +00001796 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001797 }
1798 }
1799 return result;
Pablo Galindo175421b2019-02-23 03:02:06 +00001800
1801error:
1802 Py_DECREF(result);
1803 return NULL;
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +00001804}
1805
Serhiy Storchaka93260282017-02-04 11:19:59 +02001806/*[clinic input]
1807gc.get_stats
1808
1809Return a list of dictionaries containing per-generation statistics.
1810[clinic start generated code]*/
Antoine Pitroud4156c12012-10-30 22:43:19 +01001811
1812static PyObject *
Serhiy Storchaka93260282017-02-04 11:19:59 +02001813gc_get_stats_impl(PyObject *module)
1814/*[clinic end generated code: output=a8ab1d8a5d26f3ab input=1ef4ed9d17b1a470]*/
Antoine Pitroud4156c12012-10-30 22:43:19 +01001815{
1816 int i;
Antoine Pitroud4156c12012-10-30 22:43:19 +01001817 struct gc_generation_stats stats[NUM_GENERATIONS], *st;
1818
1819 /* To get consistent values despite allocations while constructing
1820 the result list, we use a snapshot of the running stats. */
Victor Stinner1bcc32f2020-06-10 20:08:26 +02001821 GCState *gcstate = get_gc_state();
Antoine Pitroud4156c12012-10-30 22:43:19 +01001822 for (i = 0; i < NUM_GENERATIONS; i++) {
Victor Stinner67e0de62019-11-20 11:48:18 +01001823 stats[i] = gcstate->generation_stats[i];
Antoine Pitroud4156c12012-10-30 22:43:19 +01001824 }
1825
Victor Stinner9db03242019-04-26 02:32:01 +02001826 PyObject *result = PyList_New(0);
Antoine Pitroud4156c12012-10-30 22:43:19 +01001827 if (result == NULL)
1828 return NULL;
1829
1830 for (i = 0; i < NUM_GENERATIONS; i++) {
1831 PyObject *dict;
1832 st = &stats[i];
1833 dict = Py_BuildValue("{snsnsn}",
1834 "collections", st->collections,
1835 "collected", st->collected,
1836 "uncollectable", st->uncollectable
1837 );
1838 if (dict == NULL)
1839 goto error;
1840 if (PyList_Append(result, dict)) {
1841 Py_DECREF(dict);
1842 goto error;
1843 }
1844 Py_DECREF(dict);
1845 }
1846 return result;
1847
1848error:
1849 Py_XDECREF(result);
1850 return NULL;
1851}
1852
1853
Serhiy Storchaka93260282017-02-04 11:19:59 +02001854/*[clinic input]
1855gc.is_tracked
1856
1857 obj: object
1858 /
1859
1860Returns true if the object is tracked by the garbage collector.
1861
1862Simple atomic objects will return false.
1863[clinic start generated code]*/
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001864
1865static PyObject *
Serhiy Storchaka93260282017-02-04 11:19:59 +02001866gc_is_tracked(PyObject *module, PyObject *obj)
1867/*[clinic end generated code: output=14f0103423b28e31 input=d83057f170ea2723]*/
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001868{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001869 PyObject *result;
1870
Hai Shi675d9a32020-04-15 02:11:20 +08001871 if (_PyObject_IS_GC(obj) && _PyObject_GC_IS_TRACKED(obj))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001872 result = Py_True;
1873 else
1874 result = Py_False;
1875 Py_INCREF(result);
1876 return result;
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001877}
1878
brainfvckc75edab2017-10-16 12:49:41 -07001879/*[clinic input]
Pablo Galindoa2ec3f02020-01-14 12:06:45 +00001880gc.is_finalized
1881
1882 obj: object
1883 /
1884
1885Returns true if the object has been already finalized by the GC.
1886[clinic start generated code]*/
1887
1888static PyObject *
1889gc_is_finalized(PyObject *module, PyObject *obj)
1890/*[clinic end generated code: output=e1516ac119a918ed input=201d0c58f69ae390]*/
1891{
Hai Shi675d9a32020-04-15 02:11:20 +08001892 if (_PyObject_IS_GC(obj) && _PyGCHead_FINALIZED(AS_GC(obj))) {
Pablo Galindoa2ec3f02020-01-14 12:06:45 +00001893 Py_RETURN_TRUE;
1894 }
1895 Py_RETURN_FALSE;
1896}
1897
1898/*[clinic input]
brainfvckc75edab2017-10-16 12:49:41 -07001899gc.freeze
1900
1901Freeze all current tracked objects and ignore them for future collections.
1902
1903This can be used before a POSIX fork() call to make the gc copy-on-write friendly.
1904Note: collection before a POSIX fork() call may free pages for future allocation
1905which can cause copy-on-write.
1906[clinic start generated code]*/
1907
1908static PyObject *
1909gc_freeze_impl(PyObject *module)
1910/*[clinic end generated code: output=502159d9cdc4c139 input=b602b16ac5febbe5]*/
1911{
Victor Stinner1bcc32f2020-06-10 20:08:26 +02001912 GCState *gcstate = get_gc_state();
brainfvckc75edab2017-10-16 12:49:41 -07001913 for (int i = 0; i < NUM_GENERATIONS; ++i) {
Victor Stinner67e0de62019-11-20 11:48:18 +01001914 gc_list_merge(GEN_HEAD(gcstate, i), &gcstate->permanent_generation.head);
1915 gcstate->generations[i].count = 0;
brainfvckc75edab2017-10-16 12:49:41 -07001916 }
1917 Py_RETURN_NONE;
1918}
1919
1920/*[clinic input]
1921gc.unfreeze
1922
1923Unfreeze all objects in the permanent generation.
1924
1925Put all objects in the permanent generation back into oldest generation.
1926[clinic start generated code]*/
1927
1928static PyObject *
1929gc_unfreeze_impl(PyObject *module)
1930/*[clinic end generated code: output=1c15f2043b25e169 input=2dd52b170f4cef6c]*/
1931{
Victor Stinner1bcc32f2020-06-10 20:08:26 +02001932 GCState *gcstate = get_gc_state();
Victor Stinner67e0de62019-11-20 11:48:18 +01001933 gc_list_merge(&gcstate->permanent_generation.head,
1934 GEN_HEAD(gcstate, NUM_GENERATIONS-1));
brainfvckc75edab2017-10-16 12:49:41 -07001935 Py_RETURN_NONE;
1936}
1937
1938/*[clinic input]
Victor Stinner05d68a82018-01-18 11:15:25 +01001939gc.get_freeze_count -> Py_ssize_t
brainfvckc75edab2017-10-16 12:49:41 -07001940
1941Return the number of objects in the permanent generation.
1942[clinic start generated code]*/
1943
Victor Stinner05d68a82018-01-18 11:15:25 +01001944static Py_ssize_t
brainfvckc75edab2017-10-16 12:49:41 -07001945gc_get_freeze_count_impl(PyObject *module)
Victor Stinner05d68a82018-01-18 11:15:25 +01001946/*[clinic end generated code: output=61cbd9f43aa032e1 input=45ffbc65cfe2a6ed]*/
brainfvckc75edab2017-10-16 12:49:41 -07001947{
Victor Stinner1bcc32f2020-06-10 20:08:26 +02001948 GCState *gcstate = get_gc_state();
Victor Stinner67e0de62019-11-20 11:48:18 +01001949 return gc_list_size(&gcstate->permanent_generation.head);
brainfvckc75edab2017-10-16 12:49:41 -07001950}
1951
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +00001952
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001953PyDoc_STRVAR(gc__doc__,
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001954"This module provides access to the garbage collector for reference cycles.\n"
1955"\n"
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001956"enable() -- Enable automatic garbage collection.\n"
1957"disable() -- Disable automatic garbage collection.\n"
1958"isenabled() -- Returns true if automatic collection is enabled.\n"
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001959"collect() -- Do a full collection right now.\n"
Thomas Wouters89f507f2006-12-13 04:49:30 +00001960"get_count() -- Return the current collection counts.\n"
R David Murray0e814632013-12-26 15:11:28 -05001961"get_stats() -- Return list of dictionaries containing per-generation stats.\n"
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001962"set_debug() -- Set debugging flags.\n"
1963"get_debug() -- Get debugging flags.\n"
1964"set_threshold() -- Set the collection thresholds.\n"
1965"get_threshold() -- Return the current the collection thresholds.\n"
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +00001966"get_objects() -- Return a list of all objects tracked by the collector.\n"
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001967"is_tracked() -- Returns true if a given object is tracked.\n"
Pablo Galindob6791372020-01-14 17:38:15 +00001968"is_finalized() -- Returns true if a given object has been already finalized.\n"
Jeremy Hylton5bd378b2003-04-03 16:28:38 +00001969"get_referrers() -- Return the list of objects that refer to an object.\n"
brainfvckc75edab2017-10-16 12:49:41 -07001970"get_referents() -- Return the list of objects that an object refers to.\n"
1971"freeze() -- Freeze all tracked objects and ignore them for future collections.\n"
1972"unfreeze() -- Unfreeze all objects in the permanent generation.\n"
1973"get_freeze_count() -- Return the number of objects in the permanent generation.\n");
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001974
1975static PyMethodDef GcMethods[] = {
Serhiy Storchaka93260282017-02-04 11:19:59 +02001976 GC_ENABLE_METHODDEF
1977 GC_DISABLE_METHODDEF
1978 GC_ISENABLED_METHODDEF
1979 GC_SET_DEBUG_METHODDEF
1980 GC_GET_DEBUG_METHODDEF
1981 GC_GET_COUNT_METHODDEF
Victor Stinner9db03242019-04-26 02:32:01 +02001982 {"set_threshold", gc_set_threshold, METH_VARARGS, gc_set_thresh__doc__},
Serhiy Storchaka93260282017-02-04 11:19:59 +02001983 GC_GET_THRESHOLD_METHODDEF
1984 GC_COLLECT_METHODDEF
1985 GC_GET_OBJECTS_METHODDEF
1986 GC_GET_STATS_METHODDEF
1987 GC_IS_TRACKED_METHODDEF
Pablo Galindoa2ec3f02020-01-14 12:06:45 +00001988 GC_IS_FINALIZED_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001989 {"get_referrers", gc_get_referrers, METH_VARARGS,
1990 gc_get_referrers__doc__},
1991 {"get_referents", gc_get_referents, METH_VARARGS,
1992 gc_get_referents__doc__},
brainfvckc75edab2017-10-16 12:49:41 -07001993 GC_FREEZE_METHODDEF
1994 GC_UNFREEZE_METHODDEF
1995 GC_GET_FREEZE_COUNT_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001996 {NULL, NULL} /* Sentinel */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001997};
1998
Christian Heimes646d7fd2020-11-19 15:08:34 +01001999static int
2000gcmodule_exec(PyObject *module)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00002001{
Victor Stinner1bcc32f2020-06-10 20:08:26 +02002002 GCState *gcstate = get_gc_state();
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00002003
Christian Heimes646d7fd2020-11-19 15:08:34 +01002004 /* garbage and callbacks are initialized by _PyGC_Init() early in
2005 * interpreter lifecycle. */
2006 assert(gcstate->garbage != NULL);
2007 if (PyModule_AddObjectRef(module, "garbage", gcstate->garbage) < 0) {
2008 return -1;
2009 }
2010 assert(gcstate->callbacks != NULL);
2011 if (PyModule_AddObjectRef(module, "callbacks", gcstate->callbacks) < 0) {
2012 return -1;
Victor Stinner9db03242019-04-26 02:32:01 +02002013 }
Tim Peters11558872003-04-06 23:30:52 +00002014
Christian Heimes646d7fd2020-11-19 15:08:34 +01002015#define ADD_INT(NAME) if (PyModule_AddIntConstant(module, #NAME, NAME) < 0) { return -1; }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002016 ADD_INT(DEBUG_STATS);
2017 ADD_INT(DEBUG_COLLECTABLE);
2018 ADD_INT(DEBUG_UNCOLLECTABLE);
2019 ADD_INT(DEBUG_SAVEALL);
2020 ADD_INT(DEBUG_LEAK);
Tim Peters11558872003-04-06 23:30:52 +00002021#undef ADD_INT
Christian Heimes646d7fd2020-11-19 15:08:34 +01002022 return 0;
2023}
2024
2025static PyModuleDef_Slot gcmodule_slots[] = {
2026 {Py_mod_exec, gcmodule_exec},
2027 {0, NULL}
2028};
2029
2030static struct PyModuleDef gcmodule = {
2031 PyModuleDef_HEAD_INIT,
2032 .m_name = "gc",
2033 .m_doc = gc__doc__,
2034 .m_size = 0, // per interpreter state, see: get_gc_state()
2035 .m_methods = GcMethods,
2036 .m_slots = gcmodule_slots
2037};
2038
2039PyMODINIT_FUNC
2040PyInit_gc(void)
2041{
2042 return PyModuleDef_Init(&gcmodule);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00002043}
2044
Victor Stinner8b341482020-10-30 17:00:00 +01002045/* Public API to invoke gc.collect() from C */
Neal Norwitz7b216c52006-03-04 20:01:53 +00002046Py_ssize_t
Guido van Rossume13ddc92003-04-17 17:29:22 +00002047PyGC_Collect(void)
2048{
Victor Stinner2e969062019-11-20 01:49:32 +01002049 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner72474072019-11-20 12:25:50 +01002050 GCState *gcstate = &tstate->interp->gc;
Victor Stinner2e969062019-11-20 01:49:32 +01002051
Victor Stinner67e0de62019-11-20 11:48:18 +01002052 if (!gcstate->enabled) {
Victor Stinner9db03242019-04-26 02:32:01 +02002053 return 0;
2054 }
Guido van Rossume13ddc92003-04-17 17:29:22 +00002055
Victor Stinner9db03242019-04-26 02:32:01 +02002056 Py_ssize_t n;
Victor Stinner67e0de62019-11-20 11:48:18 +01002057 if (gcstate->collecting) {
Victor Stinner9db03242019-04-26 02:32:01 +02002058 /* already collecting, don't do anything */
2059 n = 0;
2060 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002061 else {
Serhiy Storchaka301e3cc2018-05-24 15:19:29 +03002062 PyObject *exc, *value, *tb;
Victor Stinner67e0de62019-11-20 11:48:18 +01002063 gcstate->collecting = 1;
Victor Stinner2e969062019-11-20 01:49:32 +01002064 _PyErr_Fetch(tstate, &exc, &value, &tb);
Victor Stinner8b341482020-10-30 17:00:00 +01002065 n = gc_collect_with_callback(tstate, NUM_GENERATIONS - 1);
Victor Stinner2e969062019-11-20 01:49:32 +01002066 _PyErr_Restore(tstate, exc, value, tb);
Victor Stinner67e0de62019-11-20 11:48:18 +01002067 gcstate->collecting = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002068 }
Guido van Rossume13ddc92003-04-17 17:29:22 +00002069
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002070 return n;
Guido van Rossume13ddc92003-04-17 17:29:22 +00002071}
2072
Antoine Pitroufef34e32013-05-19 01:11:58 +02002073Py_ssize_t
Victor Stinner8b341482020-10-30 17:00:00 +01002074_PyGC_CollectNoFail(PyThreadState *tstate)
Łukasz Langafef7e942016-09-09 21:47:46 -07002075{
Antoine Pitrouc69c9bc2013-08-15 20:15:15 +02002076 /* Ideally, this function is only called on interpreter shutdown,
2077 and therefore not recursively. Unfortunately, when there are daemon
2078 threads, a daemon thread can start a cyclic garbage collection
2079 during interpreter shutdown (and then never finish it).
2080 See http://bugs.python.org/issue8713#msg195178 for an example.
2081 */
Victor Stinnereba5bf22020-10-30 22:51:02 +01002082 GCState *gcstate = &tstate->interp->gc;
Victor Stinner67e0de62019-11-20 11:48:18 +01002083 if (gcstate->collecting) {
Victor Stinner8b341482020-10-30 17:00:00 +01002084 return 0;
Victor Stinner9db03242019-04-26 02:32:01 +02002085 }
Victor Stinner8b341482020-10-30 17:00:00 +01002086
2087 Py_ssize_t n;
2088 gcstate->collecting = 1;
2089 n = gc_collect_main(tstate, NUM_GENERATIONS - 1, NULL, NULL, 1);
2090 gcstate->collecting = 0;
Antoine Pitroufef34e32013-05-19 01:11:58 +02002091 return n;
2092}
Antoine Pitrou5f454a02013-05-06 21:15:57 +02002093
Antoine Pitrou696e0352010-08-08 22:18:46 +00002094void
Victor Stinner67e0de62019-11-20 11:48:18 +01002095_PyGC_DumpShutdownStats(PyThreadState *tstate)
Antoine Pitrou696e0352010-08-08 22:18:46 +00002096{
Victor Stinner72474072019-11-20 12:25:50 +01002097 GCState *gcstate = &tstate->interp->gc;
Victor Stinner67e0de62019-11-20 11:48:18 +01002098 if (!(gcstate->debug & DEBUG_SAVEALL)
2099 && gcstate->garbage != NULL && PyList_GET_SIZE(gcstate->garbage) > 0) {
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02002100 const char *message;
Victor Stinner67e0de62019-11-20 11:48:18 +01002101 if (gcstate->debug & DEBUG_UNCOLLECTABLE)
Antoine Pitroub5d82042010-11-05 00:05:25 +00002102 message = "gc: %zd uncollectable objects at " \
Georg Brandl08be72d2010-10-24 15:11:22 +00002103 "shutdown";
2104 else
Antoine Pitroub5d82042010-11-05 00:05:25 +00002105 message = "gc: %zd uncollectable objects at " \
Georg Brandl08be72d2010-10-24 15:11:22 +00002106 "shutdown; use gc.set_debug(gc.DEBUG_UNCOLLECTABLE) to list them";
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02002107 /* PyErr_WarnFormat does too many things and we are at shutdown,
2108 the warnings module's dependencies (e.g. linecache) may be gone
2109 already. */
2110 if (PyErr_WarnExplicitFormat(PyExc_ResourceWarning, "gc", 0,
2111 "gc", NULL, message,
Victor Stinner67e0de62019-11-20 11:48:18 +01002112 PyList_GET_SIZE(gcstate->garbage)))
Georg Brandl08be72d2010-10-24 15:11:22 +00002113 PyErr_WriteUnraisable(NULL);
Victor Stinner67e0de62019-11-20 11:48:18 +01002114 if (gcstate->debug & DEBUG_UNCOLLECTABLE) {
Antoine Pitrou696e0352010-08-08 22:18:46 +00002115 PyObject *repr = NULL, *bytes = NULL;
Victor Stinner67e0de62019-11-20 11:48:18 +01002116 repr = PyObject_Repr(gcstate->garbage);
Antoine Pitrou696e0352010-08-08 22:18:46 +00002117 if (!repr || !(bytes = PyUnicode_EncodeFSDefault(repr)))
Victor Stinner67e0de62019-11-20 11:48:18 +01002118 PyErr_WriteUnraisable(gcstate->garbage);
Antoine Pitrou696e0352010-08-08 22:18:46 +00002119 else {
2120 PySys_WriteStderr(
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02002121 " %s\n",
Antoine Pitrou696e0352010-08-08 22:18:46 +00002122 PyBytes_AS_STRING(bytes)
2123 );
2124 }
2125 Py_XDECREF(repr);
2126 Py_XDECREF(bytes);
2127 }
Antoine Pitrou696e0352010-08-08 22:18:46 +00002128 }
Antoine Pitrou5f454a02013-05-06 21:15:57 +02002129}
2130
2131void
Victor Stinner7eee5be2019-11-20 10:38:34 +01002132_PyGC_Fini(PyThreadState *tstate)
Antoine Pitrou5f454a02013-05-06 21:15:57 +02002133{
Victor Stinner72474072019-11-20 12:25:50 +01002134 GCState *gcstate = &tstate->interp->gc;
Victor Stinner67e0de62019-11-20 11:48:18 +01002135 Py_CLEAR(gcstate->garbage);
2136 Py_CLEAR(gcstate->callbacks);
Antoine Pitrou696e0352010-08-08 22:18:46 +00002137}
2138
Neil Schemenauer43411b52001-08-30 00:05:51 +00002139/* for debugging */
Guido van Rossume13ddc92003-04-17 17:29:22 +00002140void
2141_PyGC_Dump(PyGC_Head *g)
Neil Schemenauer43411b52001-08-30 00:05:51 +00002142{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002143 _PyObject_Dump(FROM_GC(g));
Neil Schemenauer43411b52001-08-30 00:05:51 +00002144}
2145
Victor Stinnera5447732019-10-10 09:32:13 +02002146
2147#ifdef Py_DEBUG
Victor Stinner1b184552019-10-08 00:09:31 +02002148static int
2149visit_validate(PyObject *op, void *parent_raw)
2150{
2151 PyObject *parent = _PyObject_CAST(parent_raw);
2152 if (_PyObject_IsFreed(op)) {
2153 _PyObject_ASSERT_FAILED_MSG(parent,
2154 "PyObject_GC_Track() object is not valid");
2155 }
2156 return 0;
2157}
Victor Stinnera5447732019-10-10 09:32:13 +02002158#endif
Victor Stinner1b184552019-10-08 00:09:31 +02002159
2160
Neil Schemenauer43411b52001-08-30 00:05:51 +00002161/* extension modules might be compiled with GC support so these
2162 functions must always be available */
2163
2164void
Victor Stinnera42de742018-11-22 10:25:22 +01002165PyObject_GC_Track(void *op_raw)
Neil Schemenauer43411b52001-08-30 00:05:51 +00002166{
Victor Stinnera42de742018-11-22 10:25:22 +01002167 PyObject *op = _PyObject_CAST(op_raw);
Victor Stinner271753a2018-11-22 01:02:54 +01002168 if (_PyObject_GC_IS_TRACKED(op)) {
2169 _PyObject_ASSERT_FAILED_MSG(op,
2170 "object already tracked "
2171 "by the garbage collector");
2172 }
Victor Stinnera42de742018-11-22 10:25:22 +01002173 _PyObject_GC_TRACK(op);
Victor Stinner1b184552019-10-08 00:09:31 +02002174
2175#ifdef Py_DEBUG
2176 /* Check that the object is valid: validate objects traversed
2177 by tp_traverse() */
2178 traverseproc traverse = Py_TYPE(op)->tp_traverse;
2179 (void)traverse(op, visit_validate, op);
2180#endif
Neil Schemenauer43411b52001-08-30 00:05:51 +00002181}
2182
Neil Schemenauerfec4eb12002-04-12 02:41:03 +00002183void
Victor Stinnera42de742018-11-22 10:25:22 +01002184PyObject_GC_UnTrack(void *op_raw)
Neil Schemenauer43411b52001-08-30 00:05:51 +00002185{
Victor Stinnera42de742018-11-22 10:25:22 +01002186 PyObject *op = _PyObject_CAST(op_raw);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002187 /* Obscure: the Py_TRASHCAN mechanism requires that we be able to
2188 * call PyObject_GC_UnTrack twice on an object.
2189 */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002190 if (_PyObject_GC_IS_TRACKED(op)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002191 _PyObject_GC_UNTRACK(op);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002192 }
Neil Schemenauer43411b52001-08-30 00:05:51 +00002193}
2194
Hai Shi675d9a32020-04-15 02:11:20 +08002195int
2196PyObject_IS_GC(PyObject *obj)
2197{
2198 return _PyObject_IS_GC(obj);
2199}
2200
Victor Stinnerdb067af2014-05-02 22:31:14 +02002201static PyObject *
2202_PyObject_GC_Alloc(int use_calloc, size_t basicsize)
Neil Schemenauer43411b52001-08-30 00:05:51 +00002203{
Victor Stinner67e0de62019-11-20 11:48:18 +01002204 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner72474072019-11-20 12:25:50 +01002205 GCState *gcstate = &tstate->interp->gc;
Victor Stinner2e969062019-11-20 01:49:32 +01002206 if (basicsize > PY_SSIZE_T_MAX - sizeof(PyGC_Head)) {
Victor Stinner67e0de62019-11-20 11:48:18 +01002207 return _PyErr_NoMemory(tstate);
Victor Stinner2e969062019-11-20 01:49:32 +01002208 }
2209 size_t size = sizeof(PyGC_Head) + basicsize;
2210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002211 PyGC_Head *g;
Victor Stinner2e969062019-11-20 01:49:32 +01002212 if (use_calloc) {
Victor Stinnerdb067af2014-05-02 22:31:14 +02002213 g = (PyGC_Head *)PyObject_Calloc(1, size);
Victor Stinner2e969062019-11-20 01:49:32 +01002214 }
2215 else {
Victor Stinnerdb067af2014-05-02 22:31:14 +02002216 g = (PyGC_Head *)PyObject_Malloc(size);
Victor Stinner2e969062019-11-20 01:49:32 +01002217 }
2218 if (g == NULL) {
Victor Stinner67e0de62019-11-20 11:48:18 +01002219 return _PyErr_NoMemory(tstate);
Victor Stinner2e969062019-11-20 01:49:32 +01002220 }
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002221 assert(((uintptr_t)g & 3) == 0); // g must be aligned 4bytes boundary
Victor Stinner2e969062019-11-20 01:49:32 +01002222
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002223 g->_gc_next = 0;
2224 g->_gc_prev = 0;
Victor Stinner67e0de62019-11-20 11:48:18 +01002225 gcstate->generations[0].count++; /* number of allocated GC objects */
2226 if (gcstate->generations[0].count > gcstate->generations[0].threshold &&
2227 gcstate->enabled &&
2228 gcstate->generations[0].threshold &&
2229 !gcstate->collecting &&
2230 !_PyErr_Occurred(tstate))
Victor Stinner2e969062019-11-20 01:49:32 +01002231 {
Victor Stinner67e0de62019-11-20 11:48:18 +01002232 gcstate->collecting = 1;
Victor Stinner8b341482020-10-30 17:00:00 +01002233 gc_collect_generations(tstate);
Victor Stinner67e0de62019-11-20 11:48:18 +01002234 gcstate->collecting = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002235 }
Victor Stinner2e969062019-11-20 01:49:32 +01002236 PyObject *op = FROM_GC(g);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002237 return op;
Neil Schemenauer43411b52001-08-30 00:05:51 +00002238}
2239
2240PyObject *
Victor Stinnerdb067af2014-05-02 22:31:14 +02002241_PyObject_GC_Malloc(size_t basicsize)
2242{
2243 return _PyObject_GC_Alloc(0, basicsize);
2244}
2245
2246PyObject *
2247_PyObject_GC_Calloc(size_t basicsize)
2248{
2249 return _PyObject_GC_Alloc(1, basicsize);
2250}
2251
2252PyObject *
Neil Schemenauer43411b52001-08-30 00:05:51 +00002253_PyObject_GC_New(PyTypeObject *tp)
2254{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002255 PyObject *op = _PyObject_GC_Malloc(_PyObject_SIZE(tp));
Victor Stinner04fc4f22020-06-16 01:28:07 +02002256 if (op == NULL) {
2257 return NULL;
2258 }
2259 _PyObject_Init(op, tp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002260 return op;
Neil Schemenauer43411b52001-08-30 00:05:51 +00002261}
2262
2263PyVarObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00002264_PyObject_GC_NewVar(PyTypeObject *tp, Py_ssize_t nitems)
Neil Schemenauer43411b52001-08-30 00:05:51 +00002265{
Victor Stinner5d1866c2013-07-08 22:17:52 +02002266 size_t size;
2267 PyVarObject *op;
2268
2269 if (nitems < 0) {
2270 PyErr_BadInternalCall();
2271 return NULL;
2272 }
2273 size = _PyObject_VAR_SIZE(tp, nitems);
2274 op = (PyVarObject *) _PyObject_GC_Malloc(size);
Victor Stinner04fc4f22020-06-16 01:28:07 +02002275 if (op == NULL) {
2276 return NULL;
2277 }
2278 _PyObject_InitVar(op, tp, nitems);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002279 return op;
Neil Schemenauer43411b52001-08-30 00:05:51 +00002280}
2281
2282PyVarObject *
Martin v. Löwis41290682006-02-16 14:56:14 +00002283_PyObject_GC_Resize(PyVarObject *op, Py_ssize_t nitems)
Neil Schemenauer43411b52001-08-30 00:05:51 +00002284{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002285 const size_t basicsize = _PyObject_VAR_SIZE(Py_TYPE(op), nitems);
Victor Stinnera4b2bc72018-10-26 18:00:13 +02002286 _PyObject_ASSERT((PyObject *)op, !_PyObject_GC_IS_TRACKED(op));
2287 if (basicsize > PY_SSIZE_T_MAX - sizeof(PyGC_Head)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002288 return (PyVarObject *)PyErr_NoMemory();
Victor Stinnera4b2bc72018-10-26 18:00:13 +02002289 }
2290
2291 PyGC_Head *g = AS_GC(op);
Victor Stinner32bd68c2020-12-01 10:37:39 +01002292 g = (PyGC_Head *)PyObject_Realloc(g, sizeof(PyGC_Head) + basicsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002293 if (g == NULL)
2294 return (PyVarObject *)PyErr_NoMemory();
2295 op = (PyVarObject *) FROM_GC(g);
Victor Stinner60ac6ed2020-02-07 23:18:08 +01002296 Py_SET_SIZE(op, nitems);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002297 return op;
Neil Schemenauer43411b52001-08-30 00:05:51 +00002298}
2299
2300void
Neil Schemenauerfec4eb12002-04-12 02:41:03 +00002301PyObject_GC_Del(void *op)
Neil Schemenauer43411b52001-08-30 00:05:51 +00002302{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002303 PyGC_Head *g = AS_GC(op);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002304 if (_PyObject_GC_IS_TRACKED(op)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002305 gc_list_remove(g);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002306 }
Victor Stinner1bcc32f2020-06-10 20:08:26 +02002307 GCState *gcstate = get_gc_state();
Victor Stinner67e0de62019-11-20 11:48:18 +01002308 if (gcstate->generations[0].count > 0) {
2309 gcstate->generations[0].count--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002310 }
Victor Stinner32bd68c2020-12-01 10:37:39 +01002311 PyObject_Free(g);
Neil Schemenauer43411b52001-08-30 00:05:51 +00002312}
Pablo Galindof13072b2020-04-11 01:21:54 +01002313
2314int
2315PyObject_GC_IsTracked(PyObject* obj)
2316{
Hai Shi675d9a32020-04-15 02:11:20 +08002317 if (_PyObject_IS_GC(obj) && _PyObject_GC_IS_TRACKED(obj)) {
Pablo Galindof13072b2020-04-11 01:21:54 +01002318 return 1;
2319 }
2320 return 0;
2321}
2322
2323int
2324PyObject_GC_IsFinalized(PyObject *obj)
2325{
Hai Shi675d9a32020-04-15 02:11:20 +08002326 if (_PyObject_IS_GC(obj) && _PyGCHead_FINALIZED(AS_GC(obj))) {
Pablo Galindof13072b2020-04-11 01:21:54 +01002327 return 1;
2328 }
2329 return 0;
2330}