Path: blob/main/contrib/libevent/evbuffer-internal.h
39475 views
/*1* Copyright (c) 2000-2007 Niels Provos <[email protected]>2* Copyright (c) 2007-2012 Niels Provos and Nick Mathewson3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12* 3. The name of the author may not be used to endorse or promote products13* derived from this software without specific prior written permission.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR16* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES17* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.18* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,19* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT20* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,21* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY22* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT23* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF24* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.25*/26#ifndef EVBUFFER_INTERNAL_H_INCLUDED_27#define EVBUFFER_INTERNAL_H_INCLUDED_2829#ifdef __cplusplus30extern "C" {31#endif3233#include "event2/event-config.h"34#include "evconfig-private.h"35#include "event2/util.h"36#include "event2/event_struct.h"37#include "util-internal.h"38#include "defer-internal.h"3940/* Experimental cb flag: "never deferred." Implementation note:41* these callbacks may get an inaccurate view of n_del/n_added in their42* arguments. */43#define EVBUFFER_CB_NODEFER 24445#ifdef _WIN3246#include <winsock2.h>47#endif48#include <sys/queue.h>4950/* Minimum allocation for a chain. We define this so that we're burning no51* more than 5% of each allocation on overhead. It would be nice to lose even52* less space, though. */53#if EVENT__SIZEOF_VOID_P < 854#define MIN_BUFFER_SIZE 51255#else56#define MIN_BUFFER_SIZE 102457#endif5859/** A single evbuffer callback for an evbuffer. This function will be invoked60* when bytes are added to or removed from the evbuffer. */61struct evbuffer_cb_entry {62/** Structures to implement a doubly-linked queue of callbacks */63LIST_ENTRY(evbuffer_cb_entry) next;64/** The callback function to invoke when this callback is called.65If EVBUFFER_CB_OBSOLETE is set in flags, the cb_obsolete field is66valid; otherwise, cb_func is valid. */67union {68evbuffer_cb_func cb_func;69evbuffer_cb cb_obsolete;70} cb;71/** Argument to pass to cb. */72void *cbarg;73/** Currently set flags on this callback. */74ev_uint32_t flags;75};7677struct bufferevent;78struct evbuffer_chain;79struct evbuffer {80/** The first chain in this buffer's linked list of chains. */81struct evbuffer_chain *first;82/** The last chain in this buffer's linked list of chains. */83struct evbuffer_chain *last;8485/** Pointer to the next pointer pointing at the 'last_with_data' chain.86*87* To unpack:88*89* The last_with_data chain is the last chain that has any data in it.90* If all chains in the buffer are empty, it is the first chain.91* If the buffer has no chains, it is NULL.92*93* The last_with_datap pointer points at _whatever 'next' pointer_94* pointing at the last_with_data chain. If the last_with_data chain95* is the first chain, or it is NULL, then the last_with_datap pointer96* is &buf->first.97*/98struct evbuffer_chain **last_with_datap;99100/** Total amount of bytes stored in all chains.*/101size_t total_len;102103/** Number of bytes we have added to the buffer since we last tried to104* invoke callbacks. */105size_t n_add_for_cb;106/** Number of bytes we have removed from the buffer since we last107* tried to invoke callbacks. */108size_t n_del_for_cb;109110#ifndef EVENT__DISABLE_THREAD_SUPPORT111/** A lock used to mediate access to this buffer. */112void *lock;113#endif114/** True iff we should free the lock field when we free this115* evbuffer. */116unsigned own_lock : 1;117/** True iff we should not allow changes to the front of the buffer118* (drains or prepends). */119unsigned freeze_start : 1;120/** True iff we should not allow changes to the end of the buffer121* (appends) */122unsigned freeze_end : 1;123/** True iff this evbuffer's callbacks are not invoked immediately124* upon a change in the buffer, but instead are deferred to be invoked125* from the event_base's loop. Useful for preventing enormous stack126* overflows when we have mutually recursive callbacks, and for127* serializing callbacks in a single thread. */128unsigned deferred_cbs : 1;129#ifdef _WIN32130/** True iff this buffer is set up for overlapped IO. */131unsigned is_overlapped : 1;132#endif133/** Zero or more EVBUFFER_FLAG_* bits */134ev_uint32_t flags;135136/** Used to implement deferred callbacks. */137struct event_base *cb_queue;138139/** A reference count on this evbuffer. When the reference count140* reaches 0, the buffer is destroyed. Manipulated with141* evbuffer_incref and evbuffer_decref_and_unlock and142* evbuffer_free. */143int refcnt;144145/** A struct event_callback handle to make all of this buffer's callbacks146* invoked from the event loop. */147struct event_callback deferred;148149/** A doubly-linked-list of callback functions */150LIST_HEAD(evbuffer_cb_queue, evbuffer_cb_entry) callbacks;151152/** The parent bufferevent object this evbuffer belongs to.153* NULL if the evbuffer stands alone. */154struct bufferevent *parent;155};156157#if EVENT__SIZEOF_OFF_T < EVENT__SIZEOF_SIZE_T158typedef ev_ssize_t ev_misalign_t;159#define EVBUFFER_CHAIN_MAX ((size_t)EV_SSIZE_MAX)160#else161typedef ev_off_t ev_misalign_t;162#if EVENT__SIZEOF_OFF_T > EVENT__SIZEOF_SIZE_T163#define EVBUFFER_CHAIN_MAX EV_SIZE_MAX164#else165#define EVBUFFER_CHAIN_MAX ((size_t)EV_SSIZE_MAX)166#endif167#endif168169/** A single item in an evbuffer. */170struct evbuffer_chain {171/** points to next buffer in the chain */172struct evbuffer_chain *next;173174/** total allocation available in the buffer field. */175size_t buffer_len;176177/** unused space at the beginning of buffer or an offset into a178* file for sendfile buffers. */179ev_misalign_t misalign;180181/** Offset into buffer + misalign at which to start writing.182* In other words, the total number of bytes actually stored183* in buffer. */184size_t off;185186/** Set if special handling is required for this chain */187unsigned flags;188#define EVBUFFER_FILESEGMENT 0x0001 /**< A chain used for a file segment */189#define EVBUFFER_SENDFILE 0x0002 /**< a chain used with sendfile */190#define EVBUFFER_REFERENCE 0x0004 /**< a chain with a mem reference */191#define EVBUFFER_IMMUTABLE 0x0008 /**< read-only chain */192/** a chain that mustn't be reallocated or freed, or have its contents193* memmoved, until the chain is un-pinned. */194#define EVBUFFER_MEM_PINNED_R 0x0010195#define EVBUFFER_MEM_PINNED_W 0x0020196#define EVBUFFER_MEM_PINNED_ANY (EVBUFFER_MEM_PINNED_R|EVBUFFER_MEM_PINNED_W)197/** a chain that should be freed, but can't be freed until it is198* un-pinned. */199#define EVBUFFER_DANGLING 0x0040200/** a chain that is a referenced copy of another chain */201#define EVBUFFER_MULTICAST 0x0080202203/** number of references to this chain */204int refcnt;205206/** Usually points to the read-write memory belonging to this207* buffer allocated as part of the evbuffer_chain allocation.208* For mmap, this can be a read-only buffer and209* EVBUFFER_IMMUTABLE will be set in flags. For sendfile, it210* may point to NULL.211*/212unsigned char *buffer;213};214215/** callback for a reference chain; lets us know what to do with it when216* we're done with it. Lives at the end of an evbuffer_chain with the217* EVBUFFER_REFERENCE flag set */218struct evbuffer_chain_reference {219evbuffer_ref_cleanup_cb cleanupfn;220void *extra;221};222223/** File segment for a file-segment chain. Lives at the end of an224* evbuffer_chain with the EVBUFFER_FILESEGMENT flag set. */225struct evbuffer_chain_file_segment {226struct evbuffer_file_segment *segment;227#ifdef _WIN32228/** If we're using CreateFileMapping, this is the handle to the view. */229HANDLE view_handle;230#endif231};232233/* Declared in event2/buffer.h; defined here. */234struct evbuffer_file_segment {235void *lock; /**< lock prevent concurrent access to refcnt */236int refcnt; /**< Reference count for this file segment */237unsigned flags; /**< combination of EVBUF_FS_* flags */238239/** What kind of file segment is this? */240unsigned can_sendfile : 1;241unsigned is_mapping : 1;242243/** The fd that we read the data from. */244int fd;245/** If we're using mmap, this is the raw mapped memory. */246void *mapping;247#ifdef _WIN32248/** If we're using CreateFileMapping, this is the mapping */249HANDLE mapping_handle;250#endif251/** If we're using mmap or IO, this is the content of the file252* segment. */253char *contents;254/** Position of this segment within the file. */255ev_off_t file_offset;256/** If we're using mmap, this is the offset within 'mapping' where257* this data segment begins. */258ev_off_t mmap_offset;259/** The length of this segment. */260ev_off_t length;261/** Cleanup callback function */262evbuffer_file_segment_cleanup_cb cleanup_cb;263/** Argument to be pass to cleanup callback function */264void *cleanup_cb_arg;265};266267/** Information about the multicast parent of a chain. Lives at the268* end of an evbuffer_chain with the EVBUFFER_MULTICAST flag set. */269struct evbuffer_multicast_parent {270/** source buffer the multicast parent belongs to */271struct evbuffer *source;272/** multicast parent for this chain */273struct evbuffer_chain *parent;274};275276#define EVBUFFER_CHAIN_SIZE sizeof(struct evbuffer_chain)277/** Return a pointer to extra data allocated along with an evbuffer. */278#define EVBUFFER_CHAIN_EXTRA(t, c) (t *)((struct evbuffer_chain *)(c) + 1)279280/** Assert that we are holding the lock on an evbuffer */281#define ASSERT_EVBUFFER_LOCKED(buffer) \282EVLOCK_ASSERT_LOCKED((buffer)->lock)283284#define EVBUFFER_LOCK(buffer) \285do { \286EVLOCK_LOCK((buffer)->lock, 0); \287} while (0)288#define EVBUFFER_UNLOCK(buffer) \289do { \290EVLOCK_UNLOCK((buffer)->lock, 0); \291} while (0)292#define EVBUFFER_LOCK2(buffer1, buffer2) \293do { \294EVLOCK_LOCK2((buffer1)->lock, (buffer2)->lock, 0, 0); \295} while (0)296#define EVBUFFER_UNLOCK2(buffer1, buffer2) \297do { \298EVLOCK_UNLOCK2((buffer1)->lock, (buffer2)->lock, 0, 0); \299} while (0)300301/** Increase the reference count of buf by one. */302void evbuffer_incref_(struct evbuffer *buf);303/** Increase the reference count of buf by one and acquire the lock. */304void evbuffer_incref_and_lock_(struct evbuffer *buf);305/** Pin a single buffer chain using a given flag. A pinned chunk may not be306* moved or freed until it is unpinned. */307void evbuffer_chain_pin_(struct evbuffer_chain *chain, unsigned flag);308/** Unpin a single buffer chain using a given flag. */309void evbuffer_chain_unpin_(struct evbuffer_chain *chain, unsigned flag);310/** As evbuffer_free, but requires that we hold a lock on the buffer, and311* releases the lock before freeing it and the buffer. */312void evbuffer_decref_and_unlock_(struct evbuffer *buffer);313314/** As evbuffer_expand, but does not guarantee that the newly allocated memory315* is contiguous. Instead, it may be split across two or more chunks. */316int evbuffer_expand_fast_(struct evbuffer *, size_t, int);317318/** Helper: prepares for a readv/WSARecv call by expanding the buffer to319* hold enough memory to read 'howmuch' bytes in possibly noncontiguous memory.320* Sets up the one or two iovecs in 'vecs' to point to the free memory and its321* extent, and *chainp to point to the first chain that we'll try to read into.322* Returns the number of vecs used.323*/324int evbuffer_read_setup_vecs_(struct evbuffer *buf, ev_ssize_t howmuch,325struct evbuffer_iovec *vecs, int n_vecs, struct evbuffer_chain ***chainp,326int exact);327328/* Helper macro: copies an evbuffer_iovec in ei to a win32 WSABUF in i. */329#define WSABUF_FROM_EVBUFFER_IOV(i,ei) do { \330(i)->buf = (ei)->iov_base; \331(i)->len = (unsigned long)(ei)->iov_len; \332} while (0)333/* XXXX the cast above is safe for now, but not if we allow mmaps on win64.334* See note in buffer_iocp's launch_write function */335336/** Set the parent bufferevent object for buf to bev */337void evbuffer_set_parent_(struct evbuffer *buf, struct bufferevent *bev);338339void evbuffer_invoke_callbacks_(struct evbuffer *buf);340341342int evbuffer_get_callbacks_(struct evbuffer *buffer,343struct event_callback **cbs,344int max_cbs);345346#ifdef __cplusplus347}348#endif349350#endif /* EVBUFFER_INTERNAL_H_INCLUDED_ */351352353