ark_list.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. #ifndef __ARK_LIST_H__
  2. #define __ARK_LIST_H__
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. //#define LIST_MOVE_REPLACE_CUT_SPLICE
  7. #define offsetof0(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
  8. /**
  9. * container_of - cast a member of a structure out to the containing structure
  10. * @ptr: the pointer to the member. * @type: the type of the container struct this is embedded in.
  11. * @member: the name of the member within the struct.
  12. *
  13. */
  14. #define container_of(ptr, type, member) (type *)((char *)ptr -offsetof0(type,member))
  15. #define LIST_POISON1 NULL
  16. #define LIST_POISON2 NULL
  17. struct list_head {
  18. struct list_head *next, *prev;
  19. };
  20. /**
  21. * list_entry - get the struct for this entry
  22. * @ptr: the &struct list_head pointer.
  23. * @type: the type of the struct this is embedded in.
  24. * @member: the name of the list_struct within the struct.
  25. */
  26. #define list_entry(ptr, type, member) \
  27. container_of(ptr, type, member)
  28. /**
  29. * list_first_entry - get the first element from a list
  30. * @ptr: the list head to take the element from.
  31. * @type: the type of the struct this is embedded in.
  32. * @member: the name of the list_struct within the struct.
  33. *
  34. * Note, that list is expected to be not empty.
  35. */
  36. #define list_first_entry(ptr, type, member) \
  37. list_entry((ptr)->next, type, member)
  38. /**
  39. * LIST_HEAD(name) = { &(name), &(name) }
  40. * init the struct list_head, next = prev = &name
  41. */
  42. #define LIST_HEAD_INIT(name) { &(name), &(name) }
  43. #define LIST_HEAD(name) \
  44. struct list_head name = LIST_HEAD_INIT(name)
  45. /**
  46. * INIT_LIST_HEAD is a fun for init list
  47. * LIST_HEAD_INIT is macro for init list
  48. */
  49. static void INIT_LIST_HEAD(struct list_head *list)
  50. {
  51. list->next = list;
  52. list->prev = list;
  53. }
  54. /**
  55. * list_for_each - iterate over a list
  56. * @pos: the &struct list_head to use as a loop counter.
  57. * @head: the head for your list.
  58. */
  59. #define list_for_each(pos, head) \
  60. for (pos = (head)->next; pos != (head); pos = pos->next)
  61. /**
  62. * list_for_each_safe - iterate over a list safe against removal of list entry
  63. * @pos: the &struct list_head to use as a loop cursor.
  64. * @n: another &struct list_head to use as temporary storage
  65. * @head: the head for your list.
  66. */
  67. #define list_for_each_safe(pos, n, head) \
  68. for (pos = (head)->next, n = pos->next; pos != (head); \
  69. pos = n, n = pos->next)
  70. /**
  71. * list_for_each_r - iterate over a list reversely
  72. * @pos: the &struct list_head to use as a loop counter.
  73. * @head: the head for your list.
  74. */
  75. #define list_for_each_r(pos, head) \
  76. for (pos = (head)->prev; pos != (head); pos = pos->prev)
  77. /*
  78. * Insert a new entry between two known consecutive entries.
  79. *
  80. * This is only for internal list manipulation where we know
  81. * the prev/next entries already!
  82. */
  83. static void __list_add(struct list_head *pnew,
  84. struct list_head *prev,
  85. struct list_head *next)
  86. {
  87. next->prev = pnew;
  88. pnew->next = next;
  89. pnew->prev = prev;
  90. prev->next = pnew;
  91. }
  92. /**
  93. * list_add - add a new entry
  94. * @new: new entry to be added
  95. * @head: list head to add it after
  96. *
  97. * Insert a new entry after the specified head.
  98. * This is good for implementing stacks.
  99. */
  100. static void list_add(struct list_head *pnew, struct list_head *head)
  101. {
  102. __list_add(pnew, head, head->next);
  103. }
  104. /**
  105. * list_add_tail - add a new entry
  106. * @new: new entry to be added
  107. * @head: list head to add it before
  108. *
  109. * Insert a new entry before the specified head.
  110. * This is useful for implementing queues.
  111. */
  112. static void list_add_tail(struct list_head *pnew, struct list_head *head)
  113. {
  114. __list_add(pnew, head->prev, head);
  115. }
  116. /*
  117. * Delete a list entry by making the prev/next entries
  118. * point to each other.
  119. *
  120. * This is only for internal list manipulation where we know
  121. * the prev/next entries already!
  122. * The caller must free the memerry.
  123. */
  124. static void __list_del(struct list_head * prev, struct list_head * next)
  125. {
  126. next->prev = prev;
  127. prev->next = next;
  128. }
  129. /**
  130. * list_del - deletes entry from list.
  131. * @entry: the element to delete from the list.
  132. * Note: list_empty() on entry does not return true after this, the entry is
  133. * in an undefined state.
  134. */
  135. static void list_del(struct list_head *entry)
  136. {
  137. __list_del(entry->prev, entry->next);
  138. entry->next = LIST_POISON1;
  139. entry->prev = LIST_POISON2;
  140. }
  141. /**
  142. * list_del_init - deletes entry from list and reinitialize it.
  143. * @entry: the element to delete from the list.
  144. */
  145. static void list_del_init(struct list_head *entry)
  146. {
  147. __list_del(entry->prev, entry->next);
  148. INIT_LIST_HEAD(entry);
  149. }
  150. /**
  151. * list_is_last - tests whether @list is the last entry in list @head
  152. * @list: the entry to test
  153. * @head: the head of the list
  154. */
  155. static int list_is_last(const struct list_head *list,
  156. const struct list_head *head)
  157. {
  158. return list->next == head;
  159. }
  160. /**
  161. * list_empty - tests whether a list is empty
  162. * @head: the list to test.
  163. */
  164. static inline int list_empty(const struct list_head *head)
  165. {
  166. return head->next == head;
  167. }
  168. /**
  169. * list_empty_careful - tests whether a list is empty and not being modified
  170. * @head: the list to test
  171. *
  172. * Description:
  173. * tests whether a list is empty _and_ checks that no other CPU might be
  174. * in the process of modifying either member (next or prev)
  175. *
  176. * NOTE: using list_empty_careful() without synchronization
  177. * can only be safe if the only activity that can happen
  178. * to the list entry is list_del_init(). Eg. it cannot be used
  179. * if another CPU could re-list_add() it.
  180. */
  181. static int list_empty_careful(const struct list_head *head)
  182. {
  183. struct list_head *next = head->next;
  184. return (next == head) && (next == head->prev);
  185. }
  186. /**
  187. * list_is_singular - tests whether a list has just one entry.
  188. * @head: the list to test.
  189. */
  190. static int list_is_singular(const struct list_head *head)
  191. {
  192. return !list_empty(head) && (head->next == head->prev);
  193. }
  194. /**************************************************************************************
  195. *
  196. * LIST_MOVE_REPLACE_CUT_SPLICE
  197. *
  198. ***************************************************************************************
  199. */
  200. #ifdef LIST_MOVE_REPLACE_CUT_SPLICE
  201. /**
  202. * list_replace - replace old entry by new one
  203. * @old : the element to be replaced
  204. * @new : the new element to insert
  205. *
  206. * If @old was empty, it will be overwritten.
  207. */
  208. static void list_replace(struct list_head *old,
  209. struct list_head *pnew)
  210. {
  211. pnew->next = old->next;
  212. pnew->next->prev = pnew;
  213. pnew->prev = old->prev;
  214. pnew->prev->next = pnew;
  215. }
  216. static void list_replace_init(struct list_head *old,
  217. struct list_head *pnew)
  218. {
  219. list_replace(old, pnew);
  220. INIT_LIST_HEAD(old);
  221. }
  222. /**
  223. * list_move - delete from one list and add as another's head
  224. * @list: the entry to move
  225. * @head: the head that will precede our entry
  226. */
  227. static void list_move(struct list_head *list, struct list_head *head)
  228. {
  229. __list_del(list->prev, list->next);
  230. list_add(list, head);
  231. }
  232. /**
  233. * list_move_tail - delete from one list and add as another's tail
  234. * @list: the entry to move
  235. * @head: the head that will follow our entry
  236. */
  237. static void list_move_tail(struct list_head *list,
  238. struct list_head *head)
  239. {
  240. __list_del(list->prev, list->next);
  241. list_add_tail(list, head);
  242. }
  243. static void __list_cut_position(struct list_head *list,
  244. struct list_head *head, struct list_head *entry)
  245. {
  246. struct list_head *new_first = entry->next;
  247. list->next = head->next;
  248. list->next->prev = list;
  249. list->prev = entry;
  250. entry->next = list;
  251. head->next = new_first;
  252. new_first->prev = head;
  253. }
  254. /**
  255. * list_cut_position - cut a list into two
  256. * @list: a new list to add all removed entries
  257. * @head: a list with entries
  258. * @entry: an entry within head, could be the head itself
  259. * and if so we won't cut the list
  260. *
  261. * This helper moves the initial part of @head, up to and
  262. * including @entry, from @head to @list. You should
  263. * pass on @entry an element you know is on @head. @list
  264. * should be an empty list or a list you do not care about
  265. * losing its data.
  266. *
  267. */
  268. static void list_cut_position(struct list_head *list,
  269. struct list_head *head, struct list_head *entry)
  270. {
  271. if (list_empty(head))
  272. return;
  273. if (list_is_singular(head) &&
  274. (head->next != entry && head != entry))
  275. return;
  276. if (entry == head)
  277. INIT_LIST_HEAD(list);
  278. else
  279. __list_cut_position(list, head, entry);
  280. }
  281. /**
  282. * add list between prev and next
  283. */
  284. static void __list_splice(const struct list_head *list,
  285. struct list_head *prev,
  286. struct list_head *next)
  287. {
  288. struct list_head *first = list->next;
  289. struct list_head *last = list->prev;
  290. first->prev = prev;
  291. prev->next = first;
  292. last->next = next;
  293. next->prev = last;
  294. }
  295. /**
  296. * list_splice - join two lists, this is designed for stacks
  297. * @list: the new list to add.
  298. * @head: the place to add it in the first list.
  299. */
  300. static void list_splice(const struct list_head *list,
  301. struct list_head *head)
  302. {
  303. if (!list_empty(list))
  304. __list_splice(list, head, head->next);
  305. }
  306. /**
  307. * list_splice_tail - join two lists, each list being a queue
  308. * @list: the new list to add.
  309. * @head: the place to add it in the first list.
  310. */
  311. static void list_splice_tail(struct list_head *list,
  312. struct list_head *head)
  313. {
  314. if (!list_empty(list))
  315. __list_splice(list, head->prev, head);
  316. }
  317. /**
  318. * list_splice_init - join two lists and reinitialise the emptied list.
  319. * @list: the new list to add. * @head: the place to add it in the first list.
  320. *
  321. * The list at @list is reinitialised
  322. */
  323. static void list_splice_init(struct list_head *list,
  324. struct list_head *head)
  325. {
  326. if (!list_empty(list))
  327. {
  328. __list_splice(list, head, head->next);
  329. INIT_LIST_HEAD(list);
  330. }
  331. }
  332. /**
  333. * list_splice_tail_init - join two lists and reinitialise the emptied list
  334. * @list: the new list to add.
  335. * @head: the place to add it in the first list.
  336. *
  337. * Each of the lists is a queue.
  338. * The list at @list is reinitialised
  339. */
  340. static void list_splice_tail_init(struct list_head *list,
  341. struct list_head *head)
  342. {
  343. if (!list_empty(list))
  344. {
  345. __list_splice(list, head->prev, head);
  346. INIT_LIST_HEAD(list);
  347. }
  348. }
  349. #endif
  350. #ifdef __cplusplus
  351. }
  352. #endif
  353. #endif// __ARK_LIST_H__