Revert "Batch merge"
[ghc.git] / compiler / utils / FastMutInt.hs
1 {-# LANGUAGE BangPatterns, MagicHash, UnboxedTuples #-}
2 {-# OPTIONS_GHC -O2 #-}
3 -- We always optimise this, otherwise performance of a non-optimised
4 -- compiler is severely affected
5 --
6 -- (c) The University of Glasgow 2002-2006
7 --
8 -- Unboxed mutable Ints
9
10 module FastMutInt(
11 FastMutInt, newFastMutInt,
12 readFastMutInt, writeFastMutInt,
13
14 FastMutPtr, newFastMutPtr,
15 readFastMutPtr, writeFastMutPtr
16 ) where
17
18 import GhcPrelude
19
20 import Data.Bits
21 import GHC.Base
22 import GHC.Ptr
23
24 newFastMutInt :: IO FastMutInt
25 readFastMutInt :: FastMutInt -> IO Int
26 writeFastMutInt :: FastMutInt -> Int -> IO ()
27
28 newFastMutPtr :: IO FastMutPtr
29 readFastMutPtr :: FastMutPtr -> IO (Ptr a)
30 writeFastMutPtr :: FastMutPtr -> Ptr a -> IO ()
31
32 data FastMutInt = FastMutInt (MutableByteArray# RealWorld)
33
34 newFastMutInt = IO $ \s ->
35 case newByteArray# size s of { (# s, arr #) ->
36 (# s, FastMutInt arr #) }
37 where !(I# size) = finiteBitSize (0 :: Int)
38
39 readFastMutInt (FastMutInt arr) = IO $ \s ->
40 case readIntArray# arr 0# s of { (# s, i #) ->
41 (# s, I# i #) }
42
43 writeFastMutInt (FastMutInt arr) (I# i) = IO $ \s ->
44 case writeIntArray# arr 0# i s of { s ->
45 (# s, () #) }
46
47 data FastMutPtr = FastMutPtr (MutableByteArray# RealWorld)
48
49 newFastMutPtr = IO $ \s ->
50 case newByteArray# size s of { (# s, arr #) ->
51 (# s, FastMutPtr arr #) }
52 -- GHC assumes 'sizeof (Int) == sizeof (Ptr a)'
53 where !(I# size) = finiteBitSize (0 :: Int)
54
55 readFastMutPtr (FastMutPtr arr) = IO $ \s ->
56 case readAddrArray# arr 0# s of { (# s, i #) ->
57 (# s, Ptr i #) }
58
59 writeFastMutPtr (FastMutPtr arr) (Ptr i) = IO $ \s ->
60 case writeAddrArray# arr 0# i s of { s ->
61 (# s, () #) }