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