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"
19 #ifdef ENABLE_DISCLAIM
20 # include "gc_disclaim.h"
25 GC_INNER signed_word GC_bytes_found = 0;
26 /* Number of bytes of memory reclaimed */
27 /* minus the number of bytes originally */
28 /* on free lists which we had to drop. */
30 #if defined(PARALLEL_MARK)
31 GC_INNER word GC_fl_builder_count = 0;
32 /* Number of threads currently building free lists without */
33 /* holding GC lock. It is not safe to collect if this is */
35 #endif /* PARALLEL_MARK */
37 /* We defer printing of leaked objects until we're done with the GC */
38 /* cycle, since the routine for printing objects needs to run outside */
39 /* the collector, e.g. without the allocation lock. */
41 # define MAX_LEAKED 40
43 STATIC ptr_t GC_leaked[MAX_LEAKED] = { NULL };
44 STATIC unsigned GC_n_leaked = 0;
46 GC_INNER GC_bool GC_have_errors = FALSE;
48 #if !defined(EAGER_SWEEP) && defined(ENABLE_DISCLAIM)
49 STATIC void GC_reclaim_unconditionally_marked(void);
52 GC_INLINE void GC_add_leaked(ptr_t leaked)
54 # ifndef SHORT_DBG_HDRS
55 if (GC_findleak_delay_free && !GC_check_leaked(leaked))
59 GC_have_errors = TRUE;
60 /* FIXME: Prevent adding an object while printing leaked ones. */
61 if (GC_n_leaked < MAX_LEAKED) {
62 GC_leaked[GC_n_leaked++] = leaked;
63 /* Make sure it's not reclaimed this cycle */
64 GC_set_mark_bit(leaked);
68 /* Print all objects on the list after printing any smashed objects. */
69 /* Clear both lists. Called without the allocation lock held. */
70 GC_INNER void GC_print_all_errors(void)
72 static GC_bool printing_errors = FALSE;
78 if (printing_errors) {
82 have_errors = GC_have_errors;
83 printing_errors = TRUE;
86 if (GC_debugging_started) {
87 GC_print_all_smashed();
92 if (GC_n_leaked > 0) {
93 GC_err_printf("Found %u leaked objects:\n", GC_n_leaked);
96 for (i = 0; i < GC_n_leaked; ++i) {
97 ptr_t p = GC_leaked[i];
105 # ifndef GC_ABORT_ON_LEAK
106 && GETENV("GC_ABORT_ON_LEAK") != NULL
109 ABORT("Leaked or smashed objects encountered");
112 printing_errors = FALSE;
121 /* Test whether a block is completely empty, i.e. contains no marked */
122 /* objects. This does not require the block to be in physical memory. */
123 GC_INNER GC_bool GC_block_empty(hdr *hhdr)
125 return (hhdr -> hb_n_marks == 0);
128 STATIC GC_bool GC_block_nearly_full(hdr *hhdr)
130 return (hhdr -> hb_n_marks > 7 * HBLK_OBJS(hhdr -> hb_sz)/8);
133 /* FIXME: This should perhaps again be specialized for USE_MARK_BYTES */
134 /* and USE_MARK_BITS cases. */
137 * Restore unmarked small objects in h of size sz to the object
138 * free list. Returns the new list.
139 * Clears unmarked objects. Sz is in bytes.
141 STATIC ptr_t GC_reclaim_clear(struct hblk *hbp, hdr *hhdr, size_t sz,
142 ptr_t list, signed_word *count)
146 signed_word n_bytes_found = 0;
148 GC_ASSERT(hhdr == GC_find_header((ptr_t)hbp));
149 GC_ASSERT(sz == hhdr -> hb_sz);
150 GC_ASSERT((sz & (BYTES_PER_WORD-1)) == 0);
151 p = (word *)(hbp->hb_body);
152 plim = (word *)(hbp->hb_body + HBLKSIZE - sz);
154 /* go through all words in block */
155 while ((word)p <= (word)plim) {
156 if (mark_bit_from_hdr(hhdr, bit_no)) {
157 p = (word *)((ptr_t)p + sz);
160 /* object is available - put on list */
163 /* Clear object, advance p to next object in the process */
164 q = (word *)((ptr_t)p + sz);
165 # ifdef USE_MARK_BYTES
167 && !((word)p & (2 * sizeof(word) - 1)));
170 while ((word)p < (word)q) {
175 p++; /* Skip link field */
176 while ((word)p < (word)q) {
181 bit_no += MARK_BIT_OFFSET(sz);
183 *count += n_bytes_found;
187 /* The same thing, but don't clear objects: */
188 STATIC ptr_t GC_reclaim_uninit(struct hblk *hbp, hdr *hhdr, size_t sz,
189 ptr_t list, signed_word *count)
193 signed_word n_bytes_found = 0;
195 GC_ASSERT(sz == hhdr -> hb_sz);
196 p = (word *)(hbp->hb_body);
197 plim = (word *)((ptr_t)hbp + HBLKSIZE - sz);
199 /* go through all words in block */
200 while ((word)p <= (word)plim) {
201 if (!mark_bit_from_hdr(hhdr, bit_no)) {
203 /* object is available - put on list */
207 p = (word *)((ptr_t)p + sz);
208 bit_no += MARK_BIT_OFFSET(sz);
210 *count += n_bytes_found;
214 #ifdef ENABLE_DISCLAIM
215 /* Call reclaim notifier for block's kind on each unmarked object in */
216 /* block, all within a pair of corresponding enter/leave callbacks. */
217 STATIC ptr_t GC_disclaim_and_reclaim(struct hblk *hbp, hdr *hhdr, size_t sz,
218 ptr_t list, signed_word *count)
222 signed_word n_bytes_found = 0;
223 struct obj_kind *ok = &GC_obj_kinds[hhdr->hb_obj_kind];
224 int (GC_CALLBACK *disclaim)(void *) = ok->ok_disclaim_proc;
226 GC_ASSERT(sz == hhdr -> hb_sz);
227 p = (word *)(hbp -> hb_body);
228 plim = (word *)((ptr_t)p + HBLKSIZE - sz);
230 while ((word)p <= (word)plim) {
231 int marked = mark_bit_from_hdr(hhdr, bit_no);
232 if (!marked && (*disclaim)(p)) {
233 hhdr -> hb_n_marks++;
237 p = (word *)((ptr_t)p + sz);
240 /* object is available - put on list */
243 /* Clear object, advance p to next object in the process */
244 q = (word *)((ptr_t)p + sz);
245 # ifdef USE_MARK_BYTES
246 GC_ASSERT((sz & 1) == 0);
247 GC_ASSERT(((word)p & (2 * sizeof(word) - 1)) == 0);
250 while ((word)p < (word)q) {
255 p++; /* Skip link field */
256 while ((word)p < (word)q) {
261 bit_no += MARK_BIT_OFFSET(sz);
263 *count += n_bytes_found;
266 #endif /* ENABLE_DISCLAIM */
268 /* Don't really reclaim objects, just check for unmarked ones: */
269 STATIC void GC_reclaim_check(struct hblk *hbp, hdr *hhdr, word sz)
273 GC_ASSERT(sz == hhdr -> hb_sz);
275 /* go through all words in block */
277 plim = p + HBLKSIZE - sz;
278 for (bit_no = 0; (word)p <= (word)plim;
279 p += sz, bit_no += MARK_BIT_OFFSET(sz)) {
280 if (!mark_bit_from_hdr(hhdr, bit_no)) {
287 * Generic procedure to rebuild a free list in hbp.
288 * Also called directly from GC_malloc_many.
289 * Sz is now in bytes.
291 GC_INNER ptr_t GC_reclaim_generic(struct hblk * hbp, hdr *hhdr, size_t sz,
292 GC_bool init, ptr_t list,
297 GC_ASSERT(GC_find_header((ptr_t)hbp) == hhdr);
298 # ifndef GC_DISABLE_INCREMENTAL
299 GC_remove_protection(hbp, 1, (hhdr)->hb_descr == 0 /* Pointer-free? */);
301 # ifdef ENABLE_DISCLAIM
302 if ((hhdr -> hb_flags & HAS_DISCLAIM) != 0) {
303 result = GC_disclaim_and_reclaim(hbp, hhdr, sz, list, count);
306 /* else */ if (init || GC_debugging_started) {
307 result = GC_reclaim_clear(hbp, hhdr, sz, list, count);
309 GC_ASSERT((hhdr)->hb_descr == 0 /* Pointer-free block */);
310 result = GC_reclaim_uninit(hbp, hhdr, sz, list, count);
312 if (IS_UNCOLLECTABLE(hhdr -> hb_obj_kind)) GC_set_hdr_marks(hhdr);
317 * Restore unmarked small objects in the block pointed to by hbp
318 * to the appropriate object free list.
319 * If entirely empty blocks are to be completely deallocated, then
320 * caller should perform that check.
322 STATIC void GC_reclaim_small_nonempty_block(struct hblk *hbp,
323 GC_bool report_if_found)
325 hdr *hhdr = HDR(hbp);
326 size_t sz = hhdr -> hb_sz;
327 struct obj_kind * ok = &GC_obj_kinds[hhdr -> hb_obj_kind];
328 void **flh = &(ok -> ok_freelist[BYTES_TO_GRANULES(sz)]);
330 hhdr -> hb_last_reclaimed = (unsigned short) GC_gc_no;
332 if (report_if_found) {
333 GC_reclaim_check(hbp, hhdr, sz);
335 *flh = GC_reclaim_generic(hbp, hhdr, sz, ok -> ok_init,
336 *flh, &GC_bytes_found);
340 #ifdef ENABLE_DISCLAIM
341 STATIC void GC_disclaim_and_reclaim_or_free_small_block(struct hblk *hbp)
343 hdr *hhdr = HDR(hbp);
344 size_t sz = hhdr -> hb_sz;
345 struct obj_kind * ok = &GC_obj_kinds[hhdr -> hb_obj_kind];
346 void **flh = &(ok -> ok_freelist[BYTES_TO_GRANULES(sz)]);
349 hhdr -> hb_last_reclaimed = (unsigned short) GC_gc_no;
350 flh_next = GC_reclaim_generic(hbp, hhdr, sz, ok -> ok_init,
351 *flh, &GC_bytes_found);
352 if (hhdr -> hb_n_marks)
355 GC_bytes_found += HBLKSIZE;
359 #endif /* ENABLE_DISCLAIM */
362 * Restore an unmarked large object or an entirely empty blocks of small objects
363 * to the heap block free list.
364 * Otherwise enqueue the block for later processing
365 * by GC_reclaim_small_nonempty_block.
366 * If report_if_found is TRUE, then process any block immediately, and
367 * simply report free objects; do not actually reclaim them.
369 STATIC void GC_reclaim_block(struct hblk *hbp, word report_if_found)
371 hdr * hhdr = HDR(hbp);
372 size_t sz = hhdr -> hb_sz; /* size of objects in current block */
373 struct obj_kind * ok = &GC_obj_kinds[hhdr -> hb_obj_kind];
376 if( sz > MAXOBJBYTES ) { /* 1 big object */
377 if( !mark_bit_from_hdr(hhdr, 0) ) {
378 if (report_if_found) {
379 GC_add_leaked((ptr_t)hbp);
383 # ifdef ENABLE_DISCLAIM
384 if (EXPECT(hhdr->hb_flags & HAS_DISCLAIM, 0)) {
385 struct obj_kind *ok = &GC_obj_kinds[hhdr->hb_obj_kind];
386 if ((*ok->ok_disclaim_proc)(hbp)) {
387 /* Not disclaimed => resurrect the object. */
388 set_mark_bit_from_hdr(hhdr, 0);
393 blocks = OBJ_SZ_TO_BLOCKS(sz);
395 GC_large_allocd_bytes -= blocks * HBLKSIZE;
397 GC_bytes_found += sz;
401 # ifdef ENABLE_DISCLAIM
404 if (hhdr -> hb_descr != 0) {
405 GC_composite_in_use += sz;
407 GC_atomic_in_use += sz;
411 GC_bool empty = GC_block_empty(hhdr);
412 # ifdef PARALLEL_MARK
413 /* Count can be low or one too high because we sometimes */
414 /* have to ignore decrements. Objects can also potentially */
415 /* be repeatedly marked by each marker. */
416 /* Here we assume two markers, but this is extremely */
417 /* unlikely to fail spuriously with more. And if it does, it */
418 /* should be looked at. */
419 GC_ASSERT(hhdr -> hb_n_marks <= 2 * (HBLKSIZE/sz + 1) + 16);
421 GC_ASSERT(sz * hhdr -> hb_n_marks <= HBLKSIZE);
423 if (report_if_found) {
424 GC_reclaim_small_nonempty_block(hbp, TRUE /* report_if_found */);
426 # ifdef ENABLE_DISCLAIM
427 if ((hhdr -> hb_flags & HAS_DISCLAIM) != 0) {
428 GC_disclaim_and_reclaim_or_free_small_block(hbp);
432 GC_bytes_found += HBLKSIZE;
435 } else if (GC_find_leak || !GC_block_nearly_full(hhdr)) {
436 /* group of smaller objects, enqueue the real work */
437 rlh = &(ok -> ok_reclaim_list[BYTES_TO_GRANULES(sz)]);
438 hhdr -> hb_next = *rlh;
440 } /* else not worth salvaging. */
441 /* We used to do the nearly_full check later, but we */
442 /* already have the right cache context here. Also */
443 /* doing it here avoids some silly lock contention in */
444 /* GC_malloc_many. */
446 if (hhdr -> hb_descr != 0) {
447 GC_composite_in_use += sz * hhdr -> hb_n_marks;
449 GC_atomic_in_use += sz * hhdr -> hb_n_marks;
454 #if !defined(NO_DEBUGGING)
455 /* Routines to gather and print heap block info */
456 /* intended for debugging. Otherwise should be called */
461 size_t number_of_blocks;
465 #ifdef USE_MARK_BYTES
467 /* Return the number of set mark bits in the given header. */
468 /* Remains externally visible as used by GNU GCJ currently. */
469 int GC_n_set_marks(hdr *hhdr)
473 size_t sz = hhdr -> hb_sz;
474 int offset = (int)MARK_BIT_OFFSET(sz);
475 int limit = (int)FINAL_MARK_BIT(sz);
477 for (i = 0; i < limit; i += offset) {
478 result += hhdr -> hb_marks[i];
480 GC_ASSERT(hhdr -> hb_marks[limit]);
486 /* Number of set bits in a word. Not performance critical. */
487 static int set_bits(word n)
499 int GC_n_set_marks(hdr *hhdr)
504 # ifdef MARK_BIT_PER_OBJ
505 int n_objs = (int)HBLK_OBJS(hhdr -> hb_sz);
507 if (0 == n_objs) n_objs = 1;
508 n_mark_words = divWORDSZ(n_objs + WORDSZ - 1);
509 # else /* MARK_BIT_PER_GRANULE */
510 n_mark_words = MARK_BITS_SZ;
512 for (i = 0; i < n_mark_words - 1; i++) {
513 result += set_bits(hhdr -> hb_marks[i]);
515 # ifdef MARK_BIT_PER_OBJ
516 result += set_bits((hhdr -> hb_marks[n_mark_words - 1])
517 << (n_mark_words * WORDSZ - n_objs));
519 result += set_bits(hhdr -> hb_marks[n_mark_words - 1]);
524 #endif /* !USE_MARK_BYTES */
526 STATIC void GC_print_block_descr(struct hblk *h,
527 word /* struct PrintStats */ raw_ps)
530 size_t bytes = hhdr -> hb_sz;
531 struct Print_stats *ps;
532 unsigned n_marks = GC_n_set_marks(hhdr);
534 if (hhdr -> hb_n_marks != n_marks) {
535 GC_printf("(%u:%u,%u!=%u)\n", hhdr->hb_obj_kind, (unsigned)bytes,
536 (unsigned)hhdr->hb_n_marks, n_marks);
538 GC_printf("(%u:%u,%u)\n", hhdr->hb_obj_kind,
539 (unsigned)bytes, n_marks);
542 bytes &= ~(HBLKSIZE-1);
544 ps = (struct Print_stats *)raw_ps;
545 ps->total_bytes += bytes;
546 ps->number_of_blocks++;
549 void GC_print_block_list(void)
551 struct Print_stats pstats;
553 GC_printf("(kind(0=ptrfree,1=normal,2=unc.):size_in_bytes, #_marks_set)\n");
554 pstats.number_of_blocks = 0;
555 pstats.total_bytes = 0;
556 GC_apply_to_all_blocks(GC_print_block_descr, (word)&pstats);
557 GC_printf("blocks= %lu, bytes= %lu\n",
558 (unsigned long)pstats.number_of_blocks,
559 (unsigned long)pstats.total_bytes);
562 /* Currently for debugger use only: */
563 void GC_print_free_list(int kind, size_t sz_in_granules)
565 struct obj_kind * ok = &GC_obj_kinds[kind];
566 ptr_t flh = ok -> ok_freelist[sz_in_granules];
569 for (n = 0; flh; n++) {
570 struct hblk *block = HBLKPTR(flh);
571 GC_printf("Free object in heap block %p [%d]: %p\n",
572 (void *)block, n, flh);
577 #endif /* !NO_DEBUGGING */
580 * Clear all obj_link pointers in the list of free objects *flp.
582 * This must be done before dropping a list of free gcj-style objects,
583 * since may otherwise end up with dangling "descriptor" pointers.
584 * It may help for other pointer-containing objects.
586 STATIC void GC_clear_fl_links(void **flp)
592 flp = &(obj_link(next));
598 * Perform GC_reclaim_block on the entire heap, after first clearing
599 * small object free lists (if we are not just looking for leaks).
601 GC_INNER void GC_start_reclaim(GC_bool report_if_found)
605 # if defined(PARALLEL_MARK)
606 GC_ASSERT(0 == GC_fl_builder_count);
608 /* Reset in use counters. GC_reclaim_block recomputes them. */
609 GC_composite_in_use = 0;
610 GC_atomic_in_use = 0;
611 /* Clear reclaim- and free-lists */
612 for (kind = 0; kind < GC_n_kinds; kind++) {
615 struct hblk ** rlist = GC_obj_kinds[kind].ok_reclaim_list;
616 GC_bool should_clobber = (GC_obj_kinds[kind].ok_descriptor != 0);
618 if (rlist == 0) continue; /* This kind not used. */
619 if (!report_if_found) {
620 lim = &(GC_obj_kinds[kind].ok_freelist[MAXOBJGRANULES+1]);
621 for (fop = GC_obj_kinds[kind].ok_freelist;
622 (word)fop < (word)lim; fop++) {
624 if (should_clobber) {
625 GC_clear_fl_links(fop);
631 } /* otherwise free list objects are marked, */
632 /* and its safe to leave them */
633 BZERO(rlist, (MAXOBJGRANULES + 1) * sizeof(void *));
637 /* Go through all heap blocks (in hblklist) and reclaim unmarked objects */
638 /* or enqueue the block for later processing. */
639 GC_apply_to_all_blocks(GC_reclaim_block, (word)report_if_found);
642 /* This is a very stupid thing to do. We make it possible anyway, */
643 /* so that you can convince yourself that it really is very stupid. */
644 GC_reclaim_all((GC_stop_func)0, FALSE);
645 # elif defined(ENABLE_DISCLAIM)
646 /* However, make sure to clear reclaimable objects of kinds with */
647 /* unconditional marking enabled before we do any significant */
649 GC_reclaim_unconditionally_marked();
651 # if defined(PARALLEL_MARK)
652 GC_ASSERT(0 == GC_fl_builder_count);
658 * Sweep blocks of the indicated object size and kind until either the
659 * appropriate free list is nonempty, or there are no more blocks to
662 GC_INNER void GC_continue_reclaim(size_t sz /* granules */, int kind)
666 struct obj_kind * ok = &(GC_obj_kinds[kind]);
667 struct hblk ** rlh = ok -> ok_reclaim_list;
668 void **flh = &(ok -> ok_freelist[sz]);
670 if (rlh == 0) return; /* No blocks of this kind. */
672 while ((hbp = *rlh) != 0) {
674 *rlh = hhdr -> hb_next;
675 GC_reclaim_small_nonempty_block(hbp, FALSE);
676 if (*flh != 0) break;
681 * Reclaim all small blocks waiting to be reclaimed.
682 * Abort and return FALSE when/if (*stop_func)() returns TRUE.
683 * If this returns TRUE, then it's safe to restart the world
684 * with incorrectly cleared mark bits.
685 * If ignore_old is TRUE, then reclaim only blocks that have been
686 * recently reclaimed, and discard the rest.
687 * Stop_func may be 0.
689 GC_INNER GC_bool GC_reclaim_all(GC_stop_func stop_func, GC_bool ignore_old)
695 struct obj_kind * ok;
698 # ifndef SMALL_CONFIG
699 CLOCK_TYPE start_time = 0; /* initialized to prevent warning. */
700 CLOCK_TYPE done_time;
702 if (GC_print_stats == VERBOSE)
703 GET_TIME(start_time);
706 for (kind = 0; kind < GC_n_kinds; kind++) {
707 ok = &(GC_obj_kinds[kind]);
708 rlp = ok -> ok_reclaim_list;
709 if (rlp == 0) continue;
710 for (sz = 1; sz <= MAXOBJGRANULES; sz++) {
712 while ((hbp = *rlh) != 0) {
713 if (stop_func != (GC_stop_func)0 && (*stop_func)()) {
717 *rlh = hhdr -> hb_next;
718 if (!ignore_old || hhdr -> hb_last_reclaimed == GC_gc_no - 1) {
719 /* It's likely we'll need it this time, too */
720 /* It's been touched recently, so this */
721 /* shouldn't trigger paging. */
722 GC_reclaim_small_nonempty_block(hbp, FALSE);
727 # ifndef SMALL_CONFIG
728 if (GC_print_stats == VERBOSE) {
730 GC_verbose_log_printf("Disposing of reclaim lists took %lu msecs\n",
731 MS_TIME_DIFF(done_time,start_time));
737 #if !defined(EAGER_SWEEP) && defined(ENABLE_DISCLAIM)
738 /* We do an eager sweep on heap blocks where unconditional marking has */
739 /* been enabled, so that any reclaimable objects have been reclaimed */
740 /* before we start marking. This is a simplified GC_reclaim_all */
741 /* restricted to kinds where ok_mark_unconditionally is true. */
742 STATIC void GC_reclaim_unconditionally_marked(void)
748 struct obj_kind * ok;
752 for (kind = 0; kind < GC_n_kinds; kind++) {
753 ok = &(GC_obj_kinds[kind]);
754 if (!ok->ok_mark_unconditionally)
756 rlp = ok->ok_reclaim_list;
759 for (sz = 1; sz <= MAXOBJGRANULES; sz++) {
761 while ((hbp = *rlh) != 0) {
763 *rlh = hhdr->hb_next;
764 GC_reclaim_small_nonempty_block(hbp, FALSE);
769 #endif /* !EAGER_SWEEP && ENABLE_DISCLAIM */