99b1c0d2d8798e9427195d31dfc118be44d24e6a
[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 bool keep_old;
30 bool no_delete;
31
32 int version;
33 };
34
35 struct vlist_node {
36 struct avl_node avl;
37 int version;
38 };
39
40 void vlist_init(struct vlist_tree *tree, avl_tree_comp cmp, vlist_update_cb update);
41
42 #define vlist_find(tree, name, element, node_member) \
43 avl_find_element(&(tree)->avl, name, element, node_member.avl)
44
45 static inline void vlist_update(struct vlist_tree *tree)
46 {
47 tree->version++;
48 }
49
50 void vlist_add(struct vlist_tree *tree, struct vlist_node *node, void *key);
51 void vlist_delete(struct vlist_tree *tree, struct vlist_node *node);
52 void vlist_flush(struct vlist_tree *tree);
53 void vlist_flush_all(struct vlist_tree *tree);
54
55 #define vlist_for_each_element(tree, element, node_member) \
56 avl_for_each_element(&(tree)->avl, element, node_member.avl)
57
58
59 struct vlist_simple_tree {
60 struct list_head list;
61 int head_offset;
62 int version;
63 };
64
65 struct vlist_simple_node {
66 struct list_head list;
67 int version;
68 };
69
70 #define vlist_simple_init(tree, node, member) \
71 __vlist_simple_init(tree, offsetof(node, member))
72
73 void __vlist_simple_init(struct vlist_simple_tree *tree, int offset);
74 void vlist_simple_delete(struct vlist_simple_tree *tree, struct vlist_simple_node *node);
75 void vlist_simple_flush(struct vlist_simple_tree *tree);
76 void vlist_simple_flush_all(struct vlist_simple_tree *tree);
77
78 static inline void vlist_simple_update(struct vlist_simple_tree *tree)
79 {
80 tree->version++;
81 }
82
83 static inline void vlist_simple_add(struct vlist_simple_tree *tree, struct vlist_simple_node *node)
84 {
85 list_add(&node->list, &tree->list);
86 }
87
88 #define vlist_simple_for_each_element(tree, element, node_member) \
89 list_for_each_entry(element, &(tree)->list, node_member.list)
90
91 #define vlist_simple_empty(tree) \
92 list_empty(&(tree)->list)
93
94
95 #ifdef __linux__
96 static inline int fls(int x)
97 {
98 int r = 32;
99
100 if (!x)
101 return 0;
102 if (!(x & 0xffff0000u)) {
103 x <<= 16;
104 r -= 16;
105 }
106 if (!(x & 0xff000000u)) {
107 x <<= 8;
108 r -= 8;
109 }
110 if (!(x & 0xf0000000u)) {
111 x <<= 4;
112 r -= 4;
113 }
114 if (!(x & 0xc0000000u)) {
115 x <<= 2;
116 r -= 2;
117 }
118 if (!(x & 0x80000000u)) {
119 x <<= 1;
120 r -= 1;
121 }
122 return r;
123 }
124 #endif
125
126 int avl_strcmp(const void *k1, const void *k2, void *ptr);
127
128 #endif