1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 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 40 struct nvpair; 41 42 typedef 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 92 int *nvlist_descriptors(const nvlist_t *nvl, size_t *nitemsp); 93 size_t nvlist_ndescriptors(const nvlist_t *nvl); 94 void nvlist_set_flags(nvlist_t *nvl, int flags); 95 96 nvpair_t *nvlist_first_nvpair(const nvlist_t *nvl); 97 nvpair_t *nvlist_next_nvpair(const nvlist_t *nvl, const nvpair_t *nvp); 98 nvpair_t *nvlist_prev_nvpair(const nvlist_t *nvl, const nvpair_t *nvp); 99 100 void nvlist_add_nvpair(nvlist_t *nvl, const nvpair_t *nvp); 101 102 bool nvlist_move_nvpair(nvlist_t *nvl, nvpair_t *nvp); 103 104 void nvlist_set_parent(nvlist_t *nvl, nvpair_t *parent); 105 void nvlist_set_array_next(nvlist_t *nvl, nvpair_t *ele); 106 nvpair_t *nvlist_get_array_next_nvpair(nvlist_t *nvl); 107 108 const nvpair_t *nvlist_get_nvpair(const nvlist_t *nvl, const char *name); 109 110 nvpair_t *nvlist_take_nvpair(nvlist_t *nvl, const char *name); 111 112 /* Function removes the given nvpair from the nvlist. */ 113 void nvlist_remove_nvpair(nvlist_t *nvl, nvpair_t *nvp); 114 115 void nvlist_free_nvpair(nvlist_t *nvl, nvpair_t *nvp); 116 117 int nvpair_type(const nvpair_t *nvp); 118 const char *nvpair_name(const nvpair_t *nvp); 119 120 nvpair_t *nvpair_clone(const nvpair_t *nvp); 121 122 nvpair_t *nvpair_create_null(const char *name); 123 nvpair_t *nvpair_create_bool(const char *name, bool value); 124 nvpair_t *nvpair_create_number(const char *name, uint64_t value); 125 nvpair_t *nvpair_create_string(const char *name, const char *value); 126 nvpair_t *nvpair_create_stringf(const char *name, const char *valuefmt, ...) __printflike(2, 3); 127 nvpair_t *nvpair_create_stringv(const char *name, const char *valuefmt, va_list valueap) __printflike(2, 0); 128 nvpair_t *nvpair_create_nvlist(const char *name, const nvlist_t *value); 129 nvpair_t *nvpair_create_descriptor(const char *name, int value); 130 nvpair_t *nvpair_create_binary(const char *name, const void *value, size_t size); 131 nvpair_t *nvpair_create_bool_array(const char *name, const bool *value, size_t nitems); 132 nvpair_t *nvpair_create_number_array(const char *name, const uint64_t *value, size_t nitems); 133 nvpair_t *nvpair_create_string_array(const char *name, const char * const *value, size_t nitems); 134 nvpair_t *nvpair_create_nvlist_array(const char *name, const nvlist_t * const *value, size_t nitems); 135 nvpair_t *nvpair_create_descriptor_array(const char *name, const int *value, size_t nitems); 136 137 nvpair_t *nvpair_move_string(const char *name, char *value); 138 nvpair_t *nvpair_move_nvlist(const char *name, nvlist_t *value); 139 nvpair_t *nvpair_move_descriptor(const char *name, int value); 140 nvpair_t *nvpair_move_binary(const char *name, void *value, size_t size); 141 nvpair_t *nvpair_move_bool_array(const char *name, bool *value, size_t nitems); 142 nvpair_t *nvpair_move_nvlist_array(const char *name, nvlist_t **value, size_t nitems); 143 nvpair_t *nvpair_move_descriptor_array(const char *name, int *value, size_t nitems); 144 nvpair_t *nvpair_move_number_array(const char *name, uint64_t *value, size_t nitems); 145 nvpair_t *nvpair_move_string_array(const char *name, char **value, size_t nitems); 146 147 int nvpair_append_bool_array(nvpair_t *nvp, const bool value); 148 int nvpair_append_number_array(nvpair_t *nvp, const uint64_t value); 149 int nvpair_append_string_array(nvpair_t *nvp, const char *value); 150 int nvpair_append_nvlist_array(nvpair_t *nvp, const nvlist_t *value); 151 int nvpair_append_descriptor_array(nvpair_t *nvp, const int value); 152 153 bool nvpair_get_bool(const nvpair_t *nvp); 154 uint64_t nvpair_get_number(const nvpair_t *nvp); 155 const char *nvpair_get_string(const nvpair_t *nvp); 156 const nvlist_t *nvpair_get_nvlist(const nvpair_t *nvp); 157 int nvpair_get_descriptor(const nvpair_t *nvp); 158 const void *nvpair_get_binary(const nvpair_t *nvp, size_t *sizep); 159 const bool *nvpair_get_bool_array(const nvpair_t *nvp, size_t *nitemsp); 160 const uint64_t *nvpair_get_number_array(const nvpair_t *nvp, size_t *nitemsp); 161 const char * const *nvpair_get_string_array(const nvpair_t *nvp, size_t *nitemsp); 162 const nvlist_t * const *nvpair_get_nvlist_array(const nvpair_t *nvp, size_t *nitemsp); 163 const int *nvpair_get_descriptor_array(const nvpair_t *nvp, size_t *nitemsp); 164 165 void nvpair_free(nvpair_t *nvp); 166 167 #endif /* !_NV_IMPL_H_ */ 168