list.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #if !defined(_BLKID_LIST_H) && !defined(LIST_HEAD)
  2. #define _BLKID_LIST_H
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. #ifdef __GNUC__
  7. #define _INLINE_ static __inline__
  8. #else /* For Watcom C */
  9. #define _INLINE_ static inline
  10. #endif
  11. /*
  12. * Simple doubly linked list implementation.
  13. *
  14. * Some of the internal functions ("__xxx") are useful when
  15. * manipulating whole lists rather than single entries, as
  16. * sometimes we already know the next/prev entries and we can
  17. * generate better code by using them directly rather than
  18. * using the generic single-entry routines.
  19. */
  20. struct list_head {
  21. struct list_head *next, *prev;
  22. };
  23. #define LIST_HEAD_INIT(name) { &(name), &(name) }
  24. #define LIST_HEAD(name) \
  25. struct list_head name = LIST_HEAD_INIT(name)
  26. #define INIT_LIST_HEAD(ptr) do { \
  27. (ptr)->next = (ptr); (ptr)->prev = (ptr); \
  28. } while (0)
  29. /*
  30. * Insert a new entry between two known consecutive entries.
  31. *
  32. * This is only for internal list manipulation where we know
  33. * the prev/next entries already!
  34. */
  35. _INLINE_ void __list_add(struct list_head * add,
  36. struct list_head * prev,
  37. struct list_head * next)
  38. {
  39. next->prev = add;
  40. add->next = next;
  41. add->prev = prev;
  42. prev->next = add;
  43. }
  44. /**
  45. * list_add - add a new entry
  46. * @add: new entry to be added
  47. * @head: list head to add it after
  48. *
  49. * Insert a new entry after the specified head.
  50. * This is good for implementing stacks.
  51. */
  52. _INLINE_ void list_add(struct list_head *add, struct list_head *head)
  53. {
  54. __list_add(add, head, head->next);
  55. }
  56. /**
  57. * list_add_tail - add a new entry
  58. * @add: new entry to be added
  59. * @head: list head to add it before
  60. *
  61. * Insert a new entry before the specified head.
  62. * This is useful for implementing queues.
  63. */
  64. _INLINE_ void list_add_tail(struct list_head *add, struct list_head *head)
  65. {
  66. __list_add(add, head->prev, head);
  67. }
  68. /*
  69. * Delete a list entry by making the prev/next entries
  70. * point to each other.
  71. *
  72. * This is only for internal list manipulation where we know
  73. * the prev/next entries already!
  74. */
  75. _INLINE_ void __list_del(struct list_head * prev,
  76. struct list_head * next)
  77. {
  78. next->prev = prev;
  79. prev->next = next;
  80. }
  81. /**
  82. * list_del - deletes entry from list.
  83. * @entry: the element to delete from the list.
  84. *
  85. * list_empty() on @entry does not return true after this, @entry is
  86. * in an undefined state.
  87. */
  88. _INLINE_ void list_del(struct list_head *entry)
  89. {
  90. __list_del(entry->prev, entry->next);
  91. }
  92. /**
  93. * list_del_init - deletes entry from list and reinitialize it.
  94. * @entry: the element to delete from the list.
  95. */
  96. _INLINE_ void list_del_init(struct list_head *entry)
  97. {
  98. __list_del(entry->prev, entry->next);
  99. INIT_LIST_HEAD(entry);
  100. }
  101. /**
  102. * list_empty - tests whether a list is empty
  103. * @head: the list to test.
  104. */
  105. _INLINE_ int list_empty(struct list_head *head)
  106. {
  107. return head->next == head;
  108. }
  109. /**
  110. * list_splice - join two lists
  111. * @list: the new list to add.
  112. * @head: the place to add it in the first list.
  113. */
  114. _INLINE_ void list_splice(struct list_head *list, struct list_head *head)
  115. {
  116. struct list_head *first = list->next;
  117. if (first != list) {
  118. struct list_head *last = list->prev;
  119. struct list_head *at = head->next;
  120. first->prev = head;
  121. head->next = first;
  122. last->next = at;
  123. at->prev = last;
  124. }
  125. }
  126. /**
  127. * list_entry - get the struct for this entry
  128. * @ptr: the &struct list_head pointer.
  129. * @type: the type of the struct this is embedded in.
  130. * @member: the name of the list_struct within the struct.
  131. */
  132. #define list_entry(ptr, type, member) \
  133. ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
  134. /**
  135. * list_for_each - iterate over elements in a list
  136. * @pos: the &struct list_head to use as a loop counter.
  137. * @head: the head for your list.
  138. */
  139. #define list_for_each(pos, head) \
  140. for (pos = (head)->next; pos != (head); pos = pos->next)
  141. /**
  142. * list_for_each_safe - iterate over elements in a list, but don't dereference
  143. * pos after the body is done (in case it is freed)
  144. * @pos: the &struct list_head to use as a loop counter.
  145. * @pnext: the &struct list_head to use as a pointer to the next item.
  146. * @head: the head for your list (not included in iteration).
  147. */
  148. #define list_for_each_safe(pos, pnext, head) \
  149. for (pos = (head)->next, pnext = pos->next; pos != (head); \
  150. pos = pnext, pnext = pos->next)
  151. #undef _INLINE_
  152. #ifdef __cplusplus
  153. }
  154. #endif
  155. #endif /* _BLKID_LIST_H */