ggc.h 6.74 KB
Newer Older
1
/* Garbage collection for the GNU compiler.
2
   Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
3

4
This file is part of GCC.
5

6 7 8 9
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.
10

11 12
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 14 15 16
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
17
along with GCC; see the file COPYING.  If not, write to the Free
18 19
Software Foundation, 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.  */
20

21
#include "varray.h"
22
#include "gtype-desc.h"
23 24 25 26

/* Symbols are marked with `ggc' for `gcc gc' so as not to interfere with
   an external gc library that might be linked in.  */

27
/* Constants for general use.  */
28 29 30
extern const char empty_string[];	/* empty string */
extern const char digit_vector[];	/* "0" .. "9" */
#define digit_string(d) (digit_vector + ((d) * 2))
31

32
/* Manipulate global roots that are needed between calls to gc.  */
33 34
extern void ggc_add_root		PARAMS ((void *base, int nelt,
						 int size, void (*)(void *)));
35

36
/* Structures for the easy way to mark roots.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
   In an array, terminated by having base == NULL.*/
struct ggc_root_tab {
  void *base;
  size_t nelt;
  size_t stride;
  void (*cb) PARAMS ((void *));
};
#define LAST_GGC_ROOT_TAB { NULL, 0, 0, NULL }
/* Pointers to arrays of ggc_root_tab, terminated by NULL.  */
extern const struct ggc_root_tab * const gt_ggc_rtab[];
extern const struct ggc_root_tab * const gt_ggc_deletable_rtab[];

/* Structure for hash table cache marking.  */
struct htab;
struct ggc_cache_tab {
  struct htab * *base;
  size_t nelt;
  size_t stride;
  void (*cb) PARAMS ((void *));
  int (*marked_p) PARAMS ((const void *));
};
#define LAST_GGC_CACHE_TAB { NULL, 0, 0, NULL, NULL }
/* Pointers to arrays of ggc_cache_tab, terminated by NULL.  */
extern const struct ggc_cache_tab * const gt_ggc_cache_rtab[];

62
extern void ggc_mark_roots		PARAMS ((void));
63

64
extern void ggc_mark_rtx_children	PARAMS ((struct rtx_def *));
65

66 67 68
/* If EXPR is not NULL and previously unmarked, mark it and evaluate
   to true.  Otherwise evaluate to false.  */
#define ggc_test_and_set_mark(EXPR) \
69
  ((EXPR) != NULL && ((void *) (EXPR)) != (void *) 1 && ! ggc_set_mark (EXPR))
70 71 72

#define ggc_mark_rtx(EXPR)                      \
  do {                                          \
73
    rtx const r__ = (EXPR);                     \
74 75
    if (ggc_test_and_set_mark (r__))            \
      ggc_mark_rtx_children (r__);              \
76 77
  } while (0)

78
#define ggc_mark_tree gt_ggc_m_tree_node
79

80 81
#define ggc_mark(EXPR)				\
  do {						\
82
    const void *const a__ = (EXPR);		\
83
    if (a__ != NULL && a__ != (void *) 1)	\
84 85 86
      ggc_set_mark (a__);			\
  } while (0)

87 88
/* A GC implementation must provide these functions.  */

Kazu Hirata committed
89
/* Initialize the garbage collector.  */
90 91
extern void init_ggc		PARAMS ((void));
extern void init_stringpool	PARAMS ((void));
92

93 94
/* Start a new GGC context.  Memory allocated in previous contexts
   will not be collected while the new context is active.  */
95
extern void ggc_push_context	PARAMS ((void));
96

97 98
/* Finish a GC context.  Any uncollected memory in the new context
   will be merged with the old context.  */
99
extern void ggc_pop_context 	PARAMS ((void));
100

101
/* Allocation.  */
102 103

/* The internal primitive.  */
104
extern void *ggc_alloc		PARAMS ((size_t));
105
/* Like ggc_alloc, but allocates cleared memory.  */
106
extern void *ggc_alloc_cleared	PARAMS ((size_t));
107 108 109 110
/* Resize a block.  */
extern void *ggc_realloc	PARAMS ((void *, size_t));
/* Like ggc_alloc_cleared, but performs a multiplication.  */
extern void *ggc_calloc		PARAMS ((size_t, size_t));
111

112 113 114
#define ggc_alloc_rtx(NSLOTS)						  \
  ((struct rtx_def *) ggc_alloc (sizeof (struct rtx_def)		  \
				 + ((NSLOTS) - 1) * sizeof (rtunion)))
115 116

#define ggc_alloc_rtvec(NELT)						  \
117 118
  ((struct rtvec_def *) ggc_alloc (sizeof (struct rtvec_def)		  \
				   + ((NELT) - 1) * sizeof (rtx)))
119

120
#define ggc_alloc_tree(LENGTH) ((union tree_node *) ggc_alloc (LENGTH))
121

122 123 124
#define htab_create_ggc(SIZE, HASH, EQ, DEL) \
  htab_create_alloc (SIZE, HASH, EQ, DEL, ggc_calloc, NULL)

Zack Weinberg committed
125 126 127
/* Allocate a gc-able string, and fill it with LENGTH bytes from CONTENTS.
   If LENGTH is -1, then CONTENTS is assumed to be a
   null-terminated string and the memory sized accordingly.  */
128 129
extern const char *ggc_alloc_string	PARAMS ((const char *contents,
						 int length));
130

131 132 133
/* Make a copy of S, in GC-able memory.  */
#define ggc_strdup(S) ggc_alloc_string((S), -1)

134 135
/* Invoke the collector.  Garbage collection occurs only when this
   function is called, not during allocations.  */
136
extern void ggc_collect			PARAMS ((void));
137

138
/* Actually set the mark on a particular region of memory, but don't
139 140 141 142
   follow pointers.  This function is called by ggc_mark_*.  It
   returns zero if the object was not previously marked; non-zero if
   the object was already marked, or if, for any other reason,
   pointers in this data structure should not be traversed.  */
143 144
extern int ggc_set_mark			PARAMS ((const void *));

145
/* Return 1 if P has been marked, zero otherwise.
146 147 148
   P must have been allocated by the GC allocator; it mustn't point to
   static objects, stack variables, or memory allocated with malloc.  */
extern int ggc_marked_p			PARAMS ((const void *));
149

150 151 152 153
/* Statistics.  */

/* This structure contains the statistics common to all collectors.
   Particular collectors can extend this structure.  */
154
typedef struct ggc_statistics
155 156 157
{
  /* The Ith element is the number of nodes allocated with code I.  */
  unsigned num_trees[256];
158
  /* The Ith element is the number of bytes allocated by nodes with
159 160 161 162
     code I.  */
  size_t size_trees[256];
  /* The Ith element is the number of nodes allocated with code I.  */
  unsigned num_rtxs[256];
163
  /* The Ith element is the number of bytes allocated by nodes with
164 165 166 167 168 169
     code I.  */
  size_t size_rtxs[256];
  /* The total size of the tree nodes allocated.  */
  size_t total_size_trees;
  /* The total size of the RTL nodes allocated.  */
  size_t total_size_rtxs;
170 171 172 173
  /* The total number of tree nodes allocated.  */
  unsigned total_num_trees;
  /* The total number of RTL nodes allocated.  */
  unsigned total_num_rtxs;
174 175 176
} ggc_statistics;

/* Return the number of bytes allocated at the indicated address.  */
177
extern size_t ggc_get_size		PARAMS ((const void *));
178 179 180

/* Used by the various collectors to gather and print statistics that
   do not depend on the collector in use.  */
181
extern void ggc_print_common_statistics PARAMS ((FILE *, ggc_statistics *));
182 183

/* Print allocation statistics.  */
184 185
extern void ggc_print_statistics	PARAMS ((void));
extern void stringpool_statistics	PARAMS ((void));