xref: /netbsd-src/external/bsd/ntp/dist/sntp/libevent/evbuffer-internal.h (revision eabc0478de71e4e011a5b4e0392741e01d491794)
1*eabc0478Schristos /*	$NetBSD: evbuffer-internal.h,v 1.7 2024/08/18 20:47:21 christos Exp $	*/
28585484eSchristos 
38585484eSchristos /*
48585484eSchristos  * Copyright (c) 2000-2007 Niels Provos <provos@citi.umich.edu>
58585484eSchristos  * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
68585484eSchristos  *
78585484eSchristos  * Redistribution and use in source and binary forms, with or without
88585484eSchristos  * modification, are permitted provided that the following conditions
98585484eSchristos  * are met:
108585484eSchristos  * 1. Redistributions of source code must retain the above copyright
118585484eSchristos  *    notice, this list of conditions and the following disclaimer.
128585484eSchristos  * 2. Redistributions in binary form must reproduce the above copyright
138585484eSchristos  *    notice, this list of conditions and the following disclaimer in the
148585484eSchristos  *    documentation and/or other materials provided with the distribution.
158585484eSchristos  * 3. The name of the author may not be used to endorse or promote products
168585484eSchristos  *    derived from this software without specific prior written permission.
178585484eSchristos  *
188585484eSchristos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
198585484eSchristos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
208585484eSchristos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
218585484eSchristos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
228585484eSchristos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
238585484eSchristos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
248585484eSchristos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
258585484eSchristos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
268585484eSchristos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
278585484eSchristos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
288585484eSchristos  */
298585484eSchristos #ifndef EVBUFFER_INTERNAL_H_INCLUDED_
308585484eSchristos #define EVBUFFER_INTERNAL_H_INCLUDED_
318585484eSchristos 
328585484eSchristos #ifdef __cplusplus
338585484eSchristos extern "C" {
348585484eSchristos #endif
358585484eSchristos 
368585484eSchristos #include "event2/event-config.h"
378585484eSchristos #include "evconfig-private.h"
388585484eSchristos #include "event2/util.h"
398585484eSchristos #include "event2/event_struct.h"
408585484eSchristos #include "util-internal.h"
418585484eSchristos #include "defer-internal.h"
428585484eSchristos 
438585484eSchristos /* Experimental cb flag: "never deferred."  Implementation note:
448585484eSchristos  * these callbacks may get an inaccurate view of n_del/n_added in their
458585484eSchristos  * arguments. */
468585484eSchristos #define EVBUFFER_CB_NODEFER 2
478585484eSchristos 
488585484eSchristos #ifdef _WIN32
498585484eSchristos #include <winsock2.h>
508585484eSchristos #endif
518585484eSchristos #include <sys/queue.h>
528585484eSchristos 
538585484eSchristos /* Minimum allocation for a chain.  We define this so that we're burning no
548585484eSchristos  * more than 5% of each allocation on overhead.  It would be nice to lose even
558585484eSchristos  * less space, though. */
568585484eSchristos #if EVENT__SIZEOF_VOID_P < 8
578585484eSchristos #define MIN_BUFFER_SIZE	512
588585484eSchristos #else
598585484eSchristos #define MIN_BUFFER_SIZE	1024
608585484eSchristos #endif
618585484eSchristos 
628585484eSchristos /** A single evbuffer callback for an evbuffer. This function will be invoked
638585484eSchristos  * when bytes are added to or removed from the evbuffer. */
648585484eSchristos struct evbuffer_cb_entry {
658585484eSchristos 	/** Structures to implement a doubly-linked queue of callbacks */
668585484eSchristos 	LIST_ENTRY(evbuffer_cb_entry) next;
678585484eSchristos 	/** The callback function to invoke when this callback is called.
688585484eSchristos 	    If EVBUFFER_CB_OBSOLETE is set in flags, the cb_obsolete field is
698585484eSchristos 	    valid; otherwise, cb_func is valid. */
708585484eSchristos 	union {
718585484eSchristos 		evbuffer_cb_func cb_func;
728585484eSchristos 		evbuffer_cb cb_obsolete;
738585484eSchristos 	} cb;
748585484eSchristos 	/** Argument to pass to cb. */
758585484eSchristos 	void *cbarg;
768585484eSchristos 	/** Currently set flags on this callback. */
778585484eSchristos 	ev_uint32_t flags;
788585484eSchristos };
798585484eSchristos 
808585484eSchristos struct bufferevent;
818585484eSchristos struct evbuffer_chain;
828585484eSchristos struct evbuffer {
838585484eSchristos 	/** The first chain in this buffer's linked list of chains. */
848585484eSchristos 	struct evbuffer_chain *first;
858585484eSchristos 	/** The last chain in this buffer's linked list of chains. */
868585484eSchristos 	struct evbuffer_chain *last;
878585484eSchristos 
888585484eSchristos 	/** Pointer to the next pointer pointing at the 'last_with_data' chain.
898585484eSchristos 	 *
908585484eSchristos 	 * To unpack:
918585484eSchristos 	 *
928585484eSchristos 	 * The last_with_data chain is the last chain that has any data in it.
938585484eSchristos 	 * If all chains in the buffer are empty, it is the first chain.
948585484eSchristos 	 * If the buffer has no chains, it is NULL.
958585484eSchristos 	 *
968585484eSchristos 	 * The last_with_datap pointer points at _whatever 'next' pointer_
97*eabc0478Schristos 	 * pointing at the last_with_data chain. If the last_with_data chain
988585484eSchristos 	 * is the first chain, or it is NULL, then the last_with_datap pointer
998585484eSchristos 	 * is &buf->first.
1008585484eSchristos 	 */
1018585484eSchristos 	struct evbuffer_chain **last_with_datap;
1028585484eSchristos 
1038585484eSchristos 	/** Total amount of bytes stored in all chains.*/
1048585484eSchristos 	size_t total_len;
1058585484eSchristos 
1068585484eSchristos 	/** Number of bytes we have added to the buffer since we last tried to
1078585484eSchristos 	 * invoke callbacks. */
1088585484eSchristos 	size_t n_add_for_cb;
1098585484eSchristos 	/** Number of bytes we have removed from the buffer since we last
1108585484eSchristos 	 * tried to invoke callbacks. */
1118585484eSchristos 	size_t n_del_for_cb;
1128585484eSchristos 
1138585484eSchristos #ifndef EVENT__DISABLE_THREAD_SUPPORT
1148585484eSchristos 	/** A lock used to mediate access to this buffer. */
1158585484eSchristos 	void *lock;
1168585484eSchristos #endif
1178585484eSchristos 	/** True iff we should free the lock field when we free this
1188585484eSchristos 	 * evbuffer. */
1198585484eSchristos 	unsigned own_lock : 1;
1208585484eSchristos 	/** True iff we should not allow changes to the front of the buffer
1218585484eSchristos 	 * (drains or prepends). */
1228585484eSchristos 	unsigned freeze_start : 1;
1238585484eSchristos 	/** True iff we should not allow changes to the end of the buffer
1248585484eSchristos 	 * (appends) */
1258585484eSchristos 	unsigned freeze_end : 1;
1268585484eSchristos 	/** True iff this evbuffer's callbacks are not invoked immediately
1278585484eSchristos 	 * upon a change in the buffer, but instead are deferred to be invoked
1288585484eSchristos 	 * from the event_base's loop.	Useful for preventing enormous stack
1298585484eSchristos 	 * overflows when we have mutually recursive callbacks, and for
1308585484eSchristos 	 * serializing callbacks in a single thread. */
1318585484eSchristos 	unsigned deferred_cbs : 1;
1328585484eSchristos #ifdef _WIN32
1338585484eSchristos 	/** True iff this buffer is set up for overlapped IO. */
1348585484eSchristos 	unsigned is_overlapped : 1;
1358585484eSchristos #endif
1368585484eSchristos 	/** Zero or more EVBUFFER_FLAG_* bits */
1378585484eSchristos 	ev_uint32_t flags;
1388585484eSchristos 
1398585484eSchristos 	/** Used to implement deferred callbacks. */
1408585484eSchristos 	struct event_base *cb_queue;
1418585484eSchristos 
1428585484eSchristos 	/** A reference count on this evbuffer.	 When the reference count
1438585484eSchristos 	 * reaches 0, the buffer is destroyed.	Manipulated with
1448585484eSchristos 	 * evbuffer_incref and evbuffer_decref_and_unlock and
1458585484eSchristos 	 * evbuffer_free. */
1468585484eSchristos 	int refcnt;
1478585484eSchristos 
1488585484eSchristos 	/** A struct event_callback handle to make all of this buffer's callbacks
1498585484eSchristos 	 * invoked from the event loop. */
1508585484eSchristos 	struct event_callback deferred;
1518585484eSchristos 
1528585484eSchristos 	/** A doubly-linked-list of callback functions */
1538585484eSchristos 	LIST_HEAD(evbuffer_cb_queue, evbuffer_cb_entry) callbacks;
1548585484eSchristos 
1558585484eSchristos 	/** The parent bufferevent object this evbuffer belongs to.
1568585484eSchristos 	 * NULL if the evbuffer stands alone. */
1578585484eSchristos 	struct bufferevent *parent;
1588585484eSchristos };
1598585484eSchristos 
1607476e6e4Schristos #if EVENT__SIZEOF_OFF_T < EVENT__SIZEOF_SIZE_T
1617476e6e4Schristos typedef ev_ssize_t ev_misalign_t;
1627476e6e4Schristos #define EVBUFFER_CHAIN_MAX ((size_t)EV_SSIZE_MAX)
1637476e6e4Schristos #else
1647476e6e4Schristos typedef ev_off_t ev_misalign_t;
1657476e6e4Schristos #if EVENT__SIZEOF_OFF_T > EVENT__SIZEOF_SIZE_T
1667476e6e4Schristos #define EVBUFFER_CHAIN_MAX EV_SIZE_MAX
1677476e6e4Schristos #else
1687476e6e4Schristos #define EVBUFFER_CHAIN_MAX ((size_t)EV_SSIZE_MAX)
1697476e6e4Schristos #endif
1707476e6e4Schristos #endif
1717476e6e4Schristos 
1728585484eSchristos /** A single item in an evbuffer. */
1738585484eSchristos struct evbuffer_chain {
1748585484eSchristos 	/** points to next buffer in the chain */
1758585484eSchristos 	struct evbuffer_chain *next;
1768585484eSchristos 
1778585484eSchristos 	/** total allocation available in the buffer field. */
1788585484eSchristos 	size_t buffer_len;
1798585484eSchristos 
1808585484eSchristos 	/** unused space at the beginning of buffer or an offset into a
1818585484eSchristos 	 * file for sendfile buffers. */
1827476e6e4Schristos 	ev_misalign_t misalign;
1838585484eSchristos 
1848585484eSchristos 	/** Offset into buffer + misalign at which to start writing.
1858585484eSchristos 	 * In other words, the total number of bytes actually stored
1868585484eSchristos 	 * in buffer. */
1878585484eSchristos 	size_t off;
1888585484eSchristos 
1898585484eSchristos 	/** Set if special handling is required for this chain */
1908585484eSchristos 	unsigned flags;
1918585484eSchristos #define EVBUFFER_FILESEGMENT	0x0001  /**< A chain used for a file segment */
1928585484eSchristos #define EVBUFFER_SENDFILE	0x0002	/**< a chain used with sendfile */
1938585484eSchristos #define EVBUFFER_REFERENCE	0x0004	/**< a chain with a mem reference */
1948585484eSchristos #define EVBUFFER_IMMUTABLE	0x0008	/**< read-only chain */
1958585484eSchristos 	/** a chain that mustn't be reallocated or freed, or have its contents
1968585484eSchristos 	 * memmoved, until the chain is un-pinned. */
1978585484eSchristos #define EVBUFFER_MEM_PINNED_R	0x0010
1988585484eSchristos #define EVBUFFER_MEM_PINNED_W	0x0020
1998585484eSchristos #define EVBUFFER_MEM_PINNED_ANY (EVBUFFER_MEM_PINNED_R|EVBUFFER_MEM_PINNED_W)
2008585484eSchristos 	/** a chain that should be freed, but can't be freed until it is
2018585484eSchristos 	 * un-pinned. */
2028585484eSchristos #define EVBUFFER_DANGLING	0x0040
2038585484eSchristos 	/** a chain that is a referenced copy of another chain */
2048585484eSchristos #define EVBUFFER_MULTICAST	0x0080
2058585484eSchristos 
2068585484eSchristos 	/** number of references to this chain */
2078585484eSchristos 	int refcnt;
2088585484eSchristos 
2098585484eSchristos 	/** Usually points to the read-write memory belonging to this
2108585484eSchristos 	 * buffer allocated as part of the evbuffer_chain allocation.
2118585484eSchristos 	 * For mmap, this can be a read-only buffer and
2128585484eSchristos 	 * EVBUFFER_IMMUTABLE will be set in flags.  For sendfile, it
2138585484eSchristos 	 * may point to NULL.
2148585484eSchristos 	 */
2158585484eSchristos 	unsigned char *buffer;
2168585484eSchristos };
2178585484eSchristos 
2188585484eSchristos /** callback for a reference chain; lets us know what to do with it when
2198585484eSchristos  * we're done with it. Lives at the end of an evbuffer_chain with the
2208585484eSchristos  * EVBUFFER_REFERENCE flag set */
2218585484eSchristos struct evbuffer_chain_reference {
2228585484eSchristos 	evbuffer_ref_cleanup_cb cleanupfn;
2238585484eSchristos 	void *extra;
2248585484eSchristos };
2258585484eSchristos 
2268585484eSchristos /** File segment for a file-segment chain.  Lives at the end of an
2278585484eSchristos  * evbuffer_chain with the EVBUFFER_FILESEGMENT flag set.  */
2288585484eSchristos struct evbuffer_chain_file_segment {
2298585484eSchristos 	struct evbuffer_file_segment *segment;
2308585484eSchristos #ifdef _WIN32
2318585484eSchristos 	/** If we're using CreateFileMapping, this is the handle to the view. */
2328585484eSchristos 	HANDLE view_handle;
2338585484eSchristos #endif
2348585484eSchristos };
2358585484eSchristos 
2368585484eSchristos /* Declared in event2/buffer.h; defined here. */
2378585484eSchristos struct evbuffer_file_segment {
2388585484eSchristos 	void *lock; /**< lock prevent concurrent access to refcnt */
2398585484eSchristos 	int refcnt; /**< Reference count for this file segment */
2408585484eSchristos 	unsigned flags; /**< combination of EVBUF_FS_* flags  */
2418585484eSchristos 
2428585484eSchristos 	/** What kind of file segment is this? */
2438585484eSchristos 	unsigned can_sendfile : 1;
2448585484eSchristos 	unsigned is_mapping : 1;
2458585484eSchristos 
2468585484eSchristos 	/** The fd that we read the data from. */
2478585484eSchristos 	int fd;
2488585484eSchristos 	/** If we're using mmap, this is the raw mapped memory. */
2498585484eSchristos 	void *mapping;
2508585484eSchristos #ifdef _WIN32
2518585484eSchristos 	/** If we're using CreateFileMapping, this is the mapping */
2528585484eSchristos 	HANDLE mapping_handle;
2538585484eSchristos #endif
2548585484eSchristos 	/** If we're using mmap or IO, this is the content of the file
2558585484eSchristos 	 * segment. */
2568585484eSchristos 	char *contents;
2578585484eSchristos 	/** Position of this segment within the file. */
2588585484eSchristos 	ev_off_t file_offset;
2598585484eSchristos 	/** If we're using mmap, this is the offset within 'mapping' where
2608585484eSchristos 	 * this data segment begins. */
2618585484eSchristos 	ev_off_t mmap_offset;
2628585484eSchristos 	/** The length of this segment. */
2638585484eSchristos 	ev_off_t length;
2648585484eSchristos 	/** Cleanup callback function */
2658585484eSchristos 	evbuffer_file_segment_cleanup_cb cleanup_cb;
2668585484eSchristos 	/** Argument to be pass to cleanup callback function */
2678585484eSchristos 	void *cleanup_cb_arg;
2688585484eSchristos };
2698585484eSchristos 
2708585484eSchristos /** Information about the multicast parent of a chain.  Lives at the
2718585484eSchristos  * end of an evbuffer_chain with the EVBUFFER_MULTICAST flag set.  */
2728585484eSchristos struct evbuffer_multicast_parent {
2738585484eSchristos 	/** source buffer the multicast parent belongs to */
2748585484eSchristos 	struct evbuffer *source;
2758585484eSchristos 	/** multicast parent for this chain */
2768585484eSchristos 	struct evbuffer_chain *parent;
2778585484eSchristos };
2788585484eSchristos 
2798585484eSchristos #define EVBUFFER_CHAIN_SIZE sizeof(struct evbuffer_chain)
2808585484eSchristos /** Return a pointer to extra data allocated along with an evbuffer. */
2818585484eSchristos #define EVBUFFER_CHAIN_EXTRA(t, c) (t *)((struct evbuffer_chain *)(c) + 1)
2828585484eSchristos 
2838585484eSchristos /** Assert that we are holding the lock on an evbuffer */
2848585484eSchristos #define ASSERT_EVBUFFER_LOCKED(buffer)			\
2858585484eSchristos 	EVLOCK_ASSERT_LOCKED((buffer)->lock)
2868585484eSchristos 
2878585484eSchristos #define EVBUFFER_LOCK(buffer)						\
2888585484eSchristos 	do {								\
2898585484eSchristos 		EVLOCK_LOCK((buffer)->lock, 0);				\
2908585484eSchristos 	} while (0)
2918585484eSchristos #define EVBUFFER_UNLOCK(buffer)						\
2928585484eSchristos 	do {								\
2938585484eSchristos 		EVLOCK_UNLOCK((buffer)->lock, 0);			\
2948585484eSchristos 	} while (0)
2958585484eSchristos #define EVBUFFER_LOCK2(buffer1, buffer2)				\
2968585484eSchristos 	do {								\
2978585484eSchristos 		EVLOCK_LOCK2((buffer1)->lock, (buffer2)->lock, 0, 0);	\
2988585484eSchristos 	} while (0)
2998585484eSchristos #define EVBUFFER_UNLOCK2(buffer1, buffer2)				\
3008585484eSchristos 	do {								\
3018585484eSchristos 		EVLOCK_UNLOCK2((buffer1)->lock, (buffer2)->lock, 0, 0);	\
3028585484eSchristos 	} while (0)
3038585484eSchristos 
3048585484eSchristos /** Increase the reference count of buf by one. */
3058585484eSchristos void evbuffer_incref_(struct evbuffer *buf);
3068585484eSchristos /** Increase the reference count of buf by one and acquire the lock. */
3078585484eSchristos void evbuffer_incref_and_lock_(struct evbuffer *buf);
3088585484eSchristos /** Pin a single buffer chain using a given flag. A pinned chunk may not be
3098585484eSchristos  * moved or freed until it is unpinned. */
3108585484eSchristos void evbuffer_chain_pin_(struct evbuffer_chain *chain, unsigned flag);
3118585484eSchristos /** Unpin a single buffer chain using a given flag. */
3128585484eSchristos void evbuffer_chain_unpin_(struct evbuffer_chain *chain, unsigned flag);
3138585484eSchristos /** As evbuffer_free, but requires that we hold a lock on the buffer, and
3148585484eSchristos  * releases the lock before freeing it and the buffer. */
3158585484eSchristos void evbuffer_decref_and_unlock_(struct evbuffer *buffer);
3168585484eSchristos 
3178585484eSchristos /** As evbuffer_expand, but does not guarantee that the newly allocated memory
3188585484eSchristos  * is contiguous.  Instead, it may be split across two or more chunks. */
3198585484eSchristos int evbuffer_expand_fast_(struct evbuffer *, size_t, int);
3208585484eSchristos 
3218585484eSchristos /** Helper: prepares for a readv/WSARecv call by expanding the buffer to
3228585484eSchristos  * hold enough memory to read 'howmuch' bytes in possibly noncontiguous memory.
3238585484eSchristos  * Sets up the one or two iovecs in 'vecs' to point to the free memory and its
3248585484eSchristos  * extent, and *chainp to point to the first chain that we'll try to read into.
3258585484eSchristos  * Returns the number of vecs used.
3268585484eSchristos  */
3278585484eSchristos int evbuffer_read_setup_vecs_(struct evbuffer *buf, ev_ssize_t howmuch,
3288585484eSchristos     struct evbuffer_iovec *vecs, int n_vecs, struct evbuffer_chain ***chainp,
3298585484eSchristos     int exact);
3308585484eSchristos 
3318585484eSchristos /* Helper macro: copies an evbuffer_iovec in ei to a win32 WSABUF in i. */
3328585484eSchristos #define WSABUF_FROM_EVBUFFER_IOV(i,ei) do {		\
3338585484eSchristos 		(i)->buf = (ei)->iov_base;		\
3348585484eSchristos 		(i)->len = (unsigned long)(ei)->iov_len;	\
3358585484eSchristos 	} while (0)
3368585484eSchristos /* XXXX the cast above is safe for now, but not if we allow mmaps on win64.
3378585484eSchristos  * See note in buffer_iocp's launch_write function */
3388585484eSchristos 
3398585484eSchristos /** Set the parent bufferevent object for buf to bev */
3408585484eSchristos void evbuffer_set_parent_(struct evbuffer *buf, struct bufferevent *bev);
3418585484eSchristos 
3428585484eSchristos void evbuffer_invoke_callbacks_(struct evbuffer *buf);
3438585484eSchristos 
344b8ecfcfeSchristos 
345b8ecfcfeSchristos int evbuffer_get_callbacks_(struct evbuffer *buffer,
346b8ecfcfeSchristos     struct event_callback **cbs,
347b8ecfcfeSchristos     int max_cbs);
348b8ecfcfeSchristos 
3498585484eSchristos #ifdef __cplusplus
3508585484eSchristos }
3518585484eSchristos #endif
3528585484eSchristos 
3538585484eSchristos #endif /* EVBUFFER_INTERNAL_H_INCLUDED_ */
354