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; | |
2969b129 | 23 | node->priv = NULL; |
70b40c4a DA |
24 | rc = &node->rb_node; |
25 | } | |
26 | ||
27 | return rc; | |
28 | } | |
29 | ||
30 | static void int_node__delete(struct int_node *ilist) | |
31 | { | |
32 | free(ilist); | |
33 | } | |
34 | ||
1d037ca1 | 35 | static void intlist__node_delete(struct rblist *rblist __maybe_unused, |
70b40c4a DA |
36 | struct rb_node *rb_node) |
37 | { | |
38 | struct int_node *node = container_of(rb_node, struct int_node, rb_node); | |
39 | ||
40 | int_node__delete(node); | |
41 | } | |
42 | ||
43 | static int intlist__node_cmp(struct rb_node *rb_node, const void *entry) | |
44 | { | |
45 | int i = (int)((long)entry); | |
46 | struct int_node *node = container_of(rb_node, struct int_node, rb_node); | |
47 | ||
48 | return node->i - i; | |
49 | } | |
50 | ||
51 | int intlist__add(struct intlist *ilist, int i) | |
52 | { | |
53 | return rblist__add_node(&ilist->rblist, (void *)((long)i)); | |
54 | } | |
55 | ||
60ebf328 | 56 | void intlist__remove(struct intlist *ilist, struct int_node *node) |
70b40c4a | 57 | { |
60ebf328 | 58 | rblist__remove_node(&ilist->rblist, &node->rb_node); |
70b40c4a DA |
59 | } |
60 | ||
61 | struct int_node *intlist__find(struct intlist *ilist, int i) | |
62 | { | |
5a3d04d6 ACM |
63 | struct int_node *node; |
64 | struct rb_node *rb_node; | |
65 | ||
66 | if (ilist == NULL) | |
67 | return NULL; | |
70b40c4a | 68 | |
5a3d04d6 ACM |
69 | node = NULL; |
70 | rb_node = rblist__find(&ilist->rblist, (void *)((long)i)); | |
70b40c4a DA |
71 | if (rb_node) |
72 | node = container_of(rb_node, struct int_node, rb_node); | |
73 | ||
74 | return node; | |
75 | } | |
76 | ||
ffe0fb76 ACM |
77 | static int intlist__parse_list(struct intlist *ilist, const char *s) |
78 | { | |
79 | char *sep; | |
80 | int err; | |
81 | ||
82 | do { | |
83 | long value = strtol(s, &sep, 10); | |
84 | err = -EINVAL; | |
85 | if (*sep != ',' && *sep != '\0') | |
86 | break; | |
87 | err = intlist__add(ilist, value); | |
88 | if (err) | |
89 | break; | |
90 | s = sep + 1; | |
91 | } while (*sep != '\0'); | |
92 | ||
93 | return err; | |
94 | } | |
95 | ||
96 | struct intlist *intlist__new(const char *slist) | |
70b40c4a DA |
97 | { |
98 | struct intlist *ilist = malloc(sizeof(*ilist)); | |
99 | ||
100 | if (ilist != NULL) { | |
101 | rblist__init(&ilist->rblist); | |
102 | ilist->rblist.node_cmp = intlist__node_cmp; | |
103 | ilist->rblist.node_new = intlist__node_new; | |
104 | ilist->rblist.node_delete = intlist__node_delete; | |
ffe0fb76 ACM |
105 | |
106 | if (slist && intlist__parse_list(ilist, slist)) | |
107 | goto out_delete; | |
70b40c4a DA |
108 | } |
109 | ||
110 | return ilist; | |
ffe0fb76 ACM |
111 | out_delete: |
112 | intlist__delete(ilist); | |
113 | return NULL; | |
70b40c4a DA |
114 | } |
115 | ||
116 | void intlist__delete(struct intlist *ilist) | |
117 | { | |
118 | if (ilist != NULL) | |
119 | rblist__delete(&ilist->rblist); | |
120 | } | |
121 | ||
122 | struct int_node *intlist__entry(const struct intlist *ilist, unsigned int idx) | |
123 | { | |
124 | struct int_node *node = NULL; | |
125 | struct rb_node *rb_node; | |
126 | ||
127 | rb_node = rblist__entry(&ilist->rblist, idx); | |
128 | if (rb_node) | |
129 | node = container_of(rb_node, struct int_node, rb_node); | |
130 | ||
131 | return node; | |
132 | } |