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