[project @ 2001-07-23 17:23:19 by simonmar]
[ghc-hetmet.git] / ghc / includes / Stable.h
1 /* -----------------------------------------------------------------------------
2  * $Id: Stable.h,v 1.8 2001/07/23 17:23:19 simonmar Exp $
3  *
4  * (c) The GHC Team, 1998-2000
5  *
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.
9  *
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
13  * a void*.
14  *
15  * ----------------------------------------------------------------------------*/
16
17 #define STABLEPTR_WEIGHT_MASK   ((StgWord)0xff << ((sizeof(StgWord)-1) * BITS_PER_BYTE))
18 #define STABLEPTR_WEIGHT_SHIFT  (BITS_IN(StgWord) - 8)
19
20 /* -----------------------------------------------------------------------------
21    External C Interface
22    -------------------------------------------------------------------------- */
23
24 extern StgPtr         deRefStablePtr(StgStablePtr stable_ptr);
25 extern void           freeStablePtr(StgStablePtr sp);
26 extern StgStablePtr   splitStablePtr(StgStablePtr sp);
27
28 /* -----------------------------------------------------------------------------
29    PRIVATE from here.
30    -------------------------------------------------------------------------- */
31
32 extern StgStablePtr getStablePtr(StgPtr p);
33
34 typedef struct { 
35   StgPtr  addr;                 /* Haskell object, free list, or NULL */
36   StgPtr  old;                  /* old Haskell object, used during GC */
37   StgWord weight;               /* used for reference counting */
38   StgClosure *sn_obj;           /* the StableName object (or NULL) */
39 } snEntry;
40
41 extern DLL_IMPORT_RTS snEntry *stable_ptr_table;
42 extern DLL_IMPORT_RTS snEntry *stable_ptr_free;
43
44 extern DLL_IMPORT_RTS unsigned int SPT_size;
45
46 extern inline StgPtr
47 deRefStablePtr(StgStablePtr sp)
48 {
49   ASSERT(stable_ptr_table[stgCast(StgWord,sp) & ~STABLEPTR_WEIGHT_MASK].weight > 0);
50   return stable_ptr_table[stgCast(StgWord,sp) & ~STABLEPTR_WEIGHT_MASK].addr;
51 }
52
53 extern inline void
54 freeStablePtr(StgStablePtr sp)
55 {
56   StgWord sn = stgCast(StgWord,sp) & ~STABLEPTR_WEIGHT_MASK;
57   
58   ASSERT(sn < SPT_size
59          && stable_ptr_table[sn].addr != NULL
60          && stable_ptr_table[sn].weight > 0);
61   
62   stable_ptr_table[sn].weight += 
63       1 << ((((StgWord)sp & STABLEPTR_WEIGHT_MASK) >> STABLEPTR_WEIGHT_SHIFT) - 1);
64 }
65
66 extern inline StgStablePtr
67 splitStablePtr(StgStablePtr sp)
68 {
69   /* doesn't need access to the stable pointer table */
70   StgWord weight = (stgCast(StgWord,sp) & STABLEPTR_WEIGHT_MASK) / 2;
71   return stgCast(StgStablePtr,(stgCast(StgWord,sp) & ~STABLEPTR_WEIGHT_MASK) + weight);
72 }
73
74 /* No deRefStableName, because the existence of a stable name doesn't
75  * guarantee the existence of the object itself.
76  */