75f11a291db4bce81c13193070ddf08829953088
[ghc-hetmet.git] / ghc / rts / GC.c
1 /* -----------------------------------------------------------------------------
2  *
3  * (c) The GHC Team 1998-2003
4  *
5  * Generational garbage collector
6  *
7  * ---------------------------------------------------------------------------*/
8
9 #include "PosixSource.h"
10 #include "Rts.h"
11 #include "RtsFlags.h"
12 #include "RtsUtils.h"
13 #include "Apply.h"
14 #include "Storage.h"
15 #include "LdvProfile.h"
16 #include "Updates.h"
17 #include "Stats.h"
18 #include "Schedule.h"
19 #include "Sanity.h"
20 #include "BlockAlloc.h"
21 #include "MBlock.h"
22 #include "ProfHeap.h"
23 #include "SchedAPI.h"
24 #include "Weak.h"
25 #include "Prelude.h"
26 #include "ParTicky.h"           // ToDo: move into Rts.h
27 #include "GCCompact.h"
28 #include "Signals.h"
29 #include "STM.h"
30 #if defined(GRAN) || defined(PAR)
31 # include "GranSimRts.h"
32 # include "ParallelRts.h"
33 # include "FetchMe.h"
34 # if defined(DEBUG)
35 #  include "Printer.h"
36 #  include "ParallelDebug.h"
37 # endif
38 #endif
39 #include "HsFFI.h"
40 #include "Linker.h"
41 #if defined(RTS_GTK_FRONTPANEL)
42 #include "FrontPanel.h"
43 #endif
44
45 #include "RetainerProfile.h"
46
47 #include <string.h>
48
49 /* STATIC OBJECT LIST.
50  *
51  * During GC:
52  * We maintain a linked list of static objects that are still live.
53  * The requirements for this list are:
54  *
55  *  - we need to scan the list while adding to it, in order to
56  *    scavenge all the static objects (in the same way that
57  *    breadth-first scavenging works for dynamic objects).
58  *
59  *  - we need to be able to tell whether an object is already on
60  *    the list, to break loops.
61  *
62  * Each static object has a "static link field", which we use for
63  * linking objects on to the list.  We use a stack-type list, consing
64  * objects on the front as they are added (this means that the
65  * scavenge phase is depth-first, not breadth-first, but that
66  * shouldn't matter).  
67  *
68  * A separate list is kept for objects that have been scavenged
69  * already - this is so that we can zero all the marks afterwards.
70  *
71  * An object is on the list if its static link field is non-zero; this
72  * means that we have to mark the end of the list with '1', not NULL.  
73  *
74  * Extra notes for generational GC:
75  *
76  * Each generation has a static object list associated with it.  When
77  * collecting generations up to N, we treat the static object lists
78  * from generations > N as roots.
79  *
80  * We build up a static object list while collecting generations 0..N,
81  * which is then appended to the static object list of generation N+1.
82  */
83 static StgClosure* static_objects;      // live static objects
84 StgClosure* scavenged_static_objects;   // static objects scavenged so far
85
86 /* N is the oldest generation being collected, where the generations
87  * are numbered starting at 0.  A major GC (indicated by the major_gc
88  * flag) is when we're collecting all generations.  We only attempt to
89  * deal with static objects and GC CAFs when doing a major GC.
90  */
91 static nat N;
92 static rtsBool major_gc;
93
94 /* Youngest generation that objects should be evacuated to in
95  * evacuate().  (Logically an argument to evacuate, but it's static
96  * a lot of the time so we optimise it into a global variable).
97  */
98 static nat evac_gen;
99
100 /* Weak pointers
101  */
102 StgWeak *old_weak_ptr_list; // also pending finaliser list
103
104 /* Which stage of processing various kinds of weak pointer are we at?
105  * (see traverse_weak_ptr_list() below for discussion).
106  */
107 typedef enum { WeakPtrs, WeakThreads, WeakDone } WeakStage;
108 static WeakStage weak_stage;
109
110 /* List of all threads during GC
111  */
112 static StgTSO *old_all_threads;
113 StgTSO *resurrected_threads;
114
115 /* Flag indicating failure to evacuate an object to the desired
116  * generation.
117  */
118 static rtsBool failed_to_evac;
119
120 /* Old to-space (used for two-space collector only)
121  */
122 static bdescr *old_to_blocks;
123
124 /* Data used for allocation area sizing.
125  */
126 static lnat new_blocks;          // blocks allocated during this GC 
127 static lnat g0s0_pcnt_kept = 30; // percentage of g0s0 live at last minor GC 
128
129 /* Used to avoid long recursion due to selector thunks
130  */
131 static lnat thunk_selector_depth = 0;
132 #define MAX_THUNK_SELECTOR_DEPTH 8
133
134 /* -----------------------------------------------------------------------------
135    Static function declarations
136    -------------------------------------------------------------------------- */
137
138 static bdescr *     gc_alloc_block          ( step *stp );
139 static void         mark_root               ( StgClosure **root );
140
141 // Use a register argument for evacuate, if available.
142 #if __GNUC__ >= 2
143 #define REGPARM1 __attribute__((regparm(1)))
144 #else
145 #define REGPARM1
146 #endif
147
148 REGPARM1 static StgClosure * evacuate (StgClosure *q);
149
150 static void         zero_static_object_list ( StgClosure* first_static );
151
152 static rtsBool      traverse_weak_ptr_list  ( void );
153 static void         mark_weak_ptr_list      ( StgWeak **list );
154
155 static StgClosure * eval_thunk_selector     ( nat field, StgSelector * p );
156
157
158 static void    scavenge                ( step * );
159 static void    scavenge_mark_stack     ( void );
160 static void    scavenge_stack          ( StgPtr p, StgPtr stack_end );
161 static rtsBool scavenge_one            ( StgPtr p );
162 static void    scavenge_large          ( step * );
163 static void    scavenge_static         ( void );
164 static void    scavenge_mutable_list   ( generation *g );
165
166 static void    scavenge_large_bitmap   ( StgPtr p, 
167                                          StgLargeBitmap *large_bitmap, 
168                                          nat size );
169
170 #if 0 && defined(DEBUG)
171 static void         gcCAFs                  ( void );
172 #endif
173
174 /* -----------------------------------------------------------------------------
175    inline functions etc. for dealing with the mark bitmap & stack.
176    -------------------------------------------------------------------------- */
177
178 #define MARK_STACK_BLOCKS 4
179
180 static bdescr *mark_stack_bdescr;
181 static StgPtr *mark_stack;
182 static StgPtr *mark_sp;
183 static StgPtr *mark_splim;
184
185 // Flag and pointers used for falling back to a linear scan when the
186 // mark stack overflows.
187 static rtsBool mark_stack_overflowed;
188 static bdescr *oldgen_scan_bd;
189 static StgPtr  oldgen_scan;
190
191 STATIC_INLINE rtsBool
192 mark_stack_empty(void)
193 {
194     return mark_sp == mark_stack;
195 }
196
197 STATIC_INLINE rtsBool
198 mark_stack_full(void)
199 {
200     return mark_sp >= mark_splim;
201 }
202
203 STATIC_INLINE void
204 reset_mark_stack(void)
205 {
206     mark_sp = mark_stack;
207 }
208
209 STATIC_INLINE void
210 push_mark_stack(StgPtr p)
211 {
212     *mark_sp++ = p;
213 }
214
215 STATIC_INLINE StgPtr
216 pop_mark_stack(void)
217 {
218     return *--mark_sp;
219 }
220
221 /* -----------------------------------------------------------------------------
222    Allocate a new to-space block in the given step.
223    -------------------------------------------------------------------------- */
224
225 static bdescr *
226 gc_alloc_block(step *stp)
227 {
228     bdescr *bd = allocBlock();
229     bd->gen_no = stp->gen_no;
230     bd->step = stp;
231     bd->link = NULL;
232
233     // blocks in to-space in generations up to and including N
234     // get the BF_EVACUATED flag.
235     if (stp->gen_no <= N) {
236         bd->flags = BF_EVACUATED;
237     } else {
238         bd->flags = 0;
239     }
240
241     // Start a new to-space block, chain it on after the previous one.
242     if (stp->hp_bd == NULL) {
243         stp->hp_bd = bd;
244     } else {
245         stp->hp_bd->free = stp->hp;
246         stp->hp_bd->link = bd;
247         stp->hp_bd = bd;
248     }
249
250     stp->hp    = bd->start;
251     stp->hpLim = stp->hp + BLOCK_SIZE_W;
252
253     stp->n_to_blocks++;
254     new_blocks++;
255
256     return bd;
257 }
258
259 /* -----------------------------------------------------------------------------
260    GarbageCollect
261
262    Rough outline of the algorithm: for garbage collecting generation N
263    (and all younger generations):
264
265      - follow all pointers in the root set.  the root set includes all 
266        mutable objects in all generations (mutable_list).
267
268      - for each pointer, evacuate the object it points to into either
269
270        + to-space of the step given by step->to, which is the next
271          highest step in this generation or the first step in the next
272          generation if this is the last step.
273
274        + to-space of generations[evac_gen]->steps[0], if evac_gen != 0.
275          When we evacuate an object we attempt to evacuate
276          everything it points to into the same generation - this is
277          achieved by setting evac_gen to the desired generation.  If
278          we can't do this, then an entry in the mut list has to
279          be made for the cross-generation pointer.
280
281        + if the object is already in a generation > N, then leave
282          it alone.
283
284      - repeatedly scavenge to-space from each step in each generation
285        being collected until no more objects can be evacuated.
286       
287      - free from-space in each step, and set from-space = to-space.
288
289    Locks held: sched_mutex
290
291    -------------------------------------------------------------------------- */
292
293 void
294 GarbageCollect ( void (*get_roots)(evac_fn), rtsBool force_major_gc )
295 {
296   bdescr *bd;
297   step *stp;
298   lnat live, allocated, collected = 0, copied = 0;
299   lnat oldgen_saved_blocks = 0;
300   nat g, s;
301
302 #ifdef PROFILING
303   CostCentreStack *prev_CCS;
304 #endif
305
306 #if defined(DEBUG) && defined(GRAN)
307   IF_DEBUG(gc, debugBelch("@@ Starting garbage collection at %ld (%lx)\n", 
308                      Now, Now));
309 #endif
310
311 #if defined(RTS_USER_SIGNALS)
312   // block signals
313   blockUserSignals();
314 #endif
315
316   // tell the STM to discard any cached closures its hoping to re-use
317   stmPreGCHook();
318
319   // tell the stats department that we've started a GC 
320   stat_startGC();
321
322   // Init stats and print par specific (timing) info 
323   PAR_TICKY_PAR_START();
324
325   // attribute any costs to CCS_GC 
326 #ifdef PROFILING
327   prev_CCS = CCCS;
328   CCCS = CCS_GC;
329 #endif
330
331   /* Approximate how much we allocated.  
332    * Todo: only when generating stats? 
333    */
334   allocated = calcAllocated();
335
336   /* Figure out which generation to collect
337    */
338   if (force_major_gc) {
339     N = RtsFlags.GcFlags.generations - 1;
340     major_gc = rtsTrue;
341   } else {
342     N = 0;
343     for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
344       if (generations[g].steps[0].n_blocks +
345           generations[g].steps[0].n_large_blocks
346           >= generations[g].max_blocks) {
347         N = g;
348       }
349     }
350     major_gc = (N == RtsFlags.GcFlags.generations-1);
351   }
352
353 #ifdef RTS_GTK_FRONTPANEL
354   if (RtsFlags.GcFlags.frontpanel) {
355       updateFrontPanelBeforeGC(N);
356   }
357 #endif
358
359   // check stack sanity *before* GC (ToDo: check all threads) 
360 #if defined(GRAN)
361   // ToDo!: check sanity  IF_DEBUG(sanity, checkTSOsSanity());
362 #endif
363   IF_DEBUG(sanity, checkFreeListSanity());
364
365   /* Initialise the static object lists
366    */
367   static_objects = END_OF_STATIC_LIST;
368   scavenged_static_objects = END_OF_STATIC_LIST;
369
370   /* Save the old to-space if we're doing a two-space collection
371    */
372   if (RtsFlags.GcFlags.generations == 1) {
373     old_to_blocks = g0s0->to_blocks;
374     g0s0->to_blocks = NULL;
375     g0s0->n_to_blocks = 0;
376   }
377
378   /* Keep a count of how many new blocks we allocated during this GC
379    * (used for resizing the allocation area, later).
380    */
381   new_blocks = 0;
382
383   // Initialise to-space in all the generations/steps that we're
384   // collecting.
385   //
386   for (g = 0; g <= N; g++) {
387
388     // throw away the mutable list.  Invariant: the mutable list
389     // always has at least one block; this means we can avoid a check for
390     // NULL in recordMutable().
391     if (g != 0) {
392         freeChain(generations[g].mut_list);
393         generations[g].mut_list = allocBlock();
394     }
395
396     for (s = 0; s < generations[g].n_steps; s++) {
397
398       // generation 0, step 0 doesn't need to-space 
399       if (g == 0 && s == 0 && RtsFlags.GcFlags.generations > 1) { 
400         continue; 
401       }
402
403       stp = &generations[g].steps[s];
404       ASSERT(stp->gen_no == g);
405
406       // start a new to-space for this step.
407       stp->hp        = NULL;
408       stp->hp_bd     = NULL;
409       stp->to_blocks = NULL;
410
411       // allocate the first to-space block; extra blocks will be
412       // chained on as necessary.
413       bd = gc_alloc_block(stp);
414       stp->to_blocks   = bd;
415       stp->scan        = bd->start;
416       stp->scan_bd     = bd;
417
418       // initialise the large object queues.
419       stp->new_large_objects = NULL;
420       stp->scavenged_large_objects = NULL;
421       stp->n_scavenged_large_blocks = 0;
422
423       // mark the large objects as not evacuated yet 
424       for (bd = stp->large_objects; bd; bd = bd->link) {
425         bd->flags &= ~BF_EVACUATED;
426       }
427
428       // for a compacted step, we need to allocate the bitmap
429       if (stp->is_compacted) {
430           nat bitmap_size; // in bytes
431           bdescr *bitmap_bdescr;
432           StgWord *bitmap;
433
434           bitmap_size = stp->n_blocks * BLOCK_SIZE / (sizeof(W_)*BITS_PER_BYTE);
435
436           if (bitmap_size > 0) {
437               bitmap_bdescr = allocGroup((nat)BLOCK_ROUND_UP(bitmap_size) 
438                                          / BLOCK_SIZE);
439               stp->bitmap = bitmap_bdescr;
440               bitmap = bitmap_bdescr->start;
441               
442               IF_DEBUG(gc, debugBelch("bitmap_size: %d, bitmap: %p",
443                                    bitmap_size, bitmap););
444               
445               // don't forget to fill it with zeros!
446               memset(bitmap, 0, bitmap_size);
447               
448               // For each block in this step, point to its bitmap from the
449               // block descriptor.
450               for (bd=stp->blocks; bd != NULL; bd = bd->link) {
451                   bd->u.bitmap = bitmap;
452                   bitmap += BLOCK_SIZE_W / (sizeof(W_)*BITS_PER_BYTE);
453
454                   // Also at this point we set the BF_COMPACTED flag
455                   // for this block.  The invariant is that
456                   // BF_COMPACTED is always unset, except during GC
457                   // when it is set on those blocks which will be
458                   // compacted.
459                   bd->flags |= BF_COMPACTED;
460               }
461           }
462       }
463     }
464   }
465
466   /* make sure the older generations have at least one block to
467    * allocate into (this makes things easier for copy(), see below).
468    */
469   for (g = N+1; g < RtsFlags.GcFlags.generations; g++) {
470     for (s = 0; s < generations[g].n_steps; s++) {
471       stp = &generations[g].steps[s];
472       if (stp->hp_bd == NULL) {
473           ASSERT(stp->blocks == NULL);
474           bd = gc_alloc_block(stp);
475           stp->blocks = bd;
476           stp->n_blocks = 1;
477       }
478       /* Set the scan pointer for older generations: remember we
479        * still have to scavenge objects that have been promoted. */
480       stp->scan = stp->hp;
481       stp->scan_bd = stp->hp_bd;
482       stp->to_blocks = NULL;
483       stp->n_to_blocks = 0;
484       stp->new_large_objects = NULL;
485       stp->scavenged_large_objects = NULL;
486       stp->n_scavenged_large_blocks = 0;
487     }
488   }
489
490   /* Allocate a mark stack if we're doing a major collection.
491    */
492   if (major_gc) {
493       mark_stack_bdescr = allocGroup(MARK_STACK_BLOCKS);
494       mark_stack = (StgPtr *)mark_stack_bdescr->start;
495       mark_sp    = mark_stack;
496       mark_splim = mark_stack + (MARK_STACK_BLOCKS * BLOCK_SIZE_W);
497   } else {
498       mark_stack_bdescr = NULL;
499   }
500
501   /* -----------------------------------------------------------------------
502    * follow all the roots that we know about:
503    *   - mutable lists from each generation > N
504    * we want to *scavenge* these roots, not evacuate them: they're not
505    * going to move in this GC.
506    * Also: do them in reverse generation order.  This is because we
507    * often want to promote objects that are pointed to by older
508    * generations early, so we don't have to repeatedly copy them.
509    * Doing the generations in reverse order ensures that we don't end
510    * up in the situation where we want to evac an object to gen 3 and
511    * it has already been evaced to gen 2.
512    */
513   { 
514     int st;
515     for (g = RtsFlags.GcFlags.generations-1; g > N; g--) {
516       generations[g].saved_mut_list = generations[g].mut_list;
517       generations[g].mut_list = allocBlock(); 
518         // mut_list always has at least one block.
519     }
520
521     for (g = RtsFlags.GcFlags.generations-1; g > N; g--) {
522       IF_PAR_DEBUG(verbose, printMutableList(&generations[g]));
523       scavenge_mutable_list(&generations[g]);
524       evac_gen = g;
525       for (st = generations[g].n_steps-1; st >= 0; st--) {
526         scavenge(&generations[g].steps[st]);
527       }
528     }
529   }
530
531   /* follow roots from the CAF list (used by GHCi)
532    */
533   evac_gen = 0;
534   markCAFs(mark_root);
535
536   /* follow all the roots that the application knows about.
537    */
538   evac_gen = 0;
539   get_roots(mark_root);
540
541 #if defined(PAR)
542   /* And don't forget to mark the TSO if we got here direct from
543    * Haskell! */
544   /* Not needed in a seq version?
545   if (CurrentTSO) {
546     CurrentTSO = (StgTSO *)MarkRoot((StgClosure *)CurrentTSO);
547   }
548   */
549
550   // Mark the entries in the GALA table of the parallel system 
551   markLocalGAs(major_gc);
552   // Mark all entries on the list of pending fetches 
553   markPendingFetches(major_gc);
554 #endif
555
556   /* Mark the weak pointer list, and prepare to detect dead weak
557    * pointers.
558    */
559   mark_weak_ptr_list(&weak_ptr_list);
560   old_weak_ptr_list = weak_ptr_list;
561   weak_ptr_list = NULL;
562   weak_stage = WeakPtrs;
563
564   /* The all_threads list is like the weak_ptr_list.  
565    * See traverse_weak_ptr_list() for the details.
566    */
567   old_all_threads = all_threads;
568   all_threads = END_TSO_QUEUE;
569   resurrected_threads = END_TSO_QUEUE;
570
571   /* Mark the stable pointer table.
572    */
573   markStablePtrTable(mark_root);
574
575   /* -------------------------------------------------------------------------
576    * Repeatedly scavenge all the areas we know about until there's no
577    * more scavenging to be done.
578    */
579   { 
580     rtsBool flag;
581   loop:
582     flag = rtsFalse;
583
584     // scavenge static objects 
585     if (major_gc && static_objects != END_OF_STATIC_LIST) {
586         IF_DEBUG(sanity, checkStaticObjects(static_objects));
587         scavenge_static();
588     }
589
590     /* When scavenging the older generations:  Objects may have been
591      * evacuated from generations <= N into older generations, and we
592      * need to scavenge these objects.  We're going to try to ensure that
593      * any evacuations that occur move the objects into at least the
594      * same generation as the object being scavenged, otherwise we
595      * have to create new entries on the mutable list for the older
596      * generation.
597      */
598
599     // scavenge each step in generations 0..maxgen 
600     { 
601       long gen;
602       int st; 
603
604     loop2:
605       // scavenge objects in compacted generation
606       if (mark_stack_overflowed || oldgen_scan_bd != NULL ||
607           (mark_stack_bdescr != NULL && !mark_stack_empty())) {
608           scavenge_mark_stack();
609           flag = rtsTrue;
610       }
611
612       for (gen = RtsFlags.GcFlags.generations; --gen >= 0; ) {
613         for (st = generations[gen].n_steps; --st >= 0; ) {
614           if (gen == 0 && st == 0 && RtsFlags.GcFlags.generations > 1) { 
615             continue; 
616           }
617           stp = &generations[gen].steps[st];
618           evac_gen = gen;
619           if (stp->hp_bd != stp->scan_bd || stp->scan < stp->hp) {
620             scavenge(stp);
621             flag = rtsTrue;
622             goto loop2;
623           }
624           if (stp->new_large_objects != NULL) {
625             scavenge_large(stp);
626             flag = rtsTrue;
627             goto loop2;
628           }
629         }
630       }
631     }
632
633     if (flag) { goto loop; }
634
635     // must be last...  invariant is that everything is fully
636     // scavenged at this point.
637     if (traverse_weak_ptr_list()) { // returns rtsTrue if evaced something 
638       goto loop;
639     }
640   }
641
642   /* Update the pointers from the "main thread" list - these are
643    * treated as weak pointers because we want to allow a main thread
644    * to get a BlockedOnDeadMVar exception in the same way as any other
645    * thread.  Note that the threads should all have been retained by
646    * GC by virtue of being on the all_threads list, we're just
647    * updating pointers here.
648    */
649   {
650       StgMainThread *m;
651       StgTSO *tso;
652       for (m = main_threads; m != NULL; m = m->link) {
653           tso = (StgTSO *) isAlive((StgClosure *)m->tso);
654           if (tso == NULL) {
655               barf("main thread has been GC'd");
656           }
657           m->tso = tso;
658       }
659   }
660
661 #if defined(PAR)
662   // Reconstruct the Global Address tables used in GUM 
663   rebuildGAtables(major_gc);
664   IF_DEBUG(sanity, checkLAGAtable(rtsTrue/*check closures, too*/));
665 #endif
666
667   // Now see which stable names are still alive.
668   gcStablePtrTable();
669
670   // Tidy the end of the to-space chains 
671   for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
672       for (s = 0; s < generations[g].n_steps; s++) {
673           stp = &generations[g].steps[s];
674           if (!(g == 0 && s == 0 && RtsFlags.GcFlags.generations > 1)) {
675               ASSERT(Bdescr(stp->hp) == stp->hp_bd);
676               stp->hp_bd->free = stp->hp;
677           }
678       }
679   }
680
681 #ifdef PROFILING
682   // We call processHeapClosureForDead() on every closure destroyed during
683   // the current garbage collection, so we invoke LdvCensusForDead().
684   if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_LDV
685       || RtsFlags.ProfFlags.bioSelector != NULL)
686     LdvCensusForDead(N);
687 #endif
688
689   // NO MORE EVACUATION AFTER THIS POINT!
690   // Finally: compaction of the oldest generation.
691   if (major_gc && oldest_gen->steps[0].is_compacted) {
692       // save number of blocks for stats
693       oldgen_saved_blocks = oldest_gen->steps[0].n_blocks;
694       compact(get_roots);
695   }
696
697   IF_DEBUG(sanity, checkGlobalTSOList(rtsFalse));
698
699   /* run through all the generations/steps and tidy up 
700    */
701   copied = new_blocks * BLOCK_SIZE_W;
702   for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
703
704     if (g <= N) {
705       generations[g].collections++; // for stats 
706     }
707
708     // Count the mutable list as bytes "copied" for the purposes of
709     // stats.  Every mutable list is copied during every GC.
710     if (g > 0) {
711         for (bd = generations[g].mut_list; bd != NULL; bd = bd->link) {
712             copied += (bd->free - bd->start) * sizeof(StgWord);
713         }
714     }
715
716     for (s = 0; s < generations[g].n_steps; s++) {
717       bdescr *next;
718       stp = &generations[g].steps[s];
719
720       if (!(g == 0 && s == 0 && RtsFlags.GcFlags.generations > 1)) {
721         // stats information: how much we copied 
722         if (g <= N) {
723           copied -= stp->hp_bd->start + BLOCK_SIZE_W -
724             stp->hp_bd->free;
725         }
726       }
727
728       // for generations we collected... 
729       if (g <= N) {
730
731           // rough calculation of garbage collected, for stats output
732           if (stp->is_compacted) {
733               collected += (oldgen_saved_blocks - stp->n_blocks) * BLOCK_SIZE_W;
734           } else {
735               collected += stp->n_blocks * BLOCK_SIZE_W;
736           }
737
738         /* free old memory and shift to-space into from-space for all
739          * the collected steps (except the allocation area).  These
740          * freed blocks will probaby be quickly recycled.
741          */
742         if (!(g == 0 && s == 0)) {
743             if (stp->is_compacted) {
744                 // for a compacted step, just shift the new to-space
745                 // onto the front of the now-compacted existing blocks.
746                 for (bd = stp->to_blocks; bd != NULL; bd = bd->link) {
747                     bd->flags &= ~BF_EVACUATED;  // now from-space 
748                 }
749                 // tack the new blocks on the end of the existing blocks
750                 if (stp->blocks == NULL) {
751                     stp->blocks = stp->to_blocks;
752                 } else {
753                     for (bd = stp->blocks; bd != NULL; bd = next) {
754                         next = bd->link;
755                         if (next == NULL) {
756                             bd->link = stp->to_blocks;
757                         }
758                         // NB. this step might not be compacted next
759                         // time, so reset the BF_COMPACTED flags.
760                         // They are set before GC if we're going to
761                         // compact.  (search for BF_COMPACTED above).
762                         bd->flags &= ~BF_COMPACTED;
763                     }
764                 }
765                 // add the new blocks to the block tally
766                 stp->n_blocks += stp->n_to_blocks;
767             } else {
768                 freeChain(stp->blocks);
769                 stp->blocks = stp->to_blocks;
770                 stp->n_blocks = stp->n_to_blocks;
771                 for (bd = stp->blocks; bd != NULL; bd = bd->link) {
772                     bd->flags &= ~BF_EVACUATED;  // now from-space 
773                 }
774             }
775             stp->to_blocks = NULL;
776             stp->n_to_blocks = 0;
777         }
778
779         /* LARGE OBJECTS.  The current live large objects are chained on
780          * scavenged_large, having been moved during garbage
781          * collection from large_objects.  Any objects left on
782          * large_objects list are therefore dead, so we free them here.
783          */
784         for (bd = stp->large_objects; bd != NULL; bd = next) {
785           next = bd->link;
786           freeGroup(bd);
787           bd = next;
788         }
789
790         // update the count of blocks used by large objects
791         for (bd = stp->scavenged_large_objects; bd != NULL; bd = bd->link) {
792           bd->flags &= ~BF_EVACUATED;
793         }
794         stp->large_objects  = stp->scavenged_large_objects;
795         stp->n_large_blocks = stp->n_scavenged_large_blocks;
796
797       } else {
798         // for older generations... 
799         
800         /* For older generations, we need to append the
801          * scavenged_large_object list (i.e. large objects that have been
802          * promoted during this GC) to the large_object list for that step.
803          */
804         for (bd = stp->scavenged_large_objects; bd; bd = next) {
805           next = bd->link;
806           bd->flags &= ~BF_EVACUATED;
807           dbl_link_onto(bd, &stp->large_objects);
808         }
809
810         // add the new blocks we promoted during this GC 
811         stp->n_blocks += stp->n_to_blocks;
812         stp->n_to_blocks = 0;
813         stp->n_large_blocks += stp->n_scavenged_large_blocks;
814       }
815     }
816   }
817
818   /* Reset the sizes of the older generations when we do a major
819    * collection.
820    *
821    * CURRENT STRATEGY: make all generations except zero the same size.
822    * We have to stay within the maximum heap size, and leave a certain
823    * percentage of the maximum heap size available to allocate into.
824    */
825   if (major_gc && RtsFlags.GcFlags.generations > 1) {
826       nat live, size, min_alloc;
827       nat max  = RtsFlags.GcFlags.maxHeapSize;
828       nat gens = RtsFlags.GcFlags.generations;
829
830       // live in the oldest generations
831       live = oldest_gen->steps[0].n_blocks +
832              oldest_gen->steps[0].n_large_blocks;
833
834       // default max size for all generations except zero
835       size = stg_max(live * RtsFlags.GcFlags.oldGenFactor,
836                      RtsFlags.GcFlags.minOldGenSize);
837
838       // minimum size for generation zero
839       min_alloc = stg_max((RtsFlags.GcFlags.pcFreeHeap * max) / 200,
840                           RtsFlags.GcFlags.minAllocAreaSize);
841
842       // Auto-enable compaction when the residency reaches a
843       // certain percentage of the maximum heap size (default: 30%).
844       if (RtsFlags.GcFlags.generations > 1 &&
845           (RtsFlags.GcFlags.compact ||
846            (max > 0 &&
847             oldest_gen->steps[0].n_blocks > 
848             (RtsFlags.GcFlags.compactThreshold * max) / 100))) {
849           oldest_gen->steps[0].is_compacted = 1;
850 //        debugBelch("compaction: on\n", live);
851       } else {
852           oldest_gen->steps[0].is_compacted = 0;
853 //        debugBelch("compaction: off\n", live);
854       }
855
856       // if we're going to go over the maximum heap size, reduce the
857       // size of the generations accordingly.  The calculation is
858       // different if compaction is turned on, because we don't need
859       // to double the space required to collect the old generation.
860       if (max != 0) {
861
862           // this test is necessary to ensure that the calculations
863           // below don't have any negative results - we're working
864           // with unsigned values here.
865           if (max < min_alloc) {
866               heapOverflow();
867           }
868
869           if (oldest_gen->steps[0].is_compacted) {
870               if ( (size + (size - 1) * (gens - 2) * 2) + min_alloc > max ) {
871                   size = (max - min_alloc) / ((gens - 1) * 2 - 1);
872               }
873           } else {
874               if ( (size * (gens - 1) * 2) + min_alloc > max ) {
875                   size = (max - min_alloc) / ((gens - 1) * 2);
876               }
877           }
878
879           if (size < live) {
880               heapOverflow();
881           }
882       }
883
884 #if 0
885       debugBelch("live: %d, min_alloc: %d, size : %d, max = %d\n", live,
886               min_alloc, size, max);
887 #endif
888
889       for (g = 0; g < gens; g++) {
890           generations[g].max_blocks = size;
891       }
892   }
893
894   // Guess the amount of live data for stats.
895   live = calcLive();
896
897   /* Free the small objects allocated via allocate(), since this will
898    * all have been copied into G0S1 now.  
899    */
900   if (small_alloc_list != NULL) {
901     freeChain(small_alloc_list);
902   }
903   small_alloc_list = NULL;
904   alloc_blocks = 0;
905   alloc_Hp = NULL;
906   alloc_HpLim = NULL;
907   alloc_blocks_lim = RtsFlags.GcFlags.minAllocAreaSize;
908
909   // Start a new pinned_object_block
910   pinned_object_block = NULL;
911
912   /* Free the mark stack.
913    */
914   if (mark_stack_bdescr != NULL) {
915       freeGroup(mark_stack_bdescr);
916   }
917
918   /* Free any bitmaps.
919    */
920   for (g = 0; g <= N; g++) {
921       for (s = 0; s < generations[g].n_steps; s++) {
922           stp = &generations[g].steps[s];
923           if (stp->is_compacted && stp->bitmap != NULL) {
924               freeGroup(stp->bitmap);
925           }
926       }
927   }
928
929   /* Two-space collector:
930    * Free the old to-space, and estimate the amount of live data.
931    */
932   if (RtsFlags.GcFlags.generations == 1) {
933     nat blocks;
934     
935     if (old_to_blocks != NULL) {
936       freeChain(old_to_blocks);
937     }
938     for (bd = g0s0->to_blocks; bd != NULL; bd = bd->link) {
939       bd->flags = 0;    // now from-space 
940     }
941
942     /* For a two-space collector, we need to resize the nursery. */
943     
944     /* set up a new nursery.  Allocate a nursery size based on a
945      * function of the amount of live data (by default a factor of 2)
946      * Use the blocks from the old nursery if possible, freeing up any
947      * left over blocks.
948      *
949      * If we get near the maximum heap size, then adjust our nursery
950      * size accordingly.  If the nursery is the same size as the live
951      * data (L), then we need 3L bytes.  We can reduce the size of the
952      * nursery to bring the required memory down near 2L bytes.
953      * 
954      * A normal 2-space collector would need 4L bytes to give the same
955      * performance we get from 3L bytes, reducing to the same
956      * performance at 2L bytes.
957      */
958     blocks = g0s0->n_to_blocks;
959
960     if ( RtsFlags.GcFlags.maxHeapSize != 0 &&
961          blocks * RtsFlags.GcFlags.oldGenFactor * 2 > 
962            RtsFlags.GcFlags.maxHeapSize ) {
963       long adjusted_blocks;  // signed on purpose 
964       int pc_free; 
965       
966       adjusted_blocks = (RtsFlags.GcFlags.maxHeapSize - 2 * blocks);
967       IF_DEBUG(gc, debugBelch("@@ Near maximum heap size of 0x%x blocks, blocks = %d, adjusted to %ld", RtsFlags.GcFlags.maxHeapSize, blocks, adjusted_blocks));
968       pc_free = adjusted_blocks * 100 / RtsFlags.GcFlags.maxHeapSize;
969       if (pc_free < RtsFlags.GcFlags.pcFreeHeap) /* might even be < 0 */ {
970         heapOverflow();
971       }
972       blocks = adjusted_blocks;
973       
974     } else {
975       blocks *= RtsFlags.GcFlags.oldGenFactor;
976       if (blocks < RtsFlags.GcFlags.minAllocAreaSize) {
977         blocks = RtsFlags.GcFlags.minAllocAreaSize;
978       }
979     }
980     resizeNursery(blocks);
981     
982   } else {
983     /* Generational collector:
984      * If the user has given us a suggested heap size, adjust our
985      * allocation area to make best use of the memory available.
986      */
987
988     if (RtsFlags.GcFlags.heapSizeSuggestion) {
989       long blocks;
990       nat needed = calcNeeded();        // approx blocks needed at next GC 
991
992       /* Guess how much will be live in generation 0 step 0 next time.
993        * A good approximation is obtained by finding the
994        * percentage of g0s0 that was live at the last minor GC.
995        */
996       if (N == 0) {
997         g0s0_pcnt_kept = (new_blocks * 100) / g0s0->n_blocks;
998       }
999
1000       /* Estimate a size for the allocation area based on the
1001        * information available.  We might end up going slightly under
1002        * or over the suggested heap size, but we should be pretty
1003        * close on average.
1004        *
1005        * Formula:            suggested - needed
1006        *                ----------------------------
1007        *                    1 + g0s0_pcnt_kept/100
1008        *
1009        * where 'needed' is the amount of memory needed at the next
1010        * collection for collecting all steps except g0s0.
1011        */
1012       blocks = 
1013         (((long)RtsFlags.GcFlags.heapSizeSuggestion - (long)needed) * 100) /
1014         (100 + (long)g0s0_pcnt_kept);
1015       
1016       if (blocks < (long)RtsFlags.GcFlags.minAllocAreaSize) {
1017         blocks = RtsFlags.GcFlags.minAllocAreaSize;
1018       }
1019       
1020       resizeNursery((nat)blocks);
1021
1022     } else {
1023       // we might have added extra large blocks to the nursery, so
1024       // resize back to minAllocAreaSize again.
1025       resizeNursery(RtsFlags.GcFlags.minAllocAreaSize);
1026     }
1027   }
1028
1029  // mark the garbage collected CAFs as dead 
1030 #if 0 && defined(DEBUG) // doesn't work at the moment 
1031   if (major_gc) { gcCAFs(); }
1032 #endif
1033   
1034 #ifdef PROFILING
1035   // resetStaticObjectForRetainerProfiling() must be called before
1036   // zeroing below.
1037   resetStaticObjectForRetainerProfiling();
1038 #endif
1039
1040   // zero the scavenged static object list 
1041   if (major_gc) {
1042     zero_static_object_list(scavenged_static_objects);
1043   }
1044
1045   // Reset the nursery
1046   resetNurseries();
1047
1048   RELEASE_LOCK(&sched_mutex);
1049   
1050   // start any pending finalizers 
1051   scheduleFinalizers(old_weak_ptr_list);
1052   
1053   // send exceptions to any threads which were about to die 
1054   resurrectThreads(resurrected_threads);
1055   
1056   ACQUIRE_LOCK(&sched_mutex);
1057
1058   // Update the stable pointer hash table.
1059   updateStablePtrTable(major_gc);
1060
1061   // check sanity after GC 
1062   IF_DEBUG(sanity, checkSanity());
1063
1064   // extra GC trace info 
1065   IF_DEBUG(gc, statDescribeGens());
1066
1067 #ifdef DEBUG
1068   // symbol-table based profiling 
1069   /*  heapCensus(to_blocks); */ /* ToDo */
1070 #endif
1071
1072   // restore enclosing cost centre 
1073 #ifdef PROFILING
1074   CCCS = prev_CCS;
1075 #endif
1076
1077   // check for memory leaks if sanity checking is on 
1078   IF_DEBUG(sanity, memInventory());
1079
1080 #ifdef RTS_GTK_FRONTPANEL
1081   if (RtsFlags.GcFlags.frontpanel) {
1082       updateFrontPanelAfterGC( N, live );
1083   }
1084 #endif
1085
1086   // ok, GC over: tell the stats department what happened. 
1087   stat_endGC(allocated, collected, live, copied, N);
1088
1089 #if defined(RTS_USER_SIGNALS)
1090   // unblock signals again
1091   unblockUserSignals();
1092 #endif
1093
1094   //PAR_TICKY_TP();
1095 }
1096
1097
1098 /* -----------------------------------------------------------------------------
1099    Weak Pointers
1100
1101    traverse_weak_ptr_list is called possibly many times during garbage
1102    collection.  It returns a flag indicating whether it did any work
1103    (i.e. called evacuate on any live pointers).
1104
1105    Invariant: traverse_weak_ptr_list is called when the heap is in an
1106    idempotent state.  That means that there are no pending
1107    evacuate/scavenge operations.  This invariant helps the weak
1108    pointer code decide which weak pointers are dead - if there are no
1109    new live weak pointers, then all the currently unreachable ones are
1110    dead.
1111
1112    For generational GC: we just don't try to finalize weak pointers in
1113    older generations than the one we're collecting.  This could
1114    probably be optimised by keeping per-generation lists of weak
1115    pointers, but for a few weak pointers this scheme will work.
1116
1117    There are three distinct stages to processing weak pointers:
1118
1119    - weak_stage == WeakPtrs
1120
1121      We process all the weak pointers whos keys are alive (evacuate
1122      their values and finalizers), and repeat until we can find no new
1123      live keys.  If no live keys are found in this pass, then we
1124      evacuate the finalizers of all the dead weak pointers in order to
1125      run them.
1126
1127    - weak_stage == WeakThreads
1128
1129      Now, we discover which *threads* are still alive.  Pointers to
1130      threads from the all_threads and main thread lists are the
1131      weakest of all: a pointers from the finalizer of a dead weak
1132      pointer can keep a thread alive.  Any threads found to be unreachable
1133      are evacuated and placed on the resurrected_threads list so we 
1134      can send them a signal later.
1135
1136    - weak_stage == WeakDone
1137
1138      No more evacuation is done.
1139
1140    -------------------------------------------------------------------------- */
1141
1142 static rtsBool 
1143 traverse_weak_ptr_list(void)
1144 {
1145   StgWeak *w, **last_w, *next_w;
1146   StgClosure *new;
1147   rtsBool flag = rtsFalse;
1148
1149   switch (weak_stage) {
1150
1151   case WeakDone:
1152       return rtsFalse;
1153
1154   case WeakPtrs:
1155       /* doesn't matter where we evacuate values/finalizers to, since
1156        * these pointers are treated as roots (iff the keys are alive).
1157        */
1158       evac_gen = 0;
1159       
1160       last_w = &old_weak_ptr_list;
1161       for (w = old_weak_ptr_list; w != NULL; w = next_w) {
1162           
1163           /* There might be a DEAD_WEAK on the list if finalizeWeak# was
1164            * called on a live weak pointer object.  Just remove it.
1165            */
1166           if (w->header.info == &stg_DEAD_WEAK_info) {
1167               next_w = ((StgDeadWeak *)w)->link;
1168               *last_w = next_w;
1169               continue;
1170           }
1171           
1172           switch (get_itbl(w)->type) {
1173
1174           case EVACUATED:
1175               next_w = (StgWeak *)((StgEvacuated *)w)->evacuee;
1176               *last_w = next_w;
1177               continue;
1178
1179           case WEAK:
1180               /* Now, check whether the key is reachable.
1181                */
1182               new = isAlive(w->key);
1183               if (new != NULL) {
1184                   w->key = new;
1185                   // evacuate the value and finalizer 
1186                   w->value = evacuate(w->value);
1187                   w->finalizer = evacuate(w->finalizer);
1188                   // remove this weak ptr from the old_weak_ptr list 
1189                   *last_w = w->link;
1190                   // and put it on the new weak ptr list 
1191                   next_w  = w->link;
1192                   w->link = weak_ptr_list;
1193                   weak_ptr_list = w;
1194                   flag = rtsTrue;
1195                   IF_DEBUG(weak, debugBelch("Weak pointer still alive at %p -> %p", 
1196                                        w, w->key));
1197                   continue;
1198               }
1199               else {
1200                   last_w = &(w->link);
1201                   next_w = w->link;
1202                   continue;
1203               }
1204
1205           default:
1206               barf("traverse_weak_ptr_list: not WEAK");
1207           }
1208       }
1209       
1210       /* If we didn't make any changes, then we can go round and kill all
1211        * the dead weak pointers.  The old_weak_ptr list is used as a list
1212        * of pending finalizers later on.
1213        */
1214       if (flag == rtsFalse) {
1215           for (w = old_weak_ptr_list; w; w = w->link) {
1216               w->finalizer = evacuate(w->finalizer);
1217           }
1218
1219           // Next, move to the WeakThreads stage after fully
1220           // scavenging the finalizers we've just evacuated.
1221           weak_stage = WeakThreads;
1222       }
1223
1224       return rtsTrue;
1225
1226   case WeakThreads:
1227       /* Now deal with the all_threads list, which behaves somewhat like
1228        * the weak ptr list.  If we discover any threads that are about to
1229        * become garbage, we wake them up and administer an exception.
1230        */
1231       {
1232           StgTSO *t, *tmp, *next, **prev;
1233           
1234           prev = &old_all_threads;
1235           for (t = old_all_threads; t != END_TSO_QUEUE; t = next) {
1236               
1237               tmp = (StgTSO *)isAlive((StgClosure *)t);
1238               
1239               if (tmp != NULL) {
1240                   t = tmp;
1241               }
1242               
1243               ASSERT(get_itbl(t)->type == TSO);
1244               switch (t->what_next) {
1245               case ThreadRelocated:
1246                   next = t->link;
1247                   *prev = next;
1248                   continue;
1249               case ThreadKilled:
1250               case ThreadComplete:
1251                   // finshed or died.  The thread might still be alive, but we
1252                   // don't keep it on the all_threads list.  Don't forget to
1253                   // stub out its global_link field.
1254                   next = t->global_link;
1255                   t->global_link = END_TSO_QUEUE;
1256                   *prev = next;
1257                   continue;
1258               default:
1259                   ;
1260               }
1261               
1262               if (tmp == NULL) {
1263                   // not alive (yet): leave this thread on the
1264                   // old_all_threads list.
1265                   prev = &(t->global_link);
1266                   next = t->global_link;
1267               } 
1268               else {
1269                   // alive: move this thread onto the all_threads list.
1270                   next = t->global_link;
1271                   t->global_link = all_threads;
1272                   all_threads  = t;
1273                   *prev = next;
1274               }
1275           }
1276       }
1277       
1278       /* And resurrect any threads which were about to become garbage.
1279        */
1280       {
1281           StgTSO *t, *tmp, *next;
1282           for (t = old_all_threads; t != END_TSO_QUEUE; t = next) {
1283               next = t->global_link;
1284               tmp = (StgTSO *)evacuate((StgClosure *)t);
1285               tmp->global_link = resurrected_threads;
1286               resurrected_threads = tmp;
1287           }
1288       }
1289       
1290       weak_stage = WeakDone;  // *now* we're done,
1291       return rtsTrue;         // but one more round of scavenging, please
1292
1293   default:
1294       barf("traverse_weak_ptr_list");
1295       return rtsTrue;
1296   }
1297
1298 }
1299
1300 /* -----------------------------------------------------------------------------
1301    After GC, the live weak pointer list may have forwarding pointers
1302    on it, because a weak pointer object was evacuated after being
1303    moved to the live weak pointer list.  We remove those forwarding
1304    pointers here.
1305
1306    Also, we don't consider weak pointer objects to be reachable, but
1307    we must nevertheless consider them to be "live" and retain them.
1308    Therefore any weak pointer objects which haven't as yet been
1309    evacuated need to be evacuated now.
1310    -------------------------------------------------------------------------- */
1311
1312
1313 static void
1314 mark_weak_ptr_list ( StgWeak **list )
1315 {
1316   StgWeak *w, **last_w;
1317
1318   last_w = list;
1319   for (w = *list; w; w = w->link) {
1320       // w might be WEAK, EVACUATED, or DEAD_WEAK (actually CON_STATIC) here
1321       ASSERT(w->header.info == &stg_DEAD_WEAK_info 
1322              || get_itbl(w)->type == WEAK || get_itbl(w)->type == EVACUATED);
1323       w = (StgWeak *)evacuate((StgClosure *)w);
1324       *last_w = w;
1325       last_w = &(w->link);
1326   }
1327 }
1328
1329 /* -----------------------------------------------------------------------------
1330    isAlive determines whether the given closure is still alive (after
1331    a garbage collection) or not.  It returns the new address of the
1332    closure if it is alive, or NULL otherwise.
1333
1334    NOTE: Use it before compaction only!
1335    -------------------------------------------------------------------------- */
1336
1337
1338 StgClosure *
1339 isAlive(StgClosure *p)
1340 {
1341   const StgInfoTable *info;
1342   bdescr *bd;
1343
1344   while (1) {
1345
1346     ASSERT(LOOKS_LIKE_CLOSURE_PTR(p));
1347     info = get_itbl(p);
1348
1349     // ignore static closures 
1350     //
1351     // ToDo: for static closures, check the static link field.
1352     // Problem here is that we sometimes don't set the link field, eg.
1353     // for static closures with an empty SRT or CONSTR_STATIC_NOCAFs.
1354     //
1355     if (!HEAP_ALLOCED(p)) {
1356         return p;
1357     }
1358
1359     // ignore closures in generations that we're not collecting. 
1360     bd = Bdescr((P_)p);
1361     if (bd->gen_no > N) {
1362         return p;
1363     }
1364
1365     // if it's a pointer into to-space, then we're done
1366     if (bd->flags & BF_EVACUATED) {
1367         return p;
1368     }
1369
1370     // large objects use the evacuated flag
1371     if (bd->flags & BF_LARGE) {
1372         return NULL;
1373     }
1374
1375     // check the mark bit for compacted steps
1376     if ((bd->flags & BF_COMPACTED) && is_marked((P_)p,bd)) {
1377         return p;
1378     }
1379
1380     switch (info->type) {
1381
1382     case IND:
1383     case IND_STATIC:
1384     case IND_PERM:
1385     case IND_OLDGEN:            // rely on compatible layout with StgInd 
1386     case IND_OLDGEN_PERM:
1387       // follow indirections 
1388       p = ((StgInd *)p)->indirectee;
1389       continue;
1390
1391     case EVACUATED:
1392       // alive! 
1393       return ((StgEvacuated *)p)->evacuee;
1394
1395     case TSO:
1396       if (((StgTSO *)p)->what_next == ThreadRelocated) {
1397         p = (StgClosure *)((StgTSO *)p)->link;
1398         continue;
1399       } 
1400       return NULL;
1401
1402     default:
1403       // dead. 
1404       return NULL;
1405     }
1406   }
1407 }
1408
1409 static void
1410 mark_root(StgClosure **root)
1411 {
1412   *root = evacuate(*root);
1413 }
1414
1415 STATIC_INLINE void 
1416 upd_evacuee(StgClosure *p, StgClosure *dest)
1417 {
1418     // not true: (ToDo: perhaps it should be)
1419     // ASSERT(Bdescr((P_)dest)->flags & BF_EVACUATED);
1420     SET_INFO(p, &stg_EVACUATED_info);
1421     ((StgEvacuated *)p)->evacuee = dest;
1422 }
1423
1424
1425 STATIC_INLINE StgClosure *
1426 copy(StgClosure *src, nat size, step *stp)
1427 {
1428   P_ to, from, dest;
1429 #ifdef PROFILING
1430   // @LDV profiling
1431   nat size_org = size;
1432 #endif
1433
1434   TICK_GC_WORDS_COPIED(size);
1435   /* Find out where we're going, using the handy "to" pointer in 
1436    * the step of the source object.  If it turns out we need to
1437    * evacuate to an older generation, adjust it here (see comment
1438    * by evacuate()).
1439    */
1440   if (stp->gen_no < evac_gen) {
1441 #ifdef NO_EAGER_PROMOTION    
1442     failed_to_evac = rtsTrue;
1443 #else
1444     stp = &generations[evac_gen].steps[0];
1445 #endif
1446   }
1447
1448   /* chain a new block onto the to-space for the destination step if
1449    * necessary.
1450    */
1451   if (stp->hp + size >= stp->hpLim) {
1452     gc_alloc_block(stp);
1453   }
1454
1455   for(to = stp->hp, from = (P_)src; size>0; --size) {
1456     *to++ = *from++;
1457   }
1458
1459   dest = stp->hp;
1460   stp->hp = to;
1461   upd_evacuee(src,(StgClosure *)dest);
1462 #ifdef PROFILING
1463   // We store the size of the just evacuated object in the LDV word so that
1464   // the profiler can guess the position of the next object later.
1465   SET_EVACUAEE_FOR_LDV(src, size_org);
1466 #endif
1467   return (StgClosure *)dest;
1468 }
1469
1470 /* Special version of copy() for when we only want to copy the info
1471  * pointer of an object, but reserve some padding after it.  This is
1472  * used to optimise evacuation of BLACKHOLEs.
1473  */
1474
1475
1476 static StgClosure *
1477 copyPart(StgClosure *src, nat size_to_reserve, nat size_to_copy, step *stp)
1478 {
1479   P_ dest, to, from;
1480 #ifdef PROFILING
1481   // @LDV profiling
1482   nat size_to_copy_org = size_to_copy;
1483 #endif
1484
1485   TICK_GC_WORDS_COPIED(size_to_copy);
1486   if (stp->gen_no < evac_gen) {
1487 #ifdef NO_EAGER_PROMOTION    
1488     failed_to_evac = rtsTrue;
1489 #else
1490     stp = &generations[evac_gen].steps[0];
1491 #endif
1492   }
1493
1494   if (stp->hp + size_to_reserve >= stp->hpLim) {
1495     gc_alloc_block(stp);
1496   }
1497
1498   for(to = stp->hp, from = (P_)src; size_to_copy>0; --size_to_copy) {
1499     *to++ = *from++;
1500   }
1501   
1502   dest = stp->hp;
1503   stp->hp += size_to_reserve;
1504   upd_evacuee(src,(StgClosure *)dest);
1505 #ifdef PROFILING
1506   // We store the size of the just evacuated object in the LDV word so that
1507   // the profiler can guess the position of the next object later.
1508   // size_to_copy_org is wrong because the closure already occupies size_to_reserve
1509   // words.
1510   SET_EVACUAEE_FOR_LDV(src, size_to_reserve);
1511   // fill the slop
1512   if (size_to_reserve - size_to_copy_org > 0)
1513     FILL_SLOP(stp->hp - 1, (int)(size_to_reserve - size_to_copy_org)); 
1514 #endif
1515   return (StgClosure *)dest;
1516 }
1517
1518
1519 /* -----------------------------------------------------------------------------
1520    Evacuate a large object
1521
1522    This just consists of removing the object from the (doubly-linked)
1523    step->large_objects list, and linking it on to the (singly-linked)
1524    step->new_large_objects list, from where it will be scavenged later.
1525
1526    Convention: bd->flags has BF_EVACUATED set for a large object
1527    that has been evacuated, or unset otherwise.
1528    -------------------------------------------------------------------------- */
1529
1530
1531 STATIC_INLINE void
1532 evacuate_large(StgPtr p)
1533 {
1534   bdescr *bd = Bdescr(p);
1535   step *stp;
1536
1537   // object must be at the beginning of the block (or be a ByteArray)
1538   ASSERT(get_itbl((StgClosure *)p)->type == ARR_WORDS ||
1539          (((W_)p & BLOCK_MASK) == 0));
1540
1541   // already evacuated? 
1542   if (bd->flags & BF_EVACUATED) { 
1543     /* Don't forget to set the failed_to_evac flag if we didn't get
1544      * the desired destination (see comments in evacuate()).
1545      */
1546     if (bd->gen_no < evac_gen) {
1547       failed_to_evac = rtsTrue;
1548       TICK_GC_FAILED_PROMOTION();
1549     }
1550     return;
1551   }
1552
1553   stp = bd->step;
1554   // remove from large_object list 
1555   if (bd->u.back) {
1556     bd->u.back->link = bd->link;
1557   } else { // first object in the list 
1558     stp->large_objects = bd->link;
1559   }
1560   if (bd->link) {
1561     bd->link->u.back = bd->u.back;
1562   }
1563   
1564   /* link it on to the evacuated large object list of the destination step
1565    */
1566   stp = bd->step->to;
1567   if (stp->gen_no < evac_gen) {
1568 #ifdef NO_EAGER_PROMOTION    
1569     failed_to_evac = rtsTrue;
1570 #else
1571     stp = &generations[evac_gen].steps[0];
1572 #endif
1573   }
1574
1575   bd->step = stp;
1576   bd->gen_no = stp->gen_no;
1577   bd->link = stp->new_large_objects;
1578   stp->new_large_objects = bd;
1579   bd->flags |= BF_EVACUATED;
1580 }
1581
1582 /* -----------------------------------------------------------------------------
1583    Evacuate
1584
1585    This is called (eventually) for every live object in the system.
1586
1587    The caller to evacuate specifies a desired generation in the
1588    evac_gen global variable.  The following conditions apply to
1589    evacuating an object which resides in generation M when we're
1590    collecting up to generation N
1591
1592    if  M >= evac_gen 
1593            if  M > N     do nothing
1594            else          evac to step->to
1595
1596    if  M < evac_gen      evac to evac_gen, step 0
1597
1598    if the object is already evacuated, then we check which generation
1599    it now resides in.
1600
1601    if  M >= evac_gen     do nothing
1602    if  M <  evac_gen     set failed_to_evac flag to indicate that we
1603                          didn't manage to evacuate this object into evac_gen.
1604
1605
1606    OPTIMISATION NOTES:
1607
1608    evacuate() is the single most important function performance-wise
1609    in the GC.  Various things have been tried to speed it up, but as
1610    far as I can tell the code generated by gcc 3.2 with -O2 is about
1611    as good as it's going to get.  We pass the argument to evacuate()
1612    in a register using the 'regparm' attribute (see the prototype for
1613    evacuate() near the top of this file).
1614
1615    Changing evacuate() to take an (StgClosure **) rather than
1616    returning the new pointer seems attractive, because we can avoid
1617    writing back the pointer when it hasn't changed (eg. for a static
1618    object, or an object in a generation > N).  However, I tried it and
1619    it doesn't help.  One reason is that the (StgClosure **) pointer
1620    gets spilled to the stack inside evacuate(), resulting in far more
1621    extra reads/writes than we save.
1622    -------------------------------------------------------------------------- */
1623
1624 REGPARM1 static StgClosure *
1625 evacuate(StgClosure *q)
1626 {
1627   StgClosure *to;
1628   bdescr *bd = NULL;
1629   step *stp;
1630   const StgInfoTable *info;
1631
1632 loop:
1633   if (HEAP_ALLOCED(q)) {
1634     bd = Bdescr((P_)q);
1635
1636     if (bd->gen_no > N) {
1637         /* Can't evacuate this object, because it's in a generation
1638          * older than the ones we're collecting.  Let's hope that it's
1639          * in evac_gen or older, or we will have to arrange to track
1640          * this pointer using the mutable list.
1641          */
1642         if (bd->gen_no < evac_gen) {
1643             // nope 
1644             failed_to_evac = rtsTrue;
1645             TICK_GC_FAILED_PROMOTION();
1646         }
1647         return q;
1648     }
1649
1650     /* evacuate large objects by re-linking them onto a different list.
1651      */
1652     if (bd->flags & BF_LARGE) {
1653         info = get_itbl(q);
1654         if (info->type == TSO && 
1655             ((StgTSO *)q)->what_next == ThreadRelocated) {
1656             q = (StgClosure *)((StgTSO *)q)->link;
1657             goto loop;
1658         }
1659         evacuate_large((P_)q);
1660         return q;
1661     }
1662
1663     /* If the object is in a step that we're compacting, then we
1664      * need to use an alternative evacuate procedure.
1665      */
1666     if (bd->flags & BF_COMPACTED) {
1667         if (!is_marked((P_)q,bd)) {
1668             mark((P_)q,bd);
1669             if (mark_stack_full()) {
1670                 mark_stack_overflowed = rtsTrue;
1671                 reset_mark_stack();
1672             }
1673             push_mark_stack((P_)q);
1674         }
1675         return q;
1676     }
1677
1678     /* Object is not already evacuated. */
1679     ASSERT((bd->flags & BF_EVACUATED) == 0);
1680
1681     stp = bd->step->to;
1682   }
1683 #ifdef DEBUG
1684   else stp = NULL; // make sure copy() will crash if HEAP_ALLOCED is wrong 
1685 #endif
1686
1687   // make sure the info pointer is into text space 
1688   ASSERT(LOOKS_LIKE_CLOSURE_PTR(q));
1689   info = get_itbl(q);
1690   
1691   switch (info -> type) {
1692
1693   case MUT_VAR:
1694   case MVAR:
1695       return copy(q,sizeW_fromITBL(info),stp);
1696
1697   case CONSTR_0_1:
1698   { 
1699       StgWord w = (StgWord)q->payload[0];
1700       if (q->header.info == Czh_con_info &&
1701           // unsigned, so always true:  (StgChar)w >= MIN_CHARLIKE &&  
1702           (StgChar)w <= MAX_CHARLIKE) {
1703           return (StgClosure *)CHARLIKE_CLOSURE((StgChar)w);
1704       }
1705       if (q->header.info == Izh_con_info &&
1706           (StgInt)w >= MIN_INTLIKE && (StgInt)w <= MAX_INTLIKE) {
1707           return (StgClosure *)INTLIKE_CLOSURE((StgInt)w);
1708       }
1709       // else, fall through ... 
1710   }
1711
1712   case FUN_1_0:
1713   case FUN_0_1:
1714   case CONSTR_1_0:
1715   case THUNK_1_0:
1716   case THUNK_0_1:
1717     return copy(q,sizeofW(StgHeader)+1,stp);
1718
1719   case THUNK_1_1:
1720   case THUNK_0_2:
1721   case THUNK_2_0:
1722 #ifdef NO_PROMOTE_THUNKS
1723     if (bd->gen_no == 0 && 
1724         bd->step->no != 0 &&
1725         bd->step->no == generations[bd->gen_no].n_steps-1) {
1726       stp = bd->step;
1727     }
1728 #endif
1729     return copy(q,sizeofW(StgHeader)+2,stp);
1730
1731   case FUN_1_1:
1732   case FUN_0_2:
1733   case FUN_2_0:
1734   case CONSTR_1_1:
1735   case CONSTR_0_2:
1736   case CONSTR_2_0:
1737     return copy(q,sizeofW(StgHeader)+2,stp);
1738
1739   case FUN:
1740   case THUNK:
1741   case CONSTR:
1742   case IND_PERM:
1743   case IND_OLDGEN_PERM:
1744   case WEAK:
1745   case FOREIGN:
1746   case STABLE_NAME:
1747     return copy(q,sizeW_fromITBL(info),stp);
1748
1749   case BCO:
1750       return copy(q,bco_sizeW((StgBCO *)q),stp);
1751
1752   case CAF_BLACKHOLE:
1753   case SE_CAF_BLACKHOLE:
1754   case SE_BLACKHOLE:
1755   case BLACKHOLE:
1756     return copyPart(q,BLACKHOLE_sizeW(),sizeofW(StgHeader),stp);
1757
1758   case BLACKHOLE_BQ:
1759     to = copy(q,BLACKHOLE_sizeW(),stp); 
1760     return to;
1761
1762   case THUNK_SELECTOR:
1763     {
1764         StgClosure *p;
1765
1766         if (thunk_selector_depth > MAX_THUNK_SELECTOR_DEPTH) {
1767             return copy(q,THUNK_SELECTOR_sizeW(),stp);
1768         }
1769
1770         p = eval_thunk_selector(info->layout.selector_offset,
1771                                 (StgSelector *)q);
1772
1773         if (p == NULL) {
1774             return copy(q,THUNK_SELECTOR_sizeW(),stp);
1775         } else {
1776             // q is still BLACKHOLE'd.
1777             thunk_selector_depth++;
1778             p = evacuate(p);
1779             thunk_selector_depth--;
1780             upd_evacuee(q,p);
1781 #ifdef PROFILING
1782             // We store the size of the just evacuated object in the
1783             // LDV word so that the profiler can guess the position of
1784             // the next object later.
1785             SET_EVACUAEE_FOR_LDV(q, THUNK_SELECTOR_sizeW());
1786 #endif
1787             return p;
1788         }
1789     }
1790
1791   case IND:
1792   case IND_OLDGEN:
1793     // follow chains of indirections, don't evacuate them 
1794     q = ((StgInd*)q)->indirectee;
1795     goto loop;
1796
1797   case THUNK_STATIC:
1798     if (info->srt_bitmap != 0 && major_gc && 
1799         THUNK_STATIC_LINK((StgClosure *)q) == NULL) {
1800       THUNK_STATIC_LINK((StgClosure *)q) = static_objects;
1801       static_objects = (StgClosure *)q;
1802     }
1803     return q;
1804
1805   case FUN_STATIC:
1806     if (info->srt_bitmap != 0 && major_gc && 
1807         FUN_STATIC_LINK((StgClosure *)q) == NULL) {
1808       FUN_STATIC_LINK((StgClosure *)q) = static_objects;
1809       static_objects = (StgClosure *)q;
1810     }
1811     return q;
1812
1813   case IND_STATIC:
1814     /* If q->saved_info != NULL, then it's a revertible CAF - it'll be
1815      * on the CAF list, so don't do anything with it here (we'll
1816      * scavenge it later).
1817      */
1818     if (major_gc
1819           && ((StgIndStatic *)q)->saved_info == NULL
1820           && IND_STATIC_LINK((StgClosure *)q) == NULL) {
1821         IND_STATIC_LINK((StgClosure *)q) = static_objects;
1822         static_objects = (StgClosure *)q;
1823     }
1824     return q;
1825
1826   case CONSTR_STATIC:
1827     if (major_gc && STATIC_LINK(info,(StgClosure *)q) == NULL) {
1828       STATIC_LINK(info,(StgClosure *)q) = static_objects;
1829       static_objects = (StgClosure *)q;
1830     }
1831     return q;
1832
1833   case CONSTR_INTLIKE:
1834   case CONSTR_CHARLIKE:
1835   case CONSTR_NOCAF_STATIC:
1836     /* no need to put these on the static linked list, they don't need
1837      * to be scavenged.
1838      */
1839     return q;
1840
1841   case RET_BCO:
1842   case RET_SMALL:
1843   case RET_VEC_SMALL:
1844   case RET_BIG:
1845   case RET_VEC_BIG:
1846   case RET_DYN:
1847   case UPDATE_FRAME:
1848   case STOP_FRAME:
1849   case CATCH_FRAME:
1850   case CATCH_STM_FRAME:
1851   case CATCH_RETRY_FRAME:
1852   case ATOMICALLY_FRAME:
1853     // shouldn't see these 
1854     barf("evacuate: stack frame at %p\n", q);
1855
1856   case PAP:
1857   case AP:
1858       return copy(q,pap_sizeW((StgPAP*)q),stp);
1859
1860   case AP_STACK:
1861       return copy(q,ap_stack_sizeW((StgAP_STACK*)q),stp);
1862
1863   case EVACUATED:
1864     /* Already evacuated, just return the forwarding address.
1865      * HOWEVER: if the requested destination generation (evac_gen) is
1866      * older than the actual generation (because the object was
1867      * already evacuated to a younger generation) then we have to
1868      * set the failed_to_evac flag to indicate that we couldn't 
1869      * manage to promote the object to the desired generation.
1870      */
1871     if (evac_gen > 0) {         // optimisation 
1872       StgClosure *p = ((StgEvacuated*)q)->evacuee;
1873       if (HEAP_ALLOCED(p) && Bdescr((P_)p)->gen_no < evac_gen) {
1874         failed_to_evac = rtsTrue;
1875         TICK_GC_FAILED_PROMOTION();
1876       }
1877     }
1878     return ((StgEvacuated*)q)->evacuee;
1879
1880   case ARR_WORDS:
1881       // just copy the block 
1882       return copy(q,arr_words_sizeW((StgArrWords *)q),stp);
1883
1884   case MUT_ARR_PTRS:
1885   case MUT_ARR_PTRS_FROZEN:
1886   case MUT_ARR_PTRS_FROZEN0:
1887       // just copy the block 
1888       return copy(q,mut_arr_ptrs_sizeW((StgMutArrPtrs *)q),stp);
1889
1890   case TSO:
1891     {
1892       StgTSO *tso = (StgTSO *)q;
1893
1894       /* Deal with redirected TSOs (a TSO that's had its stack enlarged).
1895        */
1896       if (tso->what_next == ThreadRelocated) {
1897         q = (StgClosure *)tso->link;
1898         goto loop;
1899       }
1900
1901       /* To evacuate a small TSO, we need to relocate the update frame
1902        * list it contains.  
1903        */
1904       {
1905           StgTSO *new_tso;
1906           StgPtr p, q;
1907
1908           new_tso = (StgTSO *)copyPart((StgClosure *)tso,
1909                                        tso_sizeW(tso),
1910                                        sizeofW(StgTSO), stp);
1911           move_TSO(tso, new_tso);
1912           for (p = tso->sp, q = new_tso->sp;
1913                p < tso->stack+tso->stack_size;) {
1914               *q++ = *p++;
1915           }
1916           
1917           return (StgClosure *)new_tso;
1918       }
1919     }
1920
1921 #if defined(PAR)
1922   case RBH: // cf. BLACKHOLE_BQ
1923     {
1924       //StgInfoTable *rip = get_closure_info(q, &size, &ptrs, &nonptrs, &vhs, str);
1925       to = copy(q,BLACKHOLE_sizeW(),stp); 
1926       //ToDo: derive size etc from reverted IP
1927       //to = copy(q,size,stp);
1928       IF_DEBUG(gc,
1929                debugBelch("@@ evacuate: RBH %p (%s) to %p (%s)",
1930                      q, info_type(q), to, info_type(to)));
1931       return to;
1932     }
1933
1934   case BLOCKED_FETCH:
1935     ASSERT(sizeofW(StgBlockedFetch) >= MIN_NONUPD_SIZE);
1936     to = copy(q,sizeofW(StgBlockedFetch),stp);
1937     IF_DEBUG(gc,
1938              debugBelch("@@ evacuate: %p (%s) to %p (%s)",
1939                    q, info_type(q), to, info_type(to)));
1940     return to;
1941
1942 # ifdef DIST    
1943   case REMOTE_REF:
1944 # endif
1945   case FETCH_ME:
1946     ASSERT(sizeofW(StgBlockedFetch) >= MIN_UPD_SIZE);
1947     to = copy(q,sizeofW(StgFetchMe),stp);
1948     IF_DEBUG(gc,
1949              debugBelch("@@ evacuate: %p (%s) to %p (%s)",
1950                    q, info_type(q), to, info_type(to)));
1951     return to;
1952
1953   case FETCH_ME_BQ:
1954     ASSERT(sizeofW(StgBlockedFetch) >= MIN_UPD_SIZE);
1955     to = copy(q,sizeofW(StgFetchMeBlockingQueue),stp);
1956     IF_DEBUG(gc,
1957              debugBelch("@@ evacuate: %p (%s) to %p (%s)",
1958                    q, info_type(q), to, info_type(to)));
1959     return to;
1960 #endif
1961
1962   case TREC_HEADER: 
1963     return copy(q,sizeofW(StgTRecHeader),stp);
1964
1965   case TVAR_WAIT_QUEUE:
1966     return copy(q,sizeofW(StgTVarWaitQueue),stp);
1967
1968   case TVAR:
1969     return copy(q,sizeofW(StgTVar),stp);
1970     
1971   case TREC_CHUNK:
1972     return copy(q,sizeofW(StgTRecChunk),stp);
1973
1974   default:
1975     barf("evacuate: strange closure type %d", (int)(info->type));
1976   }
1977
1978   barf("evacuate");
1979 }
1980
1981 /* -----------------------------------------------------------------------------
1982    Evaluate a THUNK_SELECTOR if possible.
1983
1984    returns: NULL if we couldn't evaluate this THUNK_SELECTOR, or
1985    a closure pointer if we evaluated it and this is the result.  Note
1986    that "evaluating" the THUNK_SELECTOR doesn't necessarily mean
1987    reducing it to HNF, just that we have eliminated the selection.
1988    The result might be another thunk, or even another THUNK_SELECTOR.
1989
1990    If the return value is non-NULL, the original selector thunk has
1991    been BLACKHOLE'd, and should be updated with an indirection or a
1992    forwarding pointer.  If the return value is NULL, then the selector
1993    thunk is unchanged.
1994    -------------------------------------------------------------------------- */
1995
1996 static inline rtsBool
1997 is_to_space ( StgClosure *p )
1998 {
1999     bdescr *bd;
2000
2001     bd = Bdescr((StgPtr)p);
2002     if (HEAP_ALLOCED(p) &&
2003         ((bd->flags & BF_EVACUATED) 
2004          || ((bd->flags & BF_COMPACTED) &&
2005              is_marked((P_)p,bd)))) {
2006         return rtsTrue;
2007     } else {
2008         return rtsFalse;
2009     }
2010 }    
2011
2012 static StgClosure *
2013 eval_thunk_selector( nat field, StgSelector * p )
2014 {
2015     StgInfoTable *info;
2016     const StgInfoTable *info_ptr;
2017     StgClosure *selectee;
2018     
2019     selectee = p->selectee;
2020
2021     // Save the real info pointer (NOTE: not the same as get_itbl()).
2022     info_ptr = p->header.info;
2023
2024     // If the THUNK_SELECTOR is in a generation that we are not
2025     // collecting, then bail out early.  We won't be able to save any
2026     // space in any case, and updating with an indirection is trickier
2027     // in an old gen.
2028     if (Bdescr((StgPtr)p)->gen_no > N) {
2029         return NULL;
2030     }
2031
2032     // BLACKHOLE the selector thunk, since it is now under evaluation.
2033     // This is important to stop us going into an infinite loop if
2034     // this selector thunk eventually refers to itself.
2035     SET_INFO(p,&stg_BLACKHOLE_info);
2036
2037 selector_loop:
2038
2039     // We don't want to end up in to-space, because this causes
2040     // problems when the GC later tries to evacuate the result of
2041     // eval_thunk_selector().  There are various ways this could
2042     // happen:
2043     //
2044     // 1. following an IND_STATIC
2045     //
2046     // 2. when the old generation is compacted, the mark phase updates
2047     //    from-space pointers to be to-space pointers, and we can't
2048     //    reliably tell which we're following (eg. from an IND_STATIC).
2049     // 
2050     // 3. compacting GC again: if we're looking at a constructor in
2051     //    the compacted generation, it might point directly to objects
2052     //    in to-space.  We must bale out here, otherwise doing the selection
2053     //    will result in a to-space pointer being returned.
2054     //
2055     //  (1) is dealt with using a BF_EVACUATED test on the
2056     //  selectee. (2) and (3): we can tell if we're looking at an
2057     //  object in the compacted generation that might point to
2058     //  to-space objects by testing that (a) it is BF_COMPACTED, (b)
2059     //  the compacted generation is being collected, and (c) the
2060     //  object is marked.  Only a marked object may have pointers that
2061     //  point to to-space objects, because that happens when
2062     //  scavenging.
2063     //
2064     //  The to-space test is now embodied in the in_to_space() inline
2065     //  function, as it is re-used below.
2066     //
2067     if (is_to_space(selectee)) {
2068         goto bale_out;
2069     }
2070
2071     info = get_itbl(selectee);
2072     switch (info->type) {
2073       case CONSTR:
2074       case CONSTR_1_0:
2075       case CONSTR_0_1:
2076       case CONSTR_2_0:
2077       case CONSTR_1_1:
2078       case CONSTR_0_2:
2079       case CONSTR_STATIC:
2080       case CONSTR_NOCAF_STATIC:
2081           // check that the size is in range 
2082           ASSERT(field <  (StgWord32)(info->layout.payload.ptrs + 
2083                                       info->layout.payload.nptrs));
2084           
2085           // Select the right field from the constructor, and check
2086           // that the result isn't in to-space.  It might be in
2087           // to-space if, for example, this constructor contains
2088           // pointers to younger-gen objects (and is on the mut-once
2089           // list).
2090           //
2091           { 
2092               StgClosure *q;
2093               q = selectee->payload[field];
2094               if (is_to_space(q)) {
2095                   goto bale_out;
2096               } else {
2097                   return q;
2098               }
2099           }
2100
2101       case IND:
2102       case IND_PERM:
2103       case IND_OLDGEN:
2104       case IND_OLDGEN_PERM:
2105       case IND_STATIC:
2106           selectee = ((StgInd *)selectee)->indirectee;
2107           goto selector_loop;
2108
2109       case EVACUATED:
2110           // We don't follow pointers into to-space; the constructor
2111           // has already been evacuated, so we won't save any space
2112           // leaks by evaluating this selector thunk anyhow.
2113           break;
2114
2115       case THUNK_SELECTOR:
2116       {
2117           StgClosure *val;
2118
2119           // check that we don't recurse too much, re-using the
2120           // depth bound also used in evacuate().
2121           if (thunk_selector_depth >= MAX_THUNK_SELECTOR_DEPTH) {
2122               break;
2123           }
2124           thunk_selector_depth++;
2125
2126           val = eval_thunk_selector(info->layout.selector_offset, 
2127                                     (StgSelector *)selectee);
2128
2129           thunk_selector_depth--;
2130
2131           if (val == NULL) { 
2132               break;
2133           } else {
2134               // We evaluated this selector thunk, so update it with
2135               // an indirection.  NOTE: we don't use UPD_IND here,
2136               // because we are guaranteed that p is in a generation
2137               // that we are collecting, and we never want to put the
2138               // indirection on a mutable list.
2139 #ifdef PROFILING
2140               // For the purposes of LDV profiling, we have destroyed
2141               // the original selector thunk.
2142               SET_INFO(p, info_ptr);
2143               LDV_RECORD_DEAD_FILL_SLOP_DYNAMIC(selectee);
2144 #endif
2145               ((StgInd *)selectee)->indirectee = val;
2146               SET_INFO(selectee,&stg_IND_info);
2147
2148               // For the purposes of LDV profiling, we have created an
2149               // indirection.
2150               LDV_RECORD_CREATE(selectee);
2151
2152               selectee = val;
2153               goto selector_loop;
2154           }
2155       }
2156
2157       case AP:
2158       case AP_STACK:
2159       case THUNK:
2160       case THUNK_1_0:
2161       case THUNK_0_1:
2162       case THUNK_2_0:
2163       case THUNK_1_1:
2164       case THUNK_0_2:
2165       case THUNK_STATIC:
2166       case CAF_BLACKHOLE:
2167       case SE_CAF_BLACKHOLE:
2168       case SE_BLACKHOLE:
2169       case BLACKHOLE:
2170       case BLACKHOLE_BQ:
2171 #if defined(PAR)
2172       case RBH:
2173       case BLOCKED_FETCH:
2174 # ifdef DIST    
2175       case REMOTE_REF:
2176 # endif
2177       case FETCH_ME:
2178       case FETCH_ME_BQ:
2179 #endif
2180           // not evaluated yet 
2181           break;
2182     
2183       default:
2184         barf("eval_thunk_selector: strange selectee %d",
2185              (int)(info->type));
2186     }
2187
2188 bale_out:
2189     // We didn't manage to evaluate this thunk; restore the old info pointer
2190     SET_INFO(p, info_ptr);
2191     return NULL;
2192 }
2193
2194 /* -----------------------------------------------------------------------------
2195    move_TSO is called to update the TSO structure after it has been
2196    moved from one place to another.
2197    -------------------------------------------------------------------------- */
2198
2199 void
2200 move_TSO (StgTSO *src, StgTSO *dest)
2201 {
2202     ptrdiff_t diff;
2203
2204     // relocate the stack pointer... 
2205     diff = (StgPtr)dest - (StgPtr)src; // In *words* 
2206     dest->sp = (StgPtr)dest->sp + diff;
2207 }
2208
2209 /* Similar to scavenge_large_bitmap(), but we don't write back the
2210  * pointers we get back from evacuate().
2211  */
2212 static void
2213 scavenge_large_srt_bitmap( StgLargeSRT *large_srt )
2214 {
2215     nat i, b, size;
2216     StgWord bitmap;
2217     StgClosure **p;
2218     
2219     b = 0;
2220     bitmap = large_srt->l.bitmap[b];
2221     size   = (nat)large_srt->l.size;
2222     p      = (StgClosure **)large_srt->srt;
2223     for (i = 0; i < size; ) {
2224         if ((bitmap & 1) != 0) {
2225             evacuate(*p);
2226         }
2227         i++;
2228         p++;
2229         if (i % BITS_IN(W_) == 0) {
2230             b++;
2231             bitmap = large_srt->l.bitmap[b];
2232         } else {
2233             bitmap = bitmap >> 1;
2234         }
2235     }
2236 }
2237
2238 /* evacuate the SRT.  If srt_bitmap is zero, then there isn't an
2239  * srt field in the info table.  That's ok, because we'll
2240  * never dereference it.
2241  */
2242 STATIC_INLINE void
2243 scavenge_srt (StgClosure **srt, nat srt_bitmap)
2244 {
2245   nat bitmap;
2246   StgClosure **p;
2247
2248   bitmap = srt_bitmap;
2249   p = srt;
2250
2251   if (bitmap == (StgHalfWord)(-1)) {  
2252       scavenge_large_srt_bitmap( (StgLargeSRT *)srt );
2253       return;
2254   }
2255
2256   while (bitmap != 0) {
2257       if ((bitmap & 1) != 0) {
2258 #ifdef ENABLE_WIN32_DLL_SUPPORT
2259           // Special-case to handle references to closures hiding out in DLLs, since
2260           // double indirections required to get at those. The code generator knows
2261           // which is which when generating the SRT, so it stores the (indirect)
2262           // reference to the DLL closure in the table by first adding one to it.
2263           // We check for this here, and undo the addition before evacuating it.
2264           // 
2265           // If the SRT entry hasn't got bit 0 set, the SRT entry points to a
2266           // closure that's fixed at link-time, and no extra magic is required.
2267           if ( (unsigned long)(*srt) & 0x1 ) {
2268               evacuate(*stgCast(StgClosure**,(stgCast(unsigned long, *srt) & ~0x1)));
2269           } else {
2270               evacuate(*p);
2271           }
2272 #else
2273           evacuate(*p);
2274 #endif
2275       }
2276       p++;
2277       bitmap = bitmap >> 1;
2278   }
2279 }
2280
2281
2282 STATIC_INLINE void
2283 scavenge_thunk_srt(const StgInfoTable *info)
2284 {
2285     StgThunkInfoTable *thunk_info;
2286
2287     thunk_info = itbl_to_thunk_itbl(info);
2288     scavenge_srt((StgClosure **)GET_SRT(thunk_info), thunk_info->i.srt_bitmap);
2289 }
2290
2291 STATIC_INLINE void
2292 scavenge_fun_srt(const StgInfoTable *info)
2293 {
2294     StgFunInfoTable *fun_info;
2295
2296     fun_info = itbl_to_fun_itbl(info);
2297     scavenge_srt((StgClosure **)GET_FUN_SRT(fun_info), fun_info->i.srt_bitmap);
2298 }
2299
2300 STATIC_INLINE void
2301 scavenge_ret_srt(const StgInfoTable *info)
2302 {
2303     StgRetInfoTable *ret_info;
2304
2305     ret_info = itbl_to_ret_itbl(info);
2306     scavenge_srt((StgClosure **)GET_SRT(ret_info), ret_info->i.srt_bitmap);
2307 }
2308
2309 /* -----------------------------------------------------------------------------
2310    Scavenge a TSO.
2311    -------------------------------------------------------------------------- */
2312
2313 static void
2314 scavengeTSO (StgTSO *tso)
2315 {
2316     // chase the link field for any TSOs on the same queue 
2317     tso->link = (StgTSO *)evacuate((StgClosure *)tso->link);
2318     if (   tso->why_blocked == BlockedOnMVar
2319         || tso->why_blocked == BlockedOnBlackHole
2320         || tso->why_blocked == BlockedOnException
2321 #if defined(PAR)
2322         || tso->why_blocked == BlockedOnGA
2323         || tso->why_blocked == BlockedOnGA_NoSend
2324 #endif
2325         ) {
2326         tso->block_info.closure = evacuate(tso->block_info.closure);
2327     }
2328     if ( tso->blocked_exceptions != NULL ) {
2329         tso->blocked_exceptions = 
2330             (StgTSO *)evacuate((StgClosure *)tso->blocked_exceptions);
2331     }
2332     
2333     // scavange current transaction record
2334     tso->trec = (StgTRecHeader *)evacuate((StgClosure *)tso->trec);
2335     
2336     // scavenge this thread's stack 
2337     scavenge_stack(tso->sp, &(tso->stack[tso->stack_size]));
2338 }
2339
2340 /* -----------------------------------------------------------------------------
2341    Blocks of function args occur on the stack (at the top) and
2342    in PAPs.
2343    -------------------------------------------------------------------------- */
2344
2345 STATIC_INLINE StgPtr
2346 scavenge_arg_block (StgFunInfoTable *fun_info, StgClosure **args)
2347 {
2348     StgPtr p;
2349     StgWord bitmap;
2350     nat size;
2351
2352     p = (StgPtr)args;
2353     switch (fun_info->f.fun_type) {
2354     case ARG_GEN:
2355         bitmap = BITMAP_BITS(fun_info->f.bitmap);
2356         size = BITMAP_SIZE(fun_info->f.bitmap);
2357         goto small_bitmap;
2358     case ARG_GEN_BIG:
2359         size = GET_FUN_LARGE_BITMAP(fun_info)->size;
2360         scavenge_large_bitmap(p, GET_FUN_LARGE_BITMAP(fun_info), size);
2361         p += size;
2362         break;
2363     default:
2364         bitmap = BITMAP_BITS(stg_arg_bitmaps[fun_info->f.fun_type]);
2365         size = BITMAP_SIZE(stg_arg_bitmaps[fun_info->f.fun_type]);
2366     small_bitmap:
2367         while (size > 0) {
2368             if ((bitmap & 1) == 0) {
2369                 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
2370             }
2371             p++;
2372             bitmap = bitmap >> 1;
2373             size--;
2374         }
2375         break;
2376     }
2377     return p;
2378 }
2379
2380 STATIC_INLINE StgPtr
2381 scavenge_PAP (StgPAP *pap)
2382 {
2383     StgPtr p;
2384     StgWord bitmap, size;
2385     StgFunInfoTable *fun_info;
2386
2387     pap->fun = evacuate(pap->fun);
2388     fun_info = get_fun_itbl(pap->fun);
2389     ASSERT(fun_info->i.type != PAP);
2390
2391     p = (StgPtr)pap->payload;
2392     size = pap->n_args;
2393
2394     switch (fun_info->f.fun_type) {
2395     case ARG_GEN:
2396         bitmap = BITMAP_BITS(fun_info->f.bitmap);
2397         goto small_bitmap;
2398     case ARG_GEN_BIG:
2399         scavenge_large_bitmap(p, GET_FUN_LARGE_BITMAP(fun_info), size);
2400         p += size;
2401         break;
2402     case ARG_BCO:
2403         scavenge_large_bitmap((StgPtr)pap->payload, BCO_BITMAP(pap->fun), size);
2404         p += size;
2405         break;
2406     default:
2407         bitmap = BITMAP_BITS(stg_arg_bitmaps[fun_info->f.fun_type]);
2408     small_bitmap:
2409         size = pap->n_args;
2410         while (size > 0) {
2411             if ((bitmap & 1) == 0) {
2412                 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
2413             }
2414             p++;
2415             bitmap = bitmap >> 1;
2416             size--;
2417         }
2418         break;
2419     }
2420     return p;
2421 }
2422
2423 /* -----------------------------------------------------------------------------
2424    Scavenge a given step until there are no more objects in this step
2425    to scavenge.
2426
2427    evac_gen is set by the caller to be either zero (for a step in a
2428    generation < N) or G where G is the generation of the step being
2429    scavenged.  
2430
2431    We sometimes temporarily change evac_gen back to zero if we're
2432    scavenging a mutable object where early promotion isn't such a good
2433    idea.  
2434    -------------------------------------------------------------------------- */
2435
2436 static void
2437 scavenge(step *stp)
2438 {
2439   StgPtr p, q;
2440   StgInfoTable *info;
2441   bdescr *bd;
2442   nat saved_evac_gen = evac_gen;
2443
2444   p = stp->scan;
2445   bd = stp->scan_bd;
2446
2447   failed_to_evac = rtsFalse;
2448
2449   /* scavenge phase - standard breadth-first scavenging of the
2450    * evacuated objects 
2451    */
2452
2453   while (bd != stp->hp_bd || p < stp->hp) {
2454
2455     // If we're at the end of this block, move on to the next block 
2456     if (bd != stp->hp_bd && p == bd->free) {
2457       bd = bd->link;
2458       p = bd->start;
2459       continue;
2460     }
2461
2462     ASSERT(LOOKS_LIKE_CLOSURE_PTR(p));
2463     info = get_itbl((StgClosure *)p);
2464     
2465     ASSERT(thunk_selector_depth == 0);
2466
2467     q = p;
2468     switch (info->type) {
2469
2470     case MVAR:
2471     { 
2472         StgMVar *mvar = ((StgMVar *)p);
2473         evac_gen = 0;
2474         mvar->head = (StgTSO *)evacuate((StgClosure *)mvar->head);
2475         mvar->tail = (StgTSO *)evacuate((StgClosure *)mvar->tail);
2476         mvar->value = evacuate((StgClosure *)mvar->value);
2477         evac_gen = saved_evac_gen;
2478         failed_to_evac = rtsTrue; // mutable.
2479         p += sizeofW(StgMVar);
2480         break;
2481     }
2482
2483     case FUN_2_0:
2484         scavenge_fun_srt(info);
2485         ((StgClosure *)p)->payload[1] = evacuate(((StgClosure *)p)->payload[1]);
2486         ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2487         p += sizeofW(StgHeader) + 2;
2488         break;
2489
2490     case THUNK_2_0:
2491         scavenge_thunk_srt(info);
2492     case CONSTR_2_0:
2493         ((StgClosure *)p)->payload[1] = evacuate(((StgClosure *)p)->payload[1]);
2494         ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2495         p += sizeofW(StgHeader) + 2;
2496         break;
2497         
2498     case THUNK_1_0:
2499         scavenge_thunk_srt(info);
2500         ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2501         p += sizeofW(StgHeader) + 1;
2502         break;
2503         
2504     case FUN_1_0:
2505         scavenge_fun_srt(info);
2506     case CONSTR_1_0:
2507         ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2508         p += sizeofW(StgHeader) + 1;
2509         break;
2510         
2511     case THUNK_0_1:
2512         scavenge_thunk_srt(info);
2513         p += sizeofW(StgHeader) + 1;
2514         break;
2515         
2516     case FUN_0_1:
2517         scavenge_fun_srt(info);
2518     case CONSTR_0_1:
2519         p += sizeofW(StgHeader) + 1;
2520         break;
2521         
2522     case THUNK_0_2:
2523         scavenge_thunk_srt(info);
2524         p += sizeofW(StgHeader) + 2;
2525         break;
2526         
2527     case FUN_0_2:
2528         scavenge_fun_srt(info);
2529     case CONSTR_0_2:
2530         p += sizeofW(StgHeader) + 2;
2531         break;
2532         
2533     case THUNK_1_1:
2534         scavenge_thunk_srt(info);
2535         ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2536         p += sizeofW(StgHeader) + 2;
2537         break;
2538
2539     case FUN_1_1:
2540         scavenge_fun_srt(info);
2541     case CONSTR_1_1:
2542         ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2543         p += sizeofW(StgHeader) + 2;
2544         break;
2545         
2546     case FUN:
2547         scavenge_fun_srt(info);
2548         goto gen_obj;
2549
2550     case THUNK:
2551         scavenge_thunk_srt(info);
2552         // fall through 
2553         
2554     gen_obj:
2555     case CONSTR:
2556     case WEAK:
2557     case FOREIGN:
2558     case STABLE_NAME:
2559     {
2560         StgPtr end;
2561
2562         end = (P_)((StgClosure *)p)->payload + info->layout.payload.ptrs;
2563         for (p = (P_)((StgClosure *)p)->payload; p < end; p++) {
2564             *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
2565         }
2566         p += info->layout.payload.nptrs;
2567         break;
2568     }
2569
2570     case BCO: {
2571         StgBCO *bco = (StgBCO *)p;
2572         bco->instrs = (StgArrWords *)evacuate((StgClosure *)bco->instrs);
2573         bco->literals = (StgArrWords *)evacuate((StgClosure *)bco->literals);
2574         bco->ptrs = (StgMutArrPtrs *)evacuate((StgClosure *)bco->ptrs);
2575         bco->itbls = (StgArrWords *)evacuate((StgClosure *)bco->itbls);
2576         p += bco_sizeW(bco);
2577         break;
2578     }
2579
2580     case IND_PERM:
2581       if (stp->gen->no != 0) {
2582 #ifdef PROFILING
2583         // @LDV profiling
2584         // No need to call LDV_recordDead_FILL_SLOP_DYNAMIC() because an 
2585         // IND_OLDGEN_PERM closure is larger than an IND_PERM closure.
2586         LDV_recordDead((StgClosure *)p, sizeofW(StgInd));
2587 #endif        
2588         // 
2589         // Todo: maybe use SET_HDR() and remove LDV_RECORD_CREATE()?
2590         //
2591         SET_INFO(((StgClosure *)p), &stg_IND_OLDGEN_PERM_info);
2592
2593         // We pretend that p has just been created.
2594         LDV_RECORD_CREATE((StgClosure *)p);
2595       }
2596         // fall through 
2597     case IND_OLDGEN_PERM:
2598         ((StgInd *)p)->indirectee = evacuate(((StgInd *)p)->indirectee);
2599         p += sizeofW(StgInd);
2600         break;
2601
2602     case MUT_VAR:
2603         evac_gen = 0;
2604         ((StgMutVar *)p)->var = evacuate(((StgMutVar *)p)->var);
2605         evac_gen = saved_evac_gen;
2606         failed_to_evac = rtsTrue; // mutable anyhow
2607         p += sizeofW(StgMutVar);
2608         break;
2609
2610     case CAF_BLACKHOLE:
2611     case SE_CAF_BLACKHOLE:
2612     case SE_BLACKHOLE:
2613     case BLACKHOLE:
2614         p += BLACKHOLE_sizeW();
2615         break;
2616
2617     case BLACKHOLE_BQ:
2618     { 
2619         StgBlockingQueue *bh = (StgBlockingQueue *)p;
2620         bh->blocking_queue = 
2621             (StgTSO *)evacuate((StgClosure *)bh->blocking_queue);
2622         failed_to_evac = rtsTrue;
2623         p += BLACKHOLE_sizeW();
2624         break;
2625     }
2626
2627     case THUNK_SELECTOR:
2628     { 
2629         StgSelector *s = (StgSelector *)p;
2630         s->selectee = evacuate(s->selectee);
2631         p += THUNK_SELECTOR_sizeW();
2632         break;
2633     }
2634
2635     // A chunk of stack saved in a heap object
2636     case AP_STACK:
2637     {
2638         StgAP_STACK *ap = (StgAP_STACK *)p;
2639
2640         ap->fun = evacuate(ap->fun);
2641         scavenge_stack((StgPtr)ap->payload, (StgPtr)ap->payload + ap->size);
2642         p = (StgPtr)ap->payload + ap->size;
2643         break;
2644     }
2645
2646     case PAP:
2647     case AP:
2648         p = scavenge_PAP((StgPAP *)p);
2649         break;
2650
2651     case ARR_WORDS:
2652         // nothing to follow 
2653         p += arr_words_sizeW((StgArrWords *)p);
2654         break;
2655
2656     case MUT_ARR_PTRS:
2657         // follow everything 
2658     {
2659         StgPtr next;
2660
2661         evac_gen = 0;           // repeatedly mutable 
2662         next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
2663         for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
2664             *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
2665         }
2666         evac_gen = saved_evac_gen;
2667         failed_to_evac = rtsTrue; // mutable anyhow.
2668         break;
2669     }
2670
2671     case MUT_ARR_PTRS_FROZEN:
2672     case MUT_ARR_PTRS_FROZEN0:
2673         // follow everything 
2674     {
2675         StgPtr next;
2676
2677         next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
2678         for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
2679             *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
2680         }
2681         // it's tempting to recordMutable() if failed_to_evac is
2682         // false, but that breaks some assumptions (eg. every
2683         // closure on the mutable list is supposed to have the MUT
2684         // flag set, and MUT_ARR_PTRS_FROZEN doesn't).
2685         break;
2686     }
2687
2688     case TSO:
2689     { 
2690         StgTSO *tso = (StgTSO *)p;
2691         evac_gen = 0;
2692         scavengeTSO(tso);
2693         evac_gen = saved_evac_gen;
2694         failed_to_evac = rtsTrue; // mutable anyhow.
2695         p += tso_sizeW(tso);
2696         break;
2697     }
2698
2699 #if defined(PAR)
2700     case RBH: // cf. BLACKHOLE_BQ
2701     { 
2702 #if 0
2703         nat size, ptrs, nonptrs, vhs;
2704         char str[80];
2705         StgInfoTable *rip = get_closure_info(p, &size, &ptrs, &nonptrs, &vhs, str);
2706 #endif
2707         StgRBH *rbh = (StgRBH *)p;
2708         (StgClosure *)rbh->blocking_queue = 
2709             evacuate((StgClosure *)rbh->blocking_queue);
2710         failed_to_evac = rtsTrue;  // mutable anyhow.
2711         IF_DEBUG(gc,
2712                  debugBelch("@@ scavenge: RBH %p (%s) (new blocking_queue link=%p)",
2713                        p, info_type(p), (StgClosure *)rbh->blocking_queue));
2714         // ToDo: use size of reverted closure here!
2715         p += BLACKHOLE_sizeW(); 
2716         break;
2717     }
2718
2719     case BLOCKED_FETCH:
2720     { 
2721         StgBlockedFetch *bf = (StgBlockedFetch *)p;
2722         // follow the pointer to the node which is being demanded 
2723         (StgClosure *)bf->node = 
2724             evacuate((StgClosure *)bf->node);
2725         // follow the link to the rest of the blocking queue 
2726         (StgClosure *)bf->link = 
2727             evacuate((StgClosure *)bf->link);
2728         IF_DEBUG(gc,
2729                  debugBelch("@@ scavenge: %p (%s); node is now %p; exciting, isn't it",
2730                        bf, info_type((StgClosure *)bf), 
2731                        bf->node, info_type(bf->node)));
2732         p += sizeofW(StgBlockedFetch);
2733         break;
2734     }
2735
2736 #ifdef DIST
2737     case REMOTE_REF:
2738 #endif
2739     case FETCH_ME:
2740         p += sizeofW(StgFetchMe);
2741         break; // nothing to do in this case
2742
2743     case FETCH_ME_BQ: // cf. BLACKHOLE_BQ
2744     { 
2745         StgFetchMeBlockingQueue *fmbq = (StgFetchMeBlockingQueue *)p;
2746         (StgClosure *)fmbq->blocking_queue = 
2747             evacuate((StgClosure *)fmbq->blocking_queue);
2748         IF_DEBUG(gc,
2749                  debugBelch("@@ scavenge: %p (%s) exciting, isn't it",
2750                        p, info_type((StgClosure *)p)));
2751         p += sizeofW(StgFetchMeBlockingQueue);
2752         break;
2753     }
2754 #endif
2755
2756     case TVAR_WAIT_QUEUE:
2757       {
2758         StgTVarWaitQueue *wq = ((StgTVarWaitQueue *) p);
2759         evac_gen = 0;
2760         wq->waiting_tso = (StgTSO *)evacuate((StgClosure*)wq->waiting_tso);
2761         wq->next_queue_entry = (StgTVarWaitQueue *)evacuate((StgClosure*)wq->next_queue_entry);
2762         wq->prev_queue_entry = (StgTVarWaitQueue *)evacuate((StgClosure*)wq->prev_queue_entry);
2763         evac_gen = saved_evac_gen;
2764         failed_to_evac = rtsTrue; // mutable
2765         p += sizeofW(StgTVarWaitQueue);
2766         break;
2767       }
2768
2769     case TVAR:
2770       {
2771         StgTVar *tvar = ((StgTVar *) p);
2772         evac_gen = 0;
2773         tvar->current_value = evacuate((StgClosure*)tvar->current_value);
2774         tvar->first_wait_queue_entry = (StgTVarWaitQueue *)evacuate((StgClosure*)tvar->first_wait_queue_entry);
2775         evac_gen = saved_evac_gen;
2776         failed_to_evac = rtsTrue; // mutable
2777         p += sizeofW(StgTVar);
2778         break;
2779       }
2780
2781     case TREC_HEADER:
2782       {
2783         StgTRecHeader *trec = ((StgTRecHeader *) p);
2784         evac_gen = 0;
2785         trec->enclosing_trec = (StgTRecHeader *)evacuate((StgClosure*)trec->enclosing_trec);
2786         trec->current_chunk = (StgTRecChunk *)evacuate((StgClosure*)trec->current_chunk);
2787         evac_gen = saved_evac_gen;
2788         failed_to_evac = rtsTrue; // mutable
2789         p += sizeofW(StgTRecHeader);
2790         break;
2791       }
2792
2793     case TREC_CHUNK:
2794       {
2795         StgWord i;
2796         StgTRecChunk *tc = ((StgTRecChunk *) p);
2797         TRecEntry *e = &(tc -> entries[0]);
2798         evac_gen = 0;
2799         tc->prev_chunk = (StgTRecChunk *)evacuate((StgClosure*)tc->prev_chunk);
2800         for (i = 0; i < tc -> next_entry_idx; i ++, e++ ) {
2801           e->tvar = (StgTVar *)evacuate((StgClosure*)e->tvar);
2802           e->expected_value = evacuate((StgClosure*)e->expected_value);
2803           e->new_value = evacuate((StgClosure*)e->new_value);
2804         }
2805         evac_gen = saved_evac_gen;
2806         failed_to_evac = rtsTrue; // mutable
2807         p += sizeofW(StgTRecChunk);
2808         break;
2809       }
2810
2811     default:
2812         barf("scavenge: unimplemented/strange closure type %d @ %p", 
2813              info->type, p);
2814     }
2815
2816     /*
2817      * We need to record the current object on the mutable list if
2818      *  (a) It is actually mutable, or 
2819      *  (b) It contains pointers to a younger generation.
2820      * Case (b) arises if we didn't manage to promote everything that
2821      * the current object points to into the current generation.
2822      */
2823     if (failed_to_evac) {
2824         failed_to_evac = rtsFalse;
2825         recordMutableGen((StgClosure *)q, stp->gen);
2826     }
2827   }
2828
2829   stp->scan_bd = bd;
2830   stp->scan = p;
2831 }    
2832
2833 /* -----------------------------------------------------------------------------
2834    Scavenge everything on the mark stack.
2835
2836    This is slightly different from scavenge():
2837       - we don't walk linearly through the objects, so the scavenger
2838         doesn't need to advance the pointer on to the next object.
2839    -------------------------------------------------------------------------- */
2840
2841 static void
2842 scavenge_mark_stack(void)
2843 {
2844     StgPtr p, q;
2845     StgInfoTable *info;
2846     nat saved_evac_gen;
2847
2848     evac_gen = oldest_gen->no;
2849     saved_evac_gen = evac_gen;
2850
2851 linear_scan:
2852     while (!mark_stack_empty()) {
2853         p = pop_mark_stack();
2854
2855         ASSERT(LOOKS_LIKE_CLOSURE_PTR(p));
2856         info = get_itbl((StgClosure *)p);
2857         
2858         q = p;
2859         switch (info->type) {
2860             
2861         case MVAR:
2862         {
2863             StgMVar *mvar = ((StgMVar *)p);
2864             evac_gen = 0;
2865             mvar->head = (StgTSO *)evacuate((StgClosure *)mvar->head);
2866             mvar->tail = (StgTSO *)evacuate((StgClosure *)mvar->tail);
2867             mvar->value = evacuate((StgClosure *)mvar->value);
2868             evac_gen = saved_evac_gen;
2869             failed_to_evac = rtsTrue; // mutable.
2870             break;
2871         }
2872
2873         case FUN_2_0:
2874             scavenge_fun_srt(info);
2875             ((StgClosure *)p)->payload[1] = evacuate(((StgClosure *)p)->payload[1]);
2876             ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2877             break;
2878
2879         case THUNK_2_0:
2880             scavenge_thunk_srt(info);
2881         case CONSTR_2_0:
2882             ((StgClosure *)p)->payload[1] = evacuate(((StgClosure *)p)->payload[1]);
2883             ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2884             break;
2885         
2886         case FUN_1_0:
2887         case FUN_1_1:
2888             scavenge_fun_srt(info);
2889             ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2890             break;
2891
2892         case THUNK_1_0:
2893         case THUNK_1_1:
2894             scavenge_thunk_srt(info);
2895         case CONSTR_1_0:
2896         case CONSTR_1_1:
2897             ((StgClosure *)p)->payload[0] = evacuate(((StgClosure *)p)->payload[0]);
2898             break;
2899         
2900         case FUN_0_1:
2901         case FUN_0_2:
2902             scavenge_fun_srt(info);
2903             break;
2904
2905         case THUNK_0_1:
2906         case THUNK_0_2:
2907             scavenge_thunk_srt(info);
2908             break;
2909
2910         case CONSTR_0_1:
2911         case CONSTR_0_2:
2912             break;
2913         
2914         case FUN:
2915             scavenge_fun_srt(info);
2916             goto gen_obj;
2917
2918         case THUNK:
2919             scavenge_thunk_srt(info);
2920             // fall through 
2921         
2922         gen_obj:
2923         case CONSTR:
2924         case WEAK:
2925         case FOREIGN:
2926         case STABLE_NAME:
2927         {
2928             StgPtr end;
2929             
2930             end = (P_)((StgClosure *)p)->payload + info->layout.payload.ptrs;
2931             for (p = (P_)((StgClosure *)p)->payload; p < end; p++) {
2932                 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
2933             }
2934             break;
2935         }
2936
2937         case BCO: {
2938             StgBCO *bco = (StgBCO *)p;
2939             bco->instrs = (StgArrWords *)evacuate((StgClosure *)bco->instrs);
2940             bco->literals = (StgArrWords *)evacuate((StgClosure *)bco->literals);
2941             bco->ptrs = (StgMutArrPtrs *)evacuate((StgClosure *)bco->ptrs);
2942             bco->itbls = (StgArrWords *)evacuate((StgClosure *)bco->itbls);
2943             break;
2944         }
2945
2946         case IND_PERM:
2947             // don't need to do anything here: the only possible case
2948             // is that we're in a 1-space compacting collector, with
2949             // no "old" generation.
2950             break;
2951
2952         case IND_OLDGEN:
2953         case IND_OLDGEN_PERM:
2954             ((StgInd *)p)->indirectee = 
2955                 evacuate(((StgInd *)p)->indirectee);
2956             break;
2957
2958         case MUT_VAR:
2959             evac_gen = 0;
2960             ((StgMutVar *)p)->var = evacuate(((StgMutVar *)p)->var);
2961             evac_gen = saved_evac_gen;
2962             failed_to_evac = rtsTrue;
2963             break;
2964
2965         case CAF_BLACKHOLE:
2966         case SE_CAF_BLACKHOLE:
2967         case SE_BLACKHOLE:
2968         case BLACKHOLE:
2969         case ARR_WORDS:
2970             break;
2971
2972         case BLACKHOLE_BQ:
2973         { 
2974             StgBlockingQueue *bh = (StgBlockingQueue *)p;
2975             bh->blocking_queue = 
2976                 (StgTSO *)evacuate((StgClosure *)bh->blocking_queue);
2977             failed_to_evac = rtsTrue;
2978             break;
2979         }
2980
2981         case THUNK_SELECTOR:
2982         { 
2983             StgSelector *s = (StgSelector *)p;
2984             s->selectee = evacuate(s->selectee);
2985             break;
2986         }
2987
2988         // A chunk of stack saved in a heap object
2989         case AP_STACK:
2990         {
2991             StgAP_STACK *ap = (StgAP_STACK *)p;
2992             
2993             ap->fun = evacuate(ap->fun);
2994             scavenge_stack((StgPtr)ap->payload, (StgPtr)ap->payload + ap->size);
2995             break;
2996         }
2997
2998         case PAP:
2999         case AP:
3000             scavenge_PAP((StgPAP *)p);
3001             break;
3002       
3003         case MUT_ARR_PTRS:
3004             // follow everything 
3005         {
3006             StgPtr next;
3007             
3008             evac_gen = 0;               // repeatedly mutable 
3009             next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
3010             for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
3011                 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
3012             }
3013             evac_gen = saved_evac_gen;
3014             failed_to_evac = rtsTrue; // mutable anyhow.
3015             break;
3016         }
3017
3018         case MUT_ARR_PTRS_FROZEN:
3019         case MUT_ARR_PTRS_FROZEN0:
3020             // follow everything 
3021         {
3022             StgPtr next;
3023             
3024             next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
3025             for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
3026                 *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
3027             }
3028             break;
3029         }
3030
3031         case TSO:
3032         { 
3033             StgTSO *tso = (StgTSO *)p;
3034             evac_gen = 0;
3035             scavengeTSO(tso);
3036             evac_gen = saved_evac_gen;
3037             failed_to_evac = rtsTrue;
3038             break;
3039         }
3040
3041 #if defined(PAR)
3042         case RBH: // cf. BLACKHOLE_BQ
3043         { 
3044 #if 0
3045             nat size, ptrs, nonptrs, vhs;
3046             char str[80];
3047             StgInfoTable *rip = get_closure_info(p, &size, &ptrs, &nonptrs, &vhs, str);
3048 #endif
3049             StgRBH *rbh = (StgRBH *)p;
3050             bh->blocking_queue = 
3051                 (StgTSO *)evacuate((StgClosure *)bh->blocking_queue);
3052             failed_to_evac = rtsTrue;  // mutable anyhow.
3053             IF_DEBUG(gc,
3054                      debugBelch("@@ scavenge: RBH %p (%s) (new blocking_queue link=%p)",
3055                            p, info_type(p), (StgClosure *)rbh->blocking_queue));
3056             break;
3057         }
3058         
3059         case BLOCKED_FETCH:
3060         { 
3061             StgBlockedFetch *bf = (StgBlockedFetch *)p;
3062             // follow the pointer to the node which is being demanded 
3063             (StgClosure *)bf->node = 
3064                 evacuate((StgClosure *)bf->node);
3065             // follow the link to the rest of the blocking queue 
3066             (StgClosure *)bf->link = 
3067                 evacuate((StgClosure *)bf->link);
3068             IF_DEBUG(gc,
3069                      debugBelch("@@ scavenge: %p (%s); node is now %p; exciting, isn't it",
3070                            bf, info_type((StgClosure *)bf), 
3071                            bf->node, info_type(bf->node)));
3072             break;
3073         }
3074
3075 #ifdef DIST
3076         case REMOTE_REF:
3077 #endif
3078         case FETCH_ME:
3079             break; // nothing to do in this case
3080
3081         case FETCH_ME_BQ: // cf. BLACKHOLE_BQ
3082         { 
3083             StgFetchMeBlockingQueue *fmbq = (StgFetchMeBlockingQueue *)p;
3084             (StgClosure *)fmbq->blocking_queue = 
3085                 evacuate((StgClosure *)fmbq->blocking_queue);
3086             IF_DEBUG(gc,
3087                      debugBelch("@@ scavenge: %p (%s) exciting, isn't it",
3088                            p, info_type((StgClosure *)p)));
3089             break;
3090         }
3091 #endif // PAR
3092
3093         case TVAR_WAIT_QUEUE:
3094           {
3095             StgTVarWaitQueue *wq = ((StgTVarWaitQueue *) p);
3096             evac_gen = 0;
3097             wq->waiting_tso = (StgTSO *)evacuate((StgClosure*)wq->waiting_tso);
3098             wq->next_queue_entry = (StgTVarWaitQueue *)evacuate((StgClosure*)wq->next_queue_entry);
3099             wq->prev_queue_entry = (StgTVarWaitQueue *)evacuate((StgClosure*)wq->prev_queue_entry);
3100             evac_gen = saved_evac_gen;
3101             failed_to_evac = rtsTrue; // mutable
3102             break;
3103           }
3104           
3105         case TVAR:
3106           {
3107             StgTVar *tvar = ((StgTVar *) p);
3108             evac_gen = 0;
3109             tvar->current_value = evacuate((StgClosure*)tvar->current_value);
3110             tvar->first_wait_queue_entry = (StgTVarWaitQueue *)evacuate((StgClosure*)tvar->first_wait_queue_entry);
3111             evac_gen = saved_evac_gen;
3112             failed_to_evac = rtsTrue; // mutable
3113             break;
3114           }
3115           
3116         case TREC_CHUNK:
3117           {
3118             StgWord i;
3119             StgTRecChunk *tc = ((StgTRecChunk *) p);
3120             TRecEntry *e = &(tc -> entries[0]);
3121             evac_gen = 0;
3122             tc->prev_chunk = (StgTRecChunk *)evacuate((StgClosure*)tc->prev_chunk);
3123             for (i = 0; i < tc -> next_entry_idx; i ++, e++ ) {
3124               e->tvar = (StgTVar *)evacuate((StgClosure*)e->tvar);
3125               e->expected_value = evacuate((StgClosure*)e->expected_value);
3126               e->new_value = evacuate((StgClosure*)e->new_value);
3127             }
3128             evac_gen = saved_evac_gen;
3129             failed_to_evac = rtsTrue; // mutable
3130             break;
3131           }
3132
3133         case TREC_HEADER:
3134           {
3135             StgTRecHeader *trec = ((StgTRecHeader *) p);
3136             evac_gen = 0;
3137             trec->enclosing_trec = (StgTRecHeader *)evacuate((StgClosure*)trec->enclosing_trec);
3138             trec->current_chunk = (StgTRecChunk *)evacuate((StgClosure*)trec->current_chunk);
3139             evac_gen = saved_evac_gen;
3140             failed_to_evac = rtsTrue; // mutable
3141             break;
3142           }
3143
3144         default:
3145             barf("scavenge_mark_stack: unimplemented/strange closure type %d @ %p", 
3146                  info->type, p);
3147         }
3148
3149         if (failed_to_evac) {
3150             failed_to_evac = rtsFalse;
3151             recordMutableGen((StgClosure *)q, &generations[evac_gen]);
3152         }
3153         
3154         // mark the next bit to indicate "scavenged"
3155         mark(q+1, Bdescr(q));
3156
3157     } // while (!mark_stack_empty())
3158
3159     // start a new linear scan if the mark stack overflowed at some point
3160     if (mark_stack_overflowed && oldgen_scan_bd == NULL) {
3161         IF_DEBUG(gc, debugBelch("scavenge_mark_stack: starting linear scan"));
3162         mark_stack_overflowed = rtsFalse;
3163         oldgen_scan_bd = oldest_gen->steps[0].blocks;
3164         oldgen_scan = oldgen_scan_bd->start;
3165     }
3166
3167     if (oldgen_scan_bd) {
3168         // push a new thing on the mark stack
3169     loop:
3170         // find a closure that is marked but not scavenged, and start
3171         // from there.
3172         while (oldgen_scan < oldgen_scan_bd->free 
3173                && !is_marked(oldgen_scan,oldgen_scan_bd)) {
3174             oldgen_scan++;
3175         }
3176
3177         if (oldgen_scan < oldgen_scan_bd->free) {
3178
3179             // already scavenged?
3180             if (is_marked(oldgen_scan+1,oldgen_scan_bd)) {
3181                 oldgen_scan += sizeofW(StgHeader) + MIN_NONUPD_SIZE;
3182                 goto loop;
3183             }
3184             push_mark_stack(oldgen_scan);
3185             // ToDo: bump the linear scan by the actual size of the object
3186             oldgen_scan += sizeofW(StgHeader) + MIN_NONUPD_SIZE;
3187             goto linear_scan;
3188         }
3189
3190         oldgen_scan_bd = oldgen_scan_bd->link;
3191         if (oldgen_scan_bd != NULL) {
3192             oldgen_scan = oldgen_scan_bd->start;
3193             goto loop;
3194         }
3195     }
3196 }
3197
3198 /* -----------------------------------------------------------------------------
3199    Scavenge one object.
3200
3201    This is used for objects that are temporarily marked as mutable
3202    because they contain old-to-new generation pointers.  Only certain
3203    objects can have this property.
3204    -------------------------------------------------------------------------- */
3205
3206 static rtsBool
3207 scavenge_one(StgPtr p)
3208 {
3209     const StgInfoTable *info;
3210     nat saved_evac_gen = evac_gen;
3211     rtsBool no_luck;
3212     
3213     ASSERT(LOOKS_LIKE_CLOSURE_PTR(p));
3214     info = get_itbl((StgClosure *)p);
3215     
3216     switch (info->type) {
3217         
3218     case MVAR:
3219     { 
3220         StgMVar *mvar = ((StgMVar *)p);
3221         evac_gen = 0;
3222         mvar->head = (StgTSO *)evacuate((StgClosure *)mvar->head);
3223         mvar->tail = (StgTSO *)evacuate((StgClosure *)mvar->tail);
3224         mvar->value = evacuate((StgClosure *)mvar->value);
3225         evac_gen = saved_evac_gen;
3226         failed_to_evac = rtsTrue; // mutable.
3227         break;
3228     }
3229
3230     case FUN:
3231     case FUN_1_0:                       // hardly worth specialising these guys
3232     case FUN_0_1:
3233     case FUN_1_1:
3234     case FUN_0_2:
3235     case FUN_2_0:
3236     case THUNK:
3237     case THUNK_1_0:
3238     case THUNK_0_1:
3239     case THUNK_1_1:
3240     case THUNK_0_2:
3241     case THUNK_2_0:
3242     case CONSTR:
3243     case CONSTR_1_0:
3244     case CONSTR_0_1:
3245     case CONSTR_1_1:
3246     case CONSTR_0_2:
3247     case CONSTR_2_0:
3248     case WEAK:
3249     case FOREIGN:
3250     case IND_PERM:
3251     {
3252         StgPtr q, end;
3253         
3254         end = (StgPtr)((StgClosure *)p)->payload + info->layout.payload.ptrs;
3255         for (q = (StgPtr)((StgClosure *)p)->payload; q < end; q++) {
3256             *q = (StgWord)(StgPtr)evacuate((StgClosure *)*q);
3257         }
3258         break;
3259     }
3260     
3261     case MUT_VAR:
3262         evac_gen = 0;
3263         ((StgMutVar *)p)->var = evacuate(((StgMutVar *)p)->var);
3264         evac_gen = saved_evac_gen;
3265         failed_to_evac = rtsTrue; // mutable anyhow
3266         break;
3267
3268     case CAF_BLACKHOLE:
3269     case SE_CAF_BLACKHOLE:
3270     case SE_BLACKHOLE:
3271     case BLACKHOLE:
3272         break;
3273         
3274     case BLACKHOLE_BQ:
3275     { 
3276         StgBlockingQueue *bh = (StgBlockingQueue *)p;
3277         evac_gen = 0;           // repeatedly mutable 
3278         bh->blocking_queue = 
3279             (StgTSO *)evacuate((StgClosure *)bh->blocking_queue);
3280         failed_to_evac = rtsTrue;
3281         break;
3282     }
3283
3284     case THUNK_SELECTOR:
3285     { 
3286         StgSelector *s = (StgSelector *)p;
3287         s->selectee = evacuate(s->selectee);
3288         break;
3289     }
3290     
3291     case AP_STACK:
3292     {
3293         StgAP_STACK *ap = (StgAP_STACK *)p;
3294
3295         ap->fun = evacuate(ap->fun);
3296         scavenge_stack((StgPtr)ap->payload, (StgPtr)ap->payload + ap->size);
3297         p = (StgPtr)ap->payload + ap->size;
3298         break;
3299     }
3300
3301     case PAP:
3302     case AP:
3303         p = scavenge_PAP((StgPAP *)p);
3304         break;
3305
3306     case ARR_WORDS:
3307         // nothing to follow 
3308         break;
3309
3310     case MUT_ARR_PTRS:
3311     {
3312         // follow everything 
3313         StgPtr next;
3314       
3315         evac_gen = 0;           // repeatedly mutable 
3316         next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
3317         for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
3318             *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
3319         }
3320         evac_gen = saved_evac_gen;
3321         failed_to_evac = rtsTrue;
3322         break;
3323     }
3324
3325     case MUT_ARR_PTRS_FROZEN:
3326     case MUT_ARR_PTRS_FROZEN0:
3327     {
3328         // follow everything 
3329         StgPtr next;
3330       
3331         next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
3332         for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
3333             *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
3334         }
3335         break;
3336     }
3337
3338     case TSO:
3339     {
3340         StgTSO *tso = (StgTSO *)p;
3341       
3342         evac_gen = 0;           // repeatedly mutable 
3343         scavengeTSO(tso);
3344         evac_gen = saved_evac_gen;
3345         failed_to_evac = rtsTrue;
3346         break;
3347     }
3348   
3349 #if defined(PAR)
3350     case RBH: // cf. BLACKHOLE_BQ
3351     { 
3352 #if 0
3353         nat size, ptrs, nonptrs, vhs;
3354         char str[80];
3355         StgInfoTable *rip = get_closure_info(p, &size, &ptrs, &nonptrs, &vhs, str);
3356 #endif
3357         StgRBH *rbh = (StgRBH *)p;
3358         (StgClosure *)rbh->blocking_queue = 
3359             evacuate((StgClosure *)rbh->blocking_queue);
3360         failed_to_evac = rtsTrue;  // mutable anyhow.
3361         IF_DEBUG(gc,
3362                  debugBelch("@@ scavenge: RBH %p (%s) (new blocking_queue link=%p)",
3363                        p, info_type(p), (StgClosure *)rbh->blocking_queue));
3364         // ToDo: use size of reverted closure here!
3365         break;
3366     }
3367
3368     case BLOCKED_FETCH:
3369     { 
3370         StgBlockedFetch *bf = (StgBlockedFetch *)p;
3371         // follow the pointer to the node which is being demanded 
3372         (StgClosure *)bf->node = 
3373             evacuate((StgClosure *)bf->node);
3374         // follow the link to the rest of the blocking queue 
3375         (StgClosure *)bf->link = 
3376             evacuate((StgClosure *)bf->link);
3377         IF_DEBUG(gc,
3378                  debugBelch("@@ scavenge: %p (%s); node is now %p; exciting, isn't it",
3379                        bf, info_type((StgClosure *)bf), 
3380                        bf->node, info_type(bf->node)));
3381         break;
3382     }
3383
3384 #ifdef DIST
3385     case REMOTE_REF:
3386 #endif
3387     case FETCH_ME:
3388         break; // nothing to do in this case
3389
3390     case FETCH_ME_BQ: // cf. BLACKHOLE_BQ
3391     { 
3392         StgFetchMeBlockingQueue *fmbq = (StgFetchMeBlockingQueue *)p;
3393         (StgClosure *)fmbq->blocking_queue = 
3394             evacuate((StgClosure *)fmbq->blocking_queue);
3395         IF_DEBUG(gc,
3396                  debugBelch("@@ scavenge: %p (%s) exciting, isn't it",
3397                        p, info_type((StgClosure *)p)));
3398         break;
3399     }
3400 #endif
3401
3402     case TVAR_WAIT_QUEUE:
3403       {
3404         StgTVarWaitQueue *wq = ((StgTVarWaitQueue *) p);
3405         evac_gen = 0;
3406         wq->waiting_tso = (StgTSO *)evacuate((StgClosure*)wq->waiting_tso);
3407         wq->next_queue_entry = (StgTVarWaitQueue *)evacuate((StgClosure*)wq->next_queue_entry);
3408         wq->prev_queue_entry = (StgTVarWaitQueue *)evacuate((StgClosure*)wq->prev_queue_entry);
3409         evac_gen = saved_evac_gen;
3410         failed_to_evac = rtsTrue; // mutable
3411         break;
3412       }
3413
3414     case TVAR:
3415       {
3416         StgTVar *tvar = ((StgTVar *) p);
3417         evac_gen = 0;
3418         tvar->current_value = evacuate((StgClosure*)tvar->current_value);
3419         tvar->first_wait_queue_entry = (StgTVarWaitQueue *)evacuate((StgClosure*)tvar->first_wait_queue_entry);
3420         evac_gen = saved_evac_gen;
3421         failed_to_evac = rtsTrue; // mutable
3422         break;
3423       }
3424
3425     case TREC_HEADER:
3426       {
3427         StgTRecHeader *trec = ((StgTRecHeader *) p);
3428         evac_gen = 0;
3429         trec->enclosing_trec = (StgTRecHeader *)evacuate((StgClosure*)trec->enclosing_trec);
3430         trec->current_chunk = (StgTRecChunk *)evacuate((StgClosure*)trec->current_chunk);
3431         evac_gen = saved_evac_gen;
3432         failed_to_evac = rtsTrue; // mutable
3433         break;
3434       }
3435
3436     case TREC_CHUNK:
3437       {
3438         StgWord i;
3439         StgTRecChunk *tc = ((StgTRecChunk *) p);
3440         TRecEntry *e = &(tc -> entries[0]);
3441         evac_gen = 0;
3442         tc->prev_chunk = (StgTRecChunk *)evacuate((StgClosure*)tc->prev_chunk);
3443         for (i = 0; i < tc -> next_entry_idx; i ++, e++ ) {
3444           e->tvar = (StgTVar *)evacuate((StgClosure*)e->tvar);
3445           e->expected_value = evacuate((StgClosure*)e->expected_value);
3446           e->new_value = evacuate((StgClosure*)e->new_value);
3447         }
3448         evac_gen = saved_evac_gen;
3449         failed_to_evac = rtsTrue; // mutable
3450         break;
3451       }
3452
3453     case IND_OLDGEN:
3454     case IND_OLDGEN_PERM:
3455     case IND_STATIC:
3456     {
3457         /* Careful here: a THUNK can be on the mutable list because
3458          * it contains pointers to young gen objects.  If such a thunk
3459          * is updated, the IND_OLDGEN will be added to the mutable
3460          * list again, and we'll scavenge it twice.  evacuate()
3461          * doesn't check whether the object has already been
3462          * evacuated, so we perform that check here.
3463          */
3464         StgClosure *q = ((StgInd *)p)->indirectee;
3465         if (HEAP_ALLOCED(q) && Bdescr((StgPtr)q)->flags & BF_EVACUATED) {
3466             break;
3467         }
3468         ((StgInd *)p)->indirectee = evacuate(q);
3469     }
3470
3471 #if 0 && defined(DEBUG)
3472       if (RtsFlags.DebugFlags.gc) 
3473       /* Debugging code to print out the size of the thing we just
3474        * promoted 
3475        */
3476       { 
3477         StgPtr start = gen->steps[0].scan;
3478         bdescr *start_bd = gen->steps[0].scan_bd;
3479         nat size = 0;
3480         scavenge(&gen->steps[0]);
3481         if (start_bd != gen->steps[0].scan_bd) {
3482           size += (P_)BLOCK_ROUND_UP(start) - start;
3483           start_bd = start_bd->link;
3484           while (start_bd != gen->steps[0].scan_bd) {
3485             size += BLOCK_SIZE_W;
3486             start_bd = start_bd->link;
3487           }
3488           size += gen->steps[0].scan -
3489             (P_)BLOCK_ROUND_DOWN(gen->steps[0].scan);
3490         } else {
3491           size = gen->steps[0].scan - start;
3492         }
3493         debugBelch("evac IND_OLDGEN: %ld bytes", size * sizeof(W_));
3494       }
3495 #endif
3496       break;
3497
3498     default:
3499         barf("scavenge_one: strange object %d", (int)(info->type));
3500     }    
3501
3502     no_luck = failed_to_evac;
3503     failed_to_evac = rtsFalse;
3504     return (no_luck);
3505 }
3506
3507 /* -----------------------------------------------------------------------------
3508    Scavenging mutable lists.
3509
3510    We treat the mutable list of each generation > N (i.e. all the
3511    generations older than the one being collected) as roots.  We also
3512    remove non-mutable objects from the mutable list at this point.
3513    -------------------------------------------------------------------------- */
3514
3515 static void
3516 scavenge_mutable_list(generation *gen)
3517 {
3518     bdescr *bd;
3519     StgPtr p, q;
3520
3521     bd = gen->saved_mut_list;
3522
3523     evac_gen = gen->no;
3524     for (; bd != NULL; bd = bd->link) {
3525         for (q = bd->start; q < bd->free; q++) {
3526             p = (StgPtr)*q;
3527             ASSERT(LOOKS_LIKE_CLOSURE_PTR(p));
3528             if (scavenge_one(p)) {
3529                 /* didn't manage to promote everything, so put the
3530                  * object back on the list.
3531                  */
3532                 recordMutableGen((StgClosure *)p,gen);
3533             }
3534         }
3535     }
3536
3537     // free the old mut_list
3538     freeChain(gen->saved_mut_list);
3539     gen->saved_mut_list = NULL;
3540 }
3541
3542
3543 static void
3544 scavenge_static(void)
3545 {
3546   StgClosure* p = static_objects;
3547   const StgInfoTable *info;
3548
3549   /* Always evacuate straight to the oldest generation for static
3550    * objects */
3551   evac_gen = oldest_gen->no;
3552
3553   /* keep going until we've scavenged all the objects on the linked
3554      list... */
3555   while (p != END_OF_STATIC_LIST) {
3556
3557     ASSERT(LOOKS_LIKE_CLOSURE_PTR(p));
3558     info = get_itbl(p);
3559     /*
3560     if (info->type==RBH)
3561       info = REVERT_INFOPTR(info); // if it's an RBH, look at the orig closure
3562     */
3563     // make sure the info pointer is into text space 
3564     
3565     /* Take this object *off* the static_objects list,
3566      * and put it on the scavenged_static_objects list.
3567      */
3568     static_objects = STATIC_LINK(info,p);
3569     STATIC_LINK(info,p) = scavenged_static_objects;
3570     scavenged_static_objects = p;
3571     
3572     switch (info -> type) {
3573       
3574     case IND_STATIC:
3575       {
3576         StgInd *ind = (StgInd *)p;
3577         ind->indirectee = evacuate(ind->indirectee);
3578
3579         /* might fail to evacuate it, in which case we have to pop it
3580          * back on the mutable list of the oldest generation.  We
3581          * leave it *on* the scavenged_static_objects list, though,
3582          * in case we visit this object again.
3583          */
3584         if (failed_to_evac) {
3585           failed_to_evac = rtsFalse;
3586           recordMutableGen((StgClosure *)p,oldest_gen);
3587         }
3588         break;
3589       }
3590       
3591     case THUNK_STATIC:
3592       scavenge_thunk_srt(info);
3593       break;
3594
3595     case FUN_STATIC:
3596       scavenge_fun_srt(info);
3597       break;
3598       
3599     case CONSTR_STATIC:
3600       { 
3601         StgPtr q, next;
3602         
3603         next = (P_)p->payload + info->layout.payload.ptrs;
3604         // evacuate the pointers 
3605         for (q = (P_)p->payload; q < next; q++) {
3606             *q = (StgWord)(StgPtr)evacuate((StgClosure *)*q);
3607         }
3608         break;
3609       }
3610       
3611     default:
3612       barf("scavenge_static: strange closure %d", (int)(info->type));
3613     }
3614
3615     ASSERT(failed_to_evac == rtsFalse);
3616
3617     /* get the next static object from the list.  Remember, there might
3618      * be more stuff on this list now that we've done some evacuating!
3619      * (static_objects is a global)
3620      */
3621     p = static_objects;
3622   }
3623 }
3624
3625 /* -----------------------------------------------------------------------------
3626    scavenge a chunk of memory described by a bitmap
3627    -------------------------------------------------------------------------- */
3628
3629 static void
3630 scavenge_large_bitmap( StgPtr p, StgLargeBitmap *large_bitmap, nat size )
3631 {
3632     nat i, b;
3633     StgWord bitmap;
3634     
3635     b = 0;
3636     bitmap = large_bitmap->bitmap[b];
3637     for (i = 0; i < size; ) {
3638         if ((bitmap & 1) == 0) {
3639             *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
3640         }
3641         i++;
3642         p++;
3643         if (i % BITS_IN(W_) == 0) {
3644             b++;
3645             bitmap = large_bitmap->bitmap[b];
3646         } else {
3647             bitmap = bitmap >> 1;
3648         }
3649     }
3650 }
3651
3652 STATIC_INLINE StgPtr
3653 scavenge_small_bitmap (StgPtr p, nat size, StgWord bitmap)
3654 {
3655     while (size > 0) {
3656         if ((bitmap & 1) == 0) {
3657             *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
3658         }
3659         p++;
3660         bitmap = bitmap >> 1;
3661         size--;
3662     }
3663     return p;
3664 }
3665
3666 /* -----------------------------------------------------------------------------
3667    scavenge_stack walks over a section of stack and evacuates all the
3668    objects pointed to by it.  We can use the same code for walking
3669    AP_STACK_UPDs, since these are just sections of copied stack.
3670    -------------------------------------------------------------------------- */
3671
3672
3673 static void
3674 scavenge_stack(StgPtr p, StgPtr stack_end)
3675 {
3676   const StgRetInfoTable* info;
3677   StgWord bitmap;
3678   nat size;
3679
3680   //IF_DEBUG(sanity, debugBelch("  scavenging stack between %p and %p", p, stack_end));
3681
3682   /* 
3683    * Each time around this loop, we are looking at a chunk of stack
3684    * that starts with an activation record. 
3685    */
3686
3687   while (p < stack_end) {
3688     info  = get_ret_itbl((StgClosure *)p);
3689       
3690     switch (info->i.type) {
3691         
3692     case UPDATE_FRAME:
3693         ((StgUpdateFrame *)p)->updatee 
3694             = evacuate(((StgUpdateFrame *)p)->updatee);
3695         p += sizeofW(StgUpdateFrame);
3696         continue;
3697
3698       // small bitmap (< 32 entries, or 64 on a 64-bit machine) 
3699     case CATCH_STM_FRAME:
3700     case CATCH_RETRY_FRAME:
3701     case ATOMICALLY_FRAME:
3702     case STOP_FRAME:
3703     case CATCH_FRAME:
3704     case RET_SMALL:
3705     case RET_VEC_SMALL:
3706         bitmap = BITMAP_BITS(info->i.layout.bitmap);
3707         size   = BITMAP_SIZE(info->i.layout.bitmap);
3708         // NOTE: the payload starts immediately after the info-ptr, we
3709         // don't have an StgHeader in the same sense as a heap closure.
3710         p++;
3711         p = scavenge_small_bitmap(p, size, bitmap);
3712
3713     follow_srt:
3714         scavenge_srt((StgClosure **)GET_SRT(info), info->i.srt_bitmap);
3715         continue;
3716
3717     case RET_BCO: {
3718         StgBCO *bco;
3719         nat size;
3720
3721         p++;
3722         *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
3723         bco = (StgBCO *)*p;
3724         p++;
3725         size = BCO_BITMAP_SIZE(bco);
3726         scavenge_large_bitmap(p, BCO_BITMAP(bco), size);
3727         p += size;
3728         continue;
3729     }
3730
3731       // large bitmap (> 32 entries, or > 64 on a 64-bit machine) 
3732     case RET_BIG:
3733     case RET_VEC_BIG:
3734     {
3735         nat size;
3736
3737         size = GET_LARGE_BITMAP(&info->i)->size;
3738         p++;
3739         scavenge_large_bitmap(p, GET_LARGE_BITMAP(&info->i), size);
3740         p += size;
3741         // and don't forget to follow the SRT 
3742         goto follow_srt;
3743     }
3744
3745       // Dynamic bitmap: the mask is stored on the stack, and
3746       // there are a number of non-pointers followed by a number
3747       // of pointers above the bitmapped area.  (see StgMacros.h,
3748       // HEAP_CHK_GEN).
3749     case RET_DYN:
3750     {
3751         StgWord dyn;
3752         dyn = ((StgRetDyn *)p)->liveness;
3753
3754         // traverse the bitmap first
3755         bitmap = RET_DYN_LIVENESS(dyn);
3756         p      = (P_)&((StgRetDyn *)p)->payload[0];
3757         size   = RET_DYN_BITMAP_SIZE;
3758         p = scavenge_small_bitmap(p, size, bitmap);
3759
3760         // skip over the non-ptr words
3761         p += RET_DYN_NONPTRS(dyn) + RET_DYN_NONPTR_REGS_SIZE;
3762         
3763         // follow the ptr words
3764         for (size = RET_DYN_PTRS(dyn); size > 0; size--) {
3765             *p = (StgWord)(StgPtr)evacuate((StgClosure *)*p);
3766             p++;
3767         }
3768         continue;
3769     }
3770
3771     case RET_FUN:
3772     {
3773         StgRetFun *ret_fun = (StgRetFun *)p;
3774         StgFunInfoTable *fun_info;
3775
3776         ret_fun->fun = evacuate(ret_fun->fun);
3777         fun_info = get_fun_itbl(ret_fun->fun);
3778         p = scavenge_arg_block(fun_info, ret_fun->payload);
3779         goto follow_srt;
3780     }
3781
3782     default:
3783         barf("scavenge_stack: weird activation record found on stack: %d", (int)(info->i.type));
3784     }
3785   }                  
3786 }
3787
3788 /*-----------------------------------------------------------------------------
3789   scavenge the large object list.
3790
3791   evac_gen set by caller; similar games played with evac_gen as with
3792   scavenge() - see comment at the top of scavenge().  Most large
3793   objects are (repeatedly) mutable, so most of the time evac_gen will
3794   be zero.
3795   --------------------------------------------------------------------------- */
3796
3797 static void
3798 scavenge_large(step *stp)
3799 {
3800   bdescr *bd;
3801   StgPtr p;
3802
3803   bd = stp->new_large_objects;
3804
3805   for (; bd != NULL; bd = stp->new_large_objects) {
3806
3807     /* take this object *off* the large objects list and put it on
3808      * the scavenged large objects list.  This is so that we can
3809      * treat new_large_objects as a stack and push new objects on
3810      * the front when evacuating.
3811      */
3812     stp->new_large_objects = bd->link;
3813     dbl_link_onto(bd, &stp->scavenged_large_objects);
3814
3815     // update the block count in this step.
3816     stp->n_scavenged_large_blocks += bd->blocks;
3817
3818     p = bd->start;
3819     if (scavenge_one(p)) {
3820         recordMutableGen((StgClosure *)p, stp->gen);
3821     }
3822   }
3823 }
3824
3825 /* -----------------------------------------------------------------------------
3826    Initialising the static object & mutable lists
3827    -------------------------------------------------------------------------- */
3828
3829 static void
3830 zero_static_object_list(StgClosure* first_static)
3831 {
3832   StgClosure* p;
3833   StgClosure* link;
3834   const StgInfoTable *info;
3835
3836   for (p = first_static; p != END_OF_STATIC_LIST; p = link) {
3837     info = get_itbl(p);
3838     link = STATIC_LINK(info, p);
3839     STATIC_LINK(info,p) = NULL;
3840   }
3841 }
3842
3843 /* -----------------------------------------------------------------------------
3844    Reverting CAFs
3845    -------------------------------------------------------------------------- */
3846
3847 void
3848 revertCAFs( void )
3849 {
3850     StgIndStatic *c;
3851
3852     for (c = (StgIndStatic *)caf_list; c != NULL; 
3853          c = (StgIndStatic *)c->static_link) 
3854     {
3855         SET_INFO(c, c->saved_info);
3856         c->saved_info = NULL;
3857         // could, but not necessary: c->static_link = NULL; 
3858     }
3859     caf_list = NULL;
3860 }
3861
3862 void
3863 markCAFs( evac_fn evac )
3864 {
3865     StgIndStatic *c;
3866
3867     for (c = (StgIndStatic *)caf_list; c != NULL; 
3868          c = (StgIndStatic *)c->static_link) 
3869     {
3870         evac(&c->indirectee);
3871     }
3872 }
3873
3874 /* -----------------------------------------------------------------------------
3875    Sanity code for CAF garbage collection.
3876
3877    With DEBUG turned on, we manage a CAF list in addition to the SRT
3878    mechanism.  After GC, we run down the CAF list and blackhole any
3879    CAFs which have been garbage collected.  This means we get an error
3880    whenever the program tries to enter a garbage collected CAF.
3881
3882    Any garbage collected CAFs are taken off the CAF list at the same
3883    time. 
3884    -------------------------------------------------------------------------- */
3885
3886 #if 0 && defined(DEBUG)
3887
3888 static void
3889 gcCAFs(void)
3890 {
3891   StgClosure*  p;
3892   StgClosure** pp;
3893   const StgInfoTable *info;
3894   nat i;
3895
3896   i = 0;
3897   p = caf_list;
3898   pp = &caf_list;
3899
3900   while (p != NULL) {
3901     
3902     info = get_itbl(p);
3903
3904     ASSERT(info->type == IND_STATIC);
3905
3906     if (STATIC_LINK(info,p) == NULL) {
3907       IF_DEBUG(gccafs, debugBelch("CAF gc'd at 0x%04lx", (long)p));
3908       // black hole it 
3909       SET_INFO(p,&stg_BLACKHOLE_info);
3910       p = STATIC_LINK2(info,p);
3911       *pp = p;
3912     }
3913     else {
3914       pp = &STATIC_LINK2(info,p);
3915       p = *pp;
3916       i++;
3917     }
3918
3919   }
3920
3921   //  debugBelch("%d CAFs live", i); 
3922 }
3923 #endif
3924
3925
3926 /* -----------------------------------------------------------------------------
3927    Lazy black holing.
3928
3929    Whenever a thread returns to the scheduler after possibly doing
3930    some work, we have to run down the stack and black-hole all the
3931    closures referred to by update frames.
3932    -------------------------------------------------------------------------- */
3933
3934 static void
3935 threadLazyBlackHole(StgTSO *tso)
3936 {
3937     StgClosure *frame;
3938     StgRetInfoTable *info;
3939     StgBlockingQueue *bh;
3940     StgPtr stack_end;
3941     
3942     stack_end = &tso->stack[tso->stack_size];
3943     
3944     frame = (StgClosure *)tso->sp;
3945
3946     while (1) {
3947         info = get_ret_itbl(frame);
3948         
3949         switch (info->i.type) {
3950             
3951         case UPDATE_FRAME:
3952             bh = (StgBlockingQueue *)((StgUpdateFrame *)frame)->updatee;
3953             
3954             /* if the thunk is already blackholed, it means we've also
3955              * already blackholed the rest of the thunks on this stack,
3956              * so we can stop early.
3957              *
3958              * The blackhole made for a CAF is a CAF_BLACKHOLE, so they
3959              * don't interfere with this optimisation.
3960              */
3961             if (bh->header.info == &stg_BLACKHOLE_info) {
3962                 return;
3963             }
3964             
3965             if (bh->header.info != &stg_BLACKHOLE_BQ_info &&
3966                 bh->header.info != &stg_CAF_BLACKHOLE_info) {
3967 #if (!defined(LAZY_BLACKHOLING)) && defined(DEBUG)
3968                 debugBelch("Unexpected lazy BHing required at 0x%04x",(int)bh);
3969 #endif
3970 #ifdef PROFILING
3971                 // @LDV profiling
3972                 // We pretend that bh is now dead.
3973                 LDV_recordDead_FILL_SLOP_DYNAMIC((StgClosure *)bh);
3974 #endif
3975                 SET_INFO(bh,&stg_BLACKHOLE_info);
3976
3977                 // We pretend that bh has just been created.
3978                 LDV_RECORD_CREATE(bh);
3979             }
3980             
3981             frame = (StgClosure *) ((StgUpdateFrame *)frame + 1);
3982             break;
3983             
3984         case STOP_FRAME:
3985             return;
3986             
3987             // normal stack frames; do nothing except advance the pointer
3988         default:
3989             frame = (StgClosure *)((StgPtr)frame + stack_frame_sizeW(frame));
3990         }
3991     }
3992 }
3993
3994
3995 /* -----------------------------------------------------------------------------
3996  * Stack squeezing
3997  *
3998  * Code largely pinched from old RTS, then hacked to bits.  We also do
3999  * lazy black holing here.
4000  *
4001  * -------------------------------------------------------------------------- */
4002
4003 struct stack_gap { StgWord gap_size; struct stack_gap *next_gap; };
4004
4005 static void
4006 threadSqueezeStack(StgTSO *tso)
4007 {
4008     StgPtr frame;
4009     rtsBool prev_was_update_frame;
4010     StgClosure *updatee = NULL;
4011     StgPtr bottom;
4012     StgRetInfoTable *info;
4013     StgWord current_gap_size;
4014     struct stack_gap *gap;
4015
4016     // Stage 1: 
4017     //    Traverse the stack upwards, replacing adjacent update frames
4018     //    with a single update frame and a "stack gap".  A stack gap
4019     //    contains two values: the size of the gap, and the distance
4020     //    to the next gap (or the stack top).
4021
4022     bottom = &(tso->stack[tso->stack_size]);
4023
4024     frame = tso->sp;
4025
4026     ASSERT(frame < bottom);
4027     
4028     prev_was_update_frame = rtsFalse;
4029     current_gap_size = 0;
4030     gap = (struct stack_gap *) (tso->sp - sizeofW(StgUpdateFrame));
4031
4032     while (frame < bottom) {
4033         
4034         info = get_ret_itbl((StgClosure *)frame);
4035         switch (info->i.type) {
4036
4037         case UPDATE_FRAME:
4038         { 
4039             StgUpdateFrame *upd = (StgUpdateFrame *)frame;
4040
4041             if (upd->updatee->header.info == &stg_BLACKHOLE_info) {
4042
4043                 // found a BLACKHOLE'd update frame; we've been here
4044                 // before, in a previous GC, so just break out.
4045
4046                 // Mark the end of the gap, if we're in one.
4047                 if (current_gap_size != 0) {
4048                     gap = (struct stack_gap *)(frame-sizeofW(StgUpdateFrame));
4049                 }
4050                 
4051                 frame += sizeofW(StgUpdateFrame);
4052                 goto done_traversing;
4053             }
4054
4055             if (prev_was_update_frame) {
4056
4057                 TICK_UPD_SQUEEZED();
4058                 /* wasn't there something about update squeezing and ticky to be
4059                  * sorted out?  oh yes: we aren't counting each enter properly
4060                  * in this case.  See the log somewhere.  KSW 1999-04-21
4061                  *
4062                  * Check two things: that the two update frames don't point to
4063                  * the same object, and that the updatee_bypass isn't already an
4064                  * indirection.  Both of these cases only happen when we're in a
4065                  * block hole-style loop (and there are multiple update frames
4066                  * on the stack pointing to the same closure), but they can both
4067                  * screw us up if we don't check.
4068                  */
4069                 if (upd->updatee != updatee && !closure_IND(upd->updatee)) {
4070                     // this wakes the threads up 
4071                     UPD_IND_NOLOCK(upd->updatee, updatee);
4072                 }
4073
4074                 // now mark this update frame as a stack gap.  The gap
4075                 // marker resides in the bottom-most update frame of
4076                 // the series of adjacent frames, and covers all the
4077                 // frames in this series.
4078                 current_gap_size += sizeofW(StgUpdateFrame);
4079                 ((struct stack_gap *)frame)->gap_size = current_gap_size;
4080                 ((struct stack_gap *)frame)->next_gap = gap;
4081
4082                 frame += sizeofW(StgUpdateFrame);
4083                 continue;
4084             } 
4085
4086             // single update frame, or the topmost update frame in a series
4087             else {
4088                 StgBlockingQueue *bh = (StgBlockingQueue *)upd->updatee;
4089
4090                 // Do lazy black-holing
4091                 if (bh->header.info != &stg_BLACKHOLE_info &&
4092                     bh->header.info != &stg_BLACKHOLE_BQ_info &&
4093                     bh->header.info != &stg_CAF_BLACKHOLE_info) {
4094 #if (!defined(LAZY_BLACKHOLING)) && defined(DEBUG)
4095                     debugBelch("Unexpected lazy BHing required at 0x%04x",(int)bh);
4096 #endif
4097 #ifdef DEBUG
4098                     /* zero out the slop so that the sanity checker can tell
4099                      * where the next closure is.
4100                      */
4101                     { 
4102                         StgInfoTable *bh_info = get_itbl(bh);
4103                         nat np = bh_info->layout.payload.ptrs, 
4104                             nw = bh_info->layout.payload.nptrs, i;
4105                         /* don't zero out slop for a THUNK_SELECTOR,
4106                          * because its layout info is used for a
4107                          * different purpose, and it's exactly the
4108                          * same size as a BLACKHOLE in any case.
4109                          */
4110                         if (bh_info->type != THUNK_SELECTOR) {
4111                             for (i = 0; i < np + nw; i++) {
4112                                 ((StgClosure *)bh)->payload[i] = INVALID_OBJECT;
4113                             }
4114                         }
4115                     }
4116 #endif
4117 #ifdef PROFILING
4118                     // We pretend that bh is now dead.
4119                     LDV_recordDead_FILL_SLOP_DYNAMIC((StgClosure *)bh);
4120 #endif
4121                     // Todo: maybe use SET_HDR() and remove LDV_RECORD_CREATE()?
4122                     SET_INFO(bh,&stg_BLACKHOLE_info);
4123
4124                     // Set the update frame to stg_bh_upd_info, which
4125                     // checks for blackholes (the normal update frame
4126                     // doesn't check, for efficiency).
4127                     ((StgClosure *)frame)->header.info = &stg_bh_upd_frame_info;
4128
4129                     // We pretend that bh has just been created.
4130                     LDV_RECORD_CREATE(bh);
4131                 }
4132
4133                 prev_was_update_frame = rtsTrue;
4134                 updatee = upd->updatee;
4135                 frame += sizeofW(StgUpdateFrame);
4136                 continue;
4137             }
4138         }
4139             
4140         default:
4141             prev_was_update_frame = rtsFalse;
4142
4143             // we're not in a gap... check whether this is the end of a gap
4144             // (an update frame can't be the end of a gap).
4145             if (current_gap_size != 0) {
4146                 gap = (struct stack_gap *) (frame - sizeofW(StgUpdateFrame));
4147             }
4148             current_gap_size = 0;
4149
4150             frame += stack_frame_sizeW((StgClosure *)frame);
4151             continue;
4152         }
4153     }
4154
4155 done_traversing:
4156             
4157     // Now we have a stack with gaps in it, and we have to walk down
4158     // shoving the stack up to fill in the gaps.  A diagram might
4159     // help:
4160     //
4161     //    +| ********* |
4162     //     | ********* | <- sp
4163     //     |           |
4164     //     |           | <- gap_start
4165     //     | ......... |                |
4166     //     | stack_gap | <- gap         | chunk_size
4167     //     | ......... |                | 
4168     //     | ......... | <- gap_end     v
4169     //     | ********* | 
4170     //     | ********* | 
4171     //     | ********* | 
4172     //    -| ********* | 
4173     //
4174     // 'sp'  points the the current top-of-stack
4175     // 'gap' points to the stack_gap structure inside the gap
4176     // *****   indicates real stack data
4177     // .....   indicates gap
4178     // <empty> indicates unused
4179     //
4180     {
4181         void *sp;
4182         void *gap_start, *next_gap_start, *gap_end;
4183         nat chunk_size;
4184
4185         next_gap_start = (void *)((unsigned char*)gap + sizeof(StgUpdateFrame));
4186         sp = next_gap_start;
4187
4188         while ((StgPtr)gap > tso->sp) {
4189
4190             // we're working in *bytes* now...
4191             gap_start = next_gap_start;
4192             gap_end = (void*) ((unsigned char*)gap_start - gap->gap_size * sizeof(W_));
4193
4194             gap = gap->next_gap;
4195             next_gap_start = (void *)((unsigned char*)gap + sizeof(StgUpdateFrame));
4196
4197             chunk_size = (unsigned char*)gap_end - (unsigned char*)next_gap_start;
4198             sp -= chunk_size;
4199             memmove(sp, next_gap_start, chunk_size);
4200         }
4201
4202         tso->sp = (StgPtr)sp;
4203     }
4204 }    
4205
4206 /* -----------------------------------------------------------------------------
4207  * Pausing a thread
4208  * 
4209  * We have to prepare for GC - this means doing lazy black holing
4210  * here.  We also take the opportunity to do stack squeezing if it's
4211  * turned on.
4212  * -------------------------------------------------------------------------- */
4213 void
4214 threadPaused(StgTSO *tso)
4215 {
4216   if ( RtsFlags.GcFlags.squeezeUpdFrames == rtsTrue )
4217     threadSqueezeStack(tso);    // does black holing too 
4218   else
4219     threadLazyBlackHole(tso);
4220 }
4221
4222 /* -----------------------------------------------------------------------------
4223  * Debugging
4224  * -------------------------------------------------------------------------- */
4225
4226 #if DEBUG
4227 void
4228 printMutableList(generation *gen)
4229 {
4230     bdescr *bd;
4231     StgPtr p;
4232
4233     debugBelch("@@ Mutable list %p: ", gen->mut_list);
4234
4235     for (bd = gen->mut_list; bd != NULL; bd = bd->link) {
4236         for (p = bd->start; p < bd->free; p++) {
4237             debugBelch("%p (%s), ", (void *)*p, info_type((StgClosure *)*p));
4238         }
4239     }
4240     debugBelch("\n");
4241 }
4242
4243 STATIC_INLINE rtsBool
4244 maybeLarge(StgClosure *closure)
4245 {
4246   StgInfoTable *info = get_itbl(closure);
4247
4248   /* closure types that may be found on the new_large_objects list; 
4249      see scavenge_large */
4250   return (info->type == MUT_ARR_PTRS ||
4251           info->type == MUT_ARR_PTRS_FROZEN ||
4252           info->type == MUT_ARR_PTRS_FROZEN0 ||
4253           info->type == TSO ||
4254           info->type == ARR_WORDS);
4255 }
4256
4257   
4258 #endif // DEBUG