blob: a44752b1cc4da7ff5346e2899a50d7fc528e3e6e [file] [log] [blame]
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001/*
Tim Peters88396172002-06-30 17:56:40 +00002
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00003 Reference Cycle Garbage Collection
4 ==================================
5
Neil Schemenauerb2c2c9e2000-10-04 16:34:09 +00006 Neil Schemenauer <nas@arctrix.com>
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00007
8 Based on a post on the python-dev list. Ideas from Guido van Rossum,
9 Eric Tiedemann, and various others.
10
Neil Schemenauer43411b52001-08-30 00:05:51 +000011 http://www.arctrix.com/nas/python/gc/
Neil Schemenauera7024e92008-07-15 19:24:01 +000012
13 The following mailing list threads provide a historical perspective on
14 the design of this module. Note that a fair amount of refinement has
15 occurred since those discussions.
16
17 http://mail.python.org/pipermail/python-dev/2000-March/002385.html
18 http://mail.python.org/pipermail/python-dev/2000-March/002434.html
19 http://mail.python.org/pipermail/python-dev/2000-March/002497.html
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000020
21 For a highlevel view of the collection process, read the collect
22 function.
23
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000024*/
25
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000026#include "Python.h"
Victor Stinner27e2d1f2018-11-01 00:52:28 +010027#include "pycore_context.h"
Victor Stinner444b39b2019-11-20 01:18:11 +010028#include "pycore_initconfig.h"
Victor Stinnere5014be2020-04-14 17:52:15 +020029#include "pycore_interp.h" // PyInterpreterState.gc
Victor Stinnerbcda8f12018-11-21 22:27:47 +010030#include "pycore_object.h"
Victor Stinner2e969062019-11-20 01:49:32 +010031#include "pycore_pyerrors.h"
Victor Stinnere5014be2020-04-14 17:52:15 +020032#include "pycore_pystate.h" // _PyThreadState_GET()
Łukasz Langaa785c872016-09-09 17:37:37 -070033#include "pydtrace.h"
Victor Stinnere5014be2020-04-14 17:52:15 +020034#include "pytime.h" // _PyTime_GetMonotonicClock()
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000035
Victor Stinner67e0de62019-11-20 11:48:18 +010036typedef struct _gc_runtime_state GCState;
37
Serhiy Storchaka93260282017-02-04 11:19:59 +020038/*[clinic input]
39module gc
40[clinic start generated code]*/
41/*[clinic end generated code: output=da39a3ee5e6b4b0d input=b5c9690ecc842d79]*/
42
Pablo Galindo320dd502019-10-10 22:45:17 +010043
44#ifdef Py_DEBUG
45# define GC_DEBUG
46#endif
INADA Naoki5ac9e6e2018-07-10 17:19:53 +090047
48#define GC_NEXT _PyGCHead_NEXT
49#define GC_PREV _PyGCHead_PREV
50
51// update_refs() set this bit for all objects in current generation.
52// subtract_refs() and move_unreachable() uses this to distinguish
53// visited object is in GCing or not.
54//
55// move_unreachable() removes this flag from reachable objects.
56// Only unreachable objects have this flag.
57//
58// No objects in interpreter have this flag after GC ends.
59#define PREV_MASK_COLLECTING _PyGC_PREV_MASK_COLLECTING
60
61// Lowest bit of _gc_next is used for UNREACHABLE flag.
62//
63// This flag represents the object is in unreachable list in move_unreachable()
64//
65// Although this flag is used only in move_unreachable(), move_unreachable()
66// doesn't clear this flag to skip unnecessary iteration.
67// move_legacy_finalizers() removes this flag instead.
68// Between them, unreachable list is not normal list and we can not use
69// most gc_list_* functions for it.
70#define NEXT_MASK_UNREACHABLE (1)
71
Victor Stinner626bff82018-10-25 17:31:10 +020072/* Get an object's GC head */
73#define AS_GC(o) ((PyGC_Head *)(o)-1)
74
75/* Get the object given the GC head */
76#define FROM_GC(g) ((PyObject *)(((PyGC_Head *)g)+1))
77
INADA Naoki5ac9e6e2018-07-10 17:19:53 +090078static inline int
79gc_is_collecting(PyGC_Head *g)
80{
81 return (g->_gc_prev & PREV_MASK_COLLECTING) != 0;
82}
83
84static inline void
85gc_clear_collecting(PyGC_Head *g)
86{
87 g->_gc_prev &= ~PREV_MASK_COLLECTING;
88}
89
90static inline Py_ssize_t
91gc_get_refs(PyGC_Head *g)
92{
93 return (Py_ssize_t)(g->_gc_prev >> _PyGC_PREV_SHIFT);
94}
95
96static inline void
97gc_set_refs(PyGC_Head *g, Py_ssize_t refs)
98{
99 g->_gc_prev = (g->_gc_prev & ~_PyGC_PREV_MASK)
100 | ((uintptr_t)(refs) << _PyGC_PREV_SHIFT);
101}
102
103static inline void
104gc_reset_refs(PyGC_Head *g, Py_ssize_t refs)
105{
106 g->_gc_prev = (g->_gc_prev & _PyGC_PREV_MASK_FINALIZED)
107 | PREV_MASK_COLLECTING
108 | ((uintptr_t)(refs) << _PyGC_PREV_SHIFT);
109}
110
111static inline void
112gc_decref(PyGC_Head *g)
113{
Victor Stinner626bff82018-10-25 17:31:10 +0200114 _PyObject_ASSERT_WITH_MSG(FROM_GC(g),
115 gc_get_refs(g) > 0,
116 "refcount is too small");
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900117 g->_gc_prev -= 1 << _PyGC_PREV_SHIFT;
118}
119
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000120/* set for debugging information */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121#define DEBUG_STATS (1<<0) /* print collection statistics */
122#define DEBUG_COLLECTABLE (1<<1) /* print collectable objects */
123#define DEBUG_UNCOLLECTABLE (1<<2) /* print uncollectable objects */
124#define DEBUG_SAVEALL (1<<5) /* save all garbage in gc.garbage */
125#define DEBUG_LEAK DEBUG_COLLECTABLE | \
126 DEBUG_UNCOLLECTABLE | \
127 DEBUG_SAVEALL
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000128
Victor Stinner67e0de62019-11-20 11:48:18 +0100129#define GEN_HEAD(gcstate, n) (&(gcstate)->generations[n].head)
Antoine Pitroud4156c12012-10-30 22:43:19 +0100130
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600131void
Victor Stinner72474072019-11-20 12:25:50 +0100132_PyGC_InitState(GCState *gcstate)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600133{
Victor Stinner67e0de62019-11-20 11:48:18 +0100134 gcstate->enabled = 1; /* automatic collection enabled? */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600135
Victor Stinner67e0de62019-11-20 11:48:18 +0100136#define _GEN_HEAD(n) GEN_HEAD(gcstate, n)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600137 struct gc_generation generations[NUM_GENERATIONS] = {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900138 /* PyGC_Head, threshold, count */
139 {{(uintptr_t)_GEN_HEAD(0), (uintptr_t)_GEN_HEAD(0)}, 700, 0},
140 {{(uintptr_t)_GEN_HEAD(1), (uintptr_t)_GEN_HEAD(1)}, 10, 0},
141 {{(uintptr_t)_GEN_HEAD(2), (uintptr_t)_GEN_HEAD(2)}, 10, 0},
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600142 };
143 for (int i = 0; i < NUM_GENERATIONS; i++) {
Victor Stinner67e0de62019-11-20 11:48:18 +0100144 gcstate->generations[i] = generations[i];
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600145 };
Victor Stinner67e0de62019-11-20 11:48:18 +0100146 gcstate->generation0 = GEN_HEAD(gcstate, 0);
brainfvckc75edab2017-10-16 12:49:41 -0700147 struct gc_generation permanent_generation = {
Victor Stinner67e0de62019-11-20 11:48:18 +0100148 {(uintptr_t)&gcstate->permanent_generation.head,
149 (uintptr_t)&gcstate->permanent_generation.head}, 0, 0
brainfvckc75edab2017-10-16 12:49:41 -0700150 };
Victor Stinner67e0de62019-11-20 11:48:18 +0100151 gcstate->permanent_generation = permanent_generation;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600152}
Antoine Pitroud4156c12012-10-30 22:43:19 +0100153
Victor Stinner444b39b2019-11-20 01:18:11 +0100154
155PyStatus
Victor Stinner01b1cc12019-11-20 02:27:56 +0100156_PyGC_Init(PyThreadState *tstate)
Victor Stinner444b39b2019-11-20 01:18:11 +0100157{
Victor Stinner72474072019-11-20 12:25:50 +0100158 GCState *gcstate = &tstate->interp->gc;
Victor Stinner67e0de62019-11-20 11:48:18 +0100159 if (gcstate->garbage == NULL) {
160 gcstate->garbage = PyList_New(0);
161 if (gcstate->garbage == NULL) {
Victor Stinner444b39b2019-11-20 01:18:11 +0100162 return _PyStatus_NO_MEMORY();
163 }
164 }
165 return _PyStatus_OK();
166}
167
168
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900169/*
170_gc_prev values
171---------------
Neil Schemenauer43411b52001-08-30 00:05:51 +0000172
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900173Between collections, _gc_prev is used for doubly linked list.
Tim Peters6fc13d92002-07-02 18:12:35 +0000174
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900175Lowest two bits of _gc_prev are used for flags.
176PREV_MASK_COLLECTING is used only while collecting and cleared before GC ends
177or _PyObject_GC_UNTRACK() is called.
Tim Peters6fc13d92002-07-02 18:12:35 +0000178
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900179During a collection, _gc_prev is temporary used for gc_refs, and the gc list
180is singly linked until _gc_prev is restored.
Tim Peters6fc13d92002-07-02 18:12:35 +0000181
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900182gc_refs
Tim Peters6fc13d92002-07-02 18:12:35 +0000183 At the start of a collection, update_refs() copies the true refcount
184 to gc_refs, for each object in the generation being collected.
185 subtract_refs() then adjusts gc_refs so that it equals the number of
186 times an object is referenced directly from outside the generation
187 being collected.
Tim Peters6fc13d92002-07-02 18:12:35 +0000188
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900189PREV_MASK_COLLECTING
190 Objects in generation being collected are marked PREV_MASK_COLLECTING in
191 update_refs().
192
193
194_gc_next values
195---------------
196
197_gc_next takes these values:
198
1990
200 The object is not tracked
201
202!= 0
203 Pointer to the next object in the GC list.
204 Additionally, lowest bit is used temporary for
205 NEXT_MASK_UNREACHABLE flag described below.
206
207NEXT_MASK_UNREACHABLE
Tim Peters6fc13d92002-07-02 18:12:35 +0000208 move_unreachable() then moves objects not reachable (whether directly or
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900209 indirectly) from outside the generation into an "unreachable" set and
210 set this flag.
Tim Peters6fc13d92002-07-02 18:12:35 +0000211
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900212 Objects that are found to be reachable have gc_refs set to 1.
213 When this flag is set for the reachable object, the object must be in
214 "unreachable" set.
215 The flag is unset and the object is moved back to "reachable" set.
216
217 move_legacy_finalizers() will remove this flag from "unreachable" set.
Tim Peters6fc13d92002-07-02 18:12:35 +0000218*/
Neil Schemenauera2b11ec2002-05-21 15:53:24 +0000219
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000220/*** list functions ***/
221
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900222static inline void
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000223gc_list_init(PyGC_Head *list)
224{
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900225 // List header must not have flags.
226 // We can assign pointer by simple cast.
227 list->_gc_prev = (uintptr_t)list;
228 list->_gc_next = (uintptr_t)list;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000229}
230
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900231static inline int
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000232gc_list_is_empty(PyGC_Head *list)
233{
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900234 return (list->_gc_next == (uintptr_t)list);
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000235}
236
Tim Peterse2d59182004-11-01 01:39:08 +0000237/* Append `node` to `list`. */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900238static inline void
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000239gc_list_append(PyGC_Head *node, PyGC_Head *list)
240{
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900241 PyGC_Head *last = (PyGC_Head *)list->_gc_prev;
242
243 // last <-> node
244 _PyGCHead_SET_PREV(node, last);
245 _PyGCHead_SET_NEXT(last, node);
246
247 // node <-> list
248 _PyGCHead_SET_NEXT(node, list);
249 list->_gc_prev = (uintptr_t)node;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000250}
251
Tim Peterse2d59182004-11-01 01:39:08 +0000252/* Remove `node` from the gc list it's currently in. */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900253static inline void
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000254gc_list_remove(PyGC_Head *node)
255{
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900256 PyGC_Head *prev = GC_PREV(node);
257 PyGC_Head *next = GC_NEXT(node);
258
259 _PyGCHead_SET_NEXT(prev, next);
260 _PyGCHead_SET_PREV(next, prev);
261
262 node->_gc_next = 0; /* object is not currently tracked */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000263}
264
Tim Peterse2d59182004-11-01 01:39:08 +0000265/* Move `node` from the gc list it's currently in (which is not explicitly
266 * named here) to the end of `list`. This is semantically the same as
267 * gc_list_remove(node) followed by gc_list_append(node, list).
268 */
269static void
270gc_list_move(PyGC_Head *node, PyGC_Head *list)
271{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 /* Unlink from current list. */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900273 PyGC_Head *from_prev = GC_PREV(node);
274 PyGC_Head *from_next = GC_NEXT(node);
275 _PyGCHead_SET_NEXT(from_prev, from_next);
276 _PyGCHead_SET_PREV(from_next, from_prev);
277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000278 /* Relink at end of new list. */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900279 // list must not have flags. So we can skip macros.
280 PyGC_Head *to_prev = (PyGC_Head*)list->_gc_prev;
281 _PyGCHead_SET_PREV(node, to_prev);
282 _PyGCHead_SET_NEXT(to_prev, node);
283 list->_gc_prev = (uintptr_t)node;
284 _PyGCHead_SET_NEXT(node, list);
Tim Peterse2d59182004-11-01 01:39:08 +0000285}
286
287/* append list `from` onto list `to`; `from` becomes an empty list */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000288static void
289gc_list_merge(PyGC_Head *from, PyGC_Head *to)
290{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000291 assert(from != to);
292 if (!gc_list_is_empty(from)) {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900293 PyGC_Head *to_tail = GC_PREV(to);
294 PyGC_Head *from_head = GC_NEXT(from);
295 PyGC_Head *from_tail = GC_PREV(from);
296 assert(from_head != from);
297 assert(from_tail != from);
298
299 _PyGCHead_SET_NEXT(to_tail, from_head);
300 _PyGCHead_SET_PREV(from_head, to_tail);
301
302 _PyGCHead_SET_NEXT(from_tail, to);
303 _PyGCHead_SET_PREV(to, from_tail);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 }
305 gc_list_init(from);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000306}
307
Neal Norwitz7b216c52006-03-04 20:01:53 +0000308static Py_ssize_t
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000309gc_list_size(PyGC_Head *list)
310{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 PyGC_Head *gc;
312 Py_ssize_t n = 0;
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900313 for (gc = GC_NEXT(list); gc != list; gc = GC_NEXT(gc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000314 n++;
315 }
316 return n;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000317}
318
Pablo Galindo466326d2019-10-13 16:48:59 +0100319/* Walk the list and mark all objects as non-collecting */
320static inline void
321gc_list_clear_collecting(PyGC_Head *collectable)
322{
323 PyGC_Head *gc;
324 for (gc = GC_NEXT(collectable); gc != collectable; gc = GC_NEXT(gc)) {
325 gc_clear_collecting(gc);
326 }
327}
328
Tim Peters259272b2003-04-06 19:41:39 +0000329/* Append objects in a GC list to a Python list.
Pablo Galindo466326d2019-10-13 16:48:59 +0100330 * Return 0 if all OK, < 0 if error (out of memory for list)
Tim Peters259272b2003-04-06 19:41:39 +0000331 */
332static int
333append_objects(PyObject *py_list, PyGC_Head *gc_list)
334{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 PyGC_Head *gc;
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900336 for (gc = GC_NEXT(gc_list); gc != gc_list; gc = GC_NEXT(gc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000337 PyObject *op = FROM_GC(gc);
338 if (op != py_list) {
339 if (PyList_Append(py_list, op)) {
340 return -1; /* exception */
341 }
342 }
343 }
344 return 0;
Tim Peters259272b2003-04-06 19:41:39 +0000345}
346
Tim Petersea55c512019-10-18 20:59:14 -0500347// Constants for validate_list's flags argument.
348enum flagstates {collecting_clear_unreachable_clear,
349 collecting_clear_unreachable_set,
350 collecting_set_unreachable_clear,
351 collecting_set_unreachable_set};
352
Pablo Galindo320dd502019-10-10 22:45:17 +0100353#ifdef GC_DEBUG
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900354// validate_list checks list consistency. And it works as document
Tim Peters95bfc8a2019-10-13 16:47:04 -0500355// describing when flags are expected to be set / unset.
356// `head` must be a doubly-linked gc list, although it's fine (expected!) if
357// the prev and next pointers are "polluted" with flags.
358// What's checked:
359// - The `head` pointers are not polluted.
Tim Petersea55c512019-10-18 20:59:14 -0500360// - The objects' PREV_MASK_COLLECTING and NEXT_MASK_UNREACHABLE flags are all
361// `set or clear, as specified by the 'flags' argument.
Tim Peters95bfc8a2019-10-13 16:47:04 -0500362// - The prev and next pointers are mutually consistent.
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900363static void
Tim Petersea55c512019-10-18 20:59:14 -0500364validate_list(PyGC_Head *head, enum flagstates flags)
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900365{
Tim Peters95bfc8a2019-10-13 16:47:04 -0500366 assert((head->_gc_prev & PREV_MASK_COLLECTING) == 0);
367 assert((head->_gc_next & NEXT_MASK_UNREACHABLE) == 0);
Tim Petersea55c512019-10-18 20:59:14 -0500368 uintptr_t prev_value = 0, next_value = 0;
369 switch (flags) {
370 case collecting_clear_unreachable_clear:
371 break;
372 case collecting_set_unreachable_clear:
373 prev_value = PREV_MASK_COLLECTING;
374 break;
375 case collecting_clear_unreachable_set:
376 next_value = NEXT_MASK_UNREACHABLE;
377 break;
378 case collecting_set_unreachable_set:
379 prev_value = PREV_MASK_COLLECTING;
380 next_value = NEXT_MASK_UNREACHABLE;
381 break;
382 default:
383 assert(! "bad internal flags argument");
384 }
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900385 PyGC_Head *prev = head;
386 PyGC_Head *gc = GC_NEXT(head);
387 while (gc != head) {
Tim Peters95bfc8a2019-10-13 16:47:04 -0500388 PyGC_Head *trueprev = GC_PREV(gc);
389 PyGC_Head *truenext = (PyGC_Head *)(gc->_gc_next & ~NEXT_MASK_UNREACHABLE);
390 assert(truenext != NULL);
391 assert(trueprev == prev);
392 assert((gc->_gc_prev & PREV_MASK_COLLECTING) == prev_value);
393 assert((gc->_gc_next & NEXT_MASK_UNREACHABLE) == next_value);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900394 prev = gc;
Tim Peters95bfc8a2019-10-13 16:47:04 -0500395 gc = truenext;
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900396 }
397 assert(prev == GC_PREV(head));
398}
399#else
Tim Petersea55c512019-10-18 20:59:14 -0500400#define validate_list(x, y) do{}while(0)
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900401#endif
402
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000403/*** end of list stuff ***/
404
405
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900406/* Set all gc_refs = ob_refcnt. After this, gc_refs is > 0 and
407 * PREV_MASK_COLLECTING bit is set for all objects in containers.
Tim Peters88396172002-06-30 17:56:40 +0000408 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000409static void
410update_refs(PyGC_Head *containers)
411{
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900412 PyGC_Head *gc = GC_NEXT(containers);
413 for (; gc != containers; gc = GC_NEXT(gc)) {
414 gc_reset_refs(gc, Py_REFCNT(FROM_GC(gc)));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 /* Python's cyclic gc should never see an incoming refcount
416 * of 0: if something decref'ed to 0, it should have been
417 * deallocated immediately at that time.
418 * Possible cause (if the assert triggers): a tp_dealloc
419 * routine left a gc-aware object tracked during its teardown
420 * phase, and did something-- or allowed something to happen --
421 * that called back into Python. gc can trigger then, and may
422 * see the still-tracked dying object. Before this assert
423 * was added, such mistakes went on to allow gc to try to
424 * delete the object again. In a debug build, that caused
425 * a mysterious segfault, when _Py_ForgetReference tried
426 * to remove the object from the doubly-linked list of all
427 * objects a second time. In a release build, an actual
428 * double deallocation occurred, which leads to corruption
429 * of the allocator's internal bookkeeping pointers. That's
430 * so serious that maybe this should be a release-build
431 * check instead of an assert?
432 */
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200433 _PyObject_ASSERT(FROM_GC(gc), gc_get_refs(gc) != 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000435}
436
Tim Peters19b74c72002-07-01 03:52:19 +0000437/* A traversal callback for subtract_refs. */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000438static int
Victor Stinner4d5f94b2019-10-08 02:37:38 +0200439visit_decref(PyObject *op, void *parent)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000440{
Victor Stinner4d5f94b2019-10-08 02:37:38 +0200441 _PyObject_ASSERT(_PyObject_CAST(parent), !_PyObject_IsFreed(op));
Victor Stinnerd91d4de2019-09-09 17:44:59 +0200442
Hai Shi675d9a32020-04-15 02:11:20 +0800443 if (_PyObject_IS_GC(op)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000444 PyGC_Head *gc = AS_GC(op);
445 /* We're only interested in gc_refs for objects in the
446 * generation being collected, which can be recognized
447 * because only they have positive gc_refs.
448 */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900449 if (gc_is_collecting(gc)) {
450 gc_decref(gc);
451 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000452 }
453 return 0;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000454}
455
Tim Peters19b74c72002-07-01 03:52:19 +0000456/* Subtract internal references from gc_refs. After this, gc_refs is >= 0
457 * for all objects in containers, and is GC_REACHABLE for all tracked gc
458 * objects not in containers. The ones with gc_refs > 0 are directly
459 * reachable from outside containers, and so can't be collected.
460 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000461static void
462subtract_refs(PyGC_Head *containers)
463{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000464 traverseproc traverse;
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900465 PyGC_Head *gc = GC_NEXT(containers);
466 for (; gc != containers; gc = GC_NEXT(gc)) {
Victor Stinner4d5f94b2019-10-08 02:37:38 +0200467 PyObject *op = FROM_GC(gc);
468 traverse = Py_TYPE(op)->tp_traverse;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469 (void) traverse(FROM_GC(gc),
470 (visitproc)visit_decref,
Victor Stinner4d5f94b2019-10-08 02:37:38 +0200471 op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000472 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000473}
474
Tim Peters19b74c72002-07-01 03:52:19 +0000475/* A traversal callback for move_unreachable. */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000476static int
Tim Peters19b74c72002-07-01 03:52:19 +0000477visit_reachable(PyObject *op, PyGC_Head *reachable)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000478{
Hai Shi675d9a32020-04-15 02:11:20 +0800479 if (!_PyObject_IS_GC(op)) {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900480 return 0;
481 }
Tim Peters19b74c72002-07-01 03:52:19 +0000482
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900483 PyGC_Head *gc = AS_GC(op);
484 const Py_ssize_t gc_refs = gc_get_refs(gc);
485
Tim Peters1e739452019-10-21 11:21:35 -0500486 // Ignore objects in other generation.
Tim Peters95bfc8a2019-10-13 16:47:04 -0500487 // This also skips objects "to the left" of the current position in
488 // move_unreachable's scan of the 'young' list - they've already been
489 // traversed, and no longer have the PREV_MASK_COLLECTING flag.
Tim Peters1e739452019-10-21 11:21:35 -0500490 if (! gc_is_collecting(gc)) {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900491 return 0;
492 }
Tim Peters1e739452019-10-21 11:21:35 -0500493 // It would be a logic error elsewhere if the collecting flag were set on
494 // an untracked object.
495 assert(gc->_gc_next != 0);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900496
497 if (gc->_gc_next & NEXT_MASK_UNREACHABLE) {
498 /* This had gc_refs = 0 when move_unreachable got
499 * to it, but turns out it's reachable after all.
500 * Move it back to move_unreachable's 'young' list,
501 * and move_unreachable will eventually get to it
502 * again.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 */
Tim Peters95bfc8a2019-10-13 16:47:04 -0500504 // Manually unlink gc from unreachable list because the list functions
505 // don't work right in the presence of NEXT_MASK_UNREACHABLE flags.
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900506 PyGC_Head *prev = GC_PREV(gc);
507 PyGC_Head *next = (PyGC_Head*)(gc->_gc_next & ~NEXT_MASK_UNREACHABLE);
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200508 _PyObject_ASSERT(FROM_GC(prev),
509 prev->_gc_next & NEXT_MASK_UNREACHABLE);
510 _PyObject_ASSERT(FROM_GC(next),
511 next->_gc_next & NEXT_MASK_UNREACHABLE);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900512 prev->_gc_next = gc->_gc_next; // copy NEXT_MASK_UNREACHABLE
513 _PyGCHead_SET_PREV(next, prev);
514
515 gc_list_append(gc, reachable);
516 gc_set_refs(gc, 1);
517 }
518 else if (gc_refs == 0) {
519 /* This is in move_unreachable's 'young' list, but
520 * the traversal hasn't yet gotten to it. All
521 * we need to do is tell move_unreachable that it's
522 * reachable.
523 */
524 gc_set_refs(gc, 1);
525 }
526 /* Else there's nothing to do.
527 * If gc_refs > 0, it must be in move_unreachable's 'young'
528 * list, and move_unreachable will eventually get to it.
529 */
530 else {
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200531 _PyObject_ASSERT_WITH_MSG(op, gc_refs > 0, "refcount is too small");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000532 }
533 return 0;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000534}
535
Tim Peters19b74c72002-07-01 03:52:19 +0000536/* Move the unreachable objects from young to unreachable. After this,
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900537 * all objects in young don't have PREV_MASK_COLLECTING flag and
538 * unreachable have the flag.
Tim Peters19b74c72002-07-01 03:52:19 +0000539 * All objects in young after this are directly or indirectly reachable
540 * from outside the original young; and all objects in unreachable are
541 * not.
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900542 *
543 * This function restores _gc_prev pointer. young and unreachable are
544 * doubly linked list after this function.
545 * But _gc_next in unreachable list has NEXT_MASK_UNREACHABLE flag.
546 * So we can not gc_list_* functions for unreachable until we remove the flag.
Tim Peters88396172002-06-30 17:56:40 +0000547 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000548static void
Tim Peters19b74c72002-07-01 03:52:19 +0000549move_unreachable(PyGC_Head *young, PyGC_Head *unreachable)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000550{
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900551 // previous elem in the young list, used for restore gc_prev.
552 PyGC_Head *prev = young;
553 PyGC_Head *gc = GC_NEXT(young);
Tim Peters19b74c72002-07-01 03:52:19 +0000554
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900555 /* Invariants: all objects "to the left" of us in young are reachable
556 * (directly or indirectly) from outside the young list as it was at entry.
557 *
558 * All other objects from the original young "to the left" of us are in
559 * unreachable now, and have NEXT_MASK_UNREACHABLE. All objects to the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 * left of us in 'young' now have been scanned, and no objects here
561 * or to the right have been scanned yet.
562 */
Tim Peters19b74c72002-07-01 03:52:19 +0000563
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 while (gc != young) {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900565 if (gc_get_refs(gc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 /* gc is definitely reachable from outside the
567 * original 'young'. Mark it as such, and traverse
568 * its pointers to find any other objects that may
569 * be directly reachable from it. Note that the
570 * call to tp_traverse may append objects to young,
571 * so we have to wait until it returns to determine
572 * the next object to visit.
573 */
574 PyObject *op = FROM_GC(gc);
575 traverseproc traverse = Py_TYPE(op)->tp_traverse;
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200576 _PyObject_ASSERT_WITH_MSG(op, gc_get_refs(gc) > 0,
577 "refcount is too small");
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900578 // NOTE: visit_reachable may change gc->_gc_next when
579 // young->_gc_prev == gc. Don't do gc = GC_NEXT(gc) before!
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 (void) traverse(op,
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900581 (visitproc)visit_reachable,
582 (void *)young);
583 // relink gc_prev to prev element.
584 _PyGCHead_SET_PREV(gc, prev);
Quan Tian3bd0d622018-10-20 05:30:03 +0800585 // gc is not COLLECTING state after here.
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900586 gc_clear_collecting(gc);
587 prev = gc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 }
589 else {
590 /* This *may* be unreachable. To make progress,
591 * assume it is. gc isn't directly reachable from
592 * any object we've already traversed, but may be
593 * reachable from an object we haven't gotten to yet.
594 * visit_reachable will eventually move gc back into
595 * young if that's so, and we'll see it again.
596 */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900597 // Move gc to unreachable.
598 // No need to gc->next->prev = prev because it is single linked.
599 prev->_gc_next = gc->_gc_next;
600
601 // We can't use gc_list_append() here because we use
602 // NEXT_MASK_UNREACHABLE here.
603 PyGC_Head *last = GC_PREV(unreachable);
604 // NOTE: Since all objects in unreachable set has
605 // NEXT_MASK_UNREACHABLE flag, we set it unconditionally.
Tim Peters95bfc8a2019-10-13 16:47:04 -0500606 // But this may pollute the unreachable list head's 'next' pointer
607 // too. That's semantically senseless but expedient here - the
Pablo Galindo97f12672020-01-13 12:25:05 +0000608 // damage is repaired when this function ends.
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900609 last->_gc_next = (NEXT_MASK_UNREACHABLE | (uintptr_t)gc);
610 _PyGCHead_SET_PREV(gc, last);
611 gc->_gc_next = (NEXT_MASK_UNREACHABLE | (uintptr_t)unreachable);
612 unreachable->_gc_prev = (uintptr_t)gc;
613 }
614 gc = (PyGC_Head*)prev->_gc_next;
615 }
616 // young->_gc_prev must be last element remained in the list.
617 young->_gc_prev = (uintptr_t)prev;
Tim Peters95bfc8a2019-10-13 16:47:04 -0500618 // don't let the pollution of the list head's next pointer leak
619 unreachable->_gc_next &= ~NEXT_MASK_UNREACHABLE;
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900620}
621
622static void
623untrack_tuples(PyGC_Head *head)
624{
625 PyGC_Head *next, *gc = GC_NEXT(head);
626 while (gc != head) {
627 PyObject *op = FROM_GC(gc);
628 next = GC_NEXT(gc);
629 if (PyTuple_CheckExact(op)) {
630 _PyTuple_MaybeUntrack(op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000631 }
632 gc = next;
633 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000634}
635
Antoine Pitroue1ad3da2012-05-28 22:22:34 +0200636/* Try to untrack all currently tracked dictionaries */
637static void
638untrack_dicts(PyGC_Head *head)
639{
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900640 PyGC_Head *next, *gc = GC_NEXT(head);
Antoine Pitroue1ad3da2012-05-28 22:22:34 +0200641 while (gc != head) {
642 PyObject *op = FROM_GC(gc);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900643 next = GC_NEXT(gc);
644 if (PyDict_CheckExact(op)) {
Antoine Pitroue1ad3da2012-05-28 22:22:34 +0200645 _PyDict_MaybeUntrack(op);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900646 }
Antoine Pitroue1ad3da2012-05-28 22:22:34 +0200647 gc = next;
648 }
649}
650
Antoine Pitrou796564c2013-07-30 19:59:21 +0200651/* Return true if object has a pre-PEP 442 finalization method. */
Neil Schemenauera765c122001-11-01 17:35:23 +0000652static int
Antoine Pitrou796564c2013-07-30 19:59:21 +0200653has_legacy_finalizer(PyObject *op)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000654{
Victor Stinnerdaa97562020-02-07 03:37:06 +0100655 return Py_TYPE(op)->tp_del != NULL;
Neil Schemenauera765c122001-11-01 17:35:23 +0000656}
657
Antoine Pitrou796564c2013-07-30 19:59:21 +0200658/* Move the objects in unreachable with tp_del slots into `finalizers`.
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900659 *
660 * This function also removes NEXT_MASK_UNREACHABLE flag
661 * from _gc_next in unreachable.
Jeremy Hyltonce136e92003-04-04 19:59:06 +0000662 */
Neil Schemenauera765c122001-11-01 17:35:23 +0000663static void
Antoine Pitrou796564c2013-07-30 19:59:21 +0200664move_legacy_finalizers(PyGC_Head *unreachable, PyGC_Head *finalizers)
Neil Schemenauera765c122001-11-01 17:35:23 +0000665{
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900666 PyGC_Head *gc, *next;
Tim Peters95bfc8a2019-10-13 16:47:04 -0500667 assert((unreachable->_gc_next & NEXT_MASK_UNREACHABLE) == 0);
Tim Petersf6b80452003-04-07 19:21:15 +0000668
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 /* March over unreachable. Move objects with finalizers into
670 * `finalizers`.
671 */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900672 for (gc = GC_NEXT(unreachable); gc != unreachable; gc = next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 PyObject *op = FROM_GC(gc);
Jeremy Hyltonce136e92003-04-04 19:59:06 +0000674
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200675 _PyObject_ASSERT(op, gc->_gc_next & NEXT_MASK_UNREACHABLE);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900676 gc->_gc_next &= ~NEXT_MASK_UNREACHABLE;
677 next = (PyGC_Head*)gc->_gc_next;
Tim Petersf6ae7a42003-04-05 18:40:50 +0000678
Antoine Pitrou796564c2013-07-30 19:59:21 +0200679 if (has_legacy_finalizer(op)) {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900680 gc_clear_collecting(gc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000681 gc_list_move(gc, finalizers);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 }
683 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000684}
685
Pablo Galindo466326d2019-10-13 16:48:59 +0100686static inline void
687clear_unreachable_mask(PyGC_Head *unreachable)
688{
689 /* Check that the list head does not have the unreachable bit set */
690 assert(((uintptr_t)unreachable & NEXT_MASK_UNREACHABLE) == 0);
691
692 PyGC_Head *gc, *next;
Tim Peters95bfc8a2019-10-13 16:47:04 -0500693 assert((unreachable->_gc_next & NEXT_MASK_UNREACHABLE) == 0);
Pablo Galindo466326d2019-10-13 16:48:59 +0100694 for (gc = GC_NEXT(unreachable); gc != unreachable; gc = next) {
695 _PyObject_ASSERT((PyObject*)FROM_GC(gc), gc->_gc_next & NEXT_MASK_UNREACHABLE);
696 gc->_gc_next &= ~NEXT_MASK_UNREACHABLE;
697 next = (PyGC_Head*)gc->_gc_next;
698 }
Tim Petersea55c512019-10-18 20:59:14 -0500699 validate_list(unreachable, collecting_set_unreachable_clear);
Pablo Galindo466326d2019-10-13 16:48:59 +0100700}
701
Antoine Pitrou796564c2013-07-30 19:59:21 +0200702/* A traversal callback for move_legacy_finalizer_reachable. */
Tim Peters19b74c72002-07-01 03:52:19 +0000703static int
704visit_move(PyObject *op, PyGC_Head *tolist)
705{
Hai Shi675d9a32020-04-15 02:11:20 +0800706 if (_PyObject_IS_GC(op)) {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900707 PyGC_Head *gc = AS_GC(op);
708 if (gc_is_collecting(gc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000709 gc_list_move(gc, tolist);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900710 gc_clear_collecting(gc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 }
712 }
713 return 0;
Tim Peters19b74c72002-07-01 03:52:19 +0000714}
715
716/* Move objects that are reachable from finalizers, from the unreachable set
Tim Petersf6b80452003-04-07 19:21:15 +0000717 * into finalizers set.
Tim Peters19b74c72002-07-01 03:52:19 +0000718 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000719static void
Antoine Pitrou796564c2013-07-30 19:59:21 +0200720move_legacy_finalizer_reachable(PyGC_Head *finalizers)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000721{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722 traverseproc traverse;
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900723 PyGC_Head *gc = GC_NEXT(finalizers);
724 for (; gc != finalizers; gc = GC_NEXT(gc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000725 /* Note that the finalizers list may grow during this. */
726 traverse = Py_TYPE(FROM_GC(gc))->tp_traverse;
727 (void) traverse(FROM_GC(gc),
728 (visitproc)visit_move,
729 (void *)finalizers);
730 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000731}
732
Tim Petersead8b7a2004-10-30 23:09:22 +0000733/* Clear all weakrefs to unreachable objects, and if such a weakref has a
734 * callback, invoke it if necessary. Note that it's possible for such
735 * weakrefs to be outside the unreachable set -- indeed, those are precisely
736 * the weakrefs whose callbacks must be invoked. See gc_weakref.txt for
737 * overview & some details. Some weakrefs with callbacks may be reclaimed
738 * directly by this routine; the number reclaimed is the return value. Other
739 * weakrefs with callbacks may be moved into the `old` generation. Objects
740 * moved into `old` have gc_refs set to GC_REACHABLE; the objects remaining in
741 * unreachable are left at GC_TENTATIVELY_UNREACHABLE. When this returns,
742 * no object in `unreachable` is weakly referenced anymore.
Tim Peters403a2032003-11-20 21:21:46 +0000743 */
744static int
Tim Petersead8b7a2004-10-30 23:09:22 +0000745handle_weakrefs(PyGC_Head *unreachable, PyGC_Head *old)
Tim Peters403a2032003-11-20 21:21:46 +0000746{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 PyGC_Head *gc;
748 PyObject *op; /* generally FROM_GC(gc) */
749 PyWeakReference *wr; /* generally a cast of op */
750 PyGC_Head wrcb_to_call; /* weakrefs with callbacks to call */
751 PyGC_Head *next;
752 int num_freed = 0;
Tim Peters403a2032003-11-20 21:21:46 +0000753
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000754 gc_list_init(&wrcb_to_call);
Tim Peters403a2032003-11-20 21:21:46 +0000755
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000756 /* Clear all weakrefs to the objects in unreachable. If such a weakref
757 * also has a callback, move it into `wrcb_to_call` if the callback
758 * needs to be invoked. Note that we cannot invoke any callbacks until
759 * all weakrefs to unreachable objects are cleared, lest the callback
760 * resurrect an unreachable object via a still-active weakref. We
761 * make another pass over wrcb_to_call, invoking callbacks, after this
762 * pass completes.
763 */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900764 for (gc = GC_NEXT(unreachable); gc != unreachable; gc = next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 PyWeakReference **wrlist;
Tim Petersead8b7a2004-10-30 23:09:22 +0000766
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 op = FROM_GC(gc);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900768 next = GC_NEXT(gc);
Tim Petersead8b7a2004-10-30 23:09:22 +0000769
Neil Schemenauerbcda4602019-09-30 10:06:45 -0700770 if (PyWeakref_Check(op)) {
771 /* A weakref inside the unreachable set must be cleared. If we
772 * allow its callback to execute inside delete_garbage(), it
773 * could expose objects that have tp_clear already called on
774 * them. Or, it could resurrect unreachable objects. One way
775 * this can happen is if some container objects do not implement
776 * tp_traverse. Then, wr_object can be outside the unreachable
777 * set but can be deallocated as a result of breaking the
778 * reference cycle. If we don't clear the weakref, the callback
779 * will run and potentially cause a crash. See bpo-38006 for
780 * one example.
781 */
782 _PyWeakref_ClearRef((PyWeakReference *)op);
783 }
784
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 if (! PyType_SUPPORTS_WEAKREFS(Py_TYPE(op)))
786 continue;
Tim Petersead8b7a2004-10-30 23:09:22 +0000787
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 /* It supports weakrefs. Does it have any? */
789 wrlist = (PyWeakReference **)
Victor Stinner38aefc52020-04-06 14:07:02 +0200790 _PyObject_GET_WEAKREFS_LISTPTR(op);
Tim Petersead8b7a2004-10-30 23:09:22 +0000791
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 /* `op` may have some weakrefs. March over the list, clear
793 * all the weakrefs, and move the weakrefs with callbacks
794 * that must be called into wrcb_to_call.
795 */
796 for (wr = *wrlist; wr != NULL; wr = *wrlist) {
797 PyGC_Head *wrasgc; /* AS_GC(wr) */
Tim Petersead8b7a2004-10-30 23:09:22 +0000798
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 /* _PyWeakref_ClearRef clears the weakref but leaves
800 * the callback pointer intact. Obscure: it also
801 * changes *wrlist.
802 */
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200803 _PyObject_ASSERT((PyObject *)wr, wr->wr_object == op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 _PyWeakref_ClearRef(wr);
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200805 _PyObject_ASSERT((PyObject *)wr, wr->wr_object == Py_None);
806 if (wr->wr_callback == NULL) {
807 /* no callback */
808 continue;
809 }
Tim Petersead8b7a2004-10-30 23:09:22 +0000810
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200811 /* Headache time. `op` is going away, and is weakly referenced by
812 * `wr`, which has a callback. Should the callback be invoked? If wr
813 * is also trash, no:
814 *
815 * 1. There's no need to call it. The object and the weakref are
816 * both going away, so it's legitimate to pretend the weakref is
817 * going away first. The user has to ensure a weakref outlives its
818 * referent if they want a guarantee that the wr callback will get
819 * invoked.
820 *
821 * 2. It may be catastrophic to call it. If the callback is also in
822 * cyclic trash (CT), then although the CT is unreachable from
823 * outside the current generation, CT may be reachable from the
824 * callback. Then the callback could resurrect insane objects.
825 *
826 * Since the callback is never needed and may be unsafe in this case,
827 * wr is simply left in the unreachable set. Note that because we
828 * already called _PyWeakref_ClearRef(wr), its callback will never
829 * trigger.
830 *
831 * OTOH, if wr isn't part of CT, we should invoke the callback: the
832 * weakref outlived the trash. Note that since wr isn't CT in this
833 * case, its callback can't be CT either -- wr acted as an external
834 * root to this generation, and therefore its callback did too. So
835 * nothing in CT is reachable from the callback either, so it's hard
836 * to imagine how calling it later could create a problem for us. wr
837 * is moved to wrcb_to_call in this case.
838 */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900839 if (gc_is_collecting(AS_GC(wr))) {
Neil Schemenauerbcda4602019-09-30 10:06:45 -0700840 /* it should already have been cleared above */
841 assert(wr->wr_object == Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842 continue;
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900843 }
Tim Peterscc2a8662004-10-31 22:12:43 +0000844
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000845 /* Create a new reference so that wr can't go away
846 * before we can process it again.
847 */
848 Py_INCREF(wr);
Tim Petersead8b7a2004-10-30 23:09:22 +0000849
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 /* Move wr to wrcb_to_call, for the next pass. */
851 wrasgc = AS_GC(wr);
852 assert(wrasgc != next); /* wrasgc is reachable, but
853 next isn't, so they can't
854 be the same */
855 gc_list_move(wrasgc, &wrcb_to_call);
856 }
857 }
Tim Petersead8b7a2004-10-30 23:09:22 +0000858
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000859 /* Invoke the callbacks we decided to honor. It's safe to invoke them
860 * because they can't reference unreachable objects.
861 */
862 while (! gc_list_is_empty(&wrcb_to_call)) {
863 PyObject *temp;
864 PyObject *callback;
Tim Petersead8b7a2004-10-30 23:09:22 +0000865
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900866 gc = (PyGC_Head*)wrcb_to_call._gc_next;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000867 op = FROM_GC(gc);
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200868 _PyObject_ASSERT(op, PyWeakref_Check(op));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000869 wr = (PyWeakReference *)op;
870 callback = wr->wr_callback;
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200871 _PyObject_ASSERT(op, callback != NULL);
Tim Petersead8b7a2004-10-30 23:09:22 +0000872
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 /* copy-paste of weakrefobject.c's handle_callback() */
Petr Viktorinffd97532020-02-11 17:46:57 +0100874 temp = PyObject_CallOneArg(callback, (PyObject *)wr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 if (temp == NULL)
876 PyErr_WriteUnraisable(callback);
877 else
878 Py_DECREF(temp);
Tim Petersead8b7a2004-10-30 23:09:22 +0000879
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000880 /* Give up the reference we created in the first pass. When
881 * op's refcount hits 0 (which it may or may not do right now),
882 * op's tp_dealloc will decref op->wr_callback too. Note
883 * that the refcount probably will hit 0 now, and because this
884 * weakref was reachable to begin with, gc didn't already
885 * add it to its count of freed objects. Example: a reachable
886 * weak value dict maps some key to this reachable weakref.
887 * The callback removes this key->weakref mapping from the
888 * dict, leaving no other references to the weakref (excepting
889 * ours).
890 */
891 Py_DECREF(op);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900892 if (wrcb_to_call._gc_next == (uintptr_t)gc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 /* object is still alive -- move it */
894 gc_list_move(gc, old);
895 }
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900896 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 ++num_freed;
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900898 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000899 }
Tim Petersead8b7a2004-10-30 23:09:22 +0000900
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 return num_freed;
Tim Peters403a2032003-11-20 21:21:46 +0000902}
903
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000904static void
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200905debug_cycle(const char *msg, PyObject *op)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000906{
Victor Stinner499dfcf2011-03-21 13:26:24 +0100907 PySys_FormatStderr("gc: %s <%s %p>\n",
908 msg, Py_TYPE(op)->tp_name, op);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000909}
910
Antoine Pitrou796564c2013-07-30 19:59:21 +0200911/* Handle uncollectable garbage (cycles with tp_del slots, and stuff reachable
Tim Petersbf384c22003-04-06 00:11:39 +0000912 * only from such cycles).
Tim Petersf6b80452003-04-07 19:21:15 +0000913 * If DEBUG_SAVEALL, all objects in finalizers are appended to the module
914 * garbage list (a Python list), else only the objects in finalizers with
915 * __del__ methods are appended to garbage. All objects in finalizers are
916 * merged into the old list regardless.
Tim Petersbf384c22003-04-06 00:11:39 +0000917 */
Serhiy Storchaka301e3cc2018-05-24 15:19:29 +0300918static void
Victor Stinner2e969062019-11-20 01:49:32 +0100919handle_legacy_finalizers(PyThreadState *tstate,
Victor Stinner67e0de62019-11-20 11:48:18 +0100920 GCState *gcstate,
Victor Stinner9db03242019-04-26 02:32:01 +0200921 PyGC_Head *finalizers, PyGC_Head *old)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000922{
Victor Stinner2e969062019-11-20 01:49:32 +0100923 assert(!_PyErr_Occurred(tstate));
Victor Stinner67e0de62019-11-20 11:48:18 +0100924 assert(gcstate->garbage != NULL);
Victor Stinner9db03242019-04-26 02:32:01 +0200925
926 PyGC_Head *gc = GC_NEXT(finalizers);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900927 for (; gc != finalizers; gc = GC_NEXT(gc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 PyObject *op = FROM_GC(gc);
Tim Petersf6b80452003-04-07 19:21:15 +0000929
Victor Stinner67e0de62019-11-20 11:48:18 +0100930 if ((gcstate->debug & DEBUG_SAVEALL) || has_legacy_finalizer(op)) {
931 if (PyList_Append(gcstate->garbage, op) < 0) {
Victor Stinner2e969062019-11-20 01:49:32 +0100932 _PyErr_Clear(tstate);
Serhiy Storchaka301e3cc2018-05-24 15:19:29 +0300933 break;
Serhiy Storchakac4653c92018-05-29 18:50:10 +0300934 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 }
936 }
Tim Petersf6b80452003-04-07 19:21:15 +0000937
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000938 gc_list_merge(finalizers, old);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000939}
940
Tim Peters5fbc7b12014-05-08 17:42:19 -0500941/* Run first-time finalizers (if any) on all the objects in collectable.
942 * Note that this may remove some (or even all) of the objects from the
943 * list, due to refcounts falling to 0.
944 */
Antoine Pitrou796564c2013-07-30 19:59:21 +0200945static void
Victor Stinner67e0de62019-11-20 11:48:18 +0100946finalize_garbage(PyThreadState *tstate, PyGC_Head *collectable)
Antoine Pitrou796564c2013-07-30 19:59:21 +0200947{
948 destructor finalize;
Tim Peters5fbc7b12014-05-08 17:42:19 -0500949 PyGC_Head seen;
Antoine Pitrou796564c2013-07-30 19:59:21 +0200950
Tim Peters5fbc7b12014-05-08 17:42:19 -0500951 /* While we're going through the loop, `finalize(op)` may cause op, or
952 * other objects, to be reclaimed via refcounts falling to zero. So
953 * there's little we can rely on about the structure of the input
954 * `collectable` list across iterations. For safety, we always take the
955 * first object in that list and move it to a temporary `seen` list.
956 * If objects vanish from the `collectable` and `seen` lists we don't
957 * care.
958 */
959 gc_list_init(&seen);
960
961 while (!gc_list_is_empty(collectable)) {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900962 PyGC_Head *gc = GC_NEXT(collectable);
Antoine Pitrou796564c2013-07-30 19:59:21 +0200963 PyObject *op = FROM_GC(gc);
Tim Peters5fbc7b12014-05-08 17:42:19 -0500964 gc_list_move(gc, &seen);
Antoine Pitrou796564c2013-07-30 19:59:21 +0200965 if (!_PyGCHead_FINALIZED(gc) &&
Tim Peters5fbc7b12014-05-08 17:42:19 -0500966 (finalize = Py_TYPE(op)->tp_finalize) != NULL) {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900967 _PyGCHead_SET_FINALIZED(gc);
Antoine Pitrou796564c2013-07-30 19:59:21 +0200968 Py_INCREF(op);
969 finalize(op);
Victor Stinner67e0de62019-11-20 11:48:18 +0100970 assert(!_PyErr_Occurred(tstate));
Antoine Pitrou796564c2013-07-30 19:59:21 +0200971 Py_DECREF(op);
972 }
973 }
Tim Peters5fbc7b12014-05-08 17:42:19 -0500974 gc_list_merge(&seen, collectable);
Antoine Pitrou796564c2013-07-30 19:59:21 +0200975}
976
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977/* Break reference cycles by clearing the containers involved. This is
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000978 * tricky business as the lists can be changing and we don't know which
Tim Peters19b74c72002-07-01 03:52:19 +0000979 * objects may be freed. It is possible I screwed something up here.
980 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000981static void
Victor Stinner67e0de62019-11-20 11:48:18 +0100982delete_garbage(PyThreadState *tstate, GCState *gcstate,
Victor Stinner9db03242019-04-26 02:32:01 +0200983 PyGC_Head *collectable, PyGC_Head *old)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000984{
Victor Stinner2e969062019-11-20 01:49:32 +0100985 assert(!_PyErr_Occurred(tstate));
Victor Stinner9db03242019-04-26 02:32:01 +0200986
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 while (!gc_list_is_empty(collectable)) {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900988 PyGC_Head *gc = GC_NEXT(collectable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 PyObject *op = FROM_GC(gc);
Tim Peters88396172002-06-30 17:56:40 +0000990
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200991 _PyObject_ASSERT_WITH_MSG(op, Py_REFCNT(op) > 0,
992 "refcount is too small");
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900993
Victor Stinner67e0de62019-11-20 11:48:18 +0100994 if (gcstate->debug & DEBUG_SAVEALL) {
995 assert(gcstate->garbage != NULL);
996 if (PyList_Append(gcstate->garbage, op) < 0) {
Victor Stinner2e969062019-11-20 01:49:32 +0100997 _PyErr_Clear(tstate);
Serhiy Storchakac4653c92018-05-29 18:50:10 +0300998 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 }
1000 else {
Victor Stinner9db03242019-04-26 02:32:01 +02001001 inquiry clear;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 if ((clear = Py_TYPE(op)->tp_clear) != NULL) {
1003 Py_INCREF(op);
Serhiy Storchakac4653c92018-05-29 18:50:10 +03001004 (void) clear(op);
Victor Stinner2e969062019-11-20 01:49:32 +01001005 if (_PyErr_Occurred(tstate)) {
Victor Stinner71c52e32019-05-27 08:57:14 +02001006 _PyErr_WriteUnraisableMsg("in tp_clear of",
1007 (PyObject*)Py_TYPE(op));
Serhiy Storchakac4653c92018-05-29 18:50:10 +03001008 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 Py_DECREF(op);
1010 }
1011 }
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09001012 if (GC_NEXT(collectable) == gc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001013 /* object is still alive, move it, it may die later */
Pablo Galindo466326d2019-10-13 16:48:59 +01001014 gc_clear_collecting(gc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 gc_list_move(gc, old);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 }
1017 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001018}
1019
Christian Heimesa156e092008-02-16 07:38:31 +00001020/* Clear all free lists
1021 * All free lists are cleared during the collection of the highest generation.
1022 * Allocated items in the free list may keep a pymalloc arena occupied.
1023 * Clearing the free lists may give back memory to the OS earlier.
1024 */
1025static void
1026clear_freelists(void)
1027{
Victor Stinnerae00a5a2020-04-29 02:29:20 +02001028 _PyFrame_ClearFreeList();
1029 _PyTuple_ClearFreeList();
1030 _PyFloat_ClearFreeList();
1031 _PyList_ClearFreeList();
1032 _PyDict_ClearFreeList();
1033 _PyAsyncGen_ClearFreeLists();
1034 _PyContext_ClearFreeList();
Christian Heimesa156e092008-02-16 07:38:31 +00001035}
1036
Pablo Galindo97f12672020-01-13 12:25:05 +00001037// Show stats for objects in each generations
Inada Naokibf8162c2019-08-02 16:25:29 +09001038static void
Victor Stinner67e0de62019-11-20 11:48:18 +01001039show_stats_each_generations(GCState *gcstate)
Inada Naokibf8162c2019-08-02 16:25:29 +09001040{
1041 char buf[100];
1042 size_t pos = 0;
1043
1044 for (int i = 0; i < NUM_GENERATIONS && pos < sizeof(buf); i++) {
1045 pos += PyOS_snprintf(buf+pos, sizeof(buf)-pos,
1046 " %"PY_FORMAT_SIZE_T"d",
Victor Stinner67e0de62019-11-20 11:48:18 +01001047 gc_list_size(GEN_HEAD(gcstate, i)));
Inada Naokibf8162c2019-08-02 16:25:29 +09001048 }
1049
1050 PySys_FormatStderr(
1051 "gc: objects in each generation:%s\n"
1052 "gc: objects in permanent generation: %zd\n",
Victor Stinner67e0de62019-11-20 11:48:18 +01001053 buf, gc_list_size(&gcstate->permanent_generation.head));
Inada Naokibf8162c2019-08-02 16:25:29 +09001054}
1055
Pablo Galindo97f12672020-01-13 12:25:05 +00001056/* Deduce which objects among "base" are unreachable from outside the list
Pablo Galindo466326d2019-10-13 16:48:59 +01001057 and move them to 'unreachable'. The process consist in the following steps:
1058
10591. Copy all reference counts to a different field (gc_prev is used to hold
1060 this copy to save memory).
10612. Traverse all objects in "base" and visit all referred objects using
Pablo Galindo97f12672020-01-13 12:25:05 +00001062 "tp_traverse" and for every visited object, subtract 1 to the reference
Pablo Galindo466326d2019-10-13 16:48:59 +01001063 count (the one that we copied in the previous step). After this step, all
1064 objects that can be reached directly from outside must have strictly positive
1065 reference count, while all unreachable objects must have a count of exactly 0.
Pablo Galindo97f12672020-01-13 12:25:05 +000010663. Identify all unreachable objects (the ones with 0 reference count) and move
Pablo Galindo466326d2019-10-13 16:48:59 +01001067 them to the "unreachable" list. This step also needs to move back to "base" all
1068 objects that were initially marked as unreachable but are referred transitively
1069 by the reachable objects (the ones with strictly positive reference count).
1070
1071Contracts:
1072
1073 * The "base" has to be a valid list with no mask set.
1074
1075 * The "unreachable" list must be uninitialized (this function calls
1076 gc_list_init over 'unreachable').
Tim Peters95bfc8a2019-10-13 16:47:04 -05001077
Pablo Galindo466326d2019-10-13 16:48:59 +01001078IMPORTANT: This function leaves 'unreachable' with the NEXT_MASK_UNREACHABLE
1079flag set but it does not clear it to skip unnecessary iteration. Before the
1080flag is cleared (for example, by using 'clear_unreachable_mask' function or
1081by a call to 'move_legacy_finalizers'), the 'unreachable' list is not a normal
1082list and we can not use most gc_list_* functions for it. */
1083static inline void
1084deduce_unreachable(PyGC_Head *base, PyGC_Head *unreachable) {
Tim Petersea55c512019-10-18 20:59:14 -05001085 validate_list(base, collecting_clear_unreachable_clear);
Pablo Galindo466326d2019-10-13 16:48:59 +01001086 /* Using ob_refcnt and gc_refs, calculate which objects in the
1087 * container set are reachable from outside the set (i.e., have a
1088 * refcount greater than 0 when all the references within the
1089 * set are taken into account).
1090 */
1091 update_refs(base); // gc_prev is used for gc_refs
1092 subtract_refs(base);
1093
1094 /* Leave everything reachable from outside base in base, and move
1095 * everything else (in base) to unreachable.
Pablo Galindo97f12672020-01-13 12:25:05 +00001096 *
Pablo Galindo466326d2019-10-13 16:48:59 +01001097 * NOTE: This used to move the reachable objects into a reachable
1098 * set instead. But most things usually turn out to be reachable,
Pablo Galindo97f12672020-01-13 12:25:05 +00001099 * so it's more efficient to move the unreachable things. It "sounds slick"
1100 * to move the unreachable objects, until you think about it - the reason it
1101 * pays isn't actually obvious.
1102 *
1103 * Suppose we create objects A, B, C in that order. They appear in the young
1104 * generation in the same order. If B points to A, and C to B, and C is
1105 * reachable from outside, then the adjusted refcounts will be 0, 0, and 1
1106 * respectively.
1107 *
1108 * When move_unreachable finds A, A is moved to the unreachable list. The
1109 * same for B when it's first encountered. Then C is traversed, B is moved
1110 * _back_ to the reachable list. B is eventually traversed, and then A is
1111 * moved back to the reachable list.
1112 *
1113 * So instead of not moving at all, the reachable objects B and A are moved
1114 * twice each. Why is this a win? A straightforward algorithm to move the
1115 * reachable objects instead would move A, B, and C once each.
1116 *
1117 * The key is that this dance leaves the objects in order C, B, A - it's
1118 * reversed from the original order. On all _subsequent_ scans, none of
1119 * them will move. Since most objects aren't in cycles, this can save an
1120 * unbounded number of moves across an unbounded number of later collections.
1121 * It can cost more only the first time the chain is scanned.
1122 *
1123 * Drawback: move_unreachable is also used to find out what's still trash
1124 * after finalizers may resurrect objects. In _that_ case most unreachable
1125 * objects will remain unreachable, so it would be more efficient to move
1126 * the reachable objects instead. But this is a one-time cost, probably not
1127 * worth complicating the code to speed just a little.
Pablo Galindo466326d2019-10-13 16:48:59 +01001128 */
1129 gc_list_init(unreachable);
1130 move_unreachable(base, unreachable); // gc_prev is pointer again
Tim Petersea55c512019-10-18 20:59:14 -05001131 validate_list(base, collecting_clear_unreachable_clear);
1132 validate_list(unreachable, collecting_set_unreachable_set);
Pablo Galindo466326d2019-10-13 16:48:59 +01001133}
1134
1135/* Handle objects that may have resurrected after a call to 'finalize_garbage', moving
1136 them to 'old_generation' and placing the rest on 'still_unreachable'.
1137
1138 Contracts:
1139 * After this function 'unreachable' must not be used anymore and 'still_unreachable'
1140 will contain the objects that did not resurrect.
1141
1142 * The "still_unreachable" list must be uninitialized (this function calls
1143 gc_list_init over 'still_unreachable').
1144
1145IMPORTANT: After a call to this function, the 'still_unreachable' set will have the
1146PREV_MARK_COLLECTING set, but the objects in this set are going to be removed so
1147we can skip the expense of clearing the flag to avoid extra iteration. */
1148static inline void
1149handle_resurrected_objects(PyGC_Head *unreachable, PyGC_Head* still_unreachable,
1150 PyGC_Head *old_generation)
1151{
1152 // Remove the PREV_MASK_COLLECTING from unreachable
1153 // to prepare it for a new call to 'deduce_unreachable'
1154 gc_list_clear_collecting(unreachable);
1155
1156 // After the call to deduce_unreachable, the 'still_unreachable' set will
1157 // have the PREV_MARK_COLLECTING set, but the objects are going to be
1158 // removed so we can skip the expense of clearing the flag.
1159 PyGC_Head* resurrected = unreachable;
1160 deduce_unreachable(resurrected, still_unreachable);
1161 clear_unreachable_mask(still_unreachable);
1162
1163 // Move the resurrected objects to the old generation for future collection.
1164 gc_list_merge(resurrected, old_generation);
1165}
1166
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001167/* This is the main function. Read this to understand how the
1168 * collection process works. */
Neal Norwitz7b216c52006-03-04 20:01:53 +00001169static Py_ssize_t
Victor Stinner67e0de62019-11-20 11:48:18 +01001170collect(PyThreadState *tstate, int generation,
Victor Stinner9db03242019-04-26 02:32:01 +02001171 Py_ssize_t *n_collected, Py_ssize_t *n_uncollectable, int nofail)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001172{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001173 int i;
1174 Py_ssize_t m = 0; /* # objects collected */
1175 Py_ssize_t n = 0; /* # unreachable objects that couldn't be collected */
1176 PyGC_Head *young; /* the generation we are examining */
1177 PyGC_Head *old; /* next older generation */
1178 PyGC_Head unreachable; /* non-problematic unreachable trash */
1179 PyGC_Head finalizers; /* objects with, & reachable from, __del__ */
1180 PyGC_Head *gc;
Victor Stinner7181dec2015-03-27 17:47:53 +01001181 _PyTime_t t1 = 0; /* initialize to prevent a compiler warning */
Victor Stinner72474072019-11-20 12:25:50 +01001182 GCState *gcstate = &tstate->interp->gc;
Antoine Pitrou40f6b122014-05-24 19:21:53 +02001183
Victor Stinnerd8135e92020-05-06 18:25:06 +02001184#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
1185 if (tstate->interp->config._isolated_interpreter) {
1186 // bpo-40533: The garbage collector must not be run on parallel on
1187 // Python objects shared by multiple interpreters.
1188 return 0;
1189 }
1190#endif
1191
Victor Stinner67e0de62019-11-20 11:48:18 +01001192 if (gcstate->debug & DEBUG_STATS) {
Inada Naokibf8162c2019-08-02 16:25:29 +09001193 PySys_WriteStderr("gc: collecting generation %d...\n", generation);
Victor Stinner67e0de62019-11-20 11:48:18 +01001194 show_stats_each_generations(gcstate);
Victor Stinner7181dec2015-03-27 17:47:53 +01001195 t1 = _PyTime_GetMonotonicClock();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001196 }
Neil Schemenauer2880ae52002-05-04 05:35:20 +00001197
Łukasz Langaa785c872016-09-09 17:37:37 -07001198 if (PyDTrace_GC_START_ENABLED())
1199 PyDTrace_GC_START(generation);
1200
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001201 /* update collection and allocation counters */
1202 if (generation+1 < NUM_GENERATIONS)
Victor Stinner67e0de62019-11-20 11:48:18 +01001203 gcstate->generations[generation+1].count += 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001204 for (i = 0; i <= generation; i++)
Victor Stinner67e0de62019-11-20 11:48:18 +01001205 gcstate->generations[i].count = 0;
Neil Schemenauer2880ae52002-05-04 05:35:20 +00001206
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001207 /* merge younger generations with one we are currently collecting */
1208 for (i = 0; i < generation; i++) {
Victor Stinner67e0de62019-11-20 11:48:18 +01001209 gc_list_merge(GEN_HEAD(gcstate, i), GEN_HEAD(gcstate, generation));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 }
Neil Schemenauer2880ae52002-05-04 05:35:20 +00001211
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001212 /* handy references */
Victor Stinner67e0de62019-11-20 11:48:18 +01001213 young = GEN_HEAD(gcstate, generation);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001214 if (generation < NUM_GENERATIONS-1)
Victor Stinner67e0de62019-11-20 11:48:18 +01001215 old = GEN_HEAD(gcstate, generation+1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 else
1217 old = young;
Tim Petersea55c512019-10-18 20:59:14 -05001218 validate_list(old, collecting_clear_unreachable_clear);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001219
Pablo Galindo466326d2019-10-13 16:48:59 +01001220 deduce_unreachable(young, &unreachable);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001221
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09001222 untrack_tuples(young);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001223 /* Move reachable objects to next generation. */
1224 if (young != old) {
1225 if (generation == NUM_GENERATIONS - 2) {
Victor Stinner67e0de62019-11-20 11:48:18 +01001226 gcstate->long_lived_pending += gc_list_size(young);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001227 }
1228 gc_list_merge(young, old);
1229 }
1230 else {
Pablo Galindo97f12672020-01-13 12:25:05 +00001231 /* We only un-track dicts in full collections, to avoid quadratic
Antoine Pitroue1ad3da2012-05-28 22:22:34 +02001232 dict build-up. See issue #14775. */
1233 untrack_dicts(young);
Victor Stinner67e0de62019-11-20 11:48:18 +01001234 gcstate->long_lived_pending = 0;
1235 gcstate->long_lived_total = gc_list_size(young);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001236 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001237
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001238 /* All objects in unreachable are trash, but objects reachable from
Antoine Pitrou796564c2013-07-30 19:59:21 +02001239 * legacy finalizers (e.g. tp_del) can't safely be deleted.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001240 */
1241 gc_list_init(&finalizers);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09001242 // NEXT_MASK_UNREACHABLE is cleared here.
1243 // After move_legacy_finalizers(), unreachable is normal list.
Antoine Pitrou796564c2013-07-30 19:59:21 +02001244 move_legacy_finalizers(&unreachable, &finalizers);
1245 /* finalizers contains the unreachable objects with a legacy finalizer;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001246 * unreachable objects reachable *from* those are also uncollectable,
1247 * and we move those into the finalizers list too.
1248 */
Antoine Pitrou796564c2013-07-30 19:59:21 +02001249 move_legacy_finalizer_reachable(&finalizers);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001250
Tim Petersea55c512019-10-18 20:59:14 -05001251 validate_list(&finalizers, collecting_clear_unreachable_clear);
1252 validate_list(&unreachable, collecting_set_unreachable_clear);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09001253
Tim Petersecbf35f2019-10-09 12:37:30 -05001254 /* Print debugging information. */
Victor Stinner67e0de62019-11-20 11:48:18 +01001255 if (gcstate->debug & DEBUG_COLLECTABLE) {
Tim Petersecbf35f2019-10-09 12:37:30 -05001256 for (gc = GC_NEXT(&unreachable); gc != &unreachable; gc = GC_NEXT(gc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001257 debug_cycle("collectable", FROM_GC(gc));
1258 }
1259 }
Tim Petersead8b7a2004-10-30 23:09:22 +00001260
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 /* Clear weakrefs and invoke callbacks as necessary. */
1262 m += handle_weakrefs(&unreachable, old);
Tim Petersead8b7a2004-10-30 23:09:22 +00001263
Tim Petersea55c512019-10-18 20:59:14 -05001264 validate_list(old, collecting_clear_unreachable_clear);
1265 validate_list(&unreachable, collecting_set_unreachable_clear);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09001266
Antoine Pitrou796564c2013-07-30 19:59:21 +02001267 /* Call tp_finalize on objects which have one. */
Victor Stinner67e0de62019-11-20 11:48:18 +01001268 finalize_garbage(tstate, &unreachable);
Antoine Pitrou796564c2013-07-30 19:59:21 +02001269
Pablo Galindo466326d2019-10-13 16:48:59 +01001270 /* Handle any objects that may have resurrected after the call
1271 * to 'finalize_garbage' and continue the collection with the
1272 * objects that are still unreachable */
1273 PyGC_Head final_unreachable;
1274 handle_resurrected_objects(&unreachable, &final_unreachable, old);
1275
1276 /* Call tp_clear on objects in the final_unreachable set. This will cause
1277 * the reference cycles to be broken. It may also cause some objects
1278 * in finalizers to be freed.
1279 */
1280 m += gc_list_size(&final_unreachable);
Victor Stinner67e0de62019-11-20 11:48:18 +01001281 delete_garbage(tstate, gcstate, &final_unreachable, old);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001283 /* Collect statistics on uncollectable objects found and print
1284 * debugging information. */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09001285 for (gc = GC_NEXT(&finalizers); gc != &finalizers; gc = GC_NEXT(gc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001286 n++;
Victor Stinner67e0de62019-11-20 11:48:18 +01001287 if (gcstate->debug & DEBUG_UNCOLLECTABLE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 debug_cycle("uncollectable", FROM_GC(gc));
1289 }
Victor Stinner67e0de62019-11-20 11:48:18 +01001290 if (gcstate->debug & DEBUG_STATS) {
Inada Naokibf8162c2019-08-02 16:25:29 +09001291 double d = _PyTime_AsSecondsDouble(_PyTime_GetMonotonicClock() - t1);
Inada Naoki013e52f2019-08-31 09:13:42 +09001292 PySys_WriteStderr(
1293 "gc: done, %" PY_FORMAT_SIZE_T "d unreachable, "
1294 "%" PY_FORMAT_SIZE_T "d uncollectable, %.4fs elapsed\n",
Inada Naokibf8162c2019-08-02 16:25:29 +09001295 n+m, n, d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001296 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001298 /* Append instances in the uncollectable set to a Python
1299 * reachable list of garbage. The programmer has to deal with
1300 * this if they insist on creating this type of structure.
1301 */
Victor Stinner67e0de62019-11-20 11:48:18 +01001302 handle_legacy_finalizers(tstate, gcstate, &finalizers, old);
Tim Petersea55c512019-10-18 20:59:14 -05001303 validate_list(old, collecting_clear_unreachable_clear);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001304
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001305 /* Clear free list only during the collection of the highest
1306 * generation */
1307 if (generation == NUM_GENERATIONS-1) {
1308 clear_freelists();
1309 }
Christian Heimesa156e092008-02-16 07:38:31 +00001310
Victor Stinner2e969062019-11-20 01:49:32 +01001311 if (_PyErr_Occurred(tstate)) {
Antoine Pitroufef34e32013-05-19 01:11:58 +02001312 if (nofail) {
Victor Stinner2e969062019-11-20 01:49:32 +01001313 _PyErr_Clear(tstate);
Antoine Pitroufef34e32013-05-19 01:11:58 +02001314 }
1315 else {
Victor Stinner656c45e2020-01-24 18:05:24 +01001316 _PyErr_WriteUnraisableMsg("in garbage collection", NULL);
Antoine Pitroufef34e32013-05-19 01:11:58 +02001317 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001318 }
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001319
Antoine Pitroud4156c12012-10-30 22:43:19 +01001320 /* Update stats */
Victor Stinner9db03242019-04-26 02:32:01 +02001321 if (n_collected) {
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001322 *n_collected = m;
Victor Stinner9db03242019-04-26 02:32:01 +02001323 }
1324 if (n_uncollectable) {
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001325 *n_uncollectable = n;
Victor Stinner9db03242019-04-26 02:32:01 +02001326 }
1327
Victor Stinner67e0de62019-11-20 11:48:18 +01001328 struct gc_generation_stats *stats = &gcstate->generation_stats[generation];
Antoine Pitroud4156c12012-10-30 22:43:19 +01001329 stats->collections++;
1330 stats->collected += m;
1331 stats->uncollectable += n;
Łukasz Langaa785c872016-09-09 17:37:37 -07001332
Victor Stinner9db03242019-04-26 02:32:01 +02001333 if (PyDTrace_GC_DONE_ENABLED()) {
Victor Stinner2e969062019-11-20 01:49:32 +01001334 PyDTrace_GC_DONE(n + m);
Victor Stinner9db03242019-04-26 02:32:01 +02001335 }
Łukasz Langaa785c872016-09-09 17:37:37 -07001336
Victor Stinner2e969062019-11-20 01:49:32 +01001337 assert(!_PyErr_Occurred(tstate));
1338 return n + m;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001339}
1340
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001341/* Invoke progress callbacks to notify clients that garbage collection
1342 * is starting or stopping
1343 */
1344static void
Victor Stinner67e0de62019-11-20 11:48:18 +01001345invoke_gc_callback(PyThreadState *tstate, const char *phase,
Victor Stinner9db03242019-04-26 02:32:01 +02001346 int generation, Py_ssize_t collected,
1347 Py_ssize_t uncollectable)
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001348{
Victor Stinner67e0de62019-11-20 11:48:18 +01001349 assert(!_PyErr_Occurred(tstate));
Victor Stinner9db03242019-04-26 02:32:01 +02001350
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001351 /* we may get called very early */
Victor Stinner72474072019-11-20 12:25:50 +01001352 GCState *gcstate = &tstate->interp->gc;
Victor Stinner67e0de62019-11-20 11:48:18 +01001353 if (gcstate->callbacks == NULL) {
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001354 return;
Victor Stinner9db03242019-04-26 02:32:01 +02001355 }
1356
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001357 /* The local variable cannot be rebound, check it for sanity */
Victor Stinner67e0de62019-11-20 11:48:18 +01001358 assert(PyList_CheckExact(gcstate->callbacks));
Victor Stinner9db03242019-04-26 02:32:01 +02001359 PyObject *info = NULL;
Victor Stinner67e0de62019-11-20 11:48:18 +01001360 if (PyList_GET_SIZE(gcstate->callbacks) != 0) {
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001361 info = Py_BuildValue("{sisnsn}",
1362 "generation", generation,
1363 "collected", collected,
1364 "uncollectable", uncollectable);
1365 if (info == NULL) {
1366 PyErr_WriteUnraisable(NULL);
1367 return;
1368 }
1369 }
Victor Stinner67e0de62019-11-20 11:48:18 +01001370 for (Py_ssize_t i=0; i<PyList_GET_SIZE(gcstate->callbacks); i++) {
1371 PyObject *r, *cb = PyList_GET_ITEM(gcstate->callbacks, i);
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001372 Py_INCREF(cb); /* make sure cb doesn't go away */
1373 r = PyObject_CallFunction(cb, "sO", phase, info);
Serhiy Storchaka301e3cc2018-05-24 15:19:29 +03001374 if (r == NULL) {
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001375 PyErr_WriteUnraisable(cb);
Serhiy Storchaka301e3cc2018-05-24 15:19:29 +03001376 }
1377 else {
1378 Py_DECREF(r);
1379 }
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001380 Py_DECREF(cb);
1381 }
1382 Py_XDECREF(info);
Victor Stinner67e0de62019-11-20 11:48:18 +01001383 assert(!_PyErr_Occurred(tstate));
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001384}
1385
1386/* Perform garbage collection of a generation and invoke
1387 * progress callbacks.
1388 */
1389static Py_ssize_t
Victor Stinner67e0de62019-11-20 11:48:18 +01001390collect_with_callback(PyThreadState *tstate, int generation)
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001391{
Victor Stinner67e0de62019-11-20 11:48:18 +01001392 assert(!_PyErr_Occurred(tstate));
Victor Stinner9db03242019-04-26 02:32:01 +02001393 Py_ssize_t result, collected, uncollectable;
Victor Stinner67e0de62019-11-20 11:48:18 +01001394 invoke_gc_callback(tstate, "start", generation, 0, 0);
1395 result = collect(tstate, generation, &collected, &uncollectable, 0);
1396 invoke_gc_callback(tstate, "stop", generation, collected, uncollectable);
1397 assert(!_PyErr_Occurred(tstate));
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001398 return result;
1399}
1400
Neal Norwitz7b216c52006-03-04 20:01:53 +00001401static Py_ssize_t
Victor Stinner67e0de62019-11-20 11:48:18 +01001402collect_generations(PyThreadState *tstate)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001403{
Victor Stinner72474072019-11-20 12:25:50 +01001404 GCState *gcstate = &tstate->interp->gc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 /* Find the oldest generation (highest numbered) where the count
1406 * exceeds the threshold. Objects in the that generation and
1407 * generations younger than it will be collected. */
Victor Stinner9db03242019-04-26 02:32:01 +02001408 Py_ssize_t n = 0;
1409 for (int i = NUM_GENERATIONS-1; i >= 0; i--) {
Victor Stinner67e0de62019-11-20 11:48:18 +01001410 if (gcstate->generations[i].count > gcstate->generations[i].threshold) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001411 /* Avoid quadratic performance degradation in number
Pablo Galindo90913982019-12-27 21:55:56 +00001412 of tracked objects (see also issue #4074):
1413
1414 To limit the cost of garbage collection, there are two strategies;
1415 - make each collection faster, e.g. by scanning fewer objects
1416 - do less collections
1417 This heuristic is about the latter strategy.
1418
1419 In addition to the various configurable thresholds, we only trigger a
1420 full collection if the ratio
1421
1422 long_lived_pending / long_lived_total
1423
1424 is above a given value (hardwired to 25%).
1425
1426 The reason is that, while "non-full" collections (i.e., collections of
1427 the young and middle generations) will always examine roughly the same
1428 number of objects -- determined by the aforementioned thresholds --,
1429 the cost of a full collection is proportional to the total number of
1430 long-lived objects, which is virtually unbounded.
1431
1432 Indeed, it has been remarked that doing a full collection every
1433 <constant number> of object creations entails a dramatic performance
1434 degradation in workloads which consist in creating and storing lots of
1435 long-lived objects (e.g. building a large list of GC-tracked objects would
1436 show quadratic performance, instead of linear as expected: see issue #4074).
1437
1438 Using the above ratio, instead, yields amortized linear performance in
1439 the total number of objects (the effect of which can be summarized
1440 thusly: "each full garbage collection is more and more costly as the
1441 number of objects grows, but we do fewer and fewer of them").
1442
1443 This heuristic was suggested by Martin von Löwis on python-dev in
1444 June 2008. His original analysis and proposal can be found at:
1445 http://mail.python.org/pipermail/python-dev/2008-June/080579.html
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001446 */
1447 if (i == NUM_GENERATIONS - 1
Victor Stinner67e0de62019-11-20 11:48:18 +01001448 && gcstate->long_lived_pending < gcstate->long_lived_total / 4)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001449 continue;
Victor Stinner67e0de62019-11-20 11:48:18 +01001450 n = collect_with_callback(tstate, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001451 break;
1452 }
1453 }
1454 return n;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001455}
1456
Serhiy Storchaka93260282017-02-04 11:19:59 +02001457#include "clinic/gcmodule.c.h"
1458
1459/*[clinic input]
1460gc.enable
1461
1462Enable automatic garbage collection.
1463[clinic start generated code]*/
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001464
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001465static PyObject *
Serhiy Storchaka93260282017-02-04 11:19:59 +02001466gc_enable_impl(PyObject *module)
1467/*[clinic end generated code: output=45a427e9dce9155c input=81ac4940ca579707]*/
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001468{
Victor Stinner67e0de62019-11-20 11:48:18 +01001469 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner72474072019-11-20 12:25:50 +01001470 GCState *gcstate = &tstate->interp->gc;
Victor Stinner67e0de62019-11-20 11:48:18 +01001471 gcstate->enabled = 1;
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001472 Py_RETURN_NONE;
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001473}
1474
Serhiy Storchaka93260282017-02-04 11:19:59 +02001475/*[clinic input]
1476gc.disable
1477
1478Disable automatic garbage collection.
1479[clinic start generated code]*/
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001480
1481static PyObject *
Serhiy Storchaka93260282017-02-04 11:19:59 +02001482gc_disable_impl(PyObject *module)
1483/*[clinic end generated code: output=97d1030f7aa9d279 input=8c2e5a14e800d83b]*/
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001484{
Victor Stinner67e0de62019-11-20 11:48:18 +01001485 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner72474072019-11-20 12:25:50 +01001486 GCState *gcstate = &tstate->interp->gc;
Victor Stinner67e0de62019-11-20 11:48:18 +01001487 gcstate->enabled = 0;
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001488 Py_RETURN_NONE;
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001489}
1490
Serhiy Storchaka93260282017-02-04 11:19:59 +02001491/*[clinic input]
1492gc.isenabled -> bool
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001493
Serhiy Storchaka93260282017-02-04 11:19:59 +02001494Returns true if automatic garbage collection is enabled.
1495[clinic start generated code]*/
1496
1497static int
1498gc_isenabled_impl(PyObject *module)
1499/*[clinic end generated code: output=1874298331c49130 input=30005e0422373b31]*/
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001500{
Victor Stinner67e0de62019-11-20 11:48:18 +01001501 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner72474072019-11-20 12:25:50 +01001502 GCState *gcstate = &tstate->interp->gc;
Victor Stinner67e0de62019-11-20 11:48:18 +01001503 return gcstate->enabled;
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001504}
1505
Serhiy Storchaka93260282017-02-04 11:19:59 +02001506/*[clinic input]
1507gc.collect -> Py_ssize_t
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001508
Serhiy Storchaka93260282017-02-04 11:19:59 +02001509 generation: int(c_default="NUM_GENERATIONS - 1") = 2
1510
1511Run the garbage collector.
1512
1513With no arguments, run a full collection. The optional argument
1514may be an integer specifying which generation to collect. A ValueError
1515is raised if the generation number is invalid.
1516
1517The number of unreachable objects is returned.
1518[clinic start generated code]*/
1519
1520static Py_ssize_t
1521gc_collect_impl(PyObject *module, int generation)
1522/*[clinic end generated code: output=b697e633043233c7 input=40720128b682d879]*/
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001523{
Victor Stinner2e969062019-11-20 01:49:32 +01001524 PyThreadState *tstate = _PyThreadState_GET();
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001525
Serhiy Storchaka93260282017-02-04 11:19:59 +02001526 if (generation < 0 || generation >= NUM_GENERATIONS) {
Victor Stinner2e969062019-11-20 01:49:32 +01001527 _PyErr_SetString(tstate, PyExc_ValueError, "invalid generation");
Serhiy Storchaka93260282017-02-04 11:19:59 +02001528 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001529 }
Barry Warsawd3c38ff2006-03-07 09:46:03 +00001530
Victor Stinner72474072019-11-20 12:25:50 +01001531 GCState *gcstate = &tstate->interp->gc;
Victor Stinner9db03242019-04-26 02:32:01 +02001532 Py_ssize_t n;
Victor Stinner67e0de62019-11-20 11:48:18 +01001533 if (gcstate->collecting) {
Victor Stinner9db03242019-04-26 02:32:01 +02001534 /* already collecting, don't do anything */
1535 n = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001536 }
Victor Stinner9db03242019-04-26 02:32:01 +02001537 else {
Victor Stinner67e0de62019-11-20 11:48:18 +01001538 gcstate->collecting = 1;
1539 n = collect_with_callback(tstate, generation);
1540 gcstate->collecting = 0;
Victor Stinner9db03242019-04-26 02:32:01 +02001541 }
Serhiy Storchaka93260282017-02-04 11:19:59 +02001542 return n;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001543}
1544
Serhiy Storchaka93260282017-02-04 11:19:59 +02001545/*[clinic input]
1546gc.set_debug
1547
1548 flags: int
1549 An integer that can have the following bits turned on:
1550 DEBUG_STATS - Print statistics during collection.
1551 DEBUG_COLLECTABLE - Print collectable objects found.
1552 DEBUG_UNCOLLECTABLE - Print unreachable but uncollectable objects
1553 found.
1554 DEBUG_SAVEALL - Save objects to gc.garbage rather than freeing them.
1555 DEBUG_LEAK - Debug leaking programs (everything but STATS).
1556 /
1557
1558Set the garbage collection debugging flags.
1559
1560Debugging information is written to sys.stderr.
1561[clinic start generated code]*/
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001562
1563static PyObject *
Serhiy Storchaka93260282017-02-04 11:19:59 +02001564gc_set_debug_impl(PyObject *module, int flags)
1565/*[clinic end generated code: output=7c8366575486b228 input=5e5ce15e84fbed15]*/
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001566{
Victor Stinner67e0de62019-11-20 11:48:18 +01001567 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner72474072019-11-20 12:25:50 +01001568 GCState *gcstate = &tstate->interp->gc;
Victor Stinner67e0de62019-11-20 11:48:18 +01001569 gcstate->debug = flags;
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001570 Py_RETURN_NONE;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001571}
1572
Serhiy Storchaka93260282017-02-04 11:19:59 +02001573/*[clinic input]
1574gc.get_debug -> int
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001575
Serhiy Storchaka93260282017-02-04 11:19:59 +02001576Get the garbage collection debugging flags.
1577[clinic start generated code]*/
1578
1579static int
1580gc_get_debug_impl(PyObject *module)
1581/*[clinic end generated code: output=91242f3506cd1e50 input=91a101e1c3b98366]*/
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001582{
Victor Stinner67e0de62019-11-20 11:48:18 +01001583 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner72474072019-11-20 12:25:50 +01001584 GCState *gcstate = &tstate->interp->gc;
Victor Stinner67e0de62019-11-20 11:48:18 +01001585 return gcstate->debug;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001586}
1587
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001588PyDoc_STRVAR(gc_set_thresh__doc__,
Neal Norwitz2a47c0f2002-01-29 00:53:41 +00001589"set_threshold(threshold0, [threshold1, threshold2]) -> None\n"
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001590"\n"
1591"Sets the collection thresholds. Setting threshold0 to zero disables\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001592"collection.\n");
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001593
1594static PyObject *
Victor Stinner9db03242019-04-26 02:32:01 +02001595gc_set_threshold(PyObject *self, PyObject *args)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001596{
Victor Stinner67e0de62019-11-20 11:48:18 +01001597 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner72474072019-11-20 12:25:50 +01001598 GCState *gcstate = &tstate->interp->gc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001599 if (!PyArg_ParseTuple(args, "i|ii:set_threshold",
Victor Stinner67e0de62019-11-20 11:48:18 +01001600 &gcstate->generations[0].threshold,
1601 &gcstate->generations[1].threshold,
1602 &gcstate->generations[2].threshold))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603 return NULL;
Victor Stinner9db03242019-04-26 02:32:01 +02001604 for (int i = 3; i < NUM_GENERATIONS; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001605 /* generations higher than 2 get the same threshold */
Victor Stinner67e0de62019-11-20 11:48:18 +01001606 gcstate->generations[i].threshold = gcstate->generations[2].threshold;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001607 }
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001608 Py_RETURN_NONE;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001609}
1610
Serhiy Storchaka93260282017-02-04 11:19:59 +02001611/*[clinic input]
1612gc.get_threshold
1613
1614Return the current collection thresholds.
1615[clinic start generated code]*/
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001616
1617static PyObject *
Serhiy Storchaka93260282017-02-04 11:19:59 +02001618gc_get_threshold_impl(PyObject *module)
1619/*[clinic end generated code: output=7902bc9f41ecbbd8 input=286d79918034d6e6]*/
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001620{
Victor Stinner67e0de62019-11-20 11:48:18 +01001621 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner72474072019-11-20 12:25:50 +01001622 GCState *gcstate = &tstate->interp->gc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001623 return Py_BuildValue("(iii)",
Victor Stinner67e0de62019-11-20 11:48:18 +01001624 gcstate->generations[0].threshold,
1625 gcstate->generations[1].threshold,
1626 gcstate->generations[2].threshold);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001627}
1628
Serhiy Storchaka93260282017-02-04 11:19:59 +02001629/*[clinic input]
1630gc.get_count
1631
1632Return a three-tuple of the current collection counts.
1633[clinic start generated code]*/
Barry Warsawd3c38ff2006-03-07 09:46:03 +00001634
1635static PyObject *
Serhiy Storchaka93260282017-02-04 11:19:59 +02001636gc_get_count_impl(PyObject *module)
1637/*[clinic end generated code: output=354012e67b16398f input=a392794a08251751]*/
Barry Warsawd3c38ff2006-03-07 09:46:03 +00001638{
Victor Stinner67e0de62019-11-20 11:48:18 +01001639 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner72474072019-11-20 12:25:50 +01001640 GCState *gcstate = &tstate->interp->gc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001641 return Py_BuildValue("(iii)",
Victor Stinner67e0de62019-11-20 11:48:18 +01001642 gcstate->generations[0].count,
1643 gcstate->generations[1].count,
1644 gcstate->generations[2].count);
Barry Warsawd3c38ff2006-03-07 09:46:03 +00001645}
1646
Neil Schemenauer48c70342001-08-09 15:38:31 +00001647static int
Martin v. Löwis560da622001-11-24 09:24:51 +00001648referrersvisit(PyObject* obj, PyObject *objs)
Neil Schemenauer48c70342001-08-09 15:38:31 +00001649{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001650 Py_ssize_t i;
1651 for (i = 0; i < PyTuple_GET_SIZE(objs); i++)
1652 if (PyTuple_GET_ITEM(objs, i) == obj)
1653 return 1;
1654 return 0;
Neil Schemenauer48c70342001-08-09 15:38:31 +00001655}
1656
Neil Schemenauer17e7be62001-08-10 14:46:47 +00001657static int
Martin v. Löwis560da622001-11-24 09:24:51 +00001658gc_referrers_for(PyObject *objs, PyGC_Head *list, PyObject *resultlist)
Neil Schemenauer48c70342001-08-09 15:38:31 +00001659{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001660 PyGC_Head *gc;
1661 PyObject *obj;
1662 traverseproc traverse;
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09001663 for (gc = GC_NEXT(list); gc != list; gc = GC_NEXT(gc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001664 obj = FROM_GC(gc);
1665 traverse = Py_TYPE(obj)->tp_traverse;
1666 if (obj == objs || obj == resultlist)
1667 continue;
1668 if (traverse(obj, (visitproc)referrersvisit, objs)) {
1669 if (PyList_Append(resultlist, obj) < 0)
1670 return 0; /* error */
1671 }
1672 }
1673 return 1; /* no error */
Neil Schemenauer48c70342001-08-09 15:38:31 +00001674}
1675
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001676PyDoc_STRVAR(gc_get_referrers__doc__,
Martin v. Löwis560da622001-11-24 09:24:51 +00001677"get_referrers(*objs) -> list\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001678Return the list of objects that directly refer to any of objs.");
Neil Schemenauer48c70342001-08-09 15:38:31 +00001679
Neil Schemenauer17e7be62001-08-10 14:46:47 +00001680static PyObject *
Martin v. Löwis560da622001-11-24 09:24:51 +00001681gc_get_referrers(PyObject *self, PyObject *args)
Neil Schemenauer48c70342001-08-09 15:38:31 +00001682{
Victor Stinner67e0de62019-11-20 11:48:18 +01001683 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001684 int i;
1685 PyObject *result = PyList_New(0);
Victor Stinner67e0de62019-11-20 11:48:18 +01001686 if (!result) {
1687 return NULL;
1688 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001689
Victor Stinner72474072019-11-20 12:25:50 +01001690 GCState *gcstate = &tstate->interp->gc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001691 for (i = 0; i < NUM_GENERATIONS; i++) {
Victor Stinner67e0de62019-11-20 11:48:18 +01001692 if (!(gc_referrers_for(args, GEN_HEAD(gcstate, i), result))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001693 Py_DECREF(result);
1694 return NULL;
1695 }
1696 }
1697 return result;
Neil Schemenauer48c70342001-08-09 15:38:31 +00001698}
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001699
Tim Peters0f81ab62003-04-08 16:39:48 +00001700/* Append obj to list; return true if error (out of memory), false if OK. */
Jeremy Hylton5bd378b2003-04-03 16:28:38 +00001701static int
Tim Peters730f5532003-04-08 17:17:17 +00001702referentsvisit(PyObject *obj, PyObject *list)
Jeremy Hylton5bd378b2003-04-03 16:28:38 +00001703{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001704 return PyList_Append(list, obj) < 0;
Jeremy Hylton5bd378b2003-04-03 16:28:38 +00001705}
1706
Tim Peters730f5532003-04-08 17:17:17 +00001707PyDoc_STRVAR(gc_get_referents__doc__,
1708"get_referents(*objs) -> list\n\
Jeremy Hylton059b0942003-04-03 16:29:13 +00001709Return the list of objects that are directly referred to by objs.");
Jeremy Hylton5bd378b2003-04-03 16:28:38 +00001710
1711static PyObject *
Tim Peters730f5532003-04-08 17:17:17 +00001712gc_get_referents(PyObject *self, PyObject *args)
Jeremy Hylton5bd378b2003-04-03 16:28:38 +00001713{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001714 Py_ssize_t i;
1715 PyObject *result = PyList_New(0);
Tim Peters0f81ab62003-04-08 16:39:48 +00001716
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001717 if (result == NULL)
1718 return NULL;
Tim Peters0f81ab62003-04-08 16:39:48 +00001719
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001720 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
1721 traverseproc traverse;
1722 PyObject *obj = PyTuple_GET_ITEM(args, i);
Tim Peters0f81ab62003-04-08 16:39:48 +00001723
Hai Shi675d9a32020-04-15 02:11:20 +08001724 if (!_PyObject_IS_GC(obj))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001725 continue;
1726 traverse = Py_TYPE(obj)->tp_traverse;
1727 if (! traverse)
1728 continue;
1729 if (traverse(obj, (visitproc)referentsvisit, result)) {
1730 Py_DECREF(result);
1731 return NULL;
1732 }
1733 }
1734 return result;
Jeremy Hylton5bd378b2003-04-03 16:28:38 +00001735}
1736
Serhiy Storchaka93260282017-02-04 11:19:59 +02001737/*[clinic input]
1738gc.get_objects
Pablo Galindo175421b2019-02-23 03:02:06 +00001739 generation: Py_ssize_t(accept={int, NoneType}, c_default="-1") = None
1740 Generation to extract the objects from.
Serhiy Storchaka93260282017-02-04 11:19:59 +02001741
1742Return a list of objects tracked by the collector (excluding the list returned).
Pablo Galindo175421b2019-02-23 03:02:06 +00001743
1744If generation is not None, return only the objects tracked by the collector
1745that are in that generation.
Serhiy Storchaka93260282017-02-04 11:19:59 +02001746[clinic start generated code]*/
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +00001747
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +00001748static PyObject *
Pablo Galindo175421b2019-02-23 03:02:06 +00001749gc_get_objects_impl(PyObject *module, Py_ssize_t generation)
1750/*[clinic end generated code: output=48b35fea4ba6cb0e input=ef7da9df9806754c]*/
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +00001751{
Victor Stinner67e0de62019-11-20 11:48:18 +01001752 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001753 int i;
1754 PyObject* result;
Victor Stinner72474072019-11-20 12:25:50 +01001755 GCState *gcstate = &tstate->interp->gc;
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +00001756
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001757 result = PyList_New(0);
Pablo Galindo175421b2019-02-23 03:02:06 +00001758 if (result == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001759 return NULL;
Pablo Galindo175421b2019-02-23 03:02:06 +00001760 }
1761
1762 /* If generation is passed, we extract only that generation */
Victor Stinner0810fa72019-04-15 17:54:09 +02001763 if (generation != -1) {
Pablo Galindo175421b2019-02-23 03:02:06 +00001764 if (generation >= NUM_GENERATIONS) {
Victor Stinner67e0de62019-11-20 11:48:18 +01001765 _PyErr_Format(tstate, PyExc_ValueError,
1766 "generation parameter must be less than the number of "
1767 "available generations (%i)",
1768 NUM_GENERATIONS);
Pablo Galindo175421b2019-02-23 03:02:06 +00001769 goto error;
1770 }
1771
1772 if (generation < 0) {
Victor Stinner67e0de62019-11-20 11:48:18 +01001773 _PyErr_SetString(tstate, PyExc_ValueError,
1774 "generation parameter cannot be negative");
Pablo Galindo175421b2019-02-23 03:02:06 +00001775 goto error;
1776 }
1777
Victor Stinner67e0de62019-11-20 11:48:18 +01001778 if (append_objects(result, GEN_HEAD(gcstate, generation))) {
Pablo Galindo175421b2019-02-23 03:02:06 +00001779 goto error;
1780 }
1781
1782 return result;
1783 }
1784
1785 /* If generation is not passed or None, get all objects from all generations */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001786 for (i = 0; i < NUM_GENERATIONS; i++) {
Victor Stinner67e0de62019-11-20 11:48:18 +01001787 if (append_objects(result, GEN_HEAD(gcstate, i))) {
Pablo Galindo175421b2019-02-23 03:02:06 +00001788 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001789 }
1790 }
1791 return result;
Pablo Galindo175421b2019-02-23 03:02:06 +00001792
1793error:
1794 Py_DECREF(result);
1795 return NULL;
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +00001796}
1797
Serhiy Storchaka93260282017-02-04 11:19:59 +02001798/*[clinic input]
1799gc.get_stats
1800
1801Return a list of dictionaries containing per-generation statistics.
1802[clinic start generated code]*/
Antoine Pitroud4156c12012-10-30 22:43:19 +01001803
1804static PyObject *
Serhiy Storchaka93260282017-02-04 11:19:59 +02001805gc_get_stats_impl(PyObject *module)
1806/*[clinic end generated code: output=a8ab1d8a5d26f3ab input=1ef4ed9d17b1a470]*/
Antoine Pitroud4156c12012-10-30 22:43:19 +01001807{
1808 int i;
Antoine Pitroud4156c12012-10-30 22:43:19 +01001809 struct gc_generation_stats stats[NUM_GENERATIONS], *st;
Victor Stinner67e0de62019-11-20 11:48:18 +01001810 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitroud4156c12012-10-30 22:43:19 +01001811
1812 /* To get consistent values despite allocations while constructing
1813 the result list, we use a snapshot of the running stats. */
Victor Stinner72474072019-11-20 12:25:50 +01001814 GCState *gcstate = &tstate->interp->gc;
Antoine Pitroud4156c12012-10-30 22:43:19 +01001815 for (i = 0; i < NUM_GENERATIONS; i++) {
Victor Stinner67e0de62019-11-20 11:48:18 +01001816 stats[i] = gcstate->generation_stats[i];
Antoine Pitroud4156c12012-10-30 22:43:19 +01001817 }
1818
Victor Stinner9db03242019-04-26 02:32:01 +02001819 PyObject *result = PyList_New(0);
Antoine Pitroud4156c12012-10-30 22:43:19 +01001820 if (result == NULL)
1821 return NULL;
1822
1823 for (i = 0; i < NUM_GENERATIONS; i++) {
1824 PyObject *dict;
1825 st = &stats[i];
1826 dict = Py_BuildValue("{snsnsn}",
1827 "collections", st->collections,
1828 "collected", st->collected,
1829 "uncollectable", st->uncollectable
1830 );
1831 if (dict == NULL)
1832 goto error;
1833 if (PyList_Append(result, dict)) {
1834 Py_DECREF(dict);
1835 goto error;
1836 }
1837 Py_DECREF(dict);
1838 }
1839 return result;
1840
1841error:
1842 Py_XDECREF(result);
1843 return NULL;
1844}
1845
1846
Serhiy Storchaka93260282017-02-04 11:19:59 +02001847/*[clinic input]
1848gc.is_tracked
1849
1850 obj: object
1851 /
1852
1853Returns true if the object is tracked by the garbage collector.
1854
1855Simple atomic objects will return false.
1856[clinic start generated code]*/
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001857
1858static PyObject *
Serhiy Storchaka93260282017-02-04 11:19:59 +02001859gc_is_tracked(PyObject *module, PyObject *obj)
1860/*[clinic end generated code: output=14f0103423b28e31 input=d83057f170ea2723]*/
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001861{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001862 PyObject *result;
1863
Hai Shi675d9a32020-04-15 02:11:20 +08001864 if (_PyObject_IS_GC(obj) && _PyObject_GC_IS_TRACKED(obj))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001865 result = Py_True;
1866 else
1867 result = Py_False;
1868 Py_INCREF(result);
1869 return result;
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001870}
1871
brainfvckc75edab2017-10-16 12:49:41 -07001872/*[clinic input]
Pablo Galindoa2ec3f02020-01-14 12:06:45 +00001873gc.is_finalized
1874
1875 obj: object
1876 /
1877
1878Returns true if the object has been already finalized by the GC.
1879[clinic start generated code]*/
1880
1881static PyObject *
1882gc_is_finalized(PyObject *module, PyObject *obj)
1883/*[clinic end generated code: output=e1516ac119a918ed input=201d0c58f69ae390]*/
1884{
Hai Shi675d9a32020-04-15 02:11:20 +08001885 if (_PyObject_IS_GC(obj) && _PyGCHead_FINALIZED(AS_GC(obj))) {
Pablo Galindoa2ec3f02020-01-14 12:06:45 +00001886 Py_RETURN_TRUE;
1887 }
1888 Py_RETURN_FALSE;
1889}
1890
1891/*[clinic input]
brainfvckc75edab2017-10-16 12:49:41 -07001892gc.freeze
1893
1894Freeze all current tracked objects and ignore them for future collections.
1895
1896This can be used before a POSIX fork() call to make the gc copy-on-write friendly.
1897Note: collection before a POSIX fork() call may free pages for future allocation
1898which can cause copy-on-write.
1899[clinic start generated code]*/
1900
1901static PyObject *
1902gc_freeze_impl(PyObject *module)
1903/*[clinic end generated code: output=502159d9cdc4c139 input=b602b16ac5febbe5]*/
1904{
Victor Stinner67e0de62019-11-20 11:48:18 +01001905 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner72474072019-11-20 12:25:50 +01001906 GCState *gcstate = &tstate->interp->gc;
brainfvckc75edab2017-10-16 12:49:41 -07001907 for (int i = 0; i < NUM_GENERATIONS; ++i) {
Victor Stinner67e0de62019-11-20 11:48:18 +01001908 gc_list_merge(GEN_HEAD(gcstate, i), &gcstate->permanent_generation.head);
1909 gcstate->generations[i].count = 0;
brainfvckc75edab2017-10-16 12:49:41 -07001910 }
1911 Py_RETURN_NONE;
1912}
1913
1914/*[clinic input]
1915gc.unfreeze
1916
1917Unfreeze all objects in the permanent generation.
1918
1919Put all objects in the permanent generation back into oldest generation.
1920[clinic start generated code]*/
1921
1922static PyObject *
1923gc_unfreeze_impl(PyObject *module)
1924/*[clinic end generated code: output=1c15f2043b25e169 input=2dd52b170f4cef6c]*/
1925{
Victor Stinner67e0de62019-11-20 11:48:18 +01001926 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner72474072019-11-20 12:25:50 +01001927 GCState *gcstate = &tstate->interp->gc;
Victor Stinner67e0de62019-11-20 11:48:18 +01001928 gc_list_merge(&gcstate->permanent_generation.head,
1929 GEN_HEAD(gcstate, NUM_GENERATIONS-1));
brainfvckc75edab2017-10-16 12:49:41 -07001930 Py_RETURN_NONE;
1931}
1932
1933/*[clinic input]
Victor Stinner05d68a82018-01-18 11:15:25 +01001934gc.get_freeze_count -> Py_ssize_t
brainfvckc75edab2017-10-16 12:49:41 -07001935
1936Return the number of objects in the permanent generation.
1937[clinic start generated code]*/
1938
Victor Stinner05d68a82018-01-18 11:15:25 +01001939static Py_ssize_t
brainfvckc75edab2017-10-16 12:49:41 -07001940gc_get_freeze_count_impl(PyObject *module)
Victor Stinner05d68a82018-01-18 11:15:25 +01001941/*[clinic end generated code: output=61cbd9f43aa032e1 input=45ffbc65cfe2a6ed]*/
brainfvckc75edab2017-10-16 12:49:41 -07001942{
Victor Stinner67e0de62019-11-20 11:48:18 +01001943 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner72474072019-11-20 12:25:50 +01001944 GCState *gcstate = &tstate->interp->gc;
Victor Stinner67e0de62019-11-20 11:48:18 +01001945 return gc_list_size(&gcstate->permanent_generation.head);
brainfvckc75edab2017-10-16 12:49:41 -07001946}
1947
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +00001948
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001949PyDoc_STRVAR(gc__doc__,
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001950"This module provides access to the garbage collector for reference cycles.\n"
1951"\n"
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001952"enable() -- Enable automatic garbage collection.\n"
1953"disable() -- Disable automatic garbage collection.\n"
1954"isenabled() -- Returns true if automatic collection is enabled.\n"
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001955"collect() -- Do a full collection right now.\n"
Thomas Wouters89f507f2006-12-13 04:49:30 +00001956"get_count() -- Return the current collection counts.\n"
R David Murray0e814632013-12-26 15:11:28 -05001957"get_stats() -- Return list of dictionaries containing per-generation stats.\n"
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001958"set_debug() -- Set debugging flags.\n"
1959"get_debug() -- Get debugging flags.\n"
1960"set_threshold() -- Set the collection thresholds.\n"
1961"get_threshold() -- Return the current the collection thresholds.\n"
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +00001962"get_objects() -- Return a list of all objects tracked by the collector.\n"
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001963"is_tracked() -- Returns true if a given object is tracked.\n"
Pablo Galindob6791372020-01-14 17:38:15 +00001964"is_finalized() -- Returns true if a given object has been already finalized.\n"
Jeremy Hylton5bd378b2003-04-03 16:28:38 +00001965"get_referrers() -- Return the list of objects that refer to an object.\n"
brainfvckc75edab2017-10-16 12:49:41 -07001966"get_referents() -- Return the list of objects that an object refers to.\n"
1967"freeze() -- Freeze all tracked objects and ignore them for future collections.\n"
1968"unfreeze() -- Unfreeze all objects in the permanent generation.\n"
1969"get_freeze_count() -- Return the number of objects in the permanent generation.\n");
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001970
1971static PyMethodDef GcMethods[] = {
Serhiy Storchaka93260282017-02-04 11:19:59 +02001972 GC_ENABLE_METHODDEF
1973 GC_DISABLE_METHODDEF
1974 GC_ISENABLED_METHODDEF
1975 GC_SET_DEBUG_METHODDEF
1976 GC_GET_DEBUG_METHODDEF
1977 GC_GET_COUNT_METHODDEF
Victor Stinner9db03242019-04-26 02:32:01 +02001978 {"set_threshold", gc_set_threshold, METH_VARARGS, gc_set_thresh__doc__},
Serhiy Storchaka93260282017-02-04 11:19:59 +02001979 GC_GET_THRESHOLD_METHODDEF
1980 GC_COLLECT_METHODDEF
1981 GC_GET_OBJECTS_METHODDEF
1982 GC_GET_STATS_METHODDEF
1983 GC_IS_TRACKED_METHODDEF
Pablo Galindoa2ec3f02020-01-14 12:06:45 +00001984 GC_IS_FINALIZED_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001985 {"get_referrers", gc_get_referrers, METH_VARARGS,
1986 gc_get_referrers__doc__},
1987 {"get_referents", gc_get_referents, METH_VARARGS,
1988 gc_get_referents__doc__},
brainfvckc75edab2017-10-16 12:49:41 -07001989 GC_FREEZE_METHODDEF
1990 GC_UNFREEZE_METHODDEF
1991 GC_GET_FREEZE_COUNT_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001992 {NULL, NULL} /* Sentinel */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001993};
1994
Martin v. Löwis1a214512008-06-11 05:26:20 +00001995static struct PyModuleDef gcmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001996 PyModuleDef_HEAD_INIT,
Antoine Pitrou696e0352010-08-08 22:18:46 +00001997 "gc", /* m_name */
1998 gc__doc__, /* m_doc */
1999 -1, /* m_size */
2000 GcMethods, /* m_methods */
2001 NULL, /* m_reload */
2002 NULL, /* m_traverse */
2003 NULL, /* m_clear */
2004 NULL /* m_free */
Martin v. Löwis1a214512008-06-11 05:26:20 +00002005};
2006
Jason Tishler6bc06ec2003-09-04 11:59:50 +00002007PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00002008PyInit_gc(void)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00002009{
Victor Stinner72474072019-11-20 12:25:50 +01002010 PyThreadState *tstate = _PyThreadState_GET();
2011 GCState *gcstate = &tstate->interp->gc;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00002012
Victor Stinner72474072019-11-20 12:25:50 +01002013 PyObject *m = PyModule_Create(&gcmodule);
Martin v. Löwis1a214512008-06-11 05:26:20 +00002014
Victor Stinner9db03242019-04-26 02:32:01 +02002015 if (m == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002016 return NULL;
Victor Stinner9db03242019-04-26 02:32:01 +02002017 }
Tim Peters11558872003-04-06 23:30:52 +00002018
Victor Stinner67e0de62019-11-20 11:48:18 +01002019 if (gcstate->garbage == NULL) {
2020 gcstate->garbage = PyList_New(0);
Victor Stinner72474072019-11-20 12:25:50 +01002021 if (gcstate->garbage == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002022 return NULL;
Victor Stinner72474072019-11-20 12:25:50 +01002023 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002024 }
Victor Stinner67e0de62019-11-20 11:48:18 +01002025 Py_INCREF(gcstate->garbage);
Victor Stinner72474072019-11-20 12:25:50 +01002026 if (PyModule_AddObject(m, "garbage", gcstate->garbage) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002027 return NULL;
Victor Stinner72474072019-11-20 12:25:50 +01002028 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002029
Victor Stinner67e0de62019-11-20 11:48:18 +01002030 if (gcstate->callbacks == NULL) {
2031 gcstate->callbacks = PyList_New(0);
Victor Stinner72474072019-11-20 12:25:50 +01002032 if (gcstate->callbacks == NULL) {
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00002033 return NULL;
Victor Stinner72474072019-11-20 12:25:50 +01002034 }
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00002035 }
Victor Stinner67e0de62019-11-20 11:48:18 +01002036 Py_INCREF(gcstate->callbacks);
Victor Stinner72474072019-11-20 12:25:50 +01002037 if (PyModule_AddObject(m, "callbacks", gcstate->callbacks) < 0) {
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00002038 return NULL;
Victor Stinner72474072019-11-20 12:25:50 +01002039 }
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00002040
Victor Stinner72474072019-11-20 12:25:50 +01002041#define ADD_INT(NAME) if (PyModule_AddIntConstant(m, #NAME, NAME) < 0) { return NULL; }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002042 ADD_INT(DEBUG_STATS);
2043 ADD_INT(DEBUG_COLLECTABLE);
2044 ADD_INT(DEBUG_UNCOLLECTABLE);
2045 ADD_INT(DEBUG_SAVEALL);
2046 ADD_INT(DEBUG_LEAK);
Tim Peters11558872003-04-06 23:30:52 +00002047#undef ADD_INT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002048 return m;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00002049}
2050
Guido van Rossume13ddc92003-04-17 17:29:22 +00002051/* API to invoke gc.collect() from C */
Neal Norwitz7b216c52006-03-04 20:01:53 +00002052Py_ssize_t
Guido van Rossume13ddc92003-04-17 17:29:22 +00002053PyGC_Collect(void)
2054{
Victor Stinner2e969062019-11-20 01:49:32 +01002055 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner72474072019-11-20 12:25:50 +01002056 GCState *gcstate = &tstate->interp->gc;
Victor Stinner2e969062019-11-20 01:49:32 +01002057
Victor Stinner67e0de62019-11-20 11:48:18 +01002058 if (!gcstate->enabled) {
Victor Stinner9db03242019-04-26 02:32:01 +02002059 return 0;
2060 }
Guido van Rossume13ddc92003-04-17 17:29:22 +00002061
Victor Stinner9db03242019-04-26 02:32:01 +02002062 Py_ssize_t n;
Victor Stinner67e0de62019-11-20 11:48:18 +01002063 if (gcstate->collecting) {
Victor Stinner9db03242019-04-26 02:32:01 +02002064 /* already collecting, don't do anything */
2065 n = 0;
2066 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002067 else {
Serhiy Storchaka301e3cc2018-05-24 15:19:29 +03002068 PyObject *exc, *value, *tb;
Victor Stinner67e0de62019-11-20 11:48:18 +01002069 gcstate->collecting = 1;
Victor Stinner2e969062019-11-20 01:49:32 +01002070 _PyErr_Fetch(tstate, &exc, &value, &tb);
Victor Stinner67e0de62019-11-20 11:48:18 +01002071 n = collect_with_callback(tstate, NUM_GENERATIONS - 1);
Victor Stinner2e969062019-11-20 01:49:32 +01002072 _PyErr_Restore(tstate, exc, value, tb);
Victor Stinner67e0de62019-11-20 11:48:18 +01002073 gcstate->collecting = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002074 }
Guido van Rossume13ddc92003-04-17 17:29:22 +00002075
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002076 return n;
Guido van Rossume13ddc92003-04-17 17:29:22 +00002077}
2078
Antoine Pitroufef34e32013-05-19 01:11:58 +02002079Py_ssize_t
Łukasz Langafef7e942016-09-09 21:47:46 -07002080_PyGC_CollectIfEnabled(void)
2081{
Łukasz Langafef7e942016-09-09 21:47:46 -07002082 return PyGC_Collect();
2083}
2084
2085Py_ssize_t
Antoine Pitroufef34e32013-05-19 01:11:58 +02002086_PyGC_CollectNoFail(void)
2087{
Victor Stinner2e969062019-11-20 01:49:32 +01002088 PyThreadState *tstate = _PyThreadState_GET();
2089 assert(!_PyErr_Occurred(tstate));
Victor Stinner9db03242019-04-26 02:32:01 +02002090
Victor Stinner72474072019-11-20 12:25:50 +01002091 GCState *gcstate = &tstate->interp->gc;
Antoine Pitroufef34e32013-05-19 01:11:58 +02002092 Py_ssize_t n;
2093
Antoine Pitrouc69c9bc2013-08-15 20:15:15 +02002094 /* Ideally, this function is only called on interpreter shutdown,
2095 and therefore not recursively. Unfortunately, when there are daemon
2096 threads, a daemon thread can start a cyclic garbage collection
2097 during interpreter shutdown (and then never finish it).
2098 See http://bugs.python.org/issue8713#msg195178 for an example.
2099 */
Victor Stinner67e0de62019-11-20 11:48:18 +01002100 if (gcstate->collecting) {
Antoine Pitrouc69c9bc2013-08-15 20:15:15 +02002101 n = 0;
Victor Stinner9db03242019-04-26 02:32:01 +02002102 }
Antoine Pitrouc69c9bc2013-08-15 20:15:15 +02002103 else {
Victor Stinner67e0de62019-11-20 11:48:18 +01002104 gcstate->collecting = 1;
2105 n = collect(tstate, NUM_GENERATIONS - 1, NULL, NULL, 1);
2106 gcstate->collecting = 0;
Antoine Pitrouc69c9bc2013-08-15 20:15:15 +02002107 }
Antoine Pitroufef34e32013-05-19 01:11:58 +02002108 return n;
2109}
Antoine Pitrou5f454a02013-05-06 21:15:57 +02002110
Antoine Pitrou696e0352010-08-08 22:18:46 +00002111void
Victor Stinner67e0de62019-11-20 11:48:18 +01002112_PyGC_DumpShutdownStats(PyThreadState *tstate)
Antoine Pitrou696e0352010-08-08 22:18:46 +00002113{
Victor Stinner72474072019-11-20 12:25:50 +01002114 GCState *gcstate = &tstate->interp->gc;
Victor Stinner67e0de62019-11-20 11:48:18 +01002115 if (!(gcstate->debug & DEBUG_SAVEALL)
2116 && gcstate->garbage != NULL && PyList_GET_SIZE(gcstate->garbage) > 0) {
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02002117 const char *message;
Victor Stinner67e0de62019-11-20 11:48:18 +01002118 if (gcstate->debug & DEBUG_UNCOLLECTABLE)
Antoine Pitroub5d82042010-11-05 00:05:25 +00002119 message = "gc: %zd uncollectable objects at " \
Georg Brandl08be72d2010-10-24 15:11:22 +00002120 "shutdown";
2121 else
Antoine Pitroub5d82042010-11-05 00:05:25 +00002122 message = "gc: %zd uncollectable objects at " \
Georg Brandl08be72d2010-10-24 15:11:22 +00002123 "shutdown; use gc.set_debug(gc.DEBUG_UNCOLLECTABLE) to list them";
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02002124 /* PyErr_WarnFormat does too many things and we are at shutdown,
2125 the warnings module's dependencies (e.g. linecache) may be gone
2126 already. */
2127 if (PyErr_WarnExplicitFormat(PyExc_ResourceWarning, "gc", 0,
2128 "gc", NULL, message,
Victor Stinner67e0de62019-11-20 11:48:18 +01002129 PyList_GET_SIZE(gcstate->garbage)))
Georg Brandl08be72d2010-10-24 15:11:22 +00002130 PyErr_WriteUnraisable(NULL);
Victor Stinner67e0de62019-11-20 11:48:18 +01002131 if (gcstate->debug & DEBUG_UNCOLLECTABLE) {
Antoine Pitrou696e0352010-08-08 22:18:46 +00002132 PyObject *repr = NULL, *bytes = NULL;
Victor Stinner67e0de62019-11-20 11:48:18 +01002133 repr = PyObject_Repr(gcstate->garbage);
Antoine Pitrou696e0352010-08-08 22:18:46 +00002134 if (!repr || !(bytes = PyUnicode_EncodeFSDefault(repr)))
Victor Stinner67e0de62019-11-20 11:48:18 +01002135 PyErr_WriteUnraisable(gcstate->garbage);
Antoine Pitrou696e0352010-08-08 22:18:46 +00002136 else {
2137 PySys_WriteStderr(
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02002138 " %s\n",
Antoine Pitrou696e0352010-08-08 22:18:46 +00002139 PyBytes_AS_STRING(bytes)
2140 );
2141 }
2142 Py_XDECREF(repr);
2143 Py_XDECREF(bytes);
2144 }
Antoine Pitrou696e0352010-08-08 22:18:46 +00002145 }
Antoine Pitrou5f454a02013-05-06 21:15:57 +02002146}
2147
2148void
Victor Stinner7eee5be2019-11-20 10:38:34 +01002149_PyGC_Fini(PyThreadState *tstate)
Antoine Pitrou5f454a02013-05-06 21:15:57 +02002150{
Victor Stinner72474072019-11-20 12:25:50 +01002151 GCState *gcstate = &tstate->interp->gc;
Victor Stinner67e0de62019-11-20 11:48:18 +01002152 Py_CLEAR(gcstate->garbage);
2153 Py_CLEAR(gcstate->callbacks);
Antoine Pitrou696e0352010-08-08 22:18:46 +00002154}
2155
Neil Schemenauer43411b52001-08-30 00:05:51 +00002156/* for debugging */
Guido van Rossume13ddc92003-04-17 17:29:22 +00002157void
2158_PyGC_Dump(PyGC_Head *g)
Neil Schemenauer43411b52001-08-30 00:05:51 +00002159{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002160 _PyObject_Dump(FROM_GC(g));
Neil Schemenauer43411b52001-08-30 00:05:51 +00002161}
2162
Victor Stinnera5447732019-10-10 09:32:13 +02002163
2164#ifdef Py_DEBUG
Victor Stinner1b184552019-10-08 00:09:31 +02002165static int
2166visit_validate(PyObject *op, void *parent_raw)
2167{
2168 PyObject *parent = _PyObject_CAST(parent_raw);
2169 if (_PyObject_IsFreed(op)) {
2170 _PyObject_ASSERT_FAILED_MSG(parent,
2171 "PyObject_GC_Track() object is not valid");
2172 }
2173 return 0;
2174}
Victor Stinnera5447732019-10-10 09:32:13 +02002175#endif
Victor Stinner1b184552019-10-08 00:09:31 +02002176
2177
Neil Schemenauer43411b52001-08-30 00:05:51 +00002178/* extension modules might be compiled with GC support so these
2179 functions must always be available */
2180
2181void
Victor Stinnera42de742018-11-22 10:25:22 +01002182PyObject_GC_Track(void *op_raw)
Neil Schemenauer43411b52001-08-30 00:05:51 +00002183{
Victor Stinnera42de742018-11-22 10:25:22 +01002184 PyObject *op = _PyObject_CAST(op_raw);
Victor Stinner271753a2018-11-22 01:02:54 +01002185 if (_PyObject_GC_IS_TRACKED(op)) {
2186 _PyObject_ASSERT_FAILED_MSG(op,
2187 "object already tracked "
2188 "by the garbage collector");
2189 }
Victor Stinnera42de742018-11-22 10:25:22 +01002190 _PyObject_GC_TRACK(op);
Victor Stinner1b184552019-10-08 00:09:31 +02002191
2192#ifdef Py_DEBUG
2193 /* Check that the object is valid: validate objects traversed
2194 by tp_traverse() */
2195 traverseproc traverse = Py_TYPE(op)->tp_traverse;
2196 (void)traverse(op, visit_validate, op);
2197#endif
Neil Schemenauer43411b52001-08-30 00:05:51 +00002198}
2199
Neil Schemenauerfec4eb12002-04-12 02:41:03 +00002200void
Victor Stinnera42de742018-11-22 10:25:22 +01002201PyObject_GC_UnTrack(void *op_raw)
Neil Schemenauer43411b52001-08-30 00:05:51 +00002202{
Victor Stinnera42de742018-11-22 10:25:22 +01002203 PyObject *op = _PyObject_CAST(op_raw);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002204 /* Obscure: the Py_TRASHCAN mechanism requires that we be able to
2205 * call PyObject_GC_UnTrack twice on an object.
2206 */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002207 if (_PyObject_GC_IS_TRACKED(op)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002208 _PyObject_GC_UNTRACK(op);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002209 }
Neil Schemenauer43411b52001-08-30 00:05:51 +00002210}
2211
Hai Shi675d9a32020-04-15 02:11:20 +08002212int
2213PyObject_IS_GC(PyObject *obj)
2214{
2215 return _PyObject_IS_GC(obj);
2216}
2217
Victor Stinnerdb067af2014-05-02 22:31:14 +02002218static PyObject *
2219_PyObject_GC_Alloc(int use_calloc, size_t basicsize)
Neil Schemenauer43411b52001-08-30 00:05:51 +00002220{
Victor Stinner67e0de62019-11-20 11:48:18 +01002221 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner72474072019-11-20 12:25:50 +01002222 GCState *gcstate = &tstate->interp->gc;
Victor Stinner2e969062019-11-20 01:49:32 +01002223 if (basicsize > PY_SSIZE_T_MAX - sizeof(PyGC_Head)) {
Victor Stinner67e0de62019-11-20 11:48:18 +01002224 return _PyErr_NoMemory(tstate);
Victor Stinner2e969062019-11-20 01:49:32 +01002225 }
2226 size_t size = sizeof(PyGC_Head) + basicsize;
2227
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002228 PyGC_Head *g;
Victor Stinner2e969062019-11-20 01:49:32 +01002229 if (use_calloc) {
Victor Stinnerdb067af2014-05-02 22:31:14 +02002230 g = (PyGC_Head *)PyObject_Calloc(1, size);
Victor Stinner2e969062019-11-20 01:49:32 +01002231 }
2232 else {
Victor Stinnerdb067af2014-05-02 22:31:14 +02002233 g = (PyGC_Head *)PyObject_Malloc(size);
Victor Stinner2e969062019-11-20 01:49:32 +01002234 }
2235 if (g == NULL) {
Victor Stinner67e0de62019-11-20 11:48:18 +01002236 return _PyErr_NoMemory(tstate);
Victor Stinner2e969062019-11-20 01:49:32 +01002237 }
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002238 assert(((uintptr_t)g & 3) == 0); // g must be aligned 4bytes boundary
Victor Stinner2e969062019-11-20 01:49:32 +01002239
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002240 g->_gc_next = 0;
2241 g->_gc_prev = 0;
Victor Stinner67e0de62019-11-20 11:48:18 +01002242 gcstate->generations[0].count++; /* number of allocated GC objects */
2243 if (gcstate->generations[0].count > gcstate->generations[0].threshold &&
2244 gcstate->enabled &&
2245 gcstate->generations[0].threshold &&
2246 !gcstate->collecting &&
2247 !_PyErr_Occurred(tstate))
Victor Stinner2e969062019-11-20 01:49:32 +01002248 {
Victor Stinner67e0de62019-11-20 11:48:18 +01002249 gcstate->collecting = 1;
2250 collect_generations(tstate);
2251 gcstate->collecting = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002252 }
Victor Stinner2e969062019-11-20 01:49:32 +01002253 PyObject *op = FROM_GC(g);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002254 return op;
Neil Schemenauer43411b52001-08-30 00:05:51 +00002255}
2256
2257PyObject *
Victor Stinnerdb067af2014-05-02 22:31:14 +02002258_PyObject_GC_Malloc(size_t basicsize)
2259{
2260 return _PyObject_GC_Alloc(0, basicsize);
2261}
2262
2263PyObject *
2264_PyObject_GC_Calloc(size_t basicsize)
2265{
2266 return _PyObject_GC_Alloc(1, basicsize);
2267}
2268
2269PyObject *
Neil Schemenauer43411b52001-08-30 00:05:51 +00002270_PyObject_GC_New(PyTypeObject *tp)
2271{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002272 PyObject *op = _PyObject_GC_Malloc(_PyObject_SIZE(tp));
2273 if (op != NULL)
2274 op = PyObject_INIT(op, tp);
2275 return op;
Neil Schemenauer43411b52001-08-30 00:05:51 +00002276}
2277
2278PyVarObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00002279_PyObject_GC_NewVar(PyTypeObject *tp, Py_ssize_t nitems)
Neil Schemenauer43411b52001-08-30 00:05:51 +00002280{
Victor Stinner5d1866c2013-07-08 22:17:52 +02002281 size_t size;
2282 PyVarObject *op;
2283
2284 if (nitems < 0) {
2285 PyErr_BadInternalCall();
2286 return NULL;
2287 }
2288 size = _PyObject_VAR_SIZE(tp, nitems);
2289 op = (PyVarObject *) _PyObject_GC_Malloc(size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002290 if (op != NULL)
2291 op = PyObject_INIT_VAR(op, tp, nitems);
2292 return op;
Neil Schemenauer43411b52001-08-30 00:05:51 +00002293}
2294
2295PyVarObject *
Martin v. Löwis41290682006-02-16 14:56:14 +00002296_PyObject_GC_Resize(PyVarObject *op, Py_ssize_t nitems)
Neil Schemenauer43411b52001-08-30 00:05:51 +00002297{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002298 const size_t basicsize = _PyObject_VAR_SIZE(Py_TYPE(op), nitems);
Victor Stinnera4b2bc72018-10-26 18:00:13 +02002299 _PyObject_ASSERT((PyObject *)op, !_PyObject_GC_IS_TRACKED(op));
2300 if (basicsize > PY_SSIZE_T_MAX - sizeof(PyGC_Head)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002301 return (PyVarObject *)PyErr_NoMemory();
Victor Stinnera4b2bc72018-10-26 18:00:13 +02002302 }
2303
2304 PyGC_Head *g = AS_GC(op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002305 g = (PyGC_Head *)PyObject_REALLOC(g, sizeof(PyGC_Head) + basicsize);
2306 if (g == NULL)
2307 return (PyVarObject *)PyErr_NoMemory();
2308 op = (PyVarObject *) FROM_GC(g);
Victor Stinner60ac6ed2020-02-07 23:18:08 +01002309 Py_SET_SIZE(op, nitems);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002310 return op;
Neil Schemenauer43411b52001-08-30 00:05:51 +00002311}
2312
2313void
Neil Schemenauerfec4eb12002-04-12 02:41:03 +00002314PyObject_GC_Del(void *op)
Neil Schemenauer43411b52001-08-30 00:05:51 +00002315{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002316 PyGC_Head *g = AS_GC(op);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002317 if (_PyObject_GC_IS_TRACKED(op)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002318 gc_list_remove(g);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002319 }
Victor Stinner67e0de62019-11-20 11:48:18 +01002320 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner72474072019-11-20 12:25:50 +01002321 GCState *gcstate = &tstate->interp->gc;
Victor Stinner67e0de62019-11-20 11:48:18 +01002322 if (gcstate->generations[0].count > 0) {
2323 gcstate->generations[0].count--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002324 }
2325 PyObject_FREE(g);
Neil Schemenauer43411b52001-08-30 00:05:51 +00002326}
Pablo Galindof13072b2020-04-11 01:21:54 +01002327
2328int
2329PyObject_GC_IsTracked(PyObject* obj)
2330{
Hai Shi675d9a32020-04-15 02:11:20 +08002331 if (_PyObject_IS_GC(obj) && _PyObject_GC_IS_TRACKED(obj)) {
Pablo Galindof13072b2020-04-11 01:21:54 +01002332 return 1;
2333 }
2334 return 0;
2335}
2336
2337int
2338PyObject_GC_IsFinalized(PyObject *obj)
2339{
Hai Shi675d9a32020-04-15 02:11:20 +08002340 if (_PyObject_IS_GC(obj) && _PyGCHead_FINALIZED(AS_GC(obj))) {
Pablo Galindof13072b2020-04-11 01:21:54 +01002341 return 1;
2342 }
2343 return 0;
2344}