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_root *next;
59 void (*cb) PROTO ((void *));
62 static struct ggc_root *roots;
66 struct ggc_rtx *chain;
72 struct ggc_rtvec *chain;
78 struct ggc_tree *chain;
84 struct ggc_string *chain;
85 unsigned int magic_mark;
89 /* A generic allocation, with an external mark bit. */
93 struct ggc_any *chain;
94 unsigned int magic_mark;
96 /* Make sure the data is reasonably aligned. */
106 struct ggc_status *next;
107 struct ggc_rtx *rtxs;
108 struct ggc_rtvec *vecs;
109 struct ggc_tree *trees;
110 struct ggc_string *strings;
111 struct ggc_any *anys;
112 size_t bytes_alloced_since_gc;
115 /* A chain of GGC contexts. The currently active context is at the
116 front of the chain. */
117 static struct ggc_status *ggc_chain;
119 /* Some statistics. */
121 static int n_rtxs_collected;
122 static int n_vecs_collected;
123 static int n_trees_collected;
124 static int n_strings_collected;
125 static int n_anys_collected;
132 /* Local function prototypes. */
134 static void ggc_free_rtx PROTO ((struct ggc_rtx *r));
135 static void ggc_free_rtvec PROTO ((struct ggc_rtvec *v));
136 static void ggc_free_tree PROTO ((struct ggc_tree *t));
137 static void ggc_free_string PROTO ((struct ggc_string *s));
138 static void ggc_free_any PROTO ((struct ggc_any *a));
140 static void ggc_mark_rtx_ptr PROTO ((void *elt));
141 static void ggc_mark_tree_ptr PROTO ((void *elt));
142 static void ggc_mark_string_ptr PROTO ((void *elt));
143 static void ggc_mark_tree_varray_ptr PROTO ((void *elt));
144 static void ggc_mark_tree_hash_table_ptr PROTO ((void *elt));
145 static boolean ggc_mark_tree_hash_table_entry PROTO ((struct hash_entry *,
148 /* Called once to initialize the garbage collector. */
151 init_ggc PROTO ((void))
153 /* Initialize the global context. */
157 dump = fopen ("zgcdump", "w");
162 /* Start a new GGC context. Memory allocated in previous contexts
163 will not be collected while the new context is active. */
166 ggc_push_context PROTO ((void))
168 struct ggc_status *gs = (struct ggc_status *) xcalloc (1, sizeof (*gs));
169 gs->next = ggc_chain;
173 /* Finish a GC context. Any uncollected memory in the new context
174 will be merged with the old context. */
177 ggc_pop_context PROTO ((void))
182 struct ggc_string *s;
183 struct ggc_status *gs;
192 r->chain = gs->next->rtxs;
193 gs->next->rtxs = gs->rtxs;
201 v->chain = gs->next->vecs;
202 gs->next->vecs = gs->vecs;
210 t->chain = gs->next->trees;
211 gs->next->trees = gs->trees;
219 s->chain = gs->next->strings;
220 gs->next->strings = gs->strings;
223 ggc_chain = gs->next;
227 /* These allocators are dreadfully simple, with no caching whatsoever so
228 that Purify-like tools that do allocation versioning can catch errors.
229 This collector is never going to go fast anyway. */
232 ggc_alloc_rtx (nslots)
236 int size = sizeof(*n) + (nslots-1) * sizeof(rtunion);
238 n = (struct ggc_rtx *) xcalloc (1, size);
239 n->chain = ggc_chain->rtxs;
243 fprintf (dump, "alloc rtx %p\n", &n->rtx);
246 ggc_chain->bytes_alloced_since_gc += size;
252 ggc_alloc_rtvec (nelt)
256 int size = sizeof (*v) + (nelt - 1) * sizeof (rtx);
258 v = (struct ggc_rtvec *) xcalloc (1, size);
259 v->chain = ggc_chain->vecs;
263 fprintf(dump, "alloc vec %p\n", &v->vec);
266 ggc_chain->bytes_alloced_since_gc += size;
272 ggc_alloc_tree (length)
276 int size = sizeof(*n) - sizeof(n->tree) + length;
278 n = (struct ggc_tree *) xcalloc (1, size);
279 n->chain = ggc_chain->trees;
280 ggc_chain->trees = n;
283 fprintf(dump, "alloc tree %p\n", &n->tree);
286 ggc_chain->bytes_alloced_since_gc += size;
292 ggc_alloc_string (contents, length)
293 const char *contents;
296 struct ggc_string *s;
301 if (contents == NULL)
303 length = strlen (contents);
306 size = (s->string - (char *)s) + length + 1;
307 s = (struct ggc_string *) xmalloc (size);
308 s->chain = ggc_chain->strings;
309 s->magic_mark = GGC_STRING_MAGIC;
310 ggc_chain->strings = s;
313 memcpy (s->string, contents, length);
314 s->string[length] = 0;
317 fprintf(dump, "alloc string %p\n", &s->string);
320 ggc_chain->bytes_alloced_since_gc += size;
325 /* Like xmalloc, but allocates GC-able memory. */
335 bytes += (&((struct ggc_any *) 0)->u.c - (char *) 0);
337 a = (struct ggc_any *) xmalloc (bytes);
338 a->chain = ggc_chain->anys;
339 a->magic_mark = GGC_ANY_MAGIC;
342 ggc_chain->bytes_alloced_since_gc += bytes;
347 /* Freeing a bit of rtl is as simple as calling free. */
354 fprintf (dump, "collect rtx %p\n", &r->rtx);
357 memset (r, 0xAA, sizeof(*r) + ((GET_RTX_LENGTH (r->rtx.code) -1)
364 /* Freeing an rtvec is as simple as calling free. */
371 fprintf(dump, "collect vec %p\n", &v->vec);
374 memset (v, 0xBB, sizeof (*v) + ((GET_NUM_ELEM (&v->vec) - 1)
375 * sizeof (rtunion)));
381 /* Freeing a tree node is almost, but not quite, as simple as calling free.
382 Mostly we need to let the language clean up its lang_specific bits. */
389 fprintf (dump, "collect tree %p\n", &t->tree);
392 memset(&t->tree.common, 0xCC, sizeof(t->tree.common));
398 /* Freeing a string is as simple as calling free. */
402 struct ggc_string *s;
405 fprintf(dump, "collect string %p\n", s->string);
408 s->magic_mark = 0xDDDDDDDD;
415 /* Freeing anonymous memory is as simple as calling free. */
422 fprintf(dump, "collect mem %p\n", &a->u);
425 a->magic_mark = 0xEEEEEEEE;
440 if (r == NULL_RTX || r->gc_mark)
444 /* ??? If (some of) these are really pass-dependant info, do we have
445 any right poking our noses in? */
446 switch (GET_CODE (r))
449 ggc_mark_rtx (JUMP_LABEL (r));
452 ggc_mark_rtx (LABEL_REFS (r));
455 ggc_mark_rtx (LABEL_NEXTREF (r));
456 ggc_mark_rtx (CONTAINING_INSN (r));
459 ggc_mark_tree (ADDRESSOF_DECL (r));
462 ggc_mark_rtx (CONST_DOUBLE_CHAIN (r));
465 switch (NOTE_LINE_NUMBER (r))
467 case NOTE_INSN_RANGE_START:
468 case NOTE_INSN_RANGE_END:
470 ggc_mark_rtx (NOTE_RANGE_INFO (r));
474 if (NOTE_LINE_NUMBER (r) >= 0)
475 ggc_mark_string (NOTE_SOURCE_FILE (r));
484 for (fmt = GET_RTX_FORMAT (GET_CODE (r)), i = 0; *fmt ; ++fmt, ++i)
489 ggc_mark_rtx (XEXP (r, i));
492 ggc_mark_rtvec (XVEC (r, i));
495 ggc_mark_string (XSTR (r, i));
507 if (v == NULL || v->gc_mark)
511 i = GET_NUM_ELEM (v);
513 ggc_mark_rtx (RTVEC_ELT (v, i));
520 if (t == NULL_TREE || t->common.gc_mark)
522 t->common.gc_mark = 1;
524 /* Bits from common. */
525 ggc_mark_tree (TREE_TYPE (t));
526 ggc_mark_tree (TREE_CHAIN (t));
528 /* Some nodes require special handling. */
529 switch (TREE_CODE (t))
532 ggc_mark_tree (TREE_PURPOSE (t));
533 ggc_mark_tree (TREE_VALUE (t));
538 int i = TREE_VEC_LENGTH (t);
540 ggc_mark_tree (TREE_VEC_ELT (t, i));
545 ggc_mark_tree (TREE_OPERAND (t, 0));
546 ggc_mark_tree (SAVE_EXPR_CONTEXT (t));
547 ggc_mark_rtx (SAVE_EXPR_RTL (t));
551 ggc_mark_rtx (RTL_EXPR_SEQUENCE (t));
552 ggc_mark_rtx (RTL_EXPR_RTL (t));
556 ggc_mark_tree (TREE_OPERAND (t, 0));
557 ggc_mark_tree (TREE_OPERAND (t, 1));
558 ggc_mark_rtx (CALL_EXPR_RTL (t));
562 ggc_mark_tree (TREE_REALPART (t));
563 ggc_mark_tree (TREE_IMAGPART (t));
567 ggc_mark_string (TREE_STRING_POINTER (t));
571 ggc_mark_rtx (DECL_INCOMING_RTL (t));
574 case IDENTIFIER_NODE:
575 ggc_mark_string (IDENTIFIER_POINTER (t));
583 /* But in general we can handle them by class. */
584 switch (TREE_CODE_CLASS (TREE_CODE (t)))
586 case 'd': /* A decl node. */
587 ggc_mark_tree (DECL_SIZE (t));
588 ggc_mark_tree (DECL_NAME (t));
589 ggc_mark_tree (DECL_CONTEXT (t));
590 ggc_mark_tree (DECL_ARGUMENTS (t));
591 ggc_mark_tree (DECL_RESULT (t));
592 ggc_mark_tree (DECL_INITIAL (t));
593 ggc_mark_tree (DECL_ABSTRACT_ORIGIN (t));
594 ggc_mark_tree (DECL_ASSEMBLER_NAME (t));
595 ggc_mark_tree (DECL_SECTION_NAME (t));
596 ggc_mark_tree (DECL_MACHINE_ATTRIBUTES (t));
597 ggc_mark_rtx (DECL_RTL (t));
598 ggc_mark_tree (DECL_VINDEX (t));
602 case 't': /* A type node. */
603 ggc_mark_tree (TYPE_SIZE (t));
604 ggc_mark_tree (TYPE_SIZE_UNIT (t));
605 ggc_mark_tree (TYPE_ATTRIBUTES (t));
606 ggc_mark_tree (TYPE_VALUES (t));
607 ggc_mark_tree (TYPE_POINTER_TO (t));
608 ggc_mark_tree (TYPE_REFERENCE_TO (t));
609 ggc_mark_tree (TYPE_NAME (t));
610 ggc_mark_tree (TYPE_MIN_VALUE (t));
611 ggc_mark_tree (TYPE_MAX_VALUE (t));
612 ggc_mark_tree (TYPE_NEXT_VARIANT (t));
613 ggc_mark_tree (TYPE_MAIN_VARIANT (t));
614 ggc_mark_tree (TYPE_BINFO (t));
615 ggc_mark_tree (TYPE_NONCOPIED_PARTS (t));
616 ggc_mark_tree (TYPE_CONTEXT (t));
620 case 'b': /* A lexical block. */
621 ggc_mark_tree (BLOCK_VARS (t));
622 ggc_mark_tree (BLOCK_TYPE_TAGS (t));
623 ggc_mark_tree (BLOCK_SUBBLOCKS (t));
624 ggc_mark_tree (BLOCK_SUPERCONTEXT (t));
625 ggc_mark_tree (BLOCK_ABSTRACT_ORIGIN (t));
626 ggc_mark_rtx (BLOCK_END_NOTE (t));
629 case 'c': /* A constant. */
630 ggc_mark_rtx (TREE_CST_RTL (t));
633 case 'r': case '<': case '1':
634 case '2': case 'e': case 's': /* Expressions. */
636 int i = tree_code_length[TREE_CODE (t)];
638 ggc_mark_tree (TREE_OPERAND (t, i));
648 /* Mark all the elements of the varray V, which contains trees. */
651 ggc_mark_tree_varray (v)
657 for (i = v->num_elements - 1; i >= 0; --i)
658 ggc_mark_tree (VARRAY_TREE (v, i));
661 /* Mark the hash table-entry HE. It's key field is really a tree. */
664 ggc_mark_tree_hash_table_entry (he, k)
665 struct hash_entry *he;
666 hash_table_key k ATTRIBUTE_UNUSED;
668 ggc_mark_tree ((tree) he->key);
672 /* Mark all the elements of the hash-table H, which contains trees. */
675 ggc_mark_tree_hash_table (ht)
676 struct hash_table *ht;
678 hash_traverse (ht, ggc_mark_tree_hash_table_entry, /*info=*/0);
685 const ptrdiff_t d = (((struct ggc_string *) 0)->string - (char *) 0);
686 struct ggc_string *gs;
691 gs = (struct ggc_string *)(s - d);
692 if (IGNORE_MARK (gs->magic_mark) != GGC_STRING_MAGIC)
694 gs->magic_mark = GGC_STRING_MAGIC_MARK;
697 /* Mark P, allocated with ggc_alloc. */
703 const ptrdiff_t d = (&((struct ggc_any *) 0)->u.c - (char *) 0);
709 a = (struct ggc_any *) (((char*) p) - d);
710 if (IGNORE_MARK (a->magic_mark) != GGC_ANY_MAGIC)
712 a->magic_mark = GGC_ANY_MAGIC_MARK;
715 /* The top level mark-and-sweep routine. */
720 struct ggc_rtx *r, **rp;
721 struct ggc_rtvec *v, **vp;
722 struct ggc_tree *t, **tp;
723 struct ggc_string *s, **sp;
725 struct ggc_status *gs;
726 struct ggc_any *a, **ap;
727 int time, n_rtxs, n_trees, n_vecs, n_strings, n_anys;
729 #ifndef ENABLE_CHECKING
730 /* See if it's even worth our while. */
731 if (ggc_chain->bytes_alloced_since_gc < 64*1024)
736 fputs (" {GC ", stderr);
738 time = get_run_time ();
740 /* Clean out all of the GC marks. */
741 for (gs = ggc_chain; gs; gs = gs->next)
743 for (r = gs->rtxs; r != NULL; r = r->chain)
745 for (v = gs->vecs; v != NULL; v = v->chain)
747 for (t = gs->trees; t != NULL; t = t->chain)
748 t->tree.common.gc_mark = 0;
749 for (s = gs->strings; s != NULL; s = s->chain)
750 s->magic_mark = GGC_STRING_MAGIC;
751 for (a = gs->anys; a != NULL; a = a->chain)
752 a->magic_mark = GGC_ANY_MAGIC;
755 /* Mark through all the roots. */
756 for (x = roots; x != NULL; x = x->next)
759 int s = x->size, n = x->nelt;
760 void (*cb) PROTO ((void *)) = x->cb;
763 for (i = 0; i < n; ++i, elt += s)
767 /* Sweep the resulting dead nodes. */
771 rp = &ggc_chain->rtxs;
776 struct ggc_rtx *chain = r->chain;
788 n_rtxs_collected += n_rtxs;
792 vp = &ggc_chain->vecs;
797 struct ggc_rtvec *chain = v->chain;
809 n_vecs_collected += n_vecs;
813 tp = &ggc_chain->trees;
814 t = ggc_chain->trees;
818 struct ggc_tree *chain = t->chain;
819 if (!t->tree.common.gc_mark)
830 n_trees_collected += n_trees;
834 sp = &ggc_chain->strings;
835 s = ggc_chain->strings;
839 struct ggc_string *chain = s->chain;
840 if (! IS_MARKED (s->magic_mark))
851 n_strings_collected += n_strings;
853 /* The generic data. */
855 ap = &ggc_chain->anys;
860 struct ggc_any *chain = a->chain;
861 if (! IS_MARKED (a->magic_mark))
871 n_anys_collected += n_anys;
873 ggc_chain->bytes_alloced_since_gc = 0;
875 time = get_run_time () - time;
880 time = (time + 500) / 1000;
881 fprintf (stderr, "%dr,%dv,%dt,%ds,%da %d.%03d}", n_rtxs, n_vecs,
882 n_trees, n_strings, n_anys, time / 1000, time % 1000);
886 /* Manipulate global roots that are needed between calls to gc. */
889 ggc_add_root (base, nelt, size, cb)
892 void (*cb) PROTO ((void *));
894 struct ggc_root *x = (struct ggc_root *) xmalloc (sizeof(*x));
906 ggc_add_rtx_root (base, nelt)
910 ggc_add_root (base, nelt, sizeof(rtx), ggc_mark_rtx_ptr);
914 ggc_add_tree_root (base, nelt)
918 ggc_add_root (base, nelt, sizeof(tree), ggc_mark_tree_ptr);
922 ggc_add_string_root (base, nelt)
926 ggc_add_root (base, nelt, sizeof(char *), ggc_mark_string_ptr);
929 /* Add V (a varray full of trees) to the list of GC roots. */
932 ggc_add_tree_varray_root (base, nelt)
936 ggc_add_root (base, nelt, sizeof (varray_type),
937 ggc_mark_tree_varray_ptr);
940 /* Add HT (a hash-table where ever key is a tree) to the list of GC
944 ggc_add_tree_hash_table_root (base, nelt)
945 struct hash_table **base;
948 ggc_add_root (base, nelt, sizeof (struct hash_table *),
949 ggc_mark_tree_hash_table_ptr);
956 struct ggc_root *x, **p;
958 p = &roots, x = roots;
975 ggc_mark_rtx_ptr (elt)
978 ggc_mark_rtx (*(rtx *)elt);
982 ggc_mark_tree_ptr (elt)
985 ggc_mark_tree (*(tree *)elt);
988 /* Type-correct function to pass to ggc_add_root. It just forwards
989 ELT (which is really a char **) to ggc_mark_string. */
992 ggc_mark_string_ptr (elt)
995 ggc_mark_string (*(char **)elt);
998 /* Type-correct function to pass to ggc_add_root. It just forwards
999 ELT (which is really a varray_type *) to ggc_mark_tree_varray. */
1002 ggc_mark_tree_varray_ptr (elt)
1005 ggc_mark_tree_varray (*(varray_type *)elt);
1008 /* Type-correct function to pass to ggc_add_root. It just forwards
1009 ELT (which is really a struct hash_table **) to
1010 ggc_mark_tree_hash_table. */
1013 ggc_mark_tree_hash_table_ptr (elt)
1016 ggc_mark_tree_hash_table (*(struct hash_table **) elt);
1020 /* GDB really should have a memory search function. Since this is just
1021 for initial debugging, I won't even pretend to get the __data_start
1022 to work on any but alpha-dec-linux-gnu. */
1024 search_data(void **start, void *target)
1026 extern void *__data_start[];
1027 void **_end = (void **)sbrk(0);
1030 start = __data_start;
1031 while (start < _end)
1033 if (*start == target)