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 by Hewlett-Packard Company. All rights reserved.
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.
20 signed_word GC_mem_found
= 0;
21 /* Number of words of memory reclaimed */
23 static void report_leak(p
, sz
)
27 if (HDR(p
) -> hb_obj_kind
== PTRFREE
) {
28 GC_err_printf0("Leaked atomic object at ");
30 GC_err_printf0("Leaked composite object at ");
32 if (GC_debugging_started
&& GC_has_debug_info(p
)) {
35 GC_err_printf2("0x%lx (appr. size = %ld)\n",
37 (unsigned long)WORDS_TO_BYTES(sz
));
41 # define FOUND_FREE(hblk, word_no) \
43 report_leak((ptr_t)hblk + WORDS_TO_BYTES(word_no), \
44 HDR(hblk) -> hb_sz); \
54 * Test whether a block is completely empty, i.e. contains no marked
55 * objects. This does not require the block to be in physical
59 GC_bool
GC_block_empty(hhdr
)
62 register word
*p
= (word
*)(&(hhdr
-> hb_marks
[0]));
63 register word
* plim
=
64 (word
*)(&(hhdr
-> hb_marks
[MARK_BITS_SZ
]));
66 if (*p
++) return(FALSE
);
71 /* The following functions sometimes return a DONT_KNOW value. */
75 # define GC_block_nearly_full1(hhdr, pat1) DONT_KNOW
76 # define GC_block_nearly_full3(hhdr, pat1, pat2) DONT_KNOW
77 # define GC_block_nearly_full(hhdr) DONT_KNOW
81 * Test whether nearly all of the mark words consist of the same
84 #define FULL_THRESHOLD (MARK_BITS_SZ/16)
86 GC_bool
GC_block_nearly_full1(hhdr
, pat1
)
92 GC_ASSERT((MARK_BITS_SZ
& 1) == 0);
93 for (i
= 0; i
< MARK_BITS_SZ
; ++i
) {
94 if ((hhdr
-> hb_marks
[i
] | ~pat1
) != ONES
) {
95 if (++misses
> FULL_THRESHOLD
) return FALSE
;
102 * Test whether the same repeating 3 word pattern occurs in nearly
103 * all the mark bit slots.
104 * This is used as a heuristic, so we're a bit sloppy and ignore
105 * the last one or two words.
107 GC_bool
GC_block_nearly_full3(hhdr
, pat1
, pat2
, pat3
)
109 word pat1
, pat2
, pat3
;
114 if (MARK_BITS_SZ
< 4) {
117 for (i
= 0; i
< MARK_BITS_SZ
- 2; i
+= 3) {
118 if ((hhdr
-> hb_marks
[i
] | ~pat1
) != ONES
) {
119 if (++misses
> FULL_THRESHOLD
) return FALSE
;
121 if ((hhdr
-> hb_marks
[i
+1] | ~pat2
) != ONES
) {
122 if (++misses
> FULL_THRESHOLD
) return FALSE
;
124 if ((hhdr
-> hb_marks
[i
+2] | ~pat3
) != ONES
) {
125 if (++misses
> FULL_THRESHOLD
) return FALSE
;
131 /* Check whether a small object block is nearly full by looking at only */
133 /* We manually precomputed the mark bit patterns that need to be */
134 /* checked for, and we give up on the ones that are unlikely to occur, */
135 /* or have period > 3. */
136 /* This would be a lot easier with a mark bit per object instead of per */
137 /* word, but that would rewuire computing object numbers in the mark */
138 /* loop, which would require different data structures ... */
139 GC_bool
GC_block_nearly_full(hhdr
)
142 int sz
= hhdr
-> hb_sz
;
144 # if CPP_WORDSZ != 32 && CPP_WORDSZ != 64
145 return DONT_KNOW
; /* Shouldn't be used in any standard config. */
147 if (0 != HDR_WORDS
) return DONT_KNOW
;
148 /* Also shouldn't happen */
149 # if CPP_WORDSZ == 32
152 return GC_block_nearly_full1(hhdr
, 0xffffffffl
);
154 return GC_block_nearly_full1(hhdr
, 0x55555555l
);
156 return GC_block_nearly_full1(hhdr
, 0x11111111l
);
158 return GC_block_nearly_full3(hhdr
, 0x41041041l
,
162 return GC_block_nearly_full1(hhdr
, 0x01010101l
);
164 return GC_block_nearly_full3(hhdr
, 0x01001001l
,
168 return GC_block_nearly_full1(hhdr
, 0x00010001l
);
170 return GC_block_nearly_full1(hhdr
, 0x00000001l
);
175 # if CPP_WORDSZ == 64
178 return GC_block_nearly_full1(hhdr
, 0xffffffffffffffffl
);
180 return GC_block_nearly_full1(hhdr
, 0x5555555555555555l
);
182 return GC_block_nearly_full1(hhdr
, 0x1111111111111111l
);
184 return GC_block_nearly_full3(hhdr
, 0x1041041041041041l
,
186 0x0410410410410410l
);
188 return GC_block_nearly_full1(hhdr
, 0x0101010101010101l
);
190 return GC_block_nearly_full3(hhdr
, 0x1001001001001001l
,
192 0x0010010010010010l
);
194 return GC_block_nearly_full1(hhdr
, 0x0001000100010001l
);
196 return GC_block_nearly_full1(hhdr
, 0x0000000100000001l
);
202 #endif /* !SMALL_CONFIG */
205 # define INCR_WORDS(sz) n_words_found += (sz)
207 # define INCR_WORDS(sz)
210 * Restore unmarked small objects in h of size sz to the object
211 * free list. Returns the new list.
212 * Clears unmarked objects.
215 ptr_t
GC_reclaim_clear(hbp
, hhdr
, sz
, list
)
216 register struct hblk
*hbp
; /* ptr to current heap block */
221 register int word_no
;
222 register word
*p
, *q
, *plim
;
224 register int n_words_found
= 0;
227 p
= (word
*)(hbp
->hb_body
);
229 plim
= (word
*)((((word
)hbp
) + HBLKSIZE
)
230 - WORDS_TO_BYTES(sz
));
232 /* go through all words in block */
234 if( mark_bit_from_hdr(hhdr
, word_no
) ) {
238 /* object is available - put on list */
241 /* Clear object, advance p to next object in the process */
243 p
++; /* Skip link field */
251 GC_mem_found
+= n_words_found
;
259 * A special case for 2 word composite objects (e.g. cons cells):
262 ptr_t
GC_reclaim_clear2(hbp
, hhdr
, list
)
263 register struct hblk
*hbp
; /* ptr to current heap block */
267 register word
* mark_word_addr
= &(hhdr
->hb_marks
[divWORDSZ(HDR_WORDS
)]);
268 register word
*p
, *plim
;
270 register int n_words_found
= 0;
272 register word mark_word
;
274 # define DO_OBJ(start_displ) \
275 if (!(mark_word & ((word)1 << start_displ))) { \
276 p[start_displ] = (word)list; \
277 list = (ptr_t)(p+start_displ); \
278 p[start_displ+1] = 0; \
282 p
= (word
*)(hbp
->hb_body
);
283 plim
= (word
*)(((word
)hbp
) + HBLKSIZE
);
285 /* go through all words in block */
287 mark_word
= *mark_word_addr
++;
288 for (i
= 0; i
< WORDSZ
; i
+= 8) {
298 GC_mem_found
+= n_words_found
;
305 * Another special case for 4 word composite objects:
308 ptr_t
GC_reclaim_clear4(hbp
, hhdr
, list
)
309 register struct hblk
*hbp
; /* ptr to current heap block */
313 register word
* mark_word_addr
= &(hhdr
->hb_marks
[divWORDSZ(HDR_WORDS
)]);
314 register word
*p
, *plim
;
316 register int n_words_found
= 0;
318 register word mark_word
;
319 # define DO_OBJ(start_displ) \
320 if (!(mark_word & ((word)1 << start_displ))) { \
321 p[start_displ] = (word)list; \
322 list = (ptr_t)(p+start_displ); \
323 p[start_displ+1] = 0; \
324 p[start_displ+2] = 0; \
325 p[start_displ+3] = 0; \
329 p
= (word
*)(hbp
->hb_body
);
330 plim
= (word
*)(((word
)hbp
) + HBLKSIZE
);
332 /* go through all words in block */
334 mark_word
= *mark_word_addr
++;
343 # if CPP_WORDSZ == 64
356 GC_mem_found
+= n_words_found
;
362 #endif /* !SMALL_CONFIG */
364 /* The same thing, but don't clear objects: */
366 ptr_t
GC_reclaim_uninit(hbp
, hhdr
, sz
, list
)
367 register struct hblk
*hbp
; /* ptr to current heap block */
372 register int word_no
;
373 register word
*p
, *plim
;
375 register int n_words_found
= 0;
378 p
= (word
*)(hbp
->hb_body
);
380 plim
= (word
*)((((word
)hbp
) + HBLKSIZE
)
381 - WORDS_TO_BYTES(sz
));
383 /* go through all words in block */
385 if( !mark_bit_from_hdr(hhdr
, word_no
) ) {
387 /* object is available - put on list */
395 GC_mem_found
+= n_words_found
;
400 /* Don't really reclaim objects, just check for unmarked ones: */
402 void GC_reclaim_check(hbp
, hhdr
, sz
)
403 register struct hblk
*hbp
; /* ptr to current heap block */
407 register int word_no
;
408 register word
*p
, *plim
;
410 register int n_words_found
= 0;
413 p
= (word
*)(hbp
->hb_body
);
415 plim
= (word
*)((((word
)hbp
) + HBLKSIZE
)
416 - WORDS_TO_BYTES(sz
));
418 /* go through all words in block */
420 if( !mark_bit_from_hdr(hhdr
, word_no
) ) {
421 FOUND_FREE(hbp
, word_no
);
430 * Another special case for 2 word atomic objects:
433 ptr_t
GC_reclaim_uninit2(hbp
, hhdr
, list
)
434 register struct hblk
*hbp
; /* ptr to current heap block */
438 register word
* mark_word_addr
= &(hhdr
->hb_marks
[divWORDSZ(HDR_WORDS
)]);
439 register word
*p
, *plim
;
441 register int n_words_found
= 0;
443 register word mark_word
;
445 # define DO_OBJ(start_displ) \
446 if (!(mark_word & ((word)1 << start_displ))) { \
447 p[start_displ] = (word)list; \
448 list = (ptr_t)(p+start_displ); \
452 p
= (word
*)(hbp
->hb_body
);
453 plim
= (word
*)(((word
)hbp
) + HBLKSIZE
);
455 /* go through all words in block */
457 mark_word
= *mark_word_addr
++;
458 for (i
= 0; i
< WORDSZ
; i
+= 8) {
468 GC_mem_found
+= n_words_found
;
475 * Another special case for 4 word atomic objects:
478 ptr_t
GC_reclaim_uninit4(hbp
, hhdr
, list
)
479 register struct hblk
*hbp
; /* ptr to current heap block */
483 register word
* mark_word_addr
= &(hhdr
->hb_marks
[divWORDSZ(HDR_WORDS
)]);
484 register word
*p
, *plim
;
486 register int n_words_found
= 0;
488 register word mark_word
;
489 # define DO_OBJ(start_displ) \
490 if (!(mark_word & ((word)1 << start_displ))) { \
491 p[start_displ] = (word)list; \
492 list = (ptr_t)(p+start_displ); \
496 p
= (word
*)(hbp
->hb_body
);
497 plim
= (word
*)(((word
)hbp
) + HBLKSIZE
);
499 /* go through all words in block */
501 mark_word
= *mark_word_addr
++;
510 # if CPP_WORDSZ == 64
523 GC_mem_found
+= n_words_found
;
529 /* Finally the one word case, which never requires any clearing: */
531 ptr_t
GC_reclaim1(hbp
, hhdr
, list
)
532 register struct hblk
*hbp
; /* ptr to current heap block */
536 register word
* mark_word_addr
= &(hhdr
->hb_marks
[divWORDSZ(HDR_WORDS
)]);
537 register word
*p
, *plim
;
539 register int n_words_found
= 0;
541 register word mark_word
;
543 # define DO_OBJ(start_displ) \
544 if (!(mark_word & ((word)1 << start_displ))) { \
545 p[start_displ] = (word)list; \
546 list = (ptr_t)(p+start_displ); \
550 p
= (word
*)(hbp
->hb_body
);
551 plim
= (word
*)(((word
)hbp
) + HBLKSIZE
);
553 /* go through all words in block */
555 mark_word
= *mark_word_addr
++;
556 for (i
= 0; i
< WORDSZ
; i
+= 4) {
566 GC_mem_found
+= n_words_found
;
572 #endif /* !SMALL_CONFIG */
575 * Restore unmarked small objects in the block pointed to by hbp
576 * to the appropriate object free list.
577 * If entirely empty blocks are to be completely deallocated, then
578 * caller should perform that check.
580 void GC_reclaim_small_nonempty_block(hbp
, report_if_found
)
581 register struct hblk
*hbp
; /* ptr to current heap block */
582 int report_if_found
; /* Abort if a reclaimable object is found */
585 word sz
; /* size of objects in current block */
586 struct obj_kind
* ok
;
593 hhdr
-> hb_last_reclaimed
= (unsigned short) GC_gc_no
;
594 kind
= hhdr
-> hb_obj_kind
;
595 ok
= &GC_obj_kinds
[kind
];
596 flh
= &(ok
-> ok_freelist
[sz
]);
598 if (report_if_found
) {
599 GC_reclaim_check(hbp
, hhdr
, sz
);
600 } else if (ok
-> ok_init
) {
602 # ifndef SMALL_CONFIG
604 full
= GC_block_nearly_full1(hhdr
, 0xffffffffl
);
605 if (TRUE
== full
) goto out
;
606 if (FALSE
== full
) GC_write_hint(hbp
);
607 /* In the DONT_KNOW case, we let reclaim fault. */
608 *flh
= GC_reclaim1(hbp
, hhdr
, *flh
);
611 full
= GC_block_nearly_full1(hhdr
, 0x55555555l
);
612 if (TRUE
== full
) goto out
;
613 if (FALSE
== full
) GC_write_hint(hbp
);
614 *flh
= GC_reclaim_clear2(hbp
, hhdr
, *flh
);
617 full
= GC_block_nearly_full1(hhdr
, 0x11111111l
);
618 if (TRUE
== full
) goto out
;
619 if (FALSE
== full
) GC_write_hint(hbp
);
620 *flh
= GC_reclaim_clear4(hbp
, hhdr
, *flh
);
624 full
= GC_block_nearly_full(hhdr
);
625 if (TRUE
== full
) goto out
;
626 if (FALSE
== full
) GC_write_hint(hbp
);
627 *flh
= GC_reclaim_clear(hbp
, hhdr
, sz
, *flh
);
632 # ifndef SMALL_CONFIG
634 full
= GC_block_nearly_full1(hhdr
, 0xffffffffl
);
635 if (TRUE
== full
) goto out
;
636 if (FALSE
== full
) GC_write_hint(hbp
);
637 *flh
= GC_reclaim1(hbp
, hhdr
, *flh
);
640 full
= GC_block_nearly_full1(hhdr
, 0x55555555l
);
641 if (TRUE
== full
) goto out
;
642 if (FALSE
== full
) GC_write_hint(hbp
);
643 *flh
= GC_reclaim_uninit2(hbp
, hhdr
, *flh
);
646 full
= GC_block_nearly_full1(hhdr
, 0x11111111l
);
647 if (TRUE
== full
) goto out
;
648 if (FALSE
== full
) GC_write_hint(hbp
);
649 *flh
= GC_reclaim_uninit4(hbp
, hhdr
, *flh
);
653 full
= GC_block_nearly_full(hhdr
);
654 if (TRUE
== full
) goto out
;
655 if (FALSE
== full
) GC_write_hint(hbp
);
656 *flh
= GC_reclaim_uninit(hbp
, hhdr
, sz
, *flh
);
661 if (IS_UNCOLLECTABLE(kind
)) GC_set_hdr_marks(hhdr
);
665 * Restore an unmarked large object or an entirely empty blocks of small objects
666 * to the heap block free list.
667 * Otherwise enqueue the block for later processing
668 * by GC_reclaim_small_nonempty_block.
669 * If report_if_found is TRUE, then process any block immediately, and
670 * simply report free objects; do not actually reclaim them.
672 void GC_reclaim_block(hbp
, report_if_found
)
673 register struct hblk
*hbp
; /* ptr to current heap block */
674 word report_if_found
; /* Abort if a reclaimable object is found */
677 register word sz
; /* size of objects in current block */
678 register struct obj_kind
* ok
;
683 ok
= &GC_obj_kinds
[hhdr
-> hb_obj_kind
];
685 if( sz
> MAXOBJSZ
) { /* 1 big object */
686 if( !mark_bit_from_hdr(hhdr
, HDR_WORDS
) ) {
687 if (report_if_found
) {
688 FOUND_FREE(hbp
, HDR_WORDS
);
697 GC_bool empty
= GC_block_empty(hhdr
);
698 if (report_if_found
) {
699 GC_reclaim_small_nonempty_block(hbp
, (int)report_if_found
);
702 GC_mem_found
+= BYTES_TO_WORDS(HBLKSIZE
);
706 /* group of smaller objects, enqueue the real work */
707 rlh
= &(ok
-> ok_reclaim_list
[sz
]);
708 hhdr
-> hb_next
= *rlh
;
714 #if !defined(NO_DEBUGGING)
715 /* Routines to gather and print heap block info */
716 /* intended for debugging. Otherwise should be called */
718 static size_t number_of_blocks
;
719 static size_t total_bytes
;
721 /* Number of set bits in a word. Not performance critical. */
722 static int set_bits(n
)
726 register int result
= 0;
735 /* Return the number of set mark bits in the given header */
736 int GC_n_set_marks(hhdr
)
739 register int result
= 0;
742 for (i
= 0; i
< MARK_BITS_SZ
; i
++) {
743 result
+= set_bits(hhdr
-> hb_marks
[i
]);
749 void GC_print_block_descr(h
, dummy
)
753 register hdr
* hhdr
= HDR(h
);
754 register size_t bytes
= WORDS_TO_BYTES(hhdr
-> hb_sz
);
756 GC_printf3("(%lu:%lu,%lu)", (unsigned long)(hhdr
-> hb_obj_kind
),
757 (unsigned long)bytes
,
758 (unsigned long)(GC_n_set_marks(hhdr
)));
759 bytes
+= HDR_BYTES
+ HBLKSIZE
-1;
760 bytes
&= ~(HBLKSIZE
-1);
761 total_bytes
+= bytes
;
765 void GC_print_block_list()
767 GC_printf0("(kind(0=ptrfree,1=normal,2=unc.,3=stubborn):size_in_bytes, #_marks_set)\n");
768 number_of_blocks
= 0;
770 GC_apply_to_all_blocks(GC_print_block_descr
, (word
)0);
771 GC_printf2("\nblocks = %lu, bytes = %lu\n",
772 (unsigned long)number_of_blocks
,
773 (unsigned long)total_bytes
);
776 #endif /* NO_DEBUGGING */
779 * Perform GC_reclaim_block on the entire heap, after first clearing
780 * small object free lists (if we are not just looking for leaks).
782 void GC_start_reclaim(report_if_found
)
783 int report_if_found
; /* Abort if a GC_reclaimable object is found */
787 /* Clear reclaim- and free-lists */
788 for (kind
= 0; kind
< GC_n_kinds
; kind
++) {
791 register struct hblk
** rlp
;
792 register struct hblk
** rlim
;
793 register struct hblk
** rlist
= GC_obj_kinds
[kind
].ok_reclaim_list
;
795 if (rlist
== 0) continue; /* This kind not used. */
796 if (!report_if_found
) {
797 lim
= &(GC_obj_kinds
[kind
].ok_freelist
[MAXOBJSZ
+1]);
798 for( fop
= GC_obj_kinds
[kind
].ok_freelist
; fop
< lim
; fop
++ ) {
801 } /* otherwise free list objects are marked, */
802 /* and its safe to leave them */
803 rlim
= rlist
+ MAXOBJSZ
+1;
804 for( rlp
= rlist
; rlp
< rlim
; rlp
++ ) {
810 GC_printf0("GC_reclaim: current block sizes:\n");
811 GC_print_block_list();
814 /* Go through all heap blocks (in hblklist) and reclaim unmarked objects */
815 /* or enqueue the block for later processing. */
816 GC_apply_to_all_blocks(GC_reclaim_block
, (word
)report_if_found
);
821 * Sweep blocks of the indicated object size and kind until either the
822 * appropriate free list is nonempty, or there are no more blocks to
825 void GC_continue_reclaim(sz
, kind
)
830 register struct hblk
* hbp
;
831 register struct obj_kind
* ok
= &(GC_obj_kinds
[kind
]);
832 struct hblk
** rlh
= ok
-> ok_reclaim_list
;
833 ptr_t
*flh
= &(ok
-> ok_freelist
[sz
]);
835 if (rlh
== 0) return; /* No blocks of this kind. */
837 while ((hbp
= *rlh
) != 0) {
839 *rlh
= hhdr
-> hb_next
;
840 GC_reclaim_small_nonempty_block(hbp
, FALSE
);
841 if (*flh
!= 0) break;
846 * Reclaim all small blocks waiting to be reclaimed.
847 * Abort and return FALSE when/if (*stop_func)() returns TRUE.
848 * If this returns TRUE, then it's safe to restart the world
849 * with incorrectly cleared mark bits.
850 * If ignore_old is TRUE, then reclain only blocks that have been
851 * recently reclaimed, and discard the rest.
852 * Stop_func may be 0.
854 GC_bool
GC_reclaim_all(stop_func
, ignore_old
)
855 GC_stop_func stop_func
;
861 register struct hblk
* hbp
;
862 register struct obj_kind
* ok
;
866 CLOCK_TYPE start_time
;
867 CLOCK_TYPE done_time
;
869 GET_TIME(start_time
);
872 for (kind
= 0; kind
< GC_n_kinds
; kind
++) {
873 ok
= &(GC_obj_kinds
[kind
]);
874 rlp
= ok
-> ok_reclaim_list
;
875 if (rlp
== 0) continue;
876 for (sz
= 1; sz
<= MAXOBJSZ
; sz
++) {
878 while ((hbp
= *rlh
) != 0) {
879 if (stop_func
!= (GC_stop_func
)0 && (*stop_func
)()) {
883 *rlh
= hhdr
-> hb_next
;
884 if (!ignore_old
|| hhdr
-> hb_last_reclaimed
== GC_gc_no
- 1) {
885 /* It's likely we'll need it this time, too */
886 /* It's been touched recently, so this */
887 /* shouldn't trigger paging. */
888 GC_reclaim_small_nonempty_block(hbp
, FALSE
);
895 GC_printf1("Disposing of reclaim lists took %lu msecs\n",
896 MS_TIME_DIFF(done_time
,start_time
));