base- Basic libraries
Copyright(c) The University of Glasgow 2008
Licensesee libraries/base/LICENSE
Portabilitynon-portable (GHC Extensions)
Safe HaskellTrustworthy



Buffers used in the IO system


Buffers of any element

data Buffer e Source #

A mutable array of bytes that can be passed to foreign functions.

The buffer is represented by a record, where the record contains the raw buffer and the start/end points of the filled portion. The buffer contents itself is mutable, but the rest of the record is immutable. This is a slightly odd mix, but it turns out to be quite practical: by making all the buffer metadata immutable, we can have operations on buffer metadata outside of the IO monad.

The "live" elements of the buffer are those between the bufL and bufR offsets. In an empty buffer, bufL is equal to bufR, but they might not be zero: for example, the buffer might correspond to a memory-mapped file and in which case bufL will point to the next location to be written, which is not necessarily the beginning of the file.

On Posix systems the I/O manager has an implicit reliance on doing a file read moving the file pointer. However on Windows async operations the kernel object representing a file does not use the file pointer offset. Logically this makes sense since operations can be performed in any arbitrary order. OVERLAPPED operations don't respect the file pointer offset as their intention is to support arbitrary async reads to anywhere at a much lower level. As such we should explicitly keep track of the file offsets of the target in the buffer. Any operation to seek should also update this entry.

In order to keep us sane we try to uphold the invariant that any function being passed a Handle is responsible for updating the handles offset unless other behaviour is documented.




data BufferState Source #




Instances details
Eq BufferState #

Since: base-

Instance details

Defined in GHC.IO.Buffer



slideContents :: Buffer Word8 -> IO (Buffer Word8) Source #

slides the contents of the buffer to the beginning


Operating on the raw buffer as a Ptr

withBuffer :: Buffer e -> (Ptr e -> IO a) -> IO a Source #

withRawBuffer :: RawBuffer e -> (Ptr e -> IO a) -> IO a Source #


Raw buffers