module Data.Primitive.Addr (
Addr(..),
nullAddr, plusAddr, minusAddr, remAddr,
indexOffAddr, readOffAddr, writeOffAddr,
memcpyAddr
) where
import Control.Monad.Primitive
import Data.Primitive.Types
import GHC.Base ( Int(..) )
import GHC.Prim
import GHC.Ptr
import Foreign.Marshal.Utils
nullAddr :: Addr
nullAddr = Addr nullAddr#
infixl 6 `plusAddr`, `minusAddr`
infixl 7 `remAddr`
plusAddr :: Addr -> Int -> Addr
plusAddr (Addr a#) (I# i#) = Addr (plusAddr# a# i#)
minusAddr :: Addr -> Addr -> Int
minusAddr (Addr a#) (Addr b#) = I# (minusAddr# a# b#)
remAddr :: Addr -> Int -> Int
remAddr (Addr a#) (I# i#) = I# (remAddr# a# i#)
indexOffAddr :: Prim a => Addr -> Int -> a
indexOffAddr (Addr addr#) (I# i#) = indexOffAddr# addr# i#
readOffAddr :: (Prim a, PrimMonad m) => Addr -> Int -> m a
readOffAddr (Addr addr#) (I# i#) = primitive (readOffAddr# addr# i#)
writeOffAddr :: (Prim a, PrimMonad m) => Addr -> Int -> a -> m ()
writeOffAddr (Addr addr#) (I# i#) x = primitive_ (writeOffAddr# addr# i# x)
memcpyAddr :: PrimMonad m => Addr -> Addr -> Int -> m ()
memcpyAddr (Addr dst#) (Addr src#) n
= unsafePrimToPrim $ copyBytes (Ptr dst#) (Ptr src#) n