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