1
0

list.h 3.7 KB

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