nv_impl.h revision 1.3 1 1.3 christos /* $NetBSD: nv_impl.h,v 1.3 2018/09/08 14:12:53 christos 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 # define nv_free(buf) free((buf), M_NVLIST)
69 1.3 christos # ifdef __FreeBSD__
70 1.3 christos # define nv_strdup(buf) strdup((buf), M_NVLIST)
71 1.3 christos # else
72 1.2 christos extern char *nv_strdup(const char *);
73 1.3 christos # endif
74 1.3 christos # define nv_vasprintf(ptr, ...) vasprintf(ptr, M_NVLIST, __VA_ARGS__)
75 1.2 christos #elif defined(_STANDALONE)
76 1.2 christos extern void *nv_malloc(size_t);
77 1.2 christos extern void *nv_calloc(size_t, size_t);
78 1.2 christos extern void *nv_realloc(void *, size_t);
79 1.2 christos extern void nv_free(void *);
80 1.2 christos extern char *nv_strdup(const char *);
81 1.3 christos # define nv_vasprintf(ptr, ...) vasprintf(ptr, M_NVLIST, __VA_ARGS__)
82 1.3 christos #else /* USERLAND */
83 1.1 christos
84 1.3 christos # define nv_malloc(size) malloc((size))
85 1.3 christos # define nv_calloc(n, size) calloc(n, 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_strdup(buf) strdup(buf)
89 1.3 christos # define nv_vasprintf(ptr, ...) vasprintf(ptr, __VA_ARGS__)
90 1.3 christos
91 1.3 christos # define ERRNO_SET(var) do { \
92 1.3 christos errno = (var); \
93 1.3 christos } while (/*CONSTCOND*/0)
94 1.3 christos # define ERRNO_SAVE() do { \
95 1.1 christos int _serrno; \
96 1.1 christos _serrno = errno
97 1.1 christos
98 1.3 christos # define ERRNO_RESTORE() errno = _serrno; \
99 1.3 christos } while (/*CONSTCOND*/0)
100 1.1 christos
101 1.3 christos # define ERRNO_OR_DEFAULT(default) (errno == 0 ? (default) : errno)
102 1.1 christos
103 1.1 christos #endif
104 1.1 christos
105 1.2 christos #ifndef ERRNO_SET
106 1.3 christos # define ERRNO_SET(var) do { } while (/*CONSTCOND*/0)
107 1.3 christos # define ERRNO_SAVE() do { do { } while(/*CONSTCOND*/0)
108 1.3 christos # define ERRNO_RESTORE() } while (/*CONSTCOND*/0)
109 1.3 christos
110 1.3 christos # define ERRNO_OR_DEFAULT(default) (default)
111 1.2 christos #endif
112 1.2 christos
113 1.1 christos int *nvlist_descriptors(const nvlist_t *nvl, size_t *nitemsp);
114 1.1 christos size_t nvlist_ndescriptors(const nvlist_t *nvl);
115 1.1 christos void nvlist_set_flags(nvlist_t *nvl, int flags);
116 1.1 christos
117 1.1 christos nvpair_t *nvlist_first_nvpair(const nvlist_t *nvl);
118 1.1 christos nvpair_t *nvlist_next_nvpair(const nvlist_t *nvl, const nvpair_t *nvp);
119 1.1 christos nvpair_t *nvlist_prev_nvpair(const nvlist_t *nvl, const nvpair_t *nvp);
120 1.1 christos
121 1.1 christos void nvlist_add_nvpair(nvlist_t *nvl, const nvpair_t *nvp);
122 1.1 christos
123 1.1 christos bool nvlist_move_nvpair(nvlist_t *nvl, nvpair_t *nvp);
124 1.1 christos
125 1.1 christos void nvlist_set_parent(nvlist_t *nvl, nvpair_t *parent);
126 1.1 christos void nvlist_set_array_next(nvlist_t *nvl, nvpair_t *ele);
127 1.1 christos
128 1.1 christos const nvpair_t *nvlist_get_nvpair(const nvlist_t *nvl, const char *name);
129 1.1 christos
130 1.1 christos nvpair_t *nvlist_take_nvpair(nvlist_t *nvl, const char *name);
131 1.1 christos
132 1.1 christos /* Function removes the given nvpair from the nvlist. */
133 1.1 christos void nvlist_remove_nvpair(nvlist_t *nvl, nvpair_t *nvp);
134 1.1 christos
135 1.1 christos void nvlist_free_nvpair(nvlist_t *nvl, nvpair_t *nvp);
136 1.1 christos
137 1.1 christos int nvpair_type(const nvpair_t *nvp);
138 1.1 christos const char *nvpair_name(const nvpair_t *nvp);
139 1.1 christos
140 1.1 christos nvpair_t *nvpair_clone(const nvpair_t *nvp);
141 1.1 christos
142 1.1 christos nvpair_t *nvpair_create_null(const char *name);
143 1.1 christos nvpair_t *nvpair_create_bool(const char *name, bool value);
144 1.1 christos nvpair_t *nvpair_create_number(const char *name, uint64_t value);
145 1.1 christos nvpair_t *nvpair_create_string(const char *name, const char *value);
146 1.1 christos nvpair_t *nvpair_create_stringf(const char *name, const char *valuefmt, ...) __printflike(2, 3);
147 1.1 christos nvpair_t *nvpair_create_stringv(const char *name, const char *valuefmt, va_list valueap) __printflike(2, 0);
148 1.1 christos nvpair_t *nvpair_create_nvlist(const char *name, const nvlist_t *value);
149 1.1 christos nvpair_t *nvpair_create_descriptor(const char *name, int value);
150 1.1 christos nvpair_t *nvpair_create_binary(const char *name, const void *value, size_t size);
151 1.1 christos nvpair_t *nvpair_create_bool_array(const char *name, const bool *value, size_t nitems);
152 1.1 christos nvpair_t *nvpair_create_number_array(const char *name, const uint64_t *value, size_t nitems);
153 1.1 christos nvpair_t *nvpair_create_string_array(const char *name, const char * const *value, size_t nitems);
154 1.1 christos nvpair_t *nvpair_create_nvlist_array(const char *name, const nvlist_t * const *value, size_t nitems);
155 1.1 christos nvpair_t *nvpair_create_descriptor_array(const char *name, const int *value, size_t nitems);
156 1.1 christos
157 1.1 christos nvpair_t *nvpair_move_string(const char *name, char *value);
158 1.1 christos nvpair_t *nvpair_move_nvlist(const char *name, nvlist_t *value);
159 1.1 christos nvpair_t *nvpair_move_descriptor(const char *name, int value);
160 1.1 christos nvpair_t *nvpair_move_binary(const char *name, void *value, size_t size);
161 1.1 christos nvpair_t *nvpair_move_bool_array(const char *name, bool *value, size_t nitems);
162 1.1 christos nvpair_t *nvpair_move_nvlist_array(const char *name, nvlist_t **value, size_t nitems);
163 1.1 christos nvpair_t *nvpair_move_descriptor_array(const char *name, int *value, size_t nitems);
164 1.1 christos nvpair_t *nvpair_move_number_array(const char *name, uint64_t *value, size_t nitems);
165 1.1 christos nvpair_t *nvpair_move_string_array(const char *name, char **value, size_t nitems);
166 1.1 christos
167 1.1 christos int nvpair_append_bool_array(nvpair_t *nvp, const bool value);
168 1.1 christos int nvpair_append_number_array(nvpair_t *nvp, const uint64_t value);
169 1.1 christos int nvpair_append_string_array(nvpair_t *nvp, const char *value);
170 1.1 christos int nvpair_append_nvlist_array(nvpair_t *nvp, const nvlist_t *value);
171 1.1 christos int nvpair_append_descriptor_array(nvpair_t *nvp, const int value);
172 1.1 christos
173 1.1 christos bool nvpair_get_bool(const nvpair_t *nvp);
174 1.1 christos uint64_t nvpair_get_number(const nvpair_t *nvp);
175 1.1 christos const char *nvpair_get_string(const nvpair_t *nvp);
176 1.1 christos const nvlist_t *nvpair_get_nvlist(const nvpair_t *nvp);
177 1.1 christos int nvpair_get_descriptor(const nvpair_t *nvp);
178 1.1 christos const void *nvpair_get_binary(const nvpair_t *nvp, size_t *sizep);
179 1.1 christos const bool *nvpair_get_bool_array(const nvpair_t *nvp, size_t *nitemsp);
180 1.1 christos const uint64_t *nvpair_get_number_array(const nvpair_t *nvp, size_t *nitemsp);
181 1.1 christos const char * const *nvpair_get_string_array(const nvpair_t *nvp, size_t *nitemsp);
182 1.1 christos const nvlist_t * const *nvpair_get_nvlist_array(const nvpair_t *nvp, size_t *nitemsp);
183 1.1 christos const int *nvpair_get_descriptor_array(const nvpair_t *nvp, size_t *nitemsp);
184 1.1 christos
185 1.1 christos void nvpair_free(nvpair_t *nvp);
186 1.1 christos
187 1.1 christos #endif /* !_NV_IMPL_H_ */
188