1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/ipc/chromium/src/third_party/libevent/evbuffer-internal.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,281 @@ 1.4 +/* 1.5 + * Copyright (c) 2000-2007 Niels Provos <provos@citi.umich.edu> 1.6 + * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson 1.7 + * 1.8 + * Redistribution and use in source and binary forms, with or without 1.9 + * modification, are permitted provided that the following conditions 1.10 + * are met: 1.11 + * 1. Redistributions of source code must retain the above copyright 1.12 + * notice, this list of conditions and the following disclaimer. 1.13 + * 2. Redistributions in binary form must reproduce the above copyright 1.14 + * notice, this list of conditions and the following disclaimer in the 1.15 + * documentation and/or other materials provided with the distribution. 1.16 + * 3. The name of the author may not be used to endorse or promote products 1.17 + * derived from this software without specific prior written permission. 1.18 + * 1.19 + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1.20 + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1.21 + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 1.22 + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 1.23 + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 1.24 + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.25 + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.26 + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.27 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 1.28 + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.29 + */ 1.30 +#ifndef _EVBUFFER_INTERNAL_H_ 1.31 +#define _EVBUFFER_INTERNAL_H_ 1.32 + 1.33 +#ifdef __cplusplus 1.34 +extern "C" { 1.35 +#endif 1.36 + 1.37 +#include "event2/event-config.h" 1.38 +#include "event2/util.h" 1.39 +#include "util-internal.h" 1.40 +#include "defer-internal.h" 1.41 + 1.42 +/* Experimental cb flag: "never deferred." Implementation note: 1.43 + * these callbacks may get an inaccurate view of n_del/n_added in their 1.44 + * arguments. */ 1.45 +#define EVBUFFER_CB_NODEFER 2 1.46 + 1.47 +#ifdef WIN32 1.48 +#include <winsock2.h> 1.49 +#endif 1.50 +#include <sys/queue.h> 1.51 + 1.52 +/* Minimum allocation for a chain. We define this so that we're burning no 1.53 + * more than 5% of each allocation on overhead. It would be nice to lose even 1.54 + * less space, though. */ 1.55 +#if _EVENT_SIZEOF_VOID_P < 8 1.56 +#define MIN_BUFFER_SIZE 512 1.57 +#else 1.58 +#define MIN_BUFFER_SIZE 1024 1.59 +#endif 1.60 + 1.61 +/** A single evbuffer callback for an evbuffer. This function will be invoked 1.62 + * when bytes are added to or removed from the evbuffer. */ 1.63 +struct evbuffer_cb_entry { 1.64 + /** Structures to implement a doubly-linked queue of callbacks */ 1.65 + TAILQ_ENTRY(evbuffer_cb_entry) next; 1.66 + /** The callback function to invoke when this callback is called. 1.67 + If EVBUFFER_CB_OBSOLETE is set in flags, the cb_obsolete field is 1.68 + valid; otherwise, cb_func is valid. */ 1.69 + union { 1.70 + evbuffer_cb_func cb_func; 1.71 + evbuffer_cb cb_obsolete; 1.72 + } cb; 1.73 + /** Argument to pass to cb. */ 1.74 + void *cbarg; 1.75 + /** Currently set flags on this callback. */ 1.76 + ev_uint32_t flags; 1.77 +}; 1.78 + 1.79 +struct bufferevent; 1.80 +struct evbuffer_chain; 1.81 +struct evbuffer { 1.82 + /** The first chain in this buffer's linked list of chains. */ 1.83 + struct evbuffer_chain *first; 1.84 + /** The last chain in this buffer's linked list of chains. */ 1.85 + struct evbuffer_chain *last; 1.86 + 1.87 + /** Pointer to the next pointer pointing at the 'last_with_data' chain. 1.88 + * 1.89 + * To unpack: 1.90 + * 1.91 + * The last_with_data chain is the last chain that has any data in it. 1.92 + * If all chains in the buffer are empty, it is the first chain. 1.93 + * If the buffer has no chains, it is NULL. 1.94 + * 1.95 + * The last_with_datap pointer points at _whatever 'next' pointer_ 1.96 + * points at the last_with_datap chain. If the last_with_data chain 1.97 + * is the first chain, or it is NULL, then the last_with_datap pointer 1.98 + * is &buf->first. 1.99 + */ 1.100 + struct evbuffer_chain **last_with_datap; 1.101 + 1.102 + /** Total amount of bytes stored in all chains.*/ 1.103 + size_t total_len; 1.104 + 1.105 + /** Number of bytes we have added to the buffer since we last tried to 1.106 + * invoke callbacks. */ 1.107 + size_t n_add_for_cb; 1.108 + /** Number of bytes we have removed from the buffer since we last 1.109 + * tried to invoke callbacks. */ 1.110 + size_t n_del_for_cb; 1.111 + 1.112 +#ifndef _EVENT_DISABLE_THREAD_SUPPORT 1.113 + /** A lock used to mediate access to this buffer. */ 1.114 + void *lock; 1.115 +#endif 1.116 + /** True iff we should free the lock field when we free this 1.117 + * evbuffer. */ 1.118 + unsigned own_lock : 1; 1.119 + /** True iff we should not allow changes to the front of the buffer 1.120 + * (drains or prepends). */ 1.121 + unsigned freeze_start : 1; 1.122 + /** True iff we should not allow changes to the end of the buffer 1.123 + * (appends) */ 1.124 + unsigned freeze_end : 1; 1.125 + /** True iff this evbuffer's callbacks are not invoked immediately 1.126 + * upon a change in the buffer, but instead are deferred to be invoked 1.127 + * from the event_base's loop. Useful for preventing enormous stack 1.128 + * overflows when we have mutually recursive callbacks, and for 1.129 + * serializing callbacks in a single thread. */ 1.130 + unsigned deferred_cbs : 1; 1.131 +#ifdef WIN32 1.132 + /** True iff this buffer is set up for overlapped IO. */ 1.133 + unsigned is_overlapped : 1; 1.134 +#endif 1.135 + /** Zero or more EVBUFFER_FLAG_* bits */ 1.136 + ev_uint32_t flags; 1.137 + 1.138 + /** Used to implement deferred callbacks. */ 1.139 + struct deferred_cb_queue *cb_queue; 1.140 + 1.141 + /** A reference count on this evbuffer. When the reference count 1.142 + * reaches 0, the buffer is destroyed. Manipulated with 1.143 + * evbuffer_incref and evbuffer_decref_and_unlock and 1.144 + * evbuffer_free. */ 1.145 + int refcnt; 1.146 + 1.147 + /** A deferred_cb handle to make all of this buffer's callbacks 1.148 + * invoked from the event loop. */ 1.149 + struct deferred_cb deferred; 1.150 + 1.151 + /** A doubly-linked-list of callback functions */ 1.152 + TAILQ_HEAD(evbuffer_cb_queue, evbuffer_cb_entry) callbacks; 1.153 + 1.154 + /** The parent bufferevent object this evbuffer belongs to. 1.155 + * NULL if the evbuffer stands alone. */ 1.156 + struct bufferevent *parent; 1.157 +}; 1.158 + 1.159 +/** A single item in an evbuffer. */ 1.160 +struct evbuffer_chain { 1.161 + /** points to next buffer in the chain */ 1.162 + struct evbuffer_chain *next; 1.163 + 1.164 + /** total allocation available in the buffer field. */ 1.165 + size_t buffer_len; 1.166 + 1.167 + /** unused space at the beginning of buffer or an offset into a 1.168 + * file for sendfile buffers. */ 1.169 + ev_off_t misalign; 1.170 + 1.171 + /** Offset into buffer + misalign at which to start writing. 1.172 + * In other words, the total number of bytes actually stored 1.173 + * in buffer. */ 1.174 + size_t off; 1.175 + 1.176 + /** Set if special handling is required for this chain */ 1.177 + unsigned flags; 1.178 +#define EVBUFFER_MMAP 0x0001 /**< memory in buffer is mmaped */ 1.179 +#define EVBUFFER_SENDFILE 0x0002 /**< a chain used for sendfile */ 1.180 +#define EVBUFFER_REFERENCE 0x0004 /**< a chain with a mem reference */ 1.181 +#define EVBUFFER_IMMUTABLE 0x0008 /**< read-only chain */ 1.182 + /** a chain that mustn't be reallocated or freed, or have its contents 1.183 + * memmoved, until the chain is un-pinned. */ 1.184 +#define EVBUFFER_MEM_PINNED_R 0x0010 1.185 +#define EVBUFFER_MEM_PINNED_W 0x0020 1.186 +#define EVBUFFER_MEM_PINNED_ANY (EVBUFFER_MEM_PINNED_R|EVBUFFER_MEM_PINNED_W) 1.187 + /** a chain that should be freed, but can't be freed until it is 1.188 + * un-pinned. */ 1.189 +#define EVBUFFER_DANGLING 0x0040 1.190 + 1.191 + /** Usually points to the read-write memory belonging to this 1.192 + * buffer allocated as part of the evbuffer_chain allocation. 1.193 + * For mmap, this can be a read-only buffer and 1.194 + * EVBUFFER_IMMUTABLE will be set in flags. For sendfile, it 1.195 + * may point to NULL. 1.196 + */ 1.197 + unsigned char *buffer; 1.198 +}; 1.199 + 1.200 +/* this is currently used by both mmap and sendfile */ 1.201 +/* TODO(niels): something strange needs to happen for Windows here, I am not 1.202 + * sure what that is, but it needs to get looked into. 1.203 + */ 1.204 +struct evbuffer_chain_fd { 1.205 + int fd; /**< the fd associated with this chain */ 1.206 +}; 1.207 + 1.208 +/** callback for a reference buffer; lets us know what to do with it when 1.209 + * we're done with it. */ 1.210 +struct evbuffer_chain_reference { 1.211 + evbuffer_ref_cleanup_cb cleanupfn; 1.212 + void *extra; 1.213 +}; 1.214 + 1.215 +#define EVBUFFER_CHAIN_SIZE sizeof(struct evbuffer_chain) 1.216 +/** Return a pointer to extra data allocated along with an evbuffer. */ 1.217 +#define EVBUFFER_CHAIN_EXTRA(t, c) (t *)((struct evbuffer_chain *)(c) + 1) 1.218 + 1.219 +/** Assert that we are holding the lock on an evbuffer */ 1.220 +#define ASSERT_EVBUFFER_LOCKED(buffer) \ 1.221 + EVLOCK_ASSERT_LOCKED((buffer)->lock) 1.222 + 1.223 +#define EVBUFFER_LOCK(buffer) \ 1.224 + do { \ 1.225 + EVLOCK_LOCK((buffer)->lock, 0); \ 1.226 + } while (0) 1.227 +#define EVBUFFER_UNLOCK(buffer) \ 1.228 + do { \ 1.229 + EVLOCK_UNLOCK((buffer)->lock, 0); \ 1.230 + } while (0) 1.231 +#define EVBUFFER_LOCK2(buffer1, buffer2) \ 1.232 + do { \ 1.233 + EVLOCK_LOCK2((buffer1)->lock, (buffer2)->lock, 0, 0); \ 1.234 + } while (0) 1.235 +#define EVBUFFER_UNLOCK2(buffer1, buffer2) \ 1.236 + do { \ 1.237 + EVLOCK_UNLOCK2((buffer1)->lock, (buffer2)->lock, 0, 0); \ 1.238 + } while (0) 1.239 + 1.240 +/** Increase the reference count of buf by one. */ 1.241 +void _evbuffer_incref(struct evbuffer *buf); 1.242 +/** Increase the reference count of buf by one and acquire the lock. */ 1.243 +void _evbuffer_incref_and_lock(struct evbuffer *buf); 1.244 +/** Pin a single buffer chain using a given flag. A pinned chunk may not be 1.245 + * moved or freed until it is unpinned. */ 1.246 +void _evbuffer_chain_pin(struct evbuffer_chain *chain, unsigned flag); 1.247 +/** Unpin a single buffer chain using a given flag. */ 1.248 +void _evbuffer_chain_unpin(struct evbuffer_chain *chain, unsigned flag); 1.249 +/** As evbuffer_free, but requires that we hold a lock on the buffer, and 1.250 + * releases the lock before freeing it and the buffer. */ 1.251 +void _evbuffer_decref_and_unlock(struct evbuffer *buffer); 1.252 + 1.253 +/** As evbuffer_expand, but does not guarantee that the newly allocated memory 1.254 + * is contiguous. Instead, it may be split across two or more chunks. */ 1.255 +int _evbuffer_expand_fast(struct evbuffer *, size_t, int); 1.256 + 1.257 +/** Helper: prepares for a readv/WSARecv call by expanding the buffer to 1.258 + * hold enough memory to read 'howmuch' bytes in possibly noncontiguous memory. 1.259 + * Sets up the one or two iovecs in 'vecs' to point to the free memory and its 1.260 + * extent, and *chainp to point to the first chain that we'll try to read into. 1.261 + * Returns the number of vecs used. 1.262 + */ 1.263 +int _evbuffer_read_setup_vecs(struct evbuffer *buf, ev_ssize_t howmuch, 1.264 + struct evbuffer_iovec *vecs, int n_vecs, struct evbuffer_chain ***chainp, 1.265 + int exact); 1.266 + 1.267 +/* Helper macro: copies an evbuffer_iovec in ei to a win32 WSABUF in i. */ 1.268 +#define WSABUF_FROM_EVBUFFER_IOV(i,ei) do { \ 1.269 + (i)->buf = (ei)->iov_base; \ 1.270 + (i)->len = (unsigned long)(ei)->iov_len; \ 1.271 + } while (0) 1.272 +/* XXXX the cast above is safe for now, but not if we allow mmaps on win64. 1.273 + * See note in buffer_iocp's launch_write function */ 1.274 + 1.275 +/** Set the parent bufferevent object for buf to bev */ 1.276 +void evbuffer_set_parent(struct evbuffer *buf, struct bufferevent *bev); 1.277 + 1.278 +void evbuffer_invoke_callbacks(struct evbuffer *buf); 1.279 + 1.280 +#ifdef __cplusplus 1.281 +} 1.282 +#endif 1.283 + 1.284 +#endif /* _EVBUFFER_INTERNAL_H_ */