node_list.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #ifndef _Node_List_h_
  2. #define _Node_List_h_
  3. typedef struct node_list {
  4. struct node_list *next;
  5. struct node_list *prev;
  6. }node_list_t;
  7. #define LIST_HEAD_INIT(name) { &(name), &(name) }
  8. #define LIST_HEAD(name) \
  9. struct node_list name = LIST_HEAD_INIT(name)
  10. static inline void init_list_node(struct node_list *list)
  11. {
  12. list->next = list;
  13. list->prev = list;
  14. }
  15. static inline void INIT_LIST_HEAD(struct node_list *list)
  16. {
  17. list->next = list;
  18. list->prev = list;
  19. }
  20. /*
  21. * Insert a new entry between two known consecutive entries.
  22. *
  23. * This is only for internal list manipulation where we know
  24. * the prev/next entries already!
  25. */
  26. static inline void __list_add(struct node_list *node, struct node_list *prev, struct node_list *next)
  27. {
  28. next->prev = node;
  29. node->next = next;
  30. node->prev = prev;
  31. prev->next = node;
  32. }
  33. static inline void list_add_head(struct node_list *head, struct node_list *node) {
  34. __list_add(node, head, head->next);
  35. }
  36. static inline void list_add_tail(struct node_list *head, struct node_list *node)
  37. {
  38. __list_add(node, head->prev, head);
  39. }
  40. static inline void __list_del(struct node_list * prev, struct node_list * next)
  41. {
  42. next->prev = prev;
  43. prev->next = next;
  44. }
  45. /**
  46. * list_del - deletes entry from list.
  47. * @entry: the element to delete from the list.
  48. * Note: list_empty() on entry does not return true after this, the entry is
  49. * in an undefined state.
  50. */
  51. static inline void __list_del_entry(struct node_list *entry)
  52. {
  53. __list_del(entry->prev, entry->next);
  54. }
  55. static inline void list_del(struct node_list *entry)
  56. {
  57. __list_del_entry(entry);
  58. }
  59. /**
  60. * list_empty - tests whether a list is empty
  61. * @head: the list to test.
  62. */
  63. static inline int list_empty(const struct node_list *head)
  64. {
  65. return head->next == head;
  66. }
  67. #define list_for_each(pos, head) \
  68. for (pos = (head)->next; pos != (head); pos = pos->next)
  69. #define list_for_each_safe(pos, n, head) \
  70. for (pos = (head)->next, n = pos->next; pos != (head); pos = n, n = pos->next)
  71. #define list_add list_add_tail
  72. #endif //_Node_List_h_