Skip to content
Merged
Show file tree
Hide file tree
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
13 changes: 12 additions & 1 deletion Data/Primitive/ByteArray.hs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
isByteArrayPinned, isMutableByteArrayPinned,
#endif
byteArrayAsForeignPtr,
mutableByteArrayAsForeignPtr,
byteArrayContents,
withByteArrayContents,
mutableByteArrayContents,
Expand Down Expand Up @@ -130,11 +131,21 @@

-- | 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 (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
-- 'newAlignedPinnedByteArray' are guaranteed to be pinned. Byte arrays
Expand Down Expand Up @@ -292,7 +303,7 @@
sizeofMutableByteArray :: MutableByteArray s -> Int
{-# INLINE sizeofMutableByteArray #-}
{-# DEPRECATED sizeofMutableByteArray "use getSizeofMutableByteArray instead" #-}
sizeofMutableByteArray (MutableByteArray arr#) = I# (sizeofMutableByteArray# arr#)

Check warning on line 306 in Data/Primitive/ByteArray.hs

View workflow job for this annotation

GitHub Actions / build (windows-latest, latest)

In the use of ‘sizeofMutableByteArray#’

Check warning on line 306 in Data/Primitive/ByteArray.hs

View workflow job for this annotation

GitHub Actions / build (windows-latest, latest)

In the use of ‘sizeofMutableByteArray#’

Check warning on line 306 in Data/Primitive/ByteArray.hs

View workflow job for this annotation

GitHub Actions / build (macOS-latest, latest)

In the use of ‘sizeofMutableByteArray#’

Check warning on line 306 in Data/Primitive/ByteArray.hs

View workflow job for this annotation

GitHub Actions / build (macOS-latest, latest)

In the use of ‘sizeofMutableByteArray#’

-- | Shrink a mutable byte array. The new size is given in bytes.
-- It must be smaller than the old size. The array will be resized in place.
Expand Down
18 changes: 17 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
Loading