annotate Modules/gcmodule.c @ 58765:2ba7677bfa95 legacy-trunk

testCopysign was defined twice in test_math; combine the definitions
author Mark Dickinson <dickinsm@gmail.com>
date Sat, 06 Feb 2010 23:11:25 +0000
parents 8e82fb043ae8
children b3c3b12460d4
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"
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
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 {
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
33 PyGC_Head head;
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
34 int threshold; /* collection threshold */
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
35 int count; /* count of allocations or collections of younger
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
36 generations */
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] = {
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
44 /* PyGC_Head, threshold, count */
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
45 {{{GEN_HEAD(0), GEN_HEAD(0), 0}}, 700, 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
46 {{{GEN_HEAD(1), GEN_HEAD(1), 0}}, 10, 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
47 {{{GEN_HEAD(2), GEN_HEAD(2), 0}}, 10, 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
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
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
89 long_lived_pending / long_lived_total
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:
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
111 http://mail.python.org/pipermail/python-dev/2008-June/080579.html
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 */
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
116 #define DEBUG_STATS (1<<0) /* print collection statistics */
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
117 #define DEBUG_COLLECTABLE (1<<1) /* print collectable objects */
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
118 #define DEBUG_UNCOLLECTABLE (1<<2) /* print uncollectable objects */
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
119 #define DEBUG_INSTANCES (1<<3) /* print instances */
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
120 #define DEBUG_OBJECTS (1<<4) /* print other objects */
14756
6e9331e68a3b - Add DEBUG_SAVEALL option. When enabled all garbage objects found by the
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14424
diff changeset
121 #define DEBUG_SAVEALL (1<<5) /* save all garbage in gc.garbage */
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
122 #define DEBUG_LEAK DEBUG_COLLECTABLE | \
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
123 DEBUG_UNCOLLECTABLE | \
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
124 DEBUG_INSTANCES | \
14756
6e9331e68a3b - Add DEBUG_SAVEALL option. When enabled all garbage objects found by the
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14424
diff changeset
125 DEBUG_OBJECTS | \
6e9331e68a3b - Add DEBUG_SAVEALL option. When enabled all garbage objects found by the
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14424
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 */
24077
a5162fd65c3d Reserved another gc_refs value for untracked objects. Every live gc
Tim Peters <tim.peters@gmail.com>
parents: 24075
diff changeset
169 #define GC_UNTRACKED _PyGC_REFS_UNTRACKED
a5162fd65c3d Reserved another gc_refs value for untracked objects. Every live gc
Tim Peters <tim.peters@gmail.com>
parents: 24075
diff changeset
170 #define GC_REACHABLE _PyGC_REFS_REACHABLE
a5162fd65c3d Reserved another gc_refs value for untracked objects. Every live gc
Tim Peters <tim.peters@gmail.com>
parents: 24075
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) ( \
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
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 {
20381
b4f829941f3d SF bug [#467145] Python 2.2a4 build problem on HPUX 11.0.
Tim Peters <tim.peters@gmail.com>
parents: 20296
diff changeset
183 list->gc.gc_prev = list;
b4f829941f3d SF bug [#467145] Python 2.2a4 build problem on HPUX 11.0.
Tim Peters <tim.peters@gmail.com>
parents: 20296
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 {
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
190 return (list->gc.gc_next == 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
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 {
20381
b4f829941f3d SF bug [#467145] Python 2.2a4 build problem on HPUX 11.0.
Tim Peters <tim.peters@gmail.com>
parents: 20296
diff changeset
199 node->gc.gc_next = list;
b4f829941f3d SF bug [#467145] Python 2.2a4 build problem on HPUX 11.0.
Tim Peters <tim.peters@gmail.com>
parents: 20296
diff changeset
200 node->gc.gc_prev = list->gc.gc_prev;
b4f829941f3d SF bug [#467145] Python 2.2a4 build problem on HPUX 11.0.
Tim Peters <tim.peters@gmail.com>
parents: 20296
diff changeset
201 node->gc.gc_prev->gc.gc_next = node;
b4f829941f3d SF bug [#467145] Python 2.2a4 build problem on HPUX 11.0.
Tim Peters <tim.peters@gmail.com>
parents: 20296
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 {
20381
b4f829941f3d SF bug [#467145] Python 2.2a4 build problem on HPUX 11.0.
Tim Peters <tim.peters@gmail.com>
parents: 20296
diff changeset
210 node->gc.gc_prev->gc.gc_next = node->gc.gc_next;
b4f829941f3d SF bug [#467145] Python 2.2a4 build problem on HPUX 11.0.
Tim Peters <tim.peters@gmail.com>
parents: 20296
diff changeset
211 node->gc.gc_next->gc.gc_prev = node->gc.gc_prev;
b4f829941f3d SF bug [#467145] Python 2.2a4 build problem on HPUX 11.0.
Tim Peters <tim.peters@gmail.com>
parents: 20296
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 {
33876
633cadebf68d gc_list_move(): Make this truly equivalent to remove+append. While
Tim Peters <tim.peters@gmail.com>
parents: 33869
diff changeset
222 PyGC_Head *new_prev;
33869
223d20afc8b5 gc list function cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 33867
diff changeset
223 PyGC_Head *current_prev = node->gc.gc_prev;
223d20afc8b5 gc list function cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 33867
diff changeset
224 PyGC_Head *current_next = node->gc.gc_next;
33876
633cadebf68d gc_list_move(): Make this truly equivalent to remove+append. While
Tim Peters <tim.peters@gmail.com>
parents: 33869
diff changeset
225 /* Unlink from current list. */
33869
223d20afc8b5 gc list function cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 33867
diff changeset
226 current_prev->gc.gc_next = current_next;
223d20afc8b5 gc list function cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 33867
diff changeset
227 current_next->gc.gc_prev = current_prev;
33876
633cadebf68d gc_list_move(): Make this truly equivalent to remove+append. While
Tim Peters <tim.peters@gmail.com>
parents: 33869
diff changeset
228 /* Relink at end of new list. */
633cadebf68d gc_list_move(): Make this truly equivalent to remove+append. While
Tim Peters <tim.peters@gmail.com>
parents: 33869
diff changeset
229 new_prev = node->gc.gc_prev = list->gc.gc_prev;
633cadebf68d gc_list_move(): Make this truly equivalent to remove+append. While
Tim Peters <tim.peters@gmail.com>
parents: 33869
diff changeset
230 new_prev->gc.gc_next = list->gc.gc_prev = node;
33869
223d20afc8b5 gc list function cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 33867
diff changeset
231 node->gc.gc_next = list;
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 {
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
238 PyGC_Head *tail;
33869
223d20afc8b5 gc list function cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 33867
diff changeset
239 assert(from != to);
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
240 if (!gc_list_is_empty(from)) {
20381
b4f829941f3d SF bug [#467145] Python 2.2a4 build problem on HPUX 11.0.
Tim Peters <tim.peters@gmail.com>
parents: 20296
diff changeset
241 tail = to->gc.gc_prev;
b4f829941f3d SF bug [#467145] Python 2.2a4 build problem on HPUX 11.0.
Tim Peters <tim.peters@gmail.com>
parents: 20296
diff changeset
242 tail->gc.gc_next = from->gc.gc_next;
b4f829941f3d SF bug [#467145] Python 2.2a4 build problem on HPUX 11.0.
Tim Peters <tim.peters@gmail.com>
parents: 20296
diff changeset
243 tail->gc.gc_next->gc.gc_prev = tail;
b4f829941f3d SF bug [#467145] Python 2.2a4 build problem on HPUX 11.0.
Tim Peters <tim.peters@gmail.com>
parents: 20296
diff changeset
244 to->gc.gc_prev = from->gc.gc_prev;
b4f829941f3d SF bug [#467145] Python 2.2a4 build problem on HPUX 11.0.
Tim Peters <tim.peters@gmail.com>
parents: 20296
diff changeset
245 to->gc.gc_prev->gc.gc_next = to;
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
246 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
247 gc_list_init(from);
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 {
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
253 PyGC_Head *gc;
36839
7c1aca8e3d7d Make PyGC_Collect() use Py_ssize_t.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36769
diff changeset
254 Py_ssize_t n = 0;
20381
b4f829941f3d SF bug [#467145] Python 2.2a4 build problem on HPUX 11.0.
Tim Peters <tim.peters@gmail.com>
parents: 20296
diff changeset
255 for (gc = list->gc.gc_next; gc != list; gc = gc->gc.gc_next) {
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
256 n++;
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
257 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
258 return n;
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 {
4c596dd60c22 handle_finalizers(): Rewrote to call append_objects() and gc_list_merge()
Tim Peters <tim.peters@gmail.com>
parents: 28512
diff changeset
267 PyGC_Head *gc;
4c596dd60c22 handle_finalizers(): Rewrote to call append_objects() and gc_list_merge()
Tim Peters <tim.peters@gmail.com>
parents: 28512
diff changeset
268 for (gc = gc_list->gc.gc_next; gc != gc_list; gc = gc->gc.gc_next) {
4c596dd60c22 handle_finalizers(): Rewrote to call append_objects() and gc_list_merge()
Tim Peters <tim.peters@gmail.com>
parents: 28512
diff changeset
269 PyObject *op = FROM_GC(gc);
4c596dd60c22 handle_finalizers(): Rewrote to call append_objects() and gc_list_merge()
Tim Peters <tim.peters@gmail.com>
parents: 28512
diff changeset
270 if (op != py_list) {
4c596dd60c22 handle_finalizers(): Rewrote to call append_objects() and gc_list_merge()
Tim Peters <tim.peters@gmail.com>
parents: 28512
diff changeset
271 if (PyList_Append(py_list, op)) {
4c596dd60c22 handle_finalizers(): Rewrote to call append_objects() and gc_list_merge()
Tim Peters <tim.peters@gmail.com>
parents: 28512
diff changeset
272 return -1; /* exception */
4c596dd60c22 handle_finalizers(): Rewrote to call append_objects() and gc_list_merge()
Tim Peters <tim.peters@gmail.com>
parents: 28512
diff changeset
273 }
4c596dd60c22 handle_finalizers(): Rewrote to call append_objects() and gc_list_merge()
Tim Peters <tim.peters@gmail.com>
parents: 28512
diff changeset
274 }
4c596dd60c22 handle_finalizers(): Rewrote to call append_objects() and gc_list_merge()
Tim Peters <tim.peters@gmail.com>
parents: 28512
diff changeset
275 }
4c596dd60c22 handle_finalizers(): Rewrote to call append_objects() and gc_list_merge()
Tim Peters <tim.peters@gmail.com>
parents: 28512
diff changeset
276 return 0;
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 {
20381
b4f829941f3d SF bug [#467145] Python 2.2a4 build problem on HPUX 11.0.
Tim Peters <tim.peters@gmail.com>
parents: 20296
diff changeset
289 PyGC_Head *gc = containers->gc.gc_next;
24077
a5162fd65c3d Reserved another gc_refs value for untracked objects. Every live gc
Tim Peters <tim.peters@gmail.com>
parents: 24075
diff changeset
290 for (; gc != containers; gc = gc->gc.gc_next) {
a5162fd65c3d Reserved another gc_refs value for untracked objects. Every live gc
Tim Peters <tim.peters@gmail.com>
parents: 24075
diff changeset
291 assert(gc->gc.gc_refs == GC_REACHABLE);
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 42333
diff changeset
292 gc->gc.gc_refs = Py_REFCNT(FROM_GC(gc));
30918
8aa9237f6dd6 update_refs(): assert that incoming refcounts aren't 0. The comment
Tim Peters <tim.peters@gmail.com>
parents: 30311
diff changeset
293 /* Python's cyclic gc should never see an incoming refcount
8aa9237f6dd6 update_refs(): assert that incoming refcounts aren't 0. The comment
Tim Peters <tim.peters@gmail.com>
parents: 30311
diff changeset
294 * of 0: if something decref'ed to 0, it should have been
8aa9237f6dd6 update_refs(): assert that incoming refcounts aren't 0. The comment
Tim Peters <tim.peters@gmail.com>
parents: 30311
diff changeset
295 * deallocated immediately at that time.
8aa9237f6dd6 update_refs(): assert that incoming refcounts aren't 0. The comment
Tim Peters <tim.peters@gmail.com>
parents: 30311
diff changeset
296 * Possible cause (if the assert triggers): a tp_dealloc
8aa9237f6dd6 update_refs(): assert that incoming refcounts aren't 0. The comment
Tim Peters <tim.peters@gmail.com>
parents: 30311
diff changeset
297 * routine left a gc-aware object tracked during its teardown
8aa9237f6dd6 update_refs(): assert that incoming refcounts aren't 0. The comment
Tim Peters <tim.peters@gmail.com>
parents: 30311
diff changeset
298 * phase, and did something-- or allowed something to happen --
8aa9237f6dd6 update_refs(): assert that incoming refcounts aren't 0. The comment
Tim Peters <tim.peters@gmail.com>
parents: 30311
diff changeset
299 * that called back into Python. gc can trigger then, and may
8aa9237f6dd6 update_refs(): assert that incoming refcounts aren't 0. The comment
Tim Peters <tim.peters@gmail.com>
parents: 30311
diff changeset
300 * see the still-tracked dying object. Before this assert
8aa9237f6dd6 update_refs(): assert that incoming refcounts aren't 0. The comment
Tim Peters <tim.peters@gmail.com>
parents: 30311
diff changeset
301 * was added, such mistakes went on to allow gc to try to
8aa9237f6dd6 update_refs(): assert that incoming refcounts aren't 0. The comment
Tim Peters <tim.peters@gmail.com>
parents: 30311
diff changeset
302 * delete the object again. In a debug build, that caused
8aa9237f6dd6 update_refs(): assert that incoming refcounts aren't 0. The comment
Tim Peters <tim.peters@gmail.com>
parents: 30311
diff changeset
303 * a mysterious segfault, when _Py_ForgetReference tried
8aa9237f6dd6 update_refs(): assert that incoming refcounts aren't 0. The comment
Tim Peters <tim.peters@gmail.com>
parents: 30311
diff changeset
304 * to remove the object from the doubly-linked list of all
8aa9237f6dd6 update_refs(): assert that incoming refcounts aren't 0. The comment
Tim Peters <tim.peters@gmail.com>
parents: 30311
diff changeset
305 * objects a second time. In a release build, an actual
8aa9237f6dd6 update_refs(): assert that incoming refcounts aren't 0. The comment
Tim Peters <tim.peters@gmail.com>
parents: 30311
diff changeset
306 * double deallocation occurred, which leads to corruption
8aa9237f6dd6 update_refs(): assert that incoming refcounts aren't 0. The comment
Tim Peters <tim.peters@gmail.com>
parents: 30311
diff changeset
307 * of the allocator's internal bookkeeping pointers. That's
8aa9237f6dd6 update_refs(): assert that incoming refcounts aren't 0. The comment
Tim Peters <tim.peters@gmail.com>
parents: 30311
diff changeset
308 * so serious that maybe this should be a release-build
8aa9237f6dd6 update_refs(): assert that incoming refcounts aren't 0. The comment
Tim Peters <tim.peters@gmail.com>
parents: 30311
diff changeset
309 * check instead of an assert?
8aa9237f6dd6 update_refs(): assert that incoming refcounts aren't 0. The comment
Tim Peters <tim.peters@gmail.com>
parents: 30311
diff changeset
310 */
8aa9237f6dd6 update_refs(): assert that incoming refcounts aren't 0. The comment
Tim Peters <tim.peters@gmail.com>
parents: 30311
diff changeset
311 assert(gc->gc.gc_refs != 0);
24077
a5162fd65c3d Reserved another gc_refs value for untracked objects. Every live gc
Tim Peters <tim.peters@gmail.com>
parents: 24075
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 {
24074
a1805a54666f visit_decref(): Two optimizations.
Tim Peters <tim.peters@gmail.com>
parents: 24072
diff changeset
319 assert(op != NULL);
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
320 if (PyObject_IS_GC(op)) {
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
321 PyGC_Head *gc = AS_GC(op);
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
322 /* We're only interested in gc_refs for objects in the
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
323 * generation being collected, which can be recognized
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
324 * because only they have positive gc_refs.
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
325 */
24097
f7abbd15b4f4 visit_decref(): Added another assert.
Tim Peters <tim.peters@gmail.com>
parents: 24085
diff changeset
326 assert(gc->gc.gc_refs != 0); /* else refcount was too small */
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
327 if (gc->gc.gc_refs > 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
328 gc->gc.gc_refs--;
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
329 }
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
330 return 0;
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 {
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
341 traverseproc traverse;
20381
b4f829941f3d SF bug [#467145] Python 2.2a4 build problem on HPUX 11.0.
Tim Peters <tim.peters@gmail.com>
parents: 20296
diff changeset
342 PyGC_Head *gc = containers->gc.gc_next;
b4f829941f3d SF bug [#467145] Python 2.2a4 build problem on HPUX 11.0.
Tim Peters <tim.peters@gmail.com>
parents: 20296
diff changeset
343 for (; gc != containers; gc=gc->gc.gc_next) {
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 42333
diff changeset
344 traverse = Py_TYPE(FROM_GC(gc))->tp_traverse;
19585
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
345 (void) traverse(FROM_GC(gc),
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
346 (visitproc)visit_decref,
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
347 NULL);
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
348 }
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 {
24077
a5162fd65c3d Reserved another gc_refs value for untracked objects. Every live gc
Tim Peters <tim.peters@gmail.com>
parents: 24075
diff changeset
355 if (PyObject_IS_GC(op)) {
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
356 PyGC_Head *gc = AS_GC(op);
36769
ab1b53d54901 Change GC refcount to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 36524
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
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
359 if (gc_refs == 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
360 /* This is in move_unreachable's 'young' list, but
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
361 * the traversal hasn't yet gotten to it. All
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
362 * we need to do is tell move_unreachable that it's
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
363 * 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
364 */
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
365 gc->gc.gc_refs = 1;
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
366 }
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
367 else if (gc_refs == GC_TENTATIVELY_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
368 /* This had gc_refs = 0 when move_unreachable got
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
369 * to it, but turns out it's reachable after all.
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
370 * Move it back to move_unreachable's 'young' list,
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
371 * and move_unreachable will eventually get to it
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
372 * again.
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
373 */
33869
223d20afc8b5 gc list function cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 33867
diff changeset
374 gc_list_move(gc, reachable);
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
375 gc->gc.gc_refs = 1;
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
376 }
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
377 /* Else there's nothing to do.
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
378 * If gc_refs > 0, it must be in move_unreachable's 'young'
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
379 * list, and move_unreachable will eventually get to it.
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
380 * If gc_refs == GC_REACHABLE, it's either in some other
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
381 * generation so we don't care about it, or move_unreachable
24085
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
382 * already dealt with it.
24077
a5162fd65c3d Reserved another gc_refs value for untracked objects. Every live gc
Tim Peters <tim.peters@gmail.com>
parents: 24075
diff changeset
383 * If gc_refs == GC_UNTRACKED, it must be ignored.
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
384 */
24077
a5162fd65c3d Reserved another gc_refs value for untracked objects. Every live gc
Tim Peters <tim.peters@gmail.com>
parents: 24075
diff changeset
385 else {
a5162fd65c3d Reserved another gc_refs value for untracked objects. Every live gc
Tim Peters <tim.peters@gmail.com>
parents: 24075
diff changeset
386 assert(gc_refs > 0
a5162fd65c3d Reserved another gc_refs value for untracked objects. Every live gc
Tim Peters <tim.peters@gmail.com>
parents: 24075
diff changeset
387 || gc_refs == GC_REACHABLE
a5162fd65c3d Reserved another gc_refs value for untracked objects. Every live gc
Tim Peters <tim.peters@gmail.com>
parents: 24075
diff changeset
388 || gc_refs == GC_UNTRACKED);
a5162fd65c3d Reserved another gc_refs value for untracked objects. Every live gc
Tim Peters <tim.peters@gmail.com>
parents: 24075
diff changeset
389 }
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
390 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
391 return 0;
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 {
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
405 PyGC_Head *gc = young->gc.gc_next;
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
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
407 /* Invariants: all objects "to the left" of us in young have gc_refs
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
408 * = GC_REACHABLE, and are indeed reachable (directly or indirectly)
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
409 * from outside the young list as it was at entry. All other 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
410 * from the original young "to the left" of us are in unreachable now,
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
411 * and have gc_refs = GC_TENTATIVELY_UNREACHABLE. All objects to the
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
412 * left of us in 'young' now have been scanned, and no objects 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
413 * or to the right have been scanned yet.
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
414 */
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
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
416 while (gc != young) {
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
417 PyGC_Head *next;
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
24085
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
419 if (gc->gc.gc_refs) {
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
420 /* gc is definitely reachable from outside the
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
421 * original 'young'. Mark it as such, and traverse
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
422 * its pointers to find any other objects that may
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
423 * be directly reachable from it. Note that the
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
424 * call to tp_traverse may append objects to young,
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
425 * so we have to wait until it returns to determine
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
426 * the next object to visit.
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
427 */
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
428 PyObject *op = FROM_GC(gc);
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 42333
diff changeset
429 traverseproc traverse = Py_TYPE(op)->tp_traverse;
24085
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
430 assert(gc->gc.gc_refs > 0);
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
431 gc->gc.gc_refs = GC_REACHABLE;
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
432 (void) traverse(op,
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
433 (visitproc)visit_reachable,
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
434 (void *)young);
52288
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 51032
diff changeset
435 next = gc->gc.gc_next;
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 51032
diff changeset
436 if (PyTuple_CheckExact(op)) {
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 51032
diff changeset
437 _PyTuple_MaybeUntrack(op);
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 51032
diff changeset
438 }
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 51032
diff changeset
439 else if (PyDict_CheckExact(op)) {
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 51032
diff changeset
440 _PyDict_MaybeUntrack(op);
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 51032
diff changeset
441 }
24085
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
442 }
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
443 else {
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
444 /* This *may* be unreachable. To make progress,
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
445 * assume it is. gc isn't directly reachable from
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
446 * any object we've already traversed, but may be
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
447 * reachable from an object we haven't gotten to yet.
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
448 * visit_reachable will eventually move gc back into
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
449 * young if that's so, and we'll see it again.
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
450 */
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
451 next = gc->gc.gc_next;
33869
223d20afc8b5 gc list function cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 33867
diff changeset
452 gc_list_move(gc, 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
453 gc->gc.gc_refs = GC_TENTATIVELY_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
454 }
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
455 gc = next;
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
456 }
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 {
28508
8233a0294c41 New comments. Rewrote has_finalizer() as a sequence of ifs instead of
Tim Peters <tim.peters@gmail.com>
parents: 28507
diff changeset
469 if (PyInstance_Check(op)) {
8233a0294c41 New comments. Rewrote has_finalizer() as a sequence of ifs instead of
Tim Peters <tim.peters@gmail.com>
parents: 28507
diff changeset
470 assert(delstr != NULL);
28520
6653a56002ef Reworked has_finalizer() to use the new _PyObject_Lookup() instead
Tim Peters <tim.peters@gmail.com>
parents: 28516
diff changeset
471 return _PyInstance_Lookup(op, delstr) != NULL;
28508
8233a0294c41 New comments. Rewrote has_finalizer() as a sequence of ifs instead of
Tim Peters <tim.peters@gmail.com>
parents: 28507
diff changeset
472 }
37564
c782cf6b516e SF Patch #1463867: Improved generator finalization to allow generators
Phillip J. Eby <pje@telecommunity.com>
parents: 37515
diff changeset
473 else if (PyType_HasFeature(op->ob_type, Py_TPFLAGS_HEAPTYPE))
c782cf6b516e SF Patch #1463867: Improved generator finalization to allow generators
Phillip J. Eby <pje@telecommunity.com>
parents: 37515
diff changeset
474 return op->ob_type->tp_del != NULL;
c782cf6b516e SF Patch #1463867: Improved generator finalization to allow generators
Phillip J. Eby <pje@telecommunity.com>
parents: 37515
diff changeset
475 else if (PyGen_CheckExact(op))
c782cf6b516e SF Patch #1463867: Improved generator finalization to allow generators
Phillip J. Eby <pje@telecommunity.com>
parents: 37515
diff changeset
476 return PyGen_NeedsFinalizing((PyGenObject *)op);
37290
89142cadcfb9 In format strings slinging Py_ssize_t, unconditionally
Tim Peters <tim.peters@gmail.com>
parents: 37067
diff changeset
477 else
37564
c782cf6b516e SF Patch #1463867: Improved generator finalization to allow generators
Phillip J. Eby <pje@telecommunity.com>
parents: 37515
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 {
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
488 PyGC_Head *gc;
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
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
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
491 /* March over unreachable. Move objects with finalizers into
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
492 * `finalizers`.
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
493 */
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
494 for (gc = unreachable->gc.gc_next; gc != unreachable; gc = next) {
19585
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
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
28510
0de0ded19968 move_finalizers(): Rewrote. It's not necessary for this routine
Tim Peters <tim.peters@gmail.com>
parents: 28508
diff changeset
497 assert(IS_TENTATIVELY_UNREACHABLE(op));
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
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
28520
6653a56002ef Reworked has_finalizer() to use the new _PyObject_Lookup() instead
Tim Peters <tim.peters@gmail.com>
parents: 28516
diff changeset
500 if (has_finalizer(op)) {
33869
223d20afc8b5 gc list function cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 33867
diff changeset
501 gc_list_move(gc, finalizers);
28520
6653a56002ef Reworked has_finalizer() to use the new _PyObject_Lookup() instead
Tim Peters <tim.peters@gmail.com>
parents: 28516
diff changeset
502 gc->gc.gc_refs = GC_REACHABLE;
28504
cb5db5561cdc Fix Tim's boom example.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28498
diff changeset
503 }
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
504 }
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 {
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
511 if (PyObject_IS_GC(op)) {
24077
a5162fd65c3d Reserved another gc_refs value for untracked objects. Every live gc
Tim Peters <tim.peters@gmail.com>
parents: 24075
diff changeset
512 if (IS_TENTATIVELY_UNREACHABLE(op)) {
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
513 PyGC_Head *gc = AS_GC(op);
33869
223d20afc8b5 gc list function cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 33867
diff changeset
514 gc_list_move(gc, tolist);
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
515 gc->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
516 }
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
517 }
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
518 return 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
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 {
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
527 traverseproc traverse;
20381
b4f829941f3d SF bug [#467145] Python 2.2a4 build problem on HPUX 11.0.
Tim Peters <tim.peters@gmail.com>
parents: 20296
diff changeset
528 PyGC_Head *gc = finalizers->gc.gc_next;
28511
acf3596fcc28 Reworked move_finalizer_reachable() to create two distinct lists:
Tim Peters <tim.peters@gmail.com>
parents: 28510
diff changeset
529 for (; gc != finalizers; gc = gc->gc.gc_next) {
acf3596fcc28 Reworked move_finalizer_reachable() to create two distinct lists:
Tim Peters <tim.peters@gmail.com>
parents: 28510
diff changeset
530 /* Note that the finalizers list may grow during this. */
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 42333
diff changeset
531 traverse = Py_TYPE(FROM_GC(gc))->tp_traverse;
24072
86b3760d285d SF bug #574132: Major GC related performance regression
Tim Peters <tim.peters@gmail.com>
parents: 24036
diff changeset
532 (void) traverse(FROM_GC(gc),
28511
acf3596fcc28 Reworked move_finalizer_reachable() to create two distinct lists:
Tim Peters <tim.peters@gmail.com>
parents: 28510
diff changeset
533 (visitproc)visit_move,
28520
6653a56002ef Reworked has_finalizer() to use the new _PyObject_Lookup() instead
Tim Peters <tim.peters@gmail.com>
parents: 28516
diff changeset
534 (void *)finalizers);
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
535 }
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 {
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
552 PyGC_Head *gc;
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
553 PyObject *op; /* generally FROM_GC(gc) */
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
554 PyWeakReference *wr; /* generally a cast of op */
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
555 PyGC_Head wrcb_to_call; /* weakrefs with callbacks to call */
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
556 PyGC_Head *next;
30988
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30918
diff changeset
557 int num_freed = 0;
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30918
diff changeset
558
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
559 gc_list_init(&wrcb_to_call);
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
560
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
561 /* Clear all weakrefs to the objects in unreachable. If such a weakref
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
562 * also has a callback, move it into `wrcb_to_call` if the callback
33867
07d94a162cf8 handle_weakrefs(): Simplification -- there's no need to make a second
Tim Peters <tim.peters@gmail.com>
parents: 33862
diff changeset
563 * needs to be invoked. Note that we cannot invoke any callbacks until
07d94a162cf8 handle_weakrefs(): Simplification -- there's no need to make a second
Tim Peters <tim.peters@gmail.com>
parents: 33862
diff changeset
564 * all weakrefs to unreachable objects are cleared, lest the callback
07d94a162cf8 handle_weakrefs(): Simplification -- there's no need to make a second
Tim Peters <tim.peters@gmail.com>
parents: 33862
diff changeset
565 * resurrect an unreachable object via a still-active weakref. We
07d94a162cf8 handle_weakrefs(): Simplification -- there's no need to make a second
Tim Peters <tim.peters@gmail.com>
parents: 33862
diff changeset
566 * make another pass over wrcb_to_call, invoking callbacks, after this
07d94a162cf8 handle_weakrefs(): Simplification -- there's no need to make a second
Tim Peters <tim.peters@gmail.com>
parents: 33862
diff changeset
567 * pass completes.
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
568 */
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
569 for (gc = unreachable->gc.gc_next; gc != unreachable; gc = next) {
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
570 PyWeakReference **wrlist;
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
571
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
572 op = FROM_GC(gc);
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
573 assert(IS_TENTATIVELY_UNREACHABLE(op));
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
574 next = gc->gc.gc_next;
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
575
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 42333
diff changeset
576 if (! PyType_SUPPORTS_WEAKREFS(Py_TYPE(op)))
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
577 continue;
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
578
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
579 /* It supports weakrefs. Does it have any? */
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
580 wrlist = (PyWeakReference **)
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
581 PyObject_GET_WEAKREFS_LISTPTR(op);
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
582
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
583 /* `op` may have some weakrefs. March over the list, clear
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
584 * all the weakrefs, and move the weakrefs with callbacks
33867
07d94a162cf8 handle_weakrefs(): Simplification -- there's no need to make a second
Tim Peters <tim.peters@gmail.com>
parents: 33862
diff changeset
585 * that must be called into wrcb_to_call.
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
586 */
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
587 for (wr = *wrlist; wr != NULL; wr = *wrlist) {
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
588 PyGC_Head *wrasgc; /* AS_GC(wr) */
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
589
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
590 /* _PyWeakref_ClearRef clears the weakref but leaves
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
591 * the callback pointer intact. Obscure: it also
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
592 * changes *wrlist.
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
593 */
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
594 assert(wr->wr_object == op);
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
595 _PyWeakref_ClearRef(wr);
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
596 assert(wr->wr_object == Py_None);
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
597 if (wr->wr_callback == NULL)
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
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
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
600 /* Headache time. `op` is going away, and is weakly referenced by
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
601 * `wr`, which has a callback. Should the callback be invoked? If wr
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
602 * is also trash, no:
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
603 *
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
604 * 1. There's no need to call it. The object and the weakref are
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
605 * both going away, so it's legitimate to pretend the weakref is
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
606 * going away first. The user has to ensure a weakref outlives its
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
607 * referent if they want a guarantee that the wr callback will get
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
608 * invoked.
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
609 *
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
610 * 2. It may be catastrophic to call it. If the callback is also in
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
611 * cyclic trash (CT), then although the CT is unreachable from
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
612 * outside the current generation, CT may be reachable from the
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
613 * callback. Then the callback could resurrect insane objects.
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
614 *
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
615 * Since the callback is never needed and may be unsafe in this case,
33867
07d94a162cf8 handle_weakrefs(): Simplification -- there's no need to make a second
Tim Peters <tim.peters@gmail.com>
parents: 33862
diff changeset
616 * wr is simply left in the unreachable set. Note that because we
07d94a162cf8 handle_weakrefs(): Simplification -- there's no need to make a second
Tim Peters <tim.peters@gmail.com>
parents: 33862
diff changeset
617 * already called _PyWeakref_ClearRef(wr), its callback will never
07d94a162cf8 handle_weakrefs(): Simplification -- there's no need to make a second
Tim Peters <tim.peters@gmail.com>
parents: 33862
diff changeset
618 * trigger.
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
619 *
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
620 * OTOH, if wr isn't part of CT, we should invoke the callback: the
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
621 * weakref outlived the trash. Note that since wr isn't CT in this
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
622 * case, its callback can't be CT either -- wr acted as an external
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
623 * root to this generation, and therefore its callback did too. So
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
624 * nothing in CT is reachable from the callback either, so it's hard
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
625 * to imagine how calling it later could create a problem for us. wr
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
626 * is moved to wrcb_to_call in this case.
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
627 */
33867
07d94a162cf8 handle_weakrefs(): Simplification -- there's no need to make a second
Tim Peters <tim.peters@gmail.com>
parents: 33862
diff changeset
628 if (IS_TENTATIVELY_UNREACHABLE(wr))
07d94a162cf8 handle_weakrefs(): Simplification -- there's no need to make a second
Tim Peters <tim.peters@gmail.com>
parents: 33862
diff changeset
629 continue;
07d94a162cf8 handle_weakrefs(): Simplification -- there's no need to make a second
Tim Peters <tim.peters@gmail.com>
parents: 33862
diff changeset
630 assert(IS_REACHABLE(wr));
07d94a162cf8 handle_weakrefs(): Simplification -- there's no need to make a second
Tim Peters <tim.peters@gmail.com>
parents: 33862
diff changeset
631
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
632 /* Create a new reference so that wr can't go away
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
633 * before we can process it again.
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
634 */
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
635 Py_INCREF(wr);
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
636
33867
07d94a162cf8 handle_weakrefs(): Simplification -- there's no need to make a second
Tim Peters <tim.peters@gmail.com>
parents: 33862
diff changeset
637 /* Move wr to wrcb_to_call, for the next pass. */
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
638 wrasgc = AS_GC(wr);
33867
07d94a162cf8 handle_weakrefs(): Simplification -- there's no need to make a second
Tim Peters <tim.peters@gmail.com>
parents: 33862
diff changeset
639 assert(wrasgc != next); /* wrasgc is reachable, but
07d94a162cf8 handle_weakrefs(): Simplification -- there's no need to make a second
Tim Peters <tim.peters@gmail.com>
parents: 33862
diff changeset
640 next isn't, so they can't
07d94a162cf8 handle_weakrefs(): Simplification -- there's no need to make a second
Tim Peters <tim.peters@gmail.com>
parents: 33862
diff changeset
641 be the same */
33869
223d20afc8b5 gc list function cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 33867
diff changeset
642 gc_list_move(wrasgc, &wrcb_to_call);
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
643 }
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
644 }
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
645
33867
07d94a162cf8 handle_weakrefs(): Simplification -- there's no need to make a second
Tim Peters <tim.peters@gmail.com>
parents: 33862
diff changeset
646 /* Invoke the callbacks we decided to honor. It's safe to invoke them
07d94a162cf8 handle_weakrefs(): Simplification -- there's no need to make a second
Tim Peters <tim.peters@gmail.com>
parents: 33862
diff changeset
647 * because they can't reference unreachable objects.
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
648 */
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
649 while (! gc_list_is_empty(&wrcb_to_call)) {
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
650 PyObject *temp;
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
651 PyObject *callback;
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
652
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
653 gc = wrcb_to_call.gc.gc_next;
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
654 op = FROM_GC(gc);
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
655 assert(IS_REACHABLE(op));
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
656 assert(PyWeakref_Check(op));
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
657 wr = (PyWeakReference *)op;
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
658 callback = wr->wr_callback;
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
659 assert(callback != NULL);
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
660
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
661 /* copy-paste of weakrefobject.c's handle_callback() */
38314
1fdf29a88f0e Replace PyObject_CallFunction calls with only object args
Georg Brandl <georg@python.org>
parents: 37987
diff changeset
662 temp = PyObject_CallFunctionObjArgs(callback, wr, NULL);
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
663 if (temp == NULL)
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
664 PyErr_WriteUnraisable(callback);
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
665 else
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
666 Py_DECREF(temp);
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
667
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
668 /* Give up the reference we created in the first pass. When
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
669 * op's refcount hits 0 (which it may or may not do right now),
33867
07d94a162cf8 handle_weakrefs(): Simplification -- there's no need to make a second
Tim Peters <tim.peters@gmail.com>
parents: 33862
diff changeset
670 * op's tp_dealloc will decref op->wr_callback too. Note
07d94a162cf8 handle_weakrefs(): Simplification -- there's no need to make a second
Tim Peters <tim.peters@gmail.com>
parents: 33862
diff changeset
671 * that the refcount probably will hit 0 now, and because this
07d94a162cf8 handle_weakrefs(): Simplification -- there's no need to make a second
Tim Peters <tim.peters@gmail.com>
parents: 33862
diff changeset
672 * weakref was reachable to begin with, gc didn't already
07d94a162cf8 handle_weakrefs(): Simplification -- there's no need to make a second
Tim Peters <tim.peters@gmail.com>
parents: 33862
diff changeset
673 * add it to its count of freed objects. Example: a reachable
07d94a162cf8 handle_weakrefs(): Simplification -- there's no need to make a second
Tim Peters <tim.peters@gmail.com>
parents: 33862
diff changeset
674 * weak value dict maps some key to this reachable weakref.
07d94a162cf8 handle_weakrefs(): Simplification -- there's no need to make a second
Tim Peters <tim.peters@gmail.com>
parents: 33862
diff changeset
675 * The callback removes this key->weakref mapping from the
07d94a162cf8 handle_weakrefs(): Simplification -- there's no need to make a second
Tim Peters <tim.peters@gmail.com>
parents: 33862
diff changeset
676 * dict, leaving no other references to the weakref (excepting
07d94a162cf8 handle_weakrefs(): Simplification -- there's no need to make a second
Tim Peters <tim.peters@gmail.com>
parents: 33862
diff changeset
677 * ours).
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
678 */
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
679 Py_DECREF(op);
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
680 if (wrcb_to_call.gc.gc_next == gc) {
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
681 /* object is still alive -- move it */
33869
223d20afc8b5 gc list function cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 33867
diff changeset
682 gc_list_move(gc, old);
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
683 }
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
684 else
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
685 ++num_freed;
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
686 }
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
687
30988
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30918
diff changeset
688 return num_freed;
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 {
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
694 char *cname;
20818
9aded6235d89 Add has_finalizer predictate function. Use it when deciding which
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20810
diff changeset
695 /* simple version of instance_repr */
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
696 PyObject *classname = inst->in_class->cl_name;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
697 if (classname != NULL && PyString_Check(classname))
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
698 cname = PyString_AsString(classname);
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
699 else
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
700 cname = "?";
14294
053627e7ac14 apply patch #101362 by Vladimir Marangozov
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13878
diff changeset
701 PySys_WriteStderr("gc: %.100s <%.100s instance at %p>\n",
053627e7ac14 apply patch #101362 by Vladimir Marangozov
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13878
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 {
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
708 if ((debug & DEBUG_INSTANCES) && PyInstance_Check(op)) {
14294
053627e7ac14 apply patch #101362 by Vladimir Marangozov
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13878
diff changeset
709 debug_instance(msg, (PyInstanceObject *)op);
14756
6e9331e68a3b - Add DEBUG_SAVEALL option. When enabled all garbage objects found by the
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14424
diff changeset
710 }
6e9331e68a3b - Add DEBUG_SAVEALL option. When enabled all garbage objects found by the
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14424
diff changeset
711 else if (debug & DEBUG_OBJECTS) {
14294
053627e7ac14 apply patch #101362 by Vladimir Marangozov
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13878
diff changeset
712 PySys_WriteStderr("gc: %.100s <%.100s %p>\n",
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 42333
diff changeset
713 msg, Py_TYPE(op)->tp_name, op);
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
714 }
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 {
28520
6653a56002ef Reworked has_finalizer() to use the new _PyObject_Lookup() instead
Tim Peters <tim.peters@gmail.com>
parents: 28516
diff changeset
729 PyGC_Head *gc = finalizers->gc.gc_next;
6653a56002ef Reworked has_finalizer() to use the new _PyObject_Lookup() instead
Tim Peters <tim.peters@gmail.com>
parents: 28516
diff changeset
730
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
731 if (garbage == NULL) {
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
732 garbage = PyList_New(0);
28511
acf3596fcc28 Reworked move_finalizer_reachable() to create two distinct lists:
Tim Peters <tim.peters@gmail.com>
parents: 28510
diff changeset
733 if (garbage == NULL)
acf3596fcc28 Reworked move_finalizer_reachable() to create two distinct lists:
Tim Peters <tim.peters@gmail.com>
parents: 28510
diff changeset
734 Py_FatalError("gc couldn't create gc.garbage list");
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
735 }
28520
6653a56002ef Reworked has_finalizer() to use the new _PyObject_Lookup() instead
Tim Peters <tim.peters@gmail.com>
parents: 28516
diff changeset
736 for (; gc != finalizers; gc = gc->gc.gc_next) {
6653a56002ef Reworked has_finalizer() to use the new _PyObject_Lookup() instead
Tim Peters <tim.peters@gmail.com>
parents: 28516
diff changeset
737 PyObject *op = FROM_GC(gc);
6653a56002ef Reworked has_finalizer() to use the new _PyObject_Lookup() instead
Tim Peters <tim.peters@gmail.com>
parents: 28516
diff changeset
738
6653a56002ef Reworked has_finalizer() to use the new _PyObject_Lookup() instead
Tim Peters <tim.peters@gmail.com>
parents: 28516
diff changeset
739 if ((debug & DEBUG_SAVEALL) || has_finalizer(op)) {
6653a56002ef Reworked has_finalizer() to use the new _PyObject_Lookup() instead
Tim Peters <tim.peters@gmail.com>
parents: 28516
diff changeset
740 if (PyList_Append(garbage, op) < 0)
6653a56002ef Reworked has_finalizer() to use the new _PyObject_Lookup() instead
Tim Peters <tim.peters@gmail.com>
parents: 28516
diff changeset
741 return -1;
6653a56002ef Reworked has_finalizer() to use the new _PyObject_Lookup() instead
Tim Peters <tim.peters@gmail.com>
parents: 28516
diff changeset
742 }
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
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
28515
4c596dd60c22 handle_finalizers(): Rewrote to call append_objects() and gc_list_merge()
Tim Peters <tim.peters@gmail.com>
parents: 28512
diff changeset
745 gc_list_merge(finalizers, old);
4c596dd60c22 handle_finalizers(): Rewrote to call append_objects() and gc_list_merge()
Tim Peters <tim.peters@gmail.com>
parents: 28512
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
14756
6e9331e68a3b - Add DEBUG_SAVEALL option. When enabled all garbage objects found by the
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14424
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 {
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
756 inquiry clear;
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
757
28504
cb5db5561cdc Fix Tim's boom example.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28498
diff changeset
758 while (!gc_list_is_empty(collectable)) {
cb5db5561cdc Fix Tim's boom example.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28498
diff changeset
759 PyGC_Head *gc = collectable->gc.gc_next;
19585
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
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
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
762 assert(IS_TENTATIVELY_UNREACHABLE(op));
14756
6e9331e68a3b - Add DEBUG_SAVEALL option. When enabled all garbage objects found by the
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14424
diff changeset
763 if (debug & DEBUG_SAVEALL) {
6e9331e68a3b - Add DEBUG_SAVEALL option. When enabled all garbage objects found by the
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14424
diff changeset
764 PyList_Append(garbage, op);
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
765 }
14756
6e9331e68a3b - Add DEBUG_SAVEALL option. When enabled all garbage objects found by the
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14424
diff changeset
766 else {
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 42333
diff changeset
767 if ((clear = Py_TYPE(op)->tp_clear) != NULL) {
14756
6e9331e68a3b - Add DEBUG_SAVEALL option. When enabled all garbage objects found by the
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14424
diff changeset
768 Py_INCREF(op);
23721
48e937e1b3fe Remove casts to PyObject * when declaration is for PyObject *
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23495
diff changeset
769 clear(op);
14756
6e9331e68a3b - Add DEBUG_SAVEALL option. When enabled all garbage objects found by the
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14424
diff changeset
770 Py_DECREF(op);
6e9331e68a3b - Add DEBUG_SAVEALL option. When enabled all garbage objects found by the
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14424
diff changeset
771 }
6e9331e68a3b - Add DEBUG_SAVEALL option. When enabled all garbage objects found by the
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14424
diff changeset
772 }
28504
cb5db5561cdc Fix Tim's boom example.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28498
diff changeset
773 if (collectable->gc.gc_next == gc) {
14756
6e9331e68a3b - Add DEBUG_SAVEALL option. When enabled all garbage objects found by the
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14424
diff changeset
774 /* object is still alive, move it, it may die later */
33869
223d20afc8b5 gc list function cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 33867
diff changeset
775 gc_list_move(gc, old);
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
776 gc->gc.gc_refs = GC_REACHABLE;
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
777 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
778 }
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 {
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
789 (void)PyMethod_ClearFreeList();
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
790 (void)PyFrame_ClearFreeList();
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
791 (void)PyCFunction_ClearFreeList();
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
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
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
794 (void)PyUnicode_ClearFreeList();
51032
551257c8058a fix building the core with --disable-unicode
Benjamin Peterson <benjamin@python.org>
parents: 50676
diff changeset
795 #endif
47897
0738119c1ef8 - Issue #2862: Make int and float freelist management consistent with other
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
796 (void)PyInt_ClearFreeList();
0738119c1ef8 - Issue #2862: Make int and float freelist management consistent with other
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
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 {
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
803 double result = 0;
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
804 if (tmod != NULL) {
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
805 PyObject *f = PyObject_CallMethod(tmod, "time", NULL);
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
806 if (f == NULL) {
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
807 PyErr_Clear();
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
808 }
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
809 else {
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
810 if (PyFloat_Check(f))
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
811 result = PyFloat_AsDouble(f);
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
812 Py_DECREF(f);
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
813 }
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
814 }
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
815 return result;
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 {
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
823 int i;
36839
7c1aca8e3d7d Make PyGC_Collect() use Py_ssize_t.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36769
diff changeset
824 Py_ssize_t m = 0; /* # objects collected */
7c1aca8e3d7d Make PyGC_Collect() use Py_ssize_t.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36769
diff changeset
825 Py_ssize_t n = 0; /* # unreachable objects that couldn't be collected */
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
826 PyGC_Head *young; /* the generation we are examining */
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
827 PyGC_Head *old; /* next older generation */
30988
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30918
diff changeset
828 PyGC_Head unreachable; /* non-problematic unreachable trash */
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30918
diff changeset
829 PyGC_Head finalizers; /* objects with, & reachable from, __del__ */
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
830 PyGC_Head *gc;
37872
b0f88143dc5c This is a long-ago patch I submitted to SF (1100924) to time the gc passes.
Skip Montanaro <skip@pobox.com>
parents: 37607
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
28507
a0503bdb0c24 Fixed new seemingly random segfaults, by moving the initialization of
Tim Peters <tim.peters@gmail.com>
parents: 28504
diff changeset
833 if (delstr == NULL) {
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
834 delstr = PyString_InternFromString("__del__");
28507
a0503bdb0c24 Fixed new seemingly random segfaults, by moving the initialization of
Tim Peters <tim.peters@gmail.com>
parents: 28504
diff changeset
835 if (delstr == NULL)
a0503bdb0c24 Fixed new seemingly random segfaults, by moving the initialization of
Tim Peters <tim.peters@gmail.com>
parents: 28504
diff changeset
836 Py_FatalError("gc couldn't allocate \"__del__\"");
a0503bdb0c24 Fixed new seemingly random segfaults, by moving the initialization of
Tim Peters <tim.peters@gmail.com>
parents: 28504
diff changeset
837 }
a0503bdb0c24 Fixed new seemingly random segfaults, by moving the initialization of
Tim Peters <tim.peters@gmail.com>
parents: 28504
diff changeset
838
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
839 if (debug & DEBUG_STATS) {
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
840 t1 = get_time();
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
841 PySys_WriteStderr("gc: collecting generation %d...\n",
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
842 generation);
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
843 PySys_WriteStderr("gc: objects in each generation:");
37290
89142cadcfb9 In format strings slinging Py_ssize_t, unconditionally
Tim Peters <tim.peters@gmail.com>
parents: 37067
diff changeset
844 for (i = 0; i < NUM_GENERATIONS; i++)
89142cadcfb9 In format strings slinging Py_ssize_t, unconditionally
Tim Peters <tim.peters@gmail.com>
parents: 37067
diff changeset
845 PySys_WriteStderr(" %" PY_FORMAT_SIZE_T "d",
89142cadcfb9 In format strings slinging Py_ssize_t, unconditionally
Tim Peters <tim.peters@gmail.com>
parents: 37067
diff changeset
846 gc_list_size(GEN_HEAD(i)));
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
847 PySys_WriteStderr("\n");
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
848 }
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
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
850 /* update collection and allocation counters */
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
851 if (generation+1 < NUM_GENERATIONS)
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
852 generations[generation+1].count += 1;
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
853 for (i = 0; i <= generation; i++)
24036
eae496efa460 Fix small bug. The count of objects in all generations younger then the
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23832
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
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
856 /* merge younger generations with one we are currently collecting */
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
857 for (i = 0; i < generation; i++) {
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
858 gc_list_merge(GEN_HEAD(i), GEN_HEAD(generation));
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
859 }
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
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
861 /* handy references */
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
862 young = GEN_HEAD(generation);
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
863 if (generation < NUM_GENERATIONS-1)
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
864 old = GEN_HEAD(generation+1);
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
865 else
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
866 old = young;
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
867
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
868 /* Using ob_refcnt and gc_refs, calculate which objects in the
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
869 * container set are reachable from outside the set (i.e., have a
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
870 * refcount greater than 0 when all the references within the
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
871 * set are taken into account).
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
872 */
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
873 update_refs(young);
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
874 subtract_refs(young);
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
875
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
876 /* Leave everything reachable from outside young in young, and move
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
877 * everything else (in young) to 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
878 * NOTE: This used to move the reachable objects into a 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
879 * set instead. But most things usually turn out to be 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
880 * so it's more efficient to move the unreachable things.
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
881 */
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
882 gc_list_init(&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
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
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
885 /* Move reachable objects to next generation. */
50676
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
886 if (young != old) {
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
887 if (generation == NUM_GENERATIONS - 2) {
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
888 long_lived_pending += gc_list_size(young);
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
889 }
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
890 gc_list_merge(young, old);
50676
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
891 }
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
892 else {
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
893 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
894 long_lived_total = gc_list_size(young);
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
895 }
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
896
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
897 /* All objects in unreachable are trash, but objects reachable from
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
898 * finalizers can't safely be deleted. Python programmers should take
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
899 * care not to create such things. For Python, finalizers means
30988
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30918
diff changeset
900 * instance objects with __del__ methods. Weakrefs with callbacks
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
901 * can also call arbitrary Python code but they will be dealt with by
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
902 * handle_weakrefs().
28520
6653a56002ef Reworked has_finalizer() to use the new _PyObject_Lookup() instead
Tim Peters <tim.peters@gmail.com>
parents: 28516
diff changeset
903 */
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
904 gc_list_init(&finalizers);
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
905 move_finalizers(&unreachable, &finalizers);
28511
acf3596fcc28 Reworked move_finalizer_reachable() to create two distinct lists:
Tim Peters <tim.peters@gmail.com>
parents: 28510
diff changeset
906 /* finalizers contains the unreachable objects with a finalizer;
30988
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30918
diff changeset
907 * unreachable objects reachable *from* those are also uncollectable,
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30918
diff changeset
908 * and we move those into the finalizers list too.
28511
acf3596fcc28 Reworked move_finalizer_reachable() to create two distinct lists:
Tim Peters <tim.peters@gmail.com>
parents: 28510
diff changeset
909 */
28520
6653a56002ef Reworked has_finalizer() to use the new _PyObject_Lookup() instead
Tim Peters <tim.peters@gmail.com>
parents: 28516
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
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
912 /* Collect statistics on collectable objects found and print
30988
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30918
diff changeset
913 * debugging information.
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30918
diff changeset
914 */
28520
6653a56002ef Reworked has_finalizer() to use the new _PyObject_Lookup() instead
Tim Peters <tim.peters@gmail.com>
parents: 28516
diff changeset
915 for (gc = unreachable.gc.gc_next; gc != &unreachable;
20381
b4f829941f3d SF bug [#467145] Python 2.2a4 build problem on HPUX 11.0.
Tim Peters <tim.peters@gmail.com>
parents: 20296
diff changeset
916 gc = gc->gc.gc_next) {
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
917 m++;
14294
053627e7ac14 apply patch #101362 by Vladimir Marangozov
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13878
diff changeset
918 if (debug & DEBUG_COLLECTABLE) {
19585
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
919 debug_cycle("collectable", FROM_GC(gc));
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
920 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
921 }
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
922
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
923 /* Clear weakrefs and invoke callbacks as necessary. */
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
924 m += handle_weakrefs(&unreachable, old);
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
925
28521
964bd97ac1f5 Comment repair; no semantic changes.
Tim Peters <tim.peters@gmail.com>
parents: 28520
diff changeset
926 /* Call tp_clear on objects in the unreachable set. This will cause
964bd97ac1f5 Comment repair; no semantic changes.
Tim Peters <tim.peters@gmail.com>
parents: 28520
diff changeset
927 * the reference cycles to be broken. It may also cause some objects
964bd97ac1f5 Comment repair; no semantic changes.
Tim Peters <tim.peters@gmail.com>
parents: 28520
diff changeset
928 * in finalizers to be freed.
964bd97ac1f5 Comment repair; no semantic changes.
Tim Peters <tim.peters@gmail.com>
parents: 28520
diff changeset
929 */
28520
6653a56002ef Reworked has_finalizer() to use the new _PyObject_Lookup() instead
Tim Peters <tim.peters@gmail.com>
parents: 28516
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
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
932 /* Collect statistics on uncollectable objects found and print
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
933 * debugging information. */
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
934 for (gc = finalizers.gc.gc_next;
28511
acf3596fcc28 Reworked move_finalizer_reachable() to create two distinct lists:
Tim Peters <tim.peters@gmail.com>
parents: 28510
diff changeset
935 gc != &finalizers;
acf3596fcc28 Reworked move_finalizer_reachable() to create two distinct lists:
Tim Peters <tim.peters@gmail.com>
parents: 28510
diff changeset
936 gc = gc->gc.gc_next) {
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
937 n++;
28511
acf3596fcc28 Reworked move_finalizer_reachable() to create two distinct lists:
Tim Peters <tim.peters@gmail.com>
parents: 28510
diff changeset
938 if (debug & DEBUG_UNCOLLECTABLE)
19585
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
939 debug_cycle("uncollectable", FROM_GC(gc));
28511
acf3596fcc28 Reworked move_finalizer_reachable() to create two distinct lists:
Tim Peters <tim.peters@gmail.com>
parents: 28510
diff changeset
940 }
14294
053627e7ac14 apply patch #101362 by Vladimir Marangozov
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13878
diff changeset
941 if (debug & DEBUG_STATS) {
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
942 double t2 = get_time();
37290
89142cadcfb9 In format strings slinging Py_ssize_t, unconditionally
Tim Peters <tim.peters@gmail.com>
parents: 37067
diff changeset
943 if (m == 0 && n == 0)
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
944 PySys_WriteStderr("gc: done");
37290
89142cadcfb9 In format strings slinging Py_ssize_t, unconditionally
Tim Peters <tim.peters@gmail.com>
parents: 37067
diff changeset
945 else
36860
8ebf7997c6cd Fix warnings on x86 (32-bit) and support Win64.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36839
diff changeset
946 PySys_WriteStderr(
37290
89142cadcfb9 In format strings slinging Py_ssize_t, unconditionally
Tim Peters <tim.peters@gmail.com>
parents: 37067
diff changeset
947 "gc: done, "
89142cadcfb9 In format strings slinging Py_ssize_t, unconditionally
Tim Peters <tim.peters@gmail.com>
parents: 37067
diff changeset
948 "%" PY_FORMAT_SIZE_T "d unreachable, "
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
949 "%" PY_FORMAT_SIZE_T "d uncollectable",
36860
8ebf7997c6cd Fix warnings on x86 (32-bit) and support Win64.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36839
diff changeset
950 n+m, n);
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
951 if (t1 && t2) {
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
952 PySys_WriteStderr(", %.4fs elapsed", t2-t1);
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
953 }
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
954 PySys_WriteStderr(".\n");
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
955 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
956
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
957 /* Append instances in the uncollectable set to a Python
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
958 * reachable list of garbage. The programmer has to deal with
28511
acf3596fcc28 Reworked move_finalizer_reachable() to create two distinct lists:
Tim Peters <tim.peters@gmail.com>
parents: 28510
diff changeset
959 * this if they insist on creating this type of structure.
acf3596fcc28 Reworked move_finalizer_reachable() to create two distinct lists:
Tim Peters <tim.peters@gmail.com>
parents: 28510
diff changeset
960 */
28520
6653a56002ef Reworked has_finalizer() to use the new _PyObject_Lookup() instead
Tim Peters <tim.peters@gmail.com>
parents: 28516
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
57949
8e82fb043ae8 Fix a typo in comment
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 52288
diff changeset
963 /* Clear free list only during the collection of the highest
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
964 * 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
965 if (generation == NUM_GENERATIONS-1) {
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
966 clear_freelists();
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
967 }
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
14330
6dee1985c681 refactor __del__ exception handler into PyErr_WriteUnraisable
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 14294
diff changeset
969 if (PyErr_Occurred()) {
28520
6653a56002ef Reworked has_finalizer() to use the new _PyObject_Lookup() instead
Tim Peters <tim.peters@gmail.com>
parents: 28516
diff changeset
970 if (gc_str == NULL)
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
971 gc_str = PyString_FromString("garbage collection");
14330
6dee1985c681 refactor __del__ exception handler into PyErr_WriteUnraisable
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 14294
diff changeset
972 PyErr_WriteUnraisable(gc_str);
6dee1985c681 refactor __del__ exception handler into PyErr_WriteUnraisable
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 14294
diff changeset
973 Py_FatalError("unexpected exception during garbage collection");
6dee1985c681 refactor __del__ exception handler into PyErr_WriteUnraisable
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 14294
diff changeset
974 }
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
975 return n+m;
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 {
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
981 int i;
36839
7c1aca8e3d7d Make PyGC_Collect() use Py_ssize_t.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36769
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
57949
8e82fb043ae8 Fix a typo in comment
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 52288
diff changeset
984 /* Find the oldest generation (highest numbered) where the count
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
985 * exceeds the threshold. Objects in the that generation and
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
986 * generations younger than it will be collected. */
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
987 for (i = NUM_GENERATIONS-1; i >= 0; i--) {
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
988 if (generations[i].count > generations[i].threshold) {
50676
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
989 /* Avoid quadratic performance degradation in number
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
990 of tracked objects. See comments at the beginning
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
991 of this file, and 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
992 */
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
993 if (i == NUM_GENERATIONS - 1
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
994 && long_lived_pending < long_lived_total / 4)
79276316b94b Issue #4074: Change the criteria for doing a full garbage collection (i.e.
Antoine Pitrou <solipsis@pitrou.net>
parents: 50257
diff changeset
995 continue;
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
996 n = collect(i);
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
997 break;
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
998 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
999 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1000 return n;
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 {
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13570
diff changeset
1011 enabled = 1;
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13570
diff changeset
1012 Py_INCREF(Py_None);
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13570
diff changeset
1013 return Py_None;
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 {
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13570
diff changeset
1024 enabled = 0;
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13570
diff changeset
1025 Py_INCREF(Py_None);
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13570
diff changeset
1026 return Py_None;
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 {
31341
4915fbec608f Convert return value to boolean.
Raymond Hettinger <python@rcn.com>
parents: 31010
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 {
36863
2d24f3a37a1b SF patch #1443865; gc.get_count() added and optional argument 'generation'
Barry Warsaw <barry@python.org>
parents: 36860
diff changeset
1051 static char *keywords[] = {"generation", NULL};
2d24f3a37a1b SF patch #1443865; gc.get_count() added and optional argument 'generation'
Barry Warsaw <barry@python.org>
parents: 36860
diff changeset
1052 int genarg = NUM_GENERATIONS - 1;
36839
7c1aca8e3d7d Make PyGC_Collect() use Py_ssize_t.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36769
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
36863
2d24f3a37a1b SF patch #1443865; gc.get_count() added and optional argument 'generation'
Barry Warsaw <barry@python.org>
parents: 36860
diff changeset
1055 if (!PyArg_ParseTupleAndKeywords(args, kws, "|i", keywords, &genarg))
2d24f3a37a1b SF patch #1443865; gc.get_count() added and optional argument 'generation'
Barry Warsaw <barry@python.org>
parents: 36860
diff changeset
1056 return NULL;
2d24f3a37a1b SF patch #1443865; gc.get_count() added and optional argument 'generation'
Barry Warsaw <barry@python.org>
parents: 36860
diff changeset
1057
2d24f3a37a1b SF patch #1443865; gc.get_count() added and optional argument 'generation'
Barry Warsaw <barry@python.org>
parents: 36860
diff changeset
1058 else if (genarg < 0 || genarg >= NUM_GENERATIONS) {
2d24f3a37a1b SF patch #1443865; gc.get_count() added and optional argument 'generation'
Barry Warsaw <barry@python.org>
parents: 36860
diff changeset
1059 PyErr_SetString(PyExc_ValueError, "invalid generation");
2d24f3a37a1b SF patch #1443865; gc.get_count() added and optional argument 'generation'
Barry Warsaw <barry@python.org>
parents: 36860
diff changeset
1060 return NULL;
2d24f3a37a1b SF patch #1443865; gc.get_count() added and optional argument 'generation'
Barry Warsaw <barry@python.org>
parents: 36860
diff changeset
1061 }
2d24f3a37a1b SF patch #1443865; gc.get_count() added and optional argument 'generation'
Barry Warsaw <barry@python.org>
parents: 36860
diff changeset
1062
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
1063 if (collecting)
20796
9c23abab962d Make the gc.collect() function respect the collection lock. This fixes
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20393
diff changeset
1064 n = 0; /* already collecting, don't do anything */
9c23abab962d Make the gc.collect() function respect the collection lock. This fixes
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20393
diff changeset
1065 else {
9c23abab962d Make the gc.collect() function respect the collection lock. This fixes
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20393
diff changeset
1066 collecting = 1;
36863
2d24f3a37a1b SF patch #1443865; gc.get_count() added and optional argument 'generation'
Barry Warsaw <barry@python.org>
parents: 36860
diff changeset
1067 n = collect(genarg);
20796
9c23abab962d Make the gc.collect() function respect the collection lock. This fixes
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20393
diff changeset
1068 collecting = 0;
9c23abab962d Make the gc.collect() function respect the collection lock. This fixes
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20393
diff changeset
1069 }
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1070
36839
7c1aca8e3d7d Make PyGC_Collect() use Py_ssize_t.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36769
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 {
14774
52174b002f1f Fix some long/"l" int/"i" mismatches. Fixes bug #113779.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14756
diff changeset
1093 if (!PyArg_ParseTuple(args, "i:set_debug", &debug))
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1094 return NULL;
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1095
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1096 Py_INCREF(Py_None);
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1097 return Py_None;
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 {
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1108 return Py_BuildValue("i", debug);
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 {
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
1120 int i;
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
1121 if (!PyArg_ParseTuple(args, "i|ii:set_threshold",
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
1122 &generations[0].threshold,
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
1123 &generations[1].threshold,
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
1124 &generations[2].threshold))
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1125 return NULL;
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
1126 for (i = 2; i < NUM_GENERATIONS; i++) {
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
1127 /* generations higher than 2 get the same threshold */
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
1128 generations[i].threshold = generations[2].threshold;
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
1129 }
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1130
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1131 Py_INCREF(Py_None);
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1132 return Py_None;
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 {
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
1143 return Py_BuildValue("(iii)",
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
1144 generations[0].threshold,
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
1145 generations[1].threshold,
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
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 {
2d24f3a37a1b SF patch #1443865; gc.get_count() added and optional argument 'generation'
Barry Warsaw <barry@python.org>
parents: 36860
diff changeset
1157 return Py_BuildValue("(iii)",
2d24f3a37a1b SF patch #1443865; gc.get_count() added and optional argument 'generation'
Barry Warsaw <barry@python.org>
parents: 36860
diff changeset
1158 generations[0].count,
2d24f3a37a1b SF patch #1443865; gc.get_count() added and optional argument 'generation'
Barry Warsaw <barry@python.org>
parents: 36860
diff changeset
1159 generations[1].count,
2d24f3a37a1b SF patch #1443865; gc.get_count() added and optional argument 'generation'
Barry Warsaw <barry@python.org>
parents: 36860
diff changeset
1160 generations[2].count);
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 {
37515
c16b752ee93b Handle ssize_t
Neal Norwitz <nnorwitz@gmail.com>
parents: 37290
diff changeset
1166 Py_ssize_t i;
21105
d19513e2893c Use identity instead of equality when looking for referrers. Fixes #485781.
Martin v. Löwis <martin@v.loewis.de>
parents: 21038
diff changeset
1167 for (i = 0; i < PyTuple_GET_SIZE(objs); i++)
d19513e2893c Use identity instead of equality when looking for referrers. Fixes #485781.
Martin v. Löwis <martin@v.loewis.de>
parents: 21038
diff changeset
1168 if (PyTuple_GET_ITEM(objs, i) == obj)
d19513e2893c Use identity instead of equality when looking for referrers. Fixes #485781.
Martin v. Löwis <martin@v.loewis.de>
parents: 21038
diff changeset
1169 return 1;
19096
45ccf3b889b5 Add get_referents function. Closes SF patch #402925.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15013
diff changeset
1170 return 0;
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 {
45ccf3b889b5 Add get_referents function. Closes SF patch #402925.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15013
diff changeset
1176 PyGC_Head *gc;
45ccf3b889b5 Add get_referents function. Closes SF patch #402925.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15013
diff changeset
1177 PyObject *obj;
45ccf3b889b5 Add get_referents function. Closes SF patch #402925.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15013
diff changeset
1178 traverseproc traverse;
20381
b4f829941f3d SF bug [#467145] Python 2.2a4 build problem on HPUX 11.0.
Tim Peters <tim.peters@gmail.com>
parents: 20296
diff changeset
1179 for (gc = list->gc.gc_next; gc != list; gc = gc->gc.gc_next) {
19585
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1180 obj = FROM_GC(gc);
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 42333
diff changeset
1181 traverse = Py_TYPE(obj)->tp_traverse;
19096
45ccf3b889b5 Add get_referents function. Closes SF patch #402925.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15013
diff changeset
1182 if (obj == objs || obj == resultlist)
45ccf3b889b5 Add get_referents function. Closes SF patch #402925.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15013
diff changeset
1183 continue;
21038
afb789aada16 Rename get_referents to get_referrers. Fixes #483815.
Martin v. Löwis <martin@v.loewis.de>
parents: 20820
diff changeset
1184 if (traverse(obj, (visitproc)referrersvisit, objs)) {
19128
4901a935490d Remove "referents" structure (it's not needed). Check return value
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19097
diff changeset
1185 if (PyList_Append(resultlist, obj) < 0)
4901a935490d Remove "referents" structure (it's not needed). Check return value
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19097
diff changeset
1186 return 0; /* error */
19096
45ccf3b889b5 Add get_referents function. Closes SF patch #402925.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15013
diff changeset
1187 }
45ccf3b889b5 Add get_referents function. Closes SF patch #402925.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15013
diff changeset
1188 }
19128
4901a935490d Remove "referents" structure (it's not needed). Check return value
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19097
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 {
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
1199 int i;
19096
45ccf3b889b5 Add get_referents function. Closes SF patch #402925.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15013
diff changeset
1200 PyObject *result = PyList_New(0);
37067
1f4549865aad Fix some missing checks after PyTuple_New, PyList_New, PyDict_New
Georg Brandl <georg@python.org>
parents: 36863
diff changeset
1201 if (!result) return NULL;
1f4549865aad Fix some missing checks after PyTuple_New, PyList_New, PyDict_New
Georg Brandl <georg@python.org>
parents: 36863
diff changeset
1202
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
1203 for (i = 0; i < NUM_GENERATIONS; i++) {
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
1204 if (!(gc_referrers_for(args, GEN_HEAD(i), result))) {
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
1205 Py_DECREF(result);
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
1206 return NULL;
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
1207 }
19128
4901a935490d Remove "referents" structure (it's not needed). Check return value
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19097
diff changeset
1208 }
19096
45ccf3b889b5 Add get_referents function. Closes SF patch #402925.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15013
diff changeset
1209 return result;
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 {
28522
487df294d328 Finished implementing gc.get_referrents(): dealt with error and end
Tim Peters <tim.peters@gmail.com>
parents: 28521
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 {
37515
c16b752ee93b Handle ssize_t
Neal Norwitz <nnorwitz@gmail.com>
parents: 37290
diff changeset
1226 Py_ssize_t i;
28497
40d9659b56c9 Add get_referrents() helper function.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26138
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
487df294d328 Finished implementing gc.get_referrents(): dealt with error and end
Tim Peters <tim.peters@gmail.com>
parents: 28521
diff changeset
1229 if (result == NULL)
487df294d328 Finished implementing gc.get_referrents(): dealt with error and end
Tim Peters <tim.peters@gmail.com>
parents: 28521
diff changeset
1230 return NULL;
487df294d328 Finished implementing gc.get_referrents(): dealt with error and end
Tim Peters <tim.peters@gmail.com>
parents: 28521
diff changeset
1231
28497
40d9659b56c9 Add get_referrents() helper function.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26138
diff changeset
1232 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
28522
487df294d328 Finished implementing gc.get_referrents(): dealt with error and end
Tim Peters <tim.peters@gmail.com>
parents: 28521
diff changeset
1233 traverseproc traverse;
28507
a0503bdb0c24 Fixed new seemingly random segfaults, by moving the initialization of
Tim Peters <tim.peters@gmail.com>
parents: 28504
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
487df294d328 Finished implementing gc.get_referrents(): dealt with error and end
Tim Peters <tim.peters@gmail.com>
parents: 28521
diff changeset
1236 if (! PyObject_IS_GC(obj))
28497
40d9659b56c9 Add get_referrents() helper function.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26138
diff changeset
1237 continue;
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 42333
diff changeset
1238 traverse = Py_TYPE(obj)->tp_traverse;
28522
487df294d328 Finished implementing gc.get_referrents(): dealt with error and end
Tim Peters <tim.peters@gmail.com>
parents: 28521
diff changeset
1239 if (! traverse)
487df294d328 Finished implementing gc.get_referrents(): dealt with error and end
Tim Peters <tim.peters@gmail.com>
parents: 28521
diff changeset
1240 continue;
28523
c01fdc3a9180 s/referrents/referents/g. Gotta love that referrers remains rife with rs.
Tim Peters <tim.peters@gmail.com>
parents: 28522
diff changeset
1241 if (traverse(obj, (visitproc)referentsvisit, result)) {
28522
487df294d328 Finished implementing gc.get_referrents(): dealt with error and end
Tim Peters <tim.peters@gmail.com>
parents: 28521
diff changeset
1242 Py_DECREF(result);
28497
40d9659b56c9 Add get_referrents() helper function.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26138
diff changeset
1243 return NULL;
28522
487df294d328 Finished implementing gc.get_referrents(): dealt with error and end
Tim Peters <tim.peters@gmail.com>
parents: 28521
diff changeset
1244 }
28497
40d9659b56c9 Add get_referrents() helper function.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26138
diff changeset
1245 }
40d9659b56c9 Add get_referrents() helper function.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26138
diff changeset
1246 return result;
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 {
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
1258 int i;
19097
9e6f7804ef14 Add get_objects function. This is a low level function (like
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19096
diff changeset
1259 PyObject* result;
9e6f7804ef14 Add get_objects function. This is a low level function (like
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19096
diff changeset
1260
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
1261 result = PyList_New(0);
174ce4cdec47 Switched from METH_VARARGS to METH_NOARGS for the 7 module functions that
Tim Peters <tim.peters@gmail.com>
parents: 28511
diff changeset
1262 if (result == NULL)
19097
9e6f7804ef14 Add get_objects function. This is a low level function (like
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19096
diff changeset
1263 return NULL;
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
1264 for (i = 0; i < NUM_GENERATIONS; i++) {
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
1265 if (append_objects(result, GEN_HEAD(i))) {
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
1266 Py_DECREF(result);
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
1267 return NULL;
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
1268 }
21130
6e34151aefe6 Patch #486743: remove bad INCREF, propagate exception in append_objects.
Martin v. Löwis <martin@v.loewis.de>
parents: 21105
diff changeset
1269 }
19097
9e6f7804ef14 Add get_objects function. This is a low level function (like
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19096
diff changeset
1270 return result;
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 {
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 51032
diff changeset
1283 PyObject *result;
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 51032
diff changeset
1284
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 51032
diff changeset
1285 if (PyObject_IS_GC(obj) && IS_TRACKED(obj))
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 51032
diff changeset
1286 result = Py_True;
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 51032
diff changeset
1287 else
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 51032
diff changeset
1288 result = Py_False;
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 51032
diff changeset
1289 Py_INCREF(result);
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 51032
diff changeset
1290 return result;
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[] = {
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
1312 {"enable", gc_enable, METH_NOARGS, gc_enable__doc__},
174ce4cdec47 Switched from METH_VARARGS to METH_NOARGS for the 7 module functions that
Tim Peters <tim.peters@gmail.com>
parents: 28511
diff changeset
1313 {"disable", gc_disable, METH_NOARGS, gc_disable__doc__},
174ce4cdec47 Switched from METH_VARARGS to METH_NOARGS for the 7 module functions that
Tim Peters <tim.peters@gmail.com>
parents: 28511
diff changeset
1314 {"isenabled", gc_isenabled, METH_NOARGS, gc_isenabled__doc__},
13878
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13570
diff changeset
1315 {"set_debug", gc_set_debug, METH_VARARGS, gc_set_debug__doc__},
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
1316 {"get_debug", gc_get_debug, METH_NOARGS, gc_get_debug__doc__},
36863
2d24f3a37a1b SF patch #1443865; gc.get_count() added and optional argument 'generation'
Barry Warsaw <barry@python.org>
parents: 36860
diff changeset
1317 {"get_count", gc_get_count, METH_NOARGS, gc_get_count__doc__},
13878
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13570
diff changeset
1318 {"set_threshold", gc_set_thresh, METH_VARARGS, gc_set_thresh__doc__},
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
1319 {"get_threshold", gc_get_thresh, METH_NOARGS, gc_get_thresh__doc__},
36863
2d24f3a37a1b SF patch #1443865; gc.get_count() added and optional argument 'generation'
Barry Warsaw <barry@python.org>
parents: 36860
diff changeset
1320 {"collect", (PyCFunction)gc_collect,
2d24f3a37a1b SF patch #1443865; gc.get_count() added and optional argument 'generation'
Barry Warsaw <barry@python.org>
parents: 36860
diff changeset
1321 METH_VARARGS | METH_KEYWORDS, gc_collect__doc__},
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
1322 {"get_objects", gc_get_objects,METH_NOARGS, gc_get_objects__doc__},
52288
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 51032
diff changeset
1323 {"is_tracked", gc_is_tracked, METH_O, gc_is_tracked__doc__},
21038
afb789aada16 Rename get_referents to get_referrers. Fixes #483815.
Martin v. Löwis <martin@v.loewis.de>
parents: 20820
diff changeset
1324 {"get_referrers", gc_get_referrers, METH_VARARGS,
afb789aada16 Rename get_referents to get_referrers. Fixes #483815.
Martin v. Löwis <martin@v.loewis.de>
parents: 20820
diff changeset
1325 gc_get_referrers__doc__},
28523
c01fdc3a9180 s/referrents/referents/g. Gotta love that referrers remains rife with rs.
Tim Peters <tim.peters@gmail.com>
parents: 28522
diff changeset
1326 {"get_referents", gc_get_referents, METH_VARARGS,
c01fdc3a9180 s/referrents/referents/g. Gotta love that referrers remains rife with rs.
Tim Peters <tim.peters@gmail.com>
parents: 28522
diff changeset
1327 gc_get_referents__doc__},
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1328 {NULL, NULL} /* Sentinel */
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 {
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1334 PyObject *m;
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1335
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1336 m = Py_InitModule4("gc",
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1337 GcMethods,
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1338 gc__doc__,
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1339 NULL,
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1340 PYTHON_API_VERSION);
36324
1af7f719ed28 Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35298
diff changeset
1341 if (m == NULL)
1af7f719ed28 Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35298
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
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1344 if (garbage == NULL) {
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1345 garbage = PyList_New(0);
28516
78f846fb570a initgc(): Rewrote to use the PyModule_AddXYZ API; cuts code size.
Tim Peters <tim.peters@gmail.com>
parents: 28515
diff changeset
1346 if (garbage == NULL)
78f846fb570a initgc(): Rewrote to use the PyModule_AddXYZ API; cuts code size.
Tim Peters <tim.peters@gmail.com>
parents: 28515
diff changeset
1347 return;
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1348 }
35103
234b22a2acf3 Add missing INCREF.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 33876
diff changeset
1349 Py_INCREF(garbage);
28516
78f846fb570a initgc(): Rewrote to use the PyModule_AddXYZ API; cuts code size.
Tim Peters <tim.peters@gmail.com>
parents: 28515
diff changeset
1350 if (PyModule_AddObject(m, "garbage", garbage) < 0)
78f846fb570a initgc(): Rewrote to use the PyModule_AddXYZ API; cuts code size.
Tim Peters <tim.peters@gmail.com>
parents: 28515
diff changeset
1351 return;
37987
06c059b92d0b Patch from Aldo Cortesi (OpenBSD buildbot owner).
Neal Norwitz <nnorwitz@gmail.com>
parents: 37872
diff changeset
1352
06c059b92d0b Patch from Aldo Cortesi (OpenBSD buildbot owner).
Neal Norwitz <nnorwitz@gmail.com>
parents: 37872
diff changeset
1353 /* Importing can't be done in collect() because collect()
06c059b92d0b Patch from Aldo Cortesi (OpenBSD buildbot owner).
Neal Norwitz <nnorwitz@gmail.com>
parents: 37872
diff changeset
1354 * can be called via PyGC_Collect() in Py_Finalize().
06c059b92d0b Patch from Aldo Cortesi (OpenBSD buildbot owner).
Neal Norwitz <nnorwitz@gmail.com>
parents: 37872
diff changeset
1355 * This wouldn't be a problem, except that <initialized> is
06c059b92d0b Patch from Aldo Cortesi (OpenBSD buildbot owner).
Neal Norwitz <nnorwitz@gmail.com>
parents: 37872
diff changeset
1356 * reset to 0 before calling collect which trips up
06c059b92d0b Patch from Aldo Cortesi (OpenBSD buildbot owner).
Neal Norwitz <nnorwitz@gmail.com>
parents: 37872
diff changeset
1357 * the import and triggers an assertion.
06c059b92d0b Patch from Aldo Cortesi (OpenBSD buildbot owner).
Neal Norwitz <nnorwitz@gmail.com>
parents: 37872
diff changeset
1358 */
06c059b92d0b Patch from Aldo Cortesi (OpenBSD buildbot owner).
Neal Norwitz <nnorwitz@gmail.com>
parents: 37872
diff changeset
1359 if (tmod == NULL) {
44439
0cbe94bea2c7 Modified PyImport_Import and PyImport_ImportModule to always use absolute imports by calling __import__ with an explicit level of 0
Christian Heimes <christian@cheimes.de>
parents: 44350
diff changeset
1360 tmod = PyImport_ImportModuleNoBlock("time");
37987
06c059b92d0b Patch from Aldo Cortesi (OpenBSD buildbot owner).
Neal Norwitz <nnorwitz@gmail.com>
parents: 37872
diff changeset
1361 if (tmod == NULL)
06c059b92d0b Patch from Aldo Cortesi (OpenBSD buildbot owner).
Neal Norwitz <nnorwitz@gmail.com>
parents: 37872
diff changeset
1362 PyErr_Clear();
06c059b92d0b Patch from Aldo Cortesi (OpenBSD buildbot owner).
Neal Norwitz <nnorwitz@gmail.com>
parents: 37872
diff changeset
1363 }
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
78f846fb570a initgc(): Rewrote to use the PyModule_AddXYZ API; cuts code size.
Tim Peters <tim.peters@gmail.com>
parents: 28515
diff changeset
1366 ADD_INT(DEBUG_STATS);
78f846fb570a initgc(): Rewrote to use the PyModule_AddXYZ API; cuts code size.
Tim Peters <tim.peters@gmail.com>
parents: 28515
diff changeset
1367 ADD_INT(DEBUG_COLLECTABLE);
78f846fb570a initgc(): Rewrote to use the PyModule_AddXYZ API; cuts code size.
Tim Peters <tim.peters@gmail.com>
parents: 28515
diff changeset
1368 ADD_INT(DEBUG_UNCOLLECTABLE);
78f846fb570a initgc(): Rewrote to use the PyModule_AddXYZ API; cuts code size.
Tim Peters <tim.peters@gmail.com>
parents: 28515
diff changeset
1369 ADD_INT(DEBUG_INSTANCES);
78f846fb570a initgc(): Rewrote to use the PyModule_AddXYZ API; cuts code size.
Tim Peters <tim.peters@gmail.com>
parents: 28515
diff changeset
1370 ADD_INT(DEBUG_OBJECTS);
78f846fb570a initgc(): Rewrote to use the PyModule_AddXYZ API; cuts code size.
Tim Peters <tim.peters@gmail.com>
parents: 28515
diff changeset
1371 ADD_INT(DEBUG_SAVEALL);
78f846fb570a initgc(): Rewrote to use the PyModule_AddXYZ API; cuts code size.
Tim Peters <tim.peters@gmail.com>
parents: 28515
diff changeset
1372 ADD_INT(DEBUG_LEAK);
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 {
36839
7c1aca8e3d7d Make PyGC_Collect() use Py_ssize_t.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36769
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
7fda09f751ad - New C API PyGC_Collect(), same as calling gc.collect().
Guido van Rossum <guido@python.org>
parents: 28523
diff changeset
1382 if (collecting)
7fda09f751ad - New C API PyGC_Collect(), same as calling gc.collect().
Guido van Rossum <guido@python.org>
parents: 28523
diff changeset
1383 n = 0; /* already collecting, don't do anything */
7fda09f751ad - New C API PyGC_Collect(), same as calling gc.collect().
Guido van Rossum <guido@python.org>
parents: 28523
diff changeset
1384 else {
7fda09f751ad - New C API PyGC_Collect(), same as calling gc.collect().
Guido van Rossum <guido@python.org>
parents: 28523
diff changeset
1385 collecting = 1;
7fda09f751ad - New C API PyGC_Collect(), same as calling gc.collect().
Guido van Rossum <guido@python.org>
parents: 28523
diff changeset
1386 n = collect(NUM_GENERATIONS - 1);
7fda09f751ad - New C API PyGC_Collect(), same as calling gc.collect().
Guido van Rossum <guido@python.org>
parents: 28523
diff changeset
1387 collecting = 0;
7fda09f751ad - New C API PyGC_Collect(), same as calling gc.collect().
Guido van Rossum <guido@python.org>
parents: 28523
diff changeset
1388 }
7fda09f751ad - New C API PyGC_Collect(), same as calling gc.collect().
Guido van Rossum <guido@python.org>
parents: 28523
diff changeset
1389
7fda09f751ad - New C API PyGC_Collect(), same as calling gc.collect().
Guido van Rossum <guido@python.org>
parents: 28523
diff changeset
1390 return n;
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 {
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1397 _PyObject_Dump(FROM_GC(g));
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 {
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1411 _PyObject_GC_TRACK(op);
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 {
24122
d332484e394a Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents: 24121
diff changeset
1424 /* Obscure: the Py_TRASHCAN mechanism requires that we be able to
d332484e394a Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents: 24121
diff changeset
1425 * call PyObject_GC_UnTrack twice on an object.
d332484e394a Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents: 24121
diff changeset
1426 */
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
1427 if (IS_TRACKED(op))
22625
898c392477d9 This is Neil's fix for SF bug 535905 (Evil Trashcan and GC interaction).
Guido van Rossum <guido@python.org>
parents: 22449
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 {
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1441 PyObject *op;
48114
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 47897
diff changeset
1442 PyGC_Head *g;
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 47897
diff changeset
1443 if (basicsize > PY_SSIZE_T_MAX - sizeof(PyGC_Head))
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 47897
diff changeset
1444 return PyErr_NoMemory();
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 47897
diff changeset
1445 g = (PyGC_Head *)PyObject_MALLOC(
37607
480a5625e286 Some more changes to make code compile under a C++ compiler.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37564
diff changeset
1446 sizeof(PyGC_Head) + basicsize);
19585
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1447 if (g == NULL)
23721
48e937e1b3fe Remove casts to PyObject * when declaration is for PyObject *
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23495
diff changeset
1448 return PyErr_NoMemory();
24077
a5162fd65c3d Reserved another gc_refs value for untracked objects. Every live gc
Tim Peters <tim.peters@gmail.com>
parents: 24075
diff changeset
1449 g->gc.gc_refs = GC_UNTRACKED;
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
1450 generations[0].count++; /* number of allocated GC 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
1451 if (generations[0].count > generations[0].threshold &&
19585
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1452 enabled &&
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
1453 generations[0].threshold &&
19585
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1454 !collecting &&
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1455 !PyErr_Occurred()) {
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1456 collecting = 1;
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
1457 collect_generations();
19585
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1458 collecting = 0;
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1459 }
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1460 op = FROM_GC(g);
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1461 return op;
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 {
22972
95819f7f0bcd Allow PyObject_Del to be used as a function designator. Provide binary
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22648
diff changeset
1467 PyObject *op = _PyObject_GC_Malloc(_PyObject_SIZE(tp));
23296
17cafe805f04 _PyObject_GC_New: Could call PyObject_INIT with a NULL 1st argument.
Tim Peters <tim.peters@gmail.com>
parents: 22972
diff changeset
1468 if (op != NULL)
17cafe805f04 _PyObject_GC_New: Could call PyObject_INIT with a NULL 1st argument.
Tim Peters <tim.peters@gmail.com>
parents: 22972
diff changeset
1469 op = PyObject_INIT(op, tp);
17cafe805f04 _PyObject_GC_New: Could call PyObject_INIT with a NULL 1st argument.
Tim Peters <tim.peters@gmail.com>
parents: 22972
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 {
22972
95819f7f0bcd Allow PyObject_Del to be used as a function designator. Provide binary
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22648
diff changeset
1476 const size_t size = _PyObject_VAR_SIZE(tp, nitems);
95819f7f0bcd Allow PyObject_Del to be used as a function designator. Provide binary
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22648
diff changeset
1477 PyVarObject *op = (PyVarObject *) _PyObject_GC_Malloc(size);
23296
17cafe805f04 _PyObject_GC_New: Could call PyObject_INIT with a NULL 1st argument.
Tim Peters <tim.peters@gmail.com>
parents: 22972
diff changeset
1478 if (op != NULL)
17cafe805f04 _PyObject_GC_New: Could call PyObject_INIT with a NULL 1st argument.
Tim Peters <tim.peters@gmail.com>
parents: 22972
diff changeset
1479 op = PyObject_INIT_VAR(op, tp, nitems);
17cafe805f04 _PyObject_GC_New: Could call PyObject_INIT with a NULL 1st argument.
Tim Peters <tim.peters@gmail.com>
parents: 22972
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 {
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 42333
diff changeset
1486 const size_t basicsize = _PyObject_VAR_SIZE(Py_TYPE(op), nitems);
19585
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1487 PyGC_Head *g = AS_GC(op);
48114
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 47897
diff changeset
1488 if (basicsize > PY_SSIZE_T_MAX - sizeof(PyGC_Head))
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 47897
diff changeset
1489 return (PyVarObject *)PyErr_NoMemory();
37607
480a5625e286 Some more changes to make code compile under a C++ compiler.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37564
diff changeset
1490 g = (PyGC_Head *)PyObject_REALLOC(g, sizeof(PyGC_Head) + basicsize);
19585
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1491 if (g == NULL)
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1492 return (PyVarObject *)PyErr_NoMemory();
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1493 op = (PyVarObject *) FROM_GC(g);
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 42333
diff changeset
1494 Py_SIZE(op) = nitems;
19585
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1495 return op;
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 {
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1501 PyGC_Head *g = AS_GC(op);
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
1502 if (IS_TRACKED(op))
19585
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1503 gc_list_remove(g);
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
1504 if (generations[0].count > 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
1505 generations[0].count--;
19585
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1506 }
22972
95819f7f0bcd Allow PyObject_Del to be used as a function designator. Provide binary
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22648
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 }