xref: /freebsd/contrib/ntp/sntp/libevent/evbuffer-internal.h (revision a466cc55373fc3cf86837f09da729535b57e69a1)
12b15cb3dSCy Schubert /*
22b15cb3dSCy Schubert  * Copyright (c) 2000-2007 Niels Provos <provos@citi.umich.edu>
32b15cb3dSCy Schubert  * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
42b15cb3dSCy Schubert  *
52b15cb3dSCy Schubert  * Redistribution and use in source and binary forms, with or without
62b15cb3dSCy Schubert  * modification, are permitted provided that the following conditions
72b15cb3dSCy Schubert  * are met:
82b15cb3dSCy Schubert  * 1. Redistributions of source code must retain the above copyright
92b15cb3dSCy Schubert  *    notice, this list of conditions and the following disclaimer.
102b15cb3dSCy Schubert  * 2. Redistributions in binary form must reproduce the above copyright
112b15cb3dSCy Schubert  *    notice, this list of conditions and the following disclaimer in the
122b15cb3dSCy Schubert  *    documentation and/or other materials provided with the distribution.
132b15cb3dSCy Schubert  * 3. The name of the author may not be used to endorse or promote products
142b15cb3dSCy Schubert  *    derived from this software without specific prior written permission.
152b15cb3dSCy Schubert  *
162b15cb3dSCy Schubert  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
172b15cb3dSCy Schubert  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
182b15cb3dSCy Schubert  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
192b15cb3dSCy Schubert  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
202b15cb3dSCy Schubert  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
212b15cb3dSCy Schubert  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
222b15cb3dSCy Schubert  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
232b15cb3dSCy Schubert  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
242b15cb3dSCy Schubert  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
252b15cb3dSCy Schubert  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
262b15cb3dSCy Schubert  */
272b15cb3dSCy Schubert #ifndef EVBUFFER_INTERNAL_H_INCLUDED_
282b15cb3dSCy Schubert #define EVBUFFER_INTERNAL_H_INCLUDED_
292b15cb3dSCy Schubert 
302b15cb3dSCy Schubert #ifdef __cplusplus
312b15cb3dSCy Schubert extern "C" {
322b15cb3dSCy Schubert #endif
332b15cb3dSCy Schubert 
342b15cb3dSCy Schubert #include "event2/event-config.h"
352b15cb3dSCy Schubert #include "evconfig-private.h"
362b15cb3dSCy Schubert #include "event2/util.h"
372b15cb3dSCy Schubert #include "event2/event_struct.h"
382b15cb3dSCy Schubert #include "util-internal.h"
392b15cb3dSCy Schubert #include "defer-internal.h"
402b15cb3dSCy Schubert 
412b15cb3dSCy Schubert /* Experimental cb flag: "never deferred."  Implementation note:
422b15cb3dSCy Schubert  * these callbacks may get an inaccurate view of n_del/n_added in their
432b15cb3dSCy Schubert  * arguments. */
442b15cb3dSCy Schubert #define EVBUFFER_CB_NODEFER 2
452b15cb3dSCy Schubert 
462b15cb3dSCy Schubert #ifdef _WIN32
472b15cb3dSCy Schubert #include <winsock2.h>
482b15cb3dSCy Schubert #endif
492b15cb3dSCy Schubert #include <sys/queue.h>
502b15cb3dSCy Schubert 
512b15cb3dSCy Schubert /* Minimum allocation for a chain.  We define this so that we're burning no
522b15cb3dSCy Schubert  * more than 5% of each allocation on overhead.  It would be nice to lose even
532b15cb3dSCy Schubert  * less space, though. */
542b15cb3dSCy Schubert #if EVENT__SIZEOF_VOID_P < 8
552b15cb3dSCy Schubert #define MIN_BUFFER_SIZE	512
562b15cb3dSCy Schubert #else
572b15cb3dSCy Schubert #define MIN_BUFFER_SIZE	1024
582b15cb3dSCy Schubert #endif
592b15cb3dSCy Schubert 
602b15cb3dSCy Schubert /** A single evbuffer callback for an evbuffer. This function will be invoked
612b15cb3dSCy Schubert  * when bytes are added to or removed from the evbuffer. */
622b15cb3dSCy Schubert struct evbuffer_cb_entry {
632b15cb3dSCy Schubert 	/** Structures to implement a doubly-linked queue of callbacks */
642b15cb3dSCy Schubert 	LIST_ENTRY(evbuffer_cb_entry) next;
652b15cb3dSCy Schubert 	/** The callback function to invoke when this callback is called.
662b15cb3dSCy Schubert 	    If EVBUFFER_CB_OBSOLETE is set in flags, the cb_obsolete field is
672b15cb3dSCy Schubert 	    valid; otherwise, cb_func is valid. */
682b15cb3dSCy Schubert 	union {
692b15cb3dSCy Schubert 		evbuffer_cb_func cb_func;
702b15cb3dSCy Schubert 		evbuffer_cb cb_obsolete;
712b15cb3dSCy Schubert 	} cb;
722b15cb3dSCy Schubert 	/** Argument to pass to cb. */
732b15cb3dSCy Schubert 	void *cbarg;
742b15cb3dSCy Schubert 	/** Currently set flags on this callback. */
752b15cb3dSCy Schubert 	ev_uint32_t flags;
762b15cb3dSCy Schubert };
772b15cb3dSCy Schubert 
782b15cb3dSCy Schubert struct bufferevent;
792b15cb3dSCy Schubert struct evbuffer_chain;
802b15cb3dSCy Schubert struct evbuffer {
812b15cb3dSCy Schubert 	/** The first chain in this buffer's linked list of chains. */
822b15cb3dSCy Schubert 	struct evbuffer_chain *first;
832b15cb3dSCy Schubert 	/** The last chain in this buffer's linked list of chains. */
842b15cb3dSCy Schubert 	struct evbuffer_chain *last;
852b15cb3dSCy Schubert 
862b15cb3dSCy Schubert 	/** Pointer to the next pointer pointing at the 'last_with_data' chain.
872b15cb3dSCy Schubert 	 *
882b15cb3dSCy Schubert 	 * To unpack:
892b15cb3dSCy Schubert 	 *
902b15cb3dSCy Schubert 	 * The last_with_data chain is the last chain that has any data in it.
912b15cb3dSCy Schubert 	 * If all chains in the buffer are empty, it is the first chain.
922b15cb3dSCy Schubert 	 * If the buffer has no chains, it is NULL.
932b15cb3dSCy Schubert 	 *
942b15cb3dSCy Schubert 	 * The last_with_datap pointer points at _whatever 'next' pointer_
95*a466cc55SCy Schubert 	 * pointing at the last_with_data chain. If the last_with_data chain
962b15cb3dSCy Schubert 	 * is the first chain, or it is NULL, then the last_with_datap pointer
972b15cb3dSCy Schubert 	 * is &buf->first.
982b15cb3dSCy Schubert 	 */
992b15cb3dSCy Schubert 	struct evbuffer_chain **last_with_datap;
1002b15cb3dSCy Schubert 
1012b15cb3dSCy Schubert 	/** Total amount of bytes stored in all chains.*/
1022b15cb3dSCy Schubert 	size_t total_len;
1032b15cb3dSCy Schubert 
1042b15cb3dSCy Schubert 	/** Number of bytes we have added to the buffer since we last tried to
1052b15cb3dSCy Schubert 	 * invoke callbacks. */
1062b15cb3dSCy Schubert 	size_t n_add_for_cb;
1072b15cb3dSCy Schubert 	/** Number of bytes we have removed from the buffer since we last
1082b15cb3dSCy Schubert 	 * tried to invoke callbacks. */
1092b15cb3dSCy Schubert 	size_t n_del_for_cb;
1102b15cb3dSCy Schubert 
1112b15cb3dSCy Schubert #ifndef EVENT__DISABLE_THREAD_SUPPORT
1122b15cb3dSCy Schubert 	/** A lock used to mediate access to this buffer. */
1132b15cb3dSCy Schubert 	void *lock;
1142b15cb3dSCy Schubert #endif
1152b15cb3dSCy Schubert 	/** True iff we should free the lock field when we free this
1162b15cb3dSCy Schubert 	 * evbuffer. */
1172b15cb3dSCy Schubert 	unsigned own_lock : 1;
1182b15cb3dSCy Schubert 	/** True iff we should not allow changes to the front of the buffer
1192b15cb3dSCy Schubert 	 * (drains or prepends). */
1202b15cb3dSCy Schubert 	unsigned freeze_start : 1;
1212b15cb3dSCy Schubert 	/** True iff we should not allow changes to the end of the buffer
1222b15cb3dSCy Schubert 	 * (appends) */
1232b15cb3dSCy Schubert 	unsigned freeze_end : 1;
1242b15cb3dSCy Schubert 	/** True iff this evbuffer's callbacks are not invoked immediately
1252b15cb3dSCy Schubert 	 * upon a change in the buffer, but instead are deferred to be invoked
1262b15cb3dSCy Schubert 	 * from the event_base's loop.	Useful for preventing enormous stack
1272b15cb3dSCy Schubert 	 * overflows when we have mutually recursive callbacks, and for
1282b15cb3dSCy Schubert 	 * serializing callbacks in a single thread. */
1292b15cb3dSCy Schubert 	unsigned deferred_cbs : 1;
1302b15cb3dSCy Schubert #ifdef _WIN32
1312b15cb3dSCy Schubert 	/** True iff this buffer is set up for overlapped IO. */
1322b15cb3dSCy Schubert 	unsigned is_overlapped : 1;
1332b15cb3dSCy Schubert #endif
1342b15cb3dSCy Schubert 	/** Zero or more EVBUFFER_FLAG_* bits */
1352b15cb3dSCy Schubert 	ev_uint32_t flags;
1362b15cb3dSCy Schubert 
1372b15cb3dSCy Schubert 	/** Used to implement deferred callbacks. */
1382b15cb3dSCy Schubert 	struct event_base *cb_queue;
1392b15cb3dSCy Schubert 
1402b15cb3dSCy Schubert 	/** A reference count on this evbuffer.	 When the reference count
1412b15cb3dSCy Schubert 	 * reaches 0, the buffer is destroyed.	Manipulated with
1422b15cb3dSCy Schubert 	 * evbuffer_incref and evbuffer_decref_and_unlock and
1432b15cb3dSCy Schubert 	 * evbuffer_free. */
1442b15cb3dSCy Schubert 	int refcnt;
1452b15cb3dSCy Schubert 
1462b15cb3dSCy Schubert 	/** A struct event_callback handle to make all of this buffer's callbacks
1472b15cb3dSCy Schubert 	 * invoked from the event loop. */
1482b15cb3dSCy Schubert 	struct event_callback deferred;
1492b15cb3dSCy Schubert 
1502b15cb3dSCy Schubert 	/** A doubly-linked-list of callback functions */
1512b15cb3dSCy Schubert 	LIST_HEAD(evbuffer_cb_queue, evbuffer_cb_entry) callbacks;
1522b15cb3dSCy Schubert 
1532b15cb3dSCy Schubert 	/** The parent bufferevent object this evbuffer belongs to.
1542b15cb3dSCy Schubert 	 * NULL if the evbuffer stands alone. */
1552b15cb3dSCy Schubert 	struct bufferevent *parent;
1562b15cb3dSCy Schubert };
1572b15cb3dSCy Schubert 
158a25439b6SCy Schubert #if EVENT__SIZEOF_OFF_T < EVENT__SIZEOF_SIZE_T
159a25439b6SCy Schubert typedef ev_ssize_t ev_misalign_t;
160a25439b6SCy Schubert #define EVBUFFER_CHAIN_MAX ((size_t)EV_SSIZE_MAX)
161a25439b6SCy Schubert #else
162a25439b6SCy Schubert typedef ev_off_t ev_misalign_t;
163a25439b6SCy Schubert #if EVENT__SIZEOF_OFF_T > EVENT__SIZEOF_SIZE_T
164a25439b6SCy Schubert #define EVBUFFER_CHAIN_MAX EV_SIZE_MAX
165a25439b6SCy Schubert #else
166a25439b6SCy Schubert #define EVBUFFER_CHAIN_MAX ((size_t)EV_SSIZE_MAX)
167a25439b6SCy Schubert #endif
168a25439b6SCy Schubert #endif
169a25439b6SCy Schubert 
1702b15cb3dSCy Schubert /** A single item in an evbuffer. */
1712b15cb3dSCy Schubert struct evbuffer_chain {
1722b15cb3dSCy Schubert 	/** points to next buffer in the chain */
1732b15cb3dSCy Schubert 	struct evbuffer_chain *next;
1742b15cb3dSCy Schubert 
1752b15cb3dSCy Schubert 	/** total allocation available in the buffer field. */
1762b15cb3dSCy Schubert 	size_t buffer_len;
1772b15cb3dSCy Schubert 
1782b15cb3dSCy Schubert 	/** unused space at the beginning of buffer or an offset into a
1792b15cb3dSCy Schubert 	 * file for sendfile buffers. */
180a25439b6SCy Schubert 	ev_misalign_t misalign;
1812b15cb3dSCy Schubert 
1822b15cb3dSCy Schubert 	/** Offset into buffer + misalign at which to start writing.
1832b15cb3dSCy Schubert 	 * In other words, the total number of bytes actually stored
1842b15cb3dSCy Schubert 	 * in buffer. */
1852b15cb3dSCy Schubert 	size_t off;
1862b15cb3dSCy Schubert 
1872b15cb3dSCy Schubert 	/** Set if special handling is required for this chain */
1882b15cb3dSCy Schubert 	unsigned flags;
1892b15cb3dSCy Schubert #define EVBUFFER_FILESEGMENT	0x0001  /**< A chain used for a file segment */
1902b15cb3dSCy Schubert #define EVBUFFER_SENDFILE	0x0002	/**< a chain used with sendfile */
1912b15cb3dSCy Schubert #define EVBUFFER_REFERENCE	0x0004	/**< a chain with a mem reference */
1922b15cb3dSCy Schubert #define EVBUFFER_IMMUTABLE	0x0008	/**< read-only chain */
1932b15cb3dSCy Schubert 	/** a chain that mustn't be reallocated or freed, or have its contents
1942b15cb3dSCy Schubert 	 * memmoved, until the chain is un-pinned. */
1952b15cb3dSCy Schubert #define EVBUFFER_MEM_PINNED_R	0x0010
1962b15cb3dSCy Schubert #define EVBUFFER_MEM_PINNED_W	0x0020
1972b15cb3dSCy Schubert #define EVBUFFER_MEM_PINNED_ANY (EVBUFFER_MEM_PINNED_R|EVBUFFER_MEM_PINNED_W)
1982b15cb3dSCy Schubert 	/** a chain that should be freed, but can't be freed until it is
1992b15cb3dSCy Schubert 	 * un-pinned. */
2002b15cb3dSCy Schubert #define EVBUFFER_DANGLING	0x0040
2012b15cb3dSCy Schubert 	/** a chain that is a referenced copy of another chain */
2022b15cb3dSCy Schubert #define EVBUFFER_MULTICAST	0x0080
2032b15cb3dSCy Schubert 
2042b15cb3dSCy Schubert 	/** number of references to this chain */
2052b15cb3dSCy Schubert 	int refcnt;
2062b15cb3dSCy Schubert 
2072b15cb3dSCy Schubert 	/** Usually points to the read-write memory belonging to this
2082b15cb3dSCy Schubert 	 * buffer allocated as part of the evbuffer_chain allocation.
2092b15cb3dSCy Schubert 	 * For mmap, this can be a read-only buffer and
2102b15cb3dSCy Schubert 	 * EVBUFFER_IMMUTABLE will be set in flags.  For sendfile, it
2112b15cb3dSCy Schubert 	 * may point to NULL.
2122b15cb3dSCy Schubert 	 */
2132b15cb3dSCy Schubert 	unsigned char *buffer;
2142b15cb3dSCy Schubert };
2152b15cb3dSCy Schubert 
2162b15cb3dSCy Schubert /** callback for a reference chain; lets us know what to do with it when
2172b15cb3dSCy Schubert  * we're done with it. Lives at the end of an evbuffer_chain with the
2182b15cb3dSCy Schubert  * EVBUFFER_REFERENCE flag set */
2192b15cb3dSCy Schubert struct evbuffer_chain_reference {
2202b15cb3dSCy Schubert 	evbuffer_ref_cleanup_cb cleanupfn;
2212b15cb3dSCy Schubert 	void *extra;
2222b15cb3dSCy Schubert };
2232b15cb3dSCy Schubert 
2242b15cb3dSCy Schubert /** File segment for a file-segment chain.  Lives at the end of an
2252b15cb3dSCy Schubert  * evbuffer_chain with the EVBUFFER_FILESEGMENT flag set.  */
2262b15cb3dSCy Schubert struct evbuffer_chain_file_segment {
2272b15cb3dSCy Schubert 	struct evbuffer_file_segment *segment;
2282b15cb3dSCy Schubert #ifdef _WIN32
2292b15cb3dSCy Schubert 	/** If we're using CreateFileMapping, this is the handle to the view. */
2302b15cb3dSCy Schubert 	HANDLE view_handle;
2312b15cb3dSCy Schubert #endif
2322b15cb3dSCy Schubert };
2332b15cb3dSCy Schubert 
2342b15cb3dSCy Schubert /* Declared in event2/buffer.h; defined here. */
2352b15cb3dSCy Schubert struct evbuffer_file_segment {
2362b15cb3dSCy Schubert 	void *lock; /**< lock prevent concurrent access to refcnt */
2372b15cb3dSCy Schubert 	int refcnt; /**< Reference count for this file segment */
2382b15cb3dSCy Schubert 	unsigned flags; /**< combination of EVBUF_FS_* flags  */
2392b15cb3dSCy Schubert 
2402b15cb3dSCy Schubert 	/** What kind of file segment is this? */
2412b15cb3dSCy Schubert 	unsigned can_sendfile : 1;
2422b15cb3dSCy Schubert 	unsigned is_mapping : 1;
2432b15cb3dSCy Schubert 
2442b15cb3dSCy Schubert 	/** The fd that we read the data from. */
2452b15cb3dSCy Schubert 	int fd;
2462b15cb3dSCy Schubert 	/** If we're using mmap, this is the raw mapped memory. */
2472b15cb3dSCy Schubert 	void *mapping;
2482b15cb3dSCy Schubert #ifdef _WIN32
2492b15cb3dSCy Schubert 	/** If we're using CreateFileMapping, this is the mapping */
2502b15cb3dSCy Schubert 	HANDLE mapping_handle;
2512b15cb3dSCy Schubert #endif
2522b15cb3dSCy Schubert 	/** If we're using mmap or IO, this is the content of the file
2532b15cb3dSCy Schubert 	 * segment. */
2542b15cb3dSCy Schubert 	char *contents;
2552b15cb3dSCy Schubert 	/** Position of this segment within the file. */
2562b15cb3dSCy Schubert 	ev_off_t file_offset;
2572b15cb3dSCy Schubert 	/** If we're using mmap, this is the offset within 'mapping' where
2582b15cb3dSCy Schubert 	 * this data segment begins. */
2592b15cb3dSCy Schubert 	ev_off_t mmap_offset;
2602b15cb3dSCy Schubert 	/** The length of this segment. */
2612b15cb3dSCy Schubert 	ev_off_t length;
2622b15cb3dSCy Schubert 	/** Cleanup callback function */
2632b15cb3dSCy Schubert 	evbuffer_file_segment_cleanup_cb cleanup_cb;
2642b15cb3dSCy Schubert 	/** Argument to be pass to cleanup callback function */
2652b15cb3dSCy Schubert 	void *cleanup_cb_arg;
2662b15cb3dSCy Schubert };
2672b15cb3dSCy Schubert 
2682b15cb3dSCy Schubert /** Information about the multicast parent of a chain.  Lives at the
2692b15cb3dSCy Schubert  * end of an evbuffer_chain with the EVBUFFER_MULTICAST flag set.  */
2702b15cb3dSCy Schubert struct evbuffer_multicast_parent {
2712b15cb3dSCy Schubert 	/** source buffer the multicast parent belongs to */
2722b15cb3dSCy Schubert 	struct evbuffer *source;
2732b15cb3dSCy Schubert 	/** multicast parent for this chain */
2742b15cb3dSCy Schubert 	struct evbuffer_chain *parent;
2752b15cb3dSCy Schubert };
2762b15cb3dSCy Schubert 
2772b15cb3dSCy Schubert #define EVBUFFER_CHAIN_SIZE sizeof(struct evbuffer_chain)
2782b15cb3dSCy Schubert /** Return a pointer to extra data allocated along with an evbuffer. */
2792b15cb3dSCy Schubert #define EVBUFFER_CHAIN_EXTRA(t, c) (t *)((struct evbuffer_chain *)(c) + 1)
2802b15cb3dSCy Schubert 
2812b15cb3dSCy Schubert /** Assert that we are holding the lock on an evbuffer */
2822b15cb3dSCy Schubert #define ASSERT_EVBUFFER_LOCKED(buffer)			\
2832b15cb3dSCy Schubert 	EVLOCK_ASSERT_LOCKED((buffer)->lock)
2842b15cb3dSCy Schubert 
2852b15cb3dSCy Schubert #define EVBUFFER_LOCK(buffer)						\
2862b15cb3dSCy Schubert 	do {								\
2872b15cb3dSCy Schubert 		EVLOCK_LOCK((buffer)->lock, 0);				\
2882b15cb3dSCy Schubert 	} while (0)
2892b15cb3dSCy Schubert #define EVBUFFER_UNLOCK(buffer)						\
2902b15cb3dSCy Schubert 	do {								\
2912b15cb3dSCy Schubert 		EVLOCK_UNLOCK((buffer)->lock, 0);			\
2922b15cb3dSCy Schubert 	} while (0)
2932b15cb3dSCy Schubert #define EVBUFFER_LOCK2(buffer1, buffer2)				\
2942b15cb3dSCy Schubert 	do {								\
2952b15cb3dSCy Schubert 		EVLOCK_LOCK2((buffer1)->lock, (buffer2)->lock, 0, 0);	\
2962b15cb3dSCy Schubert 	} while (0)
2972b15cb3dSCy Schubert #define EVBUFFER_UNLOCK2(buffer1, buffer2)				\
2982b15cb3dSCy Schubert 	do {								\
2992b15cb3dSCy Schubert 		EVLOCK_UNLOCK2((buffer1)->lock, (buffer2)->lock, 0, 0);	\
3002b15cb3dSCy Schubert 	} while (0)
3012b15cb3dSCy Schubert 
3022b15cb3dSCy Schubert /** Increase the reference count of buf by one. */
3032b15cb3dSCy Schubert void evbuffer_incref_(struct evbuffer *buf);
3042b15cb3dSCy Schubert /** Increase the reference count of buf by one and acquire the lock. */
3052b15cb3dSCy Schubert void evbuffer_incref_and_lock_(struct evbuffer *buf);
3062b15cb3dSCy Schubert /** Pin a single buffer chain using a given flag. A pinned chunk may not be
3072b15cb3dSCy Schubert  * moved or freed until it is unpinned. */
3082b15cb3dSCy Schubert void evbuffer_chain_pin_(struct evbuffer_chain *chain, unsigned flag);
3092b15cb3dSCy Schubert /** Unpin a single buffer chain using a given flag. */
3102b15cb3dSCy Schubert void evbuffer_chain_unpin_(struct evbuffer_chain *chain, unsigned flag);
3112b15cb3dSCy Schubert /** As evbuffer_free, but requires that we hold a lock on the buffer, and
3122b15cb3dSCy Schubert  * releases the lock before freeing it and the buffer. */
3132b15cb3dSCy Schubert void evbuffer_decref_and_unlock_(struct evbuffer *buffer);
3142b15cb3dSCy Schubert 
3152b15cb3dSCy Schubert /** As evbuffer_expand, but does not guarantee that the newly allocated memory
3162b15cb3dSCy Schubert  * is contiguous.  Instead, it may be split across two or more chunks. */
3172b15cb3dSCy Schubert int evbuffer_expand_fast_(struct evbuffer *, size_t, int);
3182b15cb3dSCy Schubert 
3192b15cb3dSCy Schubert /** Helper: prepares for a readv/WSARecv call by expanding the buffer to
3202b15cb3dSCy Schubert  * hold enough memory to read 'howmuch' bytes in possibly noncontiguous memory.
3212b15cb3dSCy Schubert  * Sets up the one or two iovecs in 'vecs' to point to the free memory and its
3222b15cb3dSCy Schubert  * extent, and *chainp to point to the first chain that we'll try to read into.
3232b15cb3dSCy Schubert  * Returns the number of vecs used.
3242b15cb3dSCy Schubert  */
3252b15cb3dSCy Schubert int evbuffer_read_setup_vecs_(struct evbuffer *buf, ev_ssize_t howmuch,
3262b15cb3dSCy Schubert     struct evbuffer_iovec *vecs, int n_vecs, struct evbuffer_chain ***chainp,
3272b15cb3dSCy Schubert     int exact);
3282b15cb3dSCy Schubert 
3292b15cb3dSCy Schubert /* Helper macro: copies an evbuffer_iovec in ei to a win32 WSABUF in i. */
3302b15cb3dSCy Schubert #define WSABUF_FROM_EVBUFFER_IOV(i,ei) do {		\
3312b15cb3dSCy Schubert 		(i)->buf = (ei)->iov_base;		\
3322b15cb3dSCy Schubert 		(i)->len = (unsigned long)(ei)->iov_len;	\
3332b15cb3dSCy Schubert 	} while (0)
3342b15cb3dSCy Schubert /* XXXX the cast above is safe for now, but not if we allow mmaps on win64.
3352b15cb3dSCy Schubert  * See note in buffer_iocp's launch_write function */
3362b15cb3dSCy Schubert 
3372b15cb3dSCy Schubert /** Set the parent bufferevent object for buf to bev */
3382b15cb3dSCy Schubert void evbuffer_set_parent_(struct evbuffer *buf, struct bufferevent *bev);
3392b15cb3dSCy Schubert 
3402b15cb3dSCy Schubert void evbuffer_invoke_callbacks_(struct evbuffer *buf);
3412b15cb3dSCy Schubert 
3422b15cb3dSCy Schubert 
3432b15cb3dSCy Schubert int evbuffer_get_callbacks_(struct evbuffer *buffer,
3442b15cb3dSCy Schubert     struct event_callback **cbs,
3452b15cb3dSCy Schubert     int max_cbs);
3462b15cb3dSCy Schubert 
3472b15cb3dSCy Schubert #ifdef __cplusplus
3482b15cb3dSCy Schubert }
3492b15cb3dSCy Schubert #endif
3502b15cb3dSCy Schubert 
3512b15cb3dSCy Schubert #endif /* EVBUFFER_INTERNAL_H_INCLUDED_ */
352