Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add withByteArrayContents and withMutableByteArrayContents #395

Merged
merged 5 commits into from
Sep 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 45 additions & 8 deletions Data/Primitive/ByteArray.hs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ module Data.Primitive.ByteArray (
#if __GLASGOW_HASKELL__ >= 802
isByteArrayPinned, isMutableByteArrayPinned,
#endif
byteArrayContents, mutableByteArrayContents
byteArrayContents,
withByteArrayContents,
mutableByteArrayContents,
withMutableByteArrayContents

) where

Expand Down Expand Up @@ -124,24 +127,58 @@ newAlignedPinnedByteArray (I# n#) (I# k#)
(# s'#, arr# #) -> (# s'#, MutableByteArray arr# #))

-- | Yield a pointer to the array's data. This operation is only safe on
-- /pinned/ byte arrays allocated by 'newPinnedByteArray' or
-- 'newAlignedPinnedByteArray'.
-- /pinned/ byte arrays. Byte arrays allocated by 'newPinnedByteArray' and
-- 'newAlignedPinnedByteArray' are guaranteed to be pinned. Byte arrays
-- allocated by 'newByteArray' may or may not be pinned (use
-- 'isByteArrayPinned' to figure out).
--
-- Prefer 'withByteArrayContents', which ensures that the array is not
-- garbage collected while the pointer is being used.
byteArrayContents :: ByteArray -> Ptr Word8
{-# INLINE byteArrayContents #-}
byteArrayContents (ByteArray arr#) = Ptr (byteArrayContents# arr#)

-- | A composition of 'byteArrayContents' and 'keepAliveUnlifted'.
-- The callback function must not return the pointer. The argument byte
-- array must be /pinned/. See 'byteArrayContents' for an explanation
-- of which byte arrays are pinned.
--
-- Note: This could be implemented with 'keepAlive' instead of
-- 'keepAliveUnlifted', but 'keepAlive' here would cause GHC to materialize
-- the wrapper data constructor on the heap.
withByteArrayContents :: PrimBase m => ByteArray -> (Ptr Word8 -> m a) -> m a
{-# INLINE withByteArrayContents #-}
withByteArrayContents (ByteArray arr#) f =
keepAliveUnlifted arr# (f (Ptr (byteArrayContents# arr#)))
andrewthad marked this conversation as resolved.
Show resolved Hide resolved

-- | Yield a pointer to the array's data. This operation is only safe on
-- /pinned/ byte arrays allocated by 'newPinnedByteArray' or
-- 'newAlignedPinnedByteArray'.
-- /pinned/ byte arrays. See 'byteArrayContents' for an explanation
-- of which byte arrays are pinned.
--
-- Prefer 'withByteArrayContents', which ensures that the array is not
-- garbage collected while the pointer is being used.
mutableByteArrayContents :: MutableByteArray s -> Ptr Word8
{-# INLINE mutableByteArrayContents #-}
mutableByteArrayContents (MutableByteArray arr#) = Ptr
mutableByteArrayContents (MutableByteArray arr#) = Ptr (mutableByteArrayContentsShim arr#)

mutableByteArrayContentsShim :: MutableByteArray# s -> Addr#
{-# INLINE mutableByteArrayContentsShim #-}
mutableByteArrayContentsShim x =
#if __GLASGOW_HASKELL__ >= 902
(mutableByteArrayContents# arr#)
mutableByteArrayContents# x
#else
(byteArrayContents# (unsafeCoerce# arr#))
byteArrayContents# (unsafeCoerce# x)
#endif

-- | A composition of 'mutableByteArrayContents' and 'keepAliveUnlifted'.
-- The callback function must not return the pointer. The argument byte
-- array must be /pinned/. See 'byteArrayContents' for an explanation
-- of which byte arrays are pinned.
withMutableByteArrayContents :: PrimBase m => MutableByteArray (PrimState m) -> (Ptr Word8 -> m a) -> m a
{-# INLINE withMutableByteArrayContents #-}
withMutableByteArrayContents (MutableByteArray arr#) f =
keepAliveUnlifted arr# (f (Ptr (mutableByteArrayContentsShim arr#)))

-- | Check if the two arrays refer to the same memory block.
sameMutableByteArray :: MutableByteArray s -> MutableByteArray s -> Bool
{-# INLINE sameMutableByteArray #-}
Expand Down
Loading