X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=ghc%2Frts%2FStable.c;h=72b3a98c7b006d309d81dd18915d3b12cd74485e;hb=dfbf36a9588da7d50467950603fc3385088b4f72;hp=f1040758899b545c5966f224d72113f074769cb7;hpb=dfd7d6d02a597949b08161ae3d49dc6dfc9e812d;p=ghc-hetmet.git diff --git a/ghc/rts/Stable.c b/ghc/rts/Stable.c index f104075..72b3a98 100644 --- a/ghc/rts/Stable.c +++ b/ghc/rts/Stable.c @@ -1,12 +1,16 @@ /* ----------------------------------------------------------------------------- - * $Id: Stable.c,v 1.15 2001/07/23 17:23:19 simonmar Exp $ + * $Id: Stable.c,v 1.25 2003/03/31 19:19:34 sof Exp $ * - * (c) The GHC Team, 1998-1999 + * (c) The GHC Team, 1998-2002 * * Stable names and stable pointers. * * ---------------------------------------------------------------------------*/ +// Make static versions of inline functions in Stable.h: +#define RTS_STABLE_C + +#include "PosixSource.h" #include "Rts.h" #include "Hash.h" #include "StablePriv.h" @@ -86,15 +90,18 @@ make sure we can still find the object. */ -snEntry *stable_ptr_table; -snEntry *stable_ptr_free; +snEntry *stable_ptr_table = NULL; +static snEntry *stable_ptr_free = NULL; -unsigned int SPT_size; +static unsigned int SPT_size = 0; /* This hash table maps Haskell objects to stable names, so that every * call to lookupStableName on a given object will return the same * stable name. * + * OLD COMMENTS about reference counting follow. The reference count + * in a stable name entry is now just a counter. + * * Reference counting * ------------------ * A plain stable name entry has a zero reference count, which means @@ -121,7 +128,7 @@ unsigned int SPT_size; * to the weight stored in the table entry. * */ -HashTable *addrToStableHash; +static HashTable *addrToStableHash = NULL; #define INIT_SPT_SIZE 64 @@ -133,7 +140,7 @@ initFreeList(snEntry *table, nat n, snEntry *free) for (p = table + n - 1; p >= table; p--) { p->addr = (P_)free; p->old = NULL; - p->weight = 0; + p->ref = 0; p->sn_obj = NULL; free = p; } @@ -143,12 +150,9 @@ initFreeList(snEntry *table, nat n, snEntry *free) void initStablePtrTable(void) { - /* the table will be allocated the first time makeStablePtr is - * called */ - stable_ptr_table = NULL; - stable_ptr_free = NULL; - addrToStableHash = NULL; - SPT_size = 0; + // Nothing to do: + // the table will be allocated the first time makeStablePtr is + // called, and we want the table to persist through multiple inits. } /* @@ -195,7 +199,7 @@ lookupStableName(StgPtr p) } else { sn = stable_ptr_free - stable_ptr_table; (P_)stable_ptr_free = stable_ptr_free->addr; - stable_ptr_table[sn].weight = 0; + stable_ptr_table[sn].ref = 0; stable_ptr_table[sn].addr = p; stable_ptr_table[sn].sn_obj = NULL; /* IF_DEBUG(stable,fprintf(stderr,"new stable name %d at @@ -213,7 +217,7 @@ freeStableName(snEntry *sn) { ASSERT(sn->sn_obj == NULL); if (sn->addr != NULL) { - removeHashTable(addrToStableHash, (W_)sn->addr, NULL); + removeHashTable(addrToStableHash, (W_)sn->addr, NULL); } sn->addr = (P_)stable_ptr_free; stable_ptr_free = sn; @@ -222,26 +226,28 @@ freeStableName(snEntry *sn) StgStablePtr getStablePtr(StgPtr p) { - StgWord sn = lookupStableName(p); - StgWord weight, n; - weight = stable_ptr_table[sn].weight; - if (weight == 0) { - weight = (StgWord)1 << (BITS_IN(StgWord)-1); - stable_ptr_table[sn].weight = weight; - return (StgStablePtr)(sn + (BITS_IN(StgWord) << STABLEPTR_WEIGHT_SHIFT)); - } - else if (weight == 1) { - barf("getStablePtr: too light"); - } - else { - weight /= 2; - /* find log2(weight) */ - for (n = 0; weight != 1; n++) { - weight >>= 1; + StgWord sn; + + sn = lookupStableName(p); + stable_ptr_table[sn].ref++; + return (StgStablePtr)(sn); +} + +void +freeStablePtr(StgStablePtr sp) +{ + snEntry *sn = &stable_ptr_table[(StgWord)sp]; + + ASSERT((StgWord)sp < SPT_size && sn->addr != NULL && sn->ref > 0); + + sn->ref--; + + // If this entry has no StableName attached, then just free it + // immediately. This is important; it might be a while before the + // next major GC which actually collects the entry. + if (sn->sn_obj == NULL && sn->ref == 0) { + freeStableName(sn); } - stable_ptr_table[sn].weight -= 1 << n; - return (StgStablePtr)(sn + ((n+1) << STABLEPTR_WEIGHT_SHIFT)); - } } void @@ -252,8 +258,8 @@ enlargeStablePtrTable(void) if (SPT_size == 0) { // 1st time SPT_size = INIT_SPT_SIZE; - stable_ptr_table = stgMallocWords(SPT_size * sizeof(snEntry), - "initStablePtrTable"); + stable_ptr_table = stgMallocBytes(SPT_size * sizeof(snEntry), + "enlargeStablePtrTable"); /* we don't use index 0 in the stable name table, because that * would conflict with the hash table lookup operations which @@ -266,9 +272,10 @@ enlargeStablePtrTable(void) // 2nd and subsequent times SPT_size *= 2; stable_ptr_table = - stgReallocWords(stable_ptr_table, SPT_size * sizeof(snEntry), + stgReallocBytes(stable_ptr_table, + SPT_size * sizeof(snEntry), "enlargeStablePtrTable"); - + initFreeList(stable_ptr_table + old_SPT_size, old_SPT_size, NULL); } } @@ -276,7 +283,7 @@ enlargeStablePtrTable(void) /* ----------------------------------------------------------------------------- * Treat stable pointers as roots for the garbage collector. * - * A stable pointer is any stable name entry with a weight > 0. We'll + * A stable pointer is any stable name entry with a ref > 0. We'll * take the opportunity to zero the "keep" flags at the same time. * -------------------------------------------------------------------------- */ @@ -293,7 +300,9 @@ markStablePtrTable(evac_fn evac) for (p = stable_ptr_table+1; p < end_stable_ptr_table; p++) { q = p->addr; - // internal pointers or NULL are free slots + // Internal pointers are free slots. If q == NULL, it's a + // stable name where the object has been GC'd, but the + // StableName object (sn_obj) is still alive. if (q && (q < (P_)stable_ptr_table || q >= (P_)end_stable_ptr_table)) { // save the current addr away: we need to be able to tell @@ -301,8 +310,8 @@ markStablePtrTable(evac_fn evac) // the hash table later. p->old = p->addr; - // if the weight is non-zero, treat addr as a root - if (p->weight != 0) { + // if the ref is non-zero, treat addr as a root + if (p->ref != 0) { evac((StgClosure **)&p->addr); } } @@ -326,16 +335,14 @@ threadStablePtrTable( evac_fn evac ) end_stable_ptr_table = &stable_ptr_table[SPT_size]; for (p = stable_ptr_table+1; p < end_stable_ptr_table; p++) { - q = p->addr; - // internal pointers or NULL are free slots + if (p->sn_obj != NULL) { + evac((StgClosure **)&p->sn_obj); + } + + q = p->addr; if (q && (q < (P_)stable_ptr_table || q >= (P_)end_stable_ptr_table)) { - if (p->weight != 0) { - evac((StgClosure **)&p->addr); - } - if (p->sn_obj != NULL) { - evac((StgClosure **)&p->sn_obj); - } + evac((StgClosure **)&p->addr); } } } @@ -345,7 +352,7 @@ threadStablePtrTable( evac_fn evac ) * * A dead entry has: * - * - a weight of zero (i.e. 2^32) + * - a zero reference count * - a dead sn_obj * * Both of these conditions must be true in order to re-use the stable @@ -370,11 +377,14 @@ gcStablePtrTable( void ) p->sn_obj = isAlive(p->sn_obj); } + // Internal pointers are free slots. If q == NULL, it's a + // stable name where the object has been GC'd, but the + // StableName object (sn_obj) is still alive. q = p->addr; if (q && (q < (P_)stable_ptr_table || q >= (P_)end_stable_ptr_table)) { // StableNames only: - if (p->weight == 0) { + if (p->ref == 0) { if (p->sn_obj == NULL) { // StableName object is dead freeStableName(p); @@ -384,7 +394,7 @@ gcStablePtrTable( void ) } else { (StgClosure *)p->addr = isAlive((StgClosure *)p->addr); - IF_DEBUG(stable, fprintf(stderr,"Stable name %d still alive at %p, weight %d\n", p - stable_ptr_table, p->addr, p->weight)); + IF_DEBUG(stable, fprintf(stderr,"Stable name %d still alive at %p, ref %d\n", p - stable_ptr_table, p->addr, p->ref)); } } }