/* Simple garbage collection for the GNU compiler.
- Copyright (C) 1998, 1999 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
/* Local function prototypes. */
-static void tree_insert PROTO ((struct ggc_mem *));
-static int tree_lookup PROTO ((struct ggc_mem *));
-static void clear_marks PROTO ((struct ggc_mem *));
-static void sweep_objs PROTO ((struct ggc_mem **));
-static void ggc_pop_context_1 PROTO ((struct ggc_mem *, int));
+static void tree_insert PARAMS ((struct ggc_mem *));
+static int tree_lookup PARAMS ((struct ggc_mem *));
+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 PROTO ((void));
-static void tally_leaves PROTO ((struct ggc_mem *, int, size_t *, size_t *));
+extern void debug_ggc_balance PARAMS ((void));
#endif
+static void tally_leaves PARAMS ((struct ggc_mem *, int, size_t *, size_t *));
/* Insert V into the search tree. */
/* 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;
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);
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 ();
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;
}
void
ggc_collect ()
{
- int time;
-
#ifndef GGC_ALWAYS_COLLECT
if (G.allocated < GGC_MIN_EXPAND_FOR_GC * G.allocated_last_gc)
return;
debug_ggc_balance ();
#endif
- time = get_run_time ();
+ timevar_push (TV_GC);
if (!quiet_flag)
fprintf (stderr, " {GC %luk -> ", (unsigned long)G.allocated / 1024);
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 ();
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
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);
(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;
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));
+}