on master help sign in
Rename mutableArray to fromForeignPtrUnsafe
11ad72aHarendra Kumar
src/Streamly/Internal/Data/Array/Foreign/Mut/Type.hs+15−4
@@ -14,7 +14,7 @@ module Streamly.Internal.Data.Array.Foreign.Mut.Type
14 Array (..)
14 Array (..)
15
15
16 -- * Construction
16 -- * Construction
17− , mutableArray
17+ , fromForeignPtrUnsafe
18 , unsafeWithNewArray
18 , unsafeWithNewArray
19 , newArray
19 , newArray
20 , newArrayAligned
20 , newArrayAligned
@@ -229,13 +229,24 @@ data Array a =
229 , aBound :: {-# UNPACK #-} !(Ptr a) -- ^ first address beyond allocated memory
229 , aBound :: {-# UNPACK #-} !(Ptr a) -- ^ first address beyond allocated memory
230 }
230 }
231
231
232−{-# INLINE mutableArray #-}
232+-- | @fromForeignPtrUnsafe foreignPtr end bound@ creates an 'Array' that starts
233−mutableArray ::
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 ::
234#ifdef DEVBUILD
243#ifdef DEVBUILD
235 Storable a =>
244 Storable a =>
236#endif
245#endif
237 ForeignPtr a -> Ptr a -> Ptr a -> Array a
246 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)
239
250
240-------------------------------------------------------------------------------
251-------------------------------------------------------------------------------
241-- Construction
252-- Construction
src/Streamly/Internal/FileSystem/FD.hs+4−3
@@ -132,7 +132,7 @@ import qualified GHC.IO.Device as RawIO
132
132
133import Streamly.Internal.Data.Array.Foreign.Type
133import Streamly.Internal.Data.Array.Foreign.Type
134 (Array(..), byteLength, unsafeFreeze)
134 (Array(..), byteLength, unsafeFreeze)
135−import Streamly.Internal.Data.Array.Foreign.Mut.Type (mutableArray)
135+import Streamly.Internal.Data.Array.Foreign.Mut.Type (fromForeignPtrUnsafe)
136import Streamly.Internal.System.IO (defaultChunkSize)
136import Streamly.Internal.System.IO (defaultChunkSize)
137import Streamly.Internal.Data.Stream.Serial (SerialT)
137import Streamly.Internal.Data.Stream.Serial (SerialT)
138import Streamly.Internal.Data.Stream.IsStream.Type
138import Streamly.Internal.Data.Stream.IsStream.Type
@@ -225,8 +225,9 @@ readArrayUpto size (Handle fd) = do
225#endif
225#endif
226 -- XXX shrink only if the diff is significant
226 -- XXX shrink only if the diff is significant
227 -- Use unsafeFreezeWithShrink
227 -- Use unsafeFreezeWithShrink
228− return $
228+ return
229+ $ unsafeFreeze
229− unsafeFreeze $ mutableArray ptr (p `plusPtr` n) (p `plusPtr` size)
230+ $ fromForeignPtrUnsafe ptr (p `plusPtr` n) (p `plusPtr` size)
230
231
231-------------------------------------------------------------------------------
232-------------------------------------------------------------------------------
232-- Array IO (output)
233-- Array IO (output)
src/Streamly/Internal/FileSystem/Handle.hs+2−2
@@ -115,7 +115,7 @@ import Streamly.Internal.Data.Consumer.Type (Consumer(..))
115import Streamly.Internal.Data.Unfold.Type (Unfold(..))
115import Streamly.Internal.Data.Unfold.Type (Unfold(..))
116import Streamly.Internal.Data.Array.Foreign.Type
116import Streamly.Internal.Data.Array.Foreign.Type
117 (Array(..), writeNUnsafe, unsafeFreezeWithShrink)
117 (Array(..), writeNUnsafe, unsafeFreezeWithShrink)
118−import Streamly.Internal.Data.Array.Foreign.Mut.Type (mutableArray)
118+import Streamly.Internal.Data.Array.Foreign.Mut.Type (fromForeignPtrUnsafe)
119import Streamly.Internal.Data.Stream.Serial (SerialT)
119import Streamly.Internal.Data.Stream.Serial (SerialT)
120import Streamly.Internal.Data.Stream.IsStream.Type
120import Streamly.Internal.Data.Stream.IsStream.Type
121 (IsStream, mkStream, fromStreamD)
121 (IsStream, mkStream, fromStreamD)
@@ -178,7 +178,7 @@ getChunk size h = liftIO $ do
178 -- XXX shrink only if the diff is significant
178 -- XXX shrink only if the diff is significant
179 return $
179 return $
180 unsafeFreezeWithShrink $
180 unsafeFreezeWithShrink $
181− mutableArray ptr (p `plusPtr` n) (p `plusPtr` size)
181+ fromForeignPtrUnsafe ptr (p `plusPtr` n) (p `plusPtr` size)
182
182
183-- This could be useful in implementing the "reverse" read APIs or if you want
183-- This could be useful in implementing the "reverse" read APIs or if you want
184-- to read arrays of exact size instead of compacting them later. Compacting
184-- 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
87
87
88import Streamly.Internal.BaseCompat
88import Streamly.Internal.BaseCompat
89import Streamly.Internal.Control.Concurrent (MonadAsync)
89import 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)
91import Streamly.Internal.Data.Array.Foreign.Type (Array(..))
91import Streamly.Internal.Data.Array.Foreign.Type (Array(..))
92import Streamly.Internal.Data.Array.Stream.Foreign (lpackArraysChunksOf)
92import Streamly.Internal.Data.Array.Stream.Foreign (lpackArraysChunksOf)
93import Streamly.Internal.Data.Fold (Fold)
93import Streamly.Internal.Data.Fold (Fold)
@@ -259,7 +259,7 @@ readArrayUptoWith f size h = do
259 unsafeWithForeignPtr ptr $ \p -> do
259 unsafeWithForeignPtr ptr $ \p -> do
260 n <- f h p size
260 n <- f h p size
261 let v = A.unsafeFreeze
261 let v = A.unsafeFreeze
262− $ mutableArray ptr (p `plusPtr` n) (p `plusPtr` size)
262+ $ fromForeignPtrUnsafe ptr (p `plusPtr` n) (p `plusPtr` size)
263
263
264 -- XXX shrink only if the diff is significant
264 -- XXX shrink only if the diff is significant
265 -- A.shrinkToFit v
265 -- A.shrinkToFit v