1/*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1982, 1986, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#ifndef _SYS__IOVEC_H_
33#define _SYS__IOVEC_H_
34
35#include <sys/_types.h>
36
37#ifndef _SIZE_T_DECLARED
38typedef __size_t size_t;
39#define _SIZE_T_DECLARED
40#endif
41
42struct iovec {
43 void *iov_base; /* Base address. */
44 size_t iov_len; /* Length. */
45};
46
47#ifdef _KERNEL
48#define IOVEC_INIT(iovp, base, len) \
49 *(iovp) = (struct iovec){ .iov_base = (base), .iov_len = (len) }
50
51/* String with length including NUL terminator */
52#define IOVEC_INIT_CSTR(iovp, str) do { \
53 void *__str = (str); \
54 IOVEC_INIT(iovp, __str, strlen(__str) + 1); \
55} while(0)
56
57/* Object with size from sizeof() */
58#define IOVEC_INIT_OBJ(iovp, obj) \
59 IOVEC_INIT(iovp, &(obj), sizeof(obj))
60
61#define IOVEC_ADVANCE(iovp, amt) do { \
62 struct iovec *__iovp = (iovp); \
63 size_t __amt = (amt); \
64 KASSERT(__amt <= __iovp->iov_len, ("%s: amount %zu > iov_len \
65 %zu", __func__, __amt, __iovp->iov_len)); \
66 __iovp->iov_len -= __amt; \
67 __iovp->iov_base = (char *)__iovp->iov_base + __amt; \
68} while(0)
69#endif /* _KERNEL */
70
71#endif /* !_SYS__IOVEC_H_ */