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