annotate Modules/gcmodule.c @ 65035:87f4b32905df 2.7

Cosmetic fix to use print function.
author Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
date Fri, 01 Oct 2010 10:48:47 +0000
parents fd25030223b0
children b36cb4602e21
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1 /*
24072
86b3760d285d SF bug #574132: Major GC related performance regression
Tim Peters <tim.peters@gmail.com>
parents: 24036
diff changeset
2
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
3 Reference Cycle Garbage Collection
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
4 ==================================
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
5
15013
25bc16952a24 - update Neil's email address
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15012
diff changeset
6 Neil Schemenauer <nas@arctrix.com>
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
7
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
8 Based on a post on the python-dev list. Ideas from Guido van Rossum,
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
9 Eric Tiedemann, and various others.
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
10
19585
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
11 http://www.arctrix.com/nas/python/gc/
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
12 http://www.python.org/pipermail/python-dev/2000-March/003869.html
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
13 http://www.python.org/pipermail/python-dev/2000-March/004010.html
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
14 http://www.python.org/pipermail/python-dev/2000-March/004022.html
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
15
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
16 For a highlevel view of the collection process, read the collect
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
17 function.
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
18
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
19 */
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
20
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
21 #include "Python.h"
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
22 #include "frameobject.h" /* for PyFrame_ClearFreeList */
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
23
19585
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
24 /* Get an object's GC head */
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
25 #define AS_GC(o) ((PyGC_Head *)(o)-1)
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
26
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
27 /* Get the object given the GC head */
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
28 #define FROM_GC(g) ((PyObject *)(((PyGC_Head *)g)+1))
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
29
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
30 /*** Global GC state ***/
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
31
23377
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
32 struct gc_generation {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
33 PyGC_Head head;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
34 int threshold; /* collection threshold */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
35 int count; /* count of allocations or collections of younger
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
36 generations */
23377
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
37 };
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
38
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
39 #define NUM_GENERATIONS 3
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
40 #define GEN_HEAD(n) (&generations[n].head)
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
41
23377
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
42 /* linked lists of container objects */
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
43 static struct gc_generation generations[NUM_GENERATIONS] = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
44 /* PyGC_Head, threshold, count */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
45 {{{GEN_HEAD(0), GEN_HEAD(0), 0}}, 700, 0},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
46 {{{GEN_HEAD(1), GEN_HEAD(1), 0}}, 10, 0},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
47 {{{GEN_HEAD(2), GEN_HEAD(2), 0}}, 10, 0},
23377
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
48 };
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
49
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
50 PyGC_Head *_PyGC_generation0 = GEN_HEAD(0);
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
51
13878
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13570
diff changeset
52 static int enabled = 1; /* automatic collection enabled? */
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
53
19585
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
54 /* true if we are currently running the collector */
28511
acf3596fcc28 Reworked move_finalizer_reachable() to create two distinct lists:
Tim Peters <tim.peters@gmail.com>
parents: 28510
diff changeset
55 static int collecting = 0;
19585
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
56
24085
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
57 /* list of uncollectable objects */
28511
acf3596fcc28 Reworked move_finalizer_reachable() to create two distinct lists:
Tim Peters <tim.peters@gmail.com>
parents: 28510
diff changeset
58 static PyObject *garbage = NULL;
24085
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
59
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
60 /* Python string to use if unhandled exception occurs */
28511
acf3596fcc28 Reworked move_finalizer_reachable() to create two distinct lists:
Tim Peters <tim.peters@gmail.com>
parents: 28510
diff changeset
61 static PyObject *gc_str = NULL;
24085
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
62
28507
a0503bdb0c24 Fixed new seemingly random segfaults, by moving the initialization of
Tim Peters <tim.peters@gmail.com>
parents: 28504
diff changeset
63 /* Python string used to look for __del__ attribute. */
a0503bdb0c24 Fixed new seemingly random segfaults, by moving the initialization of
Tim Peters <tim.peters@gmail.com>
parents: 28504
diff changeset
64 static PyObject *delstr = NULL;
28504
cb5db5561cdc Fix Tim's boom example.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28498
diff changeset
65
50676
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
66 /* This is the number of objects who survived the last full collection. It
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
67 approximates the number of long lived objects tracked by the GC.
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
68
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
69 (by "full collection", we mean a collection of the oldest generation).
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
70 */
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
71 static Py_ssize_t long_lived_total = 0;
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
72
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
73 /* This is the number of objects who survived all "non-full" collections,
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
74 and are awaiting to undergo a full collection for the first time.
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
75
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
76 */
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
77 static Py_ssize_t long_lived_pending = 0;
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
78
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
79 /*
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
80 NOTE: about the counting of long-lived objects.
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
81
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
82 To limit the cost of garbage collection, there are two strategies;
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
83 - make each collection faster, e.g. by scanning fewer objects
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
84 - do less collections
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
85 This heuristic is about the latter strategy.
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
86
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
87 In addition to the various configurable thresholds, we only trigger a
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
88 full collection if the ratio
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
89 long_lived_pending / long_lived_total
50676
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
90 is above a given value (hardwired to 25%).
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
91
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
92 The reason is that, while "non-full" collections (i.e., collections of
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
93 the young and middle generations) will always examine roughly the same
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
94 number of objects -- determined by the aforementioned thresholds --,
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
95 the cost of a full collection is proportional to the total number of
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
96 long-lived objects, which is virtually unbounded.
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
97
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
98 Indeed, it has been remarked that doing a full collection every
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
99 <constant number> of object creations entails a dramatic performance
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
100 degradation in workloads which consist in creating and storing lots of
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
101 long-lived objects (e.g. building a large list of GC-tracked objects would
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
102 show quadratic performance, instead of linear as expected: see issue #4074).
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
103
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
104 Using the above ratio, instead, yields amortized linear performance in
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
105 the total number of objects (the effect of which can be summarized
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
106 thusly: "each full garbage collection is more and more costly as the
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
107 number of objects grows, but we do fewer and fewer of them").
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
108
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
109 This heuristic was suggested by Martin von Löwis on python-dev in
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
110 June 2008. His original analysis and proposal can be found at:
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
111 http://mail.python.org/pipermail/python-dev/2008-June/080579.html
50676
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
112 */
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
113
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
114
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
115 /* set for debugging information */
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
116 #define DEBUG_STATS (1<<0) /* print collection statistics */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
117 #define DEBUG_COLLECTABLE (1<<1) /* print collectable objects */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
118 #define DEBUG_UNCOLLECTABLE (1<<2) /* print uncollectable objects */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
119 #define DEBUG_INSTANCES (1<<3) /* print instances */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
120 #define DEBUG_OBJECTS (1<<4) /* print other objects */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
121 #define DEBUG_SAVEALL (1<<5) /* save all garbage in gc.garbage */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
122 #define DEBUG_LEAK DEBUG_COLLECTABLE | \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
123 DEBUG_UNCOLLECTABLE | \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
124 DEBUG_INSTANCES | \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
125 DEBUG_OBJECTS | \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
126 DEBUG_SAVEALL
14330
6dee1985c681 refactor __del__ exception handler into PyErr_WriteUnraisable
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 14294
diff changeset
127 static int debug;
37987
06c059b92d0b Patch from Aldo Cortesi (OpenBSD buildbot owner).
Neal Norwitz <nnorwitz@gmail.com>
parents: 37872
diff changeset
128 static PyObject *tmod = NULL;
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
129
24085
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
130 /*--------------------------------------------------------------------------
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
131 gc_refs values.
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
132
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
133 Between collections, every gc'ed object has one of two gc_refs values:
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
134
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
135 GC_UNTRACKED
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
136 The initial state; objects returned by PyObject_GC_Malloc are in this
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
137 state. The object doesn't live in any generation list, and its
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
138 tp_traverse slot must not be called.
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
139
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
140 GC_REACHABLE
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
141 The object lives in some generation list, and its tp_traverse is safe to
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
142 call. An object transitions to GC_REACHABLE when PyObject_GC_Track
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
143 is called.
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
144
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
145 During a collection, gc_refs can temporarily take on other states:
19585
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
146
24085
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
147 >= 0
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
148 At the start of a collection, update_refs() copies the true refcount
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
149 to gc_refs, for each object in the generation being collected.
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
150 subtract_refs() then adjusts gc_refs so that it equals the number of
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
151 times an object is referenced directly from outside the generation
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
152 being collected.
26138
4ab6b7c3f311 Fix typo.
Martin v. Löwis <martin@v.loewis.de>
parents: 24714
diff changeset
153 gc_refs remains >= 0 throughout these steps.
24085
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
154
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
155 GC_TENTATIVELY_UNREACHABLE
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
156 move_unreachable() then moves objects not reachable (whether directly or
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
157 indirectly) from outside the generation into an "unreachable" set.
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
158 Objects that are found to be reachable have gc_refs set to GC_REACHABLE
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
159 again. Objects that are found to be unreachable have gc_refs set to
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
160 GC_TENTATIVELY_UNREACHABLE. It's "tentatively" because the pass doing
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
161 this can't be sure until it ends, and GC_TENTATIVELY_UNREACHABLE may
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
162 transition back to GC_REACHABLE.
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
163
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
164 Only objects with GC_TENTATIVELY_UNREACHABLE still set are candidates
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
165 for collection. If it's decided not to collect such an object (e.g.,
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
166 it has a __del__ method), its gc_refs is restored to GC_REACHABLE again.
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
167 ----------------------------------------------------------------------------
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
168 */
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
169 #define GC_UNTRACKED _PyGC_REFS_UNTRACKED
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
170 #define GC_REACHABLE _PyGC_REFS_REACHABLE
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
171 #define GC_TENTATIVELY_UNREACHABLE _PyGC_REFS_TENTATIVELY_UNREACHABLE
24075
32db075def3f OK, I couldn't stand it <0.5 wink>: removed all uncertainty about what's
Tim Peters <tim.peters@gmail.com>
parents: 24074
diff changeset
172
24085
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
173 #define IS_TRACKED(o) ((AS_GC(o))->gc.gc_refs != GC_UNTRACKED)
24075
32db075def3f OK, I couldn't stand it <0.5 wink>: removed all uncertainty about what's
Tim Peters <tim.peters@gmail.com>
parents: 24074
diff changeset
174 #define IS_REACHABLE(o) ((AS_GC(o))->gc.gc_refs == GC_REACHABLE)
32db075def3f OK, I couldn't stand it <0.5 wink>: removed all uncertainty about what's
Tim Peters <tim.peters@gmail.com>
parents: 24074
diff changeset
175 #define IS_TENTATIVELY_UNREACHABLE(o) ( \
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
176 (AS_GC(o))->gc.gc_refs == GC_TENTATIVELY_UNREACHABLE)
23495
68e3f3dd4d48 Add IS_TRACKED and IS_MOVED macros. This makes the logic a little more clear.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23377
diff changeset
177
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
178 /*** list functions ***/
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
179
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
180 static void
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
181 gc_list_init(PyGC_Head *list)
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
182 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
183 list->gc.gc_prev = list;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
184 list->gc.gc_next = list;
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
185 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
186
23377
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
187 static int
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
188 gc_list_is_empty(PyGC_Head *list)
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
189 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
190 return (list->gc.gc_next == list);
23377
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
191 }
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
192
33869
223d20afc8b5 gc list function cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 33867
diff changeset
193 #if 0
223d20afc8b5 gc list function cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 33867
diff changeset
194 /* This became unused after gc_list_move() was introduced. */
223d20afc8b5 gc list function cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 33867
diff changeset
195 /* Append `node` to `list`. */
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
196 static void
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
197 gc_list_append(PyGC_Head *node, PyGC_Head *list)
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
198 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
199 node->gc.gc_next = list;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
200 node->gc.gc_prev = list->gc.gc_prev;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
201 node->gc.gc_prev->gc.gc_next = node;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
202 list->gc.gc_prev = node;
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
203 }
33869
223d20afc8b5 gc list function cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 33867
diff changeset
204 #endif
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
205
33869
223d20afc8b5 gc list function cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 33867
diff changeset
206 /* Remove `node` from the gc list it's currently in. */
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
207 static void
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
208 gc_list_remove(PyGC_Head *node)
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
209 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
210 node->gc.gc_prev->gc.gc_next = node->gc.gc_next;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
211 node->gc.gc_next->gc.gc_prev = node->gc.gc_prev;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
212 node->gc.gc_next = NULL; /* object is not currently tracked */
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
213 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
214
33869
223d20afc8b5 gc list function cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 33867
diff changeset
215 /* Move `node` from the gc list it's currently in (which is not explicitly
223d20afc8b5 gc list function cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 33867
diff changeset
216 * named here) to the end of `list`. This is semantically the same as
223d20afc8b5 gc list function cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 33867
diff changeset
217 * gc_list_remove(node) followed by gc_list_append(node, list).
223d20afc8b5 gc list function cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 33867
diff changeset
218 */
223d20afc8b5 gc list function cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 33867
diff changeset
219 static void
223d20afc8b5 gc list function cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 33867
diff changeset
220 gc_list_move(PyGC_Head *node, PyGC_Head *list)
223d20afc8b5 gc list function cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 33867
diff changeset
221 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
222 PyGC_Head *new_prev;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
223 PyGC_Head *current_prev = node->gc.gc_prev;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
224 PyGC_Head *current_next = node->gc.gc_next;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
225 /* Unlink from current list. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
226 current_prev->gc.gc_next = current_next;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
227 current_next->gc.gc_prev = current_prev;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
228 /* Relink at end of new list. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
229 new_prev = node->gc.gc_prev = list->gc.gc_prev;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
230 new_prev->gc.gc_next = list->gc.gc_prev = node;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
231 node->gc.gc_next = list;
33869
223d20afc8b5 gc list function cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 33867
diff changeset
232 }
223d20afc8b5 gc list function cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 33867
diff changeset
233
223d20afc8b5 gc list function cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 33867
diff changeset
234 /* append list `from` onto list `to`; `from` becomes an empty list */
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
235 static void
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
236 gc_list_merge(PyGC_Head *from, PyGC_Head *to)
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
237 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
238 PyGC_Head *tail;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
239 assert(from != to);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
240 if (!gc_list_is_empty(from)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
241 tail = to->gc.gc_prev;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
242 tail->gc.gc_next = from->gc.gc_next;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
243 tail->gc.gc_next->gc.gc_prev = tail;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
244 to->gc.gc_prev = from->gc.gc_prev;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
245 to->gc.gc_prev->gc.gc_next = to;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
246 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
247 gc_list_init(from);
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
248 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
249
36839
7c1aca8e3d7d Make PyGC_Collect() use Py_ssize_t.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36769
diff changeset
250 static Py_ssize_t
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
251 gc_list_size(PyGC_Head *list)
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
252 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
253 PyGC_Head *gc;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
254 Py_ssize_t n = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
255 for (gc = list->gc.gc_next; gc != list; gc = gc->gc.gc_next) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
256 n++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
257 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
258 return n;
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
259 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
260
28515
4c596dd60c22 handle_finalizers(): Rewrote to call append_objects() and gc_list_merge()
Tim Peters <tim.peters@gmail.com>
parents: 28512
diff changeset
261 /* Append objects in a GC list to a Python list.
4c596dd60c22 handle_finalizers(): Rewrote to call append_objects() and gc_list_merge()
Tim Peters <tim.peters@gmail.com>
parents: 28512
diff changeset
262 * Return 0 if all OK, < 0 if error (out of memory for list).
4c596dd60c22 handle_finalizers(): Rewrote to call append_objects() and gc_list_merge()
Tim Peters <tim.peters@gmail.com>
parents: 28512
diff changeset
263 */
4c596dd60c22 handle_finalizers(): Rewrote to call append_objects() and gc_list_merge()
Tim Peters <tim.peters@gmail.com>
parents: 28512
diff changeset
264 static int
4c596dd60c22 handle_finalizers(): Rewrote to call append_objects() and gc_list_merge()
Tim Peters <tim.peters@gmail.com>
parents: 28512
diff changeset
265 append_objects(PyObject *py_list, PyGC_Head *gc_list)
4c596dd60c22 handle_finalizers(): Rewrote to call append_objects() and gc_list_merge()
Tim Peters <tim.peters@gmail.com>
parents: 28512
diff changeset
266 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
267 PyGC_Head *gc;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
268 for (gc = gc_list->gc.gc_next; gc != gc_list; gc = gc->gc.gc_next) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
269 PyObject *op = FROM_GC(gc);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
270 if (op != py_list) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
271 if (PyList_Append(py_list, op)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
272 return -1; /* exception */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
273 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
274 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
275 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
276 return 0;
28515
4c596dd60c22 handle_finalizers(): Rewrote to call append_objects() and gc_list_merge()
Tim Peters <tim.peters@gmail.com>
parents: 28512
diff changeset
277 }
4c596dd60c22 handle_finalizers(): Rewrote to call append_objects() and gc_list_merge()
Tim Peters <tim.peters@gmail.com>
parents: 28512
diff changeset
278
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
279 /*** end of list stuff ***/
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
280
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
281
24075
32db075def3f OK, I couldn't stand it <0.5 wink>: removed all uncertainty about what's
Tim Peters <tim.peters@gmail.com>
parents: 24074
diff changeset
282 /* Set all gc_refs = ob_refcnt. After this, gc_refs is > 0 for all objects
32db075def3f OK, I couldn't stand it <0.5 wink>: removed all uncertainty about what's
Tim Peters <tim.peters@gmail.com>
parents: 24074
diff changeset
283 * in containers, and is GC_REACHABLE for all tracked gc objects not in
32db075def3f OK, I couldn't stand it <0.5 wink>: removed all uncertainty about what's
Tim Peters <tim.peters@gmail.com>
parents: 24074
diff changeset
284 * containers.
24072
86b3760d285d SF bug #574132: Major GC related performance regression
Tim Peters <tim.peters@gmail.com>
parents: 24036
diff changeset
285 */
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
286 static void
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
287 update_refs(PyGC_Head *containers)
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
288 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
289 PyGC_Head *gc = containers->gc.gc_next;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
290 for (; gc != containers; gc = gc->gc.gc_next) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
291 assert(gc->gc.gc_refs == GC_REACHABLE);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
292 gc->gc.gc_refs = Py_REFCNT(FROM_GC(gc));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
293 /* Python's cyclic gc should never see an incoming refcount
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
294 * of 0: if something decref'ed to 0, it should have been
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
295 * deallocated immediately at that time.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
296 * Possible cause (if the assert triggers): a tp_dealloc
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
297 * routine left a gc-aware object tracked during its teardown
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
298 * phase, and did something-- or allowed something to happen --
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
299 * that called back into Python. gc can trigger then, and may
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
300 * see the still-tracked dying object. Before this assert
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
301 * was added, such mistakes went on to allow gc to try to
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
302 * delete the object again. In a debug build, that caused
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
303 * a mysterious segfault, when _Py_ForgetReference tried
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
304 * to remove the object from the doubly-linked list of all
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
305 * objects a second time. In a release build, an actual
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
306 * double deallocation occurred, which leads to corruption
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
307 * of the allocator's internal bookkeeping pointers. That's
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
308 * so serious that maybe this should be a release-build
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
309 * check instead of an assert?
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
310 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
311 assert(gc->gc.gc_refs != 0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
312 }
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
313 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
314
24075
32db075def3f OK, I couldn't stand it <0.5 wink>: removed all uncertainty about what's
Tim Peters <tim.peters@gmail.com>
parents: 24074
diff changeset
315 /* A traversal callback for subtract_refs. */
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
316 static int
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
317 visit_decref(PyObject *op, void *data)
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
318 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
319 assert(op != NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
320 if (PyObject_IS_GC(op)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
321 PyGC_Head *gc = AS_GC(op);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
322 /* We're only interested in gc_refs for objects in the
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
323 * generation being collected, which can be recognized
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
324 * because only they have positive gc_refs.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
325 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
326 assert(gc->gc.gc_refs != 0); /* else refcount was too small */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
327 if (gc->gc.gc_refs > 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
328 gc->gc.gc_refs--;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
329 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
330 return 0;
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
331 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
332
24075
32db075def3f OK, I couldn't stand it <0.5 wink>: removed all uncertainty about what's
Tim Peters <tim.peters@gmail.com>
parents: 24074
diff changeset
333 /* Subtract internal references from gc_refs. After this, gc_refs is >= 0
32db075def3f OK, I couldn't stand it <0.5 wink>: removed all uncertainty about what's
Tim Peters <tim.peters@gmail.com>
parents: 24074
diff changeset
334 * for all objects in containers, and is GC_REACHABLE for all tracked gc
32db075def3f OK, I couldn't stand it <0.5 wink>: removed all uncertainty about what's
Tim Peters <tim.peters@gmail.com>
parents: 24074
diff changeset
335 * objects not in containers. The ones with gc_refs > 0 are directly
32db075def3f OK, I couldn't stand it <0.5 wink>: removed all uncertainty about what's
Tim Peters <tim.peters@gmail.com>
parents: 24074
diff changeset
336 * reachable from outside containers, and so can't be collected.
32db075def3f OK, I couldn't stand it <0.5 wink>: removed all uncertainty about what's
Tim Peters <tim.peters@gmail.com>
parents: 24074
diff changeset
337 */
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
338 static void
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
339 subtract_refs(PyGC_Head *containers)
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
340 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
341 traverseproc traverse;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
342 PyGC_Head *gc = containers->gc.gc_next;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
343 for (; gc != containers; gc=gc->gc.gc_next) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
344 traverse = Py_TYPE(FROM_GC(gc))->tp_traverse;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
345 (void) traverse(FROM_GC(gc),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
346 (visitproc)visit_decref,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
347 NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
348 }
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
349 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
350
24075
32db075def3f OK, I couldn't stand it <0.5 wink>: removed all uncertainty about what's
Tim Peters <tim.peters@gmail.com>
parents: 24074
diff changeset
351 /* A traversal callback for move_unreachable. */
32db075def3f OK, I couldn't stand it <0.5 wink>: removed all uncertainty about what's
Tim Peters <tim.peters@gmail.com>
parents: 24074
diff changeset
352 static int
32db075def3f OK, I couldn't stand it <0.5 wink>: removed all uncertainty about what's
Tim Peters <tim.peters@gmail.com>
parents: 24074
diff changeset
353 visit_reachable(PyObject *op, PyGC_Head *reachable)
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
354 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
355 if (PyObject_IS_GC(op)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
356 PyGC_Head *gc = AS_GC(op);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
357 const Py_ssize_t gc_refs = gc->gc.gc_refs;
24075
32db075def3f OK, I couldn't stand it <0.5 wink>: removed all uncertainty about what's
Tim Peters <tim.peters@gmail.com>
parents: 24074
diff changeset
358
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
359 if (gc_refs == 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
360 /* This is in move_unreachable's 'young' list, but
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
361 * the traversal hasn't yet gotten to it. All
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
362 * we need to do is tell move_unreachable that it's
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
363 * reachable.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
364 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
365 gc->gc.gc_refs = 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
366 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
367 else if (gc_refs == GC_TENTATIVELY_UNREACHABLE) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
368 /* This had gc_refs = 0 when move_unreachable got
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
369 * to it, but turns out it's reachable after all.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
370 * Move it back to move_unreachable's 'young' list,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
371 * and move_unreachable will eventually get to it
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
372 * again.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
373 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
374 gc_list_move(gc, reachable);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
375 gc->gc.gc_refs = 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
376 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
377 /* Else there's nothing to do.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
378 * If gc_refs > 0, it must be in move_unreachable's 'young'
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
379 * list, and move_unreachable will eventually get to it.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
380 * If gc_refs == GC_REACHABLE, it's either in some other
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
381 * generation so we don't care about it, or move_unreachable
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
382 * already dealt with it.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
383 * If gc_refs == GC_UNTRACKED, it must be ignored.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
384 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
385 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
386 assert(gc_refs > 0
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
387 || gc_refs == GC_REACHABLE
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
388 || gc_refs == GC_UNTRACKED);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
389 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
390 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
391 return 0;
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
392 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
393
24075
32db075def3f OK, I couldn't stand it <0.5 wink>: removed all uncertainty about what's
Tim Peters <tim.peters@gmail.com>
parents: 24074
diff changeset
394 /* Move the unreachable objects from young to unreachable. After this,
32db075def3f OK, I couldn't stand it <0.5 wink>: removed all uncertainty about what's
Tim Peters <tim.peters@gmail.com>
parents: 24074
diff changeset
395 * all objects in young have gc_refs = GC_REACHABLE, and all objects in
32db075def3f OK, I couldn't stand it <0.5 wink>: removed all uncertainty about what's
Tim Peters <tim.peters@gmail.com>
parents: 24074
diff changeset
396 * unreachable have gc_refs = GC_TENTATIVELY_UNREACHABLE. All tracked
32db075def3f OK, I couldn't stand it <0.5 wink>: removed all uncertainty about what's
Tim Peters <tim.peters@gmail.com>
parents: 24074
diff changeset
397 * gc objects not in young or unreachable still have gc_refs = GC_REACHABLE.
32db075def3f OK, I couldn't stand it <0.5 wink>: removed all uncertainty about what's
Tim Peters <tim.peters@gmail.com>
parents: 24074
diff changeset
398 * All objects in young after this are directly or indirectly reachable
32db075def3f OK, I couldn't stand it <0.5 wink>: removed all uncertainty about what's
Tim Peters <tim.peters@gmail.com>
parents: 24074
diff changeset
399 * from outside the original young; and all objects in unreachable are
32db075def3f OK, I couldn't stand it <0.5 wink>: removed all uncertainty about what's
Tim Peters <tim.peters@gmail.com>
parents: 24074
diff changeset
400 * not.
24072
86b3760d285d SF bug #574132: Major GC related performance regression
Tim Peters <tim.peters@gmail.com>
parents: 24036
diff changeset
401 */
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
402 static void
24075
32db075def3f OK, I couldn't stand it <0.5 wink>: removed all uncertainty about what's
Tim Peters <tim.peters@gmail.com>
parents: 24074
diff changeset
403 move_unreachable(PyGC_Head *young, PyGC_Head *unreachable)
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
404 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
405 PyGC_Head *gc = young->gc.gc_next;
24075
32db075def3f OK, I couldn't stand it <0.5 wink>: removed all uncertainty about what's
Tim Peters <tim.peters@gmail.com>
parents: 24074
diff changeset
406
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
407 /* Invariants: all objects "to the left" of us in young have gc_refs
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
408 * = GC_REACHABLE, and are indeed reachable (directly or indirectly)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
409 * from outside the young list as it was at entry. All other objects
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
410 * from the original young "to the left" of us are in unreachable now,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
411 * and have gc_refs = GC_TENTATIVELY_UNREACHABLE. All objects to the
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
412 * left of us in 'young' now have been scanned, and no objects here
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
413 * or to the right have been scanned yet.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
414 */
24075
32db075def3f OK, I couldn't stand it <0.5 wink>: removed all uncertainty about what's
Tim Peters <tim.peters@gmail.com>
parents: 24074
diff changeset
415
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
416 while (gc != young) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
417 PyGC_Head *next;
24075
32db075def3f OK, I couldn't stand it <0.5 wink>: removed all uncertainty about what's
Tim Peters <tim.peters@gmail.com>
parents: 24074
diff changeset
418
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
419 if (gc->gc.gc_refs) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
420 /* gc is definitely reachable from outside the
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
421 * original 'young'. Mark it as such, and traverse
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
422 * its pointers to find any other objects that may
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
423 * be directly reachable from it. Note that the
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
424 * call to tp_traverse may append objects to young,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
425 * so we have to wait until it returns to determine
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
426 * the next object to visit.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
427 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
428 PyObject *op = FROM_GC(gc);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
429 traverseproc traverse = Py_TYPE(op)->tp_traverse;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
430 assert(gc->gc.gc_refs > 0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
431 gc->gc.gc_refs = GC_REACHABLE;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
432 (void) traverse(op,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
433 (visitproc)visit_reachable,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
434 (void *)young);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
435 next = gc->gc.gc_next;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
436 if (PyTuple_CheckExact(op)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
437 _PyTuple_MaybeUntrack(op);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
438 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
439 else if (PyDict_CheckExact(op)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
440 _PyDict_MaybeUntrack(op);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
441 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
442 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
443 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
444 /* This *may* be unreachable. To make progress,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
445 * assume it is. gc isn't directly reachable from
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
446 * any object we've already traversed, but may be
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
447 * reachable from an object we haven't gotten to yet.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
448 * visit_reachable will eventually move gc back into
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
449 * young if that's so, and we'll see it again.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
450 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
451 next = gc->gc.gc_next;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
452 gc_list_move(gc, unreachable);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
453 gc->gc.gc_refs = GC_TENTATIVELY_UNREACHABLE;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
454 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
455 gc = next;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
456 }
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
457 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
458
28508
8233a0294c41 New comments. Rewrote has_finalizer() as a sequence of ifs instead of
Tim Peters <tim.peters@gmail.com>
parents: 28507
diff changeset
459 /* Return true if object has a finalization method.
8233a0294c41 New comments. Rewrote has_finalizer() as a sequence of ifs instead of
Tim Peters <tim.peters@gmail.com>
parents: 28507
diff changeset
460 * CAUTION: An instance of an old-style class has to be checked for a
28520
6653a56002ef Reworked has_finalizer() to use the new _PyObject_Lookup() instead
Tim Peters <tim.peters@gmail.com>
parents: 28516
diff changeset
461 *__del__ method, and earlier versions of this used to call PyObject_HasAttr,
6653a56002ef Reworked has_finalizer() to use the new _PyObject_Lookup() instead
Tim Peters <tim.peters@gmail.com>
parents: 28516
diff changeset
462 * which in turn could call the class's __getattr__ hook (if any). That
6653a56002ef Reworked has_finalizer() to use the new _PyObject_Lookup() instead
Tim Peters <tim.peters@gmail.com>
parents: 28516
diff changeset
463 * could invoke arbitrary Python code, mutating the object graph in arbitrary
6653a56002ef Reworked has_finalizer() to use the new _PyObject_Lookup() instead
Tim Peters <tim.peters@gmail.com>
parents: 28516
diff changeset
464 * ways, and that was the source of some excruciatingly subtle bugs.
28508
8233a0294c41 New comments. Rewrote has_finalizer() as a sequence of ifs instead of
Tim Peters <tim.peters@gmail.com>
parents: 28507
diff changeset
465 */
20818
9aded6235d89 Add has_finalizer predictate function. Use it when deciding which
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20810
diff changeset
466 static int
9aded6235d89 Add has_finalizer predictate function. Use it when deciding which
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20810
diff changeset
467 has_finalizer(PyObject *op)
9aded6235d89 Add has_finalizer predictate function. Use it when deciding which
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20810
diff changeset
468 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
469 if (PyInstance_Check(op)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
470 assert(delstr != NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
471 return _PyInstance_Lookup(op, delstr) != NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
472 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
473 else if (PyType_HasFeature(op->ob_type, Py_TPFLAGS_HEAPTYPE))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
474 return op->ob_type->tp_del != NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
475 else if (PyGen_CheckExact(op))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
476 return PyGen_NeedsFinalizing((PyGenObject *)op);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
477 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
478 return 0;
20818
9aded6235d89 Add has_finalizer predictate function. Use it when deciding which
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20810
diff changeset
479 }
9aded6235d89 Add has_finalizer predictate function. Use it when deciding which
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20810
diff changeset
480
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
481 /* Move the objects in unreachable with __del__ methods into `finalizers`.
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
482 * Objects moved into `finalizers` have gc_refs set to GC_REACHABLE; the
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
483 * objects remaining in unreachable are left at GC_TENTATIVELY_UNREACHABLE.
28504
cb5db5561cdc Fix Tim's boom example.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28498
diff changeset
484 */
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
485 static void
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
486 move_finalizers(PyGC_Head *unreachable, PyGC_Head *finalizers)
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
487 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
488 PyGC_Head *gc;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
489 PyGC_Head *next;
28520
6653a56002ef Reworked has_finalizer() to use the new _PyObject_Lookup() instead
Tim Peters <tim.peters@gmail.com>
parents: 28516
diff changeset
490
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
491 /* March over unreachable. Move objects with finalizers into
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
492 * `finalizers`.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
493 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
494 for (gc = unreachable->gc.gc_next; gc != unreachable; gc = next) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
495 PyObject *op = FROM_GC(gc);
28504
cb5db5561cdc Fix Tim's boom example.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28498
diff changeset
496
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
497 assert(IS_TENTATIVELY_UNREACHABLE(op));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
498 next = gc->gc.gc_next;
28510
0de0ded19968 move_finalizers(): Rewrote. It's not necessary for this routine
Tim Peters <tim.peters@gmail.com>
parents: 28508
diff changeset
499
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
500 if (has_finalizer(op)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
501 gc_list_move(gc, finalizers);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
502 gc->gc.gc_refs = GC_REACHABLE;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
503 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
504 }
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
505 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
506
24075
32db075def3f OK, I couldn't stand it <0.5 wink>: removed all uncertainty about what's
Tim Peters <tim.peters@gmail.com>
parents: 24074
diff changeset
507 /* A traversal callback for move_finalizer_reachable. */
32db075def3f OK, I couldn't stand it <0.5 wink>: removed all uncertainty about what's
Tim Peters <tim.peters@gmail.com>
parents: 24074
diff changeset
508 static int
32db075def3f OK, I couldn't stand it <0.5 wink>: removed all uncertainty about what's
Tim Peters <tim.peters@gmail.com>
parents: 24074
diff changeset
509 visit_move(PyObject *op, PyGC_Head *tolist)
32db075def3f OK, I couldn't stand it <0.5 wink>: removed all uncertainty about what's
Tim Peters <tim.peters@gmail.com>
parents: 24074
diff changeset
510 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
511 if (PyObject_IS_GC(op)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
512 if (IS_TENTATIVELY_UNREACHABLE(op)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
513 PyGC_Head *gc = AS_GC(op);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
514 gc_list_move(gc, tolist);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
515 gc->gc.gc_refs = GC_REACHABLE;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
516 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
517 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
518 return 0;
24075
32db075def3f OK, I couldn't stand it <0.5 wink>: removed all uncertainty about what's
Tim Peters <tim.peters@gmail.com>
parents: 24074
diff changeset
519 }
32db075def3f OK, I couldn't stand it <0.5 wink>: removed all uncertainty about what's
Tim Peters <tim.peters@gmail.com>
parents: 24074
diff changeset
520
32db075def3f OK, I couldn't stand it <0.5 wink>: removed all uncertainty about what's
Tim Peters <tim.peters@gmail.com>
parents: 24074
diff changeset
521 /* Move objects that are reachable from finalizers, from the unreachable set
28520
6653a56002ef Reworked has_finalizer() to use the new _PyObject_Lookup() instead
Tim Peters <tim.peters@gmail.com>
parents: 28516
diff changeset
522 * into finalizers set.
24075
32db075def3f OK, I couldn't stand it <0.5 wink>: removed all uncertainty about what's
Tim Peters <tim.peters@gmail.com>
parents: 24074
diff changeset
523 */
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
524 static void
28520
6653a56002ef Reworked has_finalizer() to use the new _PyObject_Lookup() instead
Tim Peters <tim.peters@gmail.com>
parents: 28516
diff changeset
525 move_finalizer_reachable(PyGC_Head *finalizers)
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
526 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
527 traverseproc traverse;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
528 PyGC_Head *gc = finalizers->gc.gc_next;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
529 for (; gc != finalizers; gc = gc->gc.gc_next) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
530 /* Note that the finalizers list may grow during this. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
531 traverse = Py_TYPE(FROM_GC(gc))->tp_traverse;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
532 (void) traverse(FROM_GC(gc),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
533 (visitproc)visit_move,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
534 (void *)finalizers);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
535 }
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
536 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
537
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
538 /* Clear all weakrefs to unreachable objects, and if such a weakref has a
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
539 * callback, invoke it if necessary. Note that it's possible for such
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
540 * weakrefs to be outside the unreachable set -- indeed, those are precisely
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
541 * the weakrefs whose callbacks must be invoked. See gc_weakref.txt for
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
542 * overview & some details. Some weakrefs with callbacks may be reclaimed
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
543 * directly by this routine; the number reclaimed is the return value. Other
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
544 * weakrefs with callbacks may be moved into the `old` generation. Objects
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
545 * moved into `old` have gc_refs set to GC_REACHABLE; the objects remaining in
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
546 * unreachable are left at GC_TENTATIVELY_UNREACHABLE. When this returns,
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
547 * no object in `unreachable` is weakly referenced anymore.
30988
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30918
diff changeset
548 */
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30918
diff changeset
549 static int
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
550 handle_weakrefs(PyGC_Head *unreachable, PyGC_Head *old)
30988
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30918
diff changeset
551 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
552 PyGC_Head *gc;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
553 PyObject *op; /* generally FROM_GC(gc) */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
554 PyWeakReference *wr; /* generally a cast of op */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
555 PyGC_Head wrcb_to_call; /* weakrefs with callbacks to call */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
556 PyGC_Head *next;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
557 int num_freed = 0;
30988
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30918
diff changeset
558
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
559 gc_list_init(&wrcb_to_call);
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
560
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
561 /* Clear all weakrefs to the objects in unreachable. If such a weakref
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
562 * also has a callback, move it into `wrcb_to_call` if the callback
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
563 * needs to be invoked. Note that we cannot invoke any callbacks until
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
564 * all weakrefs to unreachable objects are cleared, lest the callback
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
565 * resurrect an unreachable object via a still-active weakref. We
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
566 * make another pass over wrcb_to_call, invoking callbacks, after this
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
567 * pass completes.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
568 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
569 for (gc = unreachable->gc.gc_next; gc != unreachable; gc = next) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
570 PyWeakReference **wrlist;
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
571
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
572 op = FROM_GC(gc);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
573 assert(IS_TENTATIVELY_UNREACHABLE(op));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
574 next = gc->gc.gc_next;
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
575
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
576 if (! PyType_SUPPORTS_WEAKREFS(Py_TYPE(op)))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
577 continue;
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
578
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
579 /* It supports weakrefs. Does it have any? */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
580 wrlist = (PyWeakReference **)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
581 PyObject_GET_WEAKREFS_LISTPTR(op);
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
582
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
583 /* `op` may have some weakrefs. March over the list, clear
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
584 * all the weakrefs, and move the weakrefs with callbacks
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
585 * that must be called into wrcb_to_call.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
586 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
587 for (wr = *wrlist; wr != NULL; wr = *wrlist) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
588 PyGC_Head *wrasgc; /* AS_GC(wr) */
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
589
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
590 /* _PyWeakref_ClearRef clears the weakref but leaves
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
591 * the callback pointer intact. Obscure: it also
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
592 * changes *wrlist.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
593 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
594 assert(wr->wr_object == op);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
595 _PyWeakref_ClearRef(wr);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
596 assert(wr->wr_object == Py_None);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
597 if (wr->wr_callback == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
598 continue; /* no callback */
30988
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30918
diff changeset
599
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
600 /* Headache time. `op` is going away, and is weakly referenced by
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
601 * `wr`, which has a callback. Should the callback be invoked? If wr
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
602 * is also trash, no:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
603 *
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
604 * 1. There's no need to call it. The object and the weakref are
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
605 * both going away, so it's legitimate to pretend the weakref is
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
606 * going away first. The user has to ensure a weakref outlives its
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
607 * referent if they want a guarantee that the wr callback will get
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
608 * invoked.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
609 *
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
610 * 2. It may be catastrophic to call it. If the callback is also in
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
611 * cyclic trash (CT), then although the CT is unreachable from
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
612 * outside the current generation, CT may be reachable from the
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
613 * callback. Then the callback could resurrect insane objects.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
614 *
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
615 * Since the callback is never needed and may be unsafe in this case,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
616 * wr is simply left in the unreachable set. Note that because we
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
617 * already called _PyWeakref_ClearRef(wr), its callback will never
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
618 * trigger.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
619 *
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
620 * OTOH, if wr isn't part of CT, we should invoke the callback: the
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
621 * weakref outlived the trash. Note that since wr isn't CT in this
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
622 * case, its callback can't be CT either -- wr acted as an external
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
623 * root to this generation, and therefore its callback did too. So
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
624 * nothing in CT is reachable from the callback either, so it's hard
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
625 * to imagine how calling it later could create a problem for us. wr
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
626 * is moved to wrcb_to_call in this case.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
627 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
628 if (IS_TENTATIVELY_UNREACHABLE(wr))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
629 continue;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
630 assert(IS_REACHABLE(wr));
33867
07d94a162cf8 handle_weakrefs(): Simplification -- there's no need to make a second
Tim Peters <tim.peters@gmail.com>
parents: 33862
diff changeset
631
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
632 /* Create a new reference so that wr can't go away
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
633 * before we can process it again.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
634 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
635 Py_INCREF(wr);
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
636
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
637 /* Move wr to wrcb_to_call, for the next pass. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
638 wrasgc = AS_GC(wr);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
639 assert(wrasgc != next); /* wrasgc is reachable, but
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
640 next isn't, so they can't
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
641 be the same */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
642 gc_list_move(wrasgc, &wrcb_to_call);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
643 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
644 }
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
645
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
646 /* Invoke the callbacks we decided to honor. It's safe to invoke them
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
647 * because they can't reference unreachable objects.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
648 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
649 while (! gc_list_is_empty(&wrcb_to_call)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
650 PyObject *temp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
651 PyObject *callback;
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
652
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
653 gc = wrcb_to_call.gc.gc_next;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
654 op = FROM_GC(gc);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
655 assert(IS_REACHABLE(op));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
656 assert(PyWeakref_Check(op));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
657 wr = (PyWeakReference *)op;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
658 callback = wr->wr_callback;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
659 assert(callback != NULL);
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
660
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
661 /* copy-paste of weakrefobject.c's handle_callback() */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
662 temp = PyObject_CallFunctionObjArgs(callback, wr, NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
663 if (temp == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
664 PyErr_WriteUnraisable(callback);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
665 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
666 Py_DECREF(temp);
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
667
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
668 /* Give up the reference we created in the first pass. When
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
669 * op's refcount hits 0 (which it may or may not do right now),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
670 * op's tp_dealloc will decref op->wr_callback too. Note
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
671 * that the refcount probably will hit 0 now, and because this
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
672 * weakref was reachable to begin with, gc didn't already
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
673 * add it to its count of freed objects. Example: a reachable
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
674 * weak value dict maps some key to this reachable weakref.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
675 * The callback removes this key->weakref mapping from the
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
676 * dict, leaving no other references to the weakref (excepting
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
677 * ours).
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
678 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
679 Py_DECREF(op);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
680 if (wrcb_to_call.gc.gc_next == gc) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
681 /* object is still alive -- move it */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
682 gc_list_move(gc, old);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
683 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
684 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
685 ++num_freed;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
686 }
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
687
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
688 return num_freed;
30988
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30918
diff changeset
689 }
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30918
diff changeset
690
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
691 static void
14294
053627e7ac14 apply patch #101362 by Vladimir Marangozov
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13878
diff changeset
692 debug_instance(char *msg, PyInstanceObject *inst)
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
693 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
694 char *cname;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
695 /* simple version of instance_repr */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
696 PyObject *classname = inst->in_class->cl_name;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
697 if (classname != NULL && PyString_Check(classname))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
698 cname = PyString_AsString(classname);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
699 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
700 cname = "?";
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
701 PySys_WriteStderr("gc: %.100s <%.100s instance at %p>\n",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
702 msg, cname, inst);
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
703 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
704
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
705 static void
14294
053627e7ac14 apply patch #101362 by Vladimir Marangozov
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13878
diff changeset
706 debug_cycle(char *msg, PyObject *op)
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
707 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
708 if ((debug & DEBUG_INSTANCES) && PyInstance_Check(op)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
709 debug_instance(msg, (PyInstanceObject *)op);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
710 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
711 else if (debug & DEBUG_OBJECTS) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
712 PySys_WriteStderr("gc: %.100s <%.100s %p>\n",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
713 msg, Py_TYPE(op)->tp_name, op);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
714 }
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
715 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
716
28511
acf3596fcc28 Reworked move_finalizer_reachable() to create two distinct lists:
Tim Peters <tim.peters@gmail.com>
parents: 28510
diff changeset
717 /* Handle uncollectable garbage (cycles with finalizers, and stuff reachable
acf3596fcc28 Reworked move_finalizer_reachable() to create two distinct lists:
Tim Peters <tim.peters@gmail.com>
parents: 28510
diff changeset
718 * only from such cycles).
28520
6653a56002ef Reworked has_finalizer() to use the new _PyObject_Lookup() instead
Tim Peters <tim.peters@gmail.com>
parents: 28516
diff changeset
719 * If DEBUG_SAVEALL, all objects in finalizers are appended to the module
6653a56002ef Reworked has_finalizer() to use the new _PyObject_Lookup() instead
Tim Peters <tim.peters@gmail.com>
parents: 28516
diff changeset
720 * garbage list (a Python list), else only the objects in finalizers with
6653a56002ef Reworked has_finalizer() to use the new _PyObject_Lookup() instead
Tim Peters <tim.peters@gmail.com>
parents: 28516
diff changeset
721 * __del__ methods are appended to garbage. All objects in finalizers are
6653a56002ef Reworked has_finalizer() to use the new _PyObject_Lookup() instead
Tim Peters <tim.peters@gmail.com>
parents: 28516
diff changeset
722 * merged into the old list regardless.
28515
4c596dd60c22 handle_finalizers(): Rewrote to call append_objects() and gc_list_merge()
Tim Peters <tim.peters@gmail.com>
parents: 28512
diff changeset
723 * Returns 0 if all OK, <0 on error (out of memory to grow the garbage list).
4c596dd60c22 handle_finalizers(): Rewrote to call append_objects() and gc_list_merge()
Tim Peters <tim.peters@gmail.com>
parents: 28512
diff changeset
724 * The finalizers list is made empty on a successful return.
28511
acf3596fcc28 Reworked move_finalizer_reachable() to create two distinct lists:
Tim Peters <tim.peters@gmail.com>
parents: 28510
diff changeset
725 */
28515
4c596dd60c22 handle_finalizers(): Rewrote to call append_objects() and gc_list_merge()
Tim Peters <tim.peters@gmail.com>
parents: 28512
diff changeset
726 static int
28520
6653a56002ef Reworked has_finalizer() to use the new _PyObject_Lookup() instead
Tim Peters <tim.peters@gmail.com>
parents: 28516
diff changeset
727 handle_finalizers(PyGC_Head *finalizers, PyGC_Head *old)
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
728 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
729 PyGC_Head *gc = finalizers->gc.gc_next;
28520
6653a56002ef Reworked has_finalizer() to use the new _PyObject_Lookup() instead
Tim Peters <tim.peters@gmail.com>
parents: 28516
diff changeset
730
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
731 if (garbage == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
732 garbage = PyList_New(0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
733 if (garbage == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
734 Py_FatalError("gc couldn't create gc.garbage list");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
735 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
736 for (; gc != finalizers; gc = gc->gc.gc_next) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
737 PyObject *op = FROM_GC(gc);
28520
6653a56002ef Reworked has_finalizer() to use the new _PyObject_Lookup() instead
Tim Peters <tim.peters@gmail.com>
parents: 28516
diff changeset
738
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
739 if ((debug & DEBUG_SAVEALL) || has_finalizer(op)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
740 if (PyList_Append(garbage, op) < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
741 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
742 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
743 }
28520
6653a56002ef Reworked has_finalizer() to use the new _PyObject_Lookup() instead
Tim Peters <tim.peters@gmail.com>
parents: 28516
diff changeset
744
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
745 gc_list_merge(finalizers, old);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
746 return 0;
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
747 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
748
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
749 /* Break reference cycles by clearing the containers involved. This is
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
750 * tricky business as the lists can be changing and we don't know which
24075
32db075def3f OK, I couldn't stand it <0.5 wink>: removed all uncertainty about what's
Tim Peters <tim.peters@gmail.com>
parents: 24074
diff changeset
751 * objects may be freed. It is possible I screwed something up here.
32db075def3f OK, I couldn't stand it <0.5 wink>: removed all uncertainty about what's
Tim Peters <tim.peters@gmail.com>
parents: 24074
diff changeset
752 */
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
753 static void
28504
cb5db5561cdc Fix Tim's boom example.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28498
diff changeset
754 delete_garbage(PyGC_Head *collectable, PyGC_Head *old)
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
755 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
756 inquiry clear;
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
757
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
758 while (!gc_list_is_empty(collectable)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
759 PyGC_Head *gc = collectable->gc.gc_next;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
760 PyObject *op = FROM_GC(gc);
24072
86b3760d285d SF bug #574132: Major GC related performance regression
Tim Peters <tim.peters@gmail.com>
parents: 24036
diff changeset
761
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
762 assert(IS_TENTATIVELY_UNREACHABLE(op));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
763 if (debug & DEBUG_SAVEALL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
764 PyList_Append(garbage, op);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
765 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
766 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
767 if ((clear = Py_TYPE(op)->tp_clear) != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
768 Py_INCREF(op);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
769 clear(op);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
770 Py_DECREF(op);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
771 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
772 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
773 if (collectable->gc.gc_next == gc) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
774 /* object is still alive, move it, it may die later */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
775 gc_list_move(gc, old);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
776 gc->gc.gc_refs = GC_REACHABLE;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
777 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
778 }
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
779 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
780
45341
65ae68c7aca3 Implemented Martin's suggestion to clear the free lists during the garbage collection of the highest generation.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
781 /* Clear all free lists
65ae68c7aca3 Implemented Martin's suggestion to clear the free lists during the garbage collection of the highest generation.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
782 * All free lists are cleared during the collection of the highest generation.
65ae68c7aca3 Implemented Martin's suggestion to clear the free lists during the garbage collection of the highest generation.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
783 * Allocated items in the free list may keep a pymalloc arena occupied.
65ae68c7aca3 Implemented Martin's suggestion to clear the free lists during the garbage collection of the highest generation.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
784 * Clearing the free lists may give back memory to the OS earlier.
65ae68c7aca3 Implemented Martin's suggestion to clear the free lists during the garbage collection of the highest generation.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
785 */
65ae68c7aca3 Implemented Martin's suggestion to clear the free lists during the garbage collection of the highest generation.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
786 static void
65ae68c7aca3 Implemented Martin's suggestion to clear the free lists during the garbage collection of the highest generation.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
787 clear_freelists(void)
65ae68c7aca3 Implemented Martin's suggestion to clear the free lists during the garbage collection of the highest generation.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
788 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
789 (void)PyMethod_ClearFreeList();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
790 (void)PyFrame_ClearFreeList();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
791 (void)PyCFunction_ClearFreeList();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
792 (void)PyTuple_ClearFreeList();
51032
551257c8058a fix building the core with --disable-unicode
Benjamin Peterson <benjamin@python.org>
parents: 50676
diff changeset
793 #ifdef Py_USING_UNICODE
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
794 (void)PyUnicode_ClearFreeList();
51032
551257c8058a fix building the core with --disable-unicode
Benjamin Peterson <benjamin@python.org>
parents: 50676
diff changeset
795 #endif
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
796 (void)PyInt_ClearFreeList();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
797 (void)PyFloat_ClearFreeList();
45341
65ae68c7aca3 Implemented Martin's suggestion to clear the free lists during the garbage collection of the highest generation.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
798 }
65ae68c7aca3 Implemented Martin's suggestion to clear the free lists during the garbage collection of the highest generation.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
799
50257
e4675113d10e Merged revisions 67654,67676-67677,67681,67692,67725,67746,67748,67761,67784-67785,67787-67788,67802,67832,67848-67849,67859,67862-67864,67880,67882,67885,67889-67892,67895 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48114
diff changeset
800 static double
e4675113d10e Merged revisions 67654,67676-67677,67681,67692,67725,67746,67748,67761,67784-67785,67787-67788,67802,67832,67848-67849,67859,67862-67864,67880,67882,67885,67889-67892,67895 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48114
diff changeset
801 get_time(void)
e4675113d10e Merged revisions 67654,67676-67677,67681,67692,67725,67746,67748,67761,67784-67785,67787-67788,67802,67832,67848-67849,67859,67862-67864,67880,67882,67885,67889-67892,67895 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48114
diff changeset
802 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
803 double result = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
804 if (tmod != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
805 PyObject *f = PyObject_CallMethod(tmod, "time", NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
806 if (f == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
807 PyErr_Clear();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
808 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
809 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
810 if (PyFloat_Check(f))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
811 result = PyFloat_AsDouble(f);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
812 Py_DECREF(f);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
813 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
814 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
815 return result;
50257
e4675113d10e Merged revisions 67654,67676-67677,67681,67692,67725,67746,67748,67761,67784-67785,67787-67788,67802,67832,67848-67849,67859,67862-67864,67880,67882,67885,67889-67892,67895 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48114
diff changeset
816 }
e4675113d10e Merged revisions 67654,67676-67677,67681,67692,67725,67746,67748,67761,67784-67785,67787-67788,67802,67832,67848-67849,67859,67862-67864,67880,67882,67885,67889-67892,67895 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48114
diff changeset
817
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
818 /* This is the main function. Read this to understand how the
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
819 * collection process works. */
36839
7c1aca8e3d7d Make PyGC_Collect() use Py_ssize_t.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36769
diff changeset
820 static Py_ssize_t
23377
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
821 collect(int generation)
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
822 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
823 int i;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
824 Py_ssize_t m = 0; /* # objects collected */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
825 Py_ssize_t n = 0; /* # unreachable objects that couldn't be collected */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
826 PyGC_Head *young; /* the generation we are examining */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
827 PyGC_Head *old; /* next older generation */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
828 PyGC_Head unreachable; /* non-problematic unreachable trash */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
829 PyGC_Head finalizers; /* objects with, & reachable from, __del__ */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
830 PyGC_Head *gc;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
831 double t1 = 0.0;
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
832
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
833 if (delstr == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
834 delstr = PyString_InternFromString("__del__");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
835 if (delstr == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
836 Py_FatalError("gc couldn't allocate \"__del__\"");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
837 }
28507
a0503bdb0c24 Fixed new seemingly random segfaults, by moving the initialization of
Tim Peters <tim.peters@gmail.com>
parents: 28504
diff changeset
838
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
839 if (debug & DEBUG_STATS) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
840 PySys_WriteStderr("gc: collecting generation %d...\n",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
841 generation);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
842 PySys_WriteStderr("gc: objects in each generation:");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
843 for (i = 0; i < NUM_GENERATIONS; i++)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
844 PySys_WriteStderr(" %" PY_FORMAT_SIZE_T "d",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
845 gc_list_size(GEN_HEAD(i)));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
846 t1 = get_time();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
847 PySys_WriteStderr("\n");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
848 }
23377
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
849
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
850 /* update collection and allocation counters */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
851 if (generation+1 < NUM_GENERATIONS)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
852 generations[generation+1].count += 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
853 for (i = 0; i <= generation; i++)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
854 generations[i].count = 0;
23377
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
855
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
856 /* merge younger generations with one we are currently collecting */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
857 for (i = 0; i < generation; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
858 gc_list_merge(GEN_HEAD(i), GEN_HEAD(generation));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
859 }
23377
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
860
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
861 /* handy references */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
862 young = GEN_HEAD(generation);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
863 if (generation < NUM_GENERATIONS-1)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
864 old = GEN_HEAD(generation+1);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
865 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
866 old = young;
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
867
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
868 /* Using ob_refcnt and gc_refs, calculate which objects in the
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
869 * container set are reachable from outside the set (i.e., have a
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
870 * refcount greater than 0 when all the references within the
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
871 * set are taken into account).
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
872 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
873 update_refs(young);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
874 subtract_refs(young);
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
875
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
876 /* Leave everything reachable from outside young in young, and move
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
877 * everything else (in young) to unreachable.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
878 * NOTE: This used to move the reachable objects into a reachable
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
879 * set instead. But most things usually turn out to be reachable,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
880 * so it's more efficient to move the unreachable things.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
881 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
882 gc_list_init(&unreachable);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
883 move_unreachable(young, &unreachable);
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
884
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
885 /* Move reachable objects to next generation. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
886 if (young != old) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
887 if (generation == NUM_GENERATIONS - 2) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
888 long_lived_pending += gc_list_size(young);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
889 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
890 gc_list_merge(young, old);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
891 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
892 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
893 long_lived_pending = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
894 long_lived_total = gc_list_size(young);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
895 }
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
896
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
897 /* All objects in unreachable are trash, but objects reachable from
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
898 * finalizers can't safely be deleted. Python programmers should take
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
899 * care not to create such things. For Python, finalizers means
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
900 * instance objects with __del__ methods. Weakrefs with callbacks
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
901 * can also call arbitrary Python code but they will be dealt with by
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
902 * handle_weakrefs().
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
903 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
904 gc_list_init(&finalizers);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
905 move_finalizers(&unreachable, &finalizers);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
906 /* finalizers contains the unreachable objects with a finalizer;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
907 * unreachable objects reachable *from* those are also uncollectable,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
908 * and we move those into the finalizers list too.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
909 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
910 move_finalizer_reachable(&finalizers);
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
911
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
912 /* Collect statistics on collectable objects found and print
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
913 * debugging information.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
914 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
915 for (gc = unreachable.gc.gc_next; gc != &unreachable;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
916 gc = gc->gc.gc_next) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
917 m++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
918 if (debug & DEBUG_COLLECTABLE) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
919 debug_cycle("collectable", FROM_GC(gc));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
920 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
921 }
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
922
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
923 /* Clear weakrefs and invoke callbacks as necessary. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
924 m += handle_weakrefs(&unreachable, old);
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
925
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
926 /* Call tp_clear on objects in the unreachable set. This will cause
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
927 * the reference cycles to be broken. It may also cause some objects
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
928 * in finalizers to be freed.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
929 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
930 delete_garbage(&unreachable, old);
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
931
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
932 /* Collect statistics on uncollectable objects found and print
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
933 * debugging information. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
934 for (gc = finalizers.gc.gc_next;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
935 gc != &finalizers;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
936 gc = gc->gc.gc_next) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
937 n++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
938 if (debug & DEBUG_UNCOLLECTABLE)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
939 debug_cycle("uncollectable", FROM_GC(gc));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
940 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
941 if (debug & DEBUG_STATS) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
942 double t2 = get_time();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
943 if (m == 0 && n == 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
944 PySys_WriteStderr("gc: done");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
945 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
946 PySys_WriteStderr(
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
947 "gc: done, "
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
948 "%" PY_FORMAT_SIZE_T "d unreachable, "
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
949 "%" PY_FORMAT_SIZE_T "d uncollectable",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
950 n+m, n);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
951 if (t1 && t2) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
952 PySys_WriteStderr(", %.4fs elapsed", t2-t1);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
953 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
954 PySys_WriteStderr(".\n");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
955 }
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
956
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
957 /* Append instances in the uncollectable set to a Python
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
958 * reachable list of garbage. The programmer has to deal with
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
959 * this if they insist on creating this type of structure.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
960 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
961 (void)handle_finalizers(&finalizers, old);
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
962
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
963 /* Clear free list only during the collection of the highest
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
964 * generation */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
965 if (generation == NUM_GENERATIONS-1) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
966 clear_freelists();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
967 }
45341
65ae68c7aca3 Implemented Martin's suggestion to clear the free lists during the garbage collection of the highest generation.
Christian Heimes <christian@cheimes.de>
parents: 44439
diff changeset
968
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
969 if (PyErr_Occurred()) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
970 if (gc_str == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
971 gc_str = PyString_FromString("garbage collection");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
972 PyErr_WriteUnraisable(gc_str);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
973 Py_FatalError("unexpected exception during garbage collection");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
974 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
975 return n+m;
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
976 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
977
36839
7c1aca8e3d7d Make PyGC_Collect() use Py_ssize_t.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36769
diff changeset
978 static Py_ssize_t
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
979 collect_generations(void)
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
980 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
981 int i;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
982 Py_ssize_t n = 0;
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
983
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
984 /* Find the oldest generation (highest numbered) where the count
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
985 * exceeds the threshold. Objects in the that generation and
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
986 * generations younger than it will be collected. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
987 for (i = NUM_GENERATIONS-1; i >= 0; i--) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
988 if (generations[i].count > generations[i].threshold) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
989 /* Avoid quadratic performance degradation in number
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
990 of tracked objects. See comments at the beginning
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
991 of this file, and issue #4074.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
992 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
993 if (i == NUM_GENERATIONS - 1
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
994 && long_lived_pending < long_lived_total / 4)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
995 continue;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
996 n = collect(i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
997 break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
998 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
999 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1000 return n;
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1001 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1002
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23721
diff changeset
1003 PyDoc_STRVAR(gc_enable__doc__,
13878
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13570
diff changeset
1004 "enable() -> None\n"
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13570
diff changeset
1005 "\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23721
diff changeset
1006 "Enable automatic garbage collection.\n");
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1007
13878
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13570
diff changeset
1008 static PyObject *
28512
174ce4cdec47 Switched from METH_VARARGS to METH_NOARGS for the 7 module functions that
Tim Peters <tim.peters@gmail.com>
parents: 28511
diff changeset
1009 gc_enable(PyObject *self, PyObject *noargs)
13878
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13570
diff changeset
1010 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1011 enabled = 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1012 Py_INCREF(Py_None);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1013 return Py_None;
13878
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13570
diff changeset
1014 }
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13570
diff changeset
1015
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23721
diff changeset
1016 PyDoc_STRVAR(gc_disable__doc__,
13878
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13570
diff changeset
1017 "disable() -> None\n"
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13570
diff changeset
1018 "\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23721
diff changeset
1019 "Disable automatic garbage collection.\n");
13878
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13570
diff changeset
1020
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13570
diff changeset
1021 static PyObject *
28512
174ce4cdec47 Switched from METH_VARARGS to METH_NOARGS for the 7 module functions that
Tim Peters <tim.peters@gmail.com>
parents: 28511
diff changeset
1022 gc_disable(PyObject *self, PyObject *noargs)
13878
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13570
diff changeset
1023 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1024 enabled = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1025 Py_INCREF(Py_None);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1026 return Py_None;
13878
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13570
diff changeset
1027 }
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13570
diff changeset
1028
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23721
diff changeset
1029 PyDoc_STRVAR(gc_isenabled__doc__,
13878
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13570
diff changeset
1030 "isenabled() -> status\n"
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13570
diff changeset
1031 "\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23721
diff changeset
1032 "Returns true if automatic garbage collection is enabled.\n");
13878
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13570
diff changeset
1033
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13570
diff changeset
1034 static PyObject *
28512
174ce4cdec47 Switched from METH_VARARGS to METH_NOARGS for the 7 module functions that
Tim Peters <tim.peters@gmail.com>
parents: 28511
diff changeset
1035 gc_isenabled(PyObject *self, PyObject *noargs)
13878
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13570
diff changeset
1036 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1037 return PyBool_FromLong((long)enabled);
13878
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13570
diff changeset
1038 }
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13570
diff changeset
1039
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23721
diff changeset
1040 PyDoc_STRVAR(gc_collect__doc__,
36863
2d24f3a37a1b SF patch #1443865; gc.get_count() added and optional argument 'generation'
Barry Warsaw <barry@python.org>
parents: 36860
diff changeset
1041 "collect([generation]) -> n\n"
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1042 "\n"
36863
2d24f3a37a1b SF patch #1443865; gc.get_count() added and optional argument 'generation'
Barry Warsaw <barry@python.org>
parents: 36860
diff changeset
1043 "With no arguments, run a full collection. The optional argument\n"
2d24f3a37a1b SF patch #1443865; gc.get_count() added and optional argument 'generation'
Barry Warsaw <barry@python.org>
parents: 36860
diff changeset
1044 "may be an integer specifying which generation to collect. A ValueError\n"
2d24f3a37a1b SF patch #1443865; gc.get_count() added and optional argument 'generation'
Barry Warsaw <barry@python.org>
parents: 36860
diff changeset
1045 "is raised if the generation number is invalid.\n\n"
2d24f3a37a1b SF patch #1443865; gc.get_count() added and optional argument 'generation'
Barry Warsaw <barry@python.org>
parents: 36860
diff changeset
1046 "The number of unreachable objects is returned.\n");
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1047
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1048 static PyObject *
36863
2d24f3a37a1b SF patch #1443865; gc.get_count() added and optional argument 'generation'
Barry Warsaw <barry@python.org>
parents: 36860
diff changeset
1049 gc_collect(PyObject *self, PyObject *args, PyObject *kws)
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1050 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1051 static char *keywords[] = {"generation", NULL};
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1052 int genarg = NUM_GENERATIONS - 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1053 Py_ssize_t n;
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1054
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1055 if (!PyArg_ParseTupleAndKeywords(args, kws, "|i", keywords, &genarg))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1056 return NULL;
36863
2d24f3a37a1b SF patch #1443865; gc.get_count() added and optional argument 'generation'
Barry Warsaw <barry@python.org>
parents: 36860
diff changeset
1057
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1058 else if (genarg < 0 || genarg >= NUM_GENERATIONS) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1059 PyErr_SetString(PyExc_ValueError, "invalid generation");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1060 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1061 }
36863
2d24f3a37a1b SF patch #1443865; gc.get_count() added and optional argument 'generation'
Barry Warsaw <barry@python.org>
parents: 36860
diff changeset
1062
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1063 if (collecting)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1064 n = 0; /* already collecting, don't do anything */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1065 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1066 collecting = 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1067 n = collect(genarg);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1068 collecting = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1069 }
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1070
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1071 return PyInt_FromSsize_t(n);
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1072 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1073
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23721
diff changeset
1074 PyDoc_STRVAR(gc_set_debug__doc__,
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1075 "set_debug(flags) -> None\n"
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1076 "\n"
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1077 "Set the garbage collection debugging flags. Debugging information is\n"
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1078 "written to sys.stderr.\n"
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1079 "\n"
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1080 "flags is an integer and can have the following bits turned on:\n"
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1081 "\n"
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1082 " DEBUG_STATS - Print statistics during collection.\n"
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1083 " DEBUG_COLLECTABLE - Print collectable objects found.\n"
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1084 " DEBUG_UNCOLLECTABLE - Print unreachable but uncollectable objects found.\n"
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1085 " DEBUG_INSTANCES - Print instance objects.\n"
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1086 " DEBUG_OBJECTS - Print objects other than instances.\n"
14756
6e9331e68a3b - Add DEBUG_SAVEALL option. When enabled all garbage objects found by the
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14424
diff changeset
1087 " DEBUG_SAVEALL - Save objects to gc.garbage rather than freeing them.\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23721
diff changeset
1088 " DEBUG_LEAK - Debug leaking programs (everything but STATS).\n");
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1089
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1090 static PyObject *
13878
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13570
diff changeset
1091 gc_set_debug(PyObject *self, PyObject *args)
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1092 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1093 if (!PyArg_ParseTuple(args, "i:set_debug", &debug))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1094 return NULL;
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1095
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1096 Py_INCREF(Py_None);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1097 return Py_None;
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1098 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1099
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23721
diff changeset
1100 PyDoc_STRVAR(gc_get_debug__doc__,
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1101 "get_debug() -> flags\n"
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1102 "\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23721
diff changeset
1103 "Get the garbage collection debugging flags.\n");
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1104
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1105 static PyObject *
28512
174ce4cdec47 Switched from METH_VARARGS to METH_NOARGS for the 7 module functions that
Tim Peters <tim.peters@gmail.com>
parents: 28511
diff changeset
1106 gc_get_debug(PyObject *self, PyObject *noargs)
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1107 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1108 return Py_BuildValue("i", debug);
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1109 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1110
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23721
diff changeset
1111 PyDoc_STRVAR(gc_set_thresh__doc__,
21833
070d392042e3 Fix spelling mistakes. Bugfix candidates.
Neal Norwitz <nnorwitz@gmail.com>
parents: 21139
diff changeset
1112 "set_threshold(threshold0, [threshold1, threshold2]) -> None\n"
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1113 "\n"
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1114 "Sets the collection thresholds. Setting threshold0 to zero disables\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23721
diff changeset
1115 "collection.\n");
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1116
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1117 static PyObject *
13878
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13570
diff changeset
1118 gc_set_thresh(PyObject *self, PyObject *args)
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1119 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1120 int i;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1121 if (!PyArg_ParseTuple(args, "i|ii:set_threshold",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1122 &generations[0].threshold,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1123 &generations[1].threshold,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1124 &generations[2].threshold))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1125 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1126 for (i = 2; i < NUM_GENERATIONS; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1127 /* generations higher than 2 get the same threshold */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1128 generations[i].threshold = generations[2].threshold;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1129 }
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1130
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1131 Py_INCREF(Py_None);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1132 return Py_None;
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1133 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1134
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23721
diff changeset
1135 PyDoc_STRVAR(gc_get_thresh__doc__,
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1136 "get_threshold() -> (threshold0, threshold1, threshold2)\n"
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1137 "\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23721
diff changeset
1138 "Return the current collection thresholds\n");
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1139
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1140 static PyObject *
28512
174ce4cdec47 Switched from METH_VARARGS to METH_NOARGS for the 7 module functions that
Tim Peters <tim.peters@gmail.com>
parents: 28511
diff changeset
1141 gc_get_thresh(PyObject *self, PyObject *noargs)
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1142 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1143 return Py_BuildValue("(iii)",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1144 generations[0].threshold,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1145 generations[1].threshold,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1146 generations[2].threshold);
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1147 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1148
36863
2d24f3a37a1b SF patch #1443865; gc.get_count() added and optional argument 'generation'
Barry Warsaw <barry@python.org>
parents: 36860
diff changeset
1149 PyDoc_STRVAR(gc_get_count__doc__,
2d24f3a37a1b SF patch #1443865; gc.get_count() added and optional argument 'generation'
Barry Warsaw <barry@python.org>
parents: 36860
diff changeset
1150 "get_count() -> (count0, count1, count2)\n"
2d24f3a37a1b SF patch #1443865; gc.get_count() added and optional argument 'generation'
Barry Warsaw <barry@python.org>
parents: 36860
diff changeset
1151 "\n"
2d24f3a37a1b SF patch #1443865; gc.get_count() added and optional argument 'generation'
Barry Warsaw <barry@python.org>
parents: 36860
diff changeset
1152 "Return the current collection counts\n");
2d24f3a37a1b SF patch #1443865; gc.get_count() added and optional argument 'generation'
Barry Warsaw <barry@python.org>
parents: 36860
diff changeset
1153
2d24f3a37a1b SF patch #1443865; gc.get_count() added and optional argument 'generation'
Barry Warsaw <barry@python.org>
parents: 36860
diff changeset
1154 static PyObject *
2d24f3a37a1b SF patch #1443865; gc.get_count() added and optional argument 'generation'
Barry Warsaw <barry@python.org>
parents: 36860
diff changeset
1155 gc_get_count(PyObject *self, PyObject *noargs)
2d24f3a37a1b SF patch #1443865; gc.get_count() added and optional argument 'generation'
Barry Warsaw <barry@python.org>
parents: 36860
diff changeset
1156 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1157 return Py_BuildValue("(iii)",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1158 generations[0].count,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1159 generations[1].count,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1160 generations[2].count);
36863
2d24f3a37a1b SF patch #1443865; gc.get_count() added and optional argument 'generation'
Barry Warsaw <barry@python.org>
parents: 36860
diff changeset
1161 }
2d24f3a37a1b SF patch #1443865; gc.get_count() added and optional argument 'generation'
Barry Warsaw <barry@python.org>
parents: 36860
diff changeset
1162
19096
45ccf3b889b5 Add get_referents function. Closes SF patch #402925.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15013
diff changeset
1163 static int
21038
afb789aada16 Rename get_referents to get_referrers. Fixes #483815.
Martin v. Löwis <martin@v.loewis.de>
parents: 20820
diff changeset
1164 referrersvisit(PyObject* obj, PyObject *objs)
19096
45ccf3b889b5 Add get_referents function. Closes SF patch #402925.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15013
diff changeset
1165 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1166 Py_ssize_t i;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1167 for (i = 0; i < PyTuple_GET_SIZE(objs); i++)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1168 if (PyTuple_GET_ITEM(objs, i) == obj)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1169 return 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1170 return 0;
19096
45ccf3b889b5 Add get_referents function. Closes SF patch #402925.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15013
diff changeset
1171 }
45ccf3b889b5 Add get_referents function. Closes SF patch #402925.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15013
diff changeset
1172
19128
4901a935490d Remove "referents" structure (it's not needed). Check return value
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19097
diff changeset
1173 static int
21038
afb789aada16 Rename get_referents to get_referrers. Fixes #483815.
Martin v. Löwis <martin@v.loewis.de>
parents: 20820
diff changeset
1174 gc_referrers_for(PyObject *objs, PyGC_Head *list, PyObject *resultlist)
19096
45ccf3b889b5 Add get_referents function. Closes SF patch #402925.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15013
diff changeset
1175 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1176 PyGC_Head *gc;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1177 PyObject *obj;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1178 traverseproc traverse;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1179 for (gc = list->gc.gc_next; gc != list; gc = gc->gc.gc_next) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1180 obj = FROM_GC(gc);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1181 traverse = Py_TYPE(obj)->tp_traverse;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1182 if (obj == objs || obj == resultlist)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1183 continue;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1184 if (traverse(obj, (visitproc)referrersvisit, objs)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1185 if (PyList_Append(resultlist, obj) < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1186 return 0; /* error */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1187 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1188 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1189 return 1; /* no error */
19096
45ccf3b889b5 Add get_referents function. Closes SF patch #402925.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15013
diff changeset
1190 }
45ccf3b889b5 Add get_referents function. Closes SF patch #402925.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15013
diff changeset
1191
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23721
diff changeset
1192 PyDoc_STRVAR(gc_get_referrers__doc__,
21038
afb789aada16 Rename get_referents to get_referrers. Fixes #483815.
Martin v. Löwis <martin@v.loewis.de>
parents: 20820
diff changeset
1193 "get_referrers(*objs) -> list\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23721
diff changeset
1194 Return the list of objects that directly refer to any of objs.");
19096
45ccf3b889b5 Add get_referents function. Closes SF patch #402925.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15013
diff changeset
1195
19128
4901a935490d Remove "referents" structure (it's not needed). Check return value
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19097
diff changeset
1196 static PyObject *
21038
afb789aada16 Rename get_referents to get_referrers. Fixes #483815.
Martin v. Löwis <martin@v.loewis.de>
parents: 20820
diff changeset
1197 gc_get_referrers(PyObject *self, PyObject *args)
19096
45ccf3b889b5 Add get_referents function. Closes SF patch #402925.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15013
diff changeset
1198 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1199 int i;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1200 PyObject *result = PyList_New(0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1201 if (!result) return NULL;
37067
1f4549865aad Fix some missing checks after PyTuple_New, PyList_New, PyDict_New
Georg Brandl <georg@python.org>
parents: 36863
diff changeset
1202
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1203 for (i = 0; i < NUM_GENERATIONS; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1204 if (!(gc_referrers_for(args, GEN_HEAD(i), result))) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1205 Py_DECREF(result);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1206 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1207 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1208 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1209 return result;
19096
45ccf3b889b5 Add get_referents function. Closes SF patch #402925.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15013
diff changeset
1210 }
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1211
28522
487df294d328 Finished implementing gc.get_referrents(): dealt with error and end
Tim Peters <tim.peters@gmail.com>
parents: 28521
diff changeset
1212 /* Append obj to list; return true if error (out of memory), false if OK. */
28497
40d9659b56c9 Add get_referrents() helper function.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26138
diff changeset
1213 static int
28523
c01fdc3a9180 s/referrents/referents/g. Gotta love that referrers remains rife with rs.
Tim Peters <tim.peters@gmail.com>
parents: 28522
diff changeset
1214 referentsvisit(PyObject *obj, PyObject *list)
28497
40d9659b56c9 Add get_referrents() helper function.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26138
diff changeset
1215 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1216 return PyList_Append(list, obj) < 0;
28497
40d9659b56c9 Add get_referrents() helper function.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26138
diff changeset
1217 }
40d9659b56c9 Add get_referrents() helper function.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26138
diff changeset
1218
28523
c01fdc3a9180 s/referrents/referents/g. Gotta love that referrers remains rife with rs.
Tim Peters <tim.peters@gmail.com>
parents: 28522
diff changeset
1219 PyDoc_STRVAR(gc_get_referents__doc__,
c01fdc3a9180 s/referrents/referents/g. Gotta love that referrers remains rife with rs.
Tim Peters <tim.peters@gmail.com>
parents: 28522
diff changeset
1220 "get_referents(*objs) -> list\n\
28498
3e399a8618db Add get_referrents() helper function.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28497
diff changeset
1221 Return the list of objects that are directly referred to by objs.");
28497
40d9659b56c9 Add get_referrents() helper function.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26138
diff changeset
1222
40d9659b56c9 Add get_referrents() helper function.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26138
diff changeset
1223 static PyObject *
28523
c01fdc3a9180 s/referrents/referents/g. Gotta love that referrers remains rife with rs.
Tim Peters <tim.peters@gmail.com>
parents: 28522
diff changeset
1224 gc_get_referents(PyObject *self, PyObject *args)
28497
40d9659b56c9 Add get_referrents() helper function.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26138
diff changeset
1225 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1226 Py_ssize_t i;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1227 PyObject *result = PyList_New(0);
28522
487df294d328 Finished implementing gc.get_referrents(): dealt with error and end
Tim Peters <tim.peters@gmail.com>
parents: 28521
diff changeset
1228
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1229 if (result == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1230 return NULL;
28522
487df294d328 Finished implementing gc.get_referrents(): dealt with error and end
Tim Peters <tim.peters@gmail.com>
parents: 28521
diff changeset
1231
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1232 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1233 traverseproc traverse;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1234 PyObject *obj = PyTuple_GET_ITEM(args, i);
28522
487df294d328 Finished implementing gc.get_referrents(): dealt with error and end
Tim Peters <tim.peters@gmail.com>
parents: 28521
diff changeset
1235
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1236 if (! PyObject_IS_GC(obj))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1237 continue;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1238 traverse = Py_TYPE(obj)->tp_traverse;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1239 if (! traverse)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1240 continue;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1241 if (traverse(obj, (visitproc)referentsvisit, result)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1242 Py_DECREF(result);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1243 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1244 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1245 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1246 return result;
28497
40d9659b56c9 Add get_referrents() helper function.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26138
diff changeset
1247 }
40d9659b56c9 Add get_referrents() helper function.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26138
diff changeset
1248
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23721
diff changeset
1249 PyDoc_STRVAR(gc_get_objects__doc__,
19097
9e6f7804ef14 Add get_objects function. This is a low level function (like
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19096
diff changeset
1250 "get_objects() -> [...]\n"
9e6f7804ef14 Add get_objects function. This is a low level function (like
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19096
diff changeset
1251 "\n"
9e6f7804ef14 Add get_objects function. This is a low level function (like
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19096
diff changeset
1252 "Return a list of objects tracked by the collector (excluding the list\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23721
diff changeset
1253 "returned).\n");
19097
9e6f7804ef14 Add get_objects function. This is a low level function (like
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19096
diff changeset
1254
9e6f7804ef14 Add get_objects function. This is a low level function (like
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19096
diff changeset
1255 static PyObject *
28512
174ce4cdec47 Switched from METH_VARARGS to METH_NOARGS for the 7 module functions that
Tim Peters <tim.peters@gmail.com>
parents: 28511
diff changeset
1256 gc_get_objects(PyObject *self, PyObject *noargs)
19097
9e6f7804ef14 Add get_objects function. This is a low level function (like
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19096
diff changeset
1257 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1258 int i;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1259 PyObject* result;
19097
9e6f7804ef14 Add get_objects function. This is a low level function (like
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19096
diff changeset
1260
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1261 result = PyList_New(0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1262 if (result == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1263 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1264 for (i = 0; i < NUM_GENERATIONS; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1265 if (append_objects(result, GEN_HEAD(i))) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1266 Py_DECREF(result);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1267 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1268 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1269 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1270 return result;
19097
9e6f7804ef14 Add get_objects function. This is a low level function (like
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19096
diff changeset
1271 }
9e6f7804ef14 Add get_objects function. This is a low level function (like
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19096
diff changeset
1272
52288
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 51032
diff changeset
1273 PyDoc_STRVAR(gc_is_tracked__doc__,
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 51032
diff changeset
1274 "is_tracked(obj) -> bool\n"
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 51032
diff changeset
1275 "\n"
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 51032
diff changeset
1276 "Returns true if the object is tracked by the garbage collector.\n"
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 51032
diff changeset
1277 "Simple atomic objects will return false.\n"
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 51032
diff changeset
1278 );
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 51032
diff changeset
1279
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 51032
diff changeset
1280 static PyObject *
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 51032
diff changeset
1281 gc_is_tracked(PyObject *self, PyObject *obj)
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 51032
diff changeset
1282 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1283 PyObject *result;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1284
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1285 if (PyObject_IS_GC(obj) && IS_TRACKED(obj))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1286 result = Py_True;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1287 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1288 result = Py_False;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1289 Py_INCREF(result);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1290 return result;
52288
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 51032
diff changeset
1291 }
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 51032
diff changeset
1292
19097
9e6f7804ef14 Add get_objects function. This is a low level function (like
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19096
diff changeset
1293
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23721
diff changeset
1294 PyDoc_STRVAR(gc__doc__,
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1295 "This module provides access to the garbage collector for reference cycles.\n"
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1296 "\n"
13878
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13570
diff changeset
1297 "enable() -- Enable automatic garbage collection.\n"
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13570
diff changeset
1298 "disable() -- Disable automatic garbage collection.\n"
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13570
diff changeset
1299 "isenabled() -- Returns true if automatic collection is enabled.\n"
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1300 "collect() -- Do a full collection right now.\n"
40074
f46badc8dc21 List gc.get_count() in the module docstring.
Barry Warsaw <barry@python.org>
parents: 38314
diff changeset
1301 "get_count() -- Return the current collection counts.\n"
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1302 "set_debug() -- Set debugging flags.\n"
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1303 "get_debug() -- Get debugging flags.\n"
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1304 "set_threshold() -- Set the collection thresholds.\n"
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1305 "get_threshold() -- Return the current the collection thresholds.\n"
19097
9e6f7804ef14 Add get_objects function. This is a low level function (like
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19096
diff changeset
1306 "get_objects() -- Return a list of all objects tracked by the collector.\n"
52288
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 51032
diff changeset
1307 "is_tracked() -- Returns true if a given object is tracked.\n"
28497
40d9659b56c9 Add get_referrents() helper function.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26138
diff changeset
1308 "get_referrers() -- Return the list of objects that refer to an object.\n"
28523
c01fdc3a9180 s/referrents/referents/g. Gotta love that referrers remains rife with rs.
Tim Peters <tim.peters@gmail.com>
parents: 28522
diff changeset
1309 "get_referents() -- Return the list of objects that an object refers to.\n");
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1310
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1311 static PyMethodDef GcMethods[] = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1312 {"enable", gc_enable, METH_NOARGS, gc_enable__doc__},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1313 {"disable", gc_disable, METH_NOARGS, gc_disable__doc__},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1314 {"isenabled", gc_isenabled, METH_NOARGS, gc_isenabled__doc__},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1315 {"set_debug", gc_set_debug, METH_VARARGS, gc_set_debug__doc__},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1316 {"get_debug", gc_get_debug, METH_NOARGS, gc_get_debug__doc__},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1317 {"get_count", gc_get_count, METH_NOARGS, gc_get_count__doc__},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1318 {"set_threshold", gc_set_thresh, METH_VARARGS, gc_set_thresh__doc__},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1319 {"get_threshold", gc_get_thresh, METH_NOARGS, gc_get_thresh__doc__},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1320 {"collect", (PyCFunction)gc_collect,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1321 METH_VARARGS | METH_KEYWORDS, gc_collect__doc__},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1322 {"get_objects", gc_get_objects,METH_NOARGS, gc_get_objects__doc__},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1323 {"is_tracked", gc_is_tracked, METH_O, gc_is_tracked__doc__},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1324 {"get_referrers", gc_get_referrers, METH_VARARGS,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1325 gc_get_referrers__doc__},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1326 {"get_referents", gc_get_referents, METH_VARARGS,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1327 gc_get_referents__doc__},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1328 {NULL, NULL} /* Sentinel */
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1329 };
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1330
30311
f003e48f562d Bug #794140: cygwin builds do not embed
Jason Tishler <jason@tishler.net>
parents: 28651
diff changeset
1331 PyMODINIT_FUNC
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1332 initgc(void)
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1333 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1334 PyObject *m;
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1335
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1336 m = Py_InitModule4("gc",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1337 GcMethods,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1338 gc__doc__,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1339 NULL,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1340 PYTHON_API_VERSION);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1341 if (m == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1342 return;
28516
78f846fb570a initgc(): Rewrote to use the PyModule_AddXYZ API; cuts code size.
Tim Peters <tim.peters@gmail.com>
parents: 28515
diff changeset
1343
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1344 if (garbage == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1345 garbage = PyList_New(0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1346 if (garbage == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1347 return;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1348 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1349 Py_INCREF(garbage);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1350 if (PyModule_AddObject(m, "garbage", garbage) < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1351 return;
37987
06c059b92d0b Patch from Aldo Cortesi (OpenBSD buildbot owner).
Neal Norwitz <nnorwitz@gmail.com>
parents: 37872
diff changeset
1352
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1353 /* Importing can't be done in collect() because collect()
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1354 * can be called via PyGC_Collect() in Py_Finalize().
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1355 * This wouldn't be a problem, except that <initialized> is
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1356 * reset to 0 before calling collect which trips up
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1357 * the import and triggers an assertion.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1358 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1359 if (tmod == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1360 tmod = PyImport_ImportModuleNoBlock("time");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1361 if (tmod == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1362 PyErr_Clear();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1363 }
37987
06c059b92d0b Patch from Aldo Cortesi (OpenBSD buildbot owner).
Neal Norwitz <nnorwitz@gmail.com>
parents: 37872
diff changeset
1364
28516
78f846fb570a initgc(): Rewrote to use the PyModule_AddXYZ API; cuts code size.
Tim Peters <tim.peters@gmail.com>
parents: 28515
diff changeset
1365 #define ADD_INT(NAME) if (PyModule_AddIntConstant(m, #NAME, NAME) < 0) return
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1366 ADD_INT(DEBUG_STATS);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1367 ADD_INT(DEBUG_COLLECTABLE);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1368 ADD_INT(DEBUG_UNCOLLECTABLE);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1369 ADD_INT(DEBUG_INSTANCES);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1370 ADD_INT(DEBUG_OBJECTS);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1371 ADD_INT(DEBUG_SAVEALL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1372 ADD_INT(DEBUG_LEAK);
28516
78f846fb570a initgc(): Rewrote to use the PyModule_AddXYZ API; cuts code size.
Tim Peters <tim.peters@gmail.com>
parents: 28515
diff changeset
1373 #undef ADD_INT
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1374 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1375
28651
7fda09f751ad - New C API PyGC_Collect(), same as calling gc.collect().
Guido van Rossum <guido@python.org>
parents: 28523
diff changeset
1376 /* API to invoke gc.collect() from C */
36839
7c1aca8e3d7d Make PyGC_Collect() use Py_ssize_t.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36769
diff changeset
1377 Py_ssize_t
28651
7fda09f751ad - New C API PyGC_Collect(), same as calling gc.collect().
Guido van Rossum <guido@python.org>
parents: 28523
diff changeset
1378 PyGC_Collect(void)
7fda09f751ad - New C API PyGC_Collect(), same as calling gc.collect().
Guido van Rossum <guido@python.org>
parents: 28523
diff changeset
1379 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1380 Py_ssize_t n;
28651
7fda09f751ad - New C API PyGC_Collect(), same as calling gc.collect().
Guido van Rossum <guido@python.org>
parents: 28523
diff changeset
1381
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1382 if (collecting)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1383 n = 0; /* already collecting, don't do anything */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1384 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1385 collecting = 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1386 n = collect(NUM_GENERATIONS - 1);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1387 collecting = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1388 }
28651
7fda09f751ad - New C API PyGC_Collect(), same as calling gc.collect().
Guido van Rossum <guido@python.org>
parents: 28523
diff changeset
1389
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1390 return n;
28651
7fda09f751ad - New C API PyGC_Collect(), same as calling gc.collect().
Guido van Rossum <guido@python.org>
parents: 28523
diff changeset
1391 }
7fda09f751ad - New C API PyGC_Collect(), same as calling gc.collect().
Guido van Rossum <guido@python.org>
parents: 28523
diff changeset
1392
19585
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1393 /* for debugging */
28651
7fda09f751ad - New C API PyGC_Collect(), same as calling gc.collect().
Guido van Rossum <guido@python.org>
parents: 28523
diff changeset
1394 void
7fda09f751ad - New C API PyGC_Collect(), same as calling gc.collect().
Guido van Rossum <guido@python.org>
parents: 28523
diff changeset
1395 _PyGC_Dump(PyGC_Head *g)
19585
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1396 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1397 _PyObject_Dump(FROM_GC(g));
19585
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1398 }
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1399
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1400 /* extension modules might be compiled with GC support so these
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1401 functions must always be available */
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1402
22972
95819f7f0bcd Allow PyObject_Del to be used as a function designator. Provide binary
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22648
diff changeset
1403 #undef PyObject_GC_Track
95819f7f0bcd Allow PyObject_Del to be used as a function designator. Provide binary
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22648
diff changeset
1404 #undef PyObject_GC_UnTrack
95819f7f0bcd Allow PyObject_Del to be used as a function designator. Provide binary
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22648
diff changeset
1405 #undef PyObject_GC_Del
95819f7f0bcd Allow PyObject_Del to be used as a function designator. Provide binary
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22648
diff changeset
1406 #undef _PyObject_GC_Malloc
95819f7f0bcd Allow PyObject_Del to be used as a function designator. Provide binary
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22648
diff changeset
1407
19585
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1408 void
22972
95819f7f0bcd Allow PyObject_Del to be used as a function designator. Provide binary
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22648
diff changeset
1409 PyObject_GC_Track(void *op)
19585
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1410 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1411 _PyObject_GC_TRACK(op);
19585
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1412 }
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1413
22972
95819f7f0bcd Allow PyObject_Del to be used as a function designator. Provide binary
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22648
diff changeset
1414 /* for binary compatibility with 2.2 */
19585
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1415 void
22972
95819f7f0bcd Allow PyObject_Del to be used as a function designator. Provide binary
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22648
diff changeset
1416 _PyObject_GC_Track(PyObject *op)
95819f7f0bcd Allow PyObject_Del to be used as a function designator. Provide binary
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22648
diff changeset
1417 {
95819f7f0bcd Allow PyObject_Del to be used as a function designator. Provide binary
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22648
diff changeset
1418 PyObject_GC_Track(op);
95819f7f0bcd Allow PyObject_Del to be used as a function designator. Provide binary
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22648
diff changeset
1419 }
95819f7f0bcd Allow PyObject_Del to be used as a function designator. Provide binary
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22648
diff changeset
1420
95819f7f0bcd Allow PyObject_Del to be used as a function designator. Provide binary
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22648
diff changeset
1421 void
95819f7f0bcd Allow PyObject_Del to be used as a function designator. Provide binary
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22648
diff changeset
1422 PyObject_GC_UnTrack(void *op)
19585
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1423 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1424 /* Obscure: the Py_TRASHCAN mechanism requires that we be able to
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1425 * call PyObject_GC_UnTrack twice on an object.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1426 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1427 if (IS_TRACKED(op))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1428 _PyObject_GC_UNTRACK(op);
19585
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1429 }
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1430
22972
95819f7f0bcd Allow PyObject_Del to be used as a function designator. Provide binary
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22648
diff changeset
1431 /* for binary compatibility with 2.2 */
95819f7f0bcd Allow PyObject_Del to be used as a function designator. Provide binary
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22648
diff changeset
1432 void
95819f7f0bcd Allow PyObject_Del to be used as a function designator. Provide binary
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22648
diff changeset
1433 _PyObject_GC_UnTrack(PyObject *op)
95819f7f0bcd Allow PyObject_Del to be used as a function designator. Provide binary
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22648
diff changeset
1434 {
95819f7f0bcd Allow PyObject_Del to be used as a function designator. Provide binary
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22648
diff changeset
1435 PyObject_GC_UnTrack(op);
95819f7f0bcd Allow PyObject_Del to be used as a function designator. Provide binary
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22648
diff changeset
1436 }
95819f7f0bcd Allow PyObject_Del to be used as a function designator. Provide binary
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22648
diff changeset
1437
19585
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1438 PyObject *
22972
95819f7f0bcd Allow PyObject_Del to be used as a function designator. Provide binary
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22648
diff changeset
1439 _PyObject_GC_Malloc(size_t basicsize)
19585
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1440 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1441 PyObject *op;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1442 PyGC_Head *g;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1443 if (basicsize > PY_SSIZE_T_MAX - sizeof(PyGC_Head))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1444 return PyErr_NoMemory();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1445 g = (PyGC_Head *)PyObject_MALLOC(
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1446 sizeof(PyGC_Head) + basicsize);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1447 if (g == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1448 return PyErr_NoMemory();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1449 g->gc.gc_refs = GC_UNTRACKED;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1450 generations[0].count++; /* number of allocated GC objects */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1451 if (generations[0].count > generations[0].threshold &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1452 enabled &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1453 generations[0].threshold &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1454 !collecting &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1455 !PyErr_Occurred()) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1456 collecting = 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1457 collect_generations();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1458 collecting = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1459 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1460 op = FROM_GC(g);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1461 return op;
19585
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1462 }
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1463
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1464 PyObject *
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1465 _PyObject_GC_New(PyTypeObject *tp)
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1466 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1467 PyObject *op = _PyObject_GC_Malloc(_PyObject_SIZE(tp));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1468 if (op != NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1469 op = PyObject_INIT(op, tp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1470 return op;
19585
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1471 }
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1472
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1473 PyVarObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1474 _PyObject_GC_NewVar(PyTypeObject *tp, Py_ssize_t nitems)
19585
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1475 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1476 const size_t size = _PyObject_VAR_SIZE(tp, nitems);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1477 PyVarObject *op = (PyVarObject *) _PyObject_GC_Malloc(size);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1478 if (op != NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1479 op = PyObject_INIT_VAR(op, tp, nitems);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1480 return op;
19585
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1481 }
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1482
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1483 PyVarObject *
36524
4c8606191c0c Change _PyObject_GC_Resize to expect Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
1484 _PyObject_GC_Resize(PyVarObject *op, Py_ssize_t nitems)
19585
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1485 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1486 const size_t basicsize = _PyObject_VAR_SIZE(Py_TYPE(op), nitems);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1487 PyGC_Head *g = AS_GC(op);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1488 if (basicsize > PY_SSIZE_T_MAX - sizeof(PyGC_Head))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1489 return (PyVarObject *)PyErr_NoMemory();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1490 g = (PyGC_Head *)PyObject_REALLOC(g, sizeof(PyGC_Head) + basicsize);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1491 if (g == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1492 return (PyVarObject *)PyErr_NoMemory();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1493 op = (PyVarObject *) FROM_GC(g);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1494 Py_SIZE(op) = nitems;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1495 return op;
19585
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1496 }
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1497
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1498 void
22972
95819f7f0bcd Allow PyObject_Del to be used as a function designator. Provide binary
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22648
diff changeset
1499 PyObject_GC_Del(void *op)
19585
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1500 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1501 PyGC_Head *g = AS_GC(op);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1502 if (IS_TRACKED(op))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1503 gc_list_remove(g);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1504 if (generations[0].count > 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1505 generations[0].count--;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1506 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61164
diff changeset
1507 PyObject_FREE(g);
19585
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1508 }
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1509
22972
95819f7f0bcd Allow PyObject_Del to be used as a function designator. Provide binary
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22648
diff changeset
1510 /* for binary compatibility with 2.2 */
95819f7f0bcd Allow PyObject_Del to be used as a function designator. Provide binary
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22648
diff changeset
1511 #undef _PyObject_GC_Del
95819f7f0bcd Allow PyObject_Del to be used as a function designator. Provide binary
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22648
diff changeset
1512 void
95819f7f0bcd Allow PyObject_Del to be used as a function designator. Provide binary
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22648
diff changeset
1513 _PyObject_GC_Del(PyObject *op)
95819f7f0bcd Allow PyObject_Del to be used as a function designator. Provide binary
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22648
diff changeset
1514 {
95819f7f0bcd Allow PyObject_Del to be used as a function designator. Provide binary
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22648
diff changeset
1515 PyObject_GC_Del(op);
95819f7f0bcd Allow PyObject_Del to be used as a function designator. Provide binary
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22648
diff changeset
1516 }