blob: b334f9db6b66fb86dc96d99bacef80ca0549c922 [file] [log] [blame]
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001/*
2
3 Reference Cycle Garbage Collection
4 ==================================
5
Neil Schemenauerb2c2c9e2000-10-04 16:34:09 +00006 Neil Schemenauer <nas@arctrix.com>
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00007
8 Based on a post on the python-dev list. Ideas from Guido van Rossum,
9 Eric Tiedemann, and various others.
10
Neil Schemenauer43411b52001-08-30 00:05:51 +000011 http://www.arctrix.com/nas/python/gc/
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000012 http://www.python.org/pipermail/python-dev/2000-March/003869.html
13 http://www.python.org/pipermail/python-dev/2000-March/004010.html
14 http://www.python.org/pipermail/python-dev/2000-March/004022.html
15
16 For a highlevel view of the collection process, read the collect
17 function.
18
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000019*/
20
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000021#include "Python.h"
22
23#ifdef WITH_CYCLE_GC
24
Neil Schemenauer43411b52001-08-30 00:05:51 +000025/* Get an object's GC head */
26#define AS_GC(o) ((PyGC_Head *)(o)-1)
27
28/* Get the object given the GC head */
29#define FROM_GC(g) ((PyObject *)(((PyGC_Head *)g)+1))
30
Neil Schemenauera2b11ec2002-05-21 15:53:24 +000031/* True if an object is tracked by the GC */
32#define IS_TRACKED(o) ((AS_GC(o))->gc.gc_next != NULL)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000033
34/*** Global GC state ***/
35
Neil Schemenauer2880ae52002-05-04 05:35:20 +000036struct gc_generation {
37 PyGC_Head head;
38 int threshold; /* collection threshold */
39 int count; /* count of allocations or collections of younger
40 generations */
41};
42
43#define NUM_GENERATIONS 3
44#define GEN_HEAD(n) (&generations[n].head)
45
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000046/* linked lists of container objects */
Neil Schemenauer2880ae52002-05-04 05:35:20 +000047static struct gc_generation generations[NUM_GENERATIONS] = {
48 /* PyGC_Head, threshold, count */
49 {{{GEN_HEAD(0), GEN_HEAD(0), 0}}, 700, 0},
50 {{{GEN_HEAD(1), GEN_HEAD(1), 0}}, 10, 0},
51 {{{GEN_HEAD(2), GEN_HEAD(2), 0}}, 10, 0},
52};
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000053
Neil Schemenauer2880ae52002-05-04 05:35:20 +000054PyGC_Head *_PyGC_generation0 = GEN_HEAD(0);
55
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +000056static int enabled = 1; /* automatic collection enabled? */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000057
Neil Schemenauer43411b52001-08-30 00:05:51 +000058/* true if we are currently running the collector */
59static int collecting;
60
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000061/* set for debugging information */
62#define DEBUG_STATS (1<<0) /* print collection statistics */
63#define DEBUG_COLLECTABLE (1<<1) /* print collectable objects */
64#define DEBUG_UNCOLLECTABLE (1<<2) /* print uncollectable objects */
65#define DEBUG_INSTANCES (1<<3) /* print instances */
66#define DEBUG_OBJECTS (1<<4) /* print other objects */
Neil Schemenauer544de1e2000-09-22 15:22:38 +000067#define DEBUG_SAVEALL (1<<5) /* save all garbage in gc.garbage */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000068#define DEBUG_LEAK DEBUG_COLLECTABLE | \
69 DEBUG_UNCOLLECTABLE | \
70 DEBUG_INSTANCES | \
Neil Schemenauer544de1e2000-09-22 15:22:38 +000071 DEBUG_OBJECTS | \
72 DEBUG_SAVEALL
Jeremy Hyltonb709df32000-09-01 02:47:25 +000073static int debug;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000074
Neil Schemenauer43411b52001-08-30 00:05:51 +000075/* Special gc_refs value */
76#define GC_MOVED -123
77
Neil Schemenauera2b11ec2002-05-21 15:53:24 +000078/* True if an object has been moved to the older generation */
79#define IS_MOVED(o) ((AS_GC(o))->gc.gc_refs == GC_MOVED)
80
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000081/* list of uncollectable objects */
82static PyObject *garbage;
83
Jeremy Hyltonb709df32000-09-01 02:47:25 +000084/* Python string to use if unhandled exception occurs */
85static PyObject *gc_str;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000086
87/*** list functions ***/
88
89static void
90gc_list_init(PyGC_Head *list)
91{
Tim Peters9e4ca102001-10-11 18:31:31 +000092 list->gc.gc_prev = list;
93 list->gc.gc_next = list;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000094}
95
Neil Schemenauer2880ae52002-05-04 05:35:20 +000096static int
97gc_list_is_empty(PyGC_Head *list)
98{
99 return (list->gc.gc_next == list);
100}
101
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000102static void
103gc_list_append(PyGC_Head *node, PyGC_Head *list)
104{
Tim Peters9e4ca102001-10-11 18:31:31 +0000105 node->gc.gc_next = list;
106 node->gc.gc_prev = list->gc.gc_prev;
107 node->gc.gc_prev->gc.gc_next = node;
108 list->gc.gc_prev = node;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000109}
110
111static void
112gc_list_remove(PyGC_Head *node)
113{
Tim Peters9e4ca102001-10-11 18:31:31 +0000114 node->gc.gc_prev->gc.gc_next = node->gc.gc_next;
115 node->gc.gc_next->gc.gc_prev = node->gc.gc_prev;
116 node->gc.gc_next = NULL; /* object is not currently tracked */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000117}
118
119static void
120gc_list_move(PyGC_Head *from, PyGC_Head *to)
121{
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000122 if (gc_list_is_empty(from)) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000123 gc_list_init(to);
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000124 }
125 else {
Tim Peters9e4ca102001-10-11 18:31:31 +0000126 to->gc.gc_next = from->gc.gc_next;
127 to->gc.gc_next->gc.gc_prev = to;
128 to->gc.gc_prev = from->gc.gc_prev;
129 to->gc.gc_prev->gc.gc_next = to;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000130 }
131 gc_list_init(from);
132}
133
134/* append a list onto another list, from becomes an empty list */
135static void
136gc_list_merge(PyGC_Head *from, PyGC_Head *to)
137{
138 PyGC_Head *tail;
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000139 if (!gc_list_is_empty(from)) {
Tim Peters9e4ca102001-10-11 18:31:31 +0000140 tail = to->gc.gc_prev;
141 tail->gc.gc_next = from->gc.gc_next;
142 tail->gc.gc_next->gc.gc_prev = tail;
143 to->gc.gc_prev = from->gc.gc_prev;
144 to->gc.gc_prev->gc.gc_next = to;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000145 }
146 gc_list_init(from);
147}
148
149static long
150gc_list_size(PyGC_Head *list)
151{
152 PyGC_Head *gc;
153 long n = 0;
Tim Peters9e4ca102001-10-11 18:31:31 +0000154 for (gc = list->gc.gc_next; gc != list; gc = gc->gc.gc_next) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000155 n++;
156 }
157 return n;
158}
159
160/*** end of list stuff ***/
161
162
Neil Schemenauer43411b52001-08-30 00:05:51 +0000163
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000164/* Set all gc_refs = ob_refcnt */
165static void
166update_refs(PyGC_Head *containers)
167{
Tim Peters9e4ca102001-10-11 18:31:31 +0000168 PyGC_Head *gc = containers->gc.gc_next;
169 for (; gc != containers; gc=gc->gc.gc_next) {
170 gc->gc.gc_refs = FROM_GC(gc)->ob_refcnt;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000171 }
172}
173
174static int
175visit_decref(PyObject *op, void *data)
176{
177 if (op && PyObject_IS_GC(op)) {
Neil Schemenauera2b11ec2002-05-21 15:53:24 +0000178 if (IS_TRACKED(op))
Tim Peters9e4ca102001-10-11 18:31:31 +0000179 AS_GC(op)->gc.gc_refs--;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000180 }
181 return 0;
182}
183
184/* Subtract internal references from gc_refs */
185static void
186subtract_refs(PyGC_Head *containers)
187{
188 traverseproc traverse;
Tim Peters9e4ca102001-10-11 18:31:31 +0000189 PyGC_Head *gc = containers->gc.gc_next;
190 for (; gc != containers; gc=gc->gc.gc_next) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000191 traverse = FROM_GC(gc)->ob_type->tp_traverse;
192 (void) traverse(FROM_GC(gc),
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000193 (visitproc)visit_decref,
194 NULL);
195 }
196}
197
198/* Append objects with gc_refs > 0 to roots list */
199static void
200move_roots(PyGC_Head *containers, PyGC_Head *roots)
201{
202 PyGC_Head *next;
Tim Peters9e4ca102001-10-11 18:31:31 +0000203 PyGC_Head *gc = containers->gc.gc_next;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000204 while (gc != containers) {
Tim Peters9e4ca102001-10-11 18:31:31 +0000205 next = gc->gc.gc_next;
206 if (gc->gc.gc_refs > 0) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000207 gc_list_remove(gc);
208 gc_list_append(gc, roots);
Tim Peters9e4ca102001-10-11 18:31:31 +0000209 gc->gc.gc_refs = GC_MOVED;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000210 }
211 gc = next;
212 }
213}
214
215static int
Neil Schemenauer43411b52001-08-30 00:05:51 +0000216visit_move(PyObject *op, PyGC_Head *tolist)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000217{
218 if (PyObject_IS_GC(op)) {
Neil Schemenauera2b11ec2002-05-21 15:53:24 +0000219 if (IS_TRACKED(op) && !IS_MOVED(op)) {
220 PyGC_Head *gc = AS_GC(op);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000221 gc_list_remove(gc);
Neil Schemenauer43411b52001-08-30 00:05:51 +0000222 gc_list_append(gc, tolist);
Tim Peters9e4ca102001-10-11 18:31:31 +0000223 gc->gc.gc_refs = GC_MOVED;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000224 }
225 }
226 return 0;
227}
228
229/* Move objects referenced from reachable to reachable set. */
230static void
231move_root_reachable(PyGC_Head *reachable)
232{
233 traverseproc traverse;
Tim Peters9e4ca102001-10-11 18:31:31 +0000234 PyGC_Head *gc = reachable->gc.gc_next;
235 for (; gc != reachable; gc=gc->gc.gc_next) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000236 /* careful, reachable list is growing here */
Neil Schemenauer43411b52001-08-30 00:05:51 +0000237 PyObject *op = FROM_GC(gc);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000238 traverse = op->ob_type->tp_traverse;
239 (void) traverse(op,
Neil Schemenauer43411b52001-08-30 00:05:51 +0000240 (visitproc)visit_move,
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000241 (void *)reachable);
242 }
243}
244
Neil Schemenauera765c122001-11-01 17:35:23 +0000245/* return true of object has a finalization method */
246static int
247has_finalizer(PyObject *op)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000248{
Jeremy Hylton06257772000-08-31 15:10:24 +0000249 static PyObject *delstr = NULL;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000250 if (delstr == NULL) {
251 delstr = PyString_InternFromString("__del__");
Jeremy Hylton06257772000-08-31 15:10:24 +0000252 if (delstr == NULL)
253 Py_FatalError("PyGC: can't initialize __del__ string");
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000254 }
Tim Petersdb865612001-11-01 19:35:45 +0000255 return (PyInstance_Check(op) ||
256 PyType_HasFeature(op->ob_type, Py_TPFLAGS_HEAPTYPE))
257 && PyObject_HasAttr(op, delstr);
Neil Schemenauera765c122001-11-01 17:35:23 +0000258}
259
260/* Move all objects with finalizers (instances with __del__) */
261static void
262move_finalizers(PyGC_Head *unreachable, PyGC_Head *finalizers)
263{
264 PyGC_Head *next;
265 PyGC_Head *gc = unreachable->gc.gc_next;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000266 for (; gc != unreachable; gc=next) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000267 PyObject *op = FROM_GC(gc);
Tim Peters9e4ca102001-10-11 18:31:31 +0000268 next = gc->gc.gc_next;
Neil Schemenauera765c122001-11-01 17:35:23 +0000269 if (has_finalizer(op)) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000270 gc_list_remove(gc);
271 gc_list_append(gc, finalizers);
272 }
273 }
274}
275
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000276/* Move objects referenced from roots to roots */
277static void
278move_finalizer_reachable(PyGC_Head *finalizers)
279{
280 traverseproc traverse;
Tim Peters9e4ca102001-10-11 18:31:31 +0000281 PyGC_Head *gc = finalizers->gc.gc_next;
282 for (; gc != finalizers; gc=gc->gc.gc_next) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000283 /* careful, finalizers list is growing here */
Neil Schemenauer43411b52001-08-30 00:05:51 +0000284 traverse = FROM_GC(gc)->ob_type->tp_traverse;
285 (void) traverse(FROM_GC(gc),
286 (visitproc)visit_move,
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000287 (void *)finalizers);
288 }
289}
290
291static void
Jeremy Hylton06257772000-08-31 15:10:24 +0000292debug_instance(char *msg, PyInstanceObject *inst)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000293{
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000294 char *cname;
Neil Schemenauera765c122001-11-01 17:35:23 +0000295 /* simple version of instance_repr */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000296 PyObject *classname = inst->in_class->cl_name;
297 if (classname != NULL && PyString_Check(classname))
298 cname = PyString_AsString(classname);
299 else
300 cname = "?";
Jeremy Hylton06257772000-08-31 15:10:24 +0000301 PySys_WriteStderr("gc: %.100s <%.100s instance at %p>\n",
302 msg, cname, inst);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000303}
304
305static void
Jeremy Hylton06257772000-08-31 15:10:24 +0000306debug_cycle(char *msg, PyObject *op)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000307{
308 if ((debug & DEBUG_INSTANCES) && PyInstance_Check(op)) {
Jeremy Hylton06257772000-08-31 15:10:24 +0000309 debug_instance(msg, (PyInstanceObject *)op);
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000310 }
311 else if (debug & DEBUG_OBJECTS) {
Jeremy Hylton06257772000-08-31 15:10:24 +0000312 PySys_WriteStderr("gc: %.100s <%.100s %p>\n",
313 msg, op->ob_type->tp_name, op);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000314 }
315}
316
317/* Handle uncollectable garbage (cycles with finalizers). */
318static void
319handle_finalizers(PyGC_Head *finalizers, PyGC_Head *old)
320{
321 PyGC_Head *gc;
322 if (garbage == NULL) {
323 garbage = PyList_New(0);
324 }
Tim Peters9e4ca102001-10-11 18:31:31 +0000325 for (gc = finalizers->gc.gc_next; gc != finalizers;
326 gc = finalizers->gc.gc_next) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000327 PyObject *op = FROM_GC(gc);
Neil Schemenauera765c122001-11-01 17:35:23 +0000328 if ((debug & DEBUG_SAVEALL) || has_finalizer(op)) {
329 /* If SAVEALL is not set then just append objects with
330 * finalizers to the list of garbage. All objects in
331 * the finalizers list are reachable from those
332 * objects. */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000333 PyList_Append(garbage, op);
334 }
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000335 /* object is now reachable again */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000336 gc_list_remove(gc);
337 gc_list_append(gc, old);
338 }
339}
340
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000341/* Break reference cycles by clearing the containers involved. This is
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000342 * tricky business as the lists can be changing and we don't know which
343 * objects may be freed. It is possible I screwed something up here. */
344static void
345delete_garbage(PyGC_Head *unreachable, PyGC_Head *old)
346{
347 inquiry clear;
348
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000349 while (!gc_list_is_empty(unreachable)) {
Tim Peters9e4ca102001-10-11 18:31:31 +0000350 PyGC_Head *gc = unreachable->gc.gc_next;
Neil Schemenauer43411b52001-08-30 00:05:51 +0000351 PyObject *op = FROM_GC(gc);
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000352 if (debug & DEBUG_SAVEALL) {
353 PyList_Append(garbage, op);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000354 }
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000355 else {
356 if ((clear = op->ob_type->tp_clear) != NULL) {
357 Py_INCREF(op);
358 clear((PyObject *)op);
359 Py_DECREF(op);
360 }
361 }
Tim Peters9e4ca102001-10-11 18:31:31 +0000362 if (unreachable->gc.gc_next == gc) {
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000363 /* object is still alive, move it, it may die later */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000364 gc_list_remove(gc);
365 gc_list_append(gc, old);
366 }
367 }
368}
369
370/* This is the main function. Read this to understand how the
371 * collection process works. */
372static long
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000373collect(int generation)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000374{
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000375 int i;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000376 long n = 0;
377 long m = 0;
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000378 PyGC_Head *young; /* the generation we are examining */
379 PyGC_Head *old; /* next older generation */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000380 PyGC_Head reachable;
381 PyGC_Head unreachable;
382 PyGC_Head finalizers;
383 PyGC_Head *gc;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000384
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000385 if (debug & DEBUG_STATS) {
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000386 PySys_WriteStderr("gc: collecting generation %d...\n",
387 generation);
388 PySys_WriteStderr("gc: objects in each generation:");
389 for (i = 0; i < NUM_GENERATIONS; i++) {
390 PySys_WriteStderr(" %ld", gc_list_size(GEN_HEAD(i)));
391 }
392 PySys_WriteStderr("\n");
393 }
394
395 /* update collection and allocation counters */
396 if (generation+1 < NUM_GENERATIONS)
397 generations[generation+1].count += 1;
398 for (i = 0; i <= generation; i++)
399 generations[generation].count = 0;
400
401 /* merge younger generations with one we are currently collecting */
402 for (i = 0; i < generation; i++) {
403 gc_list_merge(GEN_HEAD(i), GEN_HEAD(generation));
404 }
405
406 /* handy references */
407 young = GEN_HEAD(generation);
408 if (generation < NUM_GENERATIONS-1) {
409 old = GEN_HEAD(generation+1);
410 } else {
411 old = GEN_HEAD(NUM_GENERATIONS-1);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000412 }
413
414 /* Using ob_refcnt and gc_refs, calculate which objects in the
415 * container set are reachable from outside the set (ie. have a
416 * refcount greater than 0 when all the references within the
417 * set are taken into account */
418 update_refs(young);
419 subtract_refs(young);
420
421 /* Move everything reachable from outside the set into the
422 * reachable set (ie. gc_refs > 0). Next, move everything
423 * reachable from objects in the reachable set. */
424 gc_list_init(&reachable);
425 move_roots(young, &reachable);
426 move_root_reachable(&reachable);
427
428 /* move unreachable objects to a temporary list, new objects can be
429 * allocated after this point */
430 gc_list_init(&unreachable);
431 gc_list_move(young, &unreachable);
432
433 /* move reachable objects to next generation */
434 gc_list_merge(&reachable, old);
435
436 /* Move objects reachable from finalizers, we can't safely delete
437 * them. Python programmers should take care not to create such
438 * things. For Python finalizers means instance objects with
439 * __del__ methods. */
440 gc_list_init(&finalizers);
441 move_finalizers(&unreachable, &finalizers);
442 move_finalizer_reachable(&finalizers);
443
444 /* Collect statistics on collectable objects found and print
445 * debugging information. */
Tim Peters9e4ca102001-10-11 18:31:31 +0000446 for (gc = unreachable.gc.gc_next; gc != &unreachable;
447 gc = gc->gc.gc_next) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000448 m++;
Jeremy Hylton06257772000-08-31 15:10:24 +0000449 if (debug & DEBUG_COLLECTABLE) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000450 debug_cycle("collectable", FROM_GC(gc));
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000451 }
452 }
453 /* call tp_clear on objects in the collectable set. This will cause
454 * the reference cycles to be broken. It may also cause some objects in
455 * finalizers to be freed */
456 delete_garbage(&unreachable, old);
457
458 /* Collect statistics on uncollectable objects found and print
459 * debugging information. */
Tim Peters9e4ca102001-10-11 18:31:31 +0000460 for (gc = finalizers.gc.gc_next; gc != &finalizers;
461 gc = gc->gc.gc_next) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000462 n++;
Jeremy Hylton06257772000-08-31 15:10:24 +0000463 if (debug & DEBUG_UNCOLLECTABLE) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000464 debug_cycle("uncollectable", FROM_GC(gc));
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000465 }
466 }
Jeremy Hylton06257772000-08-31 15:10:24 +0000467 if (debug & DEBUG_STATS) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000468 if (m == 0 && n == 0) {
Jeremy Hylton06257772000-08-31 15:10:24 +0000469 PySys_WriteStderr("gc: done.\n");
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000470 }
471 else {
Jeremy Hylton06257772000-08-31 15:10:24 +0000472 PySys_WriteStderr(
473 "gc: done, %ld unreachable, %ld uncollectable.\n",
474 n+m, n);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000475 }
476 }
477
478 /* Append instances in the uncollectable set to a Python
479 * reachable list of garbage. The programmer has to deal with
480 * this if they insist on creating this type of structure. */
481 handle_finalizers(&finalizers, old);
482
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000483 if (PyErr_Occurred()) {
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000484 if (gc_str == NULL) {
485 gc_str = PyString_FromString("garbage collection");
486 }
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000487 PyErr_WriteUnraisable(gc_str);
488 Py_FatalError("unexpected exception during garbage collection");
489 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000490 return n+m;
491}
492
493static long
494collect_generations(void)
495{
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000496 int i;
Vladimir Marangozovb16714b2000-07-10 05:37:39 +0000497 long n = 0;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000498
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000499 /* Find the oldest generation (higest numbered) where the count
500 * exceeds the threshold. Objects in the that generation and
501 * generations younger than it will be collected. */
502 for (i = NUM_GENERATIONS-1; i >= 0; i--) {
503 if (generations[i].count > generations[i].threshold) {
504 n = collect(i);
505 break;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000506 }
507 }
508 return n;
509}
510
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000511static char gc_enable__doc__[] =
512"enable() -> None\n"
513"\n"
514"Enable automatic garbage collection.\n"
515;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000516
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000517static PyObject *
518gc_enable(PyObject *self, PyObject *args)
519{
520
521 if (!PyArg_ParseTuple(args, ":enable")) /* check no args */
522 return NULL;
523
524 enabled = 1;
525
526 Py_INCREF(Py_None);
527 return Py_None;
528}
529
530static char gc_disable__doc__[] =
531"disable() -> None\n"
532"\n"
533"Disable automatic garbage collection.\n"
534;
535
536static PyObject *
537gc_disable(PyObject *self, PyObject *args)
538{
539
540 if (!PyArg_ParseTuple(args, ":disable")) /* check no args */
541 return NULL;
542
543 enabled = 0;
544
545 Py_INCREF(Py_None);
546 return Py_None;
547}
548
549static char gc_isenabled__doc__[] =
550"isenabled() -> status\n"
551"\n"
552"Returns true if automatic garbage collection is enabled.\n"
553;
554
555static PyObject *
556gc_isenabled(PyObject *self, PyObject *args)
557{
558
559 if (!PyArg_ParseTuple(args, ":isenabled")) /* check no args */
560 return NULL;
561
562 return Py_BuildValue("i", enabled);
563}
564
565static char gc_collect__doc__[] =
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000566"collect() -> n\n"
567"\n"
568"Run a full collection. The number of unreachable objects is returned.\n"
569;
570
571static PyObject *
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000572gc_collect(PyObject *self, PyObject *args)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000573{
574 long n;
575
Fred Drakecc1be242000-07-12 04:42:23 +0000576 if (!PyArg_ParseTuple(args, ":collect")) /* check no args */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000577 return NULL;
578
Neil Schemenauere8c40cb2001-10-31 23:09:35 +0000579 if (collecting) {
580 n = 0; /* already collecting, don't do anything */
581 }
582 else {
583 collecting = 1;
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000584 n = collect(NUM_GENERATIONS - 1);
Neil Schemenauere8c40cb2001-10-31 23:09:35 +0000585 collecting = 0;
586 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000587
Neil Schemenauer7760cff2000-09-22 22:35:36 +0000588 return Py_BuildValue("l", n);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000589}
590
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000591static char gc_set_debug__doc__[] =
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000592"set_debug(flags) -> None\n"
593"\n"
594"Set the garbage collection debugging flags. Debugging information is\n"
595"written to sys.stderr.\n"
596"\n"
597"flags is an integer and can have the following bits turned on:\n"
598"\n"
599" DEBUG_STATS - Print statistics during collection.\n"
600" DEBUG_COLLECTABLE - Print collectable objects found.\n"
601" DEBUG_UNCOLLECTABLE - Print unreachable but uncollectable objects found.\n"
602" DEBUG_INSTANCES - Print instance objects.\n"
603" DEBUG_OBJECTS - Print objects other than instances.\n"
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000604" DEBUG_SAVEALL - Save objects to gc.garbage rather than freeing them.\n"
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000605" DEBUG_LEAK - Debug leaking programs (everything but STATS).\n"
606;
607
608static PyObject *
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000609gc_set_debug(PyObject *self, PyObject *args)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000610{
Neil Schemenauer7760cff2000-09-22 22:35:36 +0000611 if (!PyArg_ParseTuple(args, "i:set_debug", &debug))
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000612 return NULL;
613
614 Py_INCREF(Py_None);
615 return Py_None;
616}
617
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000618static char gc_get_debug__doc__[] =
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000619"get_debug() -> flags\n"
620"\n"
621"Get the garbage collection debugging flags.\n"
622;
623
624static PyObject *
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000625gc_get_debug(PyObject *self, PyObject *args)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000626{
Fred Drakecc1be242000-07-12 04:42:23 +0000627 if (!PyArg_ParseTuple(args, ":get_debug")) /* no args */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000628 return NULL;
629
630 return Py_BuildValue("i", debug);
631}
632
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000633static char gc_set_thresh__doc__[] =
Neal Norwitz2a47c0f2002-01-29 00:53:41 +0000634"set_threshold(threshold0, [threshold1, threshold2]) -> None\n"
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000635"\n"
636"Sets the collection thresholds. Setting threshold0 to zero disables\n"
637"collection.\n"
638;
639
640static PyObject *
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000641gc_set_thresh(PyObject *self, PyObject *args)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000642{
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000643 int i;
644 if (!PyArg_ParseTuple(args, "i|ii:set_threshold",
645 &generations[0].threshold,
646 &generations[1].threshold,
647 &generations[2].threshold))
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000648 return NULL;
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000649 for (i = 2; i < NUM_GENERATIONS; i++) {
650 /* generations higher than 2 get the same threshold */
651 generations[i].threshold = generations[2].threshold;
652 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000653
654 Py_INCREF(Py_None);
655 return Py_None;
656}
657
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000658static char gc_get_thresh__doc__[] =
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000659"get_threshold() -> (threshold0, threshold1, threshold2)\n"
660"\n"
661"Return the current collection thresholds\n"
662;
663
664static PyObject *
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000665gc_get_thresh(PyObject *self, PyObject *args)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000666{
Fred Drakecc1be242000-07-12 04:42:23 +0000667 if (!PyArg_ParseTuple(args, ":get_threshold")) /* no args */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000668 return NULL;
669
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000670 return Py_BuildValue("(iii)",
671 generations[0].threshold,
672 generations[1].threshold,
673 generations[2].threshold);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000674}
675
Neil Schemenauer48c70342001-08-09 15:38:31 +0000676static int
Martin v. Löwis560da622001-11-24 09:24:51 +0000677referrersvisit(PyObject* obj, PyObject *objs)
Neil Schemenauer48c70342001-08-09 15:38:31 +0000678{
Martin v. Löwisc8fe77b2001-11-29 18:08:31 +0000679 int i;
680 for (i = 0; i < PyTuple_GET_SIZE(objs); i++)
681 if (PyTuple_GET_ITEM(objs, i) == obj)
682 return 1;
Neil Schemenauer48c70342001-08-09 15:38:31 +0000683 return 0;
684}
685
Neil Schemenauer17e7be62001-08-10 14:46:47 +0000686static int
Martin v. Löwis560da622001-11-24 09:24:51 +0000687gc_referrers_for(PyObject *objs, PyGC_Head *list, PyObject *resultlist)
Neil Schemenauer48c70342001-08-09 15:38:31 +0000688{
689 PyGC_Head *gc;
690 PyObject *obj;
691 traverseproc traverse;
Tim Peters9e4ca102001-10-11 18:31:31 +0000692 for (gc = list->gc.gc_next; gc != list; gc = gc->gc.gc_next) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000693 obj = FROM_GC(gc);
Neil Schemenauer48c70342001-08-09 15:38:31 +0000694 traverse = obj->ob_type->tp_traverse;
695 if (obj == objs || obj == resultlist)
696 continue;
Martin v. Löwis560da622001-11-24 09:24:51 +0000697 if (traverse(obj, (visitproc)referrersvisit, objs)) {
Neil Schemenauer17e7be62001-08-10 14:46:47 +0000698 if (PyList_Append(resultlist, obj) < 0)
699 return 0; /* error */
Neil Schemenauer48c70342001-08-09 15:38:31 +0000700 }
701 }
Neil Schemenauer17e7be62001-08-10 14:46:47 +0000702 return 1; /* no error */
Neil Schemenauer48c70342001-08-09 15:38:31 +0000703}
704
Martin v. Löwis560da622001-11-24 09:24:51 +0000705static char gc_get_referrers__doc__[]=
706"get_referrers(*objs) -> list\n\
Neil Schemenauer48c70342001-08-09 15:38:31 +0000707Return the list of objects that directly refer to any of objs.";
708
Neil Schemenauer17e7be62001-08-10 14:46:47 +0000709static PyObject *
Martin v. Löwis560da622001-11-24 09:24:51 +0000710gc_get_referrers(PyObject *self, PyObject *args)
Neil Schemenauer48c70342001-08-09 15:38:31 +0000711{
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000712 int i;
Neil Schemenauer48c70342001-08-09 15:38:31 +0000713 PyObject *result = PyList_New(0);
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000714 for (i = 0; i < NUM_GENERATIONS; i++) {
715 if (!(gc_referrers_for(args, GEN_HEAD(i), result))) {
716 Py_DECREF(result);
717 return NULL;
718 }
Neil Schemenauer17e7be62001-08-10 14:46:47 +0000719 }
Neil Schemenauer48c70342001-08-09 15:38:31 +0000720 return result;
721}
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000722
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000723static char gc_get_objects__doc__[] =
724"get_objects() -> [...]\n"
725"\n"
726"Return a list of objects tracked by the collector (excluding the list\n"
727"returned).\n"
728;
729
730/* appending objects in a GC list to a Python list */
Martin v. Löwis155aad12001-12-02 12:21:34 +0000731static int
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000732append_objects(PyObject *py_list, PyGC_Head *gc_list)
733{
734 PyGC_Head *gc;
Tim Peters9e4ca102001-10-11 18:31:31 +0000735 for (gc = gc_list->gc.gc_next; gc != gc_list; gc = gc->gc.gc_next) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000736 PyObject *op = FROM_GC(gc);
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000737 if (op != py_list) {
Martin v. Löwis155aad12001-12-02 12:21:34 +0000738 if (PyList_Append(py_list, op)) {
739 return -1; /* exception */
740 }
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000741 }
742 }
Martin v. Löwis155aad12001-12-02 12:21:34 +0000743 return 0;
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000744}
745
746static PyObject *
747gc_get_objects(PyObject *self, PyObject *args)
748{
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000749 int i;
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000750 PyObject* result;
751
752 if (!PyArg_ParseTuple(args, ":get_objects")) /* check no args */
753 return NULL;
754 result = PyList_New(0);
Martin v. Löwisf8a6f242001-12-02 18:31:02 +0000755 if (result == NULL) {
756 return NULL;
757 }
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000758 for (i = 0; i < NUM_GENERATIONS; i++) {
759 if (append_objects(result, GEN_HEAD(i))) {
760 Py_DECREF(result);
761 return NULL;
762 }
Martin v. Löwis155aad12001-12-02 12:21:34 +0000763 }
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000764 return result;
765}
766
767
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000768static char gc__doc__ [] =
769"This module provides access to the garbage collector for reference cycles.\n"
770"\n"
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000771"enable() -- Enable automatic garbage collection.\n"
772"disable() -- Disable automatic garbage collection.\n"
773"isenabled() -- Returns true if automatic collection is enabled.\n"
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000774"collect() -- Do a full collection right now.\n"
775"set_debug() -- Set debugging flags.\n"
776"get_debug() -- Get debugging flags.\n"
777"set_threshold() -- Set the collection thresholds.\n"
778"get_threshold() -- Return the current the collection thresholds.\n"
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000779"get_objects() -- Return a list of all objects tracked by the collector.\n"
Martin v. Löwis560da622001-11-24 09:24:51 +0000780"get_referrers() -- Return the list of objects that refer to an object.\n"
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000781;
782
783static PyMethodDef GcMethods[] = {
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000784 {"enable", gc_enable, METH_VARARGS, gc_enable__doc__},
785 {"disable", gc_disable, METH_VARARGS, gc_disable__doc__},
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000786 {"isenabled", gc_isenabled, METH_VARARGS, gc_isenabled__doc__},
787 {"set_debug", gc_set_debug, METH_VARARGS, gc_set_debug__doc__},
788 {"get_debug", gc_get_debug, METH_VARARGS, gc_get_debug__doc__},
789 {"set_threshold", gc_set_thresh, METH_VARARGS, gc_set_thresh__doc__},
790 {"get_threshold", gc_get_thresh, METH_VARARGS, gc_get_thresh__doc__},
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000791 {"collect", gc_collect, METH_VARARGS, gc_collect__doc__},
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000792 {"get_objects", gc_get_objects,METH_VARARGS, gc_get_objects__doc__},
Martin v. Löwis560da622001-11-24 09:24:51 +0000793 {"get_referrers", gc_get_referrers, METH_VARARGS,
794 gc_get_referrers__doc__},
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000795 {NULL, NULL} /* Sentinel */
796};
797
798void
799initgc(void)
800{
801 PyObject *m;
802 PyObject *d;
803
804 m = Py_InitModule4("gc",
805 GcMethods,
806 gc__doc__,
807 NULL,
808 PYTHON_API_VERSION);
809 d = PyModule_GetDict(m);
810 if (garbage == NULL) {
811 garbage = PyList_New(0);
812 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000813 PyDict_SetItemString(d, "garbage", garbage);
814 PyDict_SetItemString(d, "DEBUG_STATS",
815 PyInt_FromLong(DEBUG_STATS));
816 PyDict_SetItemString(d, "DEBUG_COLLECTABLE",
817 PyInt_FromLong(DEBUG_COLLECTABLE));
818 PyDict_SetItemString(d, "DEBUG_UNCOLLECTABLE",
819 PyInt_FromLong(DEBUG_UNCOLLECTABLE));
820 PyDict_SetItemString(d, "DEBUG_INSTANCES",
821 PyInt_FromLong(DEBUG_INSTANCES));
822 PyDict_SetItemString(d, "DEBUG_OBJECTS",
823 PyInt_FromLong(DEBUG_OBJECTS));
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000824 PyDict_SetItemString(d, "DEBUG_SAVEALL",
825 PyInt_FromLong(DEBUG_SAVEALL));
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000826 PyDict_SetItemString(d, "DEBUG_LEAK",
827 PyInt_FromLong(DEBUG_LEAK));
828}
829
Neil Schemenauer43411b52001-08-30 00:05:51 +0000830/* for debugging */
831void _PyGC_Dump(PyGC_Head *g)
832{
833 _PyObject_Dump(FROM_GC(g));
834}
835
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000836#endif /* WITH_CYCLE_GC */
Neil Schemenauer43411b52001-08-30 00:05:51 +0000837
838/* extension modules might be compiled with GC support so these
839 functions must always be available */
840
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000841#undef PyObject_GC_Track
842#undef PyObject_GC_UnTrack
843#undef PyObject_GC_Del
844#undef _PyObject_GC_Malloc
845
Neil Schemenauer43411b52001-08-30 00:05:51 +0000846void
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000847PyObject_GC_Track(void *op)
Neil Schemenauer43411b52001-08-30 00:05:51 +0000848{
849 _PyObject_GC_TRACK(op);
850}
851
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000852/* for binary compatibility with 2.2 */
Neil Schemenauer43411b52001-08-30 00:05:51 +0000853void
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000854_PyObject_GC_Track(PyObject *op)
855{
856 PyObject_GC_Track(op);
857}
858
859void
860PyObject_GC_UnTrack(void *op)
Neil Schemenauer43411b52001-08-30 00:05:51 +0000861{
Neil Schemenauerb8833102002-03-29 03:04:25 +0000862#ifdef WITH_CYCLE_GC
Neil Schemenauera2b11ec2002-05-21 15:53:24 +0000863 if (IS_TRACKED(op))
Guido van Rossumff413af2002-03-28 20:34:59 +0000864 _PyObject_GC_UNTRACK(op);
Neil Schemenauerb8833102002-03-29 03:04:25 +0000865#endif
Neil Schemenauer43411b52001-08-30 00:05:51 +0000866}
867
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000868/* for binary compatibility with 2.2 */
869void
870_PyObject_GC_UnTrack(PyObject *op)
871{
872 PyObject_GC_UnTrack(op);
873}
874
Neil Schemenauer43411b52001-08-30 00:05:51 +0000875PyObject *
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000876_PyObject_GC_Malloc(size_t basicsize)
Neil Schemenauer43411b52001-08-30 00:05:51 +0000877{
878 PyObject *op;
879#ifdef WITH_CYCLE_GC
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000880 PyGC_Head *g = PyObject_MALLOC(sizeof(PyGC_Head) + basicsize);
Neil Schemenauer43411b52001-08-30 00:05:51 +0000881 if (g == NULL)
882 return (PyObject *)PyErr_NoMemory();
Tim Peters9e4ca102001-10-11 18:31:31 +0000883 g->gc.gc_next = NULL;
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000884 generations[0].count++; /* number of allocated GC objects */
885 if (generations[0].count > generations[0].threshold &&
Neil Schemenauer43411b52001-08-30 00:05:51 +0000886 enabled &&
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000887 generations[0].threshold &&
Neil Schemenauer43411b52001-08-30 00:05:51 +0000888 !collecting &&
889 !PyErr_Occurred()) {
890 collecting = 1;
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000891 collect_generations();
Neil Schemenauer43411b52001-08-30 00:05:51 +0000892 collecting = 0;
893 }
894 op = FROM_GC(g);
895#else
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000896 op = PyObject_MALLOC(basicsize);
Neil Schemenauer43411b52001-08-30 00:05:51 +0000897 if (op == NULL)
898 return (PyObject *)PyErr_NoMemory();
899
900#endif
901 return op;
902}
903
904PyObject *
905_PyObject_GC_New(PyTypeObject *tp)
906{
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000907 PyObject *op = _PyObject_GC_Malloc(_PyObject_SIZE(tp));
Tim Petersfa8efab2002-04-28 01:57:25 +0000908 if (op != NULL)
909 op = PyObject_INIT(op, tp);
910 return op;
Neil Schemenauer43411b52001-08-30 00:05:51 +0000911}
912
913PyVarObject *
Tim Peters6d483d32001-10-06 21:27:34 +0000914_PyObject_GC_NewVar(PyTypeObject *tp, int nitems)
Neil Schemenauer43411b52001-08-30 00:05:51 +0000915{
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000916 const size_t size = _PyObject_VAR_SIZE(tp, nitems);
917 PyVarObject *op = (PyVarObject *) _PyObject_GC_Malloc(size);
Tim Petersfa8efab2002-04-28 01:57:25 +0000918 if (op != NULL)
919 op = PyObject_INIT_VAR(op, tp, nitems);
920 return op;
Neil Schemenauer43411b52001-08-30 00:05:51 +0000921}
922
923PyVarObject *
Tim Peters6d483d32001-10-06 21:27:34 +0000924_PyObject_GC_Resize(PyVarObject *op, int nitems)
Neil Schemenauer43411b52001-08-30 00:05:51 +0000925{
Tim Petersf2a67da2001-10-07 03:54:51 +0000926 const size_t basicsize = _PyObject_VAR_SIZE(op->ob_type, nitems);
Neil Schemenauer43411b52001-08-30 00:05:51 +0000927#ifdef WITH_CYCLE_GC
928 PyGC_Head *g = AS_GC(op);
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000929 g = PyObject_REALLOC(g, sizeof(PyGC_Head) + basicsize);
Neil Schemenauer43411b52001-08-30 00:05:51 +0000930 if (g == NULL)
931 return (PyVarObject *)PyErr_NoMemory();
932 op = (PyVarObject *) FROM_GC(g);
933#else
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000934 op = PyObject_REALLOC(op, basicsize);
Neil Schemenauer43411b52001-08-30 00:05:51 +0000935 if (op == NULL)
936 return (PyVarObject *)PyErr_NoMemory();
937#endif
Tim Peters6d483d32001-10-06 21:27:34 +0000938 op->ob_size = nitems;
Neil Schemenauer43411b52001-08-30 00:05:51 +0000939 return op;
940}
941
942void
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000943PyObject_GC_Del(void *op)
Neil Schemenauer43411b52001-08-30 00:05:51 +0000944{
945#ifdef WITH_CYCLE_GC
946 PyGC_Head *g = AS_GC(op);
Neil Schemenauera2b11ec2002-05-21 15:53:24 +0000947 if (IS_TRACKED(op))
Neil Schemenauer43411b52001-08-30 00:05:51 +0000948 gc_list_remove(g);
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000949 if (generations[0].count > 0) {
950 generations[0].count--;
Neil Schemenauer43411b52001-08-30 00:05:51 +0000951 }
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000952 PyObject_FREE(g);
Neil Schemenauer43411b52001-08-30 00:05:51 +0000953#else
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000954 PyObject_FREE(op);
Neil Schemenauer43411b52001-08-30 00:05:51 +0000955#endif
956}
957
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000958/* for binary compatibility with 2.2 */
959#undef _PyObject_GC_Del
960void
961_PyObject_GC_Del(PyObject *op)
962{
963 PyObject_GC_Del(op);
964}