lite-list.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /*
  2. * Copyright (c) 2017-2019 Tencent Group. All rights reserved.
  3. * License-Identifier: Apache-2.0
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  6. * not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. /**
  19. * Edit by shockcao@tencent.com 2018/3/15
  20. */
  21. #ifndef _LINUX_LIST_H
  22. #define _LINUX_LIST_H
  23. #define inline __inline
  24. typedef struct list_head list_head_t;
  25. struct list_head {
  26. struct list_head *next, *prev;
  27. };
  28. /*
  29. * Simple doubly linked list implementation.
  30. *
  31. * Some of the internal functions ("__xxx") are useful when
  32. * manipulating whole lists rather than single entries, as
  33. * sometimes we already know the next/prev entries and we can
  34. * generate better code by using them directly rather than
  35. * using the generic single-entry routines.
  36. */
  37. #define LIST_HEAD_INIT(name) \
  38. { \
  39. &(name), &(name) \
  40. }
  41. #define LIST_HEAD(name) struct list_head name = LIST_HEAD_INIT(name)
  42. static inline void INIT_LIST_HEAD(struct list_head *list)
  43. {
  44. list->next = list;
  45. list->prev = list;
  46. }
  47. /*
  48. * Insert a new_ptr 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_ptr, struct list_head *prev, struct list_head *next)
  54. {
  55. next->prev = new_ptr;
  56. new_ptr->next = next;
  57. new_ptr->prev = prev;
  58. prev->next = new_ptr;
  59. }
  60. /**
  61. * list_add - add a new_ptr entry
  62. * @new_ptr: new_ptr entry to be added
  63. * @head: list head to add it after
  64. *
  65. * Insert a new_ptr entry after the specified head.
  66. * This is good for implementing stacks.
  67. */
  68. static inline void list_add(struct list_head *new_ptr, struct list_head *head)
  69. {
  70. __list_add(new_ptr, head, head->next);
  71. }
  72. /**
  73. * list_add_tail - add a new_ptr entry
  74. * @new_ptr: new_ptr entry to be added
  75. * @head: list head to add it before
  76. *
  77. * Insert a new_ptr entry before the specified head.
  78. * This is useful for implementing queues.
  79. */
  80. static inline void list_add_tail(struct list_head *new_ptr, struct list_head *head)
  81. {
  82. __list_add(new_ptr, head->prev, head);
  83. }
  84. /*
  85. * Delete a list entry by making the prev/next entries
  86. * point to each other.
  87. *
  88. * This is only for internal list manipulation where we know
  89. * the prev/next entries already!
  90. */
  91. static inline void __list_del(struct list_head *prev, struct list_head *next)
  92. {
  93. next->prev = prev;
  94. prev->next = next;
  95. }
  96. /**
  97. * list_del - deletes entry from list.
  98. * @entry: the element to delete from the list.
  99. * Note: list_empty() on entry does not return true after this, the entry is
  100. * in an undefined state.
  101. */
  102. static inline void __list_del_entry(struct list_head *entry)
  103. {
  104. __list_del(entry->prev, entry->next);
  105. }
  106. static inline void list_del(struct list_head *entry)
  107. {
  108. __list_del(entry->prev, entry->next);
  109. }
  110. /**
  111. * list_replace - replace old entry by new_ptr one
  112. * @old : the element to be replaced
  113. * @new_ptr : the new_ptr element to insert
  114. *
  115. * If @old was empty, it will be overwritten.
  116. */
  117. static inline void list_replace(struct list_head *old, struct list_head *new_ptr)
  118. {
  119. new_ptr->next = old->next;
  120. new_ptr->next->prev = new_ptr;
  121. new_ptr->prev = old->prev;
  122. new_ptr->prev->next = new_ptr;
  123. }
  124. static inline void list_replace_init(struct list_head *old, struct list_head *new_ptr)
  125. {
  126. list_replace(old, new_ptr);
  127. INIT_LIST_HEAD(old);
  128. }
  129. /**
  130. * list_del_init - deletes entry from list and reinitialize it.
  131. * @entry: the element to delete from the list.
  132. */
  133. static inline void list_del_init(struct list_head *entry)
  134. {
  135. __list_del_entry(entry);
  136. INIT_LIST_HEAD(entry);
  137. }
  138. /**
  139. * list_move - delete from one list and add as another's head
  140. * @list: the entry to move
  141. * @head: the head that will precede our entry
  142. */
  143. static inline void list_move(struct list_head *list, struct list_head *head)
  144. {
  145. __list_del_entry(list);
  146. list_add(list, head);
  147. }
  148. /**
  149. * list_move_tail - delete from one list and add as another's tail
  150. * @list: the entry to move
  151. * @head: the head that will follow our entry
  152. */
  153. static inline void list_move_tail(struct list_head *list, struct list_head *head)
  154. {
  155. __list_del_entry(list);
  156. list_add_tail(list, head);
  157. }
  158. /**
  159. * list_is_last - tests whether @list is the last entry in list @head
  160. * @list: the entry to test
  161. * @head: the head of the list
  162. */
  163. static inline int list_is_last(const struct list_head *list, const struct list_head *head)
  164. {
  165. return list->next == head;
  166. }
  167. /**
  168. * list_empty - tests whether a list is empty
  169. * @head: the list to test.
  170. */
  171. static inline int list_empty(const struct list_head *head)
  172. {
  173. return head->next == head;
  174. }
  175. /**
  176. * list_empty_careful - tests whether a list is empty and not being modified
  177. * @head: the list to test
  178. *
  179. * Description:
  180. * tests whether a list is empty _and_ checks that no other CPU might be
  181. * in the process of modifying either member (next or prev)
  182. *
  183. * NOTE: using list_empty_careful() without synchronization
  184. * can only be safe if the only activity that can happen
  185. * to the list entry is list_del_init(). Eg. it cannot be used
  186. * if another CPU could re-list_add() it.
  187. */
  188. static inline int list_empty_careful(const struct list_head *head)
  189. {
  190. struct list_head *next = head->next;
  191. return (next == head) && (next == head->prev);
  192. }
  193. /**
  194. * list_rotate_left - rotate the list to the left
  195. * @head: the head of the list
  196. */
  197. static inline void list_rotate_left(struct list_head *head)
  198. {
  199. struct list_head *first;
  200. if (!list_empty(head)) {
  201. first = head->next;
  202. list_move_tail(first, head);
  203. }
  204. }
  205. /**
  206. * list_is_singular - tests whether a list has just one entry.
  207. * @head: the list to test.
  208. */
  209. static inline int list_is_singular(const struct list_head *head)
  210. {
  211. return !list_empty(head) && (head->next == head->prev);
  212. }
  213. /**
  214. * list_entry - get the struct for this entry
  215. * @ptr: the &struct list_head pointer.
  216. * @type: the type of the struct this is embedded in.
  217. * @member: the name of the list_struct within the struct.
  218. */
  219. #define list_entry(ptr, type, member) container_of(ptr, type, member)
  220. /**
  221. * list_first_entry - get the first element from a list
  222. * @ptr: the list head to take the element from.
  223. * @type: the type of the struct this is embedded in.
  224. * @member: the name of the list_struct within the struct.
  225. *
  226. * Note, that list is expected to be not empty.
  227. */
  228. #define list_first_entry(ptr, type, member) list_entry((ptr)->next, type, member)
  229. /**
  230. * list_last_entry - get the last element from a list
  231. * @ptr: the list head to take the element from.
  232. * @type: the type of the struct this is embedded in.
  233. * @member: the name of the list_struct within the struct.
  234. *
  235. * Note, that list is expected to be not empty.
  236. */
  237. #define list_last_entry(ptr, type, member) list_entry((ptr)->prev, type, member)
  238. /**
  239. * list_first_entry_or_null - get the first element from a list
  240. * @ptr: the list head to take the element from.
  241. * @type: the type of the struct this is embedded in.
  242. * @member: the name of the list_struct within the struct.
  243. *
  244. * Note that if the list is empty, it returns NULL.
  245. */
  246. #define list_first_entry_or_null(ptr, type, member) (!list_empty(ptr) ? list_first_entry(ptr, type, member) : NULL)
  247. /**
  248. * list_next_entry - get the next element in list
  249. * @pos: the type * to cursor
  250. * @member: the name of the list_struct within the struct.
  251. */
  252. #define list_next_entry(pos, member, type) list_entry((pos)->member.next, type, member)
  253. /**
  254. * list_prev_entry - get the prev element in list
  255. * @pos: the type * to cursor
  256. * @member: the name of the list_struct within the struct.
  257. */
  258. #define list_prev_entry(pos, member, type) list_entry((pos)->member.prev, type, member)
  259. /**
  260. * list_for_each - iterate over a list
  261. * @pos: the &struct list_head to use as a loop cursor.
  262. * @head: the head for your list.
  263. */
  264. #define list_for_each(pos, head) for (pos = (head)->next; pos != (head); pos = pos->next)
  265. /**
  266. * __list_for_each - iterate over a list
  267. * @pos: the &struct list_head to use as a loop cursor.
  268. * @head: the head for your list.
  269. *
  270. * This variant doesn't differ from list_for_each() any more.
  271. * We don't do prefetching in either case.
  272. */
  273. #define __list_for_each(pos, head) for (pos = (head)->next; pos != (head); pos = pos->next)
  274. /**
  275. * list_for_each_prev - iterate over a list backwards
  276. * @pos: the &struct list_head to use as a loop cursor.
  277. * @head: the head for your list.
  278. */
  279. #define list_for_each_prev(pos, head) for (pos = (head)->prev; pos != (head); pos = pos->prev)
  280. /**
  281. * list_for_each_safe - iterate over a list safe against removal of list entry
  282. * @pos: the &struct list_head to use as a loop cursor.
  283. * @n: another &struct list_head to use as temporary storage
  284. * @head: the head for your list.
  285. */
  286. #define list_for_each_safe(pos, n, head) for (pos = (head)->next, n = pos->next; pos != (head); pos = n, n = pos->next)
  287. /**
  288. * list_for_each_prev_safe - iterate over a list backwards safe against removal
  289. * of list entry
  290. * @pos: the &struct list_head to use as a loop cursor.
  291. * @n: another &struct list_head to use as temporary storage
  292. * @head: the head for your list.
  293. */
  294. #define list_for_each_prev_safe(pos, n, head) \
  295. for (pos = (head)->prev, n = pos->prev; pos != (head); pos = n, n = pos->prev)
  296. /**
  297. * list_for_each_entry - iterate over list of given type
  298. * @pos: the type * to use as a loop cursor.
  299. * @head: the head for your list.
  300. * @member: the name of the list_struct within the struct.
  301. */
  302. #define list_for_each_entry(pos, head, member, type) \
  303. for (pos = list_entry((head)->next, type, member); &pos->member != (head); \
  304. pos = list_entry(pos->member.next, type, member))
  305. /**
  306. * list_for_each_entry_reverse - iterate backwards over list of given type.
  307. * @pos: the type * to use as a loop cursor.
  308. * @head: the head for your list.
  309. * @member: the name of the list_struct within the struct.
  310. */
  311. #define list_for_each_entry_reverse(pos, head, member, type) \
  312. for (pos = list_entry((head)->prev, type, member); &pos->member != (head); \
  313. pos = list_entry(pos->member.prev, type, member))
  314. /**
  315. * list_prepare_entry - prepare a pos entry for use in
  316. * list_for_each_entry_continue()
  317. * @pos: the type * to use as a start point
  318. * @head: the head of the list
  319. * @member: the name of the list_struct within the struct.
  320. *
  321. * Prepares a pos entry for use as a start point in
  322. * list_for_each_entry_continue().
  323. */
  324. #define list_prepare_entry(pos, head, member, type) ((pos) ?: list_entry(head, type, member))
  325. /**
  326. * list_for_each_entry_continue - continue iteration over list of given type
  327. * @pos: the type * to use as a loop cursor.
  328. * @head: the head for your list.
  329. * @member: the name of the list_struct within the struct.
  330. *
  331. * Continue to iterate over list of given type, continuing after
  332. * the current position.
  333. */
  334. #define list_for_each_entry_continue(pos, head, member, type) \
  335. for (pos = list_entry(pos->member.next, type, member); &pos->member != (head); \
  336. pos = list_entry(pos->member.next, type, member))
  337. /**
  338. * list_for_each_entry_continue_reverse - iterate backwards from the given point
  339. * @pos: the type * to use as a loop cursor.
  340. * @head: the head for your list.
  341. * @member: the name of the list_struct within the struct.
  342. *
  343. * Start to iterate over list of given type backwards, continuing after
  344. * the current position.
  345. */
  346. #define list_for_each_entry_continue_reverse(pos, head, member, type) \
  347. for (pos = list_entry(pos->member.prev, type, member); &pos->member != (head); \
  348. pos = list_entry(pos->member.prev, type, member))
  349. /**
  350. * list_for_each_entry_from - iterate over list of given type from the current
  351. * point
  352. * @pos: the type * to use as a loop cursor.
  353. * @head: the head for your list.
  354. * @member: the name of the list_struct within the struct.
  355. *
  356. * Iterate over list of given type, continuing from current position.
  357. */
  358. #define list_for_each_entry_from(pos, head, member, type) \
  359. for (; &pos->member != (head); pos = list_entry(pos->member.next, type, member))
  360. /**
  361. * list_for_each_entry_safe - iterate over list of given type safe against
  362. * removal of list entry
  363. * @pos: the type * to use as a loop cursor.
  364. * @n: another type * to use as temporary storage
  365. * @head: the head for your list.
  366. * @member: the name of the list_struct within the struct.
  367. */
  368. #define list_for_each_entry_safe(pos, n, head, member, type) \
  369. for (pos = list_entry((head)->next, type, member), n = list_entry(pos->member.next, type, member); \
  370. &pos->member != (head); pos = n, n = list_entry(n->member.next, type, member))
  371. /**
  372. * list_for_each_entry_safe_continue - continue list iteration safe against
  373. * removal
  374. * @pos: the type * to use as a loop cursor.
  375. * @n: another type * to use as temporary storage
  376. * @head: the head for your list.
  377. * @member: the name of the list_struct within the struct.
  378. *
  379. * Iterate over list of given type, continuing after current point,
  380. * safe against removal of list entry.
  381. */
  382. #define list_for_each_entry_safe_continue(pos, n, head, member, type) \
  383. for (pos = list_entry(pos->member.next, type, member), n = list_entry(pos->member.next, type, member); \
  384. &pos->member != (head); pos = n, n = list_entry(n->member.next, type, member))
  385. /**
  386. * list_for_each_entry_safe_from - iterate over list from current point safe
  387. * against removal
  388. * @pos: the type * to use as a loop cursor.
  389. * @n: another type * to use as temporary storage
  390. * @head: the head for your list.
  391. * @member: the name of the list_struct within the struct.
  392. *
  393. * Iterate over list of given type from current point, safe against
  394. * removal of list entry.
  395. */
  396. #define list_for_each_entry_safe_from(pos, n, head, member, type) \
  397. for (n = list_entry(pos->member.next, type, member); &pos->member != (head); \
  398. pos = n, n = list_entry(n->member.next, type, member))
  399. /**
  400. * list_for_each_entry_safe_reverse - iterate backwards over list safe against
  401. * removal
  402. * @pos: the type * to use as a loop cursor.
  403. * @n: another type * to use as temporary storage
  404. * @head: the head for your list.
  405. * @member: the name of the list_struct within the struct.
  406. *
  407. * Iterate backwards over list of given type, safe against removal
  408. * of list entry.
  409. */
  410. #define list_for_each_entry_safe_reverse(pos, n, head, member, type) \
  411. for (pos = list_entry((head)->prev, type, member), n = list_entry(pos->member.prev, type, member); \
  412. &pos->member != (head); pos = n, n = list_entry(n->member.prev, type, member))
  413. /**
  414. * list_safe_reset_next - reset a stale list_for_each_entry_safe loop
  415. * @pos: the loop cursor used in the list_for_each_entry_safe loop
  416. * @n: temporary storage used in list_for_each_entry_safe
  417. * @member: the name of the list_struct within the struct.
  418. *
  419. * list_safe_reset_next is not safe to use in general if the list may be
  420. * modified concurrently (eg. the lock is dropped in the loop body). An
  421. * exception to this is if the cursor element (pos) is pinned in the list,
  422. * and list_safe_reset_next is called after re-taking the lock and before
  423. * completing the current iteration of the loop body.
  424. */
  425. #define list_safe_reset_next(pos, n, member, type) n = list_entry(pos->member.next, type, member)
  426. #endif