1 /* Simple garbage collection for the GNU compiler.
2 Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4 This file is part of GCC.
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
8 Software Foundation; either version 2, or (at your option) any later
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
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 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 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21 /* Generic garbage collection (GC) functions and data, not specific to
22 any particular GC implementation. */
26 #include "coretypes.h"
32 # include <sys/mman.h>
35 #ifdef ENABLE_VALGRIND_CHECKING
38 /* Avoid #ifdef:s when we can help it. */
39 #define VALGRIND_DISCARD(x)
42 /* Statistics about the allocation. */
43 static ggc_statistics
*ggc_stats
;
45 struct traversal_state
;
47 static int ggc_htab_delete
PARAMS ((void **, void *));
48 static hashval_t saving_htab_hash
PARAMS ((const PTR
));
49 static int saving_htab_eq
PARAMS ((const PTR
, const PTR
));
50 static int call_count
PARAMS ((void **, void *));
51 static int call_alloc
PARAMS ((void **, void *));
52 static int compare_ptr_data
PARAMS ((const void *, const void *));
53 static void relocate_ptrs
PARAMS ((void *, void *));
54 static void write_pch_globals
PARAMS ((const struct ggc_root_tab
* const *tab
,
55 struct traversal_state
*state
));
57 /* Maintain global roots that are preserved during GC. */
59 /* Process a slot of an htab by deleting it if it has not been marked. */
62 ggc_htab_delete (slot
, info
)
66 const struct ggc_cache_tab
*r
= (const struct ggc_cache_tab
*) info
;
68 if (! (*r
->marked_p
) (*slot
))
69 htab_clear_slot (*r
->base
, slot
);
76 /* Iterate through all registered roots and mark each element. */
81 const struct ggc_root_tab
*const *rt
;
82 const struct ggc_root_tab
*rti
;
83 const struct ggc_cache_tab
*const *ct
;
84 const struct ggc_cache_tab
*cti
;
87 for (rt
= gt_ggc_deletable_rtab
; *rt
; rt
++)
88 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
89 memset (rti
->base
, 0, rti
->stride
);
91 for (rt
= gt_ggc_rtab
; *rt
; rt
++)
92 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
93 for (i
= 0; i
< rti
->nelt
; i
++)
94 (*rti
->cb
)(*(void **)((char *)rti
->base
+ rti
->stride
* i
));
96 ggc_mark_stringpool ();
98 /* Now scan all hash tables that have objects which are to be deleted if
99 they are not already marked. */
100 for (ct
= gt_ggc_cache_rtab
; *ct
; ct
++)
101 for (cti
= *ct
; cti
->base
!= NULL
; cti
++)
104 ggc_set_mark (*cti
->base
);
105 htab_traverse (*cti
->base
, ggc_htab_delete
, (PTR
) cti
);
106 ggc_set_mark ((*cti
->base
)->entries
);
110 /* Allocate a block of memory, then clear it. */
112 ggc_alloc_cleared (size
)
115 void *buf
= ggc_alloc (size
);
116 memset (buf
, 0, size
);
120 /* Resize a block of memory, possibly re-allocating it. */
122 ggc_realloc (x
, size
)
130 return ggc_alloc (size
);
132 old_size
= ggc_get_size (x
);
133 if (size
<= old_size
)
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
142 old_size as reachable, but that would lose tracking of writes
143 after the end of the object (by small offsets). Discard the
144 handle to avoid handle leak. */
145 VALGRIND_DISCARD (VALGRIND_MAKE_NOACCESS ((char *) x
+ size
,
147 VALGRIND_DISCARD (VALGRIND_MAKE_READABLE (x
, size
));
151 r
= ggc_alloc (size
);
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. */
157 VALGRIND_DISCARD (VALGRIND_MAKE_READABLE (x
, old_size
));
159 memcpy (r
, x
, old_size
);
161 /* The old object is not supposed to be used anymore. */
162 VALGRIND_DISCARD (VALGRIND_MAKE_NOACCESS (x
, old_size
));
167 /* Like ggc_alloc_cleared, but performs a multiplication. */
172 return ggc_alloc_cleared (s1
* s2
);
175 /* These are for splay_tree_new_ggc. */
177 ggc_splay_alloc (sz
, nl
)
183 return ggc_alloc (sz
);
187 ggc_splay_dont_free (x
, nl
)
188 PTR x ATTRIBUTE_UNUSED
;
195 /* Print statistics that are independent of the collector in use. */
196 #define SCALE(x) ((unsigned long) ((x) < 1024*10 \
198 : ((x) < 1024*1024*10 \
200 : (x) / (1024*1024))))
201 #define LABEL(x) ((x) < 1024*10 ? ' ' : ((x) < 1024*1024*10 ? 'k' : 'M'))
204 ggc_print_common_statistics (stream
, stats
)
205 FILE *stream ATTRIBUTE_UNUSED
;
206 ggc_statistics
*stats
;
208 /* Set the pointer so that during collection we will actually gather
212 /* Then do one collection to fill in the statistics. */
215 /* At present, we don't really gather any interesting statistics. */
217 /* Don't gather statistics any more. */
221 /* Functions for saving and restoring GCable memory to disk. */
223 static htab_t saving_htab
;
228 void *note_ptr_cookie
;
229 gt_note_pointers note_ptr_fn
;
230 gt_handle_reorder reorder_fn
;
235 #define POINTER_HASH(x) (hashval_t)((long)x >> 3)
237 /* Register an object in the hash table. */
240 gt_pch_note_object (obj
, note_ptr_cookie
, note_ptr_fn
)
242 void *note_ptr_cookie
;
243 gt_note_pointers note_ptr_fn
;
245 struct ptr_data
**slot
;
247 if (obj
== NULL
|| obj
== (void *) 1)
250 slot
= (struct ptr_data
**)
251 htab_find_slot_with_hash (saving_htab
, obj
, POINTER_HASH (obj
),
255 if ((*slot
)->note_ptr_fn
!= note_ptr_fn
256 || (*slot
)->note_ptr_cookie
!= note_ptr_cookie
)
261 *slot
= xcalloc (sizeof (struct ptr_data
), 1);
263 (*slot
)->note_ptr_fn
= note_ptr_fn
;
264 (*slot
)->note_ptr_cookie
= note_ptr_cookie
;
265 if (note_ptr_fn
== gt_pch_p_S
)
266 (*slot
)->size
= strlen (obj
) + 1;
268 (*slot
)->size
= ggc_get_size (obj
);
272 /* Register an object in the hash table. */
275 gt_pch_note_reorder (obj
, note_ptr_cookie
, reorder_fn
)
277 void *note_ptr_cookie
;
278 gt_handle_reorder reorder_fn
;
280 struct ptr_data
*data
;
282 if (obj
== NULL
|| obj
== (void *) 1)
285 data
= htab_find_with_hash (saving_htab
, obj
, POINTER_HASH (obj
));
287 || data
->note_ptr_cookie
!= note_ptr_cookie
)
290 data
->reorder_fn
= reorder_fn
;
293 /* Hash and equality functions for saving_htab, callbacks for htab_create. */
299 return POINTER_HASH (((struct ptr_data
*)p
)->obj
);
303 saving_htab_eq (p1
, p2
)
307 return ((struct ptr_data
*)p1
)->obj
== p2
;
310 /* Handy state for the traversal functions. */
312 struct traversal_state
315 struct ggc_pch_data
*d
;
317 struct ptr_data
**ptrs
;
321 /* Callbacks for htab_traverse. */
324 call_count (slot
, state_p
)
328 struct ptr_data
*d
= (struct ptr_data
*)*slot
;
329 struct traversal_state
*state
= (struct traversal_state
*)state_p
;
331 ggc_pch_count_object (state
->d
, d
->obj
, d
->size
);
337 call_alloc (slot
, state_p
)
341 struct ptr_data
*d
= (struct ptr_data
*)*slot
;
342 struct traversal_state
*state
= (struct traversal_state
*)state_p
;
344 d
->new_addr
= ggc_pch_alloc_object (state
->d
, d
->obj
, d
->size
);
345 state
->ptrs
[state
->ptrs_i
++] = d
;
349 /* Callback for qsort. */
352 compare_ptr_data (p1_p
, p2_p
)
356 struct ptr_data
*p1
= *(struct ptr_data
*const *)p1_p
;
357 struct ptr_data
*p2
= *(struct ptr_data
*const *)p2_p
;
358 return (((size_t)p1
->new_addr
> (size_t)p2
->new_addr
)
359 - ((size_t)p1
->new_addr
< (size_t)p2
->new_addr
));
362 /* Callbacks for note_ptr_fn. */
365 relocate_ptrs (ptr_p
, state_p
)
369 void **ptr
= (void **)ptr_p
;
370 struct traversal_state
*state ATTRIBUTE_UNUSED
371 = (struct traversal_state
*)state_p
;
372 struct ptr_data
*result
;
374 if (*ptr
== NULL
|| *ptr
== (void *)1)
377 result
= htab_find_with_hash (saving_htab
, *ptr
, POINTER_HASH (*ptr
));
380 *ptr
= result
->new_addr
;
383 /* Write out, after relocation, the pointers in TAB. */
385 write_pch_globals (tab
, state
)
386 const struct ggc_root_tab
* const *tab
;
387 struct traversal_state
*state
;
389 const struct ggc_root_tab
*const *rt
;
390 const struct ggc_root_tab
*rti
;
393 for (rt
= tab
; *rt
; rt
++)
394 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
395 for (i
= 0; i
< rti
->nelt
; i
++)
397 void *ptr
= *(void **)((char *)rti
->base
+ rti
->stride
* i
);
398 struct ptr_data
*new_ptr
;
399 if (ptr
== NULL
|| ptr
== (void *)1)
401 if (fwrite (&ptr
, sizeof (void *), 1, state
->f
)
403 fatal_io_error ("can't write PCH file");
407 new_ptr
= htab_find_with_hash (saving_htab
, ptr
,
409 if (fwrite (&new_ptr
->new_addr
, sizeof (void *), 1, state
->f
)
411 fatal_io_error ("can't write PCH file");
416 /* Hold the information we need to mmap the file back in. */
422 void *preferred_base
;
425 /* Write out the state of the compiler to F. */
431 const struct ggc_root_tab
*const *rt
;
432 const struct ggc_root_tab
*rti
;
434 struct traversal_state state
;
435 char *this_object
= NULL
;
436 size_t this_object_size
= 0;
437 struct mmap_info mmi
;
438 size_t page_size
= getpagesize();
440 gt_pch_save_stringpool ();
442 saving_htab
= htab_create (50000, saving_htab_hash
, saving_htab_eq
, free
);
444 for (rt
= gt_ggc_rtab
; *rt
; rt
++)
445 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
446 for (i
= 0; i
< rti
->nelt
; i
++)
447 (*rti
->pchw
)(*(void **)((char *)rti
->base
+ rti
->stride
* i
));
449 for (rt
= gt_pch_cache_rtab
; *rt
; rt
++)
450 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
451 for (i
= 0; i
< rti
->nelt
; i
++)
452 (*rti
->pchw
)(*(void **)((char *)rti
->base
+ rti
->stride
* i
));
454 /* Prepare the objects for writing, determine addresses and such. */
456 state
.d
= init_ggc_pch();
458 htab_traverse (saving_htab
, call_count
, &state
);
460 mmi
.size
= ggc_pch_total_size (state
.d
);
462 /* Try to arrange things so that no relocation is necessary,
463 but don't try very hard. On most platforms, this will always work,
464 and on the rest it's a lot of work to do better. */
466 mmi
.preferred_base
= mmap (NULL
, mmi
.size
,
467 PROT_READ
| PROT_WRITE
, MAP_PRIVATE
,
468 fileno (state
.f
), 0);
469 if (mmi
.preferred_base
== (void *)-1)
470 mmi
.preferred_base
= NULL
;
472 munmap (mmi
.preferred_base
, mmi
.size
);
473 #else /* HAVE_MMAP_FILE */
474 mmi
.preferred_base
= NULL
;
475 #endif /* HAVE_MMAP_FILE */
477 ggc_pch_this_base (state
.d
, mmi
.preferred_base
);
479 state
.ptrs
= xmalloc (state
.count
* sizeof (*state
.ptrs
));
481 htab_traverse (saving_htab
, call_alloc
, &state
);
482 qsort (state
.ptrs
, state
.count
, sizeof (*state
.ptrs
), compare_ptr_data
);
484 /* Write out all the scalar variables. */
485 for (rt
= gt_pch_scalar_rtab
; *rt
; rt
++)
486 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
487 if (fwrite (rti
->base
, rti
->stride
, 1, f
) != 1)
488 fatal_io_error ("can't write PCH file");
490 /* Write out all the global pointers, after translation. */
491 write_pch_globals (gt_ggc_rtab
, &state
);
492 write_pch_globals (gt_pch_cache_rtab
, &state
);
494 ggc_pch_prepare_write (state
.d
, state
.f
);
496 /* Pad the PCH file so that the mmaped area starts on a page boundary. */
499 o
= ftell (state
.f
) + sizeof (mmi
);
501 fatal_io_error ("can't get position in PCH file");
502 mmi
.offset
= page_size
- o
% page_size
;
503 if (mmi
.offset
== page_size
)
507 if (fwrite (&mmi
, sizeof (mmi
), 1, state
.f
) != 1)
508 fatal_io_error ("can't write PCH file");
510 && fseek (state
.f
, mmi
.offset
, SEEK_SET
) != 0)
511 fatal_io_error ("can't write padding to PCH file");
513 /* Actually write out the objects. */
514 for (i
= 0; i
< state
.count
; i
++)
516 if (this_object_size
< state
.ptrs
[i
]->size
)
518 this_object_size
= state
.ptrs
[i
]->size
;
519 this_object
= xrealloc (this_object
, this_object_size
);
521 memcpy (this_object
, state
.ptrs
[i
]->obj
, state
.ptrs
[i
]->size
);
522 if (state
.ptrs
[i
]->reorder_fn
!= NULL
)
523 state
.ptrs
[i
]->reorder_fn (state
.ptrs
[i
]->obj
,
524 state
.ptrs
[i
]->note_ptr_cookie
,
525 relocate_ptrs
, &state
);
526 state
.ptrs
[i
]->note_ptr_fn (state
.ptrs
[i
]->obj
,
527 state
.ptrs
[i
]->note_ptr_cookie
,
528 relocate_ptrs
, &state
);
529 ggc_pch_write_object (state
.d
, state
.f
, state
.ptrs
[i
]->obj
,
530 state
.ptrs
[i
]->new_addr
, state
.ptrs
[i
]->size
);
531 if (state
.ptrs
[i
]->note_ptr_fn
!= gt_pch_p_S
)
532 memcpy (state
.ptrs
[i
]->obj
, this_object
, state
.ptrs
[i
]->size
);
534 ggc_pch_finish (state
.d
, state
.f
);
537 htab_delete (saving_htab
);
540 /* Read the state of the compiler back in from F. */
546 const struct ggc_root_tab
*const *rt
;
547 const struct ggc_root_tab
*rti
;
549 struct mmap_info mmi
;
552 /* Delete any deletable objects. This makes ggc_pch_read much
553 faster, as it can be sure that no GCable objects remain other
554 than the ones just read in. */
555 for (rt
= gt_ggc_deletable_rtab
; *rt
; rt
++)
556 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
557 memset (rti
->base
, 0, rti
->stride
);
559 /* Read in all the scalar variables. */
560 for (rt
= gt_pch_scalar_rtab
; *rt
; rt
++)
561 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
562 if (fread (rti
->base
, rti
->stride
, 1, f
) != 1)
563 fatal_io_error ("can't read PCH file");
565 /* Read in all the global pointers, in 6 easy loops. */
566 for (rt
= gt_ggc_rtab
; *rt
; rt
++)
567 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
568 for (i
= 0; i
< rti
->nelt
; i
++)
569 if (fread ((char *)rti
->base
+ rti
->stride
* i
,
570 sizeof (void *), 1, f
) != 1)
571 fatal_io_error ("can't read PCH file");
573 for (rt
= gt_pch_cache_rtab
; *rt
; rt
++)
574 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
575 for (i
= 0; i
< rti
->nelt
; i
++)
576 if (fread ((char *)rti
->base
+ rti
->stride
* i
,
577 sizeof (void *), 1, f
) != 1)
578 fatal_io_error ("can't read PCH file");
580 if (fread (&mmi
, sizeof (mmi
), 1, f
) != 1)
581 fatal_io_error ("can't read PCH file");
584 addr
= mmap (mmi
.preferred_base
, mmi
.size
,
585 PROT_READ
| PROT_WRITE
, MAP_PRIVATE
,
586 fileno (f
), mmi
.offset
);
590 if (addr
== (void *)-1)
592 addr
= xmalloc (mmi
.size
);
593 if (fseek (f
, mmi
.offset
, SEEK_SET
) != 0
594 || fread (&mmi
, mmi
.size
, 1, f
) != 1)
595 fatal_io_error ("can't read PCH file");
597 else if (fseek (f
, mmi
.offset
+ mmi
.size
, SEEK_SET
) != 0)
598 fatal_io_error ("can't read PCH file");
600 ggc_pch_read (f
, addr
);
602 if (addr
!= mmi
.preferred_base
)
604 for (rt
= gt_ggc_rtab
; *rt
; rt
++)
605 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
606 for (i
= 0; i
< rti
->nelt
; i
++)
608 char **ptr
= (char **)((char *)rti
->base
+ rti
->stride
* i
);
610 *ptr
+= (size_t)addr
- (size_t)mmi
.preferred_base
;
613 for (rt
= gt_pch_cache_rtab
; *rt
; rt
++)
614 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
615 for (i
= 0; i
< rti
->nelt
; i
++)
617 char **ptr
= (char **)((char *)rti
->base
+ rti
->stride
* i
);
619 *ptr
+= (size_t)addr
- (size_t)mmi
.preferred_base
;
622 sorry ("had to relocate PCH");
625 gt_pch_restore_stringpool ();