[project @ 1999-01-26 14:04:46 by simonm]
[ghc-hetmet.git] / ghc / includes / Stable.h
1 /* -----------------------------------------------------------------------------
2  * $Id: Stable.h,v 1.1 1999/01/26 14:04:46 simonm Exp $
3  *
4  * Stable names and stable pointers
5  *
6  * ---------------------------------------------------------------------------*/
7
8 /* -----------------------------------------------------------------------------
9    External C Interface
10    -------------------------------------------------------------------------- */
11
12 extern StgPtr         deRefStablePtr(StgStablePtr stable_ptr);
13 extern void           freeStablePtr(StgStablePtr sp);
14 extern StgStablePtr   splitStablePtr(StgStablePtr sp);
15
16 /* -----------------------------------------------------------------------------
17    PRIVATE from here.
18    -------------------------------------------------------------------------- */
19
20 extern StgStablePtr getStablePtr(StgPtr p);
21
22 typedef struct { 
23   StgPtr  addr;                 /* either Haskell object or free list */
24   StgWord weight;               /* used for reference counting */
25   unsigned int keep;            /* set by the garbage collector */
26 } snEntry;
27
28 extern snEntry *stable_ptr_table;
29 extern snEntry *stable_ptr_free;
30
31 extern unsigned int SPT_size;
32
33 extern inline StgPtr
34 deRefStablePtr(StgStablePtr sp)
35 {
36   ASSERT(stable_ptr_table[sp & ~STABLEPTR_WEIGHT_MASK].weight > 0);
37   return stable_ptr_table[sp & ~STABLEPTR_WEIGHT_MASK].addr;
38 }
39
40 extern inline void
41 freeStablePtr(StgStablePtr sp)
42 {
43   StgWord sn = sp & ~STABLEPTR_WEIGHT_MASK;
44   
45   ASSERT(sn < SPT_size
46          && stable_ptr_table[sn].addr != NULL
47          && stable_ptr_table[sn].weight > 0);
48   
49   stable_ptr_table[sn].weight += (sp & STABLEPTR_WEIGHT_MASK) >> STABLEPTR_WEIGHT_SHIFT;
50 }
51
52 extern inline StgStablePtr
53 splitStablePtr(StgStablePtr sp)
54 {
55   /* doesn't need access to the stable pointer table */
56   StgWord weight = (sp & STABLEPTR_WEIGHT_MASK) / 2;
57   return (sp & ~STABLEPTR_WEIGHT_MASK) + weight;
58 }
59
60 /* No deRefStableName, because the existence of a stable name doesn't
61  * guarantee the existence of the object itself.
62  */