1 /* 2 * Copyright (c) 2000-2007 Niels Provos <[email protected]> 3 * Copyright (c) 2007-2010 Niels Provos and Nick Mathewson 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 #ifndef _EVBUFFER_INTERNAL_H_ 28 #define _EVBUFFER_INTERNAL_H_ 29 30 #ifdef __cplusplus 31 extern "C" { 32 #endif 33 34 #include "event2/event-config.h" 35 #include "evconfig-private.h" 36 #include "event2/util.h" 37 #include "util-internal.h" 38 #include "defer-internal.h" 39 40 /* Experimental cb flag: "never deferred." Implementation note: 41 * these callbacks may get an inaccurate view of n_del/n_added in their 42 * arguments. */ 43 #define EVBUFFER_CB_NODEFER 2 44 45 #ifdef _WIN32 46 #include <winsock2.h> 47 #endif 48 #include <sys/queue.h> 49 50 /* Minimum allocation for a chain. We define this so that we're burning no 51 * more than 5% of each allocation on overhead. It would be nice to lose even 52 * less space, though. */ 53 #if _EVENT_SIZEOF_VOID_P < 8 54 #define MIN_BUFFER_SIZE 512 55 #else 56 #define MIN_BUFFER_SIZE 1024 57 #endif 58 59 /** A single evbuffer callback for an evbuffer. This function will be invoked 60 * when bytes are added to or removed from the evbuffer. */ 61 struct evbuffer_cb_entry { 62 /** Structures to implement a doubly-linked queue of callbacks */ 63 TAILQ_ENTRY(evbuffer_cb_entry) next; 64 /** The callback function to invoke when this callback is called. 65 If EVBUFFER_CB_OBSOLETE is set in flags, the cb_obsolete field is 66 valid; otherwise, cb_func is valid. */ 67 union { 68 evbuffer_cb_func cb_func; 69 evbuffer_cb cb_obsolete; 70 } cb; 71 /** Argument to pass to cb. */ 72 void *cbarg; 73 /** Currently set flags on this callback. */ 74 ev_uint32_t flags; 75 }; 76 77 struct bufferevent; 78 struct evbuffer_chain; 79 struct evbuffer { 80 /** The first chain in this buffer's linked list of chains. */ 81 struct evbuffer_chain *first; 82 /** The last chain in this buffer's linked list of chains. */ 83 struct evbuffer_chain *last; 84 85 /** 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 * points at the last_with_datap chain. If the last_with_data chain 95 * is the first chain, or it is NULL, then the last_with_datap pointer 96 * is &buf->first. 97 */ 98 struct evbuffer_chain **last_with_datap; 99 100 /** Total amount of bytes stored in all chains.*/ 101 size_t total_len; 102 103 /** Number of bytes we have added to the buffer since we last tried to 104 * invoke callbacks. */ 105 size_t n_add_for_cb; 106 /** Number of bytes we have removed from the buffer since we last 107 * tried to invoke callbacks. */ 108 size_t n_del_for_cb; 109 110 #ifndef _EVENT_DISABLE_THREAD_SUPPORT 111 /** A lock used to mediate access to this buffer. */ 112 void *lock; 113 #endif 114 /** True iff we should free the lock field when we free this 115 * evbuffer. */ 116 unsigned own_lock : 1; 117 /** True iff we should not allow changes to the front of the buffer 118 * (drains or prepends). */ 119 unsigned freeze_start : 1; 120 /** True iff we should not allow changes to the end of the buffer 121 * (appends) */ 122 unsigned freeze_end : 1; 123 /** True iff this evbuffer's callbacks are not invoked immediately 124 * upon a change in the buffer, but instead are deferred to be invoked 125 * from the event_base's loop. Useful for preventing enormous stack 126 * overflows when we have mutually recursive callbacks, and for 127 * serializing callbacks in a single thread. */ 128 unsigned deferred_cbs : 1; 129 #ifdef _WIN32 130 /** True iff this buffer is set up for overlapped IO. */ 131 unsigned is_overlapped : 1; 132 #endif 133 134 /** Used to implement deferred callbacks. */ 135 struct deferred_cb_queue *cb_queue; 136 137 /** A reference count on this evbuffer. When the reference count 138 * reaches 0, the buffer is destroyed. Manipulated with 139 * evbuffer_incref and evbuffer_decref_and_unlock and 140 * evbuffer_free. */ 141 int refcnt; 142 143 /** A deferred_cb handle to make all of this buffer's callbacks 144 * invoked from the event loop. */ 145 struct deferred_cb deferred; 146 147 /** A doubly-linked-list of callback functions */ 148 TAILQ_HEAD(evbuffer_cb_queue, evbuffer_cb_entry) callbacks; 149 150 /** The parent bufferevent object this evbuffer belongs to. 151 * NULL if the evbuffer stands alone. */ 152 struct bufferevent *parent; 153 }; 154 155 /** A single item in an evbuffer. */ 156 struct evbuffer_chain { 157 /** points to next buffer in the chain */ 158 struct evbuffer_chain *next; 159 160 /** total allocation available in the buffer field. */ 161 size_t buffer_len; 162 163 /** unused space at the beginning of buffer or an offset into a 164 * file for sendfile buffers. */ 165 ev_off_t misalign; 166 167 /** Offset into buffer + misalign at which to start writing. 168 * In other words, the total number of bytes actually stored 169 * in buffer. */ 170 size_t off; 171 172 /** Set if special handling is required for this chain */ 173 unsigned flags; 174 #define EVBUFFER_FILESEGMENT 0x0001 /**< A chain used for a file segment */ 175 #define EVBUFFER_SENDFILE 0x0002 /**< a chain used with sendfile */ 176 #define EVBUFFER_REFERENCE 0x0004 /**< a chain with a mem reference */ 177 #define EVBUFFER_IMMUTABLE 0x0008 /**< read-only chain */ 178 /** a chain that mustn't be reallocated or freed, or have its contents 179 * memmoved, until the chain is un-pinned. */ 180 #define EVBUFFER_MEM_PINNED_R 0x0010 181 #define EVBUFFER_MEM_PINNED_W 0x0020 182 #define EVBUFFER_MEM_PINNED_ANY (EVBUFFER_MEM_PINNED_R|EVBUFFER_MEM_PINNED_W) 183 /** a chain that should be freed, but can't be freed until it is 184 * un-pinned. */ 185 #define EVBUFFER_DANGLING 0x0040 186 /** a chain that is a referenced copy of another chain */ 187 #define EVBUFFER_MULTICAST 0x0080 188 189 /** number of multicast references to this chain */ 190 int refcnt; 191 192 /** Usually points to the read-write memory belonging to this 193 * buffer allocated as part of the evbuffer_chain allocation. 194 * For mmap, this can be a read-only buffer and 195 * EVBUFFER_IMMUTABLE will be set in flags. For sendfile, it 196 * may point to NULL. 197 */ 198 unsigned char *buffer; 199 }; 200 201 /** callback for a reference chain; lets us know what to do with it when 202 * we're done with it. Lives at the end of an evbuffer_chain with the 203 * EVBUFFER_REFERENCE flag set */ 204 struct evbuffer_chain_reference { 205 evbuffer_ref_cleanup_cb cleanupfn; 206 void *extra; 207 }; 208 209 /** File segment for a file-segment chain. Lives at the end of an 210 * evbuffer_chain with the EVBUFFER_FILESEGMENT flag set. */ 211 struct evbuffer_chain_file_segment { 212 struct evbuffer_file_segment *segment; 213 #ifdef _WIN32 214 /** If we're using CreateFileMapping, this is the handle to the view. */ 215 HANDLE view_handle; 216 #endif 217 }; 218 219 /* Declared in event2/buffer.h; defined here. */ 220 struct evbuffer_file_segment { 221 void *lock; /**< lock prevent concurrent access to refcnt */ 222 int refcnt; /**< Reference count for this file segment */ 223 unsigned flags; /**< combination of EVBUF_FS_* flags */ 224 225 /** What kind of file segment is this? */ 226 enum {EVBUF_FS_MMAP, EVBUF_FS_SENDFILE, EVBUF_FS_IO} type; 227 228 /** The fd that we read the data from. */ 229 int fd; 230 /** If we're using mmap, this is the raw mapped memory. */ 231 void *mapping; 232 #ifdef _WIN32 233 /** If we're using CreateFileMapping, this is the mapping */ 234 HANDLE mapping_handle; 235 #endif 236 /** If we're using mmap or IO, this is the content of the file 237 * segment. */ 238 char *contents; 239 /** If we're using mmap, this is the offset within 'mapping' where 240 * this data segment begins. If we're using sendfile, this is the 241 * offset within the file where this data begins. If we're using IO, 242 * this is 0. */ 243 ev_off_t offset; 244 /** The length of this segment. */ 245 ev_off_t length; 246 }; 247 248 /** Information about the multicast parent of a chain. Lives at the 249 * end of an evbuffer_chain with the EVBUFFER_MULTICAST flag set. */ 250 struct evbuffer_multicast_parent { 251 /** source buffer the multicast parent belongs to */ 252 struct evbuffer *source; 253 /** multicast parent for this chain */ 254 struct evbuffer_chain *parent; 255 }; 256 257 #define EVBUFFER_CHAIN_SIZE sizeof(struct evbuffer_chain) 258 /** Return a pointer to extra data allocated along with an evbuffer. */ 259 #define EVBUFFER_CHAIN_EXTRA(t, c) (t *)((struct evbuffer_chain *)(c) + 1) 260 261 /** Assert that we are holding the lock on an evbuffer */ 262 #define ASSERT_EVBUFFER_LOCKED(buffer) \ 263 EVLOCK_ASSERT_LOCKED((buffer)->lock) 264 265 #define EVBUFFER_LOCK(buffer) \ 266 do { \ 267 EVLOCK_LOCK((buffer)->lock, 0); \ 268 } while (0) 269 #define EVBUFFER_UNLOCK(buffer) \ 270 do { \ 271 EVLOCK_UNLOCK((buffer)->lock, 0); \ 272 } while (0) 273 #define EVBUFFER_LOCK2(buffer1, buffer2) \ 274 do { \ 275 EVLOCK_LOCK2((buffer1)->lock, (buffer2)->lock, 0, 0); \ 276 } while (0) 277 #define EVBUFFER_UNLOCK2(buffer1, buffer2) \ 278 do { \ 279 EVLOCK_UNLOCK2((buffer1)->lock, (buffer2)->lock, 0, 0); \ 280 } while (0) 281 282 /** Increase the reference count of buf by one. */ 283 void _evbuffer_incref(struct evbuffer *buf); 284 /** Increase the reference count of buf by one and acquire the lock. */ 285 void _evbuffer_incref_and_lock(struct evbuffer *buf); 286 /** Pin a single buffer chain using a given flag. A pinned chunk may not be 287 * moved or freed until it is unpinned. */ 288 void _evbuffer_chain_pin(struct evbuffer_chain *chain, unsigned flag); 289 /** Unpin a single buffer chain using a given flag. */ 290 void _evbuffer_chain_unpin(struct evbuffer_chain *chain, unsigned flag); 291 /** As evbuffer_free, but requires that we hold a lock on the buffer, and 292 * releases the lock before freeing it and the buffer. */ 293 void _evbuffer_decref_and_unlock(struct evbuffer *buffer); 294 295 /** As evbuffer_expand, but does not guarantee that the newly allocated memory 296 * is contiguous. Instead, it may be split across two or more chunks. */ 297 int _evbuffer_expand_fast(struct evbuffer *, size_t, int); 298 299 /** Helper: prepares for a readv/WSARecv call by expanding the buffer to 300 * hold enough memory to read 'howmuch' bytes in possibly noncontiguous memory. 301 * Sets up the one or two iovecs in 'vecs' to point to the free memory and its 302 * extent, and *chainp to point to the first chain that we'll try to read into. 303 * Returns the number of vecs used. 304 */ 305 int _evbuffer_read_setup_vecs(struct evbuffer *buf, ev_ssize_t howmuch, 306 struct evbuffer_iovec *vecs, int n_vecs, struct evbuffer_chain ***chainp, 307 int exact); 308 309 /* Helper macro: copies an evbuffer_iovec in ei to a win32 WSABUF in i. */ 310 #define WSABUF_FROM_EVBUFFER_IOV(i,ei) do { \ 311 (i)->buf = (ei)->iov_base; \ 312 (i)->len = (unsigned long)(ei)->iov_len; \ 313 } while (0) 314 /* XXXX the cast above is safe for now, but not if we allow mmaps on win64. 315 * See note in buffer_iocp's launch_write function */ 316 317 /** Set the parent bufferevent object for buf to bev */ 318 void evbuffer_set_parent(struct evbuffer *buf, struct bufferevent *bev); 319 320 #ifdef __cplusplus 321 } 322 #endif 323 324 #endif /* _EVBUFFER_INTERNAL_H_ */ 325