{-# LANGUAGE CPP #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE UnboxedTuples #-}
{-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-}
module GHC.StaticPtr
( StaticPtr
, deRefStaticPtr
, StaticKey
, staticKey
, unsafeLookupStaticPtr
, StaticPtrInfo(..)
, staticPtrInfo
, staticPtrKeys
, IsStatic(..)
) where
import Foreign.C.Types (CInt(..))
import Foreign.Marshal (allocaArray, peekArray, withArray)
import GHC.Ptr (Ptr(..), nullPtr)
import GHC.Fingerprint (Fingerprint(..))
import GHC.Prim
import GHC.Word (Word64(..))
#include "MachDeps.h"
#if WORD_SIZE_IN_BITS < 64
data StaticPtr a = StaticPtr Word64# Word64#
StaticPtrInfo a
#else
data StaticPtr a = StaticPtr Word# Word#
StaticPtrInfo a
#endif
deRefStaticPtr :: StaticPtr a -> a
deRefStaticPtr :: forall a. StaticPtr a -> a
deRefStaticPtr (StaticPtr Word#
_ Word#
_ StaticPtrInfo
_ a
v) = a
v
type StaticKey = Fingerprint
staticKey :: StaticPtr a -> StaticKey
staticKey :: forall a. StaticPtr a -> StaticKey
staticKey (StaticPtr Word#
w0 Word#
w1 StaticPtrInfo
_ a
_) = Word64 -> Word64 -> StaticKey
Fingerprint (Word# -> Word64
W64# Word#
w0) (Word# -> Word64
W64# Word#
w1)
unsafeLookupStaticPtr :: StaticKey -> IO (Maybe (StaticPtr a))
unsafeLookupStaticPtr :: forall a. StaticKey -> IO (Maybe (StaticPtr a))
unsafeLookupStaticPtr (Fingerprint Word64
w1 Word64
w2) = do
ptr :: Ptr Any
ptr@(Ptr Addr#
addr) <- forall a b. Storable a => [a] -> (Ptr a -> IO b) -> IO b
withArray [Word64
w1, Word64
w2] forall a. Ptr Word64 -> IO (Ptr a)
hs_spt_lookup
if (Ptr Any
ptr forall a. Eq a => a -> a -> Bool
== forall a. Ptr a
nullPtr)
then forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
else case forall a. Addr# -> (# a #)
addrToAny# Addr#
addr of
(# StaticPtr a
spe #) -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. a -> Maybe a
Just StaticPtr a
spe)
foreign import ccall unsafe hs_spt_lookup :: Ptr Word64 -> IO (Ptr a)
class IsStatic p where
fromStaticPtr :: StaticPtr a -> p a
instance IsStatic StaticPtr where
fromStaticPtr :: forall a. StaticPtr a -> StaticPtr a
fromStaticPtr = forall a. a -> a
id
data StaticPtrInfo = StaticPtrInfo
{
StaticPtrInfo -> String
spInfoUnitId :: String
, StaticPtrInfo -> String
spInfoModuleName :: String
, StaticPtrInfo -> (Int, Int)
spInfoSrcLoc :: (Int, Int)
}
deriving Int -> StaticPtrInfo -> ShowS
[StaticPtrInfo] -> ShowS
StaticPtrInfo -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [StaticPtrInfo] -> ShowS
$cshowList :: [StaticPtrInfo] -> ShowS
show :: StaticPtrInfo -> String
$cshow :: StaticPtrInfo -> String
showsPrec :: Int -> StaticPtrInfo -> ShowS
$cshowsPrec :: Int -> StaticPtrInfo -> ShowS
Show
staticPtrInfo :: StaticPtr a -> StaticPtrInfo
staticPtrInfo :: forall a. StaticPtr a -> StaticPtrInfo
staticPtrInfo (StaticPtr Word#
_ Word#
_ StaticPtrInfo
n a
_) = StaticPtrInfo
n
staticPtrKeys :: IO [StaticKey]
staticPtrKeys :: IO [StaticKey]
staticPtrKeys = do
CInt
keyCount <- IO CInt
hs_spt_key_count
forall a b. Storable a => Int -> (Ptr a -> IO b) -> IO b
allocaArray (forall a b. (Integral a, Num b) => a -> b
fromIntegral CInt
keyCount) forall a b. (a -> b) -> a -> b
$ \Ptr (Ptr Word64)
p -> do
CInt
count <- forall a. Ptr a -> CInt -> IO CInt
hs_spt_keys Ptr (Ptr Word64)
p CInt
keyCount
forall a. Storable a => Int -> Ptr a -> IO [a]
peekArray (forall a b. (Integral a, Num b) => a -> b
fromIntegral CInt
count) Ptr (Ptr Word64)
p forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (\Ptr Word64
pa -> forall a. Storable a => Int -> Ptr a -> IO [a]
peekArray Int
2 Ptr Word64
pa forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \[Word64
w1, Word64
w2] -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Word64 -> Word64 -> StaticKey
Fingerprint Word64
w1 Word64
w2)
{-# NOINLINE staticPtrKeys #-}
foreign import ccall unsafe hs_spt_key_count :: IO CInt
foreign import ccall unsafe hs_spt_keys :: Ptr a -> CInt -> IO CInt