michael@0: /* michael@0: * Copyright (c) 2000-2007 Niels Provos michael@0: * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * 1. Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * 2. Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in the michael@0: * documentation and/or other materials provided with the distribution. michael@0: * 3. The name of the author may not be used to endorse or promote products michael@0: * derived from this software without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR michael@0: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES michael@0: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. michael@0: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, michael@0: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT michael@0: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF michael@0: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: */ michael@0: #ifndef _EVBUFFER_INTERNAL_H_ michael@0: #define _EVBUFFER_INTERNAL_H_ michael@0: michael@0: #ifdef __cplusplus michael@0: extern "C" { michael@0: #endif michael@0: michael@0: #include "event2/event-config.h" michael@0: #include "event2/util.h" michael@0: #include "util-internal.h" michael@0: #include "defer-internal.h" michael@0: michael@0: /* Experimental cb flag: "never deferred." Implementation note: michael@0: * these callbacks may get an inaccurate view of n_del/n_added in their michael@0: * arguments. */ michael@0: #define EVBUFFER_CB_NODEFER 2 michael@0: michael@0: #ifdef WIN32 michael@0: #include michael@0: #endif michael@0: #include michael@0: michael@0: /* Minimum allocation for a chain. We define this so that we're burning no michael@0: * more than 5% of each allocation on overhead. It would be nice to lose even michael@0: * less space, though. */ michael@0: #if _EVENT_SIZEOF_VOID_P < 8 michael@0: #define MIN_BUFFER_SIZE 512 michael@0: #else michael@0: #define MIN_BUFFER_SIZE 1024 michael@0: #endif michael@0: michael@0: /** A single evbuffer callback for an evbuffer. This function will be invoked michael@0: * when bytes are added to or removed from the evbuffer. */ michael@0: struct evbuffer_cb_entry { michael@0: /** Structures to implement a doubly-linked queue of callbacks */ michael@0: TAILQ_ENTRY(evbuffer_cb_entry) next; michael@0: /** The callback function to invoke when this callback is called. michael@0: If EVBUFFER_CB_OBSOLETE is set in flags, the cb_obsolete field is michael@0: valid; otherwise, cb_func is valid. */ michael@0: union { michael@0: evbuffer_cb_func cb_func; michael@0: evbuffer_cb cb_obsolete; michael@0: } cb; michael@0: /** Argument to pass to cb. */ michael@0: void *cbarg; michael@0: /** Currently set flags on this callback. */ michael@0: ev_uint32_t flags; michael@0: }; michael@0: michael@0: struct bufferevent; michael@0: struct evbuffer_chain; michael@0: struct evbuffer { michael@0: /** The first chain in this buffer's linked list of chains. */ michael@0: struct evbuffer_chain *first; michael@0: /** The last chain in this buffer's linked list of chains. */ michael@0: struct evbuffer_chain *last; michael@0: michael@0: /** Pointer to the next pointer pointing at the 'last_with_data' chain. michael@0: * michael@0: * To unpack: michael@0: * michael@0: * The last_with_data chain is the last chain that has any data in it. michael@0: * If all chains in the buffer are empty, it is the first chain. michael@0: * If the buffer has no chains, it is NULL. michael@0: * michael@0: * The last_with_datap pointer points at _whatever 'next' pointer_ michael@0: * points at the last_with_datap chain. If the last_with_data chain michael@0: * is the first chain, or it is NULL, then the last_with_datap pointer michael@0: * is &buf->first. michael@0: */ michael@0: struct evbuffer_chain **last_with_datap; michael@0: michael@0: /** Total amount of bytes stored in all chains.*/ michael@0: size_t total_len; michael@0: michael@0: /** Number of bytes we have added to the buffer since we last tried to michael@0: * invoke callbacks. */ michael@0: size_t n_add_for_cb; michael@0: /** Number of bytes we have removed from the buffer since we last michael@0: * tried to invoke callbacks. */ michael@0: size_t n_del_for_cb; michael@0: michael@0: #ifndef _EVENT_DISABLE_THREAD_SUPPORT michael@0: /** A lock used to mediate access to this buffer. */ michael@0: void *lock; michael@0: #endif michael@0: /** True iff we should free the lock field when we free this michael@0: * evbuffer. */ michael@0: unsigned own_lock : 1; michael@0: /** True iff we should not allow changes to the front of the buffer michael@0: * (drains or prepends). */ michael@0: unsigned freeze_start : 1; michael@0: /** True iff we should not allow changes to the end of the buffer michael@0: * (appends) */ michael@0: unsigned freeze_end : 1; michael@0: /** True iff this evbuffer's callbacks are not invoked immediately michael@0: * upon a change in the buffer, but instead are deferred to be invoked michael@0: * from the event_base's loop. Useful for preventing enormous stack michael@0: * overflows when we have mutually recursive callbacks, and for michael@0: * serializing callbacks in a single thread. */ michael@0: unsigned deferred_cbs : 1; michael@0: #ifdef WIN32 michael@0: /** True iff this buffer is set up for overlapped IO. */ michael@0: unsigned is_overlapped : 1; michael@0: #endif michael@0: /** Zero or more EVBUFFER_FLAG_* bits */ michael@0: ev_uint32_t flags; michael@0: michael@0: /** Used to implement deferred callbacks. */ michael@0: struct deferred_cb_queue *cb_queue; michael@0: michael@0: /** A reference count on this evbuffer. When the reference count michael@0: * reaches 0, the buffer is destroyed. Manipulated with michael@0: * evbuffer_incref and evbuffer_decref_and_unlock and michael@0: * evbuffer_free. */ michael@0: int refcnt; michael@0: michael@0: /** A deferred_cb handle to make all of this buffer's callbacks michael@0: * invoked from the event loop. */ michael@0: struct deferred_cb deferred; michael@0: michael@0: /** A doubly-linked-list of callback functions */ michael@0: TAILQ_HEAD(evbuffer_cb_queue, evbuffer_cb_entry) callbacks; michael@0: michael@0: /** The parent bufferevent object this evbuffer belongs to. michael@0: * NULL if the evbuffer stands alone. */ michael@0: struct bufferevent *parent; michael@0: }; michael@0: michael@0: /** A single item in an evbuffer. */ michael@0: struct evbuffer_chain { michael@0: /** points to next buffer in the chain */ michael@0: struct evbuffer_chain *next; michael@0: michael@0: /** total allocation available in the buffer field. */ michael@0: size_t buffer_len; michael@0: michael@0: /** unused space at the beginning of buffer or an offset into a michael@0: * file for sendfile buffers. */ michael@0: ev_off_t misalign; michael@0: michael@0: /** Offset into buffer + misalign at which to start writing. michael@0: * In other words, the total number of bytes actually stored michael@0: * in buffer. */ michael@0: size_t off; michael@0: michael@0: /** Set if special handling is required for this chain */ michael@0: unsigned flags; michael@0: #define EVBUFFER_MMAP 0x0001 /**< memory in buffer is mmaped */ michael@0: #define EVBUFFER_SENDFILE 0x0002 /**< a chain used for sendfile */ michael@0: #define EVBUFFER_REFERENCE 0x0004 /**< a chain with a mem reference */ michael@0: #define EVBUFFER_IMMUTABLE 0x0008 /**< read-only chain */ michael@0: /** a chain that mustn't be reallocated or freed, or have its contents michael@0: * memmoved, until the chain is un-pinned. */ michael@0: #define EVBUFFER_MEM_PINNED_R 0x0010 michael@0: #define EVBUFFER_MEM_PINNED_W 0x0020 michael@0: #define EVBUFFER_MEM_PINNED_ANY (EVBUFFER_MEM_PINNED_R|EVBUFFER_MEM_PINNED_W) michael@0: /** a chain that should be freed, but can't be freed until it is michael@0: * un-pinned. */ michael@0: #define EVBUFFER_DANGLING 0x0040 michael@0: michael@0: /** Usually points to the read-write memory belonging to this michael@0: * buffer allocated as part of the evbuffer_chain allocation. michael@0: * For mmap, this can be a read-only buffer and michael@0: * EVBUFFER_IMMUTABLE will be set in flags. For sendfile, it michael@0: * may point to NULL. michael@0: */ michael@0: unsigned char *buffer; michael@0: }; michael@0: michael@0: /* this is currently used by both mmap and sendfile */ michael@0: /* TODO(niels): something strange needs to happen for Windows here, I am not michael@0: * sure what that is, but it needs to get looked into. michael@0: */ michael@0: struct evbuffer_chain_fd { michael@0: int fd; /**< the fd associated with this chain */ michael@0: }; michael@0: michael@0: /** callback for a reference buffer; lets us know what to do with it when michael@0: * we're done with it. */ michael@0: struct evbuffer_chain_reference { michael@0: evbuffer_ref_cleanup_cb cleanupfn; michael@0: void *extra; michael@0: }; michael@0: michael@0: #define EVBUFFER_CHAIN_SIZE sizeof(struct evbuffer_chain) michael@0: /** Return a pointer to extra data allocated along with an evbuffer. */ michael@0: #define EVBUFFER_CHAIN_EXTRA(t, c) (t *)((struct evbuffer_chain *)(c) + 1) michael@0: michael@0: /** Assert that we are holding the lock on an evbuffer */ michael@0: #define ASSERT_EVBUFFER_LOCKED(buffer) \ michael@0: EVLOCK_ASSERT_LOCKED((buffer)->lock) michael@0: michael@0: #define EVBUFFER_LOCK(buffer) \ michael@0: do { \ michael@0: EVLOCK_LOCK((buffer)->lock, 0); \ michael@0: } while (0) michael@0: #define EVBUFFER_UNLOCK(buffer) \ michael@0: do { \ michael@0: EVLOCK_UNLOCK((buffer)->lock, 0); \ michael@0: } while (0) michael@0: #define EVBUFFER_LOCK2(buffer1, buffer2) \ michael@0: do { \ michael@0: EVLOCK_LOCK2((buffer1)->lock, (buffer2)->lock, 0, 0); \ michael@0: } while (0) michael@0: #define EVBUFFER_UNLOCK2(buffer1, buffer2) \ michael@0: do { \ michael@0: EVLOCK_UNLOCK2((buffer1)->lock, (buffer2)->lock, 0, 0); \ michael@0: } while (0) michael@0: michael@0: /** Increase the reference count of buf by one. */ michael@0: void _evbuffer_incref(struct evbuffer *buf); michael@0: /** Increase the reference count of buf by one and acquire the lock. */ michael@0: void _evbuffer_incref_and_lock(struct evbuffer *buf); michael@0: /** Pin a single buffer chain using a given flag. A pinned chunk may not be michael@0: * moved or freed until it is unpinned. */ michael@0: void _evbuffer_chain_pin(struct evbuffer_chain *chain, unsigned flag); michael@0: /** Unpin a single buffer chain using a given flag. */ michael@0: void _evbuffer_chain_unpin(struct evbuffer_chain *chain, unsigned flag); michael@0: /** As evbuffer_free, but requires that we hold a lock on the buffer, and michael@0: * releases the lock before freeing it and the buffer. */ michael@0: void _evbuffer_decref_and_unlock(struct evbuffer *buffer); michael@0: michael@0: /** As evbuffer_expand, but does not guarantee that the newly allocated memory michael@0: * is contiguous. Instead, it may be split across two or more chunks. */ michael@0: int _evbuffer_expand_fast(struct evbuffer *, size_t, int); michael@0: michael@0: /** Helper: prepares for a readv/WSARecv call by expanding the buffer to michael@0: * hold enough memory to read 'howmuch' bytes in possibly noncontiguous memory. michael@0: * Sets up the one or two iovecs in 'vecs' to point to the free memory and its michael@0: * extent, and *chainp to point to the first chain that we'll try to read into. michael@0: * Returns the number of vecs used. michael@0: */ michael@0: int _evbuffer_read_setup_vecs(struct evbuffer *buf, ev_ssize_t howmuch, michael@0: struct evbuffer_iovec *vecs, int n_vecs, struct evbuffer_chain ***chainp, michael@0: int exact); michael@0: michael@0: /* Helper macro: copies an evbuffer_iovec in ei to a win32 WSABUF in i. */ michael@0: #define WSABUF_FROM_EVBUFFER_IOV(i,ei) do { \ michael@0: (i)->buf = (ei)->iov_base; \ michael@0: (i)->len = (unsigned long)(ei)->iov_len; \ michael@0: } while (0) michael@0: /* XXXX the cast above is safe for now, but not if we allow mmaps on win64. michael@0: * See note in buffer_iocp's launch_write function */ michael@0: michael@0: /** Set the parent bufferevent object for buf to bev */ michael@0: void evbuffer_set_parent(struct evbuffer *buf, struct bufferevent *bev); michael@0: michael@0: void evbuffer_invoke_callbacks(struct evbuffer *buf); michael@0: michael@0: #ifdef __cplusplus michael@0: } michael@0: #endif michael@0: michael@0: #endif /* _EVBUFFER_INTERNAL_H_ */