Warning fix for unused and redundant imports
[ghc-hetmet.git] / compiler / utils / BufWrite.hs
1 -----------------------------------------------------------------------------
2 --
3 -- Fast write-buffered Handles
4 --
5 -- (c) The University of Glasgow 2005-2006
6 --
7 -- This is a simple abstraction over Handles that offers very fast write
8 -- buffering, but without the thread safety that Handles provide.  It's used
9 -- to save time in Pretty.printDoc.
10 --
11 -----------------------------------------------------------------------------
12
13 module BufWrite (
14         BufHandle(..),
15         newBufHandle,
16         bPutChar,
17         bPutStr,
18         bPutFS,
19         bPutLitString,
20         bFlush,
21   ) where
22
23 #include "HsVersions.h"
24
25 import FastString
26 import FastMutInt
27
28 import Control.Monad    ( when )
29 import Data.Char        ( ord )
30 import Foreign
31 import System.IO
32
33 import GHC.IOBase       ( IO(..) )
34 import GHC.Ptr          ( Ptr(..) )
35
36 import GHC.Exts         ( Int(..), Int#, Addr# )
37
38 -- -----------------------------------------------------------------------------
39
40 data BufHandle = BufHandle {-#UNPACK#-}!(Ptr Word8)
41                            {-#UNPACK#-}!FastMutInt
42                            Handle
43
44 newBufHandle :: Handle -> IO BufHandle
45 newBufHandle hdl = do
46   ptr <- mallocBytes buf_size
47   r <- newFastMutInt
48   writeFastMutInt r 0
49   return (BufHandle ptr r hdl)
50
51 buf_size = 8192 :: Int
52
53 #define STRICT2(f) f a b | a `seq` b `seq` False = undefined
54 #define STRICT3(f) f a b c | a `seq` b `seq` c `seq` False = undefined
55
56 bPutChar :: BufHandle -> Char -> IO ()
57 STRICT2(bPutChar)
58 bPutChar b@(BufHandle buf r hdl) c = do
59   i <- readFastMutInt r
60   if (i >= buf_size)
61         then do hPutBuf hdl buf buf_size
62                 writeFastMutInt r 0
63                 bPutChar b c
64         else do pokeElemOff buf i (fromIntegral (ord c) :: Word8)
65                 writeFastMutInt r (i+1)
66
67 bPutStr :: BufHandle -> String -> IO ()
68 STRICT2(bPutStr)
69 bPutStr b@(BufHandle buf r hdl) str = do
70   i <- readFastMutInt r
71   loop str i
72   where loop _ i | i `seq` False = undefined
73         loop "" i = do writeFastMutInt r i; return ()
74         loop (c:cs) i
75            | i >= buf_size = do
76                 hPutBuf hdl buf buf_size
77                 loop (c:cs) 0
78            | otherwise = do
79                 pokeElemOff buf i (fromIntegral (ord c))
80                 loop cs (i+1)
81   
82 bPutFS :: BufHandle -> FastString -> IO ()
83 bPutFS b@(BufHandle buf r hdl) fs@(FastString _ len _ fp _) =
84  withForeignPtr fp $ \ptr -> do
85   i <- readFastMutInt r
86   if (i + len) >= buf_size
87         then do hPutBuf hdl buf i
88                 writeFastMutInt r 0
89                 if (len >= buf_size) 
90                     then hPutBuf hdl ptr len
91                     else bPutFS b fs
92         else do
93                 copyBytes (buf `plusPtr` i) ptr len
94                 writeFastMutInt r (i+len)
95
96 bPutLitString :: BufHandle -> Addr# -> Int# -> IO ()
97 bPutLitString b@(BufHandle buf r hdl) a# len# = do
98   let len = I# len#
99   i <- readFastMutInt r
100   if (i+len) >= buf_size
101         then do hPutBuf hdl buf i
102                 writeFastMutInt r 0
103                 if (len >= buf_size) 
104                     then hPutBuf hdl (Ptr a#) len
105                     else bPutLitString b a# len#
106         else do
107                 copyBytes (buf `plusPtr` i) (Ptr a#) len
108                 writeFastMutInt r (i+len)
109
110 bFlush :: BufHandle -> IO ()
111 bFlush b@(BufHandle buf r hdl) = do
112   i <- readFastMutInt r
113   when (i > 0) $ hPutBuf hdl buf i
114   free buf
115   return ()
116
117 #if 0
118 myPutBuf s hdl buf i = 
119   modifyIOError (\e -> ioeSetErrorString e (ioeGetErrorString e ++ ':':s ++ " (" ++ show buf ++ "," ++ show i ++ ")")) $
120
121   hPutBuf hdl buf i
122 #endif