on master help sign in
Rename mutableArray to fromForeignPtrUnsafe
commit11ad72a6bd3f52a282fd3f1888d78124fa97aeb1 authorHarendra Kumar <harendra@composewell.com> dateThu 7 Oct 2021, 16:11 (5 years ago) parent3a4e916
4 files changed+23−11
src/Streamly/Internal/Data/Array/Foreign/Mut/Type.hs+15−4 src/Streamly/Internal/FileSystem/FD.hs+4−3 src/Streamly/Internal/FileSystem/Handle.hs+2−2 src/Streamly/Internal/Network/Socket.hs+2−2
src/Streamly/Internal/Data/Array/Foreign/Mut/Type.hs+15−4
@@ -14,7 +14,7 @@ module Streamly.Internal.Data.Array.Foreign.Mut.Type
1414 Array (..)
1515
1616 -- * Construction
17− , mutableArray
17+ , fromForeignPtrUnsafe
1818 , unsafeWithNewArray
1919 , newArray
2020 , newArrayAligned
@@ -229,13 +229,24 @@ data Array a =
229229 , aBound :: {-# UNPACK #-} !(Ptr a) -- ^ first address beyond allocated memory
230230 }
231231
232−{-# INLINE mutableArray #-}
233−mutableArray ::
232+-- | @fromForeignPtrUnsafe foreignPtr end bound@ creates an 'Array' that starts
233+-- at the memory pointed by the @foreignPtr@, @end@ is the first unused
234+-- address, and @bound@ is the first address beyond the allocated memory.
235+--
236+-- Unsafe: Make sure that foreignPtr <= end <= bound and (end - start) is an
237+-- integral multiple of the element size.
238+--
239+-- /Pre-release/
240+--
241+{-# INLINE fromForeignPtrUnsafe #-}
242+fromForeignPtrUnsafe ::
234243#ifdef DEVBUILD
235244 Storable a =>
236245#endif
237246 ForeignPtr a -> Ptr a -> Ptr a -> Array a
238−mutableArray = Array
247+fromForeignPtrUnsafe fp end bound =
248+ assert (unsafeForeignPtrToPtr fp <= end && end <= bound)
249+ (Array fp end bound)
239250
240251-------------------------------------------------------------------------------
241252-- Construction
src/Streamly/Internal/FileSystem/FD.hs+4−3
@@ -132,7 +132,7 @@ import qualified GHC.IO.Device as RawIO
132132
133133import Streamly.Internal.Data.Array.Foreign.Type
134134 (Array(..), byteLength, unsafeFreeze)
135−import Streamly.Internal.Data.Array.Foreign.Mut.Type (mutableArray)
135+import Streamly.Internal.Data.Array.Foreign.Mut.Type (fromForeignPtrUnsafe)
136136import Streamly.Internal.System.IO (defaultChunkSize)
137137import Streamly.Internal.Data.Stream.Serial (SerialT)
138138import Streamly.Internal.Data.Stream.IsStream.Type
@@ -225,8 +225,9 @@ readArrayUpto size (Handle fd) = do
225225#endif
226226 -- XXX shrink only if the diff is significant
227227 -- Use unsafeFreezeWithShrink
228− return $
229− unsafeFreeze $ mutableArray ptr (p `plusPtr` n) (p `plusPtr` size)
228+ return
229+ $ unsafeFreeze
230+ $ fromForeignPtrUnsafe ptr (p `plusPtr` n) (p `plusPtr` size)
230231
231232-------------------------------------------------------------------------------
232233-- Array IO (output)
src/Streamly/Internal/FileSystem/Handle.hs+2−2
@@ -115,7 +115,7 @@ import Streamly.Internal.Data.Consumer.Type (Consumer(..))
115115import Streamly.Internal.Data.Unfold.Type (Unfold(..))
116116import Streamly.Internal.Data.Array.Foreign.Type
117117 (Array(..), writeNUnsafe, unsafeFreezeWithShrink)
118−import Streamly.Internal.Data.Array.Foreign.Mut.Type (mutableArray)
118+import Streamly.Internal.Data.Array.Foreign.Mut.Type (fromForeignPtrUnsafe)
119119import Streamly.Internal.Data.Stream.Serial (SerialT)
120120import Streamly.Internal.Data.Stream.IsStream.Type
121121 (IsStream, mkStream, fromStreamD)
@@ -178,7 +178,7 @@ getChunk size h = liftIO $ do
178178 -- XXX shrink only if the diff is significant
179179 return $
180180 unsafeFreezeWithShrink $
181− mutableArray ptr (p `plusPtr` n) (p `plusPtr` size)
181+ fromForeignPtrUnsafe ptr (p `plusPtr` n) (p `plusPtr` size)
182182
183183-- This could be useful in implementing the "reverse" read APIs or if you want
184184-- to read arrays of exact size instead of compacting them later. Compacting
src/Streamly/Internal/Network/Socket.hs+2−2
@@ -87,7 +87,7 @@ import qualified Network.Socket as Net
8787
8888import Streamly.Internal.BaseCompat
8989import Streamly.Internal.Control.Concurrent (MonadAsync)
90−import Streamly.Internal.Data.Array.Foreign.Mut.Type (mutableArray)
90+import Streamly.Internal.Data.Array.Foreign.Mut.Type (fromForeignPtrUnsafe)
9191import Streamly.Internal.Data.Array.Foreign.Type (Array(..))
9292import Streamly.Internal.Data.Array.Stream.Foreign (lpackArraysChunksOf)
9393import Streamly.Internal.Data.Fold (Fold)
@@ -259,7 +259,7 @@ readArrayUptoWith f size h = do
259259 unsafeWithForeignPtr ptr $ \p -> do
260260 n <- f h p size
261261 let v = A.unsafeFreeze
262− $ mutableArray ptr (p `plusPtr` n) (p `plusPtr` size)
262+ $ fromForeignPtrUnsafe ptr (p `plusPtr` n) (p `plusPtr` size)
263263
264264 -- XXX shrink only if the diff is significant
265265 -- A.shrinkToFit v