intlist.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 - 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(pos, ilist) \
  53. for (pos = intlist__first(ilist); pos; pos = intlist__next(pos))
  54. /**
  55. * intlist_for_each_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_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 */