tiny-list.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #ifndef TINY_LIST_H_
  2. #define TINY_LIST_H_
  3. #include "util.h"
  4. struct tiny_list {
  5. struct tiny_list *prev;
  6. struct tiny_list *next;
  7. };
  8. static inline void tlist_init(struct tiny_list *list)
  9. {
  10. list->prev = list;
  11. list->next = list;
  12. }
  13. #define TLIST_INITIALIZER(l) { (l).prev = &(l), (l).next = &(l), }
  14. #define TLIST(l) struct tiny_list l = TLIST_INITIALIZER(l)
  15. static inline bool tlist_is_empty(struct tiny_list *list)
  16. {
  17. return list->next == list;
  18. }
  19. static inline void tlist_add_tail(struct tiny_list *e, struct tiny_list *list)
  20. {
  21. e->prev = list->prev;
  22. list->prev->next = e;
  23. list->prev = e;
  24. e->next = list;
  25. }
  26. static inline void tlist_add_head(struct tiny_list *e, struct tiny_list *list)
  27. {
  28. e->next = list->next;
  29. list->next->prev = e;
  30. list->next = e;
  31. e->prev = list;
  32. }
  33. static inline void tlist_del(struct tiny_list *e)
  34. {
  35. e->next->prev = e->prev;
  36. e->prev->next = e->next;
  37. tlist_init(e);
  38. }
  39. static inline void tlist_move_tail(struct tiny_list *e, struct tiny_list *list)
  40. {
  41. tlist_del(e);
  42. tlist_add_tail(e, list);
  43. }
  44. static inline void tlist_move_head(struct tiny_list *e, struct tiny_list *list)
  45. {
  46. tlist_del(e);
  47. tlist_add_head(e, list);
  48. }
  49. static inline void tlist_relocate(struct tiny_list *from, struct tiny_list *to)
  50. {
  51. if (tlist_is_empty(from)) {
  52. tlist_init(to);
  53. } else {
  54. to->next = from->next;
  55. to->prev = from->prev;
  56. to->next->prev = to;
  57. to->prev->next = to;
  58. }
  59. tlist_init(from);
  60. }
  61. #define tlist_entry(p, type, member) container_of(p, type, member)
  62. #define tlist_first_entry(list, type, member) \
  63. tlist_entry((list)->next, type, member)
  64. #define tlist_last_entry(list, type, member) \
  65. tlist_entry((list)->prev, type, member)
  66. #define tlist_for_each(p, list, member) \
  67. for (p = tlist_entry((list)->next, typeof(*p), member); \
  68. &p->member != (list); \
  69. p = tlist_entry(p->member.next, typeof(*p), member))
  70. #define tlist_for_each_delsafe(p, tmp, list, member) \
  71. for (p = tlist_entry((list)->next, typeof(*p), member), \
  72. tmp = tlist_entry(p->member.next, typeof(*p), member); \
  73. &p->member != (list); \
  74. p = tmp, \
  75. tmp = tlist_entry(tmp->member.next, typeof(*p), member))
  76. #endif /* TINY_LIST_H_ */