dnvlist.c revision 1.2 1 1.2 christos /* $NetBSD: dnvlist.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) 2013 The FreeBSD Foundation
7 1.1 christos * All rights reserved.
8 1.1 christos *
9 1.1 christos * This software was developed by Pawel Jakub Dawidek under sponsorship from
10 1.1 christos * the FreeBSD Foundation.
11 1.1 christos *
12 1.1 christos * Redistribution and use in source and binary forms, with or without
13 1.1 christos * modification, are permitted provided that the following conditions
14 1.1 christos * are met:
15 1.1 christos * 1. Redistributions of source code must retain the above copyright
16 1.1 christos * notice, this list of conditions and the following disclaimer.
17 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
18 1.1 christos * notice, this list of conditions and the following disclaimer in the
19 1.1 christos * documentation and/or other materials provided with the distribution.
20 1.1 christos *
21 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
25 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 christos * SUCH DAMAGE.
32 1.1 christos */
33 1.1 christos
34 1.1 christos #include <sys/cdefs.h>
35 1.2 christos #ifdef __FreeBSD__
36 1.1 christos __FBSDID("$FreeBSD: head/sys/contrib/libnv/dnvlist.c 328474 2018-01-27 12:58:21Z oshogbo $");
37 1.2 christos #else
38 1.2 christos __RCSID("$NetBSD: dnvlist.c,v 1.2 2018/09/08 14:02:15 christos Exp $");
39 1.2 christos #endif
40 1.1 christos
41 1.2 christos #if defined(_KERNEL) || defined(_STANDALONE)
42 1.1 christos
43 1.1 christos #include <sys/types.h>
44 1.1 christos #include <sys/param.h>
45 1.1 christos #include <sys/kernel.h>
46 1.1 christos #include <sys/systm.h>
47 1.1 christos #include <sys/malloc.h>
48 1.1 christos
49 1.2 christos #ifndef __FreeBSD__
50 1.1 christos #include <machine/stdarg.h>
51 1.2 christos #endif
52 1.1 christos
53 1.1 christos #else
54 1.1 christos #include <stdarg.h>
55 1.1 christos #include <stdbool.h>
56 1.1 christos #include <stdint.h>
57 1.1 christos #include <stdlib.h>
58 1.1 christos #endif
59 1.1 christos
60 1.2 christos #ifndef __FreeBSD__
61 1.1 christos #include <sys/dnv.h>
62 1.1 christos #include <sys/nv.h>
63 1.2 christos #else
64 1.2 christos #include "dnv.h"
65 1.2 christos #include "nv.h"
66 1.2 christos #endif
67 1.1 christos
68 1.1 christos #include "nv_impl.h"
69 1.1 christos
70 1.1 christos #define DNVLIST_GET(ftype, type) \
71 1.1 christos ftype \
72 1.1 christos dnvlist_get_##type(const nvlist_t *nvl, const char *name, ftype defval) \
73 1.1 christos { \
74 1.1 christos \
75 1.1 christos if (nvlist_exists_##type(nvl, name)) \
76 1.1 christos return (nvlist_get_##type(nvl, name)); \
77 1.1 christos else \
78 1.1 christos return (defval); \
79 1.1 christos }
80 1.1 christos
81 1.1 christos DNVLIST_GET(bool, bool)
82 1.1 christos DNVLIST_GET(uint64_t, number)
83 1.1 christos DNVLIST_GET(const char *, string)
84 1.1 christos DNVLIST_GET(const nvlist_t *, nvlist)
85 1.2 christos #if !defined(_KERNEL) && !defined(_STANDALONE)
86 1.1 christos DNVLIST_GET(int, descriptor)
87 1.1 christos #endif
88 1.1 christos
89 1.1 christos #undef DNVLIST_GET
90 1.1 christos
91 1.1 christos const void *
92 1.1 christos dnvlist_get_binary(const nvlist_t *nvl, const char *name, size_t *sizep,
93 1.1 christos const void *defval, size_t defsize)
94 1.1 christos {
95 1.1 christos const void *value;
96 1.1 christos
97 1.1 christos if (nvlist_exists_binary(nvl, name))
98 1.1 christos value = nvlist_get_binary(nvl, name, sizep);
99 1.1 christos else {
100 1.1 christos if (sizep != NULL)
101 1.1 christos *sizep = defsize;
102 1.1 christos value = defval;
103 1.1 christos }
104 1.1 christos return (value);
105 1.1 christos }
106 1.1 christos
107 1.1 christos #define DNVLIST_TAKE(ftype, type) \
108 1.1 christos ftype \
109 1.1 christos dnvlist_take_##type(nvlist_t *nvl, const char *name, ftype defval) \
110 1.1 christos { \
111 1.1 christos \
112 1.1 christos if (nvlist_exists_##type(nvl, name)) \
113 1.1 christos return (nvlist_take_##type(nvl, name)); \
114 1.1 christos else \
115 1.1 christos return (defval); \
116 1.1 christos }
117 1.1 christos
118 1.1 christos DNVLIST_TAKE(bool, bool)
119 1.1 christos DNVLIST_TAKE(uint64_t, number)
120 1.1 christos DNVLIST_TAKE(char *, string)
121 1.1 christos DNVLIST_TAKE(nvlist_t *, nvlist)
122 1.2 christos #if !defined(_KERNEL) && !defined(_STANDALONE)
123 1.1 christos DNVLIST_TAKE(int, descriptor)
124 1.1 christos #endif
125 1.1 christos
126 1.1 christos #undef DNVLIST_TAKE
127 1.1 christos
128 1.1 christos void *
129 1.1 christos dnvlist_take_binary(nvlist_t *nvl, const char *name, size_t *sizep,
130 1.1 christos void *defval, size_t defsize)
131 1.1 christos {
132 1.1 christos void *value;
133 1.1 christos
134 1.1 christos if (nvlist_exists_binary(nvl, name))
135 1.1 christos value = nvlist_take_binary(nvl, name, sizep);
136 1.1 christos else {
137 1.1 christos if (sizep != NULL)
138 1.1 christos *sizep = defsize;
139 1.1 christos value = defval;
140 1.1 christos }
141 1.1 christos return (value);
142 1.1 christos }
143 1.1 christos
144