893d3d3a57fa93f18426c9994b0e1c520ac046ff
2 * netifd - network interface daemon
3 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
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
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.
14 #ifndef __NETIFD_UTILS_H
15 #define __NETIFD_UTILS_H
17 #include <libubox/list.h>
18 #include <libubox/avl.h>
19 #include <libubox/blobmsg.h>
22 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
24 extern int __build_bug_on_failed
;
25 #define BUILD_BUG_ON(condition) \
27 ((void)sizeof(char[1 - 2*!!(condition)])); \
28 if (condition) __build_bug_on_failed = 1; \
32 static inline bool blobmsg_get_bool_default(struct blob_attr
*attr
, bool val
)
37 return blobmsg_get_bool(attr
);
40 #define __init __attribute__((constructor))
45 typedef void (*vlist_update_cb
)(struct vlist_tree
*tree
,
46 struct vlist_node
*node_new
,
47 struct vlist_node
*node_old
);
52 vlist_update_cb update
;
64 void vlist_init(struct vlist_tree
*tree
, avl_tree_comp cmp
, vlist_update_cb update
);
66 #define vlist_find(tree, name, element, node_member) \
67 avl_find_element(&(tree)->avl, name, element, node_member.avl)
69 static inline void vlist_update(struct vlist_tree
*tree
)
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
);
79 #define vlist_for_each_element(tree, element, node_member) \
80 avl_for_each_element(&(tree)->avl, element, node_member.avl)
83 struct vlist_simple_tree
{
84 struct list_head list
;
89 struct vlist_simple_node
{
90 struct list_head list
;
94 #define vlist_simple_init(tree, node, member) \
95 __vlist_simple_init(tree, offsetof(node, member))
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
);
102 static inline void vlist_simple_update(struct vlist_simple_tree
*tree
)
107 static inline void vlist_simple_add(struct vlist_simple_tree
*tree
, struct vlist_simple_node
*node
)
109 node
->version
= tree
->version
;
110 list_add(&node
->list
, &tree
->list
);
113 #define vlist_simple_for_each_element(tree, element, node_member) \
114 list_for_each_entry(element, &(tree)->list, node_member.list)
116 #define vlist_simple_empty(tree) \
117 list_empty(&(tree)->list)
121 static inline int fls(int x
)
127 if (!(x
& 0xffff0000u
)) {
131 if (!(x
& 0xff000000u
)) {
135 if (!(x
& 0xf0000000u
)) {
139 if (!(x
& 0xc0000000u
)) {
143 if (!(x
& 0x80000000u
)) {
151 int avl_strcmp(const void *k1
, const void *k2
, void *ptr
);