[project @ 2001-12-12 14:03:30 by simonmar]
[ghc-hetmet.git] / ghc / includes / Stable.h
1 /* -----------------------------------------------------------------------------
2  * $Id: Stable.h,v 1.11 2001/12/12 14:03:30 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 #ifndef STABLE_H
18 #define STABLE_H
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 extern StgStablePtr   getStablePtr(StgPtr p);
28
29 /* -----------------------------------------------------------------------------
30    PRIVATE from here.
31    -------------------------------------------------------------------------- */
32
33 typedef struct { 
34   StgPtr  addr;                 /* Haskell object, free list, or NULL */
35   StgPtr  old;                  /* old Haskell object, used during GC */
36   StgWord ref;                  /* used for reference counting */
37   StgClosure *sn_obj;           /* the StableName object (or NULL) */
38 } snEntry;
39
40 extern DLL_IMPORT_RTS snEntry *stable_ptr_table;
41 extern DLL_IMPORT_RTS snEntry *stable_ptr_free;
42
43 extern DLL_IMPORT_RTS unsigned int SPT_size;
44
45 extern inline StgPtr
46 deRefStablePtr(StgStablePtr sp)
47 {
48     ASSERT(stable_ptr_table[(StgWord)sp].ref > 0);
49     return stable_ptr_table[(StgWord)sp].addr;
50 }
51     
52 extern inline void
53 freeStablePtr(StgStablePtr sp)
54 {
55     StgWord sn = (StgWord)sp;
56     
57     ASSERT(sn < SPT_size
58            && stable_ptr_table[sn].addr != NULL
59            && stable_ptr_table[sn].ref > 0);
60     
61     stable_ptr_table[sn].ref --;
62 }
63
64 extern inline StgStablePtr
65 splitStablePtr(StgStablePtr sp)
66 {
67     stable_ptr_table[(StgWord)sp].ref ++;
68     return sp;
69 }
70
71 /* No deRefStableName, because the existence of a stable name doesn't
72  * guarantee the existence of the object itself.
73  */
74
75 #endif