@@ -13,8 +13,9 @@
14module Streamly.Internal.Data.Unbox
14module Streamly.Internal.Data.Unbox
16− -- ** MutableByteArray
16+ -- ** MutByteArray
17− MutableByteArray(..)
17+ MutByteArray(..)
18 , getMutableByteArray#
19 , getMutableByteArray#
19 , sizeOfMutableByteArray
20 , sizeOfMutableByteArray
@@ -100,23 +101,26 @@ data PinnedState
100-- | A lifted mutable byte array type wrapping @MutableByteArray# RealWorld@.
101-- | A lifted mutable byte array type wrapping @MutableByteArray# RealWorld@.
101-- This is a low level array used to back high level unboxed arrays and
102-- This is a low level array used to back high level unboxed arrays and
102-- serialized data.
103-- serialized data.
103−data MutableByteArray = MutableByteArray (MutableByteArray# RealWorld)
104+data MutByteArray = MutByteArray (MutableByteArray# RealWorld)
106+{-# DEPRECATED MutableByteArray "Please use MutByteArray instead" #-}
107+type MutableByteArray = MutByteArray
105{-# INLINE getMutableByteArray# #-}
109{-# INLINE getMutableByteArray# #-}
106−getMutableByteArray# :: MutableByteArray -> MutableByteArray# RealWorld
110+getMutableByteArray# :: MutByteArray -> MutableByteArray# RealWorld
107−getMutableByteArray# (MutableByteArray mbarr) = mbarr
111+getMutableByteArray# (MutByteArray mbarr) = mbarr
109-- | Return the size of the array in bytes.
113-- | Return the size of the array in bytes.
110{-# INLINE sizeOfMutableByteArray #-}
114{-# INLINE sizeOfMutableByteArray #-}
111−sizeOfMutableByteArray :: MutableByteArray -> IO Int
115+sizeOfMutableByteArray :: MutByteArray -> IO Int
112−sizeOfMutableByteArray (MutableByteArray arr) =
116+sizeOfMutableByteArray (MutByteArray arr) =
113 IO $ \s ->
117 IO $ \s ->
114 case getSizeofMutableByteArray# arr s of
118 case getSizeofMutableByteArray# arr s of
115 (# s1, i #) -> (# s1, I# i #)
119 (# s1, i #) -> (# s1, I# i #)
117{-# INLINE touch #-}
121{-# INLINE touch #-}
118−touch :: MutableByteArray -> IO ()
122+touch :: MutByteArray -> IO ()
119−touch (MutableByteArray contents) =
123+touch (MutByteArray contents) =
120 IO $ \s -> case touch# contents s of s' -> (# s', () #)
124 IO $ \s -> case touch# contents s of s' -> (# s', () #)
122-- XXX We can provide another API for "unsafe" FFI calls passing an unlifted
126-- XXX We can provide another API for "unsafe" FFI calls passing an unlifted
@@ -131,7 +135,7 @@ touch (MutableByteArray contents) =
131-- guarantees that unsafe calls will be performed in the calling thread. Making
135-- guarantees that unsafe calls will be performed in the calling thread. Making
132-- it safe to pass heap-allocated objects to unsafe functions.
136-- it safe to pass heap-allocated objects to unsafe functions.
134−-- | Use a @MutableByteArray@ as @Ptr a@. This is useful when we want to pass
138+-- | Use a @MutByteArray@ as @Ptr a@. This is useful when we want to pass
135-- an array as a pointer to some operating system call or to a "safe" FFI call.
139-- an array as a pointer to some operating system call or to a "safe" FFI call.
137-- If the array is not pinned it is copied to pinned memory before passing it
141-- If the array is not pinned it is copied to pinned memory before passing it
@@ -148,7 +152,7 @@ touch (MutableByteArray contents) =
148-- /Pre-release/
152-- /Pre-release/
150{-# INLINE asPtrUnsafe #-}
154{-# INLINE asPtrUnsafe #-}
151−asPtrUnsafe :: MonadIO m => MutableByteArray -> (Ptr a -> m b) -> m b
155+asPtrUnsafe :: MonadIO m => MutByteArray -> (Ptr a -> m b) -> m b
152asPtrUnsafe arr f = do
156asPtrUnsafe arr f = do
153 contents <- liftIO $ pin arr
157 contents <- liftIO $ pin arr
154 let !ptr = Ptr (byteArrayContents#
158 let !ptr = Ptr (byteArrayContents#
@@ -162,41 +166,41 @@ asPtrUnsafe arr f = do
162--------------------------------------------------------------------------------
166--------------------------------------------------------------------------------
164{-# NOINLINE nil #-}
168{-# NOINLINE nil #-}
165−nil :: MutableByteArray
169+nil :: MutByteArray
166nil = unsafePerformIO $ newBytes 0
170nil = unsafePerformIO $ newBytes 0
168{-# INLINE newBytes #-}
172{-# INLINE newBytes #-}
169−newBytes :: Int -> IO MutableByteArray
173+newBytes :: Int -> IO MutByteArray
170newBytes nbytes | nbytes < 0 =
174newBytes nbytes | nbytes < 0 =
171 errorWithoutStackTrace "newBytes: size must be >= 0"
175 errorWithoutStackTrace "newBytes: size must be >= 0"
172newBytes (I# nbytes) = IO $ \s ->
176newBytes (I# nbytes) = IO $ \s ->
173 case newByteArray# nbytes s of
177 case newByteArray# nbytes s of
174 (# s', mbarr# #) ->
178 (# s', mbarr# #) ->
175− let c = MutableByteArray mbarr#
179+ let c = MutByteArray mbarr#
176 in (# s', c #)
180 in (# s', c #)
178{-# INLINE pinnedNewBytes #-}
182{-# INLINE pinnedNewBytes #-}
179−pinnedNewBytes :: Int -> IO MutableByteArray
183+pinnedNewBytes :: Int -> IO MutByteArray
180pinnedNewBytes nbytes | nbytes < 0 =
184pinnedNewBytes nbytes | nbytes < 0 =
181 errorWithoutStackTrace "pinnedNewBytes: size must be >= 0"
185 errorWithoutStackTrace "pinnedNewBytes: size must be >= 0"
182pinnedNewBytes (I# nbytes) = IO $ \s ->
186pinnedNewBytes (I# nbytes) = IO $ \s ->
183 case newPinnedByteArray# nbytes s of
187 case newPinnedByteArray# nbytes s of
184 (# s', mbarr# #) ->
188 (# s', mbarr# #) ->
185− let c = MutableByteArray mbarr#
189+ let c = MutByteArray mbarr#
186 in (# s', c #)
190 in (# s', c #)
188{-# INLINE pinnedNewAlignedBytes #-}
192{-# INLINE pinnedNewAlignedBytes #-}
189−pinnedNewAlignedBytes :: Int -> Int -> IO MutableByteArray
193+pinnedNewAlignedBytes :: Int -> Int -> IO MutByteArray
190pinnedNewAlignedBytes nbytes _align | nbytes < 0 =
194pinnedNewAlignedBytes nbytes _align | nbytes < 0 =
191 errorWithoutStackTrace "pinnedNewAlignedBytes: size must be >= 0"
195 errorWithoutStackTrace "pinnedNewAlignedBytes: size must be >= 0"
192pinnedNewAlignedBytes (I# nbytes) (I# align) = IO $ \s ->
196pinnedNewAlignedBytes (I# nbytes) (I# align) = IO $ \s ->
193 case newAlignedPinnedByteArray# nbytes align s of
197 case newAlignedPinnedByteArray# nbytes align s of
194 (# s', mbarr# #) ->
198 (# s', mbarr# #) ->
195− let c = MutableByteArray mbarr#
199+ let c = MutByteArray mbarr#
196 in (# s', c #)
200 in (# s', c #)
198{-# INLINE newBytesAs #-}
202{-# INLINE newBytesAs #-}
199−newBytesAs :: PinnedState -> Int -> IO MutableByteArray
203+newBytesAs :: PinnedState -> Int -> IO MutByteArray
200newBytesAs Unpinned = newBytes
204newBytesAs Unpinned = newBytes
201newBytesAs Pinned = pinnedNewBytes
205newBytesAs Pinned = pinnedNewBytes
@@ -209,9 +213,9 @@ newBytesAs Pinned = pinnedNewBytes
209{-# INLINE putSliceUnsafe #-}
213{-# INLINE putSliceUnsafe #-}
210putSliceUnsafe ::
214putSliceUnsafe ::
211 MonadIO m
215 MonadIO m
212− => MutableByteArray
216+ => MutByteArray
214− -> MutableByteArray
218+ -> MutByteArray
@@ -236,8 +240,8 @@ putSliceUnsafe src srcStartBytes dst dstStartBytes lenBytes = liftIO $ do
236-------------------------------------------------------------------------------
240-------------------------------------------------------------------------------
238{-# INLINE isPinned #-}
242{-# INLINE isPinned #-}
239−isPinned :: MutableByteArray -> Bool
243+isPinned :: MutByteArray -> Bool
240−isPinned (MutableByteArray arr#) =
244+isPinned (MutByteArray arr#) =
241 let pinnedInt = I# (isMutableByteArrayPinned# arr#)
245 let pinnedInt = I# (isMutableByteArrayPinned# arr#)
242 in pinnedInt /= 0
246 in pinnedInt /= 0
@@ -258,8 +262,8 @@ cloneMutableArrayWith# alloc# arr# s# =
258 s3# -> (# s3#, arr1# #)
262 s3# -> (# s3#, arr1# #)
260{-# INLINE pin #-}
264{-# INLINE pin #-}
261−pin :: MutableByteArray -> IO MutableByteArray
265+pin :: MutByteArray -> IO MutByteArray
262−pin arr@(MutableByteArray marr#) =
266+pin arr@(MutByteArray marr#) =
263 if isPinned arr
267 if isPinned arr
264 then return arr
268 then return arr
@@ -271,11 +275,11 @@ pin arr@(MutableByteArray marr#) =
273 case cloneMutableArrayWith# newPinnedByteArray# marr# s# of
277 case cloneMutableArrayWith# newPinnedByteArray# marr# s# of
274− (# s1#, marr1# #) -> (# s1#, MutableByteArray marr1# #)
278+ (# s1#, marr1# #) -> (# s1#, MutByteArray marr1# #)
276{-# INLINE unpin #-}
280{-# INLINE unpin #-}
277−unpin :: MutableByteArray -> IO MutableByteArray
281+unpin :: MutByteArray -> IO MutByteArray
278−unpin arr@(MutableByteArray marr#) =
282+unpin arr@(MutByteArray marr#) =
279 if not (isPinned arr)
283 if not (isPinned arr)
280 then return arr
284 then return arr
@@ -287,7 +291,7 @@ unpin arr@(MutableByteArray marr#) =
289 case cloneMutableArrayWith# newByteArray# marr# s# of
293 case cloneMutableArrayWith# newByteArray# marr# s# of
290− (# s1#, marr1# #) -> (# s1#, MutableByteArray marr1# #)
294+ (# s1#, marr1# #) -> (# s1#, MutByteArray marr1# #)
292--------------------------------------------------------------------------------
296--------------------------------------------------------------------------------
293-- The Unbox type class
297-- The Unbox type class
@@ -458,11 +462,11 @@ class Unbox a where
459 -- IMPORTANT: The implementation of this interface may not check the bounds
463 -- IMPORTANT: The implementation of this interface may not check the bounds
460 -- of the array, the caller must not assume that.
464 -- of the array, the caller must not assume that.
461− peekByteIndex :: Int -> MutableByteArray -> IO a
465+ peekByteIndex :: Int -> MutByteArray -> IO a
463 {-# INLINE peekByteIndex #-}
467 {-# INLINE peekByteIndex #-}
464 default peekByteIndex :: (Generic a, PeekRep (Rep a)) =>
468 default peekByteIndex :: (Generic a, PeekRep (Rep a)) =>
465− Int -> MutableByteArray -> IO a
469+ Int -> MutByteArray -> IO a
466 peekByteIndex i arr = genericPeekByteIndex arr i
470 peekByteIndex i arr = genericPeekByteIndex arr i
468 -- | @pokeByteIndex byte-index array@ writes an element of type @a@ to the
472 -- | @pokeByteIndex byte-index array@ writes an element of type @a@ to the
@@ -470,11 +474,11 @@ class Unbox a where
471 -- IMPORTANT: The implementation of this interface may not check the bounds
475 -- IMPORTANT: The implementation of this interface may not check the bounds
472 -- of the array, the caller must not assume that.
476 -- of the array, the caller must not assume that.
473− pokeByteIndex :: Int -> MutableByteArray -> a -> IO ()
477+ pokeByteIndex :: Int -> MutByteArray -> a -> IO ()
475 {-# INLINE pokeByteIndex #-}
479 {-# INLINE pokeByteIndex #-}
476 default pokeByteIndex :: (Generic a, PokeRep (Rep a)) =>
480 default pokeByteIndex :: (Generic a, PokeRep (Rep a)) =>
477− Int -> MutableByteArray -> a -> IO ()
481+ Int -> MutByteArray -> a -> IO ()
478 pokeByteIndex i arr = genericPokeByteIndex arr i
482 pokeByteIndex i arr = genericPokeByteIndex arr i
480-- XXX Add asserts to check bounds
484-- XXX Add asserts to check bounds
@@ -482,11 +486,11 @@ class Unbox a where
482#define DERIVE_UNBOXED(_type, _constructor, _readArray, _writeArray, _sizeOf) \
486#define DERIVE_UNBOXED(_type, _constructor, _readArray, _writeArray, _sizeOf) \
483instance Unbox _type where { \
487instance Unbox _type where { \
484; {-# INLINE peekByteIndex #-} \
488; {-# INLINE peekByteIndex #-} \
485−; peekByteIndex (I# n) (MutableByteArray mbarr) = IO $ \s -> \
489+; peekByteIndex (I# n) (MutByteArray mbarr) = IO $ \s -> \
486 case _readArray mbarr n s of \
490 case _readArray mbarr n s of \
487 { (# s1, i #) -> (# s1, _constructor i #) } \
491 { (# s1, i #) -> (# s1, _constructor i #) } \
488; {-# INLINE pokeByteIndex #-} \
492; {-# INLINE pokeByteIndex #-} \
489−; pokeByteIndex (I# n) (MutableByteArray mbarr) (_constructor val) = \
493+; pokeByteIndex (I# n) (MutByteArray mbarr) (_constructor val) = \
490 IO $ \s -> (# _writeArray mbarr n val s, () #) \
494 IO $ \s -> (# _writeArray mbarr n val s, () #) \
491; {-# INLINE sizeOf #-} \
495; {-# INLINE sizeOf #-} \
492; sizeOf _ = _sizeOf \
496; sizeOf _ = _sizeOf \
@@ -681,7 +685,7 @@ instance Unbox Bool where
681-- cheaper to just get the bound using the size of the array whenever needed?
685-- cheaper to just get the bound using the size of the array whenever needed?
682data BoundedPtr =
686data BoundedPtr =
683 BoundedPtr
687 BoundedPtr
684− MutableByteArray -- byte array
688+ MutByteArray -- byte array
685 Int -- current pos
689 Int -- current pos
686 Int -- position after end
690 Int -- position after end
@@ -966,7 +970,7 @@ genericPokeObj a ptr = void $ genericPokeObject a ptr
967{-# INLINE genericPokeByteIndex #-}
971{-# INLINE genericPokeByteIndex #-}
968genericPokeByteIndex :: (Generic a, PokeRep (Rep a)) =>
972genericPokeByteIndex :: (Generic a, PokeRep (Rep a)) =>
969− MutableByteArray -> Int -> a -> IO ()
973+ MutByteArray -> Int -> a -> IO ()
970genericPokeByteIndex arr index x = do
974genericPokeByteIndex arr index x = do
971 -- XXX Should we use unsafe poke?
975 -- XXX Should we use unsafe poke?
972#ifdef DEBUG
976#ifdef DEBUG
@@ -1072,7 +1076,7 @@ genericPeekBoundedPtr = runPeeker genericPeeker
1073{-# INLINE genericPeekByteIndex #-}
1077{-# INLINE genericPeekByteIndex #-}
1074genericPeekByteIndex :: (Generic a, PeekRep (Rep a)) =>
1078genericPeekByteIndex :: (Generic a, PeekRep (Rep a)) =>
1075− MutableByteArray -> Int -> IO a
1079+ MutByteArray -> Int -> IO a
1076genericPeekByteIndex arr index = do
1080genericPeekByteIndex arr index = do
1077 -- XXX Should we use unsafe peek?
1081 -- XXX Should we use unsafe peek?
1078#ifdef DEBUG
1082#ifdef DEBUG