c-family/
[official-gcc.git] / gcc / ggc.h
blob5f25a580b182d6e844e9aee440bb053c4981c3a9
1 /* Garbage collection for the GNU compiler.
3 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007,
4 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #ifndef GCC_GGC_H
23 #define GCC_GGC_H
24 #include "statistics.h"
26 /* Symbols are marked with `ggc' for `gcc gc' so as not to interfere with
27 an external gc library that might be linked in. */
29 /* Constants for general use. */
30 extern const char empty_string[]; /* empty string */
32 /* Internal functions and data structures used by the GTY
33 machinery, including the generated gt*.[hc] files. */
35 #include "gtype-desc.h"
37 /* One of these applies its third parameter (with cookie in the fourth
38 parameter) to each pointer in the object pointed to by the first
39 parameter, using the second parameter. */
40 typedef void (*gt_note_pointers) (void *, void *, gt_pointer_operator,
41 void *);
43 /* One of these is called before objects are re-ordered in memory.
44 The first parameter is the original object, the second is the
45 subobject that has had its pointers reordered, the third parameter
46 can compute the new values of a pointer when given the cookie in
47 the fourth parameter. */
48 typedef void (*gt_handle_reorder) (void *, void *, gt_pointer_operator,
49 void *);
51 /* Used by the gt_pch_n_* routines. Register an object in the hash table. */
52 extern int gt_pch_note_object (void *, void *, gt_note_pointers,
53 enum gt_types_enum);
55 /* Used by the gt_pch_n_* routines. Register that an object has a reorder
56 function. */
57 extern void gt_pch_note_reorder (void *, void *, gt_handle_reorder);
59 /* Mark the object in the first parameter and anything it points to. */
60 typedef void (*gt_pointer_walker) (void *);
62 /* Structures for the easy way to mark roots.
63 In an array, terminated by having base == NULL. */
64 struct ggc_root_tab {
65 void *base;
66 size_t nelt;
67 size_t stride;
68 gt_pointer_walker cb;
69 gt_pointer_walker pchw;
71 #define LAST_GGC_ROOT_TAB { NULL, 0, 0, NULL, NULL }
72 /* Pointers to arrays of ggc_root_tab, terminated by NULL. */
73 extern const struct ggc_root_tab * const gt_ggc_rtab[];
74 extern const struct ggc_root_tab * const gt_ggc_deletable_rtab[];
75 extern const struct ggc_root_tab * const gt_pch_cache_rtab[];
76 extern const struct ggc_root_tab * const gt_pch_scalar_rtab[];
78 /* Structure for hash table cache marking. */
79 struct htab;
80 struct ggc_cache_tab {
81 struct htab * *base;
82 size_t nelt;
83 size_t stride;
84 gt_pointer_walker cb;
85 gt_pointer_walker pchw;
86 int (*marked_p) (const void *);
88 #define LAST_GGC_CACHE_TAB { NULL, 0, 0, NULL, NULL, NULL }
89 /* Pointers to arrays of ggc_cache_tab, terminated by NULL. */
90 extern const struct ggc_cache_tab * const gt_ggc_cache_rtab[];
92 /* If EXPR is not NULL and previously unmarked, mark it and evaluate
93 to true. Otherwise evaluate to false. */
94 #define ggc_test_and_set_mark(EXPR) \
95 ((EXPR) != NULL && ((void *) (EXPR)) != (void *) 1 && ! ggc_set_mark (EXPR))
97 #define ggc_mark(EXPR) \
98 do { \
99 const void *const a__ = (EXPR); \
100 if (a__ != NULL && a__ != (void *) 1) \
101 ggc_set_mark (a__); \
102 } while (0)
104 /* Actually set the mark on a particular region of memory, but don't
105 follow pointers. This function is called by ggc_mark_*. It
106 returns zero if the object was not previously marked; nonzero if
107 the object was already marked, or if, for any other reason,
108 pointers in this data structure should not be traversed. */
109 extern int ggc_set_mark (const void *);
111 /* Return 1 if P has been marked, zero otherwise.
112 P must have been allocated by the GC allocator; it mustn't point to
113 static objects, stack variables, or memory allocated with malloc. */
114 extern int ggc_marked_p (const void *);
116 /* PCH and GGC handling for strings, mostly trivial. */
117 extern void gt_pch_n_S (const void *);
118 extern void gt_ggc_m_S (const void *);
120 /* End of GTY machinery API. */
122 struct alloc_zone;
124 /* Initialize the string pool. */
125 extern void init_stringpool (void);
127 /* Initialize the garbage collector. */
128 extern void init_ggc (void);
130 /* When true, identifier nodes are considered as GC roots. When
131 false, identifier nodes are treated like any other GC-allocated
132 object, and the identifier hash table is treated as a weak
133 hash. */
134 extern bool ggc_protect_identifiers;
136 /* Write out all GCed objects to F. */
137 extern void gt_pch_save (FILE *f);
140 /* Allocation. */
142 /* The internal primitive. */
143 extern void *ggc_internal_alloc_stat (size_t MEM_STAT_DECL)
144 ATTRIBUTE_MALLOC;
146 extern size_t ggc_round_alloc_size (size_t requested_size);
148 #define ggc_internal_alloc(s) ggc_internal_alloc_stat (s MEM_STAT_INFO)
150 /* Allocate an object of the specified type and size. */
151 extern void *ggc_alloc_typed_stat (enum gt_types_enum, size_t MEM_STAT_DECL)
152 ATTRIBUTE_MALLOC;
154 #define ggc_alloc_typed(s, z) ggc_alloc_typed_stat (s, z MEM_STAT_INFO)
156 /* Allocates cleared memory. */
157 extern void *ggc_internal_cleared_alloc_stat (size_t MEM_STAT_DECL)
158 ATTRIBUTE_MALLOC;
160 /* Resize a block. */
161 extern void *ggc_realloc_stat (void *, size_t MEM_STAT_DECL);
163 /* Free a block. To be used when known for certain it's not reachable. */
164 extern void ggc_free (void *);
166 extern void dump_ggc_loc_statistics (bool);
168 /* Reallocators. */
169 #define GGC_RESIZEVEC(T, P, N) \
170 ((T *) ggc_realloc_stat ((P), (N) * sizeof (T) MEM_STAT_INFO))
172 #define GGC_RESIZEVAR(T, P, N) \
173 ((T *) ggc_realloc_stat ((P), (N) MEM_STAT_INFO))
175 static inline void *
176 ggc_internal_vec_alloc_stat (size_t s, size_t c MEM_STAT_DECL)
178 return ggc_internal_alloc_stat (c * s PASS_MEM_STAT);
181 static inline void *
182 ggc_internal_cleared_vec_alloc_stat (size_t s, size_t c MEM_STAT_DECL)
184 return ggc_internal_cleared_alloc_stat (c * s PASS_MEM_STAT);
187 #define ggc_internal_cleared_vec_alloc(s, c) \
188 (ggc_internal_cleared_vec_alloc_stat ((s), (c) MEM_STAT_INFO))
190 static inline void *
191 ggc_alloc_atomic_stat (size_t s MEM_STAT_DECL)
193 return ggc_internal_alloc_stat (s PASS_MEM_STAT);
196 #define ggc_alloc_atomic(S) (ggc_alloc_atomic_stat ((S) MEM_STAT_INFO))
198 #define ggc_alloc_cleared_atomic(S) \
199 (ggc_internal_cleared_alloc_stat ((S) MEM_STAT_INFO))
201 extern void *ggc_cleared_alloc_htab_ignore_args (size_t, size_t)
202 ATTRIBUTE_MALLOC;
204 extern void *ggc_cleared_alloc_ptr_array_two_args (size_t, size_t)
205 ATTRIBUTE_MALLOC;
207 #define htab_create_ggc(SIZE, HASH, EQ, DEL) \
208 htab_create_typed_alloc (SIZE, HASH, EQ, DEL, \
209 ggc_cleared_alloc_htab_ignore_args, \
210 ggc_cleared_alloc_ptr_array_two_args, \
211 ggc_free)
213 #define splay_tree_new_ggc(COMPARE, ALLOC_TREE, ALLOC_NODE) \
214 splay_tree_new_typed_alloc (COMPARE, NULL, NULL, &ALLOC_TREE, &ALLOC_NODE, \
215 &ggc_splay_dont_free, NULL)
217 extern void *ggc_splay_alloc (enum gt_types_enum, int, void *)
218 ATTRIBUTE_MALLOC;
220 extern void ggc_splay_dont_free (void *, void *);
222 /* Allocate a gc-able string, and fill it with LENGTH bytes from CONTENTS.
223 If LENGTH is -1, then CONTENTS is assumed to be a
224 null-terminated string and the memory sized accordingly. */
225 extern const char *ggc_alloc_string_stat (const char *contents, int length
226 MEM_STAT_DECL);
228 #define ggc_alloc_string(c, l) ggc_alloc_string_stat (c, l MEM_STAT_INFO)
230 /* Make a copy of S, in GC-able memory. */
231 #define ggc_strdup(S) ggc_alloc_string_stat ((S), -1 MEM_STAT_INFO)
233 /* Invoke the collector. Garbage collection occurs only when this
234 function is called, not during allocations. */
235 extern void ggc_collect (void);
237 /* Register an additional root table. This can be useful for some
238 plugins. Does nothing if the passed pointer is NULL. */
239 extern void ggc_register_root_tab (const struct ggc_root_tab *);
241 /* Register an additional cache table. This can be useful for some
242 plugins. Does nothing if the passed pointer is NULL. */
243 extern void ggc_register_cache_tab (const struct ggc_cache_tab *);
245 /* Read objects previously saved with gt_pch_save from F. */
246 extern void gt_pch_restore (FILE *f);
248 /* Statistics. */
250 /* Print allocation statistics. */
251 extern void ggc_print_statistics (void);
253 extern void stringpool_statistics (void);
255 /* Heuristics. */
256 extern void init_ggc_heuristics (void);
258 /* Zone collection. */
260 /* For regular rtl allocations. */
261 extern struct alloc_zone rtl_zone;
263 /* For regular tree allocations. */
264 extern struct alloc_zone tree_zone;
266 /* For IDENTIFIER_NODE allocations. */
267 extern struct alloc_zone tree_id_zone;
269 #define ggc_alloc_rtvec_sized(NELT) \
270 ggc_alloc_zone_rtvec_def (sizeof (struct rtvec_def) \
271 + ((NELT) - 1) * sizeof (rtx), \
272 &rtl_zone)
274 #if defined (GGC_ZONE) && !defined (GENERATOR_FILE)
276 /* Allocate an object into the specified allocation zone. */
277 extern void *ggc_internal_alloc_zone_stat (size_t,
278 struct alloc_zone * MEM_STAT_DECL)
279 ATTRIBUTE_MALLOC;
281 extern void *ggc_internal_cleared_alloc_zone_stat (size_t,
282 struct alloc_zone * MEM_STAT_DECL)
283 ATTRIBUTE_MALLOC;
285 static inline void *
286 ggc_internal_zone_alloc_stat (struct alloc_zone * z, size_t s MEM_STAT_DECL)
288 return ggc_internal_alloc_zone_stat (s, z PASS_MEM_STAT);
291 static inline void *
292 ggc_internal_zone_cleared_alloc_stat (struct alloc_zone * z, size_t s
293 MEM_STAT_DECL)
295 return ggc_internal_cleared_alloc_zone_stat (s, z PASS_MEM_STAT);
298 static inline void *
299 ggc_internal_zone_vec_alloc_stat (struct alloc_zone * z, size_t s, size_t n
300 MEM_STAT_DECL)
302 return ggc_internal_alloc_zone_stat (s * n, z PASS_MEM_STAT);
306 #else
308 static inline void *
309 ggc_internal_zone_alloc_stat (struct alloc_zone * z ATTRIBUTE_UNUSED,
310 size_t s MEM_STAT_DECL)
312 return ggc_internal_alloc_stat (s PASS_MEM_STAT);
315 static inline void *
316 ggc_internal_zone_cleared_alloc_stat (struct alloc_zone * z ATTRIBUTE_UNUSED,
317 size_t s MEM_STAT_DECL)
319 return ggc_internal_cleared_alloc_stat (s PASS_MEM_STAT);
322 static inline void *
323 ggc_internal_zone_vec_alloc_stat (struct alloc_zone * z ATTRIBUTE_UNUSED,
324 size_t s, size_t n MEM_STAT_DECL)
326 return ggc_internal_vec_alloc_stat (s, n PASS_MEM_STAT);
329 #endif
331 /* Memory statistics passing versions of some allocators. Too few of them to
332 make gengtype produce them, so just define the needed ones here. */
333 static inline struct rtx_def *
334 ggc_alloc_zone_rtx_def_stat (struct alloc_zone * z, size_t s MEM_STAT_DECL)
336 return (struct rtx_def *) ggc_internal_zone_alloc_stat (z, s PASS_MEM_STAT);
339 static inline union tree_node *
340 ggc_alloc_zone_tree_node_stat (struct alloc_zone * z, size_t s MEM_STAT_DECL)
342 return (union tree_node *) ggc_internal_zone_alloc_stat (z, s PASS_MEM_STAT);
345 static inline union tree_node *
346 ggc_alloc_zone_cleared_tree_node_stat (struct alloc_zone * z, size_t s
347 MEM_STAT_DECL)
349 return (union tree_node *)
350 ggc_internal_zone_cleared_alloc_stat (z, s PASS_MEM_STAT);
353 static inline union gimple_statement_d *
354 ggc_alloc_cleared_gimple_statement_d_stat (size_t s MEM_STAT_DECL)
356 return (union gimple_statement_d *)
357 ggc_internal_cleared_alloc_stat (s PASS_MEM_STAT);
360 #endif