{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UnboxedTuples #-}
-- |
-- Module      : Data.Prim.Memory.Bytes
-- Copyright   : (c) Alexey Kuleshevich 2020
-- License     : BSD3
-- Maintainer  : Alexey Kuleshevich <alexey@kuleshevi.ch>
-- Stability   : experimental
-- Portability : non-portable
--
module Data.Prim.Memory.Bytes
  ( module Data.Prim
    -- * Mutable
  , Bytes
  , toByteArray#
  , fromByteArray#
  , cloneBytes
  , emptyBytes
  , eqBytes
  , singletonBytes
  , isEmptyBytes
  , createBytes
  , createBytes_
  , createBytesST
  , createBytesST_
  -- * Pinness
  , Pinned(..)
  , isPinnedBytes
  , isPinnedMBytes
  , toPinnedBytes
  , toPinnedMBytes
  , toInconclusiveBytes
  , toInconclusiveMBytes
  , relaxPinnedBytes
  , relaxPinnedMBytes
  , ensurePinnedBytes
  , ensurePinnedMBytes
  -- * Mutable
  , MBytes
  , toMutableByteArray#
  , fromMutableByteArray#
  , isSameBytes
  , isSamePinnedBytes
  , isSameMBytes
  , indexOffBytes
  , indexByteOffBytes
  , byteCountBytes
  , countBytes
  , countRemBytes
  , compareBytes
  , compareByteOffBytes
  -- * Mutable
  -- ** To/From immutable
  , thawBytes
  , freezeMBytes
  -- ** Construction
  , allocMBytes
  , singletonMBytes
  , allocPinnedMBytes
  , allocAlignedMBytes
  , allocUnpinnedMBytes
  , allocZeroMBytes
  , allocZeroPinnedMBytes
  , allocZeroAlignedMBytes
  , shrinkMBytes
  , resizeMBytes
  , reallocMBytes
  , coerceStateMBytes
  -- ** Modifying data
  , cloneMBytes
  , withCloneMBytes
  , withCloneMBytes_
  , withCloneMBytesST
  , withCloneMBytesST_
  , loadListMBytes
  , loadListMBytes_
  , copyBytesToMBytes
  , moveMBytesToMBytes
  -- ** Moving data
  -- * Size
  , getByteCountMBytes
  , getCountMBytes
  , getCountRemOfMBytes
  -- * Access
  , readOffMBytes
  , readByteOffMBytes
  , writeOffMBytes
  , writeByteOffMBytes
  , setMBytes
  , zeroMBytes
  -- ** Ptr
  , withPtrBytes
  , withNoHaltPtrBytes
  , withPtrMBytes
  , withNoHaltPtrMBytes
  , toPtrBytes
  , toPtrMBytes
  , toForeignPtrBytes
  , toForeignPtrMBytes
  -- * Conversion
  , toUArrayBytes
  , fromUArrayBytes
  , toUMArrayMBytes
  , fromUMArrayMBytes
  , fromListBytes
  , fromListBytesN
  , fromListZeroBytesN_
  , appendBytes
  , concatBytes
  , toListBytes
  , toListSlackBytes
  -- * Atomic
  , casMBytes
  , casBoolMBytes
  , casBoolFetchMBytes
  , atomicReadMBytes
  , atomicWriteMBytes
  , atomicModifyMBytes
  , atomicModifyMBytes_
  , atomicBoolModifyFetchOldMBytes
  , atomicModifyFetchOldMBytes
  , atomicModifyFetchNewMBytes
  -- ** Numberic
  , atomicAddFetchOldMBytes
  , atomicAddFetchNewMBytes
  , atomicSubFetchOldMBytes
  , atomicSubFetchNewMBytes
  -- ** Binary
  , atomicAndFetchOldMBytes
  , atomicAndFetchNewMBytes
  , atomicNandFetchOldMBytes
  , atomicNandFetchNewMBytes
  , atomicOrFetchOldMBytes
  , atomicOrFetchNewMBytes
  , atomicXorFetchOldMBytes
  , atomicXorFetchNewMBytes
  , atomicNotFetchOldMBytes
  , atomicNotFetchNewMBytes
  -- * Prefetch
  , prefetchBytes0
  , prefetchMBytes0
  , prefetchBytes1
  , prefetchMBytes1
  , prefetchBytes2
  , prefetchMBytes2
  , prefetchBytes3
  , prefetchMBytes3
  -- * Helpers
  ) where

import Control.Monad.ST
import Control.Prim.Monad
import Data.Maybe (fromMaybe)
import Data.Prim
import Data.Prim.Atomic
import Data.Prim.Memory.Internal
import Foreign.Prim

eqBytes :: Bytes p1 -> Bytes p2 -> Bool
eqBytes :: Bytes p1 -> Bytes p2 -> Bool
eqBytes Bytes p1
b1 Bytes p2
b2 = Bytes p1 -> Bytes p2 -> Bool
forall (p1 :: Pinned) (p2 :: Pinned). Bytes p1 -> Bytes p2 -> Bool
isSameBytes Bytes p1
b1 Bytes p2
b2 Bool -> Bool -> Bool
|| Bytes p1 -> Bytes p2 -> Bool
forall mr1 mr2. (MemRead mr1, MemRead mr2) => mr1 -> mr2 -> Bool
eqByteMem Bytes p1
b1 Bytes p2
b2
{-# INLINE eqBytes #-}

---- Pure

compareBytes :: Prim e => Bytes p1 -> Off e -> Bytes p2 -> Off e -> Count e -> Ordering
compareBytes :: Bytes p1 -> Off e -> Bytes p2 -> Off e -> Count e -> Ordering
compareBytes (Bytes ByteArray#
b1#) Off e
off1 (Bytes ByteArray#
b2#) Off e
off2 Count e
c =
  Int# -> Ordering
toOrdering# (ByteArray# -> Int# -> ByteArray# -> Int# -> Int# -> Int#
compareByteArrays# ByteArray#
b1# (Off e -> Int#
forall e. Prim e => Off e -> Int#
unOffBytes# Off e
off1) ByteArray#
b2# (Off e -> Int#
forall e. Prim e => Off e -> Int#
unOffBytes# Off e
off2) (Count e -> Int#
forall e. Prim e => Count e -> Int#
unCountBytes# Count e
c))
{-# INLINE compareBytes #-}


-- | This function allows the change of state token. Use with care, because it can allow
-- mutation to escape the `ST` monad.
coerceStateMBytes :: MBytes p s' -> MBytes p s
coerceStateMBytes :: MBytes p s' -> MBytes p s
coerceStateMBytes = MBytes p s' -> MBytes p s
unsafeCoerce#


emptyBytes :: Bytes p
emptyBytes :: Bytes p
emptyBytes = Bytes 'Pin -> Bytes p
forall (p' :: Pinned) (p :: Pinned). Bytes p' -> Bytes p
castPinnedBytes (Bytes 'Pin -> Bytes p) -> Bytes 'Pin -> Bytes p
forall a b. (a -> b) -> a -> b
$ (forall s. ST s (Bytes 'Pin)) -> Bytes 'Pin
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (Bytes 'Pin)) -> Bytes 'Pin)
-> (forall s. ST s (Bytes 'Pin)) -> Bytes 'Pin
forall a b. (a -> b) -> a -> b
$ Count Word8 -> ST s (MBytes 'Pin s)
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
Count e -> m (MBytes 'Pin s)
allocPinnedMBytes (Count Word8
0 :: Count Word8) ST s (MBytes 'Pin s)
-> (MBytes 'Pin s -> ST s (Bytes 'Pin)) -> ST s (Bytes 'Pin)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= MBytes 'Pin s -> ST s (Bytes 'Pin)
forall s (m :: * -> *) (p :: Pinned).
MonadPrim s m =>
MBytes p s -> m (Bytes p)
freezeMBytes
{-# INLINE emptyBytes #-}

isEmptyBytes :: Bytes p -> Bool
isEmptyBytes :: Bytes p -> Bool
isEmptyBytes Bytes p
b = Bytes p -> Count Word8
forall (p :: Pinned). Bytes p -> Count Word8
byteCountBytes Bytes p
b Count Word8 -> Count Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Count Word8
0
{-# INLINE isEmptyBytes #-}

singletonBytes :: forall e p. (Prim e, Typeable p) => e -> Bytes p
singletonBytes :: e -> Bytes p
singletonBytes e
a = (forall s. ST s (Bytes p)) -> Bytes p
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (Bytes p)) -> Bytes p)
-> (forall s. ST s (Bytes p)) -> Bytes p
forall a b. (a -> b) -> a -> b
$ e -> ST s (MBytes p s)
forall e (p :: Pinned) (m :: * -> *) s.
(Prim e, Typeable p, MonadPrim s m) =>
e -> m (MBytes p s)
singletonMBytes e
a ST s (MBytes p s)
-> (MBytes p s -> ST s (Bytes p)) -> ST s (Bytes p)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= MBytes p s -> ST s (Bytes p)
forall s (m :: * -> *) (p :: Pinned).
MonadPrim s m =>
MBytes p s -> m (Bytes p)
freezeMBytes
{-# INLINE singletonBytes #-}

---- Mutable

singletonMBytes :: forall e p m s. (Prim e, Typeable p, MonadPrim s m) => e -> m (MBytes p s)
singletonMBytes :: e -> m (MBytes p s)
singletonMBytes e
a = do
  MBytes p s
mb <- 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 (Count e
1 :: Count e)
  MBytes p s
mb MBytes p s -> m () -> m (MBytes p s)
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ 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 MBytes p s
mb Off e
0 e
a
{-# INLINE singletonMBytes #-}

cloneBytes :: Typeable p => Bytes p -> Bytes p
cloneBytes :: Bytes p -> Bytes p
cloneBytes Bytes p
b = (forall s. ST s (Bytes p)) -> Bytes p
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (Bytes p)) -> Bytes p)
-> (forall s. ST s (Bytes p)) -> Bytes p
forall a b. (a -> b) -> a -> b
$ Bytes p -> ST s (MBytes p s)
forall s (m :: * -> *) (p :: Pinned).
MonadPrim s m =>
Bytes p -> m (MBytes p s)
thawBytes Bytes p
b ST s (MBytes p s)
-> (MBytes p s -> ST s (MBytes p s)) -> ST s (MBytes p s)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= MBytes p s -> ST s (MBytes p s)
forall s (m :: * -> *) (p :: Pinned).
(MonadPrim s m, Typeable p) =>
MBytes p s -> m (MBytes p s)
cloneMBytes ST s (MBytes p s)
-> (MBytes p s -> ST s (Bytes p)) -> ST s (Bytes p)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= MBytes p s -> ST s (Bytes p)
forall s (m :: * -> *) (p :: Pinned).
MonadPrim s m =>
MBytes p s -> m (Bytes p)
freezeMBytes
{-# INLINE cloneBytes #-}

cloneMBytes :: (MonadPrim s m, Typeable p) => MBytes p s -> m (MBytes p s)
cloneMBytes :: MBytes p s -> m (MBytes p s)
cloneMBytes MBytes p s
mb = do
  Count Word8
n <- MBytes p s -> m (Count Word8)
forall s (m :: * -> *) e (p :: Pinned).
(MonadPrim s m, Prim e) =>
MBytes p s -> m (Count e)
getCountMBytes MBytes p s
mb
  MBytes p s
mb' <- Count Word8 -> m (MBytes p s)
forall (p :: Pinned) e s (m :: * -> *).
(Typeable p, Prim e, MonadPrim s m) =>
Count e -> m (MBytes p s)
allocMBytes (Count Word8
n :: Count Word8)
  MBytes p s
mb' MBytes p s -> m () -> m (MBytes p s)
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ MBytes p s
-> Off Word8 -> MBytes p s -> Off Word8 -> Count Word8 -> m ()
forall s (m :: * -> *) e (ps :: Pinned) (pd :: Pinned).
(MonadPrim s m, Prim e) =>
MBytes ps s -> Off e -> MBytes pd s -> Off e -> Count e -> m ()
moveMBytesToMBytes MBytes p s
mb Off Word8
0 MBytes p s
mb' Off Word8
0 Count Word8
n
{-# INLINE cloneMBytes #-}


copyBytesToMBytes ::
     (MonadPrim s m, Prim e) => Bytes ps -> Off e -> MBytes pd s -> Off e -> Count e -> m ()
copyBytesToMBytes :: Bytes ps -> Off e -> MBytes pd s -> Off e -> Count e -> m ()
copyBytesToMBytes (Bytes ByteArray#
src#) Off e
srcOff (MBytes MutableByteArray# s
dst#) Off e
dstOff Count e
c =
  (State# s -> State# s) -> m ()
forall s (m :: * -> *).
MonadPrim s m =>
(State# s -> State# s) -> m ()
prim_ ((State# s -> State# s) -> m ()) -> (State# s -> State# s) -> m ()
forall a b. (a -> b) -> a -> b
$
  ByteArray#
-> Int#
-> MutableByteArray# s
-> Int#
-> Int#
-> State# s
-> State# s
forall d.
ByteArray#
-> Int#
-> MutableByteArray# d
-> Int#
-> Int#
-> State# d
-> State# d
copyByteArray# ByteArray#
src# (Off e -> Int#
forall e. Prim e => Off e -> Int#
unOffBytes# Off e
srcOff) MutableByteArray# s
dst# (Off e -> Int#
forall e. Prim e => Off e -> Int#
unOffBytes# Off e
dstOff) (Count e -> Int#
forall e. Prim e => Count e -> Int#
unCountBytes# Count e
c)
{-# INLINE copyBytesToMBytes #-}


moveMBytesToMBytes ::
     (MonadPrim s m, Prim e) => MBytes ps s-> Off e -> MBytes pd s -> Off e -> Count e -> m ()
moveMBytesToMBytes :: MBytes ps s -> Off e -> MBytes pd s -> Off e -> Count e -> m ()
moveMBytesToMBytes (MBytes MutableByteArray# s
src#) Off e
srcOff (MBytes MutableByteArray# s
dst#) Off e
dstOff Count e
c =
  (State# s -> State# s) -> m ()
forall s (m :: * -> *).
MonadPrim s m =>
(State# s -> State# s) -> m ()
prim_ (MutableByteArray# s
-> Int#
-> MutableByteArray# s
-> Int#
-> Int#
-> State# s
-> State# s
forall d.
MutableByteArray# d
-> Int#
-> MutableByteArray# d
-> Int#
-> Int#
-> State# d
-> State# d
copyMutableByteArray# MutableByteArray# s
src# (Off e -> Int#
forall e. Prim e => Off e -> Int#
unOffBytes# Off e
srcOff) MutableByteArray# s
dst# (Off e -> Int#
forall e. Prim e => Off e -> Int#
unOffBytes# Off e
dstOff) (Count e -> Int#
forall e. Prim e => Count e -> Int#
unCountBytes# Count e
c))
{-# INLINE moveMBytesToMBytes #-}

-- | Allocated memory is not cleared, so make sure to fill it in properly, otherwise you
-- might find some garbage there.
createBytes ::
     forall p e b s m. (Prim e, Typeable p, MonadPrim s m)
  => Count e
  -> (MBytes p s -> m b)
  -> m (b, Bytes p)
createBytes :: Count e -> (MBytes p s -> m b) -> m (b, Bytes p)
createBytes Count e
n MBytes p s -> m b
f = do
  MBytes p s
mb <- 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 Count e
n
  !b
res <- MBytes p s -> m b
f MBytes p s
mb
  (,) b
res (Bytes p -> (b, Bytes p)) -> m (Bytes p) -> m (b, Bytes p)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> MBytes p s -> m (Bytes p)
forall s (m :: * -> *) (p :: Pinned).
MonadPrim s m =>
MBytes p s -> m (Bytes p)
freezeMBytes MBytes p s
mb
{-# INLINE createBytes #-}

createBytes_ ::
     forall p e b s m. (Prim e, Typeable p, MonadPrim s m)
  => Count e
  -> (MBytes p s -> m b)
  -> m (Bytes p)
createBytes_ :: Count e -> (MBytes p s -> m b) -> m (Bytes p)
createBytes_ Count e
n MBytes p s -> m b
f = 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 Count e
n m (MBytes p s) -> (MBytes p s -> m (Bytes p)) -> m (Bytes p)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \MBytes p s
mb -> MBytes p s -> m b
f MBytes p s
mb m b -> m (Bytes p) -> m (Bytes p)
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> MBytes p s -> m (Bytes p)
forall s (m :: * -> *) (p :: Pinned).
MonadPrim s m =>
MBytes p s -> m (Bytes p)
freezeMBytes MBytes p s
mb
{-# INLINE createBytes_ #-}

createBytesST ::
     forall p e b. (Prim e, Typeable p)
  => Count e
  -> (forall s . MBytes p s -> ST s b)
  -> (b, Bytes p)
createBytesST :: Count e -> (forall s. MBytes p s -> ST s b) -> (b, Bytes p)
createBytesST Count e
n forall s. MBytes p s -> ST s b
f = (forall s. ST s (b, Bytes p)) -> (b, Bytes p)
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (b, Bytes p)) -> (b, Bytes p))
-> (forall s. ST s (b, Bytes p)) -> (b, Bytes p)
forall a b. (a -> b) -> a -> b
$ Count e -> (MBytes p s -> ST s b) -> ST s (b, Bytes p)
forall (p :: Pinned) e b s (m :: * -> *).
(Prim e, Typeable p, MonadPrim s m) =>
Count e -> (MBytes p s -> m b) -> m (b, Bytes p)
createBytes Count e
n MBytes p s -> ST s b
forall s. MBytes p s -> ST s b
f
{-# INLINE createBytesST #-}

createBytesST_ ::
     forall p e b. (Prim e, Typeable p)
  => Count e
  -> (forall s. MBytes p s -> ST s b)
  -> Bytes p
createBytesST_ :: Count e -> (forall s. MBytes p s -> ST s b) -> Bytes p
createBytesST_ Count e
n forall s. MBytes p s -> ST s b
f =  (forall s. ST s (Bytes p)) -> Bytes p
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (Bytes p)) -> Bytes p)
-> (forall s. ST s (Bytes p)) -> Bytes p
forall a b. (a -> b) -> a -> b
$ Count e -> (MBytes p s -> ST s b) -> ST s (Bytes p)
forall (p :: Pinned) e b s (m :: * -> *).
(Prim e, Typeable p, MonadPrim s m) =>
Count e -> (MBytes p s -> m b) -> m (Bytes p)
createBytes_ Count e
n MBytes p s -> ST s b
forall s. MBytes p s -> ST s b
f
{-# INLINE createBytesST_ #-}

allocZeroMBytes :: (MonadPrim s m, Prim e, Typeable p) => Count e -> m (MBytes p s)
allocZeroMBytes :: Count e -> m (MBytes p s)
allocZeroMBytes Count e
n = 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 Count e
n m (MBytes p s) -> (MBytes p s -> m (MBytes p s)) -> m (MBytes p s)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \MBytes p s
mb -> MBytes p s
mb MBytes p s -> m () -> m (MBytes p s)
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ MBytes p s -> Off Word8 -> Count Word8 -> Word8 -> m ()
forall s (m :: * -> *) e (p :: Pinned).
(MonadPrim s m, Prim e) =>
MBytes p s -> Off e -> Count e -> e -> m ()
setMBytes MBytes p s
mb Off Word8
0 (Count e -> Count Word8
forall e. Prim e => Count e -> Count Word8
toByteCount Count e
n) Word8
0
{-# INLINE allocZeroMBytes #-}



-- | Fill the mutable array with zeros efficiently.
zeroMBytes :: MonadPrim s m => MBytes p s -> m ()
zeroMBytes :: MBytes p s -> m ()
zeroMBytes mba :: MBytes p s
mba@(MBytes MutableByteArray# s
mba#) = do
  Count (I# Int#
n#) <- MBytes p s -> m (Count Word8)
forall s (m :: * -> *) (p :: Pinned).
MonadPrim s m =>
MBytes p s -> m (Count Word8)
getByteCountMBytes MBytes p s
mba
  (State# s -> State# s) -> m ()
forall s (m :: * -> *).
MonadPrim s m =>
(State# s -> State# s) -> m ()
prim_ (MutableByteArray# s -> Int# -> Int# -> Int# -> State# s -> State# s
forall d.
MutableByteArray# d -> Int# -> Int# -> Int# -> State# d -> State# d
setByteArray# MutableByteArray# s
mba# Int#
0# Int#
n# Int#
0#)
{-# INLINE zeroMBytes #-}


withCloneMBytes ::
     (MonadPrim s m, Typeable p)
  => Bytes p
  -> (MBytes p s -> m a)
  -> m (a, Bytes p)
withCloneMBytes :: Bytes p -> (MBytes p s -> m a) -> m (a, Bytes p)
withCloneMBytes Bytes p
b MBytes p s -> m a
f = do
  MBytes p s
mb <- MBytes p s -> m (MBytes p s)
forall s (m :: * -> *) (p :: Pinned).
(MonadPrim s m, Typeable p) =>
MBytes p s -> m (MBytes p s)
cloneMBytes (MBytes p s -> m (MBytes p s)) -> m (MBytes p s) -> m (MBytes p s)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Bytes p -> m (MBytes p s)
forall s (m :: * -> *) (p :: Pinned).
MonadPrim s m =>
Bytes p -> m (MBytes p s)
thawBytes Bytes p
b
  !a
res <- MBytes p s -> m a
f MBytes p s
mb
  Bytes p
b' <- MBytes p s -> m (Bytes p)
forall s (m :: * -> *) (p :: Pinned).
MonadPrim s m =>
MBytes p s -> m (Bytes p)
freezeMBytes MBytes p s
mb
  (a, Bytes p) -> m (a, Bytes p)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a
res, Bytes p
b')
{-# INLINE withCloneMBytes #-}

withCloneMBytes_ ::
  (MonadPrim s m, Typeable p)
  => Bytes p
  -> (MBytes p s -> m a)
  -> m (Bytes p)
withCloneMBytes_ :: Bytes p -> (MBytes p s -> m a) -> m (Bytes p)
withCloneMBytes_ Bytes p
b MBytes p s -> m a
f = Bytes p -> m (MBytes p s)
forall s (m :: * -> *) (p :: Pinned).
MonadPrim s m =>
Bytes p -> m (MBytes p s)
thawBytes Bytes p
b m (MBytes p s) -> (MBytes p s -> m (MBytes p s)) -> m (MBytes p s)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= MBytes p s -> m (MBytes p s)
forall s (m :: * -> *) (p :: Pinned).
(MonadPrim s m, Typeable p) =>
MBytes p s -> m (MBytes p s)
cloneMBytes m (MBytes p s) -> (MBytes p s -> m (Bytes p)) -> m (Bytes p)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \MBytes p s
mb -> MBytes p s -> m a
f MBytes p s
mb m a -> m (Bytes p) -> m (Bytes p)
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> MBytes p s -> m (Bytes p)
forall s (m :: * -> *) (p :: Pinned).
MonadPrim s m =>
MBytes p s -> m (Bytes p)
freezeMBytes MBytes p s
mb
{-# INLINE withCloneMBytes_ #-}

withCloneMBytesST ::
  Typeable p => Bytes p -> (forall s. MBytes p s -> ST s a) -> (a, Bytes p)
withCloneMBytesST :: Bytes p -> (forall s. MBytes p s -> ST s a) -> (a, Bytes p)
withCloneMBytesST Bytes p
b forall s. MBytes p s -> ST s a
f = (forall s. ST s (a, Bytes p)) -> (a, Bytes p)
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (a, Bytes p)) -> (a, Bytes p))
-> (forall s. ST s (a, Bytes p)) -> (a, Bytes p)
forall a b. (a -> b) -> a -> b
$ Bytes p -> (MBytes p s -> ST s a) -> ST s (a, Bytes p)
forall s (m :: * -> *) (p :: Pinned) a.
(MonadPrim s m, Typeable p) =>
Bytes p -> (MBytes p s -> m a) -> m (a, Bytes p)
withCloneMBytes Bytes p
b MBytes p s -> ST s a
forall s. MBytes p s -> ST s a
f
{-# INLINE withCloneMBytesST #-}

withCloneMBytesST_ ::
  Typeable p => Bytes p -> (forall s. MBytes p s -> ST s a) -> Bytes p
withCloneMBytesST_ :: Bytes p -> (forall s. MBytes p s -> ST s a) -> Bytes p
withCloneMBytesST_ Bytes p
b forall s. MBytes p s -> ST s a
f = (forall s. ST s (Bytes p)) -> Bytes p
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (Bytes p)) -> Bytes p)
-> (forall s. ST s (Bytes p)) -> Bytes p
forall a b. (a -> b) -> a -> b
$ Bytes p -> (MBytes p s -> ST s a) -> ST s (Bytes p)
forall s (m :: * -> *) (p :: Pinned) a.
(MonadPrim s m, Typeable p) =>
Bytes p -> (MBytes p s -> m a) -> m (Bytes p)
withCloneMBytes_ Bytes p
b MBytes p s -> ST s a
forall s. MBytes p s -> ST s a
f
{-# INLINE withCloneMBytesST_ #-}




-- | Get the count of elements of type @a@ that can fit into bytes as well as the slack
-- number of bytes that would be leftover in case when total number of bytes available is
-- not exactly divisable by the size of the element that will be stored in the memory
-- chunk.
countRemBytes :: forall e p. Prim e => Bytes p -> (Count e, Count Word8)
countRemBytes :: Bytes p -> (Count e, Count Word8)
countRemBytes = Count Word8 -> (Count e, Count Word8)
forall e. Prim e => Count Word8 -> (Count e, Count Word8)
fromByteCountRem (Count Word8 -> (Count e, Count Word8))
-> (Bytes p -> Count Word8) -> Bytes p -> (Count e, Count Word8)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bytes p -> Count Word8
forall (p :: Pinned). Bytes p -> Count Word8
byteCountBytes
{-# INLINE countRemBytes #-}



-- | Get the number of elements of type @a@ that can fit into bytes as well as the slack
-- number of bytes that would be leftover in case when total number of bytes available is
-- not exactly divisable by the size of the element that will be stored in the memory
-- chunk.
getCountRemOfMBytes ::
     forall e p s m. (MonadPrim s m, Prim e)
  => MBytes p s
  -> m (Count e, Count Word8)
getCountRemOfMBytes :: MBytes p s -> m (Count e, Count Word8)
getCountRemOfMBytes MBytes p s
b = Count Word8 -> (Count e, Count Word8)
forall e. Prim e => Count Word8 -> (Count e, Count Word8)
fromByteCountRem (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
<$> MBytes p s -> m (Count Word8)
forall s (m :: * -> *) (p :: Pinned).
MonadPrim s m =>
MBytes p s -> m (Count Word8)
getByteCountMBytes MBytes p s
b
{-# INLINE getCountRemOfMBytes #-}

-- | It is only guaranteed to convert the whole memory to a list whenever the size of
-- allocated memory is exactly divisible by the size of the element, otherwise there will
-- be some slack left unaccounted for.
toListBytes :: Prim e => Bytes p -> [e]
toListBytes :: Bytes p -> [e]
toListBytes = Bytes p -> [e]
forall e mr. (MemRead mr, Prim e) => mr -> [e]
toListMem
{-# INLINE toListBytes #-}

toListSlackBytes :: Prim e => Bytes p -> ([e], [Word8])
toListSlackBytes :: Bytes p -> ([e], [Word8])
toListSlackBytes = Bytes p -> ([e], [Word8])
forall e mr. (MemRead mr, Prim e) => mr -> ([e], [Word8])
toListSlackMem
{-# INLINE toListSlackBytes #-}

-- | Same as `loadListMutMem`
loadListMBytes :: (Prim e, Typeable p, MonadPrim s m) => [e] -> MBytes p s -> m ([e], Count e)
loadListMBytes :: [e] -> MBytes p s -> m ([e], Count e)
loadListMBytes = [e] -> MBytes p s -> m ([e], Count e)
forall e (ma :: * -> *) (m :: * -> *) s.
(Prim e, MemAlloc ma, MonadPrim s m) =>
[e] -> ma s -> m ([e], Count e)
loadListMutMem
{-# INLINE loadListMBytes #-}

-- | Same as `loadListMutMem_`
loadListMBytes_ :: (Prim e, Typeable p, MonadPrim s m) => [e] -> MBytes p s -> m ()
loadListMBytes_ :: [e] -> MBytes p s -> m ()
loadListMBytes_ = [e] -> MBytes p s -> m ()
forall e (ma :: * -> *) (m :: * -> *) s.
(Prim e, MemAlloc ma, MonadPrim s m) =>
[e] -> ma s -> m ()
loadListMutMem_
{-# INLINE loadListMBytes_ #-}

-- | Same as `fromListZeroMemN_`
--
-- @since 0.3.0
fromListZeroBytesN_ :: (Prim e, Typeable p) => Count e -> [e] -> Bytes p
fromListZeroBytesN_ :: Count e -> [e] -> Bytes p
fromListZeroBytesN_ = Count e -> [e] -> Bytes p
forall e (ma :: * -> *).
(Prim e, MemAlloc ma) =>
Count e -> [e] -> FrozenMem ma
fromListZeroMemN_
{-# INLINE fromListZeroBytesN_ #-}

-- | Exactly like `fromListMemN`, but restricted to `Bytes`.
fromListBytesN ::
     (Prim e, Typeable p)
  => Count e
  -> [e]
  -> (Either [e] (Count e), Bytes p)
fromListBytesN :: Count e -> [e] -> (Either [e] (Count e), Bytes p)
fromListBytesN = Count e -> [e] -> (Either [e] (Count e), Bytes p)
forall e (ma :: * -> *).
(Prim e, MemAlloc ma) =>
Count e -> [e] -> (Either [e] (Count e), FrozenMem ma)
fromListMemN
{-# INLINE fromListBytesN #-}

fromListBytes ::
     forall e p. (Prim e, Typeable p)
  => [e]
  -> Bytes p
fromListBytes :: [e] -> Bytes p
fromListBytes = [e] -> Bytes p
forall e (ma :: * -> *).
(Prim e, MemAlloc ma) =>
[e] -> FrozenMem ma
fromListMem
{-# INLINE fromListBytes #-}

-- | Allocate new memory region and append second bytes region after the first one
appendBytes ::
     Typeable p
  => Bytes p1 -- ^ First memory region
  -> Bytes p2 -- ^ Second memory region
  -> Bytes p
appendBytes :: Bytes p1 -> Bytes p2 -> Bytes p
appendBytes = Bytes p1 -> Bytes p2 -> Bytes p
forall mr1 mr2 (ma :: * -> *).
(MemRead mr1, MemRead mr2, MemAlloc ma) =>
mr1 -> mr2 -> FrozenMem ma
appendMem
{-# INLINE appendBytes #-}


concatBytes :: Typeable p => [Bytes p'] -> Bytes p
concatBytes :: [Bytes p'] -> Bytes p
concatBytes = [Bytes p'] -> Bytes p
forall mr (ma :: * -> *).
(MemRead mr, MemAlloc ma) =>
[mr] -> FrozenMem ma
concatMem
{-# INLINE concatBytes #-}


ensurePinnedBytes :: Bytes p -> Bytes 'Pin
ensurePinnedBytes :: Bytes p -> Bytes 'Pin
ensurePinnedBytes Bytes p
b = Bytes 'Pin -> Maybe (Bytes 'Pin) -> Bytes 'Pin
forall a. a -> Maybe a -> a
fromMaybe (Bytes p -> FrozenMem (MBytes 'Pin)
forall mr (ma :: * -> *).
(MemRead mr, MemAlloc ma) =>
mr -> FrozenMem ma
convertMem Bytes p
b) (Bytes p -> Maybe (Bytes 'Pin)
forall (p :: Pinned). Bytes p -> Maybe (Bytes 'Pin)
toPinnedBytes Bytes p
b)
{-# INLINE ensurePinnedBytes #-}

ensurePinnedMBytes :: MonadPrim s m => MBytes p s -> m (MBytes 'Pin s)
ensurePinnedMBytes :: MBytes p s -> m (MBytes 'Pin s)
ensurePinnedMBytes MBytes p s
mb =
  case MBytes p s -> Maybe (MBytes 'Pin s)
forall (p :: Pinned) s. MBytes p s -> Maybe (MBytes 'Pin s)
toPinnedMBytes MBytes p s
mb of
    Just MBytes 'Pin s
pmb -> MBytes 'Pin s -> m (MBytes 'Pin s)
forall (f :: * -> *) a. Applicative f => a -> f a
pure MBytes 'Pin s
pmb
    Maybe (MBytes 'Pin s)
Nothing  -> do
      Count Word8
n8 :: Count Word8 <- MBytes p s -> m (Count Word8)
forall s (m :: * -> *) e (p :: Pinned).
(MonadPrim s m, Prim e) =>
MBytes p s -> m (Count e)
getCountMBytes MBytes p s
mb
      MBytes 'Pin s
pmb <- Count Word8 -> m (MBytes 'Pin s)
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
Count e -> m (MBytes 'Pin s)
allocPinnedMBytes Count Word8
n8
      MBytes 'Pin s
pmb MBytes 'Pin s -> m () -> m (MBytes 'Pin s)
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ MBytes p s
-> Off Word8 -> MBytes 'Pin s -> Off Word8 -> Count Word8 -> m ()
forall s (m :: * -> *) e (ps :: Pinned) (pd :: Pinned).
(MonadPrim s m, Prim e) =>
MBytes ps s -> Off e -> MBytes pd s -> Off e -> Count e -> m ()
moveMBytesToMBytes MBytes p s
mb Off Word8
0 MBytes 'Pin s
pmb Off Word8
0 Count Word8
n8
{-# INLINE ensurePinnedMBytes #-}

toPinnedBytes :: Bytes p -> Maybe (Bytes 'Pin)
toPinnedBytes :: Bytes p -> Maybe (Bytes 'Pin)
toPinnedBytes (Bytes ByteArray#
b#)
  | Int# -> Bool
isTrue# (ByteArray# -> Int#
isByteArrayPinned# ByteArray#
b#) = Bytes 'Pin -> Maybe (Bytes 'Pin)
forall k1. k1 -> Maybe k1
Just (ByteArray# -> Bytes 'Pin
forall (p :: Pinned). ByteArray# -> Bytes p
Bytes ByteArray#
b#)
  | Bool
otherwise = Maybe (Bytes 'Pin)
forall k1. Maybe k1
Nothing
{-# INLINE toPinnedBytes #-}

toPinnedMBytes :: MBytes p s -> Maybe (MBytes 'Pin s)
toPinnedMBytes :: MBytes p s -> Maybe (MBytes 'Pin s)
toPinnedMBytes (MBytes MutableByteArray# s
mb#)
  | Int# -> Bool
isTrue# (MutableByteArray# s -> Int#
forall d. MutableByteArray# d -> Int#
isMutableByteArrayPinned# MutableByteArray# s
mb#) = MBytes 'Pin s -> Maybe (MBytes 'Pin s)
forall k1. k1 -> Maybe k1
Just (MutableByteArray# s -> MBytes 'Pin s
forall (p :: Pinned) s. MutableByteArray# s -> MBytes p s
MBytes MutableByteArray# s
mb#)
  | Bool
otherwise = Maybe (MBytes 'Pin s)
forall k1. Maybe k1
Nothing
{-# INLINE toPinnedMBytes #-}



-- | Perform atomic modification of an element in the `MBytes` at the supplied
-- index. Returns the actual value.  Offset is in number of elements,
-- rather than bytes. Implies a full memory barrier.
--
-- /Note/ - Bounds are not checked, therefore this function is unsafe.
--
-- @since 0.1.0
casMBytes ::
     (MonadPrim s m, Atomic e)
  => MBytes p s -- ^ Array to be mutated
  -> Off e -- ^ Index is in elements of @__a__@, rather than bytes.
  -> e -- ^ Expected old value
  -> e -- ^ New value
  -> m e
casMBytes :: MBytes p s -> Off e -> e -> e -> m e
casMBytes (MBytes MutableByteArray# s
mba#) (Off (I# Int#
i#)) e
expected e
new =
  (State# s -> (# State# s, e #)) -> m e
forall s (m :: * -> *) a.
MonadPrim s m =>
(State# s -> (# State# s, a #)) -> m a
prim ((State# s -> (# State# s, e #)) -> m e)
-> (State# s -> (# State# s, e #)) -> m e
forall a b. (a -> b) -> a -> b
$ MutableByteArray# s
-> Int# -> e -> e -> State# s -> (# State# s, e #)
forall a s.
Atomic a =>
MutableByteArray# s
-> Int# -> a -> a -> State# s -> (# State# s, a #)
casMutableByteArray# MutableByteArray# s
mba# Int#
i# e
expected e
new
{-# INLINE casMBytes #-}


-- | Perform atomic modification of an element in the `MBytes` at the supplied
-- index. Returns `True` if swap was successfull and false otherwise.  Offset is in number
-- of elements, rather than bytes. Implies a full memory barrier.
--
-- /Note/ - Bounds are not checked, therefore this function is unsafe.
--
-- @since 0.1.0
casBoolMBytes ::
     (MonadPrim s m, Atomic e)
  => MBytes p s -- ^ Array to be mutated
  -> Off e -- ^ Index is in elements of @__a__@, rather than bytes.
  -> e -- ^ Expected old value
  -> e -- ^ New value
  -> m Bool
casBoolMBytes :: MBytes p s -> Off e -> e -> e -> m Bool
casBoolMBytes (MBytes MutableByteArray# s
mba#) (Off (I# Int#
i#)) e
expected e
new =
  (State# s -> (# State# s, Bool #)) -> m Bool
forall s (m :: * -> *) a.
MonadPrim s m =>
(State# s -> (# State# s, a #)) -> m a
prim ((State# s -> (# State# s, Bool #)) -> m Bool)
-> (State# s -> (# State# s, Bool #)) -> m Bool
forall a b. (a -> b) -> a -> b
$ MutableByteArray# s
-> Int# -> e -> e -> State# s -> (# State# s, Bool #)
forall a s.
Atomic a =>
MutableByteArray# s
-> Int# -> a -> a -> State# s -> (# State# s, Bool #)
casBoolMutableByteArray# MutableByteArray# s
mba# Int#
i# e
expected e
new
{-# INLINE casBoolMBytes #-}

-- | Just like `casBoolMBytes`, but also returns the actual value, which will match the
-- supplied expected value if the returned flag is `True`
--
-- /Note/ - Bounds are not checked, therefore this function is unsafe.
--
-- @since 0.1.0
casBoolFetchMBytes ::
     (MonadPrim s m, Atomic e)
  => MBytes p s -- ^ Array to be mutated
  -> Off e -- ^ Index is in elements of @__a__@, rather than bytes.
  -> e -- ^ Expected old value
  -> e -- ^ New value
  -> m (Bool, e)
casBoolFetchMBytes :: MBytes p s -> Off e -> e -> e -> m (Bool, e)
casBoolFetchMBytes MBytes p s
mb Off e
off e
expected e
new = do
  Bool
isCasSucc <- MBytes p s -> Off e -> e -> e -> m Bool
forall s (m :: * -> *) e (p :: Pinned).
(MonadPrim s m, Atomic e) =>
MBytes p s -> Off e -> e -> e -> m Bool
casBoolMBytes MBytes p s
mb Off e
off e
expected e
new
  e
actual <-
    if Bool
isCasSucc
      then e -> m e
forall (f :: * -> *) a. Applicative f => a -> f a
pure e
new
      else 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 MBytes p s
mb Off e
off
  (Bool, e) -> m (Bool, e)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Bool
isCasSucc, e
actual)
{-# INLINE casBoolFetchMBytes #-}


-- | Perform atomic read of `MBytes` at the supplied index. Offset is in number of
-- elements, rather than bytes. Implies a full memory barrier.
--
-- /Note/ - Bounds are not checked, therefore this function is unsafe.
--
-- @since 0.1.0
atomicReadMBytes ::
     (MonadPrim s m, Atomic e)
  => MBytes p s -- ^ Array to be mutated
  -> Off e -- ^ Index is in elements of @__a__@, rather than bytes.
  -> m e
atomicReadMBytes :: MBytes p s -> Off e -> m e
atomicReadMBytes (MBytes MutableByteArray# s
mba#) (Off (I# Int#
i#)) =
  (State# s -> (# State# s, e #)) -> m e
forall s (m :: * -> *) a.
MonadPrim s m =>
(State# s -> (# State# s, a #)) -> m a
prim ((State# s -> (# State# s, e #)) -> m e)
-> (State# s -> (# State# s, e #)) -> m e
forall a b. (a -> b) -> a -> b
$ MutableByteArray# s -> Int# -> State# s -> (# State# s, e #)
forall a s.
Atomic a =>
MutableByteArray# s -> Int# -> State# s -> (# State# s, a #)
atomicReadMutableByteArray# MutableByteArray# s
mba# Int#
i#
{-# INLINE atomicReadMBytes #-}


-- | Perform a write into `MBytes` at the supplied index atomically. Offset is in number
-- of elements, rather than bytes. Implies a full memory barrier.
--
-- /Note/ - Bounds are not checked, therefore this function is unsafe.
--
-- @since 0.1.0
atomicWriteMBytes ::
     (MonadPrim s m, Atomic e)
  => MBytes p s -- ^ Array to be mutated
  -> Off e -- ^ Index is in elements of @__a__@, rather than bytes.
  -> e
  -> m ()
atomicWriteMBytes :: MBytes p s -> Off e -> e -> m ()
atomicWriteMBytes (MBytes MutableByteArray# s
mba#) (Off (I# Int#
i#)) e
e =
  (State# s -> State# s) -> m ()
forall s (m :: * -> *).
MonadPrim s m =>
(State# s -> State# s) -> m ()
prim_ ((State# s -> State# s) -> m ()) -> (State# s -> State# s) -> m ()
forall a b. (a -> b) -> a -> b
$ MutableByteArray# s -> Int# -> e -> State# s -> State# s
forall a s.
Atomic a =>
MutableByteArray# s -> Int# -> a -> State# s -> State# s
atomicWriteMutableByteArray# MutableByteArray# s
mba# Int#
i# e
e
{-# INLINE atomicWriteMBytes #-}


-- | Perform atomic modification of an element in the `MBytes` at the supplied
-- index. Returns the artifact of computation @__b__@.  Offset is in number of elements,
-- rather than bytes. Implies a full memory barrier.
--
-- /Note/ - Bounds are not checked, therefore this function is unsafe.
--
-- @since 0.1.0
atomicModifyMBytes ::
     (MonadPrim s m, Atomic e)
  => MBytes p s -- ^ Array to be mutated
  -> Off e -- ^ Index is in elements of @__a__@, rather than bytes.
  -> (e -> (e, b)) -- ^ Function that is applied to the old value and returns new value
                   -- and some artifact of computation @__b__@
  -> m b
atomicModifyMBytes :: MBytes p s -> Off e -> (e -> (e, b)) -> m b
atomicModifyMBytes (MBytes MutableByteArray# s
mba#) (Off (I# Int#
i#)) e -> (e, b)
f =
  (State# s -> (# State# s, b #)) -> m b
forall s (m :: * -> *) a.
MonadPrim s m =>
(State# s -> (# State# s, a #)) -> m a
prim ((State# s -> (# State# s, b #)) -> m b)
-> (State# s -> (# State# s, b #)) -> m b
forall a b. (a -> b) -> a -> b
$
  MutableByteArray# s
-> Int# -> (e -> (# e, b #)) -> State# s -> (# State# s, b #)
forall a s b.
Atomic a =>
MutableByteArray# s
-> Int# -> (a -> (# a, b #)) -> State# s -> (# State# s, b #)
atomicModifyMutableByteArray# MutableByteArray# s
mba# Int#
i# ((e -> (# e, b #)) -> State# s -> (# State# s, b #))
-> (e -> (# e, b #)) -> State# s -> (# State# s, b #)
forall a b. (a -> b) -> a -> b
$ \e
a ->
    case e -> (e, b)
f e
a of
      (e
a', b
b) -> (# e
a', b
b #)
{-# INLINE atomicModifyMBytes #-}

-- | Perform atomic modification of an element in the `MBytes` at the supplied
-- index.  Offset is in number of elements, rather than bytes. Implies a full memory
-- barrier.
--
-- /Note/ - Bounds are not checked, therefore this function is unsafe.
--
-- @since 0.1.0
atomicModifyMBytes_ ::
     (MonadPrim s m, Atomic e)
  => MBytes p s -- ^ Array to be mutated
  -> Off e -- ^ Index is in elements of @__a__@, rather than bytes.
  -> (e -> e) -- ^ Function that is applied to the old value and returns new value.
  -> m ()
atomicModifyMBytes_ :: MBytes p s -> Off e -> (e -> e) -> m ()
atomicModifyMBytes_ (MBytes MutableByteArray# s
mba#) (Off (I# Int#
i#)) e -> e
f =
  (State# s -> State# s) -> m ()
forall s (m :: * -> *).
MonadPrim s m =>
(State# s -> State# s) -> m ()
prim_ ((State# s -> State# s) -> m ()) -> (State# s -> State# s) -> m ()
forall a b. (a -> b) -> a -> b
$ MutableByteArray# s -> Int# -> (e -> e) -> State# s -> State# s
forall a s.
Atomic a =>
MutableByteArray# s -> Int# -> (a -> a) -> State# s -> State# s
atomicModifyMutableByteArray_# MutableByteArray# s
mba# Int#
i# e -> e
f
{-# INLINE atomicModifyMBytes_ #-}


-- | Perform atomic modification of an element in the `MBytes` at the supplied
-- index. Returns the previous value.  Offset is in number of elements, rather than
-- bytes. Implies a full memory barrier.
--
-- /Note/ - Bounds are not checked, therefore this function is unsafe.
--
-- @since 0.1.0
atomicModifyFetchOldMBytes ::
     (MonadPrim s m, Atomic e)
  => MBytes p s -- ^ Array to be mutated
  -> Off e -- ^ Index is in elements of @__a__@, rather than bytes.
  -> (e -> e) -- ^ Function that is applied to the old value and returns the new value
  -> m e
atomicModifyFetchOldMBytes :: MBytes p s -> Off e -> (e -> e) -> m e
atomicModifyFetchOldMBytes (MBytes MutableByteArray# s
mba#) (Off (I# Int#
i#)) e -> e
f =
  (State# s -> (# State# s, e #)) -> m e
forall s (m :: * -> *) a.
MonadPrim s m =>
(State# s -> (# State# s, a #)) -> m a
prim ((State# s -> (# State# s, e #)) -> m e)
-> (State# s -> (# State# s, e #)) -> m e
forall a b. (a -> b) -> a -> b
$ MutableByteArray# s
-> Int# -> (e -> e) -> State# s -> (# State# s, e #)
forall a s.
Atomic a =>
MutableByteArray# s
-> Int# -> (a -> a) -> State# s -> (# State# s, a #)
atomicModifyFetchOldMutableByteArray# MutableByteArray# s
mba# Int#
i# e -> e
f
{-# INLINE atomicModifyFetchOldMBytes #-}


-- | Perform atomic modification of an element in the `MBytes` at the supplied
-- index. Returns the previous value.  Offset is in number of elements, rather than
-- bytes. Implies a full memory barrier.
--
-- /Note/ - Bounds are not checked, therefore this function is unsafe.
--
-- @since 0.1.0
atomicBoolModifyFetchOldMBytes ::
     (MonadPrim s m, Atomic e)
  => MBytes p s -- ^ Array to be mutated
  -> Off e -- ^ Index is in elements of @__a__@, rather than bytes.
  -> (e -> e) -- ^ Function that is applied to the old value and returns the new value
  -> m e
atomicBoolModifyFetchOldMBytes :: MBytes p s -> Off e -> (e -> e) -> m e
atomicBoolModifyFetchOldMBytes (MBytes MutableByteArray# s
mba#) (Off (I# Int#
i#)) e -> e
f =
  (State# s -> (# State# s, e #)) -> m e
forall s (m :: * -> *) a.
MonadPrim s m =>
(State# s -> (# State# s, a #)) -> m a
prim ((State# s -> (# State# s, e #)) -> m e)
-> (State# s -> (# State# s, e #)) -> m e
forall a b. (a -> b) -> a -> b
$ MutableByteArray# s
-> Int# -> (e -> e) -> State# s -> (# State# s, e #)
forall a s.
Atomic a =>
MutableByteArray# s
-> Int# -> (a -> a) -> State# s -> (# State# s, a #)
atomicBoolModifyFetchOldMutableByteArray# MutableByteArray# s
mba# Int#
i# e -> e
f
{-# INLINE atomicBoolModifyFetchOldMBytes #-}


-- | Perform atomic modification of an element in the `MBytes` at the supplied
-- index.  Offset is in number of elements, rather than bytes. Implies a full memory
-- barrier.
--
-- /Note/ - Bounds are not checked, therefore this function is unsafe.
--
-- @since 0.1.0
atomicModifyFetchNewMBytes ::
     (MonadPrim s m, Atomic e)
  => MBytes p s -- ^ Array to be mutated
  -> Off e -- ^ Index is in elements of @__a__@, rather than bytes.
  -> (e -> e) -- ^ Function that is applied to the old value and returns the new value
  -> m e
atomicModifyFetchNewMBytes :: MBytes p s -> Off e -> (e -> e) -> m e
atomicModifyFetchNewMBytes (MBytes MutableByteArray# s
mba#) (Off (I# Int#
i#)) e -> e
f =
  (State# s -> (# State# s, e #)) -> m e
forall s (m :: * -> *) a.
MonadPrim s m =>
(State# s -> (# State# s, a #)) -> m a
prim ((State# s -> (# State# s, e #)) -> m e)
-> (State# s -> (# State# s, e #)) -> m e
forall a b. (a -> b) -> a -> b
$ MutableByteArray# s
-> Int# -> (e -> e) -> State# s -> (# State# s, e #)
forall a s.
Atomic a =>
MutableByteArray# s
-> Int# -> (a -> a) -> State# s -> (# State# s, a #)
atomicModifyFetchNewMutableByteArray# MutableByteArray# s
mba# Int#
i# e -> e
f
{-# INLINE atomicModifyFetchNewMBytes #-}






-- | Add a numeric value to an element of a `MBytes`, corresponds to @(`+`)@ done
-- atomically. Returns the previous value.  Offset is in number of elements, rather
-- than bytes. Implies a full memory barrier.
--
-- /Note/ - Bounds are not checked, therefore this function is unsafe.
--
-- @since 0.1.0
atomicAddFetchOldMBytes ::
     (MonadPrim s m, AtomicCount e)
  => MBytes p s
  -> Off e
  -> e
  -> m e
atomicAddFetchOldMBytes :: MBytes p s -> Off e -> e -> m e
atomicAddFetchOldMBytes (MBytes MutableByteArray# s
mba#) (Off (I# Int#
i#)) e
a =
  (State# s -> (# State# s, e #)) -> m e
forall s (m :: * -> *) a.
MonadPrim s m =>
(State# s -> (# State# s, a #)) -> m a
prim (MutableByteArray# s -> Int# -> e -> State# s -> (# State# s, e #)
forall a s.
AtomicCount a =>
MutableByteArray# s -> Int# -> a -> State# s -> (# State# s, a #)
atomicAddFetchOldMutableByteArray# MutableByteArray# s
mba# Int#
i# e
a)
{-# INLINE atomicAddFetchOldMBytes #-}

-- | Add a numeric value to an element of a `MBytes`, corresponds to @(`+`)@ done
-- atomically. Returns the new value.  Offset is in number of elements, rather
-- than bytes. Implies a full memory barrier.
--
-- /Note/ - Bounds are not checked, therefore this function is unsafe.
--
-- @since 0.1.0
atomicAddFetchNewMBytes ::
     (MonadPrim s m, AtomicCount e)
  => MBytes p s
  -> Off e
  -> e
  -> m e
atomicAddFetchNewMBytes :: MBytes p s -> Off e -> e -> m e
atomicAddFetchNewMBytes (MBytes MutableByteArray# s
mba#) (Off (I# Int#
i#)) e
a =
  (State# s -> (# State# s, e #)) -> m e
forall s (m :: * -> *) a.
MonadPrim s m =>
(State# s -> (# State# s, a #)) -> m a
prim (MutableByteArray# s -> Int# -> e -> State# s -> (# State# s, e #)
forall a s.
AtomicCount a =>
MutableByteArray# s -> Int# -> a -> State# s -> (# State# s, a #)
atomicAddFetchNewMutableByteArray# MutableByteArray# s
mba# Int#
i# e
a)
{-# INLINE atomicAddFetchNewMBytes #-}



-- | Subtract a numeric value from an element of a `MBytes`, corresponds to
-- @(`-`)@ done atomically. Returns the previous value.  Offset is in number of elements, rather
-- than bytes. Implies a full memory barrier.
--
-- /Note/ - Bounds are not checked, therefore this function is unsafe.
--
-- @since 0.1.0
atomicSubFetchOldMBytes ::
     (MonadPrim s m, AtomicCount e)
  => MBytes p s
  -> Off e
  -> e
  -> m e
atomicSubFetchOldMBytes :: MBytes p s -> Off e -> e -> m e
atomicSubFetchOldMBytes (MBytes MutableByteArray# s
mba#) (Off (I# Int#
i#)) e
a =
  (State# s -> (# State# s, e #)) -> m e
forall s (m :: * -> *) a.
MonadPrim s m =>
(State# s -> (# State# s, a #)) -> m a
prim (MutableByteArray# s -> Int# -> e -> State# s -> (# State# s, e #)
forall a s.
AtomicCount a =>
MutableByteArray# s -> Int# -> a -> State# s -> (# State# s, a #)
atomicSubFetchOldMutableByteArray# MutableByteArray# s
mba# Int#
i# e
a)
{-# INLINE atomicSubFetchOldMBytes #-}

-- | Subtract a numeric value from an element of a `MBytes`, corresponds to
-- @(`-`)@ done atomically. Returns the new value. Offset is in number of elements, rather
-- than bytes. Implies a full memory barrier.
--
-- /Note/ - Bounds are not checked, therefore this function is unsafe.
--
-- @since 0.1.0
atomicSubFetchNewMBytes ::
     (MonadPrim s m, AtomicCount e)
  => MBytes p s
  -> Off e
  -> e
  -> m e
atomicSubFetchNewMBytes :: MBytes p s -> Off e -> e -> m e
atomicSubFetchNewMBytes (MBytes MutableByteArray# s
mba#) (Off (I# Int#
i#)) e
a =
  (State# s -> (# State# s, e #)) -> m e
forall s (m :: * -> *) a.
MonadPrim s m =>
(State# s -> (# State# s, a #)) -> m a
prim (MutableByteArray# s -> Int# -> e -> State# s -> (# State# s, e #)
forall a s.
AtomicCount a =>
MutableByteArray# s -> Int# -> a -> State# s -> (# State# s, a #)
atomicSubFetchNewMutableByteArray# MutableByteArray# s
mba# Int#
i# e
a)
{-# INLINE atomicSubFetchNewMBytes #-}



-- | Binary conjunction (AND) of an element of a `MBytes` with the supplied value,
-- corresponds to @(`Data.Bits..&.`)@ done atomically. Returns the previous value. Offset
-- is in number of elements, rather than bytes. Implies a full memory barrier.
--
-- /Note/ - Bounds are not checked, therefore this function is unsafe.
--
-- @since 0.1.0
atomicAndFetchOldMBytes ::
     (MonadPrim s m, AtomicBits e)
  => MBytes p s
  -> Off e
  -> e
  -> m e
atomicAndFetchOldMBytes :: MBytes p s -> Off e -> e -> m e
atomicAndFetchOldMBytes (MBytes MutableByteArray# s
mba#) (Off (I# Int#
i#)) e
a =
  (State# s -> (# State# s, e #)) -> m e
forall s (m :: * -> *) a.
MonadPrim s m =>
(State# s -> (# State# s, a #)) -> m a
prim (MutableByteArray# s -> Int# -> e -> State# s -> (# State# s, e #)
forall a s.
AtomicBits a =>
MutableByteArray# s -> Int# -> a -> State# s -> (# State# s, a #)
atomicAndFetchOldMutableByteArray# MutableByteArray# s
mba# Int#
i# e
a)
{-# INLINE atomicAndFetchOldMBytes #-}

-- | Binary conjunction (AND) of an element of a `MBytes` with the supplied value,
-- corresponds to @(`Data.Bits..&.`)@ done atomically. Returns the new value. Offset is
-- in number of elements, rather than bytes. Implies a full memory barrier.
--
-- /Note/ - Bounds are not checked, therefore this function is unsafe.
--
-- @since 0.1.0
atomicAndFetchNewMBytes ::
     (MonadPrim s m, AtomicBits e)
  => MBytes p s
  -> Off e
  -> e
  -> m e
atomicAndFetchNewMBytes :: MBytes p s -> Off e -> e -> m e
atomicAndFetchNewMBytes (MBytes MutableByteArray# s
mba#) (Off (I# Int#
i#)) e
a =
  (State# s -> (# State# s, e #)) -> m e
forall s (m :: * -> *) a.
MonadPrim s m =>
(State# s -> (# State# s, a #)) -> m a
prim (MutableByteArray# s -> Int# -> e -> State# s -> (# State# s, e #)
forall a s.
AtomicBits a =>
MutableByteArray# s -> Int# -> a -> State# s -> (# State# s, a #)
atomicAndFetchNewMutableByteArray# MutableByteArray# s
mba# Int#
i# e
a)
{-# INLINE atomicAndFetchNewMBytes #-}



-- | Negation of binary conjunction (NAND) of an element of a `MBytes` with the
-- supplied value, corresponds to @\\x y -> `Data.Bits.complement` (x `Data.Bits..&.` y)@
-- done atomically. Returns the previous value. Offset is in number of elements, rather
-- than bytes. Implies a full memory barrier.
--
-- /Note/ - Bounds are not checked, therefore this function is unsafe.
--
-- @since 0.1.0
atomicNandFetchOldMBytes ::
     (MonadPrim s m, AtomicBits e)
  => MBytes p s
  -> Off e
  -> e
  -> m e
atomicNandFetchOldMBytes :: MBytes p s -> Off e -> e -> m e
atomicNandFetchOldMBytes (MBytes MutableByteArray# s
mba#) (Off (I# Int#
i#)) e
a =
  (State# s -> (# State# s, e #)) -> m e
forall s (m :: * -> *) a.
MonadPrim s m =>
(State# s -> (# State# s, a #)) -> m a
prim (MutableByteArray# s -> Int# -> e -> State# s -> (# State# s, e #)
forall a s.
AtomicBits a =>
MutableByteArray# s -> Int# -> a -> State# s -> (# State# s, a #)
atomicNandFetchOldMutableByteArray# MutableByteArray# s
mba# Int#
i# e
a)
{-# INLINE atomicNandFetchOldMBytes #-}

-- | Negation of binary conjunction (NAND)  of an element of a `MBytes` with the supplied
-- value, corresponds to @\\x y -> `Data.Bits.complement` (x `Data.Bits..&.` y)@ done
-- atomically. Returns the new value. Offset is in number of elements, rather than
-- bytes. Implies a full memory barrier.
--
-- /Note/ - Bounds are not checked, therefore this function is unsafe.
--
-- @since 0.1.0
atomicNandFetchNewMBytes ::
     (MonadPrim s m, AtomicBits e)
  => MBytes p s
  -> Off e
  -> e
  -> m e
atomicNandFetchNewMBytes :: MBytes p s -> Off e -> e -> m e
atomicNandFetchNewMBytes (MBytes MutableByteArray# s
mba#) (Off (I# Int#
i#)) e
a =
  (State# s -> (# State# s, e #)) -> m e
forall s (m :: * -> *) a.
MonadPrim s m =>
(State# s -> (# State# s, a #)) -> m a
prim (MutableByteArray# s -> Int# -> e -> State# s -> (# State# s, e #)
forall a s.
AtomicBits a =>
MutableByteArray# s -> Int# -> a -> State# s -> (# State# s, a #)
atomicNandFetchNewMutableByteArray# MutableByteArray# s
mba# Int#
i# e
a)
{-# INLINE atomicNandFetchNewMBytes #-}




-- | Binary disjunction (OR) of an element of a `MBytes` with the supplied value,
-- corresponds to @(`Data.Bits..|.`)@ done atomically. Returns the previous value. Offset
-- is in number of elements, rather than bytes. Implies a full memory barrier.
--
-- /Note/ - Bounds are not checked, therefore this function is unsafe.
--
-- @since 0.1.0
atomicOrFetchOldMBytes ::
     (MonadPrim s m, AtomicBits e)
  => MBytes p s
  -> Off e
  -> e
  -> m e
atomicOrFetchOldMBytes :: MBytes p s -> Off e -> e -> m e
atomicOrFetchOldMBytes (MBytes MutableByteArray# s
mba#) (Off (I# Int#
i#)) e
a =
  (State# s -> (# State# s, e #)) -> m e
forall s (m :: * -> *) a.
MonadPrim s m =>
(State# s -> (# State# s, a #)) -> m a
prim (MutableByteArray# s -> Int# -> e -> State# s -> (# State# s, e #)
forall a s.
AtomicBits a =>
MutableByteArray# s -> Int# -> a -> State# s -> (# State# s, a #)
atomicOrFetchOldMutableByteArray# MutableByteArray# s
mba# Int#
i# e
a)
{-# INLINE atomicOrFetchOldMBytes #-}

-- | Binary disjunction (OR) of an element of a `MBytes` with the supplied value,
-- corresponds to @(`Data.Bits..|.`)@ done atomically. Returns the new value. Offset is
-- in number of elements, rather than bytes. Implies a full memory barrier.
--
-- /Note/ - Bounds are not checked, therefore this function is unsafe.
--
-- @since 0.1.0
atomicOrFetchNewMBytes ::
     (MonadPrim s m, AtomicBits e)
  => MBytes p s
  -> Off e
  -> e
  -> m e
atomicOrFetchNewMBytes :: MBytes p s -> Off e -> e -> m e
atomicOrFetchNewMBytes (MBytes MutableByteArray# s
mba#) (Off (I# Int#
i#)) e
a =
  (State# s -> (# State# s, e #)) -> m e
forall s (m :: * -> *) a.
MonadPrim s m =>
(State# s -> (# State# s, a #)) -> m a
prim (MutableByteArray# s -> Int# -> e -> State# s -> (# State# s, e #)
forall a s.
AtomicBits a =>
MutableByteArray# s -> Int# -> a -> State# s -> (# State# s, a #)
atomicOrFetchNewMutableByteArray# MutableByteArray# s
mba# Int#
i# e
a)
{-# INLINE atomicOrFetchNewMBytes #-}



-- | Binary exclusive disjunction (XOR) of an element of a `MBytes` with the supplied value,
-- corresponds to @`Data.Bits.xor`@ done atomically. Returns the previous value. Offset
-- is in number of elements, rather than bytes. Implies a full memory barrier.
--
-- /Note/ - Bounds are not checked, therefore this function is unsafe.
--
-- @since 0.1.0
atomicXorFetchOldMBytes ::
     (MonadPrim s m, AtomicBits e)
  => MBytes p s
  -> Off e
  -> e
  -> m e
atomicXorFetchOldMBytes :: MBytes p s -> Off e -> e -> m e
atomicXorFetchOldMBytes (MBytes MutableByteArray# s
mba#) (Off (I# Int#
i#)) e
a =
  (State# s -> (# State# s, e #)) -> m e
forall s (m :: * -> *) a.
MonadPrim s m =>
(State# s -> (# State# s, a #)) -> m a
prim (MutableByteArray# s -> Int# -> e -> State# s -> (# State# s, e #)
forall a s.
AtomicBits a =>
MutableByteArray# s -> Int# -> a -> State# s -> (# State# s, a #)
atomicXorFetchOldMutableByteArray# MutableByteArray# s
mba# Int#
i# e
a)
{-# INLINE atomicXorFetchOldMBytes #-}

-- | Binary exclusive disjunction (XOR) of an element of a `MBytes` with the supplied value,
-- corresponds to @`Data.Bits.xor`@ done atomically. Returns the new value. Offset is
-- in number of elements, rather than bytes. Implies a full memory barrier.
--
-- /Note/ - Bounds are not checked, therefore this function is unsafe.
--
-- @since 0.1.0
atomicXorFetchNewMBytes ::
     (MonadPrim s m, AtomicBits e)
  => MBytes p s
  -> Off e
  -> e
  -> m e
atomicXorFetchNewMBytes :: MBytes p s -> Off e -> e -> m e
atomicXorFetchNewMBytes (MBytes MutableByteArray# s
mba#) (Off (I# Int#
i#)) e
a =
  (State# s -> (# State# s, e #)) -> m e
forall s (m :: * -> *) a.
MonadPrim s m =>
(State# s -> (# State# s, a #)) -> m a
prim (MutableByteArray# s -> Int# -> e -> State# s -> (# State# s, e #)
forall a s.
AtomicBits a =>
MutableByteArray# s -> Int# -> a -> State# s -> (# State# s, a #)
atomicXorFetchNewMutableByteArray# MutableByteArray# s
mba# Int#
i# e
a)
{-# INLINE atomicXorFetchNewMBytes #-}





-- | Binary negation (NOT) of an element of a `MBytes`, corresponds to
-- @(`Data.Bits.complement`)@ done atomically. Returns the previous value. Offset is in
-- number of elements, rather than bytes. Implies a full memory barrier.
--
-- /Note/ - Bounds are not checked, therefore this function is unsafe.
--
-- @since 0.1.0
atomicNotFetchOldMBytes ::
     (MonadPrim s m, AtomicBits e)
  => MBytes p s
  -> Off e
  -> m e
atomicNotFetchOldMBytes :: MBytes p s -> Off e -> m e
atomicNotFetchOldMBytes (MBytes MutableByteArray# s
mba#) (Off (I# Int#
i#)) =
  (State# s -> (# State# s, e #)) -> m e
forall s (m :: * -> *) a.
MonadPrim s m =>
(State# s -> (# State# s, a #)) -> m a
prim (MutableByteArray# s -> Int# -> State# s -> (# State# s, e #)
forall a s.
AtomicBits a =>
MutableByteArray# s -> Int# -> State# s -> (# State# s, a #)
atomicNotFetchOldMutableByteArray# MutableByteArray# s
mba# Int#
i#)
{-# INLINE atomicNotFetchOldMBytes #-}

-- | Binary negation (NOT) of an element of a `MBytes`, corresponds to
-- @(`Data.Bits.complement`)@ done atomically. Returns the new value. Offset is in number
-- of elements, rather than bytes. Implies a full memory barrier.
--
-- /Note/ - Bounds are not checked, therefore this function is unsafe.
--
-- @since 0.1.0
atomicNotFetchNewMBytes ::
     (MonadPrim s m, AtomicBits e)
  => MBytes p s
  -> Off e
  -> m e
atomicNotFetchNewMBytes :: MBytes p s -> Off e -> m e
atomicNotFetchNewMBytes (MBytes MutableByteArray# s
mba#) (Off (I# Int#
i#)) =
  (State# s -> (# State# s, e #)) -> m e
forall s (m :: * -> *) a.
MonadPrim s m =>
(State# s -> (# State# s, a #)) -> m a
prim (MutableByteArray# s -> Int# -> State# s -> (# State# s, e #)
forall a s.
AtomicBits a =>
MutableByteArray# s -> Int# -> State# s -> (# State# s, a #)
atomicNotFetchNewMutableByteArray# MutableByteArray# s
mba# Int#
i#)
{-# INLINE atomicNotFetchNewMBytes #-}




prefetchBytes0 :: (MonadPrim s m, Prim e) => Bytes p -> Off e -> m ()
prefetchBytes0 :: Bytes p -> Off e -> m ()
prefetchBytes0 (Bytes ByteArray#
b#) Off e
off = (State# s -> State# s) -> m ()
forall s (m :: * -> *).
MonadPrim s m =>
(State# s -> State# s) -> m ()
prim_ (ByteArray# -> Int# -> State# s -> State# s
forall d. ByteArray# -> Int# -> State# d -> State# d
prefetchByteArray0# ByteArray#
b# (Off e -> Int#
forall e. Prim e => Off e -> Int#
unOffBytes# Off e
off))
{-# INLINE prefetchBytes0 #-}

prefetchMBytes0 :: (MonadPrim s m, Prim e) => MBytes p s -> Off e -> m ()
prefetchMBytes0 :: MBytes p s -> Off e -> m ()
prefetchMBytes0 (MBytes MutableByteArray# s
mb#) Off e
off = (State# s -> State# s) -> m ()
forall s (m :: * -> *).
MonadPrim s m =>
(State# s -> State# s) -> m ()
prim_ (MutableByteArray# s -> Int# -> State# s -> State# s
forall d. MutableByteArray# d -> Int# -> State# d -> State# d
prefetchMutableByteArray0# MutableByteArray# s
mb# (Off e -> Int#
forall e. Prim e => Off e -> Int#
unOffBytes# Off e
off))
{-# INLINE prefetchMBytes0 #-}

prefetchBytes1 :: (MonadPrim s m, Prim e) => Bytes p -> Off e -> m ()
prefetchBytes1 :: Bytes p -> Off e -> m ()
prefetchBytes1 (Bytes ByteArray#
b#) Off e
off = (State# s -> State# s) -> m ()
forall s (m :: * -> *).
MonadPrim s m =>
(State# s -> State# s) -> m ()
prim_ (ByteArray# -> Int# -> State# s -> State# s
forall d. ByteArray# -> Int# -> State# d -> State# d
prefetchByteArray1# ByteArray#
b# (Off e -> Int#
forall e. Prim e => Off e -> Int#
unOffBytes# Off e
off))
{-# INLINE prefetchBytes1 #-}

prefetchMBytes1 :: (MonadPrim s m, Prim e) => MBytes p s -> Off e -> m ()
prefetchMBytes1 :: MBytes p s -> Off e -> m ()
prefetchMBytes1 (MBytes MutableByteArray# s
mb#) Off e
off = (State# s -> State# s) -> m ()
forall s (m :: * -> *).
MonadPrim s m =>
(State# s -> State# s) -> m ()
prim_ (MutableByteArray# s -> Int# -> State# s -> State# s
forall d. MutableByteArray# d -> Int# -> State# d -> State# d
prefetchMutableByteArray1# MutableByteArray# s
mb# (Off e -> Int#
forall e. Prim e => Off e -> Int#
unOffBytes# Off e
off))
{-# INLINE prefetchMBytes1 #-}

prefetchBytes2 :: (MonadPrim s m, Prim e) => Bytes p -> Off e -> m ()
prefetchBytes2 :: Bytes p -> Off e -> m ()
prefetchBytes2 (Bytes ByteArray#
b#) Off e
off = (State# s -> State# s) -> m ()
forall s (m :: * -> *).
MonadPrim s m =>
(State# s -> State# s) -> m ()
prim_ (ByteArray# -> Int# -> State# s -> State# s
forall d. ByteArray# -> Int# -> State# d -> State# d
prefetchByteArray2# ByteArray#
b# (Off e -> Int#
forall e. Prim e => Off e -> Int#
unOffBytes# Off e
off))
{-# INLINE prefetchBytes2 #-}

prefetchMBytes2 :: (MonadPrim s m, Prim e) => MBytes p s -> Off e -> m ()
prefetchMBytes2 :: MBytes p s -> Off e -> m ()
prefetchMBytes2 (MBytes MutableByteArray# s
mb#) Off e
off = (State# s -> State# s) -> m ()
forall s (m :: * -> *).
MonadPrim s m =>
(State# s -> State# s) -> m ()
prim_ (MutableByteArray# s -> Int# -> State# s -> State# s
forall d. MutableByteArray# d -> Int# -> State# d -> State# d
prefetchMutableByteArray2# MutableByteArray# s
mb# (Off e -> Int#
forall e. Prim e => Off e -> Int#
unOffBytes# Off e
off))
{-# INLINE prefetchMBytes2 #-}

prefetchBytes3 :: (MonadPrim s m, Prim e) => Bytes p -> Off e -> m ()
prefetchBytes3 :: Bytes p -> Off e -> m ()
prefetchBytes3 (Bytes ByteArray#
b#) Off e
off = (State# s -> State# s) -> m ()
forall s (m :: * -> *).
MonadPrim s m =>
(State# s -> State# s) -> m ()
prim_ (ByteArray# -> Int# -> State# s -> State# s
forall d. ByteArray# -> Int# -> State# d -> State# d
prefetchByteArray3# ByteArray#
b# (Off e -> Int#
forall e. Prim e => Off e -> Int#
unOffBytes# Off e
off))
{-# INLINE prefetchBytes3 #-}

prefetchMBytes3 :: (MonadPrim s m, Prim e) => MBytes p s -> Off e -> m ()
prefetchMBytes3 :: MBytes p s -> Off e -> m ()
prefetchMBytes3 (MBytes MutableByteArray# s
mb#) Off e
off = (State# s -> State# s) -> m ()
forall s (m :: * -> *).
MonadPrim s m =>
(State# s -> State# s) -> m ()
prim_ (MutableByteArray# s -> Int# -> State# s -> State# s
forall d. MutableByteArray# d -> Int# -> State# d -> State# d
prefetchMutableByteArray3# MutableByteArray# s
mb# (Off e -> Int#
forall e. Prim e => Off e -> Int#
unOffBytes# Off e
off))
{-# INLINE prefetchMBytes3 #-}