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"
18 #include "gc_disclaim.h"
22 GC_INNER signed_word GC_bytes_found = 0;
23 /* Number of bytes of memory reclaimed */
24 /* minus the number of bytes originally */
25 /* on free lists which we had to drop. */
27 #if defined(PARALLEL_MARK)
28 GC_INNER word GC_fl_builder_count = 0;
29 /* Number of threads currently building free lists without */
30 /* holding GC lock. It is not safe to collect if this is */
32 #endif /* PARALLEL_MARK */
34 /* We defer printing of leaked objects until we're done with the GC */
35 /* cycle, since the routine for printing objects needs to run outside */
36 /* the collector, e.g. without the allocation lock. */
38 # define MAX_LEAKED 40
40 STATIC ptr_t GC_leaked[MAX_LEAKED] = { NULL };
41 STATIC unsigned GC_n_leaked = 0;
43 GC_INNER GC_bool GC_have_errors = FALSE;
45 #if !defined(EAGER_SWEEP) && defined(MARK_UNCONDITIONALLY)
46 void GC_reclaim_unconditionally_marked(void);
49 GC_INLINE void GC_add_leaked(ptr_t leaked)
51 # ifndef SHORT_DBG_HDRS
52 if (GC_findleak_delay_free && !GC_check_leaked(leaked))
56 GC_have_errors = TRUE;
57 /* FIXME: Prevent adding an object while printing leaked ones. */
58 if (GC_n_leaked < MAX_LEAKED) {
59 GC_leaked[GC_n_leaked++] = leaked;
60 /* Make sure it's not reclaimed this cycle */
61 GC_set_mark_bit(leaked);
65 /* Print all objects on the list after printing any smashed objects. */
66 /* Clear both lists. Called without the allocation lock held. */
67 GC_INNER void GC_print_all_errors(void)
69 static GC_bool printing_errors = FALSE;
75 if (printing_errors) {
79 have_errors = GC_have_errors;
80 printing_errors = TRUE;
83 if (GC_debugging_started) {
84 GC_print_all_smashed();
89 for (i = 0; i < GC_n_leaked; ++i) {
90 ptr_t p = GC_leaked[i];
91 if (HDR(p) -> hb_obj_kind == PTRFREE) {
92 GC_err_printf("Leaked atomic object at ");
94 GC_err_printf("Leaked composite object at ");
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 /* XXX: Only if reclaim notifiers have not been run. */
126 # ifdef ENABLE_DISCLAIM
127 if (hhdr -> hb_flags & HAS_DISCLAIM)
130 return (hhdr -> hb_n_marks == 0);
133 STATIC GC_bool GC_block_nearly_full(hdr *hhdr)
135 return (hhdr -> hb_n_marks > 7 * HBLK_OBJS(hhdr -> hb_sz)/8);
138 /* FIXME: This should perhaps again be specialized for USE_MARK_BYTES */
139 /* and USE_MARK_BITS cases. */
142 * Restore unmarked small objects in h of size sz to the object
143 * free list. Returns the new list.
144 * Clears unmarked objects. Sz is in bytes.
146 STATIC ptr_t GC_reclaim_clear(struct hblk *hbp, hdr *hhdr, size_t sz,
147 ptr_t list, signed_word *count)
151 signed_word n_bytes_found = 0;
153 GC_ASSERT(hhdr == GC_find_header((ptr_t)hbp));
154 GC_ASSERT(sz == hhdr -> hb_sz);
155 GC_ASSERT((sz & (BYTES_PER_WORD-1)) == 0);
156 p = (word *)(hbp->hb_body);
157 plim = (word *)(hbp->hb_body + HBLKSIZE - sz);
159 /* go through all words in block */
161 if( mark_bit_from_hdr(hhdr, bit_no) ) {
162 p = (word *)((ptr_t)p + sz);
165 /* object is available - put on list */
168 /* Clear object, advance p to next object in the process */
169 q = (word *)((ptr_t)p + sz);
170 # ifdef USE_MARK_BYTES
172 && !((word)p & (2 * sizeof(word) - 1)));
180 p++; /* Skip link field */
186 bit_no += MARK_BIT_OFFSET(sz);
188 *count += n_bytes_found;
192 /* The same thing, but don't clear objects: */
193 STATIC ptr_t GC_reclaim_uninit(struct hblk *hbp, hdr *hhdr, size_t sz,
194 ptr_t list, signed_word *count)
198 signed_word n_bytes_found = 0;
200 GC_ASSERT(sz == hhdr -> hb_sz);
201 p = (word *)(hbp->hb_body);
202 plim = (word *)((ptr_t)hbp + HBLKSIZE - sz);
204 /* go through all words in block */
206 if( !mark_bit_from_hdr(hhdr, bit_no) ) {
208 /* object is available - put on list */
212 p = (word *)((ptr_t)p + sz);
213 bit_no += MARK_BIT_OFFSET(sz);
215 *count += n_bytes_found;
219 #ifdef ENABLE_DISCLAIM
220 /* Call reclaim notifier for block's kind on each unmarked object in */
221 /* block, all within a pair of corresponding enter/leave callbacks. */
222 ptr_t GC_disclaim_and_reclaim(struct hblk *hbp, hdr *hhdr, size_t sz,
223 ptr_t list, signed_word *count)
225 register int bit_no = 0;
226 register word *p, *q, *plim;
227 signed_word n_bytes_found = 0;
228 struct obj_kind *ok = &GC_obj_kinds[hhdr->hb_obj_kind];
229 int (*proc)(void *, void *) = ok -> ok_disclaim_proc;
230 void *cd = ok -> ok_disclaim_cd;
232 GC_ASSERT(sz == hhdr -> hb_sz);
233 p = (word *)(hbp -> hb_body);
234 plim = (word *)((ptr_t)p + HBLKSIZE - sz);
237 if (mark_bit_from_hdr(hhdr, bit_no) || (*proc)(p, cd))
238 p = (word *)((ptr_t)p + sz);
241 /* object is available - put on list */
244 /* Clear object, advance p to next object in the process */
245 q = (word *)((ptr_t)p + sz);
246 # ifdef USE_MARK_BYTES
248 && !((word)p & (2 * sizeof(word) - 1)));
256 p++; /* Skip link field */
262 bit_no += MARK_BIT_OFFSET(sz);
264 *count += n_bytes_found;
267 #endif /* ENABLE_DISCLAIM */
269 /* Don't really reclaim objects, just check for unmarked ones: */
270 STATIC void GC_reclaim_check(struct hblk *hbp, hdr *hhdr, word sz)
274 GC_ASSERT(sz == hhdr -> hb_sz);
276 /* go through all words in block */
278 plim = p + HBLKSIZE - sz;
279 for (bit_no = 0; p <= plim; 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)
303 result = GC_disclaim_and_reclaim(hbp, hhdr, sz, list, count);
306 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);
341 * Restore an unmarked large object or an entirely empty blocks of small objects
342 * to the heap block free list.
343 * Otherwise enqueue the block for later processing
344 * by GC_reclaim_small_nonempty_block.
345 * If report_if_found is TRUE, then process any block immediately, and
346 * simply report free objects; do not actually reclaim them.
348 STATIC void GC_reclaim_block(struct hblk *hbp, word report_if_found)
350 hdr * hhdr = HDR(hbp);
351 size_t sz = hhdr -> hb_sz; /* size of objects in current block */
352 struct obj_kind * ok = &GC_obj_kinds[hhdr -> hb_obj_kind];
355 if( sz > MAXOBJBYTES ) { /* 1 big object */
356 if( !mark_bit_from_hdr(hhdr, 0) ) {
357 if (report_if_found) {
358 GC_add_leaked((ptr_t)hbp);
360 size_t blocks = OBJ_SZ_TO_BLOCKS(sz);
361 # ifdef ENABLE_DISCLAIM
362 if (EXPECT(hhdr->hb_flags & HAS_DISCLAIM, 0)) {
363 struct obj_kind *ok = &GC_obj_kinds[hhdr->hb_obj_kind];
365 resurrect = (*ok->ok_disclaim_proc)(hbp, ok->ok_disclaim_cd);
367 set_mark_bit_from_hdr(hhdr, 0);
368 /* excuse me, */ goto in_use;
373 GC_large_allocd_bytes -= blocks * HBLKSIZE;
375 GC_bytes_found += sz;
380 if (hhdr -> hb_descr != 0) {
381 GC_composite_in_use += sz;
383 GC_atomic_in_use += sz;
387 GC_bool empty = GC_block_empty(hhdr);
388 # ifdef PARALLEL_MARK
389 /* Count can be low or one too high because we sometimes */
390 /* have to ignore decrements. Objects can also potentially */
391 /* be repeatedly marked by each marker. */
392 /* Here we assume two markers, but this is extremely */
393 /* unlikely to fail spuriously with more. And if it does, it */
394 /* should be looked at. */
395 GC_ASSERT(hhdr -> hb_n_marks <= 2 * (HBLKSIZE/sz + 1) + 16);
397 GC_ASSERT(sz * hhdr -> hb_n_marks <= HBLKSIZE);
399 if (hhdr -> hb_descr != 0) {
400 GC_composite_in_use += sz * hhdr -> hb_n_marks;
402 GC_atomic_in_use += sz * hhdr -> hb_n_marks;
404 if (report_if_found) {
405 GC_reclaim_small_nonempty_block(hbp, TRUE /* report_if_found */);
407 GC_bytes_found += HBLKSIZE;
409 } else if (GC_find_leak || !GC_block_nearly_full(hhdr)) {
410 /* group of smaller objects, enqueue the real work */
411 rlh = &(ok -> ok_reclaim_list[BYTES_TO_GRANULES(sz)]);
412 hhdr -> hb_next = *rlh;
414 } /* else not worth salvaging. */
415 /* We used to do the nearly_full check later, but we */
416 /* already have the right cache context here. Also */
417 /* doing it here avoids some silly lock contention in */
418 /* GC_malloc_many. */
422 #if !defined(NO_DEBUGGING)
423 /* Routines to gather and print heap block info */
424 /* intended for debugging. Otherwise should be called */
429 size_t number_of_blocks;
433 #ifdef USE_MARK_BYTES
435 /* Return the number of set mark bits in the given header */
436 STATIC int GC_n_set_marks(hdr *hhdr)
440 size_t sz = hhdr -> hb_sz;
441 int offset = (int)MARK_BIT_OFFSET(sz);
442 int limit = (int)FINAL_MARK_BIT(sz);
444 for (i = 0; i < limit; i += offset) {
445 result += hhdr -> hb_marks[i];
447 GC_ASSERT(hhdr -> hb_marks[limit]);
453 /* Number of set bits in a word. Not performance critical. */
454 static int set_bits(word n)
466 /* Return the number of set mark bits in the given header */
467 STATIC int GC_n_set_marks(hdr *hhdr)
472 # ifdef MARK_BIT_PER_OBJ
473 int n_objs = (int)HBLK_OBJS(hhdr -> hb_sz);
475 if (0 == n_objs) n_objs = 1;
476 n_mark_words = divWORDSZ(n_objs + WORDSZ - 1);
477 # else /* MARK_BIT_PER_GRANULE */
478 n_mark_words = MARK_BITS_SZ;
480 for (i = 0; i < n_mark_words - 1; i++) {
481 result += set_bits(hhdr -> hb_marks[i]);
483 # ifdef MARK_BIT_PER_OBJ
484 result += set_bits((hhdr -> hb_marks[n_mark_words - 1])
485 << (n_mark_words * WORDSZ - n_objs));
487 result += set_bits(hhdr -> hb_marks[n_mark_words - 1]);
492 #endif /* !USE_MARK_BYTES */
494 STATIC void GC_print_block_descr(struct hblk *h,
495 word /* struct PrintStats */ raw_ps)
498 size_t bytes = hhdr -> hb_sz;
499 struct Print_stats *ps;
500 unsigned n_marks = GC_n_set_marks(hhdr);
502 if (hhdr -> hb_n_marks != n_marks) {
503 GC_printf("(%u:%u,%u!=%u)", hhdr -> hb_obj_kind, (unsigned)bytes,
504 (unsigned)hhdr -> hb_n_marks, n_marks);
506 GC_printf("(%u:%u,%u)", hhdr -> hb_obj_kind,
507 (unsigned)bytes, n_marks);
510 bytes &= ~(HBLKSIZE-1);
512 ps = (struct Print_stats *)raw_ps;
513 ps->total_bytes += bytes;
514 ps->number_of_blocks++;
517 void GC_print_block_list(void)
519 struct Print_stats pstats;
521 GC_printf("(kind(0=ptrfree,1=normal,2=unc.):size_in_bytes, #_marks_set)\n");
522 pstats.number_of_blocks = 0;
523 pstats.total_bytes = 0;
524 GC_apply_to_all_blocks(GC_print_block_descr, (word)&pstats);
525 GC_printf("\nblocks = %lu, bytes = %lu\n",
526 (unsigned long)pstats.number_of_blocks,
527 (unsigned long)pstats.total_bytes);
530 /* Currently for debugger use only: */
531 void GC_print_free_list(int kind, size_t sz_in_granules)
533 struct obj_kind * ok = &GC_obj_kinds[kind];
534 ptr_t flh = ok -> ok_freelist[sz_in_granules];
535 struct hblk *lastBlock = 0;
538 for (n = 1; flh; n++) {
539 struct hblk *block = HBLKPTR(flh);
540 if (block != lastBlock) {
541 GC_printf("\nIn heap block at %p:\n\t", (void *)block);
544 GC_printf("%d: %p;", n, flh);
549 #endif /* !NO_DEBUGGING */
552 * Clear all obj_link pointers in the list of free objects *flp.
554 * This must be done before dropping a list of free gcj-style objects,
555 * since may otherwise end up with dangling "descriptor" pointers.
556 * It may help for other pointer-containing objects.
558 STATIC void GC_clear_fl_links(void **flp)
564 flp = &(obj_link(next));
570 * Perform GC_reclaim_block on the entire heap, after first clearing
571 * small object free lists (if we are not just looking for leaks).
573 GC_INNER void GC_start_reclaim(GC_bool report_if_found)
577 # if defined(PARALLEL_MARK)
578 GC_ASSERT(0 == GC_fl_builder_count);
580 /* Reset in use counters. GC_reclaim_block recomputes them. */
581 GC_composite_in_use = 0;
582 GC_atomic_in_use = 0;
583 /* Clear reclaim- and free-lists */
584 for (kind = 0; kind < GC_n_kinds; kind++) {
587 struct hblk ** rlist = GC_obj_kinds[kind].ok_reclaim_list;
588 GC_bool should_clobber = (GC_obj_kinds[kind].ok_descriptor != 0);
590 if (rlist == 0) continue; /* This kind not used. */
591 if (!report_if_found) {
592 lim = &(GC_obj_kinds[kind].ok_freelist[MAXOBJGRANULES+1]);
593 for( fop = GC_obj_kinds[kind].ok_freelist; fop < lim; fop++ ) {
595 if (should_clobber) {
596 GC_clear_fl_links(fop);
602 } /* otherwise free list objects are marked, */
603 /* and its safe to leave them */
604 BZERO(rlist, (MAXOBJGRANULES + 1) * sizeof(void *));
608 /* Go through all heap blocks (in hblklist) and reclaim unmarked objects */
609 /* or enqueue the block for later processing. */
610 GC_apply_to_all_blocks(GC_reclaim_block, (word)report_if_found);
613 /* This is a very stupid thing to do. We make it possible anyway, */
614 /* so that you can convince yourself that it really is very stupid. */
615 GC_reclaim_all((GC_stop_func)0, FALSE);
616 # elif defined(MARK_UNCONDITIONALLY)
617 /* However, make sure to clear reclaimable objects of kinds with */
618 /* unconditional marking enabled before we do any significant */
620 GC_reclaim_unconditionally_marked();
622 # if defined(PARALLEL_MARK)
623 GC_ASSERT(0 == GC_fl_builder_count);
629 * Sweep blocks of the indicated object size and kind until either the
630 * appropriate free list is nonempty, or there are no more blocks to
633 GC_INNER void GC_continue_reclaim(size_t sz /* granules */, int kind)
637 struct obj_kind * ok = &(GC_obj_kinds[kind]);
638 struct hblk ** rlh = ok -> ok_reclaim_list;
639 void **flh = &(ok -> ok_freelist[sz]);
641 if (rlh == 0) return; /* No blocks of this kind. */
643 while ((hbp = *rlh) != 0) {
645 *rlh = hhdr -> hb_next;
646 GC_reclaim_small_nonempty_block(hbp, FALSE);
647 if (*flh != 0) break;
652 * Reclaim all small blocks waiting to be reclaimed.
653 * Abort and return FALSE when/if (*stop_func)() returns TRUE.
654 * If this returns TRUE, then it's safe to restart the world
655 * with incorrectly cleared mark bits.
656 * If ignore_old is TRUE, then reclaim only blocks that have been
657 * recently reclaimed, and discard the rest.
658 * Stop_func may be 0.
660 GC_INNER GC_bool GC_reclaim_all(GC_stop_func stop_func, GC_bool ignore_old)
666 struct obj_kind * ok;
669 # ifndef SMALL_CONFIG
670 CLOCK_TYPE start_time = 0; /* initialized to prevent warning. */
671 CLOCK_TYPE done_time;
673 if (GC_print_stats == VERBOSE)
674 GET_TIME(start_time);
677 for (kind = 0; kind < GC_n_kinds; kind++) {
678 ok = &(GC_obj_kinds[kind]);
679 rlp = ok -> ok_reclaim_list;
680 if (rlp == 0) continue;
681 for (sz = 1; sz <= MAXOBJGRANULES; sz++) {
683 while ((hbp = *rlh) != 0) {
684 if (stop_func != (GC_stop_func)0 && (*stop_func)()) {
688 *rlh = hhdr -> hb_next;
689 if (!ignore_old || hhdr -> hb_last_reclaimed == GC_gc_no - 1) {
690 /* It's likely we'll need it this time, too */
691 /* It's been touched recently, so this */
692 /* shouldn't trigger paging. */
693 GC_reclaim_small_nonempty_block(hbp, FALSE);
698 # ifndef SMALL_CONFIG
699 if (GC_print_stats == VERBOSE) {
701 GC_log_printf("Disposing of reclaim lists took %lu msecs\n",
702 MS_TIME_DIFF(done_time,start_time));
708 #if !defined(EAGER_SWEEP) && defined(MARK_UNCONDITIONALLY)
709 /* Part of disclaim patch. This could be merged with the above, but I don't
710 * want to clobber the original source too much by changing the prototype and
711 * testing for defined(MARK_UNCONDITIONALLY). */
712 void GC_reclaim_unconditionally_marked(void)
718 struct obj_kind * ok;
721 for (kind = 0; kind < GC_n_kinds; kind++) {
722 ok = &(GC_obj_kinds[kind]);
723 if (!ok->ok_mark_unconditionally) continue;
724 rlp = ok->ok_reclaim_list;
725 if (rlp == 0) continue;
726 for (sz = 1; sz <= MAXOBJGRANULES; sz++) {
728 while ((hbp = *rlh) != 0) {
730 *rlh = hhdr->hb_next;
731 GC_reclaim_small_nonempty_block(hbp, FALSE, &GC_bytes_found);