You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

list.h 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #ifndef _GPXE_LIST_H
  2. #define _GPXE_LIST_H
  3. /** @file
  4. *
  5. * Linked lists
  6. *
  7. * This linked list handling code is based on the Linux kernel's
  8. * list.h.
  9. */
  10. #include <stddef.h>
  11. #include <assert.h>
  12. /*
  13. * Simple doubly linked list implementation.
  14. *
  15. * Some of the internal functions ("__xxx") are useful when
  16. * manipulating whole lists rather than single entries, as
  17. * sometimes we already know the next/prev entries and we can
  18. * generate better code by using them directly rather than
  19. * using the generic single-entry routines.
  20. */
  21. struct list_head {
  22. struct list_head *next;
  23. struct list_head *prev;
  24. };
  25. #define LIST_HEAD_INIT( name ) { &(name), &(name) }
  26. #define LIST_HEAD( name ) \
  27. struct list_head name = LIST_HEAD_INIT ( name )
  28. #define INIT_LIST_HEAD( ptr ) do { \
  29. (ptr)->next = (ptr); (ptr)->prev = (ptr); \
  30. } while ( 0 )
  31. /*
  32. * Insert a new entry between two known consecutive entries.
  33. *
  34. * This is only for internal list manipulation where we know
  35. * the prev/next entries already!
  36. */
  37. static inline void __list_add ( struct list_head *new,
  38. struct list_head *prev,
  39. struct list_head *next ) {
  40. next->prev = new;
  41. new->next = next;
  42. new->prev = prev;
  43. prev->next = new;
  44. }
  45. /**
  46. * Add a new entry to the head of a list
  47. *
  48. * @v new New entry to be added
  49. * @v head List head to add it after
  50. *
  51. * Insert a new entry after the specified head. This is good for
  52. * implementing stacks.
  53. */
  54. static inline void list_add ( struct list_head *new, struct list_head *head ) {
  55. __list_add ( new, head, head->next );
  56. }
  57. #define list_add( new, head ) do { \
  58. assert ( (head)->next->prev == (head) ); \
  59. assert ( (head)->prev->next == (head) ); \
  60. list_add ( (new), (head) ); \
  61. } while ( 0 )
  62. /**
  63. * Add a new entry to the tail of a list
  64. *
  65. * @v new New entry to be added
  66. * @v head List head to add it before
  67. *
  68. * Insert a new entry before the specified head. This is useful for
  69. * implementing queues.
  70. */
  71. static inline void list_add_tail ( struct list_head *new,
  72. struct list_head *head ) {
  73. __list_add ( new, head->prev, head );
  74. }
  75. #define list_add_tail( new, head ) do { \
  76. assert ( (head)->next->prev == (head) ); \
  77. assert ( (head)->prev->next == (head) ); \
  78. list_add_tail ( (new), (head) ); \
  79. } while ( 0 )
  80. /*
  81. * Delete a list entry by making the prev/next entries
  82. * point to each other.
  83. *
  84. * This is only for internal list manipulation where we know
  85. * the prev/next entries already!
  86. */
  87. static inline void __list_del ( struct list_head * prev,
  88. struct list_head * next ) {
  89. next->prev = prev;
  90. prev->next = next;
  91. }
  92. /**
  93. * Delete an entry from a list
  94. *
  95. * @v entry Element to delete from the list
  96. *
  97. * Note that list_empty() on entry does not return true after this;
  98. * the entry is in an undefined state.
  99. */
  100. static inline void list_del ( struct list_head *entry ) {
  101. __list_del ( entry->prev, entry->next );
  102. }
  103. #define list_del( entry ) do { \
  104. assert ( (entry)->prev != NULL ); \
  105. assert ( (entry)->next != NULL ); \
  106. assert ( (entry)->next->prev == (entry) ); \
  107. assert ( (entry)->prev->next == (entry) ); \
  108. list_del ( (entry) ); \
  109. } while ( 0 )
  110. /**
  111. * Test whether a list is empty
  112. *
  113. * @v head List to test.
  114. */
  115. static inline int list_empty ( const struct list_head *head ) {
  116. return head->next == head;
  117. }
  118. /**
  119. * Get the containing struct for this entry
  120. *
  121. * @v ptr The struct list_head pointer
  122. * @v type The type of the struct this is embedded in
  123. * @v member The name of the list_struct within the struct
  124. */
  125. #define list_entry( ptr, type, member ) \
  126. container_of ( ptr, type, member )
  127. /**
  128. * Iterate over a list
  129. *
  130. * @v pos The &struct list_head to use as a loop counter
  131. * @v head The head for your list
  132. */
  133. #define list_for_each( pos, head ) \
  134. for ( pos = (head)->next; pos != (head); pos = pos->next )
  135. /**
  136. * Iterate over entries in a list
  137. *
  138. * @v pos The type * to use as a loop counter
  139. * @v head The head for your list
  140. * @v member The name of the list_struct within the struct
  141. */
  142. #define list_for_each_entry( pos, head, member ) \
  143. for ( pos = list_entry ( (head)->next, typeof ( *pos ), member ); \
  144. &pos->member != (head); \
  145. pos = list_entry ( pos->member.next, typeof ( *pos ), member ) )
  146. /**
  147. * Iterate over entries in a list, safe against deletion of entries
  148. *
  149. * @v pos The type * to use as a loop counter
  150. * @v tmp Another type * to use for temporary storage
  151. * @v head The head for your list
  152. * @v member The name of the list_struct within the struct
  153. */
  154. #define list_for_each_entry_safe( pos, tmp, head, member ) \
  155. for ( pos = list_entry ( (head)->next, typeof ( *pos ), member ), \
  156. tmp = list_entry ( pos->member.next, typeof ( *tmp ), member ); \
  157. &pos->member != (head); \
  158. pos = tmp, \
  159. tmp = list_entry ( tmp->member.next, typeof ( *tmp ), member ) )
  160. #endif /* _GPXE_LIST_H */