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 /* 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 gs->next->bytes_alloced_since_gc += gs->bytes_alloced_since_gc;
206 ggc_chain = gs->next;
210 /* These allocators are dreadfully simple, with no caching whatsoever so
211 that Purify-like tools that do allocation versioning can catch errors.
212 This collector is never going to go fast anyway. */
215 ggc_alloc_rtx (nslots)
219 int size = sizeof(*n) + (nslots-1) * sizeof(rtunion);
221 n = (struct ggc_rtx *) xcalloc (1, size);
222 n->chain = ggc_chain->rtxs;
226 fprintf (dump, "alloc rtx %p\n", &n->rtx);
229 ggc_chain->bytes_alloced_since_gc += size;
235 ggc_alloc_rtvec (nelt)
239 int size = sizeof (*v) + (nelt - 1) * sizeof (rtx);
241 v = (struct ggc_rtvec *) xcalloc (1, size);
242 v->chain = ggc_chain->vecs;
246 fprintf(dump, "alloc vec %p\n", &v->vec);
249 ggc_chain->bytes_alloced_since_gc += size;
255 ggc_alloc_tree (length)
259 int size = sizeof(*n) - sizeof(n->tree) + length;
261 n = (struct ggc_tree *) xcalloc (1, size);
262 n->chain = ggc_chain->trees;
263 ggc_chain->trees = n;
266 fprintf(dump, "alloc tree %p\n", &n->tree);
269 ggc_chain->bytes_alloced_since_gc += size;
275 ggc_alloc_string (contents, length)
276 const char *contents;
279 struct ggc_string *s;
284 if (contents == NULL)
286 length = strlen (contents);
289 size = (s->string - (char *)s) + length + 1;
290 s = (struct ggc_string *) xmalloc (size);
291 s->chain = ggc_chain->strings;
292 s->magic_mark = GGC_STRING_MAGIC;
293 ggc_chain->strings = s;
296 memcpy (s->string, contents, length);
297 s->string[length] = 0;
300 fprintf(dump, "alloc string %p\n", &s->string);
303 ggc_chain->bytes_alloced_since_gc += size;
308 /* Like xmalloc, but allocates GC-able memory. */
318 bytes += (&((struct ggc_any *) 0)->u.c - (char *) 0);
320 a = (struct ggc_any *) xmalloc (bytes);
321 a->chain = ggc_chain->anys;
322 a->magic_mark = GGC_ANY_MAGIC;
325 ggc_chain->bytes_alloced_since_gc += bytes;
330 /* Freeing a bit of rtl is as simple as calling free. */
337 fprintf (dump, "collect rtx %p\n", &r->rtx);
340 memset (r, 0xAA, sizeof(*r) + ((GET_RTX_LENGTH (r->rtx.code) -1)
347 /* Freeing an rtvec is as simple as calling free. */
354 fprintf(dump, "collect vec %p\n", &v->vec);
357 memset (v, 0xBB, sizeof (*v) + ((GET_NUM_ELEM (&v->vec) - 1)
358 * sizeof (rtunion)));
364 /* Freeing a tree node is almost, but not quite, as simple as calling free.
365 Mostly we need to let the language clean up its lang_specific bits. */
372 fprintf (dump, "collect tree %p\n", &t->tree);
375 memset(&t->tree.common, 0xCC, sizeof(t->tree.common));
381 /* Freeing a string is as simple as calling free. */
385 struct ggc_string *s;
388 fprintf(dump, "collect string %p\n", s->string);
391 s->magic_mark = 0xDDDDDDDD;
398 /* Freeing anonymous memory is as simple as calling free. */
405 fprintf(dump, "collect mem %p\n", &a->u);
408 a->magic_mark = 0xEEEEEEEE;
420 int marked = r->gc_mark;
427 ggc_set_mark_rtvec (v)
430 int marked = v->gc_mark;
437 ggc_set_mark_tree (t)
440 int marked = t->common.gc_mark;
442 t->common.gc_mark = 1;
450 const ptrdiff_t d = (((struct ggc_string *) 0)->string - (char *) 0);
451 struct ggc_string *gs;
456 gs = (struct ggc_string *)(s - d);
457 if (IGNORE_MARK (gs->magic_mark) != GGC_STRING_MAGIC)
459 gs->magic_mark = GGC_STRING_MAGIC_MARK;
462 /* Mark P, allocated with ggc_alloc. */
468 const ptrdiff_t d = (&((struct ggc_any *) 0)->u.c - (char *) 0);
474 a = (struct ggc_any *) (((char*) p) - d);
475 if (IGNORE_MARK (a->magic_mark) != GGC_ANY_MAGIC)
477 a->magic_mark = GGC_ANY_MAGIC_MARK;
480 /* The top level mark-and-sweep routine. */
485 struct ggc_rtx *r, **rp;
486 struct ggc_rtvec *v, **vp;
487 struct ggc_tree *t, **tp;
488 struct ggc_string *s, **sp;
490 struct ggc_status *gs;
491 struct ggc_any *a, **ap;
492 int time, n_rtxs, n_trees, n_vecs, n_strings, n_anys;
494 #if !defined(ENABLE_CHECKING)
495 /* See if it's even worth our while. */
496 if (ggc_chain->bytes_alloced_since_gc < 4*1024*1024)
501 fputs (" {GC ", stderr);
503 time = get_run_time ();
505 /* Clean out all of the GC marks. */
506 for (gs = ggc_chain; gs; gs = gs->next)
508 for (r = gs->rtxs; r != NULL; r = r->chain)
510 for (v = gs->vecs; v != NULL; v = v->chain)
512 for (t = gs->trees; t != NULL; t = t->chain)
513 t->tree.common.gc_mark = 0;
514 for (s = gs->strings; s != NULL; s = s->chain)
515 s->magic_mark = GGC_STRING_MAGIC;
516 for (a = gs->anys; a != NULL; a = a->chain)
517 a->magic_mark = GGC_ANY_MAGIC;
520 /* Mark through all the roots. */
521 for (x = roots; x != NULL; x = x->next)
524 int s = x->size, n = x->nelt;
525 void (*cb) PROTO ((void *)) = x->cb;
528 for (i = 0; i < n; ++i, elt += s)
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)