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