2 * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
3 * Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
5 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
6 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
8 * Permission is hereby granted to use or copy this program
9 * for any purpose, provided the above notices are retained on all copies.
10 * Permission to modify the code and to distribute modified code is granted,
11 * provided the above notices are retained, and a notice that the code was
12 * modified is included with the above copyright notice.
14 /* Boehm, February 15, 1996 2:41 pm PST */
19 signed_word GC_mem_found = 0;
20 /* 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) \
42 if (abort_if_found) { \
43 report_leak((ptr_t)hblk + WORDS_TO_BYTES(word_no), \
44 HDR(hblk) -> hb_sz); \
47 # define FOUND_FREE(hblk, word_no)
57 * Test whether a block is completely empty, i.e. contains no marked
58 * objects. This does not require the block to be in physical
62 GC_bool GC_block_empty(hhdr)
65 register word *p = (word *)(&(hhdr -> hb_marks[0]));
66 register word * plim =
67 (word *)(&(hhdr -> hb_marks[MARK_BITS_SZ]));
69 if (*p++) return(FALSE);
75 # define INCR_WORDS(sz) n_words_found += (sz)
77 # define INCR_WORDS(sz)
80 * Restore unmarked small objects in h of size sz to the object
81 * free list. Returns the new list.
82 * Clears unmarked objects.
85 ptr_t GC_reclaim_clear(hbp, hhdr, sz, list, abort_if_found)
86 register struct hblk *hbp; /* ptr to current heap block */
88 GC_bool abort_if_found; /* Abort if a reclaimable object is found */
93 register word *p, *q, *plim;
95 register int n_words_found = 0;
98 p = (word *)(hbp->hb_body);
100 plim = (word *)((((word)hbp) + HBLKSIZE)
101 - WORDS_TO_BYTES(sz));
103 /* go through all words in block */
105 if( mark_bit_from_hdr(hhdr, word_no) ) {
108 FOUND_FREE(hbp, word_no);
110 /* object is available - put on list */
113 /* Clear object, advance p to next object in the process */
115 p++; /* Skip link field */
123 GC_mem_found += n_words_found;
131 * A special case for 2 word composite objects (e.g. cons cells):
134 ptr_t GC_reclaim_clear2(hbp, hhdr, list, abort_if_found)
135 register struct hblk *hbp; /* ptr to current heap block */
137 GC_bool abort_if_found; /* Abort if a reclaimable object is found */
140 register word * mark_word_addr = &(hhdr->hb_marks[divWORDSZ(HDR_WORDS)]);
141 register word *p, *plim;
143 register int n_words_found = 0;
145 register word mark_word;
147 # define DO_OBJ(start_displ) \
148 if (!(mark_word & ((word)1 << start_displ))) { \
149 FOUND_FREE(hbp, p - (word *)hbp + start_displ); \
150 p[start_displ] = (word)list; \
151 list = (ptr_t)(p+start_displ); \
152 p[start_displ+1] = 0; \
156 p = (word *)(hbp->hb_body);
157 plim = (word *)(((word)hbp) + HBLKSIZE);
159 /* go through all words in block */
161 mark_word = *mark_word_addr++;
162 for (i = 0; i < WORDSZ; i += 8) {
172 GC_mem_found += n_words_found;
179 * Another special case for 4 word composite objects:
182 ptr_t GC_reclaim_clear4(hbp, hhdr, list, abort_if_found)
183 register struct hblk *hbp; /* ptr to current heap block */
185 GC_bool abort_if_found; /* Abort if a reclaimable object is found */
188 register word * mark_word_addr = &(hhdr->hb_marks[divWORDSZ(HDR_WORDS)]);
189 register word *p, *plim;
191 register int n_words_found = 0;
193 register word mark_word;
194 # define DO_OBJ(start_displ) \
195 if (!(mark_word & ((word)1 << start_displ))) { \
196 FOUND_FREE(hbp, p - (word *)hbp + start_displ); \
197 p[start_displ] = (word)list; \
198 list = (ptr_t)(p+start_displ); \
199 p[start_displ+1] = 0; \
200 p[start_displ+2] = 0; \
201 p[start_displ+3] = 0; \
205 p = (word *)(hbp->hb_body);
206 plim = (word *)(((word)hbp) + HBLKSIZE);
208 /* go through all words in block */
210 mark_word = *mark_word_addr++;
219 # if CPP_WORDSZ == 64
232 GC_mem_found += n_words_found;
238 #endif /* !SMALL_CONFIG */
240 /* The same thing, but don't clear objects: */
242 ptr_t GC_reclaim_uninit(hbp, hhdr, sz, list, abort_if_found)
243 register struct hblk *hbp; /* ptr to current heap block */
245 GC_bool abort_if_found; /* Abort if a reclaimable object is found */
249 register int word_no;
250 register word *p, *plim;
252 register int n_words_found = 0;
255 p = (word *)(hbp->hb_body);
257 plim = (word *)((((word)hbp) + HBLKSIZE)
258 - WORDS_TO_BYTES(sz));
260 /* go through all words in block */
262 if( !mark_bit_from_hdr(hhdr, word_no) ) {
263 FOUND_FREE(hbp, word_no);
265 /* object is available - put on list */
273 GC_mem_found += n_words_found;
280 * Another special case for 2 word atomic objects:
283 ptr_t GC_reclaim_uninit2(hbp, hhdr, list, abort_if_found)
284 register struct hblk *hbp; /* ptr to current heap block */
286 GC_bool abort_if_found; /* Abort if a reclaimable object is found */
289 register word * mark_word_addr = &(hhdr->hb_marks[divWORDSZ(HDR_WORDS)]);
290 register word *p, *plim;
292 register int n_words_found = 0;
294 register word mark_word;
296 # define DO_OBJ(start_displ) \
297 if (!(mark_word & ((word)1 << start_displ))) { \
298 FOUND_FREE(hbp, p - (word *)hbp + start_displ); \
299 p[start_displ] = (word)list; \
300 list = (ptr_t)(p+start_displ); \
304 p = (word *)(hbp->hb_body);
305 plim = (word *)(((word)hbp) + HBLKSIZE);
307 /* go through all words in block */
309 mark_word = *mark_word_addr++;
310 for (i = 0; i < WORDSZ; i += 8) {
320 GC_mem_found += n_words_found;
327 * Another special case for 4 word atomic objects:
330 ptr_t GC_reclaim_uninit4(hbp, hhdr, list, abort_if_found)
331 register struct hblk *hbp; /* ptr to current heap block */
333 GC_bool abort_if_found; /* Abort if a reclaimable object is found */
336 register word * mark_word_addr = &(hhdr->hb_marks[divWORDSZ(HDR_WORDS)]);
337 register word *p, *plim;
339 register int n_words_found = 0;
341 register word mark_word;
342 # define DO_OBJ(start_displ) \
343 if (!(mark_word & ((word)1 << start_displ))) { \
344 FOUND_FREE(hbp, p - (word *)hbp + start_displ); \
345 p[start_displ] = (word)list; \
346 list = (ptr_t)(p+start_displ); \
350 p = (word *)(hbp->hb_body);
351 plim = (word *)(((word)hbp) + HBLKSIZE);
353 /* go through all words in block */
355 mark_word = *mark_word_addr++;
364 # if CPP_WORDSZ == 64
377 GC_mem_found += n_words_found;
383 /* Finally the one word case, which never requires any clearing: */
385 ptr_t GC_reclaim1(hbp, hhdr, list, abort_if_found)
386 register struct hblk *hbp; /* ptr to current heap block */
388 GC_bool abort_if_found; /* Abort if a reclaimable object is found */
391 register word * mark_word_addr = &(hhdr->hb_marks[divWORDSZ(HDR_WORDS)]);
392 register word *p, *plim;
394 register int n_words_found = 0;
396 register word mark_word;
398 # define DO_OBJ(start_displ) \
399 if (!(mark_word & ((word)1 << start_displ))) { \
400 FOUND_FREE(hbp, p - (word *)hbp + start_displ); \
401 p[start_displ] = (word)list; \
402 list = (ptr_t)(p+start_displ); \
406 p = (word *)(hbp->hb_body);
407 plim = (word *)(((word)hbp) + HBLKSIZE);
409 /* go through all words in block */
411 mark_word = *mark_word_addr++;
412 for (i = 0; i < WORDSZ; i += 4) {
422 GC_mem_found += n_words_found;
428 #endif /* !SMALL_CONFIG */
431 * Restore unmarked small objects in the block pointed to by hbp
432 * to the appropriate object free list.
433 * If entirely empty blocks are to be completely deallocated, then
434 * caller should perform that check.
436 void GC_reclaim_small_nonempty_block(hbp, abort_if_found)
437 register struct hblk *hbp; /* ptr to current heap block */
438 int abort_if_found; /* Abort if a reclaimable object is found */
441 register word sz; /* size of objects in current block */
442 register struct obj_kind * ok;
443 register ptr_t * flh;
448 hhdr -> hb_last_reclaimed = (unsigned short) GC_gc_no;
449 kind = hhdr -> hb_obj_kind;
450 ok = &GC_obj_kinds[kind];
451 flh = &(ok -> ok_freelist[sz]);
456 # ifndef SMALL_CONFIG
458 *flh = GC_reclaim1(hbp, hhdr, *flh, abort_if_found);
461 *flh = GC_reclaim_clear2(hbp, hhdr, *flh, abort_if_found);
464 *flh = GC_reclaim_clear4(hbp, hhdr, *flh, abort_if_found);
468 *flh = GC_reclaim_clear(hbp, hhdr, sz, *flh, abort_if_found);
473 # ifndef SMALL_CONFIG
475 *flh = GC_reclaim1(hbp, hhdr, *flh, abort_if_found);
478 *flh = GC_reclaim_uninit2(hbp, hhdr, *flh, abort_if_found);
481 *flh = GC_reclaim_uninit4(hbp, hhdr, *flh, abort_if_found);
485 *flh = GC_reclaim_uninit(hbp, hhdr, sz, *flh, abort_if_found);
489 if (IS_UNCOLLECTABLE(kind)) GC_set_hdr_marks(hhdr);
493 * Restore an unmarked large object or an entirely empty blocks of small objects
494 * to the heap block free list.
495 * Otherwise enqueue the block for later processing
496 * by GC_reclaim_small_nonempty_block.
497 * If abort_if_found is TRUE, then process any block immediately.
499 void GC_reclaim_block(hbp, abort_if_found)
500 register struct hblk *hbp; /* ptr to current heap block */
501 word abort_if_found; /* Abort if a reclaimable object is found */
504 register word sz; /* size of objects in current block */
505 register struct obj_kind * ok;
510 ok = &GC_obj_kinds[hhdr -> hb_obj_kind];
512 if( sz > MAXOBJSZ ) { /* 1 big object */
513 if( !mark_bit_from_hdr(hhdr, HDR_WORDS) ) {
514 FOUND_FREE(hbp, HDR_WORDS);
521 GC_bool empty = GC_block_empty(hhdr);
522 if (abort_if_found) {
523 GC_reclaim_small_nonempty_block(hbp, (int)abort_if_found);
526 GC_mem_found += BYTES_TO_WORDS(HBLKSIZE);
530 /* group of smaller objects, enqueue the real work */
531 rlh = &(ok -> ok_reclaim_list[sz]);
532 hhdr -> hb_next = *rlh;
538 #if !defined(NO_DEBUGGING)
539 /* Routines to gather and print heap block info */
540 /* intended for debugging. Otherwise should be called */
542 static size_t number_of_blocks;
543 static size_t total_bytes;
545 /* Number of set bits in a word. Not performance critical. */
546 static int set_bits(n)
550 register int result = 0;
559 /* Return the number of set mark bits in the given header */
560 int GC_n_set_marks(hhdr)
563 register int result = 0;
566 for (i = 0; i < MARK_BITS_SZ; i++) {
567 result += set_bits(hhdr -> hb_marks[i]);
573 void GC_print_block_descr(h, dummy)
577 register hdr * hhdr = HDR(h);
578 register size_t bytes = WORDS_TO_BYTES(hhdr -> hb_sz);
580 GC_printf3("(%lu:%lu,%lu)", (unsigned long)(hhdr -> hb_obj_kind),
581 (unsigned long)bytes,
582 (unsigned long)(GC_n_set_marks(hhdr)));
583 bytes += HDR_BYTES + HBLKSIZE-1;
584 bytes &= ~(HBLKSIZE-1);
585 total_bytes += bytes;
589 void GC_print_block_list()
591 GC_printf0("(kind(0=ptrfree,1=normal,2=unc.,3=stubborn):size_in_bytes, #_marks_set)\n");
592 number_of_blocks = 0;
594 GC_apply_to_all_blocks(GC_print_block_descr, (word)0);
595 GC_printf2("\nblocks = %lu, bytes = %lu\n",
596 (unsigned long)number_of_blocks,
597 (unsigned long)total_bytes);
600 #endif /* NO_DEBUGGING */
603 * Do the same thing on the entire heap, after first clearing small object
604 * free lists (if we are not just looking for leaks).
606 void GC_start_reclaim(abort_if_found)
607 int abort_if_found; /* Abort if a GC_reclaimable object is found */
611 /* Clear reclaim- and free-lists */
612 for (kind = 0; kind < GC_n_kinds; kind++) {
615 register struct hblk ** rlp;
616 register struct hblk ** rlim;
617 register struct hblk ** rlist = GC_obj_kinds[kind].ok_reclaim_list;
619 if (rlist == 0) continue; /* This kind not used. */
620 if (!abort_if_found) {
621 lim = &(GC_obj_kinds[kind].ok_freelist[MAXOBJSZ+1]);
622 for( fop = GC_obj_kinds[kind].ok_freelist; fop < lim; fop++ ) {
625 } /* otherwise free list objects are marked, */
626 /* and its safe to leave them */
627 rlim = rlist + MAXOBJSZ+1;
628 for( rlp = rlist; rlp < rlim; rlp++ ) {
634 GC_printf0("GC_reclaim: current block sizes:\n");
635 GC_print_block_list();
638 /* Go through all heap blocks (in hblklist) and reclaim unmarked objects */
639 /* or enqueue the block for later processing. */
640 GC_apply_to_all_blocks(GC_reclaim_block, (word)abort_if_found);
645 * Sweep blocks of the indicated object size and kind until either the
646 * appropriate free list is nonempty, or there are no more blocks to
649 void GC_continue_reclaim(sz, kind)
654 register struct hblk * hbp;
655 register struct obj_kind * ok = &(GC_obj_kinds[kind]);
656 struct hblk ** rlh = ok -> ok_reclaim_list;
657 ptr_t *flh = &(ok -> ok_freelist[sz]);
659 if (rlh == 0) return; /* No blocks of this kind. */
661 while ((hbp = *rlh) != 0) {
663 *rlh = hhdr -> hb_next;
664 GC_reclaim_small_nonempty_block(hbp, FALSE);
665 if (*flh != 0) break;
670 * Reclaim all small blocks waiting to be reclaimed.
671 * Abort and return FALSE when/if (*stop_func)() returns TRUE.
672 * If this returns TRUE, then it's safe to restart the world
673 * with incorrectly cleared mark bits.
674 * If ignore_old is TRUE, then reclain only blocks that have been
675 * recently reclaimed, and discard the rest.
676 * Stop_func may be 0.
678 GC_bool GC_reclaim_all(stop_func, ignore_old)
679 GC_stop_func stop_func;
685 register struct hblk * hbp;
686 register struct obj_kind * ok;
690 CLOCK_TYPE start_time;
691 CLOCK_TYPE done_time;
693 GET_TIME(start_time);
696 for (kind = 0; kind < GC_n_kinds; kind++) {
697 ok = &(GC_obj_kinds[kind]);
698 rlp = ok -> ok_reclaim_list;
699 if (rlp == 0) continue;
700 for (sz = 1; sz <= MAXOBJSZ; sz++) {
702 while ((hbp = *rlh) != 0) {
703 if (stop_func != (GC_stop_func)0 && (*stop_func)()) {
707 *rlh = hhdr -> hb_next;
708 if (!ignore_old || hhdr -> hb_last_reclaimed == GC_gc_no - 1) {
709 /* It's likely we'll need it this time, too */
710 /* It's been touched recently, so this */
711 /* shouldn't trigger paging. */
712 GC_reclaim_small_nonempty_block(hbp, FALSE);
719 GC_printf1("Disposing of reclaim lists took %lu msecs\n",
720 MS_TIME_DIFF(done_time,start_time));