FastMutInt.hs 1.63 KB
Newer Older
1
{-# LANGUAGE CPP, BangPatterns, MagicHash, UnboxedTuples #-}
2 3 4
{-# OPTIONS_GHC -O #-}
-- We always optimise this, otherwise performance of a non-optimised
-- compiler is severely affected
5
--
Simon Marlow's avatar
Simon Marlow committed
6
-- (c) The University of Glasgow 2002-2006
7 8 9 10
--
-- Unboxed mutable Ints

module FastMutInt(
11 12
        FastMutInt, newFastMutInt,
        readFastMutInt, writeFastMutInt,
13

14 15
        FastMutPtr, newFastMutPtr,
        readFastMutPtr, writeFastMutPtr
16 17 18
  ) where


Simon Marlow's avatar
Simon Marlow committed
19
#include "../includes/MachDeps.h"
20 21 22 23
#ifndef SIZEOF_HSINT
#define SIZEOF_HSINT  INT_SIZE_IN_BYTES
#endif

24
import GHC.Base
25
import GHC.Ptr
26 27 28 29

newFastMutInt :: IO FastMutInt
readFastMutInt :: FastMutInt -> IO Int
writeFastMutInt :: FastMutInt -> Int -> IO ()
30 31 32 33

newFastMutPtr :: IO FastMutPtr
readFastMutPtr :: FastMutPtr -> IO (Ptr a)
writeFastMutPtr :: FastMutPtr -> Ptr a -> IO ()
34 35 36 37 38 39

data FastMutInt = FastMutInt (MutableByteArray# RealWorld)

newFastMutInt = IO $ \s ->
  case newByteArray# size s of { (# s, arr #) ->
  (# s, FastMutInt arr #) }
40
  where !(I# size) = SIZEOF_HSINT
41 42 43 44 45 46 47 48

readFastMutInt (FastMutInt arr) = IO $ \s ->
  case readIntArray# arr 0# s of { (# s, i #) ->
  (# s, I# i #) }

writeFastMutInt (FastMutInt arr) (I# i) = IO $ \s ->
  case writeIntArray# arr 0# i s of { s ->
  (# s, () #) }
49 50 51 52 53 54

data FastMutPtr = FastMutPtr (MutableByteArray# RealWorld)

newFastMutPtr = IO $ \s ->
  case newByteArray# size s of { (# s, arr #) ->
  (# s, FastMutPtr arr #) }
55
  where !(I# size) = SIZEOF_VOID_P
56 57 58 59 60 61 62 63

readFastMutPtr (FastMutPtr arr) = IO $ \s ->
  case readAddrArray# arr 0# s of { (# s, i #) ->
  (# s, Ptr i #) }

writeFastMutPtr (FastMutPtr arr) (Ptr i) = IO $ \s ->
  case writeAddrArray# arr 0# i s of { s ->
  (# s, () #) }