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(MARK_UNCONDITIONALLY)
49 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 for (i = 0; i < GC_n_leaked; ++i) {
93 ptr_t p = GC_leaked[i];
94 if (HDR(p) -> hb_obj_kind == PTRFREE) {
95 GC_err_printf("Leaked atomic object at ");
97 GC_err_printf("Leaked composite object at ");
108 # ifndef GC_ABORT_ON_LEAK
109 && GETENV("GC_ABORT_ON_LEAK") != NULL
112 ABORT("Leaked or smashed objects encountered");
115 printing_errors = FALSE;
124 /* Test whether a block is completely empty, i.e. contains no marked */
125 /* objects. This does not require the block to be in physical memory. */
126 GC_INNER GC_bool GC_block_empty(hdr *hhdr)
128 return (hhdr -> hb_n_marks == 0);
131 STATIC GC_bool GC_block_nearly_full(hdr *hhdr)
133 return (hhdr -> hb_n_marks > 7 * HBLK_OBJS(hhdr -> hb_sz)/8);
136 /* FIXME: This should perhaps again be specialized for USE_MARK_BYTES */
137 /* and USE_MARK_BITS cases. */
140 * Restore unmarked small objects in h of size sz to the object
141 * free list. Returns the new list.
142 * Clears unmarked objects. Sz is in bytes.
144 STATIC ptr_t GC_reclaim_clear(struct hblk *hbp, hdr *hhdr, size_t sz,
145 ptr_t list, signed_word *count)
149 signed_word n_bytes_found = 0;
151 GC_ASSERT(hhdr == GC_find_header((ptr_t)hbp));
152 GC_ASSERT(sz == hhdr -> hb_sz);
153 GC_ASSERT((sz & (BYTES_PER_WORD-1)) == 0);
154 p = (word *)(hbp->hb_body);
155 plim = (word *)(hbp->hb_body + HBLKSIZE - sz);
157 /* go through all words in block */
159 if( mark_bit_from_hdr(hhdr, bit_no) ) {
160 p = (word *)((ptr_t)p + sz);
163 /* object is available - put on list */
166 /* Clear object, advance p to next object in the process */
167 q = (word *)((ptr_t)p + sz);
168 # ifdef USE_MARK_BYTES
170 && !((word)p & (2 * sizeof(word) - 1)));
178 p++; /* Skip link field */
184 bit_no += MARK_BIT_OFFSET(sz);
186 *count += n_bytes_found;
190 /* The same thing, but don't clear objects: */
191 STATIC ptr_t GC_reclaim_uninit(struct hblk *hbp, hdr *hhdr, size_t sz,
192 ptr_t list, signed_word *count)
196 signed_word n_bytes_found = 0;
198 GC_ASSERT(sz == hhdr -> hb_sz);
199 p = (word *)(hbp->hb_body);
200 plim = (word *)((ptr_t)hbp + HBLKSIZE - sz);
202 /* go through all words in block */
204 if( !mark_bit_from_hdr(hhdr, bit_no) ) {
206 /* object is available - put on list */
210 p = (word *)((ptr_t)p + sz);
211 bit_no += MARK_BIT_OFFSET(sz);
213 *count += n_bytes_found;
217 #ifdef ENABLE_DISCLAIM
218 /* Call reclaim notifier for block's kind on each unmarked object in */
219 /* block, all within a pair of corresponding enter/leave callbacks. */
220 STATIC ptr_t GC_disclaim_and_reclaim(struct hblk *hbp, hdr *hhdr, size_t sz,
221 ptr_t list, signed_word *count)
225 signed_word n_bytes_found = 0;
226 struct obj_kind *ok = &GC_obj_kinds[hhdr->hb_obj_kind];
227 int (*proc)(void *, void *) = ok -> ok_disclaim_proc;
228 void *cd = ok -> ok_disclaim_cd;
230 GC_ASSERT(sz == hhdr -> hb_sz);
231 p = (word *)(hbp -> hb_body);
232 plim = (word *)((ptr_t)p + HBLKSIZE - sz);
235 int marked = mark_bit_from_hdr(hhdr, bit_no);
236 if (!marked && (*proc)(p, cd)) {
237 hhdr -> hb_n_marks++;
241 p = (word *)((ptr_t)p + sz);
244 /* object is available - put on list */
247 /* Clear object, advance p to next object in the process */
248 q = (word *)((ptr_t)p + sz);
249 # ifdef USE_MARK_BYTES
251 && !((word)p & (2 * sizeof(word) - 1)));
259 p++; /* Skip link field */
265 bit_no += MARK_BIT_OFFSET(sz);
267 *count += n_bytes_found;
270 #endif /* ENABLE_DISCLAIM */
272 /* Don't really reclaim objects, just check for unmarked ones: */
273 STATIC void GC_reclaim_check(struct hblk *hbp, hdr *hhdr, word sz)
277 GC_ASSERT(sz == hhdr -> hb_sz);
279 /* go through all words in block */
281 plim = p + HBLKSIZE - sz;
282 for (bit_no = 0; p <= plim; p += sz, bit_no += MARK_BIT_OFFSET(sz)) {
283 if (!mark_bit_from_hdr(hhdr, bit_no)) {
290 * Generic procedure to rebuild a free list in hbp.
291 * Also called directly from GC_malloc_many.
292 * Sz is now in bytes.
294 GC_INNER ptr_t GC_reclaim_generic(struct hblk * hbp, hdr *hhdr, size_t sz,
295 GC_bool init, ptr_t list,
300 GC_ASSERT(GC_find_header((ptr_t)hbp) == hhdr);
301 # ifndef GC_DISABLE_INCREMENTAL
302 GC_remove_protection(hbp, 1, (hhdr)->hb_descr == 0 /* Pointer-free? */);
304 # ifdef ENABLE_DISCLAIM
305 if (hhdr -> hb_flags & HAS_DISCLAIM) {
306 result = GC_disclaim_and_reclaim(hbp, hhdr, sz, list, count);
309 /* else */ if (init || GC_debugging_started) {
310 result = GC_reclaim_clear(hbp, hhdr, sz, list, count);
312 GC_ASSERT((hhdr)->hb_descr == 0 /* Pointer-free block */);
313 result = GC_reclaim_uninit(hbp, hhdr, sz, list, count);
315 if (IS_UNCOLLECTABLE(hhdr -> hb_obj_kind)) GC_set_hdr_marks(hhdr);
320 * Restore unmarked small objects in the block pointed to by hbp
321 * to the appropriate object free list.
322 * If entirely empty blocks are to be completely deallocated, then
323 * caller should perform that check.
325 STATIC void GC_reclaim_small_nonempty_block(struct hblk *hbp,
326 GC_bool report_if_found)
328 hdr *hhdr = HDR(hbp);
329 size_t sz = hhdr -> hb_sz;
330 struct obj_kind * ok = &GC_obj_kinds[hhdr -> hb_obj_kind];
331 void **flh = &(ok -> ok_freelist[BYTES_TO_GRANULES(sz)]);
333 hhdr -> hb_last_reclaimed = (unsigned short) GC_gc_no;
335 if (report_if_found) {
336 GC_reclaim_check(hbp, hhdr, sz);
338 *flh = GC_reclaim_generic(hbp, hhdr, sz, ok -> ok_init,
339 *flh, &GC_bytes_found);
343 #ifdef ENABLE_DISCLAIM
344 STATIC void GC_disclaim_and_reclaim_or_free_small_block(struct hblk *hbp)
346 hdr *hhdr = HDR(hbp);
347 size_t sz = hhdr -> hb_sz;
348 struct obj_kind * ok = &GC_obj_kinds[hhdr -> hb_obj_kind];
349 void **flh = &(ok -> ok_freelist[BYTES_TO_GRANULES(sz)]);
352 hhdr -> hb_last_reclaimed = (unsigned short) GC_gc_no;
353 flh_next = GC_reclaim_generic(hbp, hhdr, sz, ok -> ok_init,
354 *flh, &GC_bytes_found);
355 if (hhdr -> hb_n_marks)
358 GC_bytes_found += HBLKSIZE;
362 #endif /* ENABLE_DISCLAIM */
365 * Restore an unmarked large object or an entirely empty blocks of small objects
366 * to the heap block free list.
367 * Otherwise enqueue the block for later processing
368 * by GC_reclaim_small_nonempty_block.
369 * If report_if_found is TRUE, then process any block immediately, and
370 * simply report free objects; do not actually reclaim them.
372 STATIC void GC_reclaim_block(struct hblk *hbp, word report_if_found)
374 hdr * hhdr = HDR(hbp);
375 size_t sz = hhdr -> hb_sz; /* size of objects in current block */
376 struct obj_kind * ok = &GC_obj_kinds[hhdr -> hb_obj_kind];
379 if( sz > MAXOBJBYTES ) { /* 1 big object */
380 if( !mark_bit_from_hdr(hhdr, 0) ) {
381 if (report_if_found) {
382 GC_add_leaked((ptr_t)hbp);
384 size_t blocks = OBJ_SZ_TO_BLOCKS(sz);
385 # ifdef ENABLE_DISCLAIM
386 if (EXPECT(hhdr->hb_flags & HAS_DISCLAIM, 0)) {
387 struct obj_kind *ok = &GC_obj_kinds[hhdr->hb_obj_kind];
388 if ((*ok->ok_disclaim_proc)(hbp, ok->ok_disclaim_cd)) {
389 /* Not disclaimed => resurrect the object. */
390 set_mark_bit_from_hdr(hhdr, 0);
396 GC_large_allocd_bytes -= blocks * HBLKSIZE;
398 GC_bytes_found += sz;
402 # ifdef ENABLE_DISCLAIM
405 if (hhdr -> hb_descr != 0) {
406 GC_composite_in_use += sz;
408 GC_atomic_in_use += sz;
412 GC_bool empty = GC_block_empty(hhdr);
413 # ifdef PARALLEL_MARK
414 /* Count can be low or one too high because we sometimes */
415 /* have to ignore decrements. Objects can also potentially */
416 /* be repeatedly marked by each marker. */
417 /* Here we assume two markers, but this is extremely */
418 /* unlikely to fail spuriously with more. And if it does, it */
419 /* should be looked at. */
420 GC_ASSERT(hhdr -> hb_n_marks <= 2 * (HBLKSIZE/sz + 1) + 16);
422 GC_ASSERT(sz * hhdr -> hb_n_marks <= HBLKSIZE);
424 if (report_if_found) {
425 GC_reclaim_small_nonempty_block(hbp, TRUE /* report_if_found */);
427 # ifdef ENABLE_DISCLAIM
428 if ((hhdr -> hb_flags & HAS_DISCLAIM) != 0) {
429 GC_disclaim_and_reclaim_or_free_small_block(hbp);
433 GC_bytes_found += HBLKSIZE;
436 } else if (GC_find_leak || !GC_block_nearly_full(hhdr)) {
437 /* group of smaller objects, enqueue the real work */
438 rlh = &(ok -> ok_reclaim_list[BYTES_TO_GRANULES(sz)]);
439 hhdr -> hb_next = *rlh;
441 } /* else not worth salvaging. */
442 /* We used to do the nearly_full check later, but we */
443 /* already have the right cache context here. Also */
444 /* doing it here avoids some silly lock contention in */
445 /* GC_malloc_many. */
447 if (hhdr -> hb_descr != 0) {
448 GC_composite_in_use += sz * hhdr -> hb_n_marks;
450 GC_atomic_in_use += sz * hhdr -> hb_n_marks;
455 #if !defined(NO_DEBUGGING)
456 /* Routines to gather and print heap block info */
457 /* intended for debugging. Otherwise should be called */
462 size_t number_of_blocks;
466 #ifdef USE_MARK_BYTES
468 /* Return the number of set mark bits in the given header */
469 STATIC 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 /* Return the number of set mark bits in the given header */
500 STATIC int GC_n_set_marks(hdr *hhdr)
505 # ifdef MARK_BIT_PER_OBJ
506 int n_objs = (int)HBLK_OBJS(hhdr -> hb_sz);
508 if (0 == n_objs) n_objs = 1;
509 n_mark_words = divWORDSZ(n_objs + WORDSZ - 1);
510 # else /* MARK_BIT_PER_GRANULE */
511 n_mark_words = MARK_BITS_SZ;
513 for (i = 0; i < n_mark_words - 1; i++) {
514 result += set_bits(hhdr -> hb_marks[i]);
516 # ifdef MARK_BIT_PER_OBJ
517 result += set_bits((hhdr -> hb_marks[n_mark_words - 1])
518 << (n_mark_words * WORDSZ - n_objs));
520 result += set_bits(hhdr -> hb_marks[n_mark_words - 1]);
525 #endif /* !USE_MARK_BYTES */
527 STATIC void GC_print_block_descr(struct hblk *h,
528 word /* struct PrintStats */ raw_ps)
531 size_t bytes = hhdr -> hb_sz;
532 struct Print_stats *ps;
533 unsigned n_marks = GC_n_set_marks(hhdr);
535 if (hhdr -> hb_n_marks != n_marks) {
536 GC_printf("(%u:%u,%u!=%u)", hhdr -> hb_obj_kind, (unsigned)bytes,
537 (unsigned)hhdr -> hb_n_marks, n_marks);
539 GC_printf("(%u:%u,%u)", hhdr -> hb_obj_kind,
540 (unsigned)bytes, n_marks);
543 bytes &= ~(HBLKSIZE-1);
545 ps = (struct Print_stats *)raw_ps;
546 ps->total_bytes += bytes;
547 ps->number_of_blocks++;
550 void GC_print_block_list(void)
552 struct Print_stats pstats;
554 GC_printf("(kind(0=ptrfree,1=normal,2=unc.):size_in_bytes, #_marks_set)\n");
555 pstats.number_of_blocks = 0;
556 pstats.total_bytes = 0;
557 GC_apply_to_all_blocks(GC_print_block_descr, (word)&pstats);
558 GC_printf("\nblocks = %lu, bytes = %lu\n",
559 (unsigned long)pstats.number_of_blocks,
560 (unsigned long)pstats.total_bytes);
563 /* Currently for debugger use only: */
564 void GC_print_free_list(int kind, size_t sz_in_granules)
566 struct obj_kind * ok = &GC_obj_kinds[kind];
567 ptr_t flh = ok -> ok_freelist[sz_in_granules];
568 struct hblk *lastBlock = 0;
571 for (n = 1; flh; n++) {
572 struct hblk *block = HBLKPTR(flh);
573 if (block != lastBlock) {
574 GC_printf("\nIn heap block at %p:\n\t", (void *)block);
577 GC_printf("%d: %p;", n, flh);
582 #endif /* !NO_DEBUGGING */
585 * Clear all obj_link pointers in the list of free objects *flp.
587 * This must be done before dropping a list of free gcj-style objects,
588 * since may otherwise end up with dangling "descriptor" pointers.
589 * It may help for other pointer-containing objects.
591 STATIC void GC_clear_fl_links(void **flp)
597 flp = &(obj_link(next));
603 * Perform GC_reclaim_block on the entire heap, after first clearing
604 * small object free lists (if we are not just looking for leaks).
606 GC_INNER void GC_start_reclaim(GC_bool report_if_found)
610 # if defined(PARALLEL_MARK)
611 GC_ASSERT(0 == GC_fl_builder_count);
613 /* Reset in use counters. GC_reclaim_block recomputes them. */
614 GC_composite_in_use = 0;
615 GC_atomic_in_use = 0;
616 /* Clear reclaim- and free-lists */
617 for (kind = 0; kind < GC_n_kinds; kind++) {
620 struct hblk ** rlist = GC_obj_kinds[kind].ok_reclaim_list;
621 GC_bool should_clobber = (GC_obj_kinds[kind].ok_descriptor != 0);
623 if (rlist == 0) continue; /* This kind not used. */
624 if (!report_if_found) {
625 lim = &(GC_obj_kinds[kind].ok_freelist[MAXOBJGRANULES+1]);
626 for( fop = GC_obj_kinds[kind].ok_freelist; fop < lim; fop++ ) {
628 if (should_clobber) {
629 GC_clear_fl_links(fop);
635 } /* otherwise free list objects are marked, */
636 /* and its safe to leave them */
637 BZERO(rlist, (MAXOBJGRANULES + 1) * sizeof(void *));
641 /* Go through all heap blocks (in hblklist) and reclaim unmarked objects */
642 /* or enqueue the block for later processing. */
643 GC_apply_to_all_blocks(GC_reclaim_block, (word)report_if_found);
646 /* This is a very stupid thing to do. We make it possible anyway, */
647 /* so that you can convince yourself that it really is very stupid. */
648 GC_reclaim_all((GC_stop_func)0, FALSE);
649 # elif defined(MARK_UNCONDITIONALLY)
650 /* However, make sure to clear reclaimable objects of kinds with */
651 /* unconditional marking enabled before we do any significant */
653 GC_reclaim_unconditionally_marked();
655 # if defined(PARALLEL_MARK)
656 GC_ASSERT(0 == GC_fl_builder_count);
662 * Sweep blocks of the indicated object size and kind until either the
663 * appropriate free list is nonempty, or there are no more blocks to
666 GC_INNER void GC_continue_reclaim(size_t sz /* granules */, int kind)
670 struct obj_kind * ok = &(GC_obj_kinds[kind]);
671 struct hblk ** rlh = ok -> ok_reclaim_list;
672 void **flh = &(ok -> ok_freelist[sz]);
674 if (rlh == 0) return; /* No blocks of this kind. */
676 while ((hbp = *rlh) != 0) {
678 *rlh = hhdr -> hb_next;
679 GC_reclaim_small_nonempty_block(hbp, FALSE);
680 if (*flh != 0) break;
685 * Reclaim all small blocks waiting to be reclaimed.
686 * Abort and return FALSE when/if (*stop_func)() returns TRUE.
687 * If this returns TRUE, then it's safe to restart the world
688 * with incorrectly cleared mark bits.
689 * If ignore_old is TRUE, then reclaim only blocks that have been
690 * recently reclaimed, and discard the rest.
691 * Stop_func may be 0.
693 GC_INNER GC_bool GC_reclaim_all(GC_stop_func stop_func, GC_bool ignore_old)
699 struct obj_kind * ok;
702 # ifndef SMALL_CONFIG
703 CLOCK_TYPE start_time = 0; /* initialized to prevent warning. */
704 CLOCK_TYPE done_time;
706 if (GC_print_stats == VERBOSE)
707 GET_TIME(start_time);
710 for (kind = 0; kind < GC_n_kinds; kind++) {
711 ok = &(GC_obj_kinds[kind]);
712 rlp = ok -> ok_reclaim_list;
713 if (rlp == 0) continue;
714 for (sz = 1; sz <= MAXOBJGRANULES; sz++) {
716 while ((hbp = *rlh) != 0) {
717 if (stop_func != (GC_stop_func)0 && (*stop_func)()) {
721 *rlh = hhdr -> hb_next;
722 if (!ignore_old || hhdr -> hb_last_reclaimed == GC_gc_no - 1) {
723 /* It's likely we'll need it this time, too */
724 /* It's been touched recently, so this */
725 /* shouldn't trigger paging. */
726 GC_reclaim_small_nonempty_block(hbp, FALSE);
731 # ifndef SMALL_CONFIG
732 if (GC_print_stats == VERBOSE) {
734 GC_log_printf("Disposing of reclaim lists took %lu msecs\n",
735 MS_TIME_DIFF(done_time,start_time));
741 #if !defined(EAGER_SWEEP) && defined(MARK_UNCONDITIONALLY)
742 /* We do an eager sweep on heap blocks where unconditional marking has */
743 /* been enabled, so that any reclaimable objects have been reclaimed */
744 /* before we start marking. This is a simplified GC_reclaim_all */
745 /* restricted to kinds where ok_mark_unconditionally is true. */
746 void GC_reclaim_unconditionally_marked(void)
752 struct obj_kind * ok;
755 for (kind = 0; kind < GC_n_kinds; kind++) {
756 ok = &(GC_obj_kinds[kind]);
757 if (!ok->ok_mark_unconditionally) continue;
758 rlp = ok->ok_reclaim_list;
759 if (rlp == 0) continue;
760 for (sz = 1; sz <= MAXOBJGRANULES; sz++) {
762 while ((hbp = *rlh) != 0) {
764 *rlh = hhdr->hb_next;
765 GC_reclaim_small_nonempty_block(hbp, FALSE);