{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE RoleAnnotations #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilyDependencies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
{-# OPTIONS_HADDOCK hide, not-home #-}
module Data.Prim.Memory.Internal
( module Data.Prim.Memory.Internal
, module Data.Prim.Memory.Bytes.Internal
) where
import Control.Prim.Exception
import Control.Prim.Monad.Unsafe
import qualified Data.ByteString as BS
import Data.Foldable as Foldable
import Data.Kind
import Data.List as List
import Data.List.NonEmpty (NonEmpty(..))
import qualified Data.Monoid as Monoid
import Data.Prim
import Data.Prim.Array
import Data.Prim.Memory.Bytes.Internal
import Data.Prim.Memory.ByteString
import Data.Prim.Memory.ForeignPtr
import Data.Prim.Memory.Ptr
import qualified Data.Prim.Memory.Text as T
import qualified Data.Semigroup as Semigroup
import Foreign.Prim
import Numeric (showHex)
class MemRead mr where
isSameMem :: mr -> mr -> Bool
byteCountMem :: mr -> Count Word8
indexOffMem :: Prim e
=> mr
-> Off e
-> e
indexOffMem mr
mr Off e
off = mr -> Off Word8 -> e
forall mr e. (MemRead mr, Prim e) => mr -> Off Word8 -> e
indexByteOffMem mr
mr (Off e -> Off Word8
forall e. Prim e => Off e -> Off Word8
toByteOff Off e
off)
{-# INLINE indexOffMem #-}
indexByteOffMem :: Prim e
=> mr
-> Off Word8
-> e
copyByteOffToMBytesMem ::
(MonadPrim s m, Prim e)
=> mr
-> Off Word8
-> MBytes p s
-> Off Word8
-> Count e
-> m ()
copyByteOffToPtrMem ::
(MonadPrim s m, Prim e)
=> mr
-> Off Word8
-> Ptr e
-> Off Word8
-> Count e
-> m ()
compareByteOffToPtrMem ::
(MonadPrim s m, Prim e)
=> mr
-> Off Word8
-> Ptr e
-> Off Word8
-> Count e
-> m Ordering
compareByteOffToBytesMem ::
Prim e
=> mr
-> Off Word8
-> Bytes p
-> Off Word8
-> Count e
-> Ordering
compareByteOffMem ::
(MemRead mr', Prim e)
=> mr'
-> Off Word8
-> mr
-> Off Word8
-> Count e
-> Ordering
class MemWrite mw where
isSameMutMem :: mw s -> mw s -> Bool
readOffMutMem :: (MonadPrim s m, Prim e)
=> mw s
-> Off e
-> m e
readOffMutMem mw s
mw Off e
off = mw s -> Off Word8 -> m e
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
mw s -> Off Word8 -> m e
readByteOffMutMem mw s
mw (Off e -> Off Word8
forall e. Prim e => Off e -> Off Word8
toByteOff Off e
off)
{-# INLINE readOffMutMem #-}
readByteOffMutMem :: (MonadPrim s m, Prim e)
=> mw s
-> Off Word8
-> m e
writeOffMutMem :: (MonadPrim s m, Prim e)
=> mw s
-> Off e
-> e
-> m ()
writeOffMutMem mw s
mw Off e
off = mw s -> Off Word8 -> e -> m ()
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
mw s -> Off Word8 -> e -> m ()
writeByteOffMutMem mw s
mw (Off e -> Off Word8
forall e. Prim e => Off e -> Off Word8
toByteOff Off e
off)
{-# INLINE writeOffMutMem #-}
writeByteOffMutMem :: (MonadPrim s m, Prim e)
=> mw s
-> Off Word8
-> e -> m ()
moveByteOffToMBytesMutMem ::
(MonadPrim s m, Prim e)
=> mw s
-> Off Word8
-> MBytes p s
-> Off Word8
-> Count e
-> m ()
moveByteOffToPtrMutMem ::
(MonadPrim s m, Prim e)
=> mw s
-> Off Word8
-> Ptr e
-> Off Word8
-> Count e
-> m ()
copyByteOffMem :: (MonadPrim s m, MemRead mr, Prim e)
=> mr
-> Off Word8
-> mw s
-> Off Word8
-> Count e
-> m ()
moveByteOffMutMem :: (MonadPrim s m, MemWrite mw', Prim e)
=> mw' s
-> Off Word8
-> mw s
-> Off Word8
-> Count e
-> m ()
setMutMem
:: (MonadPrim s m, Prim e)
=> mw s
-> Off e
-> Count e
-> e
-> m ()
class (MemRead (FrozenMem ma), MemWrite ma) => MemAlloc ma where
type FrozenMem ma = (fm :: Type) | fm -> ma
getByteCountMutMem :: MonadPrim s m => ma s -> m (Count Word8)
allocMutMem :: (Prim e, MonadPrim s m)
=> Count e
-> m (ma s)
thawMem :: MonadPrim s m => FrozenMem ma -> m (ma s)
freezeMutMem :: MonadPrim s m => ma s -> m (FrozenMem ma)
reallocMutMem :: (MonadPrim s m, Prim e)
=> ma s
-> Count e
-> m (ma s)
reallocMutMem = ma s -> Count e -> m (ma s)
forall e (ma :: * -> *) s (m :: * -> *).
(Prim e, MemAlloc ma, MonadPrim s m) =>
ma s -> Count e -> m (ma s)
defaultReallocMutMem
{-# INLINE reallocMutMem #-}
instance MemRead (UArray e) where
isSameMem :: UArray e -> UArray e -> Bool
isSameMem = UArray e -> UArray e -> Bool
forall a b. UArray a -> UArray b -> Bool
isSameUArray
{-# INLINE isSameMem #-}
byteCountMem :: UArray e -> Count Word8
byteCountMem = Bytes 'Inc -> Count Word8
forall mr. MemRead mr => mr -> Count Word8
byteCountMem (Bytes 'Inc -> Count Word8)
-> (UArray e -> Bytes 'Inc) -> UArray e -> Count Word8
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UArray e -> Bytes 'Inc
forall e. UArray e -> Bytes 'Inc
fromUArrayBytes
{-# INLINE byteCountMem #-}
indexOffMem :: UArray e -> Off e -> e
indexOffMem UArray e
a = Bytes 'Inc -> Off e -> e
forall mr e. (MemRead mr, Prim e) => mr -> Off e -> e
indexOffMem (UArray e -> Bytes 'Inc
forall e. UArray e -> Bytes 'Inc
fromUArrayBytes UArray e
a)
{-# INLINE indexOffMem #-}
indexByteOffMem :: UArray e -> Off Word8 -> e
indexByteOffMem UArray e
a = Bytes 'Inc -> Off Word8 -> e
forall mr e. (MemRead mr, Prim e) => mr -> Off Word8 -> e
indexByteOffMem (UArray e -> Bytes 'Inc
forall e. UArray e -> Bytes 'Inc
fromUArrayBytes UArray e
a)
{-# INLINE indexByteOffMem #-}
copyByteOffToMBytesMem :: UArray e -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
copyByteOffToMBytesMem UArray e
a = Bytes 'Inc
-> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
forall mr s (m :: * -> *) e (p :: Pinned).
(MemRead mr, MonadPrim s m, Prim e) =>
mr -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
copyByteOffToMBytesMem (UArray e -> Bytes 'Inc
forall e. UArray e -> Bytes 'Inc
fromUArrayBytes UArray e
a)
{-# INLINE copyByteOffToMBytesMem #-}
copyByteOffToPtrMem :: UArray e -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
copyByteOffToPtrMem UArray e
a = Bytes 'Inc -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
forall mr s (m :: * -> *) e.
(MemRead mr, MonadPrim s m, Prim e) =>
mr -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
copyByteOffToPtrMem (UArray e -> Bytes 'Inc
forall e. UArray e -> Bytes 'Inc
fromUArrayBytes UArray e
a)
{-# INLINE copyByteOffToPtrMem #-}
compareByteOffToPtrMem :: UArray e
-> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m Ordering
compareByteOffToPtrMem UArray e
a = Bytes 'Inc
-> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m Ordering
forall mr s (m :: * -> *) e.
(MemRead mr, MonadPrim s m, Prim e) =>
mr -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m Ordering
compareByteOffToPtrMem (UArray e -> Bytes 'Inc
forall e. UArray e -> Bytes 'Inc
fromUArrayBytes UArray e
a)
{-# INLINE compareByteOffToPtrMem #-}
compareByteOffToBytesMem :: UArray e
-> Off Word8 -> Bytes p -> Off Word8 -> Count e -> Ordering
compareByteOffToBytesMem UArray e
a = Bytes 'Inc
-> Off Word8 -> Bytes p -> Off Word8 -> Count e -> Ordering
forall mr e (p :: Pinned).
(MemRead mr, Prim e) =>
mr -> Off Word8 -> Bytes p -> Off Word8 -> Count e -> Ordering
compareByteOffToBytesMem (UArray e -> Bytes 'Inc
forall e. UArray e -> Bytes 'Inc
fromUArrayBytes UArray e
a)
{-# INLINE compareByteOffToBytesMem #-}
compareByteOffMem :: mr' -> Off Word8 -> UArray e -> Off Word8 -> Count e -> Ordering
compareByteOffMem mr'
mem1 Off Word8
off1 UArray e
a = mr' -> Off Word8 -> Bytes 'Inc -> Off Word8 -> Count e -> Ordering
forall mr mr' e.
(MemRead mr, MemRead mr', Prim e) =>
mr' -> Off Word8 -> mr -> Off Word8 -> Count e -> Ordering
compareByteOffMem mr'
mem1 Off Word8
off1 (UArray e -> Bytes 'Inc
forall e. UArray e -> Bytes 'Inc
fromUArrayBytes UArray e
a)
{-# INLINE compareByteOffMem #-}
instance MemWrite (UMArray e) where
isSameMutMem :: UMArray e s -> UMArray e s -> Bool
isSameMutMem = UMArray e s -> UMArray e s -> Bool
forall a b s. UMArray a s -> UMArray b s -> Bool
isSameUMArray
{-# INLINE isSameMutMem #-}
readOffMutMem :: UMArray e s -> Off e -> m e
readOffMutMem UMArray e s
ma = MBytes 'Inc s -> Off e -> m e
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
mw s -> Off e -> m e
readOffMutMem (UMArray e s -> MBytes 'Inc s
forall e s. UMArray e s -> MBytes 'Inc s
fromUMArrayMBytes UMArray e s
ma)
{-# INLINE readOffMutMem #-}
readByteOffMutMem :: UMArray e s -> Off Word8 -> m e
readByteOffMutMem UMArray e s
ma = MBytes 'Inc s -> Off Word8 -> m e
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
mw s -> Off Word8 -> m e
readByteOffMutMem (UMArray e s -> MBytes 'Inc s
forall e s. UMArray e s -> MBytes 'Inc s
fromUMArrayMBytes UMArray e s
ma)
{-# INLINE readByteOffMutMem #-}
writeOffMutMem :: UMArray e s -> Off e -> e -> m ()
writeOffMutMem UMArray e s
ma = MBytes 'Inc s -> Off e -> e -> m ()
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
mw s -> Off e -> e -> m ()
writeOffMutMem (UMArray e s -> MBytes 'Inc s
forall e s. UMArray e s -> MBytes 'Inc s
fromUMArrayMBytes UMArray e s
ma)
{-# INLINE writeOffMutMem #-}
writeByteOffMutMem :: UMArray e s -> Off Word8 -> e -> m ()
writeByteOffMutMem UMArray e s
ma = MBytes 'Inc s -> Off Word8 -> e -> m ()
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
mw s -> Off Word8 -> e -> m ()
writeByteOffMutMem (UMArray e s -> MBytes 'Inc s
forall e s. UMArray e s -> MBytes 'Inc s
fromUMArrayMBytes UMArray e s
ma)
{-# INLINE writeByteOffMutMem #-}
moveByteOffToPtrMutMem :: UMArray e s -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
moveByteOffToPtrMutMem UMArray e s
ma = MBytes 'Inc s -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
mw s -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
moveByteOffToPtrMutMem (UMArray e s -> MBytes 'Inc s
forall e s. UMArray e s -> MBytes 'Inc s
fromUMArrayMBytes UMArray e s
ma)
{-# INLINE moveByteOffToPtrMutMem #-}
moveByteOffToMBytesMutMem :: UMArray e s
-> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
moveByteOffToMBytesMutMem UMArray e s
ma = MBytes 'Inc s
-> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
forall (mw :: * -> *) s (m :: * -> *) e (p :: Pinned).
(MemWrite mw, MonadPrim s m, Prim e) =>
mw s -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
moveByteOffToMBytesMutMem (UMArray e s -> MBytes 'Inc s
forall e s. UMArray e s -> MBytes 'Inc s
fromUMArrayMBytes UMArray e s
ma)
{-# INLINE moveByteOffToMBytesMutMem #-}
copyByteOffMem :: mr -> Off Word8 -> UMArray e s -> Off Word8 -> Count e -> m ()
copyByteOffMem mr
src Off Word8
srcOff UMArray e s
ma = mr -> Off Word8 -> MBytes 'Inc s -> Off Word8 -> Count e -> m ()
forall (mw :: * -> *) s (m :: * -> *) mr e.
(MemWrite mw, MonadPrim s m, MemRead mr, Prim e) =>
mr -> Off Word8 -> mw s -> Off Word8 -> Count e -> m ()
copyByteOffMem mr
src Off Word8
srcOff (UMArray e s -> MBytes 'Inc s
forall e s. UMArray e s -> MBytes 'Inc s
fromUMArrayMBytes UMArray e s
ma)
{-# INLINE copyByteOffMem #-}
moveByteOffMutMem :: mw' s -> Off Word8 -> UMArray e s -> Off Word8 -> Count e -> m ()
moveByteOffMutMem mw' s
src Off Word8
srcOff UMArray e s
ma = mw' s -> Off Word8 -> MBytes 'Inc s -> Off Word8 -> Count e -> m ()
forall (mw :: * -> *) s (m :: * -> *) (mw' :: * -> *) e.
(MemWrite mw, MonadPrim s m, MemWrite mw', Prim e) =>
mw' s -> Off Word8 -> mw s -> Off Word8 -> Count e -> m ()
moveByteOffMutMem mw' s
src Off Word8
srcOff (UMArray e s -> MBytes 'Inc s
forall e s. UMArray e s -> MBytes 'Inc s
fromUMArrayMBytes UMArray e s
ma)
{-# INLINE moveByteOffMutMem #-}
setMutMem :: UMArray e s -> Off e -> Count e -> e -> m ()
setMutMem UMArray e s
ma = MBytes 'Inc s -> Off e -> Count e -> e -> m ()
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
mw s -> Off e -> Count e -> e -> m ()
setMutMem (UMArray e s -> MBytes 'Inc s
forall e s. UMArray e s -> MBytes 'Inc s
fromUMArrayMBytes UMArray e s
ma)
{-# INLINE setMutMem #-}
instance MemAlloc (UMArray e) where
type FrozenMem (UMArray e) = UArray e
getByteCountMutMem :: UMArray e s -> m (Count Word8)
getByteCountMutMem = MBytes 'Inc s -> m (Count Word8)
forall (ma :: * -> *) s (m :: * -> *).
(MemAlloc ma, MonadPrim s m) =>
ma s -> m (Count Word8)
getByteCountMutMem (MBytes 'Inc s -> m (Count Word8))
-> (UMArray e s -> MBytes 'Inc s) -> UMArray e s -> m (Count Word8)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UMArray e s -> MBytes 'Inc s
forall e s. UMArray e s -> MBytes 'Inc s
fromUMArrayMBytes
{-# INLINE getByteCountMutMem #-}
allocMutMem :: Count e -> m (UMArray e s)
allocMutMem = (MBytes 'Inc s -> UMArray e s)
-> m (MBytes 'Inc s) -> m (UMArray e s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap MBytes 'Inc s -> UMArray e s
forall (p :: Pinned) s e. MBytes p s -> UMArray e s
toUMArrayMBytes (m (MBytes 'Inc s) -> m (UMArray e s))
-> (Count e -> m (MBytes 'Inc s)) -> Count e -> m (UMArray e s)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Count e -> m (MBytes 'Inc s)
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
Count e -> m (MBytes 'Inc s)
allocUnpinnedMBytes
{-# INLINE allocMutMem #-}
thawMem :: FrozenMem (UMArray e) -> m (UMArray e s)
thawMem = (MBytes 'Inc s -> UMArray e s)
-> m (MBytes 'Inc s) -> m (UMArray e s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap MBytes 'Inc s -> UMArray e s
forall (p :: Pinned) s e. MBytes p s -> UMArray e s
toUMArrayMBytes (m (MBytes 'Inc s) -> m (UMArray e s))
-> (UArray e -> m (MBytes 'Inc s)) -> UArray e -> m (UMArray e s)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bytes 'Inc -> m (MBytes 'Inc s)
forall s (m :: * -> *) (p :: Pinned).
MonadPrim s m =>
Bytes p -> m (MBytes p s)
thawBytes (Bytes 'Inc -> m (MBytes 'Inc s))
-> (UArray e -> Bytes 'Inc) -> UArray e -> m (MBytes 'Inc s)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UArray e -> Bytes 'Inc
forall e. UArray e -> Bytes 'Inc
fromUArrayBytes
{-# INLINE thawMem #-}
freezeMutMem :: UMArray e s -> m (FrozenMem (UMArray e))
freezeMutMem = (Bytes 'Inc -> UArray e) -> m (Bytes 'Inc) -> m (UArray e)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Bytes 'Inc -> UArray e
forall (p :: Pinned) e. Bytes p -> UArray e
toUArrayBytes (m (Bytes 'Inc) -> m (UArray e))
-> (UMArray e s -> m (Bytes 'Inc)) -> UMArray e s -> m (UArray e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MBytes 'Inc s -> m (Bytes 'Inc)
forall s (m :: * -> *) (p :: Pinned).
MonadPrim s m =>
MBytes p s -> m (Bytes p)
freezeMBytes (MBytes 'Inc s -> m (Bytes 'Inc))
-> (UMArray e s -> MBytes 'Inc s) -> UMArray e s -> m (Bytes 'Inc)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UMArray e s -> MBytes 'Inc s
forall e s. UMArray e s -> MBytes 'Inc s
fromUMArrayMBytes
{-# INLINE freezeMutMem #-}
reallocMutMem :: UMArray e s -> Count e -> m (UMArray e s)
reallocMutMem UMArray e s
ma = (MBytes 'Inc s -> UMArray e s)
-> m (MBytes 'Inc s) -> m (UMArray e s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap MBytes 'Inc s -> UMArray e s
forall (p :: Pinned) s e. MBytes p s -> UMArray e s
toUMArrayMBytes (m (MBytes 'Inc s) -> m (UMArray e s))
-> (Count e -> m (MBytes 'Inc s)) -> Count e -> m (UMArray e s)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MBytes 'Inc s -> Count e -> m (MBytes 'Inc s)
forall e (p :: Pinned) (m :: * -> *) s.
(MonadPrim s m, Typeable p, Prim e) =>
MBytes p s -> Count e -> m (MBytes p s)
reallocMBytes (UMArray e s -> MBytes 'Inc s
forall e s. UMArray e s -> MBytes 'Inc s
fromUMArrayMBytes UMArray e s
ma)
{-# INLINE reallocMutMem #-}
instance MemRead ByteString where
isSameMem :: ByteString -> ByteString -> Bool
isSameMem ByteString
bs1 ByteString
bs2 =
IO Bool -> Bool
forall a. IO a -> a
unsafeInlineIO (IO Bool -> Bool) -> IO Bool -> Bool
forall a b. (a -> b) -> a -> b
$
ByteString -> (Ptr Word8 -> IO Bool) -> IO Bool
forall s p (m :: * -> *) a b.
(PtrAccess s p, MonadPrim s m) =>
p -> (Ptr a -> m b) -> m b
withPtrAccess ByteString
bs1 ((Ptr Word8 -> IO Bool) -> IO Bool)
-> (Ptr Word8 -> IO Bool) -> IO Bool
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
ptr1 ->
ByteString -> (Ptr Word8 -> IO Bool) -> IO Bool
forall s p (m :: * -> *) a b.
(PtrAccess s p, MonadPrim s m) =>
p -> (Ptr a -> m b) -> m b
withPtrAccess ByteString
bs2 ((Ptr Word8 -> IO Bool) -> IO Bool)
-> (Ptr Word8 -> IO Bool) -> IO Bool
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
ptr2 ->
Bool -> IO Bool
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Ptr Word8
ptr1 Ptr Word8 -> Ptr Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== (Ptr Word8
ptr2 :: Ptr Word8) Bool -> Bool -> Bool
&& ByteString -> Int
BS.length ByteString
bs1 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString -> Int
BS.length ByteString
bs2)
{-# INLINE isSameMem #-}
byteCountMem :: ByteString -> Count Word8
byteCountMem = Int -> Count Word8
forall e. Int -> Count e
Count (Int -> Count Word8)
-> (ByteString -> Int) -> ByteString -> Count Word8
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Int
BS.length
{-# INLINE byteCountMem #-}
indexOffMem :: ByteString -> Off e -> e
indexOffMem ByteString
bs Off e
i = IO e -> e
forall a. IO a -> a
unsafeInlineIO (IO e -> e) -> IO e -> e
forall a b. (a -> b) -> a -> b
$ ByteString -> (Ptr e -> IO e) -> IO e
forall s p (m :: * -> *) a b.
(PtrAccess s p, MonadPrim s m) =>
p -> (Ptr a -> m b) -> m b
withPtrAccess ByteString
bs (Ptr e -> Off e -> IO e
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
Ptr e -> Off e -> m e
`readOffPtr` Off e
i)
{-# INLINE indexOffMem #-}
indexByteOffMem :: ByteString -> Off Word8 -> e
indexByteOffMem ByteString
bs Off Word8
i = IO e -> e
forall a. IO a -> a
unsafeInlineIO (IO e -> e) -> IO e -> e
forall a b. (a -> b) -> a -> b
$ ByteString -> (Ptr e -> IO e) -> IO e
forall s p (m :: * -> *) a b.
(PtrAccess s p, MonadPrim s m) =>
p -> (Ptr a -> m b) -> m b
withPtrAccess ByteString
bs (Ptr e -> Off Word8 -> IO e
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
Ptr e -> Off Word8 -> m e
`readByteOffPtr` Off Word8
i)
{-# INLINE indexByteOffMem #-}
copyByteOffToMBytesMem :: ByteString
-> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
copyByteOffToMBytesMem ByteString
bs Off Word8
srcOff MBytes p s
mb Off Word8
dstOff Count e
c =
ByteString -> (Ptr e -> m ()) -> m ()
forall s p (m :: * -> *) a b.
(PtrAccess s p, MonadPrim s m) =>
p -> (Ptr a -> m b) -> m b
withPtrAccess ByteString
bs ((Ptr e -> m ()) -> m ()) -> (Ptr e -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Ptr e
srcPtr ->
Ptr e -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
forall s (m :: * -> *) e (p :: Pinned).
(MonadPrim s m, Prim e) =>
Ptr e -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
copyByteOffPtrToMBytes Ptr e
srcPtr Off Word8
srcOff MBytes p s
mb Off Word8
dstOff Count e
c
{-# INLINE copyByteOffToMBytesMem #-}
copyByteOffToPtrMem :: ByteString -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
copyByteOffToPtrMem ByteString
bs Off Word8
srcOff Ptr e
dstPtr Off Word8
dstOff Count e
c =
ByteString -> (Ptr e -> m ()) -> m ()
forall s p (m :: * -> *) a b.
(PtrAccess s p, MonadPrim s m) =>
p -> (Ptr a -> m b) -> m b
withPtrAccess ByteString
bs ((Ptr e -> m ()) -> m ()) -> (Ptr e -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Ptr e
srcPtr ->
Ptr e -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
Ptr e -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
copyByteOffPtrToPtr Ptr e
srcPtr Off Word8
srcOff Ptr e
dstPtr Off Word8
dstOff Count e
c
{-# INLINE copyByteOffToPtrMem #-}
compareByteOffToPtrMem :: ByteString
-> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m Ordering
compareByteOffToPtrMem ByteString
bs Off Word8
off1 Ptr e
ptr2 Off Word8
off2 Count e
c =
ByteString -> (Ptr e -> m Ordering) -> m Ordering
forall s p (m :: * -> *) a b.
(PtrAccess s p, MonadPrim s m) =>
p -> (Ptr a -> m b) -> m b
withPtrAccess ByteString
bs ((Ptr e -> m Ordering) -> m Ordering)
-> (Ptr e -> m Ordering) -> m Ordering
forall a b. (a -> b) -> a -> b
$ \Ptr e
ptr1 ->
Ordering -> m Ordering
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Ordering -> m Ordering) -> Ordering -> m Ordering
forall a b. (a -> b) -> a -> b
$! Ptr e -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> Ordering
forall e.
Prim e =>
Ptr e -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> Ordering
compareByteOffPtrToPtr Ptr e
ptr1 Off Word8
off1 Ptr e
ptr2 Off Word8
off2 Count e
c
{-# INLINE compareByteOffToPtrMem #-}
compareByteOffToBytesMem :: ByteString
-> Off Word8 -> Bytes p -> Off Word8 -> Count e -> Ordering
compareByteOffToBytesMem ByteString
bs Off Word8
off1 Bytes p
bytes Off Word8
off2 Count e
c =
IO Ordering -> Ordering
forall a. IO a -> a
unsafeInlineIO (IO Ordering -> Ordering) -> IO Ordering -> Ordering
forall a b. (a -> b) -> a -> b
$
ByteString -> (Ptr e -> IO Ordering) -> IO Ordering
forall s p (m :: * -> *) a b.
(PtrAccess s p, MonadPrim s m) =>
p -> (Ptr a -> m b) -> m b
withPtrAccess ByteString
bs ((Ptr e -> IO Ordering) -> IO Ordering)
-> (Ptr e -> IO Ordering) -> IO Ordering
forall a b. (a -> b) -> a -> b
$ \Ptr e
ptr1 ->
Ordering -> IO Ordering
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Ordering -> IO Ordering) -> Ordering -> IO Ordering
forall a b. (a -> b) -> a -> b
$! Ptr e -> Off Word8 -> Bytes p -> Off Word8 -> Count e -> Ordering
forall e (p :: Pinned).
Prim e =>
Ptr e -> Off Word8 -> Bytes p -> Off Word8 -> Count e -> Ordering
compareByteOffPtrToBytes Ptr e
ptr1 Off Word8
off1 Bytes p
bytes Off Word8
off2 Count e
c
{-# INLINE compareByteOffToBytesMem #-}
compareByteOffMem :: mr' -> Off Word8 -> ByteString -> Off Word8 -> Count e -> Ordering
compareByteOffMem mr'
mem1 Off Word8
off1 ByteString
bs Off Word8
off2 Count e
c =
IO Ordering -> Ordering
forall a. IO a -> a
unsafeInlineIO (IO Ordering -> Ordering) -> IO Ordering -> Ordering
forall a b. (a -> b) -> a -> b
$
ByteString -> (Ptr e -> IO Ordering) -> IO Ordering
forall s p (m :: * -> *) a b.
(PtrAccess s p, MonadPrim s m) =>
p -> (Ptr a -> m b) -> m b
withPtrAccess ByteString
bs ((Ptr e -> IO Ordering) -> IO Ordering)
-> (Ptr e -> IO Ordering) -> IO Ordering
forall a b. (a -> b) -> a -> b
$ \Ptr e
ptr2 -> mr' -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> IO Ordering
forall mr s (m :: * -> *) e.
(MemRead mr, MonadPrim s m, Prim e) =>
mr -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m Ordering
compareByteOffToPtrMem mr'
mem1 Off Word8
off1 Ptr e
ptr2 Off Word8
off2 Count e
c
{-# INLINE compareByteOffMem #-}
instance MemWrite MByteString where
isSameMutMem :: MByteString s -> MByteString s -> Bool
isSameMutMem (MByteString ByteString
bs1) (MByteString ByteString
bs2) = ByteString -> ByteString -> Bool
forall mr. MemRead mr => mr -> mr -> Bool
isSameMem ByteString
bs1 ByteString
bs2
{-# INLINE isSameMutMem #-}
readOffMutMem :: MByteString s -> Off e -> m e
readOffMutMem (MByteString ByteString
mbs) Off e
i = ByteString -> (Ptr e -> m e) -> m e
forall s p (m :: * -> *) a b.
(PtrAccess s p, MonadPrim s m) =>
p -> (Ptr a -> m b) -> m b
withPtrAccess ByteString
mbs (Ptr e -> Off e -> m e
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
Ptr e -> Off e -> m e
`readOffPtr` Off e
i)
{-# INLINE readOffMutMem #-}
readByteOffMutMem :: MByteString s -> Off Word8 -> m e
readByteOffMutMem (MByteString ByteString
mbs) Off Word8
i = ByteString -> (Ptr e -> m e) -> m e
forall s p (m :: * -> *) a b.
(PtrAccess s p, MonadPrim s m) =>
p -> (Ptr a -> m b) -> m b
withPtrAccess ByteString
mbs (Ptr e -> Off Word8 -> m e
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
Ptr e -> Off Word8 -> m e
`readByteOffPtr` Off Word8
i)
{-# INLINE readByteOffMutMem #-}
writeOffMutMem :: MByteString s -> Off e -> e -> m ()
writeOffMutMem (MByteString ByteString
mbs) Off e
i e
a = ByteString -> (Ptr e -> m ()) -> m ()
forall s p (m :: * -> *) a b.
(PtrAccess s p, MonadPrim s m) =>
p -> (Ptr a -> m b) -> m b
withPtrAccess ByteString
mbs ((Ptr e -> m ()) -> m ()) -> (Ptr e -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Ptr e
ptr -> Ptr e -> Off e -> e -> m ()
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
Ptr e -> Off e -> e -> m ()
writeOffPtr Ptr e
ptr Off e
i e
a
{-# INLINE writeOffMutMem #-}
writeByteOffMutMem :: MByteString s -> Off Word8 -> e -> m ()
writeByteOffMutMem (MByteString ByteString
mbs) Off Word8
i e
a = ByteString -> (Ptr e -> m ()) -> m ()
forall s p (m :: * -> *) a b.
(PtrAccess s p, MonadPrim s m) =>
p -> (Ptr a -> m b) -> m b
withPtrAccess ByteString
mbs ((Ptr e -> m ()) -> m ()) -> (Ptr e -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Ptr e
ptr -> Ptr e -> Off Word8 -> e -> m ()
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
Ptr e -> Off Word8 -> e -> m ()
writeByteOffPtr Ptr e
ptr Off Word8
i e
a
{-# INLINE writeByteOffMutMem #-}
moveByteOffToPtrMutMem :: MByteString s -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
moveByteOffToPtrMutMem (MByteString ByteString
fsrc) Off Word8
srcOff Ptr e
dstPtr Off Word8
dstOff Count e
c =
ByteString -> (Ptr e -> m ()) -> m ()
forall s p (m :: * -> *) a b.
(PtrAccess s p, MonadPrim s m) =>
p -> (Ptr a -> m b) -> m b
withPtrAccess ByteString
fsrc ((Ptr e -> m ()) -> m ()) -> (Ptr e -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Ptr e
srcPtr -> Ptr e -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
Ptr e -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
moveByteOffPtrToPtr Ptr e
srcPtr Off Word8
srcOff Ptr e
dstPtr Off Word8
dstOff Count e
c
{-# INLINE moveByteOffToPtrMutMem #-}
moveByteOffToMBytesMutMem :: MByteString s
-> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
moveByteOffToMBytesMutMem (MByteString ByteString
fsrc) Off Word8
srcOff MBytes p s
dst Off Word8
dstOff Count e
c =
ByteString -> (Ptr e -> m ()) -> m ()
forall s p (m :: * -> *) a b.
(PtrAccess s p, MonadPrim s m) =>
p -> (Ptr a -> m b) -> m b
withPtrAccess ByteString
fsrc ((Ptr e -> m ()) -> m ()) -> (Ptr e -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Ptr e
srcPtr -> Ptr e -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
forall s (m :: * -> *) e (p :: Pinned).
(MonadPrim s m, Prim e) =>
Ptr e -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
moveByteOffPtrToMBytes Ptr e
srcPtr Off Word8
srcOff MBytes p s
dst Off Word8
dstOff Count e
c
{-# INLINE moveByteOffToMBytesMutMem #-}
copyByteOffMem :: mr -> Off Word8 -> MByteString s -> Off Word8 -> Count e -> m ()
copyByteOffMem mr
src Off Word8
srcOff (MByteString ByteString
fdst) Off Word8
dstOff Count e
c =
ByteString -> (Ptr e -> m ()) -> m ()
forall s p (m :: * -> *) a b.
(PtrAccess s p, MonadPrim s m) =>
p -> (Ptr a -> m b) -> m b
withPtrAccess ByteString
fdst ((Ptr e -> m ()) -> m ()) -> (Ptr e -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Ptr e
dstPtr -> mr -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
forall mr s (m :: * -> *) e.
(MemRead mr, MonadPrim s m, Prim e) =>
mr -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
copyByteOffToPtrMem mr
src Off Word8
srcOff Ptr e
dstPtr Off Word8
dstOff Count e
c
{-# INLINE copyByteOffMem #-}
moveByteOffMutMem :: mw' s -> Off Word8 -> MByteString s -> Off Word8 -> Count e -> m ()
moveByteOffMutMem mw' s
src Off Word8
srcOff (MByteString ByteString
fdst) Off Word8
dstOff Count e
c =
ByteString -> (Ptr e -> m ()) -> m ()
forall s p (m :: * -> *) a b.
(PtrAccess s p, MonadPrim s m) =>
p -> (Ptr a -> m b) -> m b
withPtrAccess ByteString
fdst ((Ptr e -> m ()) -> m ()) -> (Ptr e -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Ptr e
dstPtr -> mw' s -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
mw s -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
moveByteOffToPtrMutMem mw' s
src Off Word8
srcOff Ptr e
dstPtr Off Word8
dstOff Count e
c
{-# INLINE moveByteOffMutMem #-}
setMutMem :: MByteString s -> Off e -> Count e -> e -> m ()
setMutMem (MByteString ByteString
mbs) Off e
off Count e
c e
a = ByteString -> (Ptr e -> m ()) -> m ()
forall s p (m :: * -> *) a b.
(PtrAccess s p, MonadPrim s m) =>
p -> (Ptr a -> m b) -> m b
withPtrAccess ByteString
mbs ((Ptr e -> m ()) -> m ()) -> (Ptr e -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Ptr e
ptr -> Ptr e -> Off e -> Count e -> e -> m ()
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
Ptr e -> Off e -> Count e -> e -> m ()
setOffPtr Ptr e
ptr Off e
off Count e
c e
a
{-# INLINE setMutMem #-}
instance MemAlloc MByteString where
type FrozenMem MByteString = ByteString
getByteCountMutMem :: MByteString s -> m (Count Word8)
getByteCountMutMem (MByteString ByteString
bs) = Count Word8 -> m (Count Word8)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Count Word8 -> m (Count Word8)) -> Count Word8 -> m (Count Word8)
forall a b. (a -> b) -> a -> b
$! Int -> Count Word8
forall e. Int -> Count e
Count (ByteString -> Int
BS.length ByteString
bs)
{-# INLINE getByteCountMutMem #-}
allocMutMem :: Count e -> m (MByteString s)
allocMutMem Count e
c = do
let cb :: Count Word8
cb = Count e -> Count Word8
forall e. Prim e => Count e -> Count Word8
toByteCount Count e
c
ForeignPtr Word8
fp <- Count Word8 -> m (ForeignPtr Word8)
forall s (m :: * -> *) e.
MonadPrim s m =>
Count Word8 -> m (ForeignPtr e)
mallocByteCountPlainForeignPtr Count Word8
cb
MByteString s -> m (MByteString s)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (MByteString s -> m (MByteString s))
-> MByteString s -> m (MByteString s)
forall a b. (a -> b) -> a -> b
$ ByteString -> MByteString s
forall s. ByteString -> MByteString s
MByteString (ForeignPtr Word8 -> Int -> Int -> ByteString
PS ForeignPtr Word8
fp Int
0 (Count Word8 -> Int
coerce Count Word8
cb))
{-# INLINE allocMutMem #-}
thawMem :: FrozenMem MByteString -> m (MByteString s)
thawMem FrozenMem MByteString
bs = MByteString s -> m (MByteString s)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (MByteString s -> m (MByteString s))
-> MByteString s -> m (MByteString s)
forall a b. (a -> b) -> a -> b
$ ByteString -> MByteString s
forall s. ByteString -> MByteString s
MByteString ByteString
FrozenMem MByteString
bs
{-# INLINE thawMem #-}
freezeMutMem :: MByteString s -> m (FrozenMem MByteString)
freezeMutMem (MByteString ByteString
bs) = ByteString -> m ByteString
forall (f :: * -> *) a. Applicative f => a -> f a
pure ByteString
bs
{-# INLINE freezeMutMem #-}
reallocMutMem :: MByteString s -> Count e -> m (MByteString s)
reallocMutMem bsm :: MByteString s
bsm@(MByteString (PS ForeignPtr Word8
fp Int
o Int
n)) Count e
newc
| Int
newn Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
n = MByteString s -> Count e -> m (MByteString s)
forall e (ma :: * -> *) s (m :: * -> *).
(Prim e, MemAlloc ma, MonadPrim s m) =>
ma s -> Count e -> m (ma s)
defaultReallocMutMem MByteString s
bsm Count e
newc
| Bool
otherwise = MByteString s -> m (MByteString s)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (MByteString s -> m (MByteString s))
-> MByteString s -> m (MByteString s)
forall a b. (a -> b) -> a -> b
$ ByteString -> MByteString s
forall s. ByteString -> MByteString s
MByteString (ForeignPtr Word8 -> Int -> Int -> ByteString
PS ForeignPtr Word8
fp Int
o Int
newn)
where
Count Int
newn = Count e -> Count Word8
forall e. Prim e => Count e -> Count Word8
toByteCount Count e
newc
{-# INLINE reallocMutMem #-}
instance MemRead T.Array where
isSameMem :: Array -> Array -> Bool
isSameMem Array
a1 Array
a2 = Bytes 'Inc -> Bytes 'Inc -> Bool
forall mr. MemRead mr => mr -> mr -> Bool
isSameMem (Array -> Bytes 'Inc
T.fromArrayBytes Array
a1) (Array -> Bytes 'Inc
T.fromArrayBytes Array
a2)
{-# INLINE isSameMem #-}
byteCountMem :: Array -> Count Word8
byteCountMem = Bytes 'Inc -> Count Word8
forall mr. MemRead mr => mr -> Count Word8
byteCountMem (Bytes 'Inc -> Count Word8)
-> (Array -> Bytes 'Inc) -> Array -> Count Word8
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Array -> Bytes 'Inc
T.fromArrayBytes
{-# INLINE byteCountMem #-}
indexOffMem :: Array -> Off e -> e
indexOffMem Array
a = Bytes 'Inc -> Off e -> e
forall mr e. (MemRead mr, Prim e) => mr -> Off e -> e
indexOffMem (Array -> Bytes 'Inc
T.fromArrayBytes Array
a)
{-# INLINE indexOffMem #-}
indexByteOffMem :: Array -> Off Word8 -> e
indexByteOffMem Array
a = Bytes 'Inc -> Off Word8 -> e
forall mr e. (MemRead mr, Prim e) => mr -> Off Word8 -> e
indexByteOffMem (Array -> Bytes 'Inc
T.fromArrayBytes Array
a)
{-# INLINE indexByteOffMem #-}
copyByteOffToMBytesMem :: Array -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
copyByteOffToMBytesMem Array
a = Bytes 'Inc
-> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
forall mr s (m :: * -> *) e (p :: Pinned).
(MemRead mr, MonadPrim s m, Prim e) =>
mr -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
copyByteOffToMBytesMem (Array -> Bytes 'Inc
T.fromArrayBytes Array
a)
{-# INLINE copyByteOffToMBytesMem #-}
copyByteOffToPtrMem :: Array -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
copyByteOffToPtrMem Array
a = Bytes 'Inc -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
forall mr s (m :: * -> *) e.
(MemRead mr, MonadPrim s m, Prim e) =>
mr -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
copyByteOffToPtrMem (Array -> Bytes 'Inc
T.fromArrayBytes Array
a)
{-# INLINE copyByteOffToPtrMem #-}
compareByteOffToPtrMem :: Array -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m Ordering
compareByteOffToPtrMem Array
a = Bytes 'Inc
-> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m Ordering
forall mr s (m :: * -> *) e.
(MemRead mr, MonadPrim s m, Prim e) =>
mr -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m Ordering
compareByteOffToPtrMem (Array -> Bytes 'Inc
T.fromArrayBytes Array
a)
{-# INLINE compareByteOffToPtrMem #-}
compareByteOffToBytesMem :: Array -> Off Word8 -> Bytes p -> Off Word8 -> Count e -> Ordering
compareByteOffToBytesMem Array
a = Bytes 'Inc
-> Off Word8 -> Bytes p -> Off Word8 -> Count e -> Ordering
forall mr e (p :: Pinned).
(MemRead mr, Prim e) =>
mr -> Off Word8 -> Bytes p -> Off Word8 -> Count e -> Ordering
compareByteOffToBytesMem (Array -> Bytes 'Inc
T.fromArrayBytes Array
a)
{-# INLINE compareByteOffToBytesMem #-}
compareByteOffMem :: mr' -> Off Word8 -> Array -> Off Word8 -> Count e -> Ordering
compareByteOffMem mr'
mem Off Word8
off1 Array
a = mr' -> Off Word8 -> Bytes 'Inc -> Off Word8 -> Count e -> Ordering
forall mr mr' e.
(MemRead mr, MemRead mr', Prim e) =>
mr' -> Off Word8 -> mr -> Off Word8 -> Count e -> Ordering
compareByteOffMem mr'
mem Off Word8
off1 (Array -> Bytes 'Inc
T.fromArrayBytes Array
a)
{-# INLINE compareByteOffMem #-}
instance MemAlloc T.MArray where
type FrozenMem T.MArray = T.Array
getByteCountMutMem :: MArray s -> m (Count Word8)
getByteCountMutMem = MBytes 'Inc s -> m (Count Word8)
forall s (m :: * -> *) (p :: Pinned).
MonadPrim s m =>
MBytes p s -> m (Count Word8)
getByteCountMBytes (MBytes 'Inc s -> m (Count Word8))
-> (MArray s -> MBytes 'Inc s) -> MArray s -> m (Count Word8)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MArray s -> MBytes 'Inc s
forall s. MArray s -> MBytes 'Inc s
T.fromMArrayMBytes
{-# INLINE getByteCountMutMem #-}
allocMutMem :: Count e -> m (MArray s)
allocMutMem = (MBytes 'Inc s -> MArray s) -> m (MBytes 'Inc s) -> m (MArray s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap MBytes 'Inc s -> MArray s
forall (p :: Pinned) s. MBytes p s -> MArray s
T.toMArrayMBytes (m (MBytes 'Inc s) -> m (MArray s))
-> (Count e -> m (MBytes 'Inc s)) -> Count e -> m (MArray s)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Count e -> m (MBytes 'Inc s)
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
Count e -> m (MBytes 'Inc s)
allocUnpinnedMBytes
{-# INLINE allocMutMem #-}
thawMem :: FrozenMem MArray -> m (MArray s)
thawMem = (MBytes 'Inc s -> MArray s) -> m (MBytes 'Inc s) -> m (MArray s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap MBytes 'Inc s -> MArray s
forall (p :: Pinned) s. MBytes p s -> MArray s
T.toMArrayMBytes (m (MBytes 'Inc s) -> m (MArray s))
-> (Array -> m (MBytes 'Inc s)) -> Array -> m (MArray s)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bytes 'Inc -> m (MBytes 'Inc s)
forall s (m :: * -> *) (p :: Pinned).
MonadPrim s m =>
Bytes p -> m (MBytes p s)
thawBytes (Bytes 'Inc -> m (MBytes 'Inc s))
-> (Array -> Bytes 'Inc) -> Array -> m (MBytes 'Inc s)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Array -> Bytes 'Inc
T.fromArrayBytes
{-# INLINE thawMem #-}
freezeMutMem :: MArray s -> m (FrozenMem MArray)
freezeMutMem = (Bytes 'Inc -> Array) -> m (Bytes 'Inc) -> m Array
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Bytes 'Inc -> Array
forall (p :: Pinned). Bytes p -> Array
T.toArrayBytes (m (Bytes 'Inc) -> m Array)
-> (MArray s -> m (Bytes 'Inc)) -> MArray s -> m Array
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MBytes 'Inc s -> m (Bytes 'Inc)
forall s (m :: * -> *) (p :: Pinned).
MonadPrim s m =>
MBytes p s -> m (Bytes p)
freezeMBytes (MBytes 'Inc s -> m (Bytes 'Inc))
-> (MArray s -> MBytes 'Inc s) -> MArray s -> m (Bytes 'Inc)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MArray s -> MBytes 'Inc s
forall s. MArray s -> MBytes 'Inc s
T.fromMArrayMBytes
{-# INLINE freezeMutMem #-}
reallocMutMem :: MArray s -> Count e -> m (MArray s)
reallocMutMem MArray s
m = (MBytes 'Inc s -> MArray s) -> m (MBytes 'Inc s) -> m (MArray s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap MBytes 'Inc s -> MArray s
forall (p :: Pinned) s. MBytes p s -> MArray s
T.toMArrayMBytes (m (MBytes 'Inc s) -> m (MArray s))
-> (Count e -> m (MBytes 'Inc s)) -> Count e -> m (MArray s)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MBytes 'Inc s -> Count e -> m (MBytes 'Inc s)
forall e (p :: Pinned) (m :: * -> *) s.
(MonadPrim s m, Typeable p, Prim e) =>
MBytes p s -> Count e -> m (MBytes p s)
reallocMBytes (MArray s -> MBytes 'Inc s
forall s. MArray s -> MBytes 'Inc s
T.fromMArrayMBytes MArray s
m)
{-# INLINE reallocMutMem #-}
instance MemWrite T.MArray where
isSameMutMem :: MArray s -> MArray s -> Bool
isSameMutMem MArray s
ma1 MArray s
ma2 = MBytes 'Inc s -> MBytes 'Inc s -> Bool
forall (mw :: * -> *) s. MemWrite mw => mw s -> mw s -> Bool
isSameMutMem (MArray s -> MBytes 'Inc s
forall s. MArray s -> MBytes 'Inc s
T.fromMArrayMBytes MArray s
ma1) (MArray s -> MBytes 'Inc s
forall s. MArray s -> MBytes 'Inc s
T.fromMArrayMBytes MArray s
ma2)
{-# INLINE isSameMutMem #-}
readOffMutMem :: MArray s -> Off e -> m e
readOffMutMem MArray s
m = MBytes 'Inc s -> Off e -> m e
forall s (m :: * -> *) e (p :: Pinned).
(MonadPrim s m, Prim e) =>
MBytes p s -> Off e -> m e
readOffMBytes (MArray s -> MBytes 'Inc s
forall s. MArray s -> MBytes 'Inc s
T.fromMArrayMBytes MArray s
m)
{-# INLINE readOffMutMem #-}
readByteOffMutMem :: MArray s -> Off Word8 -> m e
readByteOffMutMem MArray s
m = MBytes 'Inc s -> Off Word8 -> m e
forall s (m :: * -> *) e (p :: Pinned).
(MonadPrim s m, Prim e) =>
MBytes p s -> Off Word8 -> m e
readByteOffMBytes (MArray s -> MBytes 'Inc s
forall s. MArray s -> MBytes 'Inc s
T.fromMArrayMBytes MArray s
m)
{-# INLINE readByteOffMutMem #-}
writeOffMutMem :: MArray s -> Off e -> e -> m ()
writeOffMutMem MArray s
m = MBytes 'Inc s -> Off e -> e -> m ()
forall s (m :: * -> *) e (p :: Pinned).
(MonadPrim s m, Prim e) =>
MBytes p s -> Off e -> e -> m ()
writeOffMBytes (MArray s -> MBytes 'Inc s
forall s. MArray s -> MBytes 'Inc s
T.fromMArrayMBytes MArray s
m)
{-# INLINE writeOffMutMem #-}
writeByteOffMutMem :: MArray s -> Off Word8 -> e -> m ()
writeByteOffMutMem MArray s
m = MBytes 'Inc s -> Off Word8 -> e -> m ()
forall s (m :: * -> *) e (p :: Pinned).
(MonadPrim s m, Prim e) =>
MBytes p s -> Off Word8 -> e -> m ()
writeByteOffMBytes (MArray s -> MBytes 'Inc s
forall s. MArray s -> MBytes 'Inc s
T.fromMArrayMBytes MArray s
m)
{-# INLINE writeByteOffMutMem #-}
moveByteOffToPtrMutMem :: MArray s -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
moveByteOffToPtrMutMem MArray s
m = MBytes 'Inc s -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
forall s (m :: * -> *) e (p :: Pinned).
(MonadPrim s m, Prim e) =>
MBytes p s -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
moveByteOffMBytesToPtr (MArray s -> MBytes 'Inc s
forall s. MArray s -> MBytes 'Inc s
T.fromMArrayMBytes MArray s
m)
{-# INLINE moveByteOffToPtrMutMem #-}
moveByteOffToMBytesMutMem :: MArray s -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
moveByteOffToMBytesMutMem MArray s
m = MBytes 'Inc s
-> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
forall s (m :: * -> *) e (ps :: Pinned) (pd :: Pinned).
(MonadPrim s m, Prim e) =>
MBytes ps s
-> Off Word8 -> MBytes pd s -> Off Word8 -> Count e -> m ()
moveByteOffMBytesToMBytes (MArray s -> MBytes 'Inc s
forall s. MArray s -> MBytes 'Inc s
T.fromMArrayMBytes MArray s
m)
{-# INLINE moveByteOffToMBytesMutMem #-}
moveByteOffMutMem :: mw' s -> Off Word8 -> MArray s -> Off Word8 -> Count e -> m ()
moveByteOffMutMem mw' s
src Off Word8
srcOff MArray s
m = mw' s -> Off Word8 -> MBytes 'Inc s -> Off Word8 -> Count e -> m ()
forall (mw :: * -> *) s (m :: * -> *) e (p :: Pinned).
(MemWrite mw, MonadPrim s m, Prim e) =>
mw s -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
moveByteOffToMBytesMutMem mw' s
src Off Word8
srcOff (MArray s -> MBytes 'Inc s
forall s. MArray s -> MBytes 'Inc s
T.fromMArrayMBytes MArray s
m)
{-# INLINE moveByteOffMutMem #-}
copyByteOffMem :: mr -> Off Word8 -> MArray s -> Off Word8 -> Count e -> m ()
copyByteOffMem mr
src Off Word8
srcOff MArray s
m = mr -> Off Word8 -> MBytes 'Inc s -> Off Word8 -> Count e -> m ()
forall mr s (m :: * -> *) e (p :: Pinned).
(MemRead mr, MonadPrim s m, Prim e) =>
mr -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
copyByteOffToMBytesMem mr
src Off Word8
srcOff (MArray s -> MBytes 'Inc s
forall s. MArray s -> MBytes 'Inc s
T.fromMArrayMBytes MArray s
m)
{-# INLINE copyByteOffMem #-}
setMutMem :: MArray s -> Off e -> Count e -> e -> m ()
setMutMem MArray s
m = MBytes 'Inc s -> Off e -> Count e -> e -> m ()
forall s (m :: * -> *) e (p :: Pinned).
(MonadPrim s m, Prim e) =>
MBytes p s -> Off e -> Count e -> e -> m ()
setMBytes (MArray s -> MBytes 'Inc s
forall s. MArray s -> MBytes 'Inc s
T.fromMArrayMBytes MArray s
m)
{-# INLINE setMutMem #-}
instance MemRead T.Text where
isSameMem :: Text -> Text -> Bool
isSameMem (T.Text Array
a1 Int
o1 Int
n1) (T.Text Array
a2 Int
o2 Int
n2) = Array -> Array -> Bool
forall mr. MemRead mr => mr -> mr -> Bool
isSameMem Array
a1 Array
a2 Bool -> Bool -> Bool
&& Int
o1 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
o2 Bool -> Bool -> Bool
&& Int
n1 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
n2
{-# INLINE isSameMem #-}
byteCountMem :: Text -> Count Word8
byteCountMem (T.Text Array
_ Int
_ Int
n) = Count Word16 -> Count Word8
forall e. Prim e => Count e -> Count Word8
toByteCount (Int -> Count Word16
forall e. Int -> Count e
Count Int
n :: Count Word16)
{-# INLINE byteCountMem #-}
indexByteOffMem :: Text -> Off Word8 -> e
indexByteOffMem (T.Text Array
a Int
o Int
_) Off Word8
i = Array -> Off Word8 -> e
forall mr e. (MemRead mr, Prim e) => mr -> Off Word8 -> e
indexByteOffMem Array
a (Off Word16 -> Off Word8
forall e. Prim e => Off e -> Off Word8
toByteOff (Int -> Off Word16
forall e. Int -> Off e
Off Int
o :: Off Word16) Off Word8 -> Off Word8 -> Off Word8
forall a. Num a => a -> a -> a
+ Off Word8
i)
{-# INLINE indexByteOffMem #-}
copyByteOffToMBytesMem :: Text -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
copyByteOffToMBytesMem (T.Text Array
a Int
o Int
_) Off Word8
i =
Array -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
forall mr s (m :: * -> *) e (p :: Pinned).
(MemRead mr, MonadPrim s m, Prim e) =>
mr -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
copyByteOffToMBytesMem Array
a (Off Word16 -> Off Word8
forall e. Prim e => Off e -> Off Word8
toByteOff (Int -> Off Word16
forall e. Int -> Off e
Off Int
o :: Off Word16) Off Word8 -> Off Word8 -> Off Word8
forall a. Num a => a -> a -> a
+ Off Word8
i)
{-# INLINE copyByteOffToMBytesMem #-}
copyByteOffToPtrMem :: Text -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
copyByteOffToPtrMem (T.Text Array
a Int
o Int
_) Off Word8
i =
Array -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
forall mr s (m :: * -> *) e.
(MemRead mr, MonadPrim s m, Prim e) =>
mr -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
copyByteOffToPtrMem Array
a (Off Word16 -> Off Word8
forall e. Prim e => Off e -> Off Word8
toByteOff (Int -> Off Word16
forall e. Int -> Off e
Off Int
o :: Off Word16) Off Word8 -> Off Word8 -> Off Word8
forall a. Num a => a -> a -> a
+ Off Word8
i)
{-# INLINE copyByteOffToPtrMem #-}
compareByteOffToPtrMem :: Text -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m Ordering
compareByteOffToPtrMem (T.Text Array
a Int
o Int
_) Off Word8
off =
Array -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m Ordering
forall mr s (m :: * -> *) e.
(MemRead mr, MonadPrim s m, Prim e) =>
mr -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m Ordering
compareByteOffToPtrMem Array
a (Off Word16 -> Off Word8
forall e. Prim e => Off e -> Off Word8
toByteOff (Int -> Off Word16
forall e. Int -> Off e
Off Int
o :: Off Word16) Off Word8 -> Off Word8 -> Off Word8
forall a. Num a => a -> a -> a
+ Off Word8
off)
{-# INLINE compareByteOffToPtrMem #-}
compareByteOffToBytesMem :: Text -> Off Word8 -> Bytes p -> Off Word8 -> Count e -> Ordering
compareByteOffToBytesMem (T.Text Array
a Int
o Int
_) Off Word8
off =
Array -> Off Word8 -> Bytes p -> Off Word8 -> Count e -> Ordering
forall mr e (p :: Pinned).
(MemRead mr, Prim e) =>
mr -> Off Word8 -> Bytes p -> Off Word8 -> Count e -> Ordering
compareByteOffToBytesMem Array
a (Off Word16 -> Off Word8
forall e. Prim e => Off e -> Off Word8
toByteOff (Int -> Off Word16
forall e. Int -> Off e
Off Int
o :: Off Word16) Off Word8 -> Off Word8 -> Off Word8
forall a. Num a => a -> a -> a
+ Off Word8
off)
{-# INLINE compareByteOffToBytesMem #-}
compareByteOffMem :: mr' -> Off Word8 -> Text -> Off Word8 -> Count e -> Ordering
compareByteOffMem mr'
mem Off Word8
off1 (T.Text Array
a Int
o Int
_) Off Word8
off2 =
mr' -> Off Word8 -> Array -> Off Word8 -> Count e -> Ordering
forall mr mr' e.
(MemRead mr, MemRead mr', Prim e) =>
mr' -> Off Word8 -> mr -> Off Word8 -> Count e -> Ordering
compareByteOffMem mr'
mem Off Word8
off1 Array
a (Off Word16 -> Off Word8
forall e. Prim e => Off e -> Off Word8
toByteOff (Int -> Off Word16
forall e. Int -> Off e
Off Int
o :: Off Word16) Off Word8 -> Off Word8 -> Off Word8
forall a. Num a => a -> a -> a
+ Off Word8
off2)
{-# INLINE compareByteOffMem #-}
instance MemRead ShortByteString where
isSameMem :: ShortByteString -> ShortByteString -> Bool
isSameMem ShortByteString
sbs1 ShortByteString
sbs2 = Bytes 'Inc -> Bytes 'Inc -> Bool
forall mr. MemRead mr => mr -> mr -> Bool
isSameMem (ShortByteString -> Bytes 'Inc
fromShortByteStringBytes ShortByteString
sbs1) (ShortByteString -> Bytes 'Inc
fromShortByteStringBytes ShortByteString
sbs2)
{-# INLINE isSameMem #-}
byteCountMem :: ShortByteString -> Count Word8
byteCountMem = Bytes 'Inc -> Count Word8
forall mr. MemRead mr => mr -> Count Word8
byteCountMem (Bytes 'Inc -> Count Word8)
-> (ShortByteString -> Bytes 'Inc)
-> ShortByteString
-> Count Word8
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShortByteString -> Bytes 'Inc
fromShortByteStringBytes
{-# INLINE byteCountMem #-}
indexOffMem :: ShortByteString -> Off e -> e
indexOffMem ShortByteString
sbs = Bytes 'Inc -> Off e -> e
forall mr e. (MemRead mr, Prim e) => mr -> Off e -> e
indexOffMem (ShortByteString -> Bytes 'Inc
fromShortByteStringBytes ShortByteString
sbs)
{-# INLINE indexOffMem #-}
indexByteOffMem :: ShortByteString -> Off Word8 -> e
indexByteOffMem ShortByteString
sbs = Bytes 'Inc -> Off Word8 -> e
forall mr e. (MemRead mr, Prim e) => mr -> Off Word8 -> e
indexByteOffMem (ShortByteString -> Bytes 'Inc
fromShortByteStringBytes ShortByteString
sbs)
{-# INLINE indexByteOffMem #-}
copyByteOffToMBytesMem :: ShortByteString
-> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
copyByteOffToMBytesMem ShortByteString
sbs = Bytes 'Inc
-> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
forall mr s (m :: * -> *) e (p :: Pinned).
(MemRead mr, MonadPrim s m, Prim e) =>
mr -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
copyByteOffToMBytesMem (ShortByteString -> Bytes 'Inc
fromShortByteStringBytes ShortByteString
sbs)
{-# INLINE copyByteOffToMBytesMem #-}
copyByteOffToPtrMem :: ShortByteString
-> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
copyByteOffToPtrMem ShortByteString
sbs = Bytes 'Inc -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
forall mr s (m :: * -> *) e.
(MemRead mr, MonadPrim s m, Prim e) =>
mr -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
copyByteOffToPtrMem (ShortByteString -> Bytes 'Inc
fromShortByteStringBytes ShortByteString
sbs)
{-# INLINE copyByteOffToPtrMem #-}
compareByteOffToPtrMem :: ShortByteString
-> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m Ordering
compareByteOffToPtrMem ShortByteString
sbs = Bytes 'Inc
-> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m Ordering
forall mr s (m :: * -> *) e.
(MemRead mr, MonadPrim s m, Prim e) =>
mr -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m Ordering
compareByteOffToPtrMem (ShortByteString -> Bytes 'Inc
fromShortByteStringBytes ShortByteString
sbs)
{-# INLINE compareByteOffToPtrMem #-}
compareByteOffToBytesMem :: ShortByteString
-> Off Word8 -> Bytes p -> Off Word8 -> Count e -> Ordering
compareByteOffToBytesMem ShortByteString
sbs = Bytes 'Inc
-> Off Word8 -> Bytes p -> Off Word8 -> Count e -> Ordering
forall mr e (p :: Pinned).
(MemRead mr, Prim e) =>
mr -> Off Word8 -> Bytes p -> Off Word8 -> Count e -> Ordering
compareByteOffToBytesMem (ShortByteString -> Bytes 'Inc
fromShortByteStringBytes ShortByteString
sbs)
{-# INLINE compareByteOffToBytesMem #-}
compareByteOffMem :: mr'
-> Off Word8 -> ShortByteString -> Off Word8 -> Count e -> Ordering
compareByteOffMem mr'
mem Off Word8
off1 ShortByteString
sbs = mr' -> Off Word8 -> Bytes 'Inc -> Off Word8 -> Count e -> Ordering
forall mr mr' e.
(MemRead mr, MemRead mr', Prim e) =>
mr' -> Off Word8 -> mr -> Off Word8 -> Count e -> Ordering
compareByteOffMem mr'
mem Off Word8
off1 (ShortByteString -> Bytes 'Inc
fromShortByteStringBytes ShortByteString
sbs)
{-# INLINE compareByteOffMem #-}
newtype MemState a s = MemState { MemState a s -> a
unMemState :: a }
instance MemWrite (MemState (ForeignPtr a)) where
isSameMutMem :: MemState (ForeignPtr a) s -> MemState (ForeignPtr a) s -> Bool
isSameMutMem (MemState ForeignPtr a
fptr1) (MemState ForeignPtr a
fptr2) =
IO Bool -> Bool
forall a. IO a -> a
unsafeInlineIO (IO Bool -> Bool) -> IO Bool -> Bool
forall a b. (a -> b) -> a -> b
$
ForeignPtr a -> (Ptr Word8 -> IO Bool) -> IO Bool
forall s p (m :: * -> *) a b.
(PtrAccess s p, MonadPrim s m) =>
p -> (Ptr a -> m b) -> m b
withPtrAccess ForeignPtr a
fptr1 ((Ptr Word8 -> IO Bool) -> IO Bool)
-> (Ptr Word8 -> IO Bool) -> IO Bool
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
ptr1 ->
ForeignPtr a -> (Ptr Word8 -> IO Bool) -> IO Bool
forall s p (m :: * -> *) a b.
(PtrAccess s p, MonadPrim s m) =>
p -> (Ptr a -> m b) -> m b
withPtrAccess ForeignPtr a
fptr2 ((Ptr Word8 -> IO Bool) -> IO Bool)
-> (Ptr Word8 -> IO Bool) -> IO Bool
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
ptr2 ->
Bool -> IO Bool
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Ptr Word8
ptr1 Ptr Word8 -> Ptr Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== (Ptr Word8
ptr2 :: Ptr Word8))
{-# INLINE isSameMutMem #-}
readOffMutMem :: MemState (ForeignPtr a) s -> Off e -> m e
readOffMutMem (MemState ForeignPtr a
fptr) Off e
i = ForeignPtr a -> (Ptr a -> m e) -> m e
forall s (m :: * -> *) e b.
MonadPrim s m =>
ForeignPtr e -> (Ptr e -> m b) -> m b
withForeignPtr ForeignPtr a
fptr ((Ptr a -> m e) -> m e) -> (Ptr a -> m e) -> m e
forall a b. (a -> b) -> a -> b
$ \Ptr a
ptr -> Ptr e -> Off e -> m e
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
Ptr e -> Off e -> m e
readOffPtr (Ptr a -> Ptr e
forall a b. Ptr a -> Ptr b
castPtr Ptr a
ptr) Off e
i
{-# INLINE readOffMutMem #-}
readByteOffMutMem :: MemState (ForeignPtr a) s -> Off Word8 -> m e
readByteOffMutMem (MemState ForeignPtr a
fptr) Off Word8
i =
ForeignPtr a -> (Ptr a -> m e) -> m e
forall s (m :: * -> *) e b.
MonadPrim s m =>
ForeignPtr e -> (Ptr e -> m b) -> m b
withForeignPtr ForeignPtr a
fptr ((Ptr a -> m e) -> m e) -> (Ptr a -> m e) -> m e
forall a b. (a -> b) -> a -> b
$ \Ptr a
ptr -> Ptr e -> Off Word8 -> m e
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
Ptr e -> Off Word8 -> m e
readByteOffPtr (Ptr a -> Ptr e
forall a b. Ptr a -> Ptr b
castPtr Ptr a
ptr) Off Word8
i
{-# INLINE readByteOffMutMem #-}
writeOffMutMem :: MemState (ForeignPtr a) s -> Off e -> e -> m ()
writeOffMutMem (MemState ForeignPtr a
fptr) Off e
i e
a = ForeignPtr a -> (Ptr a -> m ()) -> m ()
forall s (m :: * -> *) e b.
MonadPrim s m =>
ForeignPtr e -> (Ptr e -> m b) -> m b
withForeignPtr ForeignPtr a
fptr ((Ptr a -> m ()) -> m ()) -> (Ptr a -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Ptr a
ptr -> Ptr e -> Off e -> e -> m ()
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
Ptr e -> Off e -> e -> m ()
writeOffPtr (Ptr a -> Ptr e
forall a b. Ptr a -> Ptr b
castPtr Ptr a
ptr) Off e
i e
a
{-# INLINE writeOffMutMem #-}
writeByteOffMutMem :: MemState (ForeignPtr a) s -> Off Word8 -> e -> m ()
writeByteOffMutMem (MemState ForeignPtr a
fptr) Off Word8
i e
a =
ForeignPtr a -> (Ptr a -> m ()) -> m ()
forall s (m :: * -> *) e b.
MonadPrim s m =>
ForeignPtr e -> (Ptr e -> m b) -> m b
withForeignPtr ForeignPtr a
fptr ((Ptr a -> m ()) -> m ()) -> (Ptr a -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Ptr a
ptr -> Ptr e -> Off Word8 -> e -> m ()
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
Ptr e -> Off Word8 -> e -> m ()
writeByteOffPtr (Ptr a -> Ptr e
forall a b. Ptr a -> Ptr b
castPtr Ptr a
ptr) Off Word8
i e
a
{-# INLINE writeByteOffMutMem #-}
moveByteOffToPtrMutMem :: MemState (ForeignPtr a) s
-> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
moveByteOffToPtrMutMem (MemState ForeignPtr a
fsrc) Off Word8
srcOff Ptr e
dstPtr Off Word8
dstOff Count e
c =
ForeignPtr a -> (Ptr a -> m ()) -> m ()
forall s (m :: * -> *) e b.
MonadPrim s m =>
ForeignPtr e -> (Ptr e -> m b) -> m b
withForeignPtr ForeignPtr a
fsrc ((Ptr a -> m ()) -> m ()) -> (Ptr a -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Ptr a
srcPtr -> Ptr e -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
Ptr e -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
moveByteOffPtrToPtr (Ptr a -> Ptr e
forall a b. Ptr a -> Ptr b
castPtr Ptr a
srcPtr) Off Word8
srcOff Ptr e
dstPtr Off Word8
dstOff Count e
c
{-# INLINE moveByteOffToPtrMutMem #-}
moveByteOffToMBytesMutMem :: MemState (ForeignPtr a) s
-> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
moveByteOffToMBytesMutMem (MemState ForeignPtr a
fsrc) Off Word8
srcOff MBytes p s
dst Off Word8
dstOff Count e
c =
ForeignPtr a -> (Ptr a -> m ()) -> m ()
forall s (m :: * -> *) e b.
MonadPrim s m =>
ForeignPtr e -> (Ptr e -> m b) -> m b
withForeignPtr ForeignPtr a
fsrc ((Ptr a -> m ()) -> m ()) -> (Ptr a -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Ptr a
srcPtr -> Ptr e -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
forall s (m :: * -> *) e (p :: Pinned).
(MonadPrim s m, Prim e) =>
Ptr e -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
moveByteOffPtrToMBytes (Ptr a -> Ptr e
forall a b. Ptr a -> Ptr b
castPtr Ptr a
srcPtr) Off Word8
srcOff MBytes p s
dst Off Word8
dstOff Count e
c
{-# INLINE moveByteOffToMBytesMutMem #-}
copyByteOffMem :: mr
-> Off Word8
-> MemState (ForeignPtr a) s
-> Off Word8
-> Count e
-> m ()
copyByteOffMem mr
src Off Word8
srcOff (MemState ForeignPtr a
fdst) Off Word8
dstOff Count e
c =
ForeignPtr a -> (Ptr a -> m ()) -> m ()
forall s (m :: * -> *) e b.
MonadPrim s m =>
ForeignPtr e -> (Ptr e -> m b) -> m b
withForeignPtr ForeignPtr a
fdst ((Ptr a -> m ()) -> m ()) -> (Ptr a -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Ptr a
dstPtr ->
mr -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
forall mr s (m :: * -> *) e.
(MemRead mr, MonadPrim s m, Prim e) =>
mr -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
copyByteOffToPtrMem mr
src Off Word8
srcOff (Ptr a -> Ptr e
forall a b. Ptr a -> Ptr b
castPtr Ptr a
dstPtr) Off Word8
dstOff Count e
c
{-# INLINE copyByteOffMem #-}
moveByteOffMutMem :: mw' s
-> Off Word8
-> MemState (ForeignPtr a) s
-> Off Word8
-> Count e
-> m ()
moveByteOffMutMem mw' s
src Off Word8
srcOff (MemState ForeignPtr a
fdst) Off Word8
dstOff Count e
c =
ForeignPtr a -> (Ptr a -> m ()) -> m ()
forall s (m :: * -> *) e b.
MonadPrim s m =>
ForeignPtr e -> (Ptr e -> m b) -> m b
withForeignPtr ForeignPtr a
fdst ((Ptr a -> m ()) -> m ()) -> (Ptr a -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Ptr a
dstPtr ->
mw' s -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
mw s -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
moveByteOffToPtrMutMem mw' s
src Off Word8
srcOff (Ptr a -> Ptr e
forall a b. Ptr a -> Ptr b
castPtr Ptr a
dstPtr) Off Word8
dstOff Count e
c
{-# INLINE moveByteOffMutMem #-}
setMutMem :: MemState (ForeignPtr a) s -> Off e -> Count e -> e -> m ()
setMutMem (MemState ForeignPtr a
fptr) Off e
off Count e
c e
a = ForeignPtr a -> (Ptr a -> m ()) -> m ()
forall s (m :: * -> *) e b.
MonadPrim s m =>
ForeignPtr e -> (Ptr e -> m b) -> m b
withForeignPtr ForeignPtr a
fptr ((Ptr a -> m ()) -> m ()) -> (Ptr a -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Ptr a
ptr -> Ptr e -> Off e -> Count e -> e -> m ()
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
Ptr e -> Off e -> Count e -> e -> m ()
setOffPtr (Ptr a -> Ptr e
forall a b. Ptr a -> Ptr b
castPtr Ptr a
ptr) Off e
off Count e
c e
a
{-# INLINE setMutMem #-}
modifyFetchOldMutMem ::
(MemWrite mw, MonadPrim s m, Prim e) => mw s -> Off e -> (e -> e) -> m e
modifyFetchOldMutMem :: mw s -> Off e -> (e -> e) -> m e
modifyFetchOldMutMem mw s
mem Off e
o e -> e
f = mw s -> Off e -> (e -> m e) -> m e
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
mw s -> Off e -> (e -> m e) -> m e
modifyFetchOldMutMemM mw s
mem Off e
o (e -> m e
forall (f :: * -> *) a. Applicative f => a -> f a
pure (e -> m e) -> (e -> e) -> e -> m e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. e -> e
f)
{-# INLINE modifyFetchOldMutMem #-}
modifyFetchNewMutMem ::
(MemWrite mw, MonadPrim s m, Prim e) => mw s -> Off e -> (e -> e) -> m e
modifyFetchNewMutMem :: mw s -> Off e -> (e -> e) -> m e
modifyFetchNewMutMem mw s
mem Off e
o e -> e
f = mw s -> Off e -> (e -> m e) -> m e
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
mw s -> Off e -> (e -> m e) -> m e
modifyFetchNewMutMemM mw s
mem Off e
o (e -> m e
forall (f :: * -> *) a. Applicative f => a -> f a
pure (e -> m e) -> (e -> e) -> e -> m e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. e -> e
f)
{-# INLINE modifyFetchNewMutMem #-}
modifyFetchOldMutMemM ::
(MemWrite mw, MonadPrim s m, Prim e) => mw s -> Off e -> (e -> m e) -> m e
modifyFetchOldMutMemM :: mw s -> Off e -> (e -> m e) -> m e
modifyFetchOldMutMemM mw s
mem Off e
o e -> m e
f = do
e
a <- mw s -> Off e -> m e
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
mw s -> Off e -> m e
readOffMutMem mw s
mem Off e
o
e
a e -> m () -> m e
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ (mw s -> Off e -> e -> m ()
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
mw s -> Off e -> e -> m ()
writeOffMutMem mw s
mem Off e
o (e -> m ()) -> m e -> m ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< e -> m e
f e
a)
{-# INLINE modifyFetchOldMutMemM #-}
modifyFetchNewMutMemM ::
(MemWrite mw, MonadPrim s m, Prim e) => mw s -> Off e -> (e -> m e) -> m e
modifyFetchNewMutMemM :: mw s -> Off e -> (e -> m e) -> m e
modifyFetchNewMutMemM mw s
mem Off e
o e -> m e
f = do
e
a <- mw s -> Off e -> m e
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
mw s -> Off e -> m e
readOffMutMem mw s
mem Off e
o
e
a' <- e -> m e
f e
a
e
a' e -> m () -> m e
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ mw s -> Off e -> e -> m ()
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
mw s -> Off e -> e -> m ()
writeOffMutMem mw s
mem Off e
o e
a'
{-# INLINE modifyFetchNewMutMemM #-}
defaultReallocMutMem ::
(Prim e, MemAlloc ma, MonadPrim s m) => ma s -> Count e -> m (ma s)
defaultReallocMutMem :: ma s -> Count e -> m (ma s)
defaultReallocMutMem ma s
mem Count e
c = do
let newByteCount :: Count Word8
newByteCount = Count e -> Count Word8
forall e. Prim e => Count e -> Count Word8
toByteCount Count e
c
Count Word8
oldByteCount <- ma s -> m (Count Word8)
forall (ma :: * -> *) s (m :: * -> *).
(MemAlloc ma, MonadPrim s m) =>
ma s -> m (Count Word8)
getByteCountMutMem ma s
mem
if Count Word8
oldByteCount Count Word8 -> Count Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Count Word8
newByteCount
then ma s -> m (ma s)
forall (f :: * -> *) a. Applicative f => a -> f a
pure ma s
mem
else do
ma s
newMem <- Count Word8 -> m (ma s)
forall (ma :: * -> *) e s (m :: * -> *).
(MemAlloc ma, Prim e, MonadPrim s m) =>
Count e -> m (ma s)
allocMutMem Count Word8
newByteCount
FrozenMem ma
oldMem <- ma s -> m (FrozenMem ma)
forall (ma :: * -> *) s (m :: * -> *).
(MemAlloc ma, MonadPrim s m) =>
ma s -> m (FrozenMem ma)
freezeMutMem ma s
mem
ma s
newMem ma s -> m () -> m (ma s)
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ FrozenMem ma
-> Off Word8 -> ma s -> Off Word8 -> Count Word8 -> m ()
forall s (m :: * -> *) mr (mw :: * -> *) e.
(MonadPrim s m, MemRead mr, MemWrite mw, Prim e) =>
mr -> Off e -> mw s -> Off e -> Count e -> m ()
copyMem FrozenMem ma
oldMem Off Word8
0 ma s
newMem Off Word8
0 (Count Word8 -> Count Word8 -> Count Word8
forall a. Ord a => a -> a -> a
min Count Word8
oldByteCount Count Word8
newByteCount)
{-# INLINE defaultReallocMutMem #-}
cycleMemN ::
forall ma mr. (MemAlloc ma, MemRead mr)
=> Int
-> mr
-> FrozenMem ma
cycleMemN :: Int -> mr -> FrozenMem ma
cycleMemN Int
n mr
r
| Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = FrozenMem ma
forall (ma :: * -> *). MemAlloc ma => FrozenMem ma
emptyMem
| Bool
otherwise =
(forall s. ST s (FrozenMem ma)) -> FrozenMem ma
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (FrozenMem ma)) -> FrozenMem ma)
-> (forall s. ST s (FrozenMem ma)) -> FrozenMem ma
forall a b. (a -> b) -> a -> b
$ do
let bc :: Count Word8
bc@(Count Int
chunk) = mr -> Count Word8
forall mr. MemRead mr => mr -> Count Word8
byteCountMem mr
r
c :: Count Word8
c@(Count Int
c8) = Int -> Count Word8
forall e. Int -> Count e
Count Int
n Count Word8 -> Count Word8 -> Count Word8
forall a. Num a => a -> a -> a
* Count Word8
bc
ma s
mem <- Count Word8 -> ST s (ma s)
forall (ma :: * -> *) e s (m :: * -> *).
(MemAlloc ma, Prim e, MonadPrim s m) =>
Count e -> m (ma s)
allocMutMem Count Word8
c
let go :: Int -> ST s ()
go Int
i = Bool -> ST s () -> ST s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
c8) (ST s () -> ST s ()) -> ST s () -> ST s ()
forall a b. (a -> b) -> a -> b
$ mr -> Off Word8 -> ma s -> Off Word8 -> Count Word8 -> ST s ()
forall (mw :: * -> *) s (m :: * -> *) mr e.
(MemWrite mw, MonadPrim s m, MemRead mr, Prim e) =>
mr -> Off Word8 -> mw s -> Off Word8 -> Count e -> m ()
copyByteOffMem mr
r Off Word8
0 ma s
mem (Int -> Off Word8
forall e. Int -> Off e
Off Int
i) Count Word8
bc ST s () -> ST s () -> ST s ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Int -> ST s ()
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
chunk)
Int -> ST s ()
go Int
0
ma s -> ST s (FrozenMem ma)
forall (ma :: * -> *) s (m :: * -> *).
(MemAlloc ma, MonadPrim s m) =>
ma s -> m (FrozenMem ma)
freezeMutMem ma s
mem
{-# INLINE cycleMemN #-}
emptyMem ::
forall ma. MemAlloc ma
=> FrozenMem ma
emptyMem :: FrozenMem ma
emptyMem = Count Word8 -> (forall s. ma s -> ST s ()) -> FrozenMem ma
forall (ma :: * -> *) e b.
(MemAlloc ma, Prim e) =>
Count e -> (forall s. ma s -> ST s b) -> FrozenMem ma
createMemST_ (Count Word8
0 :: Count Word8) (\ma s
_ -> () -> ST s ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ())
{-# INLINE emptyMem #-}
singletonMem ::
forall e ma. (MemAlloc ma, Prim e)
=> e
-> FrozenMem ma
singletonMem :: e -> FrozenMem ma
singletonMem e
a = Count e -> (forall s. ma s -> ST s ()) -> FrozenMem ma
forall (ma :: * -> *) e b.
(MemAlloc ma, Prim e) =>
Count e -> (forall s. ma s -> ST s b) -> FrozenMem ma
createMemST_ (Count e
1 :: Count e) ((forall s. ma s -> ST s ()) -> FrozenMem ma)
-> (forall s. ma s -> ST s ()) -> FrozenMem ma
forall a b. (a -> b) -> a -> b
$ \ma s
mem -> ma s -> Off e -> e -> ST s ()
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
mw s -> Off e -> e -> m ()
writeOffMutMem ma s
mem Off e
0 e
a
{-# INLINE singletonMem #-}
allocZeroMutMem ::
forall e ma m s. (MemAlloc ma, MonadPrim s m, Prim e)
=> Count e
-> m (ma s)
allocZeroMutMem :: Count e -> m (ma s)
allocZeroMutMem Count e
n = do
ma s
m <- Count e -> m (ma s)
forall (ma :: * -> *) e s (m :: * -> *).
(MemAlloc ma, Prim e, MonadPrim s m) =>
Count e -> m (ma s)
allocMutMem Count e
n
ma s
m ma s -> m () -> m (ma s)
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ma s -> Off Word8 -> Count Word8 -> Word8 -> m ()
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
mw s -> Off e -> Count e -> e -> m ()
setMutMem ma s
m Off Word8
0 (Count e -> Count Word8
forall e. Prim e => Count e -> Count Word8
toByteCount Count e
n) (Word8
0 :: Word8)
{-# INLINE allocZeroMutMem #-}
createMemST ::
forall e b ma. (MemAlloc ma, Prim e)
=> Count e
-> (forall s. ma s -> ST s b)
-> (b, FrozenMem ma)
createMemST :: Count e -> (forall s. ma s -> ST s b) -> (b, FrozenMem ma)
createMemST Count e
n forall s. ma s -> ST s b
f = (forall s. ST s (b, FrozenMem ma)) -> (b, FrozenMem ma)
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (b, FrozenMem ma)) -> (b, FrozenMem ma))
-> (forall s. ST s (b, FrozenMem ma)) -> (b, FrozenMem ma)
forall a b. (a -> b) -> a -> b
$ Count e -> ST s (ma s)
forall (ma :: * -> *) e s (m :: * -> *).
(MemAlloc ma, Prim e, MonadPrim s m) =>
Count e -> m (ma s)
allocMutMem Count e
n ST s (ma s)
-> (ma s -> ST s (b, FrozenMem ma)) -> ST s (b, FrozenMem ma)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \ma s
m -> (,) (b -> FrozenMem ma -> (b, FrozenMem ma))
-> ST s b -> ST s (FrozenMem ma -> (b, FrozenMem ma))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ma s -> ST s b
forall s. ma s -> ST s b
f ma s
m ST s (FrozenMem ma -> (b, FrozenMem ma))
-> ST s (FrozenMem ma) -> ST s (b, FrozenMem ma)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ma s -> ST s (FrozenMem ma)
forall (ma :: * -> *) s (m :: * -> *).
(MemAlloc ma, MonadPrim s m) =>
ma s -> m (FrozenMem ma)
freezeMutMem ma s
m
{-# INLINE createMemST #-}
createMemST_ ::
(MemAlloc ma, Prim e)
=> Count e
-> (forall s. ma s -> ST s b)
-> FrozenMem ma
createMemST_ :: Count e -> (forall s. ma s -> ST s b) -> FrozenMem ma
createMemST_ Count e
n forall s. ma s -> ST s b
f = (forall s. ST s (FrozenMem ma)) -> FrozenMem ma
forall a. (forall s. ST s a) -> a
runST (Count e -> ST s (ma s)
forall (ma :: * -> *) e s (m :: * -> *).
(MemAlloc ma, Prim e, MonadPrim s m) =>
Count e -> m (ma s)
allocMutMem Count e
n ST s (ma s) -> (ma s -> ST s (FrozenMem ma)) -> ST s (FrozenMem ma)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \ma s
m -> ma s -> ST s b
forall s. ma s -> ST s b
f ma s
m ST s b -> ST s (FrozenMem ma) -> ST s (FrozenMem ma)
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ma s -> ST s (FrozenMem ma)
forall (ma :: * -> *) s (m :: * -> *).
(MemAlloc ma, MonadPrim s m) =>
ma s -> m (FrozenMem ma)
freezeMutMem ma s
m)
{-# INLINE createMemST_ #-}
createZeroMemST ::
forall e ma b. (MemAlloc ma, Prim e)
=> Count e
-> (forall s. ma s -> ST s b)
-> (b, FrozenMem ma)
createZeroMemST :: Count e -> (forall s. ma s -> ST s b) -> (b, FrozenMem ma)
createZeroMemST Count e
n forall s. ma s -> ST s b
f = (forall s. ST s (b, FrozenMem ma)) -> (b, FrozenMem ma)
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (b, FrozenMem ma)) -> (b, FrozenMem ma))
-> (forall s. ST s (b, FrozenMem ma)) -> (b, FrozenMem ma)
forall a b. (a -> b) -> a -> b
$ Count e -> ST s (ma s)
forall e (ma :: * -> *) (m :: * -> *) s.
(MemAlloc ma, MonadPrim s m, Prim e) =>
Count e -> m (ma s)
allocZeroMutMem Count e
n ST s (ma s)
-> (ma s -> ST s (b, FrozenMem ma)) -> ST s (b, FrozenMem ma)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \ma s
m -> (,) (b -> FrozenMem ma -> (b, FrozenMem ma))
-> ST s b -> ST s (FrozenMem ma -> (b, FrozenMem ma))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ma s -> ST s b
forall s. ma s -> ST s b
f ma s
m ST s (FrozenMem ma -> (b, FrozenMem ma))
-> ST s (FrozenMem ma) -> ST s (b, FrozenMem ma)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ma s -> ST s (FrozenMem ma)
forall (ma :: * -> *) s (m :: * -> *).
(MemAlloc ma, MonadPrim s m) =>
ma s -> m (FrozenMem ma)
freezeMutMem ma s
m
{-# INLINE createZeroMemST #-}
createZeroMemST_ ::
forall e ma b. (MemAlloc ma, Prim e)
=> Count e
-> (forall s. ma s -> ST s b)
-> FrozenMem ma
createZeroMemST_ :: Count e -> (forall s. ma s -> ST s b) -> FrozenMem ma
createZeroMemST_ Count e
n forall s. ma s -> ST s b
f = (forall s. ST s (FrozenMem ma)) -> FrozenMem ma
forall a. (forall s. ST s a) -> a
runST (Count e -> ST s (ma s)
forall e (ma :: * -> *) (m :: * -> *) s.
(MemAlloc ma, MonadPrim s m, Prim e) =>
Count e -> m (ma s)
allocZeroMutMem Count e
n ST s (ma s) -> (ma s -> ST s (FrozenMem ma)) -> ST s (FrozenMem ma)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \ma s
m -> ma s -> ST s b
forall s. ma s -> ST s b
f ma s
m ST s b -> ST s (FrozenMem ma) -> ST s (FrozenMem ma)
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ma s -> ST s (FrozenMem ma)
forall (ma :: * -> *) s (m :: * -> *).
(MemAlloc ma, MonadPrim s m) =>
ma s -> m (FrozenMem ma)
freezeMutMem ma s
m)
{-# INLINE createZeroMemST_ #-}
cloneMem ::
forall ma. MemAlloc ma
=> FrozenMem ma
-> FrozenMem ma
cloneMem :: FrozenMem ma -> FrozenMem ma
cloneMem FrozenMem ma
fm =
(forall s. ST s (FrozenMem ma)) -> FrozenMem ma
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (FrozenMem ma)) -> FrozenMem ma)
-> (forall s. ST s (FrozenMem ma)) -> FrozenMem ma
forall a b. (a -> b) -> a -> b
$ do
let n :: Count Word8
n = FrozenMem ma -> Count Word8
forall mr. MemRead mr => mr -> Count Word8
byteCountMem FrozenMem ma
fm
ma s
mm <- Count Word8 -> ST s (ma s)
forall (ma :: * -> *) e s (m :: * -> *).
(MemAlloc ma, Prim e, MonadPrim s m) =>
Count e -> m (ma s)
allocMutMem Count Word8
n
FrozenMem ma
-> Off Word8 -> ma s -> Off Word8 -> Count Word8 -> ST s ()
forall s (m :: * -> *) mr (mw :: * -> *) e.
(MonadPrim s m, MemRead mr, MemWrite mw, Prim e) =>
mr -> Off e -> mw s -> Off e -> Count e -> m ()
copyMem FrozenMem ma
fm Off Word8
0 ma s
mm Off Word8
0 Count Word8
n
ma s -> ST s (FrozenMem ma)
forall (ma :: * -> *) s (m :: * -> *).
(MemAlloc ma, MonadPrim s m) =>
ma s -> m (FrozenMem ma)
freezeMutMem ma s
mm
{-# INLINE cloneMem #-}
copyMem ::
(MonadPrim s m, MemRead mr, MemWrite mw, Prim e)
=> mr
-> Off e
-> mw s
-> Off e
-> Count e
-> m ()
copyMem :: mr -> Off e -> mw s -> Off e -> Count e -> m ()
copyMem mr
src Off e
srcOff mw s
dst Off e
dstOff = mr -> Off Word8 -> mw s -> Off Word8 -> Count e -> m ()
forall (mw :: * -> *) s (m :: * -> *) mr e.
(MemWrite mw, MonadPrim s m, MemRead mr, Prim e) =>
mr -> Off Word8 -> mw s -> Off Word8 -> Count e -> m ()
copyByteOffMem mr
src (Off e -> Off Word8
forall e. Prim e => Off e -> Off Word8
toByteOff Off e
srcOff) mw s
dst (Off e -> Off Word8
forall e. Prim e => Off e -> Off Word8
toByteOff Off e
dstOff)
{-# INLINE copyMem #-}
moveMutMem ::
(MonadPrim s m, MemWrite mw1, MemWrite mw2, Prim e)
=> mw1 s
-> Off e
-> mw2 s
-> Off e
-> Count e
-> m ()
moveMutMem :: mw1 s -> Off e -> mw2 s -> Off e -> Count e -> m ()
moveMutMem mw1 s
src Off e
srcOff mw2 s
dst Off e
dstOff = mw1 s -> Off Word8 -> mw2 s -> Off Word8 -> Count e -> m ()
forall (mw :: * -> *) s (m :: * -> *) (mw' :: * -> *) e.
(MemWrite mw, MonadPrim s m, MemWrite mw', Prim e) =>
mw' s -> Off Word8 -> mw s -> Off Word8 -> Count e -> m ()
moveByteOffMutMem mw1 s
src (Off e -> Off Word8
forall e. Prim e => Off e -> Off Word8
toByteOff Off e
srcOff) mw2 s
dst (Off e -> Off Word8
forall e. Prim e => Off e -> Off Word8
toByteOff Off e
dstOff)
{-# INLINE moveMutMem #-}
appendMem ::
forall mr1 mr2 ma. (MemRead mr1, MemRead mr2, MemAlloc ma)
=> mr1
-> mr2
-> FrozenMem ma
appendMem :: mr1 -> mr2 -> FrozenMem ma
appendMem mr1
r1 mr2
r2 =
Count Word8 -> (forall s. ma s -> ST s ()) -> FrozenMem ma
forall (ma :: * -> *) e b.
(MemAlloc ma, Prim e) =>
Count e -> (forall s. ma s -> ST s b) -> FrozenMem ma
createMemST_ (Count Word8
n1 Count Word8 -> Count Word8 -> Count Word8
forall a. Num a => a -> a -> a
+ Count Word8
n2) ((forall s. ma s -> ST s ()) -> FrozenMem ma)
-> (forall s. ma s -> ST s ()) -> FrozenMem ma
forall a b. (a -> b) -> a -> b
$ \ma s
mem -> do
mr1 -> Off Word8 -> ma s -> Off Word8 -> Count Word8 -> ST s ()
forall s (m :: * -> *) mr (mw :: * -> *) e.
(MonadPrim s m, MemRead mr, MemWrite mw, Prim e) =>
mr -> Off e -> mw s -> Off e -> Count e -> m ()
copyMem mr1
r1 Off Word8
0 ma s
mem Off Word8
0 Count Word8
n1
mr2 -> Off Word8 -> ma s -> Off Word8 -> Count Word8 -> ST s ()
forall s (m :: * -> *) mr (mw :: * -> *) e.
(MonadPrim s m, MemRead mr, MemWrite mw, Prim e) =>
mr -> Off e -> mw s -> Off e -> Count e -> m ()
copyMem mr2
r2 (Count Word8 -> Off Word8
coerce Count Word8
n1) ma s
mem (Count Word8 -> Off Word8
coerce Count Word8
n1) Count Word8
n2
where
n1 :: Count Word8
n1 = mr1 -> Count Word8
forall mr. MemRead mr => mr -> Count Word8
byteCountMem mr1
r1
n2 :: Count Word8
n2 = mr2 -> Count Word8
forall mr. MemRead mr => mr -> Count Word8
byteCountMem mr2
r2
{-# INLINABLE appendMem #-}
concatMem ::
forall mr ma. (MemRead mr, MemAlloc ma)
=> [mr]
-> FrozenMem ma
concatMem :: [mr] -> FrozenMem ma
concatMem [mr]
xs = do
let c :: Count Word8
c = (Count Word8 -> mr -> Count Word8)
-> Count Word8 -> [mr] -> Count Word8
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
Foldable.foldl' (\ !Count Word8
acc mr
b -> Count Word8
acc Count Word8 -> Count Word8 -> Count Word8
forall a. Num a => a -> a -> a
+ mr -> Count Word8
forall mr. MemRead mr => mr -> Count Word8
byteCountMem mr
b) Count Word8
0 [mr]
xs
Count Word8 -> (forall s. ma s -> ST s ()) -> FrozenMem ma
forall (ma :: * -> *) e b.
(MemAlloc ma, Prim e) =>
Count e -> (forall s. ma s -> ST s b) -> FrozenMem ma
createMemST_ Count Word8
c ((forall s. ma s -> ST s ()) -> FrozenMem ma)
-> (forall s. ma s -> ST s ()) -> FrozenMem ma
forall a b. (a -> b) -> a -> b
$ \ma s
mb -> do
let load :: Off Word8 -> mr -> ST s (Off Word8)
load Off Word8
i mr
b = do
let cb :: Count Word8
cb@(Count Int
n) = mr -> Count Word8
forall mr. MemRead mr => mr -> Count Word8
byteCountMem mr
b :: Count Word8
(Off Word8
i Off Word8 -> Off Word8 -> Off Word8
forall a. Num a => a -> a -> a
+ Int -> Off Word8
forall e. Int -> Off e
Off Int
n) Off Word8 -> ST s () -> ST s (Off Word8)
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ mr -> Off Word8 -> ma s -> Off Word8 -> Count Word8 -> ST s ()
forall s (m :: * -> *) mr (mw :: * -> *) e.
(MonadPrim s m, MemRead mr, MemWrite mw, Prim e) =>
mr -> Off e -> mw s -> Off e -> Count e -> m ()
copyMem mr
b Off Word8
0 ma s
mb Off Word8
i Count Word8
cb
(Off Word8 -> mr -> ST s (Off Word8))
-> Off Word8 -> [mr] -> ST s ()
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m ()
foldM_ Off Word8 -> mr -> ST s (Off Word8)
load Off Word8
0 [mr]
xs
{-# INLINABLE concatMem #-}
thawCloneMem ::
forall ma m s. (MemAlloc ma, MonadPrim s m)
=> FrozenMem ma
-> m (ma s)
thawCloneMem :: FrozenMem ma -> m (ma s)
thawCloneMem FrozenMem ma
a = FrozenMem ma -> Off Word8 -> Count Word8 -> m (ma s)
forall e (ma :: * -> *) (m :: * -> *) s.
(Prim e, MemAlloc ma, MonadPrim s m) =>
FrozenMem ma -> Off e -> Count e -> m (ma s)
thawCopyMem FrozenMem ma
a Off Word8
0 (FrozenMem ma -> Count Word8
forall mr. MemRead mr => mr -> Count Word8
byteCountMem FrozenMem ma
a)
{-# INLINE thawCloneMem #-}
thawCopyMem ::
forall e ma m s. (Prim e, MemAlloc ma, MonadPrim s m)
=> FrozenMem ma
-> Off e
-> Count e
-> m (ma s)
thawCopyMem :: FrozenMem ma -> Off e -> Count e -> m (ma s)
thawCopyMem FrozenMem ma
a Off e
off Count e
c = do
ma s
mem <- Count e -> m (ma s)
forall (ma :: * -> *) e s (m :: * -> *).
(MemAlloc ma, Prim e, MonadPrim s m) =>
Count e -> m (ma s)
allocMutMem Count e
c
ma s
mem ma s -> m () -> m (ma s)
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ FrozenMem ma -> Off e -> ma s -> Off e -> Count e -> m ()
forall s (m :: * -> *) mr (mw :: * -> *) e.
(MonadPrim s m, MemRead mr, MemWrite mw, Prim e) =>
mr -> Off e -> mw s -> Off e -> Count e -> m ()
copyMem FrozenMem ma
a Off e
off ma s
mem Off e
0 Count e
c
{-# INLINE thawCopyMem #-}
freezeCopyMutMem ::
forall e ma m s. (Prim e, MemAlloc ma, MonadPrim s m)
=> ma s
-> Off e
-> Count e
-> m (FrozenMem ma)
freezeCopyMutMem :: ma s -> Off e -> Count e -> m (FrozenMem ma)
freezeCopyMutMem ma s
mem Off e
off Count e
c = ma s -> m (FrozenMem ma)
forall (ma :: * -> *) s (m :: * -> *).
(MemAlloc ma, MonadPrim s m) =>
ma s -> m (FrozenMem ma)
freezeMutMem ma s
mem m (FrozenMem ma)
-> (FrozenMem ma -> m (FrozenMem ma)) -> m (FrozenMem ma)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \FrozenMem ma
r -> FrozenMem ma -> Off e -> Count e -> m (ma s)
forall e (ma :: * -> *) (m :: * -> *) s.
(Prim e, MemAlloc ma, MonadPrim s m) =>
FrozenMem ma -> Off e -> Count e -> m (ma s)
thawCopyMem FrozenMem ma
r Off e
off Count e
c m (ma s) -> (ma s -> m (FrozenMem ma)) -> m (FrozenMem ma)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ma s -> m (FrozenMem ma)
forall (ma :: * -> *) s (m :: * -> *).
(MemAlloc ma, MonadPrim s m) =>
ma s -> m (FrozenMem ma)
freezeMutMem
{-# INLINE freezeCopyMutMem #-}
freezeCloneMutMem ::
forall ma m s. (MemAlloc ma, MonadPrim s m)
=> ma s
-> m (FrozenMem ma)
freezeCloneMutMem :: ma s -> m (FrozenMem ma)
freezeCloneMutMem = ma s -> m (FrozenMem ma)
forall (ma :: * -> *) s (m :: * -> *).
(MemAlloc ma, MonadPrim s m) =>
ma s -> m (FrozenMem ma)
freezeMutMem (ma s -> m (FrozenMem ma))
-> (FrozenMem ma -> m (FrozenMem ma)) -> ma s -> m (FrozenMem ma)
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> FrozenMem ma -> m (ma s)
forall (ma :: * -> *) (m :: * -> *) s.
(MemAlloc ma, MonadPrim s m) =>
FrozenMem ma -> m (ma s)
thawCloneMem (FrozenMem ma -> m (ma s))
-> (ma s -> m (FrozenMem ma)) -> FrozenMem ma -> m (FrozenMem ma)
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> ma s -> m (FrozenMem ma)
forall (ma :: * -> *) s (m :: * -> *).
(MemAlloc ma, MonadPrim s m) =>
ma s -> m (FrozenMem ma)
freezeMutMem
{-# INLINE freezeCloneMutMem #-}
convertMem :: (MemRead mr, MemAlloc ma) => mr -> FrozenMem ma
convertMem :: mr -> FrozenMem ma
convertMem mr
m =
let c :: Count Word8
c = mr -> Count Word8
forall mr. MemRead mr => mr -> Count Word8
byteCountMem mr
m
in Count Word8 -> (forall s. ma s -> ST s ()) -> FrozenMem ma
forall (ma :: * -> *) e b.
(MemAlloc ma, Prim e) =>
Count e -> (forall s. ma s -> ST s b) -> FrozenMem ma
createMemST_ Count Word8
c (\ma s
mm -> mr -> Off Word8 -> ma s -> Off Word8 -> Count Word8 -> ST s ()
forall s (m :: * -> *) mr (mw :: * -> *) e.
(MonadPrim s m, MemRead mr, MemWrite mw, Prim e) =>
mr -> Off e -> mw s -> Off e -> Count e -> m ()
copyMem mr
m Off Word8
0 ma s
mm Off Word8
0 Count Word8
c)
{-# INLINE convertMem #-}
countMem ::
forall e mr. (MemRead mr, Prim e)
=> mr
-> Count e
countMem :: mr -> Count e
countMem = Count Word8 -> Count e
forall e. Prim e => Count Word8 -> Count e
fromByteCount (Count Word8 -> Count e) -> (mr -> Count Word8) -> mr -> Count e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. mr -> Count Word8
forall mr. MemRead mr => mr -> Count Word8
byteCountMem
{-# INLINE countMem #-}
countRemMem :: forall e mr. (MemRead mr, Prim e) => mr -> (Count e, Count Word8)
countRemMem :: mr -> (Count e, Count Word8)
countRemMem = Count Word8 -> (Count e, Count Word8)
forall e. Prim e => Count Word8 -> (Count e, Count Word8)
fromByteCountRem (Count Word8 -> (Count e, Count Word8))
-> (mr -> Count Word8) -> mr -> (Count e, Count Word8)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. mr -> Count Word8
forall mr. MemRead mr => mr -> Count Word8
byteCountMem
{-# INLINE countRemMem #-}
getCountMutMem :: forall e ma m s. (MemAlloc ma, MonadPrim s m, Prim e) => ma s -> m (Count e)
getCountMutMem :: ma s -> m (Count e)
getCountMutMem = (Count Word8 -> Count e) -> m (Count Word8) -> m (Count e)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Count Word8 -> Count e
forall e. Prim e => Count Word8 -> Count e
fromByteCount (Count Word8 -> Count e)
-> (Count Word8 -> Count Word8) -> Count Word8 -> Count e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Count Word8 -> Count Word8
coerce) (m (Count Word8) -> m (Count e))
-> (ma s -> m (Count Word8)) -> ma s -> m (Count e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ma s -> m (Count Word8)
forall (ma :: * -> *) s (m :: * -> *).
(MemAlloc ma, MonadPrim s m) =>
ma s -> m (Count Word8)
getByteCountMutMem
{-# INLINE getCountMutMem #-}
getCountRemMutMem ::
forall e ma m s. (MemAlloc ma, MonadPrim s m, Prim e)
=> ma s
-> m (Count e, Count Word8)
getCountRemMutMem :: ma s -> m (Count e, Count Word8)
getCountRemMutMem = (Count Word8 -> (Count e, Count Word8))
-> m (Count Word8) -> m (Count e, Count Word8)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Count Word8 -> (Count e, Count Word8)
forall e. Prim e => Count Word8 -> (Count e, Count Word8)
fromByteCountRem (Count Word8 -> (Count e, Count Word8))
-> (Count Word8 -> Count Word8)
-> Count Word8
-> (Count e, Count Word8)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Count Word8 -> Count Word8
coerce) (m (Count Word8) -> m (Count e, Count Word8))
-> (ma s -> m (Count Word8)) -> ma s -> m (Count e, Count Word8)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ma s -> m (Count Word8)
forall (ma :: * -> *) s (m :: * -> *).
(MemAlloc ma, MonadPrim s m) =>
ma s -> m (Count Word8)
getByteCountMutMem
{-# INLINE getCountRemMutMem #-}
cloneMutMem ::
forall ma m s. (MemAlloc ma, MonadPrim s m)
=> ma s
-> m (ma s)
cloneMutMem :: ma s -> m (ma s)
cloneMutMem = ma s -> m (FrozenMem ma)
forall (ma :: * -> *) s (m :: * -> *).
(MemAlloc ma, MonadPrim s m) =>
ma s -> m (FrozenMem ma)
freezeMutMem (ma s -> m (FrozenMem ma))
-> (FrozenMem ma -> m (ma s)) -> ma s -> m (ma s)
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> FrozenMem ma -> m (ma s)
forall (ma :: * -> *) (m :: * -> *) s.
(MemAlloc ma, MonadPrim s m) =>
FrozenMem ma -> m (ma s)
thawCloneMem
{-# INLINE cloneMutMem #-}
eqByteOffMem ::
(MemRead mr1, MemRead mr2)
=> mr1
-> Off Word8
-> mr2
-> Off Word8
-> Count Word8
-> Bool
eqByteOffMem :: mr1 -> Off Word8 -> mr2 -> Off Word8 -> Count Word8 -> Bool
eqByteOffMem mr1
b1 Off Word8
off1 mr2
b2 Off Word8
off2 Count Word8
n = mr1 -> Off Word8 -> mr2 -> Off Word8 -> Count Word8 -> Ordering
forall mr mr' e.
(MemRead mr, MemRead mr', Prim e) =>
mr' -> Off Word8 -> mr -> Off Word8 -> Count e -> Ordering
compareByteOffMem mr1
b1 Off Word8
off1 mr2
b2 Off Word8
off2 Count Word8
n Ordering -> Ordering -> Bool
forall a. Eq a => a -> a -> Bool
== Ordering
EQ
{-# INLINE eqByteOffMem #-}
eqByteMem :: (MemRead mr1, MemRead mr2) => mr1 -> mr2 -> Bool
eqByteMem :: mr1 -> mr2 -> Bool
eqByteMem mr1
b1 mr2
b2 = Count Word8
n Count Word8 -> Count Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== mr2 -> Count Word8
forall mr. MemRead mr => mr -> Count Word8
byteCountMem mr2
b2 Bool -> Bool -> Bool
&& mr1 -> Off Word8 -> mr2 -> Off Word8 -> Count Word8 -> Bool
forall mr1 mr2.
(MemRead mr1, MemRead mr2) =>
mr1 -> Off Word8 -> mr2 -> Off Word8 -> Count Word8 -> Bool
eqByteOffMem mr1
b1 Off Word8
0 mr2
b2 Off Word8
0 Count Word8
n
where
n :: Count Word8
n = mr1 -> Count Word8
forall mr. MemRead mr => mr -> Count Word8
byteCountMem mr1
b1
{-# INLINE eqByteMem #-}
compareByteMem :: (MemRead mr1, MemRead mr2) => mr1 -> mr2 -> Ordering
compareByteMem :: mr1 -> mr2 -> Ordering
compareByteMem mr1
b1 mr2
b2 = Count Word8 -> Count Word8 -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Count Word8
n (mr2 -> Count Word8
forall mr. MemRead mr => mr -> Count Word8
byteCountMem mr2
b2) Ordering -> Ordering -> Ordering
forall a. Semigroup a => a -> a -> a
<> mr1 -> Off Word8 -> mr2 -> Off Word8 -> Count Word8 -> Ordering
forall mr mr' e.
(MemRead mr, MemRead mr', Prim e) =>
mr' -> Off Word8 -> mr -> Off Word8 -> Count e -> Ordering
compareByteOffMem mr1
b1 Off Word8
0 mr2
b2 Off Word8
0 Count Word8
n
where
n :: Count Word8
n = mr1 -> Count Word8
forall mr. MemRead mr => mr -> Count Word8
byteCountMem mr1
b1
{-# INLINE compareByteMem #-}
toListMem :: forall e mr. (MemRead mr, Prim e) => mr -> [e]
toListMem :: mr -> [e]
toListMem mr
ba = (forall b. (e -> b -> b) -> b -> b) -> [e]
forall a. (forall b. (a -> b -> b) -> b -> b) -> [a]
build (\ e -> b -> b
c b
n -> Count e -> (e -> b -> b) -> b -> mr -> b
forall e b mr.
(MemRead mr, Prim e) =>
Count e -> (e -> b -> b) -> b -> mr -> b
foldrCountMem (mr -> Count e
forall e mr. (MemRead mr, Prim e) => mr -> Count e
countMem mr
ba) e -> b -> b
c b
n mr
ba)
{-# INLINE toListMem #-}
{-# SPECIALIZE toListMem :: Prim e => Bytes p -> [e] #-}
toListSlackMem ::
forall e mr. (MemRead mr, Prim e)
=> mr
-> ([e], [Word8])
toListSlackMem :: mr -> ([e], [Word8])
toListSlackMem mr
mem =
((forall b. (e -> b -> b) -> b -> b) -> [e]
forall a. (forall b. (a -> b -> b) -> b -> b) -> [a]
build (\e -> b -> b
c b
n -> Count e -> (e -> b -> b) -> b -> mr -> b
forall e b mr.
(MemRead mr, Prim e) =>
Count e -> (e -> b -> b) -> b -> mr -> b
foldrCountMem Count e
k e -> b -> b
c b
n mr
mem), Int -> [Word8] -> [Word8]
getSlack (Int
k8 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
r8) [])
where
(Count e
k, Count Int
r8) = mr -> (Count e, Count Word8)
forall e mr. (MemRead mr, Prim e) => mr -> (Count e, Count Word8)
countRemMem mr
mem
Count Int
k8 = Count e -> Count Word8
forall e. Prim e => Count e -> Count Word8
toByteCount Count e
k
getSlack :: Int -> [Word8] -> [Word8]
getSlack Int
i ![Word8]
acc
| Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
k8 = [Word8]
acc
| Bool
otherwise =
let i' :: Int
i' = Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1
in Int -> [Word8] -> [Word8]
getSlack Int
i' (mr -> Off Word8 -> Word8
forall mr e. (MemRead mr, Prim e) => mr -> Off Word8 -> e
indexByteOffMem mr
mem (Int -> Off Word8
forall e. Int -> Off e
Off Int
i') Word8 -> [Word8] -> [Word8]
forall a. a -> [a] -> [a]
: [Word8]
acc)
{-# INLINABLE toListSlackMem #-}
foldrCountMem :: forall e b mr. (MemRead mr, Prim e) => Count e -> (e -> b -> b) -> b -> mr -> b
foldrCountMem :: Count e -> (e -> b -> b) -> b -> mr -> b
foldrCountMem (Count Int
k) e -> b -> b
c b
nil mr
bs = Int -> b
go Int
0
where
go :: Int -> b
go Int
i
| Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
k = b
nil
| Bool
otherwise =
let !v :: e
v = mr -> Off e -> e
forall mr e. (MemRead mr, Prim e) => mr -> Off e -> e
indexOffMem mr
bs (Int -> Off e
forall e. Int -> Off e
Off Int
i)
in e
v e -> b -> b
`c` Int -> b
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
{-# INLINE[0] foldrCountMem #-}
fromListMem ::
forall e ma. (Prim e, MemAlloc ma)
=> [e]
-> FrozenMem ma
fromListMem :: [e] -> FrozenMem ma
fromListMem [e]
xs =
let count :: Count e
count = Int -> Count e
coerce ([e] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [e]
xs) Count e -> [e] -> Count e
forall e (proxy :: * -> *). Count e -> proxy e -> Count e
`countForProxyTypeOf` [e]
xs
in Count e -> (forall s. ma s -> ST s ()) -> FrozenMem ma
forall (ma :: * -> *) e b.
(MemAlloc ma, Prim e) =>
Count e -> (forall s. ma s -> ST s b) -> FrozenMem ma
createMemST_ Count e
count (Count e -> [e] -> ma s -> ST s ()
forall e (mw :: * -> *) (m :: * -> *) s.
(Prim e, MemWrite mw, MonadPrim s m) =>
Count e -> [e] -> mw s -> m ()
loadListMutMemN_ Count e
count [e]
xs)
{-# INLINE fromListMem #-}
fromByteListMem ::
forall ma. MemAlloc ma
=> [Word8]
-> FrozenMem ma
fromByteListMem :: [Word8] -> FrozenMem ma
fromByteListMem = [Word8] -> FrozenMem ma
forall e (ma :: * -> *).
(Prim e, MemAlloc ma) =>
[e] -> FrozenMem ma
fromListMem
{-# INLINE fromByteListMem #-}
fromListMemN ::
forall e ma. (Prim e, MemAlloc ma)
=> Count e
-> [e]
-> (Either [e] (Count e), FrozenMem ma)
fromListMemN :: Count e -> [e] -> (Either [e] (Count e), FrozenMem ma)
fromListMemN Count e
count [e]
xs =
Count e
-> (forall s. ma s -> ST s (Either [e] (Count e)))
-> (Either [e] (Count e), FrozenMem ma)
forall e b (ma :: * -> *).
(MemAlloc ma, Prim e) =>
Count e -> (forall s. ma s -> ST s b) -> (b, FrozenMem ma)
createMemST Count e
count ((forall s. ma s -> ST s (Either [e] (Count e)))
-> (Either [e] (Count e), FrozenMem ma))
-> (forall s. ma s -> ST s (Either [e] (Count e)))
-> (Either [e] (Count e), FrozenMem ma)
forall a b. (a -> b) -> a -> b
$ \ma s
mm -> do
([e]
ys, Count e
loadedCount) <- Count e -> [e] -> ma s -> Off e -> ST s ([e], Count e)
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
Count e -> [e] -> mw s -> Off e -> m ([e], Count e)
loadListOffMutMemN Count e
count [e]
xs ma s
mm Off e
0
Either [e] (Count e) -> ST s (Either [e] (Count e))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either [e] (Count e) -> ST s (Either [e] (Count e)))
-> Either [e] (Count e) -> ST s (Either [e] (Count e))
forall a b. (a -> b) -> a -> b
$
if Count e
loadedCount Count e -> Count e -> Bool
forall a. Eq a => a -> a -> Bool
/= Count e
count Bool -> Bool -> Bool
&& [e] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [e]
ys
then Count e -> Either [e] (Count e)
forall a b. b -> Either a b
Right Count e
loadedCount
else [e] -> Either [e] (Count e)
forall a b. a -> Either a b
Left [e]
ys
{-# INLINE fromListMemN #-}
fromListZeroMemN ::
forall e ma. (Prim e, MemAlloc ma)
=> Count e
-> [e]
-> (Either [e] (Count e), FrozenMem ma)
fromListZeroMemN :: Count e -> [e] -> (Either [e] (Count e), FrozenMem ma)
fromListZeroMemN Count e
count [e]
xs =
Count e
-> (forall s. ma s -> ST s (Either [e] (Count e)))
-> (Either [e] (Count e), FrozenMem ma)
forall e b (ma :: * -> *).
(MemAlloc ma, Prim e) =>
Count e -> (forall s. ma s -> ST s b) -> (b, FrozenMem ma)
createMemST (Count e -> Count e -> Count e
forall a. Ord a => a -> a -> a
max Count e
0 Count e
count) ((forall s. ma s -> ST s (Either [e] (Count e)))
-> (Either [e] (Count e), FrozenMem ma))
-> (forall s. ma s -> ST s (Either [e] (Count e)))
-> (Either [e] (Count e), FrozenMem ma)
forall a b. (a -> b) -> a -> b
$ \ma s
mm -> do
([e]
ys, Count e
loadedCount) <- Count e -> [e] -> ma s -> Off e -> ST s ([e], Count e)
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
Count e -> [e] -> mw s -> Off e -> m ([e], Count e)
loadListOffMutMemN Count e
count [e]
xs ma s
mm Off e
0
let loadedByteCount :: Count Word8
loadedByteCount = Count e -> Count Word8
forall e. Prim e => Count e -> Count Word8
toByteCount Count e
loadedCount
surplusByteCount :: Count Word8
surplusByteCount = Count e -> Count Word8
forall e. Prim e => Count e -> Count Word8
toByteCount Count e
count Count Word8 -> Count Word8 -> Count Word8
forall a. Num a => a -> a -> a
- Count Word8
loadedByteCount
Bool -> ST s () -> ST s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Count Word8
surplusByteCount Count Word8 -> Count Word8 -> Bool
forall a. Ord a => a -> a -> Bool
> Count Word8
0) (ST s () -> ST s ()) -> ST s () -> ST s ()
forall a b. (a -> b) -> a -> b
$ ma s -> Off Word8 -> Count Word8 -> Word8 -> ST s ()
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
mw s -> Off e -> Count e -> e -> m ()
setMutMem ma s
mm (Count Word8 -> Off Word8
forall e. Count e -> Off e
countToOff Count Word8
loadedByteCount) Count Word8
surplusByteCount Word8
0
Either [e] (Count e) -> ST s (Either [e] (Count e))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either [e] (Count e) -> ST s (Either [e] (Count e)))
-> Either [e] (Count e) -> ST s (Either [e] (Count e))
forall a b. (a -> b) -> a -> b
$
if Count e
loadedCount Count e -> Count e -> Bool
forall a. Eq a => a -> a -> Bool
/= Count e
count Bool -> Bool -> Bool
&& [e] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [e]
ys
then Count e -> Either [e] (Count e)
forall a b. b -> Either a b
Right Count e
loadedCount
else [e] -> Either [e] (Count e)
forall a b. a -> Either a b
Left [e]
ys
{-# INLINE fromListZeroMemN #-}
fromListZeroMemN_ ::
forall e ma. (Prim e, MemAlloc ma)
=> Count e
-> [e]
-> FrozenMem ma
fromListZeroMemN_ :: Count e -> [e] -> FrozenMem ma
fromListZeroMemN_ !Count e
n = (Either [e] (Count e), FrozenMem ma) -> FrozenMem ma
forall a b. (a, b) -> b
snd ((Either [e] (Count e), FrozenMem ma) -> FrozenMem ma)
-> ([e] -> (Either [e] (Count e), FrozenMem ma))
-> [e]
-> FrozenMem ma
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Count e -> [e] -> (Either [e] (Count e), FrozenMem ma)
forall e (ma :: * -> *).
(Prim e, MemAlloc ma) =>
Count e -> [e] -> (Either [e] (Count e), FrozenMem ma)
fromListZeroMemN Count e
n
{-# INLINE fromListZeroMemN_ #-}
loadListByteOffHelper ::
(MemWrite mw, MonadPrim s m, Prim e)
=> [e]
-> mw s
-> Off Word8
-> Off Word8
-> Off Word8
-> m ([e], Count e)
loadListByteOffHelper :: [e]
-> mw s -> Off Word8 -> Off Word8 -> Off Word8 -> m ([e], Count e)
loadListByteOffHelper [e]
ys mw s
mw Off Word8
byteOff Off Word8
k Off Word8
step =
let go :: [e] -> Off Word8 -> m ([e], Count e)
go [] Off Word8
i = ([e], Count e) -> m ([e], Count e)
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([], Off Word8 -> Count e
toLoadedCount Off Word8
i)
go a :: [e]
a@(e
x:[e]
xs) Off Word8
i
| Off Word8
i Off Word8 -> Off Word8 -> Bool
forall a. Ord a => a -> a -> Bool
< Off Word8
k = mw s -> Off Word8 -> e -> m ()
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
mw s -> Off Word8 -> e -> m ()
writeByteOffMutMem mw s
mw Off Word8
i e
x m () -> m ([e], Count e) -> m ([e], Count e)
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> [e] -> Off Word8 -> m ([e], Count e)
go [e]
xs (Off Word8
i Off Word8 -> Off Word8 -> Off Word8
forall a. Num a => a -> a -> a
+ Off Word8
step)
| Bool
otherwise = ([e], Count e) -> m ([e], Count e)
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([e]
a, Off Word8 -> Count e
toLoadedCount Off Word8
i)
toLoadedCount :: Off Word8 -> Count e
toLoadedCount Off Word8
i = Count Word8 -> Count e
forall e. Prim e => Count Word8 -> Count e
fromByteCount (Off Word8 -> Count Word8
forall e. Off e -> Count e
offToCount (Off Word8
i Off Word8 -> Off Word8 -> Off Word8
forall a. Num a => a -> a -> a
- Off Word8
byteOff))
in [e] -> Off Word8 -> m ([e], Count e)
go [e]
ys Off Word8
byteOff
{-# INLINE loadListByteOffHelper #-}
loadListByteOffMutMemN ::
(MemWrite mw, MonadPrim s m, Prim e)
=> Count e
-> [e]
-> mw s
-> Off Word8
-> m ([e], Count e)
loadListByteOffMutMemN :: Count e -> [e] -> mw s -> Off Word8 -> m ([e], Count e)
loadListByteOffMutMemN Count e
count [e]
ys mw s
mw Off Word8
byteOff = [e]
-> mw s -> Off Word8 -> Off Word8 -> Off Word8 -> m ([e], Count e)
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
[e]
-> mw s -> Off Word8 -> Off Word8 -> Off Word8 -> m ([e], Count e)
loadListByteOffHelper [e]
ys mw s
mw Off Word8
byteOff Off Word8
k Off Word8
step
where
k :: Off Word8
k = Off Word8
byteOff Off Word8 -> Off Word8 -> Off Word8
forall a. Num a => a -> a -> a
+ Count Word8 -> Off Word8
forall e. Count e -> Off e
countToOff (Count e -> Count Word8
forall e. Prim e => Count e -> Count Word8
toByteCount Count e
count)
step :: Off Word8
step = Count Word8 -> Off Word8
forall e. Count e -> Off e
countToOff (Count Word8 -> Off Word8) -> Count Word8 -> Off Word8
forall a b. (a -> b) -> a -> b
$ [e] -> Count Word8
forall (proxy :: * -> *) e. Prim e => proxy e -> Count Word8
byteCountProxy [e]
ys
{-# INLINABLE loadListByteOffMutMemN #-}
loadListByteOffMutMem ::
(MemAlloc ma, MonadPrim s m, Prim e)
=> [e]
-> ma s
-> Off Word8
-> m ([e], Count e)
loadListByteOffMutMem :: [e] -> ma s -> Off Word8 -> m ([e], Count e)
loadListByteOffMutMem [e]
ys ma s
ma Off Word8
byteOff = do
Count Word8
bCount <- ma s -> m (Count Word8)
forall (ma :: * -> *) s (m :: * -> *).
(MemAlloc ma, MonadPrim s m) =>
ma s -> m (Count Word8)
getByteCountMutMem ma s
ma
let k :: Off Word8
k = Count Word8 -> Off Word8
forall e. Count e -> Off e
countToOff Count Word8
bCount Off Word8 -> Off Word8 -> Off Word8
forall a. Num a => a -> a -> a
- Off Word8
byteOff
step :: Off Word8
step = Count Word8 -> Off Word8
forall e. Count e -> Off e
countToOff (Count Word8 -> Off Word8) -> Count Word8 -> Off Word8
forall a b. (a -> b) -> a -> b
$ [e] -> Count Word8
forall (proxy :: * -> *) e. Prim e => proxy e -> Count Word8
byteCountProxy [e]
ys
[e]
-> ma s -> Off Word8 -> Off Word8 -> Off Word8 -> m ([e], Count e)
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
[e]
-> mw s -> Off Word8 -> Off Word8 -> Off Word8 -> m ([e], Count e)
loadListByteOffHelper [e]
ys ma s
ma Off Word8
byteOff Off Word8
k Off Word8
step
{-# INLINABLE loadListByteOffMutMem #-}
loadListOffMutMemN ::
(MemWrite mw, MonadPrim s m, Prim e)
=> Count e
-> [e]
-> mw s
-> Off e
-> m ([e], Count e)
loadListOffMutMemN :: Count e -> [e] -> mw s -> Off e -> m ([e], Count e)
loadListOffMutMemN Count e
count [e]
ys mw s
mw Off e
off =
let go :: [e] -> Off e -> m ([e], Count e)
go [] Off e
i = ([e], Count e) -> m ([e], Count e)
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([], Off e -> Count e
toLoadedCount Off e
i)
go a :: [e]
a@(e
x:[e]
xs) Off e
i
| Off e
i Off e -> Off e -> Bool
forall a. Ord a => a -> a -> Bool
< Off e
k = mw s -> Off e -> e -> m ()
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
mw s -> Off e -> e -> m ()
writeOffMutMem mw s
mw Off e
i e
x m () -> m ([e], Count e) -> m ([e], Count e)
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> [e] -> Off e -> m ([e], Count e)
go [e]
xs (Off e
i Off e -> Off e -> Off e
forall a. Num a => a -> a -> a
+ Off e
1)
| Bool
otherwise = ([e], Count e) -> m ([e], Count e)
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([e]
a, Off e -> Count e
toLoadedCount Off e
i)
k :: Off e
k = Off e
off Off e -> Off e -> Off e
forall a. Num a => a -> a -> a
+ Count e -> Off e
forall e. Count e -> Off e
countToOff Count e
count
toLoadedCount :: Off e -> Count e
toLoadedCount Off e
i = Off e -> Count e
forall e. Off e -> Count e
offToCount (Off e
i Off e -> Off e -> Off e
forall a. Num a => a -> a -> a
- Off e
off)
in [e] -> Off e -> m ([e], Count e)
go [e]
ys Off e
off
{-# INLINABLE loadListOffMutMemN #-}
loadListMutMemN ::
forall e mw m s. (MemWrite mw, MonadPrim s m, Prim e)
=> Count e
-> [e]
-> mw s
-> m ([e], Count e)
loadListMutMemN :: Count e -> [e] -> mw s -> m ([e], Count e)
loadListMutMemN Count e
count [e]
xs mw s
mw = Count e -> [e] -> mw s -> Off e -> m ([e], Count e)
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
Count e -> [e] -> mw s -> Off e -> m ([e], Count e)
loadListOffMutMemN Count e
count [e]
xs mw s
mw Off e
0
{-# INLINABLE loadListMutMemN #-}
loadListMutMemN_ ::
forall e mw m s. (Prim e, MemWrite mw, MonadPrim s m)
=> Count e
-> [e]
-> mw s
-> m ()
loadListMutMemN_ :: Count e -> [e] -> mw s -> m ()
loadListMutMemN_ (Count Int
n) [e]
ys mw s
mb =
let go :: [e] -> Int -> m ()
go [] Int
_ = () -> m ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
go (e
x:[e]
xs) Int
i = Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
n) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ mw s -> Off e -> e -> m ()
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
mw s -> Off e -> e -> m ()
writeOffMutMem mw s
mb (Int -> Off e
forall e. Int -> Off e
Off Int
i) e
x m () -> m () -> m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> [e] -> Int -> m ()
go [e]
xs (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
in [e] -> Int -> m ()
go [e]
ys Int
0
{-# INLINABLE loadListMutMemN_ #-}
loadListOffMutMem ::
forall e ma m s. (Prim e, MemAlloc ma, MonadPrim s m)
=> [e]
-> ma s
-> Off e
-> m ([e], Count e)
loadListOffMutMem :: [e] -> ma s -> Off e -> m ([e], Count e)
loadListOffMutMem [e]
ys ma s
ma Off e
off = ma s -> m (Count e)
forall e (ma :: * -> *) (m :: * -> *) s.
(MemAlloc ma, MonadPrim s m, Prim e) =>
ma s -> m (Count e)
getCountMutMem ma s
ma m (Count e) -> (Count e -> m ([e], Count e)) -> m ([e], Count e)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Count e
c -> Count e -> [e] -> ma s -> Off e -> m ([e], Count e)
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
Count e -> [e] -> mw s -> Off e -> m ([e], Count e)
loadListOffMutMemN (Count e
c Count e -> Count e -> Count e
forall a. Num a => a -> a -> a
- Off e -> Count e
forall e. Off e -> Count e
offToCount Off e
off) [e]
ys ma s
ma Off e
off
{-# INLINE loadListOffMutMem #-}
loadListMutMem ::
forall e ma m s. (Prim e, MemAlloc ma, MonadPrim s m)
=> [e]
-> ma s
-> m ([e], Count e)
loadListMutMem :: [e] -> ma s -> m ([e], Count e)
loadListMutMem [e]
ys ma s
ma = ma s -> m (Count e)
forall e (ma :: * -> *) (m :: * -> *) s.
(MemAlloc ma, MonadPrim s m, Prim e) =>
ma s -> m (Count e)
getCountMutMem ma s
ma m (Count e) -> (Count e -> m ([e], Count e)) -> m ([e], Count e)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Count e
c -> Count e -> [e] -> ma s -> Off e -> m ([e], Count e)
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
Count e -> [e] -> mw s -> Off e -> m ([e], Count e)
loadListOffMutMemN (Count e
c Count e -> [e] -> Count e
forall e (proxy :: * -> *). Count e -> proxy e -> Count e
`countForProxyTypeOf` [e]
ys) [e]
ys ma s
ma Off e
0
{-# INLINE loadListMutMem #-}
loadListMutMem_ ::
forall e ma m s. (Prim e, MemAlloc ma, MonadPrim s m)
=> [e]
-> ma s
-> m ()
loadListMutMem_ :: [e] -> ma s -> m ()
loadListMutMem_ [e]
ys ma s
mb = ma s -> m (Count e)
forall e (ma :: * -> *) (m :: * -> *) s.
(MemAlloc ma, MonadPrim s m, Prim e) =>
ma s -> m (Count e)
getCountMutMem ma s
mb m (Count e) -> (Count e -> m ()) -> m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Count e
c -> Count e -> [e] -> ma s -> m ()
forall e (mw :: * -> *) (m :: * -> *) s.
(Prim e, MemWrite mw, MonadPrim s m) =>
Count e -> [e] -> mw s -> m ()
loadListMutMemN_ (Count e
c Count e -> [e] -> Count e
forall e (proxy :: * -> *). Count e -> proxy e -> Count e
`countForProxyTypeOf` [e]
ys) [e]
ys ma s
mb
{-# INLINE loadListMutMem_ #-}
toByteListMem ::
forall ma. MemAlloc ma
=> FrozenMem ma
-> [Word8]
toByteListMem :: FrozenMem ma -> [Word8]
toByteListMem = FrozenMem ma -> [Word8]
forall e mr. (MemRead mr, Prim e) => mr -> [e]
toListMem
{-# INLINE toByteListMem #-}
mapByteMem ::
forall e mr ma. (MemRead mr, MemAlloc ma, Prim e)
=> (Word8 -> e)
-> mr
-> FrozenMem ma
mapByteMem :: (Word8 -> e) -> mr -> FrozenMem ma
mapByteMem Word8 -> e
f = (Off Word8 -> Word8 -> e) -> mr -> FrozenMem ma
forall mr (ma :: * -> *) e.
(MemRead mr, MemAlloc ma, Prim e) =>
(Off Word8 -> Word8 -> e) -> mr -> FrozenMem ma
imapByteOffMem ((Word8 -> e) -> Off Word8 -> Word8 -> e
forall a b. a -> b -> a
const Word8 -> e
f)
imapByteOffMem ::
(MemRead mr, MemAlloc ma, Prim e) => (Off Word8 -> Word8 -> e) -> mr -> FrozenMem ma
imapByteOffMem :: (Off Word8 -> Word8 -> e) -> mr -> FrozenMem ma
imapByteOffMem Off Word8 -> Word8 -> e
f mr
r = (forall s. ST s (FrozenMem ma)) -> FrozenMem ma
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (FrozenMem ma)) -> FrozenMem ma)
-> (forall s. ST s (FrozenMem ma)) -> FrozenMem ma
forall a b. (a -> b) -> a -> b
$ (Off Word8 -> Word8 -> ST s e) -> mr -> ST s (FrozenMem ma)
forall e mr (ma :: * -> *) (m :: * -> *) s.
(MemRead mr, MemAlloc ma, MonadPrim s m, Prim e) =>
(Off Word8 -> Word8 -> m e) -> mr -> m (FrozenMem ma)
mapByteOffMemM (\Off Word8
i -> e -> ST s e
forall (f :: * -> *) a. Applicative f => a -> f a
pure (e -> ST s e) -> (Word8 -> e) -> Word8 -> ST s e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Off Word8 -> Word8 -> e
f Off Word8
i) mr
r
mapByteMemM ::
(MemRead mr, MemAlloc ma, MonadPrim s m, Prim e)
=> (Word8 -> m e)
-> mr
-> m (FrozenMem ma)
mapByteMemM :: (Word8 -> m e) -> mr -> m (FrozenMem ma)
mapByteMemM Word8 -> m e
f = (Off Word8 -> Word8 -> m e) -> mr -> m (FrozenMem ma)
forall e mr (ma :: * -> *) (m :: * -> *) s.
(MemRead mr, MemAlloc ma, MonadPrim s m, Prim e) =>
(Off Word8 -> Word8 -> m e) -> mr -> m (FrozenMem ma)
mapByteOffMemM ((Word8 -> m e) -> Off Word8 -> Word8 -> m e
forall a b. a -> b -> a
const Word8 -> m e
f)
mapByteOffMemM ::
forall e mr ma m s. (MemRead mr, MemAlloc ma, MonadPrim s m, Prim e)
=> (Off Word8 -> Word8 -> m e)
-> mr
-> m (FrozenMem ma)
mapByteOffMemM :: (Off Word8 -> Word8 -> m e) -> mr -> m (FrozenMem ma)
mapByteOffMemM Off Word8 -> Word8 -> m e
f mr
r = do
let bc :: Count Word8
bc@(Count Int
n) = mr -> Count Word8
forall mr. MemRead mr => mr -> Count Word8
byteCountMem mr
r
c :: Count e
c = Int -> Count e
forall e. Int -> Count e
Count Int
n Count e -> m e -> Count e
forall e (proxy :: * -> *). Count e -> proxy e -> Count e
`countForProxyTypeOf` Off Word8 -> Word8 -> m e
f Off Word8
0 Word8
0
ma s
mem <- Count e -> m (ma s)
forall (ma :: * -> *) e s (m :: * -> *).
(MemAlloc ma, Prim e, MonadPrim s m) =>
Count e -> m (ma s)
allocMutMem Count e
c
Off Word8
_ <- mr
-> Off Word8
-> Count Word8
-> (Off Word8 -> Word8 -> m e)
-> m (Off Word8)
forall mr s (m :: * -> *) e b.
(MemRead mr, MonadPrim s m, Prim e) =>
mr
-> Off Word8 -> Count e -> (Off Word8 -> e -> m b) -> m (Off Word8)
forByteOffMemM_ mr
r Off Word8
0 Count Word8
bc Off Word8 -> Word8 -> m e
f
ma s -> m (FrozenMem ma)
forall (ma :: * -> *) s (m :: * -> *).
(MemAlloc ma, MonadPrim s m) =>
ma s -> m (FrozenMem ma)
freezeMutMem ma s
mem
forByteOffMemM_ ::
(MemRead mr, MonadPrim s m, Prim e)
=> mr
-> Off Word8
-> Count e
-> (Off Word8 -> e -> m b)
-> m (Off Word8)
forByteOffMemM_ :: mr
-> Off Word8 -> Count e -> (Off Word8 -> e -> m b) -> m (Off Word8)
forByteOffMemM_ mr
r (Off Int
byteOff) Count e
c Off Word8 -> e -> m b
f =
let n :: Int
n = Count Word8 -> Int
coerce (Count e -> Count Word8
forall e. Prim e => Count e -> Count Word8
toByteCount Count e
c) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
byteOff
Count Int
k = Count e -> Count Word8
forall (proxy :: * -> *) e. Prim e => proxy e -> Count Word8
byteCountProxy Count e
c
go :: Int -> m (Off Word8)
go Int
i
| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
n = Off Word8 -> e -> m b
f (Int -> Off Word8
forall e. Int -> Off e
Off Int
i) (mr -> Off Word8 -> e
forall mr e. (MemRead mr, Prim e) => mr -> Off Word8 -> e
indexByteOffMem mr
r (Int -> Off Word8
forall e. Int -> Off e
Off Int
i)) m b -> m (Off Word8) -> m (Off Word8)
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Int -> m (Off Word8)
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
k)
| Bool
otherwise = Off Word8 -> m (Off Word8)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Off Word8 -> m (Off Word8)) -> Off Word8 -> m (Off Word8)
forall a b. (a -> b) -> a -> b
$ Int -> Off Word8
forall e. Int -> Off e
Off Int
i
in Int -> m (Off Word8)
go Int
byteOff
data MemView a = MemView
{ MemView a -> Off Word8
mvOffset :: {-# UNPACK #-} !(Off Word8)
, MemView a -> Count Word8
mvCount :: {-# UNPACK #-} !(Count Word8)
, MemView a -> a
mvMem :: !a
}
data MMemView a s = MMemView
{ MMemView a s -> Off Word8
mmvOffset :: {-# UNPACK #-} !(Off Word8)
, MMemView a s -> Count Word8
mmvCount :: {-# UNPACK #-} !(Count Word8)
, MMemView a s -> a s
mmvMem :: !(a s)
}
izipWithByteOffMemM_ ::
(MemRead mr1, MemRead mr2, MonadPrim s m, Prim e)
=> mr1
-> Off Word8
-> mr2
-> Off Word8
-> Count e
-> (Off Word8 -> e -> Off Word8 -> e -> m b)
-> m (Off Word8)
izipWithByteOffMemM_ :: mr1
-> Off Word8
-> mr2
-> Off Word8
-> Count e
-> (Off Word8 -> e -> Off Word8 -> e -> m b)
-> m (Off Word8)
izipWithByteOffMemM_ mr1
r1 (Off Int
byteOff1) mr2
r2 Off Word8
off2 Count e
c Off Word8 -> e -> Off Word8 -> e -> m b
f =
let n :: Int
n = Count Word8 -> Int
coerce (Count e -> Count Word8
forall e. Prim e => Count e -> Count Word8
toByteCount Count e
c) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
byteOff1
Count Int
k = Count e -> Count Word8
forall (proxy :: * -> *) e. Prim e => proxy e -> Count Word8
byteCountProxy Count e
c
go :: Int -> m (Off Word8)
go Int
i
| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
n =
let o1 :: Off Word8
o1 = Int -> Off Word8
forall e. Int -> Off e
Off Int
i
o2 :: Off Word8
o2 = Int -> Off Word8
forall e. Int -> Off e
Off Int
i Off Word8 -> Off Word8 -> Off Word8
forall a. Num a => a -> a -> a
+ Off Word8
off2
in Off Word8 -> e -> Off Word8 -> e -> m b
f Off Word8
o1 (mr1 -> Off Word8 -> e
forall mr e. (MemRead mr, Prim e) => mr -> Off Word8 -> e
indexByteOffMem mr1
r1 Off Word8
o1) Off Word8
o2 (mr2 -> Off Word8 -> e
forall mr e. (MemRead mr, Prim e) => mr -> Off Word8 -> e
indexByteOffMem mr2
r2 Off Word8
o2) m b -> m (Off Word8) -> m (Off Word8)
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
Int -> m (Off Word8)
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
k)
| Bool
otherwise = Off Word8 -> m (Off Word8)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Off Word8 -> m (Off Word8)) -> Off Word8 -> m (Off Word8)
forall a b. (a -> b) -> a -> b
$ Int -> Off Word8
forall e. Int -> Off e
Off Int
i
in Int -> m (Off Word8)
go Int
byteOff1
izipWithOffMemM_ ::
(MemRead mr1, MemRead mr2, MonadPrim s m, Prim e1, Prim e2)
=> mr1
-> Off e1
-> mr2
-> Off e2
-> Int
-> (Off e1 -> e1 -> Off e2 -> e2 -> m b)
-> m ()
izipWithOffMemM_ :: mr1
-> Off e1
-> mr2
-> Off e2
-> Int
-> (Off e1 -> e1 -> Off e2 -> e2 -> m b)
-> m ()
izipWithOffMemM_ mr1
r1 Off e1
off1 mr2
r2 Off e2
off2 Int
nc Off e1 -> e1 -> Off e2 -> e2 -> m b
f =
let n :: Int
n = Int
nc Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Off e1 -> Int
coerce Off e1
off1
go :: Off e1 -> Off e2 -> m ()
go o1 :: Off e1
o1@(Off Int
i) Off e2
o2 =
Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
n) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$
Off e1 -> e1 -> Off e2 -> e2 -> m b
f Off e1
o1 (mr1 -> Off e1 -> e1
forall mr e. (MemRead mr, Prim e) => mr -> Off e -> e
indexOffMem mr1
r1 Off e1
o1) Off e2
o2 (mr2 -> Off e2 -> e2
forall mr e. (MemRead mr, Prim e) => mr -> Off e -> e
indexOffMem mr2
r2 Off e2
o2) m b -> m () -> m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Off e1 -> Off e2 -> m ()
go (Off e1
o1 Off e1 -> Off e1 -> Off e1
forall a. Num a => a -> a -> a
+ Off e1
1) (Off e2
o2 Off e2 -> Off e2 -> Off e2
forall a. Num a => a -> a -> a
+ Off e2
1)
in Off e1 -> Off e2 -> m ()
go Off e1
off1 Off e2
off2
instance MemRead (Bytes p) where
isSameMem :: Bytes p -> Bytes p -> Bool
isSameMem = Bytes p -> Bytes p -> Bool
forall (p1 :: Pinned) (p2 :: Pinned). Bytes p1 -> Bytes p2 -> Bool
isSameBytes
{-# INLINE isSameMem #-}
byteCountMem :: Bytes p -> Count Word8
byteCountMem = Bytes p -> Count Word8
forall (p :: Pinned). Bytes p -> Count Word8
byteCountBytes
{-# INLINE byteCountMem #-}
indexOffMem :: Bytes p -> Off e -> e
indexOffMem = Bytes p -> Off e -> e
forall e (p :: Pinned). Prim e => Bytes p -> Off e -> e
indexOffBytes
{-# INLINE indexOffMem #-}
indexByteOffMem :: Bytes p -> Off Word8 -> e
indexByteOffMem = Bytes p -> Off Word8 -> e
forall e (p :: Pinned). Prim e => Bytes p -> Off Word8 -> e
indexByteOffBytes
{-# INLINE indexByteOffMem #-}
copyByteOffToMBytesMem :: Bytes p -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
copyByteOffToMBytesMem = Bytes p -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
forall s (m :: * -> *) e (ps :: Pinned) (pd :: Pinned).
(MonadPrim s m, Prim e) =>
Bytes ps
-> Off Word8 -> MBytes pd s -> Off Word8 -> Count e -> m ()
copyByteOffBytesToMBytes
{-# INLINE copyByteOffToMBytesMem #-}
copyByteOffToPtrMem :: Bytes p -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
copyByteOffToPtrMem = Bytes p -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
forall s (m :: * -> *) e (p :: Pinned).
(MonadPrim s m, Prim e) =>
Bytes p -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
copyByteOffBytesToPtr
{-# INLINE copyByteOffToPtrMem #-}
compareByteOffToPtrMem :: Bytes p -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m Ordering
compareByteOffToPtrMem Bytes p
bytes1 Off Word8
off1 Ptr e
ptr2 Off Word8
off2 Count e
c =
Ordering -> m Ordering
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Ordering -> m Ordering) -> Ordering -> m Ordering
forall a b. (a -> b) -> a -> b
$! Bytes p -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> Ordering
forall e (p :: Pinned).
Prim e =>
Bytes p -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> Ordering
compareByteOffBytesToPtr Bytes p
bytes1 Off Word8
off1 Ptr e
ptr2 Off Word8
off2 Count e
c
{-# INLINE compareByteOffToPtrMem #-}
compareByteOffToBytesMem :: Bytes p -> Off Word8 -> Bytes p -> Off Word8 -> Count e -> Ordering
compareByteOffToBytesMem Bytes p
bytes1 Off Word8
off1 Bytes p
bytes2 Off Word8
off2 Count e
c =
Bytes p -> Off Word8 -> Bytes p -> Off Word8 -> Count e -> Ordering
forall e (p1 :: Pinned) (p2 :: Pinned).
Prim e =>
Bytes p1
-> Off Word8 -> Bytes p2 -> Off Word8 -> Count e -> Ordering
compareByteOffBytes Bytes p
bytes1 Off Word8
off1 Bytes p
bytes2 Off Word8
off2 Count e
c
{-# INLINE compareByteOffToBytesMem #-}
compareByteOffMem :: mr' -> Off Word8 -> Bytes p -> Off Word8 -> Count e -> Ordering
compareByteOffMem mr'
mem1 Off Word8
off1 Bytes p
bs Off Word8
off2 Count e
c =
mr' -> Off Word8 -> Bytes p -> Off Word8 -> Count e -> Ordering
forall mr e (p :: Pinned).
(MemRead mr, Prim e) =>
mr -> Off Word8 -> Bytes p -> Off Word8 -> Count e -> Ordering
compareByteOffToBytesMem mr'
mem1 Off Word8
off1 Bytes p
bs Off Word8
off2 Count e
c
{-# INLINE compareByteOffMem #-}
instance Typeable p => MemAlloc (MBytes p) where
type FrozenMem (MBytes p) = Bytes p
getByteCountMutMem :: MBytes p s -> m (Count Word8)
getByteCountMutMem = MBytes p s -> m (Count Word8)
forall s (m :: * -> *) (p :: Pinned).
MonadPrim s m =>
MBytes p s -> m (Count Word8)
getByteCountMBytes
{-# INLINE getByteCountMutMem #-}
allocMutMem :: Count e -> m (MBytes p s)
allocMutMem = Count e -> m (MBytes p s)
forall (p :: Pinned) e s (m :: * -> *).
(Typeable p, Prim e, MonadPrim s m) =>
Count e -> m (MBytes p s)
allocMBytes
{-# INLINE allocMutMem #-}
thawMem :: FrozenMem (MBytes p) -> m (MBytes p s)
thawMem = FrozenMem (MBytes p) -> m (MBytes p s)
forall s (m :: * -> *) (p :: Pinned).
MonadPrim s m =>
Bytes p -> m (MBytes p s)
thawBytes
{-# INLINE thawMem #-}
freezeMutMem :: MBytes p s -> m (FrozenMem (MBytes p))
freezeMutMem = MBytes p s -> m (FrozenMem (MBytes p))
forall s (m :: * -> *) (p :: Pinned).
MonadPrim s m =>
MBytes p s -> m (Bytes p)
freezeMBytes
{-# INLINE freezeMutMem #-}
reallocMutMem :: MBytes p s -> Count e -> m (MBytes p s)
reallocMutMem = MBytes p s -> Count e -> m (MBytes p s)
forall e (p :: Pinned) (m :: * -> *) s.
(MonadPrim s m, Typeable p, Prim e) =>
MBytes p s -> Count e -> m (MBytes p s)
reallocMBytes
{-# INLINE reallocMutMem #-}
instance MemWrite (MBytes p) where
isSameMutMem :: MBytes p s -> MBytes p s -> Bool
isSameMutMem = MBytes p s -> MBytes p s -> Bool
forall (p1 :: Pinned) s (p2 :: Pinned).
MBytes p1 s -> MBytes p2 s -> Bool
isSameMBytes
{-# INLINE isSameMutMem #-}
readOffMutMem :: MBytes p s -> Off e -> m e
readOffMutMem = MBytes p s -> Off e -> m e
forall s (m :: * -> *) e (p :: Pinned).
(MonadPrim s m, Prim e) =>
MBytes p s -> Off e -> m e
readOffMBytes
{-# INLINE readOffMutMem #-}
readByteOffMutMem :: MBytes p s -> Off Word8 -> m e
readByteOffMutMem = MBytes p s -> Off Word8 -> m e
forall s (m :: * -> *) e (p :: Pinned).
(MonadPrim s m, Prim e) =>
MBytes p s -> Off Word8 -> m e
readByteOffMBytes
{-# INLINE readByteOffMutMem #-}
writeOffMutMem :: MBytes p s -> Off e -> e -> m ()
writeOffMutMem = MBytes p s -> Off e -> e -> m ()
forall s (m :: * -> *) e (p :: Pinned).
(MonadPrim s m, Prim e) =>
MBytes p s -> Off e -> e -> m ()
writeOffMBytes
{-# INLINE writeOffMutMem #-}
writeByteOffMutMem :: MBytes p s -> Off Word8 -> e -> m ()
writeByteOffMutMem = MBytes p s -> Off Word8 -> e -> m ()
forall s (m :: * -> *) e (p :: Pinned).
(MonadPrim s m, Prim e) =>
MBytes p s -> Off Word8 -> e -> m ()
writeByteOffMBytes
{-# INLINE writeByteOffMutMem #-}
moveByteOffToPtrMutMem :: MBytes p s -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
moveByteOffToPtrMutMem = MBytes p s -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
forall s (m :: * -> *) e (p :: Pinned).
(MonadPrim s m, Prim e) =>
MBytes p s -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
moveByteOffMBytesToPtr
{-# INLINE moveByteOffToPtrMutMem #-}
moveByteOffToMBytesMutMem :: MBytes p s
-> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
moveByteOffToMBytesMutMem = MBytes p s
-> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
forall s (m :: * -> *) e (ps :: Pinned) (pd :: Pinned).
(MonadPrim s m, Prim e) =>
MBytes ps s
-> Off Word8 -> MBytes pd s -> Off Word8 -> Count e -> m ()
moveByteOffMBytesToMBytes
{-# INLINE moveByteOffToMBytesMutMem #-}
moveByteOffMutMem :: mw' s -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
moveByteOffMutMem = mw' s -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
forall (mw :: * -> *) s (m :: * -> *) e (p :: Pinned).
(MemWrite mw, MonadPrim s m, Prim e) =>
mw s -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
moveByteOffToMBytesMutMem
{-# INLINE moveByteOffMutMem #-}
copyByteOffMem :: mr -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
copyByteOffMem = mr -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
forall mr s (m :: * -> *) e (p :: Pinned).
(MemRead mr, MonadPrim s m, Prim e) =>
mr -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
copyByteOffToMBytesMem
{-# INLINE copyByteOffMem #-}
setMutMem :: MBytes p s -> Off e -> Count e -> e -> m ()
setMutMem = MBytes p s -> Off e -> Count e -> e -> m ()
forall s (m :: * -> *) e (p :: Pinned).
(MonadPrim s m, Prim e) =>
MBytes p s -> Off e -> Count e -> e -> m ()
setMBytes
{-# INLINE setMutMem #-}
instance Show (Bytes p) where
show :: Bytes p -> String
show Bytes p
b =
(ShowS -> ShowS) -> String -> [ShowS] -> String
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
Foldable.foldr' ShowS -> ShowS
forall a b. (a -> b) -> a -> b
($) String
"]" ([ShowS] -> String) -> [ShowS] -> String
forall a b. (a -> b) -> a -> b
$
(Char
'[' Char -> ShowS
forall a. a -> [a] -> [a]
:) ShowS -> [ShowS] -> [ShowS]
forall a. a -> [a] -> [a]
: ShowS -> [ShowS] -> [ShowS]
forall a. a -> [a] -> [a]
List.intersperse (Char
',' Char -> ShowS
forall a. a -> [a] -> [a]
:) ((ShowS -> ShowS) -> [ShowS] -> [ShowS]
forall a b. (a -> b) -> [a] -> [b]
map ((String
"0x" String -> ShowS
forall a. [a] -> [a] -> [a]
++) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.) (Bytes p -> [ShowS]
forall mr. MemRead mr => mr -> [ShowS]
showsHexMem Bytes p
b))
instance Typeable p => IsList (Bytes p) where
type Item (Bytes p) = Word8
fromList :: [Item (Bytes p)] -> Bytes p
fromList = [Item (Bytes p)] -> Bytes p
forall e (ma :: * -> *).
(Prim e, MemAlloc ma) =>
[e] -> FrozenMem ma
fromListMem
{-# INLINE fromList #-}
fromListN :: Int -> [Item (Bytes p)] -> Bytes p
fromListN Int
n = Count Word8 -> [Word8] -> FrozenMem (MBytes p)
forall e (ma :: * -> *).
(Prim e, MemAlloc ma) =>
Count e -> [e] -> FrozenMem ma
fromListZeroMemN_ (Int -> Count Word8
forall e. Int -> Count e
Count Int
n)
{-# INLINE fromListN #-}
toList :: Bytes p -> [Item (Bytes p)]
toList = Bytes p -> [Item (Bytes p)]
forall e mr. (MemRead mr, Prim e) => mr -> [e]
toListMem
{-# INLINE toList #-}
instance Eq (Bytes p) where
Bytes p
b1 == :: Bytes p -> Bytes p -> Bool
== Bytes p
b2 = Bytes p -> Bytes p -> Bool
forall (p1 :: Pinned) (p2 :: Pinned). Bytes p1 -> Bytes p2 -> Bool
isSameBytes Bytes p
b1 Bytes p
b2 Bool -> Bool -> Bool
|| Bytes p -> Bytes p -> Bool
forall mr1 mr2. (MemRead mr1, MemRead mr2) => mr1 -> mr2 -> Bool
eqByteMem Bytes p
b1 Bytes p
b2
{-# INLINE (==) #-}
instance Ord (Bytes p) where
compare :: Bytes p -> Bytes p -> Ordering
compare Bytes p
b1 Bytes p
b2 =
Count Word8 -> Count Word8 -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Count Word8
n (Bytes p -> Count Word8
forall (p :: Pinned). Bytes p -> Count Word8
byteCountBytes Bytes p
b2) Ordering -> Ordering -> Ordering
forall a. Semigroup a => a -> a -> a
<> Bytes p
-> Off Word8 -> Bytes p -> Off Word8 -> Count Word8 -> Ordering
forall e (p1 :: Pinned) (p2 :: Pinned).
Prim e =>
Bytes p1
-> Off Word8 -> Bytes p2 -> Off Word8 -> Count e -> Ordering
compareByteOffBytes Bytes p
b1 Off Word8
0 Bytes p
b2 Off Word8
0 Count Word8
n
where
n :: Count Word8
n = Bytes p -> Count Word8
forall (p :: Pinned). Bytes p -> Count Word8
byteCountBytes Bytes p
b1
{-# INLINE compare #-}
instance Typeable p => Semigroup.Semigroup (Bytes p) where
<> :: Bytes p -> Bytes p -> Bytes p
(<>) = Bytes p -> Bytes p -> Bytes p
forall mr1 mr2 (ma :: * -> *).
(MemRead mr1, MemRead mr2, MemAlloc ma) =>
mr1 -> mr2 -> FrozenMem ma
appendMem
{-# INLINE (<>) #-}
sconcat :: NonEmpty (Bytes p) -> Bytes p
sconcat (Bytes p
x :| [Bytes p]
xs) = [Bytes p] -> FrozenMem (MBytes p)
forall mr (ma :: * -> *).
(MemRead mr, MemAlloc ma) =>
[mr] -> FrozenMem ma
concatMem (Bytes p
xBytes p -> [Bytes p] -> [Bytes p]
forall a. a -> [a] -> [a]
:[Bytes p]
xs)
{-# INLINE sconcat #-}
stimes :: b -> Bytes p -> Bytes p
stimes b
i = Int -> Bytes p -> FrozenMem (MBytes p)
forall (ma :: * -> *) mr.
(MemAlloc ma, MemRead mr) =>
Int -> mr -> FrozenMem ma
cycleMemN (b -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral b
i)
{-# INLINE stimes #-}
instance Typeable p => Monoid.Monoid (Bytes p) where
mappend :: Bytes p -> Bytes p -> Bytes p
mappend = Bytes p -> Bytes p -> Bytes p
forall mr1 mr2 (ma :: * -> *).
(MemRead mr1, MemRead mr2, MemAlloc ma) =>
mr1 -> mr2 -> FrozenMem ma
appendMem
{-# INLINE mappend #-}
mconcat :: [Bytes p] -> Bytes p
mconcat = [Bytes p] -> Bytes p
forall mr (ma :: * -> *).
(MemRead mr, MemAlloc ma) =>
[mr] -> FrozenMem ma
concatMem
{-# INLINE mconcat #-}
mempty :: Bytes p
mempty = Bytes p
forall (ma :: * -> *). MemAlloc ma => FrozenMem ma
emptyMem
{-# INLINE mempty #-}
showsHexMem :: MemRead mr => mr -> [ShowS]
showsHexMem :: mr -> [ShowS]
showsHexMem mr
b = (Word8 -> ShowS) -> [Word8] -> [ShowS]
forall a b. (a -> b) -> [a] -> [b]
map Word8 -> ShowS
forall a. (Integral a, Show a) => a -> ShowS
toHex (mr -> [Word8]
forall e mr. (MemRead mr, Prim e) => mr -> [e]
toListMem mr
b :: [Word8])
where
toHex :: a -> ShowS
toHex a
b8 =
(if a
b8 a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
0x0f
then (Char
'0' Char -> ShowS
forall a. a -> [a] -> [a]
:)
else ShowS
forall a. a -> a
id) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
a -> ShowS
forall a. (Integral a, Show a) => a -> ShowS
showHex a
b8
withScrubbedMutMem ::
forall e ma m a.
(MonadUnliftPrim RW m, Prim e, MemAlloc ma, PtrAccess RW (ma RW))
=> Count e
-> (ma RW -> m a)
-> m a
withScrubbedMutMem :: Count e -> (ma RW -> m a) -> m a
withScrubbedMutMem Count e
c ma RW -> m a
f = do
ma RW
mem <- Count e -> m (ma RW)
forall e (ma :: * -> *) (m :: * -> *) s.
(MemAlloc ma, MonadPrim s m, Prim e) =>
Count e -> m (ma s)
allocZeroMutMem Count e
c
let _fptr :: IO (ForeignPtr e)
_fptr = ma RW -> IO (ForeignPtr e)
forall s p (m :: * -> *) a.
(PtrAccess s p, MonadPrim s m) =>
p -> m (ForeignPtr a)
toForeignPtr ma RW
mem :: IO (ForeignPtr e)
ma RW -> m a
f ma RW
mem m a -> m () -> m a
forall (m :: * -> *) a b. MonadUnliftPrim RW m => m a -> m b -> m a
`ufinally` ma RW -> Off Word8 -> Count Word8 -> Word8 -> m ()
forall (mw :: * -> *) s (m :: * -> *) e.
(MemWrite mw, MonadPrim s m, Prim e) =>
mw s -> Off e -> Count e -> e -> m ()
setMutMem ma RW
mem Off Word8
0 (Count e -> Count Word8
forall e. Prim e => Count e -> Count Word8
toByteCount Count e
c) Word8
0
{-# INLINE withScrubbedMutMem #-}