on master help sign in
Add fromForeignPtrUnsafe to immutable foreign array
commitd4e6ca6f3bcec595a36cc3ce8dd99940971f66e7 authoradithyaov <adithya@composewell.com> dateWed 1 Dec 2021, 10:16 (5 years ago) parent244926b
1 file changed+20
src/Streamly/Internal/Data/Array/Foreign/Type.hs+20
src/Streamly/Internal/Data/Array/Foreign/Type.hs+20
@@ -25,6 +25,7 @@ module Streamly.Internal.Data.Array.Foreign.Type
2525 , splice
2626
2727 , fromPtr
28+ , fromForeignPtrUnsafe
2829 , fromAddr#
2930 , fromCString#
3031 , fromList
@@ -84,6 +85,7 @@ import Foreign.Ptr (plusPtr, castPtr)
8485import Foreign.Storable (Storable(..))
8586import GHC.Base (Addr#, nullAddr#, build)
8687import GHC.Exts (IsList, IsString(..))
88+import GHC.ForeignPtr (ForeignPtr)
8789
8890import GHC.IO (unsafePerformIO)
8991import GHC.Ptr (Ptr(..))
@@ -231,6 +233,24 @@ fromPtr n ptr = unsafeInlineIO $ do
231233 , aEnd = end
232234 }
233235
236+-- | @fromForeignPtrUnsafe foreignPtr end bound@ creates an 'Array' that starts
237+-- at the memory pointed by the @foreignPtr@, @end@ is the first unused
238+-- address.
239+--
240+-- Unsafe: Make sure that foreignPtr <= end and (end - start) is an
241+-- integral multiple of the element size. Only PlainPtr type ForeignPtr is
242+-- supported.
243+--
244+-- /Pre-release/
245+--
246+{-# INLINE fromForeignPtrUnsafe #-}
247+fromForeignPtrUnsafe ::
248+#ifdef DEVBUILD
249+ Storable a =>
250+#endif
251+ ForeignPtr a -> Ptr a -> Array a
252+fromForeignPtrUnsafe fp end = unsafeFreeze $ MA.fromForeignPtrUnsafe fp end end
253+
234254-- XXX when converting an array of Word8 from a literal string we can simply
235255-- refer to the literal string. Is it possible to write rules such that
236256-- fromList Word8 can be rewritten so that GHC does not first convert the