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