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

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