add a simplified vlist type
[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
54 struct vlist_simple_tree {
55 struct list_head list;
56 int head_offset;
57 int version;
58 };
59
60 struct vlist_simple_node {
61 struct list_head list;
62 int version;
63 };
64
65 #define vlist_simple_init(tree, node, member) \
66 __vlist_simple_init(tree, offsetof(node, member))
67
68 void __vlist_simple_init(struct vlist_simple_tree *tree, int offset);
69 void vlist_simple_delete(struct vlist_simple_tree *tree, struct vlist_simple_node *node);
70 void vlist_simple_flush(struct vlist_simple_tree *tree);
71 void vlist_simple_flush_all(struct vlist_simple_tree *tree);
72
73 static inline void vlist_simple_update(struct vlist_simple_tree *tree)
74 {
75 tree->version++;
76 }
77
78 static inline void vlist_simple_add(struct vlist_simple_tree *tree, struct vlist_simple_node *node)
79 {
80 list_add(&node->list, &tree->list);
81 }
82
83 #define vlist_simple_for_each_element(tree, element, node_member) \
84 list_for_each_entry(element, &(tree)->list, node_member.list)
85
86 #define vlist_simple_empty(tree) \
87 list_empty(&(tree)->list)
88
89
90 #ifdef __linux__
91 static inline int fls(int x)
92 {
93 int r = 32;
94
95 if (!x)
96 return 0;
97 if (!(x & 0xffff0000u)) {
98 x <<= 16;
99 r -= 16;
100 }
101 if (!(x & 0xff000000u)) {
102 x <<= 8;
103 r -= 8;
104 }
105 if (!(x & 0xf0000000u)) {
106 x <<= 4;
107 r -= 4;
108 }
109 if (!(x & 0xc0000000u)) {
110 x <<= 2;
111 r -= 2;
112 }
113 if (!(x & 0x80000000u)) {
114 x <<= 1;
115 r -= 1;
116 }
117 return r;
118 }
119 #endif
120
121 int avl_strcmp(const void *k1, const void *k2, void *ptr);
122
123 #endif