list.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef LIST_H
  3. #define LIST_H
  4. /*
  5. * Copied from include/linux/...
  6. */
  7. #undef offsetof
  8. #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
  9. /**
  10. * container_of - cast a member of a structure out to the containing structure
  11. * @ptr: the pointer to the member.
  12. * @type: the type of the container struct this is embedded in.
  13. * @member: the name of the member within the struct.
  14. *
  15. */
  16. #define container_of(ptr, type, member) ({ \
  17. const typeof( ((type *)0)->member ) *__mptr = (ptr); \
  18. (type *)( (char *)__mptr - offsetof(type,member) );})
  19. struct list_head {
  20. struct list_head *next, *prev;
  21. };
  22. void INIT_LIST_HEAD(struct list_head *list);
  23. #define LIST_HEAD_INIT(name) { &(name), &(name) }
  24. #define LIST_HEAD(name) \
  25. struct list_head name = LIST_HEAD_INIT(name)
  26. /**
  27. * list_entry - get the struct for this entry
  28. * @ptr: the &struct list_head pointer.
  29. * @type: the type of the struct this is embedded in.
  30. * @member: the name of the list_head within the struct.
  31. */
  32. #define list_entry(ptr, type, member) \
  33. container_of(ptr, type, member)
  34. /**
  35. * list_for_each_entry - iterate over list of given type
  36. * @pos: the type * to use as a loop cursor.
  37. * @head: the head for your list.
  38. * @member: the name of the list_head within the struct.
  39. */
  40. #define list_for_each_entry(pos, head, member) \
  41. for (pos = list_entry((head)->next, typeof(*pos), member); \
  42. &pos->member != (head); \
  43. pos = list_entry(pos->member.next, typeof(*pos), member))
  44. /**
  45. * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
  46. * @pos: the type * to use as a loop cursor.
  47. * @n: another type * to use as temporary storage
  48. * @head: the head for your list.
  49. * @member: the name of the list_head within the struct.
  50. */
  51. #define list_for_each_entry_safe(pos, n, head, member) \
  52. for (pos = list_entry((head)->next, typeof(*pos), member), \
  53. n = list_entry(pos->member.next, typeof(*pos), member); \
  54. &pos->member != (head); \
  55. pos = n, n = list_entry(n->member.next, typeof(*n), member))
  56. /**
  57. * list_empty - tests whether a list is empty
  58. * @head: the list to test.
  59. */
  60. static inline int list_empty(const struct list_head *head)
  61. {
  62. return head->next == head;
  63. }
  64. /*
  65. * Insert a new entry between two known consecutive entries.
  66. *
  67. * This is only for internal list manipulation where we know
  68. * the prev/next entries already!
  69. */
  70. static inline void __list_add(struct list_head *_new,
  71. struct list_head *prev,
  72. struct list_head *next)
  73. {
  74. next->prev = _new;
  75. _new->next = next;
  76. _new->prev = prev;
  77. prev->next = _new;
  78. }
  79. /**
  80. * list_add_tail - add a new entry
  81. * @new: new entry to be added
  82. * @head: list head to add it before
  83. *
  84. * Insert a new entry before the specified head.
  85. * This is useful for implementing queues.
  86. */
  87. static inline void list_add_tail(struct list_head *_new, struct list_head *head)
  88. {
  89. __list_add(_new, head->prev, head);
  90. }
  91. /*
  92. * Delete a list entry by making the prev/next entries
  93. * point to each other.
  94. *
  95. * This is only for internal list manipulation where we know
  96. * the prev/next entries already!
  97. */
  98. static inline void __list_del(struct list_head *prev, struct list_head *next)
  99. {
  100. next->prev = prev;
  101. prev->next = next;
  102. }
  103. #ifndef NULL
  104. #define NULL ((void *)0)
  105. #endif
  106. #define LIST_POISON1 ((void *) NULL)
  107. #define LIST_POISON2 ((void *) NULL)
  108. /**
  109. * list_del - deletes entry from list.
  110. * @entry: the element to delete from the list.
  111. * Note: list_empty() on entry does not return true after this, the entry is
  112. * in an undefined state.
  113. */
  114. static inline void list_del(struct list_head *entry)
  115. {
  116. __list_del(entry->prev, entry->next);
  117. entry->next = (struct list_head*)LIST_POISON1;
  118. entry->prev = (struct list_head*)LIST_POISON2;
  119. }
  120. #endif