2 * Based on intlist.c by:
3 * (c) 2009 Arnaldo Carvalho de Melo <acme@redhat.com>
5 * Licensed under the GPLv2.
10 #include <linux/compiler.h>
14 static struct rb_node *intlist__node_new(struct rblist *rblist __maybe_unused,
17 int i = (int)((long)entry);
18 struct rb_node *rc = NULL;
19 struct int_node *node = malloc(sizeof(*node));
30 static void int_node__delete(struct int_node *ilist)
35 static void intlist__node_delete(struct rblist *rblist __maybe_unused,
36 struct rb_node *rb_node)
38 struct int_node *node = container_of(rb_node, struct int_node, rb_node);
40 int_node__delete(node);
43 static int intlist__node_cmp(struct rb_node *rb_node, const void *entry)
45 int i = (int)((long)entry);
46 struct int_node *node = container_of(rb_node, struct int_node, rb_node);
51 int intlist__add(struct intlist *ilist, int i)
53 return rblist__add_node(&ilist->rblist, (void *)((long)i));
56 void intlist__remove(struct intlist *ilist, struct int_node *node)
58 rblist__remove_node(&ilist->rblist, &node->rb_node);
61 struct int_node *intlist__find(struct intlist *ilist, int i)
63 struct int_node *node;
64 struct rb_node *rb_node;
70 rb_node = rblist__find(&ilist->rblist, (void *)((long)i));
72 node = container_of(rb_node, struct int_node, rb_node);
77 static int intlist__parse_list(struct intlist *ilist, const char *s)
83 long value = strtol(s, &sep, 10);
85 if (*sep != ',' && *sep != '\0')
87 err = intlist__add(ilist, value);
91 } while (*sep != '\0');
96 struct intlist *intlist__new(const char *slist)
98 struct intlist *ilist = malloc(sizeof(*ilist));
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;
106 if (slist && intlist__parse_list(ilist, slist))
112 intlist__delete(ilist);
116 void intlist__delete(struct intlist *ilist)
119 rblist__delete(&ilist->rblist);
122 struct int_node *intlist__entry(const struct intlist *ilist, unsigned int idx)
124 struct int_node *node = NULL;
125 struct rb_node *rb_node;
127 rb_node = rblist__entry(&ilist->rblist, idx);
129 node = container_of(rb_node, struct int_node, rb_node);