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 /* Global lists of roots, rtxs, and trees. */
43 struct ggc_root
*next
;
47 void (*cb
) PROTO ((void *));
50 static struct ggc_root
*roots
;
54 struct ggc_rtx
*chain
;
60 struct ggc_rtvec
*chain
;
66 struct ggc_tree
*chain
;
72 struct ggc_string
*chain
;
77 /* A generic allocation, with an external mark bit. */
81 struct ggc_any
*chain
;
84 /* Make sure the data is reasonably aligned. */
92 #define GGC_STRING_MAGIC ((unsigned int)0xa1b2c3d4)
96 struct ggc_status
*next
;
98 struct ggc_rtvec
*vecs
;
99 struct ggc_tree
*trees
;
100 struct ggc_string
*strings
;
101 struct ggc_any
*anys
;
102 size_t bytes_alloced_since_gc
;
105 /* A chain of GGC contexts. The currently active context is at the
106 front of the chain. */
107 static struct ggc_status
*ggc_chain
;
109 /* Some statistics. */
111 static int n_rtxs_collected
;
112 static int n_vecs_collected
;
113 static int n_trees_collected
;
114 static int n_strings_collected
;
115 static int n_anys_collected
;
122 /* Local function prototypes. */
124 static void ggc_free_rtx
PROTO ((struct ggc_rtx
*r
));
125 static void ggc_free_tree
PROTO ((struct ggc_tree
*t
));
126 static void ggc_mark_rtx_ptr
PROTO ((void *elt
));
127 static void ggc_mark_tree_ptr
PROTO ((void *elt
));
128 static void ggc_mark_string_ptr
PROTO ((void *elt
));
129 static void ggc_mark_tree_varray_ptr
PROTO ((void *elt
));
130 static void ggc_mark_tree_hash_table_ptr
PROTO ((void *elt
));
131 static boolean ggc_mark_tree_hash_table_entry
PROTO ((struct hash_entry
*,
134 /* Called once to initialize the garbage collector. */
137 init_ggc
PROTO ((void))
139 /* Initialize the global context. */
143 dump
= fopen ("zgcdump", "w");
148 /* Start a new GGC context. Memory allocated in previous contexts
149 will not be collected while the new context is active. */
152 ggc_push_context
PROTO ((void))
154 struct ggc_status
*gs
= (struct ggc_status
*) xcalloc (1, sizeof (*gs
));
155 gs
->next
= ggc_chain
;
159 /* Finish a GC context. Any uncollected memory in the new context
160 will be merged with the old context. */
163 ggc_pop_context
PROTO ((void))
168 struct ggc_string
*s
;
169 struct ggc_status
*gs
;
178 r
->chain
= gs
->next
->rtxs
;
179 gs
->next
->rtxs
= gs
->rtxs
;
187 v
->chain
= gs
->next
->vecs
;
188 gs
->next
->vecs
= gs
->vecs
;
196 t
->chain
= gs
->next
->trees
;
197 gs
->next
->trees
= gs
->trees
;
205 s
->chain
= gs
->next
->strings
;
206 gs
->next
->strings
= gs
->strings
;
209 ggc_chain
= gs
->next
;
213 /* These allocators are dreadfully simple, with no caching whatsoever so
214 that Purify-like tools that do allocation versioning can catch errors.
215 This collector is never going to go fast anyway. */
218 ggc_alloc_rtx (nslots
)
222 int size
= sizeof(*n
) + (nslots
-1) * sizeof(rtunion
);
224 n
= (struct ggc_rtx
*) xcalloc (1, size
);
225 n
->chain
= ggc_chain
->rtxs
;
229 fprintf (dump
, "alloc rtx %p\n", &n
->rtx
);
232 ggc_chain
->bytes_alloced_since_gc
+= size
;
238 ggc_alloc_rtvec (nelt
)
242 int size
= sizeof (*v
) + (nelt
- 1) * sizeof (rtx
);
244 v
= (struct ggc_rtvec
*) xcalloc (1, size
);
245 v
->chain
= ggc_chain
->vecs
;
249 fprintf(dump
, "alloc vec %p\n", &v
->vec
);
252 ggc_chain
->bytes_alloced_since_gc
+= size
;
258 ggc_alloc_tree (length
)
262 int size
= sizeof(*n
) - sizeof(n
->tree
) + length
;
264 n
= (struct ggc_tree
*) xcalloc (1, size
);
265 n
->chain
= ggc_chain
->trees
;
266 ggc_chain
->trees
= n
;
269 fprintf(dump
, "alloc tree %p\n", &n
->tree
);
272 ggc_chain
->bytes_alloced_since_gc
+= size
;
278 ggc_alloc_string (contents
, length
)
279 const char *contents
;
282 struct ggc_string
*s
;
287 if (contents
== NULL
)
289 length
= strlen (contents
);
292 size
= (s
->string
- (char *)s
) + length
+ 1;
293 s
= (struct ggc_string
*) xmalloc(size
);
294 s
->chain
= ggc_chain
->strings
;
295 s
->magic_mark
= GGC_STRING_MAGIC
;
297 bcopy (contents
, s
->string
, length
);
298 s
->string
[length
] = 0;
299 ggc_chain
->strings
= s
;
302 fprintf(dump
, "alloc string %p\n", &s
->string
);
305 ggc_chain
->bytes_alloced_since_gc
+= size
;
310 /* Like xmalloc, but allocates GC-able memory. */
320 bytes
+= (&((struct ggc_any
*) 0)->u
.c
- (char *) 0);
322 a
= (struct ggc_any
*) xmalloc (bytes
);
323 a
->chain
= ggc_chain
->anys
;
329 /* Freeing a bit of rtl is as simple as calling free. */
336 fprintf (dump
, "collect rtx %p\n", &r
->rtx
);
339 memset (r
, 0xAA, sizeof(*r
) + ((GET_RTX_LENGTH (r
->rtx
.code
) -1)
346 /* Freeing an rtvec is as simple as calling free. */
353 fprintf(dump
, "collect vec %p\n", &v
->vec
);
356 memset (v
, 0xBB, sizeof (*v
) + ((GET_NUM_ELEM (&v
->vec
) - 1)
357 * sizeof (rtunion
)));
363 /* Freeing a tree node is almost, but not quite, as simple as calling free.
364 Mostly we need to let the language clean up its lang_specific bits. */
370 switch (TREE_CODE_CLASS (TREE_CODE (&t
->tree
)))
372 case 'd': /* A decl node. */
373 case 't': /* A type node. */
374 lang_cleanup_tree (&t
->tree
);
379 fprintf (dump
, "collect tree %p\n", &t
->tree
);
382 memset(&t
->tree
.common
, 0xCC, sizeof(t
->tree
.common
));
388 /* Freeing a string is as simple as calling free. */
392 struct ggc_string
*s
;
395 fprintf(dump
, "collect string %p\n", s
->string
);
398 s
->magic_mark
= 0xDDDDDDDD;
414 if (r
== NULL_RTX
|| r
->gc_mark
)
418 /* ??? If (some of) these are really pass-dependant info, do we have
419 any right poking our noses in? */
420 switch (GET_CODE (r
))
423 ggc_mark_rtx (JUMP_LABEL (r
));
426 ggc_mark_rtx (LABEL_REFS (r
));
429 ggc_mark_rtx (LABEL_NEXTREF (r
));
430 ggc_mark_rtx (CONTAINING_INSN (r
));
433 ggc_mark_tree (ADDRESSOF_DECL (r
));
436 ggc_mark_rtx (CONST_DOUBLE_CHAIN (r
));
439 switch (NOTE_LINE_NUMBER (r
))
441 case NOTE_INSN_RANGE_START
:
442 case NOTE_INSN_RANGE_END
:
444 ggc_mark_rtx (NOTE_RANGE_INFO (r
));
448 if (NOTE_LINE_NUMBER (r
) >= 0)
449 ggc_mark_string (NOTE_SOURCE_FILE (r
));
458 for (fmt
= GET_RTX_FORMAT (GET_CODE (r
)), i
= 0; *fmt
; ++fmt
, ++i
)
463 ggc_mark_rtx (XEXP (r
, i
));
466 ggc_mark_rtvec (XVEC (r
, i
));
469 ggc_mark_string (XSTR (r
, i
));
481 if (v
== NULL
|| v
->gc_mark
)
485 i
= GET_NUM_ELEM (v
);
487 ggc_mark_rtx (RTVEC_ELT (v
, i
));
494 if (t
== NULL_TREE
|| t
->common
.gc_mark
)
496 t
->common
.gc_mark
= 1;
498 /* Bits from common. */
499 ggc_mark_tree (TREE_TYPE (t
));
500 ggc_mark_tree (TREE_CHAIN (t
));
502 /* Some nodes require special handling. */
503 switch (TREE_CODE (t
))
506 ggc_mark_tree (TREE_PURPOSE (t
));
507 ggc_mark_tree (TREE_VALUE (t
));
512 int i
= TREE_VEC_LENGTH (t
);
514 ggc_mark_tree (TREE_VEC_ELT (t
, i
));
519 ggc_mark_tree (TREE_OPERAND (t
, 0));
520 ggc_mark_tree (SAVE_EXPR_CONTEXT (t
));
521 ggc_mark_rtx (SAVE_EXPR_RTL (t
));
525 ggc_mark_rtx (RTL_EXPR_SEQUENCE (t
));
526 ggc_mark_rtx (RTL_EXPR_RTL (t
));
530 ggc_mark_tree (TREE_OPERAND (t
, 0));
531 ggc_mark_tree (TREE_OPERAND (t
, 1));
532 ggc_mark_rtx (CALL_EXPR_RTL (t
));
536 ggc_mark_tree (TREE_REALPART (t
));
537 ggc_mark_tree (TREE_IMAGPART (t
));
541 ggc_mark_string (TREE_STRING_POINTER (t
));
545 ggc_mark_rtx (DECL_INCOMING_RTL (t
));
548 case IDENTIFIER_NODE
:
549 ggc_mark_string (IDENTIFIER_POINTER (t
));
557 /* But in general we can handle them by class. */
558 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
560 case 'd': /* A decl node. */
561 ggc_mark_tree (DECL_SIZE (t
));
562 ggc_mark_tree (DECL_NAME (t
));
563 ggc_mark_tree (DECL_CONTEXT (t
));
564 ggc_mark_tree (DECL_ARGUMENTS (t
));
565 ggc_mark_tree (DECL_RESULT (t
));
566 ggc_mark_tree (DECL_INITIAL (t
));
567 ggc_mark_tree (DECL_ABSTRACT_ORIGIN (t
));
568 ggc_mark_tree (DECL_ASSEMBLER_NAME (t
));
569 ggc_mark_tree (DECL_SECTION_NAME (t
));
570 ggc_mark_tree (DECL_MACHINE_ATTRIBUTES (t
));
571 ggc_mark_rtx (DECL_RTL (t
));
572 ggc_mark_tree (DECL_VINDEX (t
));
576 case 't': /* A type node. */
577 ggc_mark_tree (TYPE_SIZE (t
));
578 ggc_mark_tree (TYPE_SIZE_UNIT (t
));
579 ggc_mark_tree (TYPE_ATTRIBUTES (t
));
580 ggc_mark_tree (TYPE_VALUES (t
));
581 ggc_mark_tree (TYPE_POINTER_TO (t
));
582 ggc_mark_tree (TYPE_REFERENCE_TO (t
));
583 ggc_mark_tree (TYPE_NAME (t
));
584 ggc_mark_tree (TYPE_MIN_VALUE (t
));
585 ggc_mark_tree (TYPE_MAX_VALUE (t
));
586 ggc_mark_tree (TYPE_NEXT_VARIANT (t
));
587 ggc_mark_tree (TYPE_MAIN_VARIANT (t
));
588 ggc_mark_tree (TYPE_BINFO (t
));
589 ggc_mark_tree (TYPE_NONCOPIED_PARTS (t
));
590 ggc_mark_tree (TYPE_CONTEXT (t
));
594 case 'b': /* A lexical block. */
595 ggc_mark_tree (BLOCK_VARS (t
));
596 ggc_mark_tree (BLOCK_TYPE_TAGS (t
));
597 ggc_mark_tree (BLOCK_SUBBLOCKS (t
));
598 ggc_mark_tree (BLOCK_SUPERCONTEXT (t
));
599 ggc_mark_tree (BLOCK_ABSTRACT_ORIGIN (t
));
600 ggc_mark_rtx (BLOCK_END_NOTE (t
));
603 case 'c': /* A constant. */
604 ggc_mark_rtx (TREE_CST_RTL (t
));
607 case 'r': case '<': case '1':
608 case '2': case 'e': case 's': /* Expressions. */
610 int i
= tree_code_length
[TREE_CODE (t
)];
612 ggc_mark_tree (TREE_OPERAND (t
, i
));
622 /* Mark all the elements of the varray V, which contains trees. */
625 ggc_mark_tree_varray (v
)
631 for (i
= v
->num_elements
- 1; i
>= 0; --i
)
632 ggc_mark_tree (VARRAY_TREE (v
, i
));
635 /* Mark the hash table-entry HE. It's key field is really a tree. */
638 ggc_mark_tree_hash_table_entry (he
, k
)
639 struct hash_entry
*he
;
640 hash_table_key k ATTRIBUTE_UNUSED
;
642 ggc_mark_tree ((tree
) he
->key
);
646 /* Mark all the elements of the hash-table H, which contains trees. */
649 ggc_mark_tree_hash_table (ht
)
650 struct hash_table
*ht
;
652 hash_traverse (ht
, ggc_mark_tree_hash_table_entry
, /*info=*/0);
659 unsigned int *magic
= (unsigned int *)s
- 1;
664 if ((*magic
& ~(unsigned)1) != GGC_STRING_MAGIC
)
666 *magic
= GGC_STRING_MAGIC
| 1;
669 /* Mark P, allocated with ggc_alloc. */
676 ptrdiff_t d
= (&((struct ggc_any
*) 0)->u
.c
- (char *) 0);
677 a
= (struct ggc_any
*) (((char*) p
) - d
);
681 /* The top level mark-and-sweep routine. */
686 struct ggc_rtx
*r
, **rp
;
687 struct ggc_rtvec
*v
, **vp
;
688 struct ggc_tree
*t
, **tp
;
689 struct ggc_string
*s
, **sp
;
691 struct ggc_status
*gs
;
692 struct ggc_any
*a
, **ap
;
693 int time
, n_rtxs
, n_trees
, n_vecs
, n_strings
, n_anys
;
696 /* See if it's even worth our while. */
697 if (ggc_chain
->bytes_alloced_since_gc
< 64*1024)
702 fputs (" {GC ", stderr
);
704 time
= get_run_time ();
706 /* Clean out all of the GC marks. */
707 for (gs
= ggc_chain
; gs
; gs
= gs
->next
)
709 for (r
= gs
->rtxs
; r
!= NULL
; r
= r
->chain
)
711 for (v
= gs
->vecs
; v
!= NULL
; v
= v
->chain
)
713 for (t
= gs
->trees
; t
!= NULL
; t
= t
->chain
)
714 t
->tree
.common
.gc_mark
= 0;
715 for (s
= gs
->strings
; s
!= NULL
; s
= s
->chain
)
716 s
->magic_mark
= GGC_STRING_MAGIC
;
717 for (a
= gs
->anys
; a
!= NULL
; a
= a
->chain
)
721 /* Mark through all the roots. */
722 for (x
= roots
; x
!= NULL
; x
= x
->next
)
725 int s
= x
->size
, n
= x
->nelt
;
726 void (*cb
) PROTO ((void *)) = x
->cb
;
729 for (i
= 0; i
< n
; ++i
, elt
+= s
)
733 /* Sweep the resulting dead nodes. */
737 rp
= &ggc_chain
->rtxs
;
742 struct ggc_rtx
*chain
= r
->chain
;
754 n_rtxs_collected
+= n_rtxs
;
758 vp
= &ggc_chain
->vecs
;
763 struct ggc_rtvec
*chain
= v
->chain
;
775 n_vecs_collected
+= n_vecs
;
779 tp
= &ggc_chain
->trees
;
780 t
= ggc_chain
->trees
;
784 struct ggc_tree
*chain
= t
->chain
;
785 if (!t
->tree
.common
.gc_mark
)
796 n_trees_collected
+= n_trees
;
800 sp
= &ggc_chain
->strings
;
801 s
= ggc_chain
->strings
;
805 struct ggc_string
*chain
= s
->chain
;
806 if (!(s
->magic_mark
& 1))
817 n_strings_collected
+= n_strings
;
819 /* The generic data. */
821 ap
= &ggc_chain
->anys
;
826 struct ggc_any
*chain
= a
->chain
;
837 n_anys_collected
+= n_anys
;
839 ggc_chain
->bytes_alloced_since_gc
= 0;
841 time
= get_run_time () - time
;
846 time
= (time
+ 500) / 1000;
847 fprintf (stderr
, "%dr,%dv,%dt,%ds,%da %d.%03d}", n_rtxs
, n_vecs
,
848 n_trees
, n_strings
, n_anys
, time
/ 1000, time
% 1000);
852 /* Manipulate global roots that are needed between calls to gc. */
855 ggc_add_root (base
, nelt
, size
, cb
)
858 void (*cb
) PROTO ((void *));
860 struct ggc_root
*x
= (struct ggc_root
*) xmalloc (sizeof(*x
));
872 ggc_add_rtx_root (base
, nelt
)
876 ggc_add_root (base
, nelt
, sizeof(rtx
), ggc_mark_rtx_ptr
);
880 ggc_add_tree_root (base
, nelt
)
884 ggc_add_root (base
, nelt
, sizeof(tree
), ggc_mark_tree_ptr
);
888 ggc_add_string_root (base
, nelt
)
892 ggc_add_root (base
, nelt
, sizeof(char *), ggc_mark_string_ptr
);
895 /* Add V (a varray full of trees) to the list of GC roots. */
898 ggc_add_tree_varray_root (base
, nelt
)
902 ggc_add_root (base
, nelt
, sizeof (varray_type
),
903 ggc_mark_tree_varray_ptr
);
906 /* Add HT (a hash-table where ever key is a tree) to the list of GC
910 ggc_add_tree_hash_table_root (base
, nelt
)
911 struct hash_table
**base
;
914 ggc_add_root (base
, nelt
, sizeof (struct hash_table
*),
915 ggc_mark_tree_hash_table_ptr
);
922 struct ggc_root
*x
, **p
;
924 p
= &roots
, x
= roots
;
941 ggc_mark_rtx_ptr (elt
)
944 ggc_mark_rtx (*(rtx
*)elt
);
948 ggc_mark_tree_ptr (elt
)
951 ggc_mark_tree (*(tree
*)elt
);
954 /* Type-correct function to pass to ggc_add_root. It just forwards
955 ELT (which is really a char **) to ggc_mark_string. */
958 ggc_mark_string_ptr (elt
)
961 ggc_mark_string (*(char **)elt
);
964 /* Type-correct function to pass to ggc_add_root. It just forwards
965 ELT (which is really a varray_type *) to ggc_mark_tree_varray. */
968 ggc_mark_tree_varray_ptr (elt
)
971 ggc_mark_tree_varray (*(varray_type
*)elt
);
974 /* Type-correct function to pass to ggc_add_root. It just forwards
975 ELT (which is really a struct hash_table **) to
976 ggc_mark_tree_hash_table. */
979 ggc_mark_tree_hash_table_ptr (elt
)
982 ggc_mark_tree_hash_table (*(struct hash_table
**) elt
);
986 /* GDB really should have a memory search function. Since this is just
987 for initial debugging, I won't even pretend to get the __data_start
988 to work on any but alpha-dec-linux-gnu. */
990 search_data(void **start
, void *target
)
992 extern void *__data_start
[];
993 void **_end
= (void **)sbrk(0);
996 start
= __data_start
;
999 if (*start
== target
)