@@ -53,10 +53,9 @@ module Streamly.Internal.Data.Array.Mut.Type
5353 , writeNWithUnsafe
5454 , writeNWith
5555 , writeNUnsafe
56− , writeNUnsafeAs
56+ , pinnedWriteNUnsafe
5757 , writeN
5858 , pinnedWriteN
59− , writeNAs
6059 , pinnedWriteNAligned
6160
6261 , writeWith
@@ -189,8 +188,7 @@ module Streamly.Internal.Data.Array.Mut.Type
189188
190189 -- ** Construct from streams
191190 , chunksOf
192− , chunksOfAs
193− , arrayStreamKFromStreamD
191+ , pinnedChunksOf
194192 , writeChunks
195193
196194 -- ** Eliminate to streams
@@ -429,7 +427,7 @@ newBytesAs :: MonadIO m =>
429427#endif
430428 PinnedState -> Int -> m (MutArray a)
431429newBytesAs ps bytes = do
432− contents <- liftIO $ Unboxed.newBytes ps bytes
430+ contents <- liftIO $ Unboxed.newBytesAs ps bytes
433431 return $ MutArray
434432 { arrContents = contents
435433 , arrStart = 0
@@ -463,7 +461,7 @@ pinnedNewAligned =
463461newAs :: (MonadIO m, Unbox a) => PinnedState -> Int -> m (MutArray a)
464462newAs ps =
465463 newArrayWith
466− (\s _ -> liftIO $ Unboxed.newBytes ps s)
464+ (\s _ -> liftIO $ Unboxed.newBytesAs ps s)
467465 (error "new: alignment is not used in unpinned arrays.")
468466
469467-- XXX can unaligned allocation be more efficient when alignment is not needed?
@@ -742,7 +740,7 @@ reallocExplicit elemSize newCapacityInBytes MutArray{..} = do
742740 contents <-
743741 if Unboxed.isPinned arrContents
744742 then Unboxed.pinnedNewBytes newCapMaxInBytes
745− else Unboxed.newUnpinnedBytes newCapMaxInBytes
743+ else Unboxed.newBytes newCapMaxInBytes
746744 let !(MutableByteArray mbarrFrom#) = arrContents
747745 !(MutableByteArray mbarrTo#) = contents
748746
@@ -1362,6 +1360,16 @@ chunksOf :: forall m a. (MonadIO m, Unbox a)
13621360-- chunksOf n = D.foldMany (writeN n)
13631361chunksOf = chunksOfAs Unpinned
13641362
1363+-- | Like 'chunksOf' but creates pinned arrays.
1364+{-# INLINE_NORMAL pinnedChunksOf #-}
1365+pinnedChunksOf :: forall m a. (MonadIO m, Unbox a)
1366+ => Int -> D.Stream m a -> D.Stream m (MutArray a)
1367+-- pinnedChunksOf n = D.foldMany (pinnedWriteN n)
1368+pinnedChunksOf = chunksOfAs Pinned
1369+
1370+-- XXX This should take a PinnedState
1371+-- XXX buffer to a list instead?
1372+-- | Buffer the stream into arrays in memory.
13651373{-# INLINE arrayStreamKFromStreamDAs #-}
13661374arrayStreamKFromStreamDAs :: forall m a. (MonadIO m, Unbox a) =>
13671375 PinnedState -> D.Stream m a -> m (StreamK m (MutArray a))
@@ -1369,14 +1377,6 @@ arrayStreamKFromStreamDAs ps =
13691377 let n = allocBytesToElemCount (undefined :: a) defaultChunkSize
13701378 in D.foldr K.cons K.nil . chunksOfAs ps n
13711379
1372−-- XXX This should take a PinnedState
1373−-- XXX buffer to a list instead?
1374−-- | Buffer the stream into arrays in memory.
1375−{-# INLINE arrayStreamKFromStreamD #-}
1376−arrayStreamKFromStreamD :: forall m a. (MonadIO m, Unbox a) =>
1377− D.Stream m a -> m (StreamK m (MutArray a))
1378−arrayStreamKFromStreamD = arrayStreamKFromStreamDAs Pinned
1379−
13801380-------------------------------------------------------------------------------
13811381-- Streams of arrays - Flattening
13821382-------------------------------------------------------------------------------
@@ -1787,6 +1787,12 @@ writeNUnsafe :: forall m a. (MonadIO m, Unbox a)
17871787 => Int -> Fold m a (MutArray a)
17881788writeNUnsafe = writeNUnsafeAs Unpinned
17891789
1790+-- | Like 'writeNUnsafe' but creates a pinned array.
1791+{-# INLINE_NORMAL pinnedWriteNUnsafe #-}
1792+pinnedWriteNUnsafe :: forall m a. (MonadIO m, Unbox a)
1793+ => Int -> Fold m a (MutArray a)
1794+pinnedWriteNUnsafe = writeNUnsafeAs Pinned
1795+
17901796-- | @writeNWith alloc n@ folds a maximum of @n@ elements into an array
17911797-- allocated using the @alloc@ function.
17921798--
@@ -1817,6 +1823,7 @@ writeNAs ps = writeNWith (newAs ps)
18171823writeN :: forall m a. (MonadIO m, Unbox a) => Int -> Fold m a (MutArray a)
18181824writeN = writeNAs Unpinned
18191825
1826+-- | Like 'writeN' but creates a pinned array.
18201827{-# INLINE_NORMAL pinnedWriteN #-}
18211828pinnedWriteN ::
18221829 forall m a. (MonadIO m, Unbox a)
@@ -1959,6 +1966,7 @@ writeWith = writeWithAs Unpinned
19591966write :: forall m a. (MonadIO m, Unbox a) => Fold m a (MutArray a)
19601967write = writeWith (allocBytesToElemCount (undefined :: a) arrayChunkBytes)
19611968
1969+-- | Like 'write' but creates a pinned array.
19621970{-# INLINE pinnedWrite #-}
19631971pinnedWrite :: forall m a. (MonadIO m, Unbox a) => Fold m a (MutArray a)
19641972pinnedWrite =
@@ -2000,6 +2008,7 @@ fromStreamDN = fromStreamDNAs Unpinned
20002008fromListN :: (MonadIO m, Unbox a) => Int -> [a] -> m (MutArray a)
20012009fromListN n xs = fromStreamDN n $ D.fromList xs
20022010
2011+-- | Like 'fromListN' but creates a pinned array.
20032012{-# INLINABLE pinnedFromListN #-}
20042013pinnedFromListN :: (MonadIO m, Unbox a) => Int -> [a] -> m (MutArray a)
20052014pinnedFromListN n xs = fromStreamDNAs Pinned n $ D.fromList xs
@@ -2063,6 +2072,7 @@ fromStreamD = fromStreamDAs Unpinned
20632072fromList :: (MonadIO m, Unbox a) => [a] -> m (MutArray a)
20642073fromList xs = fromStreamD $ D.fromList xs
20652074
2075+-- | Like 'fromList' but creates a pinned array.
20662076{-# INLINE pinnedFromList #-}
20672077pinnedFromList :: (MonadIO m, Unbox a) => [a] -> m (MutArray a)
20682078pinnedFromList xs = fromStreamDAs Pinned $ D.fromList xs
@@ -2111,8 +2121,8 @@ spliceCopy arr1 arr2 = liftIO $ do
21112121 let newLen = len1 + len2
21122122 newArrContents <-
21132123 if Unboxed.isPinned (arrContents arr1)
2114− then Unboxed.newPinnedBytes newLen
2115− else Unboxed.newUnpinnedBytes newLen
2124+ then Unboxed.pinnedNewBytes newLen
2125+ else Unboxed.newBytes newLen
21162126 let len = len1 + len2
21172127 newArr = MutArray newArrContents 0 len len
21182128 putSliceUnsafe arr1 start1 newArr 0 len1