list.h 3.7 KB

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