rename config_init_interfaces to config_init_all
[project/netifd.git] / utils.h
1 #ifndef __NETIFD_UTILS_H
2 #define __NETIFD_UTILS_H
3
4 #include <libubox/list.h>
5 #include <libubox/avl.h>
6
7 #define __init __attribute__((constructor))
8
9 struct vlist_tree;
10 struct vlist_node;
11
12 typedef void (*vlist_update_cb)(struct vlist_tree *tree,
13 struct vlist_node *node_new,
14 struct vlist_node *node_old);
15
16 struct vlist_tree {
17 struct avl_tree avl;
18
19 vlist_update_cb update;
20 int key_offset;
21 bool keep_old;
22 bool no_delete;
23
24 int version;
25 };
26
27 struct vlist_node {
28 struct avl_node avl;
29 int version;
30 };
31
32 void __vlist_init(struct vlist_tree *tree, avl_tree_comp cmp, vlist_update_cb update, int offset);
33
34 #define vlist_init(tree, cmp, update, type, node, key) \
35 __vlist_init(tree, cmp, update, offsetof(type, key) - offsetof(type, node))
36
37 #define vlist_find(tree, name, element, node_member) \
38 avl_find_element(&(tree)->avl, name, element, node_member.avl)
39
40 static inline void vlist_update(struct vlist_tree *tree)
41 {
42 tree->version++;
43 }
44
45 void vlist_add(struct vlist_tree *tree, struct vlist_node *node);
46 void vlist_delete(struct vlist_tree *tree, struct vlist_node *node);
47 void vlist_flush(struct vlist_tree *tree);
48 void vlist_flush_all(struct vlist_tree *tree);
49
50 #define vlist_for_each_element(tree, element, node_member) \
51 avl_for_each_element(&(tree)->avl, element, node_member.avl)
52
53 #ifdef __linux__
54 static inline int fls(int x)
55 {
56 int r = 32;
57
58 if (!x)
59 return 0;
60 if (!(x & 0xffff0000u)) {
61 x <<= 16;
62 r -= 16;
63 }
64 if (!(x & 0xff000000u)) {
65 x <<= 8;
66 r -= 8;
67 }
68 if (!(x & 0xf0000000u)) {
69 x <<= 4;
70 r -= 4;
71 }
72 if (!(x & 0xc0000000u)) {
73 x <<= 2;
74 r -= 2;
75 }
76 if (!(x & 0x80000000u)) {
77 x <<= 1;
78 r -= 1;
79 }
80 return r;
81 }
82 #endif
83
84 int avl_strcmp(const void *k1, const void *k2, void *ptr);
85
86 #endif