{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE CPP #-}
module Raaz.Core.Memory
(
Memory(..), VoidMemory, copyMemory
, Initialisable(..), Extractable(..)
, InitialisableFromBuffer(..), ExtractableToBuffer(..)
, MemoryCell, withCellPointer, getCellPointer
, MemoryThread(..), doIO , getMemory, modify, execute
, MT
, liftPointerAction
, Alloc, pointerAlloc
) where
import Control.Applicative
import Control.Monad.IO.Class
#if MIN_VERSION_base(4,9,0)
import Data.Kind
#endif
import Foreign.Storable ( Storable )
import Foreign.Ptr ( castPtr, Ptr )
import Raaz.Core.MonoidalAction
import Raaz.Core.Transfer
import Raaz.Core.Types
newtype MT mem a = MT { MT mem a -> mem -> IO a
unMT :: mem -> IO a }
instance Functor (MT mem) where
fmap :: (a -> b) -> MT mem a -> MT mem b
fmap a -> b
f MT mem a
mst = (mem -> IO b) -> MT mem b
forall mem a. (mem -> IO a) -> MT mem a
MT ((mem -> IO b) -> MT mem b) -> (mem -> IO b) -> MT mem b
forall a b. (a -> b) -> a -> b
$ \ mem
m -> a -> b
f (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> MT mem a -> mem -> IO a
forall mem a. MT mem a -> mem -> IO a
unMT MT mem a
mst mem
m
instance Applicative (MT mem) where
pure :: a -> MT mem a
pure = (mem -> IO a) -> MT mem a
forall mem a. (mem -> IO a) -> MT mem a
MT ((mem -> IO a) -> MT mem a) -> (a -> mem -> IO a) -> a -> MT mem a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO a -> mem -> IO a
forall a b. a -> b -> a
const (IO a -> mem -> IO a) -> (a -> IO a) -> a -> mem -> IO a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
MT mem (a -> b)
mf <*> :: MT mem (a -> b) -> MT mem a -> MT mem b
<*> MT mem a
ma = (mem -> IO b) -> MT mem b
forall mem a. (mem -> IO a) -> MT mem a
MT ((mem -> IO b) -> MT mem b) -> (mem -> IO b) -> MT mem b
forall a b. (a -> b) -> a -> b
$ \ mem
m -> MT mem (a -> b) -> mem -> IO (a -> b)
forall mem a. MT mem a -> mem -> IO a
unMT MT mem (a -> b)
mf mem
m IO (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> MT mem a -> mem -> IO a
forall mem a. MT mem a -> mem -> IO a
unMT MT mem a
ma mem
m
instance Monad (MT mem) where
return :: a -> MT mem a
return = (mem -> IO a) -> MT mem a
forall mem a. (mem -> IO a) -> MT mem a
MT ((mem -> IO a) -> MT mem a) -> (a -> mem -> IO a) -> a -> MT mem a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO a -> mem -> IO a
forall a b. a -> b -> a
const (IO a -> mem -> IO a) -> (a -> IO a) -> a -> mem -> IO a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return
MT mem a
ma >>= :: MT mem a -> (a -> MT mem b) -> MT mem b
>>= a -> MT mem b
f = (mem -> IO b) -> MT mem b
forall mem a. (mem -> IO a) -> MT mem a
MT mem -> IO b
runIt
where runIt :: mem -> IO b
runIt mem
mem = MT mem a -> mem -> IO a
forall mem a. MT mem a -> mem -> IO a
unMT MT mem a
ma mem
mem IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \ a
a -> MT mem b -> mem -> IO b
forall mem a. MT mem a -> mem -> IO a
unMT (a -> MT mem b
f a
a) mem
mem
instance MonadIO (MT mem) where
liftIO :: IO a -> MT mem a
liftIO = (mem -> IO a) -> MT mem a
forall mem a. (mem -> IO a) -> MT mem a
MT ((mem -> IO a) -> MT mem a)
-> (IO a -> mem -> IO a) -> IO a -> MT mem a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO a -> mem -> IO a
forall a b. a -> b -> a
const
#if MIN_VERSION_base(4,9,0)
class MemoryThread (mT :: Type -> Type -> Type) where
#else
class MemoryThread (mT :: * -> * -> *) where
#endif
securely :: Memory mem => mT mem a -> IO a
insecurely :: Memory mem => mT mem a -> IO a
liftMT :: MT mem a -> mT mem a
onSubMemory :: (mem -> submem) -> mT submem a -> mT mem a
instance MemoryThread MT where
securely :: MT mem a -> IO a
securely = (mem -> IO a) -> IO a
forall m a. Memory m => (m -> IO a) -> IO a
withSecureMemory ((mem -> IO a) -> IO a)
-> (MT mem a -> mem -> IO a) -> MT mem a -> IO a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MT mem a -> mem -> IO a
forall mem a. MT mem a -> mem -> IO a
unMT
insecurely :: MT mem a -> IO a
insecurely = (mem -> IO a) -> IO a
forall m a. Memory m => (m -> IO a) -> IO a
withMemory ((mem -> IO a) -> IO a)
-> (MT mem a -> mem -> IO a) -> MT mem a -> IO a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MT mem a -> mem -> IO a
forall mem a. MT mem a -> mem -> IO a
unMT
liftMT :: MT mem a -> MT mem a
liftMT = MT mem a -> MT mem a
forall a. a -> a
id
onSubMemory :: (mem -> submem) -> MT submem a -> MT mem a
onSubMemory mem -> submem
proj MT submem a
mtsub = (mem -> IO a) -> MT mem a
forall mem a. (mem -> IO a) -> MT mem a
MT ((mem -> IO a) -> MT mem a) -> (mem -> IO a) -> MT mem a
forall a b. (a -> b) -> a -> b
$ MT submem a -> submem -> IO a
forall mem a. MT mem a -> mem -> IO a
unMT MT submem a
mtsub (submem -> IO a) -> (mem -> submem) -> mem -> IO a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. mem -> submem
proj
execute :: MemoryThread mT => (mem -> IO a) -> mT mem a
execute :: (mem -> IO a) -> mT mem a
execute = MT mem a -> mT mem a
forall (mT :: * -> * -> *) mem a.
MemoryThread mT =>
MT mem a -> mT mem a
liftMT (MT mem a -> mT mem a)
-> ((mem -> IO a) -> MT mem a) -> (mem -> IO a) -> mT mem a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (mem -> IO a) -> MT mem a
forall mem a. (mem -> IO a) -> MT mem a
MT
doIO :: MemoryThread mT => IO a -> mT mem a
doIO :: IO a -> mT mem a
doIO = (mem -> IO a) -> mT mem a
forall (mT :: * -> * -> *) mem a.
MemoryThread mT =>
(mem -> IO a) -> mT mem a
execute ((mem -> IO a) -> mT mem a)
-> (IO a -> mem -> IO a) -> IO a -> mT mem a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO a -> mem -> IO a
forall a b. a -> b -> a
const
type PointerAction m a b = (Pointer -> m a) -> m b
liftPointerAction :: PointerAction IO a b -> PointerAction (MT mem) a b
liftPointerAction :: PointerAction IO a b -> PointerAction (MT mem) a b
liftPointerAction PointerAction IO a b
allocator Pointer -> MT mem a
mtAction
= (mem -> IO b) -> MT mem b
forall (mT :: * -> * -> *) mem a.
MemoryThread mT =>
(mem -> IO a) -> mT mem a
execute ((mem -> IO b) -> MT mem b) -> (mem -> IO b) -> MT mem b
forall a b. (a -> b) -> a -> b
$ \ mem
mem -> PointerAction IO a b
allocator (\ Pointer
ptr -> MT mem a -> mem -> IO a
forall mem a. MT mem a -> mem -> IO a
unMT (Pointer -> MT mem a
mtAction Pointer
ptr) mem
mem)
getMemory :: MemoryThread mT => mT mem mem
getMemory :: mT mem mem
getMemory = (mem -> IO mem) -> mT mem mem
forall (mT :: * -> * -> *) mem a.
MemoryThread mT =>
(mem -> IO a) -> mT mem a
execute mem -> IO mem
forall (m :: * -> *) a. Monad m => a -> m a
return
type AllocField = Field Pointer
type Alloc mem = TwistRF AllocField (BYTES Int) mem
makeAlloc :: LengthUnit l => l -> (Pointer -> mem) -> Alloc mem
makeAlloc :: l -> (Pointer -> mem) -> Alloc mem
makeAlloc l
l Pointer -> mem
memCreate = WrappedArrow (->) Pointer mem -> BYTES Int -> Alloc mem
forall (f :: * -> *) m a. f a -> m -> TwistRF f m a
TwistRF ((Pointer -> mem) -> WrappedArrow (->) Pointer mem
forall (a :: * -> * -> *) b c. a b c -> WrappedArrow a b c
WrapArrow Pointer -> mem
memCreate) (BYTES Int -> Alloc mem) -> BYTES Int -> Alloc mem
forall a b. (a -> b) -> a -> b
$ l -> BYTES Int
forall src dest. (LengthUnit src, LengthUnit dest) => src -> dest
atLeast l
l
pointerAlloc :: LengthUnit l => l -> Alloc Pointer
pointerAlloc :: l -> Alloc Pointer
pointerAlloc l
l = l -> (Pointer -> Pointer) -> Alloc Pointer
forall l mem. LengthUnit l => l -> (Pointer -> mem) -> Alloc mem
makeAlloc l
l Pointer -> Pointer
forall a. a -> a
id
class Memory m where
memoryAlloc :: Alloc m
unsafeToPointer :: m -> Pointer
data VoidMemory = VoidMemory { VoidMemory -> Pointer
unVoidMemory :: Pointer }
instance Memory VoidMemory where
memoryAlloc :: Alloc VoidMemory
memoryAlloc = BYTES Int -> (Pointer -> VoidMemory) -> Alloc VoidMemory
forall l mem. LengthUnit l => l -> (Pointer -> mem) -> Alloc mem
makeAlloc (BYTES Int
0 :: BYTES Int) Pointer -> VoidMemory
VoidMemory
unsafeToPointer :: VoidMemory -> Pointer
unsafeToPointer = VoidMemory -> Pointer
unVoidMemory
instance ( Memory ma, Memory mb ) => Memory (ma, mb) where
memoryAlloc :: Alloc (ma, mb)
memoryAlloc = (,) (ma -> mb -> (ma, mb))
-> TwistRF AllocField (BYTES Int) ma
-> TwistRF AllocField (BYTES Int) (mb -> (ma, mb))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TwistRF AllocField (BYTES Int) ma
forall m. Memory m => Alloc m
memoryAlloc TwistRF AllocField (BYTES Int) (mb -> (ma, mb))
-> TwistRF AllocField (BYTES Int) mb -> Alloc (ma, mb)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> TwistRF AllocField (BYTES Int) mb
forall m. Memory m => Alloc m
memoryAlloc
unsafeToPointer :: (ma, mb) -> Pointer
unsafeToPointer (ma
ma, mb
_) = ma -> Pointer
forall m. Memory m => m -> Pointer
unsafeToPointer ma
ma
instance ( Memory ma
, Memory mb
, Memory mc
)
=> Memory (ma, mb, mc) where
memoryAlloc :: Alloc (ma, mb, mc)
memoryAlloc = (,,)
(ma -> mb -> mc -> (ma, mb, mc))
-> TwistRF AllocField (BYTES Int) ma
-> TwistRF AllocField (BYTES Int) (mb -> mc -> (ma, mb, mc))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TwistRF AllocField (BYTES Int) ma
forall m. Memory m => Alloc m
memoryAlloc
TwistRF AllocField (BYTES Int) (mb -> mc -> (ma, mb, mc))
-> TwistRF AllocField (BYTES Int) mb
-> TwistRF AllocField (BYTES Int) (mc -> (ma, mb, mc))
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> TwistRF AllocField (BYTES Int) mb
forall m. Memory m => Alloc m
memoryAlloc
TwistRF AllocField (BYTES Int) (mc -> (ma, mb, mc))
-> TwistRF AllocField (BYTES Int) mc -> Alloc (ma, mb, mc)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> TwistRF AllocField (BYTES Int) mc
forall m. Memory m => Alloc m
memoryAlloc
unsafeToPointer :: (ma, mb, mc) -> Pointer
unsafeToPointer (ma
ma,mb
_,mc
_) = ma -> Pointer
forall m. Memory m => m -> Pointer
unsafeToPointer ma
ma
instance ( Memory ma
, Memory mb
, Memory mc
, Memory md
)
=> Memory (ma, mb, mc, md) where
memoryAlloc :: Alloc (ma, mb, mc, md)
memoryAlloc = (,,,)
(ma -> mb -> mc -> md -> (ma, mb, mc, md))
-> TwistRF AllocField (BYTES Int) ma
-> TwistRF
AllocField (BYTES Int) (mb -> mc -> md -> (ma, mb, mc, md))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TwistRF AllocField (BYTES Int) ma
forall m. Memory m => Alloc m
memoryAlloc
TwistRF AllocField (BYTES Int) (mb -> mc -> md -> (ma, mb, mc, md))
-> TwistRF AllocField (BYTES Int) mb
-> TwistRF AllocField (BYTES Int) (mc -> md -> (ma, mb, mc, md))
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> TwistRF AllocField (BYTES Int) mb
forall m. Memory m => Alloc m
memoryAlloc
TwistRF AllocField (BYTES Int) (mc -> md -> (ma, mb, mc, md))
-> TwistRF AllocField (BYTES Int) mc
-> TwistRF AllocField (BYTES Int) (md -> (ma, mb, mc, md))
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> TwistRF AllocField (BYTES Int) mc
forall m. Memory m => Alloc m
memoryAlloc
TwistRF AllocField (BYTES Int) (md -> (ma, mb, mc, md))
-> TwistRF AllocField (BYTES Int) md -> Alloc (ma, mb, mc, md)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> TwistRF AllocField (BYTES Int) md
forall m. Memory m => Alloc m
memoryAlloc
unsafeToPointer :: (ma, mb, mc, md) -> Pointer
unsafeToPointer (ma
ma,mb
_,mc
_,md
_) = ma -> Pointer
forall m. Memory m => m -> Pointer
unsafeToPointer ma
ma
copyMemory :: Memory m => Dest m
-> Src m
-> IO ()
copyMemory :: Dest m -> Src m -> IO ()
copyMemory Dest m
dmem Src m
smem = Dest Pointer -> Src Pointer -> BYTES Int -> IO ()
forall (m :: * -> *) l.
(MonadIO m, LengthUnit l) =>
Dest Pointer -> Src Pointer -> l -> m ()
memcpy (m -> Pointer
forall m. Memory m => m -> Pointer
unsafeToPointer (m -> Pointer) -> Dest m -> Dest Pointer
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Dest m
dmem) (m -> Pointer
forall m. Memory m => m -> Pointer
unsafeToPointer (m -> Pointer) -> Src m -> Src Pointer
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Src m
smem) BYTES Int
sz
where sz :: BYTES Int
sz = TwistRF AllocField (BYTES Int) m -> BYTES Int
forall (f :: * -> *) m a. TwistRF f m a -> m
twistMonoidValue (TwistRF AllocField (BYTES Int) m -> BYTES Int)
-> TwistRF AllocField (BYTES Int) m -> BYTES Int
forall a b. (a -> b) -> a -> b
$ Src m -> TwistRF AllocField (BYTES Int) m
forall m. Memory m => Src m -> Alloc m
getAlloc Src m
smem
getAlloc :: Memory m => Src m -> Alloc m
getAlloc :: Src m -> Alloc m
getAlloc Src m
_ = Alloc m
forall m. Memory m => Alloc m
memoryAlloc
withMemory :: Memory m => (m -> IO a) -> IO a
withMemory :: (m -> IO a) -> IO a
withMemory = Alloc m -> (m -> IO a) -> IO a
forall m a. Alloc m -> (m -> IO a) -> IO a
withM Alloc m
forall m. Memory m => Alloc m
memoryAlloc
where withM :: Alloc m -> (m -> IO a) -> IO a
withM :: Alloc m -> (m -> IO a) -> IO a
withM Alloc m
alctr m -> IO a
action = BYTES Int -> (Pointer -> IO a) -> IO a
forall l b. LengthUnit l => l -> (Pointer -> IO b) -> IO b
allocaBuffer BYTES Int
sz Pointer -> IO a
actualAction
where sz :: BYTES Int
sz = Alloc m -> BYTES Int
forall (f :: * -> *) m a. TwistRF f m a -> m
twistMonoidValue Alloc m
alctr
getM :: Pointer -> m
getM = Field Pointer m -> Pointer -> m
forall space b. Field space b -> space -> b
computeField (Field Pointer m -> Pointer -> m)
-> Field Pointer m -> Pointer -> m
forall a b. (a -> b) -> a -> b
$ Alloc m -> Field Pointer m
forall (f :: * -> *) m a. TwistRF f m a -> f a
twistFunctorValue Alloc m
alctr
wipeIt :: Pointer -> IO ()
wipeIt Pointer
cptr = Pointer -> Word8 -> BYTES Int -> IO ()
forall (m :: * -> *) l.
(MonadIO m, LengthUnit l) =>
Pointer -> Word8 -> l -> m ()
memset Pointer
cptr Word8
0 BYTES Int
sz
actualAction :: Pointer -> IO a
actualAction Pointer
cptr = m -> IO a
action (Pointer -> m
getM Pointer
cptr) IO a -> IO () -> IO a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Pointer -> IO ()
wipeIt Pointer
cptr
withSecureMemory :: Memory m => (m -> IO a) -> IO a
withSecureMemory :: (m -> IO a) -> IO a
withSecureMemory = TwistRF AllocField (BYTES Int) m -> (m -> IO a) -> IO a
forall l b b.
LengthUnit l =>
TwistRF AllocField l b -> (b -> IO b) -> IO b
withSM TwistRF AllocField (BYTES Int) m
forall m. Memory m => Alloc m
memoryAlloc
where
withSM :: TwistRF AllocField l b -> (b -> IO b) -> IO b
withSM TwistRF AllocField l b
alctr b -> IO b
action = l -> (Pointer -> IO b) -> IO b
forall l b. LengthUnit l => l -> (Pointer -> IO b) -> IO b
allocaSecure l
sz ((Pointer -> IO b) -> IO b) -> (Pointer -> IO b) -> IO b
forall a b. (a -> b) -> a -> b
$ b -> IO b
action (b -> IO b) -> (Pointer -> b) -> Pointer -> IO b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Pointer -> b
getM
where sz :: l
sz = TwistRF AllocField l b -> l
forall (f :: * -> *) m a. TwistRF f m a -> m
twistMonoidValue TwistRF AllocField l b
alctr
getM :: Pointer -> b
getM = Field Pointer b -> Pointer -> b
forall space b. Field space b -> space -> b
computeField (Field Pointer b -> Pointer -> b)
-> Field Pointer b -> Pointer -> b
forall a b. (a -> b) -> a -> b
$ TwistRF AllocField l b -> Field Pointer b
forall (f :: * -> *) m a. TwistRF f m a -> f a
twistFunctorValue TwistRF AllocField l b
alctr
class Memory m => Initialisable m v where
initialise :: v -> MT m ()
class Memory m => m v where
:: MT m v
modify :: (Initialisable mem a, Extractable mem b, MemoryThread mT ) => (b -> a) -> mT mem ()
modify :: (b -> a) -> mT mem ()
modify b -> a
f = MT mem () -> mT mem ()
forall (mT :: * -> * -> *) mem a.
MemoryThread mT =>
MT mem a -> mT mem a
liftMT (MT mem () -> mT mem ()) -> MT mem () -> mT mem ()
forall a b. (a -> b) -> a -> b
$ MT mem b
forall m v. Extractable m v => MT m v
extract MT mem b -> (b -> MT mem ()) -> MT mem ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> MT mem ()
forall m v. Initialisable m v => v -> MT m ()
initialise (a -> MT mem ()) -> (b -> a) -> b -> MT mem ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> a
f
class Memory m => InitialisableFromBuffer m where
initialiser :: m -> ReadM (MT m)
class Memory m => m where
:: m -> WriteM (MT m)
newtype MemoryCell a = MemoryCell { MemoryCell a -> Ptr a
unMemoryCell :: Ptr a }
instance Storable a => Memory (MemoryCell a) where
memoryAlloc :: Alloc (MemoryCell a)
memoryAlloc = a -> Alloc (MemoryCell a)
forall b. Storable b => b -> Alloc (MemoryCell b)
allocator a
forall a. HasCallStack => a
undefined
where allocator :: Storable b => b -> Alloc (MemoryCell b)
allocator :: b -> Alloc (MemoryCell b)
allocator b
b = ALIGN -> (Pointer -> MemoryCell b) -> Alloc (MemoryCell b)
forall l mem. LengthUnit l => l -> (Pointer -> mem) -> Alloc mem
makeAlloc (b -> ALIGN
forall a. Storable a => a -> ALIGN
alignedSizeOf b
b) ((Pointer -> MemoryCell b) -> Alloc (MemoryCell b))
-> (Pointer -> MemoryCell b) -> Alloc (MemoryCell b)
forall a b. (a -> b) -> a -> b
$ Ptr b -> MemoryCell b
forall a. Ptr a -> MemoryCell a
MemoryCell (Ptr b -> MemoryCell b)
-> (Pointer -> Ptr b) -> Pointer -> MemoryCell b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Pointer -> Ptr b
forall a b. Ptr a -> Ptr b
castPtr
unsafeToPointer :: MemoryCell a -> Pointer
unsafeToPointer = Ptr a -> Pointer
forall a b. Ptr a -> Ptr b
castPtr (Ptr a -> Pointer)
-> (MemoryCell a -> Ptr a) -> MemoryCell a -> Pointer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MemoryCell a -> Ptr a
forall a. MemoryCell a -> Ptr a
unMemoryCell
actualCellPtr :: Storable a => MemoryCell a -> Ptr a
actualCellPtr :: MemoryCell a -> Ptr a
actualCellPtr = Ptr a -> Ptr a
forall a. Storable a => Ptr a -> Ptr a
nextAlignedPtr (Ptr a -> Ptr a)
-> (MemoryCell a -> Ptr a) -> MemoryCell a -> Ptr a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MemoryCell a -> Ptr a
forall a. MemoryCell a -> Ptr a
unMemoryCell
withCellPointer :: (MemoryThread mT, Storable a) => (Ptr a -> IO b) -> mT (MemoryCell a) b
{-# INLINE withCellPointer #-}
withCellPointer :: (Ptr a -> IO b) -> mT (MemoryCell a) b
withCellPointer Ptr a -> IO b
action = (MemoryCell a -> IO b) -> mT (MemoryCell a) b
forall (mT :: * -> * -> *) mem a.
MemoryThread mT =>
(mem -> IO a) -> mT mem a
execute ((MemoryCell a -> IO b) -> mT (MemoryCell a) b)
-> (MemoryCell a -> IO b) -> mT (MemoryCell a) b
forall a b. (a -> b) -> a -> b
$ Ptr a -> IO b
action (Ptr a -> IO b) -> (MemoryCell a -> Ptr a) -> MemoryCell a -> IO b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MemoryCell a -> Ptr a
forall a. Storable a => MemoryCell a -> Ptr a
actualCellPtr
getCellPointer :: (MemoryThread mT, Storable a) => mT (MemoryCell a) (Ptr a)
{-# INLINE getCellPointer #-}
getCellPointer :: mT (MemoryCell a) (Ptr a)
getCellPointer = MT (MemoryCell a) (Ptr a) -> mT (MemoryCell a) (Ptr a)
forall (mT :: * -> * -> *) mem a.
MemoryThread mT =>
MT mem a -> mT mem a
liftMT (MT (MemoryCell a) (Ptr a) -> mT (MemoryCell a) (Ptr a))
-> MT (MemoryCell a) (Ptr a) -> mT (MemoryCell a) (Ptr a)
forall a b. (a -> b) -> a -> b
$ MemoryCell a -> Ptr a
forall a. Storable a => MemoryCell a -> Ptr a
actualCellPtr (MemoryCell a -> Ptr a)
-> MT (MemoryCell a) (MemoryCell a) -> MT (MemoryCell a) (Ptr a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> MT (MemoryCell a) (MemoryCell a)
forall (mT :: * -> * -> *) mem. MemoryThread mT => mT mem mem
getMemory
instance Storable a => Initialisable (MemoryCell a) a where
initialise :: a -> MT (MemoryCell a) ()
initialise a
a = (MemoryCell a -> IO ()) -> MT (MemoryCell a) ()
forall (mT :: * -> * -> *) mem a.
MemoryThread mT =>
(mem -> IO a) -> mT mem a
execute ((MemoryCell a -> IO ()) -> MT (MemoryCell a) ())
-> (MemoryCell a -> IO ()) -> MT (MemoryCell a) ()
forall a b. (a -> b) -> a -> b
$ (Ptr a -> a -> IO ()) -> a -> Ptr a -> IO ()
forall a b c. (a -> b -> c) -> b -> a -> c
flip Ptr a -> a -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
pokeAligned a
a (Ptr a -> IO ())
-> (MemoryCell a -> Ptr a) -> MemoryCell a -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MemoryCell a -> Ptr a
forall a. MemoryCell a -> Ptr a
unMemoryCell
{-# INLINE initialise #-}
instance Storable a => Extractable (MemoryCell a) a where
extract :: MT (MemoryCell a) a
extract = (MemoryCell a -> IO a) -> MT (MemoryCell a) a
forall (mT :: * -> * -> *) mem a.
MemoryThread mT =>
(mem -> IO a) -> mT mem a
execute ((MemoryCell a -> IO a) -> MT (MemoryCell a) a)
-> (MemoryCell a -> IO a) -> MT (MemoryCell a) a
forall a b. (a -> b) -> a -> b
$ Ptr a -> IO a
forall a. Storable a => Ptr a -> IO a
peekAligned (Ptr a -> IO a) -> (MemoryCell a -> Ptr a) -> MemoryCell a -> IO a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MemoryCell a -> Ptr a
forall a. MemoryCell a -> Ptr a
unMemoryCell
{-# INLINE extract #-}
instance EndianStore a => InitialisableFromBuffer (MemoryCell a) where
initialiser :: MemoryCell a -> ReadM (MT (MemoryCell a))
initialiser = Int -> Dest (Ptr a) -> ReadM (MT (MemoryCell a))
forall a (m :: * -> *).
(EndianStore a, MonadIO m) =>
Int -> Dest (Ptr a) -> ReadM m
readInto Int
1 (Dest (Ptr a) -> ReadM (MT (MemoryCell a)))
-> (MemoryCell a -> Dest (Ptr a))
-> MemoryCell a
-> ReadM (MT (MemoryCell a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ptr a -> Dest (Ptr a)
forall a. a -> Dest a
destination (Ptr a -> Dest (Ptr a))
-> (MemoryCell a -> Ptr a) -> MemoryCell a -> Dest (Ptr a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MemoryCell a -> Ptr a
forall a. Storable a => MemoryCell a -> Ptr a
actualCellPtr
instance EndianStore a => ExtractableToBuffer (MemoryCell a) where
extractor :: MemoryCell a -> WriteM (MT (MemoryCell a))
extractor = Int -> Src (Ptr a) -> WriteM (MT (MemoryCell a))
forall (m :: * -> *) a.
(MonadIO m, EndianStore a) =>
Int -> Src (Ptr a) -> WriteM m
writeFrom Int
1 (Src (Ptr a) -> WriteM (MT (MemoryCell a)))
-> (MemoryCell a -> Src (Ptr a))
-> MemoryCell a
-> WriteM (MT (MemoryCell a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ptr a -> Src (Ptr a)
forall a. a -> Src a
source (Ptr a -> Src (Ptr a))
-> (MemoryCell a -> Ptr a) -> MemoryCell a -> Src (Ptr a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MemoryCell a -> Ptr a
forall a. Storable a => MemoryCell a -> Ptr a
actualCellPtr