X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;ds=sidebyside;f=ghc%2Frts%2FStable.c;h=c047469cf592f43962e433d8b30fea9a132ca7e8;hb=6a02dd25682e76484d2505ec7139948e6a32b9ee;hp=0232e18d675fbb6b7cb6a19a4d24c8a9b31da1a5;hpb=791d890ca039ce45704b879b292069ddc5821dc0;p=ghc-hetmet.git diff --git a/ghc/rts/Stable.c b/ghc/rts/Stable.c index 0232e18..c047469 100644 --- a/ghc/rts/Stable.c +++ b/ghc/rts/Stable.c @@ -1,12 +1,15 @@ /* ----------------------------------------------------------------------------- - * $Id: Stable.c,v 1.19 2001/12/20 16:12:09 sewardj Exp $ + * $Id: Stable.c,v 1.27 2003/11/12 17:49:11 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" @@ -70,27 +73,12 @@ There may be additional functions on the C side to allow evaluation, application, etc of a stable pointer. - When Haskell calls C, it normally just passes over primitive integers, - floats, bools, strings, etc. This doesn't cause any problems at all - for garbage collection because the act of passing them makes a copy - from the heap, stack or wherever they are onto the C-world stack. - However, if we were to pass a heap object such as a (Haskell) @String@ - and a garbage collection occured before we finished using it, we'd run - into problems since the heap object might have been moved or even - deleted. - - So, if a C call is able to cause a garbage collection or we want to - store a pointer to a heap object between C calls, we must be careful - when passing heap objects. Our solution is to keep a table of all - objects we've given to the C-world and to make sure that the garbage - collector collects these objects --- updating the table as required to - 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 @@ -125,11 +113,11 @@ unsigned int SPT_size; * to the weight stored in the table entry. * */ -HashTable *addrToStableHash; +static HashTable *addrToStableHash = NULL; #define INIT_SPT_SIZE 64 -static inline void +STATIC_INLINE void initFreeList(snEntry *table, nat n, snEntry *free) { snEntry *p; @@ -147,12 +135,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. } /* @@ -212,7 +197,7 @@ lookupStableName(StgPtr p) } } -static inline void +STATIC_INLINE void freeStableName(snEntry *sn) { ASSERT(sn->sn_obj == NULL); @@ -236,13 +221,18 @@ getStablePtr(StgPtr p) void freeStablePtr(StgStablePtr sp) { - StgWord sn = (StgWord)sp; - - ASSERT(sn < SPT_size - && stable_ptr_table[sn].addr != NULL - && stable_ptr_table[sn].ref > 0); + snEntry *sn = &stable_ptr_table[(StgWord)sp]; - stable_ptr_table[sn].ref --; + 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); + } } void @@ -253,8 +243,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 @@ -267,9 +257,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); } }