libevent  2.2.1
Event notification library
Data Structures | Macros | Typedefs | Enumerations | Functions
buffer.h File Reference

Functions for buffering data for network sending or receiving. More...

#include <event2/visibility.h>
#include <event2/event-config.h>
#include <stdarg.h>
#include <event2/util.h>

Go to the source code of this file.

Data Structures

struct  evbuffer
 An evbuffer is an opaque data type for efficiently buffering data to be sent or received on the network. More...
 
struct  evbuffer_cb_info
 Structure passed to an evbuffer_cb_func evbuffer callback. More...
 
struct  evbuffer_iovec
 Describes a single extent of memory inside an evbuffer. More...
 
struct  evbuffer_ptr
 Pointer to a position within an evbuffer. More...
 

Macros

#define EVBUF_FS_CLOSE_ON_FREE   0x01
 Flag for creating evbuffer_file_segment: If this flag is set, then when the evbuffer_file_segment is freed and no longer in use by any evbuffer, the underlying fd is closed.
 
#define EVBUF_FS_DISABLE_LOCKING   0x08
 Flag for creating evbuffer_file_segment: Do not allocate a lock for this segment. More...
 
#define EVBUF_FS_DISABLE_MMAP   0x02
 Flag for creating evbuffer_file_segment: Disable memory-map based implementations.
 
#define EVBUF_FS_DISABLE_SENDFILE   0x04
 Flag for creating evbuffer_file_segment: Disable direct fd-to-fd implementations (sendfile). More...
 
#define EVBUFFER_CB_ENABLED   1
 If this flag is not set, then a callback is temporarily disabled, and should not be invoked. More...
 
#define EVBUFFER_FLAG_DRAINS_TO_FD   1
 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). More...
 

Typedefs

typedef void(* evbuffer_cb_func) (struct evbuffer *buffer, const struct evbuffer_cb_info *info, void *arg)
 Type definition for a callback that is invoked whenever data is added or removed from an evbuffer. More...
 
typedef void(* evbuffer_file_segment_cleanup_cb) (struct evbuffer_file_segment const *seg, int flags, void *arg)
 A cleanup function for a evbuffer_file_segment added to an evbuffer for reference.
 
typedef void(* evbuffer_ref_cleanup_cb) (const void *data, size_t datalen, void *extra)
 A cleanup function for a piece of memory added to an evbuffer by reference. More...
 

Enumerations

enum  evbuffer_eol_style {
  EVBUFFER_EOL_ANY, EVBUFFER_EOL_CRLF, EVBUFFER_EOL_CRLF_STRICT, EVBUFFER_EOL_LF,
  EVBUFFER_EOL_NUL
}
 Used to tell evbuffer_readln what kind of line-ending to look for. More...
 
enum  evbuffer_ptr_how { EVBUFFER_PTR_SET, EVBUFFER_PTR_ADD }
 Defines how to adjust an evbuffer_ptr by evbuffer_ptr_set() More...
 

Functions

EVENT2_EXPORT_SYMBOL int evbuffer_add (struct evbuffer *buf, const void *data, size_t datlen)
 Append data to the end of an evbuffer. More...
 
EVENT2_EXPORT_SYMBOL int evbuffer_add_buffer (struct evbuffer *outbuf, struct evbuffer *inbuf)
 Move all data from one evbuffer into another evbuffer. More...
 
EVENT2_EXPORT_SYMBOL int evbuffer_add_buffer_reference (struct evbuffer *outbuf, struct evbuffer *inbuf)
 Copy data from one evbuffer into another evbuffer. More...
 
EVENT2_EXPORT_SYMBOL struct evbuffer_cb_entry * evbuffer_add_cb (struct evbuffer *buffer, evbuffer_cb_func cb, void *cbarg)
 Add a new callback to an evbuffer. More...
 
EVENT2_EXPORT_SYMBOL int evbuffer_add_file (struct evbuffer *outbuf, int fd, ev_off_t offset, ev_off_t length)
 Copy data from a file into the evbuffer for writing to a socket. More...
 
EVENT2_EXPORT_SYMBOL int evbuffer_add_file_segment (struct evbuffer *buf, struct evbuffer_file_segment *seg, ev_off_t offset, ev_off_t length)
 Insert some or all of an evbuffer_file_segment at the end of an evbuffer. More...
 
EVENT2_EXPORT_SYMBOL size_t evbuffer_add_iovec (struct evbuffer *buffer, struct evbuffer_iovec *vec, int n_vec)
 Append data from 1 or more iovec's to an evbuffer. More...
 
EVENT2_EXPORT_SYMBOL int evbuffer_add_printf (struct evbuffer *buf, const char *fmt,...)
 Append a formatted string to the end of an evbuffer. More...
 
EVENT2_EXPORT_SYMBOL int evbuffer_add_reference (struct evbuffer *outbuf, const void *data, size_t datlen, evbuffer_ref_cleanup_cb cleanupfn, void *cleanupfn_arg)
 Reference memory into an evbuffer without copying. More...
 
EVENT2_EXPORT_SYMBOL int evbuffer_add_reference_with_offset (struct evbuffer *outbuf, const void *data, size_t offset, size_t datlen, evbuffer_ref_cleanup_cb cleanupfn, void *cleanupfn_arg)
 Reference memory into an evbuffer without copying. More...
 
EVENT2_EXPORT_SYMBOL int evbuffer_add_vprintf (struct evbuffer *buf, const char *fmt, va_list ap)
 Append a va_list formatted string to the end of an evbuffer. More...
 
EVENT2_EXPORT_SYMBOL int evbuffer_cb_clear_flags (struct evbuffer *buffer, struct evbuffer_cb_entry *cb, ev_uint32_t flags)
 Change the flags that are set for a callback on a buffer by removing some. More...
 
EVENT2_EXPORT_SYMBOL int evbuffer_cb_set_flags (struct evbuffer *buffer, struct evbuffer_cb_entry *cb, ev_uint32_t flags)
 Change the flags that are set for a callback on a buffer by adding more. More...
 
EVENT2_EXPORT_SYMBOL int evbuffer_clear_flags (struct evbuffer *buf, ev_uint64_t flags)
 Change the flags that are set for an evbuffer by removing some. More...
 
EVENT2_EXPORT_SYMBOL int evbuffer_commit_space (struct evbuffer *buf, struct evbuffer_iovec *vec, int n_vecs)
 Commits previously reserved space. More...
 
EVENT2_EXPORT_SYMBOL ev_ssize_t evbuffer_copyout (struct evbuffer *buf, void *data_out, size_t datlen)
 Read data from an evbuffer, and leave the buffer unchanged. More...
 
EVENT2_EXPORT_SYMBOL ev_ssize_t evbuffer_copyout_from (struct evbuffer *buf, const struct evbuffer_ptr *pos, void *data_out, size_t datlen)
 Read data from the middle of an evbuffer, and leave the buffer unchanged. More...
 
EVENT2_EXPORT_SYMBOL int evbuffer_defer_callbacks (struct evbuffer *buffer, struct event_base *base)
 Force all the callbacks on an evbuffer to be run, not immediately after the evbuffer is altered, but instead from inside the event loop. More...
 
EVENT2_EXPORT_SYMBOL int evbuffer_drain (struct evbuffer *buf, size_t len)
 Remove a specified number of bytes data from the beginning of an evbuffer. More...
 
EVENT2_EXPORT_SYMBOL int evbuffer_enable_locking (struct evbuffer *buf, void *lock)
 Enable locking on an evbuffer so that it can safely be used by multiple threads at the same time. More...
 
EVENT2_EXPORT_SYMBOL int evbuffer_expand (struct evbuffer *buf, size_t datlen)
 Expands the available space in an evbuffer. More...
 
EVENT2_EXPORT_SYMBOL void evbuffer_file_segment_add_cleanup_cb (struct evbuffer_file_segment *seg, evbuffer_file_segment_cleanup_cb cb, void *arg)
 Add cleanup callback and argument for the callback to an evbuffer_file_segment. More...
 
EVENT2_EXPORT_SYMBOL void evbuffer_file_segment_free (struct evbuffer_file_segment *seg)
 Free an evbuffer_file_segment. More...
 
EVENT2_EXPORT_SYMBOL struct evbuffer_file_segment * evbuffer_file_segment_new (int fd, ev_off_t offset, ev_off_t length, unsigned flags)
 Create and return a new evbuffer_file_segment for reading data from a file and sending it out via an evbuffer. More...
 
EVENT2_EXPORT_SYMBOL void evbuffer_free (struct evbuffer *buf)
 Deallocate storage for an evbuffer. More...
 
EVENT2_EXPORT_SYMBOL int evbuffer_freeze (struct evbuffer *buf, int at_front)
 Prevent calls that modify an evbuffer from succeeding. More...
 
EVENT2_EXPORT_SYMBOL size_t evbuffer_get_contiguous_space (const struct evbuffer *buf)
 Returns the number of contiguous available bytes in the first buffer chain. More...
 
EVENT2_EXPORT_SYMBOL size_t evbuffer_get_length (const struct evbuffer *buf)
 Returns the total number of bytes stored in the evbuffer. More...
 
EVENT2_EXPORT_SYMBOL size_t evbuffer_get_max_read (struct evbuffer *buf)
 Get maximum read buffer size. More...
 
EVENT2_EXPORT_SYMBOL void evbuffer_lock (struct evbuffer *buf)
 Acquire the lock on an evbuffer. More...
 
EVENT2_EXPORT_SYMBOL struct evbufferevbuffer_new (void)
 Allocate storage for a new evbuffer. More...
 
EVENT2_EXPORT_SYMBOL int evbuffer_peek (struct evbuffer *buffer, ev_ssize_t len, struct evbuffer_ptr *start_at, struct evbuffer_iovec *vec_out, int n_vec)
 Function to peek at data inside an evbuffer without removing it or copying it out. More...
 
EVENT2_EXPORT_SYMBOL int evbuffer_prepend (struct evbuffer *buf, const void *data, size_t size)
 Prepends data to the beginning of the evbuffer. More...
 
EVENT2_EXPORT_SYMBOL int evbuffer_prepend_buffer (struct evbuffer *dst, struct evbuffer *src)
 Prepends all data from the src evbuffer to the beginning of the dst evbuffer. More...
 
EVENT2_EXPORT_SYMBOL int evbuffer_ptr_set (struct evbuffer *buffer, struct evbuffer_ptr *ptr, size_t position, enum evbuffer_ptr_how how)
 Sets the search pointer in the buffer to position. More...
 
EVENT2_EXPORT_SYMBOL unsigned char * evbuffer_pullup (struct evbuffer *buf, ev_ssize_t size)
 Makes the data at the beginning of an evbuffer contiguous. More...
 
EVENT2_EXPORT_SYMBOL int evbuffer_read (struct evbuffer *buffer, evutil_socket_t fd, int howmuch)
 Read from a file descriptor and store the result in an evbuffer. More...
 
EVENT2_EXPORT_SYMBOL char * evbuffer_readln (struct evbuffer *buffer, size_t *n_read_out, enum evbuffer_eol_style eol_style)
 Read a single line from an evbuffer. More...
 
EVENT2_EXPORT_SYMBOL int evbuffer_remove (struct evbuffer *buf, void *data, size_t datlen)
 Read data from an evbuffer and drain the bytes read. More...
 
EVENT2_EXPORT_SYMBOL int evbuffer_remove_buffer (struct evbuffer *src, struct evbuffer *dst, size_t datlen)
 Read data from an evbuffer into another evbuffer, draining the bytes from the source buffer. More...
 
EVENT2_EXPORT_SYMBOL int evbuffer_remove_cb (struct evbuffer *buffer, evbuffer_cb_func cb, void *cbarg)
 Remove a callback from an evbuffer, given the function and argument used to add it. More...
 
EVENT2_EXPORT_SYMBOL int evbuffer_remove_cb_entry (struct evbuffer *buffer, struct evbuffer_cb_entry *ent)
 Remove a callback from an evbuffer, given a handle returned from evbuffer_add_cb. More...
 
EVENT2_EXPORT_SYMBOL int evbuffer_reserve_space (struct evbuffer *buf, ev_ssize_t size, struct evbuffer_iovec *vec, int n_vec)
 Reserves space in the last chain or chains of an evbuffer. More...
 
EVENT2_EXPORT_SYMBOL struct evbuffer_ptr evbuffer_search (struct evbuffer *buffer, const char *what, size_t len, const struct evbuffer_ptr *start)
 Search for a string within an evbuffer. More...
 
EVENT2_EXPORT_SYMBOL struct evbuffer_ptr evbuffer_search_eol (struct evbuffer *buffer, struct evbuffer_ptr *start, size_t *eol_len_out, enum evbuffer_eol_style eol_style)
 Search for an end-of-line string within an evbuffer. More...
 
EVENT2_EXPORT_SYMBOL struct evbuffer_ptr evbuffer_search_range (struct evbuffer *buffer, const char *what, size_t len, const struct evbuffer_ptr *start, const struct evbuffer_ptr *end)
 Search for a string within part of an evbuffer. More...
 
EVENT2_EXPORT_SYMBOL int evbuffer_set_flags (struct evbuffer *buf, ev_uint64_t flags)
 Change the flags that are set for an evbuffer by adding more. More...
 
EVENT2_EXPORT_SYMBOL int evbuffer_set_max_read (struct evbuffer *buf, size_t max)
 Set maximum read buffer size. More...
 
EVENT2_EXPORT_SYMBOL int evbuffer_unfreeze (struct evbuffer *buf, int at_front)
 Re-enable calls that modify an evbuffer. More...
 
EVENT2_EXPORT_SYMBOL void evbuffer_unlock (struct evbuffer *buf)
 Release the lock on an evbuffer. More...
 
EVENT2_EXPORT_SYMBOL int evbuffer_write (struct evbuffer *buffer, evutil_socket_t fd)
 Write the contents of an evbuffer to a file descriptor. More...
 
EVENT2_EXPORT_SYMBOL int evbuffer_write_atmost (struct evbuffer *buffer, evutil_socket_t fd, ev_ssize_t howmuch)
 Write some of the contents of an evbuffer to a file descriptor. More...
 

Detailed Description

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.

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 the chain.

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.

Macro Definition Documentation

◆ EVBUF_FS_DISABLE_LOCKING

#define EVBUF_FS_DISABLE_LOCKING   0x08

Flag for creating evbuffer_file_segment: Do not allocate a lock for this segment.

If this option is set, then neither the segment nor any evbuffer it is added to may ever be accessed from more than one thread at a time.

◆ EVBUF_FS_DISABLE_SENDFILE

#define EVBUF_FS_DISABLE_SENDFILE   0x04

Flag for creating evbuffer_file_segment: Disable direct fd-to-fd implementations (sendfile).

You might want to use this option if data needs to be taken from the evbuffer by any means other than writing it to the network: the sendfile backend is fast, but it only works for sending files directly to the network.

◆ EVBUFFER_CB_ENABLED

#define EVBUFFER_CB_ENABLED   1

If this flag is not set, then a callback is temporarily disabled, and should not be invoked.

See also
evbuffer_cb_set_flags(), evbuffer_cb_clear_flags()

◆ EVBUFFER_FLAG_DRAINS_TO_FD

#define EVBUFFER_FLAG_DRAINS_TO_FD   1

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 information.

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 output buffer.

Typedef Documentation

◆ evbuffer_cb_func

typedef void(* evbuffer_cb_func) (struct evbuffer *buffer, const struct evbuffer_cb_info *info, void *arg)

Type definition for a callback that is invoked whenever data is added or removed from an evbuffer.

An evbuffer may have one or more callbacks set at a time. The order in which they are executed is undefined.

A callback function may add more callbacks, or remove itself from the list of callbacks, or add or remove data from the buffer. It may not remove another callback from the list.

If a callback adds or removes data from the buffer or from another buffer, this can cause a recursive invocation of your callback or other callbacks. If you ask for an infinite loop, you might just get one: watch out!

Parameters
bufferthe buffer whose size has changed
infoa structure describing how the buffer changed.
arga pointer to user data

◆ evbuffer_ref_cleanup_cb

typedef void(* evbuffer_ref_cleanup_cb) (const void *data, size_t datalen, void *extra)

A cleanup function for a piece of memory added to an evbuffer by reference.

Parameters
databuffer
datalen- total buffer len (including @offset if any,
See also
evbuffer_add_reference_with_offset())
evbuffer_add_reference()
evbuffer_add_reference_with_offset()

Enumeration Type Documentation

◆ evbuffer_eol_style

Used to tell evbuffer_readln what kind of line-ending to look for.

Enumerator
EVBUFFER_EOL_ANY 

Any sequence of CR and LF characters is acceptable as an EOL.

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.

EVBUFFER_EOL_CRLF 

An EOL is an LF, optionally preceded by a CR.

This style is most useful for implementing text-based internet protocols.

EVBUFFER_EOL_CRLF_STRICT 

An EOL is a CR followed by an LF.

EVBUFFER_EOL_LF 

An EOL is a LF.

EVBUFFER_EOL_NUL 

An EOL is a NUL character (that is, a single byte with value 0)

◆ evbuffer_ptr_how

Defines how to adjust an evbuffer_ptr by evbuffer_ptr_set()

See also
evbuffer_ptr_set()
Enumerator
EVBUFFER_PTR_SET 

Sets the pointer to the position; can be called on with an uninitialized evbuffer_ptr.

EVBUFFER_PTR_ADD 

Advances the pointer by adding to the current position.

Function Documentation

◆ evbuffer_add()

EVENT2_EXPORT_SYMBOL int evbuffer_add ( struct evbuffer buf,
const void *  data,
size_t  datlen 
)

Append data to the end of an evbuffer.

Parameters
bufthe evbuffer to be appended to
datapointer to the beginning of the data buffer
datlenthe number of bytes to be copied from the data buffer
Returns
0 on success, -1 on failure.

◆ evbuffer_add_buffer()

EVENT2_EXPORT_SYMBOL int evbuffer_add_buffer ( struct evbuffer outbuf,
struct evbuffer inbuf 
)

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.

Parameters
outbufthe output buffer
inbufthe input buffer
Returns
0 if successful, or -1 if an error occurred
See also
evbuffer_remove_buffer()

◆ evbuffer_add_buffer_reference()

EVENT2_EXPORT_SYMBOL int evbuffer_add_buffer_reference ( 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 to other buffers.

Parameters
outbufthe output buffer
inbufthe input buffer
Returns
0 if successful, or -1 if an error occurred

◆ evbuffer_add_cb()

EVENT2_EXPORT_SYMBOL struct evbuffer_cb_entry* evbuffer_add_cb ( struct evbuffer buffer,
evbuffer_cb_func  cb,
void *  cbarg 
)

Add a new callback to an evbuffer.

Subsequent calls to evbuffer_add_cb() add new callbacks. To remove this callback, call evbuffer_remove_cb or evbuffer_remove_cb_entry.

Parameters
bufferthe evbuffer to be monitored
cbthe callback function to invoke when the evbuffer is modified, or NULL to remove all callbacks.
cbargan argument to be provided to the callback function
Returns
a handle to the callback on success, or NULL on failure.

◆ evbuffer_add_file()

EVENT2_EXPORT_SYMBOL int evbuffer_add_file ( struct evbuffer outbuf,
int  fd,
ev_off_t  offset,
ev_off_t  length 
)

Copy data from a file into the evbuffer for writing to a socket.

This function avoids unnecessary data copies between userland and kernel. If sendfile is available and the EVBUFFER_FLAG_DRAINS_TO_FD flag is set, it uses those functions. Otherwise, it tries to use mmap (or CreateFileMapping on Windows).

The function owns the resulting file descriptor and will close (even in case of error) it when finished transferring data.

The results of using evbuffer_remove() or evbuffer_pullup() on evbuffers whose data was added using this function are undefined.

For more fine-grained control, use evbuffer_add_file_segment.

Parameters
outbufthe output buffer
fdthe file descriptor
offsetthe offset from which to read data
lengthhow much data to read, or -1 to read as much as possible. (-1 requires that 'fd' support fstat.)
Returns
0 if successful, or -1 if an error occurred

◆ evbuffer_add_file_segment()

EVENT2_EXPORT_SYMBOL int evbuffer_add_file_segment ( struct evbuffer buf,
struct evbuffer_file_segment *  seg,
ev_off_t  offset,
ev_off_t  length 
)

Insert some or all of an evbuffer_file_segment at the end of an evbuffer.

Note that the offset and length parameters of this function have a different meaning from those provided to evbuffer_file_segment_new: When you create the segment, the offset is the offset within the file, and the length is the length of the segment, whereas when you add a segment to an evbuffer, the offset is within the segment and the length is the length of the _part of the segment you want to use.

In other words, if you have a 10 KiB file, and you create an evbuffer_file_segment for it with offset 20 and length 1000, it will refer to bytes 20..1019 inclusive. If you then pass this segment to evbuffer_add_file_segment and specify an offset of 20 and a length of 50, you will be adding bytes 40..99 inclusive.

Parameters
bufthe evbuffer to append to
segthe segment to add
offsetthe offset within the segment to start from
lengththe amount of data to add, or -1 to add it all.
Returns
0 on success, -1 on failure.

◆ evbuffer_add_iovec()

EVENT2_EXPORT_SYMBOL size_t evbuffer_add_iovec ( struct evbuffer buffer,
struct evbuffer_iovec vec,
int  n_vec 
)

Append data from 1 or more iovec's to an evbuffer.

Calculates the number of bytes needed for an iovec structure and guarantees all data will fit into a single chain. Can be used in lieu of functionality which calls evbuffer_add() constantly before being used to increase performance.

Parameters
bufferthe destination buffer
vecthe source iovec
n_vecthe number of iovec structures.
Returns
the number of bytes successfully written to the output buffer.

◆ evbuffer_add_printf()

EVENT2_EXPORT_SYMBOL int evbuffer_add_printf ( struct evbuffer buf,
const char *  fmt,
  ... 
)

Append a formatted string to the end of an evbuffer.

The string is formated as printf.

Parameters
bufthe evbuffer that will be appended to
fmta format string
...arguments that will be passed to printf(3)
Returns
The number of bytes added if successful, or -1 if an error occurred.
See also
evutil_printf(), evbuffer_add_vprintf()

◆ evbuffer_add_reference()

EVENT2_EXPORT_SYMBOL int evbuffer_add_reference ( struct evbuffer outbuf,
const void *  data,
size_t  datlen,
evbuffer_ref_cleanup_cb  cleanupfn,
void *  cleanupfn_arg 
)

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.

Parameters
outbufthe output buffer
datathe memory to reference
datlenhow memory to reference
cleanupfncallback to be invoked when the memory is no longer referenced by this evbuffer.
cleanupfn_argoptional argument to the cleanup callback
Returns
0 if successful, or -1 if an error occurred

◆ evbuffer_add_reference_with_offset()

EVENT2_EXPORT_SYMBOL int evbuffer_add_reference_with_offset ( struct evbuffer outbuf,
const void *  data,
size_t  offset,
size_t  datlen,
evbuffer_ref_cleanup_cb  cleanupfn,
void *  cleanupfn_arg 
)

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.

Parameters
outbufthe output buffer
datathe memory to reference
offsetoffset inside @data
datlenhow memory to reference (excluding @offset)
cleanupfncallback to be invoked when the memory is no longer referenced by this evbuffer.
cleanupfn_argoptional argument to the cleanup callback
Returns
0 if successful, or -1 if an error occurred

◆ evbuffer_add_vprintf()

EVENT2_EXPORT_SYMBOL int evbuffer_add_vprintf ( struct evbuffer buf,
const char *  fmt,
va_list  ap 
)

Append a va_list formatted string to the end of an evbuffer.

Parameters
bufthe evbuffer that will be appended to
fmta format string
apa varargs va_list argument array that will be passed to vprintf(3)
Returns
The number of bytes added if successful, or -1 if an error occurred.

◆ evbuffer_cb_clear_flags()

EVENT2_EXPORT_SYMBOL int evbuffer_cb_clear_flags ( struct evbuffer buffer,
struct evbuffer_cb_entry *  cb,
ev_uint32_t  flags 
)

Change the flags that are set for a callback on a buffer by removing some.

Parameters
bufferthe evbuffer that the callback is watching.
cbthe callback whose status we want to change.
flagsEVBUFFER_CB_ENABLED to disable the callback.
Returns
0 on success, -1 on failure.

◆ evbuffer_cb_set_flags()

EVENT2_EXPORT_SYMBOL int evbuffer_cb_set_flags ( struct evbuffer buffer,
struct evbuffer_cb_entry *  cb,
ev_uint32_t  flags 
)

Change the flags that are set for a callback on a buffer by adding more.

Parameters
bufferthe evbuffer that the callback is watching.
cbthe callback whose status we want to change.
flagsEVBUFFER_CB_ENABLED to re-enable the callback.
Returns
0 on success, -1 on failure.

◆ evbuffer_clear_flags()

EVENT2_EXPORT_SYMBOL int evbuffer_clear_flags ( struct evbuffer buf,
ev_uint64_t  flags 
)

Change the flags that are set for an evbuffer by removing some.

Parameters
bufthe evbuffer that the callback is watching.
flagsOne or more EVBUFFER_FLAG_* options
Returns
0 on success, -1 on failure.

◆ evbuffer_commit_space()

EVENT2_EXPORT_SYMBOL int evbuffer_commit_space ( struct evbuffer buf,
struct evbuffer_iovec vec,
int  n_vecs 
)

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!

Parameters
bufthe evbuffer in which to reserve space.
vecone or two extents returned by evbuffer_reserve_space.
n_vecsthe number of extents.
Returns
0 on success, -1 on error
See also
evbuffer_reserve_space()

◆ evbuffer_copyout()

EVENT2_EXPORT_SYMBOL ev_ssize_t evbuffer_copyout ( struct evbuffer buf,
void *  data_out,
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.

Parameters
bufthe evbuffer to be read from
data_outthe destination buffer to store the result
datlenthe maximum size of the destination buffer
Returns
the number of bytes read, or -1 if we can't drain the buffer.

◆ evbuffer_copyout_from()

EVENT2_EXPORT_SYMBOL ev_ssize_t evbuffer_copyout_from ( struct evbuffer buf,
const struct evbuffer_ptr pos,
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.

Parameters
bufthe evbuffer to be read from
posthe position to start reading from
data_outthe destination buffer to store the result
datlenthe maximum size of the destination buffer
Returns
the number of bytes read, or -1 if we can't drain the buffer.

◆ evbuffer_defer_callbacks()

EVENT2_EXPORT_SYMBOL int evbuffer_defer_callbacks ( struct evbuffer buffer,
struct event_base base 
)

Force all the callbacks on an evbuffer to be run, not immediately after the evbuffer is altered, but instead from inside the event loop.

This can be used to serialize all the callbacks to a single thread of execution.

◆ evbuffer_drain()

EVENT2_EXPORT_SYMBOL int evbuffer_drain ( struct evbuffer buf,
size_t  len 
)

Remove a specified number of bytes data from the beginning of an evbuffer.

Parameters
bufthe evbuffer to be drained
lenthe number of bytes to drain from the beginning of the buffer
Returns
0 on success, -1 on failure.

◆ evbuffer_enable_locking()

EVENT2_EXPORT_SYMBOL int evbuffer_enable_locking ( struct evbuffer buf,
void *  lock 
)

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 accordingly!

Parameters
bufAn evbuffer to make lockable.
lockA lock object, or NULL if we should allocate our own.
Returns
0 on success, -1 on failure.

◆ evbuffer_expand()

EVENT2_EXPORT_SYMBOL int evbuffer_expand ( struct evbuffer buf,
size_t  datlen 
)

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.

Parameters
bufthe evbuffer to be expanded
datlenthe new minimum length requirement
Returns
0 if successful, or -1 if an error occurred

◆ evbuffer_file_segment_add_cleanup_cb()

EVENT2_EXPORT_SYMBOL void evbuffer_file_segment_add_cleanup_cb ( struct evbuffer_file_segment *  seg,
evbuffer_file_segment_cleanup_cb  cb,
void *  arg 
)

Add cleanup callback and argument for the callback to an evbuffer_file_segment.

The cleanup callback will be invoked when no more references to the evbuffer_file_segment exist.

◆ evbuffer_file_segment_free()

EVENT2_EXPORT_SYMBOL void evbuffer_file_segment_free ( struct evbuffer_file_segment *  seg)

Free an evbuffer_file_segment.

It is safe to call this function even if the segment has been added to one or more evbuffers. The evbuffer_file_segment will not be freed until no more references to it exist.

◆ evbuffer_file_segment_new()

EVENT2_EXPORT_SYMBOL struct evbuffer_file_segment* evbuffer_file_segment_new ( int  fd,
ev_off_t  offset,
ev_off_t  length,
unsigned  flags 
)

Create and return a new evbuffer_file_segment for reading data from a file and sending it out via an evbuffer.

This function avoids unnecessary data copies between userland and kernel. Where available, it uses sendfile.

The file descriptor must not be closed so long as any evbuffer is using this segment.

The results of using evbuffer_remove() or evbuffer_pullup() or any other function that reads bytes from an evbuffer on any evbuffer containing the newly returned segment are undefined, unless you pass the EVBUF_FS_DISABLE_SENDFILE flag to this function.

Parameters
fdan open file to read from.
offsetan index within the file at which to start reading
lengthhow much data to read, or -1 to read as much as possible. (-1 requires that 'fd' support fstat.)
flagsany number of the EVBUF_FS_* flags
Returns
a new evbuffer_file_segment, or NULL on failure.

◆ evbuffer_free()

EVENT2_EXPORT_SYMBOL void evbuffer_free ( struct evbuffer buf)

Deallocate storage for an evbuffer.

Parameters
bufpointer to the evbuffer to be freed

◆ evbuffer_freeze()

EVENT2_EXPORT_SYMBOL int evbuffer_freeze ( struct evbuffer buf,
int  at_front 
)

Prevent calls that modify an evbuffer from succeeding.

A buffer may frozen at the front, at the back, or at both the front and the back.

If the front of a buffer is frozen, operations that drain data from the front of the buffer, or that prepend data to the buffer, will fail until it is unfrozen. If the back a buffer is frozen, operations that append data from the buffer will fail until it is unfrozen.

Parameters
bufThe buffer to freeze
at_frontIf true, we freeze the front of the buffer. If false, we freeze the back.
Returns
0 on success, -1 on failure.

◆ evbuffer_get_contiguous_space()

EVENT2_EXPORT_SYMBOL size_t evbuffer_get_contiguous_space ( 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 avoided.

Parameters
bufpointer to the evbuffer
Returns
0 if no data is available, otherwise the number of available bytes in the first buffer chain.

◆ evbuffer_get_length()

EVENT2_EXPORT_SYMBOL size_t evbuffer_get_length ( const struct evbuffer buf)

Returns the total number of bytes stored in the evbuffer.

Parameters
bufpointer to the evbuffer
Returns
the number of bytes stored in the evbuffer

◆ evbuffer_get_max_read()

EVENT2_EXPORT_SYMBOL size_t evbuffer_get_max_read ( struct evbuffer buf)

Get maximum read buffer size.

Parameters
bufpointer to the evbuffer
Returns
current maximum buffer read

◆ evbuffer_lock()

EVENT2_EXPORT_SYMBOL void evbuffer_lock ( struct evbuffer buf)

Acquire the lock on an evbuffer.

Has no effect if locking was not enabled with evbuffer_enable_locking.

◆ evbuffer_new()

EVENT2_EXPORT_SYMBOL struct evbuffer* evbuffer_new ( void  )

Allocate storage for a new evbuffer.

Returns
a pointer to a newly allocated evbuffer struct, or NULL if an error occurred

◆ evbuffer_peek()

EVENT2_EXPORT_SYMBOL int evbuffer_peek ( struct evbuffer buffer,
ev_ssize_t  len,
struct evbuffer_ptr start_at,
struct evbuffer_iovec vec_out,
int  n_vec 
)

Function to peek at data inside an evbuffer without removing it or copying it out.

Pointers to the data are returned by filling the 'vec_out' array with pointers to one or more extents of data inside the buffer.

The total data in the extents that you get back may be more than you requested (if there is more data last extent than you asked for), or less (if you do not provide enough evbuffer_iovecs, or if the buffer does not have as much data as you asked to see).

Parameters
bufferthe evbuffer to peek into,
lenthe number of bytes to try to peek. If len is negative, we will try to fill as much of vec_out as we can. If len is negative and vec_out is not provided, we return the number of evbuffer_iovecs that would be needed to get all the data in the buffer.
start_atan evbuffer_ptr indicating the point at which we should start looking for data. NULL means, "At the start of the buffer."
vec_outan array of evbuffer_iovec
n_vecthe length of vec_out. If 0, we only count how many extents would be necessary to point to the requested amount of data.
Returns
The number of extents needed. This may be less than n_vec if we didn't need all the evbuffer_iovecs we were given, or more than n_vec if we would need more to return all the data that was requested.

◆ evbuffer_prepend()

EVENT2_EXPORT_SYMBOL int evbuffer_prepend ( struct evbuffer buf,
const void *  data,
size_t  size 
)

Prepends data to the beginning of the evbuffer.

Parameters
bufthe evbuffer to which to prepend data
dataa pointer to the memory to prepend
sizethe number of bytes to prepend
Returns
0 if successful, or -1 otherwise

◆ evbuffer_prepend_buffer()

EVENT2_EXPORT_SYMBOL int evbuffer_prepend_buffer ( struct evbuffer dst,
struct evbuffer src 
)

Prepends all data from the src evbuffer to the beginning of the dst evbuffer.

Parameters
dstthe evbuffer to which to prepend data
srcthe evbuffer to prepend; it will be emptied as a result
Returns
0 if successful, or -1 otherwise

◆ evbuffer_ptr_set()

EVENT2_EXPORT_SYMBOL int evbuffer_ptr_set ( struct evbuffer buffer,
struct evbuffer_ptr ptr,
size_t  position,
enum evbuffer_ptr_how  how 
)

Sets the search pointer in the buffer to position.

There are two ways to use this function: you can call evbuffer_ptr_set(buf, &pos, N, EVBUFFER_PTR_SET) to move 'pos' to a position 'N' bytes after the start of the buffer, or evbuffer_ptr_set(buf, &pos, N, EVBUFFER_PTR_ADD) to move 'pos' forward by 'N' bytes.

If evbuffer_ptr is not initialized, this function can only be called with EVBUFFER_PTR_SET.

An evbuffer_ptr can represent any position from the start of the buffer to a position immediately after the end of the buffer.

Parameters
bufferthe evbuffer to be search
ptra pointer to a struct evbuffer_ptr
positionthe position at which to start the next search
howdetermines how the pointer should be manipulated.
Returns
0 on success or -1 otherwise

◆ evbuffer_pullup()

EVENT2_EXPORT_SYMBOL unsigned char* evbuffer_pullup ( struct evbuffer buf,
ev_ssize_t  size 
)

Makes the data at the beginning of an evbuffer contiguous.

Parameters
bufthe evbuffer to make contiguous
sizethe number of bytes to make contiguous, or -1 to make the entire buffer contiguous.
Returns
a pointer to the contiguous memory array, or NULL if param size requested more data than is present in the buffer.

◆ evbuffer_read()

EVENT2_EXPORT_SYMBOL int evbuffer_read ( struct evbuffer buffer,
evutil_socket_t  fd,
int  howmuch 
)

Read from a file descriptor and store the result in an evbuffer.

Parameters
bufferthe evbuffer to store the result
fdthe file descriptor to read from
howmuchthe number of bytes to be read. If the given number is negative or out of maximum bytes per one read, as many bytes as we can will be read.
Returns
the number of bytes read, or -1 if an error occurred
See also
evbuffer_write()

◆ evbuffer_readln()

EVENT2_EXPORT_SYMBOL char* evbuffer_readln ( struct evbuffer buffer,
size_t *  n_read_out,
enum evbuffer_eol_style  eol_style 
)

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.

Parameters
bufferthe evbuffer to read from
n_read_outif 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.
eol_stylethe style of line-ending to use.
Returns
pointer to a single line, or NULL if an error occurred

◆ evbuffer_remove()

EVENT2_EXPORT_SYMBOL int evbuffer_remove ( struct evbuffer buf,
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.

Parameters
bufthe evbuffer to be read from
datathe destination buffer to store the result
datlenthe maximum size of the destination buffer
Returns
the number of bytes read, or -1 if we can't drain the buffer.

◆ evbuffer_remove_buffer()

EVENT2_EXPORT_SYMBOL int evbuffer_remove_buffer ( struct evbuffer src,
struct evbuffer dst,
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.

Parameters
srcthe evbuffer to be read from
dstthe destination evbuffer to store the result into
datlenthe maximum numbers of bytes to transfer
Returns
the number of bytes read

◆ evbuffer_remove_cb()

EVENT2_EXPORT_SYMBOL int evbuffer_remove_cb ( struct evbuffer buffer,
evbuffer_cb_func  cb,
void *  cbarg 
)

Remove a callback from an evbuffer, given the function and argument used to add it.

Returns
0 if a callback was removed, or -1 if no matching callback was found.

◆ evbuffer_remove_cb_entry()

EVENT2_EXPORT_SYMBOL int evbuffer_remove_cb_entry ( struct evbuffer buffer,
struct evbuffer_cb_entry *  ent 
)

Remove a callback from an evbuffer, given a handle returned from evbuffer_add_cb.

Calling this function invalidates the handle.

Returns
0 if a callback was removed, or -1 if no matching callback was found.

◆ evbuffer_reserve_space()

EVENT2_EXPORT_SYMBOL int evbuffer_reserve_space ( struct evbuffer buf,
ev_ssize_t  size,
struct evbuffer_iovec vec,
int  n_vec 
)

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 evbuffer_commit_space().

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 function more efficient.

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.

Parameters
bufthe evbuffer in which to reserve space.
sizehow much space to make available, at minimum. The total length of the extents may be greater than the requested length.
vecan array of one or more evbuffer_iovec structures to hold pointers to the reserved extents of memory.
n_vecThe length of the vec array. Must be at least 1; 2 is more efficient.
Returns
the number of provided extents, or -1 on error.
See also
evbuffer_commit_space()

◆ evbuffer_search()

EVENT2_EXPORT_SYMBOL struct evbuffer_ptr evbuffer_search ( struct evbuffer buffer,
const char *  what,
size_t  len,
const struct evbuffer_ptr start 
)

Search for a string within an evbuffer.

Parameters
bufferthe evbuffer to be searched
whatthe string to be searched for
lenthe length of the search string
startNULL or a pointer to a valid struct evbuffer_ptr.
Returns
a struct evbuffer_ptr whose 'pos' field has the offset of the first occurrence of the string in the buffer after 'start'. The 'pos' field of the result is -1 if the string was not found.

◆ evbuffer_search_eol()

EVENT2_EXPORT_SYMBOL struct evbuffer_ptr evbuffer_search_eol ( struct evbuffer buffer,
struct evbuffer_ptr start,
size_t *  eol_len_out,
enum evbuffer_eol_style  eol_style 
)

Search for an end-of-line string within an evbuffer.

Parameters
bufferthe evbuffer to be searched
startNULL or a pointer to a valid struct evbuffer_ptr to start searching at.
eol_len_outIf non-NULL, the pointed-to value will be set to the length of the end-of-line string.
eol_styleThe kind of EOL to look for; see evbuffer_readln() for more information
Returns
a struct evbuffer_ptr whose 'pos' field has the offset of the first occurrence EOL in the buffer after 'start'. The 'pos' field of the result is -1 if the string was not found.

◆ evbuffer_search_range()

EVENT2_EXPORT_SYMBOL struct evbuffer_ptr evbuffer_search_range ( struct evbuffer buffer,
const char *  what,
size_t  len,
const struct evbuffer_ptr start,
const struct evbuffer_ptr end 
)

Search for a string within part of an evbuffer.

Parameters
bufferthe evbuffer to be searched
whatthe string to be searched for
lenthe length of the search string
startNULL or a pointer to a valid struct evbuffer_ptr that indicates where we should start searching.
endNULL or a pointer to a valid struct evbuffer_ptr that indicates where we should stop searching.
Returns
a struct evbuffer_ptr whose 'pos' field has the offset of the first occurrence of the string in the buffer after 'start'. The 'pos' field of the result is -1 if the string was not found.

◆ evbuffer_set_flags()

EVENT2_EXPORT_SYMBOL int evbuffer_set_flags ( struct evbuffer buf,
ev_uint64_t  flags 
)

Change the flags that are set for an evbuffer by adding more.

Parameters
bufthe evbuffer that the callback is watching.
flagsOne or more EVBUFFER_FLAG_* options
Returns
0 on success, -1 on failure.

◆ evbuffer_set_max_read()

EVENT2_EXPORT_SYMBOL int evbuffer_set_max_read ( struct evbuffer buf,
size_t  max 
)

Set maximum read buffer size.

Default is 4096 and it works fine most of time, so before increasing the default check carefully, since this has some negative effects (like memory fragmentation and unfair resource distribution, i.e. some events will make less progress than others).

Parameters
bufpointer to the evbuffer
maxbuffer size
Returns
0 on success, -1 on failure (if max > INT_MAX).

◆ evbuffer_unfreeze()

EVENT2_EXPORT_SYMBOL int evbuffer_unfreeze ( struct evbuffer buf,
int  at_front 
)

Re-enable calls that modify an evbuffer.

Parameters
bufThe buffer to un-freeze
at_frontIf true, we unfreeze the front of the buffer. If false, we unfreeze the back.
Returns
0 on success, -1 on failure.

◆ evbuffer_unlock()

EVENT2_EXPORT_SYMBOL void evbuffer_unlock ( struct evbuffer buf)

Release the lock on an evbuffer.

Has no effect if locking was not enabled with evbuffer_enable_locking.

◆ evbuffer_write()

EVENT2_EXPORT_SYMBOL int evbuffer_write ( struct evbuffer buffer,
evutil_socket_t  fd 
)

Write the contents of an evbuffer to a file descriptor.

The evbuffer will be drained after the bytes have been successfully written.

Parameters
bufferthe evbuffer to be written and drained
fdthe file descriptor to be written to
Returns
the number of bytes written, or -1 if an error occurred
See also
evbuffer_read()

◆ evbuffer_write_atmost()

EVENT2_EXPORT_SYMBOL int evbuffer_write_atmost ( struct evbuffer buffer,
evutil_socket_t  fd,
ev_ssize_t  howmuch 
)

Write some of the contents of an evbuffer to a file descriptor.

The evbuffer will be drained after the bytes have been successfully written.

Parameters
bufferthe evbuffer to be written and drained
fdthe file descriptor to be written to
howmuchthe largest allowable number of bytes to write, or -1 to write as many bytes as we can.
Returns
the number of bytes written, or -1 if an error occurred
See also
evbuffer_read()