+hPutStr handle str = do
+ buffer_mode <- wantWriteableHandle_ "hPutStr" handle
+ (\ handle_ -> do getBuffer handle_)
+ case buffer_mode of
+ (NoBuffering, _, _) -> do
+ hPutChars handle str -- v. slow, but we don't care
+ (LineBuffering, buf, bsz) -> do
+ writeLines handle buf bsz str
+ (BlockBuffering _, buf, bsz) -> do
+ writeBlocks handle buf bsz str
+ -- ToDo: async exceptions during writeLines & writeBlocks will cause
+ -- the buffer to get lost in the void. Using ByteArrays instead of
+ -- malloced buffers is one way around this, but we really ought to
+ -- be able to handle it with exception handlers/block/unblock etc.
+
+getBuffer :: Handle__ -> IO (Handle__, (BufferMode, Ptr (), Int))
+getBuffer handle_ = do
+ let bufs = haBuffers__ handle_
+ fo = haFO__ handle_
+ mode = haBufferMode__ handle_
+ sz <- getBufSize fo
+ case mode of
+ NoBuffering -> return (handle_, (mode, nullPtr, 0))
+ _ -> case bufs of
+ [] -> do buf <- malloc sz
+ return (handle_, (mode, buf, sz))
+ (b:bs) -> return (handle_{ haBuffers__ = bs }, (mode, b, sz))
+
+freeBuffer :: Handle__ -> Ptr () -> Int -> IO Handle__
+freeBuffer handle_ buf sz = do
+ fo_sz <- getBufSize (haFO__ handle_)
+ if (sz /= fo_sz)
+ then do { free buf; return handle_ }
+ else do { return handle_{ haBuffers__ = buf : haBuffers__ handle_ } }
+
+swapBuffers :: Handle__ -> Ptr () -> Int -> IO Handle__
+swapBuffers handle_ buf sz = do
+ let fo = haFO__ handle_
+ fo_buf <- getBuf fo
+ setBuf fo buf sz
+ return (handle_{ haBuffers__ = fo_buf : haBuffers__ handle_ })
+
+-------------------------------------------------------------------------------
+-- commitAndReleaseBuffer handle buf sz count flush
+--
+-- Write the contents of the buffer 'buf' ('sz' bytes long, containing
+-- 'count' bytes of data) to handle (handle must be block or line buffered).
+--
+-- Implementation:
+--
+-- for block/line buffering,
+-- 1. If there isn't room in the handle buffer, flush the handle
+-- buffer.
+--
+-- 2. If the handle buffer is empty,
+-- if flush,
+-- then write buf directly to the device.
+-- else swap the handle buffer with buf.
+--
+-- 3. If the handle buffer is non-empty, copy buf into the
+-- handle buffer. Then, if flush != 0, flush
+-- the buffer.
+
+commitAndReleaseBuffer
+ :: Handle -- handle to commit to
+ -> Ptr () -> Int -- address and size (in bytes) of buffer
+ -> Int -- number of bytes of data in buffer
+ -> Bool -- flush the handle afterward?
+ -> IO ()
+
+commitAndReleaseBuffer hdl@(Handle h) buf sz count flush = do
+ h_ <- takeMVar h
+
+ -- First deal with any possible exceptions, by freeing the buffer.
+ -- Async exceptions are blocked, but there are still some interruptible
+ -- ops below.
+
+ -- note that commit doesn't *always* free the buffer, it might
+ -- swap it for the current handle buffer instead. This makes things
+ -- a whole lot more complicated, because we can't just do
+ -- "finally (... free buffer ...)" here.
+ catchException (commit hdl h_)
+ (\e -> do { h_ <- freeBuffer h_ buf sz; putMVar h h_ })
+
+ where
+ commit hdl@(Handle h) handle_ =
+ checkWriteableHandle "commitAndReleaseBuffer" hdl handle_ $ do
+ let fo = haFO__ handle_
+ flushConnectedBuf fo -- ???? -SDM
+ getWriteableBuf fo -- flush read buf if necessary
+ fo_buf <- getBuf fo
+ fo_wptr <- getBufWPtr fo
+ fo_bufSize <- getBufSize fo
+
+ let ok h_ = putMVar h h_ >> return ()
+
+ -- enough room in handle buffer for the new data?
+ if (flush || fo_bufSize - fo_wptr <= count)
+
+ -- The <= is to be sure that we never exactly fill up the
+ -- buffer, which would require a flush. So if copying the
+ -- new data into the buffer would make the buffer full, we
+ -- just flush the existing buffer and the new data immediately,
+ -- rather than copying before flushing.
+
+ then do rc <- mayBlock fo (flushFile fo)
+ if (rc < 0)
+ then constructErrorAndFail "commitAndReleaseBuffer"
+ else
+ if (flush || sz /= fo_bufSize || count == sz)
+ then do rc <- write_buf fo buf count
+ if (rc < 0)
+ then constructErrorAndFail "commitAndReleaseBuffer"
+ else do handle_ <- freeBuffer handle_ buf sz
+ ok handle_
+
+ -- if: (a) we don't have to flush, and
+ -- (b) size(new buffer) == size(old buffer), and
+ -- (c) new buffer is not full,
+ -- we can just just swap them over...
+ else do handle_ <- swapBuffers handle_ buf sz
+ setBufWPtr fo count
+ ok handle_
+
+ -- not flushing, and there's enough room in the buffer:
+ -- just copy the data in and update bufWPtr.
+ else do memcpy (plusPtr fo_buf fo_wptr) buf count
+ setBufWPtr fo (fo_wptr + count)
+ handle_ <- freeBuffer handle_ buf sz
+ ok handle_
+
+--------------------------------------------------------------------------------
+-- commitBuffer handle buf sz count flush
+--
+-- Flushes 'count' bytes from the buffer 'buf' (size 'sz') to 'handle'.
+-- There are several cases to consider altogether:
+--
+-- If flush,
+-- - flush handle buffer,
+-- - write out new buffer directly
+--
+-- else
+-- - if there's enough room in the handle buffer,
+-- then copy new buf into it
+-- else flush handle buffer, then copy new buffer into it
+--
+-- Make sure that we maintain the invariant that the handle buffer is never
+-- left in a full state. Several functions rely on this (eg. filePutc), so
+-- if we're about to exactly fill the buffer then we make sure we do a flush
+-- here (also see above in commitAndReleaseBuffer).
+
+commitBuffer
+ :: Handle -- handle to commit to
+ -> Ptr () -> Int -- address and size (in bytes) of buffer
+ -> Int -- number of bytes of data in buffer
+ -> Bool -- flush the handle afterward?
+ -> IO ()
+
+commitBuffer handle buf sz count flush = do
+ wantWriteableHandle "commitBuffer" handle $ \handle_ -> do
+ let fo = haFO__ handle_
+ flushConnectedBuf fo -- ???? -SDM
+ getWriteableBuf fo -- flush read buf if necessary
+ fo_buf <- getBuf fo
+ fo_wptr <- getBufWPtr fo
+ fo_bufSize <- getBufSize fo
+
+ new_wptr <- -- not enough room in handle buffer?
+ (if flush || (fo_bufSize - fo_wptr <= count)
+ then do rc <- mayBlock fo (flushFile fo)
+ if (rc < 0) then constructErrorAndFail "commitBuffer"
+ else return 0
+ else return fo_wptr )
+
+ if (flush || fo_bufSize <= count) -- committed buffer too large?
+
+ then do rc <- write_buf fo buf count
+ if (rc < 0) then constructErrorAndFail "commitBuffer"
+ else return ()
+
+ else do memcpy (plusPtr fo_buf new_wptr) buf count
+ setBufWPtr fo (new_wptr + count)
+ return ()
+
+write_buf fo buf 0 = return 0
+write_buf fo buf count = do
+ rc <- mayBlock fo (write_ fo buf count)
+ if (rc > 0)
+ then write_buf fo buf (count - rc) -- partial write
+ else return rc
+
+-- a version of commitBuffer that will free the buffer if an exception is
+-- received. DON'T use this if you intend to use the buffer again!
+checkedCommitBuffer handle buf sz count flush
+ = catchException (commitBuffer handle buf sz count flush)
+ (\e -> do withHandle__ handle (\h_ -> freeBuffer h_ buf sz)
+ throw e)
+
+foreign import "memcpy" unsafe memcpy :: Ptr () -> Ptr () -> Int -> IO ()