Boost C++ Libraries Home Libraries People FAQ More

Home | Reference | Tutorial | Examples | Design

Buffers

To allow the development of efficient network applications, the asio library provides support for scatter-gather operations. These operations involve one or more buffers (where each buffer is a contiguous region of memory):

Therefore we require an abstraction to represent a collection of buffers. The approach asio uses is to define a type (actually two types) to represent a single buffer. These can be stored in a container, which may be passed to the scatter-gather operations.

A buffer, as a contiguous region of memory, can be represented by an address and size in bytes. There is a distinction between modifiable memory (called mutable in asio) and non-modifiable memory (where the latter is created from the storage for a const-qualified variable). These two types could therefore be defined as follows:

typedef std::pair<void*, std::size_t> mutable_buffer;
typedef std::pair<const void*, std::size_t> const_buffer;

Here, a mutable_buffer would be convertible to a const_buffer, but conversion in the opposite direction is not valid.

However, the asio library does not use the above definitions, but instead defines two classes: boost::asio::mutable_buffer and boost::asio::const_buffer. The goal of these is to provide an opaque representation of contiguous memory, where:

Finally, multiple buffers can be passed to scatter-gather operations (such as boost::asio::read or boost::asio::write) by putting the buffer objects into a container. The Mutable_Buffers and Const_Buffers concepts have been defined so that containers such as std::vector, std::list, std::vector or boost::array can be used. See the documentation for boost::asio::buffer for examples.
Copyright © 2003 - 2006 Christopher M. Kohlhoff

Home | Reference | Tutorial | Examples | Design