OSDN Git Service

* config/xtensa/xtensa.c (xtensa_va_arg): Fix to handle arguments
[pf3gnuchains/gcc-fork.git] / gcc / ggc-simple.c
index 6608d8c..81d2c36 100644 (file)
@@ -1,22 +1,22 @@
 /* Simple garbage collection for the GNU compiler.
-   Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
+   Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
 
-   This file is part of GNU CC.
+   This file is part of GCC.
 
-   GNU CC is free software; you can redistribute it and/or modify
-   it under the terms of the GNU General Public License as published by
+   GCC is free software; you can redistribute it and/or modify it
+   under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2, or (at your option)
    any later version.
 
-   GNU CC is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-   GNU General Public License for more details.
+   GCC is distributed in the hope that it will be useful, but WITHOUT
+   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
+   License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GNU CC; see the file COPYING.  If not, write to
-   the Free Software Foundation, 59 Temple Place - Suite 330,
-   Boston, MA 02111-1307, USA.  */
+   along with GCC; see the file COPYING.  If not, write to the Free
+   Software Foundation, 59 Temple Place - Suite 330, Boston, MA
+   02111-1307, USA.  */
 
 #include "config.h"
 #include "system.h"
 #include "flags.h"
 #include "varray.h"
 #include "ggc.h"
-
-#ifndef offsetof
-#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
-#endif
+#include "timevar.h"
 
 /* Debugging flags.  */
 
 #define GGC_ALWAYS_COLLECT
 #endif
 
-/* Constants for general use.  */
-
-char *empty_string;
-extern int gc_time;
-
 #ifndef HOST_BITS_PER_PTR
 #define HOST_BITS_PER_PTR  HOST_BITS_PER_LONG
 #endif
@@ -141,10 +133,13 @@ static void clear_marks PARAMS ((struct ggc_mem *));
 static void sweep_objs PARAMS ((struct ggc_mem **));
 static void ggc_pop_context_1 PARAMS ((struct ggc_mem *, int));
 
+/* For use from debugger.  */
+extern void debug_ggc_tree PARAMS ((struct ggc_mem *, int));
+
 #ifdef GGC_BALANCE
 extern void debug_ggc_balance PARAMS ((void));
-static void tally_leaves PARAMS ((struct ggc_mem *, int, size_t *, size_t *));
 #endif
+static void tally_leaves PARAMS ((struct ggc_mem *, int, size_t *, size_t *));
 
 /* Insert V into the search tree.  */
 
@@ -186,9 +181,8 @@ tree_lookup (v)
 /* Alloc SIZE bytes of GC'able memory.  If ZERO, clear the memory.  */
 
 void *
-ggc_alloc_obj (size, zero)
+ggc_alloc (size)
      size_t size;
-     int zero;
 {
   struct ggc_mem *x;
 
@@ -199,11 +193,8 @@ ggc_alloc_obj (size, zero)
   x->context = G.context;
   x->size = size;
 
-  if (zero)
-    memset (&x->u, 0, size);
 #ifdef GGC_POISON
-  else
-    memset (&x->u, 0xaf, size);
+  memset (&x->u, 0xaf, size);
 #endif
 
   tree_insert (x);
@@ -217,11 +208,11 @@ ggc_alloc_obj (size, zero)
 
 int
 ggc_set_mark (p)
-     void *p;
+     const void *p;
 {
   struct ggc_mem *x;
 
-  x = (struct ggc_mem *) ((char *)p - offsetof (struct ggc_mem, u));
+  x = (struct ggc_mem *) ((const char *)p - offsetof (struct ggc_mem, u));
 #ifdef GGC_ALWAYS_VERIFY
   if (! tree_lookup (x))
     abort ();
@@ -237,37 +228,31 @@ ggc_set_mark (p)
   return 0;
 }
 
-/* Mark a node, but check first to see that it's really gc-able memory.  */
+/* Return 1 if P has been marked, zero otherwise.  */
 
-void
-ggc_mark_if_gcable (p)
-     void *p;
+int
+ggc_marked_p (p)
+     const void *p;
 {
   struct ggc_mem *x;
 
-  if (p == NULL)
-    return;
-
-  x = (struct ggc_mem *) ((char *)p - offsetof (struct ggc_mem, u));
+  x = (struct ggc_mem *) ((const char *)p - offsetof (struct ggc_mem, u));
+#ifdef GGC_ALWAYS_VERIFY
   if (! tree_lookup (x))
-    return;
-
-  if (x->mark)
-    return;
+    abort ();
+#endif
 
-  x->mark = 1;
-  G.allocated += x->size;
-  G.objects += 1;
+   return x->mark;
 }
 
 /* Return the size of the gc-able object P.  */
 
 size_t
 ggc_get_size (p)
-     void *p;
+     const void *p;
 {
   struct ggc_mem *x 
-    = (struct ggc_mem *) ((char *)p - offsetof (struct ggc_mem, u));
+    = (struct ggc_mem *) ((const char *)p - offsetof (struct ggc_mem, u));
   return x->size;
 }
 
@@ -339,8 +324,6 @@ sweep_objs (root)
 void
 ggc_collect ()
 {
-  int time;
-
 #ifndef GGC_ALWAYS_COLLECT
   if (G.allocated < GGC_MIN_EXPAND_FOR_GC * G.allocated_last_gc)
     return;
@@ -350,7 +333,7 @@ ggc_collect ()
   debug_ggc_balance ();
 #endif
 
-  time = get_run_time ();
+  timevar_push (TV_GC);
   if (!quiet_flag)
     fprintf (stderr, " {GC %luk -> ", (unsigned long)G.allocated / 1024);
 
@@ -365,14 +348,10 @@ ggc_collect ()
   if (G.allocated_last_gc < GGC_MIN_LAST_ALLOCATED)
     G.allocated_last_gc = GGC_MIN_LAST_ALLOCATED;
 
-  time = get_run_time () - time;
-  gc_time += time;
+  timevar_pop (TV_GC);
 
   if (!quiet_flag)
-    {
-      fprintf (stderr, "%luk in %.3f}", 
-              (unsigned long) G.allocated / 1024, time * 1e-6);
-    }
+    fprintf (stderr, "%luk}", (unsigned long) G.allocated / 1024);
 
 #ifdef GGC_BALANCE
   debug_ggc_balance ();
@@ -385,9 +364,6 @@ void
 init_ggc ()
 {
   G.allocated_last_gc = GGC_MIN_LAST_ALLOCATED;
-
-  empty_string = ggc_alloc_string ("", 0);
-  ggc_add_string_root (&empty_string, 1);
 }
 
 /* Start a new GGC context.  Memory allocated in previous contexts
@@ -448,7 +424,7 @@ debug_ggc_tree (p, indent)
 
   for (i = 0; i < indent; ++i)
     putc (' ', stderr);
-  fprintf (stderr, "%lx %p\n", PTR_KEY (p), p);
+  fprintf (stderr, "%lx %p\n", (unsigned long)PTR_KEY (p), p);
  
   if (p->sub[1])
     debug_ggc_tree (p->sub[1], indent + 1);
@@ -474,7 +450,9 @@ debug_ggc_balance ()
           (float)sumdepth / (float)nleaf,
           log ((double) G.objects) / M_LN2);
 }
+#endif
 
+/* Used by debug_ggc_balance, and also by ggc_print_statistics.  */
 static void
 tally_leaves (x, depth, nleaf, sumdepth)
      struct ggc_mem *x;
@@ -495,4 +473,45 @@ tally_leaves (x, depth, nleaf, sumdepth)
        tally_leaves (x->sub[1], depth + 1, nleaf, sumdepth);
     }
 }
-#endif
+
+#define SCALE(x) ((unsigned long) ((x) < 1024*10 \
+                 ? (x) \
+                 : ((x) < 1024*1024*10 \
+                    ? (x) / 1024 \
+                    : (x) / (1024*1024))))
+#define LABEL(x) ((x) < 1024*10 ? ' ' : ((x) < 1024*1024*10 ? 'k' : 'M'))
+
+/* Report on GC memory usage.  */
+void
+ggc_print_statistics ()
+{
+  struct ggc_statistics stats;
+  size_t nleaf = 0, sumdepth = 0;
+
+  /* Clear the statistics.  */
+  memset (&stats, 0, sizeof (stats));
+  
+  /* Make sure collection will really occur.  */
+  G.allocated_last_gc = 0;
+
+  /* Collect and print the statistics common across collectors.  */
+  ggc_print_common_statistics (stderr, &stats);
+
+  /* Report on tree balancing.  */
+  tally_leaves (G.root, 0, &nleaf, &sumdepth);
+
+  fprintf (stderr, "\n\
+Total internal data (bytes)\t%ld%c\n\
+Number of leaves in tree\t%d\n\
+Average leaf depth\t\t%.1f\n",
+          SCALE(G.objects * offsetof (struct ggc_mem, u)),
+          LABEL(G.objects * offsetof (struct ggc_mem, u)),
+          nleaf, (double)sumdepth / (double)nleaf);
+
+  /* Report overall memory usage.  */
+  fprintf (stderr, "\n\
+Total objects allocated\t\t%d\n\
+Total memory in GC arena\t%ld%c\n",
+          G.objects,
+          SCALE(G.allocated), LABEL(G.allocated));
+}