strlist.h 2.5 KB

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