1/*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2013 The FreeBSD Foundation 5 * Copyright (c) 2013-2015 Mariusz Zaborski <oshogbo@FreeBSD.org> 6 * All rights reserved. 7 * 8 * This software was developed by Pawel Jakub Dawidek under sponsorship from 9 * the FreeBSD Foundation. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * $FreeBSD$ 33 */ 34 35#ifndef _NV_IMPL_H_ 36#define _NV_IMPL_H_ 37 38#ifndef _NVPAIR_T_DECLARED 39#define _NVPAIR_T_DECLARED 40struct nvpair; 41 42typedef struct nvpair nvpair_t; 43#endif 44 45#define NV_TYPE_NVLIST_ARRAY_NEXT 254 46#define NV_TYPE_NVLIST_UP 255 47 48#define NV_TYPE_FIRST NV_TYPE_NULL 49#define NV_TYPE_LAST NV_TYPE_DESCRIPTOR_ARRAY 50 51#define NV_FLAG_BIG_ENDIAN 0x080 52#define NV_FLAG_IN_ARRAY 0x100 53 54#ifdef _KERNEL 55#define nv_malloc(size) malloc((size), M_NVLIST, M_NOWAIT) 56#define nv_calloc(n, size) mallocarray((n), (size), M_NVLIST, \ 57 M_NOWAIT | M_ZERO) 58#define nv_realloc(buf, size) realloc((buf), (size), M_NVLIST, \ 59 M_NOWAIT) 60#define nv_free(buf) free((buf), M_NVLIST) 61#define nv_strdup(buf) strdup_flags((buf), M_NVLIST, M_NOWAIT) 62#define nv_vasprintf(ptr, ...) vasprintf(ptr, M_NVLIST, __VA_ARGS__) 63 64#define ERRNO_SET(var) do { } while (0) 65#define ERRNO_SAVE() do { do { } while(0) 66#define ERRNO_RESTORE() } while (0) 67 68#define ERRNO_OR_DEFAULT(default) (default) 69 70#else 71 72#define nv_malloc(size) malloc((size)) 73#define nv_calloc(n, size) calloc((n), (size)) 74#define nv_realloc(buf, size) realloc((buf), (size)) 75#define nv_free(buf) free((buf)) 76#define nv_strdup(buf) strdup((buf)) 77#define nv_vasprintf(ptr, ...) vasprintf(ptr, __VA_ARGS__) 78 79#define ERRNO_SET(var) do { errno = (var); } while (0) 80#define ERRNO_SAVE() do { \ 81 int _serrno; \ 82 \ 83 _serrno = errno 84 85#define ERRNO_RESTORE() errno = _serrno; \ 86 } while (0) 87 88#define ERRNO_OR_DEFAULT(default) (errno == 0 ? (default) : errno) 89 90#endif 91 92int *nvlist_descriptors(const nvlist_t *nvl, size_t *nitemsp); 93size_t nvlist_ndescriptors(const nvlist_t *nvl); 94void nvlist_set_flags(nvlist_t *nvl, int flags); 95 96nvpair_t *nvlist_first_nvpair(const nvlist_t *nvl); 97nvpair_t *nvlist_next_nvpair(const nvlist_t *nvl, const nvpair_t *nvp); 98nvpair_t *nvlist_prev_nvpair(const nvlist_t *nvl, const nvpair_t *nvp); 99 100void nvlist_add_nvpair(nvlist_t *nvl, const nvpair_t *nvp); 101 102bool nvlist_move_nvpair(nvlist_t *nvl, nvpair_t *nvp); 103 104void nvlist_set_parent(nvlist_t *nvl, nvpair_t *parent); 105void nvlist_set_array_next(nvlist_t *nvl, nvpair_t *ele); 106nvpair_t *nvlist_get_array_next_nvpair(nvlist_t *nvl); 107 108const nvpair_t *nvlist_get_nvpair(const nvlist_t *nvl, const char *name); 109 110nvpair_t *nvlist_take_nvpair(nvlist_t *nvl, const char *name); 111 112/* Function removes the given nvpair from the nvlist. */ 113void nvlist_remove_nvpair(nvlist_t *nvl, nvpair_t *nvp); 114 115void nvlist_free_nvpair(nvlist_t *nvl, nvpair_t *nvp); 116 117int nvpair_type(const nvpair_t *nvp); 118const char *nvpair_name(const nvpair_t *nvp); 119 120nvpair_t *nvpair_clone(const nvpair_t *nvp); 121 122nvpair_t *nvpair_create_null(const char *name); 123nvpair_t *nvpair_create_bool(const char *name, bool value); 124nvpair_t *nvpair_create_number(const char *name, uint64_t value); 125nvpair_t *nvpair_create_string(const char *name, const char *value); 126nvpair_t *nvpair_create_stringf(const char *name, const char *valuefmt, ...) __printflike(2, 3); 127nvpair_t *nvpair_create_stringv(const char *name, const char *valuefmt, va_list valueap) __printflike(2, 0); 128nvpair_t *nvpair_create_nvlist(const char *name, const nvlist_t *value); 129nvpair_t *nvpair_create_descriptor(const char *name, int value); 130nvpair_t *nvpair_create_binary(const char *name, const void *value, size_t size); 131nvpair_t *nvpair_create_bool_array(const char *name, const bool *value, size_t nitems); 132nvpair_t *nvpair_create_number_array(const char *name, const uint64_t *value, size_t nitems); 133nvpair_t *nvpair_create_string_array(const char *name, const char * const *value, size_t nitems); 134nvpair_t *nvpair_create_nvlist_array(const char *name, const nvlist_t * const *value, size_t nitems); 135nvpair_t *nvpair_create_descriptor_array(const char *name, const int *value, size_t nitems); 136 137nvpair_t *nvpair_move_string(const char *name, char *value); 138nvpair_t *nvpair_move_nvlist(const char *name, nvlist_t *value); 139nvpair_t *nvpair_move_descriptor(const char *name, int value); 140nvpair_t *nvpair_move_binary(const char *name, void *value, size_t size); 141nvpair_t *nvpair_move_bool_array(const char *name, bool *value, size_t nitems); 142nvpair_t *nvpair_move_nvlist_array(const char *name, nvlist_t **value, size_t nitems); 143nvpair_t *nvpair_move_descriptor_array(const char *name, int *value, size_t nitems); 144nvpair_t *nvpair_move_number_array(const char *name, uint64_t *value, size_t nitems); 145nvpair_t *nvpair_move_string_array(const char *name, char **value, size_t nitems); 146 147int nvpair_append_bool_array(nvpair_t *nvp, const bool value); 148int nvpair_append_number_array(nvpair_t *nvp, const uint64_t value); 149int nvpair_append_string_array(nvpair_t *nvp, const char *value); 150int nvpair_append_nvlist_array(nvpair_t *nvp, const nvlist_t *value); 151int nvpair_append_descriptor_array(nvpair_t *nvp, const int value); 152 153bool nvpair_get_bool(const nvpair_t *nvp); 154uint64_t nvpair_get_number(const nvpair_t *nvp); 155const char *nvpair_get_string(const nvpair_t *nvp); 156const nvlist_t *nvpair_get_nvlist(const nvpair_t *nvp); 157int nvpair_get_descriptor(const nvpair_t *nvp); 158const void *nvpair_get_binary(const nvpair_t *nvp, size_t *sizep); 159const bool *nvpair_get_bool_array(const nvpair_t *nvp, size_t *nitemsp); 160const uint64_t *nvpair_get_number_array(const nvpair_t *nvp, size_t *nitemsp); 161const char * const *nvpair_get_string_array(const nvpair_t *nvp, size_t *nitemsp); 162const nvlist_t * const *nvpair_get_nvlist_array(const nvpair_t *nvp, size_t *nitemsp); 163const int *nvpair_get_descriptor_array(const nvpair_t *nvp, size_t *nitemsp); 164 165void nvpair_free(nvpair_t *nvp); 166 167#endif /* !_NV_IMPL_H_ */ 168