{-# 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
import Unsafe.Coerce ( unsafeCoerceAddr )
data {-# CTYPE "HsStablePtr" #-} StablePtr a = StablePtr (StablePtr# a)
newStablePtr :: a -> IO (StablePtr a)
newStablePtr :: forall a. a -> IO (StablePtr a)
newStablePtr 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
$ \ 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 (# State# RealWorld
s', 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 :: forall a. StablePtr a -> IO a
deRefStablePtr (StablePtr 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
$ \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 :: forall a. StablePtr a -> Ptr ()
castStablePtrToPtr (StablePtr StablePtr# a
s) = Addr# -> Ptr ()
forall a. Addr# -> Ptr a
Ptr (StablePtr# a -> Addr#
forall (a :: TYPE 'AddrRep) (b :: TYPE 'AddrRep). a -> b
unsafeCoerceAddr StablePtr# a
s)
castPtrToStablePtr :: Ptr () -> StablePtr a
castPtrToStablePtr :: forall a. Ptr () -> StablePtr a
castPtrToStablePtr (Ptr Addr#
a) = StablePtr# a -> StablePtr a
forall a. StablePtr# a -> StablePtr a
StablePtr (Addr# -> StablePtr# a
forall (a :: TYPE 'AddrRep) (b :: TYPE 'AddrRep). a -> b
unsafeCoerceAddr Addr#
a)
instance Eq (StablePtr a) where
(StablePtr StablePtr# a
sp1) == :: StablePtr a -> StablePtr a -> Bool
== (StablePtr StablePtr# a
sp2) =
case StablePtr# a -> StablePtr# a -> Int#
forall a. StablePtr# a -> StablePtr# a -> Int#
eqStablePtr# StablePtr# a
sp1 StablePtr# a
sp2 of
Int#
0# -> Bool
False
Int#
_ -> Bool
True