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