blob: add blob_memdup()
[project/libubox.git] / vlist.h
1 /*
2  * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #include "avl.h"
18
19 struct vlist_tree;
20 struct vlist_node;
21
22 typedef void (*vlist_update_cb)(struct vlist_tree *tree,
23                                 struct vlist_node *node_new,
24                                 struct vlist_node *node_old);
25
26 struct vlist_tree {
27         struct avl_tree avl;
28
29         vlist_update_cb update;
30         bool keep_old;
31         bool no_delete;
32
33         int version;
34 };
35
36 struct vlist_node {
37         struct avl_node avl;
38         int version;
39 };
40
41 void vlist_init(struct vlist_tree *tree, avl_tree_comp cmp, vlist_update_cb update);
42
43 #define vlist_find(tree, name, element, node_member) \
44         avl_find_element(&(tree)->avl, name, element, node_member.avl)
45
46 static inline void vlist_update(struct vlist_tree *tree)
47 {
48         tree->version++;
49 }
50
51 void vlist_add(struct vlist_tree *tree, struct vlist_node *node, void *key);
52 void vlist_delete(struct vlist_tree *tree, struct vlist_node *node);
53 void vlist_flush(struct vlist_tree *tree);
54 void vlist_flush_all(struct vlist_tree *tree);
55
56 #define vlist_for_each_element(tree, element, node_member) \
57         avl_for_each_element(&(tree)->avl, element, node_member.avl)
58