{-# LANGUAGE Unsafe #-}
{-# LANGUAGE NoImplicitPrelude
, MagicHash
, UnboxedTuples
#-}
{-# OPTIONS_HADDOCK not-home #-}
module GHC.Stable (
StablePtr(..),
newStablePtr,
deRefStablePtr,
freeStablePtr,
castStablePtrToPtr,
castPtrToStablePtr
) where
import GHC.Ptr
import GHC.Base
data {-# CTYPE "HsStablePtr" #-} StablePtr a = StablePtr (StablePtr# a)
newStablePtr :: a -> IO (StablePtr a)
newStablePtr :: a -> IO (StablePtr a)
newStablePtr a :: a
a = (State# RealWorld -> (# State# RealWorld, StablePtr a #))
-> IO (StablePtr a)
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld -> (# State# RealWorld, StablePtr a #))
-> IO (StablePtr a))
-> (State# RealWorld -> (# State# RealWorld, StablePtr a #))
-> IO (StablePtr a)
forall a b. (a -> b) -> a -> b
$ \ s :: State# RealWorld
s ->
case a -> State# RealWorld -> (# State# RealWorld, StablePtr# a #)
forall a.
a -> State# RealWorld -> (# State# RealWorld, StablePtr# a #)
makeStablePtr# a
a State# RealWorld
s of (# s' :: State# RealWorld
s', sp :: StablePtr# a
sp #) -> (# State# RealWorld
s', StablePtr# a -> StablePtr a
forall a. StablePtr# a -> StablePtr a
StablePtr StablePtr# a
sp #)
deRefStablePtr :: StablePtr a -> IO a
deRefStablePtr :: StablePtr a -> IO a
deRefStablePtr (StablePtr sp :: StablePtr# a
sp) = (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld -> (# State# RealWorld, a #)) -> IO a)
-> (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
forall a b. (a -> b) -> a -> b
$ \s :: State# RealWorld
s -> StablePtr# a -> State# RealWorld -> (# State# RealWorld, a #)
forall a.
StablePtr# a -> State# RealWorld -> (# State# RealWorld, a #)
deRefStablePtr# StablePtr# a
sp State# RealWorld
s
foreign import ccall unsafe "hs_free_stable_ptr" freeStablePtr :: StablePtr a -> IO ()
castStablePtrToPtr :: StablePtr a -> Ptr ()
castStablePtrToPtr :: StablePtr a -> Ptr ()
castStablePtrToPtr (StablePtr s :: StablePtr# a
s) = Addr# -> Ptr ()
forall a. Addr# -> Ptr a
Ptr (StablePtr# a -> Addr#
unsafeCoerce# StablePtr# a
s)
castPtrToStablePtr :: Ptr () -> StablePtr a
castPtrToStablePtr :: Ptr () -> StablePtr a
castPtrToStablePtr (Ptr a :: Addr#
a) = StablePtr# a -> StablePtr a
forall a. StablePtr# a -> StablePtr a
StablePtr (Addr# -> StablePtr# a
unsafeCoerce# Addr#
a)
instance Eq (StablePtr a) where
(StablePtr sp1 :: StablePtr# a
sp1) == :: StablePtr a -> StablePtr a -> Bool
== (StablePtr sp2 :: StablePtr# a
sp2) =
case StablePtr# a -> StablePtr# a -> Int#
forall a. StablePtr# a -> StablePtr# a -> Int#
eqStablePtr# StablePtr# a
sp1 StablePtr# a
sp2 of
0# -> Bool
False
_ -> Bool
True