avl_strcmp is now part of libubox
[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/avl-cmp.h>
20 #include <libubox/blobmsg.h>
21
22 #ifndef __OPTIMIZE__
23 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
24 #else
25 extern int __build_bug_on_failed;
26 #define BUILD_BUG_ON(condition)                          \
27         do {                                                    \
28                 ((void)sizeof(char[1 - 2*!!(condition)]));  \
29                 if (condition) __build_bug_on_failed = 1;   \
30         } while(0)
31 #endif
32
33 static inline bool blobmsg_get_bool_default(struct blob_attr *attr, bool val)
34 {
35         if (!attr)
36                 return val;
37
38         return blobmsg_get_bool(attr);
39 }
40
41 #define __init __attribute__((constructor))
42
43 struct vlist_tree;
44 struct vlist_node;
45
46 typedef void (*vlist_update_cb)(struct vlist_tree *tree,
47                                 struct vlist_node *node_new,
48                                 struct vlist_node *node_old);
49
50 struct vlist_tree {
51         struct avl_tree avl;
52
53         vlist_update_cb update;
54         bool keep_old;
55         bool no_delete;
56
57         int version;
58 };
59
60 struct vlist_node {
61         struct avl_node avl;
62         int version;
63 };
64
65 void vlist_init(struct vlist_tree *tree, avl_tree_comp cmp, vlist_update_cb update);
66
67 #define vlist_find(tree, name, element, node_member) \
68         avl_find_element(&(tree)->avl, name, element, node_member.avl)
69
70 static inline void vlist_update(struct vlist_tree *tree)
71 {
72         tree->version++;
73 }
74
75 void vlist_add(struct vlist_tree *tree, struct vlist_node *node, void *key);
76 void vlist_delete(struct vlist_tree *tree, struct vlist_node *node);
77 void vlist_flush(struct vlist_tree *tree);
78 void vlist_flush_all(struct vlist_tree *tree);
79
80 #define vlist_for_each_element(tree, element, node_member) \
81         avl_for_each_element(&(tree)->avl, element, node_member.avl)
82
83
84 struct vlist_simple_tree {
85         struct list_head list;
86         int head_offset;
87         int version;
88 };
89
90 struct vlist_simple_node {
91         struct list_head list;
92         int version;
93 };
94
95 #define vlist_simple_init(tree, node, member) \
96         __vlist_simple_init(tree, offsetof(node, member))
97
98 void __vlist_simple_init(struct vlist_simple_tree *tree, int offset);
99 void vlist_simple_delete(struct vlist_simple_tree *tree, struct vlist_simple_node *node);
100 void vlist_simple_flush(struct vlist_simple_tree *tree);
101 void vlist_simple_flush_all(struct vlist_simple_tree *tree);
102 void vlist_simple_replace(struct vlist_simple_tree *dest, struct vlist_simple_tree *old);
103
104 static inline void vlist_simple_update(struct vlist_simple_tree *tree)
105 {
106         tree->version++;
107 }
108
109 static inline void vlist_simple_add(struct vlist_simple_tree *tree, struct vlist_simple_node *node)
110 {
111         node->version = tree->version;
112         list_add(&node->list, &tree->list);
113 }
114
115 #define vlist_simple_for_each_element(tree, element, node_member) \
116         list_for_each_entry(element, &(tree)->list, node_member.list)
117
118 #define vlist_simple_empty(tree) \
119         list_empty(&(tree)->list)
120
121
122 #ifdef __linux__
123 static inline int fls(int x)
124 {
125     int r = 32;
126
127     if (!x)
128         return 0;
129     if (!(x & 0xffff0000u)) {
130         x <<= 16;
131         r -= 16;
132     }
133     if (!(x & 0xff000000u)) {
134         x <<= 8;
135         r -= 8;
136     }
137     if (!(x & 0xf0000000u)) {
138         x <<= 4;
139         r -= 4;
140     }
141     if (!(x & 0xc0000000u)) {
142         x <<= 2;
143         r -= 2;
144     }
145     if (!(x & 0x80000000u)) {
146         x <<= 1;
147         r -= 1;
148     }
149     return r;
150 }
151 #endif
152
153 #endif