1 % -----------------------------------------------------------------------------
2 % $Id: PrelArrExtra.lhs,v 1.10 2000/06/30 13:39:35 simonmar Exp $
4 % (c) The University of Glasgow, 1994-2000
7 \section[PrelArrExtra]{Module @PrelArrExtra@}
9 The following functions should be in PrelArr, but need -monly-2-regs
10 to compile. So as not to compile the whole of PrelArr with
11 -monly-2-regs, the culprits have been moved out into a separate
15 {-# OPTIONS -fno-implicit-prelude #-}
17 module PrelArrExtra where
27 %*********************************************************
29 \subsection{Moving between mutable and immutable}
31 %*********************************************************
34 freezeByteArray :: Ix ix => MutableByteArray s ix -> ST s (ByteArray ix)
36 {-# SPECIALISE freezeByteArray :: MutableByteArray s Int -> ST s (ByteArray Int) #-}
38 -- This coercion of memcpy to the ST monad is safe, because memcpy
39 -- only modifies its destination operand, which is already MutableByteArray.
40 freezeByteArray (MutableByteArray l u arr) = ST $ \ s ->
41 let n = sizeofMutableByteArray# arr in
42 case (newCharArray# n s) of { (# s, newarr #) ->
43 case ((unsafeCoerce# memcpy) newarr arr n s) of { (# s, () #) ->
44 case unsafeFreezeByteArray# newarr s of { (# s, frozen #) ->
45 (# s, ByteArray l u frozen #) }}}
47 foreign import "memcpy" unsafe
48 memcpy :: MutableByteArray# RealWorld -> ByteArray# -> Int# -> IO ()
50 unsafeFreezeByteArray :: Ix ix => MutableByteArray s ix -> ST s (ByteArray ix)
52 {-# SPECIALIZE unsafeFreezeByteArray :: MutableByteArray s Int -> ST s (ByteArray Int)
55 unsafeFreezeByteArray (MutableByteArray l u arr#) = ST $ \ s# ->
56 case unsafeFreezeByteArray# arr# s# of { (# s2#, frozen# #) ->
57 (# s2#, ByteArray l u frozen# #) }