OSDN Git Service

2002-01-18 Toon Moene <toon@moene.indiv.nluug.nl>
[pf3gnuchains/gcc-fork.git] / boehm-gc / mark.c
1
2 /*
3  * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
4  * Copyright (c) 1991-1995 by Xerox Corporation.  All rights reserved.
5  * Copyright (c) 2000 by Hewlett-Packard Company.  All rights reserved.
6  *
7  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
8  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
9  *
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.
15  *
16  */
17
18
19 # include <stdio.h>
20 # include "private/gc_pmark.h"
21
22 /* We put this here to minimize the risk of inlining. */
23 /*VARARGS*/
24 #ifdef __WATCOMC__
25   void GC_noop(void *p, ...) {}
26 #else
27   void GC_noop() {}
28 #endif
29
30 /* Single argument version, robust against whole program analysis. */
31 void GC_noop1(x)
32 word x;
33 {
34     static VOLATILE word sink;
35
36     sink = x;
37 }
38
39 /* mark_proc GC_mark_procs[MAX_MARK_PROCS] = {0} -- declared in gc_priv.h */
40
41 word GC_n_mark_procs = GC_RESERVED_MARK_PROCS;
42
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 | GC_DS_LENGTH, FALSE, FALSE },
50 /* NORMAL  */ { &GC_objfreelist[0], 0,
51                 0 | GC_DS_LENGTH,  /* Adjusted in GC_init_inner for EXTRA_BYTES */
52                 TRUE /* add length to descr */, TRUE },
53 /* UNCOLLECTABLE */
54               { &GC_uobjfreelist[0], 0,
55                 0 | GC_DS_LENGTH, TRUE /* add length to descr */, TRUE },
56 # ifdef ATOMIC_UNCOLLECTABLE
57    /* AUNCOLLECTABLE */
58               { &GC_auobjfreelist[0], 0,
59                 0 | GC_DS_LENGTH, FALSE /* add length to descr */, FALSE },
60 # endif
61 # ifdef STUBBORN_ALLOC
62 /*STUBBORN*/ { &GC_sobjfreelist[0], 0,
63                 0 | GC_DS_LENGTH, TRUE /* add length to descr */, TRUE },
64 # endif
65 };
66
67 # ifdef ATOMIC_UNCOLLECTABLE
68 #   ifdef STUBBORN_ALLOC
69       int GC_n_kinds = 5;
70 #   else
71       int GC_n_kinds = 4;
72 #   endif
73 # else
74 #   ifdef STUBBORN_ALLOC
75       int GC_n_kinds = 4;
76 #   else
77       int GC_n_kinds = 3;
78 #   endif
79 # endif
80
81
82 # ifndef INITIAL_MARK_STACK_SIZE
83 #   define INITIAL_MARK_STACK_SIZE (1*HBLKSIZE)
84                 /* INITIAL_MARK_STACK_SIZE * sizeof(mse) should be a    */
85                 /* multiple of HBLKSIZE.                                */
86                 /* The incremental collector actually likes a larger    */
87                 /* size, since it want to push all marked dirty objs    */
88                 /* before marking anything new.  Currently we let it    */
89                 /* grow dynamically.                                    */
90 # endif
91
92 /*
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.
96  */
97
98 word GC_n_rescuing_pages;       /* Number of dirty pages we marked from */
99                                 /* excludes ptrfree pages, etc.         */
100
101 mse * GC_mark_stack;
102
103 mse * GC_mark_stack_limit;
104
105 word GC_mark_stack_size = 0;
106  
107 #ifdef PARALLEL_MARK
108   mse * VOLATILE GC_mark_stack_top;
109 #else
110   mse * GC_mark_stack_top;
111 #endif
112
113 static struct hblk * scan_ptr;
114
115 mark_state_t GC_mark_state = MS_NONE;
116
117 GC_bool GC_mark_stack_too_small = FALSE;
118
119 GC_bool GC_objects_are_marked = FALSE;  /* Are there collectable marked */
120                                         /* objects in the heap?         */
121
122 /* Is a collection in progress?  Note that this can return true in the  */
123 /* nonincremental case, if a collection has been abandoned and the      */
124 /* mark state is now MS_INVALID.                                        */
125 GC_bool GC_collection_in_progress()
126 {
127     return(GC_mark_state != MS_NONE);
128 }
129
130 /* clear all mark bits in the header */
131 void GC_clear_hdr_marks(hhdr)
132 register hdr * hhdr;
133 {
134 #   ifdef USE_MARK_BYTES
135       BZERO(hhdr -> hb_marks, MARK_BITS_SZ);
136 #   else
137       BZERO(hhdr -> hb_marks, MARK_BITS_SZ*sizeof(word));
138 #   endif
139 }
140
141 /* Set all mark bits in the header.  Used for uncollectable blocks. */
142 void GC_set_hdr_marks(hhdr)
143 register hdr * hhdr;
144 {
145     register int i;
146
147     for (i = 0; i < MARK_BITS_SZ; ++i) {
148 #     ifdef USE_MARK_BYTES
149         hhdr -> hb_marks[i] = 1;
150 #     else
151         hhdr -> hb_marks[i] = ONES;
152 #     endif
153     }
154 }
155
156 /*
157  * Clear all mark bits associated with block h.
158  */
159 /*ARGSUSED*/
160 # if defined(__STDC__) || defined(__cplusplus)
161     static void clear_marks_for_block(struct hblk *h, word dummy)
162 # else
163     static void clear_marks_for_block(h, dummy)
164     struct hblk *h;
165     word dummy;
166 # endif
167 {
168     register hdr * hhdr = HDR(h);
169     
170     if (IS_UNCOLLECTABLE(hhdr -> hb_obj_kind)) return;
171         /* Mark bit for these is cleared only once the object is        */
172         /* explicitly deallocated.  This either frees the block, or     */
173         /* the bit is cleared once the object is on the free list.      */
174     GC_clear_hdr_marks(hhdr);
175 }
176
177 /* Slow but general routines for setting/clearing/asking about mark bits */
178 void GC_set_mark_bit(p)
179 ptr_t p;
180 {
181     register struct hblk *h = HBLKPTR(p);
182     register hdr * hhdr = HDR(h);
183     register int word_no = (word *)p - (word *)h;
184     
185     set_mark_bit_from_hdr(hhdr, word_no);
186 }
187
188 void GC_clear_mark_bit(p)
189 ptr_t p;
190 {
191     register struct hblk *h = HBLKPTR(p);
192     register hdr * hhdr = HDR(h);
193     register int word_no = (word *)p - (word *)h;
194     
195     clear_mark_bit_from_hdr(hhdr, word_no);
196 }
197
198 GC_bool GC_is_marked(p)
199 ptr_t p;
200 {
201     register struct hblk *h = HBLKPTR(p);
202     register hdr * hhdr = HDR(h);
203     register int word_no = (word *)p - (word *)h;
204     
205     return(mark_bit_from_hdr(hhdr, word_no));
206 }
207
208
209 /*
210  * Clear mark bits in all allocated heap blocks.  This invalidates
211  * the marker invariant, and sets GC_mark_state to reflect this.
212  * (This implicitly starts marking to reestablish the invariant.)
213  */
214 void GC_clear_marks()
215 {
216     GC_apply_to_all_blocks(clear_marks_for_block, (word)0);
217     GC_objects_are_marked = FALSE;
218     GC_mark_state = MS_INVALID;
219     scan_ptr = 0;
220 #   ifdef GATHERSTATS
221         /* Counters reflect currently marked objects: reset here */
222         GC_composite_in_use = 0;
223         GC_atomic_in_use = 0;
224 #   endif
225
226 }
227
228 /* Initiate a garbage collection.  Initiates a full collection if the   */
229 /* mark state is invalid.                                               */
230 /*ARGSUSED*/
231 void GC_initiate_gc()
232 {
233     if (GC_dirty_maintained) GC_read_dirty();
234 #   ifdef STUBBORN_ALLOC
235         GC_read_changed();
236 #   endif
237 #   ifdef CHECKSUMS
238         {
239             extern void GC_check_dirty();
240             
241             if (GC_dirty_maintained) GC_check_dirty();
242         }
243 #   endif
244     GC_n_rescuing_pages = 0;
245     if (GC_mark_state == MS_NONE) {
246         GC_mark_state = MS_PUSH_RESCUERS;
247     } else if (GC_mark_state != MS_INVALID) {
248         ABORT("unexpected state");
249     } /* else this is really a full collection, and mark        */
250       /* bits are invalid.                                      */
251     scan_ptr = 0;
252 }
253
254
255 static void alloc_mark_stack();
256
257 /* Perform a small amount of marking.                   */
258 /* We try to touch roughly a page of memory.            */
259 /* Return TRUE if we just finished a mark phase.        */
260 /* Cold_gc_frame is an address inside a GC frame that   */
261 /* remains valid until all marking is complete.         */
262 /* A zero value indicates that it's OK to miss some     */
263 /* register values.                                     */
264 GC_bool GC_mark_some(cold_gc_frame)
265 ptr_t cold_gc_frame;
266 {
267 #ifdef MSWIN32
268   /* Windows 98 appears to asynchronously create and remove writable    */
269   /* memory mappings, for reasons we haven't yet understood.  Since     */
270   /* we look for writable regions to determine the root set, we may     */
271   /* try to mark from an address range that disappeared since we        */
272   /* started the collection.  Thus we have to recover from faults here. */
273   /* This code does not appear to be necessary for Windows 95/NT/2000.  */ 
274   /* Note that this code should never generate an incremental GC write  */
275   /* fault.                                                             */
276   __try {
277 #endif
278     switch(GC_mark_state) {
279         case MS_NONE:
280             return(FALSE);
281             
282         case MS_PUSH_RESCUERS:
283             if (GC_mark_stack_top
284                 >= GC_mark_stack_limit - INITIAL_MARK_STACK_SIZE/2) {
285                 /* Go ahead and mark, even though that might cause us to */
286                 /* see more marked dirty objects later on.  Avoid this   */
287                 /* in the future.                                        */
288                 GC_mark_stack_too_small = TRUE;
289                 MARK_FROM_MARK_STACK();
290                 return(FALSE);
291             } else {
292                 scan_ptr = GC_push_next_marked_dirty(scan_ptr);
293                 if (scan_ptr == 0) {
294 #                   ifdef CONDPRINT
295                       if (GC_print_stats) {
296                         GC_printf1("Marked from %lu dirty pages\n",
297                                    (unsigned long)GC_n_rescuing_pages);
298                       }
299 #                   endif
300                     GC_push_roots(FALSE, cold_gc_frame);
301                     GC_objects_are_marked = TRUE;
302                     if (GC_mark_state != MS_INVALID) {
303                         GC_mark_state = MS_ROOTS_PUSHED;
304                     }
305                 }
306             }
307             return(FALSE);
308         
309         case MS_PUSH_UNCOLLECTABLE:
310             if (GC_mark_stack_top
311                 >= GC_mark_stack + GC_mark_stack_size/4) {
312 #               ifdef PARALLEL_MARK
313                   /* Avoid this, since we don't parallelize the marker  */
314                   /* here.                                              */
315                   if (GC_parallel) GC_mark_stack_too_small = TRUE;
316 #               endif
317                 MARK_FROM_MARK_STACK();
318                 return(FALSE);
319             } else {
320                 scan_ptr = GC_push_next_marked_uncollectable(scan_ptr);
321                 if (scan_ptr == 0) {
322                     GC_push_roots(TRUE, cold_gc_frame);
323                     GC_objects_are_marked = TRUE;
324                     if (GC_mark_state != MS_INVALID) {
325                         GC_mark_state = MS_ROOTS_PUSHED;
326                     }
327                 }
328             }
329             return(FALSE);
330         
331         case MS_ROOTS_PUSHED:
332 #           ifdef PARALLEL_MARK
333               /* In the incremental GC case, this currently doesn't     */
334               /* quite do the right thing, since it runs to             */
335               /* completion.  On the other hand, starting a             */
336               /* parallel marker is expensive, so perhaps it is         */
337               /* the right thing?                                       */
338               /* Eventually, incremental marking should run             */
339               /* asynchronously in multiple threads, without grabbing   */
340               /* the allocation lock.                                   */
341                 if (GC_parallel) {
342                   GC_do_parallel_mark();
343                   GC_ASSERT(GC_mark_stack_top < GC_first_nonempty);
344                   GC_mark_stack_top = GC_mark_stack - 1;
345                   if (GC_mark_stack_too_small) {
346                     alloc_mark_stack(2*GC_mark_stack_size);
347                   }
348                   if (GC_mark_state == MS_ROOTS_PUSHED) {
349                     GC_mark_state = MS_NONE;
350                     return(TRUE);
351                   } else {
352                     return(FALSE);
353                   }
354                 }
355 #           endif
356             if (GC_mark_stack_top >= GC_mark_stack) {
357                 MARK_FROM_MARK_STACK();
358                 return(FALSE);
359             } else {
360                 GC_mark_state = MS_NONE;
361                 if (GC_mark_stack_too_small) {
362                     alloc_mark_stack(2*GC_mark_stack_size);
363                 }
364                 return(TRUE);
365             }
366             
367         case MS_INVALID:
368         case MS_PARTIALLY_INVALID:
369             if (!GC_objects_are_marked) {
370                 GC_mark_state = MS_PUSH_UNCOLLECTABLE;
371                 return(FALSE);
372             }
373             if (GC_mark_stack_top >= GC_mark_stack) {
374                 MARK_FROM_MARK_STACK();
375                 return(FALSE);
376             }
377             if (scan_ptr == 0 && GC_mark_state == MS_INVALID) {
378                 /* About to start a heap scan for marked objects. */
379                 /* Mark stack is empty.  OK to reallocate.        */
380                 if (GC_mark_stack_too_small) {
381                     alloc_mark_stack(2*GC_mark_stack_size);
382                 }
383                 GC_mark_state = MS_PARTIALLY_INVALID;
384             }
385             scan_ptr = GC_push_next_marked(scan_ptr);
386             if (scan_ptr == 0 && GC_mark_state == MS_PARTIALLY_INVALID) {
387                 GC_push_roots(TRUE, cold_gc_frame);
388                 GC_objects_are_marked = TRUE;
389                 if (GC_mark_state != MS_INVALID) {
390                     GC_mark_state = MS_ROOTS_PUSHED;
391                 }
392             }
393             return(FALSE);
394         default:
395             ABORT("GC_mark_some: bad state");
396             return(FALSE);
397     }
398 #ifdef MSWIN32
399   } __except (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION ?
400             EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH) {
401 #   ifdef CONDPRINT
402       if (GC_print_stats) {
403         GC_printf0("Caught ACCESS_VIOLATION in marker. "
404                    "Memory mapping disappeared.\n");
405       }
406 #   endif /* CONDPRINT */
407     /* We have bad roots on the stack.  Discard mark stack.     */
408     /* Rescan from marked objects.  Redetermine roots.          */
409     GC_invalidate_mark_state(); 
410     scan_ptr = 0;
411     return FALSE;
412   }
413 #endif /* MSWIN32 */
414 }
415
416
417 GC_bool GC_mark_stack_empty()
418 {
419     return(GC_mark_stack_top < GC_mark_stack);
420 }       
421
422 #ifdef PROF_MARKER
423     word GC_prof_array[10];
424 #   define PROF(n) GC_prof_array[n]++
425 #else
426 #   define PROF(n)
427 #endif
428
429 /* Given a pointer to someplace other than a small object page or the   */
430 /* first page of a large object, either:                                */
431 /*      - return a pointer to somewhere in the first page of the large  */
432 /*        object, if current points to a large object.                  */
433 /*        In this case *hhdr is replaced with a pointer to the header   */
434 /*        for the large object.                                         */
435 /*      - just return current if it does not point to a large object.   */
436 /*ARGSUSED*/
437 # ifdef PRINT_BLACK_LIST
438   ptr_t GC_find_start(current, hhdr, new_hdr_p, source)
439   ptr_t source;
440 # else
441   ptr_t GC_find_start(current, hhdr, new_hdr_p)
442 # define source 0
443 # endif
444 register ptr_t current;
445 register hdr *hhdr, **new_hdr_p;
446 {
447     if (GC_all_interior_pointers) {
448         if (hhdr != 0) {
449             register ptr_t orig = current;
450             
451             current = (ptr_t)HBLKPTR(current);
452             do {
453               current = current - HBLKSIZE*(word)hhdr;
454               hhdr = HDR(current);
455             } while(IS_FORWARDING_ADDR_OR_NIL(hhdr));
456             /* current points to the start of the large object */
457             if (hhdr -> hb_flags & IGNORE_OFF_PAGE) return(0);
458             if ((word *)orig - (word *)current
459                  >= (ptrdiff_t)(hhdr->hb_sz)) {
460                 /* Pointer past the end of the block */
461                 return(orig);
462             }
463             *new_hdr_p = hhdr;
464             return(current);
465         } else {
466             return(current);
467         }
468     } else {
469         return(current);
470     }
471 #   undef source
472 }
473
474 void GC_invalidate_mark_state()
475 {
476     GC_mark_state = MS_INVALID;
477     GC_mark_stack_top = GC_mark_stack-1;
478 }
479
480 mse * GC_signal_mark_stack_overflow(msp)
481 mse * msp;
482 {
483     GC_mark_state = MS_INVALID;
484     GC_mark_stack_too_small = TRUE;
485 #   ifdef CONDPRINT
486       if (GC_print_stats) {
487         GC_printf1("Mark stack overflow; current size = %lu entries\n",
488                     GC_mark_stack_size);
489       }
490 #   endif
491     return(msp - GC_MARK_STACK_DISCARDS);
492 }
493
494 /*
495  * Mark objects pointed to by the regions described by
496  * mark stack entries between GC_mark_stack and GC_mark_stack_top,
497  * inclusive.  Assumes the upper limit of a mark stack entry
498  * is never 0.  A mark stack entry never has size 0.
499  * We try to traverse on the order of a hblk of memory before we return.
500  * Caller is responsible for calling this until the mark stack is empty.
501  * Note that this is the most performance critical routine in the
502  * collector.  Hence it contains all sorts of ugly hacks to speed
503  * things up.  In particular, we avoid procedure calls on the common
504  * path, we take advantage of peculiarities of the mark descriptor
505  * encoding, we optionally maintain a cache for the block address to
506  * header mapping, we prefetch when an object is "grayed", etc. 
507  */
508 mse * GC_mark_from(mark_stack_top, mark_stack, mark_stack_limit)
509 mse * mark_stack_top;
510 mse * mark_stack;
511 mse * mark_stack_limit;
512 {
513   int credit = HBLKSIZE;        /* Remaining credit for marking work    */
514   register word * current_p;    /* Pointer to current candidate ptr.    */
515   register word current;        /* Candidate pointer.                   */
516   register word * limit;        /* (Incl) limit of current candidate    */
517                                 /* range                                */
518   register word descr;
519   register ptr_t greatest_ha = GC_greatest_plausible_heap_addr;
520   register ptr_t least_ha = GC_least_plausible_heap_addr;
521   DECLARE_HDR_CACHE;
522
523 # define SPLIT_RANGE_WORDS 128  /* Must be power of 2.          */
524
525   GC_objects_are_marked = TRUE;
526   INIT_HDR_CACHE;
527 # ifdef OS2 /* Use untweaked version to circumvent compiler problem */
528   while (mark_stack_top >= mark_stack && credit >= 0) {
529 # else
530   while ((((ptr_t)mark_stack_top - (ptr_t)mark_stack) | credit)
531         >= 0) {
532 # endif
533     current_p = mark_stack_top -> mse_start;
534     descr = mark_stack_top -> mse_descr;
535   retry:
536     /* current_p and descr describe the current object.         */
537     /* *mark_stack_top is vacant.                               */
538     /* The following is 0 only for small objects described by a simple  */
539     /* length descriptor.  For many applications this is the common     */
540     /* case, so we try to detect it quickly.                            */
541     if (descr & ((~(WORDS_TO_BYTES(SPLIT_RANGE_WORDS) - 1)) | GC_DS_TAGS)) {
542       word tag = descr & GC_DS_TAGS;
543       
544       switch(tag) {
545         case GC_DS_LENGTH:
546           /* Large length.                                              */
547           /* Process part of the range to avoid pushing too much on the */
548           /* stack.                                                     */
549 #         ifdef PARALLEL_MARK
550 #           define SHARE_BYTES 2048
551             if (descr > SHARE_BYTES && GC_parallel
552                 && mark_stack_top < mark_stack_limit - 1) {
553               int new_size = (descr/2) & ~(sizeof(word)-1);
554               GC_ASSERT(descr < GC_greatest_plausible_heap_addr
555                                 - GC_least_plausible_heap_addr);
556               mark_stack_top -> mse_start = current_p;
557               mark_stack_top -> mse_descr = new_size + sizeof(word);
558                                         /* makes sure we handle         */
559                                         /* misaligned pointers.         */
560               mark_stack_top++;
561               current_p = (word *) ((char *)current_p + new_size);
562               descr -= new_size;
563               goto retry;
564             }
565 #         endif /* PARALLEL_MARK */
566           mark_stack_top -> mse_start =
567                 limit = current_p + SPLIT_RANGE_WORDS-1;
568           mark_stack_top -> mse_descr =
569                         descr - WORDS_TO_BYTES(SPLIT_RANGE_WORDS-1);
570           /* Make sure that pointers overlapping the two ranges are     */
571           /* considered.                                                */
572           limit = (word *)((char *)limit + sizeof(word) - ALIGNMENT);
573           break;
574         case GC_DS_BITMAP:
575           mark_stack_top--;
576           descr &= ~GC_DS_TAGS;
577           credit -= WORDS_TO_BYTES(WORDSZ/2); /* guess */
578           while (descr != 0) {
579             if ((signed_word)descr < 0) {
580               current = *current_p;
581               if ((ptr_t)current >= least_ha && (ptr_t)current < greatest_ha) {
582                 PREFETCH(current);
583                 HC_PUSH_CONTENTS((ptr_t)current, mark_stack_top,
584                               mark_stack_limit, current_p, exit1);
585               }
586             }
587             descr <<= 1;
588             ++ current_p;
589           }
590           continue;
591         case GC_DS_PROC:
592           mark_stack_top--;
593           credit -= GC_PROC_BYTES;
594           mark_stack_top =
595               (*PROC(descr))
596                     (current_p, mark_stack_top,
597                     mark_stack_limit, ENV(descr));
598           continue;
599         case GC_DS_PER_OBJECT:
600           if ((signed_word)descr >= 0) {
601             /* Descriptor is in the object.     */
602             descr = *(word *)((ptr_t)current_p + descr - GC_DS_PER_OBJECT);
603           } else {
604             /* Descriptor is in type descriptor pointed to by first     */
605             /* word in object.                                          */
606             ptr_t type_descr = *(ptr_t *)current_p;
607             /* type_descr is either a valid pointer to the descriptor   */
608             /* structure, or this object was on a free list.  If it     */
609             /* it was anything but the last object on the free list,    */
610             /* we will misinterpret the next object on the free list as */
611             /* the type descriptor, and get a 0 GC descriptor, which    */
612             /* is ideal.  Unfortunately, we need to check for the last  */
613             /* object case explicitly.                                  */
614             if (0 == type_descr) {
615                 /* Rarely executed.     */
616                 mark_stack_top--;
617                 continue;
618             }
619             descr = *(word *)(type_descr
620                               - (descr - (GC_DS_PER_OBJECT
621                                           - GC_INDIR_PER_OBJ_BIAS)));
622           }
623           if (0 == descr) {
624               /* Can happen either because we generated a 0 descriptor  */
625               /* or we saw a pointer to a free object.                  */
626               mark_stack_top--;
627               continue;
628           }
629           goto retry;
630       }
631     } else /* Small object with length descriptor */ {
632       mark_stack_top--;
633       limit = (word *)(((ptr_t)current_p) + (word)descr);
634     }
635     /* The simple case in which we're scanning a range. */
636     GC_ASSERT(!((word)current_p & (ALIGNMENT-1)));
637     credit -= (ptr_t)limit - (ptr_t)current_p;
638     limit -= 1;
639     {
640 #     define PREF_DIST 4
641
642 #     ifndef SMALL_CONFIG
643         word deferred;
644
645         /* Try to prefetch the next pointer to be examined asap.        */
646         /* Empirically, this also seems to help slightly without        */
647         /* prefetches, at least on linux/X86.  Presumably this loop     */
648         /* ends up with less register pressure, and gcc thus ends up    */
649         /* generating slightly better code.  Overall gcc code quality   */
650         /* for this loop is still not great.                            */
651         for(;;) {
652           PREFETCH((ptr_t)limit - PREF_DIST*CACHE_LINE_SIZE);
653           GC_ASSERT(limit >= current_p);
654           deferred = *limit;
655           limit = (word *)((char *)limit - ALIGNMENT);
656           if ((ptr_t)deferred >= least_ha && (ptr_t)deferred <  greatest_ha) {
657             PREFETCH(deferred);
658             break;
659           }
660           if (current_p > limit) goto next_object;
661           /* Unroll once, so we don't do too many of the prefetches     */
662           /* based on limit.                                            */
663           deferred = *limit;
664           limit = (word *)((char *)limit - ALIGNMENT);
665           if ((ptr_t)deferred >= least_ha && (ptr_t)deferred <  greatest_ha) {
666             PREFETCH(deferred);
667             break;
668           }
669           if (current_p > limit) goto next_object;
670         }
671 #     endif
672
673       while (current_p <= limit) {
674         /* Empirically, unrolling this loop doesn't help a lot. */
675         /* Since HC_PUSH_CONTENTS expands to a lot of code,     */
676         /* we don't.                                            */
677         current = *current_p;
678         PREFETCH((ptr_t)current_p + PREF_DIST*CACHE_LINE_SIZE);
679         if ((ptr_t)current >= least_ha && (ptr_t)current <  greatest_ha) {
680           /* Prefetch the contents of the object we just pushed.  It's  */
681           /* likely we will need them soon.                             */
682           PREFETCH(current);
683           HC_PUSH_CONTENTS((ptr_t)current, mark_stack_top,
684                            mark_stack_limit, current_p, exit2);
685         }
686         current_p = (word *)((char *)current_p + ALIGNMENT);
687       }
688
689 #     ifndef SMALL_CONFIG
690         /* We still need to mark the entry we previously prefetched.    */
691         /* We alrady know that it passes the preliminary pointer        */
692         /* validity test.                                               */
693         HC_PUSH_CONTENTS((ptr_t)deferred, mark_stack_top,
694                          mark_stack_limit, current_p, exit4);
695         next_object:;
696 #     endif
697     }
698   }
699   return mark_stack_top;
700 }
701
702 #ifdef PARALLEL_MARK
703
704 /* We assume we have an ANSI C Compiler.        */
705 GC_bool GC_help_wanted = FALSE;
706 unsigned GC_helper_count = 0;
707 unsigned GC_active_count = 0;
708 mse * VOLATILE GC_first_nonempty;
709 word GC_mark_no = 0;
710
711 #define LOCAL_MARK_STACK_SIZE HBLKSIZE
712         /* Under normal circumstances, this is big enough to guarantee  */
713         /* We don't overflow half of it in a single call to             */
714         /* GC_mark_from.                                                */
715
716
717 /* Steal mark stack entries starting at mse low into mark stack local   */
718 /* until we either steal mse high, or we have max entries.              */
719 /* Return a pointer to the top of the local mark stack.                 */
720 /* *next is replaced by a pointer to the next unscanned mark stack      */
721 /* entry.                                                               */
722 mse * GC_steal_mark_stack(mse * low, mse * high, mse * local,
723                           unsigned max, mse **next)
724 {
725     mse *p;
726     mse *top = local - 1;
727     unsigned i = 0;
728
729     GC_ASSERT(high >= low-1 && high - low + 1 <= GC_mark_stack_size);
730     for (p = low; p <= high && i <= max; ++p) {
731         word descr = *(volatile word *) &(p -> mse_descr);
732         if (descr != 0) {
733             *(volatile word *) &(p -> mse_descr) = 0;
734             ++top;
735             top -> mse_descr = descr;
736             top -> mse_start = p -> mse_start;
737             GC_ASSERT(  top -> mse_descr & GC_DS_TAGS != GC_DS_LENGTH || 
738                         top -> mse_descr < GC_greatest_plausible_heap_addr
739                                            - GC_least_plausible_heap_addr);
740             /* There is no synchronization here.  We assume that at     */
741             /* least one thread will see the original descriptor.       */
742             /* Otherwise we need a barrier.                             */
743             /* More than one thread may get this entry, but that's only */
744             /* a minor performance problem.                             */
745             /* If this is a big object, count it as                     */
746             /* size/256 + 1 objects.                                    */
747             ++i;
748             if ((descr & GC_DS_TAGS) == GC_DS_LENGTH) i += (descr >> 8);
749         }
750     }
751     *next = p;
752     return top;
753 }
754
755 /* Copy back a local mark stack.        */
756 /* low and high are inclusive bounds.   */
757 void GC_return_mark_stack(mse * low, mse * high)
758 {
759     mse * my_top;
760     mse * my_start;
761     size_t stack_size;
762
763     if (high < low) return;
764     stack_size = high - low + 1;
765     GC_acquire_mark_lock();
766     my_top = GC_mark_stack_top;
767     my_start = my_top + 1;
768     if (my_start - GC_mark_stack + stack_size > GC_mark_stack_size) {
769 #     ifdef CONDPRINT
770         if (GC_print_stats) {
771           GC_printf0("No room to copy back mark stack.");
772         }
773 #     endif
774       GC_mark_state = MS_INVALID;
775       GC_mark_stack_too_small = TRUE;
776       /* We drop the local mark stack.  We'll fix things later. */
777     } else {
778       BCOPY(low, my_start, stack_size * sizeof(mse));
779       GC_ASSERT(GC_mark_stack_top = my_top);
780 #     if !defined(IA64) && !defined(HP_PA)
781         GC_memory_write_barrier();
782 #     endif
783         /* On IA64, the volatile write acts as a release barrier. */
784       GC_mark_stack_top = my_top + stack_size;
785     }
786     GC_release_mark_lock();
787     GC_notify_all_marker();
788 }
789
790 /* Mark from the local mark stack.              */
791 /* On return, the local mark stack is empty.    */
792 /* But this may be achieved by copying the      */
793 /* local mark stack back into the global one.   */
794 void GC_do_local_mark(mse *local_mark_stack, mse *local_top)
795 {
796     unsigned n;
797 #   define N_LOCAL_ITERS 1
798
799 #   ifdef GC_ASSERTIONS
800       /* Make sure we don't hold mark lock. */
801         GC_acquire_mark_lock();
802         GC_release_mark_lock();
803 #   endif
804     for (;;) {
805         for (n = 0; n < N_LOCAL_ITERS; ++n) {
806             local_top = GC_mark_from(local_top, local_mark_stack,
807                                      local_mark_stack + LOCAL_MARK_STACK_SIZE);
808             if (local_top < local_mark_stack) return;
809             if (local_top - local_mark_stack >= LOCAL_MARK_STACK_SIZE/2) {
810                 GC_return_mark_stack(local_mark_stack, local_top);
811                 return;
812             }
813         }
814         if (GC_mark_stack_top < GC_first_nonempty &&
815             GC_active_count < GC_helper_count
816             && local_top > local_mark_stack + 1) {
817             /* Try to share the load, since the main stack is empty,    */
818             /* and helper threads are waiting for a refill.             */
819             /* The entries near the bottom of the stack are likely      */
820             /* to require more work.  Thus we return those, eventhough  */
821             /* it's harder.                                             */
822             mse * p;
823             mse * new_bottom = local_mark_stack
824                                 + (local_top - local_mark_stack)/2;
825             GC_ASSERT(new_bottom > local_mark_stack
826                       && new_bottom < local_top);
827             GC_return_mark_stack(local_mark_stack, new_bottom - 1);
828             memmove(local_mark_stack, new_bottom,
829                     (local_top - new_bottom + 1) * sizeof(mse));
830             local_top -= (new_bottom - local_mark_stack);
831         }
832     }
833 }
834
835 #define ENTRIES_TO_GET 5
836
837 long GC_markers = 2;            /* Normally changed by thread-library-  */
838                                 /* -specific code.                      */
839
840 /* Mark using the local mark stack until the global mark stack is empty */
841 /* and ther are no active workers.  Update GC_first_nonempty to reflect */
842 /* progress.                                                            */
843 /* Caller does not hold mark lock.                                      */
844 /* Caller has already incremented GC_helper_count.  We decrement it,    */
845 /* and maintain GC_active_count.                                        */
846 void GC_mark_local(mse *local_mark_stack, int id)
847 {
848     mse * my_first_nonempty;
849
850     GC_acquire_mark_lock();
851     GC_active_count++;
852     my_first_nonempty = GC_first_nonempty;
853     GC_ASSERT(GC_first_nonempty >= GC_mark_stack && 
854               GC_first_nonempty <= GC_mark_stack_top + 1);
855 #   ifdef PRINTSTATS
856         GC_printf1("Starting mark helper %lu\n", (unsigned long)id);
857 #   endif
858     GC_release_mark_lock();
859     for (;;) {
860         size_t n_on_stack;
861         size_t n_to_get;
862         mse *next;
863         mse * my_top;
864         mse * local_top;
865         mse * global_first_nonempty = GC_first_nonempty;
866
867         GC_ASSERT(my_first_nonempty >= GC_mark_stack && 
868                   my_first_nonempty <= GC_mark_stack_top + 1);
869         GC_ASSERT(global_first_nonempty >= GC_mark_stack && 
870                   global_first_nonempty <= GC_mark_stack_top + 1);
871         if (my_first_nonempty < global_first_nonempty) {
872             my_first_nonempty = global_first_nonempty;
873         } else if (global_first_nonempty < my_first_nonempty) {
874             GC_compare_and_exchange((word *)(&GC_first_nonempty), 
875                                    (word) global_first_nonempty,
876                                    (word) my_first_nonempty);
877             /* If this fails, we just go ahead, without updating        */
878             /* GC_first_nonempty.                                       */
879         }
880         /* Perhaps we should also update GC_first_nonempty, if it */
881         /* is less.  But that would require using atomic updates. */
882         my_top = GC_mark_stack_top;
883         n_on_stack = my_top - my_first_nonempty + 1;
884         if (0 == n_on_stack) {
885             GC_acquire_mark_lock();
886             my_top = GC_mark_stack_top;
887             n_on_stack = my_top - my_first_nonempty + 1;
888             if (0 == n_on_stack) {
889                 GC_active_count--;
890                 GC_ASSERT(GC_active_count <= GC_helper_count);
891                 /* Other markers may redeposit objects  */
892                 /* on the stack.                                */
893                 if (0 == GC_active_count) GC_notify_all_marker();
894                 while (GC_active_count > 0
895                        && GC_first_nonempty > GC_mark_stack_top) {
896                     /* We will be notified if either GC_active_count    */
897                     /* reaches zero, or if more objects are pushed on   */
898                     /* the global mark stack.                           */
899                     GC_wait_marker();
900                 }
901                 if (GC_active_count == 0 &&
902                     GC_first_nonempty > GC_mark_stack_top) { 
903                     GC_bool need_to_notify = FALSE;
904                     /* The above conditions can't be falsified while we */
905                     /* hold the mark lock, since neither                */
906                     /* GC_active_count nor GC_mark_stack_top can        */
907                     /* change.  GC_first_nonempty can only be           */
908                     /* incremented asynchronously.  Thus we know that   */
909                     /* both conditions actually held simultaneously.    */
910                     GC_helper_count--;
911                     if (0 == GC_helper_count) need_to_notify = TRUE;
912 #                   ifdef PRINTSTATS
913                       GC_printf1(
914                         "Finished mark helper %lu\n", (unsigned long)id);
915 #                   endif
916                     GC_release_mark_lock();
917                     if (need_to_notify) GC_notify_all_marker();
918                     return;
919                 }
920                 /* else there's something on the stack again, or        */
921                 /* another help may push something.                     */
922                 GC_active_count++;
923                 GC_ASSERT(GC_active_count > 0);
924                 GC_release_mark_lock();
925                 continue;
926             } else {
927                 GC_release_mark_lock();
928             }
929         }
930         n_to_get = ENTRIES_TO_GET;
931         if (n_on_stack < 2 * ENTRIES_TO_GET) n_to_get = 1;
932         local_top = GC_steal_mark_stack(my_first_nonempty, my_top,
933                                         local_mark_stack, n_to_get,
934                                         &my_first_nonempty);
935         GC_ASSERT(my_first_nonempty >= GC_mark_stack && 
936                   my_first_nonempty <= GC_mark_stack_top + 1);
937         GC_do_local_mark(local_mark_stack, local_top);
938     }
939 }
940
941 /* Perform Parallel mark.                       */
942 /* We hold the GC lock, not the mark lock.      */
943 /* Currently runs until the mark stack is       */
944 /* empty.                                       */
945 void GC_do_parallel_mark()
946 {
947     mse local_mark_stack[LOCAL_MARK_STACK_SIZE];
948     mse * local_top;
949     mse * my_top;
950
951     GC_acquire_mark_lock();
952     GC_ASSERT(I_HOLD_LOCK());
953     GC_ASSERT(!GC_help_wanted);
954     GC_ASSERT(GC_active_count == 0);
955 #   ifdef PRINTSTATS
956         GC_printf1("Starting marking for mark phase number %lu\n",
957                    (unsigned long)GC_mark_no);
958 #   endif
959     GC_first_nonempty = GC_mark_stack;
960     GC_active_count = 0;
961     GC_helper_count = 1;
962     GC_help_wanted = TRUE;
963     GC_release_mark_lock();
964     GC_notify_all_marker();
965         /* Wake up potential helpers.   */
966     GC_mark_local(local_mark_stack, 0);
967     GC_acquire_mark_lock();
968     GC_help_wanted = FALSE;
969     /* Done; clean up.  */
970     while (GC_helper_count > 0) GC_wait_marker();
971     /* GC_helper_count cannot be incremented while GC_help_wanted == FALSE */
972 #   ifdef PRINTSTATS
973         GC_printf1(
974             "Finished marking for mark phase number %lu\n",
975             (unsigned long)GC_mark_no);
976 #   endif
977     GC_mark_no++;
978     GC_release_mark_lock();
979     GC_notify_all_marker();
980 }
981
982
983 /* Try to help out the marker, if it's running.         */
984 /* We do not hold the GC lock, but the requestor does.  */
985 void GC_help_marker(word my_mark_no)
986 {
987     mse local_mark_stack[LOCAL_MARK_STACK_SIZE];
988     unsigned my_id;
989     mse * my_first_nonempty;
990
991     if (!GC_parallel) return;
992     GC_acquire_mark_lock();
993     while (GC_mark_no < my_mark_no
994            || !GC_help_wanted && GC_mark_no == my_mark_no) {
995       GC_wait_marker();
996     }
997     my_id = GC_helper_count;
998     if (GC_mark_no != my_mark_no || my_id >= GC_markers) {
999       /* Second test is useful only if original threads can also        */
1000       /* act as helpers.  Under Linux they can't.                       */
1001       GC_release_mark_lock();
1002       return;
1003     }
1004     GC_helper_count = my_id + 1;
1005     GC_release_mark_lock();
1006     GC_mark_local(local_mark_stack, my_id);
1007     /* GC_mark_local decrements GC_helper_count. */
1008 }
1009
1010 #endif /* PARALLEL_MARK */
1011
1012 /* Allocate or reallocate space for mark stack of size s words  */
1013 /* May silently fail.                                           */
1014 static void alloc_mark_stack(n)
1015 word n;
1016 {
1017     mse * new_stack = (mse *)GC_scratch_alloc(n * sizeof(struct GC_ms_entry));
1018     
1019     GC_mark_stack_too_small = FALSE;
1020     if (GC_mark_stack_size != 0) {
1021         if (new_stack != 0) {
1022           word displ = (word)GC_mark_stack & (GC_page_size - 1);
1023           signed_word size = GC_mark_stack_size * sizeof(struct GC_ms_entry);
1024           
1025           /* Recycle old space */
1026               if (0 != displ) displ = GC_page_size - displ;
1027               size = (size - displ) & ~(GC_page_size - 1);
1028               if (size > 0) {
1029                 GC_add_to_heap((struct hblk *)
1030                                 ((word)GC_mark_stack + displ), (word)size);
1031               }
1032           GC_mark_stack = new_stack;
1033           GC_mark_stack_size = n;
1034           GC_mark_stack_limit = new_stack + n;
1035 #         ifdef CONDPRINT
1036             if (GC_print_stats) {
1037               GC_printf1("Grew mark stack to %lu frames\n",
1038                          (unsigned long) GC_mark_stack_size);
1039             }
1040 #         endif
1041         } else {
1042 #         ifdef CONDPRINT
1043             if (GC_print_stats) {
1044               GC_printf1("Failed to grow mark stack to %lu frames\n",
1045                          (unsigned long) n);
1046             }
1047 #         endif
1048         }
1049     } else {
1050         if (new_stack == 0) {
1051             GC_err_printf0("No space for mark stack\n");
1052             EXIT();
1053         }
1054         GC_mark_stack = new_stack;
1055         GC_mark_stack_size = n;
1056         GC_mark_stack_limit = new_stack + n;
1057     }
1058     GC_mark_stack_top = GC_mark_stack-1;
1059 }
1060
1061 void GC_mark_init()
1062 {
1063     alloc_mark_stack(INITIAL_MARK_STACK_SIZE);
1064 }
1065
1066 /*
1067  * Push all locations between b and t onto the mark stack.
1068  * b is the first location to be checked. t is one past the last
1069  * location to be checked.
1070  * Should only be used if there is no possibility of mark stack
1071  * overflow.
1072  */
1073 void GC_push_all(bottom, top)
1074 ptr_t bottom;
1075 ptr_t top;
1076 {
1077     register word length;
1078     
1079     bottom = (ptr_t)(((word) bottom + ALIGNMENT-1) & ~(ALIGNMENT-1));
1080     top = (ptr_t)(((word) top) & ~(ALIGNMENT-1));
1081     if (top == 0 || bottom == top) return;
1082     GC_mark_stack_top++;
1083     if (GC_mark_stack_top >= GC_mark_stack_limit) {
1084         ABORT("unexpected mark stack overflow");
1085     }
1086     length = top - bottom;
1087 #   if GC_DS_TAGS > ALIGNMENT - 1
1088         length += GC_DS_TAGS;
1089         length &= ~GC_DS_TAGS;
1090 #   endif
1091     GC_mark_stack_top -> mse_start = (word *)bottom;
1092     GC_mark_stack_top -> mse_descr = length;
1093 }
1094
1095 /*
1096  * Analogous to the above, but push only those pages h with dirty_fn(h) != 0.
1097  * We use push_fn to actually push the block.
1098  * Used both to selectively push dirty pages, or to push a block
1099  * in piecemeal fashion, to allow for more marking concurrency.
1100  * Will not overflow mark stack if push_fn pushes a small fixed number
1101  * of entries.  (This is invoked only if push_fn pushes a single entry,
1102  * or if it marks each object before pushing it, thus ensuring progress
1103  * in the event of a stack overflow.)
1104  */
1105 void GC_push_selected(bottom, top, dirty_fn, push_fn)
1106 ptr_t bottom;
1107 ptr_t top;
1108 int (*dirty_fn) GC_PROTO((struct hblk * h));
1109 void (*push_fn) GC_PROTO((ptr_t bottom, ptr_t top));
1110 {
1111     register struct hblk * h;
1112
1113     bottom = (ptr_t)(((long) bottom + ALIGNMENT-1) & ~(ALIGNMENT-1));
1114     top = (ptr_t)(((long) top) & ~(ALIGNMENT-1));
1115
1116     if (top == 0 || bottom == top) return;
1117     h = HBLKPTR(bottom + HBLKSIZE);
1118     if (top <= (ptr_t) h) {
1119         if ((*dirty_fn)(h-1)) {
1120             (*push_fn)(bottom, top);
1121         }
1122         return;
1123     }
1124     if ((*dirty_fn)(h-1)) {
1125         (*push_fn)(bottom, (ptr_t)h);
1126     }
1127     while ((ptr_t)(h+1) <= top) {
1128         if ((*dirty_fn)(h)) {
1129             if ((word)(GC_mark_stack_top - GC_mark_stack)
1130                 > 3 * GC_mark_stack_size / 4) {
1131                 /* Danger of mark stack overflow */
1132                 (*push_fn)((ptr_t)h, top);
1133                 return;
1134             } else {
1135                 (*push_fn)((ptr_t)h, (ptr_t)(h+1));
1136             }
1137         }
1138         h++;
1139     }
1140     if ((ptr_t)h != top) {
1141         if ((*dirty_fn)(h)) {
1142             (*push_fn)((ptr_t)h, top);
1143         }
1144     }
1145     if (GC_mark_stack_top >= GC_mark_stack_limit) {
1146         ABORT("unexpected mark stack overflow");
1147     }
1148 }
1149
1150 # ifndef SMALL_CONFIG
1151
1152 #ifdef PARALLEL_MARK
1153     /* Break up root sections into page size chunks to better spread    */
1154     /* out work.                                                        */
1155     GC_bool GC_true_func(struct hblk *h) { return TRUE; }
1156 #   define GC_PUSH_ALL(b,t) GC_push_selected(b,t,GC_true_func,GC_push_all);
1157 #else
1158 #   define GC_PUSH_ALL(b,t) GC_push_all(b,t);
1159 #endif
1160
1161
1162 void GC_push_conditional(bottom, top, all)
1163 ptr_t bottom;
1164 ptr_t top;
1165 int all;
1166 {
1167     if (all) {
1168       if (GC_dirty_maintained) {
1169 #       ifdef PROC_VDB
1170             /* Pages that were never dirtied cannot contain pointers    */
1171             GC_push_selected(bottom, top, GC_page_was_ever_dirty, GC_push_all);
1172 #       else
1173             GC_push_all(bottom, top);
1174 #       endif
1175       } else {
1176         GC_push_all(bottom, top);
1177       }
1178     } else {
1179         GC_push_selected(bottom, top, GC_page_was_dirty, GC_push_all);
1180     }
1181 }
1182 #endif
1183
1184 # if defined(MSWIN32) || defined(MSWINCE)
1185   void __cdecl GC_push_one(p)
1186 # else
1187   void GC_push_one(p)
1188 # endif
1189 word p;
1190 {
1191     GC_PUSH_ONE_STACK(p, MARKED_FROM_REGISTER);
1192 }
1193
1194 struct GC_ms_entry *GC_mark_and_push(obj, mark_stack_ptr, mark_stack_limit, src)
1195 GC_PTR obj;
1196 struct GC_ms_entry * mark_stack_ptr;
1197 struct GC_ms_entry * mark_stack_limit;
1198 GC_PTR *src;
1199 {
1200    PREFETCH(obj);
1201    PUSH_CONTENTS(obj, mark_stack_ptr /* modified */, mark_stack_limit, src,
1202                  was_marked /* internally generated exit label */);
1203    return mark_stack_ptr;
1204 }
1205
1206 # ifdef __STDC__
1207 #   define BASE(p) (word)GC_base((void *)(p))
1208 # else
1209 #   define BASE(p) (word)GC_base((char *)(p))
1210 # endif
1211
1212 /* Mark and push (i.e. gray) a single object p onto the main    */
1213 /* mark stack.  Consider p to be valid if it is an interior     */
1214 /* pointer.                                                     */
1215 /* The object p has passed a preliminary pointer validity       */
1216 /* test, but we do not definitely know whether it is valid.     */
1217 /* Mark bits are NOT atomically updated.  Thus this must be the */
1218 /* only thread setting them.                                    */
1219 # if defined(PRINT_BLACK_LIST) || defined(KEEP_BACK_PTRS)
1220     void GC_mark_and_push_stack(p, source)
1221     ptr_t source;
1222 # else
1223     void GC_mark_and_push_stack(p)
1224 #   define source 0
1225 # endif
1226 register word p;
1227 {
1228     register word r;
1229     register hdr * hhdr; 
1230     register int displ;
1231   
1232     GET_HDR(p, hhdr);
1233     if (IS_FORWARDING_ADDR_OR_NIL(hhdr)) {
1234         if (hhdr != 0) {
1235           r = BASE(p);
1236           hhdr = HDR(r);
1237           displ = BYTES_TO_WORDS(HBLKDISPL(r));
1238         }
1239     } else {
1240         register map_entry_type map_entry;
1241         
1242         displ = HBLKDISPL(p);
1243         map_entry = MAP_ENTRY((hhdr -> hb_map), displ);
1244         if (map_entry >= MAX_OFFSET) {
1245           if (map_entry == OFFSET_TOO_BIG || !GC_all_interior_pointers) {
1246               r = BASE(p);
1247               displ = BYTES_TO_WORDS(HBLKDISPL(r));
1248               if (r == 0) hhdr = 0;
1249           } else {
1250               /* Offset invalid, but map reflects interior pointers     */
1251               hhdr = 0;
1252           }
1253         } else {
1254           displ = BYTES_TO_WORDS(displ);
1255           displ -= map_entry;
1256           r = (word)((word *)(HBLKPTR(p)) + displ);
1257         }
1258     }
1259     /* If hhdr != 0 then r == GC_base(p), only we did it faster. */
1260     /* displ is the word index within the block.                 */
1261     if (hhdr == 0) {
1262 #       ifdef PRINT_BLACK_LIST
1263           GC_add_to_black_list_stack(p, source);
1264 #       else
1265           GC_add_to_black_list_stack(p);
1266 #       endif
1267 #       undef source  /* In case we had to define it. */
1268     } else {
1269         if (!mark_bit_from_hdr(hhdr, displ)) {
1270             set_mark_bit_from_hdr(hhdr, displ);
1271             GC_STORE_BACK_PTR(source, (ptr_t)r);
1272             PUSH_OBJ((word *)r, hhdr, GC_mark_stack_top,
1273                      GC_mark_stack_limit);
1274         }
1275     }
1276 }
1277
1278 # ifdef TRACE_BUF
1279
1280 # define TRACE_ENTRIES 1000
1281
1282 struct trace_entry {
1283     char * kind;
1284     word gc_no;
1285     word words_allocd;
1286     word arg1;
1287     word arg2;
1288 } GC_trace_buf[TRACE_ENTRIES];
1289
1290 int GC_trace_buf_ptr = 0;
1291
1292 void GC_add_trace_entry(char *kind, word arg1, word arg2)
1293 {
1294     GC_trace_buf[GC_trace_buf_ptr].kind = kind;
1295     GC_trace_buf[GC_trace_buf_ptr].gc_no = GC_gc_no;
1296     GC_trace_buf[GC_trace_buf_ptr].words_allocd = GC_words_allocd;
1297     GC_trace_buf[GC_trace_buf_ptr].arg1 = arg1 ^ 0x80000000;
1298     GC_trace_buf[GC_trace_buf_ptr].arg2 = arg2 ^ 0x80000000;
1299     GC_trace_buf_ptr++;
1300     if (GC_trace_buf_ptr >= TRACE_ENTRIES) GC_trace_buf_ptr = 0;
1301 }
1302
1303 void GC_print_trace(word gc_no, GC_bool lock)
1304 {
1305     int i;
1306     struct trace_entry *p;
1307     
1308     if (lock) LOCK();
1309     for (i = GC_trace_buf_ptr-1; i != GC_trace_buf_ptr; i--) {
1310         if (i < 0) i = TRACE_ENTRIES-1;
1311         p = GC_trace_buf + i;
1312         if (p -> gc_no < gc_no || p -> kind == 0) return;
1313         printf("Trace:%s (gc:%d,words:%d) 0x%X, 0x%X\n",
1314                 p -> kind, p -> gc_no, p -> words_allocd,
1315                 (p -> arg1) ^ 0x80000000, (p -> arg2) ^ 0x80000000);
1316     }
1317     printf("Trace incomplete\n");
1318     if (lock) UNLOCK();
1319 }
1320
1321 # endif /* TRACE_BUF */
1322
1323 /*
1324  * A version of GC_push_all that treats all interior pointers as valid
1325  * and scans the entire region immediately, in case the contents
1326  * change.
1327  */
1328 void GC_push_all_eager(bottom, top)
1329 ptr_t bottom;
1330 ptr_t top;
1331 {
1332     word * b = (word *)(((long) bottom + ALIGNMENT-1) & ~(ALIGNMENT-1));
1333     word * t = (word *)(((long) top) & ~(ALIGNMENT-1));
1334     register word *p;
1335     register word q;
1336     register word *lim;
1337     register ptr_t greatest_ha = GC_greatest_plausible_heap_addr;
1338     register ptr_t least_ha = GC_least_plausible_heap_addr;
1339 #   define GC_greatest_plausible_heap_addr greatest_ha
1340 #   define GC_least_plausible_heap_addr least_ha
1341
1342     if (top == 0) return;
1343     /* check all pointers in range and put in push if they appear */
1344     /* to be valid.                                               */
1345       lim = t - 1 /* longword */;
1346       for (p = b; p <= lim; p = (word *)(((char *)p) + ALIGNMENT)) {
1347         q = *p;
1348         GC_PUSH_ONE_STACK(q, p);
1349       }
1350 #   undef GC_greatest_plausible_heap_addr
1351 #   undef GC_least_plausible_heap_addr
1352 }
1353
1354 #ifndef THREADS
1355 /*
1356  * A version of GC_push_all that treats all interior pointers as valid
1357  * and scans part of the area immediately, to make sure that saved
1358  * register values are not lost.
1359  * Cold_gc_frame delimits the stack section that must be scanned
1360  * eagerly.  A zero value indicates that no eager scanning is needed.
1361  */
1362 void GC_push_all_stack_partially_eager(bottom, top, cold_gc_frame)
1363 ptr_t bottom;
1364 ptr_t top;
1365 ptr_t cold_gc_frame;
1366 {
1367   if (GC_all_interior_pointers) {
1368 #   define EAGER_BYTES 1024
1369     /* Push the hot end of the stack eagerly, so that register values   */
1370     /* saved inside GC frames are marked before they disappear.         */
1371     /* The rest of the marking can be deferred until later.             */
1372     if (0 == cold_gc_frame) {
1373         GC_push_all_stack(bottom, top);
1374         return;
1375     }
1376 #   ifdef STACK_GROWS_DOWN
1377         GC_push_all_eager(bottom, cold_gc_frame);
1378         GC_push_all(cold_gc_frame - sizeof(ptr_t), top);
1379 #   else /* STACK_GROWS_UP */
1380         GC_push_all_eager(cold_gc_frame, top);
1381         GC_push_all(bottom, cold_gc_frame + sizeof(ptr_t));
1382 #   endif /* STACK_GROWS_UP */
1383   } else {
1384     GC_push_all_eager(bottom, top);
1385   }
1386 # ifdef TRACE_BUF
1387       GC_add_trace_entry("GC_push_all_stack", bottom, top);
1388 # endif
1389 }
1390 #endif /* !THREADS */
1391
1392 void GC_push_all_stack(bottom, top)
1393 ptr_t bottom;
1394 ptr_t top;
1395 {
1396   if (GC_all_interior_pointers) {
1397     GC_push_all(bottom, top);
1398   } else {
1399     GC_push_all_eager(bottom, top);
1400   }
1401 }
1402
1403 #if !defined(SMALL_CONFIG) && !defined(USE_MARK_BYTES)
1404 /* Push all objects reachable from marked objects in the given block */
1405 /* of size 1 objects.                                                */
1406 void GC_push_marked1(h, hhdr)
1407 struct hblk *h;
1408 register hdr * hhdr;
1409 {
1410     word * mark_word_addr = &(hhdr->hb_marks[0]);
1411     register word *p;
1412     word *plim;
1413     register int i;
1414     register word q;
1415     register word mark_word;
1416     register ptr_t greatest_ha = GC_greatest_plausible_heap_addr;
1417     register ptr_t least_ha = GC_least_plausible_heap_addr;
1418     register mse * mark_stack_top = GC_mark_stack_top;
1419     register mse * mark_stack_limit = GC_mark_stack_limit;
1420 #   define GC_mark_stack_top mark_stack_top
1421 #   define GC_mark_stack_limit mark_stack_limit
1422 #   define GC_greatest_plausible_heap_addr greatest_ha
1423 #   define GC_least_plausible_heap_addr least_ha
1424     
1425     p = (word *)(h->hb_body);
1426     plim = (word *)(((word)h) + HBLKSIZE);
1427
1428     /* go through all words in block */
1429         while( p < plim )  {
1430             mark_word = *mark_word_addr++;
1431             i = 0;
1432             while(mark_word != 0) {
1433               if (mark_word & 1) {
1434                   q = p[i];
1435                   GC_PUSH_ONE_HEAP(q, p + i);
1436               }
1437               i++;
1438               mark_word >>= 1;
1439             }
1440             p += WORDSZ;
1441         }
1442 #   undef GC_greatest_plausible_heap_addr
1443 #   undef GC_least_plausible_heap_addr        
1444 #   undef GC_mark_stack_top
1445 #   undef GC_mark_stack_limit
1446     GC_mark_stack_top = mark_stack_top;
1447 }
1448
1449
1450 #ifndef UNALIGNED
1451
1452 /* Push all objects reachable from marked objects in the given block */
1453 /* of size 2 objects.                                                */
1454 void GC_push_marked2(h, hhdr)
1455 struct hblk *h;
1456 register hdr * hhdr;
1457 {
1458     word * mark_word_addr = &(hhdr->hb_marks[0]);
1459     register word *p;
1460     word *plim;
1461     register int i;
1462     register word q;
1463     register word mark_word;
1464     register ptr_t greatest_ha = GC_greatest_plausible_heap_addr;
1465     register ptr_t least_ha = GC_least_plausible_heap_addr;
1466     register mse * mark_stack_top = GC_mark_stack_top;
1467     register mse * mark_stack_limit = GC_mark_stack_limit;
1468 #   define GC_mark_stack_top mark_stack_top
1469 #   define GC_mark_stack_limit mark_stack_limit
1470 #   define GC_greatest_plausible_heap_addr greatest_ha
1471 #   define GC_least_plausible_heap_addr least_ha
1472     
1473     p = (word *)(h->hb_body);
1474     plim = (word *)(((word)h) + HBLKSIZE);
1475
1476     /* go through all words in block */
1477         while( p < plim )  {
1478             mark_word = *mark_word_addr++;
1479             i = 0;
1480             while(mark_word != 0) {
1481               if (mark_word & 1) {
1482                   q = p[i];
1483                   GC_PUSH_ONE_HEAP(q, p + i);
1484                   q = p[i+1];
1485                   GC_PUSH_ONE_HEAP(q, p + i);
1486               }
1487               i += 2;
1488               mark_word >>= 2;
1489             }
1490             p += WORDSZ;
1491         }
1492 #   undef GC_greatest_plausible_heap_addr
1493 #   undef GC_least_plausible_heap_addr        
1494 #   undef GC_mark_stack_top
1495 #   undef GC_mark_stack_limit
1496     GC_mark_stack_top = mark_stack_top;
1497 }
1498
1499 /* Push all objects reachable from marked objects in the given block */
1500 /* of size 4 objects.                                                */
1501 /* There is a risk of mark stack overflow here.  But we handle that. */
1502 /* And only unmarked objects get pushed, so it's not very likely.    */
1503 void GC_push_marked4(h, hhdr)
1504 struct hblk *h;
1505 register hdr * hhdr;
1506 {
1507     word * mark_word_addr = &(hhdr->hb_marks[0]);
1508     register word *p;
1509     word *plim;
1510     register int i;
1511     register word q;
1512     register word mark_word;
1513     register ptr_t greatest_ha = GC_greatest_plausible_heap_addr;
1514     register ptr_t least_ha = GC_least_plausible_heap_addr;
1515     register mse * mark_stack_top = GC_mark_stack_top;
1516     register mse * mark_stack_limit = GC_mark_stack_limit;
1517 #   define GC_mark_stack_top mark_stack_top
1518 #   define GC_mark_stack_limit mark_stack_limit
1519 #   define GC_greatest_plausible_heap_addr greatest_ha
1520 #   define GC_least_plausible_heap_addr least_ha
1521     
1522     p = (word *)(h->hb_body);
1523     plim = (word *)(((word)h) + HBLKSIZE);
1524
1525     /* go through all words in block */
1526         while( p < plim )  {
1527             mark_word = *mark_word_addr++;
1528             i = 0;
1529             while(mark_word != 0) {
1530               if (mark_word & 1) {
1531                   q = p[i];
1532                   GC_PUSH_ONE_HEAP(q, p + i);
1533                   q = p[i+1];
1534                   GC_PUSH_ONE_HEAP(q, p + i + 1);
1535                   q = p[i+2];
1536                   GC_PUSH_ONE_HEAP(q, p + i + 2);
1537                   q = p[i+3];
1538                   GC_PUSH_ONE_HEAP(q, p + i + 3);
1539               }
1540               i += 4;
1541               mark_word >>= 4;
1542             }
1543             p += WORDSZ;
1544         }
1545 #   undef GC_greatest_plausible_heap_addr
1546 #   undef GC_least_plausible_heap_addr        
1547 #   undef GC_mark_stack_top
1548 #   undef GC_mark_stack_limit
1549     GC_mark_stack_top = mark_stack_top;
1550 }
1551
1552 #endif /* UNALIGNED */
1553
1554 #endif /* SMALL_CONFIG */
1555
1556 /* Push all objects reachable from marked objects in the given block */
1557 void GC_push_marked(h, hhdr)
1558 struct hblk *h;
1559 register hdr * hhdr;
1560 {
1561     register int sz = hhdr -> hb_sz;
1562     register int descr = hhdr -> hb_descr;
1563     register word * p;
1564     register int word_no;
1565     register word * lim;
1566     register mse * GC_mark_stack_top_reg;
1567     register mse * mark_stack_limit = GC_mark_stack_limit;
1568     
1569     /* Some quick shortcuts: */
1570         if ((0 | GC_DS_LENGTH) == descr) return;
1571         if (GC_block_empty(hhdr)/* nothing marked */) return;
1572     GC_n_rescuing_pages++;
1573     GC_objects_are_marked = TRUE;
1574     if (sz > MAXOBJSZ) {
1575         lim = (word *)h;
1576     } else {
1577         lim = (word *)(h + 1) - sz;
1578     }
1579     
1580     switch(sz) {
1581 #   if !defined(SMALL_CONFIG) && !defined(USE_MARK_BYTES)   
1582      case 1:
1583        GC_push_marked1(h, hhdr);
1584        break;
1585 #   endif
1586 #   if !defined(SMALL_CONFIG) && !defined(UNALIGNED) && \
1587        !defined(USE_MARK_BYTES)
1588      case 2:
1589        GC_push_marked2(h, hhdr);
1590        break;
1591      case 4:
1592        GC_push_marked4(h, hhdr);
1593        break;
1594 #   endif       
1595      default:
1596       GC_mark_stack_top_reg = GC_mark_stack_top;
1597       for (p = (word *)h, word_no = 0; p <= lim; p += sz, word_no += sz) {
1598          if (mark_bit_from_hdr(hhdr, word_no)) {
1599            /* Mark from fields inside the object */
1600              PUSH_OBJ((word *)p, hhdr, GC_mark_stack_top_reg, mark_stack_limit);
1601 #            ifdef GATHERSTATS
1602                 /* Subtract this object from total, since it was        */
1603                 /* added in twice.                                      */
1604                 GC_composite_in_use -= sz;
1605 #            endif
1606          }
1607       }
1608       GC_mark_stack_top = GC_mark_stack_top_reg;
1609     }
1610 }
1611
1612 #ifndef SMALL_CONFIG
1613 /* Test whether any page in the given block is dirty    */
1614 GC_bool GC_block_was_dirty(h, hhdr)
1615 struct hblk *h;
1616 register hdr * hhdr;
1617 {
1618     register int sz = hhdr -> hb_sz;
1619     
1620     if (sz < MAXOBJSZ) {
1621          return(GC_page_was_dirty(h));
1622     } else {
1623          register ptr_t p = (ptr_t)h;
1624          sz = WORDS_TO_BYTES(sz);
1625          while (p < (ptr_t)h + sz) {
1626              if (GC_page_was_dirty((struct hblk *)p)) return(TRUE);
1627              p += HBLKSIZE;
1628          }
1629          return(FALSE);
1630     }
1631 }
1632 #endif /* SMALL_CONFIG */
1633
1634 /* Similar to GC_push_next_marked, but return address of next block     */
1635 struct hblk * GC_push_next_marked(h)
1636 struct hblk *h;
1637 {
1638     register hdr * hhdr;
1639     
1640     h = GC_next_used_block(h);
1641     if (h == 0) return(0);
1642     hhdr = HDR(h);
1643     GC_push_marked(h, hhdr);
1644     return(h + OBJ_SZ_TO_BLOCKS(hhdr -> hb_sz));
1645 }
1646
1647 #ifndef SMALL_CONFIG
1648 /* Identical to above, but mark only from dirty pages   */
1649 struct hblk * GC_push_next_marked_dirty(h)
1650 struct hblk *h;
1651 {
1652     register hdr * hhdr;
1653     
1654     if (!GC_dirty_maintained) { ABORT("dirty bits not set up"); }
1655     for (;;) {
1656         h = GC_next_used_block(h);
1657         if (h == 0) return(0);
1658         hhdr = HDR(h);
1659 #       ifdef STUBBORN_ALLOC
1660           if (hhdr -> hb_obj_kind == STUBBORN) {
1661             if (GC_page_was_changed(h) && GC_block_was_dirty(h, hhdr)) {
1662                 break;
1663             }
1664           } else {
1665             if (GC_block_was_dirty(h, hhdr)) break;
1666           }
1667 #       else
1668           if (GC_block_was_dirty(h, hhdr)) break;
1669 #       endif
1670         h += OBJ_SZ_TO_BLOCKS(hhdr -> hb_sz);
1671     }
1672     GC_push_marked(h, hhdr);
1673     return(h + OBJ_SZ_TO_BLOCKS(hhdr -> hb_sz));
1674 }
1675 #endif
1676
1677 /* Similar to above, but for uncollectable pages.  Needed since we      */
1678 /* do not clear marks for such pages, even for full collections.        */
1679 struct hblk * GC_push_next_marked_uncollectable(h)
1680 struct hblk *h;
1681 {
1682     register hdr * hhdr = HDR(h);
1683     
1684     for (;;) {
1685         h = GC_next_used_block(h);
1686         if (h == 0) return(0);
1687         hhdr = HDR(h);
1688         if (hhdr -> hb_obj_kind == UNCOLLECTABLE) break;
1689         h += OBJ_SZ_TO_BLOCKS(hhdr -> hb_sz);
1690     }
1691     GC_push_marked(h, hhdr);
1692     return(h + OBJ_SZ_TO_BLOCKS(hhdr -> hb_sz));
1693 }
1694
1695