-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.h
382 lines (326 loc) · 11.2 KB
/
tree.h
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*
* tree.h - Generic tree handling
*
* YAML to DTB generator
*
* (C) Copyright Pantelis Antoniou <[email protected]>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* (1) Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* (2) Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* (3)The name of the author may not be used to
* endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef TREE_H
#define TREE_H
#include <stdint.h>
#include <sys/time.h>
#ifdef __linux__
#include <linux/limits.h>
#else
#include <limits.h>
#endif
#include "list.h"
struct node;
struct property {
struct list_head node;
struct node *np;
char *name;
struct list_head refs;
bool is_delete : 1; /* set to true it is signals deletion */
bool deleted : 1;
};
struct node {
struct list_head node;
struct node *parent;
struct list_head children;
struct list_head properties;
struct list_head labels;
char *name;
bool is_delete : 1; /* set to true it is signals deletion */
bool deleted : 1;
};
struct label {
struct list_head node;
struct node *np;
char *label;
size_t len;
struct list_head hnode; /* for hashing */
uint32_t hash;
bool duplicate : 1;
};
enum ref_type {
r_anchor,
r_path,
r_scalar,
r_null,
r_seq_start,
r_seq_end
};
struct ref {
struct list_head node;
enum ref_type type;
struct property *prop;
const void *data;
int len;
char *xtag; /* explicit tag */
const char *xtag_builtin;
};
struct tree;
typedef void (*tree_debugf_t)(struct tree *t, const char *fmt, ...)
__attribute__ ((__format__ (__printf__, 2, 0)));
struct tree_ops {
struct ref *(*ref_alloc)(struct tree *t, enum ref_type type,
const void *data, int len, const char *xtag);
void (*ref_free)(struct tree *t, struct ref *ref);
struct property *(*prop_alloc)(struct tree *t, const char *name);
void (*prop_free)(struct tree *t, struct property *prop);
struct label *(*label_alloc)(struct tree *t, const char *name);
void (*label_free)(struct tree *t, struct label *l);
struct node *(*node_alloc)(struct tree *t, const char *name,
const char *label);
void (*node_free)(struct tree *t, struct node *np);
void (*debugf)(struct tree *t, const char *fmt, ...)
__attribute__ ((__format__ (__printf__, 2, 0)));
void (*msg_at_node)(struct tree *t, struct node *np,
const char *type, const char *fmt, ...)
__attribute__ ((__format__ (__printf__, 4, 0)));
void (*msg_at_property)(struct tree *t, struct property *prop,
const char *type, const char *fmt, ...)
__attribute__ ((__format__ (__printf__, 4, 0)));
void (*msg_at_ref)(struct tree *t, struct ref *ref,
const char *type, const char *fmt, ...)
__attribute__ ((__format__ (__printf__, 4, 0)));
void (*msg_at_label)(struct tree *t, struct label *l,
const char *type, const char *fmt, ...)
__attribute__ ((__format__ (__printf__, 4, 0)));
};
#ifndef LABEL_HASH_SIZE
#define LABEL_HASH_SIZE 251 /* the largest prime less than 255 */
#endif
struct tree {
struct node *root;
struct list_head ref_nodes;
const struct tree_ops *ops;
struct list_head lhash[LABEL_HASH_SIZE];
};
/* FVN-1 hash */
static inline uint32_t label_hash(const char *label, size_t len)
{
uint32_t hval;
/* initial FV1 hash value */
hval = 0x811c9dc5;
while (len > 0) {
/* multiply by the 32 bit FNV magic prime mod 2^32 */
hval += (hval << 1) + (hval << 4) +
(hval << 7) + (hval << 8) +
(hval << 24);
/* xor the bottom with the current octet */
hval ^= (uint32_t)*label++;
len--;
}
return hval;
}
static inline struct node *tree_root(struct tree *t)
{
return t->root;
}
static inline void tree_set_root(struct tree *t, struct node *np)
{
t->root = np;
}
static inline struct list_head *tree_ref_nodes(struct tree *t)
{
return &t->ref_nodes;
}
/* children list accessors */
#define for_each_child_of_node(np, child) \
list_for_each_entry(child, &(np)->children, node) \
if (!(child)->deleted)
#define for_each_child_of_node_safe(np, child, childn) \
list_for_each_entry_safe(child, childn, &(np)->children, node) \
if (!(child)->deleted)
#define for_each_child_of_node_withdel(np, child) \
list_for_each_entry(child, &(np)->children, node)
#define for_each_child_of_node_safe_withdel(np, child, childn) \
list_for_each_entry_safe(child, childn, &(np)->children, node)
#define first_child_of_node(np) \
list_entry(&(np)->children, struct node, node)
/* property list accessors */
#define for_each_property_of_node(np, prop) \
list_for_each_entry(prop, &(np)->properties, node) \
if (!(prop)->deleted)
#define for_each_property_of_node_safe(np, prop, propn) \
list_for_each_entry_safe(prop, propn, &(np)->properties, node) \
if (!(prop)->deleted)
#define for_each_property_of_node_withdel(np, prop) \
list_for_each_entry(prop, &(np)->properties, node)
#define for_each_property_of_node_safe_withdel(np, prop, propn) \
list_for_each_entry_safe(prop, propn, &(np)->properties, node)
#define first_property_of_node(np) \
list_entry(&(np)->properties, struct property, node)
/* ref list accessors */
#define for_each_ref_of_property(prop, ref) \
list_for_each_entry(ref, &(prop)->refs, node)
#define for_each_ref_of_property_safe(prop, ref, refn) \
list_for_each_entry_safe(ref, refn, &(prop)->refs, node)
#define for_each_ref_of_property_continue(prop, ref) \
list_for_each_entry_continue(ref, &(prop)->refs, node)
#define first_ref_of_property(prop) \
list_entry(&(prop)->refs, struct ref, node)
/* label list accessors */
#define for_each_label_of_node(np, l) \
list_for_each_entry(l, &(np)->labels, node)
#define for_each_label_of_node_safe(np, l, ln) \
list_for_each_entry_safe(l, ln, &(np)->labels, node)
#define for_each_label_of_node_continue(np, l) \
list_for_each_entry_continue(l, &(np)->labels, node)
#define first_label_of_node(np) \
list_entry(&(np)->labels, struct label, node)
void tree_init(struct tree *t, const struct tree_ops *ops);
void tree_cleanup(struct tree *t);
struct ref *ref_alloc(struct tree *t, enum ref_type type,
const void *data, int len,
const char *xtag);
void ref_free(struct tree *t, struct ref *ref);
struct label *label_add_nolink(struct tree *t, struct node *np,
const char *label);
void label_add(struct tree *t, struct node *np, const char *label);
void label_free(struct tree *t, struct label *l);
struct property *prop_alloc(struct tree *t, const char *name);
void prop_free(struct tree *t, struct property *prop);
void prop_del(struct tree *t, struct property *prop);
void prop_ref_clear(struct tree *t, struct property *prop);
struct node *node_alloc(struct tree *t, const char *name, const char *label);
void node_free(struct tree *t, struct node *np);
void node_clear(struct tree *t, struct node *np);
void node_del(struct tree *t, struct node *np);
struct node *node_lookup_by_label(struct tree *t,
const char *label, int len);
struct node *node_lookup_by_path(struct tree *t,
const char *path, int len);
/* leading * ref, leading / path */
struct node *node_lookup(struct tree *t, const char *key, int len);
struct node *node_get_child_by_name(struct tree *t,
struct node *np, const char *name,
int index);
struct property *prop_get_by_name(struct tree *t,
struct node *np, const char *name,
int index);
struct ref *ref_get_by_index(struct tree *t,
struct property *prop, int index);
bool tree_apply_single_ref_node(struct tree *t, struct node *np,
bool object, bool compatible);
void tree_apply_ref_node(struct tree *t, struct node *npref,
struct node *np, bool compatible);
void tree_apply_ref_nodes(struct tree *t, bool object, bool compatible);
int tree_detect_duplicate_labels(struct tree *t, struct node *np);
/* this should be enough */
#define NODE_FULLNAME_MAX 4096
const char *dn_fullname_multi(struct node *np, char **buf, int *size);
const char *dn_fullname(struct node *np, char *buf, int bufsize);
#define tree_debug(_t, _fmt, ...) \
do { \
if (_t->ops->debugf) \
_t->ops->debugf(_t, _fmt, ##__VA_ARGS__); \
} while(0)
#define tree_error_at_node(_t, _np, _fmt, ...) \
do { \
if (_t->ops->msg_at_node) \
_t->ops->msg_at_node(_t, _np, "error", \
_fmt, ##__VA_ARGS__); \
} while(0)
#define tree_error_at_property(_t, _prop, _fmt, ...) \
do { \
if (_t->ops->msg_at_property) \
_t->ops->msg_at_property(_t, _prop, "error", \
_fmt, ##__VA_ARGS__); \
} while(0)
#define tree_error_at_ref(_t, _ref, _fmt, ...) \
do { \
if (_t->ops->msg_at_ref) \
_t->ops->msg_at_ref(_t, _ref, "error", \
_fmt, ##__VA_ARGS__); \
} while(0)
#define tree_error_at_label(_t, _ref, _fmt, ...) \
do { \
if (_t->ops->msg_at_label) \
_t->ops->msg_at_label(_t, _ref, "error", \
_fmt, ##__VA_ARGS__); \
} while(0)
#define tree_warning_at_node(_t, _np, _fmt, ...) \
do { \
if (_t->ops->msg_at_node) \
_t->ops->msg_at_node(_t, _np, "warning", \
_fmt, ##__VA_ARGS__); \
} while(0)
#define tree_warning_at_property(_t, _prop, _fmt, ...) \
do { \
if (_t->ops->msg_at_property) \
_t->ops->msg_at_property(_t, _prop, "warning", \
_fmt, ##__VA_ARGS__); \
} while(0)
#define tree_warning_at_ref(_t, _ref, _fmt, ...) \
do { \
if (_t->ops->msg_at_ref) \
_t->ops->msg_at_ref(_t, _ref, "warning", \
_fmt, ##__VA_ARGS__); \
} while(0)
#define tree_warning_at_label(_t, _ref, _fmt, ...) \
do { \
if (_t->ops->msg_at_label) \
_t->ops->msg_at_label(_t, _ref, "warning", \
_fmt, ##__VA_ARGS__); \
} while(0)
#define tree_info_at_node(_t, _np, _fmt, ...) \
do { \
if (_t->ops->msg_at_node) \
_t->ops->msg_at_node(_t, _np, "info", \
_fmt, ##__VA_ARGS__); \
} while(0)
#define tree_info_at_property(_t, _prop, _fmt, ...) \
do { \
if (_t->ops->msg_at_property) \
_t->ops->msg_at_property(_t, _prop, "info", \
_fmt, ##__VA_ARGS__); \
} while(0)
#define tree_info_at_ref(_t, _ref, _fmt, ...) \
do { \
if (_t->ops->msg_at_ref) \
_t->ops->msg_at_ref(_t, _ref, "info", \
_fmt, ##__VA_ARGS__); \
} while(0)
#define tree_info_at_label(_t, _ref, _fmt, ...) \
do { \
if (_t->ops->msg_at_label) \
_t->ops->msg_at_label(_t, _ref, "info", \
_fmt, ##__VA_ARGS__); \
} while(0)
static inline bool is_node_ref_char(const char c)
{
/* we allow both $ and * as node reference characters */
return c == '*' || c == '$';
}
#endif