@@ -20,6 +20,8 @@ module Streamly.Internal.Data.Array.Foreign.Mut.Type
20 , ArrayContents
20 , ArrayContents
21 , nilArrayContents
21 , nilArrayContents
24 -- * Constructing and Writing
26 -- * Constructing and Writing
25 -- ** Construction
27 -- ** Construction
@@ -242,7 +244,6 @@ import GHC.Base
243import GHC.Base (noinline)
245import GHC.Base (noinline)
244import GHC.Exts (unsafeCoerce#)
246import GHC.Exts (unsafeCoerce#)
246import GHC.Ptr (Ptr(..))
247import GHC.Ptr (Ptr(..))
248import Streamly.Internal.Data.Fold.Type (Fold(..))
249import Streamly.Internal.Data.Fold.Type (Fold(..))
@@ -261,6 +262,7 @@ import qualified Streamly.Internal.Data.Producer as Producer
261import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
262import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
262import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
263import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
263import qualified Streamly.Internal.Data.Stream.Type as Stream
264import qualified Streamly.Internal.Data.Stream.Type as Stream
265+import qualified Streamly.Internal.Data.Unboxed as Unboxed
264import qualified Prelude
266import qualified Prelude
266import Prelude hiding
268import Prelude hiding
@@ -351,6 +353,22 @@ data Array a =
351 , aBound :: {-# UNPACK #-} !Int -- ^ first invalid index of arrContents.
353 , aBound :: {-# UNPACK #-} !Int -- ^ first invalid index of arrContents.
356+-------------------------------------------------------------------------------
357+-- Pinning & Unpinning
358+-------------------------------------------------------------------------------
361+pin :: Array a -> IO (Array a)
362+pin arr@(Array{..}) = do
363+ contents <- Unboxed.pin arrContents
364+ return $ arr {arrContents = contents}
367+unpin :: Array a -> IO (Array a)
368+unpin arr@(Array{..}) = do
369+ contents <- Unboxed.unpin arrContents
370+ return $ arr {arrContents = contents}
354-------------------------------------------------------------------------------
372-------------------------------------------------------------------------------
355-- Construction
373-- Construction
356-------------------------------------------------------------------------------
374-------------------------------------------------------------------------------
@@ -388,6 +406,9 @@ newArrayWith alloc alignSize count = do
388 , aBound = size
406 , aBound = size
409+-- XXX Move this to Unboxed and rename this to newPinnedAlignedBytes?
410+-- XXX Similarly have newUnpinnedBytes
411+{-# INLINE newAlignedArrayContents #-}
391newAlignedArrayContents :: Int -> Int -> IO (ArrayContents a)
412newAlignedArrayContents :: Int -> Int -> IO (ArrayContents a)
392newAlignedArrayContents nbytes _align | nbytes < 0 =
413newAlignedArrayContents nbytes _align | nbytes < 0 =
393 errorWithoutStackTrace "newAlignedArrayContents: size must be >= 0"
414 errorWithoutStackTrace "newAlignedArrayContents: size must be >= 0"
@@ -417,6 +438,8 @@ nil = Array nilArrayContents 0 0 0
417newArrayAligned :: (MonadIO m, Storable a) => Int -> Int -> m (Array a)
438newArrayAligned :: (MonadIO m, Storable a) => Int -> Int -> m (Array a)
418newArrayAligned = newArrayWith (\s a -> liftIO $ newAlignedArrayContents s a)
439newArrayAligned = newArrayWith (\s a -> liftIO $ newAlignedArrayContents s a)
441+-- XXX Intead of newArray, make newPinnedArray and newUnpinnedArray. Make it
420-- XXX can unaligned allocation be more efficient when alignment is not needed?
443-- XXX can unaligned allocation be more efficient when alignment is not needed?
422-- | Allocates an empty array that can hold 'count' items. The memory of the
445-- | Allocates an empty array that can hold 'count' items. The memory of the