2 * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
3 * Copyright (c) 1991-1996 by Xerox Corporation. All rights reserved.
4 * Copyright (c) 1996-1999 by Silicon Graphics. All rights reserved.
5 * Copyright (c) 1999-2004 Hewlett-Packard Development Company, L.P.
7 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
8 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
10 * Permission is hereby granted to use or copy this program
11 * for any purpose, provided the above notices are retained on all copies.
12 * Permission to modify the code and to distribute modified code is granted,
13 * provided the above notices are retained, and a notice that the code was
14 * modified is included with the above copyright notice.
17 #include "private/gc_priv.h"
21 GC_INNER signed_word GC_bytes_found = 0;
22 /* Number of bytes of memory reclaimed */
23 /* minus the number of bytes originally */
24 /* on free lists which we had to drop. */
26 #if defined(PARALLEL_MARK)
27 GC_INNER word GC_fl_builder_count = 0;
28 /* Number of threads currently building free lists without */
29 /* holding GC lock. It is not safe to collect if this is */
31 #endif /* PARALLEL_MARK */
33 /* We defer printing of leaked objects until we're done with the GC */
34 /* cycle, since the routine for printing objects needs to run outside */
35 /* the collector, e.g. without the allocation lock. */
37 # define MAX_LEAKED 40
39 STATIC ptr_t GC_leaked[MAX_LEAKED] = { NULL };
40 STATIC unsigned GC_n_leaked = 0;
42 GC_INNER GC_bool GC_have_errors = FALSE;
44 GC_INLINE void GC_add_leaked(ptr_t leaked)
46 # ifndef SHORT_DBG_HDRS
47 if (GC_findleak_delay_free && !GC_check_leaked(leaked))
51 GC_have_errors = TRUE;
52 /* FIXME: Prevent adding an object while printing leaked ones. */
53 if (GC_n_leaked < MAX_LEAKED) {
54 GC_leaked[GC_n_leaked++] = leaked;
55 /* Make sure it's not reclaimed this cycle */
56 GC_set_mark_bit(leaked);
60 /* Print all objects on the list after printing any smashed objects. */
61 /* Clear both lists. Called without the allocation lock held. */
62 GC_INNER void GC_print_all_errors(void)
64 static GC_bool printing_errors = FALSE;
70 if (printing_errors) {
74 have_errors = GC_have_errors;
75 printing_errors = TRUE;
78 if (GC_debugging_started) {
79 GC_print_all_smashed();
84 for (i = 0; i < GC_n_leaked; ++i) {
85 ptr_t p = GC_leaked[i];
86 if (HDR(p) -> hb_obj_kind == PTRFREE) {
87 GC_err_printf("Leaked atomic object at ");
89 GC_err_printf("Leaked composite object at ");
100 # ifndef GC_ABORT_ON_LEAK
101 && GETENV("GC_ABORT_ON_LEAK") != NULL
104 ABORT("Leaked or smashed objects encountered");
107 printing_errors = FALSE;
116 /* Test whether a block is completely empty, i.e. contains no marked */
117 /* objects. This does not require the block to be in physical memory. */
118 GC_INNER GC_bool GC_block_empty(hdr *hhdr)
120 return (hhdr -> hb_n_marks == 0);
123 STATIC GC_bool GC_block_nearly_full(hdr *hhdr)
125 return (hhdr -> hb_n_marks > 7 * HBLK_OBJS(hhdr -> hb_sz)/8);
128 /* FIXME: This should perhaps again be specialized for USE_MARK_BYTES */
129 /* and USE_MARK_BITS cases. */
132 * Restore unmarked small objects in h of size sz to the object
133 * free list. Returns the new list.
134 * Clears unmarked objects. Sz is in bytes.
136 STATIC ptr_t GC_reclaim_clear(struct hblk *hbp, hdr *hhdr, size_t sz,
137 ptr_t list, signed_word *count)
141 signed_word n_bytes_found = 0;
143 GC_ASSERT(hhdr == GC_find_header((ptr_t)hbp));
144 GC_ASSERT(sz == hhdr -> hb_sz);
145 GC_ASSERT((sz & (BYTES_PER_WORD-1)) == 0);
146 p = (word *)(hbp->hb_body);
147 plim = (word *)(hbp->hb_body + HBLKSIZE - sz);
149 /* go through all words in block */
151 if( mark_bit_from_hdr(hhdr, bit_no) ) {
152 p = (word *)((ptr_t)p + sz);
155 /* object is available - put on list */
158 /* Clear object, advance p to next object in the process */
159 q = (word *)((ptr_t)p + sz);
160 # ifdef USE_MARK_BYTES
162 && !((word)p & (2 * sizeof(word) - 1)));
170 p++; /* Skip link field */
176 bit_no += MARK_BIT_OFFSET(sz);
178 *count += n_bytes_found;
182 /* The same thing, but don't clear objects: */
183 STATIC ptr_t GC_reclaim_uninit(struct hblk *hbp, hdr *hhdr, size_t sz,
184 ptr_t list, signed_word *count)
188 signed_word n_bytes_found = 0;
190 GC_ASSERT(sz == hhdr -> hb_sz);
191 p = (word *)(hbp->hb_body);
192 plim = (word *)((ptr_t)hbp + HBLKSIZE - sz);
194 /* go through all words in block */
196 if( !mark_bit_from_hdr(hhdr, bit_no) ) {
198 /* object is available - put on list */
202 p = (word *)((ptr_t)p + sz);
203 bit_no += MARK_BIT_OFFSET(sz);
205 *count += n_bytes_found;
209 /* Don't really reclaim objects, just check for unmarked ones: */
210 STATIC void GC_reclaim_check(struct hblk *hbp, hdr *hhdr, word sz)
214 GC_ASSERT(sz == hhdr -> hb_sz);
216 /* go through all words in block */
218 plim = p + HBLKSIZE - sz;
219 for (bit_no = 0; p <= plim; p += sz, bit_no += MARK_BIT_OFFSET(sz)) {
220 if (!mark_bit_from_hdr(hhdr, bit_no)) {
227 * Generic procedure to rebuild a free list in hbp.
228 * Also called directly from GC_malloc_many.
229 * Sz is now in bytes.
231 GC_INNER ptr_t GC_reclaim_generic(struct hblk * hbp, hdr *hhdr, size_t sz,
232 GC_bool init, ptr_t list,
237 GC_ASSERT(GC_find_header((ptr_t)hbp) == hhdr);
238 # ifndef GC_DISABLE_INCREMENTAL
239 GC_remove_protection(hbp, 1, (hhdr)->hb_descr == 0 /* Pointer-free? */);
241 if (init || GC_debugging_started) {
242 result = GC_reclaim_clear(hbp, hhdr, sz, list, count);
244 GC_ASSERT((hhdr)->hb_descr == 0 /* Pointer-free block */);
245 result = GC_reclaim_uninit(hbp, hhdr, sz, list, count);
247 if (IS_UNCOLLECTABLE(hhdr -> hb_obj_kind)) GC_set_hdr_marks(hhdr);
252 * Restore unmarked small objects in the block pointed to by hbp
253 * to the appropriate object free list.
254 * If entirely empty blocks are to be completely deallocated, then
255 * caller should perform that check.
257 STATIC void GC_reclaim_small_nonempty_block(struct hblk *hbp,
258 GC_bool report_if_found)
260 hdr *hhdr = HDR(hbp);
261 size_t sz = hhdr -> hb_sz;
262 struct obj_kind * ok = &GC_obj_kinds[hhdr -> hb_obj_kind];
263 void **flh = &(ok -> ok_freelist[BYTES_TO_GRANULES(sz)]);
265 hhdr -> hb_last_reclaimed = (unsigned short) GC_gc_no;
267 if (report_if_found) {
268 GC_reclaim_check(hbp, hhdr, sz);
270 *flh = GC_reclaim_generic(hbp, hhdr, sz, ok -> ok_init,
271 *flh, &GC_bytes_found);
276 * Restore an unmarked large object or an entirely empty blocks of small objects
277 * to the heap block free list.
278 * Otherwise enqueue the block for later processing
279 * by GC_reclaim_small_nonempty_block.
280 * If report_if_found is TRUE, then process any block immediately, and
281 * simply report free objects; do not actually reclaim them.
283 STATIC void GC_reclaim_block(struct hblk *hbp, word report_if_found)
285 hdr * hhdr = HDR(hbp);
286 size_t sz = hhdr -> hb_sz; /* size of objects in current block */
287 struct obj_kind * ok = &GC_obj_kinds[hhdr -> hb_obj_kind];
290 if( sz > MAXOBJBYTES ) { /* 1 big object */
291 if( !mark_bit_from_hdr(hhdr, 0) ) {
292 if (report_if_found) {
293 GC_add_leaked((ptr_t)hbp);
295 size_t blocks = OBJ_SZ_TO_BLOCKS(sz);
297 GC_large_allocd_bytes -= blocks * HBLKSIZE;
299 GC_bytes_found += sz;
303 if (hhdr -> hb_descr != 0) {
304 GC_composite_in_use += sz;
306 GC_atomic_in_use += sz;
310 GC_bool empty = GC_block_empty(hhdr);
311 # ifdef PARALLEL_MARK
312 /* Count can be low or one too high because we sometimes */
313 /* have to ignore decrements. Objects can also potentially */
314 /* be repeatedly marked by each marker. */
315 /* Here we assume two markers, but this is extremely */
316 /* unlikely to fail spuriously with more. And if it does, it */
317 /* should be looked at. */
318 GC_ASSERT(hhdr -> hb_n_marks <= 2 * (HBLKSIZE/sz + 1) + 16);
320 GC_ASSERT(sz * hhdr -> hb_n_marks <= HBLKSIZE);
322 if (hhdr -> hb_descr != 0) {
323 GC_composite_in_use += sz * hhdr -> hb_n_marks;
325 GC_atomic_in_use += sz * hhdr -> hb_n_marks;
327 if (report_if_found) {
328 GC_reclaim_small_nonempty_block(hbp, TRUE /* report_if_found */);
330 GC_bytes_found += HBLKSIZE;
332 } else if (GC_find_leak || !GC_block_nearly_full(hhdr)) {
333 /* group of smaller objects, enqueue the real work */
334 rlh = &(ok -> ok_reclaim_list[BYTES_TO_GRANULES(sz)]);
335 hhdr -> hb_next = *rlh;
337 } /* else not worth salvaging. */
338 /* We used to do the nearly_full check later, but we */
339 /* already have the right cache context here. Also */
340 /* doing it here avoids some silly lock contention in */
341 /* GC_malloc_many. */
345 #if !defined(NO_DEBUGGING)
346 /* Routines to gather and print heap block info */
347 /* intended for debugging. Otherwise should be called */
352 size_t number_of_blocks;
356 #ifdef USE_MARK_BYTES
358 /* Return the number of set mark bits in the given header. */
359 /* Remains externally visible as used by GNU GCJ currently. */
360 int GC_n_set_marks(hdr *hhdr)
364 size_t sz = hhdr -> hb_sz;
365 int offset = (int)MARK_BIT_OFFSET(sz);
366 int limit = (int)FINAL_MARK_BIT(sz);
368 for (i = 0; i < limit; i += offset) {
369 result += hhdr -> hb_marks[i];
371 GC_ASSERT(hhdr -> hb_marks[limit]);
377 /* Number of set bits in a word. Not performance critical. */
378 static int set_bits(word n)
390 int GC_n_set_marks(hdr *hhdr)
395 # ifdef MARK_BIT_PER_OBJ
396 int n_objs = (int)HBLK_OBJS(hhdr -> hb_sz);
398 if (0 == n_objs) n_objs = 1;
399 n_mark_words = divWORDSZ(n_objs + WORDSZ - 1);
400 # else /* MARK_BIT_PER_GRANULE */
401 n_mark_words = MARK_BITS_SZ;
403 for (i = 0; i < n_mark_words - 1; i++) {
404 result += set_bits(hhdr -> hb_marks[i]);
406 # ifdef MARK_BIT_PER_OBJ
407 result += set_bits((hhdr -> hb_marks[n_mark_words - 1])
408 << (n_mark_words * WORDSZ - n_objs));
410 result += set_bits(hhdr -> hb_marks[n_mark_words - 1]);
415 #endif /* !USE_MARK_BYTES */
417 STATIC void GC_print_block_descr(struct hblk *h,
418 word /* struct PrintStats */ raw_ps)
421 size_t bytes = hhdr -> hb_sz;
422 struct Print_stats *ps;
423 unsigned n_marks = GC_n_set_marks(hhdr);
425 if (hhdr -> hb_n_marks != n_marks) {
426 GC_printf("(%u:%u,%u!=%u)", hhdr -> hb_obj_kind, (unsigned)bytes,
427 (unsigned)hhdr -> hb_n_marks, n_marks);
429 GC_printf("(%u:%u,%u)", hhdr -> hb_obj_kind,
430 (unsigned)bytes, n_marks);
433 bytes &= ~(HBLKSIZE-1);
435 ps = (struct Print_stats *)raw_ps;
436 ps->total_bytes += bytes;
437 ps->number_of_blocks++;
440 void GC_print_block_list(void)
442 struct Print_stats pstats;
444 GC_printf("(kind(0=ptrfree,1=normal,2=unc.):size_in_bytes, #_marks_set)\n");
445 pstats.number_of_blocks = 0;
446 pstats.total_bytes = 0;
447 GC_apply_to_all_blocks(GC_print_block_descr, (word)&pstats);
448 GC_printf("\nblocks = %lu, bytes = %lu\n",
449 (unsigned long)pstats.number_of_blocks,
450 (unsigned long)pstats.total_bytes);
453 /* Currently for debugger use only: */
454 void GC_print_free_list(int kind, size_t sz_in_granules)
456 struct obj_kind * ok = &GC_obj_kinds[kind];
457 ptr_t flh = ok -> ok_freelist[sz_in_granules];
458 struct hblk *lastBlock = 0;
461 for (n = 1; flh; n++) {
462 struct hblk *block = HBLKPTR(flh);
463 if (block != lastBlock) {
464 GC_printf("\nIn heap block at %p:\n\t", block);
467 GC_printf("%d: %p;", n, flh);
472 #endif /* !NO_DEBUGGING */
475 * Clear all obj_link pointers in the list of free objects *flp.
477 * This must be done before dropping a list of free gcj-style objects,
478 * since may otherwise end up with dangling "descriptor" pointers.
479 * It may help for other pointer-containing objects.
481 STATIC void GC_clear_fl_links(void **flp)
487 flp = &(obj_link(next));
493 * Perform GC_reclaim_block on the entire heap, after first clearing
494 * small object free lists (if we are not just looking for leaks).
496 GC_INNER void GC_start_reclaim(GC_bool report_if_found)
500 # if defined(PARALLEL_MARK)
501 GC_ASSERT(0 == GC_fl_builder_count);
503 /* Reset in use counters. GC_reclaim_block recomputes them. */
504 GC_composite_in_use = 0;
505 GC_atomic_in_use = 0;
506 /* Clear reclaim- and free-lists */
507 for (kind = 0; kind < GC_n_kinds; kind++) {
510 struct hblk ** rlist = GC_obj_kinds[kind].ok_reclaim_list;
511 GC_bool should_clobber = (GC_obj_kinds[kind].ok_descriptor != 0);
513 if (rlist == 0) continue; /* This kind not used. */
514 if (!report_if_found) {
515 lim = &(GC_obj_kinds[kind].ok_freelist[MAXOBJGRANULES+1]);
516 for( fop = GC_obj_kinds[kind].ok_freelist; fop < lim; fop++ ) {
518 if (should_clobber) {
519 GC_clear_fl_links(fop);
525 } /* otherwise free list objects are marked, */
526 /* and its safe to leave them */
527 BZERO(rlist, (MAXOBJGRANULES + 1) * sizeof(void *));
531 /* Go through all heap blocks (in hblklist) and reclaim unmarked objects */
532 /* or enqueue the block for later processing. */
533 GC_apply_to_all_blocks(GC_reclaim_block, (word)report_if_found);
536 /* This is a very stupid thing to do. We make it possible anyway, */
537 /* so that you can convince yourself that it really is very stupid. */
538 GC_reclaim_all((GC_stop_func)0, FALSE);
540 # if defined(PARALLEL_MARK)
541 GC_ASSERT(0 == GC_fl_builder_count);
547 * Sweep blocks of the indicated object size and kind until either the
548 * appropriate free list is nonempty, or there are no more blocks to
551 GC_INNER void GC_continue_reclaim(size_t sz /* granules */, int kind)
555 struct obj_kind * ok = &(GC_obj_kinds[kind]);
556 struct hblk ** rlh = ok -> ok_reclaim_list;
557 void **flh = &(ok -> ok_freelist[sz]);
559 if (rlh == 0) return; /* No blocks of this kind. */
561 while ((hbp = *rlh) != 0) {
563 *rlh = hhdr -> hb_next;
564 GC_reclaim_small_nonempty_block(hbp, FALSE);
565 if (*flh != 0) break;
570 * Reclaim all small blocks waiting to be reclaimed.
571 * Abort and return FALSE when/if (*stop_func)() returns TRUE.
572 * If this returns TRUE, then it's safe to restart the world
573 * with incorrectly cleared mark bits.
574 * If ignore_old is TRUE, then reclaim only blocks that have been
575 * recently reclaimed, and discard the rest.
576 * Stop_func may be 0.
578 GC_INNER GC_bool GC_reclaim_all(GC_stop_func stop_func, GC_bool ignore_old)
584 struct obj_kind * ok;
587 # ifndef SMALL_CONFIG
588 CLOCK_TYPE start_time = 0; /* initialized to prevent warning. */
589 CLOCK_TYPE done_time;
591 if (GC_print_stats == VERBOSE)
592 GET_TIME(start_time);
595 for (kind = 0; kind < GC_n_kinds; kind++) {
596 ok = &(GC_obj_kinds[kind]);
597 rlp = ok -> ok_reclaim_list;
598 if (rlp == 0) continue;
599 for (sz = 1; sz <= MAXOBJGRANULES; sz++) {
601 while ((hbp = *rlh) != 0) {
602 if (stop_func != (GC_stop_func)0 && (*stop_func)()) {
606 *rlh = hhdr -> hb_next;
607 if (!ignore_old || hhdr -> hb_last_reclaimed == GC_gc_no - 1) {
608 /* It's likely we'll need it this time, too */
609 /* It's been touched recently, so this */
610 /* shouldn't trigger paging. */
611 GC_reclaim_small_nonempty_block(hbp, FALSE);
616 # ifndef SMALL_CONFIG
617 if (GC_print_stats == VERBOSE) {
619 GC_log_printf("Disposing of reclaim lists took %lu msecs\n",
620 MS_TIME_DIFF(done_time,start_time));