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 #include "gc_disclaim.h" // FIXME: add ifdef
23 GC_INNER signed_word GC_bytes_found = 0;
24 /* Number of bytes of memory reclaimed */
25 /* minus the number of bytes originally */
26 /* on free lists which we had to drop. */
28 #if defined(PARALLEL_MARK)
29 GC_INNER word GC_fl_builder_count = 0;
30 /* Number of threads currently building free lists without */
31 /* holding GC lock. It is not safe to collect if this is */
33 #endif /* PARALLEL_MARK */
35 /* We defer printing of leaked objects until we're done with the GC */
36 /* cycle, since the routine for printing objects needs to run outside */
37 /* the collector, e.g. without the allocation lock. */
39 # define MAX_LEAKED 40
41 STATIC ptr_t GC_leaked[MAX_LEAKED] = { NULL };
42 STATIC unsigned GC_n_leaked = 0;
44 GC_INNER GC_bool GC_have_errors = FALSE;
46 #if !defined(EAGER_SWEEP) && defined(MARK_UNCONDITIONALLY)
47 void GC_reclaim_unconditionally_marked(void);
50 GC_INLINE void GC_add_leaked(ptr_t leaked)
52 # ifndef SHORT_DBG_HDRS
53 if (GC_findleak_delay_free && !GC_check_leaked(leaked))
57 GC_have_errors = TRUE;
58 /* FIXME: Prevent adding an object while printing leaked ones. */
59 if (GC_n_leaked < MAX_LEAKED) {
60 GC_leaked[GC_n_leaked++] = leaked;
61 /* Make sure it's not reclaimed this cycle */
62 GC_set_mark_bit(leaked);
66 /* Print all objects on the list after printing any smashed objects. */
67 /* Clear both lists. Called without the allocation lock held. */
68 GC_INNER void GC_print_all_errors(void)
70 static GC_bool printing_errors = FALSE;
76 if (printing_errors) {
80 have_errors = GC_have_errors;
81 printing_errors = TRUE;
84 if (GC_debugging_started) {
85 GC_print_all_smashed();
90 for (i = 0; i < GC_n_leaked; ++i) {
91 ptr_t p = GC_leaked[i];
92 if (HDR(p) -> hb_obj_kind == PTRFREE) {
93 GC_err_printf("Leaked atomic object at ");
95 GC_err_printf("Leaked composite object at ");
106 # ifndef GC_ABORT_ON_LEAK
107 && GETENV("GC_ABORT_ON_LEAK") != NULL
110 ABORT("Leaked or smashed objects encountered");
113 printing_errors = FALSE;
122 /* Test whether a block is completely empty, i.e. contains no marked */
123 /* objects. This does not require the block to be in physical memory. */
124 GC_INNER GC_bool GC_block_empty(hdr *hhdr)
126 return (hhdr -> hb_n_marks == 0);
129 STATIC GC_bool GC_block_nearly_full(hdr *hhdr)
131 return (hhdr -> hb_n_marks > 7 * HBLK_OBJS(hhdr -> hb_sz)/8);
134 /* FIXME: This should perhaps again be specialized for USE_MARK_BYTES */
135 /* and USE_MARK_BITS cases. */
138 * Restore unmarked small objects in h of size sz to the object
139 * free list. Returns the new list.
140 * Clears unmarked objects. Sz is in bytes.
142 STATIC ptr_t GC_reclaim_clear(struct hblk *hbp, hdr *hhdr, size_t sz,
143 ptr_t list, signed_word *count)
147 signed_word n_bytes_found = 0;
149 GC_ASSERT(hhdr == GC_find_header((ptr_t)hbp));
150 GC_ASSERT(sz == hhdr -> hb_sz);
151 GC_ASSERT((sz & (BYTES_PER_WORD-1)) == 0);
152 p = (word *)(hbp->hb_body);
153 plim = (word *)(hbp->hb_body + HBLKSIZE - sz);
155 /* go through all words in block */
157 if( mark_bit_from_hdr(hhdr, bit_no) ) {
158 p = (word *)((ptr_t)p + sz);
161 /* object is available - put on list */
164 /* Clear object, advance p to next object in the process */
165 q = (word *)((ptr_t)p + sz);
166 # ifdef USE_MARK_BYTES
168 && !((word)p & (2 * sizeof(word) - 1)));
176 p++; /* Skip link field */
182 bit_no += MARK_BIT_OFFSET(sz);
184 *count += n_bytes_found;
188 /* The same thing, but don't clear objects: */
189 STATIC ptr_t GC_reclaim_uninit(struct hblk *hbp, hdr *hhdr, size_t sz,
190 ptr_t list, signed_word *count)
194 signed_word n_bytes_found = 0;
196 GC_ASSERT(sz == hhdr -> hb_sz);
197 p = (word *)(hbp->hb_body);
198 plim = (word *)((ptr_t)hbp + HBLKSIZE - sz);
200 /* go through all words in block */
202 if( !mark_bit_from_hdr(hhdr, bit_no) ) {
204 /* object is available - put on list */
208 p = (word *)((ptr_t)p + sz);
209 bit_no += MARK_BIT_OFFSET(sz);
211 *count += n_bytes_found;
215 #ifdef ENABLE_DISCLAIM
216 /* Call reclaim notifier for block's kind on each unmarked object in */
217 /* block, all within a pair of corresponding enter/leave callbacks. */
218 STATIC ptr_t GC_disclaim_and_reclaim(struct hblk *hbp, hdr *hhdr, size_t sz,
219 ptr_t list, signed_word *count)
221 register int bit_no = 0; // FIXME: remove register
222 register word *p, *q, *plim;
223 signed_word n_bytes_found = 0;
224 struct obj_kind *ok = &GC_obj_kinds[hhdr->hb_obj_kind];
225 int (*proc)(void *, void *) = ok -> ok_disclaim_proc;
226 void *cd = ok -> ok_disclaim_cd;
228 GC_ASSERT(sz == hhdr -> hb_sz);
229 p = (word *)(hbp -> hb_body);
230 plim = (word *)((ptr_t)p + HBLKSIZE - sz);
233 int marked = mark_bit_from_hdr(hhdr, bit_no);
234 if (!marked && (*proc)(p, cd)) {
235 hhdr -> hb_n_marks++;
239 p = (word *)((ptr_t)p + sz);
242 /* object is available - put on list */
245 /* Clear object, advance p to next object in the process */
246 q = (word *)((ptr_t)p + sz);
247 # ifdef USE_MARK_BYTES
249 && !((word)p & (2 * sizeof(word) - 1)));
257 p++; /* Skip link field */
263 bit_no += MARK_BIT_OFFSET(sz);
265 *count += n_bytes_found;
268 #endif /* ENABLE_DISCLAIM */
270 /* Don't really reclaim objects, just check for unmarked ones: */
271 STATIC void GC_reclaim_check(struct hblk *hbp, hdr *hhdr, word sz)
275 GC_ASSERT(sz == hhdr -> hb_sz);
277 /* go through all words in block */
279 plim = p + HBLKSIZE - sz;
280 for (bit_no = 0; p <= plim; p += sz, bit_no += MARK_BIT_OFFSET(sz)) {
281 if (!mark_bit_from_hdr(hhdr, bit_no)) {
288 * Generic procedure to rebuild a free list in hbp.
289 * Also called directly from GC_malloc_many.
290 * Sz is now in bytes.
292 GC_INNER ptr_t GC_reclaim_generic(struct hblk * hbp, hdr *hhdr, size_t sz,
293 GC_bool init, ptr_t list,
298 GC_ASSERT(GC_find_header((ptr_t)hbp) == hhdr);
299 # ifndef GC_DISABLE_INCREMENTAL
300 GC_remove_protection(hbp, 1, (hhdr)->hb_descr == 0 /* Pointer-free? */);
302 # ifdef ENABLE_DISCLAIM
303 if (hhdr -> hb_flags & HAS_DISCLAIM) {
304 result = GC_disclaim_and_reclaim(hbp, hhdr, sz, list, count);
307 /* else */ if (init || GC_debugging_started) {
308 result = GC_reclaim_clear(hbp, hhdr, sz, list, count);
310 GC_ASSERT((hhdr)->hb_descr == 0 /* Pointer-free block */);
311 result = GC_reclaim_uninit(hbp, hhdr, sz, list, count);
313 if (IS_UNCOLLECTABLE(hhdr -> hb_obj_kind)) GC_set_hdr_marks(hhdr);
318 * Restore unmarked small objects in the block pointed to by hbp
319 * to the appropriate object free list.
320 * If entirely empty blocks are to be completely deallocated, then
321 * caller should perform that check.
323 STATIC void GC_reclaim_small_nonempty_block(struct hblk *hbp,
324 GC_bool report_if_found)
326 hdr *hhdr = HDR(hbp);
327 size_t sz = hhdr -> hb_sz;
328 struct obj_kind * ok = &GC_obj_kinds[hhdr -> hb_obj_kind];
329 void **flh = &(ok -> ok_freelist[BYTES_TO_GRANULES(sz)]);
331 hhdr -> hb_last_reclaimed = (unsigned short) GC_gc_no;
333 if (report_if_found) {
334 GC_reclaim_check(hbp, hhdr, sz);
336 *flh = GC_reclaim_generic(hbp, hhdr, sz, ok -> ok_init,
337 *flh, &GC_bytes_found);
341 #ifdef ENABLE_DISCLAIM
342 STATIC void GC_disclaim_and_reclaim_or_free_small_block(struct hblk *hbp)
344 hdr *hhdr = HDR(hbp);
345 size_t sz = hhdr -> hb_sz;
346 struct obj_kind * ok = &GC_obj_kinds[hhdr -> hb_obj_kind];
347 void **flh = &(ok -> ok_freelist[BYTES_TO_GRANULES(sz)]);
350 hhdr -> hb_last_reclaimed = (unsigned short) GC_gc_no;
351 flh_next = GC_reclaim_generic(hbp, hhdr, sz, ok -> ok_init,
352 *flh, &GC_bytes_found);
353 if (hhdr -> hb_n_marks)
356 GC_bytes_found += HBLKSIZE;
360 #endif /* ENABLE_DISCLAIM */
363 * Restore an unmarked large object or an entirely empty blocks of small objects
364 * to the heap block free list.
365 * Otherwise enqueue the block for later processing
366 * by GC_reclaim_small_nonempty_block.
367 * If report_if_found is TRUE, then process any block immediately, and
368 * simply report free objects; do not actually reclaim them.
370 STATIC void GC_reclaim_block(struct hblk *hbp, word report_if_found)
372 hdr * hhdr = HDR(hbp);
373 size_t sz = hhdr -> hb_sz; /* size of objects in current block */
374 struct obj_kind * ok = &GC_obj_kinds[hhdr -> hb_obj_kind];
377 if( sz > MAXOBJBYTES ) { /* 1 big object */
378 if( !mark_bit_from_hdr(hhdr, 0) ) {
379 if (report_if_found) {
380 GC_add_leaked((ptr_t)hbp);
382 size_t blocks = OBJ_SZ_TO_BLOCKS(sz);
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, ok->ok_disclaim_cd)) {
387 /* Not disclaimed => resurrect the object. */
388 set_mark_bit_from_hdr(hhdr, 0);
394 GC_large_allocd_bytes -= blocks * HBLKSIZE;
396 GC_bytes_found += sz;
400 # ifdef ENABLE_DISCLAIM
403 if (hhdr -> hb_descr != 0) {
404 GC_composite_in_use += sz;
406 GC_atomic_in_use += sz;
410 GC_bool empty = GC_block_empty(hhdr);
411 # ifdef PARALLEL_MARK
412 /* Count can be low or one too high because we sometimes */
413 /* have to ignore decrements. Objects can also potentially */
414 /* be repeatedly marked by each marker. */
415 /* Here we assume two markers, but this is extremely */
416 /* unlikely to fail spuriously with more. And if it does, it */
417 /* should be looked at. */
418 GC_ASSERT(hhdr -> hb_n_marks <= 2 * (HBLKSIZE/sz + 1) + 16);
420 GC_ASSERT(sz * hhdr -> hb_n_marks <= HBLKSIZE);
422 if (report_if_found) {
423 GC_reclaim_small_nonempty_block(hbp, TRUE /* report_if_found */);
425 # ifdef ENABLE_DISCLAIM
426 if ((hhdr -> hb_flags & HAS_DISCLAIM) != 0) {
427 GC_disclaim_and_reclaim_or_free_small_block(hbp);
431 GC_bytes_found += HBLKSIZE;
434 } else if (GC_find_leak || !GC_block_nearly_full(hhdr)) {
435 /* group of smaller objects, enqueue the real work */
436 rlh = &(ok -> ok_reclaim_list[BYTES_TO_GRANULES(sz)]);
437 hhdr -> hb_next = *rlh;
439 } /* else not worth salvaging. */
440 /* We used to do the nearly_full check later, but we */
441 /* already have the right cache context here. Also */
442 /* doing it here avoids some silly lock contention in */
443 /* GC_malloc_many. */
445 if (hhdr -> hb_descr != 0) {
446 GC_composite_in_use += sz * hhdr -> hb_n_marks;
448 GC_atomic_in_use += sz * hhdr -> hb_n_marks;
453 #if !defined(NO_DEBUGGING)
454 /* Routines to gather and print heap block info */
455 /* intended for debugging. Otherwise should be called */
460 size_t number_of_blocks;
464 #ifdef USE_MARK_BYTES
466 /* Return the number of set mark bits in the given header */
467 STATIC int GC_n_set_marks(hdr *hhdr)
471 size_t sz = hhdr -> hb_sz;
472 int offset = (int)MARK_BIT_OFFSET(sz);
473 int limit = (int)FINAL_MARK_BIT(sz);
475 for (i = 0; i < limit; i += offset) {
476 result += hhdr -> hb_marks[i];
478 GC_ASSERT(hhdr -> hb_marks[limit]);
484 /* Number of set bits in a word. Not performance critical. */
485 static int set_bits(word n)
497 /* Return the number of set mark bits in the given header */
498 STATIC int GC_n_set_marks(hdr *hhdr)
503 # ifdef MARK_BIT_PER_OBJ
504 int n_objs = (int)HBLK_OBJS(hhdr -> hb_sz);
506 if (0 == n_objs) n_objs = 1;
507 n_mark_words = divWORDSZ(n_objs + WORDSZ - 1);
508 # else /* MARK_BIT_PER_GRANULE */
509 n_mark_words = MARK_BITS_SZ;
511 for (i = 0; i < n_mark_words - 1; i++) {
512 result += set_bits(hhdr -> hb_marks[i]);
514 # ifdef MARK_BIT_PER_OBJ
515 result += set_bits((hhdr -> hb_marks[n_mark_words - 1])
516 << (n_mark_words * WORDSZ - n_objs));
518 result += set_bits(hhdr -> hb_marks[n_mark_words - 1]);
523 #endif /* !USE_MARK_BYTES */
525 STATIC void GC_print_block_descr(struct hblk *h,
526 word /* struct PrintStats */ raw_ps)
529 size_t bytes = hhdr -> hb_sz;
530 struct Print_stats *ps;
531 unsigned n_marks = GC_n_set_marks(hhdr);
533 if (hhdr -> hb_n_marks != n_marks) {
534 GC_printf("(%u:%u,%u!=%u)", hhdr -> hb_obj_kind, (unsigned)bytes,
535 (unsigned)hhdr -> hb_n_marks, n_marks);
537 GC_printf("(%u:%u,%u)", hhdr -> hb_obj_kind,
538 (unsigned)bytes, n_marks);
541 bytes &= ~(HBLKSIZE-1);
543 ps = (struct Print_stats *)raw_ps;
544 ps->total_bytes += bytes;
545 ps->number_of_blocks++;
548 void GC_print_block_list(void)
550 struct Print_stats pstats;
552 GC_printf("(kind(0=ptrfree,1=normal,2=unc.):size_in_bytes, #_marks_set)\n");
553 pstats.number_of_blocks = 0;
554 pstats.total_bytes = 0;
555 GC_apply_to_all_blocks(GC_print_block_descr, (word)&pstats);
556 GC_printf("\nblocks = %lu, bytes = %lu\n",
557 (unsigned long)pstats.number_of_blocks,
558 (unsigned long)pstats.total_bytes);
561 /* Currently for debugger use only: */
562 void GC_print_free_list(int kind, size_t sz_in_granules)
564 struct obj_kind * ok = &GC_obj_kinds[kind];
565 ptr_t flh = ok -> ok_freelist[sz_in_granules];
566 struct hblk *lastBlock = 0;
569 for (n = 1; flh; n++) {
570 struct hblk *block = HBLKPTR(flh);
571 if (block != lastBlock) {
572 GC_printf("\nIn heap block at %p:\n\t", block);
575 GC_printf("%d: %p;", n, flh);
580 #endif /* !NO_DEBUGGING */
583 * Clear all obj_link pointers in the list of free objects *flp.
585 * This must be done before dropping a list of free gcj-style objects,
586 * since may otherwise end up with dangling "descriptor" pointers.
587 * It may help for other pointer-containing objects.
589 STATIC void GC_clear_fl_links(void **flp)
595 flp = &(obj_link(next));
601 * Perform GC_reclaim_block on the entire heap, after first clearing
602 * small object free lists (if we are not just looking for leaks).
604 GC_INNER void GC_start_reclaim(GC_bool report_if_found)
608 # if defined(PARALLEL_MARK)
609 GC_ASSERT(0 == GC_fl_builder_count);
611 /* Reset in use counters. GC_reclaim_block recomputes them. */
612 GC_composite_in_use = 0;
613 GC_atomic_in_use = 0;
614 /* Clear reclaim- and free-lists */
615 for (kind = 0; kind < GC_n_kinds; kind++) {
618 struct hblk ** rlist = GC_obj_kinds[kind].ok_reclaim_list;
619 GC_bool should_clobber = (GC_obj_kinds[kind].ok_descriptor != 0);
621 if (rlist == 0) continue; /* This kind not used. */
622 if (!report_if_found) {
623 lim = &(GC_obj_kinds[kind].ok_freelist[MAXOBJGRANULES+1]);
624 for( fop = GC_obj_kinds[kind].ok_freelist; fop < lim; fop++ ) {
626 if (should_clobber) {
627 GC_clear_fl_links(fop);
633 } /* otherwise free list objects are marked, */
634 /* and its safe to leave them */
635 BZERO(rlist, (MAXOBJGRANULES + 1) * sizeof(void *));
639 /* Go through all heap blocks (in hblklist) and reclaim unmarked objects */
640 /* or enqueue the block for later processing. */
641 GC_apply_to_all_blocks(GC_reclaim_block, (word)report_if_found);
644 /* This is a very stupid thing to do. We make it possible anyway, */
645 /* so that you can convince yourself that it really is very stupid. */
646 GC_reclaim_all((GC_stop_func)0, FALSE);
647 # elif defined(MARK_UNCONDITIONALLY)
648 /* However, make sure to clear reclaimable objects of kinds with */
649 /* unconditional marking enabled before we do any significant */
651 GC_reclaim_unconditionally_marked();
653 # if defined(PARALLEL_MARK)
654 GC_ASSERT(0 == GC_fl_builder_count);
660 * Sweep blocks of the indicated object size and kind until either the
661 * appropriate free list is nonempty, or there are no more blocks to
664 GC_INNER void GC_continue_reclaim(size_t sz /* granules */, int kind)
668 struct obj_kind * ok = &(GC_obj_kinds[kind]);
669 struct hblk ** rlh = ok -> ok_reclaim_list;
670 void **flh = &(ok -> ok_freelist[sz]);
672 if (rlh == 0) return; /* No blocks of this kind. */
674 while ((hbp = *rlh) != 0) {
676 *rlh = hhdr -> hb_next;
677 GC_reclaim_small_nonempty_block(hbp, FALSE);
678 if (*flh != 0) break;
683 * Reclaim all small blocks waiting to be reclaimed.
684 * Abort and return FALSE when/if (*stop_func)() returns TRUE.
685 * If this returns TRUE, then it's safe to restart the world
686 * with incorrectly cleared mark bits.
687 * If ignore_old is TRUE, then reclaim only blocks that have been
688 * recently reclaimed, and discard the rest.
689 * Stop_func may be 0.
691 GC_INNER GC_bool GC_reclaim_all(GC_stop_func stop_func, GC_bool ignore_old)
697 struct obj_kind * ok;
700 # ifndef SMALL_CONFIG
701 CLOCK_TYPE start_time = 0; /* initialized to prevent warning. */
702 CLOCK_TYPE done_time;
704 if (GC_print_stats == VERBOSE)
705 GET_TIME(start_time);
708 for (kind = 0; kind < GC_n_kinds; kind++) {
709 ok = &(GC_obj_kinds[kind]);
710 rlp = ok -> ok_reclaim_list;
711 if (rlp == 0) continue;
712 for (sz = 1; sz <= MAXOBJGRANULES; sz++) {
714 while ((hbp = *rlh) != 0) {
715 if (stop_func != (GC_stop_func)0 && (*stop_func)()) {
719 *rlh = hhdr -> hb_next;
720 if (!ignore_old || hhdr -> hb_last_reclaimed == GC_gc_no - 1) {
721 /* It's likely we'll need it this time, too */
722 /* It's been touched recently, so this */
723 /* shouldn't trigger paging. */
724 GC_reclaim_small_nonempty_block(hbp, FALSE);
729 # ifndef SMALL_CONFIG
730 if (GC_print_stats == VERBOSE) {
732 GC_log_printf("Disposing of reclaim lists took %lu msecs\n",
733 MS_TIME_DIFF(done_time,start_time));
739 #if !defined(EAGER_SWEEP) && defined(MARK_UNCONDITIONALLY)
740 /* We do an eager sweep on heap blocks where unconditional marking has */
741 /* been enabled, so that any reclaimable objects have been reclaimed */
742 /* before we start marking. This is a simplified GC_reclaim_all */
743 /* restricted to kinds where ok_mark_unconditionally is true. */
744 void GC_reclaim_unconditionally_marked(void)
750 struct obj_kind * ok;
753 for (kind = 0; kind < GC_n_kinds; kind++) {
754 ok = &(GC_obj_kinds[kind]);
755 if (!ok->ok_mark_unconditionally) continue;
756 rlp = ok->ok_reclaim_list;
757 if (rlp == 0) continue;
758 for (sz = 1; sz <= MAXOBJGRANULES; sz++) {
760 while ((hbp = *rlh) != 0) {
762 *rlh = hhdr->hb_next;
763 GC_reclaim_small_nonempty_block(hbp, FALSE);