@@ -20,6 +20,8 @@ module Streamly.Internal.Data.Array.Foreign.Mut.Type
2020 , ArrayContents
2121 , nilArrayContents
2222 , touch
23+ , pin
24+ , unpin
2325
2426 -- * Constructing and Writing
2527 -- ** Construction
@@ -242,7 +244,6 @@ import GHC.Base
242244 )
243245import GHC.Base (noinline)
244246import GHC.Exts (unsafeCoerce#)
245−
246247import GHC.Ptr (Ptr(..))
247248
248249import Streamly.Internal.Data.Fold.Type (Fold(..))
@@ -261,6 +262,7 @@ import qualified Streamly.Internal.Data.Producer as Producer
261262import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
262263import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
263264import qualified Streamly.Internal.Data.Stream.Type as Stream
265+import qualified Streamly.Internal.Data.Unboxed as Unboxed
264266import qualified Prelude
265267
266268import Prelude hiding
@@ -351,6 +353,22 @@ data Array a =
351353 , aBound :: {-# UNPACK #-} !Int -- ^ first invalid index of arrContents.
352354 }
353355
356+-------------------------------------------------------------------------------
357+-- Pinning & Unpinning
358+-------------------------------------------------------------------------------
359+
360+{-# INLINE pin #-}
361+pin :: Array a -> IO (Array a)
362+pin arr@(Array{..}) = do
363+ contents <- Unboxed.pin arrContents
364+ return $ arr {arrContents = contents}
365+
366+{-# INLINE unpin #-}
367+unpin :: Array a -> IO (Array a)
368+unpin arr@(Array{..}) = do
369+ contents <- Unboxed.unpin arrContents
370+ return $ arr {arrContents = contents}
371+
354372-------------------------------------------------------------------------------
355373-- Construction
356374-------------------------------------------------------------------------------
@@ -388,6 +406,9 @@ newArrayWith alloc alignSize count = do
388406 , aBound = size
389407 }
390408
409+-- XXX Move this to Unboxed and rename this to newPinnedAlignedBytes?
410+-- XXX Similarly have newUnpinnedBytes
411+{-# INLINE newAlignedArrayContents #-}
391412newAlignedArrayContents :: Int -> Int -> IO (ArrayContents a)
392413newAlignedArrayContents nbytes _align | nbytes < 0 =
393414 errorWithoutStackTrace "newAlignedArrayContents: size must be >= 0"
@@ -417,6 +438,8 @@ nil = Array nilArrayContents 0 0 0
417438newArrayAligned :: (MonadIO m, Storable a) => Int -> Int -> m (Array a)
418439newArrayAligned = newArrayWith (\s a -> liftIO $ newAlignedArrayContents s a)
419440
441+-- XXX Intead of newArray, make newPinnedArray and newUnpinnedArray. Make it
442+-- more explicit.
420443-- XXX can unaligned allocation be more efficient when alignment is not needed?
421444--
422445-- | Allocates an empty array that can hold 'count' items. The memory of the