893d3d3a57fa93f18426c9994b0e1c520ac046ff
[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
102 static inline void vlist_simple_update(struct vlist_simple_tree *tree)
103 {
104 tree->version++;
105 }
106
107 static inline void vlist_simple_add(struct vlist_simple_tree *tree, struct vlist_simple_node *node)
108 {
109 node->version = tree->version;
110 list_add(&node->list, &tree->list);
111 }
112
113 #define vlist_simple_for_each_element(tree, element, node_member) \
114 list_for_each_entry(element, &(tree)->list, node_member.list)
115
116 #define vlist_simple_empty(tree) \
117 list_empty(&(tree)->list)
118
119
120 #ifdef __linux__
121 static inline int fls(int x)
122 {
123 int r = 32;
124
125 if (!x)
126 return 0;
127 if (!(x & 0xffff0000u)) {
128 x <<= 16;
129 r -= 16;
130 }
131 if (!(x & 0xff000000u)) {
132 x <<= 8;
133 r -= 8;
134 }
135 if (!(x & 0xf0000000u)) {
136 x <<= 4;
137 r -= 4;
138 }
139 if (!(x & 0xc0000000u)) {
140 x <<= 2;
141 r -= 2;
142 }
143 if (!(x & 0x80000000u)) {
144 x <<= 1;
145 r -= 1;
146 }
147 return r;
148 }
149 #endif
150
151 int avl_strcmp(const void *k1, const void *k2, void *ptr);
152
153 #endif