libevent中的缓冲区定义为evbuffer,主要在文件evbuffer-internal.h文件中,定义如下
struct evbuffer {
/** The first chain in this buffer's linked list of chains. */
struct evbuffer_chain *first; //缓冲区中的第一个chain
/** The last chain in this buffer's linked list of chains. */
struct evbuffer_chain *last; //缓冲区中的最后一个chain
/** Pointer to the next pointer pointing at the 'last_with_data' chain.
*
* To unpack:
*
* The last_with_data chain is the last chain that has any data in it.
* If all chains in the buffer are empty, it is the first chain.
* If the buffer has no chains, it is NULL.
*
* The last_with_datap pointer points at _whatever 'next' pointer_
* points at the last_with_datap chain. If the last_with_data chain
* is the first chain, or it is NULL, then the last_with_datap pointer
* is &buf->first.
*/
struct evbuffer_chain **last_with_datap; //指向链中包含最后数据的chain
/** Total amount of bytes stored in all chains.*/
size_t total_len; //包含的数据长度
/** Number of bytes we have added to the buffer since we last tried to
* invoke callbacks. */
size_t n_add_for_cb; //回调时要处理的字节数
/** Number of bytes we have removed from the buffer since we last
* tried to invoke callbacks. */
size_t n_del_for_cb; //回调时要删除的字节数
#ifndef _EVENT_DISABLE_THREAD_SUPPORT
/** A lock used to mediate access to this buffer. */
void *lock;
#endif
/** True iff we should free the lock field when we free this
* evbuffer. */
unsigned own_lock : 1;
/** True iff we should not allow changes to the front of the buffer
* (drains or prepends). */
unsigned freeze_start : 1; //在缓冲区头允许修改数据的标志
/** True iff we should not allow changes to the end of the buffer
* (appends) */
unsigned freeze_end : 1; //在缓冲区尾允许修改数据的标志
/** True iff this evbuffer's callbacks are not invoked immediately
* upon a change in the buffer, but instead are deferred to be invoked
* from the event_base's loop. Useful for preventing enormous stack
* overflows when we have mutually recursive callbacks, and for
* serializing callbacks in a single thread. */
unsigned deferred_cbs : 1; //延迟回调的标志
#ifdef WIN32
/** True iff this buffer is set up for overlapped IO. */
unsigned is_overlapped : 1; //是否设置iocp
#endif
/** Zero or more EVBUFFER_FLAG_* bits */
ev_uint32_t flags; //缓冲区标志
/** Used to implement deferred callbacks. */
struct deferred_cb_queue *cb_queue; //延迟回调队列
/** A reference count on this evbuffer. When the reference count
* reaches 0, the buffer is destroyed. Manipulated with
* evbuffer_incref and evbuffer_decref_and_unlock and
* evbuffer_free. */
int refcnt; //缓冲区引用次数
/** A deferred_cb handle to make all of this buffer's callbacks
* invoked from the event loop. */
struct deferred_cb deferred; //延迟回调
/** A doubly-linked-list of callback functions */
TAILQ_HEAD(evbuffer_cb_queue, evbuffer_cb_entry) callbacks; //缓冲区回调队列
/** The parent bufferevent object this evbuffer belongs to.
* NULL if the evbuffer stands alone. */
struct bufferevent *parent; //与缓冲区相关的bufferevent
};
缓冲区链的结构evbuffer_chain定义为:
struct evbuffer_chain {
/** points to next buffer in the chain */
struct evbuffer_chain *next; //指向下一个链
/** total allocation available in the buffer field. */
size_t buffer_len; // 链中缓冲区长度
/** unused space at the beginning of buffer or an offset into a
* file for sendfile buffers. */
ev_misalign_t misalign; //在缓冲区头中没有使用的个数
/** Offset into buffer + misalign at which to start writing.
* In other words, the total number of bytes actually stored
* in buffer. */
size_t off; //与缓冲区开始使用处的偏移
/** Set if special handling is required for this chain */
unsigned flags; //缓冲区标志
#define EVBUFFER_MMAP 0x0001 /**< memory in buffer is mmaped */
#define EVBUFFER_SENDFILE 0x0002 /**< a chain used for sendfile */
#define EVBUFFER_REFERENCE 0x0004 /**< a chain with a mem reference */
#define EVBUFFER_IMMUTABLE 0x0008 /**< read-only chain */
/** a chain that mustn't be reallocated or freed, or have its contents
* memmoved, until the chain is un-pinned. */
#define EVBUFFER_MEM_PINNED_R 0x0010
#define EVBUFFER_MEM_PINNED_W 0x0020
#define EVBUFFER_MEM_PINNED_ANY (EVBUFFER_MEM_PINNED_R|EVBUFFER_MEM_PINNED_W)
/** a chain that should be freed, but can't be freed until it is
* un-pinned. */
#define EVBUFFER_DANGLING 0x0040
/** Usually points to the read-write memory belonging to this
* buffer allocated as part of the evbuffer_chain allocation.
* For mmap, this can be a read-only buffer and
* EVBUFFER_IMMUTABLE will be set in flags. For sendfile, it
* may point to NULL.
*/
unsigned char *buffer; //存放数据的缓冲区
};
其逻辑结构图为