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(void *cookiep)
62 return (nvpair_name(cookiep));
66 cnvlist_type(void *cookiep)
69 return (nvpair_type(cookiep));
72 #define CNVLIST_GET(ftype, type, NVTYPE) \
74 cnvlist_get_##type(void *cookiep) \
77 if (nvpair_type(cookiep) != NV_TYPE_##NVTYPE) { \
78 nvlist_report_missing(NV_TYPE_##NVTYPE, \
79 nvpair_name(cookiep)); \
81 return (nvpair_get_##type(cookiep)); \
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(void *cookiep, size_t *nitemsp) \
99 if (nvpair_type(cookiep) != NV_TYPE_##NVTYPE) { \
100 nvlist_report_missing(NV_TYPE_##NVTYPE, \
101 nvpair_name(cookiep)); \
103 return (nvpair_get_##type(cookiep, 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(void *cookiep, size_t *sizep)
120 if (nvpair_type(cookiep) != NV_TYPE_BINARY)
121 nvlist_report_missing(NV_TYPE_BINARY, nvpair_name(cookiep));
122 return (nvpair_get_binary(cookiep, sizep));
125 #define CNVLIST_TAKE(ftype, type, NVTYPE) \
127 cnvlist_take_##type(nvlist_t *nvl, void *cookiep) \
131 if (nvpair_type(cookiep) != NV_TYPE_##NVTYPE) { \
132 nvlist_report_missing(NV_TYPE_##NVTYPE, \
133 nvpair_name(cookiep)); \
135 value = (ftype)(intptr_t)nvpair_get_##type(cookiep); \
136 nvlist_remove_nvpair(nvl, cookiep); \
137 nvpair_free_structure(cookiep); \
141 CNVLIST_TAKE(bool, bool, BOOL)
142 CNVLIST_TAKE(uint64_t, number, NUMBER)
143 CNVLIST_TAKE(char *, string, STRING)
144 CNVLIST_TAKE(nvlist_t *, nvlist, NVLIST)
146 CNVLIST_TAKE(int, descriptor, DESCRIPTOR)
151 #define CNVLIST_TAKE_ARRAY(ftype, type, NVTYPE) \
153 cnvlist_take_##type(nvlist_t *nvl, void *cookiep, size_t *nitemsp) \
157 if (nvpair_type(cookiep) != NV_TYPE_##NVTYPE) { \
158 nvlist_report_missing(NV_TYPE_##NVTYPE, \
159 nvpair_name(cookiep)); \
161 value = (ftype)(intptr_t)nvpair_get_##type(cookiep, nitemsp); \
162 nvlist_remove_nvpair(nvl, cookiep); \
163 nvpair_free_structure(cookiep); \
167 CNVLIST_TAKE_ARRAY(bool *, bool_array, BOOL_ARRAY)
168 CNVLIST_TAKE_ARRAY(uint64_t *, number_array, NUMBER_ARRAY)
169 CNVLIST_TAKE_ARRAY(char **, string_array, STRING_ARRAY)
170 CNVLIST_TAKE_ARRAY(nvlist_t **, nvlist_array, NVLIST_ARRAY)
172 CNVLIST_TAKE_ARRAY(int *, descriptor_array, DESCRIPTOR_ARRAY);
175 #undef CNVLIST_TAKE_ARRAY
178 cnvlist_take_binary(nvlist_t *nvl, void *cookiep, size_t *sizep)
182 if (nvpair_type(cookiep) != NV_TYPE_BINARY)
183 nvlist_report_missing(NV_TYPE_BINARY, nvpair_name(cookiep));
184 value = (void *)(intptr_t)nvpair_get_binary(cookiep, sizep);
185 nvlist_remove_nvpair(nvl, cookiep);
186 nvpair_free_structure(cookiep);
191 #define CNVLIST_FREE(type) \
193 cnvlist_free_##type(nvlist_t *nvl, void *cookiep) \
196 nvlist_free_nvpair(nvl, cookiep); \
203 CNVLIST_FREE(binary);
204 CNVLIST_FREE(bool_array)
205 CNVLIST_FREE(number_array)
206 CNVLIST_FREE(string_array)
207 CNVLIST_FREE(nvlist_array)
209 CNVLIST_FREE(descriptor)
210 CNVLIST_FREE(descriptor_array)