flush list in vlist_replace()
[project/netifd.git] / utils.h
1 /*
2  * netifd - network interface daemon
3  * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2
7  * as published by the Free Software Foundation
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14 #ifndef __NETIFD_UTILS_H
15 #define __NETIFD_UTILS_H
16
17 #include <libubox/list.h>
18 #include <libubox/avl.h>
19 #include <libubox/blobmsg.h>
20
21 #ifndef __OPTIMIZE__
22 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
23 #else
24 extern int __build_bug_on_failed;
25 #define BUILD_BUG_ON(condition)                          \
26         do {                                                    \
27                 ((void)sizeof(char[1 - 2*!!(condition)]));  \
28                 if (condition) __build_bug_on_failed = 1;   \
29         } while(0)
30 #endif
31
32 static inline bool blobmsg_get_bool_default(struct blob_attr *attr, bool val)
33 {
34         if (!attr)
35                 return val;
36
37         return blobmsg_get_bool(attr);
38 }
39
40 #define __init __attribute__((constructor))
41
42 struct vlist_tree;
43 struct vlist_node;
44
45 typedef void (*vlist_update_cb)(struct vlist_tree *tree,
46                                 struct vlist_node *node_new,
47                                 struct vlist_node *node_old);
48
49 struct vlist_tree {
50         struct avl_tree avl;
51
52         vlist_update_cb update;
53         bool keep_old;
54         bool no_delete;
55
56         int version;
57 };
58
59 struct vlist_node {
60         struct avl_node avl;
61         int version;
62 };
63
64 void vlist_init(struct vlist_tree *tree, avl_tree_comp cmp, vlist_update_cb update);
65
66 #define vlist_find(tree, name, element, node_member) \
67         avl_find_element(&(tree)->avl, name, element, node_member.avl)
68
69 static inline void vlist_update(struct vlist_tree *tree)
70 {
71         tree->version++;
72 }
73
74 void vlist_add(struct vlist_tree *tree, struct vlist_node *node, void *key);
75 void vlist_delete(struct vlist_tree *tree, struct vlist_node *node);
76 void vlist_flush(struct vlist_tree *tree);
77 void vlist_flush_all(struct vlist_tree *tree);
78
79 #define vlist_for_each_element(tree, element, node_member) \
80         avl_for_each_element(&(tree)->avl, element, node_member.avl)
81
82
83 struct vlist_simple_tree {
84         struct list_head list;
85         int head_offset;
86         int version;
87 };
88
89 struct vlist_simple_node {
90         struct list_head list;
91         int version;
92 };
93
94 #define vlist_simple_init(tree, node, member) \
95         __vlist_simple_init(tree, offsetof(node, member))
96
97 void __vlist_simple_init(struct vlist_simple_tree *tree, int offset);
98 void vlist_simple_delete(struct vlist_simple_tree *tree, struct vlist_simple_node *node);
99 void vlist_simple_flush(struct vlist_simple_tree *tree);
100 void vlist_simple_flush_all(struct vlist_simple_tree *tree);
101 void vlist_simple_replace(struct vlist_simple_tree *dest, struct vlist_simple_tree *old);
102
103 static inline void vlist_simple_update(struct vlist_simple_tree *tree)
104 {
105         tree->version++;
106 }
107
108 static inline void vlist_simple_add(struct vlist_simple_tree *tree, struct vlist_simple_node *node)
109 {
110         node->version = tree->version;
111         list_add(&node->list, &tree->list);
112 }
113
114 #define vlist_simple_for_each_element(tree, element, node_member) \
115         list_for_each_entry(element, &(tree)->list, node_member.list)
116
117 #define vlist_simple_empty(tree) \
118         list_empty(&(tree)->list)
119
120
121 #ifdef __linux__
122 static inline int fls(int x)
123 {
124     int r = 32;
125
126     if (!x)
127         return 0;
128     if (!(x & 0xffff0000u)) {
129         x <<= 16;
130         r -= 16;
131     }
132     if (!(x & 0xff000000u)) {
133         x <<= 8;
134         r -= 8;
135     }
136     if (!(x & 0xf0000000u)) {
137         x <<= 4;
138         r -= 4;
139     }
140     if (!(x & 0xc0000000u)) {
141         x <<= 2;
142         r -= 2;
143     }
144     if (!(x & 0x80000000u)) {
145         x <<= 1;
146         r -= 1;
147     }
148     return r;
149 }
150 #endif
151
152 int avl_strcmp(const void *k1, const void *k2, void *ptr);
153
154 #endif