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