strlist.h 2.5 KB

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