dlist.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. #ifndef __DLIST_H
  2. #define __DLIST_H
  3. /* This file is from Linux Kernel (include/linux/list.h)
  4. * and modified by simply removing hardware prefetching of list items.
  5. * Here by copyright, credits attributed to wherever they belong.
  6. * Kulesh Shanmugasundaram (kulesh [squiggly] isis.poly.edu)
  7. */
  8. /*
  9. * Simple doubly linked list implementation.
  10. *
  11. * Some of the internal functions (“__xxx”) are useful when
  12. * manipulating whole lists rather than single entries, as
  13. * sometimes we already know the next/prev entries and we can
  14. * generate better code by using them directly rather than
  15. * using the generic single-entry routines.
  16. */
  17. /**
  18. * container_of - cast a member of a structure out to the containing structure
  19. *
  20. * @ptr: the pointer to the member.
  21. * @type: the type of the container struct this is embedded in.
  22. * @member: the name of the member within the struct.
  23. *
  24. */
  25. #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
  26. #define container_of(ptr, type, member) ({ \
  27. const typeof( ((type *)0)->member ) *__mptr = (ptr); \
  28. (type *)( (char *)__mptr - offsetof(type,member) );})
  29. /*
  30. * These are non-NULL pointers that will result in page faults
  31. * under normal circumstances, used to verify that nobody uses
  32. * non-initialized list entries.
  33. */
  34. #define LIST_POISON1 ((void *) 0x00100100)
  35. #define LIST_POISON2 ((void *) 0x00200
  36. struct list_head {
  37. struct list_head *next, *prev;
  38. };
  39. #define LIST_HEAD_INIT(name) { &(name), &(name) }
  40. //使name变量的前向指针和后向指针都指向自己
  41. #define LIST_HEAD(name) \
  42. struct list_head name = LIST_HEAD_INIT(name)
  43. // (对小结构体指针变量的初始化)struct list_head *ptr
  44. #define INIT_LIST_HEAD(ptr) do { \
  45. (ptr)->next = (ptr); (ptr)->prev = (ptr); \
  46. } while (0)
  47. /*
  48. * Insert a new entry between two known consecutive entries.
  49. *
  50. * This is only for internal list manipulation where we know
  51. * the prev/next entries already!
  52. */
  53. static inline void __list_add(struct list_head *new,
  54. struct list_head *prev,
  55. struct list_head *next)
  56. {
  57. next->prev = new;
  58. new->next = next;
  59. new->prev = prev;
  60. prev->next = new;
  61. }
  62. /**
  63. * list_add – add a new entry
  64. * @new: new entry to be added
  65. * @head: list head to add it after
  66. *
  67. * Insert a new entry after the specified head.
  68. * This is good for implementing stacks.
  69. */
  70. //将new指向的节点插入到head的后边
  71. static inline void list_add(struct list_head *new, struct list_head *head)
  72. {
  73. __list_add(new, head, head->next);
  74. }
  75. /**
  76. * list_add_tail – add a new entry
  77. * @new: new entry to be added
  78. * @head: list head to add it before
  79. *
  80. * Insert a new entry before the specified head.
  81. * This is useful for implementing queues.
  82. */
  83. //将new指向的节点插入到head的前面
  84. static inline void list_add_tail(struct list_head *new, struct list_head *head)
  85. {
  86. __list_add(new, head->prev, head);
  87. }
  88. /*
  89. * Delete a list entry by making the prev/next entries
  90. * point to each other.
  91. *
  92. * This is only for internal list manipulation where we know
  93. * the prev/next entries already!
  94. */
  95. //删除prev和next之间的结点
  96. static inline void __list_del(struct list_head *prev, struct list_head *next)
  97. {
  98. next->prev = prev;
  99. prev->next = next;
  100. }
  101. /**
  102. * list_del – deletes entry from list.
  103. * @entry: the element to delete from the list.
  104. * Note: list_empty on entry does not return true after this, the entry is in an undefined state.
  105. */
  106. //entry 是将要被删除的结点,linux内核链表不会执行free操作,而是由用户来操作。
  107. static inline void list_del(struct list_head *entry)
  108. {
  109. __list_del(entry->prev, entry->next);
  110. entry->next = (void *) 0;
  111. entry->prev = (void *) 0;
  112. }
  113. /**
  114. * list_del_init – deletes entry from list and reinitialize it.
  115. * @entry: the element to delete from the list.
  116. */
  117. //entry指向的结点被删除之后,使其指针域都指向其本身
  118. static inline void list_del_init(struct list_head *entry)
  119. {
  120. __list_del(entry->prev, entry->next);
  121. INIT_LIST_HEAD(entry);
  122. }
  123. /**
  124. * list_move – delete from one list and add as another’s head
  125. * @list: the entry to move
  126. * @head: the head that will precede our entry
  127. */
  128. //删除list指向的结点,将其插入到head的后边。
  129. static inline void list_move(struct list_head *list,
  130. struct list_head *head)
  131. {
  132. __list_del(list->prev, list->next);
  133. list_add(list, head);
  134. }
  135. /**
  136. * list_move_tail – delete from one list and add as another’s tail
  137. * @list: the entry to move
  138. * @head: the head that will follow our entry
  139. */
  140. //删除list指向的结点,将其插入到head的前边
  141. static inline void list_move_tail(struct list_head *list,
  142. struct list_head *head)
  143. {
  144. __list_del(list->prev, list->next);
  145. list_add_tail(list, head);
  146. }
  147. /**
  148. * list_empty – tests whether a list is empty
  149. * @head: the list to test.
  150. */
  151. //判断一个链表是否为空
  152. static inline int list_empty(struct list_head *head)
  153. {
  154. return head->next == head;
  155. }
  156. //实现两个链表的合并,将list链表的头结点删除,将其所剩余结点插入到head和at之间
  157. static inline void __list_splice(struct list_head *list,
  158. struct list_head *head)
  159. {
  160. struct list_head *first = list->next;
  161. struct list_head *last = list->prev;
  162. struct list_head *at = head->next;
  163. first->prev = head;
  164. head->next = first;
  165. last->next = at;
  166. at->prev = last;
  167. }
  168. /**
  169. * list_splice – join two lists
  170. * @list: the new list to add.
  171. * @head: the place to add it in the first list.
  172. */
  173. static inline void list_splice(struct list_head *list, struct list_head *head)
  174. {
  175. if (!list_empty(list))
  176. __list_splice(list, head);
  177. }
  178. /**
  179. * list_splice_init – join two lists and reinitialise the emptied list.
  180. * @list: the new list to add.
  181. * @head: the place to add it in the first list.
  182. *
  183. * The list at @list is reinitialised
  184. *///将list链表的头结点的指针指向其本身
  185. static inline void list_splice_init(struct list_head *list,
  186. struct list_head *head)
  187. {
  188. if (!list_empty(list)) {
  189. __list_splice(list, head);
  190. INIT_LIST_HEAD(list);
  191. }
  192. }
  193. /**
  194. * list_entry – get the struct for this entry
  195. * @ptr: the &struct list_head pointer.
  196. * @type: the type of the struct this is embedded in.
  197. * @member: the name of the list_struct within the struct.
  198. */
  199. //该宏的实现可以分为两部分,减号左边为小结构体的实际地址,右边为
  200. //是小结构体相对于大结构体的偏移量。
  201. //该宏的返回值是大结构体的地址。
  202. #define list_entry(ptr, type, member) \
  203. ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
  204. /**
  205. * list_for_each - iterate over a list
  206. * @pos: the &struct list_head to use as a loop counter.
  207. * @head: the head for your list.
  208. */
  209. //对小结构体从前向后遍历
  210. #define list_for_each(pos, head) \
  211. for (pos = (head)->next; pos != (head); pos = pos->next)
  212. /**
  213. * list_for_each_prev - iterate over a list backwards
  214. * @pos: the &struct list_head to use as a loop counter.
  215. * @head: the head for your list.
  216. */
  217. //对小结构体从后向前遍历
  218. #define list_for_each_prev(pos, head) \
  219. for (pos = (head)->prev; pos != (head); \
  220. pos = pos->prev)
  221. /**
  222. * list_for_each_safe - iterate over a list safe against removal of list entry
  223. * @pos: the &struct list_head to use as a loop counter.
  224. * @n: another &struct list_head to use as temporary storage
  225. * @head: the head for your list.
  226. */
  227. //当遍历当前结点时,记住下一个结点地址,及时当前被遍历的结点意外被删除,
  228. //也不至于产生断链。
  229. #define list_for_each_safe(pos, n, head) \
  230. for (pos = (head)->next, n = pos->next; pos != (head); \
  231. pos = n, n = pos->next)
  232. /**
  233. * list_for_each_entry - iterate over list of given type
  234. * @pos: the type * to use as a loop counter.
  235. * @head: the head for your list.
  236. * @member: the name of the list_struct within the struct.
  237. */
  238. //对大结构体的遍历,根据小结构来遍历大结构体,pos为大结构体地址
  239. #define list_for_each_entry(pos, head, member) \
  240. for (pos = list_entry((head)->next, typeof(*pos), member); \
  241. &pos->member != (head); \
  242. pos = list_entry(pos->member.next, typeof(*pos), member))
  243. /**
  244. * list_for_each_entry_safe – iterate over list of given type safe against removal of list entry
  245. * @pos: the type * to use as a loop counter.
  246. * @n: another type * to use as temporary storage
  247. * @head: the head for your list.
  248. * @member: the name of the list_struct within the struct.
  249. */
  250. #define list_for_each_entry_safe(pos, n, head, member) \
  251. for (pos = list_entry((head)->next, typeof(*pos), member), \
  252. n = list_entry(pos->member.next, typeof(*pos), member); \
  253. &pos->member != (head); \
  254. pos = n, n = list_entry(n->member.next, typeof(*n), member))
  255. #endif