[project @ 2002-10-11 08:04:55 by simonpj]
[ghc-hetmet.git] / ghc / rts / GCCompact.c
index b3f4fb7..ea2e474 100644 (file)
@@ -1,5 +1,5 @@
 /* -----------------------------------------------------------------------------
- * $Id: GCCompact.c,v 1.4 2001/07/25 11:55:57 simonmar Exp $
+ * $Id: GCCompact.c,v 1.12 2002/03/12 11:51:06 simonmar Exp $
  *
  * (c) The GHC Team 2001
  *
@@ -7,6 +7,7 @@
  *
  * ---------------------------------------------------------------------------*/
 
+#include "PosixSource.h"
 #include "Rts.h"
 #include "RtsUtils.h"
 #include "RtsFlags.h"
@@ -173,7 +174,7 @@ thread_stack(StgPtr p, StgPtr stack_end)
 {
     StgPtr q;
     const StgInfoTable* info;
-    StgWord32 bitmap;
+    StgWord bitmap;
     
     // highly similar to scavenge_stack, but we do pointer threading here.
     
@@ -213,7 +214,7 @@ thread_stack(StgPtr p, StgPtr stack_end)
            p++;
            continue;
            
-           // small bitmap (< 32 entries, or 64 on a 64-bit machine) 
+           // small bitmap (<= 32 entries, or 64 on a 64-bit machine) 
        case UPDATE_FRAME:
        case STOP_FRAME:
        case CATCH_FRAME:
@@ -234,7 +235,7 @@ thread_stack(StgPtr p, StgPtr stack_end)
            }
            continue;
 
-           // large bitmap (> 32 entries) 
+           // large bitmap (> 32 entries, or 64 on a 64-bit machine) 
        case RET_BIG:
        case RET_VEC_BIG:
        {
@@ -247,7 +248,7 @@ thread_stack(StgPtr p, StgPtr stack_end)
 
            for (i=0; i<large_bitmap->size; i++) {
                bitmap = large_bitmap->bitmap[i];
-               q = p + sizeof(W_) * 8;
+               q = p + BITS_IN(W_);
                while (bitmap != 0) {
                    if ((bitmap & 1) == 0) {
                        thread(p);
@@ -306,6 +307,8 @@ update_fwd_large( bdescr *bd )
     {
        StgTSO *tso = (StgTSO *)p;
        thread_stack(tso->sp, &(tso->stack[tso->stack_size]));
+       thread((StgPtr)&tso->link);
+       thread((StgPtr)&tso->global_link);
        continue;
     }
 
@@ -444,18 +447,11 @@ update_fwd( bdescr *blocks )
                break;
            }
 
-           // specialise this case, because we want to update the
-           // mut_link field too.
            case IND_OLDGEN:
            case IND_OLDGEN_PERM:
-           {
-               StgIndOldGen *ind = (StgIndOldGen *)p;
-               thread((StgPtr)&ind->indirectee);
-               if (ind->mut_link != NULL) {
-                   thread((StgPtr)&ind->mut_link);
-               }
+               thread((StgPtr)&((StgIndOldGen *)p)->indirectee);
+               p += sizeofW(StgIndOldGen);
                break;
-           }
 
            case THUNK_SELECTOR:
            { 
@@ -514,7 +510,9 @@ static void
 update_fwd_compact( bdescr *blocks )
 {
     StgPtr p, q, free;
+#if 0
     StgWord m;
+#endif
     bdescr *bd, *free_bd;
     StgInfoTable *info;
     nat size;
@@ -659,17 +657,9 @@ update_fwd_compact( bdescr *blocks )
 
            case IND_OLDGEN:
            case IND_OLDGEN_PERM:
-               // specialise this case, because we want to update the
-               // mut_link field too.
-           {
-               StgIndOldGen *ind = (StgIndOldGen *)p;
-               thread((StgPtr)&ind->indirectee);
-               if (ind->mut_link != NULL) {
-                   thread((StgPtr)&ind->mut_link);
-               }
+               thread((StgPtr)&((StgIndOldGen *)p)->indirectee);
                p += sizeofW(StgIndOldGen);
                break;
-           }
 
            case THUNK_SELECTOR:
            { 
@@ -723,8 +713,15 @@ update_fwd_compact( bdescr *blocks )
 
            size = p - q;
            if (free + size > free_bd->start + BLOCK_SIZE_W) {
+               // unset the next bit in the bitmap to indicate that
+               // this object needs to be pushed into the next
+               // block.  This saves us having to run down the
+               // threaded info pointer list twice during the next pass.
+               unmark(q+1,bd);
                free_bd = free_bd->link;
                free = free_bd->start;
+           } else {
+               ASSERT(is_marked(q+1,bd));
            }
 
            unthread(q,free);
@@ -740,7 +737,9 @@ static nat
 update_bkwd_compact( step *stp )
 {
     StgPtr p, free;
+#if 0
     StgWord m;
+#endif
     bdescr *bd, *free_bd;
     StgInfoTable *info;
     nat size, free_blocks;
@@ -784,15 +783,7 @@ update_bkwd_compact( step *stp )
            }
 #endif
 
-           // must unthread before we look at the info ptr...
-           info = get_threaded_info(p);
-
-           ASSERT(p && (LOOKS_LIKE_GHC_INFO(info)
-                        || IS_HUGS_CONSTR_INFO(info)));
-
-           size = obj_sizeW((StgClosure *)p,info);
-
-           if (free + size > free_bd->start + BLOCK_SIZE_W) {
+           if (!is_marked(p+1,bd)) {
                // don't forget to update the free ptr in the block desc.
                free_bd->free = free;
                free_bd = free_bd->link;
@@ -801,13 +792,17 @@ update_bkwd_compact( step *stp )
            }
 
            unthread(p,free);
+           info = get_itbl((StgClosure *)p);
+           size = obj_sizeW((StgClosure *)p,info);
+
+           ASSERT(p && (LOOKS_LIKE_GHC_INFO(info)
+                        || IS_HUGS_CONSTR_INFO(info)));
+
            if (free != p) {
                move(free,p,size);
            }
 
            // Rebuild the mutable list for the old generation.
-           // (the mut_once list is updated using threading, with
-           // special cases for IND_OLDGEN and MUT_CONS above).
            if (ip_MUTABLE(info)) {
                recordMutable((StgMutClosure *)free);
            }
@@ -834,14 +829,26 @@ update_bkwd_compact( step *stp )
     stp->n_blocks = free_blocks;
 
     return free_blocks;
-} 
+}
+
+static void
+thread_mut_once_list( generation *g )
+{
+    StgMutClosure *p, *next;
+
+    for (p = g->mut_once_list; p != END_MUT_LIST; p = next) {
+       next = p->mut_link;
+       thread((StgPtr)&p->mut_link);
+    }
+    
+    thread((StgPtr)&g->mut_once_list);
+}
 
 void
 compact( void (*get_roots)(evac_fn) )
 {
     nat g, s, blocks;
     step *stp;
-    extern StgWeak *old_weak_ptr_list; // tmp
 
     // 1. thread the roots
     get_roots((evac_fn)thread);
@@ -857,18 +864,32 @@ compact( void (*get_roots)(evac_fn) )
     // mutable lists
     for (g = 1; g < RtsFlags.GcFlags.generations; g++) {
        thread((StgPtr)&generations[g].mut_list);
-       thread((StgPtr)&generations[g].mut_once_list);
+       thread_mut_once_list(&generations[g]);
     }
 
     // the global thread list
     thread((StgPtr)&all_threads);
 
+    // any threads resurrected during this GC
+    thread((StgPtr)&resurrected_threads);
+
+    // the main threads list
+    {
+       StgMainThread *m;
+       for (m = main_threads; m != NULL; m = m->link) {
+           thread((StgPtr)&m->tso);
+       }
+    }
+
     // the static objects
     thread_static(scavenged_static_objects);
 
     // the stable pointer table
     threadStablePtrTable((evac_fn)thread);
 
+    // the CAF list (used by GHCi)
+    markCAFs((evac_fn)thread);
+
     // 2. update forward ptrs
     for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
        for (s = 0; s < generations[g].n_steps; s++) {