190075Sobrien/* Garbage collection for the GNU compiler.
2169689Skan   Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
3132718Skan   Free Software Foundation, Inc.
490075Sobrien
590075SobrienThis file is part of GCC.
690075Sobrien
790075SobrienGCC is free software; you can redistribute it and/or modify it under
890075Sobrienthe terms of the GNU General Public License as published by the Free
990075SobrienSoftware Foundation; either version 2, or (at your option) any later
1090075Sobrienversion.
1190075Sobrien
1290075SobrienGCC is distributed in the hope that it will be useful, but WITHOUT ANY
1390075SobrienWARRANTY; without even the implied warranty of MERCHANTABILITY or
1490075SobrienFITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1590075Sobrienfor more details.
1690075Sobrien
1790075SobrienYou should have received a copy of the GNU General Public License
1890075Sobrienalong with GCC; see the file COPYING.  If not, write to the Free
19169689SkanSoftware Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
20169689Skan02110-1301, USA.  */
2190075Sobrien
22132718Skan#ifndef GCC_GGC_H
23132718Skan#define GCC_GGC_H
24169689Skan#include "statistics.h"
2590075Sobrien
2690075Sobrien/* Symbols are marked with `ggc' for `gcc gc' so as not to interfere with
2790075Sobrien   an external gc library that might be linked in.  */
2890075Sobrien
2990075Sobrien/* Constants for general use.  */
3090075Sobrienextern const char empty_string[];	/* empty string */
3190075Sobrienextern const char digit_vector[];	/* "0" .. "9" */
3290075Sobrien#define digit_string(d) (digit_vector + ((d) * 2))
3390075Sobrien
34132718Skan/* Internal functions and data structures used by the GTY
35132718Skan   machinery.  */
3690075Sobrien
37132718Skan/* The first parameter is a pointer to a pointer, the second a cookie.  */
38132718Skantypedef void (*gt_pointer_operator) (void *, void *);
39132718Skan
40132718Skan#include "gtype-desc.h"
41132718Skan
42132718Skan/* One of these applies its third parameter (with cookie in the fourth
43132718Skan   parameter) to each pointer in the object pointed to by the first
44132718Skan   parameter, using the second parameter.  */
45132718Skantypedef void (*gt_note_pointers) (void *, void *, gt_pointer_operator,
46132718Skan				  void *);
47132718Skan
48132718Skan/* One of these is called before objects are re-ordered in memory.
49132718Skan   The first parameter is the original object, the second is the
50132718Skan   subobject that has had its pointers reordered, the third parameter
51132718Skan   can compute the new values of a pointer when given the cookie in
52132718Skan   the fourth parameter.  */
53132718Skantypedef void (*gt_handle_reorder) (void *, void *, gt_pointer_operator,
54132718Skan				   void *);
55132718Skan
56132718Skan/* Used by the gt_pch_n_* routines.  Register an object in the hash table.  */
57169689Skanextern int gt_pch_note_object (void *, void *, gt_note_pointers,
58169689Skan			       enum gt_types_enum);
59132718Skan
60132718Skan/* Used by the gt_pch_n_* routines.  Register that an object has a reorder
61132718Skan   function.  */
62132718Skanextern void gt_pch_note_reorder (void *, void *, gt_handle_reorder);
63132718Skan
64132718Skan/* Mark the object in the first parameter and anything it points to.  */
65132718Skantypedef void (*gt_pointer_walker) (void *);
66132718Skan
67117395Skan/* Structures for the easy way to mark roots.
68132718Skan   In an array, terminated by having base == NULL.  */
69117395Skanstruct ggc_root_tab {
70117395Skan  void *base;
71117395Skan  size_t nelt;
72117395Skan  size_t stride;
73132718Skan  gt_pointer_walker cb;
74132718Skan  gt_pointer_walker pchw;
75117395Skan};
76132718Skan#define LAST_GGC_ROOT_TAB { NULL, 0, 0, NULL, NULL }
77117395Skan/* Pointers to arrays of ggc_root_tab, terminated by NULL.  */
78117395Skanextern const struct ggc_root_tab * const gt_ggc_rtab[];
79117395Skanextern const struct ggc_root_tab * const gt_ggc_deletable_rtab[];
80132718Skanextern const struct ggc_root_tab * const gt_pch_cache_rtab[];
81132718Skanextern const struct ggc_root_tab * const gt_pch_scalar_rtab[];
8290075Sobrien
83117395Skan/* Structure for hash table cache marking.  */
84117395Skanstruct htab;
85117395Skanstruct ggc_cache_tab {
86117395Skan  struct htab * *base;
87117395Skan  size_t nelt;
88117395Skan  size_t stride;
89132718Skan  gt_pointer_walker cb;
90132718Skan  gt_pointer_walker pchw;
91132718Skan  int (*marked_p) (const void *);
92117395Skan};
93132718Skan#define LAST_GGC_CACHE_TAB { NULL, 0, 0, NULL, NULL, NULL }
94117395Skan/* Pointers to arrays of ggc_cache_tab, terminated by NULL.  */
95117395Skanextern const struct ggc_cache_tab * const gt_ggc_cache_rtab[];
9690075Sobrien
9790075Sobrien/* If EXPR is not NULL and previously unmarked, mark it and evaluate
9890075Sobrien   to true.  Otherwise evaluate to false.  */
9990075Sobrien#define ggc_test_and_set_mark(EXPR) \
100117395Skan  ((EXPR) != NULL && ((void *) (EXPR)) != (void *) 1 && ! ggc_set_mark (EXPR))
10190075Sobrien
10290075Sobrien#define ggc_mark(EXPR)				\
10390075Sobrien  do {						\
104117395Skan    const void *const a__ = (EXPR);		\
105117395Skan    if (a__ != NULL && a__ != (void *) 1)	\
10690075Sobrien      ggc_set_mark (a__);			\
10790075Sobrien  } while (0)
10890075Sobrien
109132718Skan/* Actually set the mark on a particular region of memory, but don't
110132718Skan   follow pointers.  This function is called by ggc_mark_*.  It
111132718Skan   returns zero if the object was not previously marked; nonzero if
112132718Skan   the object was already marked, or if, for any other reason,
113132718Skan   pointers in this data structure should not be traversed.  */
114132718Skanextern int ggc_set_mark	(const void *);
11590075Sobrien
116132718Skan/* Return 1 if P has been marked, zero otherwise.
117132718Skan   P must have been allocated by the GC allocator; it mustn't point to
118132718Skan   static objects, stack variables, or memory allocated with malloc.  */
119132718Skanextern int ggc_marked_p	(const void *);
120132718Skan
121132718Skan/* Mark the entries in the string pool.  */
122132718Skanextern void ggc_mark_stringpool	(void);
123132718Skan
124132718Skan/* Call ggc_set_mark on all the roots.  */
125132718Skan
126132718Skanextern void ggc_mark_roots (void);
127132718Skan
128132718Skan/* Save and restore the string pool entries for PCH.  */
129132718Skan
130132718Skanextern void gt_pch_save_stringpool (void);
131132718Skanextern void gt_pch_fixup_stringpool (void);
132132718Skanextern void gt_pch_restore_stringpool (void);
133132718Skan
134132718Skan/* PCH and GGC handling for strings, mostly trivial.  */
135132718Skan
136132718Skanextern void gt_pch_p_S (void *, void *, gt_pointer_operator, void *);
137132718Skanextern void gt_pch_n_S (const void *);
138132718Skanextern void gt_ggc_m_S (void *);
139132718Skan
140132718Skan/* Initialize the string pool.  */
141132718Skanextern void init_stringpool (void);
142132718Skan
143132718Skan/* A GC implementation must provide these functions.  They are internal
144132718Skan   to the GC system.  */
145132718Skan
146132718Skan/* Forward declare the zone structure.  Only ggc_zone implements this.  */
147132718Skanstruct alloc_zone;
148132718Skan
14990075Sobrien/* Initialize the garbage collector.  */
150132718Skanextern void init_ggc (void);
15190075Sobrien
152132718Skan/* Start a new GGC zone.  */
153132718Skanextern struct alloc_zone *new_ggc_zone (const char *);
154132718Skan
155132718Skan/* Free a complete GGC zone, destroying everything in it.  */
156132718Skanextern void destroy_ggc_zone (struct alloc_zone *);
157132718Skan
158132718Skanstruct ggc_pch_data;
159132718Skan
160132718Skan/* Return a new ggc_pch_data structure.  */
161132718Skanextern struct ggc_pch_data *init_ggc_pch (void);
162132718Skan
163132718Skan/* The second parameter and third parameters give the address and size
164132718Skan   of an object.  Update the ggc_pch_data structure with as much of
165169689Skan   that information as is necessary. The bool argument should be true
166132718Skan   if the object is a string.  */
167169689Skanextern void ggc_pch_count_object (struct ggc_pch_data *, void *, size_t, bool,
168169689Skan				  enum gt_types_enum);
169132718Skan
170132718Skan/* Return the total size of the data to be written to hold all
171132718Skan   the objects previously passed to ggc_pch_count_object.  */
172132718Skanextern size_t ggc_pch_total_size (struct ggc_pch_data *);
173132718Skan
174132718Skan/* The objects, when read, will most likely be at the address
175132718Skan   in the second parameter.  */
176132718Skanextern void ggc_pch_this_base (struct ggc_pch_data *, void *);
177132718Skan
178132718Skan/* Assuming that the objects really do end up at the address
179132718Skan   passed to ggc_pch_this_base, return the address of this object.
180169689Skan   The bool argument should be true if the object is a string.  */
181169689Skanextern char *ggc_pch_alloc_object (struct ggc_pch_data *, void *, size_t, bool,
182169689Skan				   enum gt_types_enum);
183132718Skan
184132718Skan/* Write out any initial information required.  */
185132718Skanextern void ggc_pch_prepare_write (struct ggc_pch_data *, FILE *);
186132718Skan/* Write out this object, including any padding.  The last argument should be
187132718Skan   true if the object is a string.  */
188132718Skanextern void ggc_pch_write_object (struct ggc_pch_data *, FILE *, void *,
189132718Skan				  void *, size_t, bool);
190132718Skan/* All objects have been written, write out any final information
191132718Skan   required.  */
192132718Skanextern void ggc_pch_finish (struct ggc_pch_data *, FILE *);
193132718Skan
194132718Skan/* A PCH file has just been read in at the address specified second
195132718Skan   parameter.  Set up the GC implementation for the new objects.  */
196132718Skanextern void ggc_pch_read (FILE *, void *);
197132718Skan
198132718Skan
19990075Sobrien/* Allocation.  */
20090075Sobrien
201169689Skan/* When set, ggc_collect will do collection.  */
202169689Skanextern bool ggc_force_collect;
203132718Skan
20490075Sobrien/* The internal primitive.  */
205169689Skanextern void *ggc_alloc_stat (size_t MEM_STAT_DECL);
206169689Skan#define ggc_alloc(s) ggc_alloc_stat (s MEM_STAT_INFO)
207132718Skan/* Allocate an object of the specified type and size.  */
208169689Skanextern void *ggc_alloc_typed_stat (enum gt_types_enum, size_t MEM_STAT_DECL);
209169689Skan#define ggc_alloc_typed(s,z) ggc_alloc_typed_stat (s,z MEM_STAT_INFO)
21090075Sobrien/* Like ggc_alloc, but allocates cleared memory.  */
211169689Skanextern void *ggc_alloc_cleared_stat (size_t MEM_STAT_DECL);
212169689Skan#define ggc_alloc_cleared(s) ggc_alloc_cleared_stat (s MEM_STAT_INFO)
213117395Skan/* Resize a block.  */
214169689Skanextern void *ggc_realloc_stat (void *, size_t MEM_STAT_DECL);
215169689Skan#define ggc_realloc(s,z) ggc_realloc_stat (s,z MEM_STAT_INFO)
216117395Skan/* Like ggc_alloc_cleared, but performs a multiplication.  */
217132718Skanextern void *ggc_calloc (size_t, size_t);
218169689Skan/* Free a block.  To be used when known for certain it's not reachable.  */
219169689Skanextern void ggc_free (void *);
220169689Skan
221169689Skanextern void ggc_record_overhead (size_t, size_t, void * MEM_STAT_DECL);
222169689Skanextern void ggc_free_overhead (void *);
223169689Skanextern void ggc_prune_overhead_list (void);
22490075Sobrien
225169689Skanextern void dump_ggc_loc_statistics (void);
22690075Sobrien
227169689Skan/* Type-safe, C++-friendly versions of ggc_alloc() and gcc_calloc().  */
228169689Skan#define GGC_NEW(T)		((T *) ggc_alloc (sizeof (T)))
229169689Skan#define GGC_CNEW(T)		((T *) ggc_alloc_cleared (sizeof (T)))
230169689Skan#define GGC_NEWVEC(T, N)	((T *) ggc_alloc ((N) * sizeof(T)))
231169689Skan#define GGC_CNEWVEC(T, N)	((T *) ggc_alloc_cleared ((N) * sizeof(T)))
232169689Skan#define GGC_NEWVAR(T, S)	((T *) ggc_alloc ((S)))
233169689Skan#define GGC_CNEWVAR(T, S)	((T *) ggc_alloc_cleared ((S)))
234169689Skan#define GGC_RESIZEVEC(T, P, N)  ((T *) ggc_realloc ((P), (N) * sizeof (T)))
23590075Sobrien
236169689Skan#define ggc_alloc_rtvec(NELT)						 \
237169689Skan  ((rtvec) ggc_alloc_zone (sizeof (struct rtvec_def) + ((NELT) - 1)	 \
238169689Skan			   * sizeof (rtx), &rtl_zone))
23990075Sobrien
240169689Skan#define ggc_alloc_tree(LENGTH) ((tree) ggc_alloc_zone (LENGTH, &tree_zone))
241169689Skan
242117395Skan#define htab_create_ggc(SIZE, HASH, EQ, DEL) \
243117395Skan  htab_create_alloc (SIZE, HASH, EQ, DEL, ggc_calloc, NULL)
244117395Skan
245132718Skan#define splay_tree_new_ggc(COMPARE)					 \
246132718Skan  splay_tree_new_with_allocator (COMPARE, NULL, NULL,			 \
247132718Skan                                 &ggc_splay_alloc, &ggc_splay_dont_free, \
248132718Skan				 NULL)
249132718Skanextern void *ggc_splay_alloc (int, void *);
250132718Skanextern void ggc_splay_dont_free (void *, void *);
251132718Skan
25290075Sobrien/* Allocate a gc-able string, and fill it with LENGTH bytes from CONTENTS.
25390075Sobrien   If LENGTH is -1, then CONTENTS is assumed to be a
25490075Sobrien   null-terminated string and the memory sized accordingly.  */
255132718Skanextern const char *ggc_alloc_string (const char *contents, int length);
25690075Sobrien
25790075Sobrien/* Make a copy of S, in GC-able memory.  */
25890075Sobrien#define ggc_strdup(S) ggc_alloc_string((S), -1)
25990075Sobrien
26090075Sobrien/* Invoke the collector.  Garbage collection occurs only when this
26190075Sobrien   function is called, not during allocations.  */
262132718Skanextern void ggc_collect	(void);
26390075Sobrien
264132718Skan/* Return the number of bytes allocated at the indicated address.  */
265132718Skanextern size_t ggc_get_size (const void *);
26690075Sobrien
267132718Skan/* Write out all GCed objects to F.  */
268132718Skanextern void gt_pch_save (FILE *f);
26990075Sobrien
270132718Skan/* Read objects previously saved with gt_pch_save from F.  */
271132718Skanextern void gt_pch_restore (FILE *f);
272132718Skan
27390075Sobrien/* Statistics.  */
27490075Sobrien
27590075Sobrien/* This structure contains the statistics common to all collectors.
27690075Sobrien   Particular collectors can extend this structure.  */
277117395Skantypedef struct ggc_statistics
27890075Sobrien{
279132718Skan  /* At present, we don't really gather any interesting statistics.  */
280132718Skan  int unused;
28190075Sobrien} ggc_statistics;
28290075Sobrien
28390075Sobrien/* Used by the various collectors to gather and print statistics that
28490075Sobrien   do not depend on the collector in use.  */
285132718Skanextern void ggc_print_common_statistics (FILE *, ggc_statistics *);
28690075Sobrien
28790075Sobrien/* Print allocation statistics.  */
288132718Skanextern void ggc_print_statistics (void);
289132718Skanextern void stringpool_statistics (void);
290117395Skan
291117395Skan/* Heuristics.  */
292132718Skanextern int ggc_min_expand_heuristic (void);
293261188Spfg/* APPLE LOCAL begin retune gc params 6124839 */
294261188Spfgextern int ggc_min_heapsize_heuristic (bool);
295261188Spfgextern void init_ggc_heuristics (bool);
296261188Spfg/* APPLE LOCAL end retune gc params 6124839 */
297132718Skan
298169689Skan/* Zone collection.  */
299169689Skan#if defined (GGC_ZONE) && !defined (GENERATOR_FILE)
300169689Skan
301169689Skan/* For regular rtl allocations.  */
302169689Skanextern struct alloc_zone rtl_zone;
303169689Skan/* For regular tree allocations.  */
304169689Skanextern struct alloc_zone tree_zone;
305169689Skan/* For IDENTIFIER_NODE allocations.  */
306169689Skanextern struct alloc_zone tree_id_zone;
307169689Skan
308169689Skan/* Allocate an object into the specified allocation zone.  */
309169689Skanextern void *ggc_alloc_zone_stat (size_t, struct alloc_zone * MEM_STAT_DECL);
310169689Skan# define ggc_alloc_zone(s,z) ggc_alloc_zone_stat (s,z MEM_STAT_INFO)
311169689Skan# define ggc_alloc_zone_pass_stat(s,z) ggc_alloc_zone_stat (s,z PASS_MEM_STAT)
312169689Skan#else
313169689Skan
314169689Skan# define ggc_alloc_zone(s, z) ggc_alloc (s)
315169689Skan# define ggc_alloc_zone_pass_stat(s, z) ggc_alloc_stat (s PASS_MEM_STAT)
316169689Skan
317132718Skan#endif
318169689Skan
319169689Skan#endif
320