1 // SPDX-License-Identifier: GPL-2.0-only
3 * Based on intlist.c by:
4 * (c) 2009 Arnaldo Carvalho de Melo <acme@redhat.com>
9 #include <linux/compiler.h>
13 static struct rb_node
*intlist__node_new(struct rblist
*rblist __maybe_unused
,
16 int i
= (int)((long)entry
);
17 struct rb_node
*rc
= NULL
;
18 struct int_node
*node
= malloc(sizeof(*node
));
29 static void int_node__delete(struct int_node
*ilist
)
34 static void intlist__node_delete(struct rblist
*rblist __maybe_unused
,
35 struct rb_node
*rb_node
)
37 struct int_node
*node
= container_of(rb_node
, struct int_node
, rb_node
);
39 int_node__delete(node
);
42 static int intlist__node_cmp(struct rb_node
*rb_node
, const void *entry
)
44 int i
= (int)((long)entry
);
45 struct int_node
*node
= container_of(rb_node
, struct int_node
, rb_node
);
50 int intlist__add(struct intlist
*ilist
, int i
)
52 return rblist__add_node(&ilist
->rblist
, (void *)((long)i
));
55 void intlist__remove(struct intlist
*ilist
, struct int_node
*node
)
57 rblist__remove_node(&ilist
->rblist
, &node
->rb_node
);
60 static struct int_node
*__intlist__findnew(struct intlist
*ilist
,
63 struct int_node
*node
= NULL
;
64 struct rb_node
*rb_node
;
70 rb_node
= rblist__findnew(&ilist
->rblist
, (void *)((long)i
));
72 rb_node
= rblist__find(&ilist
->rblist
, (void *)((long)i
));
75 node
= container_of(rb_node
, struct int_node
, rb_node
);
80 struct int_node
*intlist__find(struct intlist
*ilist
, int i
)
82 return __intlist__findnew(ilist
, i
, false);
85 struct int_node
*intlist__findnew(struct intlist
*ilist
, int i
)
87 return __intlist__findnew(ilist
, i
, true);
90 static int intlist__parse_list(struct intlist
*ilist
, const char *s
)
96 long value
= strtol(s
, &sep
, 10);
98 if (*sep
!= ',' && *sep
!= '\0')
100 err
= intlist__add(ilist
, value
);
104 } while (*sep
!= '\0');
109 struct intlist
*intlist__new(const char *slist
)
111 struct intlist
*ilist
= malloc(sizeof(*ilist
));
114 rblist__init(&ilist
->rblist
);
115 ilist
->rblist
.node_cmp
= intlist__node_cmp
;
116 ilist
->rblist
.node_new
= intlist__node_new
;
117 ilist
->rblist
.node_delete
= intlist__node_delete
;
119 if (slist
&& intlist__parse_list(ilist
, slist
))
125 intlist__delete(ilist
);
129 void intlist__delete(struct intlist
*ilist
)
132 rblist__delete(&ilist
->rblist
);
135 struct int_node
*intlist__entry(const struct intlist
*ilist
, unsigned int idx
)
137 struct int_node
*node
= NULL
;
138 struct rb_node
*rb_node
;
140 rb_node
= rblist__entry(&ilist
->rblist
, idx
);
142 node
= container_of(rb_node
, struct int_node
, rb_node
);