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