Home | History | Annotate | Line # | Download | only in dist
cnvlist.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) 2016 Adam Starak <starak.adam (at) gmail.com>
      5  1.1  christos  * All rights reserved.
      6  1.1  christos  *
      7  1.1  christos  * Redistribution and use in source and binary forms, with or without
      8  1.1  christos  * modification, are permitted provided that the following conditions
      9  1.1  christos  * are met:
     10  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     11  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     12  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  christos  *    documentation and/or other materials provided with the distribution.
     15  1.1  christos  *
     16  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
     17  1.1  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  1.1  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  1.1  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
     20  1.1  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  1.1  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1  christos  * SUCH DAMAGE.
     27  1.1  christos  *
     28  1.1  christos  * $FreeBSD: head/sys/contrib/libnv/cnvlist.c 335343 2018-06-18 21:26:58Z oshogbo $
     29  1.1  christos  */
     30  1.1  christos 
     31  1.1  christos #include <sys/cdefs.h>
     32  1.1  christos __FBSDID("$FreeBSD: head/sys/contrib/libnv/cnvlist.c 335343 2018-06-18 21:26:58Z oshogbo $");
     33  1.1  christos 
     34  1.1  christos #ifdef _KERNEL
     35  1.1  christos 
     36  1.1  christos #include <sys/types.h>
     37  1.1  christos #include <sys/param.h>
     38  1.1  christos #include <sys/kernel.h>
     39  1.1  christos #include <sys/systm.h>
     40  1.1  christos #include <sys/malloc.h>
     41  1.1  christos 
     42  1.1  christos #include <machine/stdarg.h>
     43  1.1  christos 
     44  1.1  christos #else
     45  1.1  christos #include <stdarg.h>
     46  1.1  christos #include <stdbool.h>
     47  1.1  christos #include <stdint.h>
     48  1.1  christos #include <stdlib.h>
     49  1.1  christos #endif
     50  1.1  christos 
     51  1.1  christos #include <sys/cnv.h>
     52  1.1  christos #include <sys/nv.h>
     53  1.1  christos 
     54  1.1  christos #include "nv_impl.h"
     55  1.1  christos #include "nvlist_impl.h"
     56  1.1  christos #include "nvpair_impl.h"
     57  1.1  christos 
     58  1.1  christos const char *
     59  1.1  christos cnvlist_name(const void *cookie)
     60  1.1  christos {
     61  1.1  christos 
     62  1.1  christos 	return (nvpair_name(cookie));
     63  1.1  christos }
     64  1.1  christos 
     65  1.1  christos int
     66  1.1  christos cnvlist_type(const void *cookie)
     67  1.1  christos {
     68  1.1  christos 
     69  1.1  christos 	return (nvpair_type(cookie));
     70  1.1  christos }
     71  1.1  christos 
     72  1.1  christos #define	CNVLIST_GET(ftype, type, NVTYPE)				\
     73  1.1  christos ftype									\
     74  1.1  christos cnvlist_get_##type(const void *cookie)					\
     75  1.1  christos {									\
     76  1.1  christos 									\
     77  1.1  christos 	if (nvpair_type(cookie) != NV_TYPE_##NVTYPE) {			\
     78  1.1  christos 		nvlist_report_missing(NV_TYPE_##NVTYPE,			\
     79  1.1  christos 		    nvpair_name(cookie));				\
     80  1.1  christos 	}								\
     81  1.1  christos         return (nvpair_get_##type(cookie));				\
     82  1.1  christos }
     83  1.1  christos 
     84  1.1  christos CNVLIST_GET(bool, bool, BOOL)
     85  1.1  christos CNVLIST_GET(uint64_t, number, NUMBER)
     86  1.1  christos CNVLIST_GET(const char *, string, STRING)
     87  1.1  christos CNVLIST_GET(const nvlist_t *, nvlist, NVLIST)
     88  1.1  christos #ifndef _KERNEL
     89  1.1  christos CNVLIST_GET(int, descriptor, DESCRIPTOR)
     90  1.1  christos #endif
     91  1.1  christos 
     92  1.1  christos #undef	CNVLIST_GET
     93  1.1  christos 
     94  1.1  christos #define	CNVLIST_GET_ARRAY(ftype, type, NVTYPE)				\
     95  1.1  christos ftype									\
     96  1.1  christos cnvlist_get_##type(const void *cookie, size_t *nitemsp)			\
     97  1.1  christos {									\
     98  1.1  christos 									\
     99  1.1  christos 	if (nvpair_type(cookie) != NV_TYPE_##NVTYPE) {			\
    100  1.1  christos 		nvlist_report_missing(NV_TYPE_##NVTYPE,			\
    101  1.1  christos 		    nvpair_name(cookie));				\
    102  1.1  christos 	}								\
    103  1.1  christos 	return (nvpair_get_##type(cookie, nitemsp));			\
    104  1.1  christos }
    105  1.1  christos 
    106  1.1  christos CNVLIST_GET_ARRAY(const bool *, bool_array, BOOL_ARRAY)
    107  1.1  christos CNVLIST_GET_ARRAY(const uint64_t *, number_array, NUMBER_ARRAY)
    108  1.1  christos CNVLIST_GET_ARRAY(const char * const *, string_array, STRING_ARRAY)
    109  1.1  christos CNVLIST_GET_ARRAY(const nvlist_t * const *, nvlist_array, NVLIST_ARRAY)
    110  1.1  christos #ifndef _KERNEL
    111  1.1  christos CNVLIST_GET_ARRAY(const int *, descriptor_array, DESCRIPTOR_ARRAY)
    112  1.1  christos #endif
    113  1.1  christos 
    114  1.1  christos #undef	CNVLIST_GET_ARRAY
    115  1.1  christos 
    116  1.1  christos const void *
    117  1.1  christos cnvlist_get_binary(const void *cookie, size_t *sizep)
    118  1.1  christos {
    119  1.1  christos 
    120  1.1  christos 	if (nvpair_type(cookie) != NV_TYPE_BINARY)
    121  1.1  christos 		nvlist_report_missing(NV_TYPE_BINARY, nvpair_name(cookie));
    122  1.1  christos 	return (nvpair_get_binary(cookie, sizep));
    123  1.1  christos }
    124  1.1  christos 
    125  1.1  christos #define CNVLIST_TAKE(ftype, type, NVTYPE)				\
    126  1.1  christos ftype									\
    127  1.1  christos cnvlist_take_##type(void *cookie)					\
    128  1.1  christos {									\
    129  1.1  christos 	ftype value;							\
    130  1.1  christos 	nvlist_t *nvl;							\
    131  1.1  christos 									\
    132  1.1  christos 	if (nvpair_type(cookie) != NV_TYPE_##NVTYPE) {			\
    133  1.1  christos 		nvlist_report_missing(NV_TYPE_##NVTYPE,			\
    134  1.1  christos 		    nvpair_name(cookie));				\
    135  1.1  christos 	}								\
    136  1.1  christos 	nvl = nvpair_nvlist(cookie);					\
    137  1.1  christos 	value = (ftype)(intptr_t)nvpair_get_##type(cookie);		\
    138  1.1  christos 	nvlist_remove_nvpair(nvl, cookie);				\
    139  1.1  christos 	nvpair_free_structure(cookie);					\
    140  1.1  christos 	return (value);							\
    141  1.1  christos }
    142  1.1  christos 
    143  1.1  christos CNVLIST_TAKE(bool, bool, BOOL)
    144  1.1  christos CNVLIST_TAKE(uint64_t, number, NUMBER)
    145  1.1  christos CNVLIST_TAKE(char *, string, STRING)
    146  1.1  christos CNVLIST_TAKE(nvlist_t *, nvlist, NVLIST)
    147  1.1  christos #ifndef _KERNEL
    148  1.1  christos CNVLIST_TAKE(int, descriptor, DESCRIPTOR)
    149  1.1  christos #endif
    150  1.1  christos 
    151  1.1  christos #undef	CNVLIST_TAKE
    152  1.1  christos 
    153  1.1  christos #define	CNVLIST_TAKE_ARRAY(ftype, type, NVTYPE)				\
    154  1.1  christos ftype									\
    155  1.1  christos cnvlist_take_##type(void *cookie, size_t *nitemsp)			\
    156  1.1  christos {									\
    157  1.1  christos 	ftype value;							\
    158  1.1  christos 	nvlist_t *nvl;							\
    159  1.1  christos 									\
    160  1.1  christos 	if (nvpair_type(cookie) != NV_TYPE_##NVTYPE) {			\
    161  1.1  christos 		nvlist_report_missing(NV_TYPE_##NVTYPE,			\
    162  1.1  christos 		    nvpair_name(cookie));				\
    163  1.1  christos 	}								\
    164  1.1  christos 	nvl = nvpair_nvlist(cookie);					\
    165  1.1  christos 	value = (ftype)(intptr_t)nvpair_get_##type(cookie, nitemsp);	\
    166  1.1  christos 	nvlist_remove_nvpair(nvl, cookie);				\
    167  1.1  christos 	nvpair_free_structure(cookie);					\
    168  1.1  christos 	return (value);							\
    169  1.1  christos }
    170  1.1  christos 
    171  1.1  christos CNVLIST_TAKE_ARRAY(bool *, bool_array, BOOL_ARRAY)
    172  1.1  christos CNVLIST_TAKE_ARRAY(uint64_t *, number_array, NUMBER_ARRAY)
    173  1.1  christos CNVLIST_TAKE_ARRAY(char **, string_array, STRING_ARRAY)
    174  1.1  christos CNVLIST_TAKE_ARRAY(nvlist_t **, nvlist_array, NVLIST_ARRAY)
    175  1.1  christos #ifndef _KERNEL
    176  1.1  christos CNVLIST_TAKE_ARRAY(int *, descriptor_array, DESCRIPTOR_ARRAY);
    177  1.1  christos #endif
    178  1.1  christos 
    179  1.1  christos #undef	CNVLIST_TAKE_ARRAY
    180  1.1  christos 
    181  1.1  christos void *
    182  1.1  christos cnvlist_take_binary(void *cookie, size_t *sizep)
    183  1.1  christos {
    184  1.1  christos 	void *value;
    185  1.1  christos 	nvlist_t *nvl;
    186  1.1  christos 
    187  1.1  christos 	if (nvpair_type(cookie) != NV_TYPE_BINARY)
    188  1.1  christos 		nvlist_report_missing(NV_TYPE_BINARY, nvpair_name(cookie));
    189  1.1  christos 	nvl = nvpair_nvlist(cookie);
    190  1.1  christos 	value = (void *)(intptr_t)nvpair_get_binary(cookie, sizep);
    191  1.1  christos 	nvlist_remove_nvpair(nvl, cookie);
    192  1.1  christos 	nvpair_free_structure(cookie);
    193  1.1  christos 	return (value);
    194  1.1  christos }
    195  1.1  christos 
    196  1.1  christos 
    197  1.1  christos #define	CNVLIST_FREE(type)						\
    198  1.1  christos void									\
    199  1.1  christos cnvlist_free_##type(void *cookie)					\
    200  1.1  christos {									\
    201  1.1  christos 									\
    202  1.1  christos 	nvlist_free_nvpair(nvpair_nvlist(cookie), cookie);		\
    203  1.1  christos }
    204  1.1  christos 
    205  1.1  christos CNVLIST_FREE(bool)
    206  1.1  christos CNVLIST_FREE(number)
    207  1.1  christos CNVLIST_FREE(string)
    208  1.1  christos CNVLIST_FREE(nvlist)
    209  1.1  christos CNVLIST_FREE(binary);
    210  1.1  christos CNVLIST_FREE(bool_array)
    211  1.1  christos CNVLIST_FREE(number_array)
    212  1.1  christos CNVLIST_FREE(string_array)
    213  1.1  christos CNVLIST_FREE(nvlist_array)
    214  1.1  christos #ifndef _KERNEL
    215  1.1  christos CNVLIST_FREE(descriptor)
    216  1.1  christos CNVLIST_FREE(descriptor_array)
    217  1.1  christos #endif
    218  1.1  christos 
    219  1.1  christos #undef	CNVLIST_FREE
    220