1 /* -----------------------------------------------------------------------------
2 * $Id: Stable.h,v 1.7 2000/11/07 17:05:47 simonmar Exp $
4 * (c) The GHC Team, 1998-2000
6 * Stable Pointers: A stable pointer is represented as an index into
7 * the stable pointer table in the low BITS_PER_WORD-8 bits with a
8 * weight in the upper 8 bits.
10 * SUP: StgStablePtr used to be a synonym for StgWord, but stable pointers
11 * are guaranteed to be void* on the C-side, so we have to do some occasional
12 * casting. Size is not a matter, because StgWord is always the same size as
15 * ----------------------------------------------------------------------------*/
17 #define STABLEPTR_WEIGHT_MASK ((StgWord)0xff << ((sizeof(StgWord)-1) * BITS_PER_BYTE))
18 #define STABLEPTR_WEIGHT_SHIFT (BITS_IN(StgWord) - 8)
20 /* -----------------------------------------------------------------------------
22 -------------------------------------------------------------------------- */
24 extern StgPtr deRefStablePtr(StgStablePtr stable_ptr);
25 extern void freeStablePtr(StgStablePtr sp);
26 extern StgStablePtr splitStablePtr(StgStablePtr sp);
28 /* -----------------------------------------------------------------------------
30 -------------------------------------------------------------------------- */
32 extern StgStablePtr getStablePtr(StgPtr p);
35 StgPtr addr; /* Haskell object, free list, or NULL */
36 StgWord weight; /* used for reference counting */
37 StgClosure *sn_obj; /* the StableName object (or NULL) */
40 extern DLL_IMPORT_RTS snEntry *stable_ptr_table;
41 extern DLL_IMPORT_RTS snEntry *stable_ptr_free;
43 extern DLL_IMPORT_RTS unsigned int SPT_size;
46 deRefStablePtr(StgStablePtr sp)
48 ASSERT(stable_ptr_table[stgCast(StgWord,sp) & ~STABLEPTR_WEIGHT_MASK].weight > 0);
49 return stable_ptr_table[stgCast(StgWord,sp) & ~STABLEPTR_WEIGHT_MASK].addr;
53 freeStablePtr(StgStablePtr sp)
55 StgWord sn = stgCast(StgWord,sp) & ~STABLEPTR_WEIGHT_MASK;
58 && stable_ptr_table[sn].addr != NULL
59 && stable_ptr_table[sn].weight > 0);
61 stable_ptr_table[sn].weight +=
62 1 << ((((StgWord)sp & STABLEPTR_WEIGHT_MASK) >> STABLEPTR_WEIGHT_SHIFT) - 1);
65 extern inline StgStablePtr
66 splitStablePtr(StgStablePtr sp)
68 /* doesn't need access to the stable pointer table */
69 StgWord weight = (stgCast(StgWord,sp) & STABLEPTR_WEIGHT_MASK) / 2;
70 return stgCast(StgStablePtr,(stgCast(StgWord,sp) & ~STABLEPTR_WEIGHT_MASK) + weight);
73 /* No deRefStableName, because the existence of a stable name doesn't
74 * guarantee the existence of the object itself.