[project @ 2002-03-14 12:09:49 by simonmar]
[ghc-base.git] / GHC / Stable.lhs
1 % -----------------------------------------------------------------------------
2 % $Id: Stable.lhs,v 1.2 2002/03/14 12:09:52 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 GHC.Ptr
22 import GHC.Base
23 import GHC.IOBase
24
25 -----------------------------------------------------------------------------
26 -- Stable Pointers
27
28 data StablePtr a = StablePtr (StablePtr# a)
29
30 instance CCallable   (StablePtr a)
31 instance CReturnable (StablePtr a)
32
33 newStablePtr   :: a -> IO (StablePtr a)
34 newStablePtr a = IO $ \ s ->
35     case makeStablePtr# a s of (# s', sp #) -> (# s', StablePtr sp #)
36
37 deRefStablePtr :: StablePtr a -> IO a
38 deRefStablePtr (StablePtr sp) = IO $ \s -> deRefStablePtr# sp s
39
40 foreign import unsafe freeStablePtr :: StablePtr a -> IO ()
41
42 castStablePtrToPtr :: StablePtr a -> Ptr ()
43 castStablePtrToPtr (StablePtr s) = Ptr (unsafeCoerce# s)
44
45 castPtrToStablePtr :: Ptr () -> StablePtr a
46 castPtrToStablePtr (Ptr a) = StablePtr (unsafeCoerce# a)
47
48 instance Eq (StablePtr a) where 
49     (StablePtr sp1) == (StablePtr sp2) =
50         case eqStablePtr# sp1 sp2 of
51            0# -> False
52            _  -> True
53 \end{code}