list.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. struct list {
  2. struct list *next, *prev;
  3. };
  4. static inline void
  5. list_init(struct list *list)
  6. {
  7. list->next = list;
  8. list->prev = list;
  9. }
  10. static inline int
  11. list_empty(struct list *list)
  12. {
  13. return list->next == list;
  14. }
  15. static inline void
  16. list_insert(struct list *link, struct list *new_link)
  17. {
  18. new_link->prev = link->prev;
  19. new_link->next = link;
  20. new_link->prev->next = new_link;
  21. new_link->next->prev = new_link;
  22. }
  23. static inline void
  24. list_append(struct list *list, struct list *new_link)
  25. {
  26. list_insert((struct list *)list, new_link);
  27. }
  28. static inline void
  29. list_prepend(struct list *list, struct list *new_link)
  30. {
  31. list_insert(list->next, new_link);
  32. }
  33. static inline void
  34. list_remove(struct list *link)
  35. {
  36. link->prev->next = link->next;
  37. link->next->prev = link->prev;
  38. }
  39. #define list_entry(link, type, member) \
  40. ((type *)((char *)(link)-(unsigned long)(&((type *)0)->member)))
  41. #define list_head(list, type, member) \
  42. list_entry((list)->next, type, member)
  43. #define list_tail(list, type, member) \
  44. list_entry((list)->prev, type, member)
  45. #define list_next(elm, member) \
  46. list_entry((elm)->member.next, typeof(*elm), member)
  47. #define list_for_each_entry(pos, list, member) \
  48. for (pos = list_head(list, typeof(*pos), member); \
  49. &pos->member != (list); \
  50. pos = list_next(pos, member))