2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2013 The FreeBSD Foundation
5 * Copyright (c) 2013-2015 Mariusz Zaborski <oshogbo@FreeBSD.org>
8 * This software was developed by Pawel Jakub Dawidek under sponsorship from
9 * the FreeBSD Foundation.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #ifndef _NVPAIR_T_DECLARED
39 #define _NVPAIR_T_DECLARED
42 typedef struct nvpair nvpair_t;
45 #define NV_TYPE_NVLIST_ARRAY_NEXT 254
46 #define NV_TYPE_NVLIST_UP 255
48 #define NV_TYPE_FIRST NV_TYPE_NULL
49 #define NV_TYPE_LAST NV_TYPE_DESCRIPTOR_ARRAY
51 #define NV_FLAG_BIG_ENDIAN 0x080
52 #define NV_FLAG_IN_ARRAY 0x100
55 #define nv_malloc(size) malloc((size), M_NVLIST, M_NOWAIT)
56 #define nv_calloc(n, size) mallocarray((n), (size), M_NVLIST, \
58 #define nv_realloc(buf, size) realloc((buf), (size), M_NVLIST, \
60 #define nv_free(buf) free((buf), M_NVLIST)
61 #define nv_strdup(buf) strdup_flags((buf), M_NVLIST, M_NOWAIT)
62 #define nv_vasprintf(ptr, ...) vasprintf(ptr, M_NVLIST, __VA_ARGS__)
64 #define ERRNO_SET(var) do { } while (0)
65 #define ERRNO_SAVE() do { do { } while(0)
66 #define ERRNO_RESTORE() } while (0)
68 #define ERRNO_OR_DEFAULT(default) (default)
72 #define nv_malloc(size) malloc((size))
73 #define nv_calloc(n, size) calloc((n), (size))
74 #define nv_realloc(buf, size) realloc((buf), (size))
75 #define nv_free(buf) free((buf))
76 #define nv_strdup(buf) strdup((buf))
77 #define nv_vasprintf(ptr, ...) vasprintf(ptr, __VA_ARGS__)
79 #define ERRNO_SET(var) do { errno = (var); } while (0)
80 #define ERRNO_SAVE() do { \
85 #define ERRNO_RESTORE() errno = _serrno; \
88 #define ERRNO_OR_DEFAULT(default) (errno == 0 ? (default) : errno)
92 int *nvlist_descriptors(const nvlist_t *nvl, size_t *nitemsp);
93 size_t nvlist_ndescriptors(const nvlist_t *nvl);
94 void nvlist_set_flags(nvlist_t *nvl, int flags);
96 nvpair_t *nvlist_first_nvpair(const nvlist_t *nvl);
97 nvpair_t *nvlist_next_nvpair(const nvlist_t *nvl, const nvpair_t *nvp);
98 nvpair_t *nvlist_prev_nvpair(const nvlist_t *nvl, const nvpair_t *nvp);
100 void nvlist_add_nvpair(nvlist_t *nvl, const nvpair_t *nvp);
102 bool nvlist_move_nvpair(nvlist_t *nvl, nvpair_t *nvp);
104 void nvlist_set_parent(nvlist_t *nvl, nvpair_t *parent);
105 void nvlist_set_array_next(nvlist_t *nvl, nvpair_t *ele);
106 nvpair_t *nvlist_get_array_next_nvpair(nvlist_t *nvl);
108 const nvpair_t *nvlist_get_nvpair(const nvlist_t *nvl, const char *name);
110 nvpair_t *nvlist_take_nvpair(nvlist_t *nvl, const char *name);
112 /* Function removes the given nvpair from the nvlist. */
113 void nvlist_remove_nvpair(nvlist_t *nvl, nvpair_t *nvp);
115 void nvlist_free_nvpair(nvlist_t *nvl, nvpair_t *nvp);
117 int nvpair_type(const nvpair_t *nvp);
118 const char *nvpair_name(const nvpair_t *nvp);
120 nvpair_t *nvpair_clone(const nvpair_t *nvp);
122 nvpair_t *nvpair_create_null(const char *name);
123 nvpair_t *nvpair_create_bool(const char *name, bool value);
124 nvpair_t *nvpair_create_number(const char *name, uint64_t value);
125 nvpair_t *nvpair_create_string(const char *name, const char *value);
126 nvpair_t *nvpair_create_stringf(const char *name, const char *valuefmt, ...) __printflike(2, 3);
127 nvpair_t *nvpair_create_stringv(const char *name, const char *valuefmt, va_list valueap) __printflike(2, 0);
128 nvpair_t *nvpair_create_nvlist(const char *name, const nvlist_t *value);
129 nvpair_t *nvpair_create_descriptor(const char *name, int value);
130 nvpair_t *nvpair_create_binary(const char *name, const void *value, size_t size);
131 nvpair_t *nvpair_create_bool_array(const char *name, const bool *value, size_t nitems);
132 nvpair_t *nvpair_create_number_array(const char *name, const uint64_t *value, size_t nitems);
133 nvpair_t *nvpair_create_string_array(const char *name, const char * const *value, size_t nitems);
134 nvpair_t *nvpair_create_nvlist_array(const char *name, const nvlist_t * const *value, size_t nitems);
135 nvpair_t *nvpair_create_descriptor_array(const char *name, const int *value, size_t nitems);
137 nvpair_t *nvpair_move_string(const char *name, char *value);
138 nvpair_t *nvpair_move_nvlist(const char *name, nvlist_t *value);
139 nvpair_t *nvpair_move_descriptor(const char *name, int value);
140 nvpair_t *nvpair_move_binary(const char *name, void *value, size_t size);
141 nvpair_t *nvpair_move_bool_array(const char *name, bool *value, size_t nitems);
142 nvpair_t *nvpair_move_nvlist_array(const char *name, nvlist_t **value, size_t nitems);
143 nvpair_t *nvpair_move_descriptor_array(const char *name, int *value, size_t nitems);
144 nvpair_t *nvpair_move_number_array(const char *name, uint64_t *value, size_t nitems);
145 nvpair_t *nvpair_move_string_array(const char *name, char **value, size_t nitems);
147 int nvpair_append_bool_array(nvpair_t *nvp, const bool value);
148 int nvpair_append_number_array(nvpair_t *nvp, const uint64_t value);
149 int nvpair_append_string_array(nvpair_t *nvp, const char *value);
150 int nvpair_append_nvlist_array(nvpair_t *nvp, const nvlist_t *value);
151 int nvpair_append_descriptor_array(nvpair_t *nvp, const int value);
153 bool nvpair_get_bool(const nvpair_t *nvp);
154 uint64_t nvpair_get_number(const nvpair_t *nvp);
155 const char *nvpair_get_string(const nvpair_t *nvp);
156 const nvlist_t *nvpair_get_nvlist(const nvpair_t *nvp);
157 int nvpair_get_descriptor(const nvpair_t *nvp);
158 const void *nvpair_get_binary(const nvpair_t *nvp, size_t *sizep);
159 const bool *nvpair_get_bool_array(const nvpair_t *nvp, size_t *nitemsp);
160 const uint64_t *nvpair_get_number_array(const nvpair_t *nvp, size_t *nitemsp);
161 const char * const *nvpair_get_string_array(const nvpair_t *nvp, size_t *nitemsp);
162 const nvlist_t * const *nvpair_get_nvlist_array(const nvpair_t *nvp, size_t *nitemsp);
163 const int *nvpair_get_descriptor_array(const nvpair_t *nvp, size_t *nitemsp);
165 void nvpair_free(nvpair_t *nvp);
167 #endif /* !_NV_IMPL_H_ */