list.h 3.5 KB

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