cnvlist.c revision 1.2.2.2 1 1.2.2.2 pgoyette /* $NetBSD: cnvlist.c,v 1.2.2.2 2018/09/30 01:45:55 pgoyette Exp $ */
2 1.2.2.2 pgoyette
3 1.2.2.2 pgoyette /*-
4 1.2.2.2 pgoyette * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 1.2.2.2 pgoyette *
6 1.2.2.2 pgoyette * Copyright (c) 2016 Adam Starak <starak.adam (at) gmail.com>
7 1.2.2.2 pgoyette * All rights reserved.
8 1.2.2.2 pgoyette *
9 1.2.2.2 pgoyette * Redistribution and use in source and binary forms, with or without
10 1.2.2.2 pgoyette * modification, are permitted provided that the following conditions
11 1.2.2.2 pgoyette * are met:
12 1.2.2.2 pgoyette * 1. Redistributions of source code must retain the above copyright
13 1.2.2.2 pgoyette * notice, this list of conditions and the following disclaimer.
14 1.2.2.2 pgoyette * 2. Redistributions in binary form must reproduce the above copyright
15 1.2.2.2 pgoyette * notice, this list of conditions and the following disclaimer in the
16 1.2.2.2 pgoyette * documentation and/or other materials provided with the distribution.
17 1.2.2.2 pgoyette *
18 1.2.2.2 pgoyette * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
19 1.2.2.2 pgoyette * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 1.2.2.2 pgoyette * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 1.2.2.2 pgoyette * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
22 1.2.2.2 pgoyette * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 1.2.2.2 pgoyette * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 1.2.2.2 pgoyette * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 1.2.2.2 pgoyette * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 1.2.2.2 pgoyette * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 1.2.2.2 pgoyette * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 1.2.2.2 pgoyette * SUCH DAMAGE.
29 1.2.2.2 pgoyette *
30 1.2.2.2 pgoyette * $FreeBSD: head/sys/contrib/libnv/cnvlist.c 335343 2018-06-18 21:26:58Z oshogbo $
31 1.2.2.2 pgoyette */
32 1.2.2.2 pgoyette
33 1.2.2.2 pgoyette #include <sys/cdefs.h>
34 1.2.2.2 pgoyette #ifdef __FreeBSD__
35 1.2.2.2 pgoyette __FBSDID("$FreeBSD: head/sys/contrib/libnv/cnvlist.c 335343 2018-06-18 21:26:58Z oshogbo $");
36 1.2.2.2 pgoyette #else
37 1.2.2.2 pgoyette __RCSID("$NetBSD: cnvlist.c,v 1.2.2.2 2018/09/30 01:45:55 pgoyette Exp $");
38 1.2.2.2 pgoyette #endif
39 1.2.2.2 pgoyette
40 1.2.2.2 pgoyette #if defined(_KERNEL) || defined(_STANDALONE)
41 1.2.2.2 pgoyette
42 1.2.2.2 pgoyette #include <sys/types.h>
43 1.2.2.2 pgoyette #include <sys/param.h>
44 1.2.2.2 pgoyette #include <sys/kernel.h>
45 1.2.2.2 pgoyette #include <sys/systm.h>
46 1.2.2.2 pgoyette #include <sys/malloc.h>
47 1.2.2.2 pgoyette
48 1.2.2.2 pgoyette #ifdef __FreeBSD__
49 1.2.2.2 pgoyette #include <machine/stdarg.h>
50 1.2.2.2 pgoyette #endif
51 1.2.2.2 pgoyette
52 1.2.2.2 pgoyette #else
53 1.2.2.2 pgoyette #include <stdarg.h>
54 1.2.2.2 pgoyette #include <stdbool.h>
55 1.2.2.2 pgoyette #include <stdint.h>
56 1.2.2.2 pgoyette #include <stdlib.h>
57 1.2.2.2 pgoyette #endif
58 1.2.2.2 pgoyette
59 1.2.2.2 pgoyette #ifdef __FreeBSD__
60 1.2.2.2 pgoyette #include <sys/nv.h>
61 1.2.2.2 pgoyette #include <sys/cnv.h>
62 1.2.2.2 pgoyette #else
63 1.2.2.2 pgoyette #include "nv.h"
64 1.2.2.2 pgoyette #include "cnv.h"
65 1.2.2.2 pgoyette #endif
66 1.2.2.2 pgoyette
67 1.2.2.2 pgoyette #include "nv_impl.h"
68 1.2.2.2 pgoyette #include "nvlist_impl.h"
69 1.2.2.2 pgoyette #include "nvpair_impl.h"
70 1.2.2.2 pgoyette
71 1.2.2.2 pgoyette const char *
72 1.2.2.2 pgoyette cnvlist_name(const void *cookie)
73 1.2.2.2 pgoyette {
74 1.2.2.2 pgoyette
75 1.2.2.2 pgoyette return (nvpair_name(cookie));
76 1.2.2.2 pgoyette }
77 1.2.2.2 pgoyette
78 1.2.2.2 pgoyette int
79 1.2.2.2 pgoyette cnvlist_type(const void *cookie)
80 1.2.2.2 pgoyette {
81 1.2.2.2 pgoyette
82 1.2.2.2 pgoyette return (nvpair_type(cookie));
83 1.2.2.2 pgoyette }
84 1.2.2.2 pgoyette
85 1.2.2.2 pgoyette #define CNVLIST_GET(ftype, type, NVTYPE) \
86 1.2.2.2 pgoyette ftype \
87 1.2.2.2 pgoyette cnvlist_get_##type(const void *cookie) \
88 1.2.2.2 pgoyette { \
89 1.2.2.2 pgoyette \
90 1.2.2.2 pgoyette if (nvpair_type(cookie) != NV_TYPE_##NVTYPE) { \
91 1.2.2.2 pgoyette nvlist_report_missing(NV_TYPE_##NVTYPE, \
92 1.2.2.2 pgoyette nvpair_name(cookie)); \
93 1.2.2.2 pgoyette } \
94 1.2.2.2 pgoyette return (nvpair_get_##type(cookie)); \
95 1.2.2.2 pgoyette }
96 1.2.2.2 pgoyette
97 1.2.2.2 pgoyette CNVLIST_GET(bool, bool, BOOL)
98 1.2.2.2 pgoyette CNVLIST_GET(uint64_t, number, NUMBER)
99 1.2.2.2 pgoyette CNVLIST_GET(const char *, string, STRING)
100 1.2.2.2 pgoyette CNVLIST_GET(const nvlist_t *, nvlist, NVLIST)
101 1.2.2.2 pgoyette #if !defined(_KERNEL) && !defined(_STANDALONE)
102 1.2.2.2 pgoyette CNVLIST_GET(int, descriptor, DESCRIPTOR)
103 1.2.2.2 pgoyette #endif
104 1.2.2.2 pgoyette
105 1.2.2.2 pgoyette #undef CNVLIST_GET
106 1.2.2.2 pgoyette
107 1.2.2.2 pgoyette #define CNVLIST_GET_ARRAY(ftype, type, NVTYPE) \
108 1.2.2.2 pgoyette ftype \
109 1.2.2.2 pgoyette cnvlist_get_##type(const void *cookie, size_t *nitemsp) \
110 1.2.2.2 pgoyette { \
111 1.2.2.2 pgoyette \
112 1.2.2.2 pgoyette if (nvpair_type(cookie) != NV_TYPE_##NVTYPE) { \
113 1.2.2.2 pgoyette nvlist_report_missing(NV_TYPE_##NVTYPE, \
114 1.2.2.2 pgoyette nvpair_name(cookie)); \
115 1.2.2.2 pgoyette } \
116 1.2.2.2 pgoyette return (nvpair_get_##type(cookie, nitemsp)); \
117 1.2.2.2 pgoyette }
118 1.2.2.2 pgoyette
119 1.2.2.2 pgoyette CNVLIST_GET_ARRAY(const bool *, bool_array, BOOL_ARRAY)
120 1.2.2.2 pgoyette CNVLIST_GET_ARRAY(const uint64_t *, number_array, NUMBER_ARRAY)
121 1.2.2.2 pgoyette CNVLIST_GET_ARRAY(const char * const *, string_array, STRING_ARRAY)
122 1.2.2.2 pgoyette CNVLIST_GET_ARRAY(const nvlist_t * const *, nvlist_array, NVLIST_ARRAY)
123 1.2.2.2 pgoyette #if !defined(_KERNEL) && !defined(_STANDALONE)
124 1.2.2.2 pgoyette CNVLIST_GET_ARRAY(const int *, descriptor_array, DESCRIPTOR_ARRAY)
125 1.2.2.2 pgoyette #endif
126 1.2.2.2 pgoyette
127 1.2.2.2 pgoyette #undef CNVLIST_GET_ARRAY
128 1.2.2.2 pgoyette
129 1.2.2.2 pgoyette const void *
130 1.2.2.2 pgoyette cnvlist_get_binary(const void *cookie, size_t *sizep)
131 1.2.2.2 pgoyette {
132 1.2.2.2 pgoyette
133 1.2.2.2 pgoyette if (nvpair_type(cookie) != NV_TYPE_BINARY)
134 1.2.2.2 pgoyette nvlist_report_missing(NV_TYPE_BINARY, nvpair_name(cookie));
135 1.2.2.2 pgoyette return (nvpair_get_binary(cookie, sizep));
136 1.2.2.2 pgoyette }
137 1.2.2.2 pgoyette
138 1.2.2.2 pgoyette #define CNVLIST_TAKE(ftype, type, NVTYPE) \
139 1.2.2.2 pgoyette ftype \
140 1.2.2.2 pgoyette cnvlist_take_##type(void *cookie) \
141 1.2.2.2 pgoyette { \
142 1.2.2.2 pgoyette ftype value; \
143 1.2.2.2 pgoyette nvlist_t *nvl; \
144 1.2.2.2 pgoyette \
145 1.2.2.2 pgoyette if (nvpair_type(cookie) != NV_TYPE_##NVTYPE) { \
146 1.2.2.2 pgoyette nvlist_report_missing(NV_TYPE_##NVTYPE, \
147 1.2.2.2 pgoyette nvpair_name(cookie)); \
148 1.2.2.2 pgoyette } \
149 1.2.2.2 pgoyette nvl = nvpair_nvlist(cookie); \
150 1.2.2.2 pgoyette value = (ftype)(intptr_t)nvpair_get_##type(cookie); \
151 1.2.2.2 pgoyette nvlist_remove_nvpair(nvl, cookie); \
152 1.2.2.2 pgoyette nvpair_free_structure(cookie); \
153 1.2.2.2 pgoyette return (value); \
154 1.2.2.2 pgoyette }
155 1.2.2.2 pgoyette
156 1.2.2.2 pgoyette CNVLIST_TAKE(bool, bool, BOOL)
157 1.2.2.2 pgoyette CNVLIST_TAKE(uint64_t, number, NUMBER)
158 1.2.2.2 pgoyette CNVLIST_TAKE(char *, string, STRING)
159 1.2.2.2 pgoyette CNVLIST_TAKE(nvlist_t *, nvlist, NVLIST)
160 1.2.2.2 pgoyette #if !defined(_KERNEL) && !defined(_STANDALONE)
161 1.2.2.2 pgoyette CNVLIST_TAKE(int, descriptor, DESCRIPTOR)
162 1.2.2.2 pgoyette #endif
163 1.2.2.2 pgoyette
164 1.2.2.2 pgoyette #undef CNVLIST_TAKE
165 1.2.2.2 pgoyette
166 1.2.2.2 pgoyette #define CNVLIST_TAKE_ARRAY(ftype, type, NVTYPE) \
167 1.2.2.2 pgoyette ftype \
168 1.2.2.2 pgoyette cnvlist_take_##type(void *cookie, size_t *nitemsp) \
169 1.2.2.2 pgoyette { \
170 1.2.2.2 pgoyette ftype value; \
171 1.2.2.2 pgoyette nvlist_t *nvl; \
172 1.2.2.2 pgoyette \
173 1.2.2.2 pgoyette if (nvpair_type(cookie) != NV_TYPE_##NVTYPE) { \
174 1.2.2.2 pgoyette nvlist_report_missing(NV_TYPE_##NVTYPE, \
175 1.2.2.2 pgoyette nvpair_name(cookie)); \
176 1.2.2.2 pgoyette } \
177 1.2.2.2 pgoyette nvl = nvpair_nvlist(cookie); \
178 1.2.2.2 pgoyette value = (ftype)(intptr_t)nvpair_get_##type(cookie, nitemsp); \
179 1.2.2.2 pgoyette nvlist_remove_nvpair(nvl, cookie); \
180 1.2.2.2 pgoyette nvpair_free_structure(cookie); \
181 1.2.2.2 pgoyette return (value); \
182 1.2.2.2 pgoyette }
183 1.2.2.2 pgoyette
184 1.2.2.2 pgoyette CNVLIST_TAKE_ARRAY(bool *, bool_array, BOOL_ARRAY)
185 1.2.2.2 pgoyette CNVLIST_TAKE_ARRAY(uint64_t *, number_array, NUMBER_ARRAY)
186 1.2.2.2 pgoyette CNVLIST_TAKE_ARRAY(char **, string_array, STRING_ARRAY)
187 1.2.2.2 pgoyette CNVLIST_TAKE_ARRAY(nvlist_t **, nvlist_array, NVLIST_ARRAY)
188 1.2.2.2 pgoyette #if !defined(_KERNEL) && !defined(_STANDALONE)
189 1.2.2.2 pgoyette CNVLIST_TAKE_ARRAY(int *, descriptor_array, DESCRIPTOR_ARRAY);
190 1.2.2.2 pgoyette #endif
191 1.2.2.2 pgoyette
192 1.2.2.2 pgoyette #undef CNVLIST_TAKE_ARRAY
193 1.2.2.2 pgoyette
194 1.2.2.2 pgoyette void *
195 1.2.2.2 pgoyette cnvlist_take_binary(void *cookie, size_t *sizep)
196 1.2.2.2 pgoyette {
197 1.2.2.2 pgoyette void *value;
198 1.2.2.2 pgoyette nvlist_t *nvl;
199 1.2.2.2 pgoyette
200 1.2.2.2 pgoyette if (nvpair_type(cookie) != NV_TYPE_BINARY)
201 1.2.2.2 pgoyette nvlist_report_missing(NV_TYPE_BINARY, nvpair_name(cookie));
202 1.2.2.2 pgoyette nvl = nvpair_nvlist(cookie);
203 1.2.2.2 pgoyette value = (void *)(intptr_t)nvpair_get_binary(cookie, sizep);
204 1.2.2.2 pgoyette nvlist_remove_nvpair(nvl, cookie);
205 1.2.2.2 pgoyette nvpair_free_structure(cookie);
206 1.2.2.2 pgoyette return (value);
207 1.2.2.2 pgoyette }
208 1.2.2.2 pgoyette
209 1.2.2.2 pgoyette
210 1.2.2.2 pgoyette #define CNVLIST_FREE(type) \
211 1.2.2.2 pgoyette void \
212 1.2.2.2 pgoyette cnvlist_free_##type(void *cookie) \
213 1.2.2.2 pgoyette { \
214 1.2.2.2 pgoyette \
215 1.2.2.2 pgoyette nvlist_free_nvpair(nvpair_nvlist(cookie), cookie); \
216 1.2.2.2 pgoyette }
217 1.2.2.2 pgoyette
218 1.2.2.2 pgoyette CNVLIST_FREE(bool)
219 1.2.2.2 pgoyette CNVLIST_FREE(number)
220 1.2.2.2 pgoyette CNVLIST_FREE(string)
221 1.2.2.2 pgoyette CNVLIST_FREE(nvlist)
222 1.2.2.2 pgoyette CNVLIST_FREE(binary);
223 1.2.2.2 pgoyette CNVLIST_FREE(bool_array)
224 1.2.2.2 pgoyette CNVLIST_FREE(number_array)
225 1.2.2.2 pgoyette CNVLIST_FREE(string_array)
226 1.2.2.2 pgoyette CNVLIST_FREE(nvlist_array)
227 1.2.2.2 pgoyette #if !defined(_KERNEL) && !defined(_STANDALONE)
228 1.2.2.2 pgoyette CNVLIST_FREE(descriptor)
229 1.2.2.2 pgoyette CNVLIST_FREE(descriptor_array)
230 1.2.2.2 pgoyette #endif
231 1.2.2.2 pgoyette
232 1.2.2.2 pgoyette #undef CNVLIST_FREE
233