8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / common / nvpair / fnvpair.c
blobb3cf173f6499ad565e4f94e8c0d203ca3c021a6f
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright (c) 2012 by Delphix. All rights reserved.
26 #include <sys/nvpair.h>
27 #include <sys/kmem.h>
28 #include <sys/debug.h>
29 #include <sys/param.h>
30 #ifndef _KERNEL
31 #include <stdlib.h>
32 #endif
35 * "Force" nvlist wrapper.
37 * These functions wrap the nvlist_* functions with assertions that assume
38 * the operation is successful. This allows the caller's code to be much
39 * more readable, especially for the fnvlist_lookup_* and fnvpair_value_*
40 * functions, which can return the requested value (rather than filling in
41 * a pointer).
43 * These functions use NV_UNIQUE_NAME, encoding NV_ENCODE_NATIVE, and allocate
44 * with KM_SLEEP.
46 * More wrappers should be added as needed -- for example
47 * nvlist_lookup_*_array and nvpair_value_*_array.
50 nvlist_t *
51 fnvlist_alloc(void)
53 nvlist_t *nvl;
54 VERIFY0(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP));
55 return (nvl);
58 void
59 fnvlist_free(nvlist_t *nvl)
61 nvlist_free(nvl);
64 size_t
65 fnvlist_size(nvlist_t *nvl)
67 size_t size;
68 VERIFY0(nvlist_size(nvl, &size, NV_ENCODE_NATIVE));
69 return (size);
73 * Returns allocated buffer of size *sizep. Caller must free the buffer with
74 * fnvlist_pack_free().
76 char *
77 fnvlist_pack(nvlist_t *nvl, size_t *sizep)
79 char *packed = 0;
80 VERIFY3U(nvlist_pack(nvl, &packed, sizep, NV_ENCODE_NATIVE,
81 KM_SLEEP), ==, 0);
82 return (packed);
85 /*ARGSUSED*/
86 void
87 fnvlist_pack_free(char *pack, size_t size)
89 #ifdef _KERNEL
90 kmem_free(pack, size);
91 #else
92 free(pack);
93 #endif
96 nvlist_t *
97 fnvlist_unpack(char *buf, size_t buflen)
99 nvlist_t *rv;
100 VERIFY0(nvlist_unpack(buf, buflen, &rv, KM_SLEEP));
101 return (rv);
104 nvlist_t *
105 fnvlist_dup(nvlist_t *nvl)
107 nvlist_t *rv;
108 VERIFY0(nvlist_dup(nvl, &rv, KM_SLEEP));
109 return (rv);
112 void
113 fnvlist_merge(nvlist_t *dst, nvlist_t *src)
115 VERIFY0(nvlist_merge(dst, src, KM_SLEEP));
118 size_t
119 fnvlist_num_pairs(nvlist_t *nvl)
121 size_t count = 0;
122 nvpair_t *pair;
124 for (pair = nvlist_next_nvpair(nvl, 0); pair != NULL;
125 pair = nvlist_next_nvpair(nvl, pair))
126 count++;
127 return (count);
130 void
131 fnvlist_add_boolean(nvlist_t *nvl, const char *name)
133 VERIFY0(nvlist_add_boolean(nvl, name));
136 void
137 fnvlist_add_boolean_value(nvlist_t *nvl, const char *name, boolean_t val)
139 VERIFY0(nvlist_add_boolean_value(nvl, name, val));
142 void
143 fnvlist_add_byte(nvlist_t *nvl, const char *name, uchar_t val)
145 VERIFY0(nvlist_add_byte(nvl, name, val));
148 void
149 fnvlist_add_int8(nvlist_t *nvl, const char *name, int8_t val)
151 VERIFY0(nvlist_add_int8(nvl, name, val));
154 void
155 fnvlist_add_uint8(nvlist_t *nvl, const char *name, uint8_t val)
157 VERIFY0(nvlist_add_uint8(nvl, name, val));
160 void
161 fnvlist_add_int16(nvlist_t *nvl, const char *name, int16_t val)
163 VERIFY0(nvlist_add_int16(nvl, name, val));
166 void
167 fnvlist_add_uint16(nvlist_t *nvl, const char *name, uint16_t val)
169 VERIFY0(nvlist_add_uint16(nvl, name, val));
172 void
173 fnvlist_add_int32(nvlist_t *nvl, const char *name, int32_t val)
175 VERIFY0(nvlist_add_int32(nvl, name, val));
178 void
179 fnvlist_add_uint32(nvlist_t *nvl, const char *name, uint32_t val)
181 VERIFY0(nvlist_add_uint32(nvl, name, val));
184 void
185 fnvlist_add_int64(nvlist_t *nvl, const char *name, int64_t val)
187 VERIFY0(nvlist_add_int64(nvl, name, val));
190 void
191 fnvlist_add_uint64(nvlist_t *nvl, const char *name, uint64_t val)
193 VERIFY0(nvlist_add_uint64(nvl, name, val));
196 void
197 fnvlist_add_string(nvlist_t *nvl, const char *name, const char *val)
199 VERIFY0(nvlist_add_string(nvl, name, val));
202 void
203 fnvlist_add_nvlist(nvlist_t *nvl, const char *name, nvlist_t *val)
205 VERIFY0(nvlist_add_nvlist(nvl, name, val));
208 void
209 fnvlist_add_nvpair(nvlist_t *nvl, nvpair_t *pair)
211 VERIFY0(nvlist_add_nvpair(nvl, pair));
214 void
215 fnvlist_add_boolean_array(nvlist_t *nvl, const char *name,
216 boolean_t *val, uint_t n)
218 VERIFY0(nvlist_add_boolean_array(nvl, name, val, n));
221 void
222 fnvlist_add_byte_array(nvlist_t *nvl, const char *name, uchar_t *val, uint_t n)
224 VERIFY0(nvlist_add_byte_array(nvl, name, val, n));
227 void
228 fnvlist_add_int8_array(nvlist_t *nvl, const char *name, int8_t *val, uint_t n)
230 VERIFY0(nvlist_add_int8_array(nvl, name, val, n));
233 void
234 fnvlist_add_uint8_array(nvlist_t *nvl, const char *name, uint8_t *val, uint_t n)
236 VERIFY0(nvlist_add_uint8_array(nvl, name, val, n));
239 void
240 fnvlist_add_int16_array(nvlist_t *nvl, const char *name, int16_t *val, uint_t n)
242 VERIFY0(nvlist_add_int16_array(nvl, name, val, n));
245 void
246 fnvlist_add_uint16_array(nvlist_t *nvl, const char *name,
247 uint16_t *val, uint_t n)
249 VERIFY0(nvlist_add_uint16_array(nvl, name, val, n));
252 void
253 fnvlist_add_int32_array(nvlist_t *nvl, const char *name, int32_t *val, uint_t n)
255 VERIFY0(nvlist_add_int32_array(nvl, name, val, n));
258 void
259 fnvlist_add_uint32_array(nvlist_t *nvl, const char *name,
260 uint32_t *val, uint_t n)
262 VERIFY0(nvlist_add_uint32_array(nvl, name, val, n));
265 void
266 fnvlist_add_int64_array(nvlist_t *nvl, const char *name, int64_t *val, uint_t n)
268 VERIFY0(nvlist_add_int64_array(nvl, name, val, n));
271 void
272 fnvlist_add_uint64_array(nvlist_t *nvl, const char *name,
273 uint64_t *val, uint_t n)
275 VERIFY0(nvlist_add_uint64_array(nvl, name, val, n));
278 void
279 fnvlist_add_string_array(nvlist_t *nvl, const char *name,
280 char * const *val, uint_t n)
282 VERIFY0(nvlist_add_string_array(nvl, name, val, n));
285 void
286 fnvlist_add_nvlist_array(nvlist_t *nvl, const char *name,
287 nvlist_t **val, uint_t n)
289 VERIFY0(nvlist_add_nvlist_array(nvl, name, val, n));
292 void
293 fnvlist_remove(nvlist_t *nvl, const char *name)
295 VERIFY0(nvlist_remove_all(nvl, name));
298 void
299 fnvlist_remove_nvpair(nvlist_t *nvl, nvpair_t *pair)
301 VERIFY0(nvlist_remove_nvpair(nvl, pair));
304 nvpair_t *
305 fnvlist_lookup_nvpair(nvlist_t *nvl, const char *name)
307 nvpair_t *rv;
308 VERIFY0(nvlist_lookup_nvpair(nvl, name, &rv));
309 return (rv);
312 /* returns B_TRUE if the entry exists */
313 boolean_t
314 fnvlist_lookup_boolean(nvlist_t *nvl, const char *name)
316 return (nvlist_lookup_boolean(nvl, name) == 0);
319 boolean_t
320 fnvlist_lookup_boolean_value(nvlist_t *nvl, const char *name)
322 boolean_t rv;
323 VERIFY0(nvlist_lookup_boolean_value(nvl, name, &rv));
324 return (rv);
327 uchar_t
328 fnvlist_lookup_byte(nvlist_t *nvl, const char *name)
330 uchar_t rv;
331 VERIFY0(nvlist_lookup_byte(nvl, name, &rv));
332 return (rv);
335 int8_t
336 fnvlist_lookup_int8(nvlist_t *nvl, const char *name)
338 int8_t rv;
339 VERIFY0(nvlist_lookup_int8(nvl, name, &rv));
340 return (rv);
343 int16_t
344 fnvlist_lookup_int16(nvlist_t *nvl, const char *name)
346 int16_t rv;
347 VERIFY0(nvlist_lookup_int16(nvl, name, &rv));
348 return (rv);
351 int32_t
352 fnvlist_lookup_int32(nvlist_t *nvl, const char *name)
354 int32_t rv;
355 VERIFY0(nvlist_lookup_int32(nvl, name, &rv));
356 return (rv);
359 int64_t
360 fnvlist_lookup_int64(nvlist_t *nvl, const char *name)
362 int64_t rv;
363 VERIFY0(nvlist_lookup_int64(nvl, name, &rv));
364 return (rv);
367 uint8_t
368 fnvlist_lookup_uint8_t(nvlist_t *nvl, const char *name)
370 uint8_t rv;
371 VERIFY0(nvlist_lookup_uint8(nvl, name, &rv));
372 return (rv);
375 uint16_t
376 fnvlist_lookup_uint16(nvlist_t *nvl, const char *name)
378 uint16_t rv;
379 VERIFY0(nvlist_lookup_uint16(nvl, name, &rv));
380 return (rv);
383 uint32_t
384 fnvlist_lookup_uint32(nvlist_t *nvl, const char *name)
386 uint32_t rv;
387 VERIFY0(nvlist_lookup_uint32(nvl, name, &rv));
388 return (rv);
391 uint64_t
392 fnvlist_lookup_uint64(nvlist_t *nvl, const char *name)
394 uint64_t rv;
395 VERIFY0(nvlist_lookup_uint64(nvl, name, &rv));
396 return (rv);
399 char *
400 fnvlist_lookup_string(nvlist_t *nvl, const char *name)
402 char *rv;
403 VERIFY0(nvlist_lookup_string(nvl, name, &rv));
404 return (rv);
407 nvlist_t *
408 fnvlist_lookup_nvlist(nvlist_t *nvl, const char *name)
410 nvlist_t *rv;
411 VERIFY0(nvlist_lookup_nvlist(nvl, name, &rv));
412 return (rv);
415 boolean_t
416 fnvpair_value_boolean_value(nvpair_t *nvp)
418 boolean_t rv;
419 VERIFY0(nvpair_value_boolean_value(nvp, &rv));
420 return (rv);
423 uchar_t
424 fnvpair_value_byte(nvpair_t *nvp)
426 uchar_t rv;
427 VERIFY0(nvpair_value_byte(nvp, &rv));
428 return (rv);
431 int8_t
432 fnvpair_value_int8(nvpair_t *nvp)
434 int8_t rv;
435 VERIFY0(nvpair_value_int8(nvp, &rv));
436 return (rv);
439 int16_t
440 fnvpair_value_int16(nvpair_t *nvp)
442 int16_t rv;
443 VERIFY0(nvpair_value_int16(nvp, &rv));
444 return (rv);
447 int32_t
448 fnvpair_value_int32(nvpair_t *nvp)
450 int32_t rv;
451 VERIFY0(nvpair_value_int32(nvp, &rv));
452 return (rv);
455 int64_t
456 fnvpair_value_int64(nvpair_t *nvp)
458 int64_t rv;
459 VERIFY0(nvpair_value_int64(nvp, &rv));
460 return (rv);
463 uint8_t
464 fnvpair_value_uint8_t(nvpair_t *nvp)
466 uint8_t rv;
467 VERIFY0(nvpair_value_uint8(nvp, &rv));
468 return (rv);
471 uint16_t
472 fnvpair_value_uint16(nvpair_t *nvp)
474 uint16_t rv;
475 VERIFY0(nvpair_value_uint16(nvp, &rv));
476 return (rv);
479 uint32_t
480 fnvpair_value_uint32(nvpair_t *nvp)
482 uint32_t rv;
483 VERIFY0(nvpair_value_uint32(nvp, &rv));
484 return (rv);
487 uint64_t
488 fnvpair_value_uint64(nvpair_t *nvp)
490 uint64_t rv;
491 VERIFY0(nvpair_value_uint64(nvp, &rv));
492 return (rv);
495 char *
496 fnvpair_value_string(nvpair_t *nvp)
498 char *rv;
499 VERIFY0(nvpair_value_string(nvp, &rv));
500 return (rv);
503 nvlist_t *
504 fnvpair_value_nvlist(nvpair_t *nvp)
506 nvlist_t *rv;
507 VERIFY0(nvpair_value_nvlist(nvp, &rv));
508 return (rv);