* Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef EVENT2_BUFFER_H_INCLUDED_
#define EVENT2_BUFFER_H_INCLUDED_
/** @file event2/buffer.h
Functions for buffering data for network sending or receiving.
An evbuffer can be used for preparing data before sending it to
the network or conversely for reading data from the network.
Evbuffers try to avoid memory copies as much as possible. As a
result, evbuffers can be used to pass data around without actually
incurring the overhead of copying the data.
A new evbuffer can be allocated with evbuffer_new(), and can be
freed with evbuffer_free(). Most users will be using evbuffers via
the bufferevent interface. To access a bufferevent's evbuffers, use
bufferevent_get_input() and bufferevent_get_output().
There are several guidelines for using evbuffers.
- if you already know how much data you are going to add as a result
of calling evbuffer_add() multiple times, it makes sense to use
evbuffer_expand() first to make sure that enough memory is allocated
- evbuffer_add_buffer() adds the contents of one buffer to the other
without incurring any unnecessary memory copies.
- evbuffer_add() and evbuffer_add_buffer() do not mix very well:
if you use them, you will wind up with fragmented memory in your
- For high-performance code, you may want to avoid copying data into and out
of buffers. You can skip the copy step by using
evbuffer_reserve_space()/evbuffer_commit_space() when writing into a
buffer, and evbuffer_peek() when reading.
In Libevent 2.0 and later, evbuffers are represented using a linked
list of memory chunks, with pointers to the first and last chunk in
As the contents of an evbuffer can be stored in multiple different
memory blocks, it cannot be accessed directly. Instead, evbuffer_pullup()
can be used to force a specified number of bytes to be contiguous. This
will cause memory reallocation and memory copies if the data is split
across multiple blocks. It is more efficient, however, to use
evbuffer_peek() if you don't require that the memory to be contiguous.
#include <event2/visibility.h>
#include <event2/event-config.h>
#ifdef EVENT__HAVE_SYS_TYPES_H
#ifdef EVENT__HAVE_SYS_UIO_H
An evbuffer is an opaque data type for efficiently buffering data to be
sent or received on the network.
@see event2/event.h for more information
Pointer to a position within an evbuffer.
Used when repeatedly searching through a buffer. Calling any function
that modifies or re-packs the buffer contents may invalidate all
evbuffer_ptrs for that buffer. Do not modify or contruct these values
except with evbuffer_ptr_set.
An evbuffer_ptr can represent any position from the start of a buffer up
to a position immediately after the end of a buffer.
/* Do not alter or rely on the values of fields: they are for internal
/** Describes a single extent of memory inside an evbuffer. Used for
@see evbuffer_reserve_space, evbuffer_commit_space, evbuffer_peek
#ifdef EVENT__HAVE_SYS_UIO_H
#define evbuffer_iovec iovec
/* Internal use -- defined only if we are using the native struct iovec */
#define EVBUFFER_IOVEC_IS_NATIVE_
/** The start of the extent of memory. */
/** The length of the extent of memory. */
Allocate storage for a new evbuffer.
@return a pointer to a newly allocated evbuffer struct, or NULL if an error
struct evbuffer *evbuffer_new(void);
Deallocate storage for an evbuffer.
@param buf pointer to the evbuffer to be freed
void evbuffer_free(struct evbuffer *buf);
Enable locking on an evbuffer so that it can safely be used by multiple
threads at the same time.
NOTE: when locking is enabled, the lock will be held when callbacks are
invoked. This could result in deadlock if you aren't careful. Plan
@param buf An evbuffer to make lockable.
@param lock A lock object, or NULL if we should allocate our own.
@return 0 on success, -1 on failure.
int evbuffer_enable_locking(struct evbuffer *buf, void *lock);
Acquire the lock on an evbuffer. Has no effect if locking was not enabled
with evbuffer_enable_locking.
void evbuffer_lock(struct evbuffer *buf);
Release the lock on an evbuffer. Has no effect if locking was not enabled
with evbuffer_enable_locking.
void evbuffer_unlock(struct evbuffer *buf);
/** If this flag is set, then we will not use evbuffer_peek(),
* evbuffer_remove(), evbuffer_remove_buffer(), and so on to read bytes
* from this buffer: we'll only take bytes out of this buffer by
* writing them to the network (as with evbuffer_write_atmost), by
* removing them without observing them (as with evbuffer_drain),
* or by copying them all out at once (as with evbuffer_add_buffer).
* Using this option allows the implementation to use sendfile-based
* operations for evbuffer_add_file(); see that function for more
* This flag is on by default for bufferevents that can take advantage
* of it; you should never actually need to set it on a bufferevent's
#define EVBUFFER_FLAG_DRAINS_TO_FD 1
/** Change the flags that are set for an evbuffer by adding more.
* @param buffer the evbuffer that the callback is watching.
* @param cb the callback whose status we want to change.
* @param flags One or more EVBUFFER_FLAG_* options
* @return 0 on success, -1 on failure.
int evbuffer_set_flags(struct evbuffer *buf, ev_uint64_t flags);
/** Change the flags that are set for an evbuffer by removing some.
* @param buffer the evbuffer that the callback is watching.
* @param cb the callback whose status we want to change.
* @param flags One or more EVBUFFER_FLAG_* options
* @return 0 on success, -1 on failure.
int evbuffer_clear_flags(struct evbuffer *buf, ev_uint64_t flags);
Returns the total number of bytes stored in the evbuffer
@param buf pointer to the evbuffer
@return the number of bytes stored in the evbuffer
size_t evbuffer_get_length(const struct evbuffer *buf);
Returns the number of contiguous available bytes in the first buffer chain.
This is useful when processing data that might be split into multiple
chains, or that might all be in the first chain. Calls to
evbuffer_pullup() that cause reallocation and copying of data can thus be
@param buf pointer to the evbuffer
@return 0 if no data is available, otherwise the number of available bytes
in the first buffer chain.
size_t evbuffer_get_contiguous_space(const struct evbuffer *buf);
Expands the available space in an evbuffer.
Expands the available space in the evbuffer to at least datlen, so that
appending datlen additional bytes will not require any new allocations.
@param buf the evbuffer to be expanded
@param datlen the new minimum length requirement
@return 0 if successful, or -1 if an error occurred
int evbuffer_expand(struct evbuffer *buf, size_t datlen);
Reserves space in the last chain or chains of an evbuffer.
Makes space available in the last chain or chains of an evbuffer that can
be arbitrarily written to by a user. The space does not become
available for reading until it has been committed with
The space is made available as one or more extents, represented by
an initial pointer and a length. You can force the memory to be
available as only one extent. Allowing more extents, however, makes the
Multiple subsequent calls to this function will make the same space
available until evbuffer_commit_space() has been called.
It is an error to do anything that moves around the buffer's internal
memory structures before committing the space.
NOTE: The code currently does not ever use more than two extents.
This may change in future versions.
@param buf the evbuffer in which to reserve space.
@param size how much space to make available, at minimum. The
total length of the extents may be greater than the requested
@param vec an array of one or more evbuffer_iovec structures to
hold pointers to the reserved extents of memory.
@param n_vec The length of the vec array. Must be at least 1;
@return the number of provided extents, or -1 on error.
@see evbuffer_commit_space()
evbuffer_reserve_space(struct evbuffer *buf, ev_ssize_t size,
struct evbuffer_iovec *vec, int n_vec);
Commits previously reserved space.
Commits some of the space previously reserved with
evbuffer_reserve_space(). It then becomes available for reading.
This function may return an error if the pointer in the extents do
not match those returned from evbuffer_reserve_space, or if data
has been added to the buffer since the space was reserved.
If you want to commit less data than you got reserved space for,
modify the iov_len pointer of the appropriate extent to a smaller
value. Note that you may have received more space than you
requested if it was available!
@param buf the evbuffer in which to reserve space.
@param vec one or two extents returned by evbuffer_reserve_space.
@param n_vecs the number of extents.
@return 0 on success, -1 on error
@see evbuffer_reserve_space()
int evbuffer_commit_space(struct evbuffer *buf,
struct evbuffer_iovec *vec, int n_vecs);
Append data to the end of an evbuffer.
@param buf the evbuffer to be appended to
@param data pointer to the beginning of the data buffer
@param datlen the number of bytes to be copied from the data buffer
@return 0 on success, -1 on failure.
int evbuffer_add(struct evbuffer *buf, const void *data, size_t datlen);
Read data from an evbuffer and drain the bytes read.
If more bytes are requested than are available in the evbuffer, we
only extract as many bytes as were available.
@param buf the evbuffer to be read from
@param data the destination buffer to store the result
@param datlen the maximum size of the destination buffer
@return the number of bytes read, or -1 if we can't drain the buffer.
int evbuffer_remove(struct evbuffer *buf, void *data, size_t datlen);
Read data from an evbuffer, and leave the buffer unchanged.
If more bytes are requested than are available in the evbuffer, we
only extract as many bytes as were available.
@param buf the evbuffer to be read from
@param data_out the destination buffer to store the result
@param datlen the maximum size of the destination buffer
@return the number of bytes read, or -1 if we can't drain the buffer.
ev_ssize_t evbuffer_copyout(struct evbuffer *buf, void *data_out, size_t datlen);
Read data from the middle of an evbuffer, and leave the buffer unchanged.
If more bytes are requested than are available in the evbuffer, we
only extract as many bytes as were available.
@param buf the evbuffer to be read from
@param pos the position to start reading from
@param data_out the destination buffer to store the result
@param datlen the maximum size of the destination buffer
@return the number of bytes read, or -1 if we can't drain the buffer.
ev_ssize_t evbuffer_copyout_from(struct evbuffer *buf, const struct evbuffer_ptr *pos, void *data_out, size_t datlen);
Read data from an evbuffer into another evbuffer, draining
the bytes from the source buffer. This function avoids copy
operations to the extent possible.
If more bytes are requested than are available in src, the src
buffer is drained completely.
@param src the evbuffer to be read from
@param dst the destination evbuffer to store the result into
@param datlen the maximum numbers of bytes to transfer
@return the number of bytes read
int evbuffer_remove_buffer(struct evbuffer *src, struct evbuffer *dst,
/** Used to tell evbuffer_readln what kind of line-ending to look for.
enum evbuffer_eol_style {
/** Any sequence of CR and LF characters is acceptable as an
* Note that this style can produce ambiguous results: the
* sequence "CRLF" will be treated as a single EOL if it is
* all in the buffer at once, but if you first read a CR from
* the network and later read an LF from the network, it will
* be treated as two EOLs.
/** An EOL is an LF, optionally preceded by a CR. This style is
* most useful for implementing text-based internet protocols. */
/** An EOL is a CR followed by an LF. */
EVBUFFER_EOL_CRLF_STRICT,
/** An EOL is a NUL character (that is, a single byte with value 0) */
* Read a single line from an evbuffer.
* Reads a line terminated by an EOL as determined by the evbuffer_eol_style
* argument. Returns a newly allocated nul-terminated string; the caller must
* free the returned value. The EOL is not included in the returned string.
* @param buffer the evbuffer to read from
* @param n_read_out if non-NULL, points to a size_t that is set to the
* number of characters in the returned string. This is useful for
* strings that can contain NUL characters.
* @param eol_style the style of line-ending to use.
* @return pointer to a single line, or NULL if an error occurred
char *evbuffer_readln(struct evbuffer *buffer, size_t *n_read_out,
enum evbuffer_eol_style eol_style);
Move all data from one evbuffer into another evbuffer.
This is a destructive add. The data from one buffer moves into
the other buffer. However, no unnecessary memory copies occur.
@param outbuf the output buffer
@param inbuf the input buffer
@return 0 if successful, or -1 if an error occurred
@see evbuffer_remove_buffer()
int evbuffer_add_buffer(struct evbuffer *outbuf, struct evbuffer *inbuf);
Copy data from one evbuffer into another evbuffer.
This is a non-destructive add. The data from one buffer is copied
into the other buffer. However, no unnecessary memory copies occur.
Note that buffers already containing buffer references can't be added
@param outbuf the output buffer
@param inbuf the input buffer
@return 0 if successful, or -1 if an error occurred
int evbuffer_add_buffer_reference(struct evbuffer *outbuf,
A cleanup function for a piece of memory added to an evbuffer by
@see evbuffer_add_reference()
typedef void (*evbuffer_ref_cleanup_cb)(const void *data,
size_t datalen, void *extra);
Reference memory into an evbuffer without copying.
The memory needs to remain valid until all the added data has been
read. This function keeps just a reference to the memory without
actually incurring the overhead of a copy.
@param outbuf the output buffer
@param data the memory to reference
@param datlen how memory to reference
@param cleanupfn callback to be invoked when the memory is no longer
referenced by this evbuffer.
@param cleanupfn_arg optional argument to the cleanup callback
@return 0 if successful, or -1 if an error occurred
int evbuffer_add_reference(struct evbuffer *outbuf,
const void *data, size_t datlen,
evbuffer_ref_cleanup_cb cleanupfn, void *cleanupfn_arg);