2 % (c) The AQUA Project, Glasgow University, 1994-1996
4 \section[PrelArrExtra]{Module @PrelArrExtra@}
6 The following functions should be in PrelArr, but need -monly-2-regs
7 to compile. So as not to compile the whole of PrelArr with
8 -monly-2-regs, the culprits have been moved out into a separate
12 {-# OPTIONS -fno-implicit-prelude #-}
14 module PrelArrExtra where
24 %*********************************************************
26 \subsection{Moving between mutable and immutable}
28 %*********************************************************
31 freezeByteArray :: Ix ix => MutableByteArray s ix -> ST s (ByteArray ix)
33 {-# SPECIALISE freezeByteArray :: MutableByteArray s Int -> ST s (ByteArray Int) #-}
35 -- This coercion of memcpy to the ST monad is safe, because memcpy
36 -- only modifies its destination operand, which is already MutableByteArray.
37 freezeByteArray (MutableByteArray l u arr) = ST $ \ s ->
38 let n = sizeofMutableByteArray# arr in
39 case (newCharArray# n s) of { (# s, newarr #) ->
40 case ((unsafeCoerce# memcpy) newarr arr n s) of { (# s, () #) ->
41 case unsafeFreezeByteArray# newarr s of { (# s, frozen #) ->
42 (# s, ByteArray l u frozen #) }}}
44 foreign import "memcpy" unsafe
45 memcpy :: MutableByteArray# RealWorld -> ByteArray# -> Int# -> IO ()
47 unsafeFreezeByteArray :: Ix ix => MutableByteArray s ix -> ST s (ByteArray ix)
49 {-# SPECIALIZE unsafeFreezeByteArray :: MutableByteArray s Int -> ST s (ByteArray Int)
52 unsafeFreezeByteArray (MutableByteArray l u arr#) = ST $ \ s# ->
53 case unsafeFreezeByteArray# arr# s# of { (# s2#, frozen# #) ->
54 (# s2#, ByteArray l u frozen# #) }