OSDN Git Service

* ggc-simple.c (IS_MARKED, IGNORE_MARK): New.
[pf3gnuchains/gcc-fork.git] / gcc / ggc-simple.c
1 /* Simple garbage collection for the GNU compiler.
2    Copyright (C) 1998 Free Software Foundation, Inc.
3
4    This file is part of GNU CC.
5
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)
9    any later version.
10
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.
15
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.  */
20
21 #include "config.h"
22 #include "system.h"
23 #include "rtl.h"
24 #include "tree.h"
25 #include "ggc.h"
26 #include "flags.h"
27 #include "varray.h"
28 #include "hash.h"
29
30 /* Debugging flags.  */
31
32 /* Zap memory before freeing to catch dangling pointers.  */
33 #define GGC_POISON
34
35 /* Log alloc and release.  Don't enable this unless you want a
36    really really lot of data.  */
37 #undef GGC_DUMP
38
39 /* Some magic tags for strings and anonymous memory, hoping to catch
40    certain errors wrt marking memory.  */
41
42 #define IS_MARKED(X)            ((X) & 1)
43 #define IGNORE_MARK(X)          ((X) & -2)
44
45 #define GGC_STRING_MAGIC        ((unsigned int)0xa1b2c3d4)
46 #define GGC_STRING_MAGIC_MARK   ((unsigned int)0xa1b2c3d4 | 1)
47
48 #define GGC_ANY_MAGIC           ((unsigned int)0xa9bacbdc)
49 #define GGC_ANY_MAGIC_MARK      ((unsigned int)0xa9bacbdc | 1)
50
51 /* Global lists of roots, rtxs, and trees.  */
52
53 struct ggc_root
54 {
55   struct ggc_root *next;
56   void *base;
57   int nelt;
58   int size;
59   void (*cb) PROTO ((void *));
60 };
61
62 static struct ggc_root *roots;
63
64 struct ggc_rtx
65 {
66   struct ggc_rtx *chain;
67   struct rtx_def rtx;
68 };
69
70 struct ggc_rtvec
71 {
72   struct ggc_rtvec *chain;
73   struct rtvec_def vec;
74 };
75
76 struct ggc_tree
77 {
78   struct ggc_tree *chain;
79   union tree_node tree;
80 };
81
82 struct ggc_string
83 {
84   struct ggc_string *chain;
85   unsigned int magic_mark;
86   char string[1];
87 };
88
89 /* A generic allocation, with an external mark bit.  */
90
91 struct ggc_any
92 {
93   struct ggc_any *chain;
94   unsigned int magic_mark;
95
96   /* Make sure the data is reasonably aligned.  */
97   union {
98     char c;
99     HOST_WIDE_INT i;
100     long double d;
101   } u;
102 };
103
104 struct ggc_status
105 {
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;
113 };
114
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;
118
119 /* Some statistics.  */
120
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;
126 extern int gc_time;
127
128 #ifdef GGC_DUMP
129 static FILE *dump;
130 #endif
131
132 /* Local function prototypes.  */
133
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));
139
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 *,
146                                                       hash_table_key));
147
148 /* Called once to initialize the garbage collector.  */
149
150 void 
151 init_ggc PROTO ((void))
152 {
153   /* Initialize the global context.  */
154   ggc_push_context ();
155
156 #ifdef GGC_DUMP
157   dump = fopen ("zgcdump", "w");
158   setlinebuf (dump);
159 #endif
160 }
161
162 /* Start a new GGC context.  Memory allocated in previous contexts
163    will not be collected while the new context is active.  */
164
165 void
166 ggc_push_context PROTO ((void))
167 {
168   struct ggc_status *gs = (struct ggc_status *) xcalloc (1, sizeof (*gs));
169   gs->next = ggc_chain;
170   ggc_chain = gs;
171 }
172
173 /* Finish a GC context.  Any uncollected memory in the new context
174    will be merged with the old context.  */
175
176 void 
177 ggc_pop_context PROTO ((void))
178 {
179   struct ggc_rtx *r;
180   struct ggc_rtvec *v;
181   struct ggc_tree *t;
182   struct ggc_string *s;
183   struct ggc_status *gs;
184
185   gs = ggc_chain;
186
187   r = gs->rtxs;
188   if (r)
189     {
190       while (r->chain)
191         r = r->chain;
192       r->chain = gs->next->rtxs;
193       gs->next->rtxs = gs->rtxs;
194     }
195       
196   v = gs->vecs;
197   if (v)
198     {
199       while (v->chain)
200         v = v->chain;
201       v->chain = gs->next->vecs;
202       gs->next->vecs = gs->vecs;
203     }
204
205   t = gs->trees;
206   if (t)
207     {
208       while (t->chain)
209         t = t->chain;
210       t->chain = gs->next->trees;
211       gs->next->trees = gs->trees;
212     }
213
214   s = gs->strings;
215   if (s)
216     {
217       while (s->chain)
218         s = s->chain;
219       s->chain = gs->next->strings;
220       gs->next->strings = gs->strings;
221     }
222
223   ggc_chain = gs->next;
224   free (gs);
225 }
226
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.  */
230
231 rtx
232 ggc_alloc_rtx (nslots)
233      int nslots;
234 {
235   struct ggc_rtx *n;
236   int size = sizeof(*n) + (nslots-1) * sizeof(rtunion);
237
238   n = (struct ggc_rtx *) xcalloc (1, size);
239   n->chain = ggc_chain->rtxs;
240   ggc_chain->rtxs = n;
241
242 #ifdef GGC_DUMP
243   fprintf (dump, "alloc rtx %p\n", &n->rtx);
244 #endif
245
246   ggc_chain->bytes_alloced_since_gc += size;
247
248   return &n->rtx;
249 }
250
251 rtvec
252 ggc_alloc_rtvec (nelt)
253      int nelt;
254 {
255   struct ggc_rtvec *v;
256   int size = sizeof (*v) + (nelt - 1) * sizeof (rtx);
257
258   v = (struct ggc_rtvec *) xcalloc (1, size);
259   v->chain = ggc_chain->vecs;
260   ggc_chain->vecs = v;
261
262 #ifdef GGC_DUMP
263   fprintf(dump, "alloc vec %p\n", &v->vec);
264 #endif
265
266   ggc_chain->bytes_alloced_since_gc += size;
267
268   return &v->vec;
269 }
270
271 tree
272 ggc_alloc_tree (length)
273      int length;
274 {
275   struct ggc_tree *n;
276   int size = sizeof(*n) - sizeof(n->tree) + length;
277
278   n = (struct ggc_tree *) xcalloc (1, size);
279   n->chain = ggc_chain->trees;
280   ggc_chain->trees = n;
281
282 #ifdef GGC_DUMP
283   fprintf(dump, "alloc tree %p\n", &n->tree);
284 #endif
285
286   ggc_chain->bytes_alloced_since_gc += size;
287
288   return &n->tree;
289 }
290
291 char *
292 ggc_alloc_string (contents, length)
293      const char *contents;
294      int length;
295 {
296   struct ggc_string *s;
297   int size;
298
299   if (length < 0)
300     {
301       if (contents == NULL)
302         return NULL;
303       length = strlen (contents);
304     }
305
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;
311
312   if (contents)
313     memcpy (s->string, contents, length);
314   s->string[length] = 0;
315
316 #ifdef GGC_DUMP
317   fprintf(dump, "alloc string %p\n", &s->string);
318 #endif
319
320   ggc_chain->bytes_alloced_since_gc += size;
321
322   return s->string;
323 }
324
325 /* Like xmalloc, but allocates GC-able memory.  */
326
327 void *
328 ggc_alloc (bytes)
329      size_t bytes;
330 {
331   struct ggc_any *a;
332
333   if (bytes == 0)
334     bytes = 1;
335   bytes += (&((struct ggc_any *) 0)->u.c - (char *) 0);
336
337   a = (struct ggc_any *) xmalloc (bytes);
338   a->chain = ggc_chain->anys;
339   a->magic_mark = GGC_ANY_MAGIC;
340   ggc_chain->anys = a;
341
342   ggc_chain->bytes_alloced_since_gc += bytes;
343
344   return &a->u;
345 }
346
347 /* Freeing a bit of rtl is as simple as calling free.  */
348
349 static inline void 
350 ggc_free_rtx (r)
351      struct ggc_rtx *r;
352 {
353 #ifdef GGC_DUMP
354   fprintf (dump, "collect rtx %p\n", &r->rtx);
355 #endif
356 #ifdef GGC_POISON
357   memset (r, 0xAA, sizeof(*r) + ((GET_RTX_LENGTH (r->rtx.code) -1)
358                                  * sizeof(rtunion)));
359 #endif
360
361   free (r);
362 }
363
364 /* Freeing an rtvec is as simple as calling free.  */
365
366 static inline void
367 ggc_free_rtvec (v)
368      struct ggc_rtvec *v;
369 {
370 #ifdef GGC_DUMP
371   fprintf(dump, "collect vec %p\n", &v->vec);
372 #endif
373 #ifdef GGC_POISON
374   memset (v, 0xBB, sizeof (*v) + ((GET_NUM_ELEM (&v->vec) - 1)
375                                   * sizeof (rtunion)));
376 #endif
377
378   free (v);
379 }
380
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.  */
383
384 static inline void
385 ggc_free_tree (t)
386      struct ggc_tree *t;
387 {
388   switch (TREE_CODE_CLASS (TREE_CODE (&t->tree)))
389     {
390     case 'd': /* A decl node.  */
391     case 't': /* A type node.  */
392       lang_cleanup_tree (&t->tree);
393       break;
394     }
395
396 #ifdef GGC_DUMP
397   fprintf (dump, "collect tree %p\n", &t->tree);
398 #endif
399 #ifdef GGC_POISON
400   memset(&t->tree.common, 0xCC, sizeof(t->tree.common));
401 #endif
402
403   free (t);
404 }
405
406 /* Freeing a string is as simple as calling free.  */
407
408 static inline void
409 ggc_free_string (s)
410      struct ggc_string *s;
411 {
412 #ifdef GGC_DUMP
413   fprintf(dump, "collect string %p\n", s->string);
414 #endif
415 #ifdef GGC_POISON
416   s->magic_mark = 0xDDDDDDDD;
417   s->string[0] = 0xDD;
418 #endif
419
420   free (s);
421 }
422
423 /* Freeing anonymous memory is as simple as calling free.  */
424
425 static inline void
426 ggc_free_any (a)
427      struct ggc_any *a;
428 {
429 #ifdef GGC_DUMP
430   fprintf(dump, "collect mem %p\n", &a->u);
431 #endif
432 #ifdef GGC_POISON
433   a->magic_mark = 0xEEEEEEEE;
434 #endif
435
436   free (a);
437 }
438
439 /* Mark a node.  */
440
441 void
442 ggc_mark_rtx (r)
443      rtx r;
444 {
445   const char *fmt;
446   int i;
447
448   if (r == NULL_RTX || r->gc_mark)
449     return;
450   r->gc_mark = 1;
451
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))
455     {
456     case JUMP_INSN:
457       ggc_mark_rtx (JUMP_LABEL (r));
458       break;
459     case CODE_LABEL:
460       ggc_mark_rtx (LABEL_REFS (r));
461       break;
462     case LABEL_REF:
463       ggc_mark_rtx (LABEL_NEXTREF (r));
464       ggc_mark_rtx (CONTAINING_INSN (r));
465       break;
466     case ADDRESSOF:
467       ggc_mark_tree (ADDRESSOF_DECL (r));
468       break;
469     case CONST_DOUBLE:
470       ggc_mark_rtx (CONST_DOUBLE_CHAIN (r));
471       break;
472     case NOTE:
473       switch (NOTE_LINE_NUMBER (r))
474         {
475         case NOTE_INSN_RANGE_START:
476         case NOTE_INSN_RANGE_END:
477         case NOTE_INSN_LIVE:
478           ggc_mark_rtx (NOTE_RANGE_INFO (r));
479           break;
480
481         default:
482           if (NOTE_LINE_NUMBER (r) >= 0)
483             ggc_mark_string (NOTE_SOURCE_FILE (r));
484           break;
485         }
486       break;
487
488     default:
489       break;
490     }
491
492   for (fmt = GET_RTX_FORMAT (GET_CODE (r)), i = 0; *fmt ; ++fmt, ++i)
493     {
494       switch (*fmt)
495         {
496         case 'e': case 'u':
497           ggc_mark_rtx (XEXP (r, i));
498           break;
499         case 'V': case 'E':
500           ggc_mark_rtvec (XVEC (r, i));
501           break;
502         case 'S': case 's':
503           ggc_mark_string (XSTR (r, i));
504           break;
505         }
506     }
507 }
508
509 void
510 ggc_mark_rtvec (v)
511      rtvec v;
512 {
513   int i;
514
515   if (v == NULL || v->gc_mark)
516     return;
517   v->gc_mark = 1;
518
519   i = GET_NUM_ELEM (v);
520   while (--i >= 0)
521     ggc_mark_rtx (RTVEC_ELT (v, i));
522 }
523
524 void
525 ggc_mark_tree (t)
526      tree t;
527 {
528   if (t == NULL_TREE || t->common.gc_mark)
529     return;
530   t->common.gc_mark = 1;
531
532   /* Bits from common.  */
533   ggc_mark_tree (TREE_TYPE (t));
534   ggc_mark_tree (TREE_CHAIN (t));
535
536   /* Some nodes require special handling.  */
537   switch (TREE_CODE (t))
538     {
539     case TREE_LIST:
540       ggc_mark_tree (TREE_PURPOSE (t));
541       ggc_mark_tree (TREE_VALUE (t));
542       return;
543
544     case TREE_VEC:
545       {
546         int i = TREE_VEC_LENGTH (t);
547         while (--i >= 0)
548           ggc_mark_tree (TREE_VEC_ELT (t, i));
549         return;
550       }
551
552     case SAVE_EXPR:
553       ggc_mark_tree (TREE_OPERAND (t, 0));
554       ggc_mark_tree (SAVE_EXPR_CONTEXT (t));
555       ggc_mark_rtx (SAVE_EXPR_RTL (t));
556       return;
557
558     case RTL_EXPR:
559       ggc_mark_rtx (RTL_EXPR_SEQUENCE (t));
560       ggc_mark_rtx (RTL_EXPR_RTL (t));
561       return;
562
563     case CALL_EXPR:
564       ggc_mark_tree (TREE_OPERAND (t, 0));
565       ggc_mark_tree (TREE_OPERAND (t, 1));
566       ggc_mark_rtx (CALL_EXPR_RTL (t));
567       return;
568
569     case COMPLEX_CST:
570       ggc_mark_tree (TREE_REALPART (t));
571       ggc_mark_tree (TREE_IMAGPART (t));
572       break;
573
574     case STRING_CST:
575       ggc_mark_string (TREE_STRING_POINTER (t));
576       break;
577
578     case PARM_DECL:
579       ggc_mark_rtx (DECL_INCOMING_RTL (t));
580       break;
581
582     case IDENTIFIER_NODE:
583       ggc_mark_string (IDENTIFIER_POINTER (t));
584       lang_mark_tree (t);
585       return;
586
587     default:
588       break;
589     }
590   
591   /* But in general we can handle them by class.  */
592   switch (TREE_CODE_CLASS (TREE_CODE (t)))
593     {
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));
607       lang_mark_tree (t);
608       break;
609
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));
625       lang_mark_tree (t);
626       break;
627
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));
635       break;
636
637     case 'c': /* A constant.  */
638       ggc_mark_rtx (TREE_CST_RTL (t));
639       break;
640
641     case 'r': case '<': case '1':
642     case '2': case 'e': case 's': /* Expressions.  */
643       {
644         int i = tree_code_length[TREE_CODE (t)];
645         while (--i >= 0)
646           ggc_mark_tree (TREE_OPERAND (t, i));
647         break;
648       }
649
650     case 'x':
651       lang_mark_tree (t);
652       break;
653     }
654 }
655
656 /* Mark all the elements of the varray V, which contains trees.  */
657
658 void
659 ggc_mark_tree_varray (v)
660      varray_type v;
661 {
662   int i;
663
664   if (v)
665     for (i = v->num_elements - 1; i >= 0; --i) 
666       ggc_mark_tree (VARRAY_TREE (v, i));
667 }
668
669 /* Mark the hash table-entry HE.  It's key field is really a tree.  */
670
671 static boolean
672 ggc_mark_tree_hash_table_entry (he, k)
673      struct hash_entry *he;
674      hash_table_key k ATTRIBUTE_UNUSED;
675 {
676   ggc_mark_tree ((tree) he->key);
677   return true;
678 }
679
680 /* Mark all the elements of the hash-table H, which contains trees.  */
681
682 void
683 ggc_mark_tree_hash_table (ht)
684      struct hash_table *ht;
685 {
686   hash_traverse (ht, ggc_mark_tree_hash_table_entry, /*info=*/0);
687 }
688
689 void
690 ggc_mark_string (s)
691      char *s;
692 {
693   const ptrdiff_t d = (((struct ggc_string *) 0)->string - (char *) 0);
694   struct ggc_string *gs;
695
696   if (s == NULL)
697     return;
698
699   gs = (struct ggc_string *)(s - d);
700   if (IGNORE_MARK (gs->magic_mark) != GGC_STRING_MAGIC)
701     return;   /* abort? */
702   gs->magic_mark = GGC_STRING_MAGIC_MARK;
703 }
704
705 /* Mark P, allocated with ggc_alloc.  */
706
707 void
708 ggc_mark (p)
709      void *p;
710 {
711   const ptrdiff_t d = (&((struct ggc_any *) 0)->u.c - (char *) 0);
712   struct ggc_any *a;
713
714   if (p == NULL)
715     return;
716
717   a = (struct ggc_any *) (((char*) p) - d);
718   if (IGNORE_MARK (a->magic_mark) != GGC_ANY_MAGIC)
719     abort ();
720   a->magic_mark = GGC_ANY_MAGIC_MARK;
721 }
722
723 /* The top level mark-and-sweep routine.  */
724
725 void
726 ggc_collect ()
727 {
728   struct ggc_rtx *r, **rp;
729   struct ggc_rtvec *v, **vp;
730   struct ggc_tree *t, **tp;
731   struct ggc_string *s, **sp;
732   struct ggc_root *x;
733   struct ggc_status *gs;
734   struct ggc_any *a, **ap;
735   int time, n_rtxs, n_trees, n_vecs, n_strings, n_anys;
736
737 #ifndef ENABLE_CHECKING
738   /* See if it's even worth our while.  */
739   if (ggc_chain->bytes_alloced_since_gc < 64*1024)
740     return;
741 #endif
742
743   if (!quiet_flag)
744     fputs (" {GC ", stderr);
745
746   time = get_run_time ();
747
748   /* Clean out all of the GC marks.  */
749   for (gs = ggc_chain; gs; gs = gs->next)
750     {
751       for (r = gs->rtxs; r != NULL; r = r->chain)
752         r->rtx.gc_mark = 0;
753       for (v = gs->vecs; v != NULL; v = v->chain)
754         v->vec.gc_mark = 0;
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;
761     }
762
763   /* Mark through all the roots.  */
764   for (x = roots; x != NULL; x = x->next)
765     {
766       char *elt = x->base;
767       int s = x->size, n = x->nelt;
768       void (*cb) PROTO ((void *)) = x->cb;
769       int i;
770
771       for (i = 0; i < n; ++i, elt += s)
772         (*cb)(elt);
773     }
774
775   /* Sweep the resulting dead nodes.  */
776
777   /* The RTXs.  */
778
779   rp = &ggc_chain->rtxs;
780   r = ggc_chain->rtxs;
781   n_rtxs = 0;
782   while (r != NULL)
783     {
784       struct ggc_rtx *chain = r->chain;
785       if (!r->rtx.gc_mark)
786         {
787           ggc_free_rtx (r);
788           *rp = chain;
789           n_rtxs++;
790         }
791       else
792         rp = &r->chain;
793       r = chain;
794     }
795   *rp = NULL;
796   n_rtxs_collected += n_rtxs;
797
798   /* The vectors.  */
799
800   vp = &ggc_chain->vecs;
801   v = ggc_chain->vecs;
802   n_vecs = 0;
803   while (v != NULL)
804     {
805       struct ggc_rtvec *chain = v->chain;
806       if (!v->vec.gc_mark)
807         {
808           ggc_free_rtvec (v);
809           *vp = chain;
810           n_vecs++;
811         }
812       else
813         vp = &v->chain;
814       v = chain;
815     }
816   *vp = NULL;
817   n_vecs_collected += n_vecs;
818
819   /* The trees.  */
820
821   tp = &ggc_chain->trees;
822   t = ggc_chain->trees;
823   n_trees = 0;
824   while (t != NULL)
825     {
826       struct ggc_tree *chain = t->chain;
827       if (!t->tree.common.gc_mark)
828         {
829           ggc_free_tree (t);
830           *tp = chain;
831           n_trees++;
832         }
833       else
834         tp = &t->chain;
835       t = chain;
836     }
837   *tp = NULL;
838   n_trees_collected += n_trees;
839
840   /* The strings.  */
841
842   sp = &ggc_chain->strings;
843   s = ggc_chain->strings;
844   n_strings = 0;
845   while (s != NULL)
846     {
847       struct ggc_string *chain = s->chain;
848       if (! IS_MARKED (s->magic_mark))
849         {
850           ggc_free_string (s);
851           *sp = chain;
852           n_strings++;
853         }
854       else
855         sp = &s->chain;
856       s = chain;
857     }
858   *sp = NULL;
859   n_strings_collected += n_strings;
860
861   /* The generic data.  */
862
863   ap = &ggc_chain->anys;
864   a = ggc_chain->anys;
865   n_anys = 0;
866   while (a != NULL)
867     {
868       struct ggc_any *chain = a->chain;
869       if (! IS_MARKED (a->magic_mark))
870         {
871           ggc_free_any (a);
872           *ap = chain;
873           n_anys++;
874         }
875       else
876         ap = &a->chain;
877       a = chain;
878     }
879   n_anys_collected += n_anys;
880
881   ggc_chain->bytes_alloced_since_gc = 0;
882
883   time = get_run_time () - time;
884   gc_time += time;
885
886   if (!quiet_flag)
887     {
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);
891     }
892 }
893
894 /* Manipulate global roots that are needed between calls to gc.  */
895
896 void
897 ggc_add_root (base, nelt, size, cb)
898      void *base;
899      int nelt, size;
900      void (*cb) PROTO ((void *));
901 {
902   struct ggc_root *x = (struct ggc_root *) xmalloc (sizeof(*x));
903
904   x->next = roots;
905   x->base = base;
906   x->nelt = nelt;
907   x->size = size;
908   x->cb = cb;
909
910   roots = x;
911 }
912
913 void
914 ggc_add_rtx_root (base, nelt)
915      rtx *base;
916      int nelt;
917 {
918   ggc_add_root (base, nelt, sizeof(rtx), ggc_mark_rtx_ptr);
919 }
920
921 void
922 ggc_add_tree_root (base, nelt)
923      tree *base;
924      int nelt;
925 {
926   ggc_add_root (base, nelt, sizeof(tree), ggc_mark_tree_ptr);
927 }
928
929 void
930 ggc_add_string_root (base, nelt)
931      char **base;
932      int nelt;
933 {
934   ggc_add_root (base, nelt, sizeof(char *), ggc_mark_string_ptr);
935 }
936
937 /* Add V (a varray full of trees) to the list of GC roots.  */
938
939 void
940 ggc_add_tree_varray_root (base, nelt)
941      varray_type *base;
942      int nelt;
943 {
944   ggc_add_root (base, nelt, sizeof (varray_type), 
945                 ggc_mark_tree_varray_ptr);
946 }
947
948 /* Add HT (a hash-table where ever key is a tree) to the list of GC
949    roots.  */
950
951 void
952 ggc_add_tree_hash_table_root (base, nelt)
953      struct hash_table **base;
954      int nelt;
955 {
956   ggc_add_root (base, nelt, sizeof (struct hash_table *), 
957                 ggc_mark_tree_hash_table_ptr);
958 }
959
960 void
961 ggc_del_root (base)
962      void *base;
963 {
964   struct ggc_root *x, **p;
965
966   p = &roots, x = roots;
967   while (x)
968     {
969       if (x->base == base)
970         {
971           *p = x->next;
972           free (x);
973           return;
974         }
975       p = &x->next;
976       x = x->next;
977     }
978
979   abort();
980 }
981
982 static void
983 ggc_mark_rtx_ptr (elt)
984      void *elt;
985 {
986   ggc_mark_rtx (*(rtx *)elt);
987 }
988
989 static void
990 ggc_mark_tree_ptr (elt)
991      void *elt;
992 {
993   ggc_mark_tree (*(tree *)elt);
994 }
995
996 /* Type-correct function to pass to ggc_add_root.  It just forwards
997    ELT (which is really a char **) to ggc_mark_string.  */
998
999 static void
1000 ggc_mark_string_ptr (elt)
1001      void *elt;
1002 {
1003   ggc_mark_string (*(char **)elt);
1004 }
1005
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.  */
1008
1009 static void
1010 ggc_mark_tree_varray_ptr (elt)
1011      void *elt;
1012 {
1013   ggc_mark_tree_varray (*(varray_type *)elt);
1014 }
1015
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.  */
1019
1020 static void
1021 ggc_mark_tree_hash_table_ptr (elt)
1022      void *elt;
1023 {
1024   ggc_mark_tree_hash_table (*(struct hash_table **) elt);
1025 }
1026
1027 #if 0
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.  */
1031 static void **
1032 search_data(void **start, void *target)
1033 {
1034   extern void *__data_start[];
1035   void **_end = (void **)sbrk(0);
1036
1037   if (start == NULL)
1038     start = __data_start;
1039   while (start < _end)
1040     {
1041       if (*start == target)
1042         return start;
1043       start++;
1044     }
1045   return NULL;
1046 }
1047 #endif