2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2016 Adam Starak <starak.adam@gmail.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
36 #include <sys/types.h>
37 #include <sys/param.h>
38 #include <sys/kernel.h>
39 #include <sys/systm.h>
40 #include <sys/malloc.h>
42 #include <machine/stdarg.h>
55 #include "nvlist_impl.h"
56 #include "nvpair_impl.h"
59 cnvlist_name(const void *cookie)
62 return (nvpair_name(cookie));
66 cnvlist_type(const void *cookie)
69 return (nvpair_type(cookie));
72 #define CNVLIST_GET(ftype, type, NVTYPE) \
74 cnvlist_get_##type(const void *cookie) \
77 if (nvpair_type(cookie) != NV_TYPE_##NVTYPE) { \
78 nvlist_report_missing(NV_TYPE_##NVTYPE, \
79 nvpair_name(cookie)); \
81 return (nvpair_get_##type(cookie)); \
84 CNVLIST_GET(bool, bool, BOOL)
85 CNVLIST_GET(uint64_t, number, NUMBER)
86 CNVLIST_GET(const char *, string, STRING)
87 CNVLIST_GET(const nvlist_t *, nvlist, NVLIST)
89 CNVLIST_GET(int, descriptor, DESCRIPTOR)
94 #define CNVLIST_GET_ARRAY(ftype, type, NVTYPE) \
96 cnvlist_get_##type(const void *cookie, size_t *nitemsp) \
99 if (nvpair_type(cookie) != NV_TYPE_##NVTYPE) { \
100 nvlist_report_missing(NV_TYPE_##NVTYPE, \
101 nvpair_name(cookie)); \
103 return (nvpair_get_##type(cookie, nitemsp)); \
106 CNVLIST_GET_ARRAY(const bool *, bool_array, BOOL_ARRAY)
107 CNVLIST_GET_ARRAY(const uint64_t *, number_array, NUMBER_ARRAY)
108 CNVLIST_GET_ARRAY(const char * const *, string_array, STRING_ARRAY)
109 CNVLIST_GET_ARRAY(const nvlist_t * const *, nvlist_array, NVLIST_ARRAY)
111 CNVLIST_GET_ARRAY(const int *, descriptor_array, DESCRIPTOR_ARRAY)
114 #undef CNVLIST_GET_ARRAY
117 cnvlist_get_binary(const void *cookie, size_t *sizep)
120 if (nvpair_type(cookie) != NV_TYPE_BINARY)
121 nvlist_report_missing(NV_TYPE_BINARY, nvpair_name(cookie));
122 return (nvpair_get_binary(cookie, sizep));
125 #define CNVLIST_TAKE(ftype, type, NVTYPE) \
127 cnvlist_take_##type(void *cookie) \
132 if (nvpair_type(cookie) != NV_TYPE_##NVTYPE) { \
133 nvlist_report_missing(NV_TYPE_##NVTYPE, \
134 nvpair_name(cookie)); \
136 nvl = nvpair_nvlist(cookie); \
137 value = (ftype)(intptr_t)nvpair_get_##type(cookie); \
138 nvlist_remove_nvpair(nvl, cookie); \
139 nvpair_free_structure(cookie); \
143 CNVLIST_TAKE(bool, bool, BOOL)
144 CNVLIST_TAKE(uint64_t, number, NUMBER)
145 CNVLIST_TAKE(char *, string, STRING)
146 CNVLIST_TAKE(nvlist_t *, nvlist, NVLIST)
148 CNVLIST_TAKE(int, descriptor, DESCRIPTOR)
153 #define CNVLIST_TAKE_ARRAY(ftype, type, NVTYPE) \
155 cnvlist_take_##type(void *cookie, size_t *nitemsp) \
160 if (nvpair_type(cookie) != NV_TYPE_##NVTYPE) { \
161 nvlist_report_missing(NV_TYPE_##NVTYPE, \
162 nvpair_name(cookie)); \
164 nvl = nvpair_nvlist(cookie); \
165 value = (ftype)(intptr_t)nvpair_get_##type(cookie, nitemsp); \
166 nvlist_remove_nvpair(nvl, cookie); \
167 nvpair_free_structure(cookie); \
171 CNVLIST_TAKE_ARRAY(bool *, bool_array, BOOL_ARRAY)
172 CNVLIST_TAKE_ARRAY(uint64_t *, number_array, NUMBER_ARRAY)
173 CNVLIST_TAKE_ARRAY(char **, string_array, STRING_ARRAY)
174 CNVLIST_TAKE_ARRAY(nvlist_t **, nvlist_array, NVLIST_ARRAY)
176 CNVLIST_TAKE_ARRAY(int *, descriptor_array, DESCRIPTOR_ARRAY);
179 #undef CNVLIST_TAKE_ARRAY
182 cnvlist_take_binary(void *cookie, size_t *sizep)
187 if (nvpair_type(cookie) != NV_TYPE_BINARY)
188 nvlist_report_missing(NV_TYPE_BINARY, nvpair_name(cookie));
189 nvl = nvpair_nvlist(cookie);
190 value = (void *)(intptr_t)nvpair_get_binary(cookie, sizep);
191 nvlist_remove_nvpair(nvl, cookie);
192 nvpair_free_structure(cookie);
197 #define CNVLIST_FREE(type) \
199 cnvlist_free_##type(void *cookie) \
202 nvlist_free_nvpair(nvpair_nvlist(cookie), cookie); \
209 CNVLIST_FREE(binary);
210 CNVLIST_FREE(bool_array)
211 CNVLIST_FREE(number_array)
212 CNVLIST_FREE(string_array)
213 CNVLIST_FREE(nvlist_array)
215 CNVLIST_FREE(descriptor)
216 CNVLIST_FREE(descriptor_array)