blob: 71e9596e4b93a285a415ca00992c7fd78e64fd24 [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
34/* linked lists of container objects */
Guido van Rossumbca8c2e2001-10-12 20:52:48 +000035PyGC_Head _PyGC_generation0 = {{&_PyGC_generation0, &_PyGC_generation0, 0}};
36static PyGC_Head generation1 = {{&generation1, &generation1, 0}};
37static PyGC_Head generation2 = {{&generation2, &generation2, 0}};
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000038static int generation = 0; /* current generation being collected */
39
40/* collection frequencies, XXX tune these */
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +000041static int enabled = 1; /* automatic collection enabled? */
Jeremy Hylton3263dc2b2000-09-05 15:44:50 +000042static int threshold0 = 700; /* net new containers before collection */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000043static int threshold1 = 10; /* generation0 collections before collecting 1 */
44static int threshold2 = 10; /* generation1 collections before collecting 2 */
45
46/* net new objects allocated since last collection */
47static int allocated;
48
Neil Schemenauer43411b52001-08-30 00:05:51 +000049/* true if we are currently running the collector */
50static int collecting;
51
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000052/* set for debugging information */
53#define DEBUG_STATS (1<<0) /* print collection statistics */
54#define DEBUG_COLLECTABLE (1<<1) /* print collectable objects */
55#define DEBUG_UNCOLLECTABLE (1<<2) /* print uncollectable objects */
56#define DEBUG_INSTANCES (1<<3) /* print instances */
57#define DEBUG_OBJECTS (1<<4) /* print other objects */
Neil Schemenauer544de1e2000-09-22 15:22:38 +000058#define DEBUG_SAVEALL (1<<5) /* save all garbage in gc.garbage */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000059#define DEBUG_LEAK DEBUG_COLLECTABLE | \
60 DEBUG_UNCOLLECTABLE | \
61 DEBUG_INSTANCES | \
Neil Schemenauer544de1e2000-09-22 15:22:38 +000062 DEBUG_OBJECTS | \
63 DEBUG_SAVEALL
Jeremy Hyltonb709df32000-09-01 02:47:25 +000064static int debug;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000065
Neil Schemenauer43411b52001-08-30 00:05:51 +000066/* Special gc_refs value */
67#define GC_MOVED -123
68
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000069/* list of uncollectable objects */
70static PyObject *garbage;
71
Jeremy Hyltonb709df32000-09-01 02:47:25 +000072/* Python string to use if unhandled exception occurs */
73static PyObject *gc_str;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000074
75/*** list functions ***/
76
77static void
78gc_list_init(PyGC_Head *list)
79{
Tim Peters9e4ca102001-10-11 18:31:31 +000080 list->gc.gc_prev = list;
81 list->gc.gc_next = list;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000082}
83
84static void
85gc_list_append(PyGC_Head *node, PyGC_Head *list)
86{
Tim Peters9e4ca102001-10-11 18:31:31 +000087 node->gc.gc_next = list;
88 node->gc.gc_prev = list->gc.gc_prev;
89 node->gc.gc_prev->gc.gc_next = node;
90 list->gc.gc_prev = node;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000091}
92
93static void
94gc_list_remove(PyGC_Head *node)
95{
Tim Peters9e4ca102001-10-11 18:31:31 +000096 node->gc.gc_prev->gc.gc_next = node->gc.gc_next;
97 node->gc.gc_next->gc.gc_prev = node->gc.gc_prev;
98 node->gc.gc_next = NULL; /* object is not currently tracked */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +000099}
100
101static void
102gc_list_move(PyGC_Head *from, PyGC_Head *to)
103{
Tim Peters9e4ca102001-10-11 18:31:31 +0000104 if (from->gc.gc_next == from) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000105 /* empty from list */
106 gc_list_init(to);
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000107 }
108 else {
Tim Peters9e4ca102001-10-11 18:31:31 +0000109 to->gc.gc_next = from->gc.gc_next;
110 to->gc.gc_next->gc.gc_prev = to;
111 to->gc.gc_prev = from->gc.gc_prev;
112 to->gc.gc_prev->gc.gc_next = to;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000113 }
114 gc_list_init(from);
115}
116
117/* append a list onto another list, from becomes an empty list */
118static void
119gc_list_merge(PyGC_Head *from, PyGC_Head *to)
120{
121 PyGC_Head *tail;
Tim Peters9e4ca102001-10-11 18:31:31 +0000122 if (from->gc.gc_next != from) {
123 tail = to->gc.gc_prev;
124 tail->gc.gc_next = from->gc.gc_next;
125 tail->gc.gc_next->gc.gc_prev = tail;
126 to->gc.gc_prev = from->gc.gc_prev;
127 to->gc.gc_prev->gc.gc_next = to;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000128 }
129 gc_list_init(from);
130}
131
132static long
133gc_list_size(PyGC_Head *list)
134{
135 PyGC_Head *gc;
136 long n = 0;
Tim Peters9e4ca102001-10-11 18:31:31 +0000137 for (gc = list->gc.gc_next; gc != list; gc = gc->gc.gc_next) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000138 n++;
139 }
140 return n;
141}
142
143/*** end of list stuff ***/
144
145
Neil Schemenauer43411b52001-08-30 00:05:51 +0000146
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000147/* Set all gc_refs = ob_refcnt */
148static void
149update_refs(PyGC_Head *containers)
150{
Tim Peters9e4ca102001-10-11 18:31:31 +0000151 PyGC_Head *gc = containers->gc.gc_next;
152 for (; gc != containers; gc=gc->gc.gc_next) {
153 gc->gc.gc_refs = FROM_GC(gc)->ob_refcnt;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000154 }
155}
156
157static int
158visit_decref(PyObject *op, void *data)
159{
160 if (op && PyObject_IS_GC(op)) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000161 PyGC_Head *gc = AS_GC(op);
Tim Peters9e4ca102001-10-11 18:31:31 +0000162 if (gc->gc.gc_next != NULL)
163 AS_GC(op)->gc.gc_refs--;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000164 }
165 return 0;
166}
167
168/* Subtract internal references from gc_refs */
169static void
170subtract_refs(PyGC_Head *containers)
171{
172 traverseproc traverse;
Tim Peters9e4ca102001-10-11 18:31:31 +0000173 PyGC_Head *gc = containers->gc.gc_next;
174 for (; gc != containers; gc=gc->gc.gc_next) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000175 traverse = FROM_GC(gc)->ob_type->tp_traverse;
176 (void) traverse(FROM_GC(gc),
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000177 (visitproc)visit_decref,
178 NULL);
179 }
180}
181
182/* Append objects with gc_refs > 0 to roots list */
183static void
184move_roots(PyGC_Head *containers, PyGC_Head *roots)
185{
186 PyGC_Head *next;
Tim Peters9e4ca102001-10-11 18:31:31 +0000187 PyGC_Head *gc = containers->gc.gc_next;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000188 while (gc != containers) {
Tim Peters9e4ca102001-10-11 18:31:31 +0000189 next = gc->gc.gc_next;
190 if (gc->gc.gc_refs > 0) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000191 gc_list_remove(gc);
192 gc_list_append(gc, roots);
Tim Peters9e4ca102001-10-11 18:31:31 +0000193 gc->gc.gc_refs = GC_MOVED;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000194 }
195 gc = next;
196 }
197}
198
199static int
Neil Schemenauer43411b52001-08-30 00:05:51 +0000200visit_move(PyObject *op, PyGC_Head *tolist)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000201{
202 if (PyObject_IS_GC(op)) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000203 PyGC_Head *gc = AS_GC(op);
Tim Peters9e4ca102001-10-11 18:31:31 +0000204 if (gc->gc.gc_next != NULL && gc->gc.gc_refs != GC_MOVED) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000205 gc_list_remove(gc);
Neil Schemenauer43411b52001-08-30 00:05:51 +0000206 gc_list_append(gc, tolist);
Tim Peters9e4ca102001-10-11 18:31:31 +0000207 gc->gc.gc_refs = GC_MOVED;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000208 }
209 }
210 return 0;
211}
212
213/* Move objects referenced from reachable to reachable set. */
214static void
215move_root_reachable(PyGC_Head *reachable)
216{
217 traverseproc traverse;
Tim Peters9e4ca102001-10-11 18:31:31 +0000218 PyGC_Head *gc = reachable->gc.gc_next;
219 for (; gc != reachable; gc=gc->gc.gc_next) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000220 /* careful, reachable list is growing here */
Neil Schemenauer43411b52001-08-30 00:05:51 +0000221 PyObject *op = FROM_GC(gc);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000222 traverse = op->ob_type->tp_traverse;
223 (void) traverse(op,
Neil Schemenauer43411b52001-08-30 00:05:51 +0000224 (visitproc)visit_move,
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000225 (void *)reachable);
226 }
227}
228
Neil Schemenauera765c122001-11-01 17:35:23 +0000229/* return true of object has a finalization method */
230static int
231has_finalizer(PyObject *op)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000232{
Jeremy Hylton06257772000-08-31 15:10:24 +0000233 static PyObject *delstr = NULL;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000234 if (delstr == NULL) {
235 delstr = PyString_InternFromString("__del__");
Jeremy Hylton06257772000-08-31 15:10:24 +0000236 if (delstr == NULL)
237 Py_FatalError("PyGC: can't initialize __del__ string");
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000238 }
Tim Petersdb865612001-11-01 19:35:45 +0000239 return (PyInstance_Check(op) ||
240 PyType_HasFeature(op->ob_type, Py_TPFLAGS_HEAPTYPE))
241 && PyObject_HasAttr(op, delstr);
Neil Schemenauera765c122001-11-01 17:35:23 +0000242}
243
244/* Move all objects with finalizers (instances with __del__) */
245static void
246move_finalizers(PyGC_Head *unreachable, PyGC_Head *finalizers)
247{
248 PyGC_Head *next;
249 PyGC_Head *gc = unreachable->gc.gc_next;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000250 for (; gc != unreachable; gc=next) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000251 PyObject *op = FROM_GC(gc);
Tim Peters9e4ca102001-10-11 18:31:31 +0000252 next = gc->gc.gc_next;
Neil Schemenauera765c122001-11-01 17:35:23 +0000253 if (has_finalizer(op)) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000254 gc_list_remove(gc);
255 gc_list_append(gc, finalizers);
256 }
257 }
258}
259
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000260/* Move objects referenced from roots to roots */
261static void
262move_finalizer_reachable(PyGC_Head *finalizers)
263{
264 traverseproc traverse;
Tim Peters9e4ca102001-10-11 18:31:31 +0000265 PyGC_Head *gc = finalizers->gc.gc_next;
266 for (; gc != finalizers; gc=gc->gc.gc_next) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000267 /* careful, finalizers list is growing here */
Neil Schemenauer43411b52001-08-30 00:05:51 +0000268 traverse = FROM_GC(gc)->ob_type->tp_traverse;
269 (void) traverse(FROM_GC(gc),
270 (visitproc)visit_move,
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000271 (void *)finalizers);
272 }
273}
274
275static void
Jeremy Hylton06257772000-08-31 15:10:24 +0000276debug_instance(char *msg, PyInstanceObject *inst)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000277{
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000278 char *cname;
Neil Schemenauera765c122001-11-01 17:35:23 +0000279 /* simple version of instance_repr */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000280 PyObject *classname = inst->in_class->cl_name;
281 if (classname != NULL && PyString_Check(classname))
282 cname = PyString_AsString(classname);
283 else
284 cname = "?";
Jeremy Hylton06257772000-08-31 15:10:24 +0000285 PySys_WriteStderr("gc: %.100s <%.100s instance at %p>\n",
286 msg, cname, inst);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000287}
288
289static void
Jeremy Hylton06257772000-08-31 15:10:24 +0000290debug_cycle(char *msg, PyObject *op)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000291{
292 if ((debug & DEBUG_INSTANCES) && PyInstance_Check(op)) {
Jeremy Hylton06257772000-08-31 15:10:24 +0000293 debug_instance(msg, (PyInstanceObject *)op);
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000294 }
295 else if (debug & DEBUG_OBJECTS) {
Jeremy Hylton06257772000-08-31 15:10:24 +0000296 PySys_WriteStderr("gc: %.100s <%.100s %p>\n",
297 msg, op->ob_type->tp_name, op);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000298 }
299}
300
301/* Handle uncollectable garbage (cycles with finalizers). */
302static void
303handle_finalizers(PyGC_Head *finalizers, PyGC_Head *old)
304{
305 PyGC_Head *gc;
306 if (garbage == NULL) {
307 garbage = PyList_New(0);
308 }
Tim Peters9e4ca102001-10-11 18:31:31 +0000309 for (gc = finalizers->gc.gc_next; gc != finalizers;
310 gc = finalizers->gc.gc_next) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000311 PyObject *op = FROM_GC(gc);
Neil Schemenauera765c122001-11-01 17:35:23 +0000312 if ((debug & DEBUG_SAVEALL) || has_finalizer(op)) {
313 /* If SAVEALL is not set then just append objects with
314 * finalizers to the list of garbage. All objects in
315 * the finalizers list are reachable from those
316 * objects. */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000317 PyList_Append(garbage, op);
318 }
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000319 /* object is now reachable again */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000320 gc_list_remove(gc);
321 gc_list_append(gc, old);
322 }
323}
324
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000325/* Break reference cycles by clearing the containers involved. This is
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000326 * tricky business as the lists can be changing and we don't know which
327 * objects may be freed. It is possible I screwed something up here. */
328static void
329delete_garbage(PyGC_Head *unreachable, PyGC_Head *old)
330{
331 inquiry clear;
332
Tim Peters9e4ca102001-10-11 18:31:31 +0000333 while (unreachable->gc.gc_next != unreachable) {
334 PyGC_Head *gc = unreachable->gc.gc_next;
Neil Schemenauer43411b52001-08-30 00:05:51 +0000335 PyObject *op = FROM_GC(gc);
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000336 if (debug & DEBUG_SAVEALL) {
337 PyList_Append(garbage, op);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000338 }
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000339 else {
340 if ((clear = op->ob_type->tp_clear) != NULL) {
341 Py_INCREF(op);
342 clear((PyObject *)op);
343 Py_DECREF(op);
344 }
345 }
Tim Peters9e4ca102001-10-11 18:31:31 +0000346 if (unreachable->gc.gc_next == gc) {
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000347 /* object is still alive, move it, it may die later */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000348 gc_list_remove(gc);
349 gc_list_append(gc, old);
350 }
351 }
352}
353
354/* This is the main function. Read this to understand how the
355 * collection process works. */
356static long
357collect(PyGC_Head *young, PyGC_Head *old)
358{
359 long n = 0;
360 long m = 0;
361 PyGC_Head reachable;
362 PyGC_Head unreachable;
363 PyGC_Head finalizers;
364 PyGC_Head *gc;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000365
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000366 if (debug & DEBUG_STATS) {
Jeremy Hylton06257772000-08-31 15:10:24 +0000367 PySys_WriteStderr(
368 "gc: collecting generation %d...\n"
369 "gc: objects in each generation: %ld %ld %ld\n",
370 generation,
Neil Schemenauer43411b52001-08-30 00:05:51 +0000371 gc_list_size(&_PyGC_generation0),
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000372 gc_list_size(&generation1),
373 gc_list_size(&generation2));
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000374 }
375
376 /* Using ob_refcnt and gc_refs, calculate which objects in the
377 * container set are reachable from outside the set (ie. have a
378 * refcount greater than 0 when all the references within the
379 * set are taken into account */
380 update_refs(young);
381 subtract_refs(young);
382
383 /* Move everything reachable from outside the set into the
384 * reachable set (ie. gc_refs > 0). Next, move everything
385 * reachable from objects in the reachable set. */
386 gc_list_init(&reachable);
387 move_roots(young, &reachable);
388 move_root_reachable(&reachable);
389
390 /* move unreachable objects to a temporary list, new objects can be
391 * allocated after this point */
392 gc_list_init(&unreachable);
393 gc_list_move(young, &unreachable);
394
395 /* move reachable objects to next generation */
396 gc_list_merge(&reachable, old);
397
398 /* Move objects reachable from finalizers, we can't safely delete
399 * them. Python programmers should take care not to create such
400 * things. For Python finalizers means instance objects with
401 * __del__ methods. */
402 gc_list_init(&finalizers);
403 move_finalizers(&unreachable, &finalizers);
404 move_finalizer_reachable(&finalizers);
405
406 /* Collect statistics on collectable objects found and print
407 * debugging information. */
Tim Peters9e4ca102001-10-11 18:31:31 +0000408 for (gc = unreachable.gc.gc_next; gc != &unreachable;
409 gc = gc->gc.gc_next) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000410 m++;
Jeremy Hylton06257772000-08-31 15:10:24 +0000411 if (debug & DEBUG_COLLECTABLE) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000412 debug_cycle("collectable", FROM_GC(gc));
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000413 }
414 }
415 /* call tp_clear on objects in the collectable set. This will cause
416 * the reference cycles to be broken. It may also cause some objects in
417 * finalizers to be freed */
418 delete_garbage(&unreachable, old);
419
420 /* Collect statistics on uncollectable objects found and print
421 * debugging information. */
Tim Peters9e4ca102001-10-11 18:31:31 +0000422 for (gc = finalizers.gc.gc_next; gc != &finalizers;
423 gc = gc->gc.gc_next) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000424 n++;
Jeremy Hylton06257772000-08-31 15:10:24 +0000425 if (debug & DEBUG_UNCOLLECTABLE) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000426 debug_cycle("uncollectable", FROM_GC(gc));
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000427 }
428 }
Jeremy Hylton06257772000-08-31 15:10:24 +0000429 if (debug & DEBUG_STATS) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000430 if (m == 0 && n == 0) {
Jeremy Hylton06257772000-08-31 15:10:24 +0000431 PySys_WriteStderr("gc: done.\n");
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000432 }
433 else {
Jeremy Hylton06257772000-08-31 15:10:24 +0000434 PySys_WriteStderr(
435 "gc: done, %ld unreachable, %ld uncollectable.\n",
436 n+m, n);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000437 }
438 }
439
440 /* Append instances in the uncollectable set to a Python
441 * reachable list of garbage. The programmer has to deal with
442 * this if they insist on creating this type of structure. */
443 handle_finalizers(&finalizers, old);
444
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000445 if (PyErr_Occurred()) {
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000446 if (gc_str == NULL) {
447 gc_str = PyString_FromString("garbage collection");
448 }
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000449 PyErr_WriteUnraisable(gc_str);
450 Py_FatalError("unexpected exception during garbage collection");
451 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000452 allocated = 0;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000453 return n+m;
454}
455
456static long
457collect_generations(void)
458{
459 static long collections0 = 0;
460 static long collections1 = 0;
Vladimir Marangozovb16714b2000-07-10 05:37:39 +0000461 long n = 0;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000462
463
464 if (collections1 > threshold2) {
465 generation = 2;
Neil Schemenauer43411b52001-08-30 00:05:51 +0000466 gc_list_merge(&_PyGC_generation0, &generation2);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000467 gc_list_merge(&generation1, &generation2);
Tim Peters9e4ca102001-10-11 18:31:31 +0000468 if (generation2.gc.gc_next != &generation2) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000469 n = collect(&generation2, &generation2);
470 }
471 collections1 = 0;
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000472 }
473 else if (collections0 > threshold1) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000474 generation = 1;
475 collections1++;
Neil Schemenauer43411b52001-08-30 00:05:51 +0000476 gc_list_merge(&_PyGC_generation0, &generation1);
Tim Peters9e4ca102001-10-11 18:31:31 +0000477 if (generation1.gc.gc_next != &generation1) {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000478 n = collect(&generation1, &generation2);
479 }
480 collections0 = 0;
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000481 }
482 else {
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000483 generation = 0;
484 collections0++;
Tim Peters9e4ca102001-10-11 18:31:31 +0000485 if (_PyGC_generation0.gc.gc_next != &_PyGC_generation0) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000486 n = collect(&_PyGC_generation0, &generation1);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000487 }
488 }
489 return n;
490}
491
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000492static char gc_enable__doc__[] =
493"enable() -> None\n"
494"\n"
495"Enable automatic garbage collection.\n"
496;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000497
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000498static PyObject *
499gc_enable(PyObject *self, PyObject *args)
500{
501
502 if (!PyArg_ParseTuple(args, ":enable")) /* check no args */
503 return NULL;
504
505 enabled = 1;
506
507 Py_INCREF(Py_None);
508 return Py_None;
509}
510
511static char gc_disable__doc__[] =
512"disable() -> None\n"
513"\n"
514"Disable automatic garbage collection.\n"
515;
516
517static PyObject *
518gc_disable(PyObject *self, PyObject *args)
519{
520
521 if (!PyArg_ParseTuple(args, ":disable")) /* check no args */
522 return NULL;
523
524 enabled = 0;
525
526 Py_INCREF(Py_None);
527 return Py_None;
528}
529
530static char gc_isenabled__doc__[] =
531"isenabled() -> status\n"
532"\n"
533"Returns true if automatic garbage collection is enabled.\n"
534;
535
536static PyObject *
537gc_isenabled(PyObject *self, PyObject *args)
538{
539
540 if (!PyArg_ParseTuple(args, ":isenabled")) /* check no args */
541 return NULL;
542
543 return Py_BuildValue("i", enabled);
544}
545
546static char gc_collect__doc__[] =
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000547"collect() -> n\n"
548"\n"
549"Run a full collection. The number of unreachable objects is returned.\n"
550;
551
552static PyObject *
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000553gc_collect(PyObject *self, PyObject *args)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000554{
555 long n;
556
Fred Drakecc1be242000-07-12 04:42:23 +0000557 if (!PyArg_ParseTuple(args, ":collect")) /* check no args */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000558 return NULL;
559
Neil Schemenauere8c40cb2001-10-31 23:09:35 +0000560 if (collecting) {
561 n = 0; /* already collecting, don't do anything */
562 }
563 else {
564 collecting = 1;
565 generation = 2;
566 gc_list_merge(&_PyGC_generation0, &generation2);
567 gc_list_merge(&generation1, &generation2);
568 n = collect(&generation2, &generation2);
569 collecting = 0;
570 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000571
Neil Schemenauer7760cff2000-09-22 22:35:36 +0000572 return Py_BuildValue("l", n);
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000573}
574
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000575static char gc_set_debug__doc__[] =
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000576"set_debug(flags) -> None\n"
577"\n"
578"Set the garbage collection debugging flags. Debugging information is\n"
579"written to sys.stderr.\n"
580"\n"
581"flags is an integer and can have the following bits turned on:\n"
582"\n"
583" DEBUG_STATS - Print statistics during collection.\n"
584" DEBUG_COLLECTABLE - Print collectable objects found.\n"
585" DEBUG_UNCOLLECTABLE - Print unreachable but uncollectable objects found.\n"
586" DEBUG_INSTANCES - Print instance objects.\n"
587" DEBUG_OBJECTS - Print objects other than instances.\n"
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000588" DEBUG_SAVEALL - Save objects to gc.garbage rather than freeing them.\n"
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000589" DEBUG_LEAK - Debug leaking programs (everything but STATS).\n"
590;
591
592static PyObject *
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000593gc_set_debug(PyObject *self, PyObject *args)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000594{
Neil Schemenauer7760cff2000-09-22 22:35:36 +0000595 if (!PyArg_ParseTuple(args, "i:set_debug", &debug))
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000596 return NULL;
597
598 Py_INCREF(Py_None);
599 return Py_None;
600}
601
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000602static char gc_get_debug__doc__[] =
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000603"get_debug() -> flags\n"
604"\n"
605"Get the garbage collection debugging flags.\n"
606;
607
608static PyObject *
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000609gc_get_debug(PyObject *self, PyObject *args)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000610{
Fred Drakecc1be242000-07-12 04:42:23 +0000611 if (!PyArg_ParseTuple(args, ":get_debug")) /* no args */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000612 return NULL;
613
614 return Py_BuildValue("i", debug);
615}
616
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000617static char gc_set_thresh__doc__[] =
Neal Norwitz2a47c0f2002-01-29 00:53:41 +0000618"set_threshold(threshold0, [threshold1, threshold2]) -> None\n"
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000619"\n"
620"Sets the collection thresholds. Setting threshold0 to zero disables\n"
621"collection.\n"
622;
623
624static PyObject *
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000625gc_set_thresh(PyObject *self, PyObject *args)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000626{
Fred Drakecc1be242000-07-12 04:42:23 +0000627 if (!PyArg_ParseTuple(args, "i|ii:set_threshold", &threshold0,
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000628 &threshold1, &threshold2))
629 return NULL;
630
631 Py_INCREF(Py_None);
632 return Py_None;
633}
634
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000635static char gc_get_thresh__doc__[] =
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000636"get_threshold() -> (threshold0, threshold1, threshold2)\n"
637"\n"
638"Return the current collection thresholds\n"
639;
640
641static PyObject *
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000642gc_get_thresh(PyObject *self, PyObject *args)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000643{
Fred Drakecc1be242000-07-12 04:42:23 +0000644 if (!PyArg_ParseTuple(args, ":get_threshold")) /* no args */
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000645 return NULL;
646
647 return Py_BuildValue("(iii)", threshold0, threshold1, threshold2);
648}
649
Neil Schemenauer48c70342001-08-09 15:38:31 +0000650static int
Martin v. Löwis560da622001-11-24 09:24:51 +0000651referrersvisit(PyObject* obj, PyObject *objs)
Neil Schemenauer48c70342001-08-09 15:38:31 +0000652{
Martin v. Löwisc8fe77b2001-11-29 18:08:31 +0000653 int i;
654 for (i = 0; i < PyTuple_GET_SIZE(objs); i++)
655 if (PyTuple_GET_ITEM(objs, i) == obj)
656 return 1;
Neil Schemenauer48c70342001-08-09 15:38:31 +0000657 return 0;
658}
659
Neil Schemenauer17e7be62001-08-10 14:46:47 +0000660static int
Martin v. Löwis560da622001-11-24 09:24:51 +0000661gc_referrers_for(PyObject *objs, PyGC_Head *list, PyObject *resultlist)
Neil Schemenauer48c70342001-08-09 15:38:31 +0000662{
663 PyGC_Head *gc;
664 PyObject *obj;
665 traverseproc traverse;
Tim Peters9e4ca102001-10-11 18:31:31 +0000666 for (gc = list->gc.gc_next; gc != list; gc = gc->gc.gc_next) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000667 obj = FROM_GC(gc);
Neil Schemenauer48c70342001-08-09 15:38:31 +0000668 traverse = obj->ob_type->tp_traverse;
669 if (obj == objs || obj == resultlist)
670 continue;
Martin v. Löwis560da622001-11-24 09:24:51 +0000671 if (traverse(obj, (visitproc)referrersvisit, objs)) {
Neil Schemenauer17e7be62001-08-10 14:46:47 +0000672 if (PyList_Append(resultlist, obj) < 0)
673 return 0; /* error */
Neil Schemenauer48c70342001-08-09 15:38:31 +0000674 }
675 }
Neil Schemenauer17e7be62001-08-10 14:46:47 +0000676 return 1; /* no error */
Neil Schemenauer48c70342001-08-09 15:38:31 +0000677}
678
Martin v. Löwis560da622001-11-24 09:24:51 +0000679static char gc_get_referrers__doc__[]=
680"get_referrers(*objs) -> list\n\
Neil Schemenauer48c70342001-08-09 15:38:31 +0000681Return the list of objects that directly refer to any of objs.";
682
Neil Schemenauer17e7be62001-08-10 14:46:47 +0000683static PyObject *
Martin v. Löwis560da622001-11-24 09:24:51 +0000684gc_get_referrers(PyObject *self, PyObject *args)
Neil Schemenauer48c70342001-08-09 15:38:31 +0000685{
686 PyObject *result = PyList_New(0);
Martin v. Löwis560da622001-11-24 09:24:51 +0000687 if (!(gc_referrers_for(args, &_PyGC_generation0, result) &&
688 gc_referrers_for(args, &generation1, result) &&
689 gc_referrers_for(args, &generation2, result))) {
Neil Schemenauer17e7be62001-08-10 14:46:47 +0000690 Py_DECREF(result);
691 return NULL;
692 }
Neil Schemenauer48c70342001-08-09 15:38:31 +0000693 return result;
694}
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000695
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000696static char gc_get_objects__doc__[] =
697"get_objects() -> [...]\n"
698"\n"
699"Return a list of objects tracked by the collector (excluding the list\n"
700"returned).\n"
701;
702
703/* appending objects in a GC list to a Python list */
Martin v. Löwis155aad12001-12-02 12:21:34 +0000704static int
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000705append_objects(PyObject *py_list, PyGC_Head *gc_list)
706{
707 PyGC_Head *gc;
Tim Peters9e4ca102001-10-11 18:31:31 +0000708 for (gc = gc_list->gc.gc_next; gc != gc_list; gc = gc->gc.gc_next) {
Neil Schemenauer43411b52001-08-30 00:05:51 +0000709 PyObject *op = FROM_GC(gc);
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000710 if (op != py_list) {
Martin v. Löwis155aad12001-12-02 12:21:34 +0000711 if (PyList_Append(py_list, op)) {
712 return -1; /* exception */
713 }
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000714 }
715 }
Martin v. Löwis155aad12001-12-02 12:21:34 +0000716 return 0;
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000717}
718
719static PyObject *
720gc_get_objects(PyObject *self, PyObject *args)
721{
722 PyObject* result;
723
724 if (!PyArg_ParseTuple(args, ":get_objects")) /* check no args */
725 return NULL;
726 result = PyList_New(0);
Martin v. Löwisf8a6f242001-12-02 18:31:02 +0000727 if (result == NULL) {
728 return NULL;
729 }
Martin v. Löwis155aad12001-12-02 12:21:34 +0000730 if (append_objects(result, &_PyGC_generation0) ||
731 append_objects(result, &generation1) ||
732 append_objects(result, &generation2)) {
733 Py_DECREF(result);
734 return NULL;
735 }
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000736 return result;
737}
738
739
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000740static char gc__doc__ [] =
741"This module provides access to the garbage collector for reference cycles.\n"
742"\n"
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000743"enable() -- Enable automatic garbage collection.\n"
744"disable() -- Disable automatic garbage collection.\n"
745"isenabled() -- Returns true if automatic collection is enabled.\n"
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000746"collect() -- Do a full collection right now.\n"
747"set_debug() -- Set debugging flags.\n"
748"get_debug() -- Get debugging flags.\n"
749"set_threshold() -- Set the collection thresholds.\n"
750"get_threshold() -- Return the current the collection thresholds.\n"
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000751"get_objects() -- Return a list of all objects tracked by the collector.\n"
Martin v. Löwis560da622001-11-24 09:24:51 +0000752"get_referrers() -- Return the list of objects that refer to an object.\n"
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000753;
754
755static PyMethodDef GcMethods[] = {
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000756 {"enable", gc_enable, METH_VARARGS, gc_enable__doc__},
757 {"disable", gc_disable, METH_VARARGS, gc_disable__doc__},
Vladimir Marangozovf9d20c32000-08-06 22:45:31 +0000758 {"isenabled", gc_isenabled, METH_VARARGS, gc_isenabled__doc__},
759 {"set_debug", gc_set_debug, METH_VARARGS, gc_set_debug__doc__},
760 {"get_debug", gc_get_debug, METH_VARARGS, gc_get_debug__doc__},
761 {"set_threshold", gc_set_thresh, METH_VARARGS, gc_set_thresh__doc__},
762 {"get_threshold", gc_get_thresh, METH_VARARGS, gc_get_thresh__doc__},
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000763 {"collect", gc_collect, METH_VARARGS, gc_collect__doc__},
Neil Schemenauerc7c8d8e2001-08-09 15:58:59 +0000764 {"get_objects", gc_get_objects,METH_VARARGS, gc_get_objects__doc__},
Martin v. Löwis560da622001-11-24 09:24:51 +0000765 {"get_referrers", gc_get_referrers, METH_VARARGS,
766 gc_get_referrers__doc__},
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000767 {NULL, NULL} /* Sentinel */
768};
769
770void
771initgc(void)
772{
773 PyObject *m;
774 PyObject *d;
775
776 m = Py_InitModule4("gc",
777 GcMethods,
778 gc__doc__,
779 NULL,
780 PYTHON_API_VERSION);
781 d = PyModule_GetDict(m);
782 if (garbage == NULL) {
783 garbage = PyList_New(0);
784 }
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000785 PyDict_SetItemString(d, "garbage", garbage);
786 PyDict_SetItemString(d, "DEBUG_STATS",
787 PyInt_FromLong(DEBUG_STATS));
788 PyDict_SetItemString(d, "DEBUG_COLLECTABLE",
789 PyInt_FromLong(DEBUG_COLLECTABLE));
790 PyDict_SetItemString(d, "DEBUG_UNCOLLECTABLE",
791 PyInt_FromLong(DEBUG_UNCOLLECTABLE));
792 PyDict_SetItemString(d, "DEBUG_INSTANCES",
793 PyInt_FromLong(DEBUG_INSTANCES));
794 PyDict_SetItemString(d, "DEBUG_OBJECTS",
795 PyInt_FromLong(DEBUG_OBJECTS));
Neil Schemenauer544de1e2000-09-22 15:22:38 +0000796 PyDict_SetItemString(d, "DEBUG_SAVEALL",
797 PyInt_FromLong(DEBUG_SAVEALL));
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000798 PyDict_SetItemString(d, "DEBUG_LEAK",
799 PyInt_FromLong(DEBUG_LEAK));
800}
801
Neil Schemenauer43411b52001-08-30 00:05:51 +0000802/* for debugging */
803void _PyGC_Dump(PyGC_Head *g)
804{
805 _PyObject_Dump(FROM_GC(g));
806}
807
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000808#endif /* WITH_CYCLE_GC */
Neil Schemenauer43411b52001-08-30 00:05:51 +0000809
810/* extension modules might be compiled with GC support so these
811 functions must always be available */
812
813void
814_PyObject_GC_Track(PyObject *op)
815{
816 _PyObject_GC_TRACK(op);
817}
818
819void
820_PyObject_GC_UnTrack(PyObject *op)
821{
822 _PyObject_GC_UNTRACK(op);
823}
824
825PyObject *
Tim Peters6d483d32001-10-06 21:27:34 +0000826_PyObject_GC_Malloc(PyTypeObject *tp, int nitems)
Neil Schemenauer43411b52001-08-30 00:05:51 +0000827{
828 PyObject *op;
Tim Petersf2a67da2001-10-07 03:54:51 +0000829 const size_t basicsize = _PyObject_VAR_SIZE(tp, nitems);
Neil Schemenauer43411b52001-08-30 00:05:51 +0000830#ifdef WITH_CYCLE_GC
Tim Petersf2a67da2001-10-07 03:54:51 +0000831 const size_t nbytes = sizeof(PyGC_Head) + basicsize;
Neil Schemenauerdcc819a2002-03-22 15:33:15 +0000832 PyGC_Head *g = _PyMalloc_MALLOC(nbytes);
Neil Schemenauer43411b52001-08-30 00:05:51 +0000833 if (g == NULL)
834 return (PyObject *)PyErr_NoMemory();
Tim Peters9e4ca102001-10-11 18:31:31 +0000835 g->gc.gc_next = NULL;
Neil Schemenauer43411b52001-08-30 00:05:51 +0000836 allocated++;
837 if (allocated > threshold0 &&
838 enabled &&
839 threshold0 &&
840 !collecting &&
841 !PyErr_Occurred()) {
842 collecting = 1;
843 collect_generations();
844 collecting = 0;
845 }
846 op = FROM_GC(g);
847#else
Neil Schemenauerdcc819a2002-03-22 15:33:15 +0000848 op = _PyMalloc_MALLOC(basicsize);
Neil Schemenauer43411b52001-08-30 00:05:51 +0000849 if (op == NULL)
850 return (PyObject *)PyErr_NoMemory();
851
852#endif
853 return op;
854}
855
856PyObject *
857_PyObject_GC_New(PyTypeObject *tp)
858{
Tim Peters6d483d32001-10-06 21:27:34 +0000859 PyObject *op = _PyObject_GC_Malloc(tp, 0);
Neil Schemenauer43411b52001-08-30 00:05:51 +0000860 return PyObject_INIT(op, tp);
861}
862
863PyVarObject *
Tim Peters6d483d32001-10-06 21:27:34 +0000864_PyObject_GC_NewVar(PyTypeObject *tp, int nitems)
Neil Schemenauer43411b52001-08-30 00:05:51 +0000865{
Tim Peters6d483d32001-10-06 21:27:34 +0000866 PyVarObject *op = (PyVarObject *) _PyObject_GC_Malloc(tp, nitems);
867 return PyObject_INIT_VAR(op, tp, nitems);
Neil Schemenauer43411b52001-08-30 00:05:51 +0000868}
869
870PyVarObject *
Tim Peters6d483d32001-10-06 21:27:34 +0000871_PyObject_GC_Resize(PyVarObject *op, int nitems)
Neil Schemenauer43411b52001-08-30 00:05:51 +0000872{
Tim Petersf2a67da2001-10-07 03:54:51 +0000873 const size_t basicsize = _PyObject_VAR_SIZE(op->ob_type, nitems);
Neil Schemenauer43411b52001-08-30 00:05:51 +0000874#ifdef WITH_CYCLE_GC
875 PyGC_Head *g = AS_GC(op);
Neil Schemenauer1b0e4fc2002-03-22 15:41:03 +0000876 g = _PyMalloc_REALLOC(g, sizeof(PyGC_Head) + basicsize);
Neil Schemenauer43411b52001-08-30 00:05:51 +0000877 if (g == NULL)
878 return (PyVarObject *)PyErr_NoMemory();
879 op = (PyVarObject *) FROM_GC(g);
880#else
Neil Schemenauer1b0e4fc2002-03-22 15:41:03 +0000881 op = _PyMalloc_REALLOC(op, basicsize);
Neil Schemenauer43411b52001-08-30 00:05:51 +0000882 if (op == NULL)
883 return (PyVarObject *)PyErr_NoMemory();
884#endif
Tim Peters6d483d32001-10-06 21:27:34 +0000885 op->ob_size = nitems;
Neil Schemenauer43411b52001-08-30 00:05:51 +0000886 return op;
887}
888
889void
890_PyObject_GC_Del(PyObject *op)
891{
892#ifdef WITH_CYCLE_GC
893 PyGC_Head *g = AS_GC(op);
Tim Peters9e4ca102001-10-11 18:31:31 +0000894 if (g->gc.gc_next != NULL)
Neil Schemenauer43411b52001-08-30 00:05:51 +0000895 gc_list_remove(g);
896 if (allocated > 0) {
897 allocated--;
898 }
Neil Schemenauerdcc819a2002-03-22 15:33:15 +0000899 _PyMalloc_FREE(g);
Neil Schemenauer43411b52001-08-30 00:05:51 +0000900#else
Neil Schemenauerdcc819a2002-03-22 15:33:15 +0000901 _PyMalloc_FREE(op);
Neil Schemenauer43411b52001-08-30 00:05:51 +0000902#endif
903}
904