2 * Copyright (c) 2013 The FreeBSD Foundation
5 * This software was developed by Pawel Jakub Dawidek under sponsorship from
6 * the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #ifndef _NVPAIR_T_DECLARED
36 #define _NVPAIR_T_DECLARED
39 typedef struct nvpair nvpair_t;
42 #define NV_TYPE_NVLIST_UP 255
44 #define NV_TYPE_FIRST NV_TYPE_NULL
45 #define NV_TYPE_LAST NV_TYPE_BINARY
47 #define NV_FLAG_BIG_ENDIAN 0x80
50 #define nv_malloc(size) malloc((size), M_NVLIST, M_WAITOK)
51 #define nv_calloc(n, size) malloc((n) * (size), M_NVLIST, \
53 #define nv_realloc(buf, size) realloc((buf), (size), M_NVLIST, \
55 #define nv_free(buf) free((buf), M_NVLIST)
56 #define nv_strdup(buf) strdup((buf), M_NVLIST)
57 #define nv_vasprintf(ptr, ...) vasprintf(ptr, M_NVLIST, __VA_ARGS__)
59 #define ERRNO_SET(var) do { } while (0)
60 #define ERRNO_SAVE() do { do { } while(0)
61 #define ERRNO_RESTORE() } while (0)
63 #define ERRNO_OR_DEFAULT(default) (default)
67 #define nv_malloc(size) malloc((size))
68 #define nv_calloc(n, size) calloc((n), (size))
69 #define nv_realloc(buf, size) realloc((buf), (size))
70 #define nv_free(buf) free((buf))
71 #define nv_strdup(buf) strdup((buf))
72 #define nv_vasprintf(ptr, ...) vasprintf(ptr, __VA_ARGS__)
74 #define ERRNO_SET(var) do { errno = (var); } while (0)
75 #define ERRNO_SAVE() do { \
80 #define ERRNO_RESTORE() errno = _serrno; \
83 #define ERRNO_OR_DEFAULT(default) (errno == 0 ? (default) : errno)
87 int *nvlist_descriptors(const nvlist_t *nvl, size_t *nitemsp);
88 size_t nvlist_ndescriptors(const nvlist_t *nvl);
90 nvpair_t *nvlist_first_nvpair(const nvlist_t *nvl);
91 nvpair_t *nvlist_next_nvpair(const nvlist_t *nvl, const nvpair_t *nvp);
92 nvpair_t *nvlist_prev_nvpair(const nvlist_t *nvl, const nvpair_t *nvp);
94 void nvlist_add_nvpair(nvlist_t *nvl, const nvpair_t *nvp);
96 void nvlist_move_nvpair(nvlist_t *nvl, nvpair_t *nvp);
98 void nvlist_set_parent(nvlist_t *nvl, nvpair_t *parent);
100 const nvpair_t *nvlist_get_nvpair(const nvlist_t *nvl, const char *name);
102 nvpair_t *nvlist_take_nvpair(nvlist_t *nvl, const char *name);
104 /* Function removes the given nvpair from the nvlist. */
105 void nvlist_remove_nvpair(nvlist_t *nvl, nvpair_t *nvp);
107 void nvlist_free_nvpair(nvlist_t *nvl, nvpair_t *nvp);
109 int nvpair_type(const nvpair_t *nvp);
110 const char *nvpair_name(const nvpair_t *nvp);
112 nvpair_t *nvpair_clone(const nvpair_t *nvp);
114 nvpair_t *nvpair_create_null(const char *name);
115 nvpair_t *nvpair_create_bool(const char *name, bool value);
116 nvpair_t *nvpair_create_number(const char *name, uint64_t value);
117 nvpair_t *nvpair_create_string(const char *name, const char *value);
118 nvpair_t *nvpair_create_stringf(const char *name, const char *valuefmt, ...) __printflike(2, 3);
119 nvpair_t *nvpair_create_stringv(const char *name, const char *valuefmt, va_list valueap) __printflike(2, 0);
120 nvpair_t *nvpair_create_nvlist(const char *name, const nvlist_t *value);
121 nvpair_t *nvpair_create_descriptor(const char *name, int value);
122 nvpair_t *nvpair_create_binary(const char *name, const void *value, size_t size);
124 nvpair_t *nvpair_move_string(const char *name, char *value);
125 nvpair_t *nvpair_move_nvlist(const char *name, nvlist_t *value);
126 nvpair_t *nvpair_move_descriptor(const char *name, int value);
127 nvpair_t *nvpair_move_binary(const char *name, void *value, size_t size);
129 bool nvpair_get_bool(const nvpair_t *nvp);
130 uint64_t nvpair_get_number(const nvpair_t *nvp);
131 const char *nvpair_get_string(const nvpair_t *nvp);
132 const nvlist_t *nvpair_get_nvlist(const nvpair_t *nvp);
133 int nvpair_get_descriptor(const nvpair_t *nvp);
134 const void *nvpair_get_binary(const nvpair_t *nvp, size_t *sizep);
136 void nvpair_free(nvpair_t *nvp);
138 #endif /* !_NV_IMPL_H_ */