blob: b11ae842e2295ac55490f6c2941a293c80b310d4 [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
Victor Stinner67e0de62019-11-20 11:48:18 +010037typedef struct _gc_runtime_state GCState;
38
Serhiy Storchaka93260282017-02-04 11:19:59 +020039/*[clinic input]
40module gc
41[clinic start generated code]*/
42/*[clinic end generated code: output=da39a3ee5e6b4b0d input=b5c9690ecc842d79]*/
43
Pablo Galindo320dd502019-10-10 22:45:17 +010044
45#ifdef Py_DEBUG
46# define GC_DEBUG
47#endif
INADA Naoki5ac9e6e2018-07-10 17:19:53 +090048
49#define GC_NEXT _PyGCHead_NEXT
50#define GC_PREV _PyGCHead_PREV
51
52// update_refs() set this bit for all objects in current generation.
53// subtract_refs() and move_unreachable() uses this to distinguish
54// visited object is in GCing or not.
55//
56// move_unreachable() removes this flag from reachable objects.
57// Only unreachable objects have this flag.
58//
59// No objects in interpreter have this flag after GC ends.
60#define PREV_MASK_COLLECTING _PyGC_PREV_MASK_COLLECTING
61
62// Lowest bit of _gc_next is used for UNREACHABLE flag.
63//
64// This flag represents the object is in unreachable list in move_unreachable()
65//
66// Although this flag is used only in move_unreachable(), move_unreachable()
67// doesn't clear this flag to skip unnecessary iteration.
68// move_legacy_finalizers() removes this flag instead.
69// Between them, unreachable list is not normal list and we can not use
70// most gc_list_* functions for it.
71#define NEXT_MASK_UNREACHABLE (1)
72
Victor Stinner626bff82018-10-25 17:31:10 +020073/* Get an object's GC head */
74#define AS_GC(o) ((PyGC_Head *)(o)-1)
75
76/* Get the object given the GC head */
77#define FROM_GC(g) ((PyObject *)(((PyGC_Head *)g)+1))
78
INADA Naoki5ac9e6e2018-07-10 17:19:53 +090079static inline int
80gc_is_collecting(PyGC_Head *g)
81{
82 return (g->_gc_prev & PREV_MASK_COLLECTING) != 0;
83}
84
85static inline void
86gc_clear_collecting(PyGC_Head *g)
87{
88 g->_gc_prev &= ~PREV_MASK_COLLECTING;
89}
90
91static inline Py_ssize_t
92gc_get_refs(PyGC_Head *g)
93{
94 return (Py_ssize_t)(g->_gc_prev >> _PyGC_PREV_SHIFT);
95}
96
97static inline void
98gc_set_refs(PyGC_Head *g, Py_ssize_t refs)
99{
100 g->_gc_prev = (g->_gc_prev & ~_PyGC_PREV_MASK)
101 | ((uintptr_t)(refs) << _PyGC_PREV_SHIFT);
102}
103
104static inline void
105gc_reset_refs(PyGC_Head *g, Py_ssize_t refs)
106{
107 g->_gc_prev = (g->_gc_prev & _PyGC_PREV_MASK_FINALIZED)
108 | PREV_MASK_COLLECTING
109 | ((uintptr_t)(refs) << _PyGC_PREV_SHIFT);
110}
111
112static inline void
113gc_decref(PyGC_Head *g)
114{
Victor Stinner626bff82018-10-25 17:31:10 +0200115 _PyObject_ASSERT_WITH_MSG(FROM_GC(g),
116 gc_get_refs(g) > 0,
117 "refcount is too small");
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900118 g->_gc_prev -= 1 << _PyGC_PREV_SHIFT;
119}
120
Tim Peters6fc13d92002-07-02 18:12:35 +0000121/* Python string to use if unhandled exception occurs */
Tim Petersbf384c22003-04-06 00:11:39 +0000122static PyObject *gc_str = NULL;
Tim Peters6fc13d92002-07-02 18:12:35 +0000123
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000124/* set for debugging information */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000125#define DEBUG_STATS (1<<0) /* print collection statistics */
126#define DEBUG_COLLECTABLE (1<<1) /* print collectable objects */
127#define DEBUG_UNCOLLECTABLE (1<<2) /* print uncollectable objects */
128#define DEBUG_SAVEALL (1<<5) /* save all garbage in gc.garbage */
129#define DEBUG_LEAK DEBUG_COLLECTABLE | \
130 DEBUG_UNCOLLECTABLE | \
131 DEBUG_SAVEALL
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000132
Victor Stinner67e0de62019-11-20 11:48:18 +0100133#define GEN_HEAD(gcstate, n) (&(gcstate)->generations[n].head)
Antoine Pitroud4156c12012-10-30 22:43:19 +0100134
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600135void
Victor Stinner72474072019-11-20 12:25:50 +0100136_PyGC_InitState(GCState *gcstate)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600137{
Victor Stinner67e0de62019-11-20 11:48:18 +0100138 gcstate->enabled = 1; /* automatic collection enabled? */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600139
Victor Stinner67e0de62019-11-20 11:48:18 +0100140#define _GEN_HEAD(n) GEN_HEAD(gcstate, n)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600141 struct gc_generation generations[NUM_GENERATIONS] = {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900142 /* PyGC_Head, threshold, count */
143 {{(uintptr_t)_GEN_HEAD(0), (uintptr_t)_GEN_HEAD(0)}, 700, 0},
144 {{(uintptr_t)_GEN_HEAD(1), (uintptr_t)_GEN_HEAD(1)}, 10, 0},
145 {{(uintptr_t)_GEN_HEAD(2), (uintptr_t)_GEN_HEAD(2)}, 10, 0},
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600146 };
147 for (int i = 0; i < NUM_GENERATIONS; i++) {
Victor Stinner67e0de62019-11-20 11:48:18 +0100148 gcstate->generations[i] = generations[i];
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600149 };
Victor Stinner67e0de62019-11-20 11:48:18 +0100150 gcstate->generation0 = GEN_HEAD(gcstate, 0);
brainfvckc75edab2017-10-16 12:49:41 -0700151 struct gc_generation permanent_generation = {
Victor Stinner67e0de62019-11-20 11:48:18 +0100152 {(uintptr_t)&gcstate->permanent_generation.head,
153 (uintptr_t)&gcstate->permanent_generation.head}, 0, 0
brainfvckc75edab2017-10-16 12:49:41 -0700154 };
Victor Stinner67e0de62019-11-20 11:48:18 +0100155 gcstate->permanent_generation = permanent_generation;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600156}
Antoine Pitroud4156c12012-10-30 22:43:19 +0100157
Victor Stinner444b39b2019-11-20 01:18:11 +0100158
159PyStatus
Victor Stinner01b1cc12019-11-20 02:27:56 +0100160_PyGC_Init(PyThreadState *tstate)
Victor Stinner444b39b2019-11-20 01:18:11 +0100161{
Victor Stinner72474072019-11-20 12:25:50 +0100162 GCState *gcstate = &tstate->interp->gc;
Victor Stinner67e0de62019-11-20 11:48:18 +0100163 if (gcstate->garbage == NULL) {
164 gcstate->garbage = PyList_New(0);
165 if (gcstate->garbage == NULL) {
Victor Stinner444b39b2019-11-20 01:18:11 +0100166 return _PyStatus_NO_MEMORY();
167 }
168 }
169 return _PyStatus_OK();
170}
171
172
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900173/*
174_gc_prev values
175---------------
Neil Schemenauer43411b52001-08-30 00:05:51 +0000176
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900177Between collections, _gc_prev is used for doubly linked list.
Tim Peters6fc13d92002-07-02 18:12:35 +0000178
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900179Lowest two bits of _gc_prev are used for flags.
180PREV_MASK_COLLECTING is used only while collecting and cleared before GC ends
181or _PyObject_GC_UNTRACK() is called.
Tim Peters6fc13d92002-07-02 18:12:35 +0000182
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900183During a collection, _gc_prev is temporary used for gc_refs, and the gc list
184is singly linked until _gc_prev is restored.
Tim Peters6fc13d92002-07-02 18:12:35 +0000185
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900186gc_refs
Tim Peters6fc13d92002-07-02 18:12:35 +0000187 At the start of a collection, update_refs() copies the true refcount
188 to gc_refs, for each object in the generation being collected.
189 subtract_refs() then adjusts gc_refs so that it equals the number of
190 times an object is referenced directly from outside the generation
191 being collected.
Tim Peters6fc13d92002-07-02 18:12:35 +0000192
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900193PREV_MASK_COLLECTING
194 Objects in generation being collected are marked PREV_MASK_COLLECTING in
195 update_refs().
196
197
198_gc_next values
199---------------
200
201_gc_next takes these values:
202
2030
204 The object is not tracked
205
206!= 0
207 Pointer to the next object in the GC list.
208 Additionally, lowest bit is used temporary for
209 NEXT_MASK_UNREACHABLE flag described below.
210
211NEXT_MASK_UNREACHABLE
Tim Peters6fc13d92002-07-02 18:12:35 +0000212 move_unreachable() then moves objects not reachable (whether directly or
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900213 indirectly) from outside the generation into an "unreachable" set and
214 set this flag.
Tim Peters6fc13d92002-07-02 18:12:35 +0000215
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900216 Objects that are found to be reachable have gc_refs set to 1.
217 When this flag is set for the reachable object, the object must be in
218 "unreachable" set.
219 The flag is unset and the object is moved back to "reachable" set.
220
221 move_legacy_finalizers() will remove this flag from "unreachable" set.
Tim Peters6fc13d92002-07-02 18:12:35 +0000222*/
Neil Schemenauera2b11ec2002-05-21 15:53:24 +0000223
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000224/*** list functions ***/
225
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900226static inline void
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000227gc_list_init(PyGC_Head *list)
228{
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900229 // List header must not have flags.
230 // We can assign pointer by simple cast.
231 list->_gc_prev = (uintptr_t)list;
232 list->_gc_next = (uintptr_t)list;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000233}
234
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900235static inline int
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000236gc_list_is_empty(PyGC_Head *list)
237{
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900238 return (list->_gc_next == (uintptr_t)list);
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000239}
240
Tim Peterse2d59182004-11-01 01:39:08 +0000241/* Append `node` to `list`. */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900242static inline void
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000243gc_list_append(PyGC_Head *node, PyGC_Head *list)
244{
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900245 PyGC_Head *last = (PyGC_Head *)list->_gc_prev;
246
247 // last <-> node
248 _PyGCHead_SET_PREV(node, last);
249 _PyGCHead_SET_NEXT(last, node);
250
251 // node <-> list
252 _PyGCHead_SET_NEXT(node, list);
253 list->_gc_prev = (uintptr_t)node;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000254}
255
Tim Peterse2d59182004-11-01 01:39:08 +0000256/* Remove `node` from the gc list it's currently in. */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900257static inline void
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000258gc_list_remove(PyGC_Head *node)
259{
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900260 PyGC_Head *prev = GC_PREV(node);
261 PyGC_Head *next = GC_NEXT(node);
262
263 _PyGCHead_SET_NEXT(prev, next);
264 _PyGCHead_SET_PREV(next, prev);
265
266 node->_gc_next = 0; /* object is not currently tracked */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000267}
268
Tim Peterse2d59182004-11-01 01:39:08 +0000269/* Move `node` from the gc list it's currently in (which is not explicitly
270 * named here) to the end of `list`. This is semantically the same as
271 * gc_list_remove(node) followed by gc_list_append(node, list).
272 */
273static void
274gc_list_move(PyGC_Head *node, PyGC_Head *list)
275{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000276 /* Unlink from current list. */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900277 PyGC_Head *from_prev = GC_PREV(node);
278 PyGC_Head *from_next = GC_NEXT(node);
279 _PyGCHead_SET_NEXT(from_prev, from_next);
280 _PyGCHead_SET_PREV(from_next, from_prev);
281
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000282 /* Relink at end of new list. */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900283 // list must not have flags. So we can skip macros.
284 PyGC_Head *to_prev = (PyGC_Head*)list->_gc_prev;
285 _PyGCHead_SET_PREV(node, to_prev);
286 _PyGCHead_SET_NEXT(to_prev, node);
287 list->_gc_prev = (uintptr_t)node;
288 _PyGCHead_SET_NEXT(node, list);
Tim Peterse2d59182004-11-01 01:39:08 +0000289}
290
291/* append list `from` onto list `to`; `from` becomes an empty list */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000292static void
293gc_list_merge(PyGC_Head *from, PyGC_Head *to)
294{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 assert(from != to);
296 if (!gc_list_is_empty(from)) {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900297 PyGC_Head *to_tail = GC_PREV(to);
298 PyGC_Head *from_head = GC_NEXT(from);
299 PyGC_Head *from_tail = GC_PREV(from);
300 assert(from_head != from);
301 assert(from_tail != from);
302
303 _PyGCHead_SET_NEXT(to_tail, from_head);
304 _PyGCHead_SET_PREV(from_head, to_tail);
305
306 _PyGCHead_SET_NEXT(from_tail, to);
307 _PyGCHead_SET_PREV(to, from_tail);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000308 }
309 gc_list_init(from);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000310}
311
Neal Norwitz7b216c52006-03-04 20:01:53 +0000312static Py_ssize_t
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000313gc_list_size(PyGC_Head *list)
314{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315 PyGC_Head *gc;
316 Py_ssize_t n = 0;
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900317 for (gc = GC_NEXT(list); gc != list; gc = GC_NEXT(gc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 n++;
319 }
320 return n;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000321}
322
Pablo Galindo466326d2019-10-13 16:48:59 +0100323/* Walk the list and mark all objects as non-collecting */
324static inline void
325gc_list_clear_collecting(PyGC_Head *collectable)
326{
327 PyGC_Head *gc;
328 for (gc = GC_NEXT(collectable); gc != collectable; gc = GC_NEXT(gc)) {
329 gc_clear_collecting(gc);
330 }
331}
332
Tim Peters259272b2003-04-06 19:41:39 +0000333/* Append objects in a GC list to a Python list.
Pablo Galindo466326d2019-10-13 16:48:59 +0100334 * Return 0 if all OK, < 0 if error (out of memory for list)
Tim Peters259272b2003-04-06 19:41:39 +0000335 */
336static int
337append_objects(PyObject *py_list, PyGC_Head *gc_list)
338{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339 PyGC_Head *gc;
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900340 for (gc = GC_NEXT(gc_list); gc != gc_list; gc = GC_NEXT(gc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 PyObject *op = FROM_GC(gc);
342 if (op != py_list) {
343 if (PyList_Append(py_list, op)) {
344 return -1; /* exception */
345 }
346 }
347 }
348 return 0;
Tim Peters259272b2003-04-06 19:41:39 +0000349}
350
Tim Petersea55c512019-10-18 20:59:14 -0500351// Constants for validate_list's flags argument.
352enum flagstates {collecting_clear_unreachable_clear,
353 collecting_clear_unreachable_set,
354 collecting_set_unreachable_clear,
355 collecting_set_unreachable_set};
356
Pablo Galindo320dd502019-10-10 22:45:17 +0100357#ifdef GC_DEBUG
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900358// validate_list checks list consistency. And it works as document
Tim Peters95bfc8a2019-10-13 16:47:04 -0500359// describing when flags are expected to be set / unset.
360// `head` must be a doubly-linked gc list, although it's fine (expected!) if
361// the prev and next pointers are "polluted" with flags.
362// What's checked:
363// - The `head` pointers are not polluted.
Tim Petersea55c512019-10-18 20:59:14 -0500364// - The objects' PREV_MASK_COLLECTING and NEXT_MASK_UNREACHABLE flags are all
365// `set or clear, as specified by the 'flags' argument.
Tim Peters95bfc8a2019-10-13 16:47:04 -0500366// - The prev and next pointers are mutually consistent.
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900367static void
Tim Petersea55c512019-10-18 20:59:14 -0500368validate_list(PyGC_Head *head, enum flagstates flags)
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900369{
Tim Peters95bfc8a2019-10-13 16:47:04 -0500370 assert((head->_gc_prev & PREV_MASK_COLLECTING) == 0);
371 assert((head->_gc_next & NEXT_MASK_UNREACHABLE) == 0);
Tim Petersea55c512019-10-18 20:59:14 -0500372 uintptr_t prev_value = 0, next_value = 0;
373 switch (flags) {
374 case collecting_clear_unreachable_clear:
375 break;
376 case collecting_set_unreachable_clear:
377 prev_value = PREV_MASK_COLLECTING;
378 break;
379 case collecting_clear_unreachable_set:
380 next_value = NEXT_MASK_UNREACHABLE;
381 break;
382 case collecting_set_unreachable_set:
383 prev_value = PREV_MASK_COLLECTING;
384 next_value = NEXT_MASK_UNREACHABLE;
385 break;
386 default:
387 assert(! "bad internal flags argument");
388 }
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900389 PyGC_Head *prev = head;
390 PyGC_Head *gc = GC_NEXT(head);
391 while (gc != head) {
Tim Peters95bfc8a2019-10-13 16:47:04 -0500392 PyGC_Head *trueprev = GC_PREV(gc);
393 PyGC_Head *truenext = (PyGC_Head *)(gc->_gc_next & ~NEXT_MASK_UNREACHABLE);
394 assert(truenext != NULL);
395 assert(trueprev == prev);
396 assert((gc->_gc_prev & PREV_MASK_COLLECTING) == prev_value);
397 assert((gc->_gc_next & NEXT_MASK_UNREACHABLE) == next_value);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900398 prev = gc;
Tim Peters95bfc8a2019-10-13 16:47:04 -0500399 gc = truenext;
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900400 }
401 assert(prev == GC_PREV(head));
402}
403#else
Tim Petersea55c512019-10-18 20:59:14 -0500404#define validate_list(x, y) do{}while(0)
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900405#endif
406
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000407/*** end of list stuff ***/
408
409
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900410/* Set all gc_refs = ob_refcnt. After this, gc_refs is > 0 and
411 * PREV_MASK_COLLECTING bit is set for all objects in containers.
Tim Peters88396172002-06-30 17:56:40 +0000412 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000413static void
414update_refs(PyGC_Head *containers)
415{
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900416 PyGC_Head *gc = GC_NEXT(containers);
417 for (; gc != containers; gc = GC_NEXT(gc)) {
418 gc_reset_refs(gc, Py_REFCNT(FROM_GC(gc)));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000419 /* Python's cyclic gc should never see an incoming refcount
420 * of 0: if something decref'ed to 0, it should have been
421 * deallocated immediately at that time.
422 * Possible cause (if the assert triggers): a tp_dealloc
423 * routine left a gc-aware object tracked during its teardown
424 * phase, and did something-- or allowed something to happen --
425 * that called back into Python. gc can trigger then, and may
426 * see the still-tracked dying object. Before this assert
427 * was added, such mistakes went on to allow gc to try to
428 * delete the object again. In a debug build, that caused
429 * a mysterious segfault, when _Py_ForgetReference tried
430 * to remove the object from the doubly-linked list of all
431 * objects a second time. In a release build, an actual
432 * double deallocation occurred, which leads to corruption
433 * of the allocator's internal bookkeeping pointers. That's
434 * so serious that maybe this should be a release-build
435 * check instead of an assert?
436 */
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200437 _PyObject_ASSERT(FROM_GC(gc), gc_get_refs(gc) != 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000438 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000439}
440
Tim Peters19b74c72002-07-01 03:52:19 +0000441/* A traversal callback for subtract_refs. */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000442static int
Victor Stinner4d5f94b2019-10-08 02:37:38 +0200443visit_decref(PyObject *op, void *parent)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000444{
Victor Stinner4d5f94b2019-10-08 02:37:38 +0200445 _PyObject_ASSERT(_PyObject_CAST(parent), !_PyObject_IsFreed(op));
Victor Stinnerd91d4de2019-09-09 17:44:59 +0200446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447 if (PyObject_IS_GC(op)) {
448 PyGC_Head *gc = AS_GC(op);
449 /* We're only interested in gc_refs for objects in the
450 * generation being collected, which can be recognized
451 * because only they have positive gc_refs.
452 */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900453 if (gc_is_collecting(gc)) {
454 gc_decref(gc);
455 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000456 }
457 return 0;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000458}
459
Tim Peters19b74c72002-07-01 03:52:19 +0000460/* Subtract internal references from gc_refs. After this, gc_refs is >= 0
461 * for all objects in containers, and is GC_REACHABLE for all tracked gc
462 * objects not in containers. The ones with gc_refs > 0 are directly
463 * reachable from outside containers, and so can't be collected.
464 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000465static void
466subtract_refs(PyGC_Head *containers)
467{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000468 traverseproc traverse;
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900469 PyGC_Head *gc = GC_NEXT(containers);
470 for (; gc != containers; gc = GC_NEXT(gc)) {
Victor Stinner4d5f94b2019-10-08 02:37:38 +0200471 PyObject *op = FROM_GC(gc);
472 traverse = Py_TYPE(op)->tp_traverse;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000473 (void) traverse(FROM_GC(gc),
474 (visitproc)visit_decref,
Victor Stinner4d5f94b2019-10-08 02:37:38 +0200475 op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000477}
478
Tim Peters19b74c72002-07-01 03:52:19 +0000479/* A traversal callback for move_unreachable. */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000480static int
Tim Peters19b74c72002-07-01 03:52:19 +0000481visit_reachable(PyObject *op, PyGC_Head *reachable)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000482{
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900483 if (!PyObject_IS_GC(op)) {
484 return 0;
485 }
Tim Peters19b74c72002-07-01 03:52:19 +0000486
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900487 PyGC_Head *gc = AS_GC(op);
488 const Py_ssize_t gc_refs = gc_get_refs(gc);
489
Tim Peters1e739452019-10-21 11:21:35 -0500490 // Ignore objects in other generation.
Tim Peters95bfc8a2019-10-13 16:47:04 -0500491 // This also skips objects "to the left" of the current position in
492 // move_unreachable's scan of the 'young' list - they've already been
493 // traversed, and no longer have the PREV_MASK_COLLECTING flag.
Tim Peters1e739452019-10-21 11:21:35 -0500494 if (! gc_is_collecting(gc)) {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900495 return 0;
496 }
Tim Peters1e739452019-10-21 11:21:35 -0500497 // It would be a logic error elsewhere if the collecting flag were set on
498 // an untracked object.
499 assert(gc->_gc_next != 0);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900500
501 if (gc->_gc_next & NEXT_MASK_UNREACHABLE) {
502 /* This had gc_refs = 0 when move_unreachable got
503 * to it, but turns out it's reachable after all.
504 * Move it back to move_unreachable's 'young' list,
505 * and move_unreachable will eventually get to it
506 * again.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 */
Tim Peters95bfc8a2019-10-13 16:47:04 -0500508 // Manually unlink gc from unreachable list because the list functions
509 // don't work right in the presence of NEXT_MASK_UNREACHABLE flags.
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900510 PyGC_Head *prev = GC_PREV(gc);
511 PyGC_Head *next = (PyGC_Head*)(gc->_gc_next & ~NEXT_MASK_UNREACHABLE);
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200512 _PyObject_ASSERT(FROM_GC(prev),
513 prev->_gc_next & NEXT_MASK_UNREACHABLE);
514 _PyObject_ASSERT(FROM_GC(next),
515 next->_gc_next & NEXT_MASK_UNREACHABLE);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900516 prev->_gc_next = gc->_gc_next; // copy NEXT_MASK_UNREACHABLE
517 _PyGCHead_SET_PREV(next, prev);
518
519 gc_list_append(gc, reachable);
520 gc_set_refs(gc, 1);
521 }
522 else if (gc_refs == 0) {
523 /* This is in move_unreachable's 'young' list, but
524 * the traversal hasn't yet gotten to it. All
525 * we need to do is tell move_unreachable that it's
526 * reachable.
527 */
528 gc_set_refs(gc, 1);
529 }
530 /* Else there's nothing to do.
531 * If gc_refs > 0, it must be in move_unreachable's 'young'
532 * list, and move_unreachable will eventually get to it.
533 */
534 else {
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200535 _PyObject_ASSERT_WITH_MSG(op, gc_refs > 0, "refcount is too small");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 }
537 return 0;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000538}
539
Tim Peters19b74c72002-07-01 03:52:19 +0000540/* Move the unreachable objects from young to unreachable. After this,
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900541 * all objects in young don't have PREV_MASK_COLLECTING flag and
542 * unreachable have the flag.
Tim Peters19b74c72002-07-01 03:52:19 +0000543 * All objects in young after this are directly or indirectly reachable
544 * from outside the original young; and all objects in unreachable are
545 * not.
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900546 *
547 * This function restores _gc_prev pointer. young and unreachable are
548 * doubly linked list after this function.
549 * But _gc_next in unreachable list has NEXT_MASK_UNREACHABLE flag.
550 * So we can not gc_list_* functions for unreachable until we remove the flag.
Tim Peters88396172002-06-30 17:56:40 +0000551 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000552static void
Tim Peters19b74c72002-07-01 03:52:19 +0000553move_unreachable(PyGC_Head *young, PyGC_Head *unreachable)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000554{
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900555 // previous elem in the young list, used for restore gc_prev.
556 PyGC_Head *prev = young;
557 PyGC_Head *gc = GC_NEXT(young);
Tim Peters19b74c72002-07-01 03:52:19 +0000558
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900559 /* Invariants: all objects "to the left" of us in young are reachable
560 * (directly or indirectly) from outside the young list as it was at entry.
561 *
562 * All other objects from the original young "to the left" of us are in
563 * unreachable now, and have NEXT_MASK_UNREACHABLE. All objects to the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 * left of us in 'young' now have been scanned, and no objects here
565 * or to the right have been scanned yet.
566 */
Tim Peters19b74c72002-07-01 03:52:19 +0000567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 while (gc != young) {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900569 if (gc_get_refs(gc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000570 /* gc is definitely reachable from outside the
571 * original 'young'. Mark it as such, and traverse
572 * its pointers to find any other objects that may
573 * be directly reachable from it. Note that the
574 * call to tp_traverse may append objects to young,
575 * so we have to wait until it returns to determine
576 * the next object to visit.
577 */
578 PyObject *op = FROM_GC(gc);
579 traverseproc traverse = Py_TYPE(op)->tp_traverse;
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200580 _PyObject_ASSERT_WITH_MSG(op, gc_get_refs(gc) > 0,
581 "refcount is too small");
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900582 // NOTE: visit_reachable may change gc->_gc_next when
583 // young->_gc_prev == gc. Don't do gc = GC_NEXT(gc) before!
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 (void) traverse(op,
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900585 (visitproc)visit_reachable,
586 (void *)young);
587 // relink gc_prev to prev element.
588 _PyGCHead_SET_PREV(gc, prev);
Quan Tian3bd0d622018-10-20 05:30:03 +0800589 // gc is not COLLECTING state after here.
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900590 gc_clear_collecting(gc);
591 prev = gc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000592 }
593 else {
594 /* This *may* be unreachable. To make progress,
595 * assume it is. gc isn't directly reachable from
596 * any object we've already traversed, but may be
597 * reachable from an object we haven't gotten to yet.
598 * visit_reachable will eventually move gc back into
599 * young if that's so, and we'll see it again.
600 */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900601 // Move gc to unreachable.
602 // No need to gc->next->prev = prev because it is single linked.
603 prev->_gc_next = gc->_gc_next;
604
605 // We can't use gc_list_append() here because we use
606 // NEXT_MASK_UNREACHABLE here.
607 PyGC_Head *last = GC_PREV(unreachable);
608 // NOTE: Since all objects in unreachable set has
609 // NEXT_MASK_UNREACHABLE flag, we set it unconditionally.
Tim Peters95bfc8a2019-10-13 16:47:04 -0500610 // But this may pollute the unreachable list head's 'next' pointer
611 // too. That's semantically senseless but expedient here - the
612 // damage is repaired when this fumction ends.
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900613 last->_gc_next = (NEXT_MASK_UNREACHABLE | (uintptr_t)gc);
614 _PyGCHead_SET_PREV(gc, last);
615 gc->_gc_next = (NEXT_MASK_UNREACHABLE | (uintptr_t)unreachable);
616 unreachable->_gc_prev = (uintptr_t)gc;
617 }
618 gc = (PyGC_Head*)prev->_gc_next;
619 }
620 // young->_gc_prev must be last element remained in the list.
621 young->_gc_prev = (uintptr_t)prev;
Tim Peters95bfc8a2019-10-13 16:47:04 -0500622 // don't let the pollution of the list head's next pointer leak
623 unreachable->_gc_next &= ~NEXT_MASK_UNREACHABLE;
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900624}
625
626static void
627untrack_tuples(PyGC_Head *head)
628{
629 PyGC_Head *next, *gc = GC_NEXT(head);
630 while (gc != head) {
631 PyObject *op = FROM_GC(gc);
632 next = GC_NEXT(gc);
633 if (PyTuple_CheckExact(op)) {
634 _PyTuple_MaybeUntrack(op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 }
636 gc = next;
637 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000638}
639
Antoine Pitroue1ad3da2012-05-28 22:22:34 +0200640/* Try to untrack all currently tracked dictionaries */
641static void
642untrack_dicts(PyGC_Head *head)
643{
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900644 PyGC_Head *next, *gc = GC_NEXT(head);
Antoine Pitroue1ad3da2012-05-28 22:22:34 +0200645 while (gc != head) {
646 PyObject *op = FROM_GC(gc);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900647 next = GC_NEXT(gc);
648 if (PyDict_CheckExact(op)) {
Antoine Pitroue1ad3da2012-05-28 22:22:34 +0200649 _PyDict_MaybeUntrack(op);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900650 }
Antoine Pitroue1ad3da2012-05-28 22:22:34 +0200651 gc = next;
652 }
653}
654
Antoine Pitrou796564c2013-07-30 19:59:21 +0200655/* Return true if object has a pre-PEP 442 finalization method. */
Neil Schemenauera765c122001-11-01 17:35:23 +0000656static int
Antoine Pitrou796564c2013-07-30 19:59:21 +0200657has_legacy_finalizer(PyObject *op)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000658{
Antoine Pitrou796564c2013-07-30 19:59:21 +0200659 return op->ob_type->tp_del != NULL;
Neil Schemenauera765c122001-11-01 17:35:23 +0000660}
661
Antoine Pitrou796564c2013-07-30 19:59:21 +0200662/* Move the objects in unreachable with tp_del slots into `finalizers`.
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900663 *
664 * This function also removes NEXT_MASK_UNREACHABLE flag
665 * from _gc_next in unreachable.
Jeremy Hyltonce136e92003-04-04 19:59:06 +0000666 */
Neil Schemenauera765c122001-11-01 17:35:23 +0000667static void
Antoine Pitrou796564c2013-07-30 19:59:21 +0200668move_legacy_finalizers(PyGC_Head *unreachable, PyGC_Head *finalizers)
Neil Schemenauera765c122001-11-01 17:35:23 +0000669{
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900670 PyGC_Head *gc, *next;
Tim Peters95bfc8a2019-10-13 16:47:04 -0500671 assert((unreachable->_gc_next & NEXT_MASK_UNREACHABLE) == 0);
Tim Petersf6b80452003-04-07 19:21:15 +0000672
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 /* March over unreachable. Move objects with finalizers into
674 * `finalizers`.
675 */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900676 for (gc = GC_NEXT(unreachable); gc != unreachable; gc = next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000677 PyObject *op = FROM_GC(gc);
Jeremy Hyltonce136e92003-04-04 19:59:06 +0000678
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200679 _PyObject_ASSERT(op, gc->_gc_next & NEXT_MASK_UNREACHABLE);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900680 gc->_gc_next &= ~NEXT_MASK_UNREACHABLE;
681 next = (PyGC_Head*)gc->_gc_next;
Tim Petersf6ae7a42003-04-05 18:40:50 +0000682
Antoine Pitrou796564c2013-07-30 19:59:21 +0200683 if (has_legacy_finalizer(op)) {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900684 gc_clear_collecting(gc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000685 gc_list_move(gc, finalizers);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 }
687 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000688}
689
Pablo Galindo466326d2019-10-13 16:48:59 +0100690static inline void
691clear_unreachable_mask(PyGC_Head *unreachable)
692{
693 /* Check that the list head does not have the unreachable bit set */
694 assert(((uintptr_t)unreachable & NEXT_MASK_UNREACHABLE) == 0);
695
696 PyGC_Head *gc, *next;
Tim Peters95bfc8a2019-10-13 16:47:04 -0500697 assert((unreachable->_gc_next & NEXT_MASK_UNREACHABLE) == 0);
Pablo Galindo466326d2019-10-13 16:48:59 +0100698 for (gc = GC_NEXT(unreachable); gc != unreachable; gc = next) {
699 _PyObject_ASSERT((PyObject*)FROM_GC(gc), gc->_gc_next & NEXT_MASK_UNREACHABLE);
700 gc->_gc_next &= ~NEXT_MASK_UNREACHABLE;
701 next = (PyGC_Head*)gc->_gc_next;
702 }
Tim Petersea55c512019-10-18 20:59:14 -0500703 validate_list(unreachable, collecting_set_unreachable_clear);
Pablo Galindo466326d2019-10-13 16:48:59 +0100704}
705
Antoine Pitrou796564c2013-07-30 19:59:21 +0200706/* A traversal callback for move_legacy_finalizer_reachable. */
Tim Peters19b74c72002-07-01 03:52:19 +0000707static int
708visit_move(PyObject *op, PyGC_Head *tolist)
709{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000710 if (PyObject_IS_GC(op)) {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900711 PyGC_Head *gc = AS_GC(op);
712 if (gc_is_collecting(gc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 gc_list_move(gc, tolist);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900714 gc_clear_collecting(gc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000715 }
716 }
717 return 0;
Tim Peters19b74c72002-07-01 03:52:19 +0000718}
719
720/* Move objects that are reachable from finalizers, from the unreachable set
Tim Petersf6b80452003-04-07 19:21:15 +0000721 * into finalizers set.
Tim Peters19b74c72002-07-01 03:52:19 +0000722 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000723static void
Antoine Pitrou796564c2013-07-30 19:59:21 +0200724move_legacy_finalizer_reachable(PyGC_Head *finalizers)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000725{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000726 traverseproc traverse;
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900727 PyGC_Head *gc = GC_NEXT(finalizers);
728 for (; gc != finalizers; gc = GC_NEXT(gc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000729 /* Note that the finalizers list may grow during this. */
730 traverse = Py_TYPE(FROM_GC(gc))->tp_traverse;
731 (void) traverse(FROM_GC(gc),
732 (visitproc)visit_move,
733 (void *)finalizers);
734 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000735}
736
Tim Petersead8b7a2004-10-30 23:09:22 +0000737/* Clear all weakrefs to unreachable objects, and if such a weakref has a
738 * callback, invoke it if necessary. Note that it's possible for such
739 * weakrefs to be outside the unreachable set -- indeed, those are precisely
740 * the weakrefs whose callbacks must be invoked. See gc_weakref.txt for
741 * overview & some details. Some weakrefs with callbacks may be reclaimed
742 * directly by this routine; the number reclaimed is the return value. Other
743 * weakrefs with callbacks may be moved into the `old` generation. Objects
744 * moved into `old` have gc_refs set to GC_REACHABLE; the objects remaining in
745 * unreachable are left at GC_TENTATIVELY_UNREACHABLE. When this returns,
746 * no object in `unreachable` is weakly referenced anymore.
Tim Peters403a2032003-11-20 21:21:46 +0000747 */
748static int
Tim Petersead8b7a2004-10-30 23:09:22 +0000749handle_weakrefs(PyGC_Head *unreachable, PyGC_Head *old)
Tim Peters403a2032003-11-20 21:21:46 +0000750{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000751 PyGC_Head *gc;
752 PyObject *op; /* generally FROM_GC(gc) */
753 PyWeakReference *wr; /* generally a cast of op */
754 PyGC_Head wrcb_to_call; /* weakrefs with callbacks to call */
755 PyGC_Head *next;
756 int num_freed = 0;
Tim Peters403a2032003-11-20 21:21:46 +0000757
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000758 gc_list_init(&wrcb_to_call);
Tim Peters403a2032003-11-20 21:21:46 +0000759
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000760 /* Clear all weakrefs to the objects in unreachable. If such a weakref
761 * also has a callback, move it into `wrcb_to_call` if the callback
762 * needs to be invoked. Note that we cannot invoke any callbacks until
763 * all weakrefs to unreachable objects are cleared, lest the callback
764 * resurrect an unreachable object via a still-active weakref. We
765 * make another pass over wrcb_to_call, invoking callbacks, after this
766 * pass completes.
767 */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900768 for (gc = GC_NEXT(unreachable); gc != unreachable; gc = next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 PyWeakReference **wrlist;
Tim Petersead8b7a2004-10-30 23:09:22 +0000770
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 op = FROM_GC(gc);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900772 next = GC_NEXT(gc);
Tim Petersead8b7a2004-10-30 23:09:22 +0000773
Neil Schemenauerbcda4602019-09-30 10:06:45 -0700774 if (PyWeakref_Check(op)) {
775 /* A weakref inside the unreachable set must be cleared. If we
776 * allow its callback to execute inside delete_garbage(), it
777 * could expose objects that have tp_clear already called on
778 * them. Or, it could resurrect unreachable objects. One way
779 * this can happen is if some container objects do not implement
780 * tp_traverse. Then, wr_object can be outside the unreachable
781 * set but can be deallocated as a result of breaking the
782 * reference cycle. If we don't clear the weakref, the callback
783 * will run and potentially cause a crash. See bpo-38006 for
784 * one example.
785 */
786 _PyWeakref_ClearRef((PyWeakReference *)op);
787 }
788
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000789 if (! PyType_SUPPORTS_WEAKREFS(Py_TYPE(op)))
790 continue;
Tim Petersead8b7a2004-10-30 23:09:22 +0000791
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 /* It supports weakrefs. Does it have any? */
793 wrlist = (PyWeakReference **)
794 PyObject_GET_WEAKREFS_LISTPTR(op);
Tim Petersead8b7a2004-10-30 23:09:22 +0000795
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 /* `op` may have some weakrefs. March over the list, clear
797 * all the weakrefs, and move the weakrefs with callbacks
798 * that must be called into wrcb_to_call.
799 */
800 for (wr = *wrlist; wr != NULL; wr = *wrlist) {
801 PyGC_Head *wrasgc; /* AS_GC(wr) */
Tim Petersead8b7a2004-10-30 23:09:22 +0000802
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000803 /* _PyWeakref_ClearRef clears the weakref but leaves
804 * the callback pointer intact. Obscure: it also
805 * changes *wrlist.
806 */
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200807 _PyObject_ASSERT((PyObject *)wr, wr->wr_object == op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 _PyWeakref_ClearRef(wr);
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200809 _PyObject_ASSERT((PyObject *)wr, wr->wr_object == Py_None);
810 if (wr->wr_callback == NULL) {
811 /* no callback */
812 continue;
813 }
Tim Petersead8b7a2004-10-30 23:09:22 +0000814
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200815 /* Headache time. `op` is going away, and is weakly referenced by
816 * `wr`, which has a callback. Should the callback be invoked? If wr
817 * is also trash, no:
818 *
819 * 1. There's no need to call it. The object and the weakref are
820 * both going away, so it's legitimate to pretend the weakref is
821 * going away first. The user has to ensure a weakref outlives its
822 * referent if they want a guarantee that the wr callback will get
823 * invoked.
824 *
825 * 2. It may be catastrophic to call it. If the callback is also in
826 * cyclic trash (CT), then although the CT is unreachable from
827 * outside the current generation, CT may be reachable from the
828 * callback. Then the callback could resurrect insane objects.
829 *
830 * Since the callback is never needed and may be unsafe in this case,
831 * wr is simply left in the unreachable set. Note that because we
832 * already called _PyWeakref_ClearRef(wr), its callback will never
833 * trigger.
834 *
835 * OTOH, if wr isn't part of CT, we should invoke the callback: the
836 * weakref outlived the trash. Note that since wr isn't CT in this
837 * case, its callback can't be CT either -- wr acted as an external
838 * root to this generation, and therefore its callback did too. So
839 * nothing in CT is reachable from the callback either, so it's hard
840 * to imagine how calling it later could create a problem for us. wr
841 * is moved to wrcb_to_call in this case.
842 */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900843 if (gc_is_collecting(AS_GC(wr))) {
Neil Schemenauerbcda4602019-09-30 10:06:45 -0700844 /* it should already have been cleared above */
845 assert(wr->wr_object == Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 continue;
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900847 }
Tim Peterscc2a8662004-10-31 22:12:43 +0000848
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000849 /* Create a new reference so that wr can't go away
850 * before we can process it again.
851 */
852 Py_INCREF(wr);
Tim Petersead8b7a2004-10-30 23:09:22 +0000853
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 /* Move wr to wrcb_to_call, for the next pass. */
855 wrasgc = AS_GC(wr);
856 assert(wrasgc != next); /* wrasgc is reachable, but
857 next isn't, so they can't
858 be the same */
859 gc_list_move(wrasgc, &wrcb_to_call);
860 }
861 }
Tim Petersead8b7a2004-10-30 23:09:22 +0000862
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000863 /* Invoke the callbacks we decided to honor. It's safe to invoke them
864 * because they can't reference unreachable objects.
865 */
866 while (! gc_list_is_empty(&wrcb_to_call)) {
867 PyObject *temp;
868 PyObject *callback;
Tim Petersead8b7a2004-10-30 23:09:22 +0000869
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900870 gc = (PyGC_Head*)wrcb_to_call._gc_next;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000871 op = FROM_GC(gc);
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200872 _PyObject_ASSERT(op, PyWeakref_Check(op));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 wr = (PyWeakReference *)op;
874 callback = wr->wr_callback;
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200875 _PyObject_ASSERT(op, callback != NULL);
Tim Petersead8b7a2004-10-30 23:09:22 +0000876
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 /* copy-paste of weakrefobject.c's handle_callback() */
Jeroen Demeyer196a5302019-07-04 12:31:34 +0200878 temp = _PyObject_CallOneArg(callback, (PyObject *)wr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 if (temp == NULL)
880 PyErr_WriteUnraisable(callback);
881 else
882 Py_DECREF(temp);
Tim Petersead8b7a2004-10-30 23:09:22 +0000883
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000884 /* Give up the reference we created in the first pass. When
885 * op's refcount hits 0 (which it may or may not do right now),
886 * op's tp_dealloc will decref op->wr_callback too. Note
887 * that the refcount probably will hit 0 now, and because this
888 * weakref was reachable to begin with, gc didn't already
889 * add it to its count of freed objects. Example: a reachable
890 * weak value dict maps some key to this reachable weakref.
891 * The callback removes this key->weakref mapping from the
892 * dict, leaving no other references to the weakref (excepting
893 * ours).
894 */
895 Py_DECREF(op);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900896 if (wrcb_to_call._gc_next == (uintptr_t)gc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 /* object is still alive -- move it */
898 gc_list_move(gc, old);
899 }
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900900 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 ++num_freed;
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900902 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 }
Tim Petersead8b7a2004-10-30 23:09:22 +0000904
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 return num_freed;
Tim Peters403a2032003-11-20 21:21:46 +0000906}
907
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000908static void
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200909debug_cycle(const char *msg, PyObject *op)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000910{
Victor Stinner499dfcf2011-03-21 13:26:24 +0100911 PySys_FormatStderr("gc: %s <%s %p>\n",
912 msg, Py_TYPE(op)->tp_name, op);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000913}
914
Antoine Pitrou796564c2013-07-30 19:59:21 +0200915/* Handle uncollectable garbage (cycles with tp_del slots, and stuff reachable
Tim Petersbf384c22003-04-06 00:11:39 +0000916 * only from such cycles).
Tim Petersf6b80452003-04-07 19:21:15 +0000917 * If DEBUG_SAVEALL, all objects in finalizers are appended to the module
918 * garbage list (a Python list), else only the objects in finalizers with
919 * __del__ methods are appended to garbage. All objects in finalizers are
920 * merged into the old list regardless.
Tim Petersbf384c22003-04-06 00:11:39 +0000921 */
Serhiy Storchaka301e3cc2018-05-24 15:19:29 +0300922static void
Victor Stinner2e969062019-11-20 01:49:32 +0100923handle_legacy_finalizers(PyThreadState *tstate,
Victor Stinner67e0de62019-11-20 11:48:18 +0100924 GCState *gcstate,
Victor Stinner9db03242019-04-26 02:32:01 +0200925 PyGC_Head *finalizers, PyGC_Head *old)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000926{
Victor Stinner2e969062019-11-20 01:49:32 +0100927 assert(!_PyErr_Occurred(tstate));
Victor Stinner67e0de62019-11-20 11:48:18 +0100928 assert(gcstate->garbage != NULL);
Victor Stinner9db03242019-04-26 02:32:01 +0200929
930 PyGC_Head *gc = GC_NEXT(finalizers);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900931 for (; gc != finalizers; gc = GC_NEXT(gc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 PyObject *op = FROM_GC(gc);
Tim Petersf6b80452003-04-07 19:21:15 +0000933
Victor Stinner67e0de62019-11-20 11:48:18 +0100934 if ((gcstate->debug & DEBUG_SAVEALL) || has_legacy_finalizer(op)) {
935 if (PyList_Append(gcstate->garbage, op) < 0) {
Victor Stinner2e969062019-11-20 01:49:32 +0100936 _PyErr_Clear(tstate);
Serhiy Storchaka301e3cc2018-05-24 15:19:29 +0300937 break;
Serhiy Storchakac4653c92018-05-29 18:50:10 +0300938 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 }
940 }
Tim Petersf6b80452003-04-07 19:21:15 +0000941
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000942 gc_list_merge(finalizers, old);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000943}
944
Tim Peters5fbc7b12014-05-08 17:42:19 -0500945/* Run first-time finalizers (if any) on all the objects in collectable.
946 * Note that this may remove some (or even all) of the objects from the
947 * list, due to refcounts falling to 0.
948 */
Antoine Pitrou796564c2013-07-30 19:59:21 +0200949static void
Victor Stinner67e0de62019-11-20 11:48:18 +0100950finalize_garbage(PyThreadState *tstate, PyGC_Head *collectable)
Antoine Pitrou796564c2013-07-30 19:59:21 +0200951{
952 destructor finalize;
Tim Peters5fbc7b12014-05-08 17:42:19 -0500953 PyGC_Head seen;
Antoine Pitrou796564c2013-07-30 19:59:21 +0200954
Tim Peters5fbc7b12014-05-08 17:42:19 -0500955 /* While we're going through the loop, `finalize(op)` may cause op, or
956 * other objects, to be reclaimed via refcounts falling to zero. So
957 * there's little we can rely on about the structure of the input
958 * `collectable` list across iterations. For safety, we always take the
959 * first object in that list and move it to a temporary `seen` list.
960 * If objects vanish from the `collectable` and `seen` lists we don't
961 * care.
962 */
963 gc_list_init(&seen);
964
965 while (!gc_list_is_empty(collectable)) {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900966 PyGC_Head *gc = GC_NEXT(collectable);
Antoine Pitrou796564c2013-07-30 19:59:21 +0200967 PyObject *op = FROM_GC(gc);
Tim Peters5fbc7b12014-05-08 17:42:19 -0500968 gc_list_move(gc, &seen);
Antoine Pitrou796564c2013-07-30 19:59:21 +0200969 if (!_PyGCHead_FINALIZED(gc) &&
Tim Peters5fbc7b12014-05-08 17:42:19 -0500970 (finalize = Py_TYPE(op)->tp_finalize) != NULL) {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900971 _PyGCHead_SET_FINALIZED(gc);
Antoine Pitrou796564c2013-07-30 19:59:21 +0200972 Py_INCREF(op);
973 finalize(op);
Victor Stinner67e0de62019-11-20 11:48:18 +0100974 assert(!_PyErr_Occurred(tstate));
Antoine Pitrou796564c2013-07-30 19:59:21 +0200975 Py_DECREF(op);
976 }
977 }
Tim Peters5fbc7b12014-05-08 17:42:19 -0500978 gc_list_merge(&seen, collectable);
Antoine Pitrou796564c2013-07-30 19:59:21 +0200979}
980
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981/* Break reference cycles by clearing the containers involved. This is
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000982 * tricky business as the lists can be changing and we don't know which
Tim Peters19b74c72002-07-01 03:52:19 +0000983 * objects may be freed. It is possible I screwed something up here.
984 */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000985static void
Victor Stinner67e0de62019-11-20 11:48:18 +0100986delete_garbage(PyThreadState *tstate, GCState *gcstate,
Victor Stinner9db03242019-04-26 02:32:01 +0200987 PyGC_Head *collectable, PyGC_Head *old)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000988{
Victor Stinner2e969062019-11-20 01:49:32 +0100989 assert(!_PyErr_Occurred(tstate));
Victor Stinner9db03242019-04-26 02:32:01 +0200990
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 while (!gc_list_is_empty(collectable)) {
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900992 PyGC_Head *gc = GC_NEXT(collectable);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 PyObject *op = FROM_GC(gc);
Tim Peters88396172002-06-30 17:56:40 +0000994
Victor Stinnera4b2bc72018-10-26 18:00:13 +0200995 _PyObject_ASSERT_WITH_MSG(op, Py_REFCNT(op) > 0,
996 "refcount is too small");
INADA Naoki5ac9e6e2018-07-10 17:19:53 +0900997
Victor Stinner67e0de62019-11-20 11:48:18 +0100998 if (gcstate->debug & DEBUG_SAVEALL) {
999 assert(gcstate->garbage != NULL);
1000 if (PyList_Append(gcstate->garbage, op) < 0) {
Victor Stinner2e969062019-11-20 01:49:32 +01001001 _PyErr_Clear(tstate);
Serhiy Storchakac4653c92018-05-29 18:50:10 +03001002 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001003 }
1004 else {
Victor Stinner9db03242019-04-26 02:32:01 +02001005 inquiry clear;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 if ((clear = Py_TYPE(op)->tp_clear) != NULL) {
1007 Py_INCREF(op);
Serhiy Storchakac4653c92018-05-29 18:50:10 +03001008 (void) clear(op);
Victor Stinner2e969062019-11-20 01:49:32 +01001009 if (_PyErr_Occurred(tstate)) {
Victor Stinner71c52e32019-05-27 08:57:14 +02001010 _PyErr_WriteUnraisableMsg("in tp_clear of",
1011 (PyObject*)Py_TYPE(op));
Serhiy Storchakac4653c92018-05-29 18:50:10 +03001012 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001013 Py_DECREF(op);
1014 }
1015 }
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09001016 if (GC_NEXT(collectable) == gc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 /* object is still alive, move it, it may die later */
Pablo Galindo466326d2019-10-13 16:48:59 +01001018 gc_clear_collecting(gc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001019 gc_list_move(gc, old);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 }
1021 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001022}
1023
Christian Heimesa156e092008-02-16 07:38:31 +00001024/* Clear all free lists
1025 * All free lists are cleared during the collection of the highest generation.
1026 * Allocated items in the free list may keep a pymalloc arena occupied.
1027 * Clearing the free lists may give back memory to the OS earlier.
1028 */
1029static void
1030clear_freelists(void)
1031{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 (void)PyFrame_ClearFreeList();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 (void)PyTuple_ClearFreeList();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034 (void)PyFloat_ClearFreeList();
Antoine Pitrou9a812cb2011-11-15 00:00:12 +01001035 (void)PyList_ClearFreeList();
1036 (void)PyDict_ClearFreeList();
Antoine Pitrou093ce9c2011-12-16 11:24:27 +01001037 (void)PySet_ClearFreeList();
Yury Selivanoveb636452016-09-08 22:01:51 -07001038 (void)PyAsyncGen_ClearFreeLists();
Yury Selivanovf23746a2018-01-22 19:11:18 -05001039 (void)PyContext_ClearFreeList();
Christian Heimesa156e092008-02-16 07:38:31 +00001040}
1041
Inada Naokibf8162c2019-08-02 16:25:29 +09001042// Show stats for objects in each gennerations.
1043static void
Victor Stinner67e0de62019-11-20 11:48:18 +01001044show_stats_each_generations(GCState *gcstate)
Inada Naokibf8162c2019-08-02 16:25:29 +09001045{
1046 char buf[100];
1047 size_t pos = 0;
1048
1049 for (int i = 0; i < NUM_GENERATIONS && pos < sizeof(buf); i++) {
1050 pos += PyOS_snprintf(buf+pos, sizeof(buf)-pos,
1051 " %"PY_FORMAT_SIZE_T"d",
Victor Stinner67e0de62019-11-20 11:48:18 +01001052 gc_list_size(GEN_HEAD(gcstate, i)));
Inada Naokibf8162c2019-08-02 16:25:29 +09001053 }
1054
1055 PySys_FormatStderr(
1056 "gc: objects in each generation:%s\n"
1057 "gc: objects in permanent generation: %zd\n",
Victor Stinner67e0de62019-11-20 11:48:18 +01001058 buf, gc_list_size(&gcstate->permanent_generation.head));
Inada Naokibf8162c2019-08-02 16:25:29 +09001059}
1060
Pablo Galindo466326d2019-10-13 16:48:59 +01001061/* Deduce wich objects among "base" are unreachable from outside the list
1062 and move them to 'unreachable'. The process consist in the following steps:
1063
10641. Copy all reference counts to a different field (gc_prev is used to hold
1065 this copy to save memory).
10662. Traverse all objects in "base" and visit all referred objects using
1067 "tp_traverse" and for every visited object, substract 1 to the reference
1068 count (the one that we copied in the previous step). After this step, all
1069 objects that can be reached directly from outside must have strictly positive
1070 reference count, while all unreachable objects must have a count of exactly 0.
10713. Indentify all unreachable objects (the ones with 0 reference count) and move
1072 them to the "unreachable" list. This step also needs to move back to "base" all
1073 objects that were initially marked as unreachable but are referred transitively
1074 by the reachable objects (the ones with strictly positive reference count).
1075
1076Contracts:
1077
1078 * The "base" has to be a valid list with no mask set.
1079
1080 * The "unreachable" list must be uninitialized (this function calls
1081 gc_list_init over 'unreachable').
Tim Peters95bfc8a2019-10-13 16:47:04 -05001082
Pablo Galindo466326d2019-10-13 16:48:59 +01001083IMPORTANT: This function leaves 'unreachable' with the NEXT_MASK_UNREACHABLE
1084flag set but it does not clear it to skip unnecessary iteration. Before the
1085flag is cleared (for example, by using 'clear_unreachable_mask' function or
1086by a call to 'move_legacy_finalizers'), the 'unreachable' list is not a normal
1087list and we can not use most gc_list_* functions for it. */
1088static inline void
1089deduce_unreachable(PyGC_Head *base, PyGC_Head *unreachable) {
Tim Petersea55c512019-10-18 20:59:14 -05001090 validate_list(base, collecting_clear_unreachable_clear);
Pablo Galindo466326d2019-10-13 16:48:59 +01001091 /* Using ob_refcnt and gc_refs, calculate which objects in the
1092 * container set are reachable from outside the set (i.e., have a
1093 * refcount greater than 0 when all the references within the
1094 * set are taken into account).
1095 */
1096 update_refs(base); // gc_prev is used for gc_refs
1097 subtract_refs(base);
1098
1099 /* Leave everything reachable from outside base in base, and move
1100 * everything else (in base) to unreachable.
1101 * NOTE: This used to move the reachable objects into a reachable
1102 * set instead. But most things usually turn out to be reachable,
Tim Petersd9d39932019-11-02 12:06:31 -05001103 * so it's more efficient to move the unreachable things. See note
Pablo Galindob028f582019-11-19 01:36:57 +00001104 ^ [REACHABLE OR UNREACHABLE?] at the file end.
Pablo Galindo466326d2019-10-13 16:48:59 +01001105 */
1106 gc_list_init(unreachable);
1107 move_unreachable(base, unreachable); // gc_prev is pointer again
Tim Petersea55c512019-10-18 20:59:14 -05001108 validate_list(base, collecting_clear_unreachable_clear);
1109 validate_list(unreachable, collecting_set_unreachable_set);
Pablo Galindo466326d2019-10-13 16:48:59 +01001110}
1111
1112/* Handle objects that may have resurrected after a call to 'finalize_garbage', moving
1113 them to 'old_generation' and placing the rest on 'still_unreachable'.
1114
1115 Contracts:
1116 * After this function 'unreachable' must not be used anymore and 'still_unreachable'
1117 will contain the objects that did not resurrect.
1118
1119 * The "still_unreachable" list must be uninitialized (this function calls
1120 gc_list_init over 'still_unreachable').
1121
1122IMPORTANT: After a call to this function, the 'still_unreachable' set will have the
1123PREV_MARK_COLLECTING set, but the objects in this set are going to be removed so
1124we can skip the expense of clearing the flag to avoid extra iteration. */
1125static inline void
1126handle_resurrected_objects(PyGC_Head *unreachable, PyGC_Head* still_unreachable,
1127 PyGC_Head *old_generation)
1128{
1129 // Remove the PREV_MASK_COLLECTING from unreachable
1130 // to prepare it for a new call to 'deduce_unreachable'
1131 gc_list_clear_collecting(unreachable);
1132
1133 // After the call to deduce_unreachable, the 'still_unreachable' set will
1134 // have the PREV_MARK_COLLECTING set, but the objects are going to be
1135 // removed so we can skip the expense of clearing the flag.
1136 PyGC_Head* resurrected = unreachable;
1137 deduce_unreachable(resurrected, still_unreachable);
1138 clear_unreachable_mask(still_unreachable);
1139
1140 // Move the resurrected objects to the old generation for future collection.
1141 gc_list_merge(resurrected, old_generation);
1142}
1143
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001144/* This is the main function. Read this to understand how the
1145 * collection process works. */
Neal Norwitz7b216c52006-03-04 20:01:53 +00001146static Py_ssize_t
Victor Stinner67e0de62019-11-20 11:48:18 +01001147collect(PyThreadState *tstate, int generation,
Victor Stinner9db03242019-04-26 02:32:01 +02001148 Py_ssize_t *n_collected, Py_ssize_t *n_uncollectable, int nofail)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001149{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001150 int i;
1151 Py_ssize_t m = 0; /* # objects collected */
1152 Py_ssize_t n = 0; /* # unreachable objects that couldn't be collected */
1153 PyGC_Head *young; /* the generation we are examining */
1154 PyGC_Head *old; /* next older generation */
1155 PyGC_Head unreachable; /* non-problematic unreachable trash */
1156 PyGC_Head finalizers; /* objects with, & reachable from, __del__ */
1157 PyGC_Head *gc;
Victor Stinner7181dec2015-03-27 17:47:53 +01001158 _PyTime_t t1 = 0; /* initialize to prevent a compiler warning */
Victor Stinner72474072019-11-20 12:25:50 +01001159 GCState *gcstate = &tstate->interp->gc;
Antoine Pitrou40f6b122014-05-24 19:21:53 +02001160
Victor Stinner67e0de62019-11-20 11:48:18 +01001161 if (gcstate->debug & DEBUG_STATS) {
Inada Naokibf8162c2019-08-02 16:25:29 +09001162 PySys_WriteStderr("gc: collecting generation %d...\n", generation);
Victor Stinner67e0de62019-11-20 11:48:18 +01001163 show_stats_each_generations(gcstate);
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 Stinner67e0de62019-11-20 11:48:18 +01001172 gcstate->generations[generation+1].count += 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001173 for (i = 0; i <= generation; i++)
Victor Stinner67e0de62019-11-20 11:48:18 +01001174 gcstate->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 Stinner67e0de62019-11-20 11:48:18 +01001178 gc_list_merge(GEN_HEAD(gcstate, i), GEN_HEAD(gcstate, 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 Stinner67e0de62019-11-20 11:48:18 +01001182 young = GEN_HEAD(gcstate, generation);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001183 if (generation < NUM_GENERATIONS-1)
Victor Stinner67e0de62019-11-20 11:48:18 +01001184 old = GEN_HEAD(gcstate, 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 Stinner67e0de62019-11-20 11:48:18 +01001195 gcstate->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 Stinner67e0de62019-11-20 11:48:18 +01001203 gcstate->long_lived_pending = 0;
1204 gcstate->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. */
Victor Stinner67e0de62019-11-20 11:48:18 +01001224 if (gcstate->debug & DEBUG_COLLECTABLE) {
Tim Petersecbf35f2019-10-09 12:37:30 -05001225 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. */
Victor Stinner67e0de62019-11-20 11:48:18 +01001237 finalize_garbage(tstate, &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 Stinner67e0de62019-11-20 11:48:18 +01001250 delete_garbage(tstate, gcstate, &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 Stinner67e0de62019-11-20 11:48:18 +01001256 if (gcstate->debug & DEBUG_UNCOLLECTABLE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001257 debug_cycle("uncollectable", FROM_GC(gc));
1258 }
Victor Stinner67e0de62019-11-20 11:48:18 +01001259 if (gcstate->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 Stinner67e0de62019-11-20 11:48:18 +01001271 handle_legacy_finalizers(tstate, gcstate, &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
Victor Stinner67e0de62019-11-20 11:48:18 +01001300 struct gc_generation_stats *stats = &gcstate->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 Stinner67e0de62019-11-20 11:48:18 +01001317invoke_gc_callback(PyThreadState *tstate, const char *phase,
Victor Stinner9db03242019-04-26 02:32:01 +02001318 int generation, Py_ssize_t collected,
1319 Py_ssize_t uncollectable)
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001320{
Victor Stinner67e0de62019-11-20 11:48:18 +01001321 assert(!_PyErr_Occurred(tstate));
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 Stinner72474072019-11-20 12:25:50 +01001324 GCState *gcstate = &tstate->interp->gc;
Victor Stinner67e0de62019-11-20 11:48:18 +01001325 if (gcstate->callbacks == NULL) {
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001326 return;
Victor Stinner9db03242019-04-26 02:32:01 +02001327 }
1328
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001329 /* The local variable cannot be rebound, check it for sanity */
Victor Stinner67e0de62019-11-20 11:48:18 +01001330 assert(PyList_CheckExact(gcstate->callbacks));
Victor Stinner9db03242019-04-26 02:32:01 +02001331 PyObject *info = NULL;
Victor Stinner67e0de62019-11-20 11:48:18 +01001332 if (PyList_GET_SIZE(gcstate->callbacks) != 0) {
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001333 info = Py_BuildValue("{sisnsn}",
1334 "generation", generation,
1335 "collected", collected,
1336 "uncollectable", uncollectable);
1337 if (info == NULL) {
1338 PyErr_WriteUnraisable(NULL);
1339 return;
1340 }
1341 }
Victor Stinner67e0de62019-11-20 11:48:18 +01001342 for (Py_ssize_t i=0; i<PyList_GET_SIZE(gcstate->callbacks); i++) {
1343 PyObject *r, *cb = PyList_GET_ITEM(gcstate->callbacks, i);
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001344 Py_INCREF(cb); /* make sure cb doesn't go away */
1345 r = PyObject_CallFunction(cb, "sO", phase, info);
Serhiy Storchaka301e3cc2018-05-24 15:19:29 +03001346 if (r == NULL) {
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001347 PyErr_WriteUnraisable(cb);
Serhiy Storchaka301e3cc2018-05-24 15:19:29 +03001348 }
1349 else {
1350 Py_DECREF(r);
1351 }
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001352 Py_DECREF(cb);
1353 }
1354 Py_XDECREF(info);
Victor Stinner67e0de62019-11-20 11:48:18 +01001355 assert(!_PyErr_Occurred(tstate));
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001356}
1357
1358/* Perform garbage collection of a generation and invoke
1359 * progress callbacks.
1360 */
1361static Py_ssize_t
Victor Stinner67e0de62019-11-20 11:48:18 +01001362collect_with_callback(PyThreadState *tstate, int generation)
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001363{
Victor Stinner67e0de62019-11-20 11:48:18 +01001364 assert(!_PyErr_Occurred(tstate));
Victor Stinner9db03242019-04-26 02:32:01 +02001365 Py_ssize_t result, collected, uncollectable;
Victor Stinner67e0de62019-11-20 11:48:18 +01001366 invoke_gc_callback(tstate, "start", generation, 0, 0);
1367 result = collect(tstate, generation, &collected, &uncollectable, 0);
1368 invoke_gc_callback(tstate, "stop", generation, collected, uncollectable);
1369 assert(!_PyErr_Occurred(tstate));
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 Stinner67e0de62019-11-20 11:48:18 +01001374collect_generations(PyThreadState *tstate)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001375{
Victor Stinner72474072019-11-20 12:25:50 +01001376 GCState *gcstate = &tstate->interp->gc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 /* Find the oldest generation (highest numbered) where the count
1378 * exceeds the threshold. Objects in the that generation and
1379 * generations younger than it will be collected. */
Victor Stinner9db03242019-04-26 02:32:01 +02001380 Py_ssize_t n = 0;
1381 for (int i = NUM_GENERATIONS-1; i >= 0; i--) {
Victor Stinner67e0de62019-11-20 11:48:18 +01001382 if (gcstate->generations[i].count > gcstate->generations[i].threshold) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001383 /* Avoid quadratic performance degradation in number
Pablo Galindo90913982019-12-27 21:55:56 +00001384 of tracked objects (see also issue #4074):
1385
1386 To limit the cost of garbage collection, there are two strategies;
1387 - make each collection faster, e.g. by scanning fewer objects
1388 - do less collections
1389 This heuristic is about the latter strategy.
1390
1391 In addition to the various configurable thresholds, we only trigger a
1392 full collection if the ratio
1393
1394 long_lived_pending / long_lived_total
1395
1396 is above a given value (hardwired to 25%).
1397
1398 The reason is that, while "non-full" collections (i.e., collections of
1399 the young and middle generations) will always examine roughly the same
1400 number of objects -- determined by the aforementioned thresholds --,
1401 the cost of a full collection is proportional to the total number of
1402 long-lived objects, which is virtually unbounded.
1403
1404 Indeed, it has been remarked that doing a full collection every
1405 <constant number> of object creations entails a dramatic performance
1406 degradation in workloads which consist in creating and storing lots of
1407 long-lived objects (e.g. building a large list of GC-tracked objects would
1408 show quadratic performance, instead of linear as expected: see issue #4074).
1409
1410 Using the above ratio, instead, yields amortized linear performance in
1411 the total number of objects (the effect of which can be summarized
1412 thusly: "each full garbage collection is more and more costly as the
1413 number of objects grows, but we do fewer and fewer of them").
1414
1415 This heuristic was suggested by Martin von Löwis on python-dev in
1416 June 2008. His original analysis and proposal can be found at:
1417 http://mail.python.org/pipermail/python-dev/2008-June/080579.html
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001418 */
1419 if (i == NUM_GENERATIONS - 1
Victor Stinner67e0de62019-11-20 11:48:18 +01001420 && gcstate->long_lived_pending < gcstate->long_lived_total / 4)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001421 continue;
Victor Stinner67e0de62019-11-20 11:48:18 +01001422 n = collect_with_callback(tstate, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001423 break;
1424 }
1425 }
1426 return n;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001427}
1428
Serhiy Storchaka93260282017-02-04 11:19:59 +02001429#include "clinic/gcmodule.c.h"
1430
1431/*[clinic input]
1432gc.enable
1433
1434Enable automatic garbage collection.
1435[clinic start generated code]*/
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001436
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001437static PyObject *
Serhiy Storchaka93260282017-02-04 11:19:59 +02001438gc_enable_impl(PyObject *module)
1439/*[clinic end generated code: output=45a427e9dce9155c input=81ac4940ca579707]*/
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001440{
Victor Stinner67e0de62019-11-20 11:48:18 +01001441 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner72474072019-11-20 12:25:50 +01001442 GCState *gcstate = &tstate->interp->gc;
Victor Stinner67e0de62019-11-20 11:48:18 +01001443 gcstate->enabled = 1;
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001444 Py_RETURN_NONE;
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001445}
1446
Serhiy Storchaka93260282017-02-04 11:19:59 +02001447/*[clinic input]
1448gc.disable
1449
1450Disable automatic garbage collection.
1451[clinic start generated code]*/
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001452
1453static PyObject *
Serhiy Storchaka93260282017-02-04 11:19:59 +02001454gc_disable_impl(PyObject *module)
1455/*[clinic end generated code: output=97d1030f7aa9d279 input=8c2e5a14e800d83b]*/
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001456{
Victor Stinner67e0de62019-11-20 11:48:18 +01001457 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner72474072019-11-20 12:25:50 +01001458 GCState *gcstate = &tstate->interp->gc;
Victor Stinner67e0de62019-11-20 11:48:18 +01001459 gcstate->enabled = 0;
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001460 Py_RETURN_NONE;
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001461}
1462
Serhiy Storchaka93260282017-02-04 11:19:59 +02001463/*[clinic input]
1464gc.isenabled -> bool
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001465
Serhiy Storchaka93260282017-02-04 11:19:59 +02001466Returns true if automatic garbage collection is enabled.
1467[clinic start generated code]*/
1468
1469static int
1470gc_isenabled_impl(PyObject *module)
1471/*[clinic end generated code: output=1874298331c49130 input=30005e0422373b31]*/
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001472{
Victor Stinner67e0de62019-11-20 11:48:18 +01001473 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner72474072019-11-20 12:25:50 +01001474 GCState *gcstate = &tstate->interp->gc;
Victor Stinner67e0de62019-11-20 11:48:18 +01001475 return gcstate->enabled;
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001476}
1477
Serhiy Storchaka93260282017-02-04 11:19:59 +02001478/*[clinic input]
1479gc.collect -> Py_ssize_t
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001480
Serhiy Storchaka93260282017-02-04 11:19:59 +02001481 generation: int(c_default="NUM_GENERATIONS - 1") = 2
1482
1483Run the garbage collector.
1484
1485With no arguments, run a full collection. The optional argument
1486may be an integer specifying which generation to collect. A ValueError
1487is raised if the generation number is invalid.
1488
1489The number of unreachable objects is returned.
1490[clinic start generated code]*/
1491
1492static Py_ssize_t
1493gc_collect_impl(PyObject *module, int generation)
1494/*[clinic end generated code: output=b697e633043233c7 input=40720128b682d879]*/
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001495{
Victor Stinner2e969062019-11-20 01:49:32 +01001496 PyThreadState *tstate = _PyThreadState_GET();
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001497
Serhiy Storchaka93260282017-02-04 11:19:59 +02001498 if (generation < 0 || generation >= NUM_GENERATIONS) {
Victor Stinner2e969062019-11-20 01:49:32 +01001499 _PyErr_SetString(tstate, PyExc_ValueError, "invalid generation");
Serhiy Storchaka93260282017-02-04 11:19:59 +02001500 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001501 }
Barry Warsawd3c38ff2006-03-07 09:46:03 +00001502
Victor Stinner72474072019-11-20 12:25:50 +01001503 GCState *gcstate = &tstate->interp->gc;
Victor Stinner9db03242019-04-26 02:32:01 +02001504 Py_ssize_t n;
Victor Stinner67e0de62019-11-20 11:48:18 +01001505 if (gcstate->collecting) {
Victor Stinner9db03242019-04-26 02:32:01 +02001506 /* already collecting, don't do anything */
1507 n = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001508 }
Victor Stinner9db03242019-04-26 02:32:01 +02001509 else {
Victor Stinner67e0de62019-11-20 11:48:18 +01001510 gcstate->collecting = 1;
1511 n = collect_with_callback(tstate, generation);
1512 gcstate->collecting = 0;
Victor Stinner9db03242019-04-26 02:32:01 +02001513 }
Serhiy Storchaka93260282017-02-04 11:19:59 +02001514 return n;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001515}
1516
Serhiy Storchaka93260282017-02-04 11:19:59 +02001517/*[clinic input]
1518gc.set_debug
1519
1520 flags: int
1521 An integer that can have the following bits turned on:
1522 DEBUG_STATS - Print statistics during collection.
1523 DEBUG_COLLECTABLE - Print collectable objects found.
1524 DEBUG_UNCOLLECTABLE - Print unreachable but uncollectable objects
1525 found.
1526 DEBUG_SAVEALL - Save objects to gc.garbage rather than freeing them.
1527 DEBUG_LEAK - Debug leaking programs (everything but STATS).
1528 /
1529
1530Set the garbage collection debugging flags.
1531
1532Debugging information is written to sys.stderr.
1533[clinic start generated code]*/
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001534
1535static PyObject *
Serhiy Storchaka93260282017-02-04 11:19:59 +02001536gc_set_debug_impl(PyObject *module, int flags)
1537/*[clinic end generated code: output=7c8366575486b228 input=5e5ce15e84fbed15]*/
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001538{
Victor Stinner67e0de62019-11-20 11:48:18 +01001539 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner72474072019-11-20 12:25:50 +01001540 GCState *gcstate = &tstate->interp->gc;
Victor Stinner67e0de62019-11-20 11:48:18 +01001541 gcstate->debug = flags;
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001542 Py_RETURN_NONE;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001543}
1544
Serhiy Storchaka93260282017-02-04 11:19:59 +02001545/*[clinic input]
1546gc.get_debug -> int
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001547
Serhiy Storchaka93260282017-02-04 11:19:59 +02001548Get the garbage collection debugging flags.
1549[clinic start generated code]*/
1550
1551static int
1552gc_get_debug_impl(PyObject *module)
1553/*[clinic end generated code: output=91242f3506cd1e50 input=91a101e1c3b98366]*/
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001554{
Victor Stinner67e0de62019-11-20 11:48:18 +01001555 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner72474072019-11-20 12:25:50 +01001556 GCState *gcstate = &tstate->interp->gc;
Victor Stinner67e0de62019-11-20 11:48:18 +01001557 return gcstate->debug;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001558}
1559
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001560PyDoc_STRVAR(gc_set_thresh__doc__,
Neal Norwitz2a47c0f2002-01-29 00:53:41 +00001561"set_threshold(threshold0, [threshold1, threshold2]) -> None\n"
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001562"\n"
1563"Sets the collection thresholds. Setting threshold0 to zero disables\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001564"collection.\n");
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001565
1566static PyObject *
Victor Stinner9db03242019-04-26 02:32:01 +02001567gc_set_threshold(PyObject *self, PyObject *args)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001568{
Victor Stinner67e0de62019-11-20 11:48:18 +01001569 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner72474072019-11-20 12:25:50 +01001570 GCState *gcstate = &tstate->interp->gc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001571 if (!PyArg_ParseTuple(args, "i|ii:set_threshold",
Victor Stinner67e0de62019-11-20 11:48:18 +01001572 &gcstate->generations[0].threshold,
1573 &gcstate->generations[1].threshold,
1574 &gcstate->generations[2].threshold))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001575 return NULL;
Victor Stinner9db03242019-04-26 02:32:01 +02001576 for (int i = 3; i < NUM_GENERATIONS; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001577 /* generations higher than 2 get the same threshold */
Victor Stinner67e0de62019-11-20 11:48:18 +01001578 gcstate->generations[i].threshold = gcstate->generations[2].threshold;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001579 }
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02001580 Py_RETURN_NONE;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001581}
1582
Serhiy Storchaka93260282017-02-04 11:19:59 +02001583/*[clinic input]
1584gc.get_threshold
1585
1586Return the current collection thresholds.
1587[clinic start generated code]*/
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001588
1589static PyObject *
Serhiy Storchaka93260282017-02-04 11:19:59 +02001590gc_get_threshold_impl(PyObject *module)
1591/*[clinic end generated code: output=7902bc9f41ecbbd8 input=286d79918034d6e6]*/
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001592{
Victor Stinner67e0de62019-11-20 11:48:18 +01001593 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner72474072019-11-20 12:25:50 +01001594 GCState *gcstate = &tstate->interp->gc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001595 return Py_BuildValue("(iii)",
Victor Stinner67e0de62019-11-20 11:48:18 +01001596 gcstate->generations[0].threshold,
1597 gcstate->generations[1].threshold,
1598 gcstate->generations[2].threshold);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001599}
1600
Serhiy Storchaka93260282017-02-04 11:19:59 +02001601/*[clinic input]
1602gc.get_count
1603
1604Return a three-tuple of the current collection counts.
1605[clinic start generated code]*/
Barry Warsawd3c38ff2006-03-07 09:46:03 +00001606
1607static PyObject *
Serhiy Storchaka93260282017-02-04 11:19:59 +02001608gc_get_count_impl(PyObject *module)
1609/*[clinic end generated code: output=354012e67b16398f input=a392794a08251751]*/
Barry Warsawd3c38ff2006-03-07 09:46:03 +00001610{
Victor Stinner67e0de62019-11-20 11:48:18 +01001611 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner72474072019-11-20 12:25:50 +01001612 GCState *gcstate = &tstate->interp->gc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001613 return Py_BuildValue("(iii)",
Victor Stinner67e0de62019-11-20 11:48:18 +01001614 gcstate->generations[0].count,
1615 gcstate->generations[1].count,
1616 gcstate->generations[2].count);
Barry Warsawd3c38ff2006-03-07 09:46:03 +00001617}
1618
Neil Schemenauer48c70342001-08-09 15:38:31 +00001619static int
Martin v. Löwis560da622001-11-24 09:24:51 +00001620referrersvisit(PyObject* obj, PyObject *objs)
Neil Schemenauer48c70342001-08-09 15:38:31 +00001621{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001622 Py_ssize_t i;
1623 for (i = 0; i < PyTuple_GET_SIZE(objs); i++)
1624 if (PyTuple_GET_ITEM(objs, i) == obj)
1625 return 1;
1626 return 0;
Neil Schemenauer48c70342001-08-09 15:38:31 +00001627}
1628
Neil Schemenauer17e7be62001-08-10 14:46:47 +00001629static int
Martin v. Löwis560da622001-11-24 09:24:51 +00001630gc_referrers_for(PyObject *objs, PyGC_Head *list, PyObject *resultlist)
Neil Schemenauer48c70342001-08-09 15:38:31 +00001631{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001632 PyGC_Head *gc;
1633 PyObject *obj;
1634 traverseproc traverse;
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09001635 for (gc = GC_NEXT(list); gc != list; gc = GC_NEXT(gc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001636 obj = FROM_GC(gc);
1637 traverse = Py_TYPE(obj)->tp_traverse;
1638 if (obj == objs || obj == resultlist)
1639 continue;
1640 if (traverse(obj, (visitproc)referrersvisit, objs)) {
1641 if (PyList_Append(resultlist, obj) < 0)
1642 return 0; /* error */
1643 }
1644 }
1645 return 1; /* no error */
Neil Schemenauer48c70342001-08-09 15:38:31 +00001646}
1647
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001648PyDoc_STRVAR(gc_get_referrers__doc__,
Martin v. Löwis560da622001-11-24 09:24:51 +00001649"get_referrers(*objs) -> list\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001650Return the list of objects that directly refer to any of objs.");
Neil Schemenauer48c70342001-08-09 15:38:31 +00001651
Neil Schemenauer17e7be62001-08-10 14:46:47 +00001652static PyObject *
Martin v. Löwis560da622001-11-24 09:24:51 +00001653gc_get_referrers(PyObject *self, PyObject *args)
Neil Schemenauer48c70342001-08-09 15:38:31 +00001654{
Victor Stinner67e0de62019-11-20 11:48:18 +01001655 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001656 int i;
1657 PyObject *result = PyList_New(0);
Victor Stinner67e0de62019-11-20 11:48:18 +01001658 if (!result) {
1659 return NULL;
1660 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001661
Victor Stinner72474072019-11-20 12:25:50 +01001662 GCState *gcstate = &tstate->interp->gc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001663 for (i = 0; i < NUM_GENERATIONS; i++) {
Victor Stinner67e0de62019-11-20 11:48:18 +01001664 if (!(gc_referrers_for(args, GEN_HEAD(gcstate, i), result))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001665 Py_DECREF(result);
1666 return NULL;
1667 }
1668 }
1669 return result;
Neil Schemenauer48c70342001-08-09 15:38:31 +00001670}
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001671
Tim Peters0f81ab62003-04-08 16:39:48 +00001672/* Append obj to list; return true if error (out of memory), false if OK. */
Jeremy Hylton5bd378b2003-04-03 16:28:38 +00001673static int
Tim Peters730f5532003-04-08 17:17:17 +00001674referentsvisit(PyObject *obj, PyObject *list)
Jeremy Hylton5bd378b2003-04-03 16:28:38 +00001675{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001676 return PyList_Append(list, obj) < 0;
Jeremy Hylton5bd378b2003-04-03 16:28:38 +00001677}
1678
Tim Peters730f5532003-04-08 17:17:17 +00001679PyDoc_STRVAR(gc_get_referents__doc__,
1680"get_referents(*objs) -> list\n\
Jeremy Hylton059b0942003-04-03 16:29:13 +00001681Return the list of objects that are directly referred to by objs.");
Jeremy Hylton5bd378b2003-04-03 16:28:38 +00001682
1683static PyObject *
Tim Peters730f5532003-04-08 17:17:17 +00001684gc_get_referents(PyObject *self, PyObject *args)
Jeremy Hylton5bd378b2003-04-03 16:28:38 +00001685{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001686 Py_ssize_t i;
1687 PyObject *result = PyList_New(0);
Tim Peters0f81ab62003-04-08 16:39:48 +00001688
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001689 if (result == NULL)
1690 return NULL;
Tim Peters0f81ab62003-04-08 16:39:48 +00001691
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001692 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
1693 traverseproc traverse;
1694 PyObject *obj = PyTuple_GET_ITEM(args, i);
Tim Peters0f81ab62003-04-08 16:39:48 +00001695
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001696 if (! PyObject_IS_GC(obj))
1697 continue;
1698 traverse = Py_TYPE(obj)->tp_traverse;
1699 if (! traverse)
1700 continue;
1701 if (traverse(obj, (visitproc)referentsvisit, result)) {
1702 Py_DECREF(result);
1703 return NULL;
1704 }
1705 }
1706 return result;
Jeremy Hylton5bd378b2003-04-03 16:28:38 +00001707}
1708
Serhiy Storchaka93260282017-02-04 11:19:59 +02001709/*[clinic input]
1710gc.get_objects
Pablo Galindo175421b2019-02-23 03:02:06 +00001711 generation: Py_ssize_t(accept={int, NoneType}, c_default="-1") = None
1712 Generation to extract the objects from.
Serhiy Storchaka93260282017-02-04 11:19:59 +02001713
1714Return a list of objects tracked by the collector (excluding the list returned).
Pablo Galindo175421b2019-02-23 03:02:06 +00001715
1716If generation is not None, return only the objects tracked by the collector
1717that are in that generation.
Serhiy Storchaka93260282017-02-04 11:19:59 +02001718[clinic start generated code]*/
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +00001719
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +00001720static PyObject *
Pablo Galindo175421b2019-02-23 03:02:06 +00001721gc_get_objects_impl(PyObject *module, Py_ssize_t generation)
1722/*[clinic end generated code: output=48b35fea4ba6cb0e input=ef7da9df9806754c]*/
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +00001723{
Victor Stinner67e0de62019-11-20 11:48:18 +01001724 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001725 int i;
1726 PyObject* result;
Victor Stinner72474072019-11-20 12:25:50 +01001727 GCState *gcstate = &tstate->interp->gc;
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +00001728
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001729 result = PyList_New(0);
Pablo Galindo175421b2019-02-23 03:02:06 +00001730 if (result == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001731 return NULL;
Pablo Galindo175421b2019-02-23 03:02:06 +00001732 }
1733
1734 /* If generation is passed, we extract only that generation */
Victor Stinner0810fa72019-04-15 17:54:09 +02001735 if (generation != -1) {
Pablo Galindo175421b2019-02-23 03:02:06 +00001736 if (generation >= NUM_GENERATIONS) {
Victor Stinner67e0de62019-11-20 11:48:18 +01001737 _PyErr_Format(tstate, PyExc_ValueError,
1738 "generation parameter must be less than the number of "
1739 "available generations (%i)",
1740 NUM_GENERATIONS);
Pablo Galindo175421b2019-02-23 03:02:06 +00001741 goto error;
1742 }
1743
1744 if (generation < 0) {
Victor Stinner67e0de62019-11-20 11:48:18 +01001745 _PyErr_SetString(tstate, PyExc_ValueError,
1746 "generation parameter cannot be negative");
Pablo Galindo175421b2019-02-23 03:02:06 +00001747 goto error;
1748 }
1749
Victor Stinner67e0de62019-11-20 11:48:18 +01001750 if (append_objects(result, GEN_HEAD(gcstate, generation))) {
Pablo Galindo175421b2019-02-23 03:02:06 +00001751 goto error;
1752 }
1753
1754 return result;
1755 }
1756
1757 /* If generation is not passed or None, get all objects from all generations */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001758 for (i = 0; i < NUM_GENERATIONS; i++) {
Victor Stinner67e0de62019-11-20 11:48:18 +01001759 if (append_objects(result, GEN_HEAD(gcstate, i))) {
Pablo Galindo175421b2019-02-23 03:02:06 +00001760 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001761 }
1762 }
1763 return result;
Pablo Galindo175421b2019-02-23 03:02:06 +00001764
1765error:
1766 Py_DECREF(result);
1767 return NULL;
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +00001768}
1769
Serhiy Storchaka93260282017-02-04 11:19:59 +02001770/*[clinic input]
1771gc.get_stats
1772
1773Return a list of dictionaries containing per-generation statistics.
1774[clinic start generated code]*/
Antoine Pitroud4156c12012-10-30 22:43:19 +01001775
1776static PyObject *
Serhiy Storchaka93260282017-02-04 11:19:59 +02001777gc_get_stats_impl(PyObject *module)
1778/*[clinic end generated code: output=a8ab1d8a5d26f3ab input=1ef4ed9d17b1a470]*/
Antoine Pitroud4156c12012-10-30 22:43:19 +01001779{
1780 int i;
Antoine Pitroud4156c12012-10-30 22:43:19 +01001781 struct gc_generation_stats stats[NUM_GENERATIONS], *st;
Victor Stinner67e0de62019-11-20 11:48:18 +01001782 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitroud4156c12012-10-30 22:43:19 +01001783
1784 /* To get consistent values despite allocations while constructing
1785 the result list, we use a snapshot of the running stats. */
Victor Stinner72474072019-11-20 12:25:50 +01001786 GCState *gcstate = &tstate->interp->gc;
Antoine Pitroud4156c12012-10-30 22:43:19 +01001787 for (i = 0; i < NUM_GENERATIONS; i++) {
Victor Stinner67e0de62019-11-20 11:48:18 +01001788 stats[i] = gcstate->generation_stats[i];
Antoine Pitroud4156c12012-10-30 22:43:19 +01001789 }
1790
Victor Stinner9db03242019-04-26 02:32:01 +02001791 PyObject *result = PyList_New(0);
Antoine Pitroud4156c12012-10-30 22:43:19 +01001792 if (result == NULL)
1793 return NULL;
1794
1795 for (i = 0; i < NUM_GENERATIONS; i++) {
1796 PyObject *dict;
1797 st = &stats[i];
1798 dict = Py_BuildValue("{snsnsn}",
1799 "collections", st->collections,
1800 "collected", st->collected,
1801 "uncollectable", st->uncollectable
1802 );
1803 if (dict == NULL)
1804 goto error;
1805 if (PyList_Append(result, dict)) {
1806 Py_DECREF(dict);
1807 goto error;
1808 }
1809 Py_DECREF(dict);
1810 }
1811 return result;
1812
1813error:
1814 Py_XDECREF(result);
1815 return NULL;
1816}
1817
1818
Serhiy Storchaka93260282017-02-04 11:19:59 +02001819/*[clinic input]
1820gc.is_tracked
1821
1822 obj: object
1823 /
1824
1825Returns true if the object is tracked by the garbage collector.
1826
1827Simple atomic objects will return false.
1828[clinic start generated code]*/
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001829
1830static PyObject *
Serhiy Storchaka93260282017-02-04 11:19:59 +02001831gc_is_tracked(PyObject *module, PyObject *obj)
1832/*[clinic end generated code: output=14f0103423b28e31 input=d83057f170ea2723]*/
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001833{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001834 PyObject *result;
1835
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09001836 if (PyObject_IS_GC(obj) && _PyObject_GC_IS_TRACKED(obj))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001837 result = Py_True;
1838 else
1839 result = Py_False;
1840 Py_INCREF(result);
1841 return result;
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001842}
1843
brainfvckc75edab2017-10-16 12:49:41 -07001844/*[clinic input]
1845gc.freeze
1846
1847Freeze all current tracked objects and ignore them for future collections.
1848
1849This can be used before a POSIX fork() call to make the gc copy-on-write friendly.
1850Note: collection before a POSIX fork() call may free pages for future allocation
1851which can cause copy-on-write.
1852[clinic start generated code]*/
1853
1854static PyObject *
1855gc_freeze_impl(PyObject *module)
1856/*[clinic end generated code: output=502159d9cdc4c139 input=b602b16ac5febbe5]*/
1857{
Victor Stinner67e0de62019-11-20 11:48:18 +01001858 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner72474072019-11-20 12:25:50 +01001859 GCState *gcstate = &tstate->interp->gc;
brainfvckc75edab2017-10-16 12:49:41 -07001860 for (int i = 0; i < NUM_GENERATIONS; ++i) {
Victor Stinner67e0de62019-11-20 11:48:18 +01001861 gc_list_merge(GEN_HEAD(gcstate, i), &gcstate->permanent_generation.head);
1862 gcstate->generations[i].count = 0;
brainfvckc75edab2017-10-16 12:49:41 -07001863 }
1864 Py_RETURN_NONE;
1865}
1866
1867/*[clinic input]
1868gc.unfreeze
1869
1870Unfreeze all objects in the permanent generation.
1871
1872Put all objects in the permanent generation back into oldest generation.
1873[clinic start generated code]*/
1874
1875static PyObject *
1876gc_unfreeze_impl(PyObject *module)
1877/*[clinic end generated code: output=1c15f2043b25e169 input=2dd52b170f4cef6c]*/
1878{
Victor Stinner67e0de62019-11-20 11:48:18 +01001879 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner72474072019-11-20 12:25:50 +01001880 GCState *gcstate = &tstate->interp->gc;
Victor Stinner67e0de62019-11-20 11:48:18 +01001881 gc_list_merge(&gcstate->permanent_generation.head,
1882 GEN_HEAD(gcstate, NUM_GENERATIONS-1));
brainfvckc75edab2017-10-16 12:49:41 -07001883 Py_RETURN_NONE;
1884}
1885
1886/*[clinic input]
Victor Stinner05d68a82018-01-18 11:15:25 +01001887gc.get_freeze_count -> Py_ssize_t
brainfvckc75edab2017-10-16 12:49:41 -07001888
1889Return the number of objects in the permanent generation.
1890[clinic start generated code]*/
1891
Victor Stinner05d68a82018-01-18 11:15:25 +01001892static Py_ssize_t
brainfvckc75edab2017-10-16 12:49:41 -07001893gc_get_freeze_count_impl(PyObject *module)
Victor Stinner05d68a82018-01-18 11:15:25 +01001894/*[clinic end generated code: output=61cbd9f43aa032e1 input=45ffbc65cfe2a6ed]*/
brainfvckc75edab2017-10-16 12:49:41 -07001895{
Victor Stinner67e0de62019-11-20 11:48:18 +01001896 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner72474072019-11-20 12:25:50 +01001897 GCState *gcstate = &tstate->interp->gc;
Victor Stinner67e0de62019-11-20 11:48:18 +01001898 return gc_list_size(&gcstate->permanent_generation.head);
brainfvckc75edab2017-10-16 12:49:41 -07001899}
1900
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +00001901
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001902PyDoc_STRVAR(gc__doc__,
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001903"This module provides access to the garbage collector for reference cycles.\n"
1904"\n"
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +00001905"enable() -- Enable automatic garbage collection.\n"
1906"disable() -- Disable automatic garbage collection.\n"
1907"isenabled() -- Returns true if automatic collection is enabled.\n"
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001908"collect() -- Do a full collection right now.\n"
Thomas Wouters89f507f2006-12-13 04:49:30 +00001909"get_count() -- Return the current collection counts.\n"
R David Murray0e814632013-12-26 15:11:28 -05001910"get_stats() -- Return list of dictionaries containing per-generation stats.\n"
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001911"set_debug() -- Set debugging flags.\n"
1912"get_debug() -- Get debugging flags.\n"
1913"set_threshold() -- Set the collection thresholds.\n"
1914"get_threshold() -- Return the current the collection thresholds.\n"
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +00001915"get_objects() -- Return a list of all objects tracked by the collector.\n"
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001916"is_tracked() -- Returns true if a given object is tracked.\n"
Jeremy Hylton5bd378b2003-04-03 16:28:38 +00001917"get_referrers() -- Return the list of objects that refer to an object.\n"
brainfvckc75edab2017-10-16 12:49:41 -07001918"get_referents() -- Return the list of objects that an object refers to.\n"
1919"freeze() -- Freeze all tracked objects and ignore them for future collections.\n"
1920"unfreeze() -- Unfreeze all objects in the permanent generation.\n"
1921"get_freeze_count() -- Return the number of objects in the permanent generation.\n");
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001922
1923static PyMethodDef GcMethods[] = {
Serhiy Storchaka93260282017-02-04 11:19:59 +02001924 GC_ENABLE_METHODDEF
1925 GC_DISABLE_METHODDEF
1926 GC_ISENABLED_METHODDEF
1927 GC_SET_DEBUG_METHODDEF
1928 GC_GET_DEBUG_METHODDEF
1929 GC_GET_COUNT_METHODDEF
Victor Stinner9db03242019-04-26 02:32:01 +02001930 {"set_threshold", gc_set_threshold, METH_VARARGS, gc_set_thresh__doc__},
Serhiy Storchaka93260282017-02-04 11:19:59 +02001931 GC_GET_THRESHOLD_METHODDEF
1932 GC_COLLECT_METHODDEF
1933 GC_GET_OBJECTS_METHODDEF
1934 GC_GET_STATS_METHODDEF
1935 GC_IS_TRACKED_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001936 {"get_referrers", gc_get_referrers, METH_VARARGS,
1937 gc_get_referrers__doc__},
1938 {"get_referents", gc_get_referents, METH_VARARGS,
1939 gc_get_referents__doc__},
brainfvckc75edab2017-10-16 12:49:41 -07001940 GC_FREEZE_METHODDEF
1941 GC_UNFREEZE_METHODDEF
1942 GC_GET_FREEZE_COUNT_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001943 {NULL, NULL} /* Sentinel */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001944};
1945
Martin v. Löwis1a214512008-06-11 05:26:20 +00001946static struct PyModuleDef gcmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001947 PyModuleDef_HEAD_INIT,
Antoine Pitrou696e0352010-08-08 22:18:46 +00001948 "gc", /* m_name */
1949 gc__doc__, /* m_doc */
1950 -1, /* m_size */
1951 GcMethods, /* m_methods */
1952 NULL, /* m_reload */
1953 NULL, /* m_traverse */
1954 NULL, /* m_clear */
1955 NULL /* m_free */
Martin v. Löwis1a214512008-06-11 05:26:20 +00001956};
1957
Jason Tishler6bc06ec2003-09-04 11:59:50 +00001958PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001959PyInit_gc(void)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001960{
Victor Stinner72474072019-11-20 12:25:50 +01001961 PyThreadState *tstate = _PyThreadState_GET();
1962 GCState *gcstate = &tstate->interp->gc;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001963
Victor Stinner72474072019-11-20 12:25:50 +01001964 PyObject *m = PyModule_Create(&gcmodule);
Martin v. Löwis1a214512008-06-11 05:26:20 +00001965
Victor Stinner9db03242019-04-26 02:32:01 +02001966 if (m == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001967 return NULL;
Victor Stinner9db03242019-04-26 02:32:01 +02001968 }
Tim Peters11558872003-04-06 23:30:52 +00001969
Victor Stinner67e0de62019-11-20 11:48:18 +01001970 if (gcstate->garbage == NULL) {
1971 gcstate->garbage = PyList_New(0);
Victor Stinner72474072019-11-20 12:25:50 +01001972 if (gcstate->garbage == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001973 return NULL;
Victor Stinner72474072019-11-20 12:25:50 +01001974 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001975 }
Victor Stinner67e0de62019-11-20 11:48:18 +01001976 Py_INCREF(gcstate->garbage);
Victor Stinner72474072019-11-20 12:25:50 +01001977 if (PyModule_AddObject(m, "garbage", gcstate->garbage) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001978 return NULL;
Victor Stinner72474072019-11-20 12:25:50 +01001979 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001980
Victor Stinner67e0de62019-11-20 11:48:18 +01001981 if (gcstate->callbacks == NULL) {
1982 gcstate->callbacks = PyList_New(0);
Victor Stinner72474072019-11-20 12:25:50 +01001983 if (gcstate->callbacks == NULL) {
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001984 return NULL;
Victor Stinner72474072019-11-20 12:25:50 +01001985 }
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001986 }
Victor Stinner67e0de62019-11-20 11:48:18 +01001987 Py_INCREF(gcstate->callbacks);
Victor Stinner72474072019-11-20 12:25:50 +01001988 if (PyModule_AddObject(m, "callbacks", gcstate->callbacks) < 0) {
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001989 return NULL;
Victor Stinner72474072019-11-20 12:25:50 +01001990 }
Kristján Valur Jónsson69c63522012-04-15 11:41:32 +00001991
Victor Stinner72474072019-11-20 12:25:50 +01001992#define ADD_INT(NAME) if (PyModule_AddIntConstant(m, #NAME, NAME) < 0) { return NULL; }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001993 ADD_INT(DEBUG_STATS);
1994 ADD_INT(DEBUG_COLLECTABLE);
1995 ADD_INT(DEBUG_UNCOLLECTABLE);
1996 ADD_INT(DEBUG_SAVEALL);
1997 ADD_INT(DEBUG_LEAK);
Tim Peters11558872003-04-06 23:30:52 +00001998#undef ADD_INT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001999 return m;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00002000}
2001
Guido van Rossume13ddc92003-04-17 17:29:22 +00002002/* API to invoke gc.collect() from C */
Neal Norwitz7b216c52006-03-04 20:01:53 +00002003Py_ssize_t
Guido van Rossume13ddc92003-04-17 17:29:22 +00002004PyGC_Collect(void)
2005{
Victor Stinner2e969062019-11-20 01:49:32 +01002006 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner72474072019-11-20 12:25:50 +01002007 GCState *gcstate = &tstate->interp->gc;
Victor Stinner2e969062019-11-20 01:49:32 +01002008
Victor Stinner67e0de62019-11-20 11:48:18 +01002009 if (!gcstate->enabled) {
Victor Stinner9db03242019-04-26 02:32:01 +02002010 return 0;
2011 }
Guido van Rossume13ddc92003-04-17 17:29:22 +00002012
Victor Stinner9db03242019-04-26 02:32:01 +02002013 Py_ssize_t n;
Victor Stinner67e0de62019-11-20 11:48:18 +01002014 if (gcstate->collecting) {
Victor Stinner9db03242019-04-26 02:32:01 +02002015 /* already collecting, don't do anything */
2016 n = 0;
2017 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002018 else {
Serhiy Storchaka301e3cc2018-05-24 15:19:29 +03002019 PyObject *exc, *value, *tb;
Victor Stinner67e0de62019-11-20 11:48:18 +01002020 gcstate->collecting = 1;
Victor Stinner2e969062019-11-20 01:49:32 +01002021 _PyErr_Fetch(tstate, &exc, &value, &tb);
Victor Stinner67e0de62019-11-20 11:48:18 +01002022 n = collect_with_callback(tstate, NUM_GENERATIONS - 1);
Victor Stinner2e969062019-11-20 01:49:32 +01002023 _PyErr_Restore(tstate, exc, value, tb);
Victor Stinner67e0de62019-11-20 11:48:18 +01002024 gcstate->collecting = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002025 }
Guido van Rossume13ddc92003-04-17 17:29:22 +00002026
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002027 return n;
Guido van Rossume13ddc92003-04-17 17:29:22 +00002028}
2029
Antoine Pitroufef34e32013-05-19 01:11:58 +02002030Py_ssize_t
Łukasz Langafef7e942016-09-09 21:47:46 -07002031_PyGC_CollectIfEnabled(void)
2032{
Łukasz Langafef7e942016-09-09 21:47:46 -07002033 return PyGC_Collect();
2034}
2035
2036Py_ssize_t
Antoine Pitroufef34e32013-05-19 01:11:58 +02002037_PyGC_CollectNoFail(void)
2038{
Victor Stinner2e969062019-11-20 01:49:32 +01002039 PyThreadState *tstate = _PyThreadState_GET();
2040 assert(!_PyErr_Occurred(tstate));
Victor Stinner9db03242019-04-26 02:32:01 +02002041
Victor Stinner72474072019-11-20 12:25:50 +01002042 GCState *gcstate = &tstate->interp->gc;
Antoine Pitroufef34e32013-05-19 01:11:58 +02002043 Py_ssize_t n;
2044
Antoine Pitrouc69c9bc2013-08-15 20:15:15 +02002045 /* Ideally, this function is only called on interpreter shutdown,
2046 and therefore not recursively. Unfortunately, when there are daemon
2047 threads, a daemon thread can start a cyclic garbage collection
2048 during interpreter shutdown (and then never finish it).
2049 See http://bugs.python.org/issue8713#msg195178 for an example.
2050 */
Victor Stinner67e0de62019-11-20 11:48:18 +01002051 if (gcstate->collecting) {
Antoine Pitrouc69c9bc2013-08-15 20:15:15 +02002052 n = 0;
Victor Stinner9db03242019-04-26 02:32:01 +02002053 }
Antoine Pitrouc69c9bc2013-08-15 20:15:15 +02002054 else {
Victor Stinner67e0de62019-11-20 11:48:18 +01002055 gcstate->collecting = 1;
2056 n = collect(tstate, NUM_GENERATIONS - 1, NULL, NULL, 1);
2057 gcstate->collecting = 0;
Antoine Pitrouc69c9bc2013-08-15 20:15:15 +02002058 }
Antoine Pitroufef34e32013-05-19 01:11:58 +02002059 return n;
2060}
Antoine Pitrou5f454a02013-05-06 21:15:57 +02002061
Antoine Pitrou696e0352010-08-08 22:18:46 +00002062void
Victor Stinner67e0de62019-11-20 11:48:18 +01002063_PyGC_DumpShutdownStats(PyThreadState *tstate)
Antoine Pitrou696e0352010-08-08 22:18:46 +00002064{
Victor Stinner72474072019-11-20 12:25:50 +01002065 GCState *gcstate = &tstate->interp->gc;
Victor Stinner67e0de62019-11-20 11:48:18 +01002066 if (!(gcstate->debug & DEBUG_SAVEALL)
2067 && gcstate->garbage != NULL && PyList_GET_SIZE(gcstate->garbage) > 0) {
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02002068 const char *message;
Victor Stinner67e0de62019-11-20 11:48:18 +01002069 if (gcstate->debug & DEBUG_UNCOLLECTABLE)
Antoine Pitroub5d82042010-11-05 00:05:25 +00002070 message = "gc: %zd uncollectable objects at " \
Georg Brandl08be72d2010-10-24 15:11:22 +00002071 "shutdown";
2072 else
Antoine Pitroub5d82042010-11-05 00:05:25 +00002073 message = "gc: %zd uncollectable objects at " \
Georg Brandl08be72d2010-10-24 15:11:22 +00002074 "shutdown; use gc.set_debug(gc.DEBUG_UNCOLLECTABLE) to list them";
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02002075 /* PyErr_WarnFormat does too many things and we are at shutdown,
2076 the warnings module's dependencies (e.g. linecache) may be gone
2077 already. */
2078 if (PyErr_WarnExplicitFormat(PyExc_ResourceWarning, "gc", 0,
2079 "gc", NULL, message,
Victor Stinner67e0de62019-11-20 11:48:18 +01002080 PyList_GET_SIZE(gcstate->garbage)))
Georg Brandl08be72d2010-10-24 15:11:22 +00002081 PyErr_WriteUnraisable(NULL);
Victor Stinner67e0de62019-11-20 11:48:18 +01002082 if (gcstate->debug & DEBUG_UNCOLLECTABLE) {
Antoine Pitrou696e0352010-08-08 22:18:46 +00002083 PyObject *repr = NULL, *bytes = NULL;
Victor Stinner67e0de62019-11-20 11:48:18 +01002084 repr = PyObject_Repr(gcstate->garbage);
Antoine Pitrou696e0352010-08-08 22:18:46 +00002085 if (!repr || !(bytes = PyUnicode_EncodeFSDefault(repr)))
Victor Stinner67e0de62019-11-20 11:48:18 +01002086 PyErr_WriteUnraisable(gcstate->garbage);
Antoine Pitrou696e0352010-08-08 22:18:46 +00002087 else {
2088 PySys_WriteStderr(
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02002089 " %s\n",
Antoine Pitrou696e0352010-08-08 22:18:46 +00002090 PyBytes_AS_STRING(bytes)
2091 );
2092 }
2093 Py_XDECREF(repr);
2094 Py_XDECREF(bytes);
2095 }
Antoine Pitrou696e0352010-08-08 22:18:46 +00002096 }
Antoine Pitrou5f454a02013-05-06 21:15:57 +02002097}
2098
2099void
Victor Stinner7eee5be2019-11-20 10:38:34 +01002100_PyGC_Fini(PyThreadState *tstate)
Antoine Pitrou5f454a02013-05-06 21:15:57 +02002101{
Victor Stinner72474072019-11-20 12:25:50 +01002102 GCState *gcstate = &tstate->interp->gc;
Victor Stinner67e0de62019-11-20 11:48:18 +01002103 Py_CLEAR(gcstate->garbage);
2104 Py_CLEAR(gcstate->callbacks);
Antoine Pitrou696e0352010-08-08 22:18:46 +00002105}
2106
Neil Schemenauer43411b52001-08-30 00:05:51 +00002107/* for debugging */
Guido van Rossume13ddc92003-04-17 17:29:22 +00002108void
2109_PyGC_Dump(PyGC_Head *g)
Neil Schemenauer43411b52001-08-30 00:05:51 +00002110{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002111 _PyObject_Dump(FROM_GC(g));
Neil Schemenauer43411b52001-08-30 00:05:51 +00002112}
2113
Victor Stinnera5447732019-10-10 09:32:13 +02002114
2115#ifdef Py_DEBUG
Victor Stinner1b184552019-10-08 00:09:31 +02002116static int
2117visit_validate(PyObject *op, void *parent_raw)
2118{
2119 PyObject *parent = _PyObject_CAST(parent_raw);
2120 if (_PyObject_IsFreed(op)) {
2121 _PyObject_ASSERT_FAILED_MSG(parent,
2122 "PyObject_GC_Track() object is not valid");
2123 }
2124 return 0;
2125}
Victor Stinnera5447732019-10-10 09:32:13 +02002126#endif
Victor Stinner1b184552019-10-08 00:09:31 +02002127
2128
Neil Schemenauer43411b52001-08-30 00:05:51 +00002129/* extension modules might be compiled with GC support so these
2130 functions must always be available */
2131
2132void
Victor Stinnera42de742018-11-22 10:25:22 +01002133PyObject_GC_Track(void *op_raw)
Neil Schemenauer43411b52001-08-30 00:05:51 +00002134{
Victor Stinnera42de742018-11-22 10:25:22 +01002135 PyObject *op = _PyObject_CAST(op_raw);
Victor Stinner271753a2018-11-22 01:02:54 +01002136 if (_PyObject_GC_IS_TRACKED(op)) {
2137 _PyObject_ASSERT_FAILED_MSG(op,
2138 "object already tracked "
2139 "by the garbage collector");
2140 }
Victor Stinnera42de742018-11-22 10:25:22 +01002141 _PyObject_GC_TRACK(op);
Victor Stinner1b184552019-10-08 00:09:31 +02002142
2143#ifdef Py_DEBUG
2144 /* Check that the object is valid: validate objects traversed
2145 by tp_traverse() */
2146 traverseproc traverse = Py_TYPE(op)->tp_traverse;
2147 (void)traverse(op, visit_validate, op);
2148#endif
Neil Schemenauer43411b52001-08-30 00:05:51 +00002149}
2150
Neil Schemenauerfec4eb12002-04-12 02:41:03 +00002151void
Victor Stinnera42de742018-11-22 10:25:22 +01002152PyObject_GC_UnTrack(void *op_raw)
Neil Schemenauer43411b52001-08-30 00:05:51 +00002153{
Victor Stinnera42de742018-11-22 10:25:22 +01002154 PyObject *op = _PyObject_CAST(op_raw);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002155 /* Obscure: the Py_TRASHCAN mechanism requires that we be able to
2156 * call PyObject_GC_UnTrack twice on an object.
2157 */
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002158 if (_PyObject_GC_IS_TRACKED(op)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002159 _PyObject_GC_UNTRACK(op);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002160 }
Neil Schemenauer43411b52001-08-30 00:05:51 +00002161}
2162
Victor Stinnerdb067af2014-05-02 22:31:14 +02002163static PyObject *
2164_PyObject_GC_Alloc(int use_calloc, size_t basicsize)
Neil Schemenauer43411b52001-08-30 00:05:51 +00002165{
Victor Stinner67e0de62019-11-20 11:48:18 +01002166 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner72474072019-11-20 12:25:50 +01002167 GCState *gcstate = &tstate->interp->gc;
Victor Stinner2e969062019-11-20 01:49:32 +01002168 if (basicsize > PY_SSIZE_T_MAX - sizeof(PyGC_Head)) {
Victor Stinner67e0de62019-11-20 11:48:18 +01002169 return _PyErr_NoMemory(tstate);
Victor Stinner2e969062019-11-20 01:49:32 +01002170 }
2171 size_t size = sizeof(PyGC_Head) + basicsize;
2172
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002173 PyGC_Head *g;
Victor Stinner2e969062019-11-20 01:49:32 +01002174 if (use_calloc) {
Victor Stinnerdb067af2014-05-02 22:31:14 +02002175 g = (PyGC_Head *)PyObject_Calloc(1, size);
Victor Stinner2e969062019-11-20 01:49:32 +01002176 }
2177 else {
Victor Stinnerdb067af2014-05-02 22:31:14 +02002178 g = (PyGC_Head *)PyObject_Malloc(size);
Victor Stinner2e969062019-11-20 01:49:32 +01002179 }
2180 if (g == NULL) {
Victor Stinner67e0de62019-11-20 11:48:18 +01002181 return _PyErr_NoMemory(tstate);
Victor Stinner2e969062019-11-20 01:49:32 +01002182 }
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002183 assert(((uintptr_t)g & 3) == 0); // g must be aligned 4bytes boundary
Victor Stinner2e969062019-11-20 01:49:32 +01002184
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002185 g->_gc_next = 0;
2186 g->_gc_prev = 0;
Victor Stinner67e0de62019-11-20 11:48:18 +01002187 gcstate->generations[0].count++; /* number of allocated GC objects */
2188 if (gcstate->generations[0].count > gcstate->generations[0].threshold &&
2189 gcstate->enabled &&
2190 gcstate->generations[0].threshold &&
2191 !gcstate->collecting &&
2192 !_PyErr_Occurred(tstate))
Victor Stinner2e969062019-11-20 01:49:32 +01002193 {
Victor Stinner67e0de62019-11-20 11:48:18 +01002194 gcstate->collecting = 1;
2195 collect_generations(tstate);
2196 gcstate->collecting = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002197 }
Victor Stinner2e969062019-11-20 01:49:32 +01002198 PyObject *op = FROM_GC(g);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002199 return op;
Neil Schemenauer43411b52001-08-30 00:05:51 +00002200}
2201
2202PyObject *
Victor Stinnerdb067af2014-05-02 22:31:14 +02002203_PyObject_GC_Malloc(size_t basicsize)
2204{
2205 return _PyObject_GC_Alloc(0, basicsize);
2206}
2207
2208PyObject *
2209_PyObject_GC_Calloc(size_t basicsize)
2210{
2211 return _PyObject_GC_Alloc(1, basicsize);
2212}
2213
2214PyObject *
Neil Schemenauer43411b52001-08-30 00:05:51 +00002215_PyObject_GC_New(PyTypeObject *tp)
2216{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002217 PyObject *op = _PyObject_GC_Malloc(_PyObject_SIZE(tp));
2218 if (op != NULL)
2219 op = PyObject_INIT(op, tp);
2220 return op;
Neil Schemenauer43411b52001-08-30 00:05:51 +00002221}
2222
2223PyVarObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00002224_PyObject_GC_NewVar(PyTypeObject *tp, Py_ssize_t nitems)
Neil Schemenauer43411b52001-08-30 00:05:51 +00002225{
Victor Stinner5d1866c2013-07-08 22:17:52 +02002226 size_t size;
2227 PyVarObject *op;
2228
2229 if (nitems < 0) {
2230 PyErr_BadInternalCall();
2231 return NULL;
2232 }
2233 size = _PyObject_VAR_SIZE(tp, nitems);
2234 op = (PyVarObject *) _PyObject_GC_Malloc(size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002235 if (op != NULL)
2236 op = PyObject_INIT_VAR(op, tp, nitems);
2237 return op;
Neil Schemenauer43411b52001-08-30 00:05:51 +00002238}
2239
2240PyVarObject *
Martin v. Löwis41290682006-02-16 14:56:14 +00002241_PyObject_GC_Resize(PyVarObject *op, Py_ssize_t nitems)
Neil Schemenauer43411b52001-08-30 00:05:51 +00002242{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002243 const size_t basicsize = _PyObject_VAR_SIZE(Py_TYPE(op), nitems);
Victor Stinnera4b2bc72018-10-26 18:00:13 +02002244 _PyObject_ASSERT((PyObject *)op, !_PyObject_GC_IS_TRACKED(op));
2245 if (basicsize > PY_SSIZE_T_MAX - sizeof(PyGC_Head)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002246 return (PyVarObject *)PyErr_NoMemory();
Victor Stinnera4b2bc72018-10-26 18:00:13 +02002247 }
2248
2249 PyGC_Head *g = AS_GC(op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002250 g = (PyGC_Head *)PyObject_REALLOC(g, sizeof(PyGC_Head) + basicsize);
2251 if (g == NULL)
2252 return (PyVarObject *)PyErr_NoMemory();
2253 op = (PyVarObject *) FROM_GC(g);
2254 Py_SIZE(op) = nitems;
2255 return op;
Neil Schemenauer43411b52001-08-30 00:05:51 +00002256}
2257
2258void
Neil Schemenauerfec4eb12002-04-12 02:41:03 +00002259PyObject_GC_Del(void *op)
Neil Schemenauer43411b52001-08-30 00:05:51 +00002260{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002261 PyGC_Head *g = AS_GC(op);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002262 if (_PyObject_GC_IS_TRACKED(op)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002263 gc_list_remove(g);
INADA Naoki5ac9e6e2018-07-10 17:19:53 +09002264 }
Victor Stinner67e0de62019-11-20 11:48:18 +01002265 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner72474072019-11-20 12:25:50 +01002266 GCState *gcstate = &tstate->interp->gc;
Victor Stinner67e0de62019-11-20 11:48:18 +01002267 if (gcstate->generations[0].count > 0) {
2268 gcstate->generations[0].count--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002269 }
2270 PyObject_FREE(g);
Neil Schemenauer43411b52001-08-30 00:05:51 +00002271}
Tim Petersd9d39932019-11-02 12:06:31 -05002272
2273/* ------------------------------------------------------------------------
2274Notes
2275
Pablo Galindob028f582019-11-19 01:36:57 +00002276[REACHABLE OR UNREACHABLE?]
Tim Petersd9d39932019-11-02 12:06:31 -05002277
2278It "sounds slick" to move the unreachable objects, until you think about
2279it - the reason it pays isn't actually obvious.
2280
2281Suppose we create objects A, B, C in that order. They appear in the young
2282generation in the same order. If B points to A, and C to B, and C is
2283reachable from outside, then the adjusted refcounts will be 0, 0, and 1
2284respectively.
2285
2286When move_unreachable finds A, A is moved to the unreachable list. The
2287same for B when it's first encountered. Then C is traversed, B is moved
2288_back_ to the reachable list. B is eventually traversed, and then A is
2289moved back to the reachable list.
2290
2291So instead of not moving at all, the reachable objects B and A are moved
2292twice each. Why is this a win? A straightforward algorithm to move the
2293reachable objects instead would move A, B, and C once each.
2294
2295The key is that this dance leaves the objects in order C, B, A - it's
2296reversed from the original order. On all _subsequent_ scans, none of
2297them will move. Since most objects aren't in cycles, this can save an
2298unbounded number of moves across an unbounded number of later collections.
2299It can cost more only the first time the chain is scanned.
2300
2301Drawback: move_unreachable is also used to find out what's still trash
2302after finalizers may resurrect objects. In _that_ case most unreachable
2303objects will remain unreachable, so it would be more efficient to move
2304the reachable objects instead. But this is a one-time cost, probably not
2305worth complicating the code to speed just a little.
2306------------------------------------------------------------------------ */
2307