]>
Commit | Line | Data |
---|---|---|
b49a6a90 | 1 | /* Simple garbage collection for the GNU compiler. |
85ec4feb | 2 | Copyright (C) 1999-2018 Free Software Foundation, Inc. |
b49a6a90 | 3 | |
1322177d | 4 | This file is part of GCC. |
b49a6a90 | 5 | |
1322177d LB |
6 | GCC is free software; you can redistribute it and/or modify it under |
7 | the terms of the GNU General Public License as published by the Free | |
9dcd6f09 | 8 | Software Foundation; either version 3, or (at your option) any later |
1322177d | 9 | version. |
b49a6a90 | 10 | |
1322177d LB |
11 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
12 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14a774a9 RK |
13 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
14 | for more details. | |
b49a6a90 | 15 | |
14a774a9 | 16 | You should have received a copy of the GNU General Public License |
9dcd6f09 NC |
17 | along with GCC; see the file COPYING3. If not see |
18 | <http://www.gnu.org/licenses/>. */ | |
b49a6a90 AS |
19 | |
20 | /* Generic garbage collection (GC) functions and data, not specific to | |
21 | any particular GC implementation. */ | |
22 | ||
23 | #include "config.h" | |
24 | #include "system.h" | |
4977bab6 | 25 | #include "coretypes.h" |
957060b5 | 26 | #include "timevar.h" |
718f9c0f | 27 | #include "diagnostic-core.h" |
957060b5 | 28 | #include "ggc-internal.h" |
67376cd2 | 29 | #include "params.h" |
18c81520 | 30 | #include "hosthooks.h" |
ae2392a9 | 31 | #include "plugin.h" |
17211ab5 | 32 | |
07724022 JH |
33 | /* When set, ggc_collect will do collection. */ |
34 | bool ggc_force_collect; | |
35 | ||
dae4174e TT |
36 | /* When true, protect the contents of the identifier hash table. */ |
37 | bool ggc_protect_identifiers = true; | |
38 | ||
3277221c MM |
39 | /* Statistics about the allocation. */ |
40 | static ggc_statistics *ggc_stats; | |
41 | ||
17211ab5 GK |
42 | struct traversal_state; |
43 | ||
20c1dc5e AJ |
44 | static int compare_ptr_data (const void *, const void *); |
45 | static void relocate_ptrs (void *, void *); | |
46 | static void write_pch_globals (const struct ggc_root_tab * const *tab, | |
47 | struct traversal_state *state); | |
b49a6a90 AS |
48 | |
49 | /* Maintain global roots that are preserved during GC. */ | |
50 | ||
ae2392a9 BS |
51 | /* This extra vector of dynamically registered root_tab-s is used by |
52 | ggc_mark_roots and gives the ability to dynamically add new GGC root | |
32c9b4e9 DS |
53 | tables, for instance from some plugins; this vector is on the heap |
54 | since it is used by GGC internally. */ | |
55 | typedef const struct ggc_root_tab *const_ggc_root_tab_t; | |
9771b263 | 56 | static vec<const_ggc_root_tab_t> extra_root_vec; |
ae2392a9 | 57 | |
ae2392a9 BS |
58 | /* Dynamically register a new GGC root table RT. This is useful for |
59 | plugins. */ | |
60 | ||
b8698a0f | 61 | void |
ae2392a9 BS |
62 | ggc_register_root_tab (const struct ggc_root_tab* rt) |
63 | { | |
32c9b4e9 | 64 | if (rt) |
9771b263 | 65 | extra_root_vec.safe_push (rt); |
ae2392a9 BS |
66 | } |
67 | ||
71bb2d86 NF |
68 | /* Mark all the roots in the table RT. */ |
69 | ||
70 | static void | |
71 | ggc_mark_root_tab (const_ggc_root_tab_t rt) | |
72 | { | |
73 | size_t i; | |
74 | ||
75 | for ( ; rt->base != NULL; rt++) | |
76 | for (i = 0; i < rt->nelt; i++) | |
77 | (*rt->cb) (*(void **) ((char *)rt->base + rt->stride * i)); | |
78 | } | |
79 | ||
cb2ec151 RH |
80 | /* Iterate through all registered roots and mark each element. */ |
81 | ||
b49a6a90 | 82 | void |
20c1dc5e | 83 | ggc_mark_roots (void) |
96df4529 | 84 | { |
e2500fed | 85 | const struct ggc_root_tab *const *rt; |
71bb2d86 | 86 | const_ggc_root_tab_t rtp, rti; |
e2500fed | 87 | size_t i; |
589005ff | 88 | |
e2500fed GK |
89 | for (rt = gt_ggc_deletable_rtab; *rt; rt++) |
90 | for (rti = *rt; rti->base != NULL; rti++) | |
91 | memset (rti->base, 0, rti->stride); | |
92 | ||
93 | for (rt = gt_ggc_rtab; *rt; rt++) | |
71bb2d86 | 94 | ggc_mark_root_tab (*rt); |
ae2392a9 | 95 | |
9771b263 | 96 | FOR_EACH_VEC_ELT (extra_root_vec, i, rtp) |
71bb2d86 | 97 | ggc_mark_root_tab (rtp); |
bedda2da | 98 | |
dae4174e TT |
99 | if (ggc_protect_identifiers) |
100 | ggc_mark_stringpool (); | |
bedda2da | 101 | |
aebf76a2 TS |
102 | gt_clear_caches (); |
103 | ||
dae4174e TT |
104 | if (! ggc_protect_identifiers) |
105 | ggc_purge_stringpool (); | |
ae2392a9 BS |
106 | |
107 | /* Some plugins may call ggc_set_mark from here. */ | |
108 | invoke_plugin_callbacks (PLUGIN_GGC_MARKING, NULL); | |
96df4529 AS |
109 | } |
110 | ||
e2500fed GK |
111 | /* Allocate a block of memory, then clear it. */ |
112 | void * | |
de49ce19 TS |
113 | ggc_internal_cleared_alloc (size_t size, void (*f)(void *), size_t s, size_t n |
114 | MEM_STAT_DECL) | |
ef8288f7 | 115 | { |
de49ce19 | 116 | void *buf = ggc_internal_alloc (size, f, s, n PASS_MEM_STAT); |
e2500fed GK |
117 | memset (buf, 0, size); |
118 | return buf; | |
ef8288f7 RH |
119 | } |
120 | ||
e2500fed GK |
121 | /* Resize a block of memory, possibly re-allocating it. */ |
122 | void * | |
231120e5 | 123 | ggc_realloc (void *x, size_t size MEM_STAT_DECL) |
ef8288f7 | 124 | { |
e2500fed GK |
125 | void *r; |
126 | size_t old_size; | |
ef8288f7 | 127 | |
e2500fed | 128 | if (x == NULL) |
231120e5 | 129 | return ggc_internal_alloc (size PASS_MEM_STAT); |
ef8288f7 | 130 | |
e2500fed | 131 | old_size = ggc_get_size (x); |
685fe032 | 132 | |
e2500fed | 133 | if (size <= old_size) |
9a0a7d5d HPN |
134 | { |
135 | /* Mark the unwanted memory as unaccessible. We also need to make | |
136 | the "new" size accessible, since ggc_get_size returns the size of | |
137 | the pool, not the size of the individually allocated object, the | |
138 | size which was previously made accessible. Unfortunately, we | |
139 | don't know that previously allocated size. Without that | |
140 | knowledge we have to lose some initialization-tracking for the | |
141 | old parts of the object. An alternative is to mark the whole | |
20c1dc5e | 142 | old_size as reachable, but that would lose tracking of writes |
9a0a7d5d HPN |
143 | after the end of the object (by small offsets). Discard the |
144 | handle to avoid handle leak. */ | |
35dee980 HPN |
145 | VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS ((char *) x + size, |
146 | old_size - size)); | |
147 | VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (x, size)); | |
9a0a7d5d HPN |
148 | return x; |
149 | } | |
ef8288f7 | 150 | |
231120e5 | 151 | r = ggc_internal_alloc (size PASS_MEM_STAT); |
9a0a7d5d HPN |
152 | |
153 | /* Since ggc_get_size returns the size of the pool, not the size of the | |
154 | individually allocated object, we'd access parts of the old object | |
155 | that were marked invalid with the memcpy below. We lose a bit of the | |
156 | initialization-tracking since some of it may be uninitialized. */ | |
35dee980 | 157 | VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (x, old_size)); |
9a0a7d5d | 158 | |
e2500fed | 159 | memcpy (r, x, old_size); |
9a0a7d5d HPN |
160 | |
161 | /* The old object is not supposed to be used anymore. */ | |
685fe032 | 162 | ggc_free (x); |
9a0a7d5d | 163 | |
e2500fed | 164 | return r; |
ef8288f7 RH |
165 | } |
166 | ||
f8a83ee3 | 167 | void * |
a9429e29 LB |
168 | ggc_cleared_alloc_htab_ignore_args (size_t c ATTRIBUTE_UNUSED, |
169 | size_t n ATTRIBUTE_UNUSED) | |
f8a83ee3 | 170 | { |
a9429e29 | 171 | gcc_assert (c * n == sizeof (struct htab)); |
766090c2 | 172 | return ggc_cleared_alloc<htab> (); |
a9429e29 LB |
173 | } |
174 | ||
175 | /* TODO: once we actually use type information in GGC, create a new tag | |
176 | gt_gcc_ptr_array and use it for pointer arrays. */ | |
177 | void * | |
178 | ggc_cleared_alloc_ptr_array_two_args (size_t c, size_t n) | |
179 | { | |
180 | gcc_assert (sizeof (PTR *) == n); | |
766090c2 | 181 | return ggc_cleared_vec_alloc<PTR *> (c); |
f8a83ee3 ZW |
182 | } |
183 | ||
17211ab5 | 184 | /* These are for splay_tree_new_ggc. */ |
20c1dc5e | 185 | void * |
cd030c07 | 186 | ggc_splay_alloc (int sz, void *nl) |
17211ab5 | 187 | { |
282899df | 188 | gcc_assert (!nl); |
a9429e29 | 189 | return ggc_internal_alloc (sz); |
17211ab5 GK |
190 | } |
191 | ||
192 | void | |
20c1dc5e | 193 | ggc_splay_dont_free (void * x ATTRIBUTE_UNUSED, void *nl) |
17211ab5 | 194 | { |
282899df | 195 | gcc_assert (!nl); |
17211ab5 GK |
196 | } |
197 | ||
3277221c | 198 | /* Print statistics that are independent of the collector in use. */ |
fba0bfd4 ZW |
199 | #define SCALE(x) ((unsigned long) ((x) < 1024*10 \ |
200 | ? (x) \ | |
201 | : ((x) < 1024*1024*10 \ | |
202 | ? (x) / 1024 \ | |
203 | : (x) / (1024*1024)))) | |
204 | #define LABEL(x) ((x) < 1024*10 ? ' ' : ((x) < 1024*1024*10 ? 'k' : 'M')) | |
3277221c MM |
205 | |
206 | void | |
20c1dc5e AJ |
207 | ggc_print_common_statistics (FILE *stream ATTRIBUTE_UNUSED, |
208 | ggc_statistics *stats) | |
3277221c | 209 | { |
3277221c MM |
210 | /* Set the pointer so that during collection we will actually gather |
211 | the statistics. */ | |
212 | ggc_stats = stats; | |
213 | ||
214 | /* Then do one collection to fill in the statistics. */ | |
215 | ggc_collect (); | |
216 | ||
17211ab5 GK |
217 | /* At present, we don't really gather any interesting statistics. */ |
218 | ||
219 | /* Don't gather statistics any more. */ | |
220 | ggc_stats = NULL; | |
221 | } | |
222 | \f | |
223 | /* Functions for saving and restoring GCable memory to disk. */ | |
224 | ||
20c1dc5e | 225 | struct ptr_data |
17211ab5 GK |
226 | { |
227 | void *obj; | |
228 | void *note_ptr_cookie; | |
229 | gt_note_pointers note_ptr_fn; | |
230 | gt_handle_reorder reorder_fn; | |
231 | size_t size; | |
232 | void *new_addr; | |
233 | }; | |
234 | ||
9204da15 | 235 | #define POINTER_HASH(x) (hashval_t)((intptr_t)x >> 3) |
17211ab5 | 236 | |
4a8fb1a1 LC |
237 | /* Helper for hashing saving_htab. */ |
238 | ||
95fbe13e | 239 | struct saving_hasher : free_ptr_hash <ptr_data> |
4a8fb1a1 | 240 | { |
67f58944 TS |
241 | typedef void *compare_type; |
242 | static inline hashval_t hash (const ptr_data *); | |
243 | static inline bool equal (const ptr_data *, const void *); | |
4a8fb1a1 LC |
244 | }; |
245 | ||
246 | inline hashval_t | |
67f58944 | 247 | saving_hasher::hash (const ptr_data *p) |
4a8fb1a1 LC |
248 | { |
249 | return POINTER_HASH (p->obj); | |
250 | } | |
251 | ||
252 | inline bool | |
67f58944 | 253 | saving_hasher::equal (const ptr_data *p1, const void *p2) |
4a8fb1a1 LC |
254 | { |
255 | return p1->obj == p2; | |
256 | } | |
257 | ||
c203e8a7 | 258 | static hash_table<saving_hasher> *saving_htab; |
4a8fb1a1 | 259 | |
17211ab5 GK |
260 | /* Register an object in the hash table. */ |
261 | ||
262 | int | |
20c1dc5e | 263 | gt_pch_note_object (void *obj, void *note_ptr_cookie, |
cd030c07 | 264 | gt_note_pointers note_ptr_fn) |
17211ab5 GK |
265 | { |
266 | struct ptr_data **slot; | |
20c1dc5e | 267 | |
17211ab5 GK |
268 | if (obj == NULL || obj == (void *) 1) |
269 | return 0; | |
270 | ||
271 | slot = (struct ptr_data **) | |
c203e8a7 | 272 | saving_htab->find_slot_with_hash (obj, POINTER_HASH (obj), INSERT); |
17211ab5 GK |
273 | if (*slot != NULL) |
274 | { | |
282899df NS |
275 | gcc_assert ((*slot)->note_ptr_fn == note_ptr_fn |
276 | && (*slot)->note_ptr_cookie == note_ptr_cookie); | |
17211ab5 GK |
277 | return 0; |
278 | } | |
20c1dc5e | 279 | |
d3bfe4de | 280 | *slot = XCNEW (struct ptr_data); |
17211ab5 GK |
281 | (*slot)->obj = obj; |
282 | (*slot)->note_ptr_fn = note_ptr_fn; | |
283 | (*slot)->note_ptr_cookie = note_ptr_cookie; | |
284 | if (note_ptr_fn == gt_pch_p_S) | |
d3bfe4de | 285 | (*slot)->size = strlen ((const char *)obj) + 1; |
17211ab5 GK |
286 | else |
287 | (*slot)->size = ggc_get_size (obj); | |
288 | return 1; | |
289 | } | |
290 | ||
291 | /* Register an object in the hash table. */ | |
292 | ||
293 | void | |
20c1dc5e AJ |
294 | gt_pch_note_reorder (void *obj, void *note_ptr_cookie, |
295 | gt_handle_reorder reorder_fn) | |
17211ab5 GK |
296 | { |
297 | struct ptr_data *data; | |
20c1dc5e | 298 | |
17211ab5 GK |
299 | if (obj == NULL || obj == (void *) 1) |
300 | return; | |
301 | ||
d3bfe4de | 302 | data = (struct ptr_data *) |
c203e8a7 | 303 | saving_htab->find_with_hash (obj, POINTER_HASH (obj)); |
282899df | 304 | gcc_assert (data && data->note_ptr_cookie == note_ptr_cookie); |
20c1dc5e | 305 | |
17211ab5 GK |
306 | data->reorder_fn = reorder_fn; |
307 | } | |
308 | ||
17211ab5 GK |
309 | /* Handy state for the traversal functions. */ |
310 | ||
20c1dc5e | 311 | struct traversal_state |
17211ab5 GK |
312 | { |
313 | FILE *f; | |
314 | struct ggc_pch_data *d; | |
315 | size_t count; | |
316 | struct ptr_data **ptrs; | |
317 | size_t ptrs_i; | |
318 | }; | |
319 | ||
320 | /* Callbacks for htab_traverse. */ | |
321 | ||
4a8fb1a1 LC |
322 | int |
323 | ggc_call_count (ptr_data **slot, traversal_state *state) | |
17211ab5 | 324 | { |
4a8fb1a1 | 325 | struct ptr_data *d = *slot; |
20c1dc5e | 326 | |
08cee789 | 327 | ggc_pch_count_object (state->d, d->obj, d->size, |
cd030c07 | 328 | d->note_ptr_fn == gt_pch_p_S); |
17211ab5 GK |
329 | state->count++; |
330 | return 1; | |
331 | } | |
332 | ||
4a8fb1a1 LC |
333 | int |
334 | ggc_call_alloc (ptr_data **slot, traversal_state *state) | |
17211ab5 | 335 | { |
4a8fb1a1 | 336 | struct ptr_data *d = *slot; |
20c1dc5e | 337 | |
08cee789 | 338 | d->new_addr = ggc_pch_alloc_object (state->d, d->obj, d->size, |
cd030c07 | 339 | d->note_ptr_fn == gt_pch_p_S); |
17211ab5 GK |
340 | state->ptrs[state->ptrs_i++] = d; |
341 | return 1; | |
342 | } | |
343 | ||
344 | /* Callback for qsort. */ | |
345 | ||
346 | static int | |
20c1dc5e | 347 | compare_ptr_data (const void *p1_p, const void *p2_p) |
17211ab5 | 348 | { |
58f9752a KG |
349 | const struct ptr_data *const p1 = *(const struct ptr_data *const *)p1_p; |
350 | const struct ptr_data *const p2 = *(const struct ptr_data *const *)p2_p; | |
17211ab5 GK |
351 | return (((size_t)p1->new_addr > (size_t)p2->new_addr) |
352 | - ((size_t)p1->new_addr < (size_t)p2->new_addr)); | |
353 | } | |
354 | ||
355 | /* Callbacks for note_ptr_fn. */ | |
356 | ||
357 | static void | |
20c1dc5e | 358 | relocate_ptrs (void *ptr_p, void *state_p) |
17211ab5 GK |
359 | { |
360 | void **ptr = (void **)ptr_p; | |
20c1dc5e | 361 | struct traversal_state *state ATTRIBUTE_UNUSED |
17211ab5 GK |
362 | = (struct traversal_state *)state_p; |
363 | struct ptr_data *result; | |
364 | ||
365 | if (*ptr == NULL || *ptr == (void *)1) | |
366 | return; | |
20c1dc5e | 367 | |
d3bfe4de | 368 | result = (struct ptr_data *) |
c203e8a7 | 369 | saving_htab->find_with_hash (*ptr, POINTER_HASH (*ptr)); |
282899df | 370 | gcc_assert (result); |
17211ab5 GK |
371 | *ptr = result->new_addr; |
372 | } | |
373 | ||
374 | /* Write out, after relocation, the pointers in TAB. */ | |
375 | static void | |
20c1dc5e AJ |
376 | write_pch_globals (const struct ggc_root_tab * const *tab, |
377 | struct traversal_state *state) | |
17211ab5 GK |
378 | { |
379 | const struct ggc_root_tab *const *rt; | |
380 | const struct ggc_root_tab *rti; | |
381 | size_t i; | |
382 | ||
383 | for (rt = tab; *rt; rt++) | |
384 | for (rti = *rt; rti->base != NULL; rti++) | |
385 | for (i = 0; i < rti->nelt; i++) | |
386 | { | |
387 | void *ptr = *(void **)((char *)rti->base + rti->stride * i); | |
388 | struct ptr_data *new_ptr; | |
389 | if (ptr == NULL || ptr == (void *)1) | |
390 | { | |
20c1dc5e | 391 | if (fwrite (&ptr, sizeof (void *), 1, state->f) |
17211ab5 | 392 | != 1) |
40fecdd6 | 393 | fatal_error (input_location, "can%'t write PCH file: %m"); |
17211ab5 GK |
394 | } |
395 | else | |
396 | { | |
d3bfe4de | 397 | new_ptr = (struct ptr_data *) |
c203e8a7 | 398 | saving_htab->find_with_hash (ptr, POINTER_HASH (ptr)); |
20c1dc5e | 399 | if (fwrite (&new_ptr->new_addr, sizeof (void *), 1, state->f) |
17211ab5 | 400 | != 1) |
40fecdd6 | 401 | fatal_error (input_location, "can%'t write PCH file: %m"); |
17211ab5 GK |
402 | } |
403 | } | |
404 | } | |
405 | ||
406 | /* Hold the information we need to mmap the file back in. */ | |
407 | ||
20c1dc5e | 408 | struct mmap_info |
17211ab5 GK |
409 | { |
410 | size_t offset; | |
411 | size_t size; | |
412 | void *preferred_base; | |
413 | }; | |
414 | ||
415 | /* Write out the state of the compiler to F. */ | |
416 | ||
417 | void | |
20c1dc5e | 418 | gt_pch_save (FILE *f) |
17211ab5 GK |
419 | { |
420 | const struct ggc_root_tab *const *rt; | |
421 | const struct ggc_root_tab *rti; | |
422 | size_t i; | |
423 | struct traversal_state state; | |
424 | char *this_object = NULL; | |
425 | size_t this_object_size = 0; | |
426 | struct mmap_info mmi; | |
c3284718 | 427 | const size_t mmap_offset_alignment = host_hooks.gt_pch_alloc_granularity (); |
17211ab5 GK |
428 | |
429 | gt_pch_save_stringpool (); | |
430 | ||
10d43c2d | 431 | timevar_push (TV_PCH_PTR_REALLOC); |
c203e8a7 | 432 | saving_htab = new hash_table<saving_hasher> (50000); |
17211ab5 GK |
433 | |
434 | for (rt = gt_ggc_rtab; *rt; rt++) | |
435 | for (rti = *rt; rti->base != NULL; rti++) | |
436 | for (i = 0; i < rti->nelt; i++) | |
437 | (*rti->pchw)(*(void **)((char *)rti->base + rti->stride * i)); | |
438 | ||
17211ab5 GK |
439 | /* Prepare the objects for writing, determine addresses and such. */ |
440 | state.f = f; | |
a9429e29 | 441 | state.d = init_ggc_pch (); |
17211ab5 | 442 | state.count = 0; |
c203e8a7 | 443 | saving_htab->traverse <traversal_state *, ggc_call_count> (&state); |
17211ab5 GK |
444 | |
445 | mmi.size = ggc_pch_total_size (state.d); | |
446 | ||
18c81520 GK |
447 | /* Try to arrange things so that no relocation is necessary, but |
448 | don't try very hard. On most platforms, this will always work, | |
b8698a0f | 449 | and on the rest it's a lot of work to do better. |
18c81520 GK |
450 | (The extra work goes in HOST_HOOKS_GT_PCH_GET_ADDRESS and |
451 | HOST_HOOKS_GT_PCH_USE_ADDRESS.) */ | |
4d0c31e6 | 452 | mmi.preferred_base = host_hooks.gt_pch_get_address (mmi.size, fileno (f)); |
b8698a0f | 453 | |
17211ab5 GK |
454 | ggc_pch_this_base (state.d, mmi.preferred_base); |
455 | ||
5ed6ace5 | 456 | state.ptrs = XNEWVEC (struct ptr_data *, state.count); |
17211ab5 | 457 | state.ptrs_i = 0; |
10d43c2d | 458 | |
c203e8a7 | 459 | saving_htab->traverse <traversal_state *, ggc_call_alloc> (&state); |
10d43c2d DN |
460 | timevar_pop (TV_PCH_PTR_REALLOC); |
461 | ||
462 | timevar_push (TV_PCH_PTR_SORT); | |
17211ab5 | 463 | qsort (state.ptrs, state.count, sizeof (*state.ptrs), compare_ptr_data); |
10d43c2d | 464 | timevar_pop (TV_PCH_PTR_SORT); |
17211ab5 GK |
465 | |
466 | /* Write out all the scalar variables. */ | |
467 | for (rt = gt_pch_scalar_rtab; *rt; rt++) | |
468 | for (rti = *rt; rti->base != NULL; rti++) | |
469 | if (fwrite (rti->base, rti->stride, 1, f) != 1) | |
40fecdd6 | 470 | fatal_error (input_location, "can%'t write PCH file: %m"); |
17211ab5 GK |
471 | |
472 | /* Write out all the global pointers, after translation. */ | |
473 | write_pch_globals (gt_ggc_rtab, &state); | |
17211ab5 | 474 | |
90aa6719 DS |
475 | /* Pad the PCH file so that the mmapped area starts on an allocation |
476 | granularity (usually page) boundary. */ | |
17211ab5 | 477 | { |
70f8b89f KG |
478 | long o; |
479 | o = ftell (state.f) + sizeof (mmi); | |
480 | if (o == -1) | |
40fecdd6 | 481 | fatal_error (input_location, "can%'t get position in PCH file: %m"); |
90aa6719 DS |
482 | mmi.offset = mmap_offset_alignment - o % mmap_offset_alignment; |
483 | if (mmi.offset == mmap_offset_alignment) | |
17211ab5 GK |
484 | mmi.offset = 0; |
485 | mmi.offset += o; | |
486 | } | |
487 | if (fwrite (&mmi, sizeof (mmi), 1, state.f) != 1) | |
40fecdd6 | 488 | fatal_error (input_location, "can%'t write PCH file: %m"); |
17211ab5 GK |
489 | if (mmi.offset != 0 |
490 | && fseek (state.f, mmi.offset, SEEK_SET) != 0) | |
40fecdd6 | 491 | fatal_error (input_location, "can%'t write padding to PCH file: %m"); |
17211ab5 | 492 | |
08cee789 DJ |
493 | ggc_pch_prepare_write (state.d, state.f); |
494 | ||
c132770e | 495 | #if defined ENABLE_VALGRIND_ANNOTATIONS && defined VALGRIND_GET_VBITS |
0b50e654 JJ |
496 | vec<char> vbits = vNULL; |
497 | #endif | |
498 | ||
17211ab5 GK |
499 | /* Actually write out the objects. */ |
500 | for (i = 0; i < state.count; i++) | |
3277221c | 501 | { |
17211ab5 GK |
502 | if (this_object_size < state.ptrs[i]->size) |
503 | { | |
504 | this_object_size = state.ptrs[i]->size; | |
d3bfe4de | 505 | this_object = XRESIZEVAR (char, this_object, this_object_size); |
17211ab5 | 506 | } |
c132770e | 507 | #if defined ENABLE_VALGRIND_ANNOTATIONS && defined VALGRIND_GET_VBITS |
0b50e654 JJ |
508 | /* obj might contain uninitialized bytes, e.g. in the trailing |
509 | padding of the object. Avoid warnings by making the memory | |
510 | temporarily defined and then restoring previous state. */ | |
511 | int get_vbits = 0; | |
512 | size_t valid_size = state.ptrs[i]->size; | |
513 | if (__builtin_expect (RUNNING_ON_VALGRIND, 0)) | |
514 | { | |
515 | if (vbits.length () < valid_size) | |
516 | vbits.safe_grow (valid_size); | |
517 | get_vbits = VALGRIND_GET_VBITS (state.ptrs[i]->obj, | |
518 | vbits.address (), valid_size); | |
519 | if (get_vbits == 3) | |
520 | { | |
521 | /* We assume that first part of obj is addressable, and | |
522 | the rest is unaddressable. Find out where the boundary is | |
523 | using binary search. */ | |
524 | size_t lo = 0, hi = valid_size; | |
525 | while (hi > lo) | |
526 | { | |
527 | size_t mid = (lo + hi) / 2; | |
528 | get_vbits = VALGRIND_GET_VBITS ((char *) state.ptrs[i]->obj | |
529 | + mid, vbits.address (), | |
530 | 1); | |
531 | if (get_vbits == 3) | |
532 | hi = mid; | |
533 | else if (get_vbits == 1) | |
534 | lo = mid + 1; | |
535 | else | |
536 | break; | |
537 | } | |
538 | if (get_vbits == 1 || get_vbits == 3) | |
539 | { | |
540 | valid_size = lo; | |
541 | get_vbits = VALGRIND_GET_VBITS (state.ptrs[i]->obj, | |
542 | vbits.address (), | |
543 | valid_size); | |
544 | } | |
545 | } | |
546 | if (get_vbits == 1) | |
547 | VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (state.ptrs[i]->obj, | |
548 | state.ptrs[i]->size)); | |
549 | } | |
550 | #endif | |
17211ab5 GK |
551 | memcpy (this_object, state.ptrs[i]->obj, state.ptrs[i]->size); |
552 | if (state.ptrs[i]->reorder_fn != NULL) | |
20c1dc5e | 553 | state.ptrs[i]->reorder_fn (state.ptrs[i]->obj, |
17211ab5 GK |
554 | state.ptrs[i]->note_ptr_cookie, |
555 | relocate_ptrs, &state); | |
20c1dc5e | 556 | state.ptrs[i]->note_ptr_fn (state.ptrs[i]->obj, |
17211ab5 GK |
557 | state.ptrs[i]->note_ptr_cookie, |
558 | relocate_ptrs, &state); | |
559 | ggc_pch_write_object (state.d, state.f, state.ptrs[i]->obj, | |
4d0c31e6 RH |
560 | state.ptrs[i]->new_addr, state.ptrs[i]->size, |
561 | state.ptrs[i]->note_ptr_fn == gt_pch_p_S); | |
17211ab5 GK |
562 | if (state.ptrs[i]->note_ptr_fn != gt_pch_p_S) |
563 | memcpy (state.ptrs[i]->obj, this_object, state.ptrs[i]->size); | |
c132770e | 564 | #if defined ENABLE_VALGRIND_ANNOTATIONS && defined VALGRIND_GET_VBITS |
0b50e654 JJ |
565 | if (__builtin_expect (get_vbits == 1, 0)) |
566 | { | |
567 | (void) VALGRIND_SET_VBITS (state.ptrs[i]->obj, vbits.address (), | |
568 | valid_size); | |
569 | if (valid_size != state.ptrs[i]->size) | |
570 | VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS ((char *) | |
571 | state.ptrs[i]->obj | |
572 | + valid_size, | |
573 | state.ptrs[i]->size | |
574 | - valid_size)); | |
575 | } | |
576 | #endif | |
3277221c | 577 | } |
c132770e | 578 | #if defined ENABLE_VALGRIND_ANNOTATIONS && defined VALGRIND_GET_VBITS |
0b50e654 JJ |
579 | vbits.release (); |
580 | #endif | |
581 | ||
17211ab5 | 582 | ggc_pch_finish (state.d, state.f); |
d24ecd21 | 583 | gt_pch_fixup_stringpool (); |
17211ab5 | 584 | |
0b50e654 JJ |
585 | XDELETE (state.ptrs); |
586 | XDELETE (this_object); | |
c203e8a7 TS |
587 | delete saving_htab; |
588 | saving_htab = NULL; | |
17211ab5 GK |
589 | } |
590 | ||
591 | /* Read the state of the compiler back in from F. */ | |
592 | ||
593 | void | |
20c1dc5e | 594 | gt_pch_restore (FILE *f) |
17211ab5 GK |
595 | { |
596 | const struct ggc_root_tab *const *rt; | |
597 | const struct ggc_root_tab *rti; | |
598 | size_t i; | |
599 | struct mmap_info mmi; | |
4d0c31e6 | 600 | int result; |
17211ab5 GK |
601 | |
602 | /* Delete any deletable objects. This makes ggc_pch_read much | |
603 | faster, as it can be sure that no GCable objects remain other | |
604 | than the ones just read in. */ | |
605 | for (rt = gt_ggc_deletable_rtab; *rt; rt++) | |
606 | for (rti = *rt; rti->base != NULL; rti++) | |
607 | memset (rti->base, 0, rti->stride); | |
608 | ||
609 | /* Read in all the scalar variables. */ | |
610 | for (rt = gt_pch_scalar_rtab; *rt; rt++) | |
611 | for (rti = *rt; rti->base != NULL; rti++) | |
612 | if (fread (rti->base, rti->stride, 1, f) != 1) | |
40fecdd6 | 613 | fatal_error (input_location, "can%'t read PCH file: %m"); |
17211ab5 GK |
614 | |
615 | /* Read in all the global pointers, in 6 easy loops. */ | |
616 | for (rt = gt_ggc_rtab; *rt; rt++) | |
617 | for (rti = *rt; rti->base != NULL; rti++) | |
618 | for (i = 0; i < rti->nelt; i++) | |
619 | if (fread ((char *)rti->base + rti->stride * i, | |
620 | sizeof (void *), 1, f) != 1) | |
40fecdd6 | 621 | fatal_error (input_location, "can%'t read PCH file: %m"); |
17211ab5 | 622 | |
17211ab5 | 623 | if (fread (&mmi, sizeof (mmi), 1, f) != 1) |
40fecdd6 | 624 | fatal_error (input_location, "can%'t read PCH file: %m"); |
20c1dc5e | 625 | |
4d0c31e6 RH |
626 | result = host_hooks.gt_pch_use_address (mmi.preferred_base, mmi.size, |
627 | fileno (f), mmi.offset); | |
628 | if (result < 0) | |
40fecdd6 | 629 | fatal_error (input_location, "had to relocate PCH"); |
4d0c31e6 | 630 | if (result == 0) |
18c81520 | 631 | { |
4d0c31e6 RH |
632 | if (fseek (f, mmi.offset, SEEK_SET) != 0 |
633 | || fread (mmi.preferred_base, mmi.size, 1, f) != 1) | |
40fecdd6 | 634 | fatal_error (input_location, "can%'t read PCH file: %m"); |
4d0c31e6 RH |
635 | } |
636 | else if (fseek (f, mmi.offset + mmi.size, SEEK_SET) != 0) | |
40fecdd6 | 637 | fatal_error (input_location, "can%'t read PCH file: %m"); |
8eb6a092 | 638 | |
4d0c31e6 | 639 | ggc_pch_read (f, mmi.preferred_base); |
18c81520 | 640 | |
4d0c31e6 RH |
641 | gt_pch_restore_stringpool (); |
642 | } | |
18c81520 | 643 | |
4d0c31e6 RH |
644 | /* Default version of HOST_HOOKS_GT_PCH_GET_ADDRESS when mmap is not present. |
645 | Select no address whatsoever, and let gt_pch_save choose what it will with | |
646 | malloc, presumably. */ | |
ee0d75ef | 647 | |
4d0c31e6 RH |
648 | void * |
649 | default_gt_pch_get_address (size_t size ATTRIBUTE_UNUSED, | |
650 | int fd ATTRIBUTE_UNUSED) | |
651 | { | |
652 | return NULL; | |
653 | } | |
ee0d75ef | 654 | |
4d0c31e6 RH |
655 | /* Default version of HOST_HOOKS_GT_PCH_USE_ADDRESS when mmap is not present. |
656 | Allocate SIZE bytes with malloc. Return 0 if the address we got is the | |
657 | same as base, indicating that the memory has been allocated but needs to | |
658 | be read in from the file. Return -1 if the address differs, to relocation | |
659 | of the PCH file would be required. */ | |
660 | ||
661 | int | |
662 | default_gt_pch_use_address (void *base, size_t size, int fd ATTRIBUTE_UNUSED, | |
663 | size_t offset ATTRIBUTE_UNUSED) | |
664 | { | |
665 | void *addr = xmalloc (size); | |
666 | return (addr == base) - 1; | |
667 | } | |
ee0d75ef | 668 | |
90aa6719 DS |
669 | /* Default version of HOST_HOOKS_GT_PCH_GET_ADDRESS. Return the |
670 | alignment required for allocating virtual memory. Usually this is the | |
671 | same as pagesize. */ | |
672 | ||
673 | size_t | |
674 | default_gt_pch_alloc_granularity (void) | |
675 | { | |
c3284718 | 676 | return getpagesize (); |
90aa6719 DS |
677 | } |
678 | ||
4d0c31e6 RH |
679 | #if HAVE_MMAP_FILE |
680 | /* Default version of HOST_HOOKS_GT_PCH_GET_ADDRESS when mmap is present. | |
681 | We temporarily allocate SIZE bytes, and let the kernel place the data | |
d1a6adeb | 682 | wherever it will. If it worked, that's our spot, if not we're likely |
4d0c31e6 | 683 | to be in trouble. */ |
8eb6a092 | 684 | |
4d0c31e6 RH |
685 | void * |
686 | mmap_gt_pch_get_address (size_t size, int fd) | |
687 | { | |
688 | void *ret; | |
18c81520 | 689 | |
4d0c31e6 RH |
690 | ret = mmap (NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); |
691 | if (ret == (void *) MAP_FAILED) | |
692 | ret = NULL; | |
693 | else | |
bba09b5a | 694 | munmap ((caddr_t) ret, size); |
3277221c | 695 | |
4d0c31e6 RH |
696 | return ret; |
697 | } | |
3277221c | 698 | |
4d0c31e6 | 699 | /* Default version of HOST_HOOKS_GT_PCH_USE_ADDRESS when mmap is present. |
b8698a0f | 700 | Map SIZE bytes of FD+OFFSET at BASE. Return 1 if we succeeded at |
4d0c31e6 | 701 | mapping the data at BASE, -1 if we couldn't. |
20c1dc5e | 702 | |
4d0c31e6 RH |
703 | This version assumes that the kernel honors the START operand of mmap |
704 | even without MAP_FIXED if START through START+SIZE are not currently | |
705 | mapped with something. */ | |
17211ab5 | 706 | |
4d0c31e6 RH |
707 | int |
708 | mmap_gt_pch_use_address (void *base, size_t size, int fd, size_t offset) | |
709 | { | |
710 | void *addr; | |
17211ab5 | 711 | |
4d0c31e6 RH |
712 | /* We're called with size == 0 if we're not planning to load a PCH |
713 | file at all. This allows the hook to free any static space that | |
714 | we might have allocated at link time. */ | |
715 | if (size == 0) | |
716 | return -1; | |
717 | ||
bba09b5a | 718 | addr = mmap ((caddr_t) base, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, |
4d0c31e6 RH |
719 | fd, offset); |
720 | ||
721 | return addr == base ? 1 : -1; | |
3277221c | 722 | } |
4d0c31e6 | 723 | #endif /* HAVE_MMAP_FILE */ |
9ac121af | 724 | |
e4dfaf72 LB |
725 | #if !defined ENABLE_GC_CHECKING && !defined ENABLE_GC_ALWAYS_COLLECT |
726 | ||
d37e6b50 | 727 | /* Modify the bound based on rlimits. */ |
16226f1e | 728 | static double |
20c1dc5e | 729 | ggc_rlimit_bound (double limit) |
16226f1e KG |
730 | { |
731 | #if defined(HAVE_GETRLIMIT) | |
732 | struct rlimit rlim; | |
d37e6b50 GK |
733 | # if defined (RLIMIT_AS) |
734 | /* RLIMIT_AS is what POSIX says is the limit on mmap. Presumably | |
735 | any OS which has RLIMIT_AS also has a working mmap that GCC will use. */ | |
736 | if (getrlimit (RLIMIT_AS, &rlim) == 0 | |
a2581175 | 737 | && rlim.rlim_cur != (rlim_t) RLIM_INFINITY |
16226f1e KG |
738 | && rlim.rlim_cur < limit) |
739 | limit = rlim.rlim_cur; | |
d37e6b50 GK |
740 | # elif defined (RLIMIT_DATA) |
741 | /* ... but some older OSs bound mmap based on RLIMIT_DATA, or we | |
742 | might be on an OS that has a broken mmap. (Others don't bound | |
743 | mmap at all, apparently.) */ | |
16226f1e | 744 | if (getrlimit (RLIMIT_DATA, &rlim) == 0 |
a2581175 | 745 | && rlim.rlim_cur != (rlim_t) RLIM_INFINITY |
d37e6b50 GK |
746 | && rlim.rlim_cur < limit |
747 | /* Darwin has this horribly bogus default setting of | |
748 | RLIMIT_DATA, to 6144Kb. No-one notices because RLIMIT_DATA | |
749 | appears to be ignored. Ignore such silliness. If a limit | |
750 | this small was actually effective for mmap, GCC wouldn't even | |
751 | start up. */ | |
752 | && rlim.rlim_cur >= 8 * 1024 * 1024) | |
16226f1e | 753 | limit = rlim.rlim_cur; |
d37e6b50 | 754 | # endif /* RLIMIT_AS or RLIMIT_DATA */ |
16226f1e KG |
755 | #endif /* HAVE_GETRLIMIT */ |
756 | ||
757 | return limit; | |
758 | } | |
759 | ||
9ac121af | 760 | /* Heuristic to set a default for GGC_MIN_EXPAND. */ |
e4dfaf72 | 761 | static int |
20c1dc5e | 762 | ggc_min_expand_heuristic (void) |
9ac121af | 763 | { |
c3284718 | 764 | double min_expand = physmem_total (); |
16226f1e KG |
765 | |
766 | /* Adjust for rlimits. */ | |
767 | min_expand = ggc_rlimit_bound (min_expand); | |
20c1dc5e | 768 | |
9ac121af KG |
769 | /* The heuristic is a percentage equal to 30% + 70%*(RAM/1GB), yielding |
770 | a lower bound of 30% and an upper bound of 100% (when RAM >= 1GB). */ | |
771 | min_expand /= 1024*1024*1024; | |
772 | min_expand *= 70; | |
773 | min_expand = MIN (min_expand, 70); | |
774 | min_expand += 30; | |
775 | ||
776 | return min_expand; | |
777 | } | |
778 | ||
779 | /* Heuristic to set a default for GGC_MIN_HEAPSIZE. */ | |
e4dfaf72 | 780 | static int |
20c1dc5e | 781 | ggc_min_heapsize_heuristic (void) |
9ac121af | 782 | { |
c3284718 | 783 | double phys_kbytes = physmem_total (); |
d37e6b50 | 784 | double limit_kbytes = ggc_rlimit_bound (phys_kbytes * 2); |
16226f1e | 785 | |
d37e6b50 GK |
786 | phys_kbytes /= 1024; /* Convert to Kbytes. */ |
787 | limit_kbytes /= 1024; | |
20c1dc5e | 788 | |
9ac121af KG |
789 | /* The heuristic is RAM/8, with a lower bound of 4M and an upper |
790 | bound of 128M (when RAM >= 1GB). */ | |
d37e6b50 GK |
791 | phys_kbytes /= 8; |
792 | ||
793 | #if defined(HAVE_GETRLIMIT) && defined (RLIMIT_RSS) | |
b8698a0f | 794 | /* Try not to overrun the RSS limit while doing garbage collection. |
d37e6b50 GK |
795 | The RSS limit is only advisory, so no margin is subtracted. */ |
796 | { | |
797 | struct rlimit rlim; | |
798 | if (getrlimit (RLIMIT_RSS, &rlim) == 0 | |
799 | && rlim.rlim_cur != (rlim_t) RLIM_INFINITY) | |
800 | phys_kbytes = MIN (phys_kbytes, rlim.rlim_cur / 1024); | |
801 | } | |
802 | # endif | |
803 | ||
804 | /* Don't blindly run over our data limit; do GC at least when the | |
ded5f8f4 NF |
805 | *next* GC would be within 20Mb of the limit or within a quarter of |
806 | the limit, whichever is larger. If GCC does hit the data limit, | |
807 | compilation will fail, so this tries to be conservative. */ | |
808 | limit_kbytes = MAX (0, limit_kbytes - MAX (limit_kbytes / 4, 20 * 1024)); | |
a9429e29 | 809 | limit_kbytes = (limit_kbytes * 100) / (110 + ggc_min_expand_heuristic ()); |
d37e6b50 GK |
810 | phys_kbytes = MIN (phys_kbytes, limit_kbytes); |
811 | ||
812 | phys_kbytes = MAX (phys_kbytes, 4 * 1024); | |
813 | phys_kbytes = MIN (phys_kbytes, 128 * 1024); | |
9ac121af | 814 | |
d37e6b50 | 815 | return phys_kbytes; |
9ac121af | 816 | } |
e4dfaf72 | 817 | #endif |
9ac121af KG |
818 | |
819 | void | |
20c1dc5e | 820 | init_ggc_heuristics (void) |
9ac121af | 821 | { |
d85a0aae | 822 | #if !defined ENABLE_GC_CHECKING && !defined ENABLE_GC_ALWAYS_COLLECT |
128dc8e2 JM |
823 | set_default_param_value (GGC_MIN_EXPAND, ggc_min_expand_heuristic ()); |
824 | set_default_param_value (GGC_MIN_HEAPSIZE, ggc_min_heapsize_heuristic ()); | |
9ac121af KG |
825 | #endif |
826 | } | |
b9dcdee4 | 827 | |
2d44c7de ML |
828 | /* GGC memory usage. */ |
829 | struct ggc_usage: public mem_usage | |
b9dcdee4 | 830 | { |
2d44c7de ML |
831 | /* Default constructor. */ |
832 | ggc_usage (): m_freed (0), m_collected (0), m_overhead (0) {} | |
833 | /* Constructor. */ | |
834 | ggc_usage (size_t allocated, size_t times, size_t peak, | |
835 | size_t freed, size_t collected, size_t overhead) | |
836 | : mem_usage (allocated, times, peak), | |
837 | m_freed (freed), m_collected (collected), m_overhead (overhead) {} | |
b9dcdee4 | 838 | |
b27b31dc ML |
839 | /* Equality operator. */ |
840 | inline bool | |
841 | operator== (const ggc_usage &second) const | |
842 | { | |
843 | return (get_balance () == second.get_balance () | |
844 | && m_peak == second.m_peak | |
845 | && m_times == second.m_times); | |
846 | } | |
847 | ||
2d44c7de | 848 | /* Comparison operator. */ |
80a4fe78 ML |
849 | inline bool |
850 | operator< (const ggc_usage &second) const | |
2d44c7de | 851 | { |
b27b31dc ML |
852 | if (*this == second) |
853 | return false; | |
854 | ||
2d44c7de ML |
855 | return (get_balance () == second.get_balance () ? |
856 | (m_peak == second.m_peak ? m_times < second.m_times | |
857 | : m_peak < second.m_peak) | |
858 | : get_balance () < second.get_balance ()); | |
859 | } | |
b9dcdee4 | 860 | |
2d44c7de | 861 | /* Register overhead of ALLOCATED and OVERHEAD bytes. */ |
80a4fe78 ML |
862 | inline void |
863 | register_overhead (size_t allocated, size_t overhead) | |
2d44c7de ML |
864 | { |
865 | m_allocated += allocated; | |
866 | m_overhead += overhead; | |
867 | m_times++; | |
868 | } | |
b9dcdee4 | 869 | |
2d44c7de | 870 | /* Release overhead of SIZE bytes. */ |
80a4fe78 ML |
871 | inline void |
872 | release_overhead (size_t size) | |
2d44c7de ML |
873 | { |
874 | m_freed += size; | |
875 | } | |
b9dcdee4 | 876 | |
2d44c7de | 877 | /* Sum the usage with SECOND usage. */ |
80a4fe78 ML |
878 | ggc_usage |
879 | operator+ (const ggc_usage &second) | |
2d44c7de ML |
880 | { |
881 | return ggc_usage (m_allocated + second.m_allocated, | |
882 | m_times + second.m_times, | |
883 | m_peak + second.m_peak, | |
884 | m_freed + second.m_freed, | |
885 | m_collected + second.m_collected, | |
886 | m_overhead + second.m_overhead); | |
887 | } | |
b9dcdee4 | 888 | |
2d44c7de | 889 | /* Dump usage with PREFIX, where TOTAL is sum of all rows. */ |
80a4fe78 ML |
890 | inline void |
891 | dump (const char *prefix, ggc_usage &total) const | |
2d44c7de ML |
892 | { |
893 | long balance = get_balance (); | |
894 | fprintf (stderr, | |
895 | "%-48s %10li:%5.1f%%%10li:%5.1f%%" | |
896 | "%10li:%5.1f%%%10li:%5.1f%%%10li\n", | |
897 | prefix, (long)m_collected, | |
898 | get_percent (m_collected, total.m_collected), | |
899 | (long)m_freed, get_percent (m_freed, total.m_freed), | |
900 | (long)balance, get_percent (balance, total.get_balance ()), | |
901 | (long)m_overhead, get_percent (m_overhead, total.m_overhead), | |
902 | (long)m_times); | |
903 | } | |
4a8fb1a1 | 904 | |
2d44c7de | 905 | /* Dump usage coupled to LOC location, where TOTAL is sum of all rows. */ |
80a4fe78 ML |
906 | inline void |
907 | dump (mem_location *loc, ggc_usage &total) const | |
2d44c7de | 908 | { |
ac059261 | 909 | char *location_string = loc->to_string (); |
07724022 | 910 | |
ac059261 ML |
911 | dump (location_string, total); |
912 | ||
913 | free (location_string); | |
2d44c7de | 914 | } |
4a8fb1a1 | 915 | |
2d44c7de | 916 | /* Dump footer. */ |
80a4fe78 ML |
917 | inline void |
918 | dump_footer () | |
2d44c7de ML |
919 | { |
920 | print_dash_line (); | |
921 | dump ("Total", *this); | |
922 | print_dash_line (); | |
923 | } | |
07724022 | 924 | |
2d44c7de | 925 | /* Get balance which is GGC allocation leak. */ |
80a4fe78 ML |
926 | inline long |
927 | get_balance () const | |
2d44c7de ML |
928 | { |
929 | return m_allocated + m_overhead - m_collected - m_freed; | |
930 | } | |
07724022 | 931 | |
2d44c7de | 932 | typedef std::pair<mem_location *, ggc_usage *> mem_pair_t; |
07724022 | 933 | |
2d44c7de | 934 | /* Compare wrapper used by qsort method. */ |
80a4fe78 ML |
935 | static int |
936 | compare (const void *first, const void *second) | |
2d44c7de ML |
937 | { |
938 | const mem_pair_t f = *(const mem_pair_t *)first; | |
939 | const mem_pair_t s = *(const mem_pair_t *)second; | |
4a8fb1a1 | 940 | |
b27b31dc ML |
941 | if (*f.second == *s.second) |
942 | return 0; | |
943 | ||
944 | return *f.second < *s.second ? 1 : -1; | |
2d44c7de ML |
945 | } |
946 | ||
947 | /* Compare rows in final GGC summary dump. */ | |
80a4fe78 ML |
948 | static int |
949 | compare_final (const void *first, const void *second) | |
950 | { | |
951 | typedef std::pair<mem_location *, ggc_usage *> mem_pair_t; | |
2d44c7de ML |
952 | |
953 | const ggc_usage *f = ((const mem_pair_t *)first)->second; | |
954 | const ggc_usage *s = ((const mem_pair_t *)second)->second; | |
955 | ||
956 | size_t a = f->m_allocated + f->m_overhead - f->m_freed; | |
957 | size_t b = s->m_allocated + s->m_overhead - s->m_freed; | |
958 | ||
959 | return a == b ? 0 : (a < b ? 1 : -1); | |
960 | } | |
961 | ||
962 | /* Dump header with NAME. */ | |
80a4fe78 ML |
963 | static inline void |
964 | dump_header (const char *name) | |
2d44c7de ML |
965 | { |
966 | fprintf (stderr, "%-48s %11s%17s%17s%16s%17s\n", name, "Garbage", "Freed", | |
967 | "Leak", "Overhead", "Times"); | |
968 | print_dash_line (); | |
969 | } | |
970 | ||
971 | /* Freed memory in bytes. */ | |
972 | size_t m_freed; | |
973 | /* Collected memory in bytes. */ | |
974 | size_t m_collected; | |
975 | /* Overhead memory in bytes. */ | |
976 | size_t m_overhead; | |
977 | }; | |
978 | ||
979 | /* GCC memory description. */ | |
980 | static mem_alloc_description<ggc_usage> ggc_mem_desc; | |
981 | ||
982 | /* Dump per-site memory statistics. */ | |
b9dcdee4 | 983 | |
d1a6adeb | 984 | void |
2d44c7de | 985 | dump_ggc_loc_statistics (bool final) |
b9dcdee4 | 986 | { |
2d44c7de ML |
987 | if (! GATHER_STATISTICS) |
988 | return; | |
b9dcdee4 | 989 | |
2d44c7de ML |
990 | ggc_force_collect = true; |
991 | ggc_collect (); | |
992 | ||
643e0a30 | 993 | ggc_mem_desc.dump (GGC_ORIGIN, final ? ggc_usage::compare_final : NULL); |
2d44c7de ML |
994 | |
995 | ggc_force_collect = false; | |
07724022 JH |
996 | } |
997 | ||
2d44c7de | 998 | /* Record ALLOCATED and OVERHEAD bytes to descriptor NAME:LINE (FUNCTION). */ |
07724022 | 999 | void |
2d44c7de | 1000 | ggc_record_overhead (size_t allocated, size_t overhead, void *ptr MEM_STAT_DECL) |
07724022 | 1001 | { |
643e0a30 | 1002 | ggc_usage *usage = ggc_mem_desc.register_descriptor (ptr, GGC_ORIGIN, false |
2d44c7de ML |
1003 | FINAL_PASS_MEM_STAT); |
1004 | ||
1005 | ggc_mem_desc.register_object_overhead (usage, allocated + overhead, ptr); | |
1006 | usage->register_overhead (allocated, overhead); | |
07724022 JH |
1007 | } |
1008 | ||
1009 | /* Notice that the pointer has been freed. */ | |
83f676b3 RS |
1010 | void |
1011 | ggc_free_overhead (void *ptr) | |
07724022 | 1012 | { |
2d44c7de | 1013 | ggc_mem_desc.release_object_overhead (ptr); |
a5573239 JH |
1014 | } |
1015 | ||
2d44c7de ML |
1016 | /* After live values has been marked, walk all recorded pointers and see if |
1017 | they are still live. */ | |
83f676b3 | 1018 | void |
2d44c7de | 1019 | ggc_prune_overhead_list (void) |
b9dcdee4 | 1020 | { |
2d44c7de | 1021 | typedef hash_map<const void *, std::pair<ggc_usage *, size_t > > map_t; |
b9dcdee4 | 1022 | |
2d44c7de | 1023 | map_t::iterator it = ggc_mem_desc.m_reverse_object_map->begin (); |
7aa6d18a | 1024 | |
2d44c7de ML |
1025 | for (; it != ggc_mem_desc.m_reverse_object_map->end (); ++it) |
1026 | if (!ggc_marked_p ((*it).first)) | |
1027 | (*it).second.first->m_collected += (*it).second.second; | |
07724022 | 1028 | |
2d44c7de ML |
1029 | delete ggc_mem_desc.m_reverse_object_map; |
1030 | ggc_mem_desc.m_reverse_object_map = new map_t (13, false, false); | |
b9dcdee4 | 1031 | } |