nv_impl.h revision 1.6 1 1.6 rmind /* $NetBSD: nv_impl.h,v 1.6 2019/02/12 12:49:23 rmind Exp $ */
2 1.2 christos
3 1.1 christos /*-
4 1.1 christos * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 1.1 christos *
6 1.1 christos * Copyright (c) 2013 The FreeBSD Foundation
7 1.1 christos * Copyright (c) 2013-2015 Mariusz Zaborski <oshogbo (at) FreeBSD.org>
8 1.1 christos * All rights reserved.
9 1.1 christos *
10 1.1 christos * This software was developed by Pawel Jakub Dawidek under sponsorship from
11 1.1 christos * the FreeBSD Foundation.
12 1.1 christos *
13 1.1 christos * Redistribution and use in source and binary forms, with or without
14 1.1 christos * modification, are permitted provided that the following conditions
15 1.1 christos * are met:
16 1.1 christos * 1. Redistributions of source code must retain the above copyright
17 1.1 christos * notice, this list of conditions and the following disclaimer.
18 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
19 1.1 christos * notice, this list of conditions and the following disclaimer in the
20 1.1 christos * documentation and/or other materials provided with the distribution.
21 1.1 christos *
22 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
26 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 christos * SUCH DAMAGE.
33 1.1 christos *
34 1.1 christos * $FreeBSD: head/sys/contrib/libnv/nv_impl.h 335347 2018-06-18 22:57:32Z oshogbo $
35 1.1 christos */
36 1.1 christos
37 1.1 christos #ifndef _NV_IMPL_H_
38 1.1 christos #define _NV_IMPL_H_
39 1.1 christos
40 1.2 christos #include "nv_compat.h"
41 1.2 christos
42 1.1 christos #ifndef _NVPAIR_T_DECLARED
43 1.1 christos #define _NVPAIR_T_DECLARED
44 1.1 christos struct nvpair;
45 1.1 christos
46 1.1 christos typedef struct nvpair nvpair_t;
47 1.1 christos #endif
48 1.1 christos
49 1.1 christos #define NV_TYPE_NVLIST_ARRAY_NEXT 254
50 1.1 christos #define NV_TYPE_NVLIST_UP 255
51 1.1 christos
52 1.1 christos #define NV_TYPE_FIRST NV_TYPE_NULL
53 1.1 christos #define NV_TYPE_LAST NV_TYPE_DESCRIPTOR_ARRAY
54 1.1 christos
55 1.1 christos #define NV_FLAG_BIG_ENDIAN 0x080
56 1.1 christos #define NV_FLAG_IN_ARRAY 0x100
57 1.1 christos
58 1.2 christos #if defined(_KERNEL)
59 1.3 christos # define nv_malloc(size) malloc((size), M_NVLIST, M_WAITOK)
60 1.3 christos # ifdef __FreeBSD__
61 1.3 christos # define nv_calloc(n, size) mallocarray((n), (size), M_NVLIST, \
62 1.1 christos M_WAITOK | M_ZERO)
63 1.3 christos # else
64 1.2 christos extern void *nv_calloc(size_t, size_t);
65 1.3 christos # endif
66 1.3 christos # define nv_realloc(buf, size) realloc((buf), (size), M_NVLIST, \
67 1.1 christos M_WAITOK)
68 1.3 christos # ifdef __FreeBSD__
69 1.5 rmind # define nv_free(buf) free((buf), M_NVLIST)
70 1.3 christos # define nv_strdup(buf) strdup((buf), M_NVLIST)
71 1.3 christos # else
72 1.5 rmind extern void nv_free(void *);
73 1.2 christos extern char *nv_strdup(const char *);
74 1.3 christos # endif
75 1.3 christos # define nv_vasprintf(ptr, ...) vasprintf(ptr, M_NVLIST, __VA_ARGS__)
76 1.2 christos #elif defined(_STANDALONE)
77 1.2 christos extern void *nv_malloc(size_t);
78 1.2 christos extern void *nv_calloc(size_t, size_t);
79 1.2 christos extern void *nv_realloc(void *, size_t);
80 1.2 christos extern void nv_free(void *);
81 1.2 christos extern char *nv_strdup(const char *);
82 1.3 christos # define nv_vasprintf(ptr, ...) vasprintf(ptr, M_NVLIST, __VA_ARGS__)
83 1.3 christos #else /* USERLAND */
84 1.1 christos
85 1.3 christos # define nv_malloc(size) malloc((size))
86 1.3 christos # define nv_realloc(buf, size) realloc((buf), (size))
87 1.3 christos # define nv_free(buf) free((buf))
88 1.3 christos # define nv_vasprintf(ptr, ...) vasprintf(ptr, __VA_ARGS__)
89 1.4 rmind void *nv_calloc(size_t, size_t);
90 1.4 rmind char *nv_strdup(const char *);
91 1.3 christos
92 1.3 christos # define ERRNO_SET(var) do { \
93 1.3 christos errno = (var); \
94 1.3 christos } while (/*CONSTCOND*/0)
95 1.3 christos # define ERRNO_SAVE() do { \
96 1.1 christos int _serrno; \
97 1.1 christos _serrno = errno
98 1.1 christos
99 1.3 christos # define ERRNO_RESTORE() errno = _serrno; \
100 1.3 christos } while (/*CONSTCOND*/0)
101 1.1 christos
102 1.3 christos # define ERRNO_OR_DEFAULT(default) (errno == 0 ? (default) : errno)
103 1.1 christos
104 1.1 christos #endif
105 1.1 christos
106 1.2 christos #ifndef ERRNO_SET
107 1.3 christos # define ERRNO_SET(var) do { } while (/*CONSTCOND*/0)
108 1.3 christos # define ERRNO_SAVE() do { do { } while(/*CONSTCOND*/0)
109 1.3 christos # define ERRNO_RESTORE() } while (/*CONSTCOND*/0)
110 1.3 christos
111 1.3 christos # define ERRNO_OR_DEFAULT(default) (default)
112 1.2 christos #endif
113 1.2 christos
114 1.1 christos int *nvlist_descriptors(const nvlist_t *nvl, size_t *nitemsp);
115 1.1 christos size_t nvlist_ndescriptors(const nvlist_t *nvl);
116 1.1 christos void nvlist_set_flags(nvlist_t *nvl, int flags);
117 1.1 christos
118 1.1 christos nvpair_t *nvlist_first_nvpair(const nvlist_t *nvl);
119 1.1 christos nvpair_t *nvlist_next_nvpair(const nvlist_t *nvl, const nvpair_t *nvp);
120 1.1 christos nvpair_t *nvlist_prev_nvpair(const nvlist_t *nvl, const nvpair_t *nvp);
121 1.1 christos
122 1.1 christos void nvlist_add_nvpair(nvlist_t *nvl, const nvpair_t *nvp);
123 1.1 christos
124 1.1 christos bool nvlist_move_nvpair(nvlist_t *nvl, nvpair_t *nvp);
125 1.1 christos
126 1.1 christos void nvlist_set_parent(nvlist_t *nvl, nvpair_t *parent);
127 1.1 christos void nvlist_set_array_next(nvlist_t *nvl, nvpair_t *ele);
128 1.6 rmind nvpair_t *nvlist_get_array_next_nvpair(nvlist_t *nvl);
129 1.1 christos
130 1.1 christos const nvpair_t *nvlist_get_nvpair(const nvlist_t *nvl, const char *name);
131 1.1 christos
132 1.1 christos nvpair_t *nvlist_take_nvpair(nvlist_t *nvl, const char *name);
133 1.1 christos
134 1.1 christos /* Function removes the given nvpair from the nvlist. */
135 1.1 christos void nvlist_remove_nvpair(nvlist_t *nvl, nvpair_t *nvp);
136 1.1 christos
137 1.1 christos void nvlist_free_nvpair(nvlist_t *nvl, nvpair_t *nvp);
138 1.1 christos
139 1.1 christos int nvpair_type(const nvpair_t *nvp);
140 1.1 christos const char *nvpair_name(const nvpair_t *nvp);
141 1.1 christos
142 1.1 christos nvpair_t *nvpair_clone(const nvpair_t *nvp);
143 1.1 christos
144 1.1 christos nvpair_t *nvpair_create_null(const char *name);
145 1.1 christos nvpair_t *nvpair_create_bool(const char *name, bool value);
146 1.1 christos nvpair_t *nvpair_create_number(const char *name, uint64_t value);
147 1.1 christos nvpair_t *nvpair_create_string(const char *name, const char *value);
148 1.1 christos nvpair_t *nvpair_create_stringf(const char *name, const char *valuefmt, ...) __printflike(2, 3);
149 1.1 christos nvpair_t *nvpair_create_stringv(const char *name, const char *valuefmt, va_list valueap) __printflike(2, 0);
150 1.1 christos nvpair_t *nvpair_create_nvlist(const char *name, const nvlist_t *value);
151 1.1 christos nvpair_t *nvpair_create_descriptor(const char *name, int value);
152 1.1 christos nvpair_t *nvpair_create_binary(const char *name, const void *value, size_t size);
153 1.1 christos nvpair_t *nvpair_create_bool_array(const char *name, const bool *value, size_t nitems);
154 1.1 christos nvpair_t *nvpair_create_number_array(const char *name, const uint64_t *value, size_t nitems);
155 1.1 christos nvpair_t *nvpair_create_string_array(const char *name, const char * const *value, size_t nitems);
156 1.1 christos nvpair_t *nvpair_create_nvlist_array(const char *name, const nvlist_t * const *value, size_t nitems);
157 1.1 christos nvpair_t *nvpair_create_descriptor_array(const char *name, const int *value, size_t nitems);
158 1.1 christos
159 1.1 christos nvpair_t *nvpair_move_string(const char *name, char *value);
160 1.1 christos nvpair_t *nvpair_move_nvlist(const char *name, nvlist_t *value);
161 1.1 christos nvpair_t *nvpair_move_descriptor(const char *name, int value);
162 1.1 christos nvpair_t *nvpair_move_binary(const char *name, void *value, size_t size);
163 1.1 christos nvpair_t *nvpair_move_bool_array(const char *name, bool *value, size_t nitems);
164 1.1 christos nvpair_t *nvpair_move_nvlist_array(const char *name, nvlist_t **value, size_t nitems);
165 1.1 christos nvpair_t *nvpair_move_descriptor_array(const char *name, int *value, size_t nitems);
166 1.1 christos nvpair_t *nvpair_move_number_array(const char *name, uint64_t *value, size_t nitems);
167 1.1 christos nvpair_t *nvpair_move_string_array(const char *name, char **value, size_t nitems);
168 1.1 christos
169 1.1 christos int nvpair_append_bool_array(nvpair_t *nvp, const bool value);
170 1.1 christos int nvpair_append_number_array(nvpair_t *nvp, const uint64_t value);
171 1.1 christos int nvpair_append_string_array(nvpair_t *nvp, const char *value);
172 1.1 christos int nvpair_append_nvlist_array(nvpair_t *nvp, const nvlist_t *value);
173 1.1 christos int nvpair_append_descriptor_array(nvpair_t *nvp, const int value);
174 1.1 christos
175 1.1 christos bool nvpair_get_bool(const nvpair_t *nvp);
176 1.1 christos uint64_t nvpair_get_number(const nvpair_t *nvp);
177 1.1 christos const char *nvpair_get_string(const nvpair_t *nvp);
178 1.1 christos const nvlist_t *nvpair_get_nvlist(const nvpair_t *nvp);
179 1.1 christos int nvpair_get_descriptor(const nvpair_t *nvp);
180 1.1 christos const void *nvpair_get_binary(const nvpair_t *nvp, size_t *sizep);
181 1.1 christos const bool *nvpair_get_bool_array(const nvpair_t *nvp, size_t *nitemsp);
182 1.1 christos const uint64_t *nvpair_get_number_array(const nvpair_t *nvp, size_t *nitemsp);
183 1.1 christos const char * const *nvpair_get_string_array(const nvpair_t *nvp, size_t *nitemsp);
184 1.1 christos const nvlist_t * const *nvpair_get_nvlist_array(const nvpair_t *nvp, size_t *nitemsp);
185 1.1 christos const int *nvpair_get_descriptor_array(const nvpair_t *nvp, size_t *nitemsp);
186 1.1 christos
187 1.1 christos void nvpair_free(nvpair_t *nvp);
188 1.1 christos
189 1.1 christos #endif /* !_NV_IMPL_H_ */
190