lite-list.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /*
  2. * Copyright (C) 2012-2019 UCloud. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License").
  5. * You may not use this file except in compliance with the License.
  6. * A copy of the License is located at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * or in the "license" file accompanying this file. This file is distributed
  11. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  12. * express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. */
  15. #ifndef _LINUX_LIST_H
  16. #define _LINUX_LIST_H
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
  21. !defined(inline) && !defined(__cplusplus)
  22. #define inline __inline
  23. #endif
  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) { &(name), &(name) }
  38. #define LIST_HEAD(name) \
  39. struct list_head name = LIST_HEAD_INIT(name)
  40. static inline void INIT_LIST_HEAD(struct list_head *list)
  41. {
  42. list->next = list;
  43. list->prev = list;
  44. }
  45. /*
  46. * Insert a new_ptr entry between two known consecutive entries.
  47. *
  48. * This is only for internal list manipulation where we know
  49. * the prev/next entries already!
  50. */
  51. static inline void __list_add(struct list_head *new_ptr,
  52. struct list_head *prev,
  53. 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,
  118. struct list_head *new_ptr)
  119. {
  120. new_ptr->next = old->next;
  121. new_ptr->next->prev = new_ptr;
  122. new_ptr->prev = old->prev;
  123. new_ptr->prev->next = new_ptr;
  124. }
  125. static inline void list_replace_init(struct list_head *old,
  126. struct list_head *new_ptr)
  127. {
  128. list_replace(old, new_ptr);
  129. INIT_LIST_HEAD(old);
  130. }
  131. /**
  132. * list_del_init - deletes entry from list and reinitialize it.
  133. * @entry: the element to delete from the list.
  134. */
  135. static inline void list_del_init(struct list_head *entry)
  136. {
  137. __list_del_entry(entry);
  138. INIT_LIST_HEAD(entry);
  139. }
  140. /**
  141. * list_move - delete from one list and add as another's head
  142. * @list: the entry to move
  143. * @head: the head that will precede our entry
  144. */
  145. static inline void list_move(struct list_head *list, struct list_head *head)
  146. {
  147. __list_del_entry(list);
  148. list_add(list, head);
  149. }
  150. /**
  151. * list_move_tail - delete from one list and add as another's tail
  152. * @list: the entry to move
  153. * @head: the head that will follow our entry
  154. */
  155. static inline void list_move_tail(struct list_head *list,
  156. struct list_head *head)
  157. {
  158. __list_del_entry(list);
  159. list_add_tail(list, head);
  160. }
  161. /**
  162. * list_is_last - tests whether @list is the last entry in list @head
  163. * @list: the entry to test
  164. * @head: the head of the list
  165. */
  166. static inline int list_is_last(const struct list_head *list,
  167. const struct list_head *head)
  168. {
  169. return list->next == head;
  170. }
  171. /**
  172. * list_empty - tests whether a list is empty
  173. * @head: the list to test.
  174. */
  175. static inline int list_empty(const struct list_head *head)
  176. {
  177. return head->next == head;
  178. }
  179. /**
  180. * list_empty_careful - tests whether a list is empty and not being modified
  181. * @head: the list to test
  182. *
  183. * Description:
  184. * tests whether a list is empty _and_ checks that no other CPU might be
  185. * in the process of modifying either member (next or prev)
  186. *
  187. * NOTE: using list_empty_careful() without synchronization
  188. * can only be safe if the only activity that can happen
  189. * to the list entry is list_del_init(). Eg. it cannot be used
  190. * if another CPU could re-list_add() it.
  191. */
  192. static inline int list_empty_careful(const struct list_head *head)
  193. {
  194. struct list_head *next = head->next;
  195. return (next == head) && (next == head->prev);
  196. }
  197. /**
  198. * list_rotate_left - rotate the list to the left
  199. * @head: the head of the list
  200. */
  201. static inline void list_rotate_left(struct list_head *head)
  202. {
  203. struct list_head *first;
  204. if (!list_empty(head)) {
  205. first = head->next;
  206. list_move_tail(first, head);
  207. }
  208. }
  209. /**
  210. * list_is_singular - tests whether a list has just one entry.
  211. * @head: the list to test.
  212. */
  213. static inline int list_is_singular(const struct list_head *head)
  214. {
  215. return !list_empty(head) && (head->next == head->prev);
  216. }
  217. /**
  218. * list_entry - get the struct for this entry
  219. * @ptr: the &struct list_head pointer.
  220. * @type: the type of the struct this is embedded in.
  221. * @member: the name of the list_struct within the struct.
  222. */
  223. #define list_entry(ptr, type, member) \
  224. container_of(ptr, type, member)
  225. /**
  226. * list_first_entry - get the first element from a list
  227. * @ptr: the list head to take the element from.
  228. * @type: the type of the struct this is embedded in.
  229. * @member: the name of the list_struct within the struct.
  230. *
  231. * Note, that list is expected to be not empty.
  232. */
  233. #define list_first_entry(ptr, type, member) \
  234. list_entry((ptr)->next, type, member)
  235. /**
  236. * list_last_entry - get the last element from a list
  237. * @ptr: the list head to take the element from.
  238. * @type: the type of the struct this is embedded in.
  239. * @member: the name of the list_struct within the struct.
  240. *
  241. * Note, that list is expected to be not empty.
  242. */
  243. #define list_last_entry(ptr, type, member) \
  244. list_entry((ptr)->prev, type, member)
  245. /**
  246. * list_first_entry_or_null - get the first element from a list
  247. * @ptr: the list head to take the element from.
  248. * @type: the type of the struct this is embedded in.
  249. * @member: the name of the list_struct within the struct.
  250. *
  251. * Note that if the list is empty, it returns NULL.
  252. */
  253. #define list_first_entry_or_null(ptr, type, member) \
  254. (!list_empty(ptr) ? list_first_entry(ptr, type, member) : NULL)
  255. /**
  256. * list_next_entry - get the next element in list
  257. * @pos: the type * to cursor
  258. * @member: the name of the list_struct within the struct.
  259. */
  260. #define list_next_entry(pos, member, type) \
  261. list_entry((pos)->member.next, type, member)
  262. /**
  263. * list_prev_entry - get the prev element in list
  264. * @pos: the type * to cursor
  265. * @member: the name of the list_struct within the struct.
  266. */
  267. #define list_prev_entry(pos, member, type) \
  268. list_entry((pos)->member.prev, type, member)
  269. /**
  270. * list_for_each - iterate over a list
  271. * @pos: the &struct list_head to use as a loop cursor.
  272. * @head: the head for your list.
  273. */
  274. #define list_for_each(pos, head) \
  275. for (pos = (head)->next; pos != (head); pos = pos->next)
  276. /**
  277. * __list_for_each - iterate over a list
  278. * @pos: the &struct list_head to use as a loop cursor.
  279. * @head: the head for your list.
  280. *
  281. * This variant doesn't differ from list_for_each() any more.
  282. * We don't do prefetching in either case.
  283. */
  284. #define __list_for_each(pos, head) \
  285. for (pos = (head)->next; pos != (head); pos = pos->next)
  286. /**
  287. * list_for_each_prev - iterate over a list backwards
  288. * @pos: the &struct list_head to use as a loop cursor.
  289. * @head: the head for your list.
  290. */
  291. #define list_for_each_prev(pos, head) \
  292. for (pos = (head)->prev; pos != (head); pos = pos->prev)
  293. /**
  294. * list_for_each_safe - iterate over a list safe against removal of list entry
  295. * @pos: the &struct list_head to use as a loop cursor.
  296. * @n: another &struct list_head to use as temporary storage
  297. * @head: the head for your list.
  298. */
  299. #define list_for_each_safe(pos, n, head) \
  300. for (pos = (head)->next, n = pos->next; pos != (head); \
  301. pos = n, n = pos->next)
  302. /**
  303. * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
  304. * @pos: the &struct list_head to use as a loop cursor.
  305. * @n: another &struct list_head to use as temporary storage
  306. * @head: the head for your list.
  307. */
  308. #define list_for_each_prev_safe(pos, n, head) \
  309. for (pos = (head)->prev, n = pos->prev; \
  310. pos != (head); \
  311. pos = n, n = pos->prev)
  312. /**
  313. * list_for_each_entry - iterate over list of given type
  314. * @pos: the type * to use as a loop cursor.
  315. * @head: the head for your list.
  316. * @member: the name of the list_struct within the struct.
  317. */
  318. #define list_for_each_entry(pos, head, member, type) \
  319. for (pos = list_entry((head)->next, type, member); \
  320. &pos->member != (head); \
  321. pos = list_entry(pos->member.next, type, member))
  322. /**
  323. * list_for_each_entry_reverse - iterate backwards over list of given type.
  324. * @pos: the type * to use as a loop cursor.
  325. * @head: the head for your list.
  326. * @member: the name of the list_struct within the struct.
  327. */
  328. #define list_for_each_entry_reverse(pos, head, member, type) \
  329. for (pos = list_entry((head)->prev, type, member); \
  330. &pos->member != (head); \
  331. pos = list_entry(pos->member.prev, type, member))
  332. /**
  333. * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
  334. * @pos: the type * to use as a start point
  335. * @head: the head of the list
  336. * @member: the name of the list_struct within the struct.
  337. *
  338. * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
  339. */
  340. #define list_prepare_entry(pos, head, member, type) \
  341. ((pos) ? : list_entry(head, type, member))
  342. /**
  343. * list_for_each_entry_continue - continue iteration over list of given type
  344. * @pos: the type * to use as a loop cursor.
  345. * @head: the head for your list.
  346. * @member: the name of the list_struct within the struct.
  347. *
  348. * Continue to iterate over list of given type, continuing after
  349. * the current position.
  350. */
  351. #define list_for_each_entry_continue(pos, head, member, type) \
  352. for (pos = list_entry(pos->member.next, type, member); \
  353. &pos->member != (head); \
  354. pos = list_entry(pos->member.next, type, member))
  355. /**
  356. * list_for_each_entry_continue_reverse - iterate backwards from the given point
  357. * @pos: the type * to use as a loop cursor.
  358. * @head: the head for your list.
  359. * @member: the name of the list_struct within the struct.
  360. *
  361. * Start to iterate over list of given type backwards, continuing after
  362. * the current position.
  363. */
  364. #define list_for_each_entry_continue_reverse(pos, head, member, type) \
  365. for (pos = list_entry(pos->member.prev, type, member); \
  366. &pos->member != (head); \
  367. pos = list_entry(pos->member.prev, type, member))
  368. /**
  369. * list_for_each_entry_from - iterate over list of given type from the current point
  370. * @pos: the type * to use as a loop cursor.
  371. * @head: the head for your list.
  372. * @member: the name of the list_struct within the struct.
  373. *
  374. * Iterate over list of given type, continuing from current position.
  375. */
  376. #define list_for_each_entry_from(pos, head, member, type) \
  377. for (; &pos->member != (head); \
  378. pos = list_entry(pos->member.next, type, member))
  379. /**
  380. * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
  381. * @pos: the type * to use as a loop cursor.
  382. * @n: another type * to use as temporary storage
  383. * @head: the head for your list.
  384. * @member: the name of the list_struct within the struct.
  385. */
  386. #define list_for_each_entry_safe(pos, n, head, member, type) \
  387. for (pos = list_entry((head)->next, type, member), \
  388. n = list_entry(pos->member.next, type, member); \
  389. &pos->member != (head); \
  390. pos = n, n = list_entry(n->member.next, type, member))
  391. /**
  392. * list_for_each_entry_safe_continue - continue list iteration safe against removal
  393. * @pos: the type * to use as a loop cursor.
  394. * @n: another type * to use as temporary storage
  395. * @head: the head for your list.
  396. * @member: the name of the list_struct within the struct.
  397. *
  398. * Iterate over list of given type, continuing after current point,
  399. * safe against removal of list entry.
  400. */
  401. #define list_for_each_entry_safe_continue(pos, n, head, member, type) \
  402. for (pos = list_entry(pos->member.next, type, member), \
  403. n = list_entry(pos->member.next, type, member); \
  404. &pos->member != (head); \
  405. pos = n, n = list_entry(n->member.next, type, member))
  406. /**
  407. * list_for_each_entry_safe_from - iterate over list from current point safe against removal
  408. * @pos: the type * to use as a loop cursor.
  409. * @n: another type * to use as temporary storage
  410. * @head: the head for your list.
  411. * @member: the name of the list_struct within the struct.
  412. *
  413. * Iterate over list of given type from current point, safe against
  414. * removal of list entry.
  415. */
  416. #define list_for_each_entry_safe_from(pos, n, head, member, type) \
  417. for (n = list_entry(pos->member.next, type, member); \
  418. &pos->member != (head); \
  419. pos = n, n = list_entry(n->member.next, type, member))
  420. /**
  421. * list_for_each_entry_safe_reverse - iterate backwards over list safe against removal
  422. * @pos: the type * to use as a loop cursor.
  423. * @n: another type * to use as temporary storage
  424. * @head: the head for your list.
  425. * @member: the name of the list_struct within the struct.
  426. *
  427. * Iterate backwards over list of given type, safe against removal
  428. * of list entry.
  429. */
  430. #define list_for_each_entry_safe_reverse(pos, n, head, member, type) \
  431. for (pos = list_entry((head)->prev, type, member), \
  432. n = list_entry(pos->member.prev, type, member); \
  433. &pos->member != (head); \
  434. pos = n, n = list_entry(n->member.prev, type, member))
  435. /**
  436. * list_safe_reset_next - reset a stale list_for_each_entry_safe loop
  437. * @pos: the loop cursor used in the list_for_each_entry_safe loop
  438. * @n: temporary storage used in list_for_each_entry_safe
  439. * @member: the name of the list_struct within the struct.
  440. *
  441. * list_safe_reset_next is not safe to use in general if the list may be
  442. * modified concurrently (eg. the lock is dropped in the loop body). An
  443. * exception to this is if the cursor element (pos) is pinned in the list,
  444. * and list_safe_reset_next is called after re-taking the lock and before
  445. * completing the current iteration of the loop body.
  446. */
  447. #define list_safe_reset_next(pos, n, member, type) \
  448. n = list_entry(pos->member.next, type, member)
  449. #ifdef __cplusplus
  450. }
  451. #endif
  452. #endif