@@ -63,6 +63,7 @@ module Data.Primitive.ByteArray (
63
63
isByteArrayPinned , isMutableByteArrayPinned ,
64
64
#endif
65
65
byteArrayAsForeignPtr ,
66
+ mutableByteArrayAsForeignPtr ,
66
67
byteArrayContents ,
67
68
withByteArrayContents ,
68
69
mutableByteArrayContents ,
@@ -130,11 +131,21 @@ newAlignedPinnedByteArray (I# n#) (I# k#)
130
131
131
132
-- | Create a foreign pointer that points to the array's data. This operation
132
133
-- 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.
134
136
byteArrayAsForeignPtr :: ByteArray -> ForeignPtr Word8
135
137
{-# INLINE byteArrayAsForeignPtr #-}
136
138
byteArrayAsForeignPtr (ByteArray arr# ) = ForeignPtr (byteArrayContents# arr# ) (PlainPtr (unsafeCoerce# arr# ))
137
139
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
+
138
149
-- | Yield a pointer to the array's data. This operation is only safe on
139
150
-- /pinned/ byte arrays. Byte arrays allocated by 'newPinnedByteArray' and
140
151
-- 'newAlignedPinnedByteArray' are guaranteed to be pinned. Byte arrays
0 commit comments