OSDN Git Service

2000-05-07 Bryce McKinlay <bryce@albatross.co.nz>
[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  *
6  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
7  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
8  *
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.
14  *
15  */
16
17
18 # include <stdio.h>
19 # include "gc_priv.h"
20 # include "gc_mark.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 | 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,
53 #               else
54                 0 | DS_LENGTH,
55 #               endif
56                 TRUE /* add length to descr */, TRUE },
57 /* UNCOLLECTABLE */
58               { &GC_uobjfreelist[0], 0,
59                 0 | DS_LENGTH, TRUE /* add length to descr */, TRUE },
60 # ifdef ATOMIC_UNCOLLECTABLE
61    /* AUNCOLLECTABLE */
62               { &GC_auobjfreelist[0], 0,
63                 0 | DS_LENGTH, FALSE /* add length to descr */, FALSE },
64 # endif
65 # ifdef STUBBORN_ALLOC
66 /*STUBBORN*/ { &GC_sobjfreelist[0], 0,
67                 0 | DS_LENGTH, TRUE /* add length to descr */, TRUE },
68 # endif
69 };
70
71 # ifdef ATOMIC_UNCOLLECTABLE
72 #   ifdef STUBBORN_ALLOC
73       int GC_n_kinds = 5;
74 #   else
75       int GC_n_kinds = 4;
76 #   endif
77 # else
78 #   ifdef STUBBORN_ALLOC
79       int GC_n_kinds = 4;
80 #   else
81       int GC_n_kinds = 3;
82 #   endif
83 # endif
84
85
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.                                */
90                 /* The incremental collector actually likes a larger    */
91                 /* size, since it want to push all marked dirty objs    */
92                 /* before marking anything new.  Currently we let it    */
93                 /* grow dynamically.                                    */
94 # endif
95
96 /*
97  * Limits of stack for GC_mark routine.
98  * All ranges between GC_mark_stack(incl.) and GC_mark_stack_top(incl.) still
99  * need to be marked from.
100  */
101
102 word GC_n_rescuing_pages;       /* Number of dirty pages we marked from */
103                                 /* excludes ptrfree pages, etc.         */
104
105 mse * GC_mark_stack;
106
107 word GC_mark_stack_size = 0;
108  
109 mse * GC_mark_stack_top;
110
111 static struct hblk * scan_ptr;
112
113 mark_state_t GC_mark_state = MS_NONE;
114
115 GC_bool GC_mark_stack_too_small = FALSE;
116
117 GC_bool GC_objects_are_marked = FALSE;  /* Are there collectable marked */
118                                         /* objects in the heap?         */
119
120 /* Is a collection in progress?  Note that this can return true in the  */
121 /* nonincremental case, if a collection has been abandoned and the      */
122 /* mark state is now MS_INVALID.                                        */
123 GC_bool GC_collection_in_progress()
124 {
125     return(GC_mark_state != MS_NONE);
126 }
127
128 /* clear all mark bits in the header */
129 void GC_clear_hdr_marks(hhdr)
130 register hdr * hhdr;
131 {
132     BZERO(hhdr -> hb_marks, MARK_BITS_SZ*sizeof(word));
133 }
134
135 /* Set all mark bits in the header.  Used for uncollectable blocks. */
136 void GC_set_hdr_marks(hhdr)
137 register hdr * hhdr;
138 {
139     register int i;
140
141     for (i = 0; i < MARK_BITS_SZ; ++i) {
142         hhdr -> hb_marks[i] = ONES;
143     }
144 }
145
146 /*
147  * Clear all mark bits associated with block h.
148  */
149 /*ARGSUSED*/
150 static void clear_marks_for_block(h, dummy)
151 struct hblk *h;
152 word dummy;
153 {
154     register hdr * hhdr = HDR(h);
155     
156     if (IS_UNCOLLECTABLE(hhdr -> hb_obj_kind)) return;
157         /* Mark bit for these is cleared only once the object is        */
158         /* explicitly deallocated.  This either frees the block, or     */
159         /* the bit is cleared once the object is on the free list.      */
160     GC_clear_hdr_marks(hhdr);
161 }
162
163 /* Slow but general routines for setting/clearing/asking about mark bits */
164 void GC_set_mark_bit(p)
165 ptr_t p;
166 {
167     register struct hblk *h = HBLKPTR(p);
168     register hdr * hhdr = HDR(h);
169     register int word_no = (word *)p - (word *)h;
170     
171     set_mark_bit_from_hdr(hhdr, word_no);
172 }
173
174 void GC_clear_mark_bit(p)
175 ptr_t p;
176 {
177     register struct hblk *h = HBLKPTR(p);
178     register hdr * hhdr = HDR(h);
179     register int word_no = (word *)p - (word *)h;
180     
181     clear_mark_bit_from_hdr(hhdr, word_no);
182 }
183
184 GC_bool GC_is_marked(p)
185 ptr_t p;
186 {
187     register struct hblk *h = HBLKPTR(p);
188     register hdr * hhdr = HDR(h);
189     register int word_no = (word *)p - (word *)h;
190     
191     return(mark_bit_from_hdr(hhdr, word_no));
192 }
193
194
195 /*
196  * Clear mark bits in all allocated heap blocks.  This invalidates
197  * the marker invariant, and sets GC_mark_state to reflect this.
198  * (This implicitly starts marking to reestablish the invariant.)
199  */
200 void GC_clear_marks()
201 {
202     GC_apply_to_all_blocks(clear_marks_for_block, (word)0);
203     GC_objects_are_marked = FALSE;
204     GC_mark_state = MS_INVALID;
205     scan_ptr = 0;
206 #   ifdef GATHERSTATS
207         /* Counters reflect currently marked objects: reset here */
208         GC_composite_in_use = 0;
209         GC_atomic_in_use = 0;
210 #   endif
211
212 }
213
214 /* Initiate a garbage collection.  Initiates a full collection if the   */
215 /* mark state is invalid.                                               */
216 /*ARGSUSED*/
217 void GC_initiate_gc()
218 {
219     if (GC_dirty_maintained) GC_read_dirty();
220 #   ifdef STUBBORN_ALLOC
221         GC_read_changed();
222 #   endif
223 #   ifdef CHECKSUMS
224         {
225             extern void GC_check_dirty();
226             
227             if (GC_dirty_maintained) GC_check_dirty();
228         }
229 #   endif
230 #   ifdef GATHERSTATS
231         GC_n_rescuing_pages = 0;
232 #   endif
233     if (GC_mark_state == MS_NONE) {
234         GC_mark_state = MS_PUSH_RESCUERS;
235     } else if (GC_mark_state != MS_INVALID) {
236         ABORT("unexpected state");
237     } /* else this is really a full collection, and mark        */
238       /* bits are invalid.                                      */
239     scan_ptr = 0;
240 }
241
242
243 static void alloc_mark_stack();
244
245 /* Perform a small amount of marking.                   */
246 /* We try to touch roughly a page of memory.            */
247 /* Return TRUE if we just finished a mark phase.        */
248 /* Cold_gc_frame is an address inside a GC frame that   */
249 /* remains valid until all marking is complete.         */
250 /* A zero value indicates that it's OK to miss some     */
251 /* register values.                                     */
252 GC_bool GC_mark_some(cold_gc_frame)
253 ptr_t cold_gc_frame;
254 {
255     switch(GC_mark_state) {
256         case MS_NONE:
257             return(FALSE);
258             
259         case MS_PUSH_RESCUERS:
260             if (GC_mark_stack_top
261                 >= GC_mark_stack + GC_mark_stack_size
262                    - INITIAL_MARK_STACK_SIZE/2) {
263                 /* Go ahead and mark, even though that might cause us to */
264                 /* see more marked dirty objects later on.  Avoid this   */
265                 /* in the future.                                        */
266                 GC_mark_stack_too_small = TRUE;
267                 GC_mark_from_mark_stack();
268                 return(FALSE);
269             } else {
270                 scan_ptr = GC_push_next_marked_dirty(scan_ptr);
271                 if (scan_ptr == 0) {
272 #                   ifdef PRINTSTATS
273                         GC_printf1("Marked from %lu dirty pages\n",
274                                    (unsigned long)GC_n_rescuing_pages);
275 #                   endif
276                     GC_push_roots(FALSE, cold_gc_frame);
277                     GC_objects_are_marked = TRUE;
278                     if (GC_mark_state != MS_INVALID) {
279                         GC_mark_state = MS_ROOTS_PUSHED;
280                     }
281                 }
282             }
283             return(FALSE);
284         
285         case MS_PUSH_UNCOLLECTABLE:
286             if (GC_mark_stack_top
287                 >= GC_mark_stack + INITIAL_MARK_STACK_SIZE/4) {
288                 GC_mark_from_mark_stack();
289                 return(FALSE);
290             } else {
291                 scan_ptr = GC_push_next_marked_uncollectable(scan_ptr);
292                 if (scan_ptr == 0) {
293                     GC_push_roots(TRUE, cold_gc_frame);
294                     GC_objects_are_marked = TRUE;
295                     if (GC_mark_state != MS_INVALID) {
296                         GC_mark_state = MS_ROOTS_PUSHED;
297                     }
298                 }
299             }
300             return(FALSE);
301         
302         case MS_ROOTS_PUSHED:
303             if (GC_mark_stack_top >= GC_mark_stack) {
304                 GC_mark_from_mark_stack();
305                 return(FALSE);
306             } else {
307                 GC_mark_state = MS_NONE;
308                 if (GC_mark_stack_too_small) {
309                     alloc_mark_stack(2*GC_mark_stack_size);
310                 }
311                 return(TRUE);
312             }
313             
314         case MS_INVALID:
315         case MS_PARTIALLY_INVALID:
316             if (!GC_objects_are_marked) {
317                 GC_mark_state = MS_PUSH_UNCOLLECTABLE;
318                 return(FALSE);
319             }
320             if (GC_mark_stack_top >= GC_mark_stack) {
321                 GC_mark_from_mark_stack();
322                 return(FALSE);
323             }
324             if (scan_ptr == 0 && GC_mark_state == MS_INVALID) {
325                 /* About to start a heap scan for marked objects. */
326                 /* Mark stack is empty.  OK to reallocate.        */
327                 if (GC_mark_stack_too_small) {
328                     alloc_mark_stack(2*GC_mark_stack_size);
329                 }
330                 GC_mark_state = MS_PARTIALLY_INVALID;
331             }
332             scan_ptr = GC_push_next_marked(scan_ptr);
333             if (scan_ptr == 0 && GC_mark_state == MS_PARTIALLY_INVALID) {
334                 GC_push_roots(TRUE, cold_gc_frame);
335                 GC_objects_are_marked = TRUE;
336                 if (GC_mark_state != MS_INVALID) {
337                     GC_mark_state = MS_ROOTS_PUSHED;
338                 }
339             }
340             return(FALSE);
341         default:
342             ABORT("GC_mark_some: bad state");
343             return(FALSE);
344     }
345 }
346
347
348 GC_bool GC_mark_stack_empty()
349 {
350     return(GC_mark_stack_top < GC_mark_stack);
351 }       
352
353 #ifdef PROF_MARKER
354     word GC_prof_array[10];
355 #   define PROF(n) GC_prof_array[n]++
356 #else
357 #   define PROF(n)
358 #endif
359
360 /* Given a pointer to someplace other than a small object page or the   */
361 /* first page of a large object, return a pointer either to the         */
362 /* start of the large object or NIL.                                    */
363 /* In the latter case black list the address current.                   */
364 /* Returns NIL without black listing if current points to a block       */
365 /* with IGNORE_OFF_PAGE set.                                            */
366 /*ARGSUSED*/
367 # ifdef PRINT_BLACK_LIST
368   ptr_t GC_find_start(current, hhdr, source)
369   word source;
370 # else
371   ptr_t GC_find_start(current, hhdr)
372 # define source 0
373 # endif
374 register ptr_t current;
375 register hdr * hhdr;
376 {
377 #   ifdef ALL_INTERIOR_POINTERS
378         if (hhdr != 0) {
379             register ptr_t orig = current;
380             
381             current = (ptr_t)HBLKPTR(current) + HDR_BYTES;
382             do {
383               current = current - HBLKSIZE*(word)hhdr;
384               hhdr = HDR(current);
385             } while(IS_FORWARDING_ADDR_OR_NIL(hhdr));
386             /* current points to the start of the large object */
387             if (hhdr -> hb_flags & IGNORE_OFF_PAGE) return(0);
388             if ((word *)orig - (word *)current
389                  >= (ptrdiff_t)(hhdr->hb_sz)) {
390                 /* Pointer past the end of the block */
391                 GC_ADD_TO_BLACK_LIST_NORMAL(orig, source);
392                 return(0);
393             }
394             return(current);
395         } else {
396             GC_ADD_TO_BLACK_LIST_NORMAL(current, source);
397             return(0);
398         }
399 #   else
400         GC_ADD_TO_BLACK_LIST_NORMAL(current, source);
401         return(0);
402 #   endif
403 #   undef source
404 }
405
406 void GC_invalidate_mark_state()
407 {
408     GC_mark_state = MS_INVALID;
409     GC_mark_stack_top = GC_mark_stack-1;
410 }
411
412 mse * GC_signal_mark_stack_overflow(msp)
413 mse * msp;
414 {
415     GC_mark_state = MS_INVALID;
416     GC_mark_stack_too_small = TRUE;
417 #   ifdef PRINTSTATS
418         GC_printf1("Mark stack overflow; current size = %lu entries\n",
419                     GC_mark_stack_size);
420 #    endif
421      return(msp-INITIAL_MARK_STACK_SIZE/8);
422 }
423
424
425 /*
426  * Mark objects pointed to by the regions described by
427  * mark stack entries between GC_mark_stack and GC_mark_stack_top,
428  * inclusive.  Assumes the upper limit of a mark stack entry
429  * is never 0.  A mark stack entry never has size 0.
430  * We try to traverse on the order of a hblk of memory before we return.
431  * Caller is responsible for calling this until the mark stack is empty.
432  * Note that this is the most performance critical routine in the
433  * collector.  Hence it contains all sorts of ugly hacks to speed
434  * things up.  In particular, we avoid procedure calls on the common
435  * path, we take advantage of peculiarities of the mark descriptor
436  * encoding, we optionally maintain a cache for the block address to
437  * header mapping, we prefetch when an object is "grayed", etc. 
438  */
439 void GC_mark_from_mark_stack()
440 {
441   mse * GC_mark_stack_reg = GC_mark_stack;
442   mse * GC_mark_stack_top_reg = GC_mark_stack_top;
443   mse * mark_stack_limit = &(GC_mark_stack[GC_mark_stack_size]);
444   int credit = HBLKSIZE;        /* Remaining credit for marking work    */
445   register word * current_p;    /* Pointer to current candidate ptr.    */
446   register word current;        /* Candidate pointer.                   */
447   register word * limit;        /* (Incl) limit of current candidate    */
448                                 /* range                                */
449   register word descr;
450   register ptr_t greatest_ha = GC_greatest_plausible_heap_addr;
451   register ptr_t least_ha = GC_least_plausible_heap_addr;
452   DECLARE_HDR_CACHE;
453
454 # define SPLIT_RANGE_WORDS 128  /* Must be power of 2.          */
455
456   GC_objects_are_marked = TRUE;
457   INIT_HDR_CACHE;
458 # ifdef OS2 /* Use untweaked version to circumvent compiler problem */
459   while (GC_mark_stack_top_reg >= GC_mark_stack_reg && credit >= 0) {
460 # else
461   while ((((ptr_t)GC_mark_stack_top_reg - (ptr_t)GC_mark_stack_reg) | credit)
462         >= 0) {
463 # endif
464     current_p = GC_mark_stack_top_reg -> mse_start;
465     descr = GC_mark_stack_top_reg -> mse_descr;
466   retry:
467     /* current_p and descr describe the current object.         */
468     /* *GC_mark_stack_top_reg is vacant.                        */
469     /* The following is 0 only for small objects described by a simple  */
470     /* length descriptor.  For many applications this is the common     */
471     /* case, so we try to detect it quickly.                            */
472     if (descr & ((~(WORDS_TO_BYTES(SPLIT_RANGE_WORDS) - 1)) | DS_TAGS)) {
473       word tag = descr & DS_TAGS;
474       
475       switch(tag) {
476         case DS_LENGTH:
477           /* Large length.                                              */
478           /* Process part of the range to avoid pushing too much on the */
479           /* stack.                                                     */
480           GC_mark_stack_top_reg -> mse_start =
481                 limit = current_p + SPLIT_RANGE_WORDS-1;
482           GC_mark_stack_top_reg -> mse_descr =
483                         descr - WORDS_TO_BYTES(SPLIT_RANGE_WORDS-1);
484           /* Make sure that pointers overlapping the two ranges are     */
485           /* considered.                                                */
486           limit = (word *)((char *)limit + sizeof(word) - ALIGNMENT);
487           break;
488         case DS_BITMAP:
489           GC_mark_stack_top_reg--;
490           descr &= ~DS_TAGS;
491           credit -= WORDS_TO_BYTES(WORDSZ/2); /* guess */
492           while (descr != 0) {
493             if ((signed_word)descr < 0) {
494               current = *current_p;
495               if ((ptr_t)current >= least_ha && (ptr_t)current < greatest_ha) {
496                 PREFETCH(current);
497                 HC_PUSH_CONTENTS((ptr_t)current, GC_mark_stack_top_reg,
498                               mark_stack_limit, current_p, exit1);
499               }
500             }
501             descr <<= 1;
502             ++ current_p;
503           }
504           continue;
505         case DS_PROC:
506           GC_mark_stack_top_reg--;
507           credit -= PROC_BYTES;
508 #ifdef GC_DEBUG
509           current_p = GC_debug_object_start(current_p);
510 #endif
511           GC_mark_stack_top_reg =
512               (*PROC(descr))
513                     (current_p, GC_mark_stack_top_reg,
514                     mark_stack_limit, ENV(descr));
515           continue;
516         case DS_PER_OBJECT:
517           if ((signed_word)descr >= 0) {
518             /* Descriptor is in the object.     */
519             descr = *(word *)((ptr_t)current_p + descr - DS_PER_OBJECT);
520           } else {
521             /* Descriptor is in type descriptor pointed to by first     */
522             /* word in object.                                          */
523             ptr_t type_descr = *(ptr_t *)current_p;
524             /* type_descr is either a valid pointer to the descriptor   */
525             /* structure, or this object was on a free list.  If it     */
526             /* it was anything but the last object on the free list,    */
527             /* we will misinterpret the next object on the free list as */
528             /* the type descriptor, and get a 0 GC descriptor, which    */
529             /* is ideal.  Unfortunately, we need to check for the last  */
530             /* object case explicitly.                                  */
531             if (0 == type_descr) {
532                 /* Rarely executed.     */
533                 GC_mark_stack_top_reg--;
534                 continue;
535             }
536             descr = *(word *)(type_descr
537                               - (descr - (DS_PER_OBJECT - INDIR_PER_OBJ_BIAS)));
538           }
539           goto retry;
540       }
541     } else /* Small object with length descriptor */ {
542       GC_mark_stack_top_reg--;
543       limit = (word *)(((ptr_t)current_p) + (word)descr);
544     }
545     /* The simple case in which we're scanning a range. */
546     credit -= (ptr_t)limit - (ptr_t)current_p;
547     limit -= 1;
548     {
549 #     define PREF_DIST 4
550
551 #     ifndef SMALL_CONFIG
552         word deferred;
553
554         /* Try to prefetch the next pointer to be examined asap.        */
555         /* Empirically, this also seems to help slightly without        */
556         /* prefetches, at least on linux/X86.  Presumably this loop     */
557         /* ends up with less register pressure, and gcc thus ends up    */
558         /* generating slightly better code.  Overall gcc code quality   */
559         /* for this loop is still not great.                            */
560         for(;;) {
561           PREFETCH((ptr_t)limit - PREF_DIST*CACHE_LINE_SIZE);
562           deferred = *limit;
563           limit = (word *)((char *)limit - ALIGNMENT);
564           if ((ptr_t)deferred >= least_ha && (ptr_t)deferred <  greatest_ha) {
565             PREFETCH(deferred);
566             break;
567           }
568           if (current_p > limit) goto next_object;
569           /* Unroll once, so we don't do too many of the prefetches     */
570           /* based on limit.                                            */
571           deferred = *limit;
572           limit = (word *)((char *)limit - ALIGNMENT);
573           if ((ptr_t)deferred >= least_ha && (ptr_t)deferred <  greatest_ha) {
574             PREFETCH(deferred);
575             break;
576           }
577           if (current_p > limit) goto next_object;
578         }
579 #     endif
580
581       while (current_p <= limit) {
582         /* Empirically, unrolling this loop doesn't help a lot. */
583         /* Since HC_PUSH_CONTENTS expands to a lot of code,     */
584         /* we don't.                                            */
585         current = *current_p;
586         PREFETCH((ptr_t)current_p + PREF_DIST*CACHE_LINE_SIZE);
587         if ((ptr_t)current >= least_ha && (ptr_t)current <  greatest_ha) {
588           /* Prefetch the contents of the object we just pushed.  It's  */
589           /* likely we will need them soon.                             */
590           PREFETCH(current);
591           HC_PUSH_CONTENTS((ptr_t)current, GC_mark_stack_top_reg,
592                            mark_stack_limit, current_p, exit2);
593         }
594         current_p = (word *)((char *)current_p + ALIGNMENT);
595       }
596
597 #     ifndef SMALL_CONFIG
598         /* We still need to mark the entry we previously prefetched.    */
599         /* We alrady know that it passes the preliminary pointer        */
600         /* validity test.                                               */
601         HC_PUSH_CONTENTS((ptr_t)deferred, GC_mark_stack_top_reg,
602                          mark_stack_limit, current_p, exit4);
603         next_object:;
604 #     endif
605     }
606   }
607   GC_mark_stack_top = GC_mark_stack_top_reg;
608 }
609
610 /* Allocate or reallocate space for mark stack of size s words  */
611 /* May silently fail.                                           */
612 static void alloc_mark_stack(n)
613 word n;
614 {
615     mse * new_stack = (mse *)GC_scratch_alloc(n * sizeof(struct ms_entry));
616     
617     GC_mark_stack_too_small = FALSE;
618     if (GC_mark_stack_size != 0) {
619         if (new_stack != 0) {
620           word displ = (word)GC_mark_stack & (GC_page_size - 1);
621           signed_word size = GC_mark_stack_size * sizeof(struct ms_entry);
622           
623           /* Recycle old space */
624               if (0 != displ) displ = GC_page_size - displ;
625               size = (size - displ) & ~(GC_page_size - 1);
626               if (size > 0) {
627                 GC_add_to_heap((struct hblk *)
628                                 ((word)GC_mark_stack + displ), (word)size);
629               }
630           GC_mark_stack = new_stack;
631           GC_mark_stack_size = n;
632 #         ifdef PRINTSTATS
633               GC_printf1("Grew mark stack to %lu frames\n",
634                          (unsigned long) GC_mark_stack_size);
635 #         endif
636         } else {
637 #         ifdef PRINTSTATS
638               GC_printf1("Failed to grow mark stack to %lu frames\n",
639                          (unsigned long) n);
640 #         endif
641         }
642     } else {
643         if (new_stack == 0) {
644             GC_err_printf0("No space for mark stack\n");
645             EXIT();
646         }
647         GC_mark_stack = new_stack;
648         GC_mark_stack_size = n;
649     }
650     GC_mark_stack_top = GC_mark_stack-1;
651 }
652
653 void GC_mark_init()
654 {
655     alloc_mark_stack(INITIAL_MARK_STACK_SIZE);
656 }
657
658 /*
659  * Push all locations between b and t onto the mark stack.
660  * b is the first location to be checked. t is one past the last
661  * location to be checked.
662  * Should only be used if there is no possibility of mark stack
663  * overflow.
664  */
665 void GC_push_all(bottom, top)
666 ptr_t bottom;
667 ptr_t top;
668 {
669     register word length;
670     
671     bottom = (ptr_t)(((word) bottom + ALIGNMENT-1) & ~(ALIGNMENT-1));
672     top = (ptr_t)(((word) top) & ~(ALIGNMENT-1));
673     if (top == 0 || bottom == top) return;
674     GC_mark_stack_top++;
675     if (GC_mark_stack_top >= GC_mark_stack + GC_mark_stack_size) {
676         ABORT("unexpected mark stack overflow");
677     }
678     length = top - bottom;
679 #   if DS_TAGS > ALIGNMENT - 1
680         length += DS_TAGS;
681         length &= ~DS_TAGS;
682 #   endif
683     GC_mark_stack_top -> mse_start = (word *)bottom;
684     GC_mark_stack_top -> mse_descr = length;
685 }
686
687 /*
688  * Analogous to the above, but push only those pages that may have been
689  * dirtied.  A block h is assumed dirty if dirty_fn(h) != 0.
690  * We use push_fn to actually push the block.
691  * Will not overflow mark stack if push_fn pushes a small fixed number
692  * of entries.  (This is invoked only if push_fn pushes a single entry,
693  * or if it marks each object before pushing it, thus ensuring progress
694  * in the event of a stack overflow.)
695  */
696 void GC_push_dirty(bottom, top, dirty_fn, push_fn)
697 ptr_t bottom;
698 ptr_t top;
699 int (*dirty_fn)(/* struct hblk * h */);
700 void (*push_fn)(/* ptr_t bottom, ptr_t top */);
701 {
702     register struct hblk * h;
703
704     bottom = (ptr_t)(((long) bottom + ALIGNMENT-1) & ~(ALIGNMENT-1));
705     top = (ptr_t)(((long) top) & ~(ALIGNMENT-1));
706
707     if (top == 0 || bottom == top) return;
708     h = HBLKPTR(bottom + HBLKSIZE);
709     if (top <= (ptr_t) h) {
710         if ((*dirty_fn)(h-1)) {
711             (*push_fn)(bottom, top);
712         }
713         return;
714     }
715     if ((*dirty_fn)(h-1)) {
716         (*push_fn)(bottom, (ptr_t)h);
717     }
718     while ((ptr_t)(h+1) <= top) {
719         if ((*dirty_fn)(h)) {
720             if ((word)(GC_mark_stack_top - GC_mark_stack)
721                 > 3 * GC_mark_stack_size / 4) {
722                 /* Danger of mark stack overflow */
723                 (*push_fn)((ptr_t)h, top);
724                 return;
725             } else {
726                 (*push_fn)((ptr_t)h, (ptr_t)(h+1));
727             }
728         }
729         h++;
730     }
731     if ((ptr_t)h != top) {
732         if ((*dirty_fn)(h)) {
733             (*push_fn)((ptr_t)h, top);
734         }
735     }
736     if (GC_mark_stack_top >= GC_mark_stack + GC_mark_stack_size) {
737         ABORT("unexpected mark stack overflow");
738     }
739 }
740
741 # ifndef SMALL_CONFIG
742 void GC_push_conditional(bottom, top, all)
743 ptr_t bottom;
744 ptr_t top;
745 int all;
746 {
747     if (all) {
748       if (GC_dirty_maintained) {
749 #       ifdef PROC_VDB
750             /* Pages that were never dirtied cannot contain pointers    */
751             GC_push_dirty(bottom, top, GC_page_was_ever_dirty, GC_push_all);
752 #       else
753             GC_push_all(bottom, top);
754 #       endif
755       } else {
756         GC_push_all(bottom, top);
757       }
758     } else {
759         GC_push_dirty(bottom, top, GC_page_was_dirty, GC_push_all);
760     }
761 }
762 #endif
763
764 # ifdef MSWIN32
765   void __cdecl GC_push_one(p)
766 # else
767   void GC_push_one(p)
768 # endif
769 word p;
770 {
771 #   ifdef NURSERY
772       if (0 != GC_push_proc) {
773         GC_push_proc(p);
774         return;
775       }
776 #   endif
777     GC_PUSH_ONE_STACK(p, MARKED_FROM_REGISTER);
778 }
779
780 # ifdef __STDC__
781 #   define BASE(p) (word)GC_base((void *)(p))
782 # else
783 #   define BASE(p) (word)GC_base((char *)(p))
784 # endif
785
786 /* As above, but argument passed preliminary test. */
787 # if defined(PRINT_BLACK_LIST) || defined(KEEP_BACK_PTRS)
788     void GC_push_one_checked(p, interior_ptrs, source)
789     ptr_t source;
790 # else
791     void GC_push_one_checked(p, interior_ptrs)
792 #   define source 0
793 # endif
794 register word p;
795 register GC_bool interior_ptrs;
796 {
797     register word r;
798     register hdr * hhdr; 
799     register int displ;
800   
801     GET_HDR(p, hhdr);
802     if (IS_FORWARDING_ADDR_OR_NIL(hhdr)) {
803         if (hhdr != 0 && interior_ptrs) {
804           r = BASE(p);
805           hhdr = HDR(r);
806           displ = BYTES_TO_WORDS(HBLKDISPL(r));
807         } else {
808           hhdr = 0;
809         }
810     } else {
811         register map_entry_type map_entry;
812         
813         displ = HBLKDISPL(p);
814         map_entry = MAP_ENTRY((hhdr -> hb_map), displ);
815         if (map_entry == OBJ_INVALID) {
816 #         ifndef ALL_INTERIOR_POINTERS
817             if (interior_ptrs) {
818               r = BASE(p);
819               displ = BYTES_TO_WORDS(HBLKDISPL(r));
820               if (r == 0) hhdr = 0;
821             } else {
822               hhdr = 0;
823             }
824 #         else
825             /* map already reflects interior pointers */
826             hhdr = 0;
827 #         endif
828         } else {
829           displ = BYTES_TO_WORDS(displ);
830           displ -= map_entry;
831           r = (word)((word *)(HBLKPTR(p)) + displ);
832         }
833     }
834     /* If hhdr != 0 then r == GC_base(p), only we did it faster. */
835     /* displ is the word index within the block.                 */
836     if (hhdr == 0) {
837         if (interior_ptrs) {
838 #           ifdef PRINT_BLACK_LIST
839               GC_add_to_black_list_stack(p, source);
840 #           else
841               GC_add_to_black_list_stack(p);
842 #           endif
843         } else {
844             GC_ADD_TO_BLACK_LIST_NORMAL(p, source);
845 #           undef source  /* In case we had to define it. */
846         }
847     } else {
848         if (!mark_bit_from_hdr(hhdr, displ)) {
849             set_mark_bit_from_hdr(hhdr, displ);
850             GC_STORE_BACK_PTR(source, (ptr_t)r);
851             PUSH_OBJ((word *)r, hhdr, GC_mark_stack_top,
852                      &(GC_mark_stack[GC_mark_stack_size]));
853         }
854     }
855 }
856
857 # ifdef TRACE_BUF
858
859 # define TRACE_ENTRIES 1000
860
861 struct trace_entry {
862     char * kind;
863     word gc_no;
864     word words_allocd;
865     word arg1;
866     word arg2;
867 } GC_trace_buf[TRACE_ENTRIES];
868
869 int GC_trace_buf_ptr = 0;
870
871 void GC_add_trace_entry(char *kind, word arg1, word arg2)
872 {
873     GC_trace_buf[GC_trace_buf_ptr].kind = kind;
874     GC_trace_buf[GC_trace_buf_ptr].gc_no = GC_gc_no;
875     GC_trace_buf[GC_trace_buf_ptr].words_allocd = GC_words_allocd;
876     GC_trace_buf[GC_trace_buf_ptr].arg1 = arg1 ^ 0x80000000;
877     GC_trace_buf[GC_trace_buf_ptr].arg2 = arg2 ^ 0x80000000;
878     GC_trace_buf_ptr++;
879     if (GC_trace_buf_ptr >= TRACE_ENTRIES) GC_trace_buf_ptr = 0;
880 }
881
882 void GC_print_trace(word gc_no, GC_bool lock)
883 {
884     int i;
885     struct trace_entry *p;
886     
887     if (lock) LOCK();
888     for (i = GC_trace_buf_ptr-1; i != GC_trace_buf_ptr; i--) {
889         if (i < 0) i = TRACE_ENTRIES-1;
890         p = GC_trace_buf + i;
891         if (p -> gc_no < gc_no || p -> kind == 0) return;
892         printf("Trace:%s (gc:%d,words:%d) 0x%X, 0x%X\n",
893                 p -> kind, p -> gc_no, p -> words_allocd,
894                 (p -> arg1) ^ 0x80000000, (p -> arg2) ^ 0x80000000);
895     }
896     printf("Trace incomplete\n");
897     if (lock) UNLOCK();
898 }
899
900 # endif /* TRACE_BUF */
901
902 /*
903  * A version of GC_push_all that treats all interior pointers as valid
904  * and scans the entire region immediately, in case the contents
905  * change.
906  */
907 void GC_push_all_eager(bottom, top)
908 ptr_t bottom;
909 ptr_t top;
910 {
911     word * b = (word *)(((long) bottom + ALIGNMENT-1) & ~(ALIGNMENT-1));
912     word * t = (word *)(((long) top) & ~(ALIGNMENT-1));
913     register word *p;
914     register word q;
915     register word *lim;
916     register ptr_t greatest_ha = GC_greatest_plausible_heap_addr;
917     register ptr_t least_ha = GC_least_plausible_heap_addr;
918 #   define GC_greatest_plausible_heap_addr greatest_ha
919 #   define GC_least_plausible_heap_addr least_ha
920
921     if (top == 0) return;
922     /* check all pointers in range and put in push if they appear */
923     /* to be valid.                                               */
924       lim = t - 1 /* longword */;
925       for (p = b; p <= lim; p = (word *)(((char *)p) + ALIGNMENT)) {
926         q = *p;
927         GC_PUSH_ONE_STACK(q, p);
928       }
929 #   undef GC_greatest_plausible_heap_addr
930 #   undef GC_least_plausible_heap_addr
931 }
932
933 #ifndef THREADS
934 /*
935  * A version of GC_push_all that treats all interior pointers as valid
936  * and scans part of the area immediately, to make sure that saved
937  * register values are not lost.
938  * Cold_gc_frame delimits the stack section that must be scanned
939  * eagerly.  A zero value indicates that no eager scanning is needed.
940  */
941 void GC_push_all_stack_partially_eager(bottom, top, cold_gc_frame)
942 ptr_t bottom;
943 ptr_t top;
944 ptr_t cold_gc_frame;
945 {
946 # ifdef ALL_INTERIOR_POINTERS
947 #   define EAGER_BYTES 1024
948     /* Push the hot end of the stack eagerly, so that register values   */
949     /* saved inside GC frames are marked before they disappear.         */
950     /* The rest of the marking can be deferred until later.             */
951     if (0 == cold_gc_frame) {
952         GC_push_all_stack(bottom, top);
953         return;
954     }
955 #   ifdef STACK_GROWS_DOWN
956         GC_push_all_eager(bottom, cold_gc_frame);
957         GC_push_all(cold_gc_frame - sizeof(ptr_t), top);
958 #   else /* STACK_GROWS_UP */
959         GC_push_all_eager(cold_gc_frame, top);
960         GC_push_all(bottom, cold_gc_frame + sizeof(ptr_t));
961 #   endif /* STACK_GROWS_UP */
962 # else
963     GC_push_all_eager(bottom, top);
964 # endif
965 # ifdef TRACE_BUF
966       GC_add_trace_entry("GC_push_all_stack", bottom, top);
967 # endif
968 }
969 #endif /* !THREADS */
970
971 void GC_push_all_stack(bottom, top)
972 ptr_t bottom;
973 ptr_t top;
974 {
975 # ifdef ALL_INTERIOR_POINTERS
976     GC_push_all(bottom, top);
977 # else
978     GC_push_all_eager(bottom, top);
979 # endif
980 }
981
982 #ifndef SMALL_CONFIG
983 /* Push all objects reachable from marked objects in the given block */
984 /* of size 1 objects.                                                */
985 void GC_push_marked1(h, hhdr)
986 struct hblk *h;
987 register hdr * hhdr;
988 {
989     word * mark_word_addr = &(hhdr->hb_marks[divWORDSZ(HDR_WORDS)]);
990     register word *p;
991     word *plim;
992     register int i;
993     register word q;
994     register word mark_word;
995     register ptr_t greatest_ha = GC_greatest_plausible_heap_addr;
996     register ptr_t least_ha = GC_least_plausible_heap_addr;
997 #   define GC_greatest_plausible_heap_addr greatest_ha
998 #   define GC_least_plausible_heap_addr least_ha
999     
1000     p = (word *)(h->hb_body);
1001     plim = (word *)(((word)h) + HBLKSIZE);
1002
1003     /* go through all words in block */
1004         while( p < plim )  {
1005             mark_word = *mark_word_addr++;
1006             i = 0;
1007             while(mark_word != 0) {
1008               if (mark_word & 1) {
1009                   q = p[i];
1010                   GC_PUSH_ONE_HEAP(q, p + i);
1011               }
1012               i++;
1013               mark_word >>= 1;
1014             }
1015             p += WORDSZ;
1016         }
1017 #   undef GC_greatest_plausible_heap_addr
1018 #   undef GC_least_plausible_heap_addr        
1019 }
1020
1021
1022 #ifndef UNALIGNED
1023
1024 /* Push all objects reachable from marked objects in the given block */
1025 /* of size 2 objects.                                                */
1026 void GC_push_marked2(h, hhdr)
1027 struct hblk *h;
1028 register hdr * hhdr;
1029 {
1030     word * mark_word_addr = &(hhdr->hb_marks[divWORDSZ(HDR_WORDS)]);
1031     register word *p;
1032     word *plim;
1033     register int i;
1034     register word q;
1035     register word mark_word;
1036     register ptr_t greatest_ha = GC_greatest_plausible_heap_addr;
1037     register ptr_t least_ha = GC_least_plausible_heap_addr;
1038 #   define GC_greatest_plausible_heap_addr greatest_ha
1039 #   define GC_least_plausible_heap_addr least_ha
1040     
1041     p = (word *)(h->hb_body);
1042     plim = (word *)(((word)h) + HBLKSIZE);
1043
1044     /* go through all words in block */
1045         while( p < plim )  {
1046             mark_word = *mark_word_addr++;
1047             i = 0;
1048             while(mark_word != 0) {
1049               if (mark_word & 1) {
1050                   q = p[i];
1051                   GC_PUSH_ONE_HEAP(q, p + i);
1052                   q = p[i+1];
1053                   GC_PUSH_ONE_HEAP(q, p + i);
1054               }
1055               i += 2;
1056               mark_word >>= 2;
1057             }
1058             p += WORDSZ;
1059         }
1060 #   undef GC_greatest_plausible_heap_addr
1061 #   undef GC_least_plausible_heap_addr        
1062 }
1063
1064 /* Push all objects reachable from marked objects in the given block */
1065 /* of size 4 objects.                                                */
1066 /* There is a risk of mark stack overflow here.  But we handle that. */
1067 /* And only unmarked objects get pushed, so it's not very likely.    */
1068 void GC_push_marked4(h, hhdr)
1069 struct hblk *h;
1070 register hdr * hhdr;
1071 {
1072     word * mark_word_addr = &(hhdr->hb_marks[divWORDSZ(HDR_WORDS)]);
1073     register word *p;
1074     word *plim;
1075     register int i;
1076     register word q;
1077     register word mark_word;
1078     register ptr_t greatest_ha = GC_greatest_plausible_heap_addr;
1079     register ptr_t least_ha = GC_least_plausible_heap_addr;
1080 #   define GC_greatest_plausible_heap_addr greatest_ha
1081 #   define GC_least_plausible_heap_addr least_ha
1082     
1083     p = (word *)(h->hb_body);
1084     plim = (word *)(((word)h) + HBLKSIZE);
1085
1086     /* go through all words in block */
1087         while( p < plim )  {
1088             mark_word = *mark_word_addr++;
1089             i = 0;
1090             while(mark_word != 0) {
1091               if (mark_word & 1) {
1092                   q = p[i];
1093                   GC_PUSH_ONE_HEAP(q, p + i);
1094                   q = p[i+1];
1095                   GC_PUSH_ONE_HEAP(q, p + i + 1);
1096                   q = p[i+2];
1097                   GC_PUSH_ONE_HEAP(q, p + i + 2);
1098                   q = p[i+3];
1099                   GC_PUSH_ONE_HEAP(q, p + i + 3);
1100               }
1101               i += 4;
1102               mark_word >>= 4;
1103             }
1104             p += WORDSZ;
1105         }
1106 #   undef GC_greatest_plausible_heap_addr
1107 #   undef GC_least_plausible_heap_addr        
1108 }
1109
1110 #endif /* UNALIGNED */
1111
1112 #endif /* SMALL_CONFIG */
1113
1114 /* Push all objects reachable from marked objects in the given block */
1115 void GC_push_marked(h, hhdr)
1116 struct hblk *h;
1117 register hdr * hhdr;
1118 {
1119     register int sz = hhdr -> hb_sz;
1120     register int descr = hhdr -> hb_descr;
1121     register word * p;
1122     register int word_no;
1123     register word * lim;
1124     register mse * GC_mark_stack_top_reg;
1125     register mse * mark_stack_limit = &(GC_mark_stack[GC_mark_stack_size]);
1126     
1127     /* Some quick shortcuts: */
1128         if ((0 | DS_LENGTH) == descr) return;
1129         if (GC_block_empty(hhdr)/* nothing marked */) return;
1130 #   ifdef GATHERSTATS
1131         GC_n_rescuing_pages++;
1132 #   endif
1133     GC_objects_are_marked = TRUE;
1134     if (sz > MAXOBJSZ) {
1135         lim = (word *)h + HDR_WORDS;
1136     } else {
1137         lim = (word *)(h + 1) - sz;
1138     }
1139     
1140     switch(sz) {
1141 #   if !defined(SMALL_CONFIG)    
1142      case 1:
1143        GC_push_marked1(h, hhdr);
1144        break;
1145 #   endif
1146 #   if !defined(SMALL_CONFIG) && !defined(UNALIGNED)
1147      case 2:
1148        GC_push_marked2(h, hhdr);
1149        break;
1150      case 4:
1151        GC_push_marked4(h, hhdr);
1152        break;
1153 #   endif       
1154      default:
1155       GC_mark_stack_top_reg = GC_mark_stack_top;
1156       for (p = (word *)h + HDR_WORDS, word_no = HDR_WORDS; p <= lim;
1157          p += sz, word_no += sz) {
1158          if (mark_bit_from_hdr(hhdr, word_no)) {
1159            /* Mark from fields inside the object */
1160              PUSH_OBJ((word *)p, hhdr, GC_mark_stack_top_reg, mark_stack_limit);
1161 #            ifdef GATHERSTATS
1162                 /* Subtract this object from total, since it was        */
1163                 /* added in twice.                                      */
1164                 GC_composite_in_use -= sz;
1165 #            endif
1166          }
1167       }
1168       GC_mark_stack_top = GC_mark_stack_top_reg;
1169     }
1170 }
1171
1172 #ifndef SMALL_CONFIG
1173 /* Test whether any page in the given block is dirty    */
1174 GC_bool GC_block_was_dirty(h, hhdr)
1175 struct hblk *h;
1176 register hdr * hhdr;
1177 {
1178     register int sz = hhdr -> hb_sz;
1179     
1180     if (sz < MAXOBJSZ) {
1181          return(GC_page_was_dirty(h));
1182     } else {
1183          register ptr_t p = (ptr_t)h;
1184          sz += HDR_WORDS;
1185          sz = WORDS_TO_BYTES(sz);
1186          while (p < (ptr_t)h + sz) {
1187              if (GC_page_was_dirty((struct hblk *)p)) return(TRUE);
1188              p += HBLKSIZE;
1189          }
1190          return(FALSE);
1191     }
1192 }
1193 #endif /* SMALL_CONFIG */
1194
1195 /* Similar to GC_push_next_marked, but return address of next block     */
1196 struct hblk * GC_push_next_marked(h)
1197 struct hblk *h;
1198 {
1199     register hdr * hhdr;
1200     
1201     h = GC_next_used_block(h);
1202     if (h == 0) return(0);
1203     hhdr = HDR(h);
1204     GC_push_marked(h, hhdr);
1205     return(h + OBJ_SZ_TO_BLOCKS(hhdr -> hb_sz));
1206 }
1207
1208 #ifndef SMALL_CONFIG
1209 /* Identical to above, but mark only from dirty pages   */
1210 struct hblk * GC_push_next_marked_dirty(h)
1211 struct hblk *h;
1212 {
1213     register hdr * hhdr;
1214     
1215     if (!GC_dirty_maintained) { ABORT("dirty bits not set up"); }
1216     for (;;) {
1217         h = GC_next_used_block(h);
1218         if (h == 0) return(0);
1219         hhdr = HDR(h);
1220 #       ifdef STUBBORN_ALLOC
1221           if (hhdr -> hb_obj_kind == STUBBORN) {
1222             if (GC_page_was_changed(h) && GC_block_was_dirty(h, hhdr)) {
1223                 break;
1224             }
1225           } else {
1226             if (GC_block_was_dirty(h, hhdr)) break;
1227           }
1228 #       else
1229           if (GC_block_was_dirty(h, hhdr)) break;
1230 #       endif
1231         h += OBJ_SZ_TO_BLOCKS(hhdr -> hb_sz);
1232     }
1233     GC_push_marked(h, hhdr);
1234     return(h + OBJ_SZ_TO_BLOCKS(hhdr -> hb_sz));
1235 }
1236 #endif
1237
1238 /* Similar to above, but for uncollectable pages.  Needed since we      */
1239 /* do not clear marks for such pages, even for full collections.        */
1240 struct hblk * GC_push_next_marked_uncollectable(h)
1241 struct hblk *h;
1242 {
1243     register hdr * hhdr = HDR(h);
1244     
1245     for (;;) {
1246         h = GC_next_used_block(h);
1247         if (h == 0) return(0);
1248         hhdr = HDR(h);
1249         if (hhdr -> hb_obj_kind == UNCOLLECTABLE) break;
1250         h += OBJ_SZ_TO_BLOCKS(hhdr -> hb_sz);
1251     }
1252     GC_push_marked(h, hhdr);
1253     return(h + OBJ_SZ_TO_BLOCKS(hhdr -> hb_sz));
1254 }
1255
1256