Stable.h 2.71 KB
Newer Older
1
/* -----------------------------------------------------------------------------
2
 * $Id: Stable.h,v 1.9 2001/10/23 11:28:02 simonmar Exp $
3
 *
4
 * (c) The GHC Team, 1998-2000
5
 *
6
7
8
 * Stable Pointers: A stable pointer is represented as an index into
 * the stable pointer table in the low BITS_PER_WORD-8 bits with a
 * weight in the upper 8 bits.
9
 *
10
11
12
13
14
15
16
17
18
 * SUP: StgStablePtr used to be a synonym for StgWord, but stable pointers
 * are guaranteed to be void* on the C-side, so we have to do some occasional
 * casting. Size is not a matter, because StgWord is always the same size as
 * a void*.
 *
 * ----------------------------------------------------------------------------*/

#define STABLEPTR_WEIGHT_MASK   ((StgWord)0xff << ((sizeof(StgWord)-1) * BITS_PER_BYTE))
#define STABLEPTR_WEIGHT_SHIFT  (BITS_IN(StgWord) - 8)
19
20
21
22
23
24
25
26

/* -----------------------------------------------------------------------------
   External C Interface
   -------------------------------------------------------------------------- */

extern StgPtr         deRefStablePtr(StgStablePtr stable_ptr);
extern void           freeStablePtr(StgStablePtr sp);
extern StgStablePtr   splitStablePtr(StgStablePtr sp);
27
extern StgStablePtr   getStablePtr(StgPtr p);
28
29
30
31
32
33

/* -----------------------------------------------------------------------------
   PRIVATE from here.
   -------------------------------------------------------------------------- */

typedef struct { 
34
  StgPtr  addr;			/* Haskell object, free list, or NULL */
35
  StgPtr  old;			/* old Haskell object, used during GC */
36
  StgWord weight;		/* used for reference counting */
37
  StgClosure *sn_obj;		/* the StableName object (or NULL) */
38
39
} snEntry;

sof's avatar
sof committed
40
41
extern DLL_IMPORT_RTS snEntry *stable_ptr_table;
extern DLL_IMPORT_RTS snEntry *stable_ptr_free;
42

sof's avatar
sof committed
43
extern DLL_IMPORT_RTS unsigned int SPT_size;
44
45
46
47

extern inline StgPtr
deRefStablePtr(StgStablePtr sp)
{
48
49
  ASSERT(stable_ptr_table[stgCast(StgWord,sp) & ~STABLEPTR_WEIGHT_MASK].weight > 0);
  return stable_ptr_table[stgCast(StgWord,sp) & ~STABLEPTR_WEIGHT_MASK].addr;
50
51
52
53
54
}

extern inline void
freeStablePtr(StgStablePtr sp)
{
55
  StgWord sn = stgCast(StgWord,sp) & ~STABLEPTR_WEIGHT_MASK;
56
57
58
59
60
  
  ASSERT(sn < SPT_size
	 && stable_ptr_table[sn].addr != NULL
	 && stable_ptr_table[sn].weight > 0);
  
61
62
  stable_ptr_table[sn].weight += 
      1 << ((((StgWord)sp & STABLEPTR_WEIGHT_MASK) >> STABLEPTR_WEIGHT_SHIFT) - 1);
63
64
65
66
67
68
}

extern inline StgStablePtr
splitStablePtr(StgStablePtr sp)
{
  /* doesn't need access to the stable pointer table */
69
70
  StgWord weight = (stgCast(StgWord,sp) & STABLEPTR_WEIGHT_MASK) / 2;
  return stgCast(StgStablePtr,(stgCast(StgWord,sp) & ~STABLEPTR_WEIGHT_MASK) + weight);
71
72
73
74
75
}

/* No deRefStableName, because the existence of a stable name doesn't
 * guarantee the existence of the object itself.
 */