strlist.h 2.2 KB

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