proto-shell: reset l3 device if it was set before
[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 static inline void vlist_update(struct vlist_tree *tree)
38 {
39         tree->version++;
40 }
41
42 void vlist_add(struct vlist_tree *tree, struct vlist_node *node);
43 void vlist_delete(struct vlist_tree *tree, struct vlist_node *node);
44 void vlist_flush(struct vlist_tree *tree);
45 void vlist_flush_all(struct vlist_tree *tree);
46
47 #define vlist_for_each_element(tree, element, node_member) \
48         avl_for_each_element(&(tree)->avl, element, node_member.avl)
49
50 #ifdef __linux__
51 static inline int fls(int x)
52 {
53     int r = 32;
54
55     if (!x)
56         return 0;
57     if (!(x & 0xffff0000u)) {
58         x <<= 16;
59         r -= 16;
60     }
61     if (!(x & 0xff000000u)) {
62         x <<= 8;
63         r -= 8;
64     }
65     if (!(x & 0xf0000000u)) {
66         x <<= 4;
67         r -= 4;
68     }
69     if (!(x & 0xc0000000u)) {
70         x <<= 2;
71         r -= 2;
72     }
73     if (!(x & 0x80000000u)) {
74         x <<= 1;
75         r -= 1;
76     }
77     return r;
78 }
79 #endif
80
81 int avl_strcmp(const void *k1, const void *k2, void *ptr);
82
83 #endif