65109d7b262ef82837dbfb2912922c4f036a3956
[project/netifd.git] / utils.c
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 #include <string.h>
15 #include <stdlib.h>
16 #include "utils.h"
17
18 void
19 __vlist_simple_init(struct vlist_simple_tree *tree, int offset)
20 {
21 INIT_LIST_HEAD(&tree->list);
22 tree->version = 1;
23 tree->head_offset = offset;
24 }
25
26 void
27 vlist_simple_delete(struct vlist_simple_tree *tree, struct vlist_simple_node *node)
28 {
29 char *ptr;
30
31 list_del(&node->list);
32 ptr = (char *) node - tree->head_offset;
33 free(ptr);
34 }
35
36 void
37 vlist_simple_flush(struct vlist_simple_tree *tree)
38 {
39 struct vlist_simple_node *n, *tmp;
40
41 list_for_each_entry_safe(n, tmp, &tree->list, list) {
42 if ((n->version == tree->version || n->version == -1) &&
43 tree->version != -1)
44 continue;
45
46 vlist_simple_delete(tree, n);
47 }
48 }
49
50 void
51 vlist_simple_replace(struct vlist_simple_tree *dest, struct vlist_simple_tree *old)
52 {
53 struct vlist_simple_node *n, *tmp;
54
55 vlist_simple_update(dest);
56 list_for_each_entry_safe(n, tmp, &old->list, list) {
57 list_del(&n->list);
58 vlist_simple_add(dest, n);
59 }
60 vlist_simple_flush(dest);
61 }
62
63 void
64 vlist_simple_flush_all(struct vlist_simple_tree *tree)
65 {
66 tree->version = -1;
67 vlist_simple_flush(tree);
68 }