on master help sign in
Add cloneSliceUnsafe to MutByteArray
62c12a6Harendra Kumar
core/src/Streamly/Internal/Data/MutByteArray/Type.hs+28−1
@@ -31,6 +31,9 @@ module Streamly.Internal.Data.MutByteArray.Type
31 -- ** Access
31 -- ** Access
32 , sizeOfMutableByteArray
32 , sizeOfMutableByteArray
33 , putSliceUnsafe
33 , putSliceUnsafe
34+ , cloneSliceUnsafeAs
35+ , cloneSliceUnsafe
36+ , pinnedCloneSliceUnsafe
34 , asPtrUnsafe
37 , asPtrUnsafe
35 ) where
38 ) where
36
39
@@ -164,7 +167,8 @@ newBytesAs Pinned = pinnedNew
164-------------------------------------------------------------------------------
167-------------------------------------------------------------------------------
165
168
166-- | Put a sub range of a source array into a subrange of a destination array.
169-- | Put a sub range of a source array into a subrange of a destination array.
167−-- This is not safe as it does not check the bounds.
170+-- This is not safe as it does not check the bounds of neither the src array
171+-- nor the destination array.
168{-# INLINE putSliceUnsafe #-}
172{-# INLINE putSliceUnsafe #-}
169putSliceUnsafe ::
173putSliceUnsafe ::
170 MonadIO m
174 MonadIO m
@@ -190,6 +194,29 @@ putSliceUnsafe src srcStartBytes dst dstStartBytes lenBytes = liftIO $ do
190 arrS# srcStartBytes# arrD# dstStartBytes# lenBytes# s#
194 arrS# srcStartBytes# arrD# dstStartBytes# lenBytes# s#
191 , () #)
195 , () #)
192
196
197+-- | Unsafe as it does not check whether the start offset and length supplied
198+-- are valid inside the array.
199+{-# INLINE cloneSliceUnsafeAs #-}
200+cloneSliceUnsafeAs :: MonadIO m =>
201+ PinnedState -> Int -> Int -> MutByteArray -> m MutByteArray
202+cloneSliceUnsafeAs ps srcOff srcLen src =
203+ liftIO $ do
204+ mba <- newBytesAs ps srcLen
205+ putSliceUnsafe src srcOff mba 0 srcLen
206+ return mba
207+
208+-- | @cloneSliceUnsafe offset len arr@ clones a slice of the supplied array
209+-- starting at the given offset and equal to the given length.
210+{-# INLINE cloneSliceUnsafe #-}
211+cloneSliceUnsafe :: MonadIO m => Int -> Int -> MutByteArray -> m MutByteArray
212+cloneSliceUnsafe = cloneSliceUnsafeAs Unpinned
213+
214+-- | @pinnedCloneSliceUnsafe offset len arr@
215+{-# INLINE pinnedCloneSliceUnsafe #-}
216+pinnedCloneSliceUnsafe :: MonadIO m =>
217+ Int -> Int -> MutByteArray -> m MutByteArray
218+pinnedCloneSliceUnsafe = cloneSliceUnsafeAs Pinned
219+
193-------------------------------------------------------------------------------
220-------------------------------------------------------------------------------
194-- Pinning & Unpinning
221-- Pinning & Unpinning
195-------------------------------------------------------------------------------
222-------------------------------------------------------------------------------