blob: 2ae4d427606443294ad6bbe1ee2383ef2579801e [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
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000031
32/*** Global GC state ***/
33
Neil Schemenauer2880ae52002-05-04 05:35:20 +000034struct gc_generation {
35 PyGC_Head head;
36 int threshold; /* collection threshold */
37 int count; /* count of allocations or collections of younger
38 generations */
39};
40
41#define NUM_GENERATIONS 3
42#define GEN_HEAD(n) (&generations[n].head)
43
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000044/* linked lists of container objects */
Neil Schemenauer2880ae52002-05-04 05:35:20 +000045static struct gc_generation generations[NUM_GENERATIONS] = {
46 /* PyGC_Head, threshold, count */
47 {{{GEN_HEAD(0), GEN_HEAD(0), 0}}, 700, 0},
48 {{{GEN_HEAD(1), GEN_HEAD(1), 0}}, 10, 0},
49 {{{GEN_HEAD(2), GEN_HEAD(2), 0}}, 10, 0},
50};
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000051
Neil Schemenauer2880ae52002-05-04 05:35:20 +000052PyGC_Head *_PyGC_generation0 = GEN_HEAD(0);
53
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +000054static int enabled = 1; /* automatic collection enabled? */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000055
Neil Schemenauer43411b52001-08-30 00:05:51 +000056/* true if we are currently running the collector */
57static int collecting;
58
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000059/* set for debugging information */
60#define DEBUG_STATS (1<<0) /* print collection statistics */
61#define DEBUG_COLLECTABLE (1<<1) /* print collectable objects */
62#define DEBUG_UNCOLLECTABLE (1<<2) /* print uncollectable objects */
63#define DEBUG_INSTANCES (1<<3) /* print instances */
64#define DEBUG_OBJECTS (1<<4) /* print other objects */
Neil Schemenauer544de1e2000-09-22 15:22:38 +000065#define DEBUG_SAVEALL (1<<5) /* save all garbage in gc.garbage */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000066#define DEBUG_LEAK DEBUG_COLLECTABLE | \
67 DEBUG_UNCOLLECTABLE | \
68 DEBUG_INSTANCES | \
Neil Schemenauer544de1e2000-09-22 15:22:38 +000069 DEBUG_OBJECTS | \
70 DEBUG_SAVEALL
Jeremy Hyltonb709df32000-09-01 02:47:25 +000071static int debug;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000072
Neil Schemenauer43411b52001-08-30 00:05:51 +000073/* Special gc_refs value */
74#define GC_MOVED -123
75
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000076/* list of uncollectable objects */
77static PyObject *garbage;
78
Jeremy Hyltonb709df32000-09-01 02:47:25 +000079/* Python string to use if unhandled exception occurs */
80static PyObject *gc_str;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000081
82/*** list functions ***/
83
84static void
85gc_list_init(PyGC_Head *list)
86{
Tim Peters9e4ca102001-10-11 18:31:31 +000087 list->gc.gc_prev = list;
88 list->gc.gc_next = list;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000089}
90
Neil Schemenauer2880ae52002-05-04 05:35:20 +000091static int
92gc_list_is_empty(PyGC_Head *list)
93{
94 return (list->gc.gc_next == list);
95}
96
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000097static void
98gc_list_append(PyGC_Head *node, PyGC_Head *list)
99{
Tim Peters9e4ca102001-10-11 18:31:31 +0000100 node->gc.gc_next = list;
101 node->gc.gc_prev = list->gc.gc_prev;
102 node->gc.gc_prev->gc.gc_next = node;
103 list->gc.gc_prev = node;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000104}
105
106static void
107gc_list_remove(PyGC_Head *node)
108{
Tim Peters9e4ca102001-10-11 18:31:31 +0000109 node->gc.gc_prev->gc.gc_next = node->gc.gc_next;
110 node->gc.gc_next->gc.gc_prev = node->gc.gc_prev;
111 node->gc.gc_next = NULL; /* object is not currently tracked */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000112}
113
114static void
115gc_list_move(PyGC_Head *from, PyGC_Head *to)
116{
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000117 if (gc_list_is_empty(from)) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000118 gc_list_init(to);
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000119 }
120 else {
Tim Peters9e4ca102001-10-11 18:31:31 +0000121 to->gc.gc_next = from->gc.gc_next;
122 to->gc.gc_next->gc.gc_prev = to;
123 to->gc.gc_prev = from->gc.gc_prev;
124 to->gc.gc_prev->gc.gc_next = to;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000125 }
126 gc_list_init(from);
127}
128
129/* append a list onto another list, from becomes an empty list */
130static void
131gc_list_merge(PyGC_Head *from, PyGC_Head *to)
132{
133 PyGC_Head *tail;
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000134 if (!gc_list_is_empty(from)) {
Tim Peters9e4ca102001-10-11 18:31:31 +0000135 tail = to->gc.gc_prev;
136 tail->gc.gc_next = from->gc.gc_next;
137 tail->gc.gc_next->gc.gc_prev = tail;
138 to->gc.gc_prev = from->gc.gc_prev;
139 to->gc.gc_prev->gc.gc_next = to;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000140 }
141 gc_list_init(from);
142}
143
144static long
145gc_list_size(PyGC_Head *list)
146{
147 PyGC_Head *gc;
148 long n = 0;
Tim Peters9e4ca102001-10-11 18:31:31 +0000149 for (gc = list->gc.gc_next; gc != list; gc = gc->gc.gc_next) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000150 n++;
151 }
152 return n;
153}
154
155/*** end of list stuff ***/
156
157
Neil Schemenauer43411b52001-08-30 00:05:51 +0000158
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000159/* Set all gc_refs = ob_refcnt */
160static void
161update_refs(PyGC_Head *containers)
162{
Tim Peters9e4ca102001-10-11 18:31:31 +0000163 PyGC_Head *gc = containers->gc.gc_next;
164 for (; gc != containers; gc=gc->gc.gc_next) {
165 gc->gc.gc_refs = FROM_GC(gc)->ob_refcnt;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000166 }
167}
168
169static int
170visit_decref(PyObject *op, void *data)
171{
172 if (op && PyObject_IS_GC(op)) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000173 PyGC_Head *gc = AS_GC(op);
Tim Peters9e4ca102001-10-11 18:31:31 +0000174 if (gc->gc.gc_next != NULL)
175 AS_GC(op)->gc.gc_refs--;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000176 }
177 return 0;
178}
179
180/* Subtract internal references from gc_refs */
181static void
182subtract_refs(PyGC_Head *containers)
183{
184 traverseproc traverse;
Tim Peters9e4ca102001-10-11 18:31:31 +0000185 PyGC_Head *gc = containers->gc.gc_next;
186 for (; gc != containers; gc=gc->gc.gc_next) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000187 traverse = FROM_GC(gc)->ob_type->tp_traverse;
188 (void) traverse(FROM_GC(gc),
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000189 (visitproc)visit_decref,
190 NULL);
191 }
192}
193
194/* Append objects with gc_refs > 0 to roots list */
195static void
196move_roots(PyGC_Head *containers, PyGC_Head *roots)
197{
198 PyGC_Head *next;
Tim Peters9e4ca102001-10-11 18:31:31 +0000199 PyGC_Head *gc = containers->gc.gc_next;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000200 while (gc != containers) {
Tim Peters9e4ca102001-10-11 18:31:31 +0000201 next = gc->gc.gc_next;
202 if (gc->gc.gc_refs > 0) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000203 gc_list_remove(gc);
204 gc_list_append(gc, roots);
Tim Peters9e4ca102001-10-11 18:31:31 +0000205 gc->gc.gc_refs = GC_MOVED;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000206 }
207 gc = next;
208 }
209}
210
211static int
Neil Schemenauer43411b52001-08-30 00:05:51 +0000212visit_move(PyObject *op, PyGC_Head *tolist)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000213{
214 if (PyObject_IS_GC(op)) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000215 PyGC_Head *gc = AS_GC(op);
Tim Peters9e4ca102001-10-11 18:31:31 +0000216 if (gc->gc.gc_next != NULL && gc->gc.gc_refs != GC_MOVED) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000217 gc_list_remove(gc);
Neil Schemenauer43411b52001-08-30 00:05:51 +0000218 gc_list_append(gc, tolist);
Tim Peters9e4ca102001-10-11 18:31:31 +0000219 gc->gc.gc_refs = GC_MOVED;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000220 }
221 }
222 return 0;
223}
224
225/* Move objects referenced from reachable to reachable set. */
226static void
227move_root_reachable(PyGC_Head *reachable)
228{
229 traverseproc traverse;
Tim Peters9e4ca102001-10-11 18:31:31 +0000230 PyGC_Head *gc = reachable->gc.gc_next;
231 for (; gc != reachable; gc=gc->gc.gc_next) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000232 /* careful, reachable list is growing here */
Neil Schemenauer43411b52001-08-30 00:05:51 +0000233 PyObject *op = FROM_GC(gc);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000234 traverse = op->ob_type->tp_traverse;
235 (void) traverse(op,
Neil Schemenauer43411b52001-08-30 00:05:51 +0000236 (visitproc)visit_move,
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000237 (void *)reachable);
238 }
239}
240
Neil Schemenauera765c122001-11-01 17:35:23 +0000241/* return true of object has a finalization method */
242static int
243has_finalizer(PyObject *op)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000244{
Jeremy Hylton06257772000-08-31 15:10:24 +0000245 static PyObject *delstr = NULL;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000246 if (delstr == NULL) {
247 delstr = PyString_InternFromString("__del__");
Jeremy Hylton06257772000-08-31 15:10:24 +0000248 if (delstr == NULL)
249 Py_FatalError("PyGC: can't initialize __del__ string");
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000250 }
Tim Petersdb865612001-11-01 19:35:45 +0000251 return (PyInstance_Check(op) ||
252 PyType_HasFeature(op->ob_type, Py_TPFLAGS_HEAPTYPE))
253 && PyObject_HasAttr(op, delstr);
Neil Schemenauera765c122001-11-01 17:35:23 +0000254}
255
256/* Move all objects with finalizers (instances with __del__) */
257static void
258move_finalizers(PyGC_Head *unreachable, PyGC_Head *finalizers)
259{
260 PyGC_Head *next;
261 PyGC_Head *gc = unreachable->gc.gc_next;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000262 for (; gc != unreachable; gc=next) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000263 PyObject *op = FROM_GC(gc);
Tim Peters9e4ca102001-10-11 18:31:31 +0000264 next = gc->gc.gc_next;
Neil Schemenauera765c122001-11-01 17:35:23 +0000265 if (has_finalizer(op)) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000266 gc_list_remove(gc);
267 gc_list_append(gc, finalizers);
268 }
269 }
270}
271
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000272/* Move objects referenced from roots to roots */
273static void
274move_finalizer_reachable(PyGC_Head *finalizers)
275{
276 traverseproc traverse;
Tim Peters9e4ca102001-10-11 18:31:31 +0000277 PyGC_Head *gc = finalizers->gc.gc_next;
278 for (; gc != finalizers; gc=gc->gc.gc_next) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000279 /* careful, finalizers list is growing here */
Neil Schemenauer43411b52001-08-30 00:05:51 +0000280 traverse = FROM_GC(gc)->ob_type->tp_traverse;
281 (void) traverse(FROM_GC(gc),
282 (visitproc)visit_move,
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000283 (void *)finalizers);
284 }
285}
286
287static void
Jeremy Hylton06257772000-08-31 15:10:24 +0000288debug_instance(char *msg, PyInstanceObject *inst)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000289{
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000290 char *cname;
Neil Schemenauera765c122001-11-01 17:35:23 +0000291 /* simple version of instance_repr */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000292 PyObject *classname = inst->in_class->cl_name;
293 if (classname != NULL && PyString_Check(classname))
294 cname = PyString_AsString(classname);
295 else
296 cname = "?";
Jeremy Hylton06257772000-08-31 15:10:24 +0000297 PySys_WriteStderr("gc: %.100s <%.100s instance at %p>\n",
298 msg, cname, inst);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000299}
300
301static void
Jeremy Hylton06257772000-08-31 15:10:24 +0000302debug_cycle(char *msg, PyObject *op)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000303{
304 if ((debug & DEBUG_INSTANCES) && PyInstance_Check(op)) {
Jeremy Hylton06257772000-08-31 15:10:24 +0000305 debug_instance(msg, (PyInstanceObject *)op);
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000306 }
307 else if (debug & DEBUG_OBJECTS) {
Jeremy Hylton06257772000-08-31 15:10:24 +0000308 PySys_WriteStderr("gc: %.100s <%.100s %p>\n",
309 msg, op->ob_type->tp_name, op);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000310 }
311}
312
313/* Handle uncollectable garbage (cycles with finalizers). */
314static void
315handle_finalizers(PyGC_Head *finalizers, PyGC_Head *old)
316{
317 PyGC_Head *gc;
318 if (garbage == NULL) {
319 garbage = PyList_New(0);
320 }
Tim Peters9e4ca102001-10-11 18:31:31 +0000321 for (gc = finalizers->gc.gc_next; gc != finalizers;
322 gc = finalizers->gc.gc_next) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000323 PyObject *op = FROM_GC(gc);
Neil Schemenauera765c122001-11-01 17:35:23 +0000324 if ((debug & DEBUG_SAVEALL) || has_finalizer(op)) {
325 /* If SAVEALL is not set then just append objects with
326 * finalizers to the list of garbage. All objects in
327 * the finalizers list are reachable from those
328 * objects. */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000329 PyList_Append(garbage, op);
330 }
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000331 /* object is now reachable again */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000332 gc_list_remove(gc);
333 gc_list_append(gc, old);
334 }
335}
336
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000337/* Break reference cycles by clearing the containers involved. This is
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000338 * tricky business as the lists can be changing and we don't know which
339 * objects may be freed. It is possible I screwed something up here. */
340static void
341delete_garbage(PyGC_Head *unreachable, PyGC_Head *old)
342{
343 inquiry clear;
344
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000345 while (!gc_list_is_empty(unreachable)) {
Tim Peters9e4ca102001-10-11 18:31:31 +0000346 PyGC_Head *gc = unreachable->gc.gc_next;
Neil Schemenauer43411b52001-08-30 00:05:51 +0000347 PyObject *op = FROM_GC(gc);
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000348 if (debug & DEBUG_SAVEALL) {
349 PyList_Append(garbage, op);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000350 }
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000351 else {
352 if ((clear = op->ob_type->tp_clear) != NULL) {
353 Py_INCREF(op);
354 clear((PyObject *)op);
355 Py_DECREF(op);
356 }
357 }
Tim Peters9e4ca102001-10-11 18:31:31 +0000358 if (unreachable->gc.gc_next == gc) {
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000359 /* object is still alive, move it, it may die later */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000360 gc_list_remove(gc);
361 gc_list_append(gc, old);
362 }
363 }
364}
365
366/* This is the main function. Read this to understand how the
367 * collection process works. */
368static long
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000369collect(int generation)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000370{
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000371 int i;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000372 long n = 0;
373 long m = 0;
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000374 PyGC_Head *young; /* the generation we are examining */
375 PyGC_Head *old; /* next older generation */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000376 PyGC_Head reachable;
377 PyGC_Head unreachable;
378 PyGC_Head finalizers;
379 PyGC_Head *gc;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000380
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000381 if (debug & DEBUG_STATS) {
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000382 PySys_WriteStderr("gc: collecting generation %d...\n",
383 generation);
384 PySys_WriteStderr("gc: objects in each generation:");
385 for (i = 0; i < NUM_GENERATIONS; i++) {
386 PySys_WriteStderr(" %ld", gc_list_size(GEN_HEAD(i)));
387 }
388 PySys_WriteStderr("\n");
389 }
390
391 /* update collection and allocation counters */
392 if (generation+1 < NUM_GENERATIONS)
393 generations[generation+1].count += 1;
394 for (i = 0; i <= generation; i++)
395 generations[generation].count = 0;
396
397 /* merge younger generations with one we are currently collecting */
398 for (i = 0; i < generation; i++) {
399 gc_list_merge(GEN_HEAD(i), GEN_HEAD(generation));
400 }
401
402 /* handy references */
403 young = GEN_HEAD(generation);
404 if (generation < NUM_GENERATIONS-1) {
405 old = GEN_HEAD(generation+1);
406 } else {
407 old = GEN_HEAD(NUM_GENERATIONS-1);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000408 }
409
410 /* Using ob_refcnt and gc_refs, calculate which objects in the
411 * container set are reachable from outside the set (ie. have a
412 * refcount greater than 0 when all the references within the
413 * set are taken into account */
414 update_refs(young);
415 subtract_refs(young);
416
417 /* Move everything reachable from outside the set into the
418 * reachable set (ie. gc_refs > 0). Next, move everything
419 * reachable from objects in the reachable set. */
420 gc_list_init(&reachable);
421 move_roots(young, &reachable);
422 move_root_reachable(&reachable);
423
424 /* move unreachable objects to a temporary list, new objects can be
425 * allocated after this point */
426 gc_list_init(&unreachable);
427 gc_list_move(young, &unreachable);
428
429 /* move reachable objects to next generation */
430 gc_list_merge(&reachable, old);
431
432 /* Move objects reachable from finalizers, we can't safely delete
433 * them. Python programmers should take care not to create such
434 * things. For Python finalizers means instance objects with
435 * __del__ methods. */
436 gc_list_init(&finalizers);
437 move_finalizers(&unreachable, &finalizers);
438 move_finalizer_reachable(&finalizers);
439
440 /* Collect statistics on collectable objects found and print
441 * debugging information. */
Tim Peters9e4ca102001-10-11 18:31:31 +0000442 for (gc = unreachable.gc.gc_next; gc != &unreachable;
443 gc = gc->gc.gc_next) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000444 m++;
Jeremy Hylton06257772000-08-31 15:10:24 +0000445 if (debug & DEBUG_COLLECTABLE) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000446 debug_cycle("collectable", FROM_GC(gc));
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000447 }
448 }
449 /* call tp_clear on objects in the collectable set. This will cause
450 * the reference cycles to be broken. It may also cause some objects in
451 * finalizers to be freed */
452 delete_garbage(&unreachable, old);
453
454 /* Collect statistics on uncollectable objects found and print
455 * debugging information. */
Tim Peters9e4ca102001-10-11 18:31:31 +0000456 for (gc = finalizers.gc.gc_next; gc != &finalizers;
457 gc = gc->gc.gc_next) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000458 n++;
Jeremy Hylton06257772000-08-31 15:10:24 +0000459 if (debug & DEBUG_UNCOLLECTABLE) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000460 debug_cycle("uncollectable", FROM_GC(gc));
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000461 }
462 }
Jeremy Hylton06257772000-08-31 15:10:24 +0000463 if (debug & DEBUG_STATS) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000464 if (m == 0 && n == 0) {
Jeremy Hylton06257772000-08-31 15:10:24 +0000465 PySys_WriteStderr("gc: done.\n");
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000466 }
467 else {
Jeremy Hylton06257772000-08-31 15:10:24 +0000468 PySys_WriteStderr(
469 "gc: done, %ld unreachable, %ld uncollectable.\n",
470 n+m, n);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000471 }
472 }
473
474 /* Append instances in the uncollectable set to a Python
475 * reachable list of garbage. The programmer has to deal with
476 * this if they insist on creating this type of structure. */
477 handle_finalizers(&finalizers, old);
478
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000479 if (PyErr_Occurred()) {
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000480 if (gc_str == NULL) {
481 gc_str = PyString_FromString("garbage collection");
482 }
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000483 PyErr_WriteUnraisable(gc_str);
484 Py_FatalError("unexpected exception during garbage collection");
485 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000486 return n+m;
487}
488
489static long
490collect_generations(void)
491{
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000492 int i;
Vladimir Marangozovb16714b2000-07-10 05:37:39 +0000493 long n = 0;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000494
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000495 /* Find the oldest generation (higest numbered) where the count
496 * exceeds the threshold. Objects in the that generation and
497 * generations younger than it will be collected. */
498 for (i = NUM_GENERATIONS-1; i >= 0; i--) {
499 if (generations[i].count > generations[i].threshold) {
500 n = collect(i);
501 break;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000502 }
503 }
504 return n;
505}
506
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000507static char gc_enable__doc__[] =
508"enable() -> None\n"
509"\n"
510"Enable automatic garbage collection.\n"
511;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000512
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000513static PyObject *
514gc_enable(PyObject *self, PyObject *args)
515{
516
517 if (!PyArg_ParseTuple(args, ":enable")) /* check no args */
518 return NULL;
519
520 enabled = 1;
521
522 Py_INCREF(Py_None);
523 return Py_None;
524}
525
526static char gc_disable__doc__[] =
527"disable() -> None\n"
528"\n"
529"Disable automatic garbage collection.\n"
530;
531
532static PyObject *
533gc_disable(PyObject *self, PyObject *args)
534{
535
536 if (!PyArg_ParseTuple(args, ":disable")) /* check no args */
537 return NULL;
538
539 enabled = 0;
540
541 Py_INCREF(Py_None);
542 return Py_None;
543}
544
545static char gc_isenabled__doc__[] =
546"isenabled() -> status\n"
547"\n"
548"Returns true if automatic garbage collection is enabled.\n"
549;
550
551static PyObject *
552gc_isenabled(PyObject *self, PyObject *args)
553{
554
555 if (!PyArg_ParseTuple(args, ":isenabled")) /* check no args */
556 return NULL;
557
558 return Py_BuildValue("i", enabled);
559}
560
561static char gc_collect__doc__[] =
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000562"collect() -> n\n"
563"\n"
564"Run a full collection. The number of unreachable objects is returned.\n"
565;
566
567static PyObject *
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000568gc_collect(PyObject *self, PyObject *args)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000569{
570 long n;
571
Fred Drakecc1be242000-07-12 04:42:23 +0000572 if (!PyArg_ParseTuple(args, ":collect")) /* check no args */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000573 return NULL;
574
Neil Schemenauere8c40cb2001-10-31 23:09:35 +0000575 if (collecting) {
576 n = 0; /* already collecting, don't do anything */
577 }
578 else {
579 collecting = 1;
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000580 n = collect(NUM_GENERATIONS - 1);
Neil Schemenauere8c40cb2001-10-31 23:09:35 +0000581 collecting = 0;
582 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000583
Neil Schemenauer7760cff2000-09-22 22:35:36 +0000584 return Py_BuildValue("l", n);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000585}
586
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000587static char gc_set_debug__doc__[] =
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000588"set_debug(flags) -> None\n"
589"\n"
590"Set the garbage collection debugging flags. Debugging information is\n"
591"written to sys.stderr.\n"
592"\n"
593"flags is an integer and can have the following bits turned on:\n"
594"\n"
595" DEBUG_STATS - Print statistics during collection.\n"
596" DEBUG_COLLECTABLE - Print collectable objects found.\n"
597" DEBUG_UNCOLLECTABLE - Print unreachable but uncollectable objects found.\n"
598" DEBUG_INSTANCES - Print instance objects.\n"
599" DEBUG_OBJECTS - Print objects other than instances.\n"
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000600" DEBUG_SAVEALL - Save objects to gc.garbage rather than freeing them.\n"
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000601" DEBUG_LEAK - Debug leaking programs (everything but STATS).\n"
602;
603
604static PyObject *
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000605gc_set_debug(PyObject *self, PyObject *args)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000606{
Neil Schemenauer7760cff2000-09-22 22:35:36 +0000607 if (!PyArg_ParseTuple(args, "i:set_debug", &debug))
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000608 return NULL;
609
610 Py_INCREF(Py_None);
611 return Py_None;
612}
613
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000614static char gc_get_debug__doc__[] =
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000615"get_debug() -> flags\n"
616"\n"
617"Get the garbage collection debugging flags.\n"
618;
619
620static PyObject *
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000621gc_get_debug(PyObject *self, PyObject *args)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000622{
Fred Drakecc1be242000-07-12 04:42:23 +0000623 if (!PyArg_ParseTuple(args, ":get_debug")) /* no args */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000624 return NULL;
625
626 return Py_BuildValue("i", debug);
627}
628
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000629static char gc_set_thresh__doc__[] =
Neal Norwitz2a47c0f2002-01-29 00:53:41 +0000630"set_threshold(threshold0, [threshold1, threshold2]) -> None\n"
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000631"\n"
632"Sets the collection thresholds. Setting threshold0 to zero disables\n"
633"collection.\n"
634;
635
636static PyObject *
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000637gc_set_thresh(PyObject *self, PyObject *args)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000638{
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000639 int i;
640 if (!PyArg_ParseTuple(args, "i|ii:set_threshold",
641 &generations[0].threshold,
642 &generations[1].threshold,
643 &generations[2].threshold))
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000644 return NULL;
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000645 for (i = 2; i < NUM_GENERATIONS; i++) {
646 /* generations higher than 2 get the same threshold */
647 generations[i].threshold = generations[2].threshold;
648 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000649
650 Py_INCREF(Py_None);
651 return Py_None;
652}
653
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000654static char gc_get_thresh__doc__[] =
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000655"get_threshold() -> (threshold0, threshold1, threshold2)\n"
656"\n"
657"Return the current collection thresholds\n"
658;
659
660static PyObject *
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000661gc_get_thresh(PyObject *self, PyObject *args)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000662{
Fred Drakecc1be242000-07-12 04:42:23 +0000663 if (!PyArg_ParseTuple(args, ":get_threshold")) /* no args */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000664 return NULL;
665
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000666 return Py_BuildValue("(iii)",
667 generations[0].threshold,
668 generations[1].threshold,
669 generations[2].threshold);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000670}
671
Neil Schemenauer48c70342001-08-09 15:38:31 +0000672static int
Martin v. Löwis560da622001-11-24 09:24:51 +0000673referrersvisit(PyObject* obj, PyObject *objs)
Neil Schemenauer48c70342001-08-09 15:38:31 +0000674{
Martin v. Löwisc8fe77b2001-11-29 18:08:31 +0000675 int i;
676 for (i = 0; i < PyTuple_GET_SIZE(objs); i++)
677 if (PyTuple_GET_ITEM(objs, i) == obj)
678 return 1;
Neil Schemenauer48c70342001-08-09 15:38:31 +0000679 return 0;
680}
681
Neil Schemenauer17e7be62001-08-10 14:46:47 +0000682static int
Martin v. Löwis560da622001-11-24 09:24:51 +0000683gc_referrers_for(PyObject *objs, PyGC_Head *list, PyObject *resultlist)
Neil Schemenauer48c70342001-08-09 15:38:31 +0000684{
685 PyGC_Head *gc;
686 PyObject *obj;
687 traverseproc traverse;
Tim Peters9e4ca102001-10-11 18:31:31 +0000688 for (gc = list->gc.gc_next; gc != list; gc = gc->gc.gc_next) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000689 obj = FROM_GC(gc);
Neil Schemenauer48c70342001-08-09 15:38:31 +0000690 traverse = obj->ob_type->tp_traverse;
691 if (obj == objs || obj == resultlist)
692 continue;
Martin v. Löwis560da622001-11-24 09:24:51 +0000693 if (traverse(obj, (visitproc)referrersvisit, objs)) {
Neil Schemenauer17e7be62001-08-10 14:46:47 +0000694 if (PyList_Append(resultlist, obj) < 0)
695 return 0; /* error */
Neil Schemenauer48c70342001-08-09 15:38:31 +0000696 }
697 }
Neil Schemenauer17e7be62001-08-10 14:46:47 +0000698 return 1; /* no error */
Neil Schemenauer48c70342001-08-09 15:38:31 +0000699}
700
Martin v. Löwis560da622001-11-24 09:24:51 +0000701static char gc_get_referrers__doc__[]=
702"get_referrers(*objs) -> list\n\
Neil Schemenauer48c70342001-08-09 15:38:31 +0000703Return the list of objects that directly refer to any of objs.";
704
Neil Schemenauer17e7be62001-08-10 14:46:47 +0000705static PyObject *
Martin v. Löwis560da622001-11-24 09:24:51 +0000706gc_get_referrers(PyObject *self, PyObject *args)
Neil Schemenauer48c70342001-08-09 15:38:31 +0000707{
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000708 int i;
Neil Schemenauer48c70342001-08-09 15:38:31 +0000709 PyObject *result = PyList_New(0);
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000710 for (i = 0; i < NUM_GENERATIONS; i++) {
711 if (!(gc_referrers_for(args, GEN_HEAD(i), result))) {
712 Py_DECREF(result);
713 return NULL;
714 }
Neil Schemenauer17e7be62001-08-10 14:46:47 +0000715 }
Neil Schemenauer48c70342001-08-09 15:38:31 +0000716 return result;
717}
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000718
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000719static char gc_get_objects__doc__[] =
720"get_objects() -> [...]\n"
721"\n"
722"Return a list of objects tracked by the collector (excluding the list\n"
723"returned).\n"
724;
725
726/* appending objects in a GC list to a Python list */
Martin v. Löwis155aad12001-12-02 12:21:34 +0000727static int
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000728append_objects(PyObject *py_list, PyGC_Head *gc_list)
729{
730 PyGC_Head *gc;
Tim Peters9e4ca102001-10-11 18:31:31 +0000731 for (gc = gc_list->gc.gc_next; gc != gc_list; gc = gc->gc.gc_next) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000732 PyObject *op = FROM_GC(gc);
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000733 if (op != py_list) {
Martin v. Löwis155aad12001-12-02 12:21:34 +0000734 if (PyList_Append(py_list, op)) {
735 return -1; /* exception */
736 }
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000737 }
738 }
Martin v. Löwis155aad12001-12-02 12:21:34 +0000739 return 0;
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000740}
741
742static PyObject *
743gc_get_objects(PyObject *self, PyObject *args)
744{
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000745 int i;
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000746 PyObject* result;
747
748 if (!PyArg_ParseTuple(args, ":get_objects")) /* check no args */
749 return NULL;
750 result = PyList_New(0);
Martin v. Löwisf8a6f242001-12-02 18:31:02 +0000751 if (result == NULL) {
752 return NULL;
753 }
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000754 for (i = 0; i < NUM_GENERATIONS; i++) {
755 if (append_objects(result, GEN_HEAD(i))) {
756 Py_DECREF(result);
757 return NULL;
758 }
Martin v. Löwis155aad12001-12-02 12:21:34 +0000759 }
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000760 return result;
761}
762
763
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000764static char gc__doc__ [] =
765"This module provides access to the garbage collector for reference cycles.\n"
766"\n"
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000767"enable() -- Enable automatic garbage collection.\n"
768"disable() -- Disable automatic garbage collection.\n"
769"isenabled() -- Returns true if automatic collection is enabled.\n"
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000770"collect() -- Do a full collection right now.\n"
771"set_debug() -- Set debugging flags.\n"
772"get_debug() -- Get debugging flags.\n"
773"set_threshold() -- Set the collection thresholds.\n"
774"get_threshold() -- Return the current the collection thresholds.\n"
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000775"get_objects() -- Return a list of all objects tracked by the collector.\n"
Martin v. Löwis560da622001-11-24 09:24:51 +0000776"get_referrers() -- Return the list of objects that refer to an object.\n"
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000777;
778
779static PyMethodDef GcMethods[] = {
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000780 {"enable", gc_enable, METH_VARARGS, gc_enable__doc__},
781 {"disable", gc_disable, METH_VARARGS, gc_disable__doc__},
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000782 {"isenabled", gc_isenabled, METH_VARARGS, gc_isenabled__doc__},
783 {"set_debug", gc_set_debug, METH_VARARGS, gc_set_debug__doc__},
784 {"get_debug", gc_get_debug, METH_VARARGS, gc_get_debug__doc__},
785 {"set_threshold", gc_set_thresh, METH_VARARGS, gc_set_thresh__doc__},
786 {"get_threshold", gc_get_thresh, METH_VARARGS, gc_get_thresh__doc__},
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000787 {"collect", gc_collect, METH_VARARGS, gc_collect__doc__},
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000788 {"get_objects", gc_get_objects,METH_VARARGS, gc_get_objects__doc__},
Martin v. Löwis560da622001-11-24 09:24:51 +0000789 {"get_referrers", gc_get_referrers, METH_VARARGS,
790 gc_get_referrers__doc__},
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000791 {NULL, NULL} /* Sentinel */
792};
793
794void
795initgc(void)
796{
797 PyObject *m;
798 PyObject *d;
799
800 m = Py_InitModule4("gc",
801 GcMethods,
802 gc__doc__,
803 NULL,
804 PYTHON_API_VERSION);
805 d = PyModule_GetDict(m);
806 if (garbage == NULL) {
807 garbage = PyList_New(0);
808 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000809 PyDict_SetItemString(d, "garbage", garbage);
810 PyDict_SetItemString(d, "DEBUG_STATS",
811 PyInt_FromLong(DEBUG_STATS));
812 PyDict_SetItemString(d, "DEBUG_COLLECTABLE",
813 PyInt_FromLong(DEBUG_COLLECTABLE));
814 PyDict_SetItemString(d, "DEBUG_UNCOLLECTABLE",
815 PyInt_FromLong(DEBUG_UNCOLLECTABLE));
816 PyDict_SetItemString(d, "DEBUG_INSTANCES",
817 PyInt_FromLong(DEBUG_INSTANCES));
818 PyDict_SetItemString(d, "DEBUG_OBJECTS",
819 PyInt_FromLong(DEBUG_OBJECTS));
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000820 PyDict_SetItemString(d, "DEBUG_SAVEALL",
821 PyInt_FromLong(DEBUG_SAVEALL));
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000822 PyDict_SetItemString(d, "DEBUG_LEAK",
823 PyInt_FromLong(DEBUG_LEAK));
824}
825
Neil Schemenauer43411b52001-08-30 00:05:51 +0000826/* for debugging */
827void _PyGC_Dump(PyGC_Head *g)
828{
829 _PyObject_Dump(FROM_GC(g));
830}
831
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000832#endif /* WITH_CYCLE_GC */
Neil Schemenauer43411b52001-08-30 00:05:51 +0000833
834/* extension modules might be compiled with GC support so these
835 functions must always be available */
836
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000837#undef PyObject_GC_Track
838#undef PyObject_GC_UnTrack
839#undef PyObject_GC_Del
840#undef _PyObject_GC_Malloc
841
Neil Schemenauer43411b52001-08-30 00:05:51 +0000842void
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000843PyObject_GC_Track(void *op)
Neil Schemenauer43411b52001-08-30 00:05:51 +0000844{
845 _PyObject_GC_TRACK(op);
846}
847
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000848/* for binary compatibility with 2.2 */
Neil Schemenauer43411b52001-08-30 00:05:51 +0000849void
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000850_PyObject_GC_Track(PyObject *op)
851{
852 PyObject_GC_Track(op);
853}
854
855void
856PyObject_GC_UnTrack(void *op)
Neil Schemenauer43411b52001-08-30 00:05:51 +0000857{
Neil Schemenauerb8833102002-03-29 03:04:25 +0000858#ifdef WITH_CYCLE_GC
Guido van Rossumff413af2002-03-28 20:34:59 +0000859 PyGC_Head *gc = AS_GC(op);
860 if (gc->gc.gc_next != NULL)
861 _PyObject_GC_UNTRACK(op);
Neil Schemenauerb8833102002-03-29 03:04:25 +0000862#endif
Neil Schemenauer43411b52001-08-30 00:05:51 +0000863}
864
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000865/* for binary compatibility with 2.2 */
866void
867_PyObject_GC_UnTrack(PyObject *op)
868{
869 PyObject_GC_UnTrack(op);
870}
871
Neil Schemenauer43411b52001-08-30 00:05:51 +0000872PyObject *
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000873_PyObject_GC_Malloc(size_t basicsize)
Neil Schemenauer43411b52001-08-30 00:05:51 +0000874{
875 PyObject *op;
876#ifdef WITH_CYCLE_GC
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000877 PyGC_Head *g = PyObject_MALLOC(sizeof(PyGC_Head) + basicsize);
Neil Schemenauer43411b52001-08-30 00:05:51 +0000878 if (g == NULL)
879 return (PyObject *)PyErr_NoMemory();
Tim Peters9e4ca102001-10-11 18:31:31 +0000880 g->gc.gc_next = NULL;
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000881 generations[0].count++; /* number of allocated GC objects */
882 if (generations[0].count > generations[0].threshold &&
Neil Schemenauer43411b52001-08-30 00:05:51 +0000883 enabled &&
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000884 generations[0].threshold &&
Neil Schemenauer43411b52001-08-30 00:05:51 +0000885 !collecting &&
886 !PyErr_Occurred()) {
887 collecting = 1;
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000888 collect_generations();
Neil Schemenauer43411b52001-08-30 00:05:51 +0000889 collecting = 0;
890 }
891 op = FROM_GC(g);
892#else
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000893 op = PyObject_MALLOC(basicsize);
Neil Schemenauer43411b52001-08-30 00:05:51 +0000894 if (op == NULL)
895 return (PyObject *)PyErr_NoMemory();
896
897#endif
898 return op;
899}
900
901PyObject *
902_PyObject_GC_New(PyTypeObject *tp)
903{
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000904 PyObject *op = _PyObject_GC_Malloc(_PyObject_SIZE(tp));
Tim Petersfa8efab2002-04-28 01:57:25 +0000905 if (op != NULL)
906 op = PyObject_INIT(op, tp);
907 return op;
Neil Schemenauer43411b52001-08-30 00:05:51 +0000908}
909
910PyVarObject *
Tim Peters6d483d32001-10-06 21:27:34 +0000911_PyObject_GC_NewVar(PyTypeObject *tp, int nitems)
Neil Schemenauer43411b52001-08-30 00:05:51 +0000912{
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000913 const size_t size = _PyObject_VAR_SIZE(tp, nitems);
914 PyVarObject *op = (PyVarObject *) _PyObject_GC_Malloc(size);
Tim Petersfa8efab2002-04-28 01:57:25 +0000915 if (op != NULL)
916 op = PyObject_INIT_VAR(op, tp, nitems);
917 return op;
Neil Schemenauer43411b52001-08-30 00:05:51 +0000918}
919
920PyVarObject *
Tim Peters6d483d32001-10-06 21:27:34 +0000921_PyObject_GC_Resize(PyVarObject *op, int nitems)
Neil Schemenauer43411b52001-08-30 00:05:51 +0000922{
Tim Petersf2a67da2001-10-07 03:54:51 +0000923 const size_t basicsize = _PyObject_VAR_SIZE(op->ob_type, nitems);
Neil Schemenauer43411b52001-08-30 00:05:51 +0000924#ifdef WITH_CYCLE_GC
925 PyGC_Head *g = AS_GC(op);
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000926 g = PyObject_REALLOC(g, sizeof(PyGC_Head) + basicsize);
Neil Schemenauer43411b52001-08-30 00:05:51 +0000927 if (g == NULL)
928 return (PyVarObject *)PyErr_NoMemory();
929 op = (PyVarObject *) FROM_GC(g);
930#else
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000931 op = PyObject_REALLOC(op, basicsize);
Neil Schemenauer43411b52001-08-30 00:05:51 +0000932 if (op == NULL)
933 return (PyVarObject *)PyErr_NoMemory();
934#endif
Tim Peters6d483d32001-10-06 21:27:34 +0000935 op->ob_size = nitems;
Neil Schemenauer43411b52001-08-30 00:05:51 +0000936 return op;
937}
938
939void
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000940PyObject_GC_Del(void *op)
Neil Schemenauer43411b52001-08-30 00:05:51 +0000941{
942#ifdef WITH_CYCLE_GC
943 PyGC_Head *g = AS_GC(op);
Tim Peters9e4ca102001-10-11 18:31:31 +0000944 if (g->gc.gc_next != NULL)
Neil Schemenauer43411b52001-08-30 00:05:51 +0000945 gc_list_remove(g);
Neil Schemenauer2880ae52002-05-04 05:35:20 +0000946 if (generations[0].count > 0) {
947 generations[0].count--;
Neil Schemenauer43411b52001-08-30 00:05:51 +0000948 }
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000949 PyObject_FREE(g);
Neil Schemenauer43411b52001-08-30 00:05:51 +0000950#else
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000951 PyObject_FREE(op);
Neil Schemenauer43411b52001-08-30 00:05:51 +0000952#endif
953}
954
Neil Schemenauerfec4eb12002-04-12 02:41:03 +0000955/* for binary compatibility with 2.2 */
956#undef _PyObject_GC_Del
957void
958_PyObject_GC_Del(PyObject *op)
959{
960 PyObject_GC_Del(op);
961}