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. */
388 switch (TREE_CODE_CLASS (TREE_CODE (&t->tree)))
390 case 'd': /* A decl node. */
391 case 't': /* A type node. */
392 lang_cleanup_tree (&t->tree);
397 fprintf (dump, "collect tree %p\n", &t->tree);
400 memset(&t->tree.common, 0xCC, sizeof(t->tree.common));
406 /* Freeing a string is as simple as calling free. */
410 struct ggc_string *s;
413 fprintf(dump, "collect string %p\n", s->string);
416 s->magic_mark = 0xDDDDDDDD;
423 /* Freeing anonymous memory is as simple as calling free. */
430 fprintf(dump, "collect mem %p\n", &a->u);
433 a->magic_mark = 0xEEEEEEEE;
448 if (r == NULL_RTX || r->gc_mark)
452 /* ??? If (some of) these are really pass-dependant info, do we have
453 any right poking our noses in? */
454 switch (GET_CODE (r))
457 ggc_mark_rtx (JUMP_LABEL (r));
460 ggc_mark_rtx (LABEL_REFS (r));
463 ggc_mark_rtx (LABEL_NEXTREF (r));
464 ggc_mark_rtx (CONTAINING_INSN (r));
467 ggc_mark_tree (ADDRESSOF_DECL (r));
470 ggc_mark_rtx (CONST_DOUBLE_CHAIN (r));
473 switch (NOTE_LINE_NUMBER (r))
475 case NOTE_INSN_RANGE_START:
476 case NOTE_INSN_RANGE_END:
478 ggc_mark_rtx (NOTE_RANGE_INFO (r));
482 if (NOTE_LINE_NUMBER (r) >= 0)
483 ggc_mark_string (NOTE_SOURCE_FILE (r));
492 for (fmt = GET_RTX_FORMAT (GET_CODE (r)), i = 0; *fmt ; ++fmt, ++i)
497 ggc_mark_rtx (XEXP (r, i));
500 ggc_mark_rtvec (XVEC (r, i));
503 ggc_mark_string (XSTR (r, i));
515 if (v == NULL || v->gc_mark)
519 i = GET_NUM_ELEM (v);
521 ggc_mark_rtx (RTVEC_ELT (v, i));
528 if (t == NULL_TREE || t->common.gc_mark)
530 t->common.gc_mark = 1;
532 /* Bits from common. */
533 ggc_mark_tree (TREE_TYPE (t));
534 ggc_mark_tree (TREE_CHAIN (t));
536 /* Some nodes require special handling. */
537 switch (TREE_CODE (t))
540 ggc_mark_tree (TREE_PURPOSE (t));
541 ggc_mark_tree (TREE_VALUE (t));
546 int i = TREE_VEC_LENGTH (t);
548 ggc_mark_tree (TREE_VEC_ELT (t, i));
553 ggc_mark_tree (TREE_OPERAND (t, 0));
554 ggc_mark_tree (SAVE_EXPR_CONTEXT (t));
555 ggc_mark_rtx (SAVE_EXPR_RTL (t));
559 ggc_mark_rtx (RTL_EXPR_SEQUENCE (t));
560 ggc_mark_rtx (RTL_EXPR_RTL (t));
564 ggc_mark_tree (TREE_OPERAND (t, 0));
565 ggc_mark_tree (TREE_OPERAND (t, 1));
566 ggc_mark_rtx (CALL_EXPR_RTL (t));
570 ggc_mark_tree (TREE_REALPART (t));
571 ggc_mark_tree (TREE_IMAGPART (t));
575 ggc_mark_string (TREE_STRING_POINTER (t));
579 ggc_mark_rtx (DECL_INCOMING_RTL (t));
582 case IDENTIFIER_NODE:
583 ggc_mark_string (IDENTIFIER_POINTER (t));
591 /* But in general we can handle them by class. */
592 switch (TREE_CODE_CLASS (TREE_CODE (t)))
594 case 'd': /* A decl node. */
595 ggc_mark_tree (DECL_SIZE (t));
596 ggc_mark_tree (DECL_NAME (t));
597 ggc_mark_tree (DECL_CONTEXT (t));
598 ggc_mark_tree (DECL_ARGUMENTS (t));
599 ggc_mark_tree (DECL_RESULT (t));
600 ggc_mark_tree (DECL_INITIAL (t));
601 ggc_mark_tree (DECL_ABSTRACT_ORIGIN (t));
602 ggc_mark_tree (DECL_ASSEMBLER_NAME (t));
603 ggc_mark_tree (DECL_SECTION_NAME (t));
604 ggc_mark_tree (DECL_MACHINE_ATTRIBUTES (t));
605 ggc_mark_rtx (DECL_RTL (t));
606 ggc_mark_tree (DECL_VINDEX (t));
610 case 't': /* A type node. */
611 ggc_mark_tree (TYPE_SIZE (t));
612 ggc_mark_tree (TYPE_SIZE_UNIT (t));
613 ggc_mark_tree (TYPE_ATTRIBUTES (t));
614 ggc_mark_tree (TYPE_VALUES (t));
615 ggc_mark_tree (TYPE_POINTER_TO (t));
616 ggc_mark_tree (TYPE_REFERENCE_TO (t));
617 ggc_mark_tree (TYPE_NAME (t));
618 ggc_mark_tree (TYPE_MIN_VALUE (t));
619 ggc_mark_tree (TYPE_MAX_VALUE (t));
620 ggc_mark_tree (TYPE_NEXT_VARIANT (t));
621 ggc_mark_tree (TYPE_MAIN_VARIANT (t));
622 ggc_mark_tree (TYPE_BINFO (t));
623 ggc_mark_tree (TYPE_NONCOPIED_PARTS (t));
624 ggc_mark_tree (TYPE_CONTEXT (t));
628 case 'b': /* A lexical block. */
629 ggc_mark_tree (BLOCK_VARS (t));
630 ggc_mark_tree (BLOCK_TYPE_TAGS (t));
631 ggc_mark_tree (BLOCK_SUBBLOCKS (t));
632 ggc_mark_tree (BLOCK_SUPERCONTEXT (t));
633 ggc_mark_tree (BLOCK_ABSTRACT_ORIGIN (t));
634 ggc_mark_rtx (BLOCK_END_NOTE (t));
637 case 'c': /* A constant. */
638 ggc_mark_rtx (TREE_CST_RTL (t));
641 case 'r': case '<': case '1':
642 case '2': case 'e': case 's': /* Expressions. */
644 int i = tree_code_length[TREE_CODE (t)];
646 ggc_mark_tree (TREE_OPERAND (t, i));
656 /* Mark all the elements of the varray V, which contains trees. */
659 ggc_mark_tree_varray (v)
665 for (i = v->num_elements - 1; i >= 0; --i)
666 ggc_mark_tree (VARRAY_TREE (v, i));
669 /* Mark the hash table-entry HE. It's key field is really a tree. */
672 ggc_mark_tree_hash_table_entry (he, k)
673 struct hash_entry *he;
674 hash_table_key k ATTRIBUTE_UNUSED;
676 ggc_mark_tree ((tree) he->key);
680 /* Mark all the elements of the hash-table H, which contains trees. */
683 ggc_mark_tree_hash_table (ht)
684 struct hash_table *ht;
686 hash_traverse (ht, ggc_mark_tree_hash_table_entry, /*info=*/0);
693 const ptrdiff_t d = (((struct ggc_string *) 0)->string - (char *) 0);
694 struct ggc_string *gs;
699 gs = (struct ggc_string *)(s - d);
700 if (IGNORE_MARK (gs->magic_mark) != GGC_STRING_MAGIC)
702 gs->magic_mark = GGC_STRING_MAGIC_MARK;
705 /* Mark P, allocated with ggc_alloc. */
711 const ptrdiff_t d = (&((struct ggc_any *) 0)->u.c - (char *) 0);
717 a = (struct ggc_any *) (((char*) p) - d);
718 if (IGNORE_MARK (a->magic_mark) != GGC_ANY_MAGIC)
720 a->magic_mark = GGC_ANY_MAGIC_MARK;
723 /* The top level mark-and-sweep routine. */
728 struct ggc_rtx *r, **rp;
729 struct ggc_rtvec *v, **vp;
730 struct ggc_tree *t, **tp;
731 struct ggc_string *s, **sp;
733 struct ggc_status *gs;
734 struct ggc_any *a, **ap;
735 int time, n_rtxs, n_trees, n_vecs, n_strings, n_anys;
737 #ifndef ENABLE_CHECKING
738 /* See if it's even worth our while. */
739 if (ggc_chain->bytes_alloced_since_gc < 64*1024)
744 fputs (" {GC ", stderr);
746 time = get_run_time ();
748 /* Clean out all of the GC marks. */
749 for (gs = ggc_chain; gs; gs = gs->next)
751 for (r = gs->rtxs; r != NULL; r = r->chain)
753 for (v = gs->vecs; v != NULL; v = v->chain)
755 for (t = gs->trees; t != NULL; t = t->chain)
756 t->tree.common.gc_mark = 0;
757 for (s = gs->strings; s != NULL; s = s->chain)
758 s->magic_mark = GGC_STRING_MAGIC;
759 for (a = gs->anys; a != NULL; a = a->chain)
760 a->magic_mark = GGC_ANY_MAGIC;
763 /* Mark through all the roots. */
764 for (x = roots; x != NULL; x = x->next)
767 int s = x->size, n = x->nelt;
768 void (*cb) PROTO ((void *)) = x->cb;
771 for (i = 0; i < n; ++i, elt += s)
775 /* Sweep the resulting dead nodes. */
779 rp = &ggc_chain->rtxs;
784 struct ggc_rtx *chain = r->chain;
796 n_rtxs_collected += n_rtxs;
800 vp = &ggc_chain->vecs;
805 struct ggc_rtvec *chain = v->chain;
817 n_vecs_collected += n_vecs;
821 tp = &ggc_chain->trees;
822 t = ggc_chain->trees;
826 struct ggc_tree *chain = t->chain;
827 if (!t->tree.common.gc_mark)
838 n_trees_collected += n_trees;
842 sp = &ggc_chain->strings;
843 s = ggc_chain->strings;
847 struct ggc_string *chain = s->chain;
848 if (! IS_MARKED (s->magic_mark))
859 n_strings_collected += n_strings;
861 /* The generic data. */
863 ap = &ggc_chain->anys;
868 struct ggc_any *chain = a->chain;
869 if (! IS_MARKED (a->magic_mark))
879 n_anys_collected += n_anys;
881 ggc_chain->bytes_alloced_since_gc = 0;
883 time = get_run_time () - time;
888 time = (time + 500) / 1000;
889 fprintf (stderr, "%dr,%dv,%dt,%ds,%da %d.%03d}", n_rtxs, n_vecs,
890 n_trees, n_strings, n_anys, time / 1000, time % 1000);
894 /* Manipulate global roots that are needed between calls to gc. */
897 ggc_add_root (base, nelt, size, cb)
900 void (*cb) PROTO ((void *));
902 struct ggc_root *x = (struct ggc_root *) xmalloc (sizeof(*x));
914 ggc_add_rtx_root (base, nelt)
918 ggc_add_root (base, nelt, sizeof(rtx), ggc_mark_rtx_ptr);
922 ggc_add_tree_root (base, nelt)
926 ggc_add_root (base, nelt, sizeof(tree), ggc_mark_tree_ptr);
930 ggc_add_string_root (base, nelt)
934 ggc_add_root (base, nelt, sizeof(char *), ggc_mark_string_ptr);
937 /* Add V (a varray full of trees) to the list of GC roots. */
940 ggc_add_tree_varray_root (base, nelt)
944 ggc_add_root (base, nelt, sizeof (varray_type),
945 ggc_mark_tree_varray_ptr);
948 /* Add HT (a hash-table where ever key is a tree) to the list of GC
952 ggc_add_tree_hash_table_root (base, nelt)
953 struct hash_table **base;
956 ggc_add_root (base, nelt, sizeof (struct hash_table *),
957 ggc_mark_tree_hash_table_ptr);
964 struct ggc_root *x, **p;
966 p = &roots, x = roots;
983 ggc_mark_rtx_ptr (elt)
986 ggc_mark_rtx (*(rtx *)elt);
990 ggc_mark_tree_ptr (elt)
993 ggc_mark_tree (*(tree *)elt);
996 /* Type-correct function to pass to ggc_add_root. It just forwards
997 ELT (which is really a char **) to ggc_mark_string. */
1000 ggc_mark_string_ptr (elt)
1003 ggc_mark_string (*(char **)elt);
1006 /* Type-correct function to pass to ggc_add_root. It just forwards
1007 ELT (which is really a varray_type *) to ggc_mark_tree_varray. */
1010 ggc_mark_tree_varray_ptr (elt)
1013 ggc_mark_tree_varray (*(varray_type *)elt);
1016 /* Type-correct function to pass to ggc_add_root. It just forwards
1017 ELT (which is really a struct hash_table **) to
1018 ggc_mark_tree_hash_table. */
1021 ggc_mark_tree_hash_table_ptr (elt)
1024 ggc_mark_tree_hash_table (*(struct hash_table **) elt);
1028 /* GDB really should have a memory search function. Since this is just
1029 for initial debugging, I won't even pretend to get the __data_start
1030 to work on any but alpha-dec-linux-gnu. */
1032 search_data(void **start, void *target)
1034 extern void *__data_start[];
1035 void **_end = (void **)sbrk(0);
1038 start = __data_start;
1039 while (start < _end)
1041 if (*start == target)