update the interface list version on config reload, fixes removing old interfaces
[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 #include <libubox/blobmsg.h>
7
8 static inline bool blobmsg_get_bool_default(struct blob_attr *attr, bool val)
9 {
10         if (!attr)
11                 return val;
12
13         return blobmsg_get_bool(attr);
14 }
15
16 #define __init __attribute__((constructor))
17
18 struct vlist_tree;
19 struct vlist_node;
20
21 typedef void (*vlist_update_cb)(struct vlist_tree *tree,
22                                 struct vlist_node *node_new,
23                                 struct vlist_node *node_old);
24
25 struct vlist_tree {
26         struct avl_tree avl;
27
28         vlist_update_cb update;
29         int key_offset;
30         bool keep_old;
31         bool no_delete;
32
33         int version;
34 };
35
36 struct vlist_node {
37         struct avl_node avl;
38         int version;
39 };
40
41 void __vlist_init(struct vlist_tree *tree, avl_tree_comp cmp, vlist_update_cb update, int offset);
42
43 #define vlist_init(tree, cmp, update, type, node, key) \
44         __vlist_init(tree, cmp, update, offsetof(type, key) - offsetof(type, node))
45
46 #define vlist_find(tree, name, element, node_member) \
47         avl_find_element(&(tree)->avl, name, element, node_member.avl)
48
49 static inline void vlist_update(struct vlist_tree *tree)
50 {
51         tree->version++;
52 }
53
54 void vlist_add(struct vlist_tree *tree, struct vlist_node *node);
55 void vlist_delete(struct vlist_tree *tree, struct vlist_node *node);
56 void vlist_flush(struct vlist_tree *tree);
57 void vlist_flush_all(struct vlist_tree *tree);
58
59 #define vlist_for_each_element(tree, element, node_member) \
60         avl_for_each_element(&(tree)->avl, element, node_member.avl)
61
62
63 struct vlist_simple_tree {
64         struct list_head list;
65         int head_offset;
66         int version;
67 };
68
69 struct vlist_simple_node {
70         struct list_head list;
71         int version;
72 };
73
74 #define vlist_simple_init(tree, node, member) \
75         __vlist_simple_init(tree, offsetof(node, member))
76
77 void __vlist_simple_init(struct vlist_simple_tree *tree, int offset);
78 void vlist_simple_delete(struct vlist_simple_tree *tree, struct vlist_simple_node *node);
79 void vlist_simple_flush(struct vlist_simple_tree *tree);
80 void vlist_simple_flush_all(struct vlist_simple_tree *tree);
81
82 static inline void vlist_simple_update(struct vlist_simple_tree *tree)
83 {
84         tree->version++;
85 }
86
87 static inline void vlist_simple_add(struct vlist_simple_tree *tree, struct vlist_simple_node *node)
88 {
89         list_add(&node->list, &tree->list);
90 }
91
92 #define vlist_simple_for_each_element(tree, element, node_member) \
93         list_for_each_entry(element, &(tree)->list, node_member.list)
94
95 #define vlist_simple_empty(tree) \
96         list_empty(&(tree)->list)
97
98
99 #ifdef __linux__
100 static inline int fls(int x)
101 {
102     int r = 32;
103
104     if (!x)
105         return 0;
106     if (!(x & 0xffff0000u)) {
107         x <<= 16;
108         r -= 16;
109     }
110     if (!(x & 0xff000000u)) {
111         x <<= 8;
112         r -= 8;
113     }
114     if (!(x & 0xf0000000u)) {
115         x <<= 4;
116         r -= 4;
117     }
118     if (!(x & 0xc0000000u)) {
119         x <<= 2;
120         r -= 2;
121     }
122     if (!(x & 0x80000000u)) {
123         x <<= 1;
124         r -= 1;
125     }
126     return r;
127 }
128 #endif
129
130 int avl_strcmp(const void *k1, const void *k2, void *ptr);
131
132 #endif