]>
Commit | Line | Data |
---|---|---|
0a25f1f5 | 1 | /* Garbage collection for the GNU compiler. |
cd5a58e5 | 2 | Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc. |
0a25f1f5 | 3 | |
770ae6cc RK |
4 | This file is part of GNU CC. |
5 | ||
6 | GNU CC is free software; you can redistribute it and/or modify it | |
7 | under the terms of the GNU General Public License as published by the | |
8 | Free Software Foundation; either version 2, or (at your option) any | |
9 | later version. | |
10 | ||
11 | GNU CC is distributed in the hope that it will be useful, but WITHOUT | |
12 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
13 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
14 | for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with GNU CC; see the file COPYING. If not, write to the Free | |
18 | Software Foundation, 59 Temple Place - Suite 330, Boston, MA | |
19 | 02111-1307, USA. */ | |
0a25f1f5 | 20 | |
bedda2da | 21 | #include "varray.h" |
0a25f1f5 RH |
22 | |
23 | /* Symbols are marked with `ggc' for `gcc gc' so as not to interfere with | |
24 | an external gc library that might be linked in. */ | |
25 | ||
87ff9c8e RH |
26 | /* These structures are defined in various headers throughout the |
27 | compiler. However, rather than force everyone who includes this | |
28 | header to include all the headers in which they are declared, we | |
29 | just forward-declare them here. */ | |
87ff9c8e RH |
30 | struct eh_status; |
31 | struct emit_status; | |
fa51b01b | 32 | struct expr_status; |
b49a6a90 AS |
33 | struct hash_table; |
34 | struct label_node; | |
4c85a96d | 35 | struct rtx_def; |
b49a6a90 | 36 | struct rtvec_def; |
87ff9c8e | 37 | struct stmt_status; |
b49a6a90 | 38 | union tree_node; |
87ff9c8e RH |
39 | struct varasm_status; |
40 | ||
21a427cc | 41 | /* Constants for general use. */ |
a8a05998 ZW |
42 | extern const char empty_string[]; /* empty string */ |
43 | extern const char digit_vector[]; /* "0" .. "9" */ | |
44 | #define digit_string(d) (digit_vector + ((d) * 2)) | |
21a427cc | 45 | |
bedda2da MM |
46 | /* Trees that have been marked, but whose children still need marking. */ |
47 | extern varray_type ggc_pending_trees; | |
48 | ||
b49a6a90 | 49 | /* Manipulate global roots that are needed between calls to gc. */ |
3fe41456 KG |
50 | void ggc_add_root PARAMS ((void *base, int nelt, int size, void (*)(void *))); |
51 | void ggc_add_rtx_root PARAMS ((struct rtx_def **, int nelt)); | |
52 | void ggc_add_tree_root PARAMS ((union tree_node **, int nelt)); | |
ef8288f7 | 53 | void ggc_add_rtx_varray_root PARAMS ((struct varray_head_tag **, int nelt)); |
3fe41456 KG |
54 | void ggc_add_tree_varray_root PARAMS ((struct varray_head_tag **, int nelt)); |
55 | void ggc_add_tree_hash_table_root PARAMS ((struct hash_table **, int nelt)); | |
56 | void ggc_del_root PARAMS ((void *base)); | |
0a25f1f5 | 57 | |
b49a6a90 AS |
58 | /* Mark nodes from the gc_add_root callback. These functions follow |
59 | pointers to mark other objects too. */ | |
ef8288f7 | 60 | extern void ggc_mark_rtx_varray PARAMS ((struct varray_head_tag *)); |
3fe41456 KG |
61 | extern void ggc_mark_tree_varray PARAMS ((struct varray_head_tag *)); |
62 | extern void ggc_mark_tree_hash_table PARAMS ((struct hash_table *)); | |
63 | extern void ggc_mark_roots PARAMS ((void)); | |
96df4529 | 64 | |
3fe41456 KG |
65 | extern void ggc_mark_rtx_children PARAMS ((struct rtx_def *)); |
66 | extern void ggc_mark_rtvec_children PARAMS ((struct rtvec_def *)); | |
96df4529 | 67 | |
52a92176 AS |
68 | /* If EXPR is not NULL and previously unmarked, mark it and evaluate |
69 | to true. Otherwise evaluate to false. */ | |
70 | #define ggc_test_and_set_mark(EXPR) \ | |
71 | ((EXPR) != NULL && ! ggc_set_mark (EXPR)) | |
72 | ||
73 | #define ggc_mark_rtx(EXPR) \ | |
74 | do { \ | |
75 | rtx r__ = (EXPR); \ | |
76 | if (ggc_test_and_set_mark (r__)) \ | |
77 | ggc_mark_rtx_children (r__); \ | |
005537df RH |
78 | } while (0) |
79 | ||
bedda2da MM |
80 | #define ggc_mark_tree(EXPR) \ |
81 | do { \ | |
82 | tree t__ = (EXPR); \ | |
83 | if (ggc_test_and_set_mark (t__)) \ | |
84 | VARRAY_PUSH_TREE (ggc_pending_trees, t__); \ | |
005537df | 85 | } while (0) |
74c937ca | 86 | |
2a967f3d NB |
87 | #define ggc_mark_nonnull_tree(EXPR) \ |
88 | do { \ | |
89 | tree t__ = (EXPR); \ | |
90 | if (! ggc_set_mark (t__)) \ | |
91 | VARRAY_PUSH_TREE (ggc_pending_trees, t__); \ | |
92 | } while (0) | |
93 | ||
52a92176 AS |
94 | #define ggc_mark_rtvec(EXPR) \ |
95 | do { \ | |
96 | rtvec v__ = (EXPR); \ | |
97 | if (ggc_test_and_set_mark (v__)) \ | |
98 | ggc_mark_rtvec_children (v__); \ | |
96df4529 AS |
99 | } while (0) |
100 | ||
005537df RH |
101 | #define ggc_mark(EXPR) \ |
102 | do { \ | |
3cce094d | 103 | const void *a__ = (EXPR); \ |
005537df RH |
104 | if (a__ != NULL) \ |
105 | ggc_set_mark (a__); \ | |
106 | } while (0) | |
107 | ||
b49a6a90 AS |
108 | /* A GC implementation must provide these functions. */ |
109 | ||
110 | /* Initialize the garbage collector. */ | |
3fe41456 | 111 | extern void init_ggc PARAMS ((void)); |
520a57c8 | 112 | extern void init_stringpool PARAMS ((void)); |
0a25f1f5 | 113 | |
21cd906e MM |
114 | /* Start a new GGC context. Memory allocated in previous contexts |
115 | will not be collected while the new context is active. */ | |
3fe41456 | 116 | extern void ggc_push_context PARAMS ((void)); |
b49a6a90 | 117 | |
21cd906e MM |
118 | /* Finish a GC context. Any uncollected memory in the new context |
119 | will be merged with the old context. */ | |
3fe41456 | 120 | extern void ggc_pop_context PARAMS ((void)); |
21cd906e | 121 | |
0a25f1f5 | 122 | /* Allocation. */ |
005537df RH |
123 | |
124 | /* The internal primitive. */ | |
f8a83ee3 ZW |
125 | void *ggc_alloc PARAMS ((size_t)); |
126 | /* Like ggc_alloc, but allocates cleared memory. */ | |
127 | void *ggc_alloc_cleared PARAMS ((size_t)); | |
005537df | 128 | |
f8a83ee3 ZW |
129 | #define ggc_alloc_rtx(NSLOTS) \ |
130 | ((struct rtx_def *) ggc_alloc (sizeof (struct rtx_def) \ | |
131 | + ((NSLOTS) - 1) * sizeof (rtunion))) | |
005537df RH |
132 | |
133 | #define ggc_alloc_rtvec(NELT) \ | |
f8a83ee3 ZW |
134 | ((struct rtvec_def *) ggc_alloc (sizeof (struct rtvec_def) \ |
135 | + ((NELT) - 1) * sizeof (rtx))) | |
005537df | 136 | |
f8a83ee3 | 137 | #define ggc_alloc_tree(LENGTH) ((union tree_node *) ggc_alloc (LENGTH)) |
005537df | 138 | |
520a57c8 ZW |
139 | /* Allocate a gc-able string, and fill it with LENGTH bytes from CONTENTS. |
140 | If LENGTH is -1, then CONTENTS is assumed to be a | |
141 | null-terminated string and the memory sized accordingly. */ | |
142 | const char *ggc_alloc_string PARAMS ((const char *contents, int length)); | |
0a25f1f5 | 143 | |
f15b9af9 MM |
144 | /* Make a copy of S, in GC-able memory. */ |
145 | #define ggc_strdup(S) ggc_alloc_string((S), -1) | |
146 | ||
cd5a58e5 ZW |
147 | /* Invoke the collector. Garbage collection occurs only when this |
148 | function is called, not during allocations. */ | |
3fe41456 | 149 | void ggc_collect PARAMS ((void)); |
0a25f1f5 | 150 | |
b49a6a90 | 151 | /* Actually set the mark on a particular region of memory, but don't |
005537df RH |
152 | follow pointers. This function is called by ggc_mark_*. It |
153 | returns zero if the object was not previously marked; non-zero if | |
154 | the object was already marked, or if, for any other reason, | |
155 | pointers in this data structure should not be traversed. */ | |
3cce094d | 156 | int ggc_set_mark PARAMS ((const void *)); |
0a25f1f5 | 157 | |
0a25f1f5 RH |
158 | /* Callbacks to the languages. */ |
159 | ||
160 | /* This is the language's opportunity to mark nodes held through | |
161 | the lang_specific hooks in the tree. */ | |
3fe41456 | 162 | void lang_mark_tree PARAMS ((union tree_node *)); |
0a25f1f5 | 163 | |
f15b9af9 MM |
164 | /* The FALSE_LABEL_STACK, declared in except.h, has language-dependent |
165 | semantics. If a front-end needs to mark the false label stack, it | |
166 | should set this pointer to a non-NULL value. Otherwise, no marking | |
167 | will be done. */ | |
168 | extern void (*lang_mark_false_label_stack) PARAMS ((struct label_node *)); | |
87ff9c8e | 169 | |
0a25f1f5 RH |
170 | /* Mark functions for various structs scattered about. */ |
171 | ||
3fe41456 KG |
172 | void mark_eh_status PARAMS ((struct eh_status *)); |
173 | void mark_emit_status PARAMS ((struct emit_status *)); | |
174 | void mark_expr_status PARAMS ((struct expr_status *)); | |
175 | void mark_stmt_status PARAMS ((struct stmt_status *)); | |
176 | void mark_varasm_status PARAMS ((struct varasm_status *)); | |
177 | void mark_optab PARAMS ((void *)); | |
3277221c MM |
178 | |
179 | /* Statistics. */ | |
180 | ||
181 | /* This structure contains the statistics common to all collectors. | |
182 | Particular collectors can extend this structure. */ | |
183 | typedef struct ggc_statistics | |
184 | { | |
185 | /* The Ith element is the number of nodes allocated with code I. */ | |
186 | unsigned num_trees[256]; | |
187 | /* The Ith element is the number of bytes allocated by nodes with | |
188 | code I. */ | |
189 | size_t size_trees[256]; | |
190 | /* The Ith element is the number of nodes allocated with code I. */ | |
191 | unsigned num_rtxs[256]; | |
192 | /* The Ith element is the number of bytes allocated by nodes with | |
193 | code I. */ | |
194 | size_t size_rtxs[256]; | |
3277221c MM |
195 | /* The total size of the tree nodes allocated. */ |
196 | size_t total_size_trees; | |
3277221c MM |
197 | /* The total size of the RTL nodes allocated. */ |
198 | size_t total_size_rtxs; | |
63408827 RH |
199 | /* The total number of tree nodes allocated. */ |
200 | unsigned total_num_trees; | |
201 | /* The total number of RTL nodes allocated. */ | |
202 | unsigned total_num_rtxs; | |
3277221c MM |
203 | } ggc_statistics; |
204 | ||
205 | /* Return the number of bytes allocated at the indicated address. */ | |
3cce094d | 206 | size_t ggc_get_size PARAMS ((const void *)); |
3277221c MM |
207 | |
208 | /* Used by the various collectors to gather and print statistics that | |
209 | do not depend on the collector in use. */ | |
fba0bfd4 | 210 | void ggc_print_common_statistics PARAMS ((FILE *, ggc_statistics *)); |
c6991660 KG |
211 | |
212 | /* Print allocation statistics. */ | |
fba0bfd4 | 213 | extern void ggc_print_statistics PARAMS ((void)); |
520a57c8 | 214 | void stringpool_statistics PARAMS ((void)); |