intlist.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. static inline bool intlist__has_entry(struct intlist *ilist, int i)
  21. {
  22. return intlist__find(ilist, i) != NULL;
  23. }
  24. static inline bool intlist__empty(const struct intlist *ilist)
  25. {
  26. return rblist__empty(&ilist->rblist);
  27. }
  28. static inline unsigned int intlist__nr_entries(const struct intlist *ilist)
  29. {
  30. return rblist__nr_entries(&ilist->rblist);
  31. }
  32. /* For intlist iteration */
  33. static inline struct int_node *intlist__first(struct intlist *ilist)
  34. {
  35. struct rb_node *rn = rb_first(&ilist->rblist.entries);
  36. return rn ? rb_entry(rn, struct int_node, rb_node) : NULL;
  37. }
  38. static inline struct int_node *intlist__next(struct int_node *in)
  39. {
  40. struct rb_node *rn;
  41. if (!in)
  42. return NULL;
  43. rn = rb_next(&in->rb_node);
  44. return rn ? rb_entry(rn, struct int_node, rb_node) : NULL;
  45. }
  46. /**
  47. * intlist_for_each - iterate over a intlist
  48. * @pos: the &struct int_node to use as a loop cursor.
  49. * @ilist: the &struct intlist for loop.
  50. */
  51. #define intlist__for_each(pos, ilist) \
  52. for (pos = intlist__first(ilist); pos; pos = intlist__next(pos))
  53. /**
  54. * intlist_for_each_safe - iterate over a intlist safe against removal of
  55. * int_node
  56. * @pos: the &struct int_node to use as a loop cursor.
  57. * @n: another &struct int_node to use as temporary storage.
  58. * @ilist: the &struct intlist for loop.
  59. */
  60. #define intlist__for_each_safe(pos, n, ilist) \
  61. for (pos = intlist__first(ilist), n = intlist__next(pos); pos;\
  62. pos = n, n = intlist__next(n))
  63. #endif /* __PERF_INTLIST_H */