[project @ 2001-06-28 14:15:04 by simonmar]
[ghc-base.git] / GHC / Stable.lhs
1 % -----------------------------------------------------------------------------
2 % $Id: Stable.lhs,v 1.1 2001/06/28 14:15:03 simonmar Exp $
3 %
4 % (c) The GHC Team, 1992-2000
5 %
6
7 \section{Module @GHC.Stable@}
8
9 \begin{code}
10 {-# OPTIONS -fno-implicit-prelude #-}
11
12 module GHC.Stable 
13         ( StablePtr(..)
14         , newStablePtr          -- :: a -> IO (StablePtr a)    
15         , deRefStablePtr        -- :: StablePtr a -> a
16         , freeStablePtr         -- :: StablePtr a -> IO ()
17         , castStablePtrToPtr    -- :: StablePtr a -> Ptr ()
18         , castPtrToStablePtr    -- :: Ptr () -> StablePtr a
19    ) where
20
21 import Foreign.Ptr
22
23 import GHC.Base
24 import GHC.IOBase
25
26 -----------------------------------------------------------------------------
27 -- Stable Pointers
28
29 data StablePtr a = StablePtr (StablePtr# a)
30
31 instance CCallable   (StablePtr a)
32 instance CReturnable (StablePtr a)
33
34 newStablePtr   :: a -> IO (StablePtr a)
35 newStablePtr a = IO $ \ s ->
36     case makeStablePtr# a s of (# s', sp #) -> (# s', StablePtr sp #)
37
38 deRefStablePtr :: StablePtr a -> IO a
39 deRefStablePtr (StablePtr sp) = IO $ \s -> deRefStablePtr# sp s
40
41 foreign import unsafe freeStablePtr :: StablePtr a -> IO ()
42
43 castStablePtrToPtr :: StablePtr a -> Ptr ()
44 castStablePtrToPtr (StablePtr s) = Ptr (unsafeCoerce# s)
45
46 castPtrToStablePtr :: Ptr () -> StablePtr a
47 castPtrToStablePtr (Ptr a) = StablePtr (unsafeCoerce# a)
48
49 instance Eq (StablePtr a) where 
50     (StablePtr sp1) == (StablePtr sp2) =
51         case eqStablePtr# sp1 sp2 of
52            0# -> False
53            _  -> True
54 \end{code}