]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/contrib/libnv/cnvlist.c
MFV r331695, 331700: 9166 zfs storage pool checkpoint
[FreeBSD/FreeBSD.git] / sys / contrib / libnv / cnvlist.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2016 Adam Starak <starak.adam@gmail.com>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 #ifdef _KERNEL
35
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>
41
42 #include <machine/stdarg.h>
43
44 #else
45 #include <stdarg.h>
46 #include <stdbool.h>
47 #include <stdint.h>
48 #include <stdlib.h>
49 #endif
50
51 #include <sys/cnv.h>
52 #include <sys/nv.h>
53
54 #include "nv_impl.h"
55 #include "nvlist_impl.h"
56 #include "nvpair_impl.h"
57
58 const char *
59 cnvlist_name(void *cookiep)
60 {
61
62         return (nvpair_name(cookiep));
63 }
64
65 int
66 cnvlist_type(void *cookiep)
67 {
68
69         return (nvpair_type(cookiep));
70 }
71
72 #define CNVLIST_GET(ftype, type, NVTYPE)                                \
73 ftype                                                                   \
74 cnvlist_get_##type(void *cookiep)                                       \
75 {                                                                       \
76                                                                         \
77         if (nvpair_type(cookiep) != NV_TYPE_##NVTYPE) {                 \
78                 nvlist_report_missing(NV_TYPE_##NVTYPE,                 \
79                     nvpair_name(cookiep));                              \
80         }                                                               \
81         return (nvpair_get_##type(cookiep));                            \
82 }
83
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)
88 #ifndef _KERNEL
89 CNVLIST_GET(int, descriptor, DESCRIPTOR)
90 #endif
91
92 #undef  CNVLIST_GET
93
94 #define CNVLIST_GET_ARRAY(ftype, type, NVTYPE)                          \
95 ftype                                                                   \
96 cnvlist_get_##type(void *cookiep, size_t *nitemsp)                      \
97 {                                                                       \
98                                                                         \
99         if (nvpair_type(cookiep) != NV_TYPE_##NVTYPE) {                 \
100                 nvlist_report_missing(NV_TYPE_##NVTYPE,                 \
101                     nvpair_name(cookiep));                              \
102         }                                                               \
103         return (nvpair_get_##type(cookiep, nitemsp));                   \
104 }
105
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)
110 #ifndef _KERNEL
111 CNVLIST_GET_ARRAY(const int *, descriptor_array, DESCRIPTOR_ARRAY)
112 #endif
113
114 #undef  CNVLIST_GET_ARRAY
115
116 const void *
117 cnvlist_get_binary(void *cookiep, size_t *sizep)
118 {
119
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));
123 }
124
125 #define CNVLIST_TAKE(ftype, type, NVTYPE)                               \
126 ftype                                                                   \
127 cnvlist_take_##type(nvlist_t *nvl, void *cookiep)                       \
128 {                                                                       \
129         ftype value;                                                    \
130                                                                         \
131         if (nvpair_type(cookiep) != NV_TYPE_##NVTYPE) {                 \
132                 nvlist_report_missing(NV_TYPE_##NVTYPE,                 \
133                     nvpair_name(cookiep));                              \
134         }                                                               \
135         value = (ftype)(intptr_t)nvpair_get_##type(cookiep);            \
136         nvlist_remove_nvpair(nvl, cookiep);                             \
137         nvpair_free_structure(cookiep);                                 \
138         return (value);                                                 \
139 }
140
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)
145 #ifndef _KERNEL
146 CNVLIST_TAKE(int, descriptor, DESCRIPTOR)
147 #endif
148
149 #undef  CNVLIST_TAKE
150
151 #define CNVLIST_TAKE_ARRAY(ftype, type, NVTYPE)                         \
152 ftype                                                                   \
153 cnvlist_take_##type(nvlist_t *nvl, void *cookiep, size_t *nitemsp)      \
154 {                                                                       \
155         ftype value;                                                    \
156                                                                         \
157         if (nvpair_type(cookiep) != NV_TYPE_##NVTYPE) {                 \
158                 nvlist_report_missing(NV_TYPE_##NVTYPE,                 \
159                     nvpair_name(cookiep));                              \
160         }                                                               \
161         value = (ftype)(intptr_t)nvpair_get_##type(cookiep, nitemsp);   \
162         nvlist_remove_nvpair(nvl, cookiep);                             \
163         nvpair_free_structure(cookiep);                                 \
164         return (value);                                                 \
165 }
166
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)
171 #ifndef _KERNEL
172 CNVLIST_TAKE_ARRAY(int *, descriptor_array, DESCRIPTOR_ARRAY);
173 #endif
174
175 #undef  CNVLIST_TAKE_ARRAY
176
177 void *
178 cnvlist_take_binary(nvlist_t *nvl, void *cookiep, size_t *sizep)
179 {
180         void *value;
181
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);
187         return (value);
188 }
189
190
191 #define CNVLIST_FREE(type)                                              \
192 void                                                                    \
193 cnvlist_free_##type(nvlist_t *nvl, void *cookiep)                       \
194 {                                                                       \
195                                                                         \
196         nvlist_free_nvpair(nvl, cookiep);                               \
197 }
198
199 CNVLIST_FREE(bool)
200 CNVLIST_FREE(number)
201 CNVLIST_FREE(string)
202 CNVLIST_FREE(nvlist)
203 CNVLIST_FREE(binary);
204 CNVLIST_FREE(bool_array)
205 CNVLIST_FREE(number_array)
206 CNVLIST_FREE(string_array)
207 CNVLIST_FREE(nvlist_array)
208 #ifndef _KERNEL
209 CNVLIST_FREE(descriptor)
210 CNVLIST_FREE(descriptor_array)
211 #endif
212
213 #undef  CNVLIST_FREE