1 /* Simple garbage collection for the GNU compiler.
2 Copyright (C) 1998, 1999 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 /* Constants for general use. */
55 /* Global lists of roots, rtxs, and trees. */
59 struct ggc_rtx *chain;
65 struct ggc_rtvec *chain;
71 struct ggc_tree *chain;
77 struct ggc_string *chain;
78 unsigned int magic_mark;
82 /* A generic allocation, with an external mark bit. */
86 struct ggc_any *chain;
87 unsigned int magic_mark;
89 /* Make sure the data is reasonably aligned. */
93 #ifdef HAVE_LONG_DOUBLE
103 struct ggc_status *next;
104 struct ggc_rtx *rtxs;
105 struct ggc_rtvec *vecs;
106 struct ggc_tree *trees;
107 struct ggc_string *strings;
108 struct ggc_any *anys;
109 size_t bytes_alloced_since_gc;
112 /* A chain of GGC contexts. The currently active context is at the
113 front of the chain. */
114 static struct ggc_status *ggc_chain;
116 /* Some statistics. */
118 static int n_rtxs_collected;
119 static int n_vecs_collected;
120 static int n_trees_collected;
121 static int n_strings_collected;
122 static int n_anys_collected;
129 /* Local function prototypes. */
131 static void ggc_free_rtx PROTO ((struct ggc_rtx *r));
132 static void ggc_free_rtvec PROTO ((struct ggc_rtvec *v));
133 static void ggc_free_tree PROTO ((struct ggc_tree *t));
134 static void ggc_free_string PROTO ((struct ggc_string *s));
135 static void ggc_free_any PROTO ((struct ggc_any *a));
137 /* Called once to initialize the garbage collector. */
140 init_ggc PROTO ((void))
142 /* Initialize the global context. */
146 dump = fopen ("zgcdump", "w");
150 ggc_alloc_string ("", 0);
151 ggc_add_string_root (&empty_string, 1);
154 /* Start a new GGC context. Memory allocated in previous contexts
155 will not be collected while the new context is active. */
158 ggc_push_context PROTO ((void))
160 struct ggc_status *gs = (struct ggc_status *) xcalloc (1, sizeof (*gs));
161 gs->next = ggc_chain;
165 /* Finish a GC context. Any uncollected memory in the new context
166 will be merged with the old context. */
169 ggc_pop_context PROTO ((void))
174 struct ggc_string *s;
175 struct ggc_status *gs;
184 r->chain = gs->next->rtxs;
185 gs->next->rtxs = gs->rtxs;
193 v->chain = gs->next->vecs;
194 gs->next->vecs = gs->vecs;
202 t->chain = gs->next->trees;
203 gs->next->trees = gs->trees;
211 s->chain = gs->next->strings;
212 gs->next->strings = gs->strings;
215 gs->next->bytes_alloced_since_gc += gs->bytes_alloced_since_gc;
217 ggc_chain = gs->next;
221 /* These allocators are dreadfully simple, with no caching whatsoever so
222 that Purify-like tools that do allocation versioning can catch errors.
223 This collector is never going to go fast anyway. */
226 ggc_alloc_rtx (nslots)
230 int size = sizeof(*n) + (nslots-1) * sizeof(rtunion);
232 n = (struct ggc_rtx *) xcalloc (1, size);
233 n->chain = ggc_chain->rtxs;
237 fprintf (dump, "alloc rtx %p\n", &n->rtx);
240 ggc_chain->bytes_alloced_since_gc += size;
246 ggc_alloc_rtvec (nelt)
250 int size = sizeof (*v) + (nelt - 1) * sizeof (rtx);
252 v = (struct ggc_rtvec *) xcalloc (1, size);
253 v->chain = ggc_chain->vecs;
257 fprintf(dump, "alloc vec %p\n", &v->vec);
260 ggc_chain->bytes_alloced_since_gc += size;
266 ggc_alloc_tree (length)
270 int size = sizeof(*n) - sizeof(n->tree) + length;
272 n = (struct ggc_tree *) xcalloc (1, size);
273 n->chain = ggc_chain->trees;
274 ggc_chain->trees = n;
277 fprintf(dump, "alloc tree %p\n", &n->tree);
280 ggc_chain->bytes_alloced_since_gc += size;
286 ggc_alloc_string (contents, length)
287 const char *contents;
290 struct ggc_string *s;
295 if (contents == NULL)
297 length = strlen (contents);
300 size = (s->string - (char *)s) + length + 1;
301 s = (struct ggc_string *) xmalloc (size);
302 s->chain = ggc_chain->strings;
303 s->magic_mark = GGC_STRING_MAGIC;
304 ggc_chain->strings = s;
307 memcpy (s->string, contents, length);
308 s->string[length] = 0;
311 fprintf(dump, "alloc string %p\n", &s->string);
314 ggc_chain->bytes_alloced_since_gc += size;
319 /* Like xmalloc, but allocates GC-able memory. */
329 bytes += (&((struct ggc_any *) 0)->u.c - (char *) 0);
331 a = (struct ggc_any *) xmalloc (bytes);
332 a->chain = ggc_chain->anys;
333 a->magic_mark = GGC_ANY_MAGIC;
336 ggc_chain->bytes_alloced_since_gc += bytes;
341 /* Freeing a bit of rtl is as simple as calling free. */
348 fprintf (dump, "collect rtx %p\n", &r->rtx);
351 memset (r, 0xAA, sizeof(*r) + ((GET_RTX_LENGTH (r->rtx.code) -1)
358 /* Freeing an rtvec is as simple as calling free. */
365 fprintf(dump, "collect vec %p\n", &v->vec);
368 memset (v, 0xBB, sizeof (*v) + ((GET_NUM_ELEM (&v->vec) - 1)
375 /* Freeing a tree node is almost, but not quite, as simple as calling free.
376 Mostly we need to let the language clean up its lang_specific bits. */
383 fprintf (dump, "collect tree %p\n", &t->tree);
386 memset(&t->tree.common, 0xCC, sizeof(t->tree.common));
392 /* Freeing a string is as simple as calling free. */
396 struct ggc_string *s;
399 fprintf(dump, "collect string %p\n", s->string);
402 s->magic_mark = 0xDDDDDDDD;
409 /* Freeing anonymous memory is as simple as calling free. */
416 fprintf(dump, "collect mem %p\n", &a->u);
419 a->magic_mark = 0xEEEEEEEE;
431 int marked = r->gc_mark;
438 ggc_set_mark_rtvec (v)
441 int marked = v->gc_mark;
448 ggc_set_mark_tree (t)
451 int marked = t->common.gc_mark;
453 t->common.gc_mark = 1;
461 const ptrdiff_t d = (((struct ggc_string *) 0)->string - (char *) 0);
462 struct ggc_string *gs;
467 gs = (struct ggc_string *)(s - d);
468 if (IGNORE_MARK (gs->magic_mark) != GGC_STRING_MAGIC)
470 gs->magic_mark = GGC_STRING_MAGIC_MARK;
473 /* Mark P, allocated with ggc_alloc. */
479 const ptrdiff_t d = (&((struct ggc_any *) 0)->u.c - (char *) 0);
485 a = (struct ggc_any *) (((char*) p) - d);
486 if (IGNORE_MARK (a->magic_mark) != GGC_ANY_MAGIC)
488 a->magic_mark = GGC_ANY_MAGIC_MARK;
491 /* The top level mark-and-sweep routine. */
496 struct ggc_rtx *r, **rp;
497 struct ggc_rtvec *v, **vp;
498 struct ggc_tree *t, **tp;
499 struct ggc_string *s, **sp;
500 struct ggc_status *gs;
501 struct ggc_any *a, **ap;
502 int time, n_rtxs, n_trees, n_vecs, n_strings, n_anys;
504 #if !defined(ENABLE_CHECKING)
505 /* See if it's even worth our while. */
506 if (ggc_chain->bytes_alloced_since_gc < 4*1024*1024)
511 fputs (" {GC ", stderr);
513 time = get_run_time ();
515 /* Clean out all of the GC marks. */
516 for (gs = ggc_chain; gs; gs = gs->next)
518 for (r = gs->rtxs; r != NULL; r = r->chain)
520 for (v = gs->vecs; v != NULL; v = v->chain)
522 for (t = gs->trees; t != NULL; t = t->chain)
523 t->tree.common.gc_mark = 0;
524 for (s = gs->strings; s != NULL; s = s->chain)
525 s->magic_mark = GGC_STRING_MAGIC;
526 for (a = gs->anys; a != NULL; a = a->chain)
527 a->magic_mark = GGC_ANY_MAGIC;
532 /* Sweep the resulting dead nodes. */
536 rp = &ggc_chain->rtxs;
541 struct ggc_rtx *chain = r->chain;
553 n_rtxs_collected += n_rtxs;
557 vp = &ggc_chain->vecs;
562 struct ggc_rtvec *chain = v->chain;
574 n_vecs_collected += n_vecs;
578 tp = &ggc_chain->trees;
579 t = ggc_chain->trees;
583 struct ggc_tree *chain = t->chain;
584 if (!t->tree.common.gc_mark)
595 n_trees_collected += n_trees;
599 sp = &ggc_chain->strings;
600 s = ggc_chain->strings;
604 struct ggc_string *chain = s->chain;
605 if (! IS_MARKED (s->magic_mark))
616 n_strings_collected += n_strings;
618 /* The generic data. */
620 ap = &ggc_chain->anys;
625 struct ggc_any *chain = a->chain;
626 if (! IS_MARKED (a->magic_mark))
636 n_anys_collected += n_anys;
638 ggc_chain->bytes_alloced_since_gc = 0;
640 time = get_run_time () - time;
645 time = (time + 500) / 1000;
646 fprintf (stderr, "%dr,%dv,%dt,%ds,%da %d.%03d}", n_rtxs, n_vecs,
647 n_trees, n_strings, n_anys, time / 1000, time % 1000);
652 /* GDB really should have a memory search function. Since this is just
653 for initial debugging, I won't even pretend to get the __data_start
654 to work on any but alpha-dec-linux-gnu. */
656 search_data(void **start, void *target)
658 extern void *__data_start[];
659 void **_end = (void **)sbrk(0);
662 start = __data_start;
665 if (*start == target)