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. */
25 void GC_noop(void *p, ...) {}
30 /* Single argument version, robust against whole program analysis. */
34 static VOLATILE word sink;
39 /* mark_proc GC_mark_procs[MAX_MARK_PROCS] = {0} -- declared in gc_priv.h */
41 word GC_n_mark_procs = 0;
43 /* Initialize GC_obj_kinds properly and standard free lists properly. */
44 /* This must be done statically since they may be accessed before */
45 /* GC_init is called. */
46 /* It's done here, since we need to deal with mark descriptors. */
47 struct obj_kind GC_obj_kinds[MAXOBJKINDS] = {
48 /* PTRFREE */ { &GC_aobjfreelist[0], 0 /* filled in dynamically */,
49 0 | DS_LENGTH, FALSE, FALSE },
50 /* NORMAL */ { &GC_objfreelist[0], 0,
51 # if defined(ADD_BYTE_AT_END) && ALIGNMENT > DS_TAGS
52 (word)(-ALIGNMENT) | DS_LENGTH,
56 TRUE /* add length to descr */, TRUE },
58 { &GC_uobjfreelist[0], 0,
59 0 | DS_LENGTH, TRUE /* add length to descr */, TRUE },
60 # ifdef ATOMIC_UNCOLLECTABLE
62 { &GC_auobjfreelist[0], 0,
63 0 | DS_LENGTH, FALSE /* add length to descr */, FALSE },
65 # ifdef STUBBORN_ALLOC
66 /*STUBBORN*/ { &GC_sobjfreelist[0], 0,
67 0 | DS_LENGTH, TRUE /* add length to descr */, TRUE },
71 # ifdef ATOMIC_UNCOLLECTABLE
72 # ifdef STUBBORN_ALLOC
78 # ifdef STUBBORN_ALLOC
86 # ifndef INITIAL_MARK_STACK_SIZE
87 # define INITIAL_MARK_STACK_SIZE (1*HBLKSIZE)
88 /* INITIAL_MARK_STACK_SIZE * sizeof(mse) should be a */
89 /* multiple of HBLKSIZE. */
93 * Limits of stack for GC_mark routine.
94 * All ranges between GC_mark_stack(incl.) and GC_mark_stack_top(incl.) still
95 * need to be marked from.
98 word GC_n_rescuing_pages; /* Number of dirty pages we marked from */
99 /* excludes ptrfree pages, etc. */
103 word GC_mark_stack_size = 0;
105 mse * GC_mark_stack_top;
107 static struct hblk * scan_ptr;
109 mark_state_t GC_mark_state = MS_NONE;
111 GC_bool GC_mark_stack_too_small = FALSE;
113 GC_bool GC_objects_are_marked = FALSE; /* Are there collectable marked */
114 /* objects in the heap? */
116 /* Is a collection in progress? Note that this can return true in the */
117 /* nonincremental case, if a collection has been abandoned and the */
118 /* mark state is now MS_INVALID. */
119 GC_bool GC_collection_in_progress()
121 return(GC_mark_state != MS_NONE);
124 /* clear all mark bits in the header */
125 void GC_clear_hdr_marks(hhdr)
128 BZERO(hhdr -> hb_marks, MARK_BITS_SZ*sizeof(word));
131 /* Set all mark bits in the header. Used for uncollectable blocks. */
132 void GC_set_hdr_marks(hhdr)
137 for (i = 0; i < MARK_BITS_SZ; ++i) {
138 hhdr -> hb_marks[i] = ONES;
143 * Clear all mark bits associated with block h.
146 static void clear_marks_for_block(h, dummy)
150 register hdr * hhdr = HDR(h);
152 if (IS_UNCOLLECTABLE(hhdr -> hb_obj_kind)) return;
153 /* Mark bit for these is cleared only once the object is */
154 /* explicitly deallocated. This either frees the block, or */
155 /* the bit is cleared once the object is on the free list. */
156 GC_clear_hdr_marks(hhdr);
159 /* Slow but general routines for setting/clearing/asking about mark bits */
160 void GC_set_mark_bit(p)
163 register struct hblk *h = HBLKPTR(p);
164 register hdr * hhdr = HDR(h);
165 register int word_no = (word *)p - (word *)h;
167 set_mark_bit_from_hdr(hhdr, word_no);
170 void GC_clear_mark_bit(p)
173 register struct hblk *h = HBLKPTR(p);
174 register hdr * hhdr = HDR(h);
175 register int word_no = (word *)p - (word *)h;
177 clear_mark_bit_from_hdr(hhdr, word_no);
180 GC_bool GC_is_marked(p)
183 register struct hblk *h = HBLKPTR(p);
184 register hdr * hhdr = HDR(h);
185 register int word_no = (word *)p - (word *)h;
187 return(mark_bit_from_hdr(hhdr, word_no));
192 * Clear mark bits in all allocated heap blocks. This invalidates
193 * the marker invariant, and sets GC_mark_state to reflect this.
194 * (This implicitly starts marking to reestablish the invariant.)
196 void GC_clear_marks()
198 GC_apply_to_all_blocks(clear_marks_for_block, (word)0);
199 GC_objects_are_marked = FALSE;
200 GC_mark_state = MS_INVALID;
203 /* Counters reflect currently marked objects: reset here */
204 GC_composite_in_use = 0;
205 GC_atomic_in_use = 0;
210 /* Initiate a garbage collection. Initiates a full collection if the */
211 /* mark state is invalid. */
213 void GC_initiate_gc()
215 if (GC_dirty_maintained) GC_read_dirty();
216 # ifdef STUBBORN_ALLOC
221 extern void GC_check_dirty();
223 if (GC_dirty_maintained) GC_check_dirty();
227 GC_n_rescuing_pages = 0;
229 if (GC_mark_state == MS_NONE) {
230 GC_mark_state = MS_PUSH_RESCUERS;
231 } else if (GC_mark_state != MS_INVALID) {
232 ABORT("unexpected state");
233 } /* else this is really a full collection, and mark */
234 /* bits are invalid. */
239 static void alloc_mark_stack();
241 /* Perform a small amount of marking. */
242 /* We try to touch roughly a page of memory. */
243 /* Return TRUE if we just finished a mark phase. */
244 /* Cold_gc_frame is an address inside a GC frame that */
245 /* remains valid until all marking is complete. */
246 /* A zero value indicates that it's OK to miss some */
247 /* register values. */
248 GC_bool GC_mark_some(cold_gc_frame)
251 switch(GC_mark_state) {
255 case MS_PUSH_RESCUERS:
256 if (GC_mark_stack_top
257 >= GC_mark_stack + INITIAL_MARK_STACK_SIZE/4) {
258 GC_mark_from_mark_stack();
261 scan_ptr = GC_push_next_marked_dirty(scan_ptr);
264 GC_printf1("Marked from %lu dirty pages\n",
265 (unsigned long)GC_n_rescuing_pages);
267 GC_push_roots(FALSE, cold_gc_frame);
268 GC_objects_are_marked = TRUE;
269 if (GC_mark_state != MS_INVALID) {
270 GC_mark_state = MS_ROOTS_PUSHED;
276 case MS_PUSH_UNCOLLECTABLE:
277 if (GC_mark_stack_top
278 >= GC_mark_stack + INITIAL_MARK_STACK_SIZE/4) {
279 GC_mark_from_mark_stack();
282 scan_ptr = GC_push_next_marked_uncollectable(scan_ptr);
284 GC_push_roots(TRUE, cold_gc_frame);
285 GC_objects_are_marked = TRUE;
286 if (GC_mark_state != MS_INVALID) {
287 GC_mark_state = MS_ROOTS_PUSHED;
293 case MS_ROOTS_PUSHED:
294 if (GC_mark_stack_top >= GC_mark_stack) {
295 GC_mark_from_mark_stack();
298 GC_mark_state = MS_NONE;
299 if (GC_mark_stack_too_small) {
300 alloc_mark_stack(2*GC_mark_stack_size);
306 case MS_PARTIALLY_INVALID:
307 if (!GC_objects_are_marked) {
308 GC_mark_state = MS_PUSH_UNCOLLECTABLE;
311 if (GC_mark_stack_top >= GC_mark_stack) {
312 GC_mark_from_mark_stack();
315 if (scan_ptr == 0 && GC_mark_state == MS_INVALID) {
316 /* About to start a heap scan for marked objects. */
317 /* Mark stack is empty. OK to reallocate. */
318 if (GC_mark_stack_too_small) {
319 alloc_mark_stack(2*GC_mark_stack_size);
321 GC_mark_state = MS_PARTIALLY_INVALID;
323 scan_ptr = GC_push_next_marked(scan_ptr);
324 if (scan_ptr == 0 && GC_mark_state == MS_PARTIALLY_INVALID) {
325 GC_push_roots(TRUE, cold_gc_frame);
326 GC_objects_are_marked = TRUE;
327 if (GC_mark_state != MS_INVALID) {
328 GC_mark_state = MS_ROOTS_PUSHED;
333 ABORT("GC_mark_some: bad state");
339 GC_bool GC_mark_stack_empty()
341 return(GC_mark_stack_top < GC_mark_stack);
345 word GC_prof_array[10];
346 # define PROF(n) GC_prof_array[n]++
351 /* Given a pointer to someplace other than a small object page or the */
352 /* first page of a large object, return a pointer either to the */
353 /* start of the large object or NIL. */
354 /* In the latter case black list the address current. */
355 /* Returns NIL without black listing if current points to a block */
356 /* with IGNORE_OFF_PAGE set. */
358 # ifdef PRINT_BLACK_LIST
359 word GC_find_start(current, hhdr, source)
362 word GC_find_start(current, hhdr)
365 register word current;
368 # ifdef ALL_INTERIOR_POINTERS
370 register word orig = current;
372 current = (word)HBLKPTR(current) + HDR_BYTES;
374 current = current - HBLKSIZE*(word)hhdr;
376 } while(IS_FORWARDING_ADDR_OR_NIL(hhdr));
377 /* current points to the start of the large object */
378 if (hhdr -> hb_flags & IGNORE_OFF_PAGE) return(0);
379 if ((word *)orig - (word *)current
380 >= (ptrdiff_t)(hhdr->hb_sz)) {
381 /* Pointer past the end of the block */
382 GC_ADD_TO_BLACK_LIST_NORMAL(orig, source);
387 GC_ADD_TO_BLACK_LIST_NORMAL(current, source);
391 GC_ADD_TO_BLACK_LIST_NORMAL(current, source);
397 void GC_invalidate_mark_state()
399 GC_mark_state = MS_INVALID;
400 GC_mark_stack_top = GC_mark_stack-1;
403 mse * GC_signal_mark_stack_overflow(msp)
406 GC_mark_state = MS_INVALID;
407 GC_mark_stack_too_small = TRUE;
409 GC_printf1("Mark stack overflow; current size = %lu entries\n",
412 return(msp-INITIAL_MARK_STACK_SIZE/8);
417 * Mark objects pointed to by the regions described by
418 * mark stack entries between GC_mark_stack and GC_mark_stack_top,
419 * inclusive. Assumes the upper limit of a mark stack entry
420 * is never 0. A mark stack entry never has size 0.
421 * We try to traverse on the order of a hblk of memory before we return.
422 * Caller is responsible for calling this until the mark stack is empty.
424 void GC_mark_from_mark_stack()
426 mse * GC_mark_stack_reg = GC_mark_stack;
427 mse * GC_mark_stack_top_reg = GC_mark_stack_top;
428 mse * mark_stack_limit = &(GC_mark_stack[GC_mark_stack_size]);
429 int credit = HBLKSIZE; /* Remaining credit for marking work */
430 register word * current_p; /* Pointer to current candidate ptr. */
431 register word current; /* Candidate pointer. */
432 register word * limit; /* (Incl) limit of current candidate */
435 register ptr_t greatest_ha = GC_greatest_plausible_heap_addr;
436 register ptr_t least_ha = GC_least_plausible_heap_addr;
437 # define SPLIT_RANGE_WORDS 128 /* Must be power of 2. */
439 GC_objects_are_marked = TRUE;
440 # ifdef OS2 /* Use untweaked version to circumvent compiler problem */
441 while (GC_mark_stack_top_reg >= GC_mark_stack_reg && credit >= 0) {
443 while ((((ptr_t)GC_mark_stack_top_reg - (ptr_t)GC_mark_stack_reg) | credit)
446 current_p = GC_mark_stack_top_reg -> mse_start;
448 descr = GC_mark_stack_top_reg -> mse_descr;
449 if (descr & ((~(WORDS_TO_BYTES(SPLIT_RANGE_WORDS) - 1)) | DS_TAGS)) {
450 word tag = descr & DS_TAGS;
455 /* Process part of the range to avoid pushing too much on the */
457 GC_mark_stack_top_reg -> mse_start =
458 limit = current_p + SPLIT_RANGE_WORDS-1;
459 GC_mark_stack_top_reg -> mse_descr -=
460 WORDS_TO_BYTES(SPLIT_RANGE_WORDS-1);
461 /* Make sure that pointers overlapping the two ranges are */
463 limit = (word *)((char *)limit + sizeof(word) - ALIGNMENT);
466 GC_mark_stack_top_reg--;
468 credit -= WORDS_TO_BYTES(WORDSZ/2); /* guess */
470 if ((signed_word)descr < 0) {
471 current = *current_p;
472 if ((ptr_t)current >= least_ha && (ptr_t)current < greatest_ha) {
473 PUSH_CONTENTS(current, GC_mark_stack_top_reg, mark_stack_limit,
482 GC_mark_stack_top_reg--;
483 credit -= PROC_BYTES;
484 GC_mark_stack_top_reg =
486 (current_p, GC_mark_stack_top_reg,
487 mark_stack_limit, ENV(descr));
490 GC_mark_stack_top_reg -> mse_descr =
491 *(word *)((ptr_t)current_p + descr - tag);
495 GC_mark_stack_top_reg--;
496 limit = (word *)(((ptr_t)current_p) + (word)descr);
498 /* The simple case in which we're scanning a range. */
499 credit -= (ptr_t)limit - (ptr_t)current_p;
501 while (current_p <= limit) {
502 current = *current_p;
503 if ((ptr_t)current >= least_ha && (ptr_t)current < greatest_ha) {
504 PUSH_CONTENTS(current, GC_mark_stack_top_reg,
505 mark_stack_limit, current_p, exit2);
507 current_p = (word *)((char *)current_p + ALIGNMENT);
510 GC_mark_stack_top = GC_mark_stack_top_reg;
513 /* Allocate or reallocate space for mark stack of size s words */
514 /* May silently fail. */
515 static void alloc_mark_stack(n)
518 mse * new_stack = (mse *)GC_scratch_alloc(n * sizeof(struct ms_entry));
520 GC_mark_stack_too_small = FALSE;
521 if (GC_mark_stack_size != 0) {
522 if (new_stack != 0) {
523 word displ = (word)GC_mark_stack & (GC_page_size - 1);
524 signed_word size = GC_mark_stack_size * sizeof(struct ms_entry);
526 /* Recycle old space */
527 if (0 != displ) displ = GC_page_size - displ;
528 size = (size - displ) & ~(GC_page_size - 1);
530 GC_add_to_heap((struct hblk *)
531 ((word)GC_mark_stack + displ), (word)size);
533 GC_mark_stack = new_stack;
534 GC_mark_stack_size = n;
536 GC_printf1("Grew mark stack to %lu frames\n",
537 (unsigned long) GC_mark_stack_size);
541 GC_printf1("Failed to grow mark stack to %lu frames\n",
546 if (new_stack == 0) {
547 GC_err_printf0("No space for mark stack\n");
550 GC_mark_stack = new_stack;
551 GC_mark_stack_size = n;
553 GC_mark_stack_top = GC_mark_stack-1;
558 alloc_mark_stack(INITIAL_MARK_STACK_SIZE);
562 * Push all locations between b and t onto the mark stack.
563 * b is the first location to be checked. t is one past the last
564 * location to be checked.
565 * Should only be used if there is no possibility of mark stack
568 void GC_push_all(bottom, top)
572 register word length;
574 bottom = (ptr_t)(((word) bottom + ALIGNMENT-1) & ~(ALIGNMENT-1));
575 top = (ptr_t)(((word) top) & ~(ALIGNMENT-1));
576 if (top == 0 || bottom == top) return;
578 if (GC_mark_stack_top >= GC_mark_stack + GC_mark_stack_size) {
579 ABORT("unexpected mark stack overflow");
581 length = top - bottom;
582 # if DS_TAGS > ALIGNMENT - 1
586 GC_mark_stack_top -> mse_start = (word *)bottom;
587 GC_mark_stack_top -> mse_descr = length;
591 * Analogous to the above, but push only those pages that may have been
592 * dirtied. A block h is assumed dirty if dirty_fn(h) != 0.
593 * We use push_fn to actually push the block.
594 * Will not overflow mark stack if push_fn pushes a small fixed number
595 * of entries. (This is invoked only if push_fn pushes a single entry,
596 * or if it marks each object before pushing it, thus ensuring progress
597 * in the event of a stack overflow.)
599 void GC_push_dirty(bottom, top, dirty_fn, push_fn)
602 int (*dirty_fn)(/* struct hblk * h */);
603 void (*push_fn)(/* ptr_t bottom, ptr_t top */);
605 register struct hblk * h;
607 bottom = (ptr_t)(((long) bottom + ALIGNMENT-1) & ~(ALIGNMENT-1));
608 top = (ptr_t)(((long) top) & ~(ALIGNMENT-1));
610 if (top == 0 || bottom == top) return;
611 h = HBLKPTR(bottom + HBLKSIZE);
612 if (top <= (ptr_t) h) {
613 if ((*dirty_fn)(h-1)) {
614 (*push_fn)(bottom, top);
618 if ((*dirty_fn)(h-1)) {
619 (*push_fn)(bottom, (ptr_t)h);
621 while ((ptr_t)(h+1) <= top) {
622 if ((*dirty_fn)(h)) {
623 if ((word)(GC_mark_stack_top - GC_mark_stack)
624 > 3 * GC_mark_stack_size / 4) {
625 /* Danger of mark stack overflow */
626 (*push_fn)((ptr_t)h, top);
629 (*push_fn)((ptr_t)h, (ptr_t)(h+1));
634 if ((ptr_t)h != top) {
635 if ((*dirty_fn)(h)) {
636 (*push_fn)((ptr_t)h, top);
639 if (GC_mark_stack_top >= GC_mark_stack + GC_mark_stack_size) {
640 ABORT("unexpected mark stack overflow");
644 # ifndef SMALL_CONFIG
645 void GC_push_conditional(bottom, top, all)
651 if (GC_dirty_maintained) {
653 /* Pages that were never dirtied cannot contain pointers */
654 GC_push_dirty(bottom, top, GC_page_was_ever_dirty, GC_push_all);
656 GC_push_all(bottom, top);
659 GC_push_all(bottom, top);
662 GC_push_dirty(bottom, top, GC_page_was_dirty, GC_push_all);
668 void __cdecl GC_push_one(p)
674 GC_PUSH_ONE_STACK(p, 0);
678 # define BASE(p) (word)GC_base((void *)(p))
680 # define BASE(p) (word)GC_base((char *)(p))
683 /* As above, but argument passed preliminary test. */
684 # if defined(PRINT_BLACK_LIST) || defined(KEEP_BACK_PTRS)
685 void GC_push_one_checked(p, interior_ptrs, source)
688 void GC_push_one_checked(p, interior_ptrs)
692 register GC_bool interior_ptrs;
699 if (IS_FORWARDING_ADDR_OR_NIL(hhdr)) {
700 if (hhdr != 0 && interior_ptrs) {
703 displ = BYTES_TO_WORDS(HBLKDISPL(r));
708 register map_entry_type map_entry;
710 displ = HBLKDISPL(p);
711 map_entry = MAP_ENTRY((hhdr -> hb_map), displ);
712 if (map_entry == OBJ_INVALID) {
713 # ifndef ALL_INTERIOR_POINTERS
716 displ = BYTES_TO_WORDS(HBLKDISPL(r));
717 if (r == 0) hhdr = 0;
722 /* map already reflects interior pointers */
726 displ = BYTES_TO_WORDS(displ);
728 r = (word)((word *)(HBLKPTR(p)) + displ);
731 /* If hhdr != 0 then r == GC_base(p), only we did it faster. */
732 /* displ is the word index within the block. */
735 # ifdef PRINT_BLACK_LIST
736 GC_add_to_black_list_stack(p, source);
738 GC_add_to_black_list_stack(p);
741 GC_ADD_TO_BLACK_LIST_NORMAL(p, source);
742 # undef source /* In case we had to define it. */
745 if (!mark_bit_from_hdr(hhdr, displ)) {
746 set_mark_bit_from_hdr(hhdr, displ);
747 GC_STORE_BACK_PTR(source, (ptr_t)r);
748 PUSH_OBJ((word *)r, hhdr, GC_mark_stack_top,
749 &(GC_mark_stack[GC_mark_stack_size]));
756 # define TRACE_ENTRIES 1000
764 } GC_trace_buf[TRACE_ENTRIES];
766 int GC_trace_buf_ptr = 0;
768 void GC_add_trace_entry(char *kind, word arg1, word arg2)
770 GC_trace_buf[GC_trace_buf_ptr].kind = kind;
771 GC_trace_buf[GC_trace_buf_ptr].gc_no = GC_gc_no;
772 GC_trace_buf[GC_trace_buf_ptr].words_allocd = GC_words_allocd;
773 GC_trace_buf[GC_trace_buf_ptr].arg1 = arg1 ^ 0x80000000;
774 GC_trace_buf[GC_trace_buf_ptr].arg2 = arg2 ^ 0x80000000;
776 if (GC_trace_buf_ptr >= TRACE_ENTRIES) GC_trace_buf_ptr = 0;
779 void GC_print_trace(word gc_no, GC_bool lock)
782 struct trace_entry *p;
785 for (i = GC_trace_buf_ptr-1; i != GC_trace_buf_ptr; i--) {
786 if (i < 0) i = TRACE_ENTRIES-1;
787 p = GC_trace_buf + i;
788 if (p -> gc_no < gc_no || p -> kind == 0) return;
789 printf("Trace:%s (gc:%d,words:%d) 0x%X, 0x%X\n",
790 p -> kind, p -> gc_no, p -> words_allocd,
791 (p -> arg1) ^ 0x80000000, (p -> arg2) ^ 0x80000000);
793 printf("Trace incomplete\n");
797 # endif /* TRACE_BUF */
800 * A version of GC_push_all that treats all interior pointers as valid
801 * and scans the entire region immediately, in case the contents
804 void GC_push_all_eager(bottom, top)
808 word * b = (word *)(((long) bottom + ALIGNMENT-1) & ~(ALIGNMENT-1));
809 word * t = (word *)(((long) top) & ~(ALIGNMENT-1));
813 register ptr_t greatest_ha = GC_greatest_plausible_heap_addr;
814 register ptr_t least_ha = GC_least_plausible_heap_addr;
815 # define GC_greatest_plausible_heap_addr greatest_ha
816 # define GC_least_plausible_heap_addr least_ha
818 if (top == 0) return;
819 /* check all pointers in range and put in push if they appear */
821 lim = t - 1 /* longword */;
822 for (p = b; p <= lim; p = (word *)(((char *)p) + ALIGNMENT)) {
824 GC_PUSH_ONE_STACK(q, p);
826 # undef GC_greatest_plausible_heap_addr
827 # undef GC_least_plausible_heap_addr
832 * A version of GC_push_all that treats all interior pointers as valid
833 * and scans part of the area immediately, to make sure that saved
834 * register values are not lost.
835 * Cold_gc_frame delimits the stack section that must be scanned
836 * eagerly. A zero value indicates that no eager scanning is needed.
838 void GC_push_all_stack_partially_eager(bottom, top, cold_gc_frame)
843 # ifdef ALL_INTERIOR_POINTERS
844 # define EAGER_BYTES 1024
845 /* Push the hot end of the stack eagerly, so that register values */
846 /* saved inside GC frames are marked before they disappear. */
847 /* The rest of the marking can be deferred until later. */
848 if (0 == cold_gc_frame) {
849 GC_push_all_stack(bottom, top);
852 # ifdef STACK_GROWS_DOWN
853 GC_push_all_eager(bottom, cold_gc_frame);
854 GC_push_all(cold_gc_frame - sizeof(ptr_t), top);
855 # else /* STACK_GROWS_UP */
856 GC_push_all_eager(cold_gc_frame, top);
857 GC_push_all(bottom, cold_gc_frame + sizeof(ptr_t));
858 # endif /* STACK_GROWS_UP */
860 GC_push_all_eager(bottom, top);
863 GC_add_trace_entry("GC_push_all_stack", bottom, top);
866 #endif /* !THREADS */
868 void GC_push_all_stack(bottom, top)
872 # ifdef ALL_INTERIOR_POINTERS
873 GC_push_all(bottom, top);
875 GC_push_all_eager(bottom, top);
880 /* Push all objects reachable from marked objects in the given block */
881 /* of size 1 objects. */
882 void GC_push_marked1(h, hhdr)
886 word * mark_word_addr = &(hhdr->hb_marks[divWORDSZ(HDR_WORDS)]);
891 register word mark_word;
892 register ptr_t greatest_ha = GC_greatest_plausible_heap_addr;
893 register ptr_t least_ha = GC_least_plausible_heap_addr;
894 # define GC_greatest_plausible_heap_addr greatest_ha
895 # define GC_least_plausible_heap_addr least_ha
897 p = (word *)(h->hb_body);
898 plim = (word *)(((word)h) + HBLKSIZE);
900 /* go through all words in block */
902 mark_word = *mark_word_addr++;
904 while(mark_word != 0) {
907 GC_PUSH_ONE_HEAP(q, p + i);
914 # undef GC_greatest_plausible_heap_addr
915 # undef GC_least_plausible_heap_addr
921 /* Push all objects reachable from marked objects in the given block */
922 /* of size 2 objects. */
923 void GC_push_marked2(h, hhdr)
927 word * mark_word_addr = &(hhdr->hb_marks[divWORDSZ(HDR_WORDS)]);
932 register word mark_word;
933 register ptr_t greatest_ha = GC_greatest_plausible_heap_addr;
934 register ptr_t least_ha = GC_least_plausible_heap_addr;
935 # define GC_greatest_plausible_heap_addr greatest_ha
936 # define GC_least_plausible_heap_addr least_ha
938 p = (word *)(h->hb_body);
939 plim = (word *)(((word)h) + HBLKSIZE);
941 /* go through all words in block */
943 mark_word = *mark_word_addr++;
945 while(mark_word != 0) {
948 GC_PUSH_ONE_HEAP(q, p + i);
950 GC_PUSH_ONE_HEAP(q, p + i);
957 # undef GC_greatest_plausible_heap_addr
958 # undef GC_least_plausible_heap_addr
961 /* Push all objects reachable from marked objects in the given block */
962 /* of size 4 objects. */
963 /* There is a risk of mark stack overflow here. But we handle that. */
964 /* And only unmarked objects get pushed, so it's not very likely. */
965 void GC_push_marked4(h, hhdr)
969 word * mark_word_addr = &(hhdr->hb_marks[divWORDSZ(HDR_WORDS)]);
974 register word mark_word;
975 register ptr_t greatest_ha = GC_greatest_plausible_heap_addr;
976 register ptr_t least_ha = GC_least_plausible_heap_addr;
977 # define GC_greatest_plausible_heap_addr greatest_ha
978 # define GC_least_plausible_heap_addr least_ha
980 p = (word *)(h->hb_body);
981 plim = (word *)(((word)h) + HBLKSIZE);
983 /* go through all words in block */
985 mark_word = *mark_word_addr++;
987 while(mark_word != 0) {
990 GC_PUSH_ONE_HEAP(q, p + i);
992 GC_PUSH_ONE_HEAP(q, p + i + 1);
994 GC_PUSH_ONE_HEAP(q, p + i + 2);
996 GC_PUSH_ONE_HEAP(q, p + i + 3);
1003 # undef GC_greatest_plausible_heap_addr
1004 # undef GC_least_plausible_heap_addr
1007 #endif /* UNALIGNED */
1009 #endif /* SMALL_CONFIG */
1011 /* Push all objects reachable from marked objects in the given block */
1012 void GC_push_marked(h, hhdr)
1014 register hdr * hhdr;
1016 register int sz = hhdr -> hb_sz;
1018 register int word_no;
1019 register word * lim;
1020 register mse * GC_mark_stack_top_reg;
1021 register mse * mark_stack_limit = &(GC_mark_stack[GC_mark_stack_size]);
1023 /* Some quick shortcuts: */
1025 struct obj_kind *ok = &(GC_obj_kinds[hhdr -> hb_obj_kind]);
1026 if ((0 | DS_LENGTH) == ok -> ok_descriptor
1027 && FALSE == ok -> ok_relocate_descr)
1030 if (GC_block_empty(hhdr)/* nothing marked */) return;
1032 GC_n_rescuing_pages++;
1034 GC_objects_are_marked = TRUE;
1035 if (sz > MAXOBJSZ) {
1036 lim = (word *)(h + 1);
1038 lim = (word *)(h + 1) - sz;
1042 # if !defined(SMALL_CONFIG)
1044 GC_push_marked1(h, hhdr);
1047 # if !defined(SMALL_CONFIG) && !defined(UNALIGNED)
1049 GC_push_marked2(h, hhdr);
1052 GC_push_marked4(h, hhdr);
1056 GC_mark_stack_top_reg = GC_mark_stack_top;
1057 for (p = (word *)h + HDR_WORDS, word_no = HDR_WORDS; p <= lim;
1058 p += sz, word_no += sz) {
1059 /* This ignores user specified mark procs. This currently */
1060 /* doesn't matter, since marking from the whole object */
1061 /* is always sufficient, and we will eventually use the user */
1062 /* mark proc to avoid any bogus pointers. */
1063 if (mark_bit_from_hdr(hhdr, word_no)) {
1064 /* Mark from fields inside the object */
1065 PUSH_OBJ((word *)p, hhdr, GC_mark_stack_top_reg, mark_stack_limit);
1067 /* Subtract this object from total, since it was */
1068 /* added in twice. */
1069 GC_composite_in_use -= sz;
1073 GC_mark_stack_top = GC_mark_stack_top_reg;
1077 #ifndef SMALL_CONFIG
1078 /* Test whether any page in the given block is dirty */
1079 GC_bool GC_block_was_dirty(h, hhdr)
1081 register hdr * hhdr;
1083 register int sz = hhdr -> hb_sz;
1085 if (sz < MAXOBJSZ) {
1086 return(GC_page_was_dirty(h));
1088 register ptr_t p = (ptr_t)h;
1090 sz = WORDS_TO_BYTES(sz);
1091 while (p < (ptr_t)h + sz) {
1092 if (GC_page_was_dirty((struct hblk *)p)) return(TRUE);
1098 #endif /* SMALL_CONFIG */
1100 /* Similar to GC_push_next_marked, but return address of next block */
1101 struct hblk * GC_push_next_marked(h)
1104 register hdr * hhdr;
1106 h = GC_next_used_block(h);
1107 if (h == 0) return(0);
1109 GC_push_marked(h, hhdr);
1110 return(h + OBJ_SZ_TO_BLOCKS(hhdr -> hb_sz));
1113 #ifndef SMALL_CONFIG
1114 /* Identical to above, but mark only from dirty pages */
1115 struct hblk * GC_push_next_marked_dirty(h)
1118 register hdr * hhdr = HDR(h);
1120 if (!GC_dirty_maintained) { ABORT("dirty bits not set up"); }
1122 h = GC_next_used_block(h);
1123 if (h == 0) return(0);
1125 # ifdef STUBBORN_ALLOC
1126 if (hhdr -> hb_obj_kind == STUBBORN) {
1127 if (GC_page_was_changed(h) && GC_block_was_dirty(h, hhdr)) {
1131 if (GC_block_was_dirty(h, hhdr)) break;
1134 if (GC_block_was_dirty(h, hhdr)) break;
1136 h += OBJ_SZ_TO_BLOCKS(hhdr -> hb_sz);
1138 GC_push_marked(h, hhdr);
1139 return(h + OBJ_SZ_TO_BLOCKS(hhdr -> hb_sz));
1143 /* Similar to above, but for uncollectable pages. Needed since we */
1144 /* do not clear marks for such pages, even for full collections. */
1145 struct hblk * GC_push_next_marked_uncollectable(h)
1148 register hdr * hhdr = HDR(h);
1151 h = GC_next_used_block(h);
1152 if (h == 0) return(0);
1154 if (hhdr -> hb_obj_kind == UNCOLLECTABLE) break;
1155 h += OBJ_SZ_TO_BLOCKS(hhdr -> hb_sz);
1157 GC_push_marked(h, hhdr);
1158 return(h + OBJ_SZ_TO_BLOCKS(hhdr -> hb_sz));