{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE UndecidableInstances #-}
module WGPU.Internal.Memory
(
ToRaw (raw),
FromRaw (fromRaw),
ToRawPtr (rawPtr),
FromRawPtr (fromRawPtr),
ReadableMemoryBuffer (withReadablePtr, readableMemoryBufferSize),
ByteSize (ByteSize, unByteSize),
toCSize,
evalContT,
allocaC,
rawArrayPtr,
showWithPtr,
withCZeroingAfter,
newEmptyMVar,
takeMVar,
putMVar,
freeHaskellFunPtr,
poke,
)
where
import Control.Concurrent (MVar)
import qualified Control.Concurrent (newEmptyMVar, putMVar, takeMVar)
import Control.Monad.Cont (ContT (ContT), callCC, runContT)
import Control.Monad.IO.Class (MonadIO, liftIO)
import Data.ByteString (ByteString)
import Data.ByteString.Unsafe (unsafeUseAsCString)
import Data.Text (Text)
import qualified Data.Text as Text
import Data.Vector.Generic (Vector)
import qualified Data.Vector.Generic as Vector
import qualified Data.Vector.Storable as SVector
import Data.Word (Word32, Word8)
import Foreign
( FunPtr,
Ptr,
Storable,
Word64,
advancePtr,
alignment,
alloca,
allocaArray,
castPtr,
nullPtr,
peek,
sizeOf,
)
import qualified Foreign (fillBytes, freeHaskellFunPtr, poke)
import Foreign.C (CBool, CChar, CSize (CSize), peekCString, withCString)
class ToRaw a b | a -> b where
raw :: a -> ContT r IO b
class ToRawPtr a b where
rawPtr :: a -> ContT r IO (Ptr b)
class FromRaw b a | a -> b where
fromRaw :: MonadIO m => b -> m a
class FromRawPtr b a where
fromRawPtr :: MonadIO m => Ptr b -> m a
rawArrayPtr ::
forall v r a b.
(ToRaw a b, Storable b, Vector v a) =>
v a ->
ContT r IO (Ptr b)
rawArrayPtr :: v a -> ContT r IO (Ptr b)
rawArrayPtr v a
xs = ((Ptr b -> ContT r IO (Ptr b)) -> ContT r IO (Ptr b))
-> ContT r IO (Ptr b)
forall (m :: * -> *) a b. MonadCont m => ((a -> m b) -> m a) -> m a
callCC (((Ptr b -> ContT r IO (Ptr b)) -> ContT r IO (Ptr b))
-> ContT r IO (Ptr b))
-> ((Ptr b -> ContT r IO (Ptr b)) -> ContT r IO (Ptr b))
-> ContT r IO (Ptr b)
forall a b. (a -> b) -> a -> b
$ \Ptr b -> ContT r IO (Ptr b)
k -> do
let pokeRaw :: a -> Ptr b -> ContT c IO ()
pokeRaw :: a -> Ptr b -> ContT c IO ()
pokeRaw a
value Ptr b
raw_ptr = a -> ContT c IO b
forall a b r. ToRaw a b => a -> ContT r IO b
raw a
value ContT c IO b -> (b -> ContT c IO ()) -> ContT c IO ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= IO () -> ContT c IO ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ContT c IO ()) -> (b -> IO ()) -> b -> ContT c IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ptr b -> b -> IO ()
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Ptr a -> a -> m ()
poke Ptr b
raw_ptr
n :: Int
n :: Int
n = v a -> Int
forall (v :: * -> *) a. Vector v a => v a -> Int
Vector.length v a
xs
Ptr b
arrayPtr <- Int -> ContT r IO (Ptr b)
forall a r. Storable a => Int -> ContT r IO (Ptr a)
allocaArrayC Int
n
v a -> (Int -> a -> ContT r IO ()) -> ContT r IO ()
forall (m :: * -> *) (v :: * -> *) a b.
(Monad m, Vector v a) =>
v a -> (Int -> a -> m b) -> m ()
Vector.iforM_ v a
xs ((Int -> a -> ContT r IO ()) -> ContT r IO ())
-> (Int -> a -> ContT r IO ()) -> ContT r IO ()
forall a b. (a -> b) -> a -> b
$ \Int
i a
x -> a -> Ptr b -> ContT r IO ()
forall c. a -> Ptr b -> ContT c IO ()
pokeRaw a
x (Ptr b -> Int -> Ptr b
forall a. Storable a => Ptr a -> Int -> Ptr a
advancePtr Ptr b
arrayPtr Int
i)
Ptr b
r <- Ptr b -> ContT r IO (Ptr b)
k Ptr b
arrayPtr
Ptr b -> Int -> ContT r IO ()
forall (m :: * -> *) a. MonadIO m => Ptr a -> Int -> m ()
zeroMemory Ptr b
arrayPtr (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
* b -> Int
forall a. Storable a => a -> Int
alignment (b
forall a. HasCallStack => a
undefined :: b))
Ptr b -> ContT r IO (Ptr b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Ptr b
r
{-# INLINEABLE rawArrayPtr #-}
instance {-# OVERLAPPABLE #-} (Storable b, ToRaw a b) => ToRawPtr a b where
rawPtr :: a -> ContT r IO (Ptr b)
rawPtr a
x = a -> ContT r IO b
forall a b r. ToRaw a b => a -> ContT r IO b
raw a
x ContT r IO b -> (b -> ContT r IO (Ptr b)) -> ContT r IO (Ptr b)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= b -> ContT r IO (Ptr b)
forall a r. Storable a => a -> ContT r IO (Ptr a)
withCZeroingAfter
{-# INLINEABLE rawPtr #-}
instance {-# OVERLAPPABLE #-} (Storable b, FromRaw b a) => FromRawPtr b a where
fromRawPtr :: Ptr b -> m a
fromRawPtr Ptr b
ptr = (IO b -> m b
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO b -> m b) -> (Ptr b -> IO b) -> Ptr b -> m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ptr b -> IO b
forall a. Storable a => Ptr a -> IO a
peek) Ptr b
ptr m b -> (b -> m a) -> m a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= b -> m a
forall b a (m :: * -> *). (FromRaw b a, MonadIO m) => b -> m a
fromRaw
{-# INLINEABLE fromRawPtr #-}
instance ToRaw Bool CBool where
raw :: Bool -> ContT r IO CBool
raw Bool
x = CBool -> ContT r IO CBool
forall (f :: * -> *) a. Applicative f => a -> f a
pure (if Bool
x then CBool
1 else CBool
0)
{-# INLINE raw #-}
instance ToRaw Word32 Word32 where
raw :: Word32 -> ContT r IO Word32
raw = Word32 -> ContT r IO Word32
forall (f :: * -> *) a. Applicative f => a -> f a
pure
instance ToRawPtr Text CChar where
rawPtr :: Text -> ContT r IO (Ptr CChar)
rawPtr = String -> ContT r IO (Ptr CChar)
forall r. String -> ContT r IO (Ptr CChar)
withCStringC (String -> ContT r IO (Ptr CChar))
-> (Text -> String) -> Text -> ContT r IO (Ptr CChar)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
Text.unpack
{-# INLINEABLE rawPtr #-}
instance ToRawPtr ByteString Word8 where
rawPtr :: ByteString -> ContT r IO (Ptr Word8)
rawPtr = (Ptr CChar -> Ptr Word8)
-> ContT r IO (Ptr CChar) -> ContT r IO (Ptr Word8)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Ptr CChar -> Ptr Word8
forall a b. Ptr a -> Ptr b
castPtr (ContT r IO (Ptr CChar) -> ContT r IO (Ptr Word8))
-> (ByteString -> ContT r IO (Ptr CChar))
-> ByteString
-> ContT r IO (Ptr Word8)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ContT r IO (Ptr CChar)
forall r. ByteString -> ContT r IO (Ptr CChar)
unsafeUseAsCStringC
{-# INLINEABLE rawPtr #-}
instance FromRaw (Ptr CChar) Text where
fromRaw :: Ptr CChar -> m Text
fromRaw Ptr CChar
ptr =
if Ptr CChar
ptr Ptr CChar -> Ptr CChar -> Bool
forall a. Eq a => a -> a -> Bool
== Ptr CChar
forall a. Ptr a
nullPtr
then Text -> m Text
forall (f :: * -> *) a. Applicative f => a -> f a
pure Text
Text.empty
else (IO Text -> m Text
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Text -> m Text)
-> (Ptr CChar -> IO Text) -> Ptr CChar -> m Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String -> Text) -> IO String -> IO Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap String -> Text
Text.pack (IO String -> IO Text)
-> (Ptr CChar -> IO String) -> Ptr CChar -> IO Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ptr CChar -> IO String
peekCString) Ptr CChar
ptr
{-# INLINEABLE fromRaw #-}
allocaC :: Storable a => ContT r IO (Ptr a)
allocaC :: ContT r IO (Ptr a)
allocaC = ((Ptr a -> IO r) -> IO r) -> ContT r IO (Ptr a)
forall k (r :: k) (m :: k -> *) a.
((a -> m r) -> m r) -> ContT r m a
ContT (Ptr a -> IO r) -> IO r
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca
{-# INLINEABLE allocaC #-}
allocaArrayC :: Storable a => Int -> ContT r IO (Ptr a)
allocaArrayC :: Int -> ContT r IO (Ptr a)
allocaArrayC Int
sz = ((Ptr a -> IO r) -> IO r) -> ContT r IO (Ptr a)
forall k (r :: k) (m :: k -> *) a.
((a -> m r) -> m r) -> ContT r m a
ContT (Int -> (Ptr a -> IO r) -> IO r
forall a b. Storable a => Int -> (Ptr a -> IO b) -> IO b
allocaArray Int
sz)
{-# INLINEABLE allocaArrayC #-}
withCStringC :: String -> ContT r IO (Ptr CChar)
withCStringC :: String -> ContT r IO (Ptr CChar)
withCStringC String
str = ((Ptr CChar -> IO r) -> IO r) -> ContT r IO (Ptr CChar)
forall k (r :: k) (m :: k -> *) a.
((a -> m r) -> m r) -> ContT r m a
ContT (String -> (Ptr CChar -> IO r) -> IO r
forall a. String -> (Ptr CChar -> IO a) -> IO a
withCString String
str)
{-# INLINEABLE withCStringC #-}
unsafeUseAsCStringC :: ByteString -> ContT r IO (Ptr CChar)
unsafeUseAsCStringC :: ByteString -> ContT r IO (Ptr CChar)
unsafeUseAsCStringC ByteString
byteString = ((Ptr CChar -> IO r) -> IO r) -> ContT r IO (Ptr CChar)
forall k (r :: k) (m :: k -> *) a.
((a -> m r) -> m r) -> ContT r m a
ContT (ByteString -> (Ptr CChar -> IO r) -> IO r
forall a. ByteString -> (Ptr CChar -> IO a) -> IO a
unsafeUseAsCString ByteString
byteString)
{-# INLINEABLE unsafeUseAsCStringC #-}
withCZeroingAfter :: Storable a => a -> ContT r IO (Ptr a)
withCZeroingAfter :: a -> ContT r IO (Ptr a)
withCZeroingAfter a
x = ((Ptr a -> ContT r IO (Ptr a)) -> ContT r IO (Ptr a))
-> ContT r IO (Ptr a)
forall (m :: * -> *) a b. MonadCont m => ((a -> m b) -> m a) -> m a
callCC (((Ptr a -> ContT r IO (Ptr a)) -> ContT r IO (Ptr a))
-> ContT r IO (Ptr a))
-> ((Ptr a -> ContT r IO (Ptr a)) -> ContT r IO (Ptr a))
-> ContT r IO (Ptr a)
forall a b. (a -> b) -> a -> b
$ \Ptr a -> ContT r IO (Ptr a)
k -> do
Ptr a
ptr <- ContT r IO (Ptr a)
forall a r. Storable a => ContT r IO (Ptr a)
allocaC
Ptr a -> a -> ContT r IO ()
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Ptr a -> a -> m ()
poke Ptr a
ptr a
x
Ptr a
r <- Ptr a -> ContT r IO (Ptr a)
k Ptr a
ptr
Ptr a -> Int -> ContT r IO ()
forall (m :: * -> *) a. MonadIO m => Ptr a -> Int -> m ()
zeroMemory Ptr a
ptr (a -> Int
forall a. Storable a => a -> Int
sizeOf a
x)
Ptr a -> ContT r IO (Ptr a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Ptr a
r
{-# INLINEABLE withCZeroingAfter #-}
newEmptyMVar :: MonadIO m => m (MVar a)
newEmptyMVar :: m (MVar a)
newEmptyMVar = IO (MVar a) -> m (MVar a)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO (MVar a)
forall a. IO (MVar a)
Control.Concurrent.newEmptyMVar
{-# INLINEABLE newEmptyMVar #-}
takeMVar :: MonadIO m => MVar a -> m a
takeMVar :: MVar a -> m a
takeMVar = IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO a -> m a) -> (MVar a -> IO a) -> MVar a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MVar a -> IO a
forall a. MVar a -> IO a
Control.Concurrent.takeMVar
{-# INLINEABLE takeMVar #-}
putMVar :: MonadIO m => MVar a -> a -> m ()
putMVar :: MVar a -> a -> m ()
putMVar MVar a
mvar a
x = IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ MVar a -> a -> IO ()
forall a. MVar a -> a -> IO ()
Control.Concurrent.putMVar MVar a
mvar a
x
{-# INLINEABLE putMVar #-}
poke :: (MonadIO m, Storable a) => Ptr a -> a -> m ()
poke :: Ptr a -> a -> m ()
poke Ptr a
ptr a
value = IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Ptr a -> a -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
Foreign.poke Ptr a
ptr a
value)
{-# INLINEABLE poke #-}
freeHaskellFunPtr :: MonadIO m => FunPtr a -> m ()
freeHaskellFunPtr :: FunPtr a -> m ()
freeHaskellFunPtr = IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> (FunPtr a -> IO ()) -> FunPtr a -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FunPtr a -> IO ()
forall a. FunPtr a -> IO ()
Foreign.freeHaskellFunPtr
{-# INLINEABLE freeHaskellFunPtr #-}
fillBytes :: MonadIO m => Ptr a -> Word8 -> Int -> m ()
fillBytes :: Ptr a -> Word8 -> Int -> m ()
fillBytes Ptr a
ptr Word8
x Int
sz = IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Ptr a -> Word8 -> Int -> IO ()
forall a. Ptr a -> Word8 -> Int -> IO ()
Foreign.fillBytes Ptr a
ptr Word8
x Int
sz)
{-# INLINEABLE fillBytes #-}
zeroMemory :: MonadIO m => Ptr a -> Int -> m ()
zeroMemory :: Ptr a -> Int -> m ()
zeroMemory Ptr a
ptr = Ptr a -> Word8 -> Int -> m ()
forall (m :: * -> *) a. MonadIO m => Ptr a -> Word8 -> Int -> m ()
fillBytes Ptr a
ptr Word8
0x00
{-# INLINEABLE zeroMemory #-}
evalContT :: Monad m => ContT a m a -> m a
evalContT :: ContT a m a -> m a
evalContT ContT a m a
cont = ContT a m a -> (a -> m a) -> m a
forall k (r :: k) (m :: k -> *) a. ContT r m a -> (a -> m r) -> m r
runContT ContT a m a
cont a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
newtype ByteSize = ByteSize {ByteSize -> Word64
unByteSize :: Word64}
deriving (ByteSize -> ByteSize -> Bool
(ByteSize -> ByteSize -> Bool)
-> (ByteSize -> ByteSize -> Bool) -> Eq ByteSize
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ByteSize -> ByteSize -> Bool
$c/= :: ByteSize -> ByteSize -> Bool
== :: ByteSize -> ByteSize -> Bool
$c== :: ByteSize -> ByteSize -> Bool
Eq, Eq ByteSize
Eq ByteSize
-> (ByteSize -> ByteSize -> Ordering)
-> (ByteSize -> ByteSize -> Bool)
-> (ByteSize -> ByteSize -> Bool)
-> (ByteSize -> ByteSize -> Bool)
-> (ByteSize -> ByteSize -> Bool)
-> (ByteSize -> ByteSize -> ByteSize)
-> (ByteSize -> ByteSize -> ByteSize)
-> Ord ByteSize
ByteSize -> ByteSize -> Bool
ByteSize -> ByteSize -> Ordering
ByteSize -> ByteSize -> ByteSize
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: ByteSize -> ByteSize -> ByteSize
$cmin :: ByteSize -> ByteSize -> ByteSize
max :: ByteSize -> ByteSize -> ByteSize
$cmax :: ByteSize -> ByteSize -> ByteSize
>= :: ByteSize -> ByteSize -> Bool
$c>= :: ByteSize -> ByteSize -> Bool
> :: ByteSize -> ByteSize -> Bool
$c> :: ByteSize -> ByteSize -> Bool
<= :: ByteSize -> ByteSize -> Bool
$c<= :: ByteSize -> ByteSize -> Bool
< :: ByteSize -> ByteSize -> Bool
$c< :: ByteSize -> ByteSize -> Bool
compare :: ByteSize -> ByteSize -> Ordering
$ccompare :: ByteSize -> ByteSize -> Ordering
$cp1Ord :: Eq ByteSize
Ord, Int -> ByteSize -> ShowS
[ByteSize] -> ShowS
ByteSize -> String
(Int -> ByteSize -> ShowS)
-> (ByteSize -> String) -> ([ByteSize] -> ShowS) -> Show ByteSize
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ByteSize] -> ShowS
$cshowList :: [ByteSize] -> ShowS
show :: ByteSize -> String
$cshow :: ByteSize -> String
showsPrec :: Int -> ByteSize -> ShowS
$cshowsPrec :: Int -> ByteSize -> ShowS
Show, Int -> ByteSize
ByteSize -> Int
ByteSize -> [ByteSize]
ByteSize -> ByteSize
ByteSize -> ByteSize -> [ByteSize]
ByteSize -> ByteSize -> ByteSize -> [ByteSize]
(ByteSize -> ByteSize)
-> (ByteSize -> ByteSize)
-> (Int -> ByteSize)
-> (ByteSize -> Int)
-> (ByteSize -> [ByteSize])
-> (ByteSize -> ByteSize -> [ByteSize])
-> (ByteSize -> ByteSize -> [ByteSize])
-> (ByteSize -> ByteSize -> ByteSize -> [ByteSize])
-> Enum ByteSize
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: ByteSize -> ByteSize -> ByteSize -> [ByteSize]
$cenumFromThenTo :: ByteSize -> ByteSize -> ByteSize -> [ByteSize]
enumFromTo :: ByteSize -> ByteSize -> [ByteSize]
$cenumFromTo :: ByteSize -> ByteSize -> [ByteSize]
enumFromThen :: ByteSize -> ByteSize -> [ByteSize]
$cenumFromThen :: ByteSize -> ByteSize -> [ByteSize]
enumFrom :: ByteSize -> [ByteSize]
$cenumFrom :: ByteSize -> [ByteSize]
fromEnum :: ByteSize -> Int
$cfromEnum :: ByteSize -> Int
toEnum :: Int -> ByteSize
$ctoEnum :: Int -> ByteSize
pred :: ByteSize -> ByteSize
$cpred :: ByteSize -> ByteSize
succ :: ByteSize -> ByteSize
$csucc :: ByteSize -> ByteSize
Enum, Num ByteSize
Ord ByteSize
Num ByteSize
-> Ord ByteSize -> (ByteSize -> Rational) -> Real ByteSize
ByteSize -> Rational
forall a. Num a -> Ord a -> (a -> Rational) -> Real a
toRational :: ByteSize -> Rational
$ctoRational :: ByteSize -> Rational
$cp2Real :: Ord ByteSize
$cp1Real :: Num ByteSize
Real, Enum ByteSize
Real ByteSize
Real ByteSize
-> Enum ByteSize
-> (ByteSize -> ByteSize -> ByteSize)
-> (ByteSize -> ByteSize -> ByteSize)
-> (ByteSize -> ByteSize -> ByteSize)
-> (ByteSize -> ByteSize -> ByteSize)
-> (ByteSize -> ByteSize -> (ByteSize, ByteSize))
-> (ByteSize -> ByteSize -> (ByteSize, ByteSize))
-> (ByteSize -> Integer)
-> Integral ByteSize
ByteSize -> Integer
ByteSize -> ByteSize -> (ByteSize, ByteSize)
ByteSize -> ByteSize -> ByteSize
forall a.
Real a
-> Enum a
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> (a, a))
-> (a -> a -> (a, a))
-> (a -> Integer)
-> Integral a
toInteger :: ByteSize -> Integer
$ctoInteger :: ByteSize -> Integer
divMod :: ByteSize -> ByteSize -> (ByteSize, ByteSize)
$cdivMod :: ByteSize -> ByteSize -> (ByteSize, ByteSize)
quotRem :: ByteSize -> ByteSize -> (ByteSize, ByteSize)
$cquotRem :: ByteSize -> ByteSize -> (ByteSize, ByteSize)
mod :: ByteSize -> ByteSize -> ByteSize
$cmod :: ByteSize -> ByteSize -> ByteSize
div :: ByteSize -> ByteSize -> ByteSize
$cdiv :: ByteSize -> ByteSize -> ByteSize
rem :: ByteSize -> ByteSize -> ByteSize
$crem :: ByteSize -> ByteSize -> ByteSize
quot :: ByteSize -> ByteSize -> ByteSize
$cquot :: ByteSize -> ByteSize -> ByteSize
$cp2Integral :: Enum ByteSize
$cp1Integral :: Real ByteSize
Integral, Integer -> ByteSize
ByteSize -> ByteSize
ByteSize -> ByteSize -> ByteSize
(ByteSize -> ByteSize -> ByteSize)
-> (ByteSize -> ByteSize -> ByteSize)
-> (ByteSize -> ByteSize -> ByteSize)
-> (ByteSize -> ByteSize)
-> (ByteSize -> ByteSize)
-> (ByteSize -> ByteSize)
-> (Integer -> ByteSize)
-> Num ByteSize
forall a.
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (Integer -> a)
-> Num a
fromInteger :: Integer -> ByteSize
$cfromInteger :: Integer -> ByteSize
signum :: ByteSize -> ByteSize
$csignum :: ByteSize -> ByteSize
abs :: ByteSize -> ByteSize
$cabs :: ByteSize -> ByteSize
negate :: ByteSize -> ByteSize
$cnegate :: ByteSize -> ByteSize
* :: ByteSize -> ByteSize -> ByteSize
$c* :: ByteSize -> ByteSize -> ByteSize
- :: ByteSize -> ByteSize -> ByteSize
$c- :: ByteSize -> ByteSize -> ByteSize
+ :: ByteSize -> ByteSize -> ByteSize
$c+ :: ByteSize -> ByteSize -> ByteSize
Num)
instance ToRaw ByteSize CSize where
raw :: ByteSize -> ContT r IO CSize
raw = CSize -> ContT r IO CSize
forall (f :: * -> *) a. Applicative f => a -> f a
pure (CSize -> ContT r IO CSize)
-> (ByteSize -> CSize) -> ByteSize -> ContT r IO CSize
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteSize -> CSize
toCSize
toCSize :: ByteSize -> CSize
toCSize :: ByteSize -> CSize
toCSize = Word64 -> CSize
CSize (Word64 -> CSize) -> (ByteSize -> Word64) -> ByteSize -> CSize
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteSize -> Word64
unByteSize
class ReadableMemoryBuffer a where
withReadablePtr :: a -> (Ptr () -> IO b) -> IO b
readableMemoryBufferSize :: a -> ByteSize
instance {-# OVERLAPPABLE #-} Storable a => ReadableMemoryBuffer a where
withReadablePtr :: a -> (Ptr () -> IO b) -> IO b
withReadablePtr a
x Ptr () -> IO b
action =
(Ptr a -> IO b) -> IO b
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca ((Ptr a -> IO b) -> IO b) -> (Ptr a -> IO b) -> IO b
forall a b. (a -> b) -> a -> b
$ \Ptr a
ptr -> do
Ptr a -> a -> IO ()
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Ptr a -> a -> m ()
poke Ptr a
ptr a
x
b
result <- Ptr () -> IO b
action (Ptr a -> Ptr ()
forall a b. Ptr a -> Ptr b
castPtr Ptr a
ptr)
Ptr a -> Int -> IO ()
forall (m :: * -> *) a. MonadIO m => Ptr a -> Int -> m ()
zeroMemory Ptr a
ptr (a -> Int
forall a. Storable a => a -> Int
sizeOf (a
forall a. HasCallStack => a
undefined :: a))
b -> IO b
forall (f :: * -> *) a. Applicative f => a -> f a
pure b
result
{-# INLINEABLE withReadablePtr #-}
readableMemoryBufferSize :: a -> ByteSize
readableMemoryBufferSize a
x = Word64 -> ByteSize
ByteSize (Int -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (a -> Int
forall a. Storable a => a -> Int
sizeOf a
x))
{-# INLINEABLE readableMemoryBufferSize #-}
instance
{-# OVERLAPPABLE #-}
Storable a =>
ReadableMemoryBuffer (SVector.Vector a)
where
withReadablePtr :: Vector a -> (Ptr () -> IO b) -> IO b
withReadablePtr Vector a
vec Ptr () -> IO b
action = Vector a -> (Ptr a -> IO b) -> IO b
forall a b. Storable a => Vector a -> (Ptr a -> IO b) -> IO b
SVector.unsafeWith Vector a
vec (Ptr () -> IO b
action (Ptr () -> IO b) -> (Ptr a -> Ptr ()) -> Ptr a -> IO b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ptr a -> Ptr ()
forall a b. Ptr a -> Ptr b
castPtr)
{-# INLINEABLE withReadablePtr #-}
readableMemoryBufferSize :: Vector a -> ByteSize
readableMemoryBufferSize Vector a
vec =
Word64 -> ByteSize
ByteSize (Word64 -> ByteSize) -> (Int -> Word64) -> Int -> ByteSize
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> ByteSize) -> Int -> ByteSize
forall a b. (a -> b) -> a -> b
$ a -> Int
forall a. Storable a => a -> Int
sizeOf (a
forall a. HasCallStack => a
undefined :: a) Int -> Int -> Int
forall a. Num a => a -> a -> a
* Vector a -> Int
forall a. Storable a => Vector a -> Int
SVector.length Vector a
vec
{-# INLINEABLE readableMemoryBufferSize #-}
showWithPtr ::
String ->
Ptr a ->
String
showWithPtr :: String -> Ptr a -> String
showWithPtr String
name Ptr a
ptr = String
"<" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
name String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
":" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Ptr a -> String
forall a. Show a => a -> String
show Ptr a
ptr String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
">"
{-# INLINEABLE showWithPtr #-}