{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RoleAnnotations #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UnboxedTuples #-}
-- |
-- Module      : Data.Prim.Memory.Addr
-- Copyright   : (c) Alexey Kuleshevich 2020
-- License     : BSD3
-- Maintainer  : Alexey Kuleshevich <alexey@kuleshevi.ch>
-- Stability   : experimental
-- Portability : non-portable
--
module Data.Prim.Memory.Addr
  ( -- * Immutable Addr
    Addr(..)
  , castAddr
  , fromBytesAddr
  , curOffAddr
  , byteCountAddr
  , countAddr
  , plusOffAddr
  , plusByteOffAddr
  , indexAddr
  , indexOffAddr
  , indexByteOffAddr
  , readAddr
  , readOffAddr
  , readByteOffAddr
  , thawAddr
  , freezeMAddr
  , withPtrAddr
  , withAddrAddr#
  , withNoHaltPtrAddr

   -- * Mutable MAddr
  , MAddr(..)
  , castMAddr
  , newMAddr
  , allocMAddr
  , allocAlignedMAddr
  , allocZeroMAddr
  , allocZeroAlignedMAddr
  , reallocMAddr
  , shrinkMAddr
  , shrinkByteCountMAddr
  , setMAddr
  , curOffMAddr
  , getByteCountMAddr
  , getCountMAddr
  , plusOffMAddr
  , plusByteOffMAddr
  , readMAddr
  , readOffMAddr
  , readByteOffMAddr
  , writeMAddr
  , writeOffMAddr
  , writeByteOffMAddr
  , copyAddrToMAddr
  , moveMAddrToMAddr


  , modifyMAddr
  , modifyMAddr_
  , modifyFetchOldMAddr
  , modifyFetchNewMAddr
  , modifyMAddrM
  , modifyMAddrM_
  , modifyFetchOldMAddrM
  , modifyFetchNewMAddrM
  , swapMAddrs_
  , swapMAddrs

  , withPtrMAddr
  , withAddrMAddr#
  , withNoHaltPtrMAddr
  , toForeignPtrAddr
  , toForeignPtrMAddr
  , fromForeignPtrAddr
  , fromForeignPtrMAddr
  -- * Conversion
  -- ** ByteString
  , toByteStringAddr
  , toShortByteStringAddr
  , fromShortByteStringAddr
  , fromByteStringAddr
  , fromByteStringMAddr

  -- * Atomic
  , casOffMAddr
  , casBoolOffMAddr
  , casBoolFetchOffMAddr
  , atomicReadOffMAddr
  , atomicWriteOffMAddr
  , atomicModifyOffMAddr
  , atomicModifyOffMAddr_
  , atomicModifyFetchOldOffMAddr
  , atomicModifyFetchNewOffMAddr
  -- ** Numeric
  , atomicAddFetchOldOffMAddr
  , atomicAddFetchNewOffMAddr
  , atomicSubFetchOldOffMAddr
  , atomicSubFetchNewOffMAddr
  -- ** Binary
  , atomicAndFetchOldOffMAddr
  , atomicAndFetchNewOffMAddr
  , atomicNandFetchOldOffMAddr
  , atomicNandFetchNewOffMAddr
  , atomicOrFetchOldOffMAddr
  , atomicOrFetchNewOffMAddr
  , atomicXorFetchOldOffMAddr
  , atomicXorFetchNewOffMAddr
  , atomicNotFetchOldOffMAddr
  , atomicNotFetchNewOffMAddr
  -- * Prefetch
  -- ** Directly
  , prefetchAddr0
  , prefetchMAddr0
  , prefetchAddr1
  , prefetchMAddr1
  , prefetchAddr2
  , prefetchMAddr2
  , prefetchAddr3
  , prefetchMAddr3
  -- ** With offset
  , prefetchOffAddr0
  , prefetchOffMAddr0
  , prefetchOffAddr1
  , prefetchOffMAddr1
  , prefetchOffAddr2
  , prefetchOffMAddr2
  , prefetchOffAddr3
  , prefetchOffMAddr3
  -- * Re-export
  , module Data.Prim
  ) where

import Control.Arrow (first)
import Control.DeepSeq
import Control.Prim.Eval
import Control.Prim.Monad
import Control.Prim.Monad.Unsafe
import Data.ByteString.Internal
import Data.ByteString.Short.Internal
import Data.List.NonEmpty (NonEmpty(..))
import qualified Data.Monoid as Monoid
import Data.Prim
import Data.Prim.Atomic
import Data.Prim.Class
import Data.Prim.Memory.Bytes
import Data.Prim.Memory.Bytes.Internal
import Data.Prim.Memory.ByteString
import Data.Prim.Memory.Fold
import Data.Prim.Memory.ForeignPtr
import Data.Prim.Memory.Internal
import Data.Prim.Memory.Ptr
import qualified Data.Semigroup as Semigroup
import Foreign.Prim
import Unsafe.Coerce


-- | Immutable read-only address
data Addr e = Addr
  { Addr e -> Addr#
addrAddr# :: Addr#
  , Addr e -> Bytes 'Pin
addrBytes :: Bytes 'Pin
  }
type role Addr nominal

-- | Mutable address
data MAddr e s = MAddr
  { MAddr e s -> Addr#
mAddrAddr#  :: Addr#
  , MAddr e s -> MBytes 'Pin s
mAddrMBytes :: MBytes 'Pin s
  }
type role MAddr nominal nominal


instance (Eq e, Prim e) => Eq (Addr e) where
  == :: Addr e -> Addr e -> Bool
(==) = forall mr. (Prim e, Eq e, MemRead mr) => mr -> mr -> Bool
forall e mr. (Prim e, Eq e, MemRead mr) => mr -> mr -> Bool
eqMem @e
  {-# INLINE (==) #-}

instance (Prim e, Ord e) => Ord (Addr e) where
  compare :: Addr e -> Addr e -> Ordering
compare = forall mr. (Prim e, Ord e, MemRead mr) => mr -> mr -> Ordering
forall e mr. (Prim e, Ord e, MemRead mr) => mr -> mr -> Ordering
compareMem @e
  {-# INLINE compare #-}


instance (Show e, Prim e) => Show (Addr e) where
  show :: Addr e -> String
show Addr e
a = [e] -> String
forall a. Show a => a -> String
show (Addr e -> [e]
forall e mr. (MemRead mr, Prim e) => mr -> [e]
toListMem Addr e
a :: [e])

instance IsString (Addr Char) where
  fromString :: String -> Addr Char
fromString = String -> Addr Char
forall e (ma :: * -> *).
(Prim e, MemAlloc ma) =>
[e] -> FrozenMem ma
fromListMem

instance Prim e => IsList (Addr e) where
  type Item (Addr e) = e
  fromList :: [Item (Addr e)] -> Addr e
fromList = [Item (Addr e)] -> Addr e
forall e (ma :: * -> *).
(Prim e, MemAlloc ma) =>
[e] -> FrozenMem ma
fromListMem
  fromListN :: Int -> [Item (Addr e)] -> Addr e
fromListN Int
n = Count e -> [e] -> FrozenMem (MAddr e)
forall e (ma :: * -> *).
(Prim e, MemAlloc ma) =>
Count e -> [e] -> FrozenMem ma
fromListZeroMemN_ (Int -> Count e
forall e. Int -> Count e
Count Int
n)
  toList :: Addr e -> [Item (Addr e)]
toList = Addr e -> [Item (Addr e)]
forall e mr. (MemRead mr, Prim e) => mr -> [e]
toListMem

instance Semigroup.Semigroup (Addr e) where
  <> :: Addr e -> Addr e -> Addr e
(<>) = Addr e -> Addr e -> Addr e
forall mr1 mr2 (ma :: * -> *).
(MemRead mr1, MemRead mr2, MemAlloc ma) =>
mr1 -> mr2 -> FrozenMem ma
appendMem
  {-# INLINE (<>) #-}
  sconcat :: NonEmpty (Addr e) -> Addr e
sconcat (Addr e
x :| [Addr e]
xs) = [Addr e] -> FrozenMem (MAddr e)
forall mr (ma :: * -> *).
(MemRead mr, MemAlloc ma) =>
[mr] -> FrozenMem ma
concatMem (Addr e
xAddr e -> [Addr e] -> [Addr e]
forall a. a -> [a] -> [a]
:[Addr e]
xs)
  {-# INLINE sconcat #-}
  stimes :: b -> Addr e -> Addr e
stimes b
i = Int -> Addr e -> FrozenMem (MAddr e)
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 Monoid.Monoid (Addr e) where
  mappend :: Addr e -> Addr e -> Addr e
mappend = Addr e -> Addr e -> Addr e
forall mr1 mr2 (ma :: * -> *).
(MemRead mr1, MemRead mr2, MemAlloc ma) =>
mr1 -> mr2 -> FrozenMem ma
appendMem
  {-# INLINE mappend #-}
  mconcat :: [Addr e] -> Addr e
mconcat = [Addr e] -> Addr e
forall mr (ma :: * -> *).
(MemRead mr, MemAlloc ma) =>
[mr] -> FrozenMem ma
concatMem
  {-# INLINE mconcat #-}
  mempty :: Addr e
mempty = Addr e
forall (ma :: * -> *). MemAlloc ma => FrozenMem ma
emptyMem
  {-# INLINE mempty #-}


castAddr :: Addr e -> Addr b
castAddr :: Addr e -> Addr b
castAddr (Addr Addr#
a Bytes 'Pin
b) = Addr# -> Bytes 'Pin -> Addr b
forall e. Addr# -> Bytes 'Pin -> Addr e
Addr Addr#
a Bytes 'Pin
b
{-# INLINE castAddr #-}

castMAddr :: MAddr e s -> MAddr b s
castMAddr :: MAddr e s -> MAddr b s
castMAddr (MAddr Addr#
a MBytes 'Pin s
mb) = Addr# -> MBytes 'Pin s -> MAddr b s
forall e s. Addr# -> MBytes 'Pin s -> MAddr e s
MAddr Addr#
a MBytes 'Pin s
mb
{-# INLINE castMAddr #-}

castStateMAddr :: MAddr e s' -> MAddr b s
castStateMAddr :: MAddr e s' -> MAddr b s
castStateMAddr = MAddr e s' -> MAddr b s
forall a b. a -> b
unsafeCoerce

isSameAddr :: Addr e -> Addr e -> Bool
isSameAddr :: Addr e -> Addr e -> Bool
isSameAddr (Addr Addr#
a1# Bytes 'Pin
_) (Addr Addr#
a2# Bytes 'Pin
_) = Int# -> Bool
isTrue# (Addr#
a1# Addr# -> Addr# -> Int#
`eqAddr#` Addr#
a2#)

isSameMAddr :: MAddr e s -> MAddr e s -> Bool
isSameMAddr :: MAddr e s -> MAddr e s -> Bool
isSameMAddr (MAddr Addr#
a1# MBytes 'Pin s
_) (MAddr Addr#
a2# MBytes 'Pin s
_) = Int# -> Bool
isTrue# (Addr#
a1# Addr# -> Addr# -> Int#
`eqAddr#` Addr#
a2#)

instance NFData (Addr e) where
  rnf :: Addr e -> ()
rnf (Addr Addr#
_ Bytes 'Pin
_) = ()

instance NFData (MAddr e s) where
  rnf :: MAddr e s -> ()
rnf (MAddr Addr#
_ MBytes 'Pin s
_) = ()

toBytesAddr :: Addr e -> (Bytes 'Pin, Off Word8)
toBytesAddr :: Addr e -> (Bytes 'Pin, Off Word8)
toBytesAddr addr :: Addr e
addr@(Addr Addr#
_ Bytes 'Pin
b) = (Bytes 'Pin
b, Addr e -> Off Word8
forall e. Addr e -> Off Word8
curByteOffAddr Addr e
addr)

fromBytesAddr :: Bytes 'Pin -> Addr e
fromBytesAddr :: Bytes 'Pin -> Addr e
fromBytesAddr b :: Bytes 'Pin
b@(Bytes ByteArray#
b#) = Addr# -> Bytes 'Pin -> Addr e
forall e. Addr# -> Bytes 'Pin -> Addr e
Addr (ByteArray# -> Addr#
byteArrayContents# ByteArray#
b#) Bytes 'Pin
b

fromMBytesMAddr :: MBytes 'Pin s -> MAddr e s
fromMBytesMAddr :: MBytes 'Pin s -> MAddr e s
fromMBytesMAddr MBytes 'Pin s
mb =
  case MBytes 'Pin s -> Ptr Any
forall s e. MBytes 'Pin s -> Ptr e
toPtrMBytes MBytes 'Pin s
mb of
    Ptr Addr#
addr# -> Addr# -> MBytes 'Pin s -> MAddr e s
forall e s. Addr# -> MBytes 'Pin s -> MAddr e s
MAddr Addr#
addr# MBytes 'Pin s
mb
{-# INLINE fromMBytesMAddr #-}

newMAddr :: forall e m s. (MonadPrim s m, Prim e) => e -> m (MAddr e s)
newMAddr :: e -> m (MAddr e s)
newMAddr e
e = do
  MAddr e s
maddr <- MBytes 'Pin s -> MAddr e s
forall s e. MBytes 'Pin s -> MAddr e s
fromMBytesMAddr (MBytes 'Pin s -> MAddr e s) -> m (MBytes 'Pin s) -> m (MAddr e s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Count e -> m (MBytes 'Pin s)
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
Count e -> m (MBytes 'Pin s)
allocPinnedMBytes (Count e
1 :: Count e)
  MAddr e s -> e -> m ()
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
MAddr e s -> e -> m ()
writeMAddr MAddr e s
maddr e
e
  MAddr e s -> m (MAddr e s)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (MAddr e s -> m (MAddr e s)) -> MAddr e s -> m (MAddr e s)
forall a b. (a -> b) -> a -> b
$! MAddr e s
maddr
{-# INLINE newMAddr #-}

allocMAddr :: forall e m s. (MonadPrim s m, Prim e) => Count e -> m (MAddr e s)
allocMAddr :: Count e -> m (MAddr e s)
allocMAddr Count e
c = MBytes 'Pin s -> MAddr e s
forall s e. MBytes 'Pin s -> MAddr e s
fromMBytesMAddr (MBytes 'Pin s -> MAddr e s) -> m (MBytes 'Pin s) -> m (MAddr e s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Count e -> m (MBytes 'Pin s)
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
Count e -> m (MBytes 'Pin s)
allocPinnedMBytes Count e
c

allocZeroMAddr :: forall e m s. (MonadPrim s m, Prim e) => Count e -> m (MAddr e s)
allocZeroMAddr :: Count e -> m (MAddr e s)
allocZeroMAddr Count e
c = MBytes 'Pin s -> MAddr e s
forall s e. MBytes 'Pin s -> MAddr e s
fromMBytesMAddr (MBytes 'Pin s -> MAddr e s) -> m (MBytes 'Pin s) -> m (MAddr e s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Count e -> m (MBytes 'Pin s)
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
Count e -> m (MBytes 'Pin s)
allocZeroPinnedMBytes Count e
c


allocAlignedMAddr :: forall e m s. (MonadPrim s m, Prim e) => Count e -> m (MAddr e s)
allocAlignedMAddr :: Count e -> m (MAddr e s)
allocAlignedMAddr Count e
c = MBytes 'Pin s -> MAddr e s
forall s e. MBytes 'Pin s -> MAddr e s
fromMBytesMAddr (MBytes 'Pin s -> MAddr e s) -> m (MBytes 'Pin s) -> m (MAddr e s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Count e -> m (MBytes 'Pin s)
forall e (m :: * -> *) s.
(MonadPrim s m, Prim e) =>
Count e -> m (MBytes 'Pin s)
allocAlignedMBytes Count e
c

allocZeroAlignedMAddr :: forall e m s. (MonadPrim s m, Prim e) => Count e -> m (MAddr e s)
allocZeroAlignedMAddr :: Count e -> m (MAddr e s)
allocZeroAlignedMAddr Count e
c = MBytes 'Pin s -> MAddr e s
forall s e. MBytes 'Pin s -> MAddr e s
fromMBytesMAddr (MBytes 'Pin s -> MAddr e s) -> m (MBytes 'Pin s) -> m (MAddr e s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Count e -> m (MBytes 'Pin s)
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
Count e -> m (MBytes 'Pin s)
allocZeroAlignedMBytes Count e
c


-- | Shrink mutable address to new specified size in number of elements. The new count
-- must be less than or equal to the current as reported by `getCountMAddr`.
shrinkMAddr :: (MonadPrim s m, Prim e) => MAddr e s -> Count e -> m ()
shrinkMAddr :: MAddr e s -> Count e -> m ()
shrinkMAddr maddr :: MAddr e s
maddr@(MAddr Addr#
_ MBytes 'Pin s
mb) Count e
c = MBytes 'Pin s -> Count Word8 -> m ()
forall s (m :: * -> *) e (p :: Pinned).
(MonadPrim s m, Prim e) =>
MBytes p s -> Count e -> m ()
shrinkMBytes MBytes 'Pin s
mb (Count e -> Count Word8
forall e. Prim e => Count e -> Count Word8
toByteCount Count e
c Count Word8 -> Count Word8 -> Count Word8
forall a. Num a => a -> a -> a
+ Off Word8 -> Count Word8
coerce (MAddr e s -> Off Word8
forall e s. MAddr e s -> Off Word8
curByteOffMAddr MAddr e s
maddr))
{-# INLINE shrinkMAddr #-}

-- | Shrink mutable address to new specified size in bytes. The new count must be less
-- than or equal to the current as reported by `getByteCountMAddr`.
shrinkByteCountMAddr :: MonadPrim s m => MAddr e s -> Count Word8 -> m ()
shrinkByteCountMAddr :: MAddr e s -> Count Word8 -> m ()
shrinkByteCountMAddr maddr :: MAddr e s
maddr@(MAddr Addr#
_ MBytes 'Pin s
mb) Count Word8
c = MBytes 'Pin s -> Count Word8 -> m ()
forall s (m :: * -> *) e (p :: Pinned).
(MonadPrim s m, Prim e) =>
MBytes p s -> Count e -> m ()
shrinkMBytes MBytes 'Pin s
mb (Count Word8
c Count Word8 -> Count Word8 -> Count Word8
forall a. Num a => a -> a -> a
+ Off Word8 -> Count Word8
coerce (MAddr e s -> Off Word8
forall e s. MAddr e s -> Off Word8
curByteOffMAddr MAddr e s
maddr))
{-# INLINE shrinkByteCountMAddr #-}


reallocMAddr :: (MonadPrim s m, Prim e) => MAddr e s -> Count e -> m (MAddr e s)
reallocMAddr :: MAddr e s -> Count e -> m (MAddr e s)
reallocMAddr MAddr e s
maddr Count e
c = do
  Count Word8
oldByteCount <- MAddr e s -> m (Count Word8)
forall s (m :: * -> *) e.
MonadPrim s m =>
MAddr e s -> m (Count Word8)
getByteCountMAddr MAddr e s
maddr
  let newByteCount :: Count Word8
newByteCount = Count e -> Count Word8
forall e. Prim e => Count e -> Count Word8
toByteCount Count e
c
  if Count Word8
newByteCount Count Word8 -> Count Word8 -> Bool
forall a. Ord a => a -> a -> Bool
<= Count Word8
oldByteCount
    then MAddr e s
maddr MAddr e s -> m () -> m (MAddr e s)
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$
         Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Count Word8
newByteCount Count Word8 -> Count Word8 -> Bool
forall a. Ord a => a -> a -> Bool
< Count Word8
oldByteCount) (MAddr e s -> Count Word8 -> m ()
forall s (m :: * -> *) e.
MonadPrim s m =>
MAddr e s -> Count Word8 -> m ()
shrinkByteCountMAddr MAddr e s
maddr Count Word8
newByteCount)
    else do
      Addr e
addr <- MAddr e s -> m (Addr e)
forall s (m :: * -> *) e. MonadPrim s m => MAddr e s -> m (Addr e)
freezeMAddr MAddr e s
maddr
      MAddr Word8 s
maddr' <- Count Word8 -> m (MAddr Word8 s)
forall e (m :: * -> *) s.
(MonadPrim s m, Prim e) =>
Count e -> m (MAddr e s)
allocMAddr Count Word8
newByteCount
      MAddr Word8 s -> MAddr e s
forall e s b. MAddr e s -> MAddr b s
castMAddr MAddr Word8 s
maddr' MAddr e s -> m () -> m (MAddr e s)
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$
        Addr Word8
-> Off Word8 -> MAddr Word8 s -> Off Word8 -> Count Word8 -> m ()
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
Addr e -> Off e -> MAddr e s -> Off e -> Count e -> m ()
copyAddrToMAddr (Addr e -> Addr Word8
forall e b. Addr e -> Addr b
castAddr Addr e
addr) Off Word8
0 MAddr Word8 s
maddr' Off Word8
0 Count Word8
oldByteCount
{-# INLINABLE reallocMAddr #-}


plusOffAddr :: Prim e => Addr e -> Off e -> Addr e
plusOffAddr :: Addr e -> Off e -> Addr e
plusOffAddr (Addr Addr#
addr# Bytes 'Pin
b) Off e
off = Addr# -> Bytes 'Pin -> Addr e
forall e. Addr# -> Bytes 'Pin -> Addr e
Addr (Addr#
addr# Addr# -> Int# -> Addr#
`plusAddr#` Off e -> Int#
forall e. Prim e => Off e -> Int#
unOffBytes# Off e
off) Bytes 'Pin
b

plusByteOffAddr :: Addr e -> Off Word8 -> Addr e
plusByteOffAddr :: Addr e -> Off Word8 -> Addr e
plusByteOffAddr (Addr Addr#
addr# Bytes 'Pin
b) Off Word8
off = Addr# -> Bytes 'Pin -> Addr e
forall e. Addr# -> Bytes 'Pin -> Addr e
Addr (Addr#
addr# Addr# -> Int# -> Addr#
`plusAddr#` Off Word8 -> Int#
forall e. Prim e => Off e -> Int#
unOffBytes# Off Word8
off) Bytes 'Pin
b

plusOffMAddr :: Prim e => MAddr e s -> Off e -> MAddr e s
plusOffMAddr :: MAddr e s -> Off e -> MAddr e s
plusOffMAddr (MAddr Addr#
addr# MBytes 'Pin s
mb) Off e
off = Addr# -> MBytes 'Pin s -> MAddr e s
forall e s. Addr# -> MBytes 'Pin s -> MAddr e s
MAddr (Addr#
addr# Addr# -> Int# -> Addr#
`plusAddr#` Off e -> Int#
forall e. Prim e => Off e -> Int#
unOffBytes# Off e
off) MBytes 'Pin s
mb

plusByteOffMAddr :: MAddr e s -> Off Word8 -> MAddr e s
plusByteOffMAddr :: MAddr e s -> Off Word8 -> MAddr e s
plusByteOffMAddr (MAddr Addr#
addr# MBytes 'Pin s
mb) Off Word8
off = Addr# -> MBytes 'Pin s -> MAddr e s
forall e s. Addr# -> MBytes 'Pin s -> MAddr e s
MAddr (Addr#
addr# Addr# -> Int# -> Addr#
`plusAddr#` Off Word8 -> Int#
forall e. Prim e => Off e -> Int#
unOffBytes# Off Word8
off) MBytes 'Pin s
mb

curOffAddr :: Prim e => Addr e -> Off e
curOffAddr :: Addr e -> Off e
curOffAddr a :: Addr e
a@(Addr Addr#
addr# Bytes 'Pin
b) = (Addr# -> Ptr e
forall a. Addr# -> Ptr a
Ptr Addr#
addr# Ptr e -> Ptr e -> Off e
forall e. Prim e => Ptr e -> Ptr e -> Off e
`minusOffPtr` Bytes 'Pin -> Ptr e
forall e. Bytes 'Pin -> Ptr e
toPtrBytes Bytes 'Pin
b) Off e -> Addr e -> Off e
forall e (proxy :: * -> *). Off e -> proxy e -> Off e
`offForProxyTypeOf` Addr e
a

curByteOffAddr :: Addr e -> Off Word8
curByteOffAddr :: Addr e -> Off Word8
curByteOffAddr (Addr Addr#
addr# Bytes 'Pin
b) = Addr# -> Ptr Any
forall a. Addr# -> Ptr a
Ptr Addr#
addr# Ptr Any -> Ptr Any -> Off Word8
forall e. Ptr e -> Ptr e -> Off Word8
`minusByteOffPtr` Bytes 'Pin -> Ptr Any
forall e. Bytes 'Pin -> Ptr e
toPtrBytes Bytes 'Pin
b

countAddr ::
     forall e. Prim e
  => Addr e
  -> Count e
countAddr :: Addr e -> Count e
countAddr addr :: Addr e
addr@(Addr Addr#
_ Bytes 'Pin
b) = Bytes 'Pin -> Count e
forall e (p :: Pinned). Prim e => Bytes p -> Count e
countBytes Bytes 'Pin
b Count e -> Count e -> Count e
forall a. Num a => a -> a -> a
- Off e -> Count e
coerce (Addr e -> Off e
forall e. Prim e => Addr e -> Off e
curOffAddr Addr e
addr)

byteCountAddr :: Addr e -> Count Word8
byteCountAddr :: Addr e -> Count Word8
byteCountAddr = Addr Word8 -> Count Word8
forall e. Prim e => Addr e -> Count e
countAddr (Addr Word8 -> Count Word8)
-> (Addr e -> Addr Word8) -> Addr e -> Count Word8
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Addr e -> Addr Word8
forall e b. Addr e -> Addr b
castAddr

getCountMAddr :: (MonadPrim s m, Prim e) => MAddr e s -> m (Count e)
getCountMAddr :: MAddr e s -> m (Count e)
getCountMAddr maddr :: MAddr e s
maddr@(MAddr Addr#
_ MBytes 'Pin s
mb) =
  Count e -> Count e -> Count e
forall a. Num a => a -> a -> a
subtract (Off e -> Count e
coerce (MAddr e s -> Off e
forall e s. Prim e => MAddr e s -> Off e
curOffMAddr MAddr e s
maddr)) (Count e -> Count e) -> m (Count e) -> m (Count e)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> MBytes 'Pin s -> m (Count e)
forall s (m :: * -> *) e (p :: Pinned).
(MonadPrim s m, Prim e) =>
MBytes p s -> m (Count e)
getCountMBytes MBytes 'Pin s
mb

getByteCountMAddr :: MonadPrim s m => MAddr e s -> m (Count Word8)
getByteCountMAddr :: MAddr e s -> m (Count Word8)
getByteCountMAddr = MAddr Word8 s -> m (Count Word8)
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
MAddr e s -> m (Count e)
getCountMAddr (MAddr Word8 s -> m (Count Word8))
-> (MAddr e s -> MAddr Word8 s) -> MAddr e s -> m (Count Word8)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MAddr e s -> MAddr Word8 s
forall e s b. MAddr e s -> MAddr b s
castMAddr

indexAddr :: Prim e => Addr e -> e
indexAddr :: Addr e -> e
indexAddr Addr e
addr = Addr e -> Off e -> e
forall e. Prim e => Addr e -> Off e -> e
indexOffAddr Addr e
addr Off e
0
{-# INLINE indexAddr #-}

indexOffAddr :: Prim e => Addr e -> Off e -> e
indexOffAddr :: Addr e -> Off e -> e
indexOffAddr Addr e
addr (Off (I# Int#
off#)) =
  IO e -> e
forall a. IO a -> a
unsafeInlineIO (IO e -> e) -> IO e -> e
forall a b. (a -> b) -> a -> b
$ Addr e -> (Addr# -> IO e) -> IO e
forall s (m :: * -> *) e b.
MonadPrim s m =>
Addr e -> (Addr# -> m b) -> m b
withAddrAddr# Addr e
addr ((Addr# -> IO e) -> IO e) -> (Addr# -> IO e) -> IO e
forall a b. (a -> b) -> a -> b
$ \Addr#
addr# -> e -> IO e
forall (f :: * -> *) a. Applicative f => a -> f a
pure (e -> IO e) -> e -> IO e
forall a b. (a -> b) -> a -> b
$ Addr# -> Int# -> e
forall a. Prim a => Addr# -> Int# -> a
indexOffAddr# Addr#
addr# Int#
off#
{-# INLINE indexOffAddr #-}

indexByteOffAddr :: Prim e => Addr e -> Off Word8 -> e
indexByteOffAddr :: Addr e -> Off Word8 -> e
indexByteOffAddr Addr e
addr Off Word8
off = IO e -> e
forall a. IO a -> a
unsafeInlineIO (IO e -> e) -> IO e -> e
forall a b. (a -> b) -> a -> b
$ Addr e -> Off Word8 -> IO e
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
Addr e -> Off Word8 -> m e
readByteOffAddr Addr e
addr Off Word8
off

withPtrAddr :: MonadPrim s m => Addr e -> (Ptr e -> m b) -> m b
withPtrAddr :: Addr e -> (Ptr e -> m b) -> m b
withPtrAddr Addr e
addr Ptr e -> m b
f = Addr e -> (Addr# -> m b) -> m b
forall s (m :: * -> *) e b.
MonadPrim s m =>
Addr e -> (Addr# -> m b) -> m b
withAddrAddr# Addr e
addr ((Addr# -> m b) -> m b) -> (Addr# -> m b) -> m b
forall a b. (a -> b) -> a -> b
$ \Addr#
addr# -> Ptr e -> m b
f (Addr# -> Ptr e
forall a. Addr# -> Ptr a
Ptr Addr#
addr#)
{-# INLINE withPtrAddr #-}

withAddrAddr# :: MonadPrim s m => Addr e -> (Addr# -> m b) -> m b
withAddrAddr# :: Addr e -> (Addr# -> m b) -> m b
withAddrAddr# (Addr Addr#
addr# Bytes 'Pin
b) Addr# -> m b
f = do
  b
a <- Addr# -> m b
f Addr#
addr#
  b
a b -> m () -> m b
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Bytes 'Pin -> m ()
forall s (m :: * -> *) a. MonadPrim s m => a -> m ()
touch Bytes 'Pin
b
{-# INLINE withAddrAddr# #-}

withNoHaltPtrAddr :: MonadUnliftPrim s m => Addr e -> (Ptr e -> m b) -> m b
withNoHaltPtrAddr :: Addr e -> (Ptr e -> m b) -> m b
withNoHaltPtrAddr (Addr Addr#
addr# Bytes 'Pin
b) Ptr e -> m b
f = Bytes 'Pin -> m b -> m b
forall s (m :: * -> *) a b. MonadUnliftPrim s m => a -> m b -> m b
keepAlive Bytes 'Pin
b (m b -> m b) -> m b -> m b
forall a b. (a -> b) -> a -> b
$ Ptr e -> m b
f (Addr# -> Ptr e
forall a. Addr# -> Ptr a
Ptr Addr#
addr#)
{-# INLINE withNoHaltPtrAddr #-}

curOffMAddr :: forall e s . Prim e => MAddr e s -> Off e
curOffMAddr :: MAddr e s -> Off e
curOffMAddr (MAddr Addr#
addr# MBytes 'Pin s
mb) = (Addr# -> Ptr e
forall a. Addr# -> Ptr a
Ptr Addr#
addr# :: Ptr e) Ptr e -> Ptr e -> Off e
forall e. Prim e => Ptr e -> Ptr e -> Off e
`minusOffPtr` MBytes 'Pin s -> Ptr e
forall s e. MBytes 'Pin s -> Ptr e
toPtrMBytes MBytes 'Pin s
mb

curByteOffMAddr :: forall e s . MAddr e s -> Off Word8
curByteOffMAddr :: MAddr e s -> Off Word8
curByteOffMAddr (MAddr Addr#
addr# MBytes 'Pin s
mb) = (Addr# -> Ptr e
forall a. Addr# -> Ptr a
Ptr Addr#
addr# :: Ptr e) Ptr e -> Ptr e -> Off Word8
forall e. Ptr e -> Ptr e -> Off Word8
`minusByteOffPtr` MBytes 'Pin s -> Ptr e
forall s e. MBytes 'Pin s -> Ptr e
toPtrMBytes MBytes 'Pin s
mb

withPtrMAddr :: MonadPrim s m => MAddr e s -> (Ptr e -> m b) -> m b
withPtrMAddr :: MAddr e s -> (Ptr e -> m b) -> m b
withPtrMAddr MAddr e s
maddr Ptr e -> m b
f = MAddr e s -> (Addr# -> m b) -> m b
forall s (m :: * -> *) e b.
MonadPrim s m =>
MAddr e s -> (Addr# -> m b) -> m b
withAddrMAddr# MAddr e s
maddr ((Addr# -> m b) -> m b) -> (Addr# -> m b) -> m b
forall a b. (a -> b) -> a -> b
$ \Addr#
addr# -> Ptr e -> m b
f (Addr# -> Ptr e
forall a. Addr# -> Ptr a
Ptr Addr#
addr#)
{-# INLINE withPtrMAddr #-}



toForeignPtrAddr :: Addr e -> ForeignPtr e
toForeignPtrAddr :: Addr e -> ForeignPtr e
toForeignPtrAddr (Addr Addr#
addr# (Bytes ByteArray#
ba#)) = Addr# -> ForeignPtrContents -> ForeignPtr e
forall a. Addr# -> ForeignPtrContents -> ForeignPtr a
ForeignPtr Addr#
addr# (MutableByteArray# RealWorld -> ForeignPtrContents
PlainPtr (ByteArray# -> MutableByteArray# RealWorld
unsafeCoerce# ByteArray#
ba#))


toForeignPtrMAddr :: MAddr e s -> ForeignPtr e
toForeignPtrMAddr :: MAddr e s -> ForeignPtr e
toForeignPtrMAddr (MAddr Addr#
addr# (MBytes MutableByteArray# s
mba#)) = Addr# -> ForeignPtrContents -> ForeignPtr e
forall a. Addr# -> ForeignPtrContents -> ForeignPtr a
ForeignPtr Addr#
addr# (MutableByteArray# RealWorld -> ForeignPtrContents
PlainPtr (MutableByteArray# s -> MutableByteArray# RealWorld
unsafeCoerce# MutableByteArray# s
mba#))

-- | This is a unsafe cast therefore modification of `ForeignPtr` will be reflected in
-- resulting immutable `Addr`. Pointer created with @malloc@ cannot be converted to `Addr`
-- and will result in `Nothing`
--
-- @since 0.1.0
fromForeignPtrAddr :: ForeignPtr e -> Maybe (Addr e)
fromForeignPtrAddr :: ForeignPtr e -> Maybe (Addr e)
fromForeignPtrAddr ForeignPtr e
fptr =
  IO (Maybe (Addr e)) -> Maybe (Addr e)
forall a. IO a -> a
unsafePerformIO (IO (Maybe (Addr e)) -> Maybe (Addr e))
-> IO (Maybe (Addr e)) -> Maybe (Addr e)
forall a b. (a -> b) -> a -> b
$ ForeignPtr e -> IO (Maybe (MAddr e RealWorld))
forall e. ForeignPtr e -> IO (Maybe (MAddr e RealWorld))
fromForeignPtrIO ForeignPtr e
fptr IO (Maybe (MAddr e RealWorld))
-> (Maybe (MAddr e RealWorld) -> IO (Maybe (Addr e)))
-> IO (Maybe (Addr e))
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (MAddr e RealWorld -> IO (Addr e))
-> Maybe (MAddr e RealWorld) -> IO (Maybe (Addr e))
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse MAddr e RealWorld -> IO (Addr e)
forall s (m :: * -> *) e. MonadPrim s m => MAddr e s -> m (Addr e)
freezeMAddr


-- | Discarding the original ForeignPtr will trigger finalizers that were attached to it,
-- because `MAddr` does not retain any finalizers. Pointer created with @malloc@ cannot be
-- converted to `MAddr` and will result in `Nothing`
--
-- @since 0.1.0
fromForeignPtrMAddr :: ForeignPtr e -> Maybe (MAddr e s)
fromForeignPtrMAddr :: ForeignPtr e -> Maybe (MAddr e s)
fromForeignPtrMAddr ForeignPtr e
fptr =
  IO (Maybe (MAddr e s)) -> Maybe (MAddr e s)
forall a. IO a -> a
unsafePerformIO ((MAddr e RealWorld -> MAddr e s)
-> Maybe (MAddr e RealWorld) -> Maybe (MAddr e s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap MAddr e RealWorld -> MAddr e s
forall e s' b s. MAddr e s' -> MAddr b s
castStateMAddr (Maybe (MAddr e RealWorld) -> Maybe (MAddr e s))
-> IO (Maybe (MAddr e RealWorld)) -> IO (Maybe (MAddr e s))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ForeignPtr e -> IO (Maybe (MAddr e RealWorld))
forall e. ForeignPtr e -> IO (Maybe (MAddr e RealWorld))
fromForeignPtrIO ForeignPtr e
fptr)
  -- case c of
  --   PlainPtr mba#    -> Just (MAddr addr# (MBytes (unsafeCoerce# mba#)))
  --   MallocPtr mba# _ -> Just (MAddr addr# (MBytes (unsafeCoerce# mba#)))
  --   _                -> Nothing


fromForeignPtrIO :: ForeignPtr e -> IO (Maybe (MAddr e RW))
fromForeignPtrIO :: ForeignPtr e -> IO (Maybe (MAddr e RealWorld))
fromForeignPtrIO ForeignPtr e
fptr =
  ForeignPtr e
-> (Addr#
    -> MutableByteArray# RealWorld
    -> IO Bool
    -> IO (Maybe (MAddr e RealWorld)))
-> (Addr# -> IO (Maybe (MAddr e RealWorld)))
-> IO (Maybe (MAddr e RealWorld))
forall (m :: * -> *) e a.
MonadPrim RealWorld m =>
ForeignPtr e
-> (Addr# -> MutableByteArray# RealWorld -> m Bool -> m a)
-> (Addr# -> m a)
-> m a
onForeignPtrContents ForeignPtr e
fptr Addr#
-> MutableByteArray# RealWorld
-> IO Bool
-> IO (Maybe (MAddr e RealWorld))
forall (m :: * -> *) s e.
Monad m =>
Addr# -> MutableByteArray# s -> m Bool -> m (Maybe (MAddr e s))
checkConvert ((Addr# -> IO (Maybe (MAddr e RealWorld)))
 -> IO (Maybe (MAddr e RealWorld)))
-> (Addr# -> IO (Maybe (MAddr e RealWorld)))
-> IO (Maybe (MAddr e RealWorld))
forall a b. (a -> b) -> a -> b
$ \Addr#
_ -> Maybe (MAddr e RealWorld) -> IO (Maybe (MAddr e RealWorld))
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe (MAddr e RealWorld)
forall a. Maybe a
Nothing
  where
    checkConvert :: Addr# -> MutableByteArray# s -> m Bool -> m (Maybe (MAddr e s))
checkConvert Addr#
addr# MutableByteArray# s
mba# m Bool
checkFinalizers = do
      Bool
hasFinalizers <- m Bool
checkFinalizers
      Maybe (MAddr e s) -> m (Maybe (MAddr e s))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe (MAddr e s) -> m (Maybe (MAddr e s)))
-> Maybe (MAddr e s) -> m (Maybe (MAddr e s))
forall a b. (a -> b) -> a -> b
$
        if Bool
hasFinalizers
          then Maybe (MAddr e s)
forall a. Maybe a
Nothing
          else MAddr e s -> Maybe (MAddr e s)
forall a. a -> Maybe a
Just (Addr# -> MBytes 'Pin s -> MAddr e s
forall e s. Addr# -> MBytes 'Pin s -> MAddr e s
MAddr Addr#
addr# (MutableByteArray# s -> MBytes 'Pin s
forall (p :: Pinned) s. MutableByteArray# s -> MBytes p s
MBytes MutableByteArray# s
mba#))

withAddrMAddr# :: MonadPrim s m => MAddr e s -> (Addr# -> m b) -> m b
withAddrMAddr# :: MAddr e s -> (Addr# -> m b) -> m b
withAddrMAddr# (MAddr Addr#
addr# MBytes 'Pin s
mb) Addr# -> m b
f = do
  b
a <- Addr# -> m b
f Addr#
addr#
  b
a b -> m () -> m b
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ MBytes 'Pin s -> m ()
forall s (m :: * -> *) a. MonadPrim s m => a -> m ()
touch MBytes 'Pin s
mb
{-# INLINE withAddrMAddr# #-}

withNoHaltPtrMAddr :: MonadUnliftPrim s m => MAddr e s -> (Ptr e -> m b) -> m b
withNoHaltPtrMAddr :: MAddr e s -> (Ptr e -> m b) -> m b
withNoHaltPtrMAddr (MAddr Addr#
addr# MBytes 'Pin s
mb) Ptr e -> m b
f = MBytes 'Pin s -> m b -> m b
forall s (m :: * -> *) a b. MonadUnliftPrim s m => a -> m b -> m b
keepAlive MBytes 'Pin s
mb (m b -> m b) -> m b -> m b
forall a b. (a -> b) -> a -> b
$ Ptr e -> m b
f (Addr# -> Ptr e
forall a. Addr# -> Ptr a
Ptr Addr#
addr#)
{-# INLINE withNoHaltPtrMAddr #-}



-- | Read-only access, but it is not enforced.
instance PtrAccess s (Addr e) where
  toForeignPtr :: Addr e -> m (ForeignPtr a)
toForeignPtr = ForeignPtr a -> m (ForeignPtr a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ForeignPtr a -> m (ForeignPtr a))
-> (Addr e -> ForeignPtr a) -> Addr e -> m (ForeignPtr a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Addr a -> ForeignPtr a
forall e. Addr e -> ForeignPtr e
toForeignPtrAddr (Addr a -> ForeignPtr a)
-> (Addr e -> Addr a) -> Addr e -> ForeignPtr a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Addr e -> Addr a
forall e b. Addr e -> Addr b
castAddr
  {-# INLINE toForeignPtr #-}
  withPtrAccess :: Addr e -> (Ptr a -> m b) -> m b
withPtrAccess Addr e
addr = Addr a -> (Ptr a -> m b) -> m b
forall s (m :: * -> *) e b.
MonadPrim s m =>
Addr e -> (Ptr e -> m b) -> m b
withPtrAddr (Addr e -> Addr a
forall e b. Addr e -> Addr b
castAddr Addr e
addr)
  {-# INLINE withPtrAccess #-}
  withNoHaltPtrAccess :: Addr e -> (Ptr a -> m b) -> m b
withNoHaltPtrAccess Addr e
addr = Addr a -> (Ptr a -> m b) -> m b
forall s (m :: * -> *) e b.
MonadUnliftPrim s m =>
Addr e -> (Ptr e -> m b) -> m b
withNoHaltPtrAddr (Addr e -> Addr a
forall e b. Addr e -> Addr b
castAddr Addr e
addr)
  {-# INLINE withNoHaltPtrAccess #-}

instance PtrAccess s (MAddr e s) where
  toForeignPtr :: MAddr e s -> m (ForeignPtr a)
toForeignPtr = ForeignPtr a -> m (ForeignPtr a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ForeignPtr a -> m (ForeignPtr a))
-> (MAddr e s -> ForeignPtr a) -> MAddr e s -> m (ForeignPtr a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MAddr a s -> ForeignPtr a
forall e s. MAddr e s -> ForeignPtr e
toForeignPtrMAddr (MAddr a s -> ForeignPtr a)
-> (MAddr e s -> MAddr a s) -> MAddr e s -> ForeignPtr a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MAddr e s -> MAddr a s
forall e s b. MAddr e s -> MAddr b s
castMAddr
  {-# INLINE toForeignPtr #-}
  withPtrAccess :: MAddr e s -> (Ptr a -> m b) -> m b
withPtrAccess MAddr e s
maddr = MAddr a s -> (Ptr a -> m b) -> m b
forall s (m :: * -> *) e b.
MonadPrim s m =>
MAddr e s -> (Ptr e -> m b) -> m b
withPtrMAddr (MAddr e s -> MAddr a s
forall e s b. MAddr e s -> MAddr b s
castMAddr MAddr e s
maddr)
  {-# INLINE withPtrAccess #-}
  withNoHaltPtrAccess :: MAddr e s -> (Ptr a -> m b) -> m b
withNoHaltPtrAccess MAddr e s
maddr = MAddr a s -> (Ptr a -> m b) -> m b
forall s (m :: * -> *) e b.
MonadUnliftPrim s m =>
MAddr e s -> (Ptr e -> m b) -> m b
withNoHaltPtrMAddr (MAddr e s -> MAddr a s
forall e s b. MAddr e s -> MAddr b s
castMAddr MAddr e s
maddr)
  {-# INLINE withNoHaltPtrAccess #-}



instance MemAlloc (MAddr e) where
  type FrozenMem (MAddr e) = Addr e
  getByteCountMutMem :: MAddr e s -> m (Count Word8)
getByteCountMutMem = MAddr e s -> m (Count Word8)
forall s (m :: * -> *) e.
MonadPrim s m =>
MAddr e s -> m (Count Word8)
getByteCountMAddr
  {-# INLINE getByteCountMutMem #-}
  allocMutMem :: Count e -> m (MAddr e s)
allocMutMem = (MAddr e s -> MAddr e s) -> m (MAddr e s) -> m (MAddr e s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap MAddr e s -> MAddr e s
forall e s b. MAddr e s -> MAddr b s
castMAddr (m (MAddr e s) -> m (MAddr e s))
-> (Count e -> m (MAddr e s)) -> Count e -> m (MAddr e s)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Count e -> m (MAddr e s)
forall e (m :: * -> *) s.
(MonadPrim s m, Prim e) =>
Count e -> m (MAddr e s)
allocMAddr
  {-# INLINE allocMutMem #-}
  thawMem :: FrozenMem (MAddr e) -> m (MAddr e s)
thawMem = FrozenMem (MAddr e) -> m (MAddr e s)
forall s (m :: * -> *) e. MonadPrim s m => Addr e -> m (MAddr e s)
thawAddr
  {-# INLINE thawMem #-}
  freezeMutMem :: MAddr e s -> m (FrozenMem (MAddr e))
freezeMutMem = MAddr e s -> m (FrozenMem (MAddr e))
forall s (m :: * -> *) e. MonadPrim s m => MAddr e s -> m (Addr e)
freezeMAddr
  {-# INLINE freezeMutMem #-}
  reallocMutMem :: MAddr e s -> Count e -> m (MAddr e s)
reallocMutMem MAddr e s
maddr = (MAddr e s -> MAddr e s) -> m (MAddr e s) -> m (MAddr e s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap MAddr e s -> MAddr e s
forall e s b. MAddr e s -> MAddr b s
castMAddr (m (MAddr e s) -> m (MAddr e s))
-> (Count e -> m (MAddr e s)) -> Count e -> m (MAddr e s)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MAddr e s -> Count e -> m (MAddr e s)
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
MAddr e s -> Count e -> m (MAddr e s)
reallocMAddr (MAddr e s -> MAddr e s
forall e s b. MAddr e s -> MAddr b s
castMAddr MAddr e s
maddr)
  {-# INLINE reallocMutMem #-}


instance MemRead (Addr e) where
  isSameMem :: Addr e -> Addr e -> Bool
isSameMem = Addr e -> Addr e -> Bool
forall e. Addr e -> Addr e -> Bool
isSameAddr
  {-# INLINE isSameMem #-}
  byteCountMem :: Addr e -> Count Word8
byteCountMem = Addr e -> Count Word8
forall e. Addr e -> Count Word8
byteCountAddr
  {-# INLINE byteCountMem #-}
  indexOffMem :: Addr e -> Off e -> e
indexOffMem Addr e
a 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
$ Addr e -> (Addr# -> IO e) -> IO e
forall s (m :: * -> *) e b.
MonadPrim s m =>
Addr e -> (Addr# -> m b) -> m b
withAddrAddr# Addr e
a ((Addr# -> IO e) -> IO e) -> (Addr# -> IO e) -> IO e
forall a b. (a -> b) -> a -> b
$ \Addr#
addr# -> Ptr e -> Off e -> IO e
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
Ptr e -> Off e -> m e
readOffPtr (Addr# -> Ptr e
forall a. Addr# -> Ptr a
Ptr Addr#
addr#) Off e
i
  {-# INLINE indexOffMem #-}
  indexByteOffMem :: Addr e -> Off Word8 -> e
indexByteOffMem Addr e
a 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
$ Addr e -> (Addr# -> IO e) -> IO e
forall s (m :: * -> *) e b.
MonadPrim s m =>
Addr e -> (Addr# -> m b) -> m b
withAddrAddr# Addr e
a ((Addr# -> IO e) -> IO e) -> (Addr# -> IO e) -> IO e
forall a b. (a -> b) -> a -> b
$ \Addr#
addr# -> Ptr e -> Off Word8 -> IO e
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
Ptr e -> Off Word8 -> m e
readByteOffPtr (Addr# -> Ptr e
forall a. Addr# -> Ptr a
Ptr Addr#
addr#) Off Word8
i
  {-# INLINE indexByteOffMem #-}
  copyByteOffToMBytesMem :: Addr e -> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
copyByteOffToMBytesMem Addr e
a Off Word8
si MBytes p s
mb Off Word8
di Count e
c =
    Addr e -> (Ptr e -> m ()) -> m ()
forall s (m :: * -> *) e b.
MonadPrim s m =>
Addr e -> (Ptr e -> m b) -> m b
withPtrAddr Addr e
a ((Ptr e -> m ()) -> m ()) -> (Ptr e -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Ptr e
ptr -> 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 -> Ptr e
forall a b. Ptr a -> Ptr b
castPtr Ptr e
ptr) Off Word8
si MBytes p s
mb Off Word8
di Count e
c
  {-# INLINE copyByteOffToMBytesMem #-}
  copyByteOffToPtrMem :: Addr e -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
copyByteOffToPtrMem Addr e
a Off Word8
si Ptr e
mb Off Word8
di Count e
c =
    Addr e -> (Ptr e -> m ()) -> m ()
forall s (m :: * -> *) e b.
MonadPrim s m =>
Addr e -> (Ptr e -> m b) -> m b
withPtrAddr Addr e
a ((Ptr e -> m ()) -> m ()) -> (Ptr e -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Ptr e
ptr -> 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 -> Ptr e
forall a b. Ptr a -> Ptr b
castPtr Ptr e
ptr) Off Word8
si Ptr e
mb Off Word8
di Count e
c
  {-# INLINE copyByteOffToPtrMem #-}
  compareByteOffToPtrMem :: Addr e -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m Ordering
compareByteOffToPtrMem Addr e
addr Off Word8
off1 Ptr e
ptr2 Off Word8
off2 Count e
c =
    Addr e -> (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 Addr e
addr ((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 :: Addr e -> Off Word8 -> Bytes p -> Off Word8 -> Count e -> Ordering
compareByteOffToBytesMem Addr e
addr 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
$ Addr e -> (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 Addr e
addr ((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 -> Addr e -> Off Word8 -> Count e -> Ordering
compareByteOffMem mr'
mem1 Off Word8
off1 Addr e
addr 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
$ Addr e -> (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 Addr e
addr ((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 (MAddr e) where
  isSameMutMem :: MAddr e s -> MAddr e s -> Bool
isSameMutMem = MAddr e s -> MAddr e s -> Bool
forall e s. MAddr e s -> MAddr e s -> Bool
isSameMAddr
  {-# INLINE isSameMutMem #-}
  readOffMutMem :: MAddr e s -> Off e -> m e
readOffMutMem MAddr e s
a = MAddr e s -> Off e -> m e
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
MAddr e s -> Off e -> m e
readOffMAddr (MAddr e s -> MAddr e s
forall e s b. MAddr e s -> MAddr b s
castMAddr MAddr e s
a)
  {-# INLINE readOffMutMem #-}
  readByteOffMutMem :: MAddr e s -> Off Word8 -> m e
readByteOffMutMem MAddr e s
a = MAddr e s -> Off Word8 -> m e
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
MAddr e s -> Off Word8 -> m e
readByteOffMAddr (MAddr e s -> MAddr e s
forall e s b. MAddr e s -> MAddr b s
castMAddr MAddr e s
a)
  {-# INLINE readByteOffMutMem #-}
  writeOffMutMem :: MAddr e s -> Off e -> e -> m ()
writeOffMutMem MAddr e s
a = MAddr e s -> Off e -> e -> m ()
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
MAddr e s -> Off e -> e -> m ()
writeOffMAddr (MAddr e s -> MAddr e s
forall e s b. MAddr e s -> MAddr b s
castMAddr MAddr e s
a)
  {-# INLINE writeOffMutMem #-}
  writeByteOffMutMem :: MAddr e s -> Off Word8 -> e -> m ()
writeByteOffMutMem MAddr e s
a = MAddr e s -> Off Word8 -> e -> m ()
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
MAddr e s -> Off Word8 -> e -> m ()
writeByteOffMAddr (MAddr e s -> MAddr e s
forall e s b. MAddr e s -> MAddr b s
castMAddr MAddr e s
a)
  {-# INLINE writeByteOffMutMem #-}
  moveByteOffToPtrMutMem :: MAddr e s -> Off Word8 -> Ptr e -> Off Word8 -> Count e -> m ()
moveByteOffToPtrMutMem MAddr e s
src Off Word8
srcOff Ptr e
dstPtr Off Word8
dstOff Count e
c =
    MAddr e s -> (Addr# -> m ()) -> m ()
forall s (m :: * -> *) e b.
MonadPrim s m =>
MAddr e s -> (Addr# -> m b) -> m b
withAddrMAddr# MAddr e s
src ((Addr# -> m ()) -> m ()) -> (Addr# -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \ Addr#
srcAddr# ->
      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 (Addr# -> Ptr e
forall a. Addr# -> Ptr a
Ptr Addr#
srcAddr#) Off Word8
srcOff Ptr e
dstPtr Off Word8
dstOff Count e
c
  {-# INLINE moveByteOffToPtrMutMem #-}
  moveByteOffToMBytesMutMem :: MAddr e s
-> Off Word8 -> MBytes p s -> Off Word8 -> Count e -> m ()
moveByteOffToMBytesMutMem MAddr e s
src Off Word8
srcOff MBytes p s
dst Off Word8
dstOff Count e
c =
    MAddr e s -> (Addr# -> m ()) -> m ()
forall s (m :: * -> *) e b.
MonadPrim s m =>
MAddr e s -> (Addr# -> m b) -> m b
withAddrMAddr# MAddr e s
src ((Addr# -> m ()) -> m ()) -> (Addr# -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \ Addr#
srcAddr# ->
      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 (Addr# -> Ptr e
forall a. Addr# -> Ptr a
Ptr Addr#
srcAddr#) Off Word8
srcOff MBytes p s
dst Off Word8
dstOff Count e
c
  {-# INLINE moveByteOffToMBytesMutMem #-}
  copyByteOffMem :: mr -> Off Word8 -> MAddr e s -> Off Word8 -> Count e -> m ()
copyByteOffMem mr
src Off Word8
srcOff MAddr e s
dst Off Word8
dstOff Count e
c =
    MAddr e s -> (Addr# -> m ()) -> m ()
forall s (m :: * -> *) e b.
MonadPrim s m =>
MAddr e s -> (Addr# -> m b) -> m b
withAddrMAddr# MAddr e s
dst ((Addr# -> m ()) -> m ()) -> (Addr# -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \ Addr#
dstAddr# ->
      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 (Addr# -> Ptr e
forall a. Addr# -> Ptr a
Ptr Addr#
dstAddr#) Off Word8
dstOff Count e
c
  {-# INLINE copyByteOffMem #-}
  moveByteOffMutMem :: mw' s -> Off Word8 -> MAddr e s -> Off Word8 -> Count e -> m ()
moveByteOffMutMem mw' s
src Off Word8
srcOff MAddr e s
dst Off Word8
dstOff Count e
c =
    MAddr e s -> (Addr# -> m ()) -> m ()
forall s (m :: * -> *) e b.
MonadPrim s m =>
MAddr e s -> (Addr# -> m b) -> m b
withAddrMAddr# MAddr e s
dst ((Addr# -> m ()) -> m ()) -> (Addr# -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \ Addr#
dstAddr# ->
      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 (Addr# -> Ptr e
forall a. Addr# -> Ptr a
Ptr Addr#
dstAddr#) Off Word8
dstOff Count e
c
  {-# INLINE moveByteOffMutMem #-}
  setMutMem :: MAddr e s -> Off e -> Count e -> e -> m ()
setMutMem MAddr e s
maddr = MAddr e s -> Off e -> Count e -> e -> m ()
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
MAddr e s -> Off e -> Count e -> e -> m ()
setMAddr (MAddr e s -> MAddr e s
forall e s b. MAddr e s -> MAddr b s
castMAddr MAddr e s
maddr)
  {-# INLINE setMutMem #-}



thawAddr :: MonadPrim s m => Addr e -> m (MAddr e s)
thawAddr :: Addr e -> m (MAddr e s)
thawAddr (Addr Addr#
addr# Bytes 'Pin
b) = Addr# -> MBytes 'Pin s -> MAddr e s
forall e s. Addr# -> MBytes 'Pin s -> MAddr e s
MAddr Addr#
addr# (MBytes 'Pin s -> MAddr e s) -> m (MBytes 'Pin s) -> m (MAddr e s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Bytes 'Pin -> m (MBytes 'Pin s)
forall s (m :: * -> *) (p :: Pinned).
MonadPrim s m =>
Bytes p -> m (MBytes p s)
thawBytes Bytes 'Pin
b
{-# INLINE thawAddr #-}

freezeMAddr :: MonadPrim s m => MAddr e s -> m (Addr e)
freezeMAddr :: MAddr e s -> m (Addr e)
freezeMAddr (MAddr Addr#
addr# MBytes 'Pin s
mb) = Addr# -> Bytes 'Pin -> Addr e
forall e. Addr# -> Bytes 'Pin -> Addr e
Addr Addr#
addr# (Bytes 'Pin -> Addr e) -> m (Bytes 'Pin) -> m (Addr e)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> MBytes 'Pin s -> m (Bytes 'Pin)
forall s (m :: * -> *) (p :: Pinned).
MonadPrim s m =>
MBytes p s -> m (Bytes p)
freezeMBytes MBytes 'Pin s
mb
{-# INLINE freezeMAddr #-}


readAddr :: (MonadPrim s m, Prim e) => Addr e -> m e
readAddr :: Addr e -> m e
readAddr (Addr Addr#
addr# Bytes 'Pin
b) = do
  e
a <- (State# s -> (# State# s, e #)) -> m e
forall s (m :: * -> *) a.
MonadPrim s m =>
(State# s -> (# State# s, a #)) -> m a
prim (Addr# -> Int# -> State# s -> (# State# s, e #)
forall a s.
Prim a =>
Addr# -> Int# -> State# s -> (# State# s, a #)
readOffAddr# Addr#
addr# Int#
0#)
  e
a e -> m () -> m e
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Bytes 'Pin -> m ()
forall s (m :: * -> *) a. MonadPrim s m => a -> m ()
touch Bytes 'Pin
b
{-# INLINE readAddr #-}

readOffAddr :: (MonadPrim s m, Prim e) => Addr e -> Off e -> m e
readOffAddr :: Addr e -> Off e -> m e
readOffAddr (Addr Addr#
addr# Bytes 'Pin
b) (Off (I# Int#
off#)) = do
  e
a <- (State# s -> (# State# s, e #)) -> m e
forall s (m :: * -> *) a.
MonadPrim s m =>
(State# s -> (# State# s, a #)) -> m a
prim (Addr# -> Int# -> State# s -> (# State# s, e #)
forall a s.
Prim a =>
Addr# -> Int# -> State# s -> (# State# s, a #)
readOffAddr# Addr#
addr# Int#
off#)
  e
a e -> m () -> m e
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Bytes 'Pin -> m ()
forall s (m :: * -> *) a. MonadPrim s m => a -> m ()
touch Bytes 'Pin
b
{-# INLINE readOffAddr #-}

readByteOffAddr :: (MonadPrim s m, Prim e) => Addr e -> Off Word8 -> m e
readByteOffAddr :: Addr e -> Off Word8 -> m e
readByteOffAddr (Addr Addr#
addr# Bytes 'Pin
b) (Off (I# Int#
off#)) = do
  e
a <- (State# s -> (# State# s, e #)) -> m e
forall s (m :: * -> *) a.
MonadPrim s m =>
(State# s -> (# State# s, a #)) -> m a
prim (Addr# -> Int# -> State# s -> (# State# s, e #)
forall a s.
Prim a =>
Addr# -> Int# -> State# s -> (# State# s, a #)
readOffAddr# (Addr#
addr# Addr# -> Int# -> Addr#
`plusAddr#` Int#
off#) Int#
0#)
  e
a e -> m () -> m e
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Bytes 'Pin -> m ()
forall s (m :: * -> *) a. MonadPrim s m => a -> m ()
touch Bytes 'Pin
b
{-# INLINE readByteOffAddr #-}

readMAddr :: (MonadPrim s m, Prim e) => MAddr e s -> m e
readMAddr :: MAddr e s -> m e
readMAddr (MAddr Addr#
addr# MBytes 'Pin s
mb) = do
  e
a <- (State# s -> (# State# s, e #)) -> m e
forall s (m :: * -> *) a.
MonadPrim s m =>
(State# s -> (# State# s, a #)) -> m a
prim (Addr# -> Int# -> State# s -> (# State# s, e #)
forall a s.
Prim a =>
Addr# -> Int# -> State# s -> (# State# s, a #)
readOffAddr# Addr#
addr# Int#
0#)
  e
a e -> m () -> m e
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ MBytes 'Pin s -> m ()
forall s (m :: * -> *) a. MonadPrim s m => a -> m ()
touch MBytes 'Pin s
mb
{-# INLINE readMAddr #-}

readOffMAddr :: (MonadPrim s m, Prim e) => MAddr e s -> Off e -> m e
readOffMAddr :: MAddr e s -> Off e -> m e
readOffMAddr (MAddr Addr#
addr# MBytes 'Pin s
mb) (Off (I# Int#
off#)) = do
  e
a <- (State# s -> (# State# s, e #)) -> m e
forall s (m :: * -> *) a.
MonadPrim s m =>
(State# s -> (# State# s, a #)) -> m a
prim (Addr# -> Int# -> State# s -> (# State# s, e #)
forall a s.
Prim a =>
Addr# -> Int# -> State# s -> (# State# s, a #)
readOffAddr# Addr#
addr# Int#
off#)
  e
a e -> m () -> m e
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ MBytes 'Pin s -> m ()
forall s (m :: * -> *) a. MonadPrim s m => a -> m ()
touch MBytes 'Pin s
mb
{-# INLINE readOffMAddr #-}

readByteOffMAddr :: (MonadPrim s m, Prim e) => MAddr e s -> Off Word8 -> m e
readByteOffMAddr :: MAddr e s -> Off Word8 -> m e
readByteOffMAddr (MAddr Addr#
addr# MBytes 'Pin s
mb) (Off (I# Int#
off#)) = do
  e
a <- (State# s -> (# State# s, e #)) -> m e
forall s (m :: * -> *) a.
MonadPrim s m =>
(State# s -> (# State# s, a #)) -> m a
prim (Addr# -> Int# -> State# s -> (# State# s, e #)
forall a s.
Prim a =>
Addr# -> Int# -> State# s -> (# State# s, a #)
readOffAddr# (Addr#
addr# Addr# -> Int# -> Addr#
`plusAddr#` Int#
off#) Int#
0#)
  e
a e -> m () -> m e
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ MBytes 'Pin s -> m ()
forall s (m :: * -> *) a. MonadPrim s m => a -> m ()
touch MBytes 'Pin s
mb
{-# INLINE readByteOffMAddr #-}

writeMAddr :: (MonadPrim s m, Prim e) => MAddr e s -> e -> m ()
writeMAddr :: MAddr e s -> e -> m ()
writeMAddr (MAddr Addr#
addr# MBytes 'Pin s
mb) 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
$ \State# s
s -> MBytes 'Pin s -> State# s -> State# s
forall a s. a -> State# s -> State# s
touch# MBytes 'Pin s
mb (Addr# -> Int# -> e -> State# s -> State# s
forall a s. Prim a => Addr# -> Int# -> a -> State# s -> State# s
writeOffAddr# Addr#
addr# Int#
0# e
e State# s
s)
{-# INLINE writeMAddr #-}

writeOffMAddr :: (MonadPrim s m, Prim e) => MAddr e s -> Off e -> e -> m ()
writeOffMAddr :: MAddr e s -> Off e -> e -> m ()
writeOffMAddr (MAddr Addr#
addr# MBytes 'Pin s
mb) (Off (I# Int#
off#)) 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
$ \State# s
s -> MBytes 'Pin s -> State# s -> State# s
forall a s. a -> State# s -> State# s
touch# MBytes 'Pin s
mb (Addr# -> Int# -> e -> State# s -> State# s
forall a s. Prim a => Addr# -> Int# -> a -> State# s -> State# s
writeOffAddr# Addr#
addr# Int#
off# e
e State# s
s)
{-# INLINE writeOffMAddr #-}

writeByteOffMAddr :: (MonadPrim s m, Prim e) => MAddr e s -> Off Word8 -> e -> m ()
writeByteOffMAddr :: MAddr e s -> Off Word8 -> e -> m ()
writeByteOffMAddr (MAddr Addr#
addr# MBytes 'Pin s
mb) (Off (I# Int#
off#)) e
a =
  (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
$ \State# s
s -> MBytes 'Pin s -> State# s -> State# s
forall a s. a -> State# s -> State# s
touch# MBytes 'Pin s
mb (Addr# -> Int# -> e -> State# s -> State# s
forall a s. Prim a => Addr# -> Int# -> a -> State# s -> State# s
writeOffAddr# (Addr#
addr# Addr# -> Int# -> Addr#
`plusAddr#` Int#
off#) Int#
0# e
a State# s
s)
{-# INLINE writeByteOffMAddr #-}


copyAddrToMAddr ::
     (MonadPrim s m, Prim e) => Addr e -> Off e -> MAddr e s -> Off e -> Count e -> m ()
copyAddrToMAddr :: Addr e -> Off e -> MAddr e s -> Off e -> Count e -> m ()
copyAddrToMAddr Addr e
src Off e
srcOff MAddr e s
dst Off e
dstOff Count e
c =
  Addr e -> (Ptr e -> m ()) -> m ()
forall s (m :: * -> *) e b.
MonadPrim s m =>
Addr e -> (Ptr e -> m b) -> m b
withPtrAddr Addr e
src ((Ptr e -> m ()) -> m ()) -> (Ptr e -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \ Ptr e
srcPtr ->
    MAddr e s -> (Ptr e -> m ()) -> m ()
forall s (m :: * -> *) e b.
MonadPrim s m =>
MAddr e s -> (Ptr e -> m b) -> m b
withPtrMAddr MAddr e s
dst ((Ptr e -> m ()) -> m ()) -> (Ptr e -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \ Ptr e
dstPtr ->
      Ptr e -> Off e -> Ptr e -> Off e -> Count e -> m ()
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
Ptr e -> Off e -> Ptr e -> Off e -> Count e -> m ()
copyPtrToPtr Ptr e
srcPtr Off e
srcOff Ptr e
dstPtr Off e
dstOff Count e
c
{-# INLINE copyAddrToMAddr #-}

moveMAddrToMAddr ::
     (MonadPrim s m, Prim e) => MAddr e s -> Off e -> MAddr e s -> Off e -> Count e -> m ()
moveMAddrToMAddr :: MAddr e s -> Off e -> MAddr e s -> Off e -> Count e -> m ()
moveMAddrToMAddr MAddr e s
src Off e
srcOff MAddr e s
dst Off e
dstOff Count e
c =
  MAddr e s -> (Ptr e -> m ()) -> m ()
forall s (m :: * -> *) e b.
MonadPrim s m =>
MAddr e s -> (Ptr e -> m b) -> m b
withPtrMAddr MAddr e s
src ((Ptr e -> m ()) -> m ()) -> (Ptr e -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \ Ptr e
srcPtr ->
    MAddr e s -> (Ptr e -> m ()) -> m ()
forall s (m :: * -> *) e b.
MonadPrim s m =>
MAddr e s -> (Ptr e -> m b) -> m b
withPtrMAddr MAddr e s
dst ((Ptr e -> m ()) -> m ()) -> (Ptr e -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \ Ptr e
dstPtr ->
      Ptr e -> Off e -> Ptr e -> Off e -> Count e -> m ()
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
Ptr e -> Off e -> Ptr e -> Off e -> Count e -> m ()
movePtrToPtr Ptr e
srcPtr Off e
srcOff Ptr e
dstPtr Off e
dstOff Count e
c
{-# INLINE moveMAddrToMAddr #-}

setMAddr :: (MonadPrim s m, Prim e) => MAddr e s -> Off e -> Count e -> e -> m ()
setMAddr :: MAddr e s -> Off e -> Count e -> e -> m ()
setMAddr (MAddr Addr#
addr# MBytes 'Pin s
mb) (Off (I# Int#
off#)) (Count (I# Int#
n#)) e
a =
  (State# s -> State# s) -> m ()
forall s (m :: * -> *).
MonadPrim s m =>
(State# s -> State# s) -> m ()
prim_ (Addr# -> Int# -> Int# -> e -> State# s -> State# s
forall a s.
Prim a =>
Addr# -> Int# -> Int# -> a -> State# s -> State# s
setOffAddr# Addr#
addr# Int#
off# Int#
n# e
a) m () -> m () -> m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> MBytes 'Pin s -> m ()
forall s (m :: * -> *) a. MonadPrim s m => a -> m ()
touch MBytes 'Pin s
mb
{-# INLINE setMAddr #-}



-- | Apply a pure function to the contents of a mutable variable. Returns the artifact of
-- computation.
--
-- @since 0.2.0
modifyMAddr :: (MonadPrim s m, Prim a) => MAddr a s -> (a -> (a, b)) -> m b
modifyMAddr :: MAddr a s -> (a -> (a, b)) -> m b
modifyMAddr MAddr a s
maddr a -> (a, b)
f = MAddr a s -> (a -> m (a, b)) -> m b
forall s (m :: * -> *) a b.
(MonadPrim s m, Prim a) =>
MAddr a s -> (a -> m (a, b)) -> m b
modifyMAddrM MAddr a s
maddr ((a, b) -> m (a, b)
forall (m :: * -> *) a. Monad m => a -> m a
return ((a, b) -> m (a, b)) -> (a -> (a, b)) -> a -> m (a, b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> (a, b)
f)
{-# INLINE modifyMAddr #-}

-- | Apply a pure function to the contents of a mutable variable.
--
-- @since 0.1.0
modifyMAddr_ :: (MonadPrim s m, Prim a) => MAddr a s -> (a -> a) -> m ()
modifyMAddr_ :: MAddr a s -> (a -> a) -> m ()
modifyMAddr_ MAddr a s
maddr a -> a
f = MAddr a s -> (a -> m a) -> m ()
forall s (m :: * -> *) a.
(MonadPrim s m, Prim a) =>
MAddr a s -> (a -> m a) -> m ()
modifyMAddrM_ MAddr a s
maddr (a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> m a) -> (a -> a) -> a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> a
f)
{-# INLINE modifyMAddr_ #-}


-- | Apply a pure function to the contents of a mutable variable. Returns the old value.
--
-- @since 2.0.0
modifyFetchOldMAddr :: (MonadPrim s m, Prim a) => MAddr a s -> (a -> a) -> m a
modifyFetchOldMAddr :: MAddr a s -> (a -> a) -> m a
modifyFetchOldMAddr MAddr a s
maddr a -> a
f = MAddr a s -> (a -> m a) -> m a
forall s (m :: * -> *) a.
(MonadPrim s m, Prim a) =>
MAddr a s -> (a -> m a) -> m a
modifyFetchOldMAddrM MAddr a s
maddr (a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> m a) -> (a -> a) -> a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> a
f)
{-# INLINE modifyFetchOldMAddr #-}

-- | Apply a pure function to the contents of a mutable variable. Returns the new value.
--
-- @since 2.0.0
modifyFetchNewMAddr :: (MonadPrim s m, Prim a) => MAddr a s -> (a -> a) -> m a
modifyFetchNewMAddr :: MAddr a s -> (a -> a) -> m a
modifyFetchNewMAddr MAddr a s
maddr a -> a
f = MAddr a s -> (a -> m a) -> m a
forall s (m :: * -> *) a.
(MonadPrim s m, Prim a) =>
MAddr a s -> (a -> m a) -> m a
modifyFetchNewMAddrM MAddr a s
maddr (a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> m a) -> (a -> a) -> a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> a
f)
{-# INLINE modifyFetchNewMAddr #-}


-- | Apply a monadic action to the contents of a mutable variable. Returns the artifact of
-- computation.
--
-- @since 0.2.0
modifyMAddrM :: (MonadPrim s m, Prim a) => MAddr a s -> (a -> m (a, b)) -> m b
modifyMAddrM :: MAddr a s -> (a -> m (a, b)) -> m b
modifyMAddrM MAddr a s
maddr a -> m (a, b)
f = do
  a
a <- MAddr a s -> m a
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
MAddr e s -> m e
readMAddr MAddr a s
maddr
  (a
a', b
b) <- a -> m (a, b)
f a
a
  b
b b -> m () -> m b
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ MAddr a s -> a -> m ()
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
MAddr e s -> e -> m ()
writeMAddr MAddr a s
maddr a
a'
{-# INLINE modifyMAddrM #-}

-- | Apply a monadic action to the contents of a mutable variable. Returns the old value.
--
-- @since 2.0.0
modifyFetchOldMAddrM :: (MonadPrim s m, Prim a) => MAddr a s -> (a -> m a) -> m a
modifyFetchOldMAddrM :: MAddr a s -> (a -> m a) -> m a
modifyFetchOldMAddrM MAddr a s
maddr a -> m a
f = do
  a
a <- MAddr a s -> m a
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
MAddr e s -> m e
readMAddr MAddr a s
maddr
  a
a a -> m () -> m a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ (MAddr a s -> a -> m ()
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
MAddr e s -> e -> m ()
writeMAddr MAddr a s
maddr (a -> m ()) -> m a -> m ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< a -> m a
f a
a)
{-# INLINE modifyFetchOldMAddrM #-}


-- | Apply a monadic action to the contents of a mutable variable. Returns the new value.
--
-- @since 2.0.0
modifyFetchNewMAddrM :: (MonadPrim s m, Prim a) => MAddr a s -> (a -> m a) -> m a
modifyFetchNewMAddrM :: MAddr a s -> (a -> m a) -> m a
modifyFetchNewMAddrM MAddr a s
maddr a -> m a
f = do
  a
a <- MAddr a s -> m a
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
MAddr e s -> m e
readMAddr MAddr a s
maddr
  a
a' <- a -> m a
f a
a
  a
a' a -> m () -> m a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ MAddr a s -> a -> m ()
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
MAddr e s -> e -> m ()
writeMAddr MAddr a s
maddr a
a'
{-# INLINE modifyFetchNewMAddrM #-}


-- | Apply a monadic action to the contents of a mutable variable.
--
-- @since 0.1.0
modifyMAddrM_ :: (MonadPrim s m, Prim a) => MAddr a s -> (a -> m a) -> m ()
modifyMAddrM_ :: MAddr a s -> (a -> m a) -> m ()
modifyMAddrM_ MAddr a s
maddr a -> m a
f = MAddr a s -> m a
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
MAddr e s -> m e
readMAddr MAddr a s
maddr m a -> (a -> m a) -> m a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> m a
f m a -> (a -> m ()) -> m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= MAddr a s -> a -> m ()
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
MAddr e s -> e -> m ()
writeMAddr MAddr a s
maddr
{-# INLINE modifyMAddrM_ #-}

-- | Swap contents of two mutable variables. Returns their old values.
--
-- @since 0.1.0
swapMAddrs :: (MonadPrim s m, Prim a) => MAddr a s -> MAddr a s -> m (a, a)
swapMAddrs :: MAddr a s -> MAddr a s -> m (a, a)
swapMAddrs MAddr a s
maddr1 MAddr a s
maddr2 = do
  a
a1 <- MAddr a s -> m a
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
MAddr e s -> m e
readMAddr MAddr a s
maddr1
  a
a2 <- MAddr a s -> (a -> a) -> m a
forall s (m :: * -> *) a.
(MonadPrim s m, Prim a) =>
MAddr a s -> (a -> a) -> m a
modifyFetchOldMAddr MAddr a s
maddr2 (a -> a -> a
forall a b. a -> b -> a
const a
a1)
  (a
a1, a
a2) (a, a) -> m () -> m (a, a)
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ MAddr a s -> a -> m ()
forall s (m :: * -> *) e.
(MonadPrim s m, Prim e) =>
MAddr e s -> e -> m ()
writeMAddr MAddr a s
maddr1 a
a2
{-# INLINE swapMAddrs #-}

-- | Swap contents of two mutable variables.
--
-- @since 0.1.0
swapMAddrs_ :: (MonadPrim s m, Prim a) => MAddr a s -> MAddr a s -> m ()
swapMAddrs_ :: MAddr a s -> MAddr a s -> m ()
swapMAddrs_ MAddr a s
maddr1 MAddr a s
maddr2 = m (a, a) -> m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (m (a, a) -> m ()) -> m (a, a) -> m ()
forall a b. (a -> b) -> a -> b
$ MAddr a s -> MAddr a s -> m (a, a)
forall s (m :: * -> *) a.
(MonadPrim s m, Prim a) =>
MAddr a s -> MAddr a s -> m (a, a)
swapMAddrs MAddr a s
maddr1 MAddr a s
maddr2
{-# INLINE swapMAddrs_ #-}



-- | /O(1)/ - Cast an immutable `Addr` to an immutable `ByteString`
--
-- @since 0.1.0
toByteStringAddr :: Addr Word8 -> ByteString
toByteStringAddr :: Addr Word8 -> ByteString
toByteStringAddr Addr Word8
addr = ForeignPtr Word8 -> Int -> Int -> ByteString
PS (Addr Word8 -> ForeignPtr Word8
forall e. Addr e -> ForeignPtr e
toForeignPtrAddr Addr Word8
addr) Int
0 (Count Word8 -> Int
forall e. Count e -> Int
unCount (Addr Word8 -> Count Word8
forall e. Prim e => Addr e -> Count e
countAddr Addr Word8
addr))

-- | /O(1)/ - Cast an immutable `Addr` to an immutable `ShortByteString`
--
-- @since 0.1.0
toShortByteStringAddr :: Addr Word8 -> (ShortByteString, Off Word8)
toShortByteStringAddr :: Addr Word8 -> (ShortByteString, Off Word8)
toShortByteStringAddr = (Bytes 'Pin -> ShortByteString)
-> (Bytes 'Pin, Off Word8) -> (ShortByteString, Off Word8)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (b, d) (c, d)
first Bytes 'Pin -> ShortByteString
forall (p :: Pinned). Bytes p -> ShortByteString
toShortByteStringBytes ((Bytes 'Pin, Off Word8) -> (ShortByteString, Off Word8))
-> (Addr Word8 -> (Bytes 'Pin, Off Word8))
-> Addr Word8
-> (ShortByteString, Off Word8)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Addr Word8 -> (Bytes 'Pin, Off Word8)
forall e. Addr e -> (Bytes 'Pin, Off Word8)
toBytesAddr

-- | /O(n)/ - Convert an immutable `ShortByteString` to an immutable `Addr`. In a most common
-- case when `ShortByteString` is not backed by pinned memory, this function will return
-- `Nothing`.
--
-- @since 0.1.0
fromShortByteStringAddr :: ShortByteString -> Addr Word8
fromShortByteStringAddr :: ShortByteString -> Addr Word8
fromShortByteStringAddr = Bytes 'Pin -> Addr Word8
forall e. Bytes 'Pin -> Addr e
fromBytesAddr (Bytes 'Pin -> Addr Word8)
-> (ShortByteString -> Bytes 'Pin) -> ShortByteString -> Addr Word8
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bytes 'Inc -> Bytes 'Pin
forall (p :: Pinned). Bytes p -> Bytes 'Pin
ensurePinnedBytes (Bytes 'Inc -> Bytes 'Pin)
-> (ShortByteString -> Bytes 'Inc) -> ShortByteString -> Bytes 'Pin
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShortByteString -> Bytes 'Inc
fromShortByteStringBytes

-- | /O(1)/ - Cast an immutable `ByteString` to `Addr`. Also returns the original length of
-- ByteString, which will be less or equal to `countOfAddr` in the produced `Addr`.
--
-- @since 0.1.0
fromByteStringAddr :: ByteString -> (Addr Word8, Count Word8)
fromByteStringAddr :: ByteString -> (Addr Word8, Count Word8)
fromByteStringAddr (PS ForeignPtr Word8
fptr Int
i Int
n) =
  case ForeignPtr Word8 -> Maybe (Addr Word8)
forall e. ForeignPtr e -> Maybe (Addr e)
fromForeignPtrAddr ForeignPtr Word8
fptr of
    Just Addr Word8
addr -> (Addr Word8
addr Addr Word8 -> Off Word8 -> Addr Word8
forall e. Prim e => Addr e -> Off e -> Addr e
`plusOffAddr` Int -> Off Word8
forall e. Int -> Off e
Off Int
i, Int -> Count Word8
forall e. Int -> Count e
Count Int
n)
    Maybe (Addr Word8)
Nothing -> String -> (Addr Word8, Count Word8)
forall a. String -> a
byteStringConvertError String
"ByteString was allocated outside of 'bytestring' package"

-- | /O(1)/ - Cast an immutable `ByteString` to a mutable `MAddr`. Also returns the
-- original length of ByteString, which will be less or equal to `getCountOfMAddr` in the
-- produced `MAddr`.
--
-- __Unsafe__ - Further modification of `MAddr` will affect the source `ByteString`
--
-- @since 0.1.0
fromByteStringMAddr :: ByteString -> (MAddr Word8 s, Count Word8)
fromByteStringMAddr :: ByteString -> (MAddr Word8 s, Count Word8)
fromByteStringMAddr (PS ForeignPtr Word8
fptr Int
i Int
n) =
  case ForeignPtr Word8 -> Maybe (MAddr Word8 s)
forall e s. ForeignPtr e -> Maybe (MAddr e s)
fromForeignPtrMAddr ForeignPtr Word8
fptr of
    Just MAddr Word8 s
maddr -> (MAddr Word8 s
maddr MAddr Word8 s -> Off Word8 -> MAddr Word8 s
forall e s. Prim e => MAddr e s -> Off e -> MAddr e s
`plusOffMAddr` Int -> Off Word8
forall e. Int -> Off e
Off Int
i, Int -> Count Word8
forall e. Int -> Count e
Count Int
n)
    Maybe (MAddr Word8 s)
Nothing -> String -> (MAddr Word8 s, Count Word8)
forall a. String -> a
byteStringConvertError String
"It was allocated outside of 'bytestring' package"



-- | Perform atomic modification of an element in the `MAddr` 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
casOffMAddr ::
     (MonadPrim s m, Atomic e)
  => MAddr e s -- ^ Array to be mutated
  -> Off e -- ^ Index is in elements of @__e__@, rather than bytes.
  -> e -- ^ Expected old value
  -> e -- ^ New value
  -> m e
casOffMAddr :: MAddr e s -> Off e -> e -> e -> m e
casOffMAddr MAddr e s
maddr (Off (I# Int#
i#)) e
old e
new =
  MAddr e s -> (Addr# -> m e) -> m e
forall s (m :: * -> *) e b.
MonadPrim s m =>
MAddr e s -> (Addr# -> m b) -> m b
withAddrMAddr# MAddr e s
maddr ((Addr# -> m e) -> m e) -> (Addr# -> m e) -> m e
forall a b. (a -> b) -> a -> b
$ \ Addr#
addr# -> (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
$ Addr# -> Int# -> e -> e -> State# s -> (# State# s, e #)
forall a s.
Atomic a =>
Addr# -> Int# -> a -> a -> State# s -> (# State# s, a #)
casOffAddr# Addr#
addr# Int#
i# e
old e
new
{-# INLINE casOffMAddr #-}


-- | Perform atomic modification of an element in the `MAddr` 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
casBoolOffMAddr ::
     (MonadPrim s m, Atomic e)
  => MAddr e s -- ^ Array to be mutated
  -> Off e -- ^ Index is in elements of @__e__@, rather than bytes.
  -> e -- ^ Expected old value
  -> e -- ^ New value
  -> m Bool
casBoolOffMAddr :: MAddr e s -> Off e -> e -> e -> m Bool
casBoolOffMAddr MAddr e s
maddr (Off (I# Int#
i#)) e
old e
new =
  MAddr e s -> (Addr# -> m Bool) -> m Bool
forall s (m :: * -> *) e b.
MonadPrim s m =>
MAddr e s -> (Addr# -> m b) -> m b
withAddrMAddr# MAddr e s
maddr ((Addr# -> m Bool) -> m Bool) -> (Addr# -> m Bool) -> m Bool
forall a b. (a -> b) -> a -> b
$ \ Addr#
addr# -> (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
$ Addr# -> Int# -> e -> e -> State# s -> (# State# s, Bool #)
forall a s.
Atomic a =>
Addr# -> Int# -> a -> a -> State# s -> (# State# s, Bool #)
casBoolOffAddr# Addr#
addr# Int#
i# e
old e
new
{-# INLINE casBoolOffMAddr #-}

-- | Just like `casBoolOffMAddr`, 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
casBoolFetchOffMAddr ::
     (MonadPrim s m, Atomic e)
  => MAddr e s -- ^ Array to be mutated
  -> Off e -- ^ Index is in elements of @__e__@, rather than bytes.
  -> e -- ^ Expected old value
  -> e -- ^ New value
  -> m (Bool, e)
casBoolFetchOffMAddr :: MAddr e s -> Off e -> e -> e -> m (Bool, e)
casBoolFetchOffMAddr MAddr e s
maddr (Off (I# Int#
i#)) e
expected e
new = do
  MAddr e s -> (Addr# -> m (Bool, e)) -> m (Bool, e)
forall s (m :: * -> *) e b.
MonadPrim s m =>
MAddr e s -> (Addr# -> m b) -> m b
withAddrMAddr# MAddr e s
maddr ((Addr# -> m (Bool, e)) -> m (Bool, e))
-> (Addr# -> m (Bool, e)) -> m (Bool, e)
forall a b. (a -> b) -> a -> b
$ \Addr#
addr# ->
    (State# s -> (# State# s, (Bool, e) #)) -> m (Bool, e)
forall s (m :: * -> *) a.
MonadPrim s m =>
(State# s -> (# State# s, a #)) -> m a
prim ((State# s -> (# State# s, (Bool, e) #)) -> m (Bool, e))
-> (State# s -> (# State# s, (Bool, e) #)) -> m (Bool, e)
forall a b. (a -> b) -> a -> b
$ \State# s
s ->
      case Addr# -> Int# -> e -> e -> State# s -> (# State# s, Bool #)
forall a s.
Atomic a =>
Addr# -> Int# -> a -> a -> State# s -> (# State# s, Bool #)
casBoolOffAddr# Addr#
addr# Int#
i# e
expected e
new State# s
s of
        (# State# s
s', Bool
isCasSucc #)
          | Bool
isCasSucc -> (# State# s
s', (Bool
True, e
new) #)
          | Bool
otherwise ->
            case Addr# -> Int# -> State# s -> (# State# s, e #)
forall a s.
Prim a =>
Addr# -> Int# -> State# s -> (# State# s, a #)
readOffAddr# Addr#
addr# Int#
i# State# s
s' of
              (# State# s
s'', e
actual #) -> (# State# s
s'', (Bool
False, e
actual) #)
{-# INLINE casBoolFetchOffMAddr #-}


-- | Perform atomic read of an element in the `MAddr` at the supplied offset. 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
atomicReadOffMAddr ::
     (MonadPrim s m, Atomic e)
  => MAddr e s -- ^ Array to be mutated
  -> Off e -- ^ Index is in elements of @__e__@, rather than bytes.
  -> m e
atomicReadOffMAddr :: MAddr e s -> Off e -> m e
atomicReadOffMAddr MAddr e s
maddr (Off (I# Int#
i#)) =
  MAddr e s -> (Addr# -> m e) -> m e
forall s (m :: * -> *) e b.
MonadPrim s m =>
MAddr e s -> (Addr# -> m b) -> m b
withAddrMAddr# MAddr e s
maddr ((Addr# -> m e) -> m e) -> (Addr# -> m e) -> m e
forall a b. (a -> b) -> a -> b
$ \ Addr#
addr# -> (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
$ Addr# -> Int# -> State# s -> (# State# s, e #)
forall a s.
Atomic a =>
Addr# -> Int# -> State# s -> (# State# s, a #)
atomicReadOffAddr# Addr#
addr# Int#
i#
{-# INLINE atomicReadOffMAddr #-}

-- | Perform atomic write of an element in the `MAddr` at the supplied offset. 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
atomicWriteOffMAddr ::
     (MonadPrim s m, Atomic e)
  => MAddr e s -- ^ Array to be mutated
  -> Off e -- ^ Index is in elements of @__e__@, rather than bytes.
  -> e
  -> m ()
atomicWriteOffMAddr :: MAddr e s -> Off e -> e -> m ()
atomicWriteOffMAddr MAddr e s
maddr (Off (I# Int#
i#)) e
e =
  MAddr e s -> (Addr# -> m ()) -> m ()
forall s (m :: * -> *) e b.
MonadPrim s m =>
MAddr e s -> (Addr# -> m b) -> m b
withAddrMAddr# MAddr e s
maddr ((Addr# -> m ()) -> m ()) -> (Addr# -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \ Addr#
addr# -> (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
$ Addr# -> Int# -> e -> State# s -> State# s
forall a s. Atomic a => Addr# -> Int# -> a -> State# s -> State# s
atomicWriteOffAddr# Addr#
addr# Int#
i# e
e
{-# INLINE atomicWriteOffMAddr #-}


-- | Perform atomic modification of an element in the `MAddr` 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
atomicModifyOffMAddr ::
     (MonadPrim s m, Atomic e)
  => MAddr e s -- ^ Array to be mutated
  -> Off e -- ^ Index is in elements of @__e__@, 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
atomicModifyOffMAddr :: MAddr e s -> Off e -> (e -> (e, b)) -> m b
atomicModifyOffMAddr MAddr e s
maddr (Off (I# Int#
i#)) e -> (e, b)
f =
  MAddr e s -> (Addr# -> m b) -> m b
forall s (m :: * -> *) e b.
MonadPrim s m =>
MAddr e s -> (Addr# -> m b) -> m b
withAddrMAddr# MAddr e s
maddr ((Addr# -> m b) -> m b) -> (Addr# -> m b) -> m b
forall a b. (a -> b) -> a -> b
$ \ Addr#
addr# -> (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
$
  Addr# -> Int# -> (e -> (# e, b #)) -> State# s -> (# State# s, b #)
forall a b s.
Atomic a =>
Addr# -> Int# -> (a -> (# a, b #)) -> State# s -> (# State# s, b #)
atomicModifyOffAddr# Addr#
addr# 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 atomicModifyOffMAddr #-}

-- | Perform atomic modification of an element in the `MAddr` 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
atomicModifyOffMAddr_ ::
     (MonadPrim s m, Atomic e)
  => MAddr e s -- ^ Array to be mutated
  -> Off e -- ^ Index is in elements of @__e__@, rather than bytes.
  -> (e -> e) -- ^ Function that is applied to the current value
  -> m ()
atomicModifyOffMAddr_ :: MAddr e s -> Off e -> (e -> e) -> m ()
atomicModifyOffMAddr_ MAddr e s
maddr (Off (I# Int#
i#)) e -> e
f =
  MAddr e s -> (Addr# -> m ()) -> m ()
forall s (m :: * -> *) e b.
MonadPrim s m =>
MAddr e s -> (Addr# -> m b) -> m b
withAddrMAddr# MAddr e s
maddr ((Addr# -> m ()) -> m ()) -> (Addr# -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \ Addr#
addr# -> (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
$ Addr# -> Int# -> (e -> e) -> State# s -> State# s
forall a s.
Atomic a =>
Addr# -> Int# -> (a -> a) -> State# s -> State# s
atomicModifyOffAddr_# Addr#
addr# Int#
i# e -> e
f
{-# INLINE atomicModifyOffMAddr_ #-}


-- | Perform atomic modification of an element in the `MAddr` 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
atomicModifyFetchOldOffMAddr ::
     (MonadPrim s m, Atomic e)
  => MAddr e s -- ^ Array to be mutated
  -> Off e -- ^ Index is in elements of @__e__@, rather than bytes.
  -> (e -> e) -- ^ Function that is applied to the old value
  -> m e -- ^ Returns the old value
atomicModifyFetchOldOffMAddr :: MAddr e s -> Off e -> (e -> e) -> m e
atomicModifyFetchOldOffMAddr MAddr e s
maddr (Off (I# Int#
i#)) e -> e
f =
  MAddr e s -> (Addr# -> m e) -> m e
forall s (m :: * -> *) e b.
MonadPrim s m =>
MAddr e s -> (Addr# -> m b) -> m b
withAddrMAddr# MAddr e s
maddr ((Addr# -> m e) -> m e) -> (Addr# -> m e) -> m e
forall a b. (a -> b) -> a -> b
$ \ Addr#
addr# -> (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
$ Addr# -> Int# -> (e -> e) -> State# s -> (# State# s, e #)
forall a s.
Atomic a =>
Addr# -> Int# -> (a -> a) -> State# s -> (# State# s, a #)
atomicModifyFetchOldOffAddr# Addr#
addr# Int#
i# e -> e
f
{-# INLINE atomicModifyFetchOldOffMAddr #-}


-- | Perform atomic modification of an element in the `MAddr` 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
atomicModifyFetchNewOffMAddr ::
     (MonadPrim s m, Atomic e)
  => MAddr e s -- ^ Array to be mutated
  -> Off e -- ^ Index is in elements of @__e__@, rather than bytes
  -> (e -> e) -- ^ Function that is applied to the old value
  -> m e -- ^ Returns the new value
atomicModifyFetchNewOffMAddr :: MAddr e s -> Off e -> (e -> e) -> m e
atomicModifyFetchNewOffMAddr MAddr e s
maddr (Off (I# Int#
i#)) e -> e
f =
  MAddr e s -> (Addr# -> m e) -> m e
forall s (m :: * -> *) e b.
MonadPrim s m =>
MAddr e s -> (Addr# -> m b) -> m b
withAddrMAddr# MAddr e s
maddr ((Addr# -> m e) -> m e) -> (Addr# -> m e) -> m e
forall a b. (a -> b) -> a -> b
$ \ Addr#
addr# -> (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
$ Addr# -> Int# -> (e -> e) -> State# s -> (# State# s, e #)
forall a s.
Atomic a =>
Addr# -> Int# -> (a -> a) -> State# s -> (# State# s, a #)
atomicModifyFetchNewOffAddr# Addr#
addr# Int#
i# e -> e
f
{-# INLINE atomicModifyFetchNewOffMAddr #-}



-- | Add a numeric value to an element of a `MAddr`, 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
atomicAddFetchOldOffMAddr ::
     (MonadPrim s m, AtomicCount e)
  => MAddr e s
  -> Off e
  -> e
  -> m e
atomicAddFetchOldOffMAddr :: MAddr e s -> Off e -> e -> m e
atomicAddFetchOldOffMAddr MAddr e s
maddr (Off (I# Int#
i#)) e
a =
  MAddr e s -> (Addr# -> m e) -> m e
forall s (m :: * -> *) e b.
MonadPrim s m =>
MAddr e s -> (Addr# -> m b) -> m b
withAddrMAddr# MAddr e s
maddr ((Addr# -> m e) -> m e) -> (Addr# -> m e) -> m e
forall a b. (a -> b) -> a -> b
$ \ Addr#
addr# -> (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
$ Addr# -> Int# -> e -> State# s -> (# State# s, e #)
forall a s.
AtomicCount a =>
Addr# -> Int# -> a -> State# s -> (# State# s, a #)
atomicAddFetchOldOffAddr# Addr#
addr# Int#
i# e
a
{-# INLINE atomicAddFetchOldOffMAddr #-}

-- | Add a numeric value to an element of a `MAddr`, 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
atomicAddFetchNewOffMAddr ::
     (MonadPrim s m, AtomicCount e)
  => MAddr e s
  -> Off e
  -> e
  -> m e
atomicAddFetchNewOffMAddr :: MAddr e s -> Off e -> e -> m e
atomicAddFetchNewOffMAddr MAddr e s
maddr (Off (I# Int#
i#)) e
a =
  MAddr e s -> (Addr# -> m e) -> m e
forall s (m :: * -> *) e b.
MonadPrim s m =>
MAddr e s -> (Addr# -> m b) -> m b
withAddrMAddr# MAddr e s
maddr ((Addr# -> m e) -> m e) -> (Addr# -> m e) -> m e
forall a b. (a -> b) -> a -> b
$ \ Addr#
addr# -> (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
$ Addr# -> Int# -> e -> State# s -> (# State# s, e #)
forall a s.
AtomicCount a =>
Addr# -> Int# -> a -> State# s -> (# State# s, a #)
atomicAddFetchNewOffAddr# Addr#
addr# Int#
i# e
a
{-# INLINE atomicAddFetchNewOffMAddr #-}



-- | Subtract a numeric value from an element of a `MAddr`, 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
atomicSubFetchOldOffMAddr ::
     (MonadPrim s m, AtomicCount e)
  => MAddr e s
  -> Off e
  -> e
  -> m e
atomicSubFetchOldOffMAddr :: MAddr e s -> Off e -> e -> m e
atomicSubFetchOldOffMAddr MAddr e s
maddr (Off (I# Int#
i#)) e
a =
  MAddr e s -> (Addr# -> m e) -> m e
forall s (m :: * -> *) e b.
MonadPrim s m =>
MAddr e s -> (Addr# -> m b) -> m b
withAddrMAddr# MAddr e s
maddr ((Addr# -> m e) -> m e) -> (Addr# -> m e) -> m e
forall a b. (a -> b) -> a -> b
$ \ Addr#
addr# -> (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
$ Addr# -> Int# -> e -> State# s -> (# State# s, e #)
forall a s.
AtomicCount a =>
Addr# -> Int# -> a -> State# s -> (# State# s, a #)
atomicSubFetchOldOffAddr# Addr#
addr# Int#
i# e
a
{-# INLINE atomicSubFetchOldOffMAddr #-}

-- | Subtract a numeric value from an element of a `MAddr`, 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
atomicSubFetchNewOffMAddr ::
     (MonadPrim s m, AtomicCount e)
  => MAddr e s
  -> Off e
  -> e
  -> m e
atomicSubFetchNewOffMAddr :: MAddr e s -> Off e -> e -> m e
atomicSubFetchNewOffMAddr MAddr e s
maddr (Off (I# Int#
i#)) e
a =
  MAddr e s -> (Addr# -> m e) -> m e
forall s (m :: * -> *) e b.
MonadPrim s m =>
MAddr e s -> (Addr# -> m b) -> m b
withAddrMAddr# MAddr e s
maddr ((Addr# -> m e) -> m e) -> (Addr# -> m e) -> m e
forall a b. (a -> b) -> a -> b
$ \ Addr#
addr# -> (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
$ Addr# -> Int# -> e -> State# s -> (# State# s, e #)
forall a s.
AtomicCount a =>
Addr# -> Int# -> a -> State# s -> (# State# s, a #)
atomicSubFetchNewOffAddr# Addr#
addr# Int#
i# e
a
{-# INLINE atomicSubFetchNewOffMAddr #-}



-- | Binary conjunction (AND) of an element of a `MAddr` 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
atomicAndFetchOldOffMAddr ::
     (MonadPrim s m, AtomicBits e)
  => MAddr e s
  -> Off e
  -> e
  -> m e
atomicAndFetchOldOffMAddr :: MAddr e s -> Off e -> e -> m e
atomicAndFetchOldOffMAddr MAddr e s
maddr (Off (I# Int#
i#)) e
a =
  MAddr e s -> (Addr# -> m e) -> m e
forall s (m :: * -> *) e b.
MonadPrim s m =>
MAddr e s -> (Addr# -> m b) -> m b
withAddrMAddr# MAddr e s
maddr ((Addr# -> m e) -> m e) -> (Addr# -> m e) -> m e
forall a b. (a -> b) -> a -> b
$ \ Addr#
addr# -> (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
$ Addr# -> Int# -> e -> State# s -> (# State# s, e #)
forall a s.
AtomicBits a =>
Addr# -> Int# -> a -> State# s -> (# State# s, a #)
atomicAndFetchOldOffAddr# Addr#
addr# Int#
i# e
a
{-# INLINE atomicAndFetchOldOffMAddr #-}

-- | Binary conjunction (AND) of an element of a `MAddr` 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
atomicAndFetchNewOffMAddr ::
     (MonadPrim s m, AtomicBits e)
  => MAddr e s
  -> Off e
  -> e
  -> m e
atomicAndFetchNewOffMAddr :: MAddr e s -> Off e -> e -> m e
atomicAndFetchNewOffMAddr MAddr e s
maddr (Off (I# Int#
i#)) e
a =
  MAddr e s -> (Addr# -> m e) -> m e
forall s (m :: * -> *) e b.
MonadPrim s m =>
MAddr e s -> (Addr# -> m b) -> m b
withAddrMAddr# MAddr e s
maddr ((Addr# -> m e) -> m e) -> (Addr# -> m e) -> m e
forall a b. (a -> b) -> a -> b
$ \ Addr#
addr# -> (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
$ Addr# -> Int# -> e -> State# s -> (# State# s, e #)
forall a s.
AtomicBits a =>
Addr# -> Int# -> a -> State# s -> (# State# s, a #)
atomicAndFetchNewOffAddr# Addr#
addr# Int#
i# e
a
{-# INLINE atomicAndFetchNewOffMAddr #-}



-- | Negation of binary conjunction (NAND) of an element of a `MAddr` 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
atomicNandFetchOldOffMAddr ::
     (MonadPrim s m, AtomicBits e)
  => MAddr e s
  -> Off e
  -> e
  -> m e
atomicNandFetchOldOffMAddr :: MAddr e s -> Off e -> e -> m e
atomicNandFetchOldOffMAddr MAddr e s
maddr (Off (I# Int#
i#)) e
a =
  MAddr e s -> (Addr# -> m e) -> m e
forall s (m :: * -> *) e b.
MonadPrim s m =>
MAddr e s -> (Addr# -> m b) -> m b
withAddrMAddr# MAddr e s
maddr ((Addr# -> m e) -> m e) -> (Addr# -> m e) -> m e
forall a b. (a -> b) -> a -> b
$ \ Addr#
addr# -> (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
$ Addr# -> Int# -> e -> State# s -> (# State# s, e #)
forall a s.
AtomicBits a =>
Addr# -> Int# -> a -> State# s -> (# State# s, a #)
atomicNandFetchOldOffAddr# Addr#
addr# Int#
i# e
a
{-# INLINE atomicNandFetchOldOffMAddr #-}

-- | Negation of binary conjunction (NAND)  of an element of a `MAddr` 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
atomicNandFetchNewOffMAddr ::
     (MonadPrim s m, AtomicBits e)
  => MAddr e s
  -> Off e
  -> e
  -> m e
atomicNandFetchNewOffMAddr :: MAddr e s -> Off e -> e -> m e
atomicNandFetchNewOffMAddr MAddr e s
maddr (Off (I# Int#
i#)) e
a =
  MAddr e s -> (Addr# -> m e) -> m e
forall s (m :: * -> *) e b.
MonadPrim s m =>
MAddr e s -> (Addr# -> m b) -> m b
withAddrMAddr# MAddr e s
maddr ((Addr# -> m e) -> m e) -> (Addr# -> m e) -> m e
forall a b. (a -> b) -> a -> b
$ \ Addr#
addr# -> (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
$ Addr# -> Int# -> e -> State# s -> (# State# s, e #)
forall a s.
AtomicBits a =>
Addr# -> Int# -> a -> State# s -> (# State# s, a #)
atomicNandFetchNewOffAddr# Addr#
addr# Int#
i# e
a
{-# INLINE atomicNandFetchNewOffMAddr #-}




-- | Binary disjunction (OR) of an element of a `MAddr` 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
atomicOrFetchOldOffMAddr ::
     (MonadPrim s m, AtomicBits e)
  => MAddr e s
  -> Off e
  -> e
  -> m e
atomicOrFetchOldOffMAddr :: MAddr e s -> Off e -> e -> m e
atomicOrFetchOldOffMAddr MAddr e s
maddr (Off (I# Int#
i#)) e
a =
  MAddr e s -> (Addr# -> m e) -> m e
forall s (m :: * -> *) e b.
MonadPrim s m =>
MAddr e s -> (Addr# -> m b) -> m b
withAddrMAddr# MAddr e s
maddr ((Addr# -> m e) -> m e) -> (Addr# -> m e) -> m e
forall a b. (a -> b) -> a -> b
$ \ Addr#
addr# -> (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
$ Addr# -> Int# -> e -> State# s -> (# State# s, e #)
forall a s.
AtomicBits a =>
Addr# -> Int# -> a -> State# s -> (# State# s, a #)
atomicOrFetchOldOffAddr# Addr#
addr# Int#
i# e
a
{-# INLINE atomicOrFetchOldOffMAddr #-}

-- | Binary disjunction (OR) of an element of a `MAddr` 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
atomicOrFetchNewOffMAddr ::
     (MonadPrim s m, AtomicBits e)
  => MAddr e s
  -> Off e
  -> e
  -> m e
atomicOrFetchNewOffMAddr :: MAddr e s -> Off e -> e -> m e
atomicOrFetchNewOffMAddr MAddr e s
maddr (Off (I# Int#
i#)) e
a =
  MAddr e s -> (Addr# -> m e) -> m e
forall s (m :: * -> *) e b.
MonadPrim s m =>
MAddr e s -> (Addr# -> m b) -> m b
withAddrMAddr# MAddr e s
maddr ((Addr# -> m e) -> m e) -> (Addr# -> m e) -> m e
forall a b. (a -> b) -> a -> b
$ \ Addr#
addr# -> (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
$ Addr# -> Int# -> e -> State# s -> (# State# s, e #)
forall a s.
AtomicBits a =>
Addr# -> Int# -> a -> State# s -> (# State# s, a #)
atomicOrFetchNewOffAddr# Addr#
addr# Int#
i# e
a
{-# INLINE atomicOrFetchNewOffMAddr #-}



-- | Binary exclusive disjunction (XOR) of an element of a `MAddr` 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
atomicXorFetchOldOffMAddr ::
     (MonadPrim s m, AtomicBits e)
  => MAddr e s
  -> Off e
  -> e
  -> m e
atomicXorFetchOldOffMAddr :: MAddr e s -> Off e -> e -> m e
atomicXorFetchOldOffMAddr MAddr e s
maddr (Off (I# Int#
i#)) e
a =
  MAddr e s -> (Addr# -> m e) -> m e
forall s (m :: * -> *) e b.
MonadPrim s m =>
MAddr e s -> (Addr# -> m b) -> m b
withAddrMAddr# MAddr e s
maddr ((Addr# -> m e) -> m e) -> (Addr# -> m e) -> m e
forall a b. (a -> b) -> a -> b
$ \ Addr#
addr# -> (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
$ Addr# -> Int# -> e -> State# s -> (# State# s, e #)
forall a s.
AtomicBits a =>
Addr# -> Int# -> a -> State# s -> (# State# s, a #)
atomicXorFetchOldOffAddr# Addr#
addr# Int#
i# e
a
{-# INLINE atomicXorFetchOldOffMAddr #-}

-- | Binary exclusive disjunction (XOR) of an element of a `MAddr` 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
atomicXorFetchNewOffMAddr ::
     (MonadPrim s m, AtomicBits e)
  => MAddr e s
  -> Off e
  -> e
  -> m e
atomicXorFetchNewOffMAddr :: MAddr e s -> Off e -> e -> m e
atomicXorFetchNewOffMAddr MAddr e s
maddr (Off (I# Int#
i#)) e
a =
  MAddr e s -> (Addr# -> m e) -> m e
forall s (m :: * -> *) e b.
MonadPrim s m =>
MAddr e s -> (Addr# -> m b) -> m b
withAddrMAddr# MAddr e s
maddr ((Addr# -> m e) -> m e) -> (Addr# -> m e) -> m e
forall a b. (a -> b) -> a -> b
$ \ Addr#
addr# -> (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
$ Addr# -> Int# -> e -> State# s -> (# State# s, e #)
forall a s.
AtomicBits a =>
Addr# -> Int# -> a -> State# s -> (# State# s, a #)
atomicXorFetchNewOffAddr# Addr#
addr# Int#
i# e
a
{-# INLINE atomicXorFetchNewOffMAddr #-}





-- | Binary negation (NOT) of an element of a `MAddr`, 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
atomicNotFetchOldOffMAddr ::
     (MonadPrim s m, AtomicBits e)
  => MAddr e s
  -> Off e
  -> m e
atomicNotFetchOldOffMAddr :: MAddr e s -> Off e -> m e
atomicNotFetchOldOffMAddr MAddr e s
maddr (Off (I# Int#
i#)) =
  MAddr e s -> (Addr# -> m e) -> m e
forall s (m :: * -> *) e b.
MonadPrim s m =>
MAddr e s -> (Addr# -> m b) -> m b
withAddrMAddr# MAddr e s
maddr ((Addr# -> m e) -> m e) -> (Addr# -> m e) -> m e
forall a b. (a -> b) -> a -> b
$ \ Addr#
addr# -> (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
$ Addr# -> Int# -> State# s -> (# State# s, e #)
forall a s.
AtomicBits a =>
Addr# -> Int# -> State# s -> (# State# s, a #)
atomicNotFetchOldOffAddr# Addr#
addr# Int#
i#
{-# INLINE atomicNotFetchOldOffMAddr #-}

-- | Binary negation (NOT) of an element of a `MAddr`, 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
atomicNotFetchNewOffMAddr ::
     (MonadPrim s m, AtomicBits e)
  => MAddr e s
  -> Off e
  -> m e
atomicNotFetchNewOffMAddr :: MAddr e s -> Off e -> m e
atomicNotFetchNewOffMAddr MAddr e s
maddr (Off (I# Int#
i#)) =
  MAddr e s -> (Addr# -> m e) -> m e
forall s (m :: * -> *) e b.
MonadPrim s m =>
MAddr e s -> (Addr# -> m b) -> m b
withAddrMAddr# MAddr e s
maddr ((Addr# -> m e) -> m e) -> (Addr# -> m e) -> m e
forall a b. (a -> b) -> a -> b
$ \ Addr#
addr# -> (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
$ Addr# -> Int# -> State# s -> (# State# s, e #)
forall a s.
AtomicBits a =>
Addr# -> Int# -> State# s -> (# State# s, a #)
atomicNotFetchNewOffAddr# Addr#
addr# Int#
i#
{-# INLINE atomicNotFetchNewOffMAddr #-}




prefetchAddr0 :: MonadPrim s m => Addr e -> m ()
prefetchAddr0 :: Addr e -> m ()
prefetchAddr0 (Addr Addr#
addr# Bytes 'Pin
_) = (State# s -> State# s) -> m ()
forall s (m :: * -> *).
MonadPrim s m =>
(State# s -> State# s) -> m ()
prim_ (Addr# -> Int# -> State# s -> State# s
forall d. Addr# -> Int# -> State# d -> State# d
prefetchAddr0# Addr#
addr# Int#
0#)
{-# INLINE prefetchAddr0 #-}

prefetchMAddr0 :: MonadPrim s m => MAddr e s -> m ()
prefetchMAddr0 :: MAddr e s -> m ()
prefetchMAddr0 (MAddr Addr#
maddr# MBytes 'Pin s
_) = (State# s -> State# s) -> m ()
forall s (m :: * -> *).
MonadPrim s m =>
(State# s -> State# s) -> m ()
prim_ (Addr# -> Int# -> State# s -> State# s
forall d. Addr# -> Int# -> State# d -> State# d
prefetchAddr0# Addr#
maddr# Int#
0#)
{-# INLINE prefetchMAddr0 #-}

prefetchAddr1 :: MonadPrim s m => Addr e -> m ()
prefetchAddr1 :: Addr e -> m ()
prefetchAddr1 (Addr Addr#
addr# Bytes 'Pin
_) = (State# s -> State# s) -> m ()
forall s (m :: * -> *).
MonadPrim s m =>
(State# s -> State# s) -> m ()
prim_ (Addr# -> Int# -> State# s -> State# s
forall d. Addr# -> Int# -> State# d -> State# d
prefetchAddr1# Addr#
addr# Int#
0#)
{-# INLINE prefetchAddr1 #-}

prefetchMAddr1 :: MonadPrim s m => MAddr e s -> m ()
prefetchMAddr1 :: MAddr e s -> m ()
prefetchMAddr1 (MAddr Addr#
maddr# MBytes 'Pin s
_) = (State# s -> State# s) -> m ()
forall s (m :: * -> *).
MonadPrim s m =>
(State# s -> State# s) -> m ()
prim_ (Addr# -> Int# -> State# s -> State# s
forall d. Addr# -> Int# -> State# d -> State# d
prefetchAddr1# Addr#
maddr# Int#
0#)
{-# INLINE prefetchMAddr1 #-}

prefetchAddr2 :: MonadPrim s m => Addr e -> m ()
prefetchAddr2 :: Addr e -> m ()
prefetchAddr2 (Addr Addr#
addr# Bytes 'Pin
_) = (State# s -> State# s) -> m ()
forall s (m :: * -> *).
MonadPrim s m =>
(State# s -> State# s) -> m ()
prim_ (Addr# -> Int# -> State# s -> State# s
forall d. Addr# -> Int# -> State# d -> State# d
prefetchAddr2# Addr#
addr# Int#
0#)
{-# INLINE prefetchAddr2 #-}

prefetchMAddr2 :: MonadPrim s m => MAddr e s -> m ()
prefetchMAddr2 :: MAddr e s -> m ()
prefetchMAddr2 (MAddr Addr#
maddr# MBytes 'Pin s
_) = (State# s -> State# s) -> m ()
forall s (m :: * -> *).
MonadPrim s m =>
(State# s -> State# s) -> m ()
prim_ (Addr# -> Int# -> State# s -> State# s
forall d. Addr# -> Int# -> State# d -> State# d
prefetchAddr2# Addr#
maddr# Int#
0#)
{-# INLINE prefetchMAddr2 #-}

prefetchAddr3 :: MonadPrim s m => Addr e -> m ()
prefetchAddr3 :: Addr e -> m ()
prefetchAddr3 (Addr Addr#
addr# Bytes 'Pin
_) = (State# s -> State# s) -> m ()
forall s (m :: * -> *).
MonadPrim s m =>
(State# s -> State# s) -> m ()
prim_ (Addr# -> Int# -> State# s -> State# s
forall d. Addr# -> Int# -> State# d -> State# d
prefetchAddr3# Addr#
addr# Int#
0#)
{-# INLINE prefetchAddr3 #-}

prefetchMAddr3 :: MonadPrim s m => MAddr e s -> m ()
prefetchMAddr3 :: MAddr e s -> m ()
prefetchMAddr3 (MAddr Addr#
maddr# MBytes 'Pin s
_) = (State# s -> State# s) -> m ()
forall s (m :: * -> *).
MonadPrim s m =>
(State# s -> State# s) -> m ()
prim_ (Addr# -> Int# -> State# s -> State# s
forall d. Addr# -> Int# -> State# d -> State# d
prefetchAddr3# Addr#
maddr# Int#
0#)
{-# INLINE prefetchMAddr3 #-}


prefetchOffAddr0 :: (MonadPrim s m, Prim e) => Addr e -> Off e -> m ()
prefetchOffAddr0 :: Addr e -> Off e -> m ()
prefetchOffAddr0 (Addr Addr#
addr# Bytes 'Pin
_) Off e
off = (State# s -> State# s) -> m ()
forall s (m :: * -> *).
MonadPrim s m =>
(State# s -> State# s) -> m ()
prim_ (Addr# -> Int# -> State# s -> State# s
forall d. Addr# -> Int# -> State# d -> State# d
prefetchAddr0# Addr#
addr# (Off e -> Int#
forall e. Prim e => Off e -> Int#
unOffBytes# Off e
off))
{-# INLINE prefetchOffAddr0 #-}

prefetchOffMAddr0 :: (MonadPrim s m, Prim e) => MAddr e s -> Off e -> m ()
prefetchOffMAddr0 :: MAddr e s -> Off e -> m ()
prefetchOffMAddr0 (MAddr Addr#
maddr# MBytes 'Pin s
_) Off e
off = (State# s -> State# s) -> m ()
forall s (m :: * -> *).
MonadPrim s m =>
(State# s -> State# s) -> m ()
prim_ (Addr# -> Int# -> State# s -> State# s
forall d. Addr# -> Int# -> State# d -> State# d
prefetchAddr0# Addr#
maddr# (Off e -> Int#
forall e. Prim e => Off e -> Int#
unOffBytes# Off e
off))
{-# INLINE prefetchOffMAddr0 #-}

prefetchOffAddr1 :: (MonadPrim s m, Prim e) => Addr e -> Off e -> m ()
prefetchOffAddr1 :: Addr e -> Off e -> m ()
prefetchOffAddr1 (Addr Addr#
addr# Bytes 'Pin
_) Off e
off = (State# s -> State# s) -> m ()
forall s (m :: * -> *).
MonadPrim s m =>
(State# s -> State# s) -> m ()
prim_ (Addr# -> Int# -> State# s -> State# s
forall d. Addr# -> Int# -> State# d -> State# d
prefetchAddr1# Addr#
addr# (Off e -> Int#
forall e. Prim e => Off e -> Int#
unOffBytes# Off e
off))
{-# INLINE prefetchOffAddr1 #-}

prefetchOffMAddr1 :: (MonadPrim s m, Prim e) => MAddr e s -> Off e -> m ()
prefetchOffMAddr1 :: MAddr e s -> Off e -> m ()
prefetchOffMAddr1 (MAddr Addr#
maddr# MBytes 'Pin s
_) Off e
off = (State# s -> State# s) -> m ()
forall s (m :: * -> *).
MonadPrim s m =>
(State# s -> State# s) -> m ()
prim_ (Addr# -> Int# -> State# s -> State# s
forall d. Addr# -> Int# -> State# d -> State# d
prefetchAddr1# Addr#
maddr# (Off e -> Int#
forall e. Prim e => Off e -> Int#
unOffBytes# Off e
off))
{-# INLINE prefetchOffMAddr1 #-}

prefetchOffAddr2 :: (MonadPrim s m, Prim e) => Addr e -> Off e -> m ()
prefetchOffAddr2 :: Addr e -> Off e -> m ()
prefetchOffAddr2 (Addr Addr#
addr# Bytes 'Pin
_) Off e
off = (State# s -> State# s) -> m ()
forall s (m :: * -> *).
MonadPrim s m =>
(State# s -> State# s) -> m ()
prim_ (Addr# -> Int# -> State# s -> State# s
forall d. Addr# -> Int# -> State# d -> State# d
prefetchAddr2# Addr#
addr# (Off e -> Int#
forall e. Prim e => Off e -> Int#
unOffBytes# Off e
off))
{-# INLINE prefetchOffAddr2 #-}

prefetchOffMAddr2 :: (MonadPrim s m, Prim e) => MAddr e s -> Off e -> m ()
prefetchOffMAddr2 :: MAddr e s -> Off e -> m ()
prefetchOffMAddr2 (MAddr Addr#
maddr# MBytes 'Pin s
_) Off e
off = (State# s -> State# s) -> m ()
forall s (m :: * -> *).
MonadPrim s m =>
(State# s -> State# s) -> m ()
prim_ (Addr# -> Int# -> State# s -> State# s
forall d. Addr# -> Int# -> State# d -> State# d
prefetchAddr2# Addr#
maddr# (Off e -> Int#
forall e. Prim e => Off e -> Int#
unOffBytes# Off e
off))
{-# INLINE prefetchOffMAddr2 #-}

prefetchOffAddr3 :: (MonadPrim s m, Prim e) => Addr e -> Off e -> m ()
prefetchOffAddr3 :: Addr e -> Off e -> m ()
prefetchOffAddr3 (Addr Addr#
addr# Bytes 'Pin
_) Off e
off = (State# s -> State# s) -> m ()
forall s (m :: * -> *).
MonadPrim s m =>
(State# s -> State# s) -> m ()
prim_ (Addr# -> Int# -> State# s -> State# s
forall d. Addr# -> Int# -> State# d -> State# d
prefetchAddr3# Addr#
addr# (Off e -> Int#
forall e. Prim e => Off e -> Int#
unOffBytes# Off e
off))
{-# INLINE prefetchOffAddr3 #-}

prefetchOffMAddr3 :: (MonadPrim s m, Prim e) => MAddr e s -> Off e -> m ()
prefetchOffMAddr3 :: MAddr e s -> Off e -> m ()
prefetchOffMAddr3 (MAddr Addr#
maddr# MBytes 'Pin s
_) Off e
off = (State# s -> State# s) -> m ()
forall s (m :: * -> *).
MonadPrim s m =>
(State# s -> State# s) -> m ()
prim_ (Addr# -> Int# -> State# s -> State# s
forall d. Addr# -> Int# -> State# d -> State# d
prefetchAddr3# Addr#
maddr# (Off e -> Int#
forall e. Prim e => Off e -> Int#
unOffBytes# Off e
off))
{-# INLINE prefetchOffMAddr3 #-}