source: trunk/debian/packages/libubox/trunk/vlist.c @ 857

Last change on this file since 857 was 857, checked in by amain, 4 years ago

libubox: initial import / part 4

File size: 2.0 KB
Line 
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#include "vlist.h"
17
18void
19vlist_init(struct vlist_tree *tree, avl_tree_comp cmp, vlist_update_cb update)
20{
21        tree->update = update;
22        tree->version = 1;
23
24        avl_init(&tree->avl, cmp, 0, tree);
25}
26
27void
28vlist_delete(struct vlist_tree *tree, struct vlist_node *node)
29{
30        if (!tree->no_delete)
31                avl_delete(&tree->avl, &node->avl);
32        tree->update(tree, NULL, node);
33}
34
35void
36vlist_add(struct vlist_tree *tree, struct vlist_node *node, const void *key)
37{
38        struct vlist_node *old_node = NULL;
39        struct avl_node *anode;
40
41        node->avl.key = key;
42        node->version = tree->version;
43
44        anode = avl_find(&tree->avl, key);
45        if (anode) {
46                old_node = container_of(anode, struct vlist_node, avl);
47                if (tree->keep_old || tree->no_delete) {
48                        old_node->version = tree->version;
49                        goto update_only;
50                }
51
52                avl_delete(&tree->avl, anode);
53        }
54
55        avl_insert(&tree->avl, &node->avl);
56
57update_only:
58        tree->update(tree, node, old_node);
59}
60
61void
62vlist_flush(struct vlist_tree *tree)
63{
64        struct vlist_node *node, *tmp;
65
66        avl_for_each_element_safe(&tree->avl, node, avl, tmp) {
67                if ((node->version == tree->version || node->version == -1) &&
68                    tree->version != -1)
69                        continue;
70
71                vlist_delete(tree, node);
72        }
73}
74
75void
76vlist_flush_all(struct vlist_tree *tree)
77{
78        tree->version = -1;
79        vlist_flush(tree);
80}
81
82
Note: See TracBrowser for help on using the repository browser.