[project @ 2002-02-12 11:44:54 by simonmar]
[ghc-hetmet.git] / ghc / lib / std / PrelStable.lhs
1 % -----------------------------------------------------------------------------
2 % $Id: PrelStable.lhs,v 1.9 2001/03/25 09:57:26 qrczak Exp $
3 %
4 % (c) The GHC Team, 1992-2000
5 %
6
7 \section{Module @PrelStable@}
8
9 \begin{code}
10 {-# OPTIONS -fno-implicit-prelude #-}
11
12 module PrelStable 
13         ( StablePtr(..)
14         , newStablePtr    -- :: a -> IO (StablePtr a)    
15         , deRefStablePtr  -- :: StablePtr a -> a
16         , freeStablePtr   -- :: StablePtr a -> IO ()
17    ) where
18
19 import PrelBase
20 import PrelIOBase
21
22 -----------------------------------------------------------------------------
23 -- Stable Pointers
24
25 data StablePtr a = StablePtr (StablePtr# a)
26
27 instance CCallable   (StablePtr a)
28 instance CReturnable (StablePtr a)
29
30 newStablePtr   :: a -> IO (StablePtr a)
31 newStablePtr a = IO $ \ s ->
32     case makeStablePtr# a s of (# s', sp #) -> (# s', StablePtr sp #)
33
34 deRefStablePtr :: StablePtr a -> IO a
35 deRefStablePtr (StablePtr sp) = IO $ \s -> deRefStablePtr# sp s
36
37 foreign import unsafe freeStablePtr :: StablePtr a -> IO ()
38
39 instance Eq (StablePtr a) where 
40     (StablePtr sp1) == (StablePtr sp2) =
41         case eqStablePtr# sp1 sp2 of
42            0# -> False
43            _  -> True
44 \end{code}