-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnvl.c
More file actions
347 lines (277 loc) · 7.81 KB
/
nvl.c
File metadata and controls
347 lines (277 loc) · 7.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
* Copyright (c) 2017-2018 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <jeffpc/error.h>
#include <jeffpc/mem.h>
#include <jeffpc/nvl.h>
#include "val_impl.h"
int nvl_merge(struct nvlist *dest, struct nvlist *src)
{
const struct nvpair *spair;
nvl_for_each(spair, src) {
int ret;
ret = nvl_set(dest, str_cstr(spair->name),
val_getref(spair->value));
if (ret)
return ret;
}
return 0;
}
static struct nvpair *find(struct nvlist *nvl, const char *name)
{
struct str name_str = STR_STATIC_INITIALIZER(name);
struct nvpair key = {
.name = &name_str,
};
return rb_find(&nvl->val._set_nvl.values, &key, NULL);
}
/*
* nvlist iteration
*/
const struct nvpair *nvl_iter_start(struct nvlist *nvl)
{
return rb_first(&nvl->val._set_nvl.values);
}
const struct nvpair *nvl_iter_next(struct nvlist *nvl,
const struct nvpair *prev)
{
return rb_next(&nvl->val._set_nvl.values, (void *) prev);
}
/*
* nvlist lookup
*/
#define LOOKUP_INT(fxn, ctype, pairfxn) \
int fxn(struct nvlist *nvl, const char *name, ctype *out) \
{ \
return pairfxn(find(nvl, name), out); \
}
#define LOOKUP_PTR(fxn, ctype, pairfxn) \
ctype fxn(struct nvlist *nvl, const char *name) \
{ \
return pairfxn(find(nvl, name)); \
}
const struct nvpair *nvl_lookup(struct nvlist *nvl, const char *name)
{
struct nvpair *pair;
pair = find(nvl, name);
return pair ? pair : ERR_PTR(-ENOENT);
}
int nvl_lookup_array(struct nvlist *nvl, const char *name,
struct val ***vals, size_t *nelem)
{
return nvpair_value_array(find(nvl, name), vals, nelem);
}
int nvl_lookup_blob(struct nvlist *nvl, const char *name,
const void **ptr, size_t *size)
{
return nvpair_value_blob(find(nvl, name), ptr, size);
}
LOOKUP_INT(nvl_lookup_bool, bool, nvpair_value_bool);
LOOKUP_INT(nvl_lookup_int, uint64_t, nvpair_value_int);
int nvl_lookup_null(struct nvlist *nvl, const char *name)
{
return nvpair_value_null(find(nvl, name));
}
LOOKUP_PTR(nvl_lookup_nvl, struct nvlist *, nvpair_value_nvl);
LOOKUP_PTR(nvl_lookup_str, struct str *, nvpair_value_str);
/*
* nvlist set
*/
static inline int do_nvl_set(struct nvlist *nvl, const char *cname,
struct str *name, struct val *val)
{
struct nvpair *pair;
pair = find(nvl, cname);
if (!pair) {
/* not found - allocate a new pair */
pair = __nvpair_alloc(name ? name : str_dup(cname));
if (!pair) {
val_putref(val);
return -ENOMEM;
}
rb_add(&nvl->val._set_nvl.values, pair);
} else {
str_putref(name);
}
val_putref(pair->value);
pair->value = val;
return 0;
}
int nvl_set_pair(struct nvlist *nvl, const struct nvpair *value)
{
return do_nvl_set(nvl, nvpair_name(value), nvpair_name_str(value),
nvpair_value(value));
}
int nvl_set(struct nvlist *nvl, const char *name, struct val *val)
{
return do_nvl_set(nvl, name, NULL, val);
}
#define SET(nvl, name, valalloc) \
do { \
struct val *val; \
\
val = valalloc; \
if (IS_ERR(val)) \
return PTR_ERR(val); \
\
return nvl_set(nvl, name, val); \
} while (0)
#define SET_ARG0(fxn, valalloc) \
int fxn(struct nvlist *nvl, const char *name) \
{ \
SET(nvl, name, valalloc()); \
}
#define SET_ARG1(fxn, ctype, valalloc) \
int fxn(struct nvlist *nvl, const char *name, ctype v) \
{ \
SET(nvl, name, valalloc(v)); \
}
#define SET_ARG2(fxn, ctype1, ctype2, valalloc) \
int fxn(struct nvlist *nvl, const char *name, ctype1 a, ctype2 b) \
{ \
SET(nvl, name, valalloc(a, b)); \
}
SET_ARG2(nvl_set_array, struct val **, size_t, val_alloc_array);
SET_ARG2(nvl_set_array_copy, struct val **, size_t, val_alloc_array_dup);
SET_ARG2(nvl_set_blob, void *, size_t, val_alloc_blob);
SET_ARG2(nvl_set_blob_copy, const void *, size_t, val_alloc_blob_dup);
SET_ARG1(nvl_set_bool, bool, val_alloc_bool);
SET_ARG1(nvl_set_cstr_dup, const char *, val_dup_str);
SET_ARG1(nvl_set_int, uint64_t, val_alloc_int);
SET_ARG0(nvl_set_null, val_alloc_null);
SET_ARG1(nvl_set_nvl, struct nvlist *, nvl_cast_to_val);
SET_ARG1(nvl_set_str, struct str *, str_cast_to_val);
/*
* nvlist unset
*/
static int unset(struct nvlist *nvl, const char *name, enum val_type type,
bool matchtype)
{
struct nvpair *pair;
pair = find(nvl, name);
if (!pair)
return -ENOENT;
if (matchtype && (pair->value->type != type))
return -ERANGE;
rb_remove(&nvl->val._set_nvl.values, pair);
__nvpair_free(pair);
return 0;
}
int nvl_unset(struct nvlist *nvl, const char *name)
{
return unset(nvl, name, VT_NULL, false);
}
int nvl_unset_type(struct nvlist *nvl, const char *name, enum val_type type)
{
return unset(nvl, name, type, true);
}
/*
* nvlist exists
*/
bool nvl_exists(struct nvlist *nvl, const char *name)
{
return find(nvl, name) != NULL;
}
int nvl_exists_type(struct nvlist *nvl, const char *name, enum val_type type)
{
struct nvpair *pair;
pair = find(nvl, name);
if (!pair)
return -ENOENT;
if (pair->value->type != type)
return -ERANGE;
return 0;
}
/*
* nvpair value
*/
#define VALUE_INT(fxn, ctype, nvtype, valmember) \
int fxn(const struct nvpair *pair, ctype *out) \
{ \
if (!pair) \
return -ENOENT; \
\
if (pair->value->type != nvtype) \
return -ERANGE; \
\
*out = pair->value->valmember; \
\
return 0; \
}
#define VALUE_PTR(fxn, ctype, valtype, getref) \
ctype fxn(const struct nvpair *pair) \
{ \
if (!pair) \
return ERR_PTR(-ENOENT); \
\
if (pair->value->type != valtype) \
return ERR_PTR(-ERANGE); \
\
return getref(pair->value); \
}
int nvpair_value_array(const struct nvpair *pair, struct val ***vals,
size_t *nelem)
{
if (!pair)
return -ENOENT;
if (pair->value->type != VT_ARRAY)
return -ERANGE;
*vals = pair->value->_set_array.vals;
*nelem = pair->value->array.nelem;
return 0;
}
int nvpair_value_blob(const struct nvpair *pair, const void **ptr, size_t *size)
{
if (!pair)
return -ENOENT;
if (pair->value->type != VT_BLOB)
return -ERANGE;
*ptr = pair->value->blob.ptr;
*size = pair->value->blob.size;
return 0;
}
VALUE_INT(nvpair_value_bool, bool, VT_BOOL, b);
VALUE_INT(nvpair_value_int, uint64_t, VT_INT, i);
int nvpair_value_null(const struct nvpair *pair)
{
if (!pair)
return -ENOENT;
if (pair->value->type != VT_NULL)
return -ERANGE;
return 0;
}
VALUE_PTR(nvpair_value_nvl, struct nvlist *, VT_NVL, val_getref_nvl);
VALUE_PTR(nvpair_value_str, struct str *, VT_STR, val_getref_str);
/*
* Packing & unpacking
*/
struct nvlist *nvl_unpack(const void *ptr, size_t len, enum val_format format)
{
struct val *val;
val = val_unpack(ptr, len, format);
if (IS_ERR(val))
return ERR_CAST(val);
if (val->type != VT_NVL) {
val_putref(val);
return ERR_PTR(-EINVAL);
}
return val_cast_to_nvl(val);
}