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