xref: /openbmc/linux/tools/perf/util/strlist.h (revision ca55b2fe)
1 #ifndef __PERF_STRLIST_H
2 #define __PERF_STRLIST_H
3 
4 #include <linux/rbtree.h>
5 #include <stdbool.h>
6 
7 #include "rblist.h"
8 
9 struct str_node {
10 	struct rb_node rb_node;
11 	const char     *s;
12 };
13 
14 struct strlist {
15 	struct rblist rblist;
16 	bool	       dupstr;
17 };
18 
19 struct strlist_config {
20 	bool dont_dupstr;
21 	const char *dirname;
22 };
23 
24 struct strlist *strlist__new(const char *slist, const struct strlist_config *config);
25 void strlist__delete(struct strlist *slist);
26 
27 void strlist__remove(struct strlist *slist, struct str_node *sn);
28 int strlist__load(struct strlist *slist, const char *filename);
29 int strlist__add(struct strlist *slist, const char *str);
30 
31 struct str_node *strlist__entry(const struct strlist *slist, unsigned int idx);
32 struct str_node *strlist__find(struct strlist *slist, const char *entry);
33 
34 static inline bool strlist__has_entry(struct strlist *slist, const char *entry)
35 {
36 	return strlist__find(slist, entry) != NULL;
37 }
38 
39 static inline bool strlist__empty(const struct strlist *slist)
40 {
41 	return rblist__empty(&slist->rblist);
42 }
43 
44 static inline unsigned int strlist__nr_entries(const struct strlist *slist)
45 {
46 	return rblist__nr_entries(&slist->rblist);
47 }
48 
49 /* For strlist iteration */
50 static inline struct str_node *strlist__first(struct strlist *slist)
51 {
52 	struct rb_node *rn = rb_first(&slist->rblist.entries);
53 	return rn ? rb_entry(rn, struct str_node, rb_node) : NULL;
54 }
55 static inline struct str_node *strlist__next(struct str_node *sn)
56 {
57 	struct rb_node *rn;
58 	if (!sn)
59 		return NULL;
60 	rn = rb_next(&sn->rb_node);
61 	return rn ? rb_entry(rn, struct str_node, rb_node) : NULL;
62 }
63 
64 /**
65  * strlist_for_each      - iterate over a strlist
66  * @pos:	the &struct str_node to use as a loop cursor.
67  * @slist:	the &struct strlist for loop.
68  */
69 #define strlist__for_each(pos, slist)	\
70 	for (pos = strlist__first(slist); pos; pos = strlist__next(pos))
71 
72 /**
73  * strlist_for_each_safe - iterate over a strlist safe against removal of
74  *                         str_node
75  * @pos:	the &struct str_node to use as a loop cursor.
76  * @n:		another &struct str_node to use as temporary storage.
77  * @slist:	the &struct strlist for loop.
78  */
79 #define strlist__for_each_safe(pos, n, slist)	\
80 	for (pos = strlist__first(slist), n = strlist__next(pos); pos;\
81 	     pos = n, n = strlist__next(n))
82 #endif /* __PERF_STRLIST_H */
83