@@ -25,6 +25,7 @@ module Streamly.Internal.Data.Array.Foreign.Type
28+ , fromForeignPtrUnsafe
28 , fromAddr#
29 , fromAddr#
29 , fromCString#
30 , fromCString#
30 , fromList
31 , fromList
@@ -84,6 +85,7 @@ import Foreign.Ptr (plusPtr, castPtr)
84import Foreign.Storable (Storable(..))
85import Foreign.Storable (Storable(..))
85import GHC.Base (Addr#, nullAddr#, build)
86import GHC.Base (Addr#, nullAddr#, build)
86import GHC.Exts (IsList, IsString(..))
87import GHC.Exts (IsList, IsString(..))
88+import GHC.ForeignPtr (ForeignPtr)
88import GHC.IO (unsafePerformIO)
90import GHC.IO (unsafePerformIO)
89import GHC.Ptr (Ptr(..))
91import GHC.Ptr (Ptr(..))
@@ -231,6 +233,24 @@ fromPtr n ptr = unsafeInlineIO $ do
231 , aEnd = end
233 , aEnd = end
236+-- | @fromForeignPtrUnsafe foreignPtr end bound@ creates an 'Array' that starts
237+-- at the memory pointed by the @foreignPtr@, @end@ is the first unused
240+-- Unsafe: Make sure that foreignPtr <= end and (end - start) is an
241+-- integral multiple of the element size. Only PlainPtr type ForeignPtr is
246+{-# INLINE fromForeignPtrUnsafe #-}
247+fromForeignPtrUnsafe ::
251+ ForeignPtr a -> Ptr a -> Array a
252+fromForeignPtrUnsafe fp end = unsafeFreeze $ MA.fromForeignPtrUnsafe fp end end
234-- XXX when converting an array of Word8 from a literal string we can simply
254-- XXX when converting an array of Word8 from a literal string we can simply
235-- refer to the literal string. Is it possible to write rules such that
255-- refer to the literal string. Is it possible to write rules such that
236-- fromList Word8 can be rewritten so that GHC does not first convert the
256-- fromList Word8 can be rewritten so that GHC does not first convert the