intlist.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __PERF_INTLIST_H
  3. #define __PERF_INTLIST_H
  4. #include <linux/rbtree.h>
  5. #include <stdbool.h>
  6. #include "rblist.h"
  7. struct int_node {
  8. struct rb_node rb_node;
  9. int i;
  10. void *priv;
  11. };
  12. struct intlist {
  13. struct rblist rblist;
  14. };
  15. struct intlist *intlist__new(const char *slist);
  16. void intlist__delete(struct intlist *ilist);
  17. void intlist__remove(struct intlist *ilist, struct int_node *in);
  18. int intlist__add(struct intlist *ilist, int i);
  19. struct int_node *intlist__entry(const struct intlist *ilist, unsigned int idx);
  20. struct int_node *intlist__find(struct intlist *ilist, int i);
  21. struct int_node *intlist__findnew(struct intlist *ilist, int i);
  22. static inline bool intlist__has_entry(struct intlist *ilist, int i)
  23. {
  24. return intlist__find(ilist, i) != NULL;
  25. }
  26. static inline bool intlist__empty(const struct intlist *ilist)
  27. {
  28. return rblist__empty(&ilist->rblist);
  29. }
  30. static inline unsigned int intlist__nr_entries(const struct intlist *ilist)
  31. {
  32. return rblist__nr_entries(&ilist->rblist);
  33. }
  34. /* For intlist iteration */
  35. static inline struct int_node *intlist__first(struct intlist *ilist)
  36. {
  37. struct rb_node *rn = rb_first_cached(&ilist->rblist.entries);
  38. return rn ? rb_entry(rn, struct int_node, rb_node) : NULL;
  39. }
  40. static inline struct int_node *intlist__next(struct int_node *in)
  41. {
  42. struct rb_node *rn;
  43. if (!in)
  44. return NULL;
  45. rn = rb_next(&in->rb_node);
  46. return rn ? rb_entry(rn, struct int_node, rb_node) : NULL;
  47. }
  48. /**
  49. * intlist__for_each_entry - iterate over a intlist
  50. * @pos: the &struct int_node to use as a loop cursor.
  51. * @ilist: the &struct intlist for loop.
  52. */
  53. #define intlist__for_each_entry(pos, ilist) \
  54. for (pos = intlist__first(ilist); pos; pos = intlist__next(pos))
  55. /**
  56. * intlist__for_each_entry_safe - iterate over a intlist safe against removal of
  57. * int_node
  58. * @pos: the &struct int_node to use as a loop cursor.
  59. * @n: another &struct int_node to use as temporary storage.
  60. * @ilist: the &struct intlist for loop.
  61. */
  62. #define intlist__for_each_entry_safe(pos, n, ilist) \
  63. for (pos = intlist__first(ilist), n = intlist__next(pos); pos;\
  64. pos = n, n = intlist__next(n))
  65. #endif /* __PERF_INTLIST_H */