xref: /openbmc/linux/tools/perf/util/intlist.h (revision 0244ad00)
1 #ifndef __PERF_INTLIST_H
2 #define __PERF_INTLIST_H
3 
4 #include <linux/rbtree.h>
5 #include <stdbool.h>
6 
7 #include "rblist.h"
8 
9 struct int_node {
10 	struct rb_node rb_node;
11 	int i;
12 };
13 
14 struct intlist {
15 	struct rblist rblist;
16 };
17 
18 struct intlist *intlist__new(const char *slist);
19 void intlist__delete(struct intlist *ilist);
20 
21 void intlist__remove(struct intlist *ilist, struct int_node *in);
22 int intlist__add(struct intlist *ilist, int i);
23 
24 struct int_node *intlist__entry(const struct intlist *ilist, unsigned int idx);
25 struct int_node *intlist__find(struct intlist *ilist, int i);
26 
27 static inline bool intlist__has_entry(struct intlist *ilist, int i)
28 {
29 	return intlist__find(ilist, i) != NULL;
30 }
31 
32 static inline bool intlist__empty(const struct intlist *ilist)
33 {
34 	return rblist__empty(&ilist->rblist);
35 }
36 
37 static inline unsigned int intlist__nr_entries(const struct intlist *ilist)
38 {
39 	return rblist__nr_entries(&ilist->rblist);
40 }
41 
42 /* For intlist iteration */
43 static inline struct int_node *intlist__first(struct intlist *ilist)
44 {
45 	struct rb_node *rn = rb_first(&ilist->rblist.entries);
46 	return rn ? rb_entry(rn, struct int_node, rb_node) : NULL;
47 }
48 static inline struct int_node *intlist__next(struct int_node *in)
49 {
50 	struct rb_node *rn;
51 	if (!in)
52 		return NULL;
53 	rn = rb_next(&in->rb_node);
54 	return rn ? rb_entry(rn, struct int_node, rb_node) : NULL;
55 }
56 
57 /**
58  * intlist_for_each      - iterate over a intlist
59  * @pos:	the &struct int_node to use as a loop cursor.
60  * @ilist:	the &struct intlist for loop.
61  */
62 #define intlist__for_each(pos, ilist)	\
63 	for (pos = intlist__first(ilist); pos; pos = intlist__next(pos))
64 
65 /**
66  * intlist_for_each_safe - iterate over a intlist safe against removal of
67  *                         int_node
68  * @pos:	the &struct int_node to use as a loop cursor.
69  * @n:		another &struct int_node to use as temporary storage.
70  * @ilist:	the &struct intlist for loop.
71  */
72 #define intlist__for_each_safe(pos, n, ilist)	\
73 	for (pos = intlist__first(ilist), n = intlist__next(pos); pos;\
74 	     pos = n, n = intlist__next(n))
75 #endif /* __PERF_INTLIST_H */
76