Commit | Line | Data |
---|---|---|
70b40c4a DA |
1 | /* |
2 | * Based on intlist.c by: | |
3 | * (c) 2009 Arnaldo Carvalho de Melo <acme@redhat.com> | |
4 | * | |
5 | * Licensed under the GPLv2. | |
6 | */ | |
7 | ||
8 | #include <errno.h> | |
9 | #include <stdlib.h> | |
10 | #include <linux/compiler.h> | |
11 | ||
12 | #include "intlist.h" | |
13 | ||
1d037ca1 | 14 | static struct rb_node *intlist__node_new(struct rblist *rblist __maybe_unused, |
70b40c4a DA |
15 | const void *entry) |
16 | { | |
17 | int i = (int)((long)entry); | |
18 | struct rb_node *rc = NULL; | |
19 | struct int_node *node = malloc(sizeof(*node)); | |
20 | ||
21 | if (node != NULL) { | |
22 | node->i = i; | |
23 | rc = &node->rb_node; | |
24 | } | |
25 | ||
26 | return rc; | |
27 | } | |
28 | ||
29 | static void int_node__delete(struct int_node *ilist) | |
30 | { | |
31 | free(ilist); | |
32 | } | |
33 | ||
1d037ca1 | 34 | static void intlist__node_delete(struct rblist *rblist __maybe_unused, |
70b40c4a DA |
35 | struct rb_node *rb_node) |
36 | { | |
37 | struct int_node *node = container_of(rb_node, struct int_node, rb_node); | |
38 | ||
39 | int_node__delete(node); | |
40 | } | |
41 | ||
42 | static int intlist__node_cmp(struct rb_node *rb_node, const void *entry) | |
43 | { | |
44 | int i = (int)((long)entry); | |
45 | struct int_node *node = container_of(rb_node, struct int_node, rb_node); | |
46 | ||
47 | return node->i - i; | |
48 | } | |
49 | ||
50 | int intlist__add(struct intlist *ilist, int i) | |
51 | { | |
52 | return rblist__add_node(&ilist->rblist, (void *)((long)i)); | |
53 | } | |
54 | ||
60ebf328 | 55 | void intlist__remove(struct intlist *ilist, struct int_node *node) |
70b40c4a | 56 | { |
60ebf328 | 57 | rblist__remove_node(&ilist->rblist, &node->rb_node); |
70b40c4a DA |
58 | } |
59 | ||
60 | struct int_node *intlist__find(struct intlist *ilist, int i) | |
61 | { | |
5a3d04d6 ACM |
62 | struct int_node *node; |
63 | struct rb_node *rb_node; | |
64 | ||
65 | if (ilist == NULL) | |
66 | return NULL; | |
70b40c4a | 67 | |
5a3d04d6 ACM |
68 | node = NULL; |
69 | rb_node = rblist__find(&ilist->rblist, (void *)((long)i)); | |
70b40c4a DA |
70 | if (rb_node) |
71 | node = container_of(rb_node, struct int_node, rb_node); | |
72 | ||
73 | return node; | |
74 | } | |
75 | ||
76 | struct intlist *intlist__new(void) | |
77 | { | |
78 | struct intlist *ilist = malloc(sizeof(*ilist)); | |
79 | ||
80 | if (ilist != NULL) { | |
81 | rblist__init(&ilist->rblist); | |
82 | ilist->rblist.node_cmp = intlist__node_cmp; | |
83 | ilist->rblist.node_new = intlist__node_new; | |
84 | ilist->rblist.node_delete = intlist__node_delete; | |
85 | } | |
86 | ||
87 | return ilist; | |
88 | } | |
89 | ||
90 | void intlist__delete(struct intlist *ilist) | |
91 | { | |
92 | if (ilist != NULL) | |
93 | rblist__delete(&ilist->rblist); | |
94 | } | |
95 | ||
96 | struct int_node *intlist__entry(const struct intlist *ilist, unsigned int idx) | |
97 | { | |
98 | struct int_node *node = NULL; | |
99 | struct rb_node *rb_node; | |
100 | ||
101 | rb_node = rblist__entry(&ilist->rblist, idx); | |
102 | if (rb_node) | |
103 | node = container_of(rb_node, struct int_node, rb_node); | |
104 | ||
105 | return node; | |
106 | } |