2 % (c) The AQUA Project, Glasgow University, 1994-1996
4 \section[IOExts]{Module @IOExts@}
6 @IOExts@ provides useful functionality that fall outside the
7 standard Haskell IO interface. Expect the contents of IOExts
8 to be the same for Hugs and GHC (same goes for any other
9 Hugs/GHC extension libraries, unless a function/type is
10 explicitly flagged as being implementation specific
14 {-# OPTIONS -fno-implicit-prelude #-}
21 , IORef -- instance of: Eq
28 , IOArray -- instance of: Eq
58 , freeHaskellFunctionPtr
71 import PrelHandle ( openFileEx, IOModeEx(..),
72 hSetEcho, hGetEcho, getHandleFd
80 import IO ( hPutStr, hPutChar )
81 import PrelAddr ( Addr )
85 unsafePtrEq :: a -> a -> Bool
88 unsafePtrEq = primReallyUnsafePtrEquality
91 case reallyUnsafePtrEquality# a b of
98 newIORef :: a -> IO (IORef a)
99 readIORef :: IORef a -> IO a
100 writeIORef :: IORef a -> a -> IO ()
103 type IORef a = STRef RealWorld a
105 readIORef = readSTRef
106 writeIORef = writeSTRef
108 newtype IORef a = IORef (MutableVar RealWorld a)
111 newIORef v = stToIO (newVar v) >>= \ var -> return (IORef var)
112 readIORef (IORef var) = stToIO (readVar var)
113 writeIORef (IORef var) v = stToIO (writeVar var v)
116 mkWeakIORef :: IORef a -> IO () -> IO (Weak (IORef a))
117 mkWeakIORef r@(IORef (MutableVar r#)) f = IO $ \s ->
118 case mkWeak# r# r f s of (# s1, w #) -> (# s1, Weak w #)
122 newIOArray :: Ix ix => (ix,ix) -> elt -> IO (IOArray ix elt)
123 boundsIOArray :: Ix ix => IOArray ix elt -> (ix, ix)
124 readIOArray :: Ix ix => IOArray ix elt -> ix -> IO elt
125 writeIOArray :: Ix ix => IOArray ix elt -> ix -> elt -> IO ()
126 freezeIOArray :: Ix ix => IOArray ix elt -> IO (Array ix elt)
127 thawIOArray :: Ix ix => Array ix elt -> IO (IOArray ix elt)
129 unsafeFreezeIOArray :: Ix ix => IOArray ix elt -> IO (Array ix elt)
130 unsafeThawIOArray :: Ix ix => Array ix elt -> IO (IOArray ix elt)
134 type IOArray ix elt = STArray RealWorld ix elt
135 newIOArray = newSTArray
136 boundsIOArray = boundsSTArray
137 readIOArray = readSTArray
138 writeIOArray = writeSTArray
139 freezeIOArray = freezeSTArray
140 thawIOArray = thawSTArray
142 newtype IOArray ix elt = IOArray (MutableArray RealWorld ix elt)
146 stToIO (newArray ixs elt) >>= \arr ->
149 boundsIOArray (IOArray arr) = boundsOfArray arr
151 readIOArray (IOArray arr) ix = stToIO (readArray arr ix)
153 writeIOArray (IOArray arr) ix elt = stToIO (writeArray arr ix elt)
155 freezeIOArray (IOArray arr) = stToIO (freezeArray arr)
158 marr <- stToIO (thawArray arr)
159 return (IOArray marr)
161 unsafeFreezeIOArray (IOArray arr) = stToIO (unsafeFreezeArray arr)
162 unsafeThawIOArray arr = do
163 marr <- stToIO (unsafeThawArray arr)
164 return (IOArray marr)
169 {-# NOINLINE trace #-}
170 trace :: String -> a -> a
172 trace string expr = unsafePerformIO $ do
176 trace string expr = unsafePerformIO $ do
177 fd <- getHandleFd stderr
178 hPutStr stderr string
180 _ccall_ PostTraceHook fd
185 Not something you want to call normally, but useful
186 in the cases where you do want to flush stuff out of
187 the heap or make sure you've got room enough
193 performGC = _ccall_GC_ performGC
197 When using 'foreign export dynamic' to dress up a Haskell
198 IO action to look like a C function pointer, a little bit
199 of memory is allocated (along with a stable pointer to
200 the Haskell IO action). When done with the C function
201 pointer, you'll need to call @freeHaskellFunctionPtr()@ to
202 let go of these resources - here's the Haskell wrapper for
203 that RTS entry point, should you want to free it from
207 foreign import ccall "freeHaskellFunctionPtr"
208 freeHaskellFunctionPtr :: Addr -> IO ()