annotate Modules/gcmodule.c @ 48715:48dddfd2fbf3

RPC_WSTR is not available Visual Studio 2003 or earlier. (Maybe I should have defined RPC_WSTR on old compiler, but uuidcreate() is only place using it, so I simply replaced with unsigned short *)
author Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
date Sun, 17 Aug 2008 07:26:26 +0000
parents de0bf60f1245
children afb9808b0f30
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/
48479
d040a3b63df4 Fix some broken URLs to GC design discussions.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 47580
diff changeset
12
d040a3b63df4 Fix some broken URLs to GC design discussions.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 47580
diff changeset
13 The following mailing list threads provide a historical perspective on
d040a3b63df4 Fix some broken URLs to GC design discussions.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 47580
diff changeset
14 the design of this module. Note that a fair amount of refinement has
d040a3b63df4 Fix some broken URLs to GC design discussions.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 47580
diff changeset
15 occurred since those discussions.
d040a3b63df4 Fix some broken URLs to GC design discussions.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 47580
diff changeset
16
d040a3b63df4 Fix some broken URLs to GC design discussions.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 47580
diff changeset
17 http://mail.python.org/pipermail/python-dev/2000-March/002385.html
d040a3b63df4 Fix some broken URLs to GC design discussions.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 47580
diff changeset
18 http://mail.python.org/pipermail/python-dev/2000-March/002434.html
d040a3b63df4 Fix some broken URLs to GC design discussions.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 47580
diff changeset
19 http://mail.python.org/pipermail/python-dev/2000-March/002497.html
13183
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 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
22 function.
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
23
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
24 */
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
25
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
26 #include "Python.h"
45371
7c4c46342137 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60845 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44441
diff changeset
27 #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
28
19585
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
29 /* 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
30 #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
31
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
32 /* 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
33 #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
34
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
35 /*** Global GC state ***/
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
36
23377
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
37 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
38 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
39 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
40 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
41 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
42 };
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
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 #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
45 #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
46
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
47 /* 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
48 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
49 /* 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
50 {{{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
51 {{{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
52 {{{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
53 };
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
54
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
55 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
56
13878
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13570
diff changeset
57 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
58
19585
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
59 /* 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
60 static int collecting = 0;
19585
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
61
24085
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
62 /* 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
63 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
64
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
65 /* 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
66 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
67
28507
a0503bdb0c24 Fixed new seemingly random segfaults, by moving the initialization of
Tim Peters <tim.peters@gmail.com>
parents: 28504
diff changeset
68 /* 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
69 static PyObject *delstr = NULL;
28504
cb5db5561cdc Fix Tim's boom example.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28498
diff changeset
70
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
71 /* set for debugging information */
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
72 #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
73 #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
74 #define DEBUG_UNCOLLECTABLE (1<<2) /* print uncollectable 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
75 #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
76 #define DEBUG_LEAK DEBUG_COLLECTABLE | \
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
77 DEBUG_UNCOLLECTABLE | \
14756
6e9331e68a3b - Add DEBUG_SAVEALL option. When enabled all garbage objects found by the
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14424
diff changeset
78 DEBUG_SAVEALL
14330
6dee1985c681 refactor __del__ exception handler into PyErr_WriteUnraisable
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 14294
diff changeset
79 static int debug;
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
80 static PyObject *tmod = NULL;
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
81
24085
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
82 /*--------------------------------------------------------------------------
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
83 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
84
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
85 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
86
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
87 GC_UNTRACKED
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
88 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
89 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
90 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
91
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
92 GC_REACHABLE
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
93 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
94 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
95 is called.
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
96
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
97 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
98
24085
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
99 >= 0
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
100 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
101 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
102 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
103 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
104 being collected.
26138
4ab6b7c3f311 Fix typo.
Martin v. Löwis <martin@v.loewis.de>
parents: 24714
diff changeset
105 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
106
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
107 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
108 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
109 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
110 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
111 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
112 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
113 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
114 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
115
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
116 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
117 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
118 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
119 ----------------------------------------------------------------------------
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
120 */
24077
a5162fd65c3d Reserved another gc_refs value for untracked objects. Every live gc
Tim Peters <tim.peters@gmail.com>
parents: 24075
diff changeset
121 #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
122 #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
123 #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
124
24085
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
125 #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
126 #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
127 #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
128 (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
129
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
130 /*** list functions ***/
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
131
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
132 static void
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
133 gc_list_init(PyGC_Head *list)
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
134 {
20381
b4f829941f3d SF bug [#467145] Python 2.2a4 build problem on HPUX 11.0.
Tim Peters <tim.peters@gmail.com>
parents: 20296
diff changeset
135 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
136 list->gc.gc_next = list;
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
137 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
138
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
139 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
140 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
141 {
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
142 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
143 }
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
144
33869
223d20afc8b5 gc list function cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 33867
diff changeset
145 #if 0
223d20afc8b5 gc list function cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 33867
diff changeset
146 /* This became unused after gc_list_move() was introduced. */
223d20afc8b5 gc list function cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 33867
diff changeset
147 /* Append `node` to `list`. */
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
148 static void
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
149 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
150 {
20381
b4f829941f3d SF bug [#467145] Python 2.2a4 build problem on HPUX 11.0.
Tim Peters <tim.peters@gmail.com>
parents: 20296
diff changeset
151 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
152 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
153 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
154 list->gc.gc_prev = node;
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
155 }
33869
223d20afc8b5 gc list function cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 33867
diff changeset
156 #endif
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
157
33869
223d20afc8b5 gc list function cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 33867
diff changeset
158 /* 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
159 static void
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
160 gc_list_remove(PyGC_Head *node)
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
161 {
20381
b4f829941f3d SF bug [#467145] Python 2.2a4 build problem on HPUX 11.0.
Tim Peters <tim.peters@gmail.com>
parents: 20296
diff changeset
162 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
163 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
164 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
165 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
166
33869
223d20afc8b5 gc list function cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 33867
diff changeset
167 /* 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
168 * 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
169 * 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
170 */
223d20afc8b5 gc list function cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 33867
diff changeset
171 static void
223d20afc8b5 gc list function cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 33867
diff changeset
172 gc_list_move(PyGC_Head *node, PyGC_Head *list)
223d20afc8b5 gc list function cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 33867
diff changeset
173 {
33876
633cadebf68d gc_list_move(): Make this truly equivalent to remove+append. While
Tim Peters <tim.peters@gmail.com>
parents: 33869
diff changeset
174 PyGC_Head *new_prev;
33869
223d20afc8b5 gc list function cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 33867
diff changeset
175 PyGC_Head *current_prev = node->gc.gc_prev;
223d20afc8b5 gc list function cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 33867
diff changeset
176 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
177 /* Unlink from current list. */
33869
223d20afc8b5 gc list function cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 33867
diff changeset
178 current_prev->gc.gc_next = current_next;
223d20afc8b5 gc list function cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 33867
diff changeset
179 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
180 /* 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
181 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
182 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
183 node->gc.gc_next = list;
223d20afc8b5 gc list function cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 33867
diff changeset
184 }
223d20afc8b5 gc list function cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 33867
diff changeset
185
223d20afc8b5 gc list function cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 33867
diff changeset
186 /* 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
187 static void
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
188 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
189 {
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
190 PyGC_Head *tail;
33869
223d20afc8b5 gc list function cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 33867
diff changeset
191 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
192 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
193 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
194 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
195 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
196 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
197 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
198 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
199 gc_list_init(from);
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
200 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
201
36839
7c1aca8e3d7d Make PyGC_Collect() use Py_ssize_t.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36769
diff changeset
202 static Py_ssize_t
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
203 gc_list_size(PyGC_Head *list)
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
204 {
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
205 PyGC_Head *gc;
36839
7c1aca8e3d7d Make PyGC_Collect() use Py_ssize_t.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36769
diff changeset
206 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
207 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
208 n++;
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
209 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
210 return n;
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
211 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
212
28515
4c596dd60c22 handle_finalizers(): Rewrote to call append_objects() and gc_list_merge()
Tim Peters <tim.peters@gmail.com>
parents: 28512
diff changeset
213 /* 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
214 * 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
215 */
4c596dd60c22 handle_finalizers(): Rewrote to call append_objects() and gc_list_merge()
Tim Peters <tim.peters@gmail.com>
parents: 28512
diff changeset
216 static int
4c596dd60c22 handle_finalizers(): Rewrote to call append_objects() and gc_list_merge()
Tim Peters <tim.peters@gmail.com>
parents: 28512
diff changeset
217 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
218 {
4c596dd60c22 handle_finalizers(): Rewrote to call append_objects() and gc_list_merge()
Tim Peters <tim.peters@gmail.com>
parents: 28512
diff changeset
219 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
220 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
221 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
222 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
223 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
224 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
225 }
4c596dd60c22 handle_finalizers(): Rewrote to call append_objects() and gc_list_merge()
Tim Peters <tim.peters@gmail.com>
parents: 28512
diff changeset
226 }
4c596dd60c22 handle_finalizers(): Rewrote to call append_objects() and gc_list_merge()
Tim Peters <tim.peters@gmail.com>
parents: 28512
diff changeset
227 }
4c596dd60c22 handle_finalizers(): Rewrote to call append_objects() and gc_list_merge()
Tim Peters <tim.peters@gmail.com>
parents: 28512
diff changeset
228 return 0;
4c596dd60c22 handle_finalizers(): Rewrote to call append_objects() and gc_list_merge()
Tim Peters <tim.peters@gmail.com>
parents: 28512
diff changeset
229 }
4c596dd60c22 handle_finalizers(): Rewrote to call append_objects() and gc_list_merge()
Tim Peters <tim.peters@gmail.com>
parents: 28512
diff changeset
230
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
231 /*** end of list stuff ***/
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
232
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
233
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
234 /* 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
235 * 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
236 * containers.
24072
86b3760d285d SF bug #574132: Major GC related performance regression
Tim Peters <tim.peters@gmail.com>
parents: 24036
diff changeset
237 */
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
238 static void
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
239 update_refs(PyGC_Head *containers)
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
240 {
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 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
242 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
243 assert(gc->gc.gc_refs == GC_REACHABLE);
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44265
diff changeset
244 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
245 /* 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
246 * 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
247 * 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
248 * 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
249 * 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
250 * 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
251 * 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
252 * 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
253 * 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
254 * 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
255 * 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
256 * 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
257 * 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
258 * 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
259 * 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
260 * 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
261 * 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
262 */
8aa9237f6dd6 update_refs(): assert that incoming refcounts aren't 0. The comment
Tim Peters <tim.peters@gmail.com>
parents: 30311
diff changeset
263 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
264 }
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
265 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
266
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
267 /* 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
268 static int
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
269 visit_decref(PyObject *op, void *data)
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
270 {
24074
a1805a54666f visit_decref(): Two optimizations.
Tim Peters <tim.peters@gmail.com>
parents: 24072
diff changeset
271 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
272 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
273 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
274 /* 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
275 * 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
276 * 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
277 */
24097
f7abbd15b4f4 visit_decref(): Added another assert.
Tim Peters <tim.peters@gmail.com>
parents: 24085
diff changeset
278 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
279 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
280 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
281 }
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
282 return 0;
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
283 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
284
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
285 /* 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
286 * 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
287 * 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
288 * 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
289 */
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
290 static void
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
291 subtract_refs(PyGC_Head *containers)
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
292 {
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
293 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
294 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
295 for (; gc != containers; gc=gc->gc.gc_next) {
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44265
diff changeset
296 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
297 (void) traverse(FROM_GC(gc),
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
298 (visitproc)visit_decref,
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
299 NULL);
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
300 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
301 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
302
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
303 /* 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
304 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
305 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
306 {
24077
a5162fd65c3d Reserved another gc_refs value for untracked objects. Every live gc
Tim Peters <tim.peters@gmail.com>
parents: 24075
diff changeset
307 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
308 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
309 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
310
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
311 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
312 /* 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
313 * 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
314 * 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
315 * 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
316 */
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
317 gc->gc.gc_refs = 1;
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
318 }
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
319 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
320 /* 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
321 * 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
322 * 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
323 * 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
324 * 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
325 */
33869
223d20afc8b5 gc list function cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 33867
diff changeset
326 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
327 gc->gc.gc_refs = 1;
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
328 }
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
329 /* 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
330 * 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
331 * 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
332 * 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
333 * 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
334 * 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
335 * 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
336 */
24077
a5162fd65c3d Reserved another gc_refs value for untracked objects. Every live gc
Tim Peters <tim.peters@gmail.com>
parents: 24075
diff changeset
337 else {
a5162fd65c3d Reserved another gc_refs value for untracked objects. Every live gc
Tim Peters <tim.peters@gmail.com>
parents: 24075
diff changeset
338 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
339 || 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
340 || 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
341 }
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
342 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
343 return 0;
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
344 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
345
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
346 /* 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
347 * 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
348 * 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
349 * 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
350 * 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
351 * 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
352 * not.
24072
86b3760d285d SF bug #574132: Major GC related performance regression
Tim Peters <tim.peters@gmail.com>
parents: 24036
diff changeset
353 */
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
354 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
355 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
356 {
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
357 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
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 /* 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
360 * = 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
361 * 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
362 * 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
363 * 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
364 * 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
365 * 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
366 */
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
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 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
369 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
370
24085
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
371 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
372 /* 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
373 * 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
374 * 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
375 * 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
376 * 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
377 * 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
378 * 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
379 */
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
380 PyObject *op = FROM_GC(gc);
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44265
diff changeset
381 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
382 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
383 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
384 (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
385 (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
386 (void *)young);
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
387 next = gc->gc.gc_next;
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
388 }
22d2f037af9d Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents: 24077
diff changeset
389 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
390 /* 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
391 * 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
392 * 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
393 * 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
394 * 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
395 * 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
396 */
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 next = gc->gc.gc_next;
33869
223d20afc8b5 gc list function cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 33867
diff changeset
398 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
399 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
400 }
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
401 gc = next;
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
402 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
403 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
404
44265
2c6fe2dcc88e Minor cleanup in the gc module.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44099
diff changeset
405 /* Return true if object has a finalization method. */
20818
9aded6235d89 Add has_finalizer predictate function. Use it when deciding which
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20810
diff changeset
406 static int
9aded6235d89 Add has_finalizer predictate function. Use it when deciding which
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20810
diff changeset
407 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
408 {
39536
8f5b9982beba Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
409 if (PyGen_CheckExact(op))
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36863
diff changeset
410 return PyGen_NeedsFinalizing((PyGenObject *)op);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36863
diff changeset
411 else
39620
3982475c5144 Fix test_gc failure by fixing the bug test_gc found: __del__ methods were no
Thomas Wouters <thomas@python.org>
parents: 39536
diff changeset
412 return op->ob_type->tp_del != NULL;
20818
9aded6235d89 Add has_finalizer predictate function. Use it when deciding which
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20810
diff changeset
413 }
9aded6235d89 Add has_finalizer predictate function. Use it when deciding which
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20810
diff changeset
414
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
415 /* 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
416 * 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
417 * 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
418 */
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
419 static void
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
420 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
421 {
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
422 PyGC_Head *gc;
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
423 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
424
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
425 /* 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
426 * `finalizers`.
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
427 */
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
428 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
429 PyObject *op = FROM_GC(gc);
28504
cb5db5561cdc Fix Tim's boom example.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28498
diff changeset
430
28510
0de0ded19968 move_finalizers(): Rewrote. It's not necessary for this routine
Tim Peters <tim.peters@gmail.com>
parents: 28508
diff changeset
431 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
432 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
433
28520
6653a56002ef Reworked has_finalizer() to use the new _PyObject_Lookup() instead
Tim Peters <tim.peters@gmail.com>
parents: 28516
diff changeset
434 if (has_finalizer(op)) {
33869
223d20afc8b5 gc list function cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 33867
diff changeset
435 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
436 gc->gc.gc_refs = GC_REACHABLE;
28504
cb5db5561cdc Fix Tim's boom example.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28498
diff changeset
437 }
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
438 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
439 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
440
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
441 /* 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
442 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
443 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
444 {
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 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
446 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
447 PyGC_Head *gc = AS_GC(op);
33869
223d20afc8b5 gc list function cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 33867
diff changeset
448 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
449 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
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 }
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
452 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
453 }
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 /* 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
456 * 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
457 */
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
458 static void
28520
6653a56002ef Reworked has_finalizer() to use the new _PyObject_Lookup() instead
Tim Peters <tim.peters@gmail.com>
parents: 28516
diff changeset
459 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
460 {
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
461 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
462 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
463 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
464 /* Note that the finalizers list may grow during this. */
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44265
diff changeset
465 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
466 (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
467 (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
468 (void *)finalizers);
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
469 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
470 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
471
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
472 /* 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
473 * 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
474 * 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
475 * 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
476 * 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
477 * 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
478 * 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
479 * 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
480 * 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
481 * 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
482 */
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30918
diff changeset
483 static int
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
484 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
485 {
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
486 PyGC_Head *gc;
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
487 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
488 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
489 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
490 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
491 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
492
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
493 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
494
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
495 /* 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
496 * 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
497 * 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
498 * 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
499 * 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
500 * 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
501 * pass completes.
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
502 */
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
503 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
504 PyWeakReference **wrlist;
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
505
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
506 op = FROM_GC(gc);
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
507 assert(IS_TENTATIVELY_UNREACHABLE(op));
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
508 next = gc->gc.gc_next;
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
509
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44265
diff changeset
510 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
511 continue;
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
512
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
513 /* 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
514 wrlist = (PyWeakReference **)
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
515 PyObject_GET_WEAKREFS_LISTPTR(op);
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
516
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
517 /* `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
518 * 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
519 * 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
520 */
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
521 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
522 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
523
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
524 /* _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
525 * 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
526 * changes *wrlist.
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
527 */
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
528 assert(wr->wr_object == op);
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
529 _PyWeakref_ClearRef(wr);
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
530 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
531 if (wr->wr_callback == NULL)
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
532 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
533
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
534 /* 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
535 * `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
536 * is also trash, no:
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
537 *
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
538 * 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
539 * 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
540 * 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
541 * 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
542 * invoked.
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
543 *
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
544 * 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
545 * 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
546 * 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
547 * 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
548 *
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
549 * 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
550 * 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
551 * 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
552 * trigger.
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
553 *
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
554 * 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
555 * 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
556 * 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
557 * 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
558 * 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
559 * 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
560 * 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
561 */
33867
07d94a162cf8 handle_weakrefs(): Simplification -- there's no need to make a second
Tim Peters <tim.peters@gmail.com>
parents: 33862
diff changeset
562 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
563 continue;
07d94a162cf8 handle_weakrefs(): Simplification -- there's no need to make a second
Tim Peters <tim.peters@gmail.com>
parents: 33862
diff changeset
564 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
565
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
566 /* 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
567 * 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
568 */
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
569 Py_INCREF(wr);
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
570
33867
07d94a162cf8 handle_weakrefs(): Simplification -- there's no need to make a second
Tim Peters <tim.peters@gmail.com>
parents: 33862
diff changeset
571 /* 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
572 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
573 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
574 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
575 be the same */
33869
223d20afc8b5 gc list function cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 33867
diff changeset
576 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
577 }
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
33867
07d94a162cf8 handle_weakrefs(): Simplification -- there's no need to make a second
Tim Peters <tim.peters@gmail.com>
parents: 33862
diff changeset
580 /* 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
581 * 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
582 */
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
583 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
584 PyObject *temp;
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
585 PyObject *callback;
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 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
588 op = FROM_GC(gc);
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
589 assert(IS_REACHABLE(op));
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
590 assert(PyWeakref_Check(op));
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
591 wr = (PyWeakReference *)op;
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
592 callback = wr->wr_callback;
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
593 assert(callback != NULL);
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
594
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
595 /* copy-paste of weakrefobject.c's handle_callback() */
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
596 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
597 if (temp == NULL)
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
598 PyErr_WriteUnraisable(callback);
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
599 else
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
600 Py_DECREF(temp);
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
601
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
602 /* 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
603 * 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
604 * 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
605 * 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
606 * 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
607 * 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
608 * 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
609 * 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
610 * 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
611 * ours).
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
612 */
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
613 Py_DECREF(op);
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
614 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
615 /* object is still alive -- move it */
33869
223d20afc8b5 gc list function cleanup.
Tim Peters <tim.peters@gmail.com>
parents: 33867
diff changeset
616 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
617 }
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
618 else
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
619 ++num_freed;
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
620 }
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
621
30988
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30918
diff changeset
622 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
623 }
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30918
diff changeset
624
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
625 static void
14294
053627e7ac14 apply patch #101362 by Vladimir Marangozov
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13878
diff changeset
626 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
627 {
44265
2c6fe2dcc88e Minor cleanup in the gc module.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44099
diff changeset
628 PySys_WriteStderr("gc: %.100s <%.100s %p>\n",
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44265
diff changeset
629 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
630 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
631
28511
acf3596fcc28 Reworked move_finalizer_reachable() to create two distinct lists:
Tim Peters <tim.peters@gmail.com>
parents: 28510
diff changeset
632 /* 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
633 * 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
634 * 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
635 * 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
636 * __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
637 * 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
638 * 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
639 * 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
640 */
28515
4c596dd60c22 handle_finalizers(): Rewrote to call append_objects() and gc_list_merge()
Tim Peters <tim.peters@gmail.com>
parents: 28512
diff changeset
641 static int
28520
6653a56002ef Reworked has_finalizer() to use the new _PyObject_Lookup() instead
Tim Peters <tim.peters@gmail.com>
parents: 28516
diff changeset
642 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
643 {
28520
6653a56002ef Reworked has_finalizer() to use the new _PyObject_Lookup() instead
Tim Peters <tim.peters@gmail.com>
parents: 28516
diff changeset
644 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
645
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
646 if (garbage == NULL) {
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
647 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
648 if (garbage == NULL)
acf3596fcc28 Reworked move_finalizer_reachable() to create two distinct lists:
Tim Peters <tim.peters@gmail.com>
parents: 28510
diff changeset
649 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
650 }
28520
6653a56002ef Reworked has_finalizer() to use the new _PyObject_Lookup() instead
Tim Peters <tim.peters@gmail.com>
parents: 28516
diff changeset
651 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
652 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
653
6653a56002ef Reworked has_finalizer() to use the new _PyObject_Lookup() instead
Tim Peters <tim.peters@gmail.com>
parents: 28516
diff changeset
654 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
655 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
656 return -1;
6653a56002ef Reworked has_finalizer() to use the new _PyObject_Lookup() instead
Tim Peters <tim.peters@gmail.com>
parents: 28516
diff changeset
657 }
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
658 }
28520
6653a56002ef Reworked has_finalizer() to use the new _PyObject_Lookup() instead
Tim Peters <tim.peters@gmail.com>
parents: 28516
diff changeset
659
28515
4c596dd60c22 handle_finalizers(): Rewrote to call append_objects() and gc_list_merge()
Tim Peters <tim.peters@gmail.com>
parents: 28512
diff changeset
660 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
661 return 0;
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
662 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
663
14756
6e9331e68a3b - Add DEBUG_SAVEALL option. When enabled all garbage objects found by the
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14424
diff changeset
664 /* 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
665 * 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
666 * 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
667 */
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
668 static void
28504
cb5db5561cdc Fix Tim's boom example.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28498
diff changeset
669 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
670 {
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
671 inquiry clear;
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
672
28504
cb5db5561cdc Fix Tim's boom example.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28498
diff changeset
673 while (!gc_list_is_empty(collectable)) {
cb5db5561cdc Fix Tim's boom example.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28498
diff changeset
674 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
675 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
676
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
677 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
678 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
679 PyList_Append(garbage, op);
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
680 }
14756
6e9331e68a3b - Add DEBUG_SAVEALL option. When enabled all garbage objects found by the
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14424
diff changeset
681 else {
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44265
diff changeset
682 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
683 Py_INCREF(op);
23721
48e937e1b3fe Remove casts to PyObject * when declaration is for PyObject *
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23495
diff changeset
684 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
685 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
686 }
6e9331e68a3b - Add DEBUG_SAVEALL option. When enabled all garbage objects found by the
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14424
diff changeset
687 }
28504
cb5db5561cdc Fix Tim's boom example.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28498
diff changeset
688 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
689 /* 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
690 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
691 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
692 }
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 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
695
45371
7c4c46342137 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60845 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44441
diff changeset
696 /* Clear all free lists
7c4c46342137 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60845 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44441
diff changeset
697 * All free lists are cleared during the collection of the highest generation.
7c4c46342137 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60845 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44441
diff changeset
698 * Allocated items in the free list may keep a pymalloc arena occupied.
7c4c46342137 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60845 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44441
diff changeset
699 * Clearing the free lists may give back memory to the OS earlier.
7c4c46342137 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60845 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44441
diff changeset
700 */
7c4c46342137 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60845 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44441
diff changeset
701 static void
7c4c46342137 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60845 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44441
diff changeset
702 clear_freelists(void)
7c4c46342137 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60845 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44441
diff changeset
703 {
7c4c46342137 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60845 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44441
diff changeset
704 (void)PyMethod_ClearFreeList();
7c4c46342137 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60845 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44441
diff changeset
705 (void)PyFrame_ClearFreeList();
7c4c46342137 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60845 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44441
diff changeset
706 (void)PyCFunction_ClearFreeList();
7c4c46342137 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60845 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44441
diff changeset
707 (void)PyTuple_ClearFreeList();
7c4c46342137 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60845 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44441
diff changeset
708 (void)PyUnicode_ClearFreeList();
48499
de0bf60f1245 Merged revisions 64722,64729,64753,64845-64846,64849,64871,64880-64882,64885,64888,64897,64900-64901,64915,64926-64929,64938-64941,64944,64961,64966,64973 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48479
diff changeset
709 (void)PyFloat_ClearFreeList();
45371
7c4c46342137 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60845 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44441
diff changeset
710 }
7c4c46342137 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60845 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44441
diff changeset
711
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
712 /* 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
713 * collection process works. */
36839
7c1aca8e3d7d Make PyGC_Collect() use Py_ssize_t.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36769
diff changeset
714 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
715 collect(int generation)
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
716 {
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
717 int i;
36839
7c1aca8e3d7d Make PyGC_Collect() use Py_ssize_t.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36769
diff changeset
718 Py_ssize_t m = 0; /* # objects collected */
7c1aca8e3d7d Make PyGC_Collect() use Py_ssize_t.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36769
diff changeset
719 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
720 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
721 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
722 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
723 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
724 PyGC_Head *gc;
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36863
diff changeset
725 double t1 = 0.0;
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
726
28507
a0503bdb0c24 Fixed new seemingly random segfaults, by moving the initialization of
Tim Peters <tim.peters@gmail.com>
parents: 28504
diff changeset
727 if (delstr == NULL) {
42013
cd125fe83051 Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents: 40450
diff changeset
728 delstr = PyUnicode_InternFromString("__del__");
28507
a0503bdb0c24 Fixed new seemingly random segfaults, by moving the initialization of
Tim Peters <tim.peters@gmail.com>
parents: 28504
diff changeset
729 if (delstr == NULL)
a0503bdb0c24 Fixed new seemingly random segfaults, by moving the initialization of
Tim Peters <tim.peters@gmail.com>
parents: 28504
diff changeset
730 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
731 }
a0503bdb0c24 Fixed new seemingly random segfaults, by moving the initialization of
Tim Peters <tim.peters@gmail.com>
parents: 28504
diff changeset
732
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
733 if (debug & DEBUG_STATS) {
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36863
diff changeset
734 if (tmod != NULL) {
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36863
diff changeset
735 PyObject *f = PyObject_CallMethod(tmod, "time", NULL);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36863
diff changeset
736 if (f == NULL) {
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36863
diff changeset
737 PyErr_Clear();
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36863
diff changeset
738 }
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36863
diff changeset
739 else {
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36863
diff changeset
740 t1 = PyFloat_AsDouble(f);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36863
diff changeset
741 Py_DECREF(f);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36863
diff changeset
742 }
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36863
diff changeset
743 }
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
744 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
745 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
746 PySys_WriteStderr("gc: objects in each generation:");
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36863
diff changeset
747 for (i = 0; i < NUM_GENERATIONS; i++)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36863
diff changeset
748 PySys_WriteStderr(" %" PY_FORMAT_SIZE_T "d",
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36863
diff changeset
749 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
750 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
751 }
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
752
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
753 /* 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
754 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
755 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
756 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
757 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
758
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
759 /* 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
760 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
761 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
762 }
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
763
2f7d869dad4d Move all data for a single generation into a structure. The set of
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23296
diff changeset
764 /* 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
765 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
766 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
767 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
768 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
769 old = young;
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
770
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
771 /* 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
772 * 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
773 * 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
774 * 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
775 */
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
776 update_refs(young);
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
777 subtract_refs(young);
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
778
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
779 /* 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
780 * 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
781 * 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
782 * 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
783 * 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
784 */
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
785 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
786 move_unreachable(young, &unreachable);
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
787
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
788 /* Move reachable objects to next generation. */
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
789 if (young != old)
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
790 gc_list_merge(young, old);
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
791
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
792 /* 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
793 * 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
794 * 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
795 * 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
796 * 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
797 * handle_weakrefs().
28520
6653a56002ef Reworked has_finalizer() to use the new _PyObject_Lookup() instead
Tim Peters <tim.peters@gmail.com>
parents: 28516
diff changeset
798 */
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
799 gc_list_init(&finalizers);
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
800 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
801 /* 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
802 * 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
803 * 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
804 */
28520
6653a56002ef Reworked has_finalizer() to use the new _PyObject_Lookup() instead
Tim Peters <tim.peters@gmail.com>
parents: 28516
diff changeset
805 move_finalizer_reachable(&finalizers);
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
806
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
807 /* 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
808 * debugging information.
caa1a48da4cc SF bug 839548: Bug in type's GC handling causes segfaults.
Tim Peters <tim.peters@gmail.com>
parents: 30918
diff changeset
809 */
28520
6653a56002ef Reworked has_finalizer() to use the new _PyObject_Lookup() instead
Tim Peters <tim.peters@gmail.com>
parents: 28516
diff changeset
810 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
811 gc = gc->gc.gc_next) {
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
812 m++;
14294
053627e7ac14 apply patch #101362 by Vladimir Marangozov
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13878
diff changeset
813 if (debug & DEBUG_COLLECTABLE) {
19585
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
814 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
815 }
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36863
diff changeset
816 if (tmod != NULL && (debug & DEBUG_STATS)) {
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36863
diff changeset
817 PyObject *f = PyObject_CallMethod(tmod, "time", NULL);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36863
diff changeset
818 if (f == NULL) {
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36863
diff changeset
819 PyErr_Clear();
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36863
diff changeset
820 }
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36863
diff changeset
821 else {
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36863
diff changeset
822 t1 = PyFloat_AsDouble(f)-t1;
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36863
diff changeset
823 Py_DECREF(f);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36863
diff changeset
824 PySys_WriteStderr("gc: %.4fs elapsed.\n", t1);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36863
diff changeset
825 }
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36863
diff changeset
826 }
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
827 }
33862
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
828
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
829 /* 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
830 m += handle_weakrefs(&unreachable, old);
facb5a2f4b53 SF 1055820: weakref callback vs gc vs threads
Tim Peters <tim.peters@gmail.com>
parents: 31341
diff changeset
831
28521
964bd97ac1f5 Comment repair; no semantic changes.
Tim Peters <tim.peters@gmail.com>
parents: 28520
diff changeset
832 /* 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
833 * 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
834 * in finalizers to be freed.
964bd97ac1f5 Comment repair; no semantic changes.
Tim Peters <tim.peters@gmail.com>
parents: 28520
diff changeset
835 */
28520
6653a56002ef Reworked has_finalizer() to use the new _PyObject_Lookup() instead
Tim Peters <tim.peters@gmail.com>
parents: 28516
diff changeset
836 delete_garbage(&unreachable, old);
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
837
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
838 /* 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
839 * 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
840 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
841 gc != &finalizers;
acf3596fcc28 Reworked move_finalizer_reachable() to create two distinct lists:
Tim Peters <tim.peters@gmail.com>
parents: 28510
diff changeset
842 gc = gc->gc.gc_next) {
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
843 n++;
28511
acf3596fcc28 Reworked move_finalizer_reachable() to create two distinct lists:
Tim Peters <tim.peters@gmail.com>
parents: 28510
diff changeset
844 if (debug & DEBUG_UNCOLLECTABLE)
19585
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
845 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
846 }
14294
053627e7ac14 apply patch #101362 by Vladimir Marangozov
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13878
diff changeset
847 if (debug & DEBUG_STATS) {
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36863
diff changeset
848 if (m == 0 && n == 0)
14294
053627e7ac14 apply patch #101362 by Vladimir Marangozov
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13878
diff changeset
849 PySys_WriteStderr("gc: done.\n");
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36863
diff changeset
850 else
36860
8ebf7997c6cd Fix warnings on x86 (32-bit) and support Win64.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36839
diff changeset
851 PySys_WriteStderr(
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36863
diff changeset
852 "gc: done, "
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36863
diff changeset
853 "%" PY_FORMAT_SIZE_T "d unreachable, "
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36863
diff changeset
854 "%" PY_FORMAT_SIZE_T "d uncollectable.\n",
36860
8ebf7997c6cd Fix warnings on x86 (32-bit) and support Win64.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36839
diff changeset
855 n+m, n);
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
856 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
857
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
858 /* 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
859 * 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
860 * 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
861 */
28520
6653a56002ef Reworked has_finalizer() to use the new _PyObject_Lookup() instead
Tim Peters <tim.peters@gmail.com>
parents: 28516
diff changeset
862 (void)handle_finalizers(&finalizers, old);
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
863
45371
7c4c46342137 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60845 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44441
diff changeset
864 /* Clear free list only during the collection of the higest
7c4c46342137 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60845 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44441
diff changeset
865 * generation */
7c4c46342137 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60845 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44441
diff changeset
866 if (generation == NUM_GENERATIONS-1) {
7c4c46342137 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60845 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44441
diff changeset
867 clear_freelists();
7c4c46342137 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60845 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44441
diff changeset
868 }
7c4c46342137 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60845 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44441
diff changeset
869
14330
6dee1985c681 refactor __del__ exception handler into PyErr_WriteUnraisable
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 14294
diff changeset
870 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
871 if (gc_str == NULL)
42750
ce52cd71a8d9 Convert a bunch of constant strings in C to unicode.
Neal Norwitz <nnorwitz@gmail.com>
parents: 42334
diff changeset
872 gc_str = PyUnicode_FromString("garbage collection");
14330
6dee1985c681 refactor __del__ exception handler into PyErr_WriteUnraisable
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 14294
diff changeset
873 PyErr_WriteUnraisable(gc_str);
6dee1985c681 refactor __del__ exception handler into PyErr_WriteUnraisable
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 14294
diff changeset
874 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
875 }
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
876 return n+m;
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
877 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
878
36839
7c1aca8e3d7d Make PyGC_Collect() use Py_ssize_t.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36769
diff changeset
879 static Py_ssize_t
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
880 collect_generations(void)
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
881 {
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
882 int i;
36839
7c1aca8e3d7d Make PyGC_Collect() use Py_ssize_t.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36769
diff changeset
883 Py_ssize_t n = 0;
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
884
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
885 /* Find the oldest generation (higest numbered) where the 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
886 * 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
887 * 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
888 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
889 if (generations[i].count > generations[i].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
890 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
891 break;
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
892 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
893 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
894 return n;
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
895 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
896
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23721
diff changeset
897 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
898 "enable() -> None\n"
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13570
diff changeset
899 "\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23721
diff changeset
900 "Enable automatic garbage collection.\n");
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
901
13878
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13570
diff changeset
902 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
903 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
904 {
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13570
diff changeset
905 enabled = 1;
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13570
diff changeset
906 Py_INCREF(Py_None);
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13570
diff changeset
907 return Py_None;
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13570
diff changeset
908 }
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13570
diff changeset
909
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23721
diff changeset
910 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
911 "disable() -> None\n"
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13570
diff changeset
912 "\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23721
diff changeset
913 "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
914
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13570
diff changeset
915 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
916 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
917 {
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13570
diff changeset
918 enabled = 0;
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13570
diff changeset
919 Py_INCREF(Py_None);
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13570
diff changeset
920 return Py_None;
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13570
diff changeset
921 }
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13570
diff changeset
922
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23721
diff changeset
923 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
924 "isenabled() -> status\n"
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13570
diff changeset
925 "\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23721
diff changeset
926 "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
927
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13570
diff changeset
928 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
929 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
930 {
31341
4915fbec608f Convert return value to boolean.
Raymond Hettinger <python@rcn.com>
parents: 31010
diff changeset
931 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
932 }
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13570
diff changeset
933
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23721
diff changeset
934 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
935 "collect([generation]) -> n\n"
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
936 "\n"
36863
2d24f3a37a1b SF patch #1443865; gc.get_count() added and optional argument 'generation'
Barry Warsaw <barry@python.org>
parents: 36860
diff changeset
937 "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
938 "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
939 "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
940 "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
941
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
942 static PyObject *
36863
2d24f3a37a1b SF patch #1443865; gc.get_count() added and optional argument 'generation'
Barry Warsaw <barry@python.org>
parents: 36860
diff changeset
943 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
944 {
36863
2d24f3a37a1b SF patch #1443865; gc.get_count() added and optional argument 'generation'
Barry Warsaw <barry@python.org>
parents: 36860
diff changeset
945 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
946 int genarg = NUM_GENERATIONS - 1;
36839
7c1aca8e3d7d Make PyGC_Collect() use Py_ssize_t.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36769
diff changeset
947 Py_ssize_t n;
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
948
36863
2d24f3a37a1b SF patch #1443865; gc.get_count() added and optional argument 'generation'
Barry Warsaw <barry@python.org>
parents: 36860
diff changeset
949 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
950 return NULL;
2d24f3a37a1b SF patch #1443865; gc.get_count() added and optional argument 'generation'
Barry Warsaw <barry@python.org>
parents: 36860
diff changeset
951
2d24f3a37a1b SF patch #1443865; gc.get_count() added and optional argument 'generation'
Barry Warsaw <barry@python.org>
parents: 36860
diff changeset
952 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
953 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
954 return NULL;
2d24f3a37a1b SF patch #1443865; gc.get_count() added and optional argument 'generation'
Barry Warsaw <barry@python.org>
parents: 36860
diff changeset
955 }
2d24f3a37a1b SF patch #1443865; gc.get_count() added and optional argument 'generation'
Barry Warsaw <barry@python.org>
parents: 36860
diff changeset
956
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
957 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
958 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
959 else {
9c23abab962d Make the gc.collect() function respect the collection lock. This fixes
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20393
diff changeset
960 collecting = 1;
36863
2d24f3a37a1b SF patch #1443865; gc.get_count() added and optional argument 'generation'
Barry Warsaw <barry@python.org>
parents: 36860
diff changeset
961 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
962 collecting = 0;
9c23abab962d Make the gc.collect() function respect the collection lock. This fixes
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20393
diff changeset
963 }
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
964
44099
f324631462a2 Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents: 42750
diff changeset
965 return PyLong_FromSsize_t(n);
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
966 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
967
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23721
diff changeset
968 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
969 "set_debug(flags) -> None\n"
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
970 "\n"
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
971 "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
972 "written to sys.stderr.\n"
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
973 "\n"
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
974 "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
975 "\n"
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
976 " 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
977 " 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
978 " DEBUG_UNCOLLECTABLE - Print unreachable but uncollectable objects found.\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
979 " 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
980 " 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
981
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
982 static PyObject *
13878
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13570
diff changeset
983 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
984 {
14774
52174b002f1f Fix some long/"l" int/"i" mismatches. Fixes bug #113779.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 14756
diff changeset
985 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
986 return NULL;
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
987
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
988 Py_INCREF(Py_None);
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
989 return Py_None;
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
990 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
991
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23721
diff changeset
992 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
993 "get_debug() -> flags\n"
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
994 "\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23721
diff changeset
995 "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
996
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
997 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
998 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
999 {
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1000 return Py_BuildValue("i", debug);
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_set_thresh__doc__,
21833
070d392042e3 Fix spelling mistakes. Bugfix candidates.
Neal Norwitz <nnorwitz@gmail.com>
parents: 21139
diff changeset
1004 "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
1005 "\n"
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1006 "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
1007 "collection.\n");
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1008
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1009 static PyObject *
13878
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13570
diff changeset
1010 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
1011 {
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
1012 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
1013 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
1014 &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
1015 &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
1016 &generations[2].threshold))
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1017 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
1018 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
1019 /* 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
1020 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
1021 }
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1022
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1023 Py_INCREF(Py_None);
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1024 return Py_None;
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1025 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1026
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23721
diff changeset
1027 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
1028 "get_threshold() -> (threshold0, threshold1, threshold2)\n"
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1029 "\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23721
diff changeset
1030 "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
1031
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1032 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
1033 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
1034 {
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
1035 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
1036 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
1037 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
1038 generations[2].threshold);
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1039 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1040
36863
2d24f3a37a1b SF patch #1443865; gc.get_count() added and optional argument 'generation'
Barry Warsaw <barry@python.org>
parents: 36860
diff changeset
1041 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
1042 "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
1043 "\n"
2d24f3a37a1b SF patch #1443865; gc.get_count() added and optional argument 'generation'
Barry Warsaw <barry@python.org>
parents: 36860
diff changeset
1044 "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
1045
2d24f3a37a1b SF patch #1443865; gc.get_count() added and optional argument 'generation'
Barry Warsaw <barry@python.org>
parents: 36860
diff changeset
1046 static PyObject *
2d24f3a37a1b SF patch #1443865; gc.get_count() added and optional argument 'generation'
Barry Warsaw <barry@python.org>
parents: 36860
diff changeset
1047 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
1048 {
2d24f3a37a1b SF patch #1443865; gc.get_count() added and optional argument 'generation'
Barry Warsaw <barry@python.org>
parents: 36860
diff changeset
1049 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
1050 generations[0].count,
2d24f3a37a1b SF patch #1443865; gc.get_count() added and optional argument 'generation'
Barry Warsaw <barry@python.org>
parents: 36860
diff changeset
1051 generations[1].count,
2d24f3a37a1b SF patch #1443865; gc.get_count() added and optional argument 'generation'
Barry Warsaw <barry@python.org>
parents: 36860
diff changeset
1052 generations[2].count);
2d24f3a37a1b SF patch #1443865; gc.get_count() added and optional argument 'generation'
Barry Warsaw <barry@python.org>
parents: 36860
diff changeset
1053 }
2d24f3a37a1b SF patch #1443865; gc.get_count() added and optional argument 'generation'
Barry Warsaw <barry@python.org>
parents: 36860
diff changeset
1054
19096
45ccf3b889b5 Add get_referents function. Closes SF patch #402925.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15013
diff changeset
1055 static int
21038
afb789aada16 Rename get_referents to get_referrers. Fixes #483815.
Martin v. Löwis <martin@v.loewis.de>
parents: 20820
diff changeset
1056 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
1057 {
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36863
diff changeset
1058 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
1059 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
1060 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
1061 return 1;
19096
45ccf3b889b5 Add get_referents function. Closes SF patch #402925.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15013
diff changeset
1062 return 0;
45ccf3b889b5 Add get_referents function. Closes SF patch #402925.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15013
diff changeset
1063 }
45ccf3b889b5 Add get_referents function. Closes SF patch #402925.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15013
diff changeset
1064
19128
4901a935490d Remove "referents" structure (it's not needed). Check return value
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19097
diff changeset
1065 static int
21038
afb789aada16 Rename get_referents to get_referrers. Fixes #483815.
Martin v. Löwis <martin@v.loewis.de>
parents: 20820
diff changeset
1066 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
1067 {
45ccf3b889b5 Add get_referents function. Closes SF patch #402925.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15013
diff changeset
1068 PyGC_Head *gc;
45ccf3b889b5 Add get_referents function. Closes SF patch #402925.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15013
diff changeset
1069 PyObject *obj;
45ccf3b889b5 Add get_referents function. Closes SF patch #402925.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15013
diff changeset
1070 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
1071 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
1072 obj = FROM_GC(gc);
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44265
diff changeset
1073 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
1074 if (obj == objs || obj == resultlist)
45ccf3b889b5 Add get_referents function. Closes SF patch #402925.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15013
diff changeset
1075 continue;
21038
afb789aada16 Rename get_referents to get_referrers. Fixes #483815.
Martin v. Löwis <martin@v.loewis.de>
parents: 20820
diff changeset
1076 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
1077 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
1078 return 0; /* error */
19096
45ccf3b889b5 Add get_referents function. Closes SF patch #402925.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15013
diff changeset
1079 }
45ccf3b889b5 Add get_referents function. Closes SF patch #402925.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15013
diff changeset
1080 }
19128
4901a935490d Remove "referents" structure (it's not needed). Check return value
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19097
diff changeset
1081 return 1; /* no error */
19096
45ccf3b889b5 Add get_referents function. Closes SF patch #402925.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15013
diff changeset
1082 }
45ccf3b889b5 Add get_referents function. Closes SF patch #402925.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15013
diff changeset
1083
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23721
diff changeset
1084 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
1085 "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
1086 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
1087
19128
4901a935490d Remove "referents" structure (it's not needed). Check return value
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19097
diff changeset
1088 static PyObject *
21038
afb789aada16 Rename get_referents to get_referrers. Fixes #483815.
Martin v. Löwis <martin@v.loewis.de>
parents: 20820
diff changeset
1089 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
1090 {
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
1091 int i;
19096
45ccf3b889b5 Add get_referents function. Closes SF patch #402925.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15013
diff changeset
1092 PyObject *result = PyList_New(0);
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36863
diff changeset
1093 if (!result) return NULL;
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36863
diff changeset
1094
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
1095 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
1096 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
1097 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
1098 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
1099 }
19128
4901a935490d Remove "referents" structure (it's not needed). Check return value
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19097
diff changeset
1100 }
19096
45ccf3b889b5 Add get_referents function. Closes SF patch #402925.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15013
diff changeset
1101 return result;
45ccf3b889b5 Add get_referents function. Closes SF patch #402925.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15013
diff changeset
1102 }
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1103
28522
487df294d328 Finished implementing gc.get_referrents(): dealt with error and end
Tim Peters <tim.peters@gmail.com>
parents: 28521
diff changeset
1104 /* 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
1105 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
1106 referentsvisit(PyObject *obj, PyObject *list)
28497
40d9659b56c9 Add get_referrents() helper function.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26138
diff changeset
1107 {
28522
487df294d328 Finished implementing gc.get_referrents(): dealt with error and end
Tim Peters <tim.peters@gmail.com>
parents: 28521
diff changeset
1108 return PyList_Append(list, obj) < 0;
28497
40d9659b56c9 Add get_referrents() helper function.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26138
diff changeset
1109 }
40d9659b56c9 Add get_referrents() helper function.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26138
diff changeset
1110
28523
c01fdc3a9180 s/referrents/referents/g. Gotta love that referrers remains rife with rs.
Tim Peters <tim.peters@gmail.com>
parents: 28522
diff changeset
1111 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
1112 "get_referents(*objs) -> list\n\
28498
3e399a8618db Add get_referrents() helper function.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28497
diff changeset
1113 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
1114
40d9659b56c9 Add get_referrents() helper function.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26138
diff changeset
1115 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
1116 gc_get_referents(PyObject *self, PyObject *args)
28497
40d9659b56c9 Add get_referrents() helper function.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26138
diff changeset
1117 {
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36863
diff changeset
1118 Py_ssize_t i;
28497
40d9659b56c9 Add get_referrents() helper function.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26138
diff changeset
1119 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
1120
487df294d328 Finished implementing gc.get_referrents(): dealt with error and end
Tim Peters <tim.peters@gmail.com>
parents: 28521
diff changeset
1121 if (result == NULL)
487df294d328 Finished implementing gc.get_referrents(): dealt with error and end
Tim Peters <tim.peters@gmail.com>
parents: 28521
diff changeset
1122 return NULL;
487df294d328 Finished implementing gc.get_referrents(): dealt with error and end
Tim Peters <tim.peters@gmail.com>
parents: 28521
diff changeset
1123
28497
40d9659b56c9 Add get_referrents() helper function.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26138
diff changeset
1124 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
1125 traverseproc traverse;
28507
a0503bdb0c24 Fixed new seemingly random segfaults, by moving the initialization of
Tim Peters <tim.peters@gmail.com>
parents: 28504
diff changeset
1126 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
1127
487df294d328 Finished implementing gc.get_referrents(): dealt with error and end
Tim Peters <tim.peters@gmail.com>
parents: 28521
diff changeset
1128 if (! PyObject_IS_GC(obj))
28497
40d9659b56c9 Add get_referrents() helper function.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26138
diff changeset
1129 continue;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44265
diff changeset
1130 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
1131 if (! traverse)
487df294d328 Finished implementing gc.get_referrents(): dealt with error and end
Tim Peters <tim.peters@gmail.com>
parents: 28521
diff changeset
1132 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
1133 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
1134 Py_DECREF(result);
28497
40d9659b56c9 Add get_referrents() helper function.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26138
diff changeset
1135 return NULL;
28522
487df294d328 Finished implementing gc.get_referrents(): dealt with error and end
Tim Peters <tim.peters@gmail.com>
parents: 28521
diff changeset
1136 }
28497
40d9659b56c9 Add get_referrents() helper function.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26138
diff changeset
1137 }
40d9659b56c9 Add get_referrents() helper function.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26138
diff changeset
1138 return result;
40d9659b56c9 Add get_referrents() helper function.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26138
diff changeset
1139 }
40d9659b56c9 Add get_referrents() helper function.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26138
diff changeset
1140
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23721
diff changeset
1141 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
1142 "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
1143 "\n"
9e6f7804ef14 Add get_objects function. This is a low level function (like
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19096
diff changeset
1144 "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
1145 "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
1146
9e6f7804ef14 Add get_objects function. This is a low level function (like
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19096
diff changeset
1147 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
1148 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
1149 {
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
1150 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
1151 PyObject* result;
9e6f7804ef14 Add get_objects function. This is a low level function (like
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19096
diff changeset
1152
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
1153 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
1154 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
1155 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
1156 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
1157 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
1158 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
1159 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
1160 }
21130
6e34151aefe6 Patch #486743: remove bad INCREF, propagate exception in append_objects.
Martin v. Löwis <martin@v.loewis.de>
parents: 21105
diff changeset
1161 }
19097
9e6f7804ef14 Add get_objects function. This is a low level function (like
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19096
diff changeset
1162 return result;
9e6f7804ef14 Add get_objects function. This is a low level function (like
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19096
diff changeset
1163 }
9e6f7804ef14 Add get_objects function. This is a low level function (like
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19096
diff changeset
1164
9e6f7804ef14 Add get_objects function. This is a low level function (like
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19096
diff changeset
1165
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23721
diff changeset
1166 PyDoc_STRVAR(gc__doc__,
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1167 "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
1168 "\n"
13878
abfa4e4b2f3e Neil Schemenauer: GC enable(), disable(), isenabled() interface.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13570
diff changeset
1169 "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
1170 "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
1171 "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
1172 "collect() -- Do a full collection right now.\n"
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39620
diff changeset
1173 "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
1174 "set_debug() -- Set debugging flags.\n"
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1175 "get_debug() -- Get debugging flags.\n"
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1176 "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
1177 "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
1178 "get_objects() -- Return a list of all objects tracked by the collector.\n"
28497
40d9659b56c9 Add get_referrents() helper function.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26138
diff changeset
1179 "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
1180 "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
1181
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1182 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
1183 {"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
1184 {"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
1185 {"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
1186 {"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
1187 {"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
1188 {"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
1189 {"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
1190 {"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
1191 {"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
1192 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
1193 {"get_objects", gc_get_objects,METH_NOARGS, gc_get_objects__doc__},
21038
afb789aada16 Rename get_referents to get_referrers. Fixes #483815.
Martin v. Löwis <martin@v.loewis.de>
parents: 20820
diff changeset
1194 {"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
1195 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
1196 {"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
1197 gc_get_referents__doc__},
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1198 {NULL, NULL} /* Sentinel */
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1199 };
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1200
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 45371
diff changeset
1201 static struct PyModuleDef gcmodule = {
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 45371
diff changeset
1202 PyModuleDef_HEAD_INIT,
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 45371
diff changeset
1203 "gc",
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 45371
diff changeset
1204 gc__doc__,
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 45371
diff changeset
1205 -1,
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 45371
diff changeset
1206 GcMethods,
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 45371
diff changeset
1207 NULL,
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 45371
diff changeset
1208 NULL,
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 45371
diff changeset
1209 NULL,
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 45371
diff changeset
1210 NULL
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 45371
diff changeset
1211 };
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 45371
diff changeset
1212
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 45371
diff changeset
1213
30311
f003e48f562d Bug #794140: cygwin builds do not embed
Jason Tishler <jason@tishler.net>
parents: 28651
diff changeset
1214 PyMODINIT_FUNC
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 45371
diff changeset
1215 PyInit_gc(void)
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1216 {
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1217 PyObject *m;
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1218
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 45371
diff changeset
1219 m = PyModule_Create(&gcmodule);
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 45371
diff changeset
1220
36324
1af7f719ed28 Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35298
diff changeset
1221 if (m == NULL)
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 45371
diff changeset
1222 return NULL;
28516
78f846fb570a initgc(): Rewrote to use the PyModule_AddXYZ API; cuts code size.
Tim Peters <tim.peters@gmail.com>
parents: 28515
diff changeset
1223
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1224 if (garbage == NULL) {
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1225 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
1226 if (garbage == NULL)
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 45371
diff changeset
1227 return NULL;
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1228 }
35103
234b22a2acf3 Add missing INCREF.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 33876
diff changeset
1229 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
1230 if (PyModule_AddObject(m, "garbage", garbage) < 0)
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 45371
diff changeset
1231 return NULL;
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1232
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1233 /* Importing can't be done in collect() because collect()
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1234 * can be called via PyGC_Collect() in Py_Finalize().
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1235 * This wouldn't be a problem, except that <initialized> is
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1236 * reset to 0 before calling collect which trips up
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1237 * the import and triggers an assertion.
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1238 */
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1239 if (tmod == NULL) {
44441
b91ca87e6f32 Merged revisions 59666-59679 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
1240 tmod = PyImport_ImportModuleNoBlock("time");
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1241 if (tmod == NULL)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1242 PyErr_Clear();
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1243 }
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1244
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 45371
diff changeset
1245 #define ADD_INT(NAME) if (PyModule_AddIntConstant(m, #NAME, NAME) < 0) return NULL
28516
78f846fb570a initgc(): Rewrote to use the PyModule_AddXYZ API; cuts code size.
Tim Peters <tim.peters@gmail.com>
parents: 28515
diff changeset
1246 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
1247 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
1248 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
1249 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
1250 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
1251 #undef ADD_INT
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 45371
diff changeset
1252 return m;
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1253 }
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1254
28651
7fda09f751ad - New C API PyGC_Collect(), same as calling gc.collect().
Guido van Rossum <guido@python.org>
parents: 28523
diff changeset
1255 /* 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
1256 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
1257 PyGC_Collect(void)
7fda09f751ad - New C API PyGC_Collect(), same as calling gc.collect().
Guido van Rossum <guido@python.org>
parents: 28523
diff changeset
1258 {
36839
7c1aca8e3d7d Make PyGC_Collect() use Py_ssize_t.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36769
diff changeset
1259 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
1260
7fda09f751ad - New C API PyGC_Collect(), same as calling gc.collect().
Guido van Rossum <guido@python.org>
parents: 28523
diff changeset
1261 if (collecting)
7fda09f751ad - New C API PyGC_Collect(), same as calling gc.collect().
Guido van Rossum <guido@python.org>
parents: 28523
diff changeset
1262 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
1263 else {
7fda09f751ad - New C API PyGC_Collect(), same as calling gc.collect().
Guido van Rossum <guido@python.org>
parents: 28523
diff changeset
1264 collecting = 1;
7fda09f751ad - New C API PyGC_Collect(), same as calling gc.collect().
Guido van Rossum <guido@python.org>
parents: 28523
diff changeset
1265 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
1266 collecting = 0;
7fda09f751ad - New C API PyGC_Collect(), same as calling gc.collect().
Guido van Rossum <guido@python.org>
parents: 28523
diff changeset
1267 }
7fda09f751ad - New C API PyGC_Collect(), same as calling gc.collect().
Guido van Rossum <guido@python.org>
parents: 28523
diff changeset
1268
7fda09f751ad - New C API PyGC_Collect(), same as calling gc.collect().
Guido van Rossum <guido@python.org>
parents: 28523
diff changeset
1269 return n;
7fda09f751ad - New C API PyGC_Collect(), same as calling gc.collect().
Guido van Rossum <guido@python.org>
parents: 28523
diff changeset
1270 }
7fda09f751ad - New C API PyGC_Collect(), same as calling gc.collect().
Guido van Rossum <guido@python.org>
parents: 28523
diff changeset
1271
19585
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1272 /* for debugging */
28651
7fda09f751ad - New C API PyGC_Collect(), same as calling gc.collect().
Guido van Rossum <guido@python.org>
parents: 28523
diff changeset
1273 void
7fda09f751ad - New C API PyGC_Collect(), same as calling gc.collect().
Guido van Rossum <guido@python.org>
parents: 28523
diff changeset
1274 _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
1275 {
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1276 _PyObject_Dump(FROM_GC(g));
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1277 }
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1278
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1279 /* 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
1280 functions must always be available */
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1281
22972
95819f7f0bcd Allow PyObject_Del to be used as a function designator. Provide binary
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22648
diff changeset
1282 #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
1283 #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
1284 #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
1285 #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
1286
19585
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1287 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
1288 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
1289 {
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1290 _PyObject_GC_TRACK(op);
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1291 }
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1292
22972
95819f7f0bcd Allow PyObject_Del to be used as a function designator. Provide binary
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22648
diff changeset
1293 /* 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
1294 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
1295 _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
1296 {
95819f7f0bcd Allow PyObject_Del to be used as a function designator. Provide binary
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22648
diff changeset
1297 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
1298 }
95819f7f0bcd Allow PyObject_Del to be used as a function designator. Provide binary
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22648
diff changeset
1299
95819f7f0bcd Allow PyObject_Del to be used as a function designator. Provide binary
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22648
diff changeset
1300 void
95819f7f0bcd Allow PyObject_Del to be used as a function designator. Provide binary
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22648
diff changeset
1301 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
1302 {
24122
d332484e394a Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents: 24121
diff changeset
1303 /* 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
1304 * 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
1305 */
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
1306 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
1307 _PyObject_GC_UNTRACK(op);
19585
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1308 }
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1309
22972
95819f7f0bcd Allow PyObject_Del to be used as a function designator. Provide binary
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22648
diff changeset
1310 /* 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
1311 void
95819f7f0bcd Allow PyObject_Del to be used as a function designator. Provide binary
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22648
diff changeset
1312 _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
1313 {
95819f7f0bcd Allow PyObject_Del to be used as a function designator. Provide binary
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22648
diff changeset
1314 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
1315 }
95819f7f0bcd Allow PyObject_Del to be used as a function designator. Provide binary
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22648
diff changeset
1316
19585
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1317 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
1318 _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
1319 {
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1320 PyObject *op;
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36863
diff changeset
1321 PyGC_Head *g = (PyGC_Head *)PyObject_MALLOC(
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36863
diff changeset
1322 sizeof(PyGC_Head) + basicsize);
19585
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1323 if (g == NULL)
23721
48e937e1b3fe Remove casts to PyObject * when declaration is for PyObject *
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23495
diff changeset
1324 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
1325 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
1326 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
1327 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
1328 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
1329 generations[0].threshold &&
19585
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1330 !collecting &&
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1331 !PyErr_Occurred()) {
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1332 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
1333 collect_generations();
19585
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1334 collecting = 0;
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1335 }
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1336 op = FROM_GC(g);
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1337 return op;
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1338 }
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1339
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1340 PyObject *
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1341 _PyObject_GC_New(PyTypeObject *tp)
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1342 {
22972
95819f7f0bcd Allow PyObject_Del to be used as a function designator. Provide binary
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22648
diff changeset
1343 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
1344 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
1345 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
1346 return op;
19585
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1347 }
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1348
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1349 PyVarObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1350 _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
1351 {
22972
95819f7f0bcd Allow PyObject_Del to be used as a function designator. Provide binary
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22648
diff changeset
1352 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
1353 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
1354 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
1355 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
1356 return op;
19585
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1357 }
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1358
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1359 PyVarObject *
36524
4c8606191c0c Change _PyObject_GC_Resize to expect Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
1360 _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
1361 {
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44265
diff changeset
1362 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
1363 PyGC_Head *g = AS_GC(op);
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36863
diff changeset
1364 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
1365 if (g == NULL)
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1366 return (PyVarObject *)PyErr_NoMemory();
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1367 op = (PyVarObject *) FROM_GC(g);
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44265
diff changeset
1368 Py_SIZE(op) = nitems;
19585
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1369 return op;
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1370 }
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1371
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1372 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
1373 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
1374 {
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1375 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
1376 if (IS_TRACKED(op))
19585
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1377 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
1378 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
1379 generations[0].count--;
19585
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1380 }
22972
95819f7f0bcd Allow PyObject_Del to be used as a function designator. Provide binary
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22648
diff changeset
1381 PyObject_FREE(g);
19585
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1382 }
c43d7a8f00a4 Make more things internal to this file. Remove
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19128
diff changeset
1383
22972
95819f7f0bcd Allow PyObject_Del to be used as a function designator. Provide binary
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22648
diff changeset
1384 /* 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
1385 #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
1386 void
95819f7f0bcd Allow PyObject_Del to be used as a function designator. Provide binary
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22648
diff changeset
1387 _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
1388 {
95819f7f0bcd Allow PyObject_Del to be used as a function designator. Provide binary
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22648
diff changeset
1389 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
1390 }