1 /* Simple garbage collection for the GNU compiler.
2 Copyright (C) 1998 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
30 /* Debugging flags. */
32 /* Zap memory before freeing to catch dangling pointers. */
35 /* Log alloc and release. Don't enable this unless you want a
36 really really lot of data. */
39 /* Some magic tags for strings and anonymous memory, hoping to catch
40 certain errors wrt marking memory. */
42 #define IS_MARKED(X) ((X) & 1)
43 #define IGNORE_MARK(X) ((X) & -2)
45 #define GGC_STRING_MAGIC ((unsigned int)0xa1b2c3d4)
46 #define GGC_STRING_MAGIC_MARK ((unsigned int)0xa1b2c3d4 | 1)
48 #define GGC_ANY_MAGIC ((unsigned int)0xa9bacbdc)
49 #define GGC_ANY_MAGIC_MARK ((unsigned int)0xa9bacbdc | 1)
51 /* Global lists of roots, rtxs, and trees. */
55 struct ggc_rtx *chain;
61 struct ggc_rtvec *chain;
67 struct ggc_tree *chain;
73 struct ggc_string *chain;
74 unsigned int magic_mark;
78 /* A generic allocation, with an external mark bit. */
82 struct ggc_any *chain;
83 unsigned int magic_mark;
85 /* Make sure the data is reasonably aligned. */
95 struct ggc_status *next;
97 struct ggc_rtvec *vecs;
98 struct ggc_tree *trees;
99 struct ggc_string *strings;
100 struct ggc_any *anys;
101 size_t bytes_alloced_since_gc;
104 /* A chain of GGC contexts. The currently active context is at the
105 front of the chain. */
106 static struct ggc_status *ggc_chain;
108 /* Some statistics. */
110 static int n_rtxs_collected;
111 static int n_vecs_collected;
112 static int n_trees_collected;
113 static int n_strings_collected;
114 static int n_anys_collected;
121 /* Local function prototypes. */
123 static void ggc_free_rtx PROTO ((struct ggc_rtx *r));
124 static void ggc_free_rtvec PROTO ((struct ggc_rtvec *v));
125 static void ggc_free_tree PROTO ((struct ggc_tree *t));
126 static void ggc_free_string PROTO ((struct ggc_string *s));
127 static void ggc_free_any PROTO ((struct ggc_any *a));
129 /* Called once to initialize the garbage collector. */
132 init_ggc PROTO ((void))
134 /* Initialize the global context. */
138 dump = fopen ("zgcdump", "w");
143 /* Start a new GGC context. Memory allocated in previous contexts
144 will not be collected while the new context is active. */
147 ggc_push_context PROTO ((void))
149 struct ggc_status *gs = (struct ggc_status *) xcalloc (1, sizeof (*gs));
150 gs->next = ggc_chain;
154 /* Finish a GC context. Any uncollected memory in the new context
155 will be merged with the old context. */
158 ggc_pop_context PROTO ((void))
163 struct ggc_string *s;
164 struct ggc_status *gs;
173 r->chain = gs->next->rtxs;
174 gs->next->rtxs = gs->rtxs;
182 v->chain = gs->next->vecs;
183 gs->next->vecs = gs->vecs;
191 t->chain = gs->next->trees;
192 gs->next->trees = gs->trees;
200 s->chain = gs->next->strings;
201 gs->next->strings = gs->strings;
204 ggc_chain = gs->next;
208 /* These allocators are dreadfully simple, with no caching whatsoever so
209 that Purify-like tools that do allocation versioning can catch errors.
210 This collector is never going to go fast anyway. */
213 ggc_alloc_rtx (nslots)
217 int size = sizeof(*n) + (nslots-1) * sizeof(rtunion);
219 n = (struct ggc_rtx *) xcalloc (1, size);
220 n->chain = ggc_chain->rtxs;
224 fprintf (dump, "alloc rtx %p\n", &n->rtx);
227 ggc_chain->bytes_alloced_since_gc += size;
233 ggc_alloc_rtvec (nelt)
237 int size = sizeof (*v) + (nelt - 1) * sizeof (rtx);
239 v = (struct ggc_rtvec *) xcalloc (1, size);
240 v->chain = ggc_chain->vecs;
244 fprintf(dump, "alloc vec %p\n", &v->vec);
247 ggc_chain->bytes_alloced_since_gc += size;
253 ggc_alloc_tree (length)
257 int size = sizeof(*n) - sizeof(n->tree) + length;
259 n = (struct ggc_tree *) xcalloc (1, size);
260 n->chain = ggc_chain->trees;
261 ggc_chain->trees = n;
264 fprintf(dump, "alloc tree %p\n", &n->tree);
267 ggc_chain->bytes_alloced_since_gc += size;
273 ggc_alloc_string (contents, length)
274 const char *contents;
277 struct ggc_string *s;
282 if (contents == NULL)
284 length = strlen (contents);
287 size = (s->string - (char *)s) + length + 1;
288 s = (struct ggc_string *) xmalloc (size);
289 s->chain = ggc_chain->strings;
290 s->magic_mark = GGC_STRING_MAGIC;
291 ggc_chain->strings = s;
294 memcpy (s->string, contents, length);
295 s->string[length] = 0;
298 fprintf(dump, "alloc string %p\n", &s->string);
301 ggc_chain->bytes_alloced_since_gc += size;
306 /* Like xmalloc, but allocates GC-able memory. */
316 bytes += (&((struct ggc_any *) 0)->u.c - (char *) 0);
318 a = (struct ggc_any *) xmalloc (bytes);
319 a->chain = ggc_chain->anys;
320 a->magic_mark = GGC_ANY_MAGIC;
323 ggc_chain->bytes_alloced_since_gc += bytes;
328 /* Freeing a bit of rtl is as simple as calling free. */
335 fprintf (dump, "collect rtx %p\n", &r->rtx);
338 memset (r, 0xAA, sizeof(*r) + ((GET_RTX_LENGTH (r->rtx.code) -1)
345 /* Freeing an rtvec is as simple as calling free. */
352 fprintf(dump, "collect vec %p\n", &v->vec);
355 memset (v, 0xBB, sizeof (*v) + ((GET_NUM_ELEM (&v->vec) - 1)
356 * sizeof (rtunion)));
362 /* Freeing a tree node is almost, but not quite, as simple as calling free.
363 Mostly we need to let the language clean up its lang_specific bits. */
370 fprintf (dump, "collect tree %p\n", &t->tree);
373 memset(&t->tree.common, 0xCC, sizeof(t->tree.common));
379 /* Freeing a string is as simple as calling free. */
383 struct ggc_string *s;
386 fprintf(dump, "collect string %p\n", s->string);
389 s->magic_mark = 0xDDDDDDDD;
396 /* Freeing anonymous memory is as simple as calling free. */
403 fprintf(dump, "collect mem %p\n", &a->u);
406 a->magic_mark = 0xEEEEEEEE;
418 int marked = r->gc_mark;
425 ggc_set_mark_rtvec (v)
428 int marked = v->gc_mark;
435 ggc_set_mark_tree (t)
438 int marked = t->common.gc_mark;
440 t->common.gc_mark = 1;
448 const ptrdiff_t d = (((struct ggc_string *) 0)->string - (char *) 0);
449 struct ggc_string *gs;
454 gs = (struct ggc_string *)(s - d);
455 if (IGNORE_MARK (gs->magic_mark) != GGC_STRING_MAGIC)
457 gs->magic_mark = GGC_STRING_MAGIC_MARK;
460 /* Mark P, allocated with ggc_alloc. */
466 const ptrdiff_t d = (&((struct ggc_any *) 0)->u.c - (char *) 0);
472 a = (struct ggc_any *) (((char*) p) - d);
473 if (IGNORE_MARK (a->magic_mark) != GGC_ANY_MAGIC)
475 a->magic_mark = GGC_ANY_MAGIC_MARK;
478 /* The top level mark-and-sweep routine. */
483 struct ggc_rtx *r, **rp;
484 struct ggc_rtvec *v, **vp;
485 struct ggc_tree *t, **tp;
486 struct ggc_string *s, **sp;
488 struct ggc_status *gs;
489 struct ggc_any *a, **ap;
490 int time, n_rtxs, n_trees, n_vecs, n_strings, n_anys;
492 #ifndef ENABLE_CHECKING
493 /* See if it's even worth our while. */
494 if (ggc_chain->bytes_alloced_since_gc < 64*1024)
499 fputs (" {GC ", stderr);
501 time = get_run_time ();
503 /* Clean out all of the GC marks. */
504 for (gs = ggc_chain; gs; gs = gs->next)
506 for (r = gs->rtxs; r != NULL; r = r->chain)
508 for (v = gs->vecs; v != NULL; v = v->chain)
510 for (t = gs->trees; t != NULL; t = t->chain)
511 t->tree.common.gc_mark = 0;
512 for (s = gs->strings; s != NULL; s = s->chain)
513 s->magic_mark = GGC_STRING_MAGIC;
514 for (a = gs->anys; a != NULL; a = a->chain)
515 a->magic_mark = GGC_ANY_MAGIC;
518 /* Mark through all the roots. */
519 for (x = roots; x != NULL; x = x->next)
522 int s = x->size, n = x->nelt;
523 void (*cb) PROTO ((void *)) = x->cb;
526 for (i = 0; i < n; ++i, elt += s)
530 /* Sweep the resulting dead nodes. */
534 rp = &ggc_chain->rtxs;
539 struct ggc_rtx *chain = r->chain;
551 n_rtxs_collected += n_rtxs;
555 vp = &ggc_chain->vecs;
560 struct ggc_rtvec *chain = v->chain;
572 n_vecs_collected += n_vecs;
576 tp = &ggc_chain->trees;
577 t = ggc_chain->trees;
581 struct ggc_tree *chain = t->chain;
582 if (!t->tree.common.gc_mark)
593 n_trees_collected += n_trees;
597 sp = &ggc_chain->strings;
598 s = ggc_chain->strings;
602 struct ggc_string *chain = s->chain;
603 if (! IS_MARKED (s->magic_mark))
614 n_strings_collected += n_strings;
616 /* The generic data. */
618 ap = &ggc_chain->anys;
623 struct ggc_any *chain = a->chain;
624 if (! IS_MARKED (a->magic_mark))
634 n_anys_collected += n_anys;
636 ggc_chain->bytes_alloced_since_gc = 0;
638 time = get_run_time () - time;
643 time = (time + 500) / 1000;
644 fprintf (stderr, "%dr,%dv,%dt,%ds,%da %d.%03d}", n_rtxs, n_vecs,
645 n_trees, n_strings, n_anys, time / 1000, time % 1000);
650 /* GDB really should have a memory search function. Since this is just
651 for initial debugging, I won't even pretend to get the __data_start
652 to work on any but alpha-dec-linux-gnu. */
654 search_data(void **start, void *target)
656 extern void *__data_start[];
657 void **_end = (void **)sbrk(0);
660 start = __data_start;
663 if (*start == target)