From 38b7d817c806236a27fbdf2cf03fd2486a57f361 Mon Sep 17 00:00:00 2001 From: Andrew Martin Date: Fri, 28 Mar 2025 09:08:07 -0400 Subject: [PATCH 1/2] Prepare changelog for 0.9.1.0 Also, add mutableByteArrayAsForeignPtr for symmetry with byteArrayAsForeignPtr. --- Data/Primitive/ByteArray.hs | 13 ++++++++++++- changelog.md | 18 +++++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/Data/Primitive/ByteArray.hs b/Data/Primitive/ByteArray.hs index 0d19895..90f1b09 100644 --- a/Data/Primitive/ByteArray.hs +++ b/Data/Primitive/ByteArray.hs @@ -63,6 +63,7 @@ module Data.Primitive.ByteArray ( isByteArrayPinned, isMutableByteArrayPinned, #endif byteArrayAsForeignPtr, + mutableByteArrayAsForeignPtr, byteArrayContents, withByteArrayContents, mutableByteArrayContents, @@ -130,11 +131,21 @@ newAlignedPinnedByteArray (I# n#) (I# k#) -- | Create a foreign pointer that points to the array's data. This operation -- is only safe on /pinned/ byte arrays. The array's data is not garbage --- collected while references to the foreign pointer exist. +-- collected while references to the foreign pointer exist. Writing to the +-- array through the foreign pointer results in undefined behavior. byteArrayAsForeignPtr :: ByteArray -> ForeignPtr Word8 {-# INLINE byteArrayAsForeignPtr #-} byteArrayAsForeignPtr (ByteArray arr#) = ForeignPtr (byteArrayContents# arr#) (PlainPtr (unsafeCoerce# arr#)) + +-- | Variant of 'byteArrayAsForeignPtr' for mutable byte arrays. Similarly, this +-- is only safe on /pinned/ mutable byte arrays. This function differs from the +-- variant for immutable arrays in that it is safe to write to the array though +-- the foreign pointer. +mutableByteArrayAsForeignPtr :: MutableByteArray RealWorld -> ForeignPtr Word8 +{-# INLINE mutableByteArrayAsForeignPtr #-} +mutableByteArrayAsForeignPtr (MutableByteArray arr#) = ForeignPtr (mutableByteArrayContents# arr#) (PlainPtr arr#) + -- | Yield a pointer to the array's data. This operation is only safe on -- /pinned/ byte arrays. Byte arrays allocated by 'newPinnedByteArray' and -- 'newAlignedPinnedByteArray' are guaranteed to be pinned. Byte arrays diff --git a/changelog.md b/changelog.md index bdcd77d..e97ecf4 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,21 @@ ## Changes in version 0.9.1.0 - * Add `byteArrayAsForeignPtr`. + + * Make fromListN functions good consumers for list fusion. + + * Add functions to improve `MutVar`'s interoperability with `IORef` and `STRef`. + + * Add `createPrimArray` and `createByteArray`. + + * Add `byteArrayAsForeignPtr` and `mutableByteArrayAsForeignPtr`. + + * Use `copyMutableByteArrayNonOverlapping#` in the implementation of `copyMutableByteArray` + on sufficiently new GHCs. This does not change the contract for `copyMutableByteArray`. + This function has always been documented as having undefined behavior when the slices + overlap. However, overlaps previously were handled gracefully (with the semantics + of C's `memmove`). Going forward, users who do not uphold `copyMutableByteArray`'s + precondition will be met with unpredictable results. + + * Drop support for GHC 8.0. ## Changes in version 0.9.0.0 From 70cdd96fb50dd48e0438f2b51f1322b5b5408e4d Mon Sep 17 00:00:00 2001 From: Andrew Martin Date: Fri, 28 Mar 2025 10:09:37 -0400 Subject: [PATCH 2/2] Use mutableByteArrayContentsShim --- Data/Primitive/ByteArray.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Data/Primitive/ByteArray.hs b/Data/Primitive/ByteArray.hs index 90f1b09..e1d4a8c 100644 --- a/Data/Primitive/ByteArray.hs +++ b/Data/Primitive/ByteArray.hs @@ -144,7 +144,7 @@ byteArrayAsForeignPtr (ByteArray arr#) = ForeignPtr (byteArrayContents# arr#) (P -- the foreign pointer. mutableByteArrayAsForeignPtr :: MutableByteArray RealWorld -> ForeignPtr Word8 {-# INLINE mutableByteArrayAsForeignPtr #-} -mutableByteArrayAsForeignPtr (MutableByteArray arr#) = ForeignPtr (mutableByteArrayContents# arr#) (PlainPtr arr#) +mutableByteArrayAsForeignPtr (MutableByteArray arr#) = ForeignPtr (mutableByteArrayContentsShim arr#) (PlainPtr arr#) -- | Yield a pointer to the array's data. This operation is only safe on -- /pinned/ byte arrays. Byte arrays allocated by 'newPinnedByteArray' and