1 // SPDX-License-Identifier: GPL-2.0-only
3 * Based on strlist.c by:
4 * (c) 2009 Arnaldo Carvalho de Melo <acme@redhat.com>
13 int rblist__add_node(struct rblist
*rblist
, const void *new_entry
)
15 struct rb_node
**p
= &rblist
->entries
.rb_root
.rb_node
;
16 struct rb_node
*parent
= NULL
, *new_node
;
24 rc
= rblist
->node_cmp(parent
, new_entry
);
35 new_node
= rblist
->node_new(rblist
, new_entry
);
39 rb_link_node(new_node
, parent
, p
);
40 rb_insert_color_cached(new_node
, &rblist
->entries
, leftmost
);
46 void rblist__remove_node(struct rblist
*rblist
, struct rb_node
*rb_node
)
48 rb_erase_cached(rb_node
, &rblist
->entries
);
50 rblist
->node_delete(rblist
, rb_node
);
53 static struct rb_node
*__rblist__findnew(struct rblist
*rblist
,
57 struct rb_node
**p
= &rblist
->entries
.rb_root
.rb_node
;
58 struct rb_node
*parent
= NULL
, *new_node
= NULL
;
66 rc
= rblist
->node_cmp(parent
, entry
);
78 new_node
= rblist
->node_new(rblist
, entry
);
80 rb_link_node(new_node
, parent
, p
);
81 rb_insert_color_cached(new_node
,
82 &rblist
->entries
, leftmost
);
90 struct rb_node
*rblist__find(struct rblist
*rblist
, const void *entry
)
92 return __rblist__findnew(rblist
, entry
, false);
95 struct rb_node
*rblist__findnew(struct rblist
*rblist
, const void *entry
)
97 return __rblist__findnew(rblist
, entry
, true);
100 void rblist__init(struct rblist
*rblist
)
102 if (rblist
!= NULL
) {
103 rblist
->entries
= RB_ROOT_CACHED
;
104 rblist
->nr_entries
= 0;
110 void rblist__exit(struct rblist
*rblist
)
112 struct rb_node
*pos
, *next
= rb_first_cached(&rblist
->entries
);
117 rblist__remove_node(rblist
, pos
);
121 void rblist__delete(struct rblist
*rblist
)
123 if (rblist
!= NULL
) {
124 rblist__exit(rblist
);
129 struct rb_node
*rblist__entry(const struct rblist
*rblist
, unsigned int idx
)
131 struct rb_node
*node
;
133 for (node
= rb_first_cached(&rblist
->entries
); node
;
134 node
= rb_next(node
)) {