3 * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
4 * Copyright (c) 1991-1995 by Xerox Corporation. All rights reserved.
6 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
7 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
9 * Permission is hereby granted to use or copy this program
10 * for any purpose, provided the above notices are retained on all copies.
11 * Permission to modify the code and to distribute modified code is granted,
12 * provided the above notices are retained, and a notice that the code was
13 * modified is included with the above copyright notice.
22 /* We put this here to minimize the risk of inlining. */
26 /* Single argument version, robust against whole program analysis. */
30 static VOLATILE word sink
;
35 mark_proc GC_mark_procs
[MAX_MARK_PROCS
] = {0};
36 word GC_n_mark_procs
= 0;
38 /* Initialize GC_obj_kinds properly and standard free lists properly. */
39 /* This must be done statically since they may be accessed before */
40 /* GC_init is called. */
41 /* It's done here, since we need to deal with mark descriptors. */
42 struct obj_kind GC_obj_kinds
[MAXOBJKINDS
] = {
43 /* PTRFREE */ { &GC_aobjfreelist
[0], 0 /* filled in dynamically */,
44 0 | DS_LENGTH
, FALSE
, FALSE
},
45 /* NORMAL */ { &GC_objfreelist
[0], 0,
46 # if defined(ADD_BYTE_AT_END) && ALIGNMENT > DS_TAGS
47 (word
)(-ALIGNMENT
) | DS_LENGTH
,
51 TRUE
/* add length to descr */, TRUE
},
53 { &GC_uobjfreelist
[0], 0,
54 0 | DS_LENGTH
, TRUE
/* add length to descr */, TRUE
},
55 # ifdef ATOMIC_UNCOLLECTABLE
57 { &GC_auobjfreelist
[0], 0,
58 0 | DS_LENGTH
, FALSE
/* add length to descr */, FALSE
},
60 # ifdef STUBBORN_ALLOC
61 /*STUBBORN*/ { &GC_sobjfreelist
[0], 0,
62 0 | DS_LENGTH
, TRUE
/* add length to descr */, TRUE
},
66 # ifdef ATOMIC_UNCOLLECTABLE
67 # ifdef STUBBORN_ALLOC
73 # ifdef STUBBORN_ALLOC
81 # ifndef INITIAL_MARK_STACK_SIZE
82 # define INITIAL_MARK_STACK_SIZE (1*HBLKSIZE)
83 /* INITIAL_MARK_STACK_SIZE * sizeof(mse) should be a */
84 /* multiple of HBLKSIZE. */
88 * Limits of stack for GC_mark routine.
89 * All ranges between GC_mark_stack(incl.) and GC_mark_stack_top(incl.) still
90 * need to be marked from.
93 word GC_n_rescuing_pages
; /* Number of dirty pages we marked from */
94 /* excludes ptrfree pages, etc. */
98 word GC_mark_stack_size
= 0;
100 mse
* GC_mark_stack_top
;
102 static struct hblk
* scan_ptr
;
104 mark_state_t GC_mark_state
= MS_NONE
;
106 GC_bool GC_mark_stack_too_small
= FALSE
;
108 GC_bool GC_objects_are_marked
= FALSE
; /* Are there collectable marked */
109 /* objects in the heap? */
111 GC_bool
GC_collection_in_progress()
113 return(GC_mark_state
!= MS_NONE
);
116 /* clear all mark bits in the header */
117 void GC_clear_hdr_marks(hhdr
)
120 BZERO(hhdr
-> hb_marks
, MARK_BITS_SZ
*sizeof(word
));
123 /* Set all mark bits in the header. Used for uncollectable blocks. */
124 void GC_set_hdr_marks(hhdr
)
129 for (i
= 0; i
< MARK_BITS_SZ
; ++i
) {
130 hhdr
-> hb_marks
[i
] = ONES
;
135 * Clear all mark bits associated with block h.
138 static void clear_marks_for_block(h
, dummy
)
142 register hdr
* hhdr
= HDR(h
);
144 if (IS_UNCOLLECTABLE(hhdr
-> hb_obj_kind
)) return;
145 /* Mark bit for these is cleared only once the object is */
146 /* explicitly deallocated. This either frees the block, or */
147 /* the bit is cleared once the object is on the free list. */
148 GC_clear_hdr_marks(hhdr
);
151 /* Slow but general routines for setting/clearing/asking about mark bits */
152 void GC_set_mark_bit(p
)
155 register struct hblk
*h
= HBLKPTR(p
);
156 register hdr
* hhdr
= HDR(h
);
157 register int word_no
= (word
*)p
- (word
*)h
;
159 set_mark_bit_from_hdr(hhdr
, word_no
);
162 void GC_clear_mark_bit(p
)
165 register struct hblk
*h
= HBLKPTR(p
);
166 register hdr
* hhdr
= HDR(h
);
167 register int word_no
= (word
*)p
- (word
*)h
;
169 clear_mark_bit_from_hdr(hhdr
, word_no
);
172 GC_bool
GC_is_marked(p
)
175 register struct hblk
*h
= HBLKPTR(p
);
176 register hdr
* hhdr
= HDR(h
);
177 register int word_no
= (word
*)p
- (word
*)h
;
179 return(mark_bit_from_hdr(hhdr
, word_no
));
184 * Clear mark bits in all allocated heap blocks. This invalidates
185 * the marker invariant, and sets GC_mark_state to reflect this.
186 * (This implicitly starts marking to reestablish the invariant.)
188 void GC_clear_marks()
190 GC_apply_to_all_blocks(clear_marks_for_block
, (word
)0);
191 GC_objects_are_marked
= FALSE
;
192 GC_mark_state
= MS_INVALID
;
195 /* Counters reflect currently marked objects: reset here */
196 GC_composite_in_use
= 0;
197 GC_atomic_in_use
= 0;
202 /* Initiate a garbage collection. Initiates a full collection if the */
203 /* mark state is invalid. */
205 void GC_initiate_gc()
207 if (GC_dirty_maintained
) GC_read_dirty();
208 # ifdef STUBBORN_ALLOC
213 extern void GC_check_dirty();
215 if (GC_dirty_maintained
) GC_check_dirty();
219 GC_n_rescuing_pages
= 0;
221 if (GC_mark_state
== MS_NONE
) {
222 GC_mark_state
= MS_PUSH_RESCUERS
;
223 } else if (GC_mark_state
!= MS_INVALID
) {
224 ABORT("unexpected state");
225 } /* else this is really a full collection, and mark */
226 /* bits are invalid. */
231 static void alloc_mark_stack();
233 /* Perform a small amount of marking. */
234 /* We try to touch roughly a page of memory. */
235 /* Return TRUE if we just finished a mark phase. */
236 GC_bool
GC_mark_some()
238 switch(GC_mark_state
) {
242 case MS_PUSH_RESCUERS
:
243 if (GC_mark_stack_top
244 >= GC_mark_stack
+ INITIAL_MARK_STACK_SIZE
/4) {
245 GC_mark_from_mark_stack();
248 scan_ptr
= GC_push_next_marked_dirty(scan_ptr
);
251 GC_printf1("Marked from %lu dirty pages\n",
252 (unsigned long)GC_n_rescuing_pages
);
254 GC_push_roots(FALSE
);
255 GC_objects_are_marked
= TRUE
;
256 if (GC_mark_state
!= MS_INVALID
) {
257 GC_mark_state
= MS_ROOTS_PUSHED
;
263 case MS_PUSH_UNCOLLECTABLE
:
264 if (GC_mark_stack_top
265 >= GC_mark_stack
+ INITIAL_MARK_STACK_SIZE
/4) {
266 GC_mark_from_mark_stack();
269 scan_ptr
= GC_push_next_marked_uncollectable(scan_ptr
);
272 GC_objects_are_marked
= TRUE
;
273 if (GC_mark_state
!= MS_INVALID
) {
274 GC_mark_state
= MS_ROOTS_PUSHED
;
280 case MS_ROOTS_PUSHED
:
281 if (GC_mark_stack_top
>= GC_mark_stack
) {
282 GC_mark_from_mark_stack();
285 GC_mark_state
= MS_NONE
;
286 if (GC_mark_stack_too_small
) {
287 alloc_mark_stack(2*GC_mark_stack_size
);
293 case MS_PARTIALLY_INVALID
:
294 if (!GC_objects_are_marked
) {
295 GC_mark_state
= MS_PUSH_UNCOLLECTABLE
;
298 if (GC_mark_stack_top
>= GC_mark_stack
) {
299 GC_mark_from_mark_stack();
303 && (GC_mark_state
== MS_INVALID
|| GC_mark_stack_too_small
)) {
304 alloc_mark_stack(2*GC_mark_stack_size
);
305 GC_mark_state
= MS_PARTIALLY_INVALID
;
307 scan_ptr
= GC_push_next_marked(scan_ptr
);
308 if (scan_ptr
== 0 && GC_mark_state
== MS_PARTIALLY_INVALID
) {
310 GC_objects_are_marked
= TRUE
;
311 if (GC_mark_state
!= MS_INVALID
) {
312 GC_mark_state
= MS_ROOTS_PUSHED
;
317 ABORT("GC_mark_some: bad state");
323 GC_bool
GC_mark_stack_empty()
325 return(GC_mark_stack_top
< GC_mark_stack
);
329 word GC_prof_array
[10];
330 # define PROF(n) GC_prof_array[n]++
335 /* Given a pointer to someplace other than a small object page or the */
336 /* first page of a large object, return a pointer either to the */
337 /* start of the large object or NIL. */
338 /* In the latter case black list the address current. */
339 /* Returns NIL without black listing if current points to a block */
340 /* with IGNORE_OFF_PAGE set. */
342 # ifdef PRINT_BLACK_LIST
343 word
GC_find_start(current
, hhdr
, source
)
346 word
GC_find_start(current
, hhdr
)
349 register word current
;
352 # ifdef ALL_INTERIOR_POINTERS
354 register word orig
= current
;
356 current
= (word
)HBLKPTR(current
) + HDR_BYTES
;
358 current
= current
- HBLKSIZE
*(word
)hhdr
;
360 } while(IS_FORWARDING_ADDR_OR_NIL(hhdr
));
361 /* current points to the start of the large object */
362 if (hhdr
-> hb_flags
& IGNORE_OFF_PAGE
) return(0);
363 if ((word
*)orig
- (word
*)current
364 >= (ptrdiff_t)(hhdr
->hb_sz
)) {
365 /* Pointer past the end of the block */
366 GC_ADD_TO_BLACK_LIST_NORMAL(orig
, source
);
371 GC_ADD_TO_BLACK_LIST_NORMAL(current
, source
);
375 GC_ADD_TO_BLACK_LIST_NORMAL(current
, source
);
381 void GC_invalidate_mark_state()
383 GC_mark_state
= MS_INVALID
;
384 GC_mark_stack_top
= GC_mark_stack
-1;
387 mse
* GC_signal_mark_stack_overflow(msp
)
390 GC_mark_state
= MS_INVALID
;
392 GC_printf1("Mark stack overflow; current size = %lu entries\n",
395 return(msp
-INITIAL_MARK_STACK_SIZE
/8);
400 * Mark objects pointed to by the regions described by
401 * mark stack entries between GC_mark_stack and GC_mark_stack_top,
402 * inclusive. Assumes the upper limit of a mark stack entry
403 * is never 0. A mark stack entry never has size 0.
404 * We try to traverse on the order of a hblk of memory before we return.
405 * Caller is responsible for calling this until the mark stack is empty.
407 void GC_mark_from_mark_stack()
409 mse
* GC_mark_stack_reg
= GC_mark_stack
;
410 mse
* GC_mark_stack_top_reg
= GC_mark_stack_top
;
411 mse
* mark_stack_limit
= &(GC_mark_stack
[GC_mark_stack_size
]);
412 int credit
= HBLKSIZE
; /* Remaining credit for marking work */
413 register word
* current_p
; /* Pointer to current candidate ptr. */
414 register word current
; /* Candidate pointer. */
415 register word
* limit
; /* (Incl) limit of current candidate */
418 register ptr_t greatest_ha
= GC_greatest_plausible_heap_addr
;
419 register ptr_t least_ha
= GC_least_plausible_heap_addr
;
420 # define SPLIT_RANGE_WORDS 128 /* Must be power of 2. */
422 GC_objects_are_marked
= TRUE
;
423 # ifdef OS2 /* Use untweaked version to circumvent compiler problem */
424 while (GC_mark_stack_top_reg
>= GC_mark_stack_reg
&& credit
>= 0) {
426 while ((((ptr_t
)GC_mark_stack_top_reg
- (ptr_t
)GC_mark_stack_reg
) | credit
)
429 current_p
= GC_mark_stack_top_reg
-> mse_start
;
431 descr
= GC_mark_stack_top_reg
-> mse_descr
;
432 if (descr
& ((~(WORDS_TO_BYTES(SPLIT_RANGE_WORDS
) - 1)) | DS_TAGS
)) {
433 word tag
= descr
& DS_TAGS
;
438 /* Process part of the range to avoid pushing too much on the */
440 GC_mark_stack_top_reg
-> mse_start
=
441 limit
= current_p
+ SPLIT_RANGE_WORDS
-1;
442 GC_mark_stack_top_reg
-> mse_descr
-=
443 WORDS_TO_BYTES(SPLIT_RANGE_WORDS
-1);
444 /* Make sure that pointers overlapping the two ranges are */
446 limit
= (word
*)((char *)limit
+ sizeof(word
) - ALIGNMENT
);
449 GC_mark_stack_top_reg
--;
451 credit
-= WORDS_TO_BYTES(WORDSZ
/2); /* guess */
453 if ((signed_word
)descr
< 0) {
454 current
= *current_p
;
455 if ((ptr_t
)current
>= least_ha
&& (ptr_t
)current
< greatest_ha
) {
456 PUSH_CONTENTS(current
, GC_mark_stack_top_reg
, mark_stack_limit
,
465 GC_mark_stack_top_reg
--;
466 credit
-= PROC_BYTES
;
468 current_p
= GC_debug_object_start(current_p
);
470 GC_mark_stack_top_reg
=
472 (current_p
, GC_mark_stack_top_reg
,
473 mark_stack_limit
, ENV(descr
));
476 GC_mark_stack_top_reg
-> mse_descr
=
477 *(word
*)((ptr_t
)current_p
+ descr
- tag
);
481 GC_mark_stack_top_reg
--;
482 limit
= (word
*)(((ptr_t
)current_p
) + (word
)descr
);
484 /* The simple case in which we're scanning a range. */
485 credit
-= (ptr_t
)limit
- (ptr_t
)current_p
;
487 while (current_p
<= limit
) {
488 current
= *current_p
;
489 if ((ptr_t
)current
>= least_ha
&& (ptr_t
)current
< greatest_ha
) {
490 PUSH_CONTENTS(current
, GC_mark_stack_top_reg
,
491 mark_stack_limit
, current_p
, exit2
);
493 current_p
= (word
*)((char *)current_p
+ ALIGNMENT
);
496 GC_mark_stack_top
= GC_mark_stack_top_reg
;
499 /* Allocate or reallocate space for mark stack of size s words */
500 /* May silently fail. */
501 static void alloc_mark_stack(n
)
504 mse
* new_stack
= (mse
*)GC_scratch_alloc(n
* sizeof(struct ms_entry
));
506 GC_mark_stack_too_small
= FALSE
;
507 if (GC_mark_stack_size
!= 0) {
508 if (new_stack
!= 0) {
509 word displ
= (word
)GC_mark_stack
& (GC_page_size
- 1);
510 word size
= GC_mark_stack_size
* sizeof(struct ms_entry
);
512 /* Recycle old space */
513 if (0 != displ
) displ
= GC_page_size
- displ
;
514 size
= (size
- displ
) & ~(GC_page_size
- 1);
515 GC_add_to_heap((struct hblk
*)
516 ((word
)GC_mark_stack
+ displ
), size
);
517 GC_mark_stack
= new_stack
;
518 GC_mark_stack_size
= n
;
520 GC_printf1("Grew mark stack to %lu frames\n",
521 (unsigned long) GC_mark_stack_size
);
525 GC_printf1("Failed to grow mark stack to %lu frames\n",
530 if (new_stack
== 0) {
531 GC_err_printf0("No space for mark stack\n");
534 GC_mark_stack
= new_stack
;
535 GC_mark_stack_size
= n
;
537 GC_mark_stack_top
= GC_mark_stack
-1;
542 alloc_mark_stack(INITIAL_MARK_STACK_SIZE
);
546 * Push all locations between b and t onto the mark stack.
547 * b is the first location to be checked. t is one past the last
548 * location to be checked.
549 * Should only be used if there is no possibility of mark stack
552 void GC_push_all(bottom
, top
)
556 register word length
;
558 bottom
= (ptr_t
)(((word
) bottom
+ ALIGNMENT
-1) & ~(ALIGNMENT
-1));
559 top
= (ptr_t
)(((word
) top
) & ~(ALIGNMENT
-1));
560 if (top
== 0 || bottom
== top
) return;
562 if (GC_mark_stack_top
>= GC_mark_stack
+ GC_mark_stack_size
) {
563 ABORT("unexpected mark stack overflow");
565 length
= top
- bottom
;
566 # if DS_TAGS > ALIGNMENT - 1
570 GC_mark_stack_top
-> mse_start
= (word
*)bottom
;
571 GC_mark_stack_top
-> mse_descr
= length
;
575 * Analogous to the above, but push only those pages that may have been
576 * dirtied. A block h is assumed dirty if dirty_fn(h) != 0.
577 * We use push_fn to actually push the block.
578 * Will not overflow mark stack if push_fn pushes a small fixed number
579 * of entries. (This is invoked only if push_fn pushes a single entry,
580 * or if it marks each object before pushing it, thus ensuring progress
581 * in the event of a stack overflow.)
583 void GC_push_dirty(bottom
, top
, dirty_fn
, push_fn
)
586 int (*dirty_fn
)(/* struct hblk * h */);
587 void (*push_fn
)(/* ptr_t bottom, ptr_t top */);
589 register struct hblk
* h
;
591 bottom
= (ptr_t
)(((long) bottom
+ ALIGNMENT
-1) & ~(ALIGNMENT
-1));
592 top
= (ptr_t
)(((long) top
) & ~(ALIGNMENT
-1));
594 if (top
== 0 || bottom
== top
) return;
595 h
= HBLKPTR(bottom
+ HBLKSIZE
);
596 if (top
<= (ptr_t
) h
) {
597 if ((*dirty_fn
)(h
-1)) {
598 (*push_fn
)(bottom
, top
);
602 if ((*dirty_fn
)(h
-1)) {
603 (*push_fn
)(bottom
, (ptr_t
)h
);
605 while ((ptr_t
)(h
+1) <= top
) {
606 if ((*dirty_fn
)(h
)) {
607 if ((word
)(GC_mark_stack_top
- GC_mark_stack
)
608 > 3 * GC_mark_stack_size
/ 4) {
609 /* Danger of mark stack overflow */
610 (*push_fn
)((ptr_t
)h
, top
);
613 (*push_fn
)((ptr_t
)h
, (ptr_t
)(h
+1));
618 if ((ptr_t
)h
!= top
) {
619 if ((*dirty_fn
)(h
)) {
620 (*push_fn
)((ptr_t
)h
, top
);
623 if (GC_mark_stack_top
>= GC_mark_stack
+ GC_mark_stack_size
) {
624 ABORT("unexpected mark stack overflow");
628 # ifndef SMALL_CONFIG
629 void GC_push_conditional(bottom
, top
, all
)
635 if (GC_dirty_maintained
) {
637 /* Pages that were never dirtied cannot contain pointers */
638 GC_push_dirty(bottom
, top
, GC_page_was_ever_dirty
, GC_push_all
);
640 GC_push_all(bottom
, top
);
643 GC_push_all(bottom
, top
);
646 GC_push_dirty(bottom
, top
, GC_page_was_dirty
, GC_push_all
);
652 void __cdecl
GC_push_one(p
)
658 GC_PUSH_ONE_STACK(p
);
662 # define BASE(p) (word)GC_base((void *)(p))
664 # define BASE(p) (word)GC_base((char *)(p))
667 /* As above, but argument passed preliminary test. */
668 # ifdef PRINT_BLACK_LIST
669 void GC_push_one_checked(p
, interior_ptrs
, source
)
672 void GC_push_one_checked(p
, interior_ptrs
)
676 register GC_bool interior_ptrs
;
683 if (IS_FORWARDING_ADDR_OR_NIL(hhdr
)) {
684 if (hhdr
!= 0 && interior_ptrs
) {
687 displ
= BYTES_TO_WORDS(HBLKDISPL(r
));
692 register map_entry_type map_entry
;
694 displ
= HBLKDISPL(p
);
695 map_entry
= MAP_ENTRY((hhdr
-> hb_map
), displ
);
696 if (map_entry
== OBJ_INVALID
) {
699 displ
= BYTES_TO_WORDS(HBLKDISPL(r
));
700 if (r
== 0) hhdr
= 0;
705 displ
= BYTES_TO_WORDS(displ
);
707 r
= (word
)((word
*)(HBLKPTR(p
)) + displ
);
710 /* If hhdr != 0 then r == GC_base(p), only we did it faster. */
711 /* displ is the word index within the block. */
714 # ifdef PRINT_BLACK_LIST
715 GC_add_to_black_list_stack(p
, source
);
717 GC_add_to_black_list_stack(p
);
720 GC_ADD_TO_BLACK_LIST_NORMAL(p
, source
);
721 # undef source /* In case we had to define it. */
724 if (!mark_bit_from_hdr(hhdr
, displ
)) {
725 set_mark_bit_from_hdr(hhdr
, displ
);
726 PUSH_OBJ((word
*)r
, hhdr
, GC_mark_stack_top
,
727 &(GC_mark_stack
[GC_mark_stack_size
]));
734 # define TRACE_ENTRIES 1000
742 } GC_trace_buf
[TRACE_ENTRIES
];
744 int GC_trace_buf_ptr
= 0;
746 void GC_add_trace_entry(char *kind
, word arg1
, word arg2
)
748 GC_trace_buf
[GC_trace_buf_ptr
].kind
= kind
;
749 GC_trace_buf
[GC_trace_buf_ptr
].gc_no
= GC_gc_no
;
750 GC_trace_buf
[GC_trace_buf_ptr
].words_allocd
= GC_words_allocd
;
751 GC_trace_buf
[GC_trace_buf_ptr
].arg1
= arg1
^ 0x80000000;
752 GC_trace_buf
[GC_trace_buf_ptr
].arg2
= arg2
^ 0x80000000;
754 if (GC_trace_buf_ptr
>= TRACE_ENTRIES
) GC_trace_buf_ptr
= 0;
757 void GC_print_trace(word gc_no
, GC_bool lock
)
760 struct trace_entry
*p
;
763 for (i
= GC_trace_buf_ptr
-1; i
!= GC_trace_buf_ptr
; i
--) {
764 if (i
< 0) i
= TRACE_ENTRIES
-1;
765 p
= GC_trace_buf
+ i
;
766 if (p
-> gc_no
< gc_no
|| p
-> kind
== 0) return;
767 printf("Trace:%s (gc:%d,words:%d) 0x%X, 0x%X\n",
768 p
-> kind
, p
-> gc_no
, p
-> words_allocd
,
769 (p
-> arg1
) ^ 0x80000000, (p
-> arg2
) ^ 0x80000000);
771 printf("Trace incomplete\n");
775 # endif /* TRACE_BUF */
778 * A version of GC_push_all that treats all interior pointers as valid
780 void GC_push_all_stack(bottom
, top
)
784 # ifdef ALL_INTERIOR_POINTERS
785 GC_push_all(bottom
, top
);
787 GC_add_trace_entry("GC_push_all_stack", bottom
, top
);
790 word
* b
= (word
*)(((long) bottom
+ ALIGNMENT
-1) & ~(ALIGNMENT
-1));
791 word
* t
= (word
*)(((long) top
) & ~(ALIGNMENT
-1));
795 register ptr_t greatest_ha
= GC_greatest_plausible_heap_addr
;
796 register ptr_t least_ha
= GC_least_plausible_heap_addr
;
797 # define GC_greatest_plausible_heap_addr greatest_ha
798 # define GC_least_plausible_heap_addr least_ha
800 if (top
== 0) return;
801 /* check all pointers in range and put in push if they appear */
803 lim
= t
- 1 /* longword */;
804 for (p
= b
; p
<= lim
; p
= (word
*)(((char *)p
) + ALIGNMENT
)) {
806 GC_PUSH_ONE_STACK(q
);
808 # undef GC_greatest_plausible_heap_addr
809 # undef GC_least_plausible_heap_addr
814 /* Push all objects reachable from marked objects in the given block */
815 /* of size 1 objects. */
816 void GC_push_marked1(h
, hhdr
)
820 word
* mark_word_addr
= &(hhdr
->hb_marks
[divWORDSZ(HDR_WORDS
)]);
825 register word mark_word
;
826 register ptr_t greatest_ha
= GC_greatest_plausible_heap_addr
;
827 register ptr_t least_ha
= GC_least_plausible_heap_addr
;
828 # define GC_greatest_plausible_heap_addr greatest_ha
829 # define GC_least_plausible_heap_addr least_ha
831 p
= (word
*)(h
->hb_body
);
832 plim
= (word
*)(((word
)h
) + HBLKSIZE
);
834 /* go through all words in block */
836 mark_word
= *mark_word_addr
++;
838 while(mark_word
!= 0) {
848 # undef GC_greatest_plausible_heap_addr
849 # undef GC_least_plausible_heap_addr
855 /* Push all objects reachable from marked objects in the given block */
856 /* of size 2 objects. */
857 void GC_push_marked2(h
, hhdr
)
861 word
* mark_word_addr
= &(hhdr
->hb_marks
[divWORDSZ(HDR_WORDS
)]);
866 register word mark_word
;
867 register ptr_t greatest_ha
= GC_greatest_plausible_heap_addr
;
868 register ptr_t least_ha
= GC_least_plausible_heap_addr
;
869 # define GC_greatest_plausible_heap_addr greatest_ha
870 # define GC_least_plausible_heap_addr least_ha
872 p
= (word
*)(h
->hb_body
);
873 plim
= (word
*)(((word
)h
) + HBLKSIZE
);
875 /* go through all words in block */
877 mark_word
= *mark_word_addr
++;
879 while(mark_word
!= 0) {
891 # undef GC_greatest_plausible_heap_addr
892 # undef GC_least_plausible_heap_addr
895 /* Push all objects reachable from marked objects in the given block */
896 /* of size 4 objects. */
897 /* There is a risk of mark stack overflow here. But we handle that. */
898 /* And only unmarked objects get pushed, so it's not very likely. */
899 void GC_push_marked4(h
, hhdr
)
903 word
* mark_word_addr
= &(hhdr
->hb_marks
[divWORDSZ(HDR_WORDS
)]);
908 register word mark_word
;
909 register ptr_t greatest_ha
= GC_greatest_plausible_heap_addr
;
910 register ptr_t least_ha
= GC_least_plausible_heap_addr
;
911 # define GC_greatest_plausible_heap_addr greatest_ha
912 # define GC_least_plausible_heap_addr least_ha
914 p
= (word
*)(h
->hb_body
);
915 plim
= (word
*)(((word
)h
) + HBLKSIZE
);
917 /* go through all words in block */
919 mark_word
= *mark_word_addr
++;
921 while(mark_word
!= 0) {
937 # undef GC_greatest_plausible_heap_addr
938 # undef GC_least_plausible_heap_addr
941 #endif /* UNALIGNED */
943 #endif /* SMALL_CONFIG */
945 /* Push all objects reachable from marked objects in the given block */
946 void GC_push_marked(h
, hhdr
)
950 register int sz
= hhdr
-> hb_sz
;
952 register int word_no
;
954 register mse
* GC_mark_stack_top_reg
;
955 register mse
* mark_stack_limit
= &(GC_mark_stack
[GC_mark_stack_size
]);
957 /* Some quick shortcuts: */
959 struct obj_kind
*ok
= &(GC_obj_kinds
[hhdr
-> hb_obj_kind
]);
960 if ((0 | DS_LENGTH
) == ok
-> ok_descriptor
961 && FALSE
== ok
-> ok_relocate_descr
)
964 if (GC_block_empty(hhdr
)/* nothing marked */) return;
966 GC_n_rescuing_pages
++;
968 GC_objects_are_marked
= TRUE
;
970 lim
= (word
*)(h
+ 1);
972 lim
= (word
*)(h
+ 1) - sz
;
976 # if !defined(SMALL_CONFIG)
978 GC_push_marked1(h
, hhdr
);
981 # if !defined(SMALL_CONFIG) && !defined(UNALIGNED)
983 GC_push_marked2(h
, hhdr
);
986 GC_push_marked4(h
, hhdr
);
990 GC_mark_stack_top_reg
= GC_mark_stack_top
;
991 for (p
= (word
*)h
+ HDR_WORDS
, word_no
= HDR_WORDS
; p
<= lim
;
992 p
+= sz
, word_no
+= sz
) {
993 /* This ignores user specified mark procs. This currently */
994 /* doesn't matter, since marking from the whole object */
995 /* is always sufficient, and we will eventually use the user */
996 /* mark proc to avoid any bogus pointers. */
997 if (mark_bit_from_hdr(hhdr
, word_no
)) {
998 /* Mark from fields inside the object */
999 PUSH_OBJ((word
*)p
, hhdr
, GC_mark_stack_top_reg
, mark_stack_limit
);
1001 /* Subtract this object from total, since it was */
1002 /* added in twice. */
1003 GC_composite_in_use
-= sz
;
1007 GC_mark_stack_top
= GC_mark_stack_top_reg
;
1011 #ifndef SMALL_CONFIG
1012 /* Test whether any page in the given block is dirty */
1013 GC_bool
GC_block_was_dirty(h
, hhdr
)
1015 register hdr
* hhdr
;
1017 register int sz
= hhdr
-> hb_sz
;
1019 if (sz
< MAXOBJSZ
) {
1020 return(GC_page_was_dirty(h
));
1022 register ptr_t p
= (ptr_t
)h
;
1024 sz
= WORDS_TO_BYTES(sz
);
1025 while (p
< (ptr_t
)h
+ sz
) {
1026 if (GC_page_was_dirty((struct hblk
*)p
)) return(TRUE
);
1032 #endif /* SMALL_CONFIG */
1034 /* Similar to GC_push_next_marked, but return address of next block */
1035 struct hblk
* GC_push_next_marked(h
)
1038 register hdr
* hhdr
;
1040 h
= GC_next_block(h
);
1041 if (h
== 0) return(0);
1043 GC_push_marked(h
, hhdr
);
1044 return(h
+ OBJ_SZ_TO_BLOCKS(hhdr
-> hb_sz
));
1047 #ifndef SMALL_CONFIG
1048 /* Identical to above, but mark only from dirty pages */
1049 struct hblk
* GC_push_next_marked_dirty(h
)
1052 register hdr
* hhdr
= HDR(h
);
1054 if (!GC_dirty_maintained
) { ABORT("dirty bits not set up"); }
1056 h
= GC_next_block(h
);
1057 if (h
== 0) return(0);
1059 # ifdef STUBBORN_ALLOC
1060 if (hhdr
-> hb_obj_kind
== STUBBORN
) {
1061 if (GC_page_was_changed(h
) && GC_block_was_dirty(h
, hhdr
)) {
1065 if (GC_block_was_dirty(h
, hhdr
)) break;
1068 if (GC_block_was_dirty(h
, hhdr
)) break;
1070 h
+= OBJ_SZ_TO_BLOCKS(hhdr
-> hb_sz
);
1072 GC_push_marked(h
, hhdr
);
1073 return(h
+ OBJ_SZ_TO_BLOCKS(hhdr
-> hb_sz
));
1077 /* Similar to above, but for uncollectable pages. Needed since we */
1078 /* do not clear marks for such pages, even for full collections. */
1079 struct hblk
* GC_push_next_marked_uncollectable(h
)
1082 register hdr
* hhdr
= HDR(h
);
1085 h
= GC_next_block(h
);
1086 if (h
== 0) return(0);
1088 if (hhdr
-> hb_obj_kind
== UNCOLLECTABLE
) break;
1089 h
+= OBJ_SZ_TO_BLOCKS(hhdr
-> hb_sz
);
1091 GC_push_marked(h
, hhdr
);
1092 return(h
+ OBJ_SZ_TO_BLOCKS(hhdr
-> hb_sz
));