1/* $NetBSD: nv_impl.h,v 1.6 2019/02/12 12:49:23 rmind 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# define nv_vasprintf(ptr, ...) vasprintf(ptr, M_NVLIST, __VA_ARGS__)
76#elif defined(_STANDALONE)
77extern void *nv_malloc(size_t);
78extern void *nv_calloc(size_t, size_t);
79extern void *nv_realloc(void *, size_t);
80extern void nv_free(void *);
81extern char *nv_strdup(const char *);
82# define nv_vasprintf(ptr, ...) vasprintf(ptr, M_NVLIST, __VA_ARGS__)
83#else /* USERLAND */
84
85# define nv_malloc(size) malloc((size))
86# define nv_realloc(buf, size) realloc((buf), (size))
87# define nv_free(buf) free((buf))
88# define nv_vasprintf(ptr, ...) vasprintf(ptr, __VA_ARGS__)
89void *nv_calloc(size_t, size_t);
90char *nv_strdup(const char *);
91
92# define ERRNO_SET(var) do { \
93 errno = (var); \
94 } while (/*CONSTCOND*/0)
95# define ERRNO_SAVE() do { \
96 int _serrno; \
97 _serrno = errno
98
99# define ERRNO_RESTORE() errno = _serrno; \
100 } while (/*CONSTCOND*/0)
101
102# define ERRNO_OR_DEFAULT(default) (errno == 0 ? (default) : errno)
103
104#endif
105
106#ifndef ERRNO_SET
107# define ERRNO_SET(var) do { } while (/*CONSTCOND*/0)
108# define ERRNO_SAVE() do { do { } while(/*CONSTCOND*/0)
109# define ERRNO_RESTORE() } while (/*CONSTCOND*/0)
110
111# define ERRNO_OR_DEFAULT(default) (default)
112#endif
113
114int *nvlist_descriptors(const nvlist_t *nvl, size_t *nitemsp);
115size_t nvlist_ndescriptors(const nvlist_t *nvl);
116void nvlist_set_flags(nvlist_t *nvl, int flags);
117
118nvpair_t *nvlist_first_nvpair(const nvlist_t *nvl);
119nvpair_t *nvlist_next_nvpair(const nvlist_t *nvl, const nvpair_t *nvp);
120nvpair_t *nvlist_prev_nvpair(const nvlist_t *nvl, const nvpair_t *nvp);
121
122void nvlist_add_nvpair(nvlist_t *nvl, const nvpair_t *nvp);
123
124bool nvlist_move_nvpair(nvlist_t *nvl, nvpair_t *nvp);
125
126void nvlist_set_parent(nvlist_t *nvl, nvpair_t *parent);
127void nvlist_set_array_next(nvlist_t *nvl, nvpair_t *ele);
128nvpair_t *nvlist_get_array_next_nvpair(nvlist_t *nvl);
129
130const nvpair_t *nvlist_get_nvpair(const nvlist_t *nvl, const char *name);
131
132nvpair_t *nvlist_take_nvpair(nvlist_t *nvl, const char *name);
133
134/* Function removes the given nvpair from the nvlist. */
135void nvlist_remove_nvpair(nvlist_t *nvl, nvpair_t *nvp);
136
137void nvlist_free_nvpair(nvlist_t *nvl, nvpair_t *nvp);
138
139int nvpair_type(const nvpair_t *nvp);
140const char *nvpair_name(const nvpair_t *nvp);
141
142nvpair_t *nvpair_clone(const nvpair_t *nvp);
143
144nvpair_t *nvpair_create_null(const char *name);
145nvpair_t *nvpair_create_bool(const char *name, bool value);
146nvpair_t *nvpair_create_number(const char *name, uint64_t value);
147nvpair_t *nvpair_create_string(const char *name, const char *value);
148nvpair_t *nvpair_create_stringf(const char *name, const char *valuefmt, ...) __printflike(2, 3);
149nvpair_t *nvpair_create_stringv(const char *name, const char *valuefmt, va_list valueap) __printflike(2, 0);
150nvpair_t *nvpair_create_nvlist(const char *name, const nvlist_t *value);
151nvpair_t *nvpair_create_descriptor(const char *name, int value);
152nvpair_t *nvpair_create_binary(const char *name, const void *value, size_t size);
153nvpair_t *nvpair_create_bool_array(const char *name, const bool *value, size_t nitems);
154nvpair_t *nvpair_create_number_array(const char *name, const uint64_t *value, size_t nitems);
155nvpair_t *nvpair_create_string_array(const char *name, const char * const *value, size_t nitems);
156nvpair_t *nvpair_create_nvlist_array(const char *name, const nvlist_t * const *value, size_t nitems);
157nvpair_t *nvpair_create_descriptor_array(const char *name, const int *value, size_t nitems);
158
159nvpair_t *nvpair_move_string(const char *name, char *value);
160nvpair_t *nvpair_move_nvlist(const char *name, nvlist_t *value);
161nvpair_t *nvpair_move_descriptor(const char *name, int value);
162nvpair_t *nvpair_move_binary(const char *name, void *value, size_t size);
163nvpair_t *nvpair_move_bool_array(const char *name, bool *value, size_t nitems);
164nvpair_t *nvpair_move_nvlist_array(const char *name, nvlist_t **value, size_t nitems);
165nvpair_t *nvpair_move_descriptor_array(const char *name, int *value, size_t nitems);
166nvpair_t *nvpair_move_number_array(const char *name, uint64_t *value, size_t nitems);
167nvpair_t *nvpair_move_string_array(const char *name, char **value, size_t nitems);
168
169int nvpair_append_bool_array(nvpair_t *nvp, const bool value);
170int nvpair_append_number_array(nvpair_t *nvp, const uint64_t value);
171int nvpair_append_string_array(nvpair_t *nvp, const char *value);
172int nvpair_append_nvlist_array(nvpair_t *nvp, const nvlist_t *value);
173int nvpair_append_descriptor_array(nvpair_t *nvp, const int value);
174
175bool nvpair_get_bool(const nvpair_t *nvp);
176uint64_t nvpair_get_number(const nvpair_t *nvp);
177const char *nvpair_get_string(const nvpair_t *nvp);
178const nvlist_t *nvpair_get_nvlist(const nvpair_t *nvp);
179int nvpair_get_descriptor(const nvpair_t *nvp);
180const void *nvpair_get_binary(const nvpair_t *nvp, size_t *sizep);
181const bool *nvpair_get_bool_array(const nvpair_t *nvp, size_t *nitemsp);
182const uint64_t *nvpair_get_number_array(const nvpair_t *nvp, size_t *nitemsp);
183const char * const *nvpair_get_string_array(const nvpair_t *nvp, size_t *nitemsp);
184const nvlist_t * const *nvpair_get_nvlist_array(const nvpair_t *nvp, size_t *nitemsp);
185const int *nvpair_get_descriptor_array(const nvpair_t *nvp, size_t *nitemsp);
186
187void nvpair_free(nvpair_t *nvp);
188
189#endif /* !_NV_IMPL_H_ */
190