blob: 78f6631866ac47c9fdac8a7ca672f27d7a70bcd7 [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 Stinnerbcda8f12018-11-21 22:27:47 +010029#include "pycore_object.h"
Victor Stinner2e969062019-11-20 01:49:32 +010030#include "pycore_pyerrors.h"
Victor Stinner621cebe2018-11-12 16:53:38 +010031#include "pycore_pymem.h"
32#include "pycore_pystate.h"
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000033#include "frameobject.h" /* for PyFrame_ClearFreeList */
Łukasz Langaa785c872016-09-09 17:37:37 -070034#include "pydtrace.h"
Victor Stinner7181dec2015-03-27 17:47:53 +010035#include "pytime.h" /* for _PyTime_GetMonotonicClock() */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000036
Serhiy Storchaka93260282017-02-04 11:19:59 +020037/*[clinic input]
38module gc
39[clinic start generated code]*/
40/*[clinic end generated code: output=da39a3ee5e6b4b0d input=b5c9690ecc842d79]*/
41
Pablo Galindo320dd502019-10-10 22:45:17 +010042
43#ifdef Py_DEBUG
44# define GC_DEBUG
45#endif
INADA Naoki5ac9e6e2018-07-10 17:19:53 +090046
47#define GC_NEXT _PyGCHead_NEXT
48#define GC_PREV _PyGCHead_PREV
49
50// update_refs() set this bit for all objects in current generation.
51// subtract_refs() and move_unreachable() uses this to distinguish
52// visited object is in GCing or not.
53//
54// move_unreachable() removes this flag from reachable objects.
55// Only unreachable objects have this flag.
56//
57// No objects in interpreter have this flag after GC ends.
58#define PREV_MASK_COLLECTING _PyGC_PREV_MASK_COLLECTING
59
60// Lowest bit of _gc_next is used for UNREACHABLE flag.
61//
62// This flag represents the object is in unreachable list in move_unreachable()
63//
64// Although this flag is used only in move_unreachable(), move_unreachable()
65// doesn't clear this flag to skip unnecessary iteration.
66// move_legacy_finalizers() removes this flag instead.
67// Between them, unreachable list is not normal list and we can not use
68// most gc_list_* functions for it.
69#define NEXT_MASK_UNREACHABLE (1)
70
Victor Stinner626bff82018-10-25 17:31:10 +020071/* Get an object's GC head */
72#define AS_GC(o) ((PyGC_Head *)(o)-1)
73
74/* Get the object given the GC head */
75#define FROM_GC(g) ((PyObject *)(((PyGC_Head *)g)+1))
76
INADA Naoki5ac9e6e2018-07-10 17:19:53 +090077static inline int
78gc_is_collecting(PyGC_Head *g)
79{
80 return (g->_gc_prev & PREV_MASK_COLLECTING) != 0;
81}
82
83static inline void
84gc_clear_collecting(PyGC_Head *g)
85{
86 g->_gc_prev &= ~PREV_MASK_COLLECTING;
87}
88
89static inline Py_ssize_t
90gc_get_refs(PyGC_Head *g)
91{
92 return (Py_ssize_t)(g->_gc_prev >> _PyGC_PREV_SHIFT);
93}
94
95static inline void
96gc_set_refs(PyGC_Head *g, Py_ssize_t refs)
97{
98 g->_gc_prev = (g->_gc_prev & ~_PyGC_PREV_MASK)
99 | ((uintptr_t)(refs) << _PyGC_PREV_SHIFT);
100}
101
102static inline void
103gc_reset_refs(PyGC_Head *g, Py_ssize_t refs)
104{
105 g->_gc_prev = (g->_gc_prev & _PyGC_PREV_MASK_FINALIZED)
106 | PREV_MASK_COLLECTING
107 | ((uintptr_t)(refs) << _PyGC_PREV_SHIFT);
108}
109
110static inline void
111gc_decref(PyGC_Head *g)
112{
Victor Stinner626bff82018-10-25 17:31:10 +0200113 _PyObject_ASSERT_WITH_MSG(FROM_GC(g),
114 gc_get_refs(g) > 0,
115 "refcount is too small");
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900116 g->_gc_prev -= 1 << _PyGC_PREV_SHIFT;
117}
118
Tim Peters6fc13d92002-07-02 18:12:35 +0000119/* Python string to use if unhandled exception occurs */
Tim Petersbf384c22003-04-06 00:11:39 +0000120static PyObject *gc_str = NULL;
Tim Peters6fc13d92002-07-02 18:12:35 +0000121
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000122/* set for debugging information */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000123#define DEBUG_STATS (1<<0) /* print collection statistics */
124#define DEBUG_COLLECTABLE (1<<1) /* print collectable objects */
125#define DEBUG_UNCOLLECTABLE (1<<2) /* print uncollectable objects */
126#define DEBUG_SAVEALL (1<<5) /* save all garbage in gc.garbage */
127#define DEBUG_LEAK DEBUG_COLLECTABLE | \
128 DEBUG_UNCOLLECTABLE | \
129 DEBUG_SAVEALL
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000130
Victor Stinner9db03242019-04-26 02:32:01 +0200131#define GEN_HEAD(state, n) (&(state)->generations[n].head)
Antoine Pitroud4156c12012-10-30 22:43:19 +0100132
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600133void
Victor Stinner444b39b2019-11-20 01:18:11 +0100134_PyGC_InitializeRuntime(struct _gc_runtime_state *state)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600135{
136 state->enabled = 1; /* automatic collection enabled? */
137
Victor Stinner9db03242019-04-26 02:32:01 +0200138#define _GEN_HEAD(n) GEN_HEAD(state, n)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600139 struct gc_generation generations[NUM_GENERATIONS] = {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900140 /* PyGC_Head, threshold, count */
141 {{(uintptr_t)_GEN_HEAD(0), (uintptr_t)_GEN_HEAD(0)}, 700, 0},
142 {{(uintptr_t)_GEN_HEAD(1), (uintptr_t)_GEN_HEAD(1)}, 10, 0},
143 {{(uintptr_t)_GEN_HEAD(2), (uintptr_t)_GEN_HEAD(2)}, 10, 0},
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600144 };
145 for (int i = 0; i < NUM_GENERATIONS; i++) {
146 state->generations[i] = generations[i];
147 };
Victor Stinner9db03242019-04-26 02:32:01 +0200148 state->generation0 = GEN_HEAD(state, 0);
brainfvckc75edab2017-10-16 12:49:41 -0700149 struct gc_generation permanent_generation = {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900150 {(uintptr_t)&state->permanent_generation.head,
151 (uintptr_t)&state->permanent_generation.head}, 0, 0
brainfvckc75edab2017-10-16 12:49:41 -0700152 };
153 state->permanent_generation = permanent_generation;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600154}
Antoine Pitroud4156c12012-10-30 22:43:19 +0100155
Victor Stinner444b39b2019-11-20 01:18:11 +0100156
157PyStatus
158_PyGC_Init(_PyRuntimeState *runtime)
159{
160 struct _gc_runtime_state *state = &runtime->gc;
161 if (state->garbage == NULL) {
162 state->garbage = PyList_New(0);
163 if (state->garbage == NULL) {
164 return _PyStatus_NO_MEMORY();
165 }
166 }
167 return _PyStatus_OK();
168}
169
170
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900171/*
172_gc_prev values
173---------------
Neil Schemenauer43411b52001-08-30 00:05:51 +0000174
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900175Between collections, _gc_prev is used for doubly linked list.
Tim Peters6fc13d92002-07-02 18:12:35 +0000176
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900177Lowest two bits of _gc_prev are used for flags.
178PREV_MASK_COLLECTING is used only while collecting and cleared before GC ends
179or _PyObject_GC_UNTRACK() is called.
Tim Peters6fc13d92002-07-02 18:12:35 +0000180
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900181During a collection, _gc_prev is temporary used for gc_refs, and the gc list
182is singly linked until _gc_prev is restored.
Tim Peters6fc13d92002-07-02 18:12:35 +0000183
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900184gc_refs
Tim Peters6fc13d92002-07-02 18:12:35 +0000185 At the start of a collection, update_refs() copies the true refcount
186 to gc_refs, for each object in the generation being collected.
187 subtract_refs() then adjusts gc_refs so that it equals the number of
188 times an object is referenced directly from outside the generation
189 being collected.
Tim Peters6fc13d92002-07-02 18:12:35 +0000190
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900191PREV_MASK_COLLECTING
192 Objects in generation being collected are marked PREV_MASK_COLLECTING in
193 update_refs().
194
195
196_gc_next values
197---------------
198
199_gc_next takes these values:
200
2010
202 The object is not tracked
203
204!= 0
205 Pointer to the next object in the GC list.
206 Additionally, lowest bit is used temporary for
207 NEXT_MASK_UNREACHABLE flag described below.
208
209NEXT_MASK_UNREACHABLE
Tim Peters6fc13d92002-07-02 18:12:35 +0000210 move_unreachable() then moves objects not reachable (whether directly or
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900211 indirectly) from outside the generation into an "unreachable" set and
212 set this flag.
Tim Peters6fc13d92002-07-02 18:12:35 +0000213
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900214 Objects that are found to be reachable have gc_refs set to 1.
215 When this flag is set for the reachable object, the object must be in
216 "unreachable" set.
217 The flag is unset and the object is moved back to "reachable" set.
218
219 move_legacy_finalizers() will remove this flag from "unreachable" set.
Tim Peters6fc13d92002-07-02 18:12:35 +0000220*/
Neil Schemenauera2b11ec2002-05-21 15:53:24 +0000221
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000222/*** list functions ***/
223
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900224static inline void
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000225gc_list_init(PyGC_Head *list)
226{
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900227 // List header must not have flags.
228 // We can assign pointer by simple cast.
229 list->_gc_prev = (uintptr_t)list;
230 list->_gc_next = (uintptr_t)list;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000231}
232
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900233static inline int
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000234gc_list_is_empty(PyGC_Head *list)
235{
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900236 return (list->_gc_next == (uintptr_t)list);
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000237}
238
Tim Peterse2d59182004-11-01 01:39:08 +0000239/* Append `node` to `list`. */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900240static inline void
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000241gc_list_append(PyGC_Head *node, PyGC_Head *list)
242{
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900243 PyGC_Head *last = (PyGC_Head *)list->_gc_prev;
244
245 // last <-> node
246 _PyGCHead_SET_PREV(node, last);
247 _PyGCHead_SET_NEXT(last, node);
248
249 // node <-> list
250 _PyGCHead_SET_NEXT(node, list);
251 list->_gc_prev = (uintptr_t)node;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000252}
253
Tim Peterse2d59182004-11-01 01:39:08 +0000254/* Remove `node` from the gc list it's currently in. */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900255static inline void
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000256gc_list_remove(PyGC_Head *node)
257{
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900258 PyGC_Head *prev = GC_PREV(node);
259 PyGC_Head *next = GC_NEXT(node);
260
261 _PyGCHead_SET_NEXT(prev, next);
262 _PyGCHead_SET_PREV(next, prev);
263
264 node->_gc_next = 0; /* object is not currently tracked */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000265}
266
Tim Peterse2d59182004-11-01 01:39:08 +0000267/* Move `node` from the gc list it's currently in (which is not explicitly
268 * named here) to the end of `list`. This is semantically the same as
269 * gc_list_remove(node) followed by gc_list_append(node, list).
270 */
271static void
272gc_list_move(PyGC_Head *node, PyGC_Head *list)
273{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000274 /* Unlink from current list. */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900275 PyGC_Head *from_prev = GC_PREV(node);
276 PyGC_Head *from_next = GC_NEXT(node);
277 _PyGCHead_SET_NEXT(from_prev, from_next);
278 _PyGCHead_SET_PREV(from_next, from_prev);
279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000280 /* Relink at end of new list. */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900281 // list must not have flags. So we can skip macros.
282 PyGC_Head *to_prev = (PyGC_Head*)list->_gc_prev;
283 _PyGCHead_SET_PREV(node, to_prev);
284 _PyGCHead_SET_NEXT(to_prev, node);
285 list->_gc_prev = (uintptr_t)node;
286 _PyGCHead_SET_NEXT(node, list);
Tim Peterse2d59182004-11-01 01:39:08 +0000287}
288
289/* append list `from` onto list `to`; `from` becomes an empty list */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000290static void
291gc_list_merge(PyGC_Head *from, PyGC_Head *to)
292{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000293 assert(from != to);
294 if (!gc_list_is_empty(from)) {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900295 PyGC_Head *to_tail = GC_PREV(to);
296 PyGC_Head *from_head = GC_NEXT(from);
297 PyGC_Head *from_tail = GC_PREV(from);
298 assert(from_head != from);
299 assert(from_tail != from);
300
301 _PyGCHead_SET_NEXT(to_tail, from_head);
302 _PyGCHead_SET_PREV(from_head, to_tail);
303
304 _PyGCHead_SET_NEXT(from_tail, to);
305 _PyGCHead_SET_PREV(to, from_tail);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 }
307 gc_list_init(from);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000308}
309
Neal Norwitz7b216c52006-03-04 20:01:53 +0000310static Py_ssize_t
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000311gc_list_size(PyGC_Head *list)
312{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313 PyGC_Head *gc;
314 Py_ssize_t n = 0;
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900315 for (gc = GC_NEXT(list); gc != list; gc = GC_NEXT(gc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000316 n++;
317 }
318 return n;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000319}
320
Pablo Galindo466326d2019-10-13 16:48:59 +0100321/* Walk the list and mark all objects as non-collecting */
322static inline void
323gc_list_clear_collecting(PyGC_Head *collectable)
324{
325 PyGC_Head *gc;
326 for (gc = GC_NEXT(collectable); gc != collectable; gc = GC_NEXT(gc)) {
327 gc_clear_collecting(gc);
328 }
329}
330
Tim Peters259272b2003-04-06 19:41:39 +0000331/* Append objects in a GC list to a Python list.
Pablo Galindo466326d2019-10-13 16:48:59 +0100332 * Return 0 if all OK, < 0 if error (out of memory for list)
Tim Peters259272b2003-04-06 19:41:39 +0000333 */
334static int
335append_objects(PyObject *py_list, PyGC_Head *gc_list)
336{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000337 PyGC_Head *gc;
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900338 for (gc = GC_NEXT(gc_list); gc != gc_list; gc = GC_NEXT(gc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339 PyObject *op = FROM_GC(gc);
340 if (op != py_list) {
341 if (PyList_Append(py_list, op)) {
342 return -1; /* exception */
343 }
344 }
345 }
346 return 0;
Tim Peters259272b2003-04-06 19:41:39 +0000347}
348
Tim Petersea55c512019-10-18 20:59:14 -0500349// Constants for validate_list's flags argument.
350enum flagstates {collecting_clear_unreachable_clear,
351 collecting_clear_unreachable_set,
352 collecting_set_unreachable_clear,
353 collecting_set_unreachable_set};
354
Pablo Galindo320dd502019-10-10 22:45:17 +0100355#ifdef GC_DEBUG
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900356// validate_list checks list consistency. And it works as document
Tim Peters95bfc8a2019-10-13 16:47:04 -0500357// describing when flags are expected to be set / unset.
358// `head` must be a doubly-linked gc list, although it's fine (expected!) if
359// the prev and next pointers are "polluted" with flags.
360// What's checked:
361// - The `head` pointers are not polluted.
Tim Petersea55c512019-10-18 20:59:14 -0500362// - The objects' PREV_MASK_COLLECTING and NEXT_MASK_UNREACHABLE flags are all
363// `set or clear, as specified by the 'flags' argument.
Tim Peters95bfc8a2019-10-13 16:47:04 -0500364// - The prev and next pointers are mutually consistent.
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900365static void
Tim Petersea55c512019-10-18 20:59:14 -0500366validate_list(PyGC_Head *head, enum flagstates flags)
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900367{
Tim Peters95bfc8a2019-10-13 16:47:04 -0500368 assert((head->_gc_prev & PREV_MASK_COLLECTING) == 0);
369 assert((head->_gc_next & NEXT_MASK_UNREACHABLE) == 0);
Tim Petersea55c512019-10-18 20:59:14 -0500370 uintptr_t prev_value = 0, next_value = 0;
371 switch (flags) {
372 case collecting_clear_unreachable_clear:
373 break;
374 case collecting_set_unreachable_clear:
375 prev_value = PREV_MASK_COLLECTING;
376 break;
377 case collecting_clear_unreachable_set:
378 next_value = NEXT_MASK_UNREACHABLE;
379 break;
380 case collecting_set_unreachable_set:
381 prev_value = PREV_MASK_COLLECTING;
382 next_value = NEXT_MASK_UNREACHABLE;
383 break;
384 default:
385 assert(! "bad internal flags argument");
386 }
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900387 PyGC_Head *prev = head;
388 PyGC_Head *gc = GC_NEXT(head);
389 while (gc != head) {
Tim Peters95bfc8a2019-10-13 16:47:04 -0500390 PyGC_Head *trueprev = GC_PREV(gc);
391 PyGC_Head *truenext = (PyGC_Head *)(gc->_gc_next & ~NEXT_MASK_UNREACHABLE);
392 assert(truenext != NULL);
393 assert(trueprev == prev);
394 assert((gc->_gc_prev & PREV_MASK_COLLECTING) == prev_value);
395 assert((gc->_gc_next & NEXT_MASK_UNREACHABLE) == next_value);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900396 prev = gc;
Tim Peters95bfc8a2019-10-13 16:47:04 -0500397 gc = truenext;
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900398 }
399 assert(prev == GC_PREV(head));
400}
401#else
Tim Petersea55c512019-10-18 20:59:14 -0500402#define validate_list(x, y) do{}while(0)
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900403#endif
404
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000405/*** end of list stuff ***/
406
407
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900408/* Set all gc_refs = ob_refcnt. After this, gc_refs is > 0 and
409 * PREV_MASK_COLLECTING bit is set for all objects in containers.
Tim Peters88396172002-06-30 17:56:40 +0000410 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000411static void
412update_refs(PyGC_Head *containers)
413{
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900414 PyGC_Head *gc = GC_NEXT(containers);
415 for (; gc != containers; gc = GC_NEXT(gc)) {
416 gc_reset_refs(gc, Py_REFCNT(FROM_GC(gc)));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 /* Python's cyclic gc should never see an incoming refcount
418 * of 0: if something decref'ed to 0, it should have been
419 * deallocated immediately at that time.
420 * Possible cause (if the assert triggers): a tp_dealloc
421 * routine left a gc-aware object tracked during its teardown
422 * phase, and did something-- or allowed something to happen --
423 * that called back into Python. gc can trigger then, and may
424 * see the still-tracked dying object. Before this assert
425 * was added, such mistakes went on to allow gc to try to
426 * delete the object again. In a debug build, that caused
427 * a mysterious segfault, when _Py_ForgetReference tried
428 * to remove the object from the doubly-linked list of all
429 * objects a second time. In a release build, an actual
430 * double deallocation occurred, which leads to corruption
431 * of the allocator's internal bookkeeping pointers. That's
432 * so serious that maybe this should be a release-build
433 * check instead of an assert?
434 */
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200435 _PyObject_ASSERT(FROM_GC(gc), gc_get_refs(gc) != 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000437}
438
Tim Peters19b74c72002-07-01 03:52:19 +0000439/* A traversal callback for subtract_refs. */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000440static int
Victor Stinner4d5f94b2019-10-08 02:37:38 +0200441visit_decref(PyObject *op, void *parent)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000442{
Victor Stinner4d5f94b2019-10-08 02:37:38 +0200443 _PyObject_ASSERT(_PyObject_CAST(parent), !_PyObject_IsFreed(op));
Victor Stinnerd91d4de2019-09-09 17:44:59 +0200444
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000445 if (PyObject_IS_GC(op)) {
446 PyGC_Head *gc = AS_GC(op);
447 /* We're only interested in gc_refs for objects in the
448 * generation being collected, which can be recognized
449 * because only they have positive gc_refs.
450 */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900451 if (gc_is_collecting(gc)) {
452 gc_decref(gc);
453 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 }
455 return 0;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000456}
457
Tim Peters19b74c72002-07-01 03:52:19 +0000458/* Subtract internal references from gc_refs. After this, gc_refs is >= 0
459 * for all objects in containers, and is GC_REACHABLE for all tracked gc
460 * objects not in containers. The ones with gc_refs > 0 are directly
461 * reachable from outside containers, and so can't be collected.
462 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000463static void
464subtract_refs(PyGC_Head *containers)
465{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000466 traverseproc traverse;
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900467 PyGC_Head *gc = GC_NEXT(containers);
468 for (; gc != containers; gc = GC_NEXT(gc)) {
Victor Stinner4d5f94b2019-10-08 02:37:38 +0200469 PyObject *op = FROM_GC(gc);
470 traverse = Py_TYPE(op)->tp_traverse;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000471 (void) traverse(FROM_GC(gc),
472 (visitproc)visit_decref,
Victor Stinner4d5f94b2019-10-08 02:37:38 +0200473 op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000475}
476
Tim Peters19b74c72002-07-01 03:52:19 +0000477/* A traversal callback for move_unreachable. */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000478static int
Tim Peters19b74c72002-07-01 03:52:19 +0000479visit_reachable(PyObject *op, PyGC_Head *reachable)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000480{
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900481 if (!PyObject_IS_GC(op)) {
482 return 0;
483 }
Tim Peters19b74c72002-07-01 03:52:19 +0000484
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900485 PyGC_Head *gc = AS_GC(op);
486 const Py_ssize_t gc_refs = gc_get_refs(gc);
487
Tim Peters1e739452019-10-21 11:21:35 -0500488 // Ignore objects in other generation.
Tim Peters95bfc8a2019-10-13 16:47:04 -0500489 // This also skips objects "to the left" of the current position in
490 // move_unreachable's scan of the 'young' list - they've already been
491 // traversed, and no longer have the PREV_MASK_COLLECTING flag.
Tim Peters1e739452019-10-21 11:21:35 -0500492 if (! gc_is_collecting(gc)) {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900493 return 0;
494 }
Tim Peters1e739452019-10-21 11:21:35 -0500495 // It would be a logic error elsewhere if the collecting flag were set on
496 // an untracked object.
497 assert(gc->_gc_next != 0);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900498
499 if (gc->_gc_next & NEXT_MASK_UNREACHABLE) {
500 /* This had gc_refs = 0 when move_unreachable got
501 * to it, but turns out it's reachable after all.
502 * Move it back to move_unreachable's 'young' list,
503 * and move_unreachable will eventually get to it
504 * again.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 */
Tim Peters95bfc8a2019-10-13 16:47:04 -0500506 // Manually unlink gc from unreachable list because the list functions
507 // don't work right in the presence of NEXT_MASK_UNREACHABLE flags.
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900508 PyGC_Head *prev = GC_PREV(gc);
509 PyGC_Head *next = (PyGC_Head*)(gc->_gc_next & ~NEXT_MASK_UNREACHABLE);
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200510 _PyObject_ASSERT(FROM_GC(prev),
511 prev->_gc_next & NEXT_MASK_UNREACHABLE);
512 _PyObject_ASSERT(FROM_GC(next),
513 next->_gc_next & NEXT_MASK_UNREACHABLE);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900514 prev->_gc_next = gc->_gc_next; // copy NEXT_MASK_UNREACHABLE
515 _PyGCHead_SET_PREV(next, prev);
516
517 gc_list_append(gc, reachable);
518 gc_set_refs(gc, 1);
519 }
520 else if (gc_refs == 0) {
521 /* This is in move_unreachable's 'young' list, but
522 * the traversal hasn't yet gotten to it. All
523 * we need to do is tell move_unreachable that it's
524 * reachable.
525 */
526 gc_set_refs(gc, 1);
527 }
528 /* Else there's nothing to do.
529 * If gc_refs > 0, it must be in move_unreachable's 'young'
530 * list, and move_unreachable will eventually get to it.
531 */
532 else {
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200533 _PyObject_ASSERT_WITH_MSG(op, gc_refs > 0, "refcount is too small");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000534 }
535 return 0;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000536}
537
Tim Peters19b74c72002-07-01 03:52:19 +0000538/* Move the unreachable objects from young to unreachable. After this,
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900539 * all objects in young don't have PREV_MASK_COLLECTING flag and
540 * unreachable have the flag.
Tim Peters19b74c72002-07-01 03:52:19 +0000541 * All objects in young after this are directly or indirectly reachable
542 * from outside the original young; and all objects in unreachable are
543 * not.
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900544 *
545 * This function restores _gc_prev pointer. young and unreachable are
546 * doubly linked list after this function.
547 * But _gc_next in unreachable list has NEXT_MASK_UNREACHABLE flag.
548 * So we can not gc_list_* functions for unreachable until we remove the flag.
Tim Peters88396172002-06-30 17:56:40 +0000549 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000550static void
Tim Peters19b74c72002-07-01 03:52:19 +0000551move_unreachable(PyGC_Head *young, PyGC_Head *unreachable)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000552{
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900553 // previous elem in the young list, used for restore gc_prev.
554 PyGC_Head *prev = young;
555 PyGC_Head *gc = GC_NEXT(young);
Tim Peters19b74c72002-07-01 03:52:19 +0000556
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900557 /* Invariants: all objects "to the left" of us in young are reachable
558 * (directly or indirectly) from outside the young list as it was at entry.
559 *
560 * All other objects from the original young "to the left" of us are in
561 * unreachable now, and have NEXT_MASK_UNREACHABLE. All objects to the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 * left of us in 'young' now have been scanned, and no objects here
563 * or to the right have been scanned yet.
564 */
Tim Peters19b74c72002-07-01 03:52:19 +0000565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 while (gc != young) {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900567 if (gc_get_refs(gc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 /* gc is definitely reachable from outside the
569 * original 'young'. Mark it as such, and traverse
570 * its pointers to find any other objects that may
571 * be directly reachable from it. Note that the
572 * call to tp_traverse may append objects to young,
573 * so we have to wait until it returns to determine
574 * the next object to visit.
575 */
576 PyObject *op = FROM_GC(gc);
577 traverseproc traverse = Py_TYPE(op)->tp_traverse;
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200578 _PyObject_ASSERT_WITH_MSG(op, gc_get_refs(gc) > 0,
579 "refcount is too small");
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900580 // NOTE: visit_reachable may change gc->_gc_next when
581 // young->_gc_prev == gc. Don't do gc = GC_NEXT(gc) before!
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 (void) traverse(op,
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900583 (visitproc)visit_reachable,
584 (void *)young);
585 // relink gc_prev to prev element.
586 _PyGCHead_SET_PREV(gc, prev);
Quan Tian3bd0d622018-10-20 05:30:03 +0800587 // gc is not COLLECTING state after here.
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900588 gc_clear_collecting(gc);
589 prev = gc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000590 }
591 else {
592 /* This *may* be unreachable. To make progress,
593 * assume it is. gc isn't directly reachable from
594 * any object we've already traversed, but may be
595 * reachable from an object we haven't gotten to yet.
596 * visit_reachable will eventually move gc back into
597 * young if that's so, and we'll see it again.
598 */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900599 // Move gc to unreachable.
600 // No need to gc->next->prev = prev because it is single linked.
601 prev->_gc_next = gc->_gc_next;
602
603 // We can't use gc_list_append() here because we use
604 // NEXT_MASK_UNREACHABLE here.
605 PyGC_Head *last = GC_PREV(unreachable);
606 // NOTE: Since all objects in unreachable set has
607 // NEXT_MASK_UNREACHABLE flag, we set it unconditionally.
Tim Peters95bfc8a2019-10-13 16:47:04 -0500608 // But this may pollute the unreachable list head's 'next' pointer
609 // too. That's semantically senseless but expedient here - the
610 // damage is repaired when this fumction ends.
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900611 last->_gc_next = (NEXT_MASK_UNREACHABLE | (uintptr_t)gc);
612 _PyGCHead_SET_PREV(gc, last);
613 gc->_gc_next = (NEXT_MASK_UNREACHABLE | (uintptr_t)unreachable);
614 unreachable->_gc_prev = (uintptr_t)gc;
615 }
616 gc = (PyGC_Head*)prev->_gc_next;
617 }
618 // young->_gc_prev must be last element remained in the list.
619 young->_gc_prev = (uintptr_t)prev;
Tim Peters95bfc8a2019-10-13 16:47:04 -0500620 // don't let the pollution of the list head's next pointer leak
621 unreachable->_gc_next &= ~NEXT_MASK_UNREACHABLE;
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900622}
623
624static void
625untrack_tuples(PyGC_Head *head)
626{
627 PyGC_Head *next, *gc = GC_NEXT(head);
628 while (gc != head) {
629 PyObject *op = FROM_GC(gc);
630 next = GC_NEXT(gc);
631 if (PyTuple_CheckExact(op)) {
632 _PyTuple_MaybeUntrack(op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000633 }
634 gc = next;
635 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000636}
637
Antoine Pitroue1ad3da2012-05-28 22:22:34 +0200638/* Try to untrack all currently tracked dictionaries */
639static void
640untrack_dicts(PyGC_Head *head)
641{
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900642 PyGC_Head *next, *gc = GC_NEXT(head);
Antoine Pitroue1ad3da2012-05-28 22:22:34 +0200643 while (gc != head) {
644 PyObject *op = FROM_GC(gc);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900645 next = GC_NEXT(gc);
646 if (PyDict_CheckExact(op)) {
Antoine Pitroue1ad3da2012-05-28 22:22:34 +0200647 _PyDict_MaybeUntrack(op);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900648 }
Antoine Pitroue1ad3da2012-05-28 22:22:34 +0200649 gc = next;
650 }
651}
652
Antoine Pitrou796564c2013-07-30 19:59:21 +0200653/* Return true if object has a pre-PEP 442 finalization method. */
Neil Schemenauera765c122001-11-01 17:35:23 +0000654static int
Antoine Pitrou796564c2013-07-30 19:59:21 +0200655has_legacy_finalizer(PyObject *op)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000656{
Antoine Pitrou796564c2013-07-30 19:59:21 +0200657 return op->ob_type->tp_del != NULL;
Neil Schemenauera765c122001-11-01 17:35:23 +0000658}
659
Antoine Pitrou796564c2013-07-30 19:59:21 +0200660/* Move the objects in unreachable with tp_del slots into `finalizers`.
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900661 *
662 * This function also removes NEXT_MASK_UNREACHABLE flag
663 * from _gc_next in unreachable.
Jeremy Hyltonce136e92003-04-04 19:59:06 +0000664 */
Neil Schemenauera765c122001-11-01 17:35:23 +0000665static void
Antoine Pitrou796564c2013-07-30 19:59:21 +0200666move_legacy_finalizers(PyGC_Head *unreachable, PyGC_Head *finalizers)
Neil Schemenauera765c122001-11-01 17:35:23 +0000667{
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900668 PyGC_Head *gc, *next;
Tim Peters95bfc8a2019-10-13 16:47:04 -0500669 assert((unreachable->_gc_next & NEXT_MASK_UNREACHABLE) == 0);
Tim Petersf6b80452003-04-07 19:21:15 +0000670
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 /* March over unreachable. Move objects with finalizers into
672 * `finalizers`.
673 */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900674 for (gc = GC_NEXT(unreachable); gc != unreachable; gc = next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675 PyObject *op = FROM_GC(gc);
Jeremy Hyltonce136e92003-04-04 19:59:06 +0000676
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200677 _PyObject_ASSERT(op, gc->_gc_next & NEXT_MASK_UNREACHABLE);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900678 gc->_gc_next &= ~NEXT_MASK_UNREACHABLE;
679 next = (PyGC_Head*)gc->_gc_next;
Tim Petersf6ae7a42003-04-05 18:40:50 +0000680
Antoine Pitrou796564c2013-07-30 19:59:21 +0200681 if (has_legacy_finalizer(op)) {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900682 gc_clear_collecting(gc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000683 gc_list_move(gc, finalizers);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 }
685 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000686}
687
Pablo Galindo466326d2019-10-13 16:48:59 +0100688static inline void
689clear_unreachable_mask(PyGC_Head *unreachable)
690{
691 /* Check that the list head does not have the unreachable bit set */
692 assert(((uintptr_t)unreachable & NEXT_MASK_UNREACHABLE) == 0);
693
694 PyGC_Head *gc, *next;
Tim Peters95bfc8a2019-10-13 16:47:04 -0500695 assert((unreachable->_gc_next & NEXT_MASK_UNREACHABLE) == 0);
Pablo Galindo466326d2019-10-13 16:48:59 +0100696 for (gc = GC_NEXT(unreachable); gc != unreachable; gc = next) {
697 _PyObject_ASSERT((PyObject*)FROM_GC(gc), gc->_gc_next & NEXT_MASK_UNREACHABLE);
698 gc->_gc_next &= ~NEXT_MASK_UNREACHABLE;
699 next = (PyGC_Head*)gc->_gc_next;
700 }
Tim Petersea55c512019-10-18 20:59:14 -0500701 validate_list(unreachable, collecting_set_unreachable_clear);
Pablo Galindo466326d2019-10-13 16:48:59 +0100702}
703
Antoine Pitrou796564c2013-07-30 19:59:21 +0200704/* A traversal callback for move_legacy_finalizer_reachable. */
Tim Peters19b74c72002-07-01 03:52:19 +0000705static int
706visit_move(PyObject *op, PyGC_Head *tolist)
707{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000708 if (PyObject_IS_GC(op)) {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900709 PyGC_Head *gc = AS_GC(op);
710 if (gc_is_collecting(gc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 gc_list_move(gc, tolist);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900712 gc_clear_collecting(gc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 }
714 }
715 return 0;
Tim Peters19b74c72002-07-01 03:52:19 +0000716}
717
718/* Move objects that are reachable from finalizers, from the unreachable set
Tim Petersf6b80452003-04-07 19:21:15 +0000719 * into finalizers set.
Tim Peters19b74c72002-07-01 03:52:19 +0000720 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000721static void
Antoine Pitrou796564c2013-07-30 19:59:21 +0200722move_legacy_finalizer_reachable(PyGC_Head *finalizers)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000723{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 traverseproc traverse;
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900725 PyGC_Head *gc = GC_NEXT(finalizers);
726 for (; gc != finalizers; gc = GC_NEXT(gc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 /* Note that the finalizers list may grow during this. */
728 traverse = Py_TYPE(FROM_GC(gc))->tp_traverse;
729 (void) traverse(FROM_GC(gc),
730 (visitproc)visit_move,
731 (void *)finalizers);
732 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000733}
734
Tim Petersead8b7a2004-10-30 23:09:22 +0000735/* Clear all weakrefs to unreachable objects, and if such a weakref has a
736 * callback, invoke it if necessary. Note that it's possible for such
737 * weakrefs to be outside the unreachable set -- indeed, those are precisely
738 * the weakrefs whose callbacks must be invoked. See gc_weakref.txt for
739 * overview & some details. Some weakrefs with callbacks may be reclaimed
740 * directly by this routine; the number reclaimed is the return value. Other
741 * weakrefs with callbacks may be moved into the `old` generation. Objects
742 * moved into `old` have gc_refs set to GC_REACHABLE; the objects remaining in
743 * unreachable are left at GC_TENTATIVELY_UNREACHABLE. When this returns,
744 * no object in `unreachable` is weakly referenced anymore.
Tim Peters403a2032003-11-20 21:21:46 +0000745 */
746static int
Tim Petersead8b7a2004-10-30 23:09:22 +0000747handle_weakrefs(PyGC_Head *unreachable, PyGC_Head *old)
Tim Peters403a2032003-11-20 21:21:46 +0000748{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000749 PyGC_Head *gc;
750 PyObject *op; /* generally FROM_GC(gc) */
751 PyWeakReference *wr; /* generally a cast of op */
752 PyGC_Head wrcb_to_call; /* weakrefs with callbacks to call */
753 PyGC_Head *next;
754 int num_freed = 0;
Tim Peters403a2032003-11-20 21:21:46 +0000755
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000756 gc_list_init(&wrcb_to_call);
Tim Peters403a2032003-11-20 21:21:46 +0000757
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000758 /* Clear all weakrefs to the objects in unreachable. If such a weakref
759 * also has a callback, move it into `wrcb_to_call` if the callback
760 * needs to be invoked. Note that we cannot invoke any callbacks until
761 * all weakrefs to unreachable objects are cleared, lest the callback
762 * resurrect an unreachable object via a still-active weakref. We
763 * make another pass over wrcb_to_call, invoking callbacks, after this
764 * pass completes.
765 */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900766 for (gc = GC_NEXT(unreachable); gc != unreachable; gc = next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 PyWeakReference **wrlist;
Tim Petersead8b7a2004-10-30 23:09:22 +0000768
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 op = FROM_GC(gc);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900770 next = GC_NEXT(gc);
Tim Petersead8b7a2004-10-30 23:09:22 +0000771
Neil Schemenauerbcda4602019-09-30 10:06:45 -0700772 if (PyWeakref_Check(op)) {
773 /* A weakref inside the unreachable set must be cleared. If we
774 * allow its callback to execute inside delete_garbage(), it
775 * could expose objects that have tp_clear already called on
776 * them. Or, it could resurrect unreachable objects. One way
777 * this can happen is if some container objects do not implement
778 * tp_traverse. Then, wr_object can be outside the unreachable
779 * set but can be deallocated as a result of breaking the
780 * reference cycle. If we don't clear the weakref, the callback
781 * will run and potentially cause a crash. See bpo-38006 for
782 * one example.
783 */
784 _PyWeakref_ClearRef((PyWeakReference *)op);
785 }
786
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000787 if (! PyType_SUPPORTS_WEAKREFS(Py_TYPE(op)))
788 continue;
Tim Petersead8b7a2004-10-30 23:09:22 +0000789
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000790 /* It supports weakrefs. Does it have any? */
791 wrlist = (PyWeakReference **)
792 PyObject_GET_WEAKREFS_LISTPTR(op);
Tim Petersead8b7a2004-10-30 23:09:22 +0000793
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 /* `op` may have some weakrefs. March over the list, clear
795 * all the weakrefs, and move the weakrefs with callbacks
796 * that must be called into wrcb_to_call.
797 */
798 for (wr = *wrlist; wr != NULL; wr = *wrlist) {
799 PyGC_Head *wrasgc; /* AS_GC(wr) */
Tim Petersead8b7a2004-10-30 23:09:22 +0000800
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 /* _PyWeakref_ClearRef clears the weakref but leaves
802 * the callback pointer intact. Obscure: it also
803 * changes *wrlist.
804 */
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200805 _PyObject_ASSERT((PyObject *)wr, wr->wr_object == op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000806 _PyWeakref_ClearRef(wr);
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200807 _PyObject_ASSERT((PyObject *)wr, wr->wr_object == Py_None);
808 if (wr->wr_callback == NULL) {
809 /* no callback */
810 continue;
811 }
Tim Petersead8b7a2004-10-30 23:09:22 +0000812
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200813 /* Headache time. `op` is going away, and is weakly referenced by
814 * `wr`, which has a callback. Should the callback be invoked? If wr
815 * is also trash, no:
816 *
817 * 1. There's no need to call it. The object and the weakref are
818 * both going away, so it's legitimate to pretend the weakref is
819 * going away first. The user has to ensure a weakref outlives its
820 * referent if they want a guarantee that the wr callback will get
821 * invoked.
822 *
823 * 2. It may be catastrophic to call it. If the callback is also in
824 * cyclic trash (CT), then although the CT is unreachable from
825 * outside the current generation, CT may be reachable from the
826 * callback. Then the callback could resurrect insane objects.
827 *
828 * Since the callback is never needed and may be unsafe in this case,
829 * wr is simply left in the unreachable set. Note that because we
830 * already called _PyWeakref_ClearRef(wr), its callback will never
831 * trigger.
832 *
833 * OTOH, if wr isn't part of CT, we should invoke the callback: the
834 * weakref outlived the trash. Note that since wr isn't CT in this
835 * case, its callback can't be CT either -- wr acted as an external
836 * root to this generation, and therefore its callback did too. So
837 * nothing in CT is reachable from the callback either, so it's hard
838 * to imagine how calling it later could create a problem for us. wr
839 * is moved to wrcb_to_call in this case.
840 */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900841 if (gc_is_collecting(AS_GC(wr))) {
Neil Schemenauerbcda4602019-09-30 10:06:45 -0700842 /* it should already have been cleared above */
843 assert(wr->wr_object == Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 continue;
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900845 }
Tim Peterscc2a8662004-10-31 22:12:43 +0000846
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000847 /* Create a new reference so that wr can't go away
848 * before we can process it again.
849 */
850 Py_INCREF(wr);
Tim Petersead8b7a2004-10-30 23:09:22 +0000851
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 /* Move wr to wrcb_to_call, for the next pass. */
853 wrasgc = AS_GC(wr);
854 assert(wrasgc != next); /* wrasgc is reachable, but
855 next isn't, so they can't
856 be the same */
857 gc_list_move(wrasgc, &wrcb_to_call);
858 }
859 }
Tim Petersead8b7a2004-10-30 23:09:22 +0000860
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 /* Invoke the callbacks we decided to honor. It's safe to invoke them
862 * because they can't reference unreachable objects.
863 */
864 while (! gc_list_is_empty(&wrcb_to_call)) {
865 PyObject *temp;
866 PyObject *callback;
Tim Petersead8b7a2004-10-30 23:09:22 +0000867
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900868 gc = (PyGC_Head*)wrcb_to_call._gc_next;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000869 op = FROM_GC(gc);
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200870 _PyObject_ASSERT(op, PyWeakref_Check(op));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000871 wr = (PyWeakReference *)op;
872 callback = wr->wr_callback;
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200873 _PyObject_ASSERT(op, callback != NULL);
Tim Petersead8b7a2004-10-30 23:09:22 +0000874
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 /* copy-paste of weakrefobject.c's handle_callback() */
Jeroen Demeyer196a5302019-07-04 12:31:34 +0200876 temp = _PyObject_CallOneArg(callback, (PyObject *)wr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 if (temp == NULL)
878 PyErr_WriteUnraisable(callback);
879 else
880 Py_DECREF(temp);
Tim Petersead8b7a2004-10-30 23:09:22 +0000881
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 /* Give up the reference we created in the first pass. When
883 * op's refcount hits 0 (which it may or may not do right now),
884 * op's tp_dealloc will decref op->wr_callback too. Note
885 * that the refcount probably will hit 0 now, and because this
886 * weakref was reachable to begin with, gc didn't already
887 * add it to its count of freed objects. Example: a reachable
888 * weak value dict maps some key to this reachable weakref.
889 * The callback removes this key->weakref mapping from the
890 * dict, leaving no other references to the weakref (excepting
891 * ours).
892 */
893 Py_DECREF(op);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900894 if (wrcb_to_call._gc_next == (uintptr_t)gc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 /* object is still alive -- move it */
896 gc_list_move(gc, old);
897 }
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900898 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000899 ++num_freed;
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900900 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 }
Tim Petersead8b7a2004-10-30 23:09:22 +0000902
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 return num_freed;
Tim Peters403a2032003-11-20 21:21:46 +0000904}
905
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000906static void
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200907debug_cycle(const char *msg, PyObject *op)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000908{
Victor Stinner499dfcf2011-03-21 13:26:24 +0100909 PySys_FormatStderr("gc: %s <%s %p>\n",
910 msg, Py_TYPE(op)->tp_name, op);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000911}
912
Antoine Pitrou796564c2013-07-30 19:59:21 +0200913/* Handle uncollectable garbage (cycles with tp_del slots, and stuff reachable
Tim Petersbf384c22003-04-06 00:11:39 +0000914 * only from such cycles).
Tim Petersf6b80452003-04-07 19:21:15 +0000915 * If DEBUG_SAVEALL, all objects in finalizers are appended to the module
916 * garbage list (a Python list), else only the objects in finalizers with
917 * __del__ methods are appended to garbage. All objects in finalizers are
918 * merged into the old list regardless.
Tim Petersbf384c22003-04-06 00:11:39 +0000919 */
Serhiy Storchaka301e3cc2018-05-24 15:19:29 +0300920static void
Victor Stinner2e969062019-11-20 01:49:32 +0100921handle_legacy_finalizers(PyThreadState *tstate,
922 struct _gc_runtime_state *state,
Victor Stinner9db03242019-04-26 02:32:01 +0200923 PyGC_Head *finalizers, PyGC_Head *old)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000924{
Victor Stinner2e969062019-11-20 01:49:32 +0100925 assert(!_PyErr_Occurred(tstate));
Victor Stinner444b39b2019-11-20 01:18:11 +0100926 assert(state->garbage != NULL);
Victor Stinner9db03242019-04-26 02:32:01 +0200927
928 PyGC_Head *gc = GC_NEXT(finalizers);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900929 for (; gc != finalizers; gc = GC_NEXT(gc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000930 PyObject *op = FROM_GC(gc);
Tim Petersf6b80452003-04-07 19:21:15 +0000931
Victor Stinner9db03242019-04-26 02:32:01 +0200932 if ((state->debug & DEBUG_SAVEALL) || has_legacy_finalizer(op)) {
933 if (PyList_Append(state->garbage, op) < 0) {
Victor Stinner2e969062019-11-20 01:49:32 +0100934 _PyErr_Clear(tstate);
Serhiy Storchaka301e3cc2018-05-24 15:19:29 +0300935 break;
Serhiy Storchakac4653c92018-05-29 18:50:10 +0300936 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 }
938 }
Tim Petersf6b80452003-04-07 19:21:15 +0000939
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 gc_list_merge(finalizers, old);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000941}
942
Tim Peters5fbc7b12014-05-08 17:42:19 -0500943/* Run first-time finalizers (if any) on all the objects in collectable.
944 * Note that this may remove some (or even all) of the objects from the
945 * list, due to refcounts falling to 0.
946 */
Antoine Pitrou796564c2013-07-30 19:59:21 +0200947static void
Tim Peters5fbc7b12014-05-08 17:42:19 -0500948finalize_garbage(PyGC_Head *collectable)
Antoine Pitrou796564c2013-07-30 19:59:21 +0200949{
950 destructor finalize;
Tim Peters5fbc7b12014-05-08 17:42:19 -0500951 PyGC_Head seen;
Antoine Pitrou796564c2013-07-30 19:59:21 +0200952
Tim Peters5fbc7b12014-05-08 17:42:19 -0500953 /* While we're going through the loop, `finalize(op)` may cause op, or
954 * other objects, to be reclaimed via refcounts falling to zero. So
955 * there's little we can rely on about the structure of the input
956 * `collectable` list across iterations. For safety, we always take the
957 * first object in that list and move it to a temporary `seen` list.
958 * If objects vanish from the `collectable` and `seen` lists we don't
959 * care.
960 */
961 gc_list_init(&seen);
962
963 while (!gc_list_is_empty(collectable)) {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900964 PyGC_Head *gc = GC_NEXT(collectable);
Antoine Pitrou796564c2013-07-30 19:59:21 +0200965 PyObject *op = FROM_GC(gc);
Tim Peters5fbc7b12014-05-08 17:42:19 -0500966 gc_list_move(gc, &seen);
Antoine Pitrou796564c2013-07-30 19:59:21 +0200967 if (!_PyGCHead_FINALIZED(gc) &&
Tim Peters5fbc7b12014-05-08 17:42:19 -0500968 (finalize = Py_TYPE(op)->tp_finalize) != NULL) {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900969 _PyGCHead_SET_FINALIZED(gc);
Antoine Pitrou796564c2013-07-30 19:59:21 +0200970 Py_INCREF(op);
971 finalize(op);
Serhiy Storchakac4653c92018-05-29 18:50:10 +0300972 assert(!PyErr_Occurred());
Antoine Pitrou796564c2013-07-30 19:59:21 +0200973 Py_DECREF(op);
974 }
975 }
Tim Peters5fbc7b12014-05-08 17:42:19 -0500976 gc_list_merge(&seen, collectable);
Antoine Pitrou796564c2013-07-30 19:59:21 +0200977}
978
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979/* Break reference cycles by clearing the containers involved. This is
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000980 * tricky business as the lists can be changing and we don't know which
Tim Peters19b74c72002-07-01 03:52:19 +0000981 * objects may be freed. It is possible I screwed something up here.
982 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000983static void
Victor Stinner2e969062019-11-20 01:49:32 +0100984delete_garbage(PyThreadState *tstate, struct _gc_runtime_state *state,
Victor Stinner9db03242019-04-26 02:32:01 +0200985 PyGC_Head *collectable, PyGC_Head *old)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000986{
Victor Stinner2e969062019-11-20 01:49:32 +0100987 assert(!_PyErr_Occurred(tstate));
Victor Stinner9db03242019-04-26 02:32:01 +0200988
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 while (!gc_list_is_empty(collectable)) {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900990 PyGC_Head *gc = GC_NEXT(collectable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 PyObject *op = FROM_GC(gc);
Tim Peters88396172002-06-30 17:56:40 +0000992
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200993 _PyObject_ASSERT_WITH_MSG(op, Py_REFCNT(op) > 0,
994 "refcount is too small");
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900995
Victor Stinner9db03242019-04-26 02:32:01 +0200996 if (state->debug & DEBUG_SAVEALL) {
997 assert(state->garbage != NULL);
998 if (PyList_Append(state->garbage, op) < 0) {
Victor Stinner2e969062019-11-20 01:49:32 +0100999 _PyErr_Clear(tstate);
Serhiy Storchakac4653c92018-05-29 18:50:10 +03001000 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 }
1002 else {
Victor Stinner9db03242019-04-26 02:32:01 +02001003 inquiry clear;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001004 if ((clear = Py_TYPE(op)->tp_clear) != NULL) {
1005 Py_INCREF(op);
Serhiy Storchakac4653c92018-05-29 18:50:10 +03001006 (void) clear(op);
Victor Stinner2e969062019-11-20 01:49:32 +01001007 if (_PyErr_Occurred(tstate)) {
Victor Stinner71c52e32019-05-27 08:57:14 +02001008 _PyErr_WriteUnraisableMsg("in tp_clear of",
1009 (PyObject*)Py_TYPE(op));
Serhiy Storchakac4653c92018-05-29 18:50:10 +03001010 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 Py_DECREF(op);
1012 }
1013 }
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09001014 if (GC_NEXT(collectable) == gc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 /* object is still alive, move it, it may die later */
Pablo Galindo466326d2019-10-13 16:48:59 +01001016 gc_clear_collecting(gc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 gc_list_move(gc, old);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 }
1019 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001020}
1021
Christian Heimesa156e092008-02-16 07:38:31 +00001022/* Clear all free lists
1023 * All free lists are cleared during the collection of the highest generation.
1024 * Allocated items in the free list may keep a pymalloc arena occupied.
1025 * Clearing the free lists may give back memory to the OS earlier.
1026 */
1027static void
1028clear_freelists(void)
1029{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 (void)PyMethod_ClearFreeList();
1031 (void)PyFrame_ClearFreeList();
1032 (void)PyCFunction_ClearFreeList();
1033 (void)PyTuple_ClearFreeList();
1034 (void)PyUnicode_ClearFreeList();
1035 (void)PyFloat_ClearFreeList();
Antoine Pitrou9a812cb2011-11-15 00:00:12 +01001036 (void)PyList_ClearFreeList();
1037 (void)PyDict_ClearFreeList();
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001038 (void)PySet_ClearFreeList();
Yury Selivanoveb636452016-09-08 22:01:51 -07001039 (void)PyAsyncGen_ClearFreeLists();
Yury Selivanovf23746a2018-01-22 19:11:18 -05001040 (void)PyContext_ClearFreeList();
Christian Heimesa156e092008-02-16 07:38:31 +00001041}
1042
Inada Naokibf8162c2019-08-02 16:25:29 +09001043// Show stats for objects in each gennerations.
1044static void
1045show_stats_each_generations(struct _gc_runtime_state *state)
1046{
1047 char buf[100];
1048 size_t pos = 0;
1049
1050 for (int i = 0; i < NUM_GENERATIONS && pos < sizeof(buf); i++) {
1051 pos += PyOS_snprintf(buf+pos, sizeof(buf)-pos,
1052 " %"PY_FORMAT_SIZE_T"d",
1053 gc_list_size(GEN_HEAD(state, i)));
1054 }
1055
1056 PySys_FormatStderr(
1057 "gc: objects in each generation:%s\n"
1058 "gc: objects in permanent generation: %zd\n",
1059 buf, gc_list_size(&state->permanent_generation.head));
1060}
1061
Pablo Galindo466326d2019-10-13 16:48:59 +01001062/* Deduce wich objects among "base" are unreachable from outside the list
1063 and move them to 'unreachable'. The process consist in the following steps:
1064
10651. Copy all reference counts to a different field (gc_prev is used to hold
1066 this copy to save memory).
10672. Traverse all objects in "base" and visit all referred objects using
1068 "tp_traverse" and for every visited object, substract 1 to the reference
1069 count (the one that we copied in the previous step). After this step, all
1070 objects that can be reached directly from outside must have strictly positive
1071 reference count, while all unreachable objects must have a count of exactly 0.
10723. Indentify all unreachable objects (the ones with 0 reference count) and move
1073 them to the "unreachable" list. This step also needs to move back to "base" all
1074 objects that were initially marked as unreachable but are referred transitively
1075 by the reachable objects (the ones with strictly positive reference count).
1076
1077Contracts:
1078
1079 * The "base" has to be a valid list with no mask set.
1080
1081 * The "unreachable" list must be uninitialized (this function calls
1082 gc_list_init over 'unreachable').
Tim Peters95bfc8a2019-10-13 16:47:04 -05001083
Pablo Galindo466326d2019-10-13 16:48:59 +01001084IMPORTANT: This function leaves 'unreachable' with the NEXT_MASK_UNREACHABLE
1085flag set but it does not clear it to skip unnecessary iteration. Before the
1086flag is cleared (for example, by using 'clear_unreachable_mask' function or
1087by a call to 'move_legacy_finalizers'), the 'unreachable' list is not a normal
1088list and we can not use most gc_list_* functions for it. */
1089static inline void
1090deduce_unreachable(PyGC_Head *base, PyGC_Head *unreachable) {
Tim Petersea55c512019-10-18 20:59:14 -05001091 validate_list(base, collecting_clear_unreachable_clear);
Pablo Galindo466326d2019-10-13 16:48:59 +01001092 /* Using ob_refcnt and gc_refs, calculate which objects in the
1093 * container set are reachable from outside the set (i.e., have a
1094 * refcount greater than 0 when all the references within the
1095 * set are taken into account).
1096 */
1097 update_refs(base); // gc_prev is used for gc_refs
1098 subtract_refs(base);
1099
1100 /* Leave everything reachable from outside base in base, and move
1101 * everything else (in base) to unreachable.
1102 * NOTE: This used to move the reachable objects into a reachable
1103 * set instead. But most things usually turn out to be reachable,
Tim Petersd9d39932019-11-02 12:06:31 -05001104 * so it's more efficient to move the unreachable things. See note
Pablo Galindob028f582019-11-19 01:36:57 +00001105 ^ [REACHABLE OR UNREACHABLE?] at the file end.
Pablo Galindo466326d2019-10-13 16:48:59 +01001106 */
1107 gc_list_init(unreachable);
1108 move_unreachable(base, unreachable); // gc_prev is pointer again
Tim Petersea55c512019-10-18 20:59:14 -05001109 validate_list(base, collecting_clear_unreachable_clear);
1110 validate_list(unreachable, collecting_set_unreachable_set);
Pablo Galindo466326d2019-10-13 16:48:59 +01001111}
1112
1113/* Handle objects that may have resurrected after a call to 'finalize_garbage', moving
1114 them to 'old_generation' and placing the rest on 'still_unreachable'.
1115
1116 Contracts:
1117 * After this function 'unreachable' must not be used anymore and 'still_unreachable'
1118 will contain the objects that did not resurrect.
1119
1120 * The "still_unreachable" list must be uninitialized (this function calls
1121 gc_list_init over 'still_unreachable').
1122
1123IMPORTANT: After a call to this function, the 'still_unreachable' set will have the
1124PREV_MARK_COLLECTING set, but the objects in this set are going to be removed so
1125we can skip the expense of clearing the flag to avoid extra iteration. */
1126static inline void
1127handle_resurrected_objects(PyGC_Head *unreachable, PyGC_Head* still_unreachable,
1128 PyGC_Head *old_generation)
1129{
1130 // Remove the PREV_MASK_COLLECTING from unreachable
1131 // to prepare it for a new call to 'deduce_unreachable'
1132 gc_list_clear_collecting(unreachable);
1133
1134 // After the call to deduce_unreachable, the 'still_unreachable' set will
1135 // have the PREV_MARK_COLLECTING set, but the objects are going to be
1136 // removed so we can skip the expense of clearing the flag.
1137 PyGC_Head* resurrected = unreachable;
1138 deduce_unreachable(resurrected, still_unreachable);
1139 clear_unreachable_mask(still_unreachable);
1140
1141 // Move the resurrected objects to the old generation for future collection.
1142 gc_list_merge(resurrected, old_generation);
1143}
1144
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001145/* This is the main function. Read this to understand how the
1146 * collection process works. */
Neal Norwitz7b216c52006-03-04 20:01:53 +00001147static Py_ssize_t
Victor Stinner2e969062019-11-20 01:49:32 +01001148collect(PyThreadState *tstate, struct _gc_runtime_state *state, int generation,
Victor Stinner9db03242019-04-26 02:32:01 +02001149 Py_ssize_t *n_collected, Py_ssize_t *n_uncollectable, int nofail)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001150{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001151 int i;
1152 Py_ssize_t m = 0; /* # objects collected */
1153 Py_ssize_t n = 0; /* # unreachable objects that couldn't be collected */
1154 PyGC_Head *young; /* the generation we are examining */
1155 PyGC_Head *old; /* next older generation */
1156 PyGC_Head unreachable; /* non-problematic unreachable trash */
1157 PyGC_Head finalizers; /* objects with, & reachable from, __del__ */
1158 PyGC_Head *gc;
Victor Stinner7181dec2015-03-27 17:47:53 +01001159 _PyTime_t t1 = 0; /* initialize to prevent a compiler warning */
Antoine Pitrou40f6b122014-05-24 19:21:53 +02001160
Victor Stinner9db03242019-04-26 02:32:01 +02001161 if (state->debug & DEBUG_STATS) {
Inada Naokibf8162c2019-08-02 16:25:29 +09001162 PySys_WriteStderr("gc: collecting generation %d...\n", generation);
1163 show_stats_each_generations(state);
Victor Stinner7181dec2015-03-27 17:47:53 +01001164 t1 = _PyTime_GetMonotonicClock();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001165 }
Neil Schemenauer2880ae52002-05-04 05:35:20 +00001166
Łukasz Langaa785c872016-09-09 17:37:37 -07001167 if (PyDTrace_GC_START_ENABLED())
1168 PyDTrace_GC_START(generation);
1169
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001170 /* update collection and allocation counters */
1171 if (generation+1 < NUM_GENERATIONS)
Victor Stinner9db03242019-04-26 02:32:01 +02001172 state->generations[generation+1].count += 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001173 for (i = 0; i <= generation; i++)
Victor Stinner9db03242019-04-26 02:32:01 +02001174 state->generations[i].count = 0;
Neil Schemenauer2880ae52002-05-04 05:35:20 +00001175
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001176 /* merge younger generations with one we are currently collecting */
1177 for (i = 0; i < generation; i++) {
Victor Stinner9db03242019-04-26 02:32:01 +02001178 gc_list_merge(GEN_HEAD(state, i), GEN_HEAD(state, generation));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001179 }
Neil Schemenauer2880ae52002-05-04 05:35:20 +00001180
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001181 /* handy references */
Victor Stinner9db03242019-04-26 02:32:01 +02001182 young = GEN_HEAD(state, generation);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001183 if (generation < NUM_GENERATIONS-1)
Victor Stinner9db03242019-04-26 02:32:01 +02001184 old = GEN_HEAD(state, generation+1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001185 else
1186 old = young;
Tim Petersea55c512019-10-18 20:59:14 -05001187 validate_list(old, collecting_clear_unreachable_clear);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001188
Pablo Galindo466326d2019-10-13 16:48:59 +01001189 deduce_unreachable(young, &unreachable);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001190
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09001191 untrack_tuples(young);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001192 /* Move reachable objects to next generation. */
1193 if (young != old) {
1194 if (generation == NUM_GENERATIONS - 2) {
Victor Stinner9db03242019-04-26 02:32:01 +02001195 state->long_lived_pending += gc_list_size(young);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001196 }
1197 gc_list_merge(young, old);
1198 }
1199 else {
Antoine Pitroue1ad3da2012-05-28 22:22:34 +02001200 /* We only untrack dicts in full collections, to avoid quadratic
1201 dict build-up. See issue #14775. */
1202 untrack_dicts(young);
Victor Stinner9db03242019-04-26 02:32:01 +02001203 state->long_lived_pending = 0;
1204 state->long_lived_total = gc_list_size(young);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001205 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001206
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001207 /* All objects in unreachable are trash, but objects reachable from
Antoine Pitrou796564c2013-07-30 19:59:21 +02001208 * legacy finalizers (e.g. tp_del) can't safely be deleted.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001209 */
1210 gc_list_init(&finalizers);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09001211 // NEXT_MASK_UNREACHABLE is cleared here.
1212 // After move_legacy_finalizers(), unreachable is normal list.
Antoine Pitrou796564c2013-07-30 19:59:21 +02001213 move_legacy_finalizers(&unreachable, &finalizers);
1214 /* finalizers contains the unreachable objects with a legacy finalizer;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001215 * unreachable objects reachable *from* those are also uncollectable,
1216 * and we move those into the finalizers list too.
1217 */
Antoine Pitrou796564c2013-07-30 19:59:21 +02001218 move_legacy_finalizer_reachable(&finalizers);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001219
Tim Petersea55c512019-10-18 20:59:14 -05001220 validate_list(&finalizers, collecting_clear_unreachable_clear);
1221 validate_list(&unreachable, collecting_set_unreachable_clear);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09001222
Tim Petersecbf35f2019-10-09 12:37:30 -05001223 /* Print debugging information. */
1224 if (state->debug & DEBUG_COLLECTABLE) {
1225 for (gc = GC_NEXT(&unreachable); gc != &unreachable; gc = GC_NEXT(gc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001226 debug_cycle("collectable", FROM_GC(gc));
1227 }
1228 }
Tim Petersead8b7a2004-10-30 23:09:22 +00001229
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001230 /* Clear weakrefs and invoke callbacks as necessary. */
1231 m += handle_weakrefs(&unreachable, old);
Tim Petersead8b7a2004-10-30 23:09:22 +00001232
Tim Petersea55c512019-10-18 20:59:14 -05001233 validate_list(old, collecting_clear_unreachable_clear);
1234 validate_list(&unreachable, collecting_set_unreachable_clear);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09001235
Antoine Pitrou796564c2013-07-30 19:59:21 +02001236 /* Call tp_finalize on objects which have one. */
Tim Peters5fbc7b12014-05-08 17:42:19 -05001237 finalize_garbage(&unreachable);
Antoine Pitrou796564c2013-07-30 19:59:21 +02001238
Pablo Galindo466326d2019-10-13 16:48:59 +01001239 /* Handle any objects that may have resurrected after the call
1240 * to 'finalize_garbage' and continue the collection with the
1241 * objects that are still unreachable */
1242 PyGC_Head final_unreachable;
1243 handle_resurrected_objects(&unreachable, &final_unreachable, old);
1244
1245 /* Call tp_clear on objects in the final_unreachable set. This will cause
1246 * the reference cycles to be broken. It may also cause some objects
1247 * in finalizers to be freed.
1248 */
1249 m += gc_list_size(&final_unreachable);
Victor Stinner2e969062019-11-20 01:49:32 +01001250 delete_garbage(tstate, state, &final_unreachable, old);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001251
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001252 /* Collect statistics on uncollectable objects found and print
1253 * debugging information. */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09001254 for (gc = GC_NEXT(&finalizers); gc != &finalizers; gc = GC_NEXT(gc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 n++;
Victor Stinner9db03242019-04-26 02:32:01 +02001256 if (state->debug & DEBUG_UNCOLLECTABLE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001257 debug_cycle("uncollectable", FROM_GC(gc));
1258 }
Victor Stinner9db03242019-04-26 02:32:01 +02001259 if (state->debug & DEBUG_STATS) {
Inada Naokibf8162c2019-08-02 16:25:29 +09001260 double d = _PyTime_AsSecondsDouble(_PyTime_GetMonotonicClock() - t1);
Inada Naoki013e52f2019-08-31 09:13:42 +09001261 PySys_WriteStderr(
1262 "gc: done, %" PY_FORMAT_SIZE_T "d unreachable, "
1263 "%" PY_FORMAT_SIZE_T "d uncollectable, %.4fs elapsed\n",
Inada Naokibf8162c2019-08-02 16:25:29 +09001264 n+m, n, d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001265 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001266
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 /* Append instances in the uncollectable set to a Python
1268 * reachable list of garbage. The programmer has to deal with
1269 * this if they insist on creating this type of structure.
1270 */
Victor Stinner2e969062019-11-20 01:49:32 +01001271 handle_legacy_finalizers(tstate, state, &finalizers, old);
Tim Petersea55c512019-10-18 20:59:14 -05001272 validate_list(old, collecting_clear_unreachable_clear);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001273
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001274 /* Clear free list only during the collection of the highest
1275 * generation */
1276 if (generation == NUM_GENERATIONS-1) {
1277 clear_freelists();
1278 }
Christian Heimesa156e092008-02-16 07:38:31 +00001279
Victor Stinner2e969062019-11-20 01:49:32 +01001280 if (_PyErr_Occurred(tstate)) {
Antoine Pitroufef34e32013-05-19 01:11:58 +02001281 if (nofail) {
Victor Stinner2e969062019-11-20 01:49:32 +01001282 _PyErr_Clear(tstate);
Antoine Pitroufef34e32013-05-19 01:11:58 +02001283 }
1284 else {
1285 if (gc_str == NULL)
1286 gc_str = PyUnicode_FromString("garbage collection");
1287 PyErr_WriteUnraisable(gc_str);
1288 Py_FatalError("unexpected exception during garbage collection");
1289 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001290 }
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001291
Antoine Pitroud4156c12012-10-30 22:43:19 +01001292 /* Update stats */
Victor Stinner9db03242019-04-26 02:32:01 +02001293 if (n_collected) {
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001294 *n_collected = m;
Victor Stinner9db03242019-04-26 02:32:01 +02001295 }
1296 if (n_uncollectable) {
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001297 *n_uncollectable = n;
Victor Stinner9db03242019-04-26 02:32:01 +02001298 }
1299
1300 struct gc_generation_stats *stats = &state->generation_stats[generation];
Antoine Pitroud4156c12012-10-30 22:43:19 +01001301 stats->collections++;
1302 stats->collected += m;
1303 stats->uncollectable += n;
Łukasz Langaa785c872016-09-09 17:37:37 -07001304
Victor Stinner9db03242019-04-26 02:32:01 +02001305 if (PyDTrace_GC_DONE_ENABLED()) {
Victor Stinner2e969062019-11-20 01:49:32 +01001306 PyDTrace_GC_DONE(n + m);
Victor Stinner9db03242019-04-26 02:32:01 +02001307 }
Łukasz Langaa785c872016-09-09 17:37:37 -07001308
Victor Stinner2e969062019-11-20 01:49:32 +01001309 assert(!_PyErr_Occurred(tstate));
1310 return n + m;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001311}
1312
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001313/* Invoke progress callbacks to notify clients that garbage collection
1314 * is starting or stopping
1315 */
1316static void
Victor Stinner9db03242019-04-26 02:32:01 +02001317invoke_gc_callback(struct _gc_runtime_state *state, const char *phase,
1318 int generation, Py_ssize_t collected,
1319 Py_ssize_t uncollectable)
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001320{
Serhiy Storchakac4653c92018-05-29 18:50:10 +03001321 assert(!PyErr_Occurred());
Victor Stinner9db03242019-04-26 02:32:01 +02001322
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001323 /* we may get called very early */
Victor Stinner9db03242019-04-26 02:32:01 +02001324 if (state->callbacks == NULL) {
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001325 return;
Victor Stinner9db03242019-04-26 02:32:01 +02001326 }
1327
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001328 /* The local variable cannot be rebound, check it for sanity */
Victor Stinner9db03242019-04-26 02:32:01 +02001329 assert(PyList_CheckExact(state->callbacks));
1330 PyObject *info = NULL;
1331 if (PyList_GET_SIZE(state->callbacks) != 0) {
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001332 info = Py_BuildValue("{sisnsn}",
1333 "generation", generation,
1334 "collected", collected,
1335 "uncollectable", uncollectable);
1336 if (info == NULL) {
1337 PyErr_WriteUnraisable(NULL);
1338 return;
1339 }
1340 }
Victor Stinner9db03242019-04-26 02:32:01 +02001341 for (Py_ssize_t i=0; i<PyList_GET_SIZE(state->callbacks); i++) {
1342 PyObject *r, *cb = PyList_GET_ITEM(state->callbacks, i);
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001343 Py_INCREF(cb); /* make sure cb doesn't go away */
1344 r = PyObject_CallFunction(cb, "sO", phase, info);
Serhiy Storchaka301e3cc2018-05-24 15:19:29 +03001345 if (r == NULL) {
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001346 PyErr_WriteUnraisable(cb);
Serhiy Storchaka301e3cc2018-05-24 15:19:29 +03001347 }
1348 else {
1349 Py_DECREF(r);
1350 }
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001351 Py_DECREF(cb);
1352 }
1353 Py_XDECREF(info);
Serhiy Storchakac4653c92018-05-29 18:50:10 +03001354 assert(!PyErr_Occurred());
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001355}
1356
1357/* Perform garbage collection of a generation and invoke
1358 * progress callbacks.
1359 */
1360static Py_ssize_t
Victor Stinner2e969062019-11-20 01:49:32 +01001361collect_with_callback(PyThreadState *tstate, struct _gc_runtime_state *state,
1362 int generation)
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001363{
Serhiy Storchakac4653c92018-05-29 18:50:10 +03001364 assert(!PyErr_Occurred());
Victor Stinner9db03242019-04-26 02:32:01 +02001365 Py_ssize_t result, collected, uncollectable;
1366 invoke_gc_callback(state, "start", generation, 0, 0);
Victor Stinner2e969062019-11-20 01:49:32 +01001367 result = collect(tstate, state, generation, &collected, &uncollectable, 0);
Victor Stinner9db03242019-04-26 02:32:01 +02001368 invoke_gc_callback(state, "stop", generation, collected, uncollectable);
Serhiy Storchakac4653c92018-05-29 18:50:10 +03001369 assert(!PyErr_Occurred());
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001370 return result;
1371}
1372
Neal Norwitz7b216c52006-03-04 20:01:53 +00001373static Py_ssize_t
Victor Stinner2e969062019-11-20 01:49:32 +01001374collect_generations(PyThreadState *tstate, struct _gc_runtime_state *state)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001375{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001376 /* Find the oldest generation (highest numbered) where the count
1377 * exceeds the threshold. Objects in the that generation and
1378 * generations younger than it will be collected. */
Victor Stinner9db03242019-04-26 02:32:01 +02001379 Py_ssize_t n = 0;
1380 for (int i = NUM_GENERATIONS-1; i >= 0; i--) {
1381 if (state->generations[i].count > state->generations[i].threshold) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001382 /* Avoid quadratic performance degradation in number
1383 of tracked objects. See comments at the beginning
1384 of this file, and issue #4074.
1385 */
1386 if (i == NUM_GENERATIONS - 1
Victor Stinner9db03242019-04-26 02:32:01 +02001387 && state->long_lived_pending < state->long_lived_total / 4)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 continue;
Victor Stinner2e969062019-11-20 01:49:32 +01001389 n = collect_with_callback(tstate, state, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001390 break;
1391 }
1392 }
1393 return n;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001394}
1395
Serhiy Storchaka93260282017-02-04 11:19:59 +02001396#include "clinic/gcmodule.c.h"
1397
1398/*[clinic input]
1399gc.enable
1400
1401Enable automatic garbage collection.
1402[clinic start generated code]*/
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001403
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001404static PyObject *
Serhiy Storchaka93260282017-02-04 11:19:59 +02001405gc_enable_impl(PyObject *module)
1406/*[clinic end generated code: output=45a427e9dce9155c input=81ac4940ca579707]*/
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001407{
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001408 _PyRuntime.gc.enabled = 1;
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001409 Py_RETURN_NONE;
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001410}
1411
Serhiy Storchaka93260282017-02-04 11:19:59 +02001412/*[clinic input]
1413gc.disable
1414
1415Disable automatic garbage collection.
1416[clinic start generated code]*/
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001417
1418static PyObject *
Serhiy Storchaka93260282017-02-04 11:19:59 +02001419gc_disable_impl(PyObject *module)
1420/*[clinic end generated code: output=97d1030f7aa9d279 input=8c2e5a14e800d83b]*/
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001421{
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001422 _PyRuntime.gc.enabled = 0;
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001423 Py_RETURN_NONE;
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001424}
1425
Serhiy Storchaka93260282017-02-04 11:19:59 +02001426/*[clinic input]
1427gc.isenabled -> bool
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001428
Serhiy Storchaka93260282017-02-04 11:19:59 +02001429Returns true if automatic garbage collection is enabled.
1430[clinic start generated code]*/
1431
1432static int
1433gc_isenabled_impl(PyObject *module)
1434/*[clinic end generated code: output=1874298331c49130 input=30005e0422373b31]*/
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001435{
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001436 return _PyRuntime.gc.enabled;
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001437}
1438
Serhiy Storchaka93260282017-02-04 11:19:59 +02001439/*[clinic input]
1440gc.collect -> Py_ssize_t
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001441
Serhiy Storchaka93260282017-02-04 11:19:59 +02001442 generation: int(c_default="NUM_GENERATIONS - 1") = 2
1443
1444Run the garbage collector.
1445
1446With no arguments, run a full collection. The optional argument
1447may be an integer specifying which generation to collect. A ValueError
1448is raised if the generation number is invalid.
1449
1450The number of unreachable objects is returned.
1451[clinic start generated code]*/
1452
1453static Py_ssize_t
1454gc_collect_impl(PyObject *module, int generation)
1455/*[clinic end generated code: output=b697e633043233c7 input=40720128b682d879]*/
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001456{
Victor Stinner2e969062019-11-20 01:49:32 +01001457 PyThreadState *tstate = _PyThreadState_GET();
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001458
Serhiy Storchaka93260282017-02-04 11:19:59 +02001459 if (generation < 0 || generation >= NUM_GENERATIONS) {
Victor Stinner2e969062019-11-20 01:49:32 +01001460 _PyErr_SetString(tstate, PyExc_ValueError, "invalid generation");
Serhiy Storchaka93260282017-02-04 11:19:59 +02001461 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001462 }
Barry Warsawd3c38ff2006-03-07 09:46:03 +00001463
Victor Stinner9db03242019-04-26 02:32:01 +02001464 struct _gc_runtime_state *state = &_PyRuntime.gc;
1465 Py_ssize_t n;
1466 if (state->collecting) {
1467 /* already collecting, don't do anything */
1468 n = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001469 }
Victor Stinner9db03242019-04-26 02:32:01 +02001470 else {
1471 state->collecting = 1;
Victor Stinner2e969062019-11-20 01:49:32 +01001472 n = collect_with_callback(tstate, state, generation);
Victor Stinner9db03242019-04-26 02:32:01 +02001473 state->collecting = 0;
1474 }
Serhiy Storchaka93260282017-02-04 11:19:59 +02001475 return n;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001476}
1477
Serhiy Storchaka93260282017-02-04 11:19:59 +02001478/*[clinic input]
1479gc.set_debug
1480
1481 flags: int
1482 An integer that can have the following bits turned on:
1483 DEBUG_STATS - Print statistics during collection.
1484 DEBUG_COLLECTABLE - Print collectable objects found.
1485 DEBUG_UNCOLLECTABLE - Print unreachable but uncollectable objects
1486 found.
1487 DEBUG_SAVEALL - Save objects to gc.garbage rather than freeing them.
1488 DEBUG_LEAK - Debug leaking programs (everything but STATS).
1489 /
1490
1491Set the garbage collection debugging flags.
1492
1493Debugging information is written to sys.stderr.
1494[clinic start generated code]*/
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001495
1496static PyObject *
Serhiy Storchaka93260282017-02-04 11:19:59 +02001497gc_set_debug_impl(PyObject *module, int flags)
1498/*[clinic end generated code: output=7c8366575486b228 input=5e5ce15e84fbed15]*/
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001499{
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001500 _PyRuntime.gc.debug = flags;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001501
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001502 Py_RETURN_NONE;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001503}
1504
Serhiy Storchaka93260282017-02-04 11:19:59 +02001505/*[clinic input]
1506gc.get_debug -> int
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001507
Serhiy Storchaka93260282017-02-04 11:19:59 +02001508Get the garbage collection debugging flags.
1509[clinic start generated code]*/
1510
1511static int
1512gc_get_debug_impl(PyObject *module)
1513/*[clinic end generated code: output=91242f3506cd1e50 input=91a101e1c3b98366]*/
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001514{
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001515 return _PyRuntime.gc.debug;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001516}
1517
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001518PyDoc_STRVAR(gc_set_thresh__doc__,
Neal Norwitz2a47c0f2002-01-29 00:53:41 +00001519"set_threshold(threshold0, [threshold1, threshold2]) -> None\n"
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001520"\n"
1521"Sets the collection thresholds. Setting threshold0 to zero disables\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001522"collection.\n");
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001523
1524static PyObject *
Victor Stinner9db03242019-04-26 02:32:01 +02001525gc_set_threshold(PyObject *self, PyObject *args)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001526{
Victor Stinner9db03242019-04-26 02:32:01 +02001527 struct _gc_runtime_state *state = &_PyRuntime.gc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001528 if (!PyArg_ParseTuple(args, "i|ii:set_threshold",
Victor Stinner9db03242019-04-26 02:32:01 +02001529 &state->generations[0].threshold,
1530 &state->generations[1].threshold,
1531 &state->generations[2].threshold))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001532 return NULL;
Victor Stinner9db03242019-04-26 02:32:01 +02001533 for (int i = 3; i < NUM_GENERATIONS; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001534 /* generations higher than 2 get the same threshold */
Victor Stinner9db03242019-04-26 02:32:01 +02001535 state->generations[i].threshold = state->generations[2].threshold;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001536 }
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001537 Py_RETURN_NONE;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001538}
1539
Serhiy Storchaka93260282017-02-04 11:19:59 +02001540/*[clinic input]
1541gc.get_threshold
1542
1543Return the current collection thresholds.
1544[clinic start generated code]*/
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001545
1546static PyObject *
Serhiy Storchaka93260282017-02-04 11:19:59 +02001547gc_get_threshold_impl(PyObject *module)
1548/*[clinic end generated code: output=7902bc9f41ecbbd8 input=286d79918034d6e6]*/
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001549{
Victor Stinner9db03242019-04-26 02:32:01 +02001550 struct _gc_runtime_state *state = &_PyRuntime.gc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001551 return Py_BuildValue("(iii)",
Victor Stinner9db03242019-04-26 02:32:01 +02001552 state->generations[0].threshold,
1553 state->generations[1].threshold,
1554 state->generations[2].threshold);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001555}
1556
Serhiy Storchaka93260282017-02-04 11:19:59 +02001557/*[clinic input]
1558gc.get_count
1559
1560Return a three-tuple of the current collection counts.
1561[clinic start generated code]*/
Barry Warsawd3c38ff2006-03-07 09:46:03 +00001562
1563static PyObject *
Serhiy Storchaka93260282017-02-04 11:19:59 +02001564gc_get_count_impl(PyObject *module)
1565/*[clinic end generated code: output=354012e67b16398f input=a392794a08251751]*/
Barry Warsawd3c38ff2006-03-07 09:46:03 +00001566{
Victor Stinner9db03242019-04-26 02:32:01 +02001567 struct _gc_runtime_state *state = &_PyRuntime.gc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001568 return Py_BuildValue("(iii)",
Victor Stinner9db03242019-04-26 02:32:01 +02001569 state->generations[0].count,
1570 state->generations[1].count,
1571 state->generations[2].count);
Barry Warsawd3c38ff2006-03-07 09:46:03 +00001572}
1573
Neil Schemenauer48c70342001-08-09 15:38:31 +00001574static int
Martin v. Löwis560da622001-11-24 09:24:51 +00001575referrersvisit(PyObject* obj, PyObject *objs)
Neil Schemenauer48c70342001-08-09 15:38:31 +00001576{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001577 Py_ssize_t i;
1578 for (i = 0; i < PyTuple_GET_SIZE(objs); i++)
1579 if (PyTuple_GET_ITEM(objs, i) == obj)
1580 return 1;
1581 return 0;
Neil Schemenauer48c70342001-08-09 15:38:31 +00001582}
1583
Neil Schemenauer17e7be62001-08-10 14:46:47 +00001584static int
Martin v. Löwis560da622001-11-24 09:24:51 +00001585gc_referrers_for(PyObject *objs, PyGC_Head *list, PyObject *resultlist)
Neil Schemenauer48c70342001-08-09 15:38:31 +00001586{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001587 PyGC_Head *gc;
1588 PyObject *obj;
1589 traverseproc traverse;
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09001590 for (gc = GC_NEXT(list); gc != list; gc = GC_NEXT(gc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591 obj = FROM_GC(gc);
1592 traverse = Py_TYPE(obj)->tp_traverse;
1593 if (obj == objs || obj == resultlist)
1594 continue;
1595 if (traverse(obj, (visitproc)referrersvisit, objs)) {
1596 if (PyList_Append(resultlist, obj) < 0)
1597 return 0; /* error */
1598 }
1599 }
1600 return 1; /* no error */
Neil Schemenauer48c70342001-08-09 15:38:31 +00001601}
1602
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001603PyDoc_STRVAR(gc_get_referrers__doc__,
Martin v. Löwis560da622001-11-24 09:24:51 +00001604"get_referrers(*objs) -> list\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001605Return the list of objects that directly refer to any of objs.");
Neil Schemenauer48c70342001-08-09 15:38:31 +00001606
Neil Schemenauer17e7be62001-08-10 14:46:47 +00001607static PyObject *
Martin v. Löwis560da622001-11-24 09:24:51 +00001608gc_get_referrers(PyObject *self, PyObject *args)
Neil Schemenauer48c70342001-08-09 15:38:31 +00001609{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001610 int i;
1611 PyObject *result = PyList_New(0);
1612 if (!result) return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001613
Victor Stinner9db03242019-04-26 02:32:01 +02001614 struct _gc_runtime_state *state = &_PyRuntime.gc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001615 for (i = 0; i < NUM_GENERATIONS; i++) {
Victor Stinner9db03242019-04-26 02:32:01 +02001616 if (!(gc_referrers_for(args, GEN_HEAD(state, i), result))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 Py_DECREF(result);
1618 return NULL;
1619 }
1620 }
1621 return result;
Neil Schemenauer48c70342001-08-09 15:38:31 +00001622}
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001623
Tim Peters0f81ab62003-04-08 16:39:48 +00001624/* Append obj to list; return true if error (out of memory), false if OK. */
Jeremy Hylton5bd378b2003-04-03 16:28:38 +00001625static int
Tim Peters730f5532003-04-08 17:17:17 +00001626referentsvisit(PyObject *obj, PyObject *list)
Jeremy Hylton5bd378b2003-04-03 16:28:38 +00001627{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001628 return PyList_Append(list, obj) < 0;
Jeremy Hylton5bd378b2003-04-03 16:28:38 +00001629}
1630
Tim Peters730f5532003-04-08 17:17:17 +00001631PyDoc_STRVAR(gc_get_referents__doc__,
1632"get_referents(*objs) -> list\n\
Jeremy Hylton059b0942003-04-03 16:29:13 +00001633Return the list of objects that are directly referred to by objs.");
Jeremy Hylton5bd378b2003-04-03 16:28:38 +00001634
1635static PyObject *
Tim Peters730f5532003-04-08 17:17:17 +00001636gc_get_referents(PyObject *self, PyObject *args)
Jeremy Hylton5bd378b2003-04-03 16:28:38 +00001637{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001638 Py_ssize_t i;
1639 PyObject *result = PyList_New(0);
Tim Peters0f81ab62003-04-08 16:39:48 +00001640
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001641 if (result == NULL)
1642 return NULL;
Tim Peters0f81ab62003-04-08 16:39:48 +00001643
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001644 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
1645 traverseproc traverse;
1646 PyObject *obj = PyTuple_GET_ITEM(args, i);
Tim Peters0f81ab62003-04-08 16:39:48 +00001647
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001648 if (! PyObject_IS_GC(obj))
1649 continue;
1650 traverse = Py_TYPE(obj)->tp_traverse;
1651 if (! traverse)
1652 continue;
1653 if (traverse(obj, (visitproc)referentsvisit, result)) {
1654 Py_DECREF(result);
1655 return NULL;
1656 }
1657 }
1658 return result;
Jeremy Hylton5bd378b2003-04-03 16:28:38 +00001659}
1660
Serhiy Storchaka93260282017-02-04 11:19:59 +02001661/*[clinic input]
1662gc.get_objects
Pablo Galindo175421b2019-02-23 03:02:06 +00001663 generation: Py_ssize_t(accept={int, NoneType}, c_default="-1") = None
1664 Generation to extract the objects from.
Serhiy Storchaka93260282017-02-04 11:19:59 +02001665
1666Return a list of objects tracked by the collector (excluding the list returned).
Pablo Galindo175421b2019-02-23 03:02:06 +00001667
1668If generation is not None, return only the objects tracked by the collector
1669that are in that generation.
Serhiy Storchaka93260282017-02-04 11:19:59 +02001670[clinic start generated code]*/
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +00001671
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +00001672static PyObject *
Pablo Galindo175421b2019-02-23 03:02:06 +00001673gc_get_objects_impl(PyObject *module, Py_ssize_t generation)
1674/*[clinic end generated code: output=48b35fea4ba6cb0e input=ef7da9df9806754c]*/
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +00001675{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001676 int i;
1677 PyObject* result;
Victor Stinner9db03242019-04-26 02:32:01 +02001678 struct _gc_runtime_state *state = &_PyRuntime.gc;
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +00001679
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001680 result = PyList_New(0);
Pablo Galindo175421b2019-02-23 03:02:06 +00001681 if (result == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001682 return NULL;
Pablo Galindo175421b2019-02-23 03:02:06 +00001683 }
1684
1685 /* If generation is passed, we extract only that generation */
Victor Stinner0810fa72019-04-15 17:54:09 +02001686 if (generation != -1) {
Pablo Galindo175421b2019-02-23 03:02:06 +00001687 if (generation >= NUM_GENERATIONS) {
1688 PyErr_Format(PyExc_ValueError,
1689 "generation parameter must be less than the number of "
1690 "available generations (%i)",
1691 NUM_GENERATIONS);
1692 goto error;
1693 }
1694
1695 if (generation < 0) {
1696 PyErr_SetString(PyExc_ValueError,
1697 "generation parameter cannot be negative");
1698 goto error;
1699 }
1700
Victor Stinner9db03242019-04-26 02:32:01 +02001701 if (append_objects(result, GEN_HEAD(state, generation))) {
Pablo Galindo175421b2019-02-23 03:02:06 +00001702 goto error;
1703 }
1704
1705 return result;
1706 }
1707
1708 /* If generation is not passed or None, get all objects from all generations */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001709 for (i = 0; i < NUM_GENERATIONS; i++) {
Victor Stinner9db03242019-04-26 02:32:01 +02001710 if (append_objects(result, GEN_HEAD(state, i))) {
Pablo Galindo175421b2019-02-23 03:02:06 +00001711 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001712 }
1713 }
1714 return result;
Pablo Galindo175421b2019-02-23 03:02:06 +00001715
1716error:
1717 Py_DECREF(result);
1718 return NULL;
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +00001719}
1720
Serhiy Storchaka93260282017-02-04 11:19:59 +02001721/*[clinic input]
1722gc.get_stats
1723
1724Return a list of dictionaries containing per-generation statistics.
1725[clinic start generated code]*/
Antoine Pitroud4156c12012-10-30 22:43:19 +01001726
1727static PyObject *
Serhiy Storchaka93260282017-02-04 11:19:59 +02001728gc_get_stats_impl(PyObject *module)
1729/*[clinic end generated code: output=a8ab1d8a5d26f3ab input=1ef4ed9d17b1a470]*/
Antoine Pitroud4156c12012-10-30 22:43:19 +01001730{
1731 int i;
Antoine Pitroud4156c12012-10-30 22:43:19 +01001732 struct gc_generation_stats stats[NUM_GENERATIONS], *st;
1733
1734 /* To get consistent values despite allocations while constructing
1735 the result list, we use a snapshot of the running stats. */
Victor Stinner9db03242019-04-26 02:32:01 +02001736 struct _gc_runtime_state *state = &_PyRuntime.gc;
Antoine Pitroud4156c12012-10-30 22:43:19 +01001737 for (i = 0; i < NUM_GENERATIONS; i++) {
Victor Stinner9db03242019-04-26 02:32:01 +02001738 stats[i] = state->generation_stats[i];
Antoine Pitroud4156c12012-10-30 22:43:19 +01001739 }
1740
Victor Stinner9db03242019-04-26 02:32:01 +02001741 PyObject *result = PyList_New(0);
Antoine Pitroud4156c12012-10-30 22:43:19 +01001742 if (result == NULL)
1743 return NULL;
1744
1745 for (i = 0; i < NUM_GENERATIONS; i++) {
1746 PyObject *dict;
1747 st = &stats[i];
1748 dict = Py_BuildValue("{snsnsn}",
1749 "collections", st->collections,
1750 "collected", st->collected,
1751 "uncollectable", st->uncollectable
1752 );
1753 if (dict == NULL)
1754 goto error;
1755 if (PyList_Append(result, dict)) {
1756 Py_DECREF(dict);
1757 goto error;
1758 }
1759 Py_DECREF(dict);
1760 }
1761 return result;
1762
1763error:
1764 Py_XDECREF(result);
1765 return NULL;
1766}
1767
1768
Serhiy Storchaka93260282017-02-04 11:19:59 +02001769/*[clinic input]
1770gc.is_tracked
1771
1772 obj: object
1773 /
1774
1775Returns true if the object is tracked by the garbage collector.
1776
1777Simple atomic objects will return false.
1778[clinic start generated code]*/
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001779
1780static PyObject *
Serhiy Storchaka93260282017-02-04 11:19:59 +02001781gc_is_tracked(PyObject *module, PyObject *obj)
1782/*[clinic end generated code: output=14f0103423b28e31 input=d83057f170ea2723]*/
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001783{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001784 PyObject *result;
1785
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09001786 if (PyObject_IS_GC(obj) && _PyObject_GC_IS_TRACKED(obj))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001787 result = Py_True;
1788 else
1789 result = Py_False;
1790 Py_INCREF(result);
1791 return result;
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001792}
1793
brainfvckc75edab2017-10-16 12:49:41 -07001794/*[clinic input]
1795gc.freeze
1796
1797Freeze all current tracked objects and ignore them for future collections.
1798
1799This can be used before a POSIX fork() call to make the gc copy-on-write friendly.
1800Note: collection before a POSIX fork() call may free pages for future allocation
1801which can cause copy-on-write.
1802[clinic start generated code]*/
1803
1804static PyObject *
1805gc_freeze_impl(PyObject *module)
1806/*[clinic end generated code: output=502159d9cdc4c139 input=b602b16ac5febbe5]*/
1807{
Victor Stinner9db03242019-04-26 02:32:01 +02001808 struct _gc_runtime_state *state = &_PyRuntime.gc;
brainfvckc75edab2017-10-16 12:49:41 -07001809 for (int i = 0; i < NUM_GENERATIONS; ++i) {
Victor Stinner9db03242019-04-26 02:32:01 +02001810 gc_list_merge(GEN_HEAD(state, i), &state->permanent_generation.head);
1811 state->generations[i].count = 0;
brainfvckc75edab2017-10-16 12:49:41 -07001812 }
1813 Py_RETURN_NONE;
1814}
1815
1816/*[clinic input]
1817gc.unfreeze
1818
1819Unfreeze all objects in the permanent generation.
1820
1821Put all objects in the permanent generation back into oldest generation.
1822[clinic start generated code]*/
1823
1824static PyObject *
1825gc_unfreeze_impl(PyObject *module)
1826/*[clinic end generated code: output=1c15f2043b25e169 input=2dd52b170f4cef6c]*/
1827{
Victor Stinner9db03242019-04-26 02:32:01 +02001828 struct _gc_runtime_state *state = &_PyRuntime.gc;
1829 gc_list_merge(&state->permanent_generation.head, GEN_HEAD(state, NUM_GENERATIONS-1));
brainfvckc75edab2017-10-16 12:49:41 -07001830 Py_RETURN_NONE;
1831}
1832
1833/*[clinic input]
Victor Stinner05d68a82018-01-18 11:15:25 +01001834gc.get_freeze_count -> Py_ssize_t
brainfvckc75edab2017-10-16 12:49:41 -07001835
1836Return the number of objects in the permanent generation.
1837[clinic start generated code]*/
1838
Victor Stinner05d68a82018-01-18 11:15:25 +01001839static Py_ssize_t
brainfvckc75edab2017-10-16 12:49:41 -07001840gc_get_freeze_count_impl(PyObject *module)
Victor Stinner05d68a82018-01-18 11:15:25 +01001841/*[clinic end generated code: output=61cbd9f43aa032e1 input=45ffbc65cfe2a6ed]*/
brainfvckc75edab2017-10-16 12:49:41 -07001842{
1843 return gc_list_size(&_PyRuntime.gc.permanent_generation.head);
1844}
1845
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +00001846
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001847PyDoc_STRVAR(gc__doc__,
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001848"This module provides access to the garbage collector for reference cycles.\n"
1849"\n"
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001850"enable() -- Enable automatic garbage collection.\n"
1851"disable() -- Disable automatic garbage collection.\n"
1852"isenabled() -- Returns true if automatic collection is enabled.\n"
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001853"collect() -- Do a full collection right now.\n"
Thomas Wouters89f507f2006-12-13 04:49:30 +00001854"get_count() -- Return the current collection counts.\n"
R David Murray0e814632013-12-26 15:11:28 -05001855"get_stats() -- Return list of dictionaries containing per-generation stats.\n"
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001856"set_debug() -- Set debugging flags.\n"
1857"get_debug() -- Get debugging flags.\n"
1858"set_threshold() -- Set the collection thresholds.\n"
1859"get_threshold() -- Return the current the collection thresholds.\n"
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +00001860"get_objects() -- Return a list of all objects tracked by the collector.\n"
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001861"is_tracked() -- Returns true if a given object is tracked.\n"
Jeremy Hylton5bd378b2003-04-03 16:28:38 +00001862"get_referrers() -- Return the list of objects that refer to an object.\n"
brainfvckc75edab2017-10-16 12:49:41 -07001863"get_referents() -- Return the list of objects that an object refers to.\n"
1864"freeze() -- Freeze all tracked objects and ignore them for future collections.\n"
1865"unfreeze() -- Unfreeze all objects in the permanent generation.\n"
1866"get_freeze_count() -- Return the number of objects in the permanent generation.\n");
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001867
1868static PyMethodDef GcMethods[] = {
Serhiy Storchaka93260282017-02-04 11:19:59 +02001869 GC_ENABLE_METHODDEF
1870 GC_DISABLE_METHODDEF
1871 GC_ISENABLED_METHODDEF
1872 GC_SET_DEBUG_METHODDEF
1873 GC_GET_DEBUG_METHODDEF
1874 GC_GET_COUNT_METHODDEF
Victor Stinner9db03242019-04-26 02:32:01 +02001875 {"set_threshold", gc_set_threshold, METH_VARARGS, gc_set_thresh__doc__},
Serhiy Storchaka93260282017-02-04 11:19:59 +02001876 GC_GET_THRESHOLD_METHODDEF
1877 GC_COLLECT_METHODDEF
1878 GC_GET_OBJECTS_METHODDEF
1879 GC_GET_STATS_METHODDEF
1880 GC_IS_TRACKED_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001881 {"get_referrers", gc_get_referrers, METH_VARARGS,
1882 gc_get_referrers__doc__},
1883 {"get_referents", gc_get_referents, METH_VARARGS,
1884 gc_get_referents__doc__},
brainfvckc75edab2017-10-16 12:49:41 -07001885 GC_FREEZE_METHODDEF
1886 GC_UNFREEZE_METHODDEF
1887 GC_GET_FREEZE_COUNT_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001888 {NULL, NULL} /* Sentinel */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001889};
1890
Martin v. Löwis1a214512008-06-11 05:26:20 +00001891static struct PyModuleDef gcmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001892 PyModuleDef_HEAD_INIT,
Antoine Pitrou696e0352010-08-08 22:18:46 +00001893 "gc", /* m_name */
1894 gc__doc__, /* m_doc */
1895 -1, /* m_size */
1896 GcMethods, /* m_methods */
1897 NULL, /* m_reload */
1898 NULL, /* m_traverse */
1899 NULL, /* m_clear */
1900 NULL /* m_free */
Martin v. Löwis1a214512008-06-11 05:26:20 +00001901};
1902
Jason Tishler6bc06ec2003-09-04 11:59:50 +00001903PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001904PyInit_gc(void)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001905{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001906 PyObject *m;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001907
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001908 m = PyModule_Create(&gcmodule);
Martin v. Löwis1a214512008-06-11 05:26:20 +00001909
Victor Stinner9db03242019-04-26 02:32:01 +02001910 if (m == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001911 return NULL;
Victor Stinner9db03242019-04-26 02:32:01 +02001912 }
Tim Peters11558872003-04-06 23:30:52 +00001913
Victor Stinner9db03242019-04-26 02:32:01 +02001914 struct _gc_runtime_state *state = &_PyRuntime.gc;
1915 if (state->garbage == NULL) {
1916 state->garbage = PyList_New(0);
1917 if (state->garbage == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001918 return NULL;
1919 }
Victor Stinner9db03242019-04-26 02:32:01 +02001920 Py_INCREF(state->garbage);
1921 if (PyModule_AddObject(m, "garbage", state->garbage) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001922 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001923
Victor Stinner9db03242019-04-26 02:32:01 +02001924 if (state->callbacks == NULL) {
1925 state->callbacks = PyList_New(0);
1926 if (state->callbacks == NULL)
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001927 return NULL;
1928 }
Victor Stinner9db03242019-04-26 02:32:01 +02001929 Py_INCREF(state->callbacks);
1930 if (PyModule_AddObject(m, "callbacks", state->callbacks) < 0)
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001931 return NULL;
1932
Martin v. Löwis1a214512008-06-11 05:26:20 +00001933#define ADD_INT(NAME) if (PyModule_AddIntConstant(m, #NAME, NAME) < 0) return NULL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001934 ADD_INT(DEBUG_STATS);
1935 ADD_INT(DEBUG_COLLECTABLE);
1936 ADD_INT(DEBUG_UNCOLLECTABLE);
1937 ADD_INT(DEBUG_SAVEALL);
1938 ADD_INT(DEBUG_LEAK);
Tim Peters11558872003-04-06 23:30:52 +00001939#undef ADD_INT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001940 return m;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001941}
1942
Guido van Rossume13ddc92003-04-17 17:29:22 +00001943/* API to invoke gc.collect() from C */
Neal Norwitz7b216c52006-03-04 20:01:53 +00001944Py_ssize_t
Guido van Rossume13ddc92003-04-17 17:29:22 +00001945PyGC_Collect(void)
1946{
Victor Stinner2e969062019-11-20 01:49:32 +01001947 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner9db03242019-04-26 02:32:01 +02001948 struct _gc_runtime_state *state = &_PyRuntime.gc;
Victor Stinner2e969062019-11-20 01:49:32 +01001949
Victor Stinner9db03242019-04-26 02:32:01 +02001950 if (!state->enabled) {
1951 return 0;
1952 }
Guido van Rossume13ddc92003-04-17 17:29:22 +00001953
Victor Stinner9db03242019-04-26 02:32:01 +02001954 Py_ssize_t n;
1955 if (state->collecting) {
1956 /* already collecting, don't do anything */
1957 n = 0;
1958 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001959 else {
Serhiy Storchaka301e3cc2018-05-24 15:19:29 +03001960 PyObject *exc, *value, *tb;
Victor Stinner9db03242019-04-26 02:32:01 +02001961 state->collecting = 1;
Victor Stinner2e969062019-11-20 01:49:32 +01001962 _PyErr_Fetch(tstate, &exc, &value, &tb);
1963 n = collect_with_callback(tstate, state, NUM_GENERATIONS - 1);
1964 _PyErr_Restore(tstate, exc, value, tb);
Victor Stinner9db03242019-04-26 02:32:01 +02001965 state->collecting = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001966 }
Guido van Rossume13ddc92003-04-17 17:29:22 +00001967
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001968 return n;
Guido van Rossume13ddc92003-04-17 17:29:22 +00001969}
1970
Antoine Pitroufef34e32013-05-19 01:11:58 +02001971Py_ssize_t
Łukasz Langafef7e942016-09-09 21:47:46 -07001972_PyGC_CollectIfEnabled(void)
1973{
Łukasz Langafef7e942016-09-09 21:47:46 -07001974 return PyGC_Collect();
1975}
1976
1977Py_ssize_t
Antoine Pitroufef34e32013-05-19 01:11:58 +02001978_PyGC_CollectNoFail(void)
1979{
Victor Stinner2e969062019-11-20 01:49:32 +01001980 PyThreadState *tstate = _PyThreadState_GET();
1981 assert(!_PyErr_Occurred(tstate));
Victor Stinner9db03242019-04-26 02:32:01 +02001982
1983 struct _gc_runtime_state *state = &_PyRuntime.gc;
Antoine Pitroufef34e32013-05-19 01:11:58 +02001984 Py_ssize_t n;
1985
Antoine Pitrouc69c9bc2013-08-15 20:15:15 +02001986 /* Ideally, this function is only called on interpreter shutdown,
1987 and therefore not recursively. Unfortunately, when there are daemon
1988 threads, a daemon thread can start a cyclic garbage collection
1989 during interpreter shutdown (and then never finish it).
1990 See http://bugs.python.org/issue8713#msg195178 for an example.
1991 */
Victor Stinner9db03242019-04-26 02:32:01 +02001992 if (state->collecting) {
Antoine Pitrouc69c9bc2013-08-15 20:15:15 +02001993 n = 0;
Victor Stinner9db03242019-04-26 02:32:01 +02001994 }
Antoine Pitrouc69c9bc2013-08-15 20:15:15 +02001995 else {
Victor Stinner9db03242019-04-26 02:32:01 +02001996 state->collecting = 1;
Victor Stinner2e969062019-11-20 01:49:32 +01001997 n = collect(tstate, state, NUM_GENERATIONS - 1, NULL, NULL, 1);
Victor Stinner9db03242019-04-26 02:32:01 +02001998 state->collecting = 0;
Antoine Pitrouc69c9bc2013-08-15 20:15:15 +02001999 }
Antoine Pitroufef34e32013-05-19 01:11:58 +02002000 return n;
2001}
Antoine Pitrou5f454a02013-05-06 21:15:57 +02002002
Antoine Pitrou696e0352010-08-08 22:18:46 +00002003void
Victor Stinner9db03242019-04-26 02:32:01 +02002004_PyGC_DumpShutdownStats(_PyRuntimeState *runtime)
Antoine Pitrou696e0352010-08-08 22:18:46 +00002005{
Victor Stinner9db03242019-04-26 02:32:01 +02002006 struct _gc_runtime_state *state = &runtime->gc;
2007 if (!(state->debug & DEBUG_SAVEALL)
2008 && state->garbage != NULL && PyList_GET_SIZE(state->garbage) > 0) {
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02002009 const char *message;
Victor Stinner9db03242019-04-26 02:32:01 +02002010 if (state->debug & DEBUG_UNCOLLECTABLE)
Antoine Pitroub5d82042010-11-05 00:05:25 +00002011 message = "gc: %zd uncollectable objects at " \
Georg Brandl08be72d2010-10-24 15:11:22 +00002012 "shutdown";
2013 else
Antoine Pitroub5d82042010-11-05 00:05:25 +00002014 message = "gc: %zd uncollectable objects at " \
Georg Brandl08be72d2010-10-24 15:11:22 +00002015 "shutdown; use gc.set_debug(gc.DEBUG_UNCOLLECTABLE) to list them";
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02002016 /* PyErr_WarnFormat does too many things and we are at shutdown,
2017 the warnings module's dependencies (e.g. linecache) may be gone
2018 already. */
2019 if (PyErr_WarnExplicitFormat(PyExc_ResourceWarning, "gc", 0,
2020 "gc", NULL, message,
Victor Stinner9db03242019-04-26 02:32:01 +02002021 PyList_GET_SIZE(state->garbage)))
Georg Brandl08be72d2010-10-24 15:11:22 +00002022 PyErr_WriteUnraisable(NULL);
Victor Stinner9db03242019-04-26 02:32:01 +02002023 if (state->debug & DEBUG_UNCOLLECTABLE) {
Antoine Pitrou696e0352010-08-08 22:18:46 +00002024 PyObject *repr = NULL, *bytes = NULL;
Victor Stinner9db03242019-04-26 02:32:01 +02002025 repr = PyObject_Repr(state->garbage);
Antoine Pitrou696e0352010-08-08 22:18:46 +00002026 if (!repr || !(bytes = PyUnicode_EncodeFSDefault(repr)))
Victor Stinner9db03242019-04-26 02:32:01 +02002027 PyErr_WriteUnraisable(state->garbage);
Antoine Pitrou696e0352010-08-08 22:18:46 +00002028 else {
2029 PySys_WriteStderr(
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02002030 " %s\n",
Antoine Pitrou696e0352010-08-08 22:18:46 +00002031 PyBytes_AS_STRING(bytes)
2032 );
2033 }
2034 Py_XDECREF(repr);
2035 Py_XDECREF(bytes);
2036 }
Antoine Pitrou696e0352010-08-08 22:18:46 +00002037 }
Antoine Pitrou5f454a02013-05-06 21:15:57 +02002038}
2039
2040void
Victor Stinner8e91c242019-04-24 17:24:01 +02002041_PyGC_Fini(_PyRuntimeState *runtime)
Antoine Pitrou5f454a02013-05-06 21:15:57 +02002042{
Victor Stinner9db03242019-04-26 02:32:01 +02002043 struct _gc_runtime_state *state = &runtime->gc;
2044 Py_CLEAR(state->garbage);
2045 Py_CLEAR(state->callbacks);
Antoine Pitrou696e0352010-08-08 22:18:46 +00002046}
2047
Neil Schemenauer43411b52001-08-30 00:05:51 +00002048/* for debugging */
Guido van Rossume13ddc92003-04-17 17:29:22 +00002049void
2050_PyGC_Dump(PyGC_Head *g)
Neil Schemenauer43411b52001-08-30 00:05:51 +00002051{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002052 _PyObject_Dump(FROM_GC(g));
Neil Schemenauer43411b52001-08-30 00:05:51 +00002053}
2054
Victor Stinnera5447732019-10-10 09:32:13 +02002055
2056#ifdef Py_DEBUG
Victor Stinner1b184552019-10-08 00:09:31 +02002057static int
2058visit_validate(PyObject *op, void *parent_raw)
2059{
2060 PyObject *parent = _PyObject_CAST(parent_raw);
2061 if (_PyObject_IsFreed(op)) {
2062 _PyObject_ASSERT_FAILED_MSG(parent,
2063 "PyObject_GC_Track() object is not valid");
2064 }
2065 return 0;
2066}
Victor Stinnera5447732019-10-10 09:32:13 +02002067#endif
Victor Stinner1b184552019-10-08 00:09:31 +02002068
2069
Neil Schemenauer43411b52001-08-30 00:05:51 +00002070/* extension modules might be compiled with GC support so these
2071 functions must always be available */
2072
2073void
Victor Stinnera42de742018-11-22 10:25:22 +01002074PyObject_GC_Track(void *op_raw)
Neil Schemenauer43411b52001-08-30 00:05:51 +00002075{
Victor Stinnera42de742018-11-22 10:25:22 +01002076 PyObject *op = _PyObject_CAST(op_raw);
Victor Stinner271753a2018-11-22 01:02:54 +01002077 if (_PyObject_GC_IS_TRACKED(op)) {
2078 _PyObject_ASSERT_FAILED_MSG(op,
2079 "object already tracked "
2080 "by the garbage collector");
2081 }
Victor Stinnera42de742018-11-22 10:25:22 +01002082 _PyObject_GC_TRACK(op);
Victor Stinner1b184552019-10-08 00:09:31 +02002083
2084#ifdef Py_DEBUG
2085 /* Check that the object is valid: validate objects traversed
2086 by tp_traverse() */
2087 traverseproc traverse = Py_TYPE(op)->tp_traverse;
2088 (void)traverse(op, visit_validate, op);
2089#endif
Neil Schemenauer43411b52001-08-30 00:05:51 +00002090}
2091
Neil Schemenauerfec4eb12002-04-12 02:41:03 +00002092void
Victor Stinnera42de742018-11-22 10:25:22 +01002093PyObject_GC_UnTrack(void *op_raw)
Neil Schemenauer43411b52001-08-30 00:05:51 +00002094{
Victor Stinnera42de742018-11-22 10:25:22 +01002095 PyObject *op = _PyObject_CAST(op_raw);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002096 /* Obscure: the Py_TRASHCAN mechanism requires that we be able to
2097 * call PyObject_GC_UnTrack twice on an object.
2098 */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002099 if (_PyObject_GC_IS_TRACKED(op)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002100 _PyObject_GC_UNTRACK(op);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002101 }
Neil Schemenauer43411b52001-08-30 00:05:51 +00002102}
2103
Victor Stinnerdb067af2014-05-02 22:31:14 +02002104static PyObject *
2105_PyObject_GC_Alloc(int use_calloc, size_t basicsize)
Neil Schemenauer43411b52001-08-30 00:05:51 +00002106{
Victor Stinner9db03242019-04-26 02:32:01 +02002107 struct _gc_runtime_state *state = &_PyRuntime.gc;
Victor Stinner2e969062019-11-20 01:49:32 +01002108 if (basicsize > PY_SSIZE_T_MAX - sizeof(PyGC_Head)) {
2109 return PyErr_NoMemory();
2110 }
2111 size_t size = sizeof(PyGC_Head) + basicsize;
2112
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002113 PyGC_Head *g;
Victor Stinner2e969062019-11-20 01:49:32 +01002114 if (use_calloc) {
Victor Stinnerdb067af2014-05-02 22:31:14 +02002115 g = (PyGC_Head *)PyObject_Calloc(1, size);
Victor Stinner2e969062019-11-20 01:49:32 +01002116 }
2117 else {
Victor Stinnerdb067af2014-05-02 22:31:14 +02002118 g = (PyGC_Head *)PyObject_Malloc(size);
Victor Stinner2e969062019-11-20 01:49:32 +01002119 }
2120 if (g == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002121 return PyErr_NoMemory();
Victor Stinner2e969062019-11-20 01:49:32 +01002122 }
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002123 assert(((uintptr_t)g & 3) == 0); // g must be aligned 4bytes boundary
Victor Stinner2e969062019-11-20 01:49:32 +01002124
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002125 g->_gc_next = 0;
2126 g->_gc_prev = 0;
Victor Stinner9db03242019-04-26 02:32:01 +02002127 state->generations[0].count++; /* number of allocated GC objects */
2128 if (state->generations[0].count > state->generations[0].threshold &&
2129 state->enabled &&
2130 state->generations[0].threshold &&
2131 !state->collecting &&
Victor Stinner2e969062019-11-20 01:49:32 +01002132 !PyErr_Occurred())
2133 {
2134 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner9db03242019-04-26 02:32:01 +02002135 state->collecting = 1;
Victor Stinner2e969062019-11-20 01:49:32 +01002136 collect_generations(tstate, state);
Victor Stinner9db03242019-04-26 02:32:01 +02002137 state->collecting = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002138 }
Victor Stinner2e969062019-11-20 01:49:32 +01002139 PyObject *op = FROM_GC(g);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002140 return op;
Neil Schemenauer43411b52001-08-30 00:05:51 +00002141}
2142
2143PyObject *
Victor Stinnerdb067af2014-05-02 22:31:14 +02002144_PyObject_GC_Malloc(size_t basicsize)
2145{
2146 return _PyObject_GC_Alloc(0, basicsize);
2147}
2148
2149PyObject *
2150_PyObject_GC_Calloc(size_t basicsize)
2151{
2152 return _PyObject_GC_Alloc(1, basicsize);
2153}
2154
2155PyObject *
Neil Schemenauer43411b52001-08-30 00:05:51 +00002156_PyObject_GC_New(PyTypeObject *tp)
2157{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002158 PyObject *op = _PyObject_GC_Malloc(_PyObject_SIZE(tp));
2159 if (op != NULL)
2160 op = PyObject_INIT(op, tp);
2161 return op;
Neil Schemenauer43411b52001-08-30 00:05:51 +00002162}
2163
2164PyVarObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00002165_PyObject_GC_NewVar(PyTypeObject *tp, Py_ssize_t nitems)
Neil Schemenauer43411b52001-08-30 00:05:51 +00002166{
Victor Stinner5d1866c2013-07-08 22:17:52 +02002167 size_t size;
2168 PyVarObject *op;
2169
2170 if (nitems < 0) {
2171 PyErr_BadInternalCall();
2172 return NULL;
2173 }
2174 size = _PyObject_VAR_SIZE(tp, nitems);
2175 op = (PyVarObject *) _PyObject_GC_Malloc(size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002176 if (op != NULL)
2177 op = PyObject_INIT_VAR(op, tp, nitems);
2178 return op;
Neil Schemenauer43411b52001-08-30 00:05:51 +00002179}
2180
2181PyVarObject *
Martin v. Löwis41290682006-02-16 14:56:14 +00002182_PyObject_GC_Resize(PyVarObject *op, Py_ssize_t nitems)
Neil Schemenauer43411b52001-08-30 00:05:51 +00002183{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002184 const size_t basicsize = _PyObject_VAR_SIZE(Py_TYPE(op), nitems);
Victor Stinnera4b2bc72018-10-26 18:00:13 +02002185 _PyObject_ASSERT((PyObject *)op, !_PyObject_GC_IS_TRACKED(op));
2186 if (basicsize > PY_SSIZE_T_MAX - sizeof(PyGC_Head)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002187 return (PyVarObject *)PyErr_NoMemory();
Victor Stinnera4b2bc72018-10-26 18:00:13 +02002188 }
2189
2190 PyGC_Head *g = AS_GC(op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002191 g = (PyGC_Head *)PyObject_REALLOC(g, sizeof(PyGC_Head) + basicsize);
2192 if (g == NULL)
2193 return (PyVarObject *)PyErr_NoMemory();
2194 op = (PyVarObject *) FROM_GC(g);
2195 Py_SIZE(op) = nitems;
2196 return op;
Neil Schemenauer43411b52001-08-30 00:05:51 +00002197}
2198
2199void
Neil Schemenauerfec4eb12002-04-12 02:41:03 +00002200PyObject_GC_Del(void *op)
Neil Schemenauer43411b52001-08-30 00:05:51 +00002201{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002202 PyGC_Head *g = AS_GC(op);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002203 if (_PyObject_GC_IS_TRACKED(op)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002204 gc_list_remove(g);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002205 }
Victor Stinner9db03242019-04-26 02:32:01 +02002206 struct _gc_runtime_state *state = &_PyRuntime.gc;
2207 if (state->generations[0].count > 0) {
2208 state->generations[0].count--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002209 }
2210 PyObject_FREE(g);
Neil Schemenauer43411b52001-08-30 00:05:51 +00002211}
Tim Petersd9d39932019-11-02 12:06:31 -05002212
2213/* ------------------------------------------------------------------------
2214Notes
2215
Pablo Galindob028f582019-11-19 01:36:57 +00002216[REACHABLE OR UNREACHABLE?]
Tim Petersd9d39932019-11-02 12:06:31 -05002217
2218It "sounds slick" to move the unreachable objects, until you think about
2219it - the reason it pays isn't actually obvious.
2220
2221Suppose we create objects A, B, C in that order. They appear in the young
2222generation in the same order. If B points to A, and C to B, and C is
2223reachable from outside, then the adjusted refcounts will be 0, 0, and 1
2224respectively.
2225
2226When move_unreachable finds A, A is moved to the unreachable list. The
2227same for B when it's first encountered. Then C is traversed, B is moved
2228_back_ to the reachable list. B is eventually traversed, and then A is
2229moved back to the reachable list.
2230
2231So instead of not moving at all, the reachable objects B and A are moved
2232twice each. Why is this a win? A straightforward algorithm to move the
2233reachable objects instead would move A, B, and C once each.
2234
2235The key is that this dance leaves the objects in order C, B, A - it's
2236reversed from the original order. On all _subsequent_ scans, none of
2237them will move. Since most objects aren't in cycles, this can save an
2238unbounded number of moves across an unbounded number of later collections.
2239It can cost more only the first time the chain is scanned.
2240
2241Drawback: move_unreachable is also used to find out what's still trash
2242after finalizers may resurrect objects. In _that_ case most unreachable
2243objects will remain unreachable, so it would be more efficient to move
2244the reachable objects instead. But this is a one-time cost, probably not
2245worth complicating the code to speed just a little.
2246------------------------------------------------------------------------ */
2247