@@ -63,6 +63,7 @@ module Data.Primitive.ByteArray (
6363 isByteArrayPinned , isMutableByteArrayPinned ,
6464#endif
6565 byteArrayAsForeignPtr ,
66+ mutableByteArrayAsForeignPtr ,
6667 byteArrayContents ,
6768 withByteArrayContents ,
6869 mutableByteArrayContents ,
@@ -130,11 +131,21 @@ newAlignedPinnedByteArray (I# n#) (I# k#)
130131
131132-- | Create a foreign pointer that points to the array's data. This operation
132133-- is only safe on /pinned/ byte arrays. The array's data is not garbage
133- -- collected while references to the foreign pointer exist.
134+ -- collected while references to the foreign pointer exist. Writing to the
135+ -- array through the foreign pointer results in undefined behavior.
134136byteArrayAsForeignPtr :: ByteArray -> ForeignPtr Word8
135137{-# INLINE byteArrayAsForeignPtr #-}
136138byteArrayAsForeignPtr (ByteArray arr# ) = ForeignPtr (byteArrayContents# arr# ) (PlainPtr (unsafeCoerce# arr# ))
137139
140+
141+ -- | Variant of 'byteArrayAsForeignPtr' for mutable byte arrays. Similarly, this
142+ -- is only safe on /pinned/ mutable byte arrays. This function differs from the
143+ -- variant for immutable arrays in that it is safe to write to the array though
144+ -- the foreign pointer.
145+ mutableByteArrayAsForeignPtr :: MutableByteArray RealWorld -> ForeignPtr Word8
146+ {-# INLINE mutableByteArrayAsForeignPtr #-}
147+ mutableByteArrayAsForeignPtr (MutableByteArray arr# ) = ForeignPtr (mutableByteArrayContentsShim arr# ) (PlainPtr arr# )
148+
138149-- | Yield a pointer to the array's data. This operation is only safe on
139150-- /pinned/ byte arrays. Byte arrays allocated by 'newPinnedByteArray' and
140151-- 'newAlignedPinnedByteArray' are guaranteed to be pinned. Byte arrays
0 commit comments