X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=GHC%2FIO%2FBufferedIO.hs;h=e1dc79d97a40504bbc207734b438f471b09c9a99;hb=d9310d8b5afbf8ba865187c4a3cac1b3c3e2db6f;hp=f5c874e01edb8883611a23abedf00ed3080b0094;hpb=207e64ee80ef749dbb46df0fa6a134b19d5a42a5;p=ghc-base.git diff --git a/GHC/IO/BufferedIO.hs b/GHC/IO/BufferedIO.hs index f5c874e..e1dc79d 100644 --- a/GHC/IO/BufferedIO.hs +++ b/GHC/IO/BufferedIO.hs @@ -1,5 +1,4 @@ {-# OPTIONS_GHC -XNoImplicitPrelude -funbox-strict-fields #-} -{-# OPTIONS_HADDOCK hide #-} ----------------------------------------------------------------------------- -- | -- Module : GHC.IO.BufferedIO @@ -52,8 +51,21 @@ class BufferedIO dev where -- buffer. fillReadBuffer0 :: dev -> Buffer Word8 -> IO (Maybe Int, Buffer Word8) - -- | Flush all the data from the supplied write buffer out to the device - flushWriteBuffer :: dev -> Buffer Word8 -> IO () + -- | Prepares an empty write buffer. This lets the device decide + -- how to set up a write buffer: the buffer may need to point to a + -- specific location in memory, for example. This is typically used + -- by the client when switching from reading to writing on a + -- buffered read/write device. + -- + -- There is no corresponding operation for read buffers, because before + -- reading the client will always call 'fillReadBuffer'. + emptyWriteBuffer :: dev -> Buffer Word8 -> IO (Buffer Word8) + emptyWriteBuffer _dev buf + = return buf{ bufL=0, bufR=0, bufState = WriteBuffer } + + -- | Flush all the data from the supplied write buffer out to the device. + -- The returned buffer should be empty, and ready for writing. + flushWriteBuffer :: dev -> Buffer Word8 -> IO (Buffer Word8) -- | Flush data from the supplied write buffer out to the device -- without blocking. Returns the number of bytes written and the @@ -65,8 +77,8 @@ class BufferedIO dev where -- for a memory-mapped file, the buffer will be the whole file in -- memory. fillReadBuffer sets the pointers to encompass the whole --- file, and flushWriteBuffer will do nothing. A memory-mapped file --- has to maintain its own file pointer. +-- file, and flushWriteBuffer needs to do no I/O. A memory-mapped +-- file has to maintain its own file pointer. -- for a bytestring, again the buffer should match the bytestring in -- memory. @@ -98,11 +110,12 @@ readBufNonBlocking dev bbuf = do Nothing -> return (Nothing, bbuf) Just n -> return (Just n, bbuf{ bufR = bufR bbuf + fromIntegral n }) -writeBuf :: RawIO dev => dev -> Buffer Word8 -> IO () +writeBuf :: RawIO dev => dev -> Buffer Word8 -> IO (Buffer Word8) writeBuf dev bbuf = do let bytes = bufferElems bbuf withBuffer bbuf $ \ptr -> IODevice.write dev (ptr `plusPtr` bufL bbuf) (fromIntegral bytes) + return bbuf{ bufL=0, bufR=0 } -- XXX ToDo writeBufNonBlocking :: RawIO dev => dev -> Buffer Word8 -> IO (Int, Buffer Word8) @@ -111,5 +124,4 @@ writeBufNonBlocking dev bbuf = do res <- withBuffer bbuf $ \ptr -> IODevice.writeNonBlocking dev (ptr `plusPtr` bufL bbuf) (fromIntegral bytes) - return (res, bbuf{ bufL = bufL bbuf + res }) - + return (res, bufferAdjustL res bbuf)