xref: /libevent-2.1.12/evbuffer-internal.h (revision caceb8f5)
15c70ea4cSNiels Provos /*
2b85b710cSNick Mathewson  * Copyright (c) 2000-2007 Niels Provos <[email protected]>
3e49e2891SNick Mathewson  * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
45c70ea4cSNiels Provos  *
55c70ea4cSNiels Provos  * Redistribution and use in source and binary forms, with or without
65c70ea4cSNiels Provos  * modification, are permitted provided that the following conditions
75c70ea4cSNiels Provos  * are met:
85c70ea4cSNiels Provos  * 1. Redistributions of source code must retain the above copyright
95c70ea4cSNiels Provos  *    notice, this list of conditions and the following disclaimer.
105c70ea4cSNiels Provos  * 2. Redistributions in binary form must reproduce the above copyright
115c70ea4cSNiels Provos  *    notice, this list of conditions and the following disclaimer in the
125c70ea4cSNiels Provos  *    documentation and/or other materials provided with the distribution.
135c70ea4cSNiels Provos  * 3. The name of the author may not be used to endorse or promote products
145c70ea4cSNiels Provos  *    derived from this software without specific prior written permission.
155c70ea4cSNiels Provos  *
165c70ea4cSNiels Provos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
175c70ea4cSNiels Provos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
185c70ea4cSNiels Provos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
195c70ea4cSNiels Provos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
205c70ea4cSNiels Provos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
215c70ea4cSNiels Provos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
225c70ea4cSNiels Provos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
235c70ea4cSNiels Provos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
245c70ea4cSNiels Provos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
255c70ea4cSNiels Provos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
265c70ea4cSNiels Provos  */
273f8c7cd0SNick Mathewson #ifndef EVBUFFER_INTERNAL_H_INCLUDED_
283f8c7cd0SNick Mathewson #define EVBUFFER_INTERNAL_H_INCLUDED_
295c70ea4cSNiels Provos 
305c70ea4cSNiels Provos #ifdef __cplusplus
315c70ea4cSNiels Provos extern "C" {
325c70ea4cSNiels Provos #endif
335c70ea4cSNiels Provos 
34ec347b92SNick Mathewson #include "event2/event-config.h"
350915ca0aSKevin Bowling #include "evconfig-private.h"
36e865eb93SNick Mathewson #include "event2/util.h"
37ae2b84b2SNick Mathewson #include "event2/event_struct.h"
38e865eb93SNick Mathewson #include "util-internal.h"
39b29b875dSNick Mathewson #include "defer-internal.h"
405c70ea4cSNiels Provos 
4129151e65SNick Mathewson /* Experimental cb flag: "never deferred."  Implementation note:
4229151e65SNick Mathewson  * these callbacks may get an inaccurate view of n_del/n_added in their
4329151e65SNick Mathewson  * arguments. */
44390e0561SNick Mathewson #define EVBUFFER_CB_NODEFER 2
45390e0561SNick Mathewson 
469f560bfaSNick Mathewson #ifdef _WIN32
47e865eb93SNick Mathewson #include <winsock2.h>
48e865eb93SNick Mathewson #endif
49c735f2b4SNick Mathewson #include <sys/queue.h>
502014ae4aSNick Mathewson 
512014ae4aSNick Mathewson /* Minimum allocation for a chain.  We define this so that we're burning no
522014ae4aSNick Mathewson  * more than 5% of each allocation on overhead.  It would be nice to lose even
532014ae4aSNick Mathewson  * less space, though. */
5468120d9bSNick Mathewson #if EVENT__SIZEOF_VOID_P < 8
552014ae4aSNick Mathewson #define MIN_BUFFER_SIZE	512
562014ae4aSNick Mathewson #else
572014ae4aSNick Mathewson #define MIN_BUFFER_SIZE	1024
582014ae4aSNick Mathewson #endif
595c70ea4cSNiels Provos 
60838d0a81SNick Mathewson /** A single evbuffer callback for an evbuffer. This function will be invoked
61838d0a81SNick Mathewson  * when bytes are added to or removed from the evbuffer. */
62c735f2b4SNick Mathewson struct evbuffer_cb_entry {
6398b7046bSNick Mathewson 	/** Structures to implement a doubly-linked queue of callbacks */
64d313c293SNick Mathewson 	LIST_ENTRY(evbuffer_cb_entry) next;
65838d0a81SNick Mathewson 	/** The callback function to invoke when this callback is called.
66838d0a81SNick Mathewson 	    If EVBUFFER_CB_OBSOLETE is set in flags, the cb_obsolete field is
67838d0a81SNick Mathewson 	    valid; otherwise, cb_func is valid. */
68f1b1bad4SNick Mathewson 	union {
69f1b1bad4SNick Mathewson 		evbuffer_cb_func cb_func;
70f1b1bad4SNick Mathewson 		evbuffer_cb cb_obsolete;
71f1b1bad4SNick Mathewson 	} cb;
7298b7046bSNick Mathewson 	/** Argument to pass to cb. */
73c735f2b4SNick Mathewson 	void *cbarg;
7498b7046bSNick Mathewson 	/** Currently set flags on this callback. */
758d3a10f8SNick Mathewson 	ev_uint32_t flags;
76c735f2b4SNick Mathewson };
77c735f2b4SNick Mathewson 
78d7d1f1daSNick Mathewson struct bufferevent;
795c70ea4cSNiels Provos struct evbuffer_chain;
805c70ea4cSNiels Provos struct evbuffer {
81838d0a81SNick Mathewson 	/** The first chain in this buffer's linked list of chains. */
825c70ea4cSNiels Provos 	struct evbuffer_chain *first;
83838d0a81SNick Mathewson 	/** The last chain in this buffer's linked list of chains. */
845c70ea4cSNiels Provos 	struct evbuffer_chain *last;
855c70ea4cSNiels Provos 
86b7442f8eSNick Mathewson 	/** Pointer to the next pointer pointing at the 'last_with_data' chain.
87b7442f8eSNick Mathewson 	 *
88b7442f8eSNick Mathewson 	 * To unpack:
89b7442f8eSNick Mathewson 	 *
90b7442f8eSNick Mathewson 	 * The last_with_data chain is the last chain that has any data in it.
91b7442f8eSNick Mathewson 	 * If all chains in the buffer are empty, it is the first chain.
92b7442f8eSNick Mathewson 	 * If the buffer has no chains, it is NULL.
93b7442f8eSNick Mathewson 	 *
94b7442f8eSNick Mathewson 	 * The last_with_datap pointer points at _whatever 'next' pointer_
95*caceb8f5SMarcin Szewczyk 	 * pointing at the last_with_data chain. If the last_with_data chain
96b7442f8eSNick Mathewson 	 * is the first chain, or it is NULL, then the last_with_datap pointer
97b7442f8eSNick Mathewson 	 * is &buf->first.
98b7442f8eSNick Mathewson 	 */
99b7442f8eSNick Mathewson 	struct evbuffer_chain **last_with_datap;
1002a6d2a1eSNick Mathewson 
101838d0a81SNick Mathewson 	/** Total amount of bytes stored in all chains.*/
102838d0a81SNick Mathewson 	size_t total_len;
1035c70ea4cSNiels Provos 
104838d0a81SNick Mathewson 	/** Number of bytes we have added to the buffer since we last tried to
105838d0a81SNick Mathewson 	 * invoke callbacks. */
106f1b1bad4SNick Mathewson 	size_t n_add_for_cb;
107838d0a81SNick Mathewson 	/** Number of bytes we have removed from the buffer since we last
108838d0a81SNick Mathewson 	 * tried to invoke callbacks. */
109f1b1bad4SNick Mathewson 	size_t n_del_for_cb;
110f1b1bad4SNick Mathewson 
11168120d9bSNick Mathewson #ifndef EVENT__DISABLE_THREAD_SUPPORT
112838d0a81SNick Mathewson 	/** A lock used to mediate access to this buffer. */
11360e0d59bSNick Mathewson 	void *lock;
11460e0d59bSNick Mathewson #endif
115838d0a81SNick Mathewson 	/** True iff we should free the lock field when we free this
116838d0a81SNick Mathewson 	 * evbuffer. */
11760e0d59bSNick Mathewson 	unsigned own_lock : 1;
118838d0a81SNick Mathewson 	/** True iff we should not allow changes to the front of the buffer
119838d0a81SNick Mathewson 	 * (drains or prepends). */
120747331d1SNick Mathewson 	unsigned freeze_start : 1;
121838d0a81SNick Mathewson 	/** True iff we should not allow changes to the end of the buffer
122838d0a81SNick Mathewson 	 * (appends) */
123747331d1SNick Mathewson 	unsigned freeze_end : 1;
124838d0a81SNick Mathewson 	/** True iff this evbuffer's callbacks are not invoked immediately
125838d0a81SNick Mathewson 	 * upon a change in the buffer, but instead are deferred to be invoked
126838d0a81SNick Mathewson 	 * from the event_base's loop.	Useful for preventing enormous stack
127838d0a81SNick Mathewson 	 * overflows when we have mutually recursive callbacks, and for
128838d0a81SNick Mathewson 	 * serializing callbacks in a single thread. */
129b29b875dSNick Mathewson 	unsigned deferred_cbs : 1;
1309f560bfaSNick Mathewson #ifdef _WIN32
131838d0a81SNick Mathewson 	/** True iff this buffer is set up for overlapped IO. */
13293d4f884SNick Mathewson 	unsigned is_overlapped : 1;
13393d4f884SNick Mathewson #endif
1340ba0af9cSNick Mathewson 	/** Zero or more EVBUFFER_FLAG_* bits */
1350ba0af9cSNick Mathewson 	ev_uint32_t flags;
136b29b875dSNick Mathewson 
137b06b2649SNick Mathewson 	/** Used to implement deferred callbacks. */
138a4079aa8SNick Mathewson 	struct event_base *cb_queue;
139d9086fc0SNick Mathewson 
140838d0a81SNick Mathewson 	/** A reference count on this evbuffer.	 When the reference count
141838d0a81SNick Mathewson 	 * reaches 0, the buffer is destroyed.	Manipulated with
142838d0a81SNick Mathewson 	 * evbuffer_incref and evbuffer_decref_and_unlock and
143838d0a81SNick Mathewson 	 * evbuffer_free. */
144dcda7915SNick Mathewson 	int refcnt;
14560e0d59bSNick Mathewson 
146a4079aa8SNick Mathewson 	/** A struct event_callback handle to make all of this buffer's callbacks
147838d0a81SNick Mathewson 	 * invoked from the event loop. */
148a4079aa8SNick Mathewson 	struct event_callback deferred;
149b29b875dSNick Mathewson 
150838d0a81SNick Mathewson 	/** A doubly-linked-list of callback functions */
151d313c293SNick Mathewson 	LIST_HEAD(evbuffer_cb_queue, evbuffer_cb_entry) callbacks;
152d7d1f1daSNick Mathewson 
153d7d1f1daSNick Mathewson 	/** The parent bufferevent object this evbuffer belongs to.
154d7d1f1daSNick Mathewson 	 * NULL if the evbuffer stands alone. */
155d7d1f1daSNick Mathewson 	struct bufferevent *parent;
1565c70ea4cSNiels Provos };
1575c70ea4cSNiels Provos 
158841ecbd9SNick Mathewson #if EVENT__SIZEOF_OFF_T < EVENT__SIZEOF_SIZE_T
159841ecbd9SNick Mathewson typedef ev_ssize_t ev_misalign_t;
160841ecbd9SNick Mathewson #define EVBUFFER_CHAIN_MAX ((size_t)EV_SSIZE_MAX)
161841ecbd9SNick Mathewson #else
162841ecbd9SNick Mathewson typedef ev_off_t ev_misalign_t;
163841ecbd9SNick Mathewson #if EVENT__SIZEOF_OFF_T > EVENT__SIZEOF_SIZE_T
164841ecbd9SNick Mathewson #define EVBUFFER_CHAIN_MAX EV_SIZE_MAX
165841ecbd9SNick Mathewson #else
166841ecbd9SNick Mathewson #define EVBUFFER_CHAIN_MAX ((size_t)EV_SSIZE_MAX)
167841ecbd9SNick Mathewson #endif
168841ecbd9SNick Mathewson #endif
169841ecbd9SNick Mathewson 
170838d0a81SNick Mathewson /** A single item in an evbuffer. */
1715c70ea4cSNiels Provos struct evbuffer_chain {
1725c70ea4cSNiels Provos 	/** points to next buffer in the chain */
1735c70ea4cSNiels Provos 	struct evbuffer_chain *next;
1745c70ea4cSNiels Provos 
175fdf69493SNiels Provos 	/** total allocation available in the buffer field. */
176fdf69493SNiels Provos 	size_t buffer_len;
1775c70ea4cSNiels Provos 
178fdf69493SNiels Provos 	/** unused space at the beginning of buffer or an offset into a
179fdf69493SNiels Provos 	 * file for sendfile buffers. */
180841ecbd9SNick Mathewson 	ev_misalign_t misalign;
181fdf69493SNiels Provos 
182fdf69493SNiels Provos 	/** Offset into buffer + misalign at which to start writing.
1830322ce0aSNick Mathewson 	 * In other words, the total number of bytes actually stored
1840322ce0aSNick Mathewson 	 * in buffer. */
185fdf69493SNiels Provos 	size_t off;
1865c70ea4cSNiels Provos 
187fdf69493SNiels Provos 	/** Set if special handling is required for this chain */
188fdf69493SNiels Provos 	unsigned flags;
189e72afae0SNick Mathewson #define EVBUFFER_FILESEGMENT	0x0001  /**< A chain used for a file segment */
190e72afae0SNick Mathewson #define EVBUFFER_SENDFILE	0x0002	/**< a chain used with sendfile */
191fdf69493SNiels Provos #define EVBUFFER_REFERENCE	0x0004	/**< a chain with a mem reference */
192fdf69493SNiels Provos #define EVBUFFER_IMMUTABLE	0x0008	/**< read-only chain */
193d9086fc0SNick Mathewson 	/** a chain that mustn't be reallocated or freed, or have its contents
194d9086fc0SNick Mathewson 	 * memmoved, until the chain is un-pinned. */
195d9086fc0SNick Mathewson #define EVBUFFER_MEM_PINNED_R	0x0010
196d9086fc0SNick Mathewson #define EVBUFFER_MEM_PINNED_W	0x0020
197d9086fc0SNick Mathewson #define EVBUFFER_MEM_PINNED_ANY (EVBUFFER_MEM_PINNED_R|EVBUFFER_MEM_PINNED_W)
198d9086fc0SNick Mathewson 	/** a chain that should be freed, but can't be freed until it is
199d9086fc0SNick Mathewson 	 * un-pinned. */
200d9086fc0SNick Mathewson #define EVBUFFER_DANGLING	0x0040
2019d7368aeSJoachim Bauch 	/** a chain that is a referenced copy of another chain */
2029d7368aeSJoachim Bauch #define EVBUFFER_MULTICAST	0x0080
2039d7368aeSJoachim Bauch 
204a8e5e2fcSJoachim Bauch 	/** number of references to this chain */
2059d7368aeSJoachim Bauch 	int refcnt;
206fdf69493SNiels Provos 
207fdf69493SNiels Provos 	/** Usually points to the read-write memory belonging to this
208fdf69493SNiels Provos 	 * buffer allocated as part of the evbuffer_chain allocation.
209fdf69493SNiels Provos 	 * For mmap, this can be a read-only buffer and
210fdf69493SNiels Provos 	 * EVBUFFER_IMMUTABLE will be set in flags.  For sendfile, it
211fdf69493SNiels Provos 	 * may point to NULL.
212fdf69493SNiels Provos 	 */
21393d4f884SNick Mathewson 	unsigned char *buffer;
2145c70ea4cSNiels Provos };
2155c70ea4cSNiels Provos 
216e72afae0SNick Mathewson /** callback for a reference chain; lets us know what to do with it when
217e72afae0SNick Mathewson  * we're done with it. Lives at the end of an evbuffer_chain with the
218e72afae0SNick Mathewson  * EVBUFFER_REFERENCE flag set */
219fdf69493SNiels Provos struct evbuffer_chain_reference {
220dc4c7b95SNick Mathewson 	evbuffer_ref_cleanup_cb cleanupfn;
221fdf69493SNiels Provos 	void *extra;
222fdf69493SNiels Provos };
223fdf69493SNiels Provos 
224e72afae0SNick Mathewson /** File segment for a file-segment chain.  Lives at the end of an
225e72afae0SNick Mathewson  * evbuffer_chain with the EVBUFFER_FILESEGMENT flag set.  */
226e72afae0SNick Mathewson struct evbuffer_chain_file_segment {
227e72afae0SNick Mathewson 	struct evbuffer_file_segment *segment;
2289f560bfaSNick Mathewson #ifdef _WIN32
2293f405d2dSNick Mathewson 	/** If we're using CreateFileMapping, this is the handle to the view. */
2303f405d2dSNick Mathewson 	HANDLE view_handle;
2313f405d2dSNick Mathewson #endif
232e72afae0SNick Mathewson };
233e72afae0SNick Mathewson 
234e72afae0SNick Mathewson /* Declared in event2/buffer.h; defined here. */
235e72afae0SNick Mathewson struct evbuffer_file_segment {
236e72afae0SNick Mathewson 	void *lock; /**< lock prevent concurrent access to refcnt */
237e72afae0SNick Mathewson 	int refcnt; /**< Reference count for this file segment */
238e72afae0SNick Mathewson 	unsigned flags; /**< combination of EVBUF_FS_* flags  */
239e72afae0SNick Mathewson 
240e72afae0SNick Mathewson 	/** What kind of file segment is this? */
241c6bbbf1bSNick Mathewson 	unsigned can_sendfile : 1;
242c6bbbf1bSNick Mathewson 	unsigned is_mapping : 1;
243e72afae0SNick Mathewson 
244e72afae0SNick Mathewson 	/** The fd that we read the data from. */
245e72afae0SNick Mathewson 	int fd;
246e72afae0SNick Mathewson 	/** If we're using mmap, this is the raw mapped memory. */
247e72afae0SNick Mathewson 	void *mapping;
2489f560bfaSNick Mathewson #ifdef _WIN32
2493f405d2dSNick Mathewson 	/** If we're using CreateFileMapping, this is the mapping */
2503f405d2dSNick Mathewson 	HANDLE mapping_handle;
2513f405d2dSNick Mathewson #endif
252e72afae0SNick Mathewson 	/** If we're using mmap or IO, this is the content of the file
253e72afae0SNick Mathewson 	 * segment. */
254e72afae0SNick Mathewson 	char *contents;
255c6bbbf1bSNick Mathewson 	/** Position of this segment within the file. */
256c6bbbf1bSNick Mathewson 	ev_off_t file_offset;
257e72afae0SNick Mathewson 	/** If we're using mmap, this is the offset within 'mapping' where
258c6bbbf1bSNick Mathewson 	 * this data segment begins. */
259c6bbbf1bSNick Mathewson 	ev_off_t mmap_offset;
260e72afae0SNick Mathewson 	/** The length of this segment. */
2613f405d2dSNick Mathewson 	ev_off_t length;
262e9f8febaSyangacer 	/** Cleanup callback function */
263e9f8febaSyangacer 	evbuffer_file_segment_cleanup_cb cleanup_cb;
264e9f8febaSyangacer 	/** Argument to be pass to cleanup callback function */
265e9f8febaSyangacer 	void *cleanup_cb_arg;
266e72afae0SNick Mathewson };
267e72afae0SNick Mathewson 
2689d7368aeSJoachim Bauch /** Information about the multicast parent of a chain.  Lives at the
2699d7368aeSJoachim Bauch  * end of an evbuffer_chain with the EVBUFFER_MULTICAST flag set.  */
2709d7368aeSJoachim Bauch struct evbuffer_multicast_parent {
27126041a8eSJoachim Bauch 	/** source buffer the multicast parent belongs to */
27226041a8eSJoachim Bauch 	struct evbuffer *source;
2739d7368aeSJoachim Bauch 	/** multicast parent for this chain */
2749d7368aeSJoachim Bauch 	struct evbuffer_chain *parent;
2759d7368aeSJoachim Bauch };
2769d7368aeSJoachim Bauch 
277fdf69493SNiels Provos #define EVBUFFER_CHAIN_SIZE sizeof(struct evbuffer_chain)
278838d0a81SNick Mathewson /** Return a pointer to extra data allocated along with an evbuffer. */
279fdf69493SNiels Provos #define EVBUFFER_CHAIN_EXTRA(t, c) (t *)((struct evbuffer_chain *)(c) + 1)
2805c70ea4cSNiels Provos 
2810cd3bb9fSNick Mathewson /** Assert that we are holding the lock on an evbuffer */
28260e0d59bSNick Mathewson #define ASSERT_EVBUFFER_LOCKED(buffer)			\
2830cd3bb9fSNick Mathewson 	EVLOCK_ASSERT_LOCKED((buffer)->lock)
28460e0d59bSNick Mathewson 
28576cd2b70SNick Mathewson #define EVBUFFER_LOCK(buffer)						\
28660e0d59bSNick Mathewson 	do {								\
28776cd2b70SNick Mathewson 		EVLOCK_LOCK((buffer)->lock, 0);				\
28860e0d59bSNick Mathewson 	} while (0)
28976cd2b70SNick Mathewson #define EVBUFFER_UNLOCK(buffer)						\
29060e0d59bSNick Mathewson 	do {								\
29176cd2b70SNick Mathewson 		EVLOCK_UNLOCK((buffer)->lock, 0);			\
29260e0d59bSNick Mathewson 	} while (0)
29360e0d59bSNick Mathewson #define EVBUFFER_LOCK2(buffer1, buffer2)				\
29460e0d59bSNick Mathewson 	do {								\
29576cd2b70SNick Mathewson 		EVLOCK_LOCK2((buffer1)->lock, (buffer2)->lock, 0, 0);	\
29660e0d59bSNick Mathewson 	} while (0)
29760e0d59bSNick Mathewson #define EVBUFFER_UNLOCK2(buffer1, buffer2)				\
29860e0d59bSNick Mathewson 	do {								\
29976cd2b70SNick Mathewson 		EVLOCK_UNLOCK2((buffer1)->lock, (buffer2)->lock, 0, 0);	\
30060e0d59bSNick Mathewson 	} while (0)
30160e0d59bSNick Mathewson 
302838d0a81SNick Mathewson /** Increase the reference count of buf by one. */
303cb9da0bfSNick Mathewson void evbuffer_incref_(struct evbuffer *buf);
304d7d1f1daSNick Mathewson /** Increase the reference count of buf by one and acquire the lock. */
305cb9da0bfSNick Mathewson void evbuffer_incref_and_lock_(struct evbuffer *buf);
306838d0a81SNick Mathewson /** Pin a single buffer chain using a given flag. A pinned chunk may not be
307838d0a81SNick Mathewson  * moved or freed until it is unpinned. */
308cb9da0bfSNick Mathewson void evbuffer_chain_pin_(struct evbuffer_chain *chain, unsigned flag);
309838d0a81SNick Mathewson /** Unpin a single buffer chain using a given flag. */
310cb9da0bfSNick Mathewson void evbuffer_chain_unpin_(struct evbuffer_chain *chain, unsigned flag);
311838d0a81SNick Mathewson /** As evbuffer_free, but requires that we hold a lock on the buffer, and
312838d0a81SNick Mathewson  * releases the lock before freeing it and the buffer. */
313cb9da0bfSNick Mathewson void evbuffer_decref_and_unlock_(struct evbuffer *buffer);
3148997f234SNick Mathewson 
315838d0a81SNick Mathewson /** As evbuffer_expand, but does not guarantee that the newly allocated memory
316c8ac57f1SNick Mathewson  * is contiguous.  Instead, it may be split across two or more chunks. */
317cb9da0bfSNick Mathewson int evbuffer_expand_fast_(struct evbuffer *, size_t, int);
318dcda7915SNick Mathewson 
319838d0a81SNick Mathewson /** Helper: prepares for a readv/WSARecv call by expanding the buffer to
320838d0a81SNick Mathewson  * hold enough memory to read 'howmuch' bytes in possibly noncontiguous memory.
321838d0a81SNick Mathewson  * Sets up the one or two iovecs in 'vecs' to point to the free memory and its
322e3fd294aSNick Mathewson  * extent, and *chainp to point to the first chain that we'll try to read into.
323838d0a81SNick Mathewson  * Returns the number of vecs used.
324838d0a81SNick Mathewson  */
325cb9da0bfSNick Mathewson int evbuffer_read_setup_vecs_(struct evbuffer *buf, ev_ssize_t howmuch,
32603afa209SChristopher Davis     struct evbuffer_iovec *vecs, int n_vecs, struct evbuffer_chain ***chainp,
32703afa209SChristopher Davis     int exact);
328829b52b6SNick Mathewson 
3298997f234SNick Mathewson /* Helper macro: copies an evbuffer_iovec in ei to a win32 WSABUF in i. */
3308997f234SNick Mathewson #define WSABUF_FROM_EVBUFFER_IOV(i,ei) do {		\
3318997f234SNick Mathewson 		(i)->buf = (ei)->iov_base;		\
3327484df61SNick Mathewson 		(i)->len = (unsigned long)(ei)->iov_len;	\
3338997f234SNick Mathewson 	} while (0)
3347484df61SNick Mathewson /* XXXX the cast above is safe for now, but not if we allow mmaps on win64.
3357484df61SNick Mathewson  * See note in buffer_iocp's launch_write function */
3368997f234SNick Mathewson 
337d7d1f1daSNick Mathewson /** Set the parent bufferevent object for buf to bev */
3388ac3c4c2SNick Mathewson void evbuffer_set_parent_(struct evbuffer *buf, struct bufferevent *bev);
339d7d1f1daSNick Mathewson 
3408ac3c4c2SNick Mathewson void evbuffer_invoke_callbacks_(struct evbuffer *buf);
3416acfbdd8SNick Mathewson 
34202fbf687SNick Mathewson 
34302fbf687SNick Mathewson int evbuffer_get_callbacks_(struct evbuffer *buffer,
34402fbf687SNick Mathewson     struct event_callback **cbs,
34502fbf687SNick Mathewson     int max_cbs);
34602fbf687SNick Mathewson 
3475c70ea4cSNiels Provos #ifdef __cplusplus
3485c70ea4cSNiels Provos }
3495c70ea4cSNiels Provos #endif
3505c70ea4cSNiels Provos 
3513f8c7cd0SNick Mathewson #endif /* EVBUFFER_INTERNAL_H_INCLUDED_ */
352