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