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