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.1KB

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