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