annotate cos/python/Modules/gcmodule.c @ 126:bbf4c9b138d4

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