intlist.h 2.1 KB

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