[project @ 2003-09-16 13:03:37 by simonmar]
[ghc-base.git] / GHC / Stable.lhs
1 \begin{code}
2 {-# OPTIONS -fno-implicit-prelude #-}
3 -----------------------------------------------------------------------------
4 -- |
5 -- Module      :  GHC.Stable
6 -- Copyright   :  (c) The University of Glasgow, 1992-2002
7 -- License     :  see libraries/base/LICENSE
8 -- 
9 -- Maintainer  :  ffi@haskell.org
10 -- Stability   :  internal
11 -- Portability :  non-portable (GHC Extensions)
12 --
13 -- Stable pointers.
14 --
15 -----------------------------------------------------------------------------
16
17 module GHC.Stable 
18         ( StablePtr(..)
19         , newStablePtr          -- :: a -> IO (StablePtr a)    
20         , deRefStablePtr        -- :: StablePtr a -> a
21         , freeStablePtr         -- :: StablePtr a -> IO ()
22         , castStablePtrToPtr    -- :: StablePtr a -> Ptr ()
23         , castPtrToStablePtr    -- :: Ptr () -> StablePtr a
24    ) where
25
26 import GHC.Ptr
27 import GHC.Base
28 import GHC.IOBase
29
30 -----------------------------------------------------------------------------
31 -- Stable Pointers
32
33 data StablePtr a = StablePtr (StablePtr# a)
34
35 newStablePtr   :: a -> IO (StablePtr a)
36 newStablePtr a = IO $ \ s ->
37     case makeStablePtr# a s of (# s', sp #) -> (# s', StablePtr sp #)
38
39 deRefStablePtr :: StablePtr a -> IO a
40 deRefStablePtr (StablePtr sp) = IO $ \s -> deRefStablePtr# sp s
41
42 foreign import ccall unsafe freeStablePtr :: StablePtr a -> IO ()
43
44 castStablePtrToPtr :: StablePtr a -> Ptr ()
45 castStablePtrToPtr (StablePtr s) = Ptr (unsafeCoerce# s)
46
47 castPtrToStablePtr :: Ptr () -> StablePtr a
48 castPtrToStablePtr (Ptr a) = StablePtr (unsafeCoerce# a)
49
50 instance Eq (StablePtr a) where 
51     (StablePtr sp1) == (StablePtr sp2) =
52         case eqStablePtr# sp1 sp2 of
53            0# -> False
54            _  -> True
55 \end{code}