[project @ 2000-09-04 15:09:49 by simonmar]
[ghc-hetmet.git] / ghc / includes / Stable.h
1 /* -----------------------------------------------------------------------------
2  * $Id: Stable.h,v 1.6 2000/09/04 15:09:49 simonmar 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;                 /* Haskell object, free list, or NULL */
26   StgWord weight;               /* used for reference counting */
27   StgClosure *sn_obj;           /* the StableName object (or NULL) */
28 } snEntry;
29
30 extern DLL_IMPORT_RTS snEntry *stable_ptr_table;
31 extern DLL_IMPORT_RTS snEntry *stable_ptr_free;
32
33 extern DLL_IMPORT_RTS unsigned int SPT_size;
34
35 extern inline StgPtr
36 deRefStablePtr(StgStablePtr sp)
37 {
38   ASSERT(stable_ptr_table[stgCast(StgWord,sp) & ~STABLEPTR_WEIGHT_MASK].weight > 0);
39   return stable_ptr_table[stgCast(StgWord,sp) & ~STABLEPTR_WEIGHT_MASK].addr;
40 }
41
42 extern inline void
43 freeStablePtr(StgStablePtr sp)
44 {
45   StgWord sn = stgCast(StgWord,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 += 
52       1 << ((((StgWord)sp & STABLEPTR_WEIGHT_MASK) >> STABLEPTR_WEIGHT_SHIFT) - 1);
53 }
54
55 extern inline StgStablePtr
56 splitStablePtr(StgStablePtr sp)
57 {
58   /* doesn't need access to the stable pointer table */
59   StgWord weight = (stgCast(StgWord,sp) & STABLEPTR_WEIGHT_MASK) / 2;
60   return stgCast(StgStablePtr,(stgCast(StgWord,sp) & ~STABLEPTR_WEIGHT_MASK) + weight);
61 }
62
63 /* No deRefStableName, because the existence of a stable name doesn't
64  * guarantee the existence of the object itself.
65  */