{-# LANGUAGE BangPatterns, CPP, MagicHash, UnboxedTuples, UnliftedFFITypes, DeriveDataTypeable #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TemplateHaskellQuotes #-}
module Data.Primitive.ByteArray (
ByteArray(..), MutableByteArray(..), ByteArray#, MutableByteArray#,
newByteArray, newPinnedByteArray, newAlignedPinnedByteArray,
resizeMutableByteArray,
shrinkMutableByteArray,
readByteArray, writeByteArray, indexByteArray,
readCharArray, writeCharArray, indexCharArray,
emptyByteArray,
byteArrayFromList, byteArrayFromListN,
foldrByteArray,
compareByteArrays,
freezeByteArray, thawByteArray, runByteArray,
unsafeFreezeByteArray, unsafeThawByteArray,
copyByteArray, copyMutableByteArray,
copyByteArrayToPtr, copyMutableByteArrayToPtr,
copyByteArrayToAddr, copyMutableByteArrayToAddr,
copyPtrToMutableByteArray,
moveByteArray,
setByteArray, fillByteArray,
cloneByteArray, cloneMutableByteArray,
sizeofByteArray,
sizeofMutableByteArray, getSizeofMutableByteArray, sameMutableByteArray,
#if __GLASGOW_HASKELL__ >= 802
isByteArrayPinned, isMutableByteArrayPinned,
#endif
byteArrayContents, mutableByteArrayContents
) where
import Control.Monad.Primitive
import Control.Monad.ST
import Data.Primitive.Types
#if MIN_VERSION_base(4,10,0)
import qualified GHC.ST as GHCST
#endif
import Foreign.C.Types
import Data.Word ( Word8 )
#if __GLASGOW_HASKELL__ >= 802
import qualified GHC.Exts as Exts
#endif
import GHC.Exts hiding (setByteArray#)
#if __GLASGOW_HASKELL__ < 804
import System.IO.Unsafe (unsafeDupablePerformIO)
#endif
import Data.Array.Byte (ByteArray(..), MutableByteArray(..))
newByteArray :: PrimMonad m => Int -> m (MutableByteArray (PrimState m))
{-# INLINE newByteArray #-}
newByteArray :: forall (m :: * -> *).
PrimMonad m =>
Int -> m (MutableByteArray (PrimState m))
newByteArray (I# Int#
n#)
= forall (m :: * -> *) a.
PrimMonad m =>
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
primitive (\State# (PrimState m)
s# -> case forall d. Int# -> State# d -> (# State# d, MutableByteArray# d #)
newByteArray# Int#
n# State# (PrimState m)
s# of
(# State# (PrimState m)
s'#, MutableByteArray# (PrimState m)
arr# #) -> (# State# (PrimState m)
s'#, forall s. MutableByteArray# s -> MutableByteArray s
MutableByteArray MutableByteArray# (PrimState m)
arr# #))
newPinnedByteArray :: PrimMonad m => Int -> m (MutableByteArray (PrimState m))
{-# INLINE newPinnedByteArray #-}
newPinnedByteArray :: forall (m :: * -> *).
PrimMonad m =>
Int -> m (MutableByteArray (PrimState m))
newPinnedByteArray (I# Int#
n#)
= forall (m :: * -> *) a.
PrimMonad m =>
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
primitive (\State# (PrimState m)
s# -> case forall d. Int# -> State# d -> (# State# d, MutableByteArray# d #)
newPinnedByteArray# Int#
n# State# (PrimState m)
s# of
(# State# (PrimState m)
s'#, MutableByteArray# (PrimState m)
arr# #) -> (# State# (PrimState m)
s'#, forall s. MutableByteArray# s -> MutableByteArray s
MutableByteArray MutableByteArray# (PrimState m)
arr# #))
newAlignedPinnedByteArray
:: PrimMonad m
=> Int
-> Int
-> m (MutableByteArray (PrimState m))
{-# INLINE newAlignedPinnedByteArray #-}
newAlignedPinnedByteArray :: forall (m :: * -> *).
PrimMonad m =>
Int -> Int -> m (MutableByteArray (PrimState m))
newAlignedPinnedByteArray (I# Int#
n#) (I# Int#
k#)
= forall (m :: * -> *) a.
PrimMonad m =>
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
primitive (\State# (PrimState m)
s# -> case forall d.
Int# -> Int# -> State# d -> (# State# d, MutableByteArray# d #)
newAlignedPinnedByteArray# Int#
n# Int#
k# State# (PrimState m)
s# of
(# State# (PrimState m)
s'#, MutableByteArray# (PrimState m)
arr# #) -> (# State# (PrimState m)
s'#, forall s. MutableByteArray# s -> MutableByteArray s
MutableByteArray MutableByteArray# (PrimState m)
arr# #))
byteArrayContents :: ByteArray -> Ptr Word8
{-# INLINE byteArrayContents #-}
byteArrayContents :: ByteArray -> Ptr Word8
byteArrayContents (ByteArray ByteArray#
arr#) = forall a. Addr# -> Ptr a
Ptr (ByteArray# -> Addr#
byteArrayContents# ByteArray#
arr#)
mutableByteArrayContents :: MutableByteArray s -> Ptr Word8
{-# INLINE mutableByteArrayContents #-}
mutableByteArrayContents :: forall s. MutableByteArray s -> Ptr Word8
mutableByteArrayContents (MutableByteArray MutableByteArray# s
arr#)
= forall a. Addr# -> Ptr a
Ptr (ByteArray# -> Addr#
byteArrayContents# (unsafeCoerce# :: forall a b. a -> b
unsafeCoerce# MutableByteArray# s
arr#))
sameMutableByteArray :: MutableByteArray s -> MutableByteArray s -> Bool
{-# INLINE sameMutableByteArray #-}
sameMutableByteArray :: forall s. MutableByteArray s -> MutableByteArray s -> Bool
sameMutableByteArray (MutableByteArray MutableByteArray# s
arr#) (MutableByteArray MutableByteArray# s
brr#)
= Int# -> Bool
isTrue# (forall d. MutableByteArray# d -> MutableByteArray# d -> Int#
sameMutableByteArray# MutableByteArray# s
arr# MutableByteArray# s
brr#)
resizeMutableByteArray
:: PrimMonad m => MutableByteArray (PrimState m) -> Int
-> m (MutableByteArray (PrimState m))
{-# INLINE resizeMutableByteArray #-}
resizeMutableByteArray :: forall (m :: * -> *).
PrimMonad m =>
MutableByteArray (PrimState m)
-> Int -> m (MutableByteArray (PrimState m))
resizeMutableByteArray (MutableByteArray MutableByteArray# (PrimState m)
arr#) (I# Int#
n#)
= forall (m :: * -> *) a.
PrimMonad m =>
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
primitive (\State# (PrimState m)
s# -> case forall d.
MutableByteArray# d
-> Int# -> State# d -> (# State# d, MutableByteArray# d #)
resizeMutableByteArray# MutableByteArray# (PrimState m)
arr# Int#
n# State# (PrimState m)
s# of
(# State# (PrimState m)
s'#, MutableByteArray# (PrimState m)
arr'# #) -> (# State# (PrimState m)
s'#, forall s. MutableByteArray# s -> MutableByteArray s
MutableByteArray MutableByteArray# (PrimState m)
arr'# #))
getSizeofMutableByteArray
:: PrimMonad m => MutableByteArray (PrimState m) -> m Int
{-# INLINE getSizeofMutableByteArray #-}
#if __GLASGOW_HASKELL__ >= 801
getSizeofMutableByteArray :: forall (m :: * -> *).
PrimMonad m =>
MutableByteArray (PrimState m) -> m Int
getSizeofMutableByteArray (MutableByteArray MutableByteArray# (PrimState m)
arr#)
= forall (m :: * -> *) a.
PrimMonad m =>
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
primitive (\State# (PrimState m)
s# -> case forall d. MutableByteArray# d -> State# d -> (# State# d, Int# #)
getSizeofMutableByteArray# MutableByteArray# (PrimState m)
arr# State# (PrimState m)
s# of
(# State# (PrimState m)
s'#, Int#
n# #) -> (# State# (PrimState m)
s'#, Int# -> Int
I# Int#
n# #))
#else
getSizeofMutableByteArray arr
= return (sizeofMutableByteArray arr)
#endif
freezeByteArray
:: PrimMonad m
=> MutableByteArray (PrimState m)
-> Int
-> Int
-> m ByteArray
{-# INLINE freezeByteArray #-}
freezeByteArray :: forall (m :: * -> *).
PrimMonad m =>
MutableByteArray (PrimState m) -> Int -> Int -> m ByteArray
freezeByteArray !MutableByteArray (PrimState m)
src !Int
off !Int
len = do
MutableByteArray (PrimState m)
dst <- forall (m :: * -> *).
PrimMonad m =>
Int -> m (MutableByteArray (PrimState m))
newByteArray Int
len
forall (m :: * -> *).
PrimMonad m =>
MutableByteArray (PrimState m)
-> Int -> MutableByteArray (PrimState m) -> Int -> Int -> m ()
copyMutableByteArray MutableByteArray (PrimState m)
dst Int
0 MutableByteArray (PrimState m)
src Int
off Int
len
forall (m :: * -> *).
PrimMonad m =>
MutableByteArray (PrimState m) -> m ByteArray
unsafeFreezeByteArray MutableByteArray (PrimState m)
dst
thawByteArray
:: PrimMonad m
=> ByteArray
-> Int
-> Int
-> m (MutableByteArray (PrimState m))
{-# INLINE thawByteArray #-}
thawByteArray :: forall (m :: * -> *).
PrimMonad m =>
ByteArray -> Int -> Int -> m (MutableByteArray (PrimState m))
thawByteArray !ByteArray
src !Int
off !Int
len = do
MutableByteArray (PrimState m)
dst <- forall (m :: * -> *).
PrimMonad m =>
Int -> m (MutableByteArray (PrimState m))
newByteArray Int
len
forall (m :: * -> *).
PrimMonad m =>
MutableByteArray (PrimState m)
-> Int -> ByteArray -> Int -> Int -> m ()
copyByteArray MutableByteArray (PrimState m)
dst Int
0 ByteArray
src Int
off Int
len
forall (m :: * -> *) a. Monad m => a -> m a
return MutableByteArray (PrimState m)
dst
unsafeFreezeByteArray
:: PrimMonad m => MutableByteArray (PrimState m) -> m ByteArray
{-# INLINE unsafeFreezeByteArray #-}
unsafeFreezeByteArray :: forall (m :: * -> *).
PrimMonad m =>
MutableByteArray (PrimState m) -> m ByteArray
unsafeFreezeByteArray (MutableByteArray MutableByteArray# (PrimState m)
arr#)
= forall (m :: * -> *) a.
PrimMonad m =>
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
primitive (\State# (PrimState m)
s# -> case forall d.
MutableByteArray# d -> State# d -> (# State# d, ByteArray# #)
unsafeFreezeByteArray# MutableByteArray# (PrimState m)
arr# State# (PrimState m)
s# of
(# State# (PrimState m)
s'#, ByteArray#
arr'# #) -> (# State# (PrimState m)
s'#, ByteArray# -> ByteArray
ByteArray ByteArray#
arr'# #))
unsafeThawByteArray
:: PrimMonad m => ByteArray -> m (MutableByteArray (PrimState m))
{-# INLINE unsafeThawByteArray #-}
unsafeThawByteArray :: forall (m :: * -> *).
PrimMonad m =>
ByteArray -> m (MutableByteArray (PrimState m))
unsafeThawByteArray (ByteArray ByteArray#
arr#)
= forall (m :: * -> *) a.
PrimMonad m =>
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
primitive (\State# (PrimState m)
s# -> (# State# (PrimState m)
s#, forall s. MutableByteArray# s -> MutableByteArray s
MutableByteArray (unsafeCoerce# :: forall a b. a -> b
unsafeCoerce# ByteArray#
arr#) #))
sizeofByteArray :: ByteArray -> Int
{-# INLINE sizeofByteArray #-}
sizeofByteArray :: ByteArray -> Int
sizeofByteArray (ByteArray ByteArray#
arr#) = Int# -> Int
I# (ByteArray# -> Int#
sizeofByteArray# ByteArray#
arr#)
sizeofMutableByteArray :: MutableByteArray s -> Int
{-# INLINE sizeofMutableByteArray #-}
sizeofMutableByteArray :: forall s. MutableByteArray s -> Int
sizeofMutableByteArray (MutableByteArray MutableByteArray# s
arr#) = Int# -> Int
I# (forall d. MutableByteArray# d -> Int#
sizeofMutableByteArray# MutableByteArray# s
arr#)
shrinkMutableByteArray :: PrimMonad m
=> MutableByteArray (PrimState m)
-> Int
-> m ()
{-# INLINE shrinkMutableByteArray #-}
shrinkMutableByteArray :: forall (m :: * -> *).
PrimMonad m =>
MutableByteArray (PrimState m) -> Int -> m ()
shrinkMutableByteArray (MutableByteArray MutableByteArray# (PrimState m)
arr#) (I# Int#
n#)
= forall (m :: * -> *).
PrimMonad m =>
(State# (PrimState m) -> State# (PrimState m)) -> m ()
primitive_ (forall d. MutableByteArray# d -> Int# -> State# d -> State# d
shrinkMutableByteArray# MutableByteArray# (PrimState m)
arr# Int#
n#)
#if __GLASGOW_HASKELL__ >= 802
isByteArrayPinned :: ByteArray -> Bool
{-# INLINE isByteArrayPinned #-}
isByteArrayPinned :: ByteArray -> Bool
isByteArrayPinned (ByteArray ByteArray#
arr#) = Int# -> Bool
isTrue# (ByteArray# -> Int#
Exts.isByteArrayPinned# ByteArray#
arr#)
isMutableByteArrayPinned :: MutableByteArray s -> Bool
{-# INLINE isMutableByteArrayPinned #-}
isMutableByteArrayPinned :: forall s. MutableByteArray s -> Bool
isMutableByteArrayPinned (MutableByteArray MutableByteArray# s
marr#) = Int# -> Bool
isTrue# (forall d. MutableByteArray# d -> Int#
Exts.isMutableByteArrayPinned# MutableByteArray# s
marr#)
#endif
indexByteArray :: Prim a => ByteArray -> Int -> a
{-# INLINE indexByteArray #-}
indexByteArray :: forall a. Prim a => ByteArray -> Int -> a
indexByteArray (ByteArray ByteArray#
arr#) (I# Int#
i#) = forall a. Prim a => ByteArray# -> Int# -> a
indexByteArray# ByteArray#
arr# Int#
i#
readByteArray
:: (Prim a, PrimMonad m) => MutableByteArray (PrimState m) -> Int -> m a
{-# INLINE readByteArray #-}
readByteArray :: forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutableByteArray (PrimState m) -> Int -> m a
readByteArray (MutableByteArray MutableByteArray# (PrimState m)
arr#) (I# Int#
i#)
= forall (m :: * -> *) a.
PrimMonad m =>
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
primitive (forall a s.
Prim a =>
MutableByteArray# s -> Int# -> State# s -> (# State# s, a #)
readByteArray# MutableByteArray# (PrimState m)
arr# Int#
i#)
writeByteArray
:: (Prim a, PrimMonad m) => MutableByteArray (PrimState m) -> Int -> a -> m ()
{-# INLINE writeByteArray #-}
writeByteArray :: forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutableByteArray (PrimState m) -> Int -> a -> m ()
writeByteArray (MutableByteArray MutableByteArray# (PrimState m)
arr#) (I# Int#
i#) a
x
= forall (m :: * -> *).
PrimMonad m =>
(State# (PrimState m) -> State# (PrimState m)) -> m ()
primitive_ (forall a s.
Prim a =>
MutableByteArray# s -> Int# -> a -> State# s -> State# s
writeByteArray# MutableByteArray# (PrimState m)
arr# Int#
i# a
x)
foldrByteArray :: forall a b. (Prim a) => (a -> b -> b) -> b -> ByteArray -> b
{-# INLINE foldrByteArray #-}
foldrByteArray :: forall a b. Prim a => (a -> b -> b) -> b -> ByteArray -> b
foldrByteArray a -> b -> b
f b
z ByteArray
arr = Int -> b
go Int
0
where
go :: Int -> b
go Int
i
| Int
i forall a. Ord a => a -> a -> Bool
< Int
maxI = a -> b -> b
f (forall a. Prim a => ByteArray -> Int -> a
indexByteArray ByteArray
arr Int
i) (Int -> b
go (Int
i forall a. Num a => a -> a -> a
+ Int
1))
| Bool
otherwise = b
z
maxI :: Int
maxI = ByteArray -> Int
sizeofByteArray ByteArray
arr forall a. Integral a => a -> a -> a
`quot` forall a. Prim a => a -> Int
sizeOf (forall a. HasCallStack => a
undefined :: a)
byteArrayFromList :: Prim a => [a] -> ByteArray
byteArrayFromList :: forall a. Prim a => [a] -> ByteArray
byteArrayFromList [a]
xs = forall a. Prim a => Int -> [a] -> ByteArray
byteArrayFromListN (forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
xs) [a]
xs
byteArrayFromListN :: Prim a => Int -> [a] -> ByteArray
byteArrayFromListN :: forall a. Prim a => Int -> [a] -> ByteArray
byteArrayFromListN Int
n [a]
ys = forall a. (forall s. ST s a) -> a
runST forall a b. (a -> b) -> a -> b
$ do
MutableByteArray s
marr <- forall (m :: * -> *).
PrimMonad m =>
Int -> m (MutableByteArray (PrimState m))
newByteArray (Int
n forall a. Num a => a -> a -> a
* forall a. Prim a => a -> Int
sizeOf (forall a. [a] -> a
head [a]
ys))
let go :: Int -> [a] -> ST s ()
go !Int
ix [] = if Int
ix forall a. Eq a => a -> a -> Bool
== Int
n
then forall (m :: * -> *) a. Monad m => a -> m a
return ()
else forall a. String -> String -> a
die String
"byteArrayFromListN" String
"list length less than specified size"
go !Int
ix (a
x : [a]
xs) = if Int
ix forall a. Ord a => a -> a -> Bool
< Int
n
then do
forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutableByteArray (PrimState m) -> Int -> a -> m ()
writeByteArray MutableByteArray s
marr Int
ix a
x
Int -> [a] -> ST s ()
go (Int
ix forall a. Num a => a -> a -> a
+ Int
1) [a]
xs
else forall a. String -> String -> a
die String
"byteArrayFromListN" String
"list length greater than specified size"
Int -> [a] -> ST s ()
go Int
0 [a]
ys
forall (m :: * -> *).
PrimMonad m =>
MutableByteArray (PrimState m) -> m ByteArray
unsafeFreezeByteArray MutableByteArray s
marr
unI# :: Int -> Int#
unI# :: Int -> Int#
unI# (I# Int#
n#) = Int#
n#
copyByteArray
:: PrimMonad m
=> MutableByteArray (PrimState m)
-> Int
-> ByteArray
-> Int
-> Int
-> m ()
{-# INLINE copyByteArray #-}
copyByteArray :: forall (m :: * -> *).
PrimMonad m =>
MutableByteArray (PrimState m)
-> Int -> ByteArray -> Int -> Int -> m ()
copyByteArray (MutableByteArray MutableByteArray# (PrimState m)
dst#) Int
doff (ByteArray ByteArray#
src#) Int
soff Int
sz
= forall (m :: * -> *).
PrimMonad m =>
(State# (PrimState m) -> State# (PrimState m)) -> m ()
primitive_ (forall d.
ByteArray#
-> Int#
-> MutableByteArray# d
-> Int#
-> Int#
-> State# d
-> State# d
copyByteArray# ByteArray#
src# (Int -> Int#
unI# Int
soff) MutableByteArray# (PrimState m)
dst# (Int -> Int#
unI# Int
doff) (Int -> Int#
unI# Int
sz))
copyMutableByteArray
:: PrimMonad m
=> MutableByteArray (PrimState m)
-> Int
-> MutableByteArray (PrimState m)
-> Int
-> Int
-> m ()
{-# INLINE copyMutableByteArray #-}
copyMutableByteArray :: forall (m :: * -> *).
PrimMonad m =>
MutableByteArray (PrimState m)
-> Int -> MutableByteArray (PrimState m) -> Int -> Int -> m ()
copyMutableByteArray (MutableByteArray MutableByteArray# (PrimState m)
dst#) Int
doff
(MutableByteArray MutableByteArray# (PrimState m)
src#) Int
soff Int
sz
= forall (m :: * -> *).
PrimMonad m =>
(State# (PrimState m) -> State# (PrimState m)) -> m ()
primitive_ (forall d.
MutableByteArray# d
-> Int#
-> MutableByteArray# d
-> Int#
-> Int#
-> State# d
-> State# d
copyMutableByteArray# MutableByteArray# (PrimState m)
src# (Int -> Int#
unI# Int
soff) MutableByteArray# (PrimState m)
dst# (Int -> Int#
unI# Int
doff) (Int -> Int#
unI# Int
sz))
copyByteArrayToPtr
:: forall m a. (PrimMonad m, Prim a)
=> Ptr a
-> ByteArray
-> Int
-> Int
-> m ()
{-# INLINE copyByteArrayToPtr #-}
copyByteArrayToPtr :: forall (m :: * -> *) a.
(PrimMonad m, Prim a) =>
Ptr a -> ByteArray -> Int -> Int -> m ()
copyByteArrayToPtr (Ptr Addr#
dst#) (ByteArray ByteArray#
src#) Int
soff Int
sz
= forall (m :: * -> *).
PrimMonad m =>
(State# (PrimState m) -> State# (PrimState m)) -> m ()
primitive_ (forall d.
ByteArray# -> Int# -> Addr# -> Int# -> State# d -> State# d
copyByteArrayToAddr# ByteArray#
src# (Int -> Int#
unI# Int
soff Int# -> Int# -> Int#
*# Int#
siz#) Addr#
dst# (Int -> Int#
unI# Int
sz Int# -> Int# -> Int#
*# Int#
siz#))
where
siz# :: Int#
siz# = forall a. Prim a => a -> Int#
sizeOf# (forall a. HasCallStack => a
undefined :: a)
copyPtrToMutableByteArray :: forall m a. (PrimMonad m, Prim a)
=> MutableByteArray (PrimState m)
-> Int
-> Ptr a
-> Int
-> m ()
{-# INLINE copyPtrToMutableByteArray #-}
copyPtrToMutableByteArray :: forall (m :: * -> *) a.
(PrimMonad m, Prim a) =>
MutableByteArray (PrimState m) -> Int -> Ptr a -> Int -> m ()
copyPtrToMutableByteArray (MutableByteArray MutableByteArray# (PrimState m)
ba#) (I# Int#
doff#) (Ptr Addr#
addr#) (I# Int#
n#) =
forall (m :: * -> *).
PrimMonad m =>
(State# (PrimState m) -> State# (PrimState m)) -> m ()
primitive_ (forall d.
Addr#
-> MutableByteArray# d -> Int# -> Int# -> State# d -> State# d
copyAddrToByteArray# Addr#
addr# MutableByteArray# (PrimState m)
ba# (Int#
doff# Int# -> Int# -> Int#
*# Int#
siz#) (Int#
n# Int# -> Int# -> Int#
*# Int#
siz#))
where
siz# :: Int#
siz# = forall a. Prim a => a -> Int#
sizeOf# (forall a. HasCallStack => a
undefined :: a)
copyMutableByteArrayToPtr
:: forall m a. (PrimMonad m, Prim a)
=> Ptr a
-> MutableByteArray (PrimState m)
-> Int
-> Int
-> m ()
{-# INLINE copyMutableByteArrayToPtr #-}
copyMutableByteArrayToPtr :: forall (m :: * -> *) a.
(PrimMonad m, Prim a) =>
Ptr a -> MutableByteArray (PrimState m) -> Int -> Int -> m ()
copyMutableByteArrayToPtr (Ptr Addr#
dst#) (MutableByteArray MutableByteArray# (PrimState m)
src#) Int
soff Int
sz
= forall (m :: * -> *).
PrimMonad m =>
(State# (PrimState m) -> State# (PrimState m)) -> m ()
primitive_ (forall d.
MutableByteArray# d
-> Int# -> Addr# -> Int# -> State# d -> State# d
copyMutableByteArrayToAddr# MutableByteArray# (PrimState m)
src# (Int -> Int#
unI# Int
soff Int# -> Int# -> Int#
*# Int#
siz#) Addr#
dst# (Int -> Int#
unI# Int
sz Int# -> Int# -> Int#
*# Int#
siz#))
where
siz# :: Int#
siz# = forall a. Prim a => a -> Int#
sizeOf# (forall a. HasCallStack => a
undefined :: a)
copyByteArrayToAddr
:: PrimMonad m
=> Ptr Word8
-> ByteArray
-> Int
-> Int
-> m ()
{-# INLINE copyByteArrayToAddr #-}
copyByteArrayToAddr :: forall (m :: * -> *).
PrimMonad m =>
Ptr Word8 -> ByteArray -> Int -> Int -> m ()
copyByteArrayToAddr (Ptr Addr#
dst#) (ByteArray ByteArray#
src#) Int
soff Int
sz
= forall (m :: * -> *).
PrimMonad m =>
(State# (PrimState m) -> State# (PrimState m)) -> m ()
primitive_ (forall d.
ByteArray# -> Int# -> Addr# -> Int# -> State# d -> State# d
copyByteArrayToAddr# ByteArray#
src# (Int -> Int#
unI# Int
soff) Addr#
dst# (Int -> Int#
unI# Int
sz))
copyMutableByteArrayToAddr
:: PrimMonad m
=> Ptr Word8
-> MutableByteArray (PrimState m)
-> Int
-> Int
-> m ()
{-# INLINE copyMutableByteArrayToAddr #-}
copyMutableByteArrayToAddr :: forall (m :: * -> *).
PrimMonad m =>
Ptr Word8 -> MutableByteArray (PrimState m) -> Int -> Int -> m ()
copyMutableByteArrayToAddr (Ptr Addr#
dst#) (MutableByteArray MutableByteArray# (PrimState m)
src#) Int
soff Int
sz
= forall (m :: * -> *).
PrimMonad m =>
(State# (PrimState m) -> State# (PrimState m)) -> m ()
primitive_ (forall d.
MutableByteArray# d
-> Int# -> Addr# -> Int# -> State# d -> State# d
copyMutableByteArrayToAddr# MutableByteArray# (PrimState m)
src# (Int -> Int#
unI# Int
soff) Addr#
dst# (Int -> Int#
unI# Int
sz))
moveByteArray
:: PrimMonad m
=> MutableByteArray (PrimState m)
-> Int
-> MutableByteArray (PrimState m)
-> Int
-> Int
-> m ()
{-# INLINE moveByteArray #-}
moveByteArray :: forall (m :: * -> *).
PrimMonad m =>
MutableByteArray (PrimState m)
-> Int -> MutableByteArray (PrimState m) -> Int -> Int -> m ()
moveByteArray (MutableByteArray MutableByteArray# (PrimState m)
dst#) Int
doff
(MutableByteArray MutableByteArray# (PrimState m)
src#) Int
soff Int
sz
= forall (m1 :: * -> *) (m2 :: * -> *) a.
(PrimBase m1, PrimMonad m2) =>
m1 a -> m2 a
unsafePrimToPrim
forall a b. (a -> b) -> a -> b
$ forall s.
MutableByteArray# s
-> CPtrdiff -> MutableByteArray# s -> CPtrdiff -> CSize -> IO ()
memmove_mba MutableByteArray# (PrimState m)
dst# (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
doff) MutableByteArray# (PrimState m)
src# (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
soff)
(forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
sz)
setByteArray
:: (Prim a, PrimMonad m)
=> MutableByteArray (PrimState m)
-> Int
-> Int
-> a
-> m ()
{-# INLINE setByteArray #-}
setByteArray :: forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutableByteArray (PrimState m) -> Int -> Int -> a -> m ()
setByteArray (MutableByteArray MutableByteArray# (PrimState m)
dst#) (I# Int#
doff#) (I# Int#
sz#) a
x
= forall (m :: * -> *).
PrimMonad m =>
(State# (PrimState m) -> State# (PrimState m)) -> m ()
primitive_ (forall a s.
Prim a =>
MutableByteArray# s -> Int# -> Int# -> a -> State# s -> State# s
setByteArray# MutableByteArray# (PrimState m)
dst# Int#
doff# Int#
sz# a
x)
fillByteArray
:: PrimMonad m
=> MutableByteArray (PrimState m)
-> Int
-> Int
-> Word8
-> m ()
{-# INLINE fillByteArray #-}
fillByteArray :: forall (m :: * -> *).
PrimMonad m =>
MutableByteArray (PrimState m) -> Int -> Int -> Word8 -> m ()
fillByteArray = forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutableByteArray (PrimState m) -> Int -> Int -> a -> m ()
setByteArray
foreign import ccall unsafe "primitive-memops.h hsprimitive_memmove"
memmove_mba :: MutableByteArray# s -> CPtrdiff
-> MutableByteArray# s -> CPtrdiff
-> CSize -> IO ()
compareByteArrays
:: ByteArray
-> Int
-> ByteArray
-> Int
-> Int
-> Ordering
{-# INLINE compareByteArrays #-}
#if __GLASGOW_HASKELL__ >= 804
compareByteArrays :: ByteArray -> Int -> ByteArray -> Int -> Int -> Ordering
compareByteArrays (ByteArray ByteArray#
ba1#) (I# Int#
off1#) (ByteArray ByteArray#
ba2#) (I# Int#
off2#) (I# Int#
n#)
= forall a. Ord a => a -> a -> Ordering
compare (Int# -> Int
I# (ByteArray# -> Int# -> ByteArray# -> Int# -> Int# -> Int#
compareByteArrays# ByteArray#
ba1# Int#
off1# ByteArray#
ba2# Int#
off2# Int#
n#)) Int
0
#else
compareByteArrays (ByteArray ba1#) (I# off1#) (ByteArray ba2#) (I# off2#) (I# n#)
= compare (fromCInt (unsafeDupablePerformIO (memcmp_ba_offs ba1# off1# ba2# off2# n))) 0
where
n = fromIntegral (I# n#) :: CSize
fromCInt = fromIntegral :: CInt -> Int
foreign import ccall unsafe "primitive-memops.h hsprimitive_memcmp_offset"
memcmp_ba_offs :: ByteArray# -> Int# -> ByteArray# -> Int# -> CSize -> IO CInt
#endif
emptyByteArray :: ByteArray
{-# NOINLINE emptyByteArray #-}
emptyByteArray :: ByteArray
emptyByteArray = forall a. (forall s. ST s a) -> a
runST (forall (m :: * -> *).
PrimMonad m =>
Int -> m (MutableByteArray (PrimState m))
newByteArray Int
0 forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *).
PrimMonad m =>
MutableByteArray (PrimState m) -> m ByteArray
unsafeFreezeByteArray)
die :: String -> String -> a
die :: forall a. String -> String -> a
die String
fun String
problem = forall a. HasCallStack => String -> a
error forall a b. (a -> b) -> a -> b
$ String
"Data.Primitive.ByteArray." forall a. [a] -> [a] -> [a]
++ String
fun forall a. [a] -> [a] -> [a]
++ String
": " forall a. [a] -> [a] -> [a]
++ String
problem
cloneByteArray
:: ByteArray
-> Int
-> Int
-> ByteArray
{-# INLINE cloneByteArray #-}
cloneByteArray :: ByteArray -> Int -> Int -> ByteArray
cloneByteArray ByteArray
src Int
off Int
n = (forall s. ST s (MutableByteArray s)) -> ByteArray
runByteArray forall a b. (a -> b) -> a -> b
$ do
MutableByteArray s
dst <- forall (m :: * -> *).
PrimMonad m =>
Int -> m (MutableByteArray (PrimState m))
newByteArray Int
n
forall (m :: * -> *).
PrimMonad m =>
MutableByteArray (PrimState m)
-> Int -> ByteArray -> Int -> Int -> m ()
copyByteArray MutableByteArray s
dst Int
0 ByteArray
src Int
off Int
n
forall (m :: * -> *) a. Monad m => a -> m a
return MutableByteArray s
dst
cloneMutableByteArray :: PrimMonad m
=> MutableByteArray (PrimState m)
-> Int
-> Int
-> m (MutableByteArray (PrimState m))
{-# INLINE cloneMutableByteArray #-}
cloneMutableByteArray :: forall (m :: * -> *).
PrimMonad m =>
MutableByteArray (PrimState m)
-> Int -> Int -> m (MutableByteArray (PrimState m))
cloneMutableByteArray MutableByteArray (PrimState m)
src Int
off Int
n = do
MutableByteArray (PrimState m)
dst <- forall (m :: * -> *).
PrimMonad m =>
Int -> m (MutableByteArray (PrimState m))
newByteArray Int
n
forall (m :: * -> *).
PrimMonad m =>
MutableByteArray (PrimState m)
-> Int -> MutableByteArray (PrimState m) -> Int -> Int -> m ()
copyMutableByteArray MutableByteArray (PrimState m)
dst Int
0 MutableByteArray (PrimState m)
src Int
off Int
n
forall (m :: * -> *) a. Monad m => a -> m a
return MutableByteArray (PrimState m)
dst
runByteArray
:: (forall s. ST s (MutableByteArray s))
-> ByteArray
#if MIN_VERSION_base(4,10,0) /* In new GHCs, runRW# is available. */
runByteArray :: (forall s. ST s (MutableByteArray s)) -> ByteArray
runByteArray forall s. ST s (MutableByteArray s)
m = ByteArray# -> ByteArray
ByteArray ((forall s. ST s (MutableByteArray s)) -> ByteArray#
runByteArray# forall s. ST s (MutableByteArray s)
m)
runByteArray#
:: (forall s. ST s (MutableByteArray s))
-> ByteArray#
runByteArray# :: (forall s. ST s (MutableByteArray s)) -> ByteArray#
runByteArray# forall s. ST s (MutableByteArray s)
m = case forall o. (State# RealWorld -> o) -> o
runRW# forall a b. (a -> b) -> a -> b
$ \State# RealWorld
s ->
case forall s a. ST s a -> State# s -> (# State# s, a #)
unST forall s. ST s (MutableByteArray s)
m State# RealWorld
s of { (# State# RealWorld
s', MutableByteArray MutableByteArray# RealWorld
mary# #) ->
forall d.
MutableByteArray# d -> State# d -> (# State# d, ByteArray# #)
unsafeFreezeByteArray# MutableByteArray# RealWorld
mary# State# RealWorld
s'} of (# State# RealWorld
_, ByteArray#
ary# #) -> ByteArray#
ary#
unST :: ST s a -> State# s -> (# State# s, a #)
unST :: forall s a. ST s a -> State# s -> (# State# s, a #)
unST (GHCST.ST STRep s a
f) = STRep s a
f
#else /* In older GHCs, runRW# is not available. */
runByteArray m = runST $ m >>= unsafeFreezeByteArray
#endif
readCharArray :: PrimMonad m => MutableByteArray (PrimState m) -> Int -> m Char
{-# INLINE readCharArray #-}
readCharArray :: forall (m :: * -> *).
PrimMonad m =>
MutableByteArray (PrimState m) -> Int -> m Char
readCharArray (MutableByteArray MutableByteArray# (PrimState m)
arr#) (I# Int#
i#) = forall (m :: * -> *) a.
PrimMonad m =>
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
primitive
(\State# (PrimState m)
s0 -> case forall d.
MutableByteArray# d -> Int# -> State# d -> (# State# d, Char# #)
readCharArray# MutableByteArray# (PrimState m)
arr# Int#
i# State# (PrimState m)
s0 of
(# State# (PrimState m)
s1, Char#
c #) -> (# State# (PrimState m)
s1, Char# -> Char
C# Char#
c #)
)
writeCharArray
:: PrimMonad m => MutableByteArray (PrimState m) -> Int -> Char -> m ()
{-# INLINE writeCharArray #-}
writeCharArray :: forall (m :: * -> *).
PrimMonad m =>
MutableByteArray (PrimState m) -> Int -> Char -> m ()
writeCharArray (MutableByteArray MutableByteArray# (PrimState m)
arr#) (I# Int#
i#) (C# Char#
c)
= forall (m :: * -> *).
PrimMonad m =>
(State# (PrimState m) -> State# (PrimState m)) -> m ()
primitive_ (forall d.
MutableByteArray# d -> Int# -> Char# -> State# d -> State# d
writeCharArray# MutableByteArray# (PrimState m)
arr# Int#
i# Char#
c)
indexCharArray :: ByteArray -> Int -> Char
{-# INLINE indexCharArray #-}
indexCharArray :: ByteArray -> Int -> Char
indexCharArray (ByteArray ByteArray#
arr#) (I# Int#
i#) = Char# -> Char
C# (ByteArray# -> Int# -> Char#
indexCharArray# ByteArray#
arr# Int#
i#)