list.h 3.6 KB

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