list.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. /* Licensed under BSD-MIT - see LICENSE file for details */
  2. #ifndef CCAN_LIST_H
  3. #define CCAN_LIST_H
  4. //#define CCAN_LIST_DEBUG 1
  5. #include <stdbool.h>
  6. #include <assert.h>
  7. #include <ccan/str/str.h>
  8. #include <ccan/container_of/container_of.h>
  9. #include <ccan/check_type/check_type.h>
  10. /**
  11. * struct list_node - an entry in a doubly-linked list
  12. * @next: next entry (self if empty)
  13. * @prev: previous entry (self if empty)
  14. *
  15. * This is used as an entry in a linked list.
  16. * Example:
  17. * struct child {
  18. * const char *name;
  19. * // Linked list of all us children.
  20. * struct list_node list;
  21. * };
  22. */
  23. struct list_node
  24. {
  25. struct list_node *next, *prev;
  26. };
  27. /**
  28. * struct list_head - the head of a doubly-linked list
  29. * @h: the list_head (containing next and prev pointers)
  30. *
  31. * This is used as the head of a linked list.
  32. * Example:
  33. * struct parent {
  34. * const char *name;
  35. * struct list_head children;
  36. * unsigned int num_children;
  37. * };
  38. */
  39. struct list_head
  40. {
  41. struct list_node n;
  42. };
  43. /**
  44. * list_check - check head of a list for consistency
  45. * @h: the list_head
  46. * @abortstr: the location to print on aborting, or NULL.
  47. *
  48. * Because list_nodes have redundant information, consistency checking between
  49. * the back and forward links can be done. This is useful as a debugging check.
  50. * If @abortstr is non-NULL, that will be printed in a diagnostic if the list
  51. * is inconsistent, and the function will abort.
  52. *
  53. * Returns the list head if the list is consistent, NULL if not (it
  54. * can never return NULL if @abortstr is set).
  55. *
  56. * See also: list_check_node()
  57. *
  58. * Example:
  59. * static void dump_parent(struct parent *p)
  60. * {
  61. * struct child *c;
  62. *
  63. * printf("%s (%u children):\n", p->name, p->num_children);
  64. * list_check(&p->children, "bad child list");
  65. * list_for_each(&p->children, c, list)
  66. * printf(" -> %s\n", c->name);
  67. * }
  68. */
  69. struct list_head *list_check(const struct list_head *h, const char *abortstr);
  70. /**
  71. * list_check_node - check node of a list for consistency
  72. * @n: the list_node
  73. * @abortstr: the location to print on aborting, or NULL.
  74. *
  75. * Check consistency of the list node is in (it must be in one).
  76. *
  77. * See also: list_check()
  78. *
  79. * Example:
  80. * static void dump_child(const struct child *c)
  81. * {
  82. * list_check_node(&c->list, "bad child list");
  83. * printf("%s\n", c->name);
  84. * }
  85. */
  86. struct list_node *list_check_node(const struct list_node *n,
  87. const char *abortstr);
  88. #define LIST_LOC __FILE__ ":" stringify(__LINE__)
  89. #ifdef CCAN_LIST_DEBUG
  90. #define list_debug(h, loc) list_check((h), loc)
  91. #define list_debug_node(n, loc) list_check_node((n), loc)
  92. #else
  93. #define list_debug(h, loc) ((void)loc, h)
  94. #define list_debug_node(n, loc) ((void)loc, n)
  95. #endif
  96. /**
  97. * LIST_HEAD_INIT - initializer for an empty list_head
  98. * @name: the name of the list.
  99. *
  100. * Explicit initializer for an empty list.
  101. *
  102. * See also:
  103. * LIST_HEAD, list_head_init()
  104. *
  105. * Example:
  106. * static struct list_head my_list = LIST_HEAD_INIT(my_list);
  107. */
  108. #define LIST_HEAD_INIT(name) { { &(name).n, &(name).n } }
  109. /**
  110. * LIST_HEAD - define and initialize an empty list_head
  111. * @name: the name of the list.
  112. *
  113. * The LIST_HEAD macro defines a list_head and initializes it to an empty
  114. * list. It can be prepended by "static" to define a static list_head.
  115. *
  116. * See also:
  117. * LIST_HEAD_INIT, list_head_init()
  118. *
  119. * Example:
  120. * static LIST_HEAD(my_global_list);
  121. */
  122. #define LIST_HEAD(name) \
  123. struct list_head name = LIST_HEAD_INIT(name)
  124. /**
  125. * list_head_init - initialize a list_head
  126. * @h: the list_head to set to the empty list
  127. *
  128. * Example:
  129. * ...
  130. * struct parent *parent = malloc(sizeof(*parent));
  131. *
  132. * list_head_init(&parent->children);
  133. * parent->num_children = 0;
  134. */
  135. static inline void list_head_init(struct list_head *h)
  136. {
  137. h->n.next = h->n.prev = &h->n;
  138. }
  139. /**
  140. * list_node_init - initialize a list_node
  141. * @n: the list_node to link to itself.
  142. *
  143. * You don't need to use this normally! But it lets you list_del(@n)
  144. * safely.
  145. */
  146. static inline void list_node_init(struct list_node *n)
  147. {
  148. n->next = n->prev = n;
  149. }
  150. /**
  151. * list_add_after - add an entry after an existing node in a linked list
  152. * @h: the list_head to add the node to (for debugging)
  153. * @p: the existing list_node to add the node after
  154. * @n: the new list_node to add to the list.
  155. *
  156. * The existing list_node must already be a member of the list.
  157. * The new list_node does not need to be initialized; it will be overwritten.
  158. *
  159. * Example:
  160. * struct child c1, c2, c3;
  161. * LIST_HEAD(h);
  162. *
  163. * list_add_tail(&h, &c1.list);
  164. * list_add_tail(&h, &c3.list);
  165. * list_add_after(&h, &c1.list, &c2.list);
  166. */
  167. #define list_add_after(h, p, n) list_add_after_(h, p, n, LIST_LOC)
  168. static inline void list_add_after_(struct list_head *h,
  169. struct list_node *p,
  170. struct list_node *n,
  171. const char *abortstr)
  172. {
  173. n->next = p->next;
  174. n->prev = p;
  175. p->next->prev = n;
  176. p->next = n;
  177. (void)list_debug(h, abortstr);
  178. }
  179. /**
  180. * list_add - add an entry at the start of a linked list.
  181. * @h: the list_head to add the node to
  182. * @n: the list_node to add to the list.
  183. *
  184. * The list_node does not need to be initialized; it will be overwritten.
  185. * Example:
  186. * struct child *child = malloc(sizeof(*child));
  187. *
  188. * child->name = "marvin";
  189. * list_add(&parent->children, &child->list);
  190. * parent->num_children++;
  191. */
  192. #define list_add(h, n) list_add_(h, n, LIST_LOC)
  193. static inline void list_add_(struct list_head *h,
  194. struct list_node *n,
  195. const char *abortstr)
  196. {
  197. list_add_after_(h, &h->n, n, abortstr);
  198. }
  199. /**
  200. * list_add_before - add an entry before an existing node in a linked list
  201. * @h: the list_head to add the node to (for debugging)
  202. * @p: the existing list_node to add the node before
  203. * @n: the new list_node to add to the list.
  204. *
  205. * The existing list_node must already be a member of the list.
  206. * The new list_node does not need to be initialized; it will be overwritten.
  207. *
  208. * Example:
  209. * list_head_init(&h);
  210. * list_add_tail(&h, &c1.list);
  211. * list_add_tail(&h, &c3.list);
  212. * list_add_before(&h, &c3.list, &c2.list);
  213. */
  214. #define list_add_before(h, p, n) list_add_before_(h, p, n, LIST_LOC)
  215. static inline void list_add_before_(struct list_head *h,
  216. struct list_node *p,
  217. struct list_node *n,
  218. const char *abortstr)
  219. {
  220. n->next = p;
  221. n->prev = p->prev;
  222. p->prev->next = n;
  223. p->prev = n;
  224. (void)list_debug(h, abortstr);
  225. }
  226. /**
  227. * list_add_tail - add an entry at the end of a linked list.
  228. * @h: the list_head to add the node to
  229. * @n: the list_node to add to the list.
  230. *
  231. * The list_node does not need to be initialized; it will be overwritten.
  232. * Example:
  233. * list_add_tail(&parent->children, &child->list);
  234. * parent->num_children++;
  235. */
  236. #define list_add_tail(h, n) list_add_tail_(h, n, LIST_LOC)
  237. static inline void list_add_tail_(struct list_head *h,
  238. struct list_node *n,
  239. const char *abortstr)
  240. {
  241. list_add_before_(h, &h->n, n, abortstr);
  242. }
  243. /**
  244. * list_empty - is a list empty?
  245. * @h: the list_head
  246. *
  247. * If the list is empty, returns true.
  248. *
  249. * Example:
  250. * assert(list_empty(&parent->children) == (parent->num_children == 0));
  251. */
  252. #define list_empty(h) list_empty_(h, LIST_LOC)
  253. static inline bool list_empty_(const struct list_head *h, const char* abortstr)
  254. {
  255. (void)list_debug(h, abortstr);
  256. return h->n.next == &h->n;
  257. }
  258. /**
  259. * list_empty_nodebug - is a list empty (and don't perform debug checks)?
  260. * @h: the list_head
  261. *
  262. * If the list is empty, returns true.
  263. * This differs from list_empty() in that if CCAN_LIST_DEBUG is set it
  264. * will NOT perform debug checks. Only use this function if you REALLY
  265. * know what you're doing.
  266. *
  267. * Example:
  268. * assert(list_empty_nodebug(&parent->children) == (parent->num_children == 0));
  269. */
  270. #ifndef CCAN_LIST_DEBUG
  271. #define list_empty_nodebug(h) list_empty(h)
  272. #else
  273. static inline bool list_empty_nodebug(const struct list_head *h)
  274. {
  275. return h->n.next == &h->n;
  276. }
  277. #endif
  278. /**
  279. * list_empty_nocheck - is a list empty?
  280. * @h: the list_head
  281. *
  282. * If the list is empty, returns true. This doesn't perform any
  283. * debug check for list consistency, so it can be called without
  284. * locks, racing with the list being modified. This is ok for
  285. * checks where an incorrect result is not an issue (optimized
  286. * bail out path for example).
  287. */
  288. static inline bool list_empty_nocheck(const struct list_head *h)
  289. {
  290. return h->n.next == &h->n;
  291. }
  292. /**
  293. * list_del - delete an entry from an (unknown) linked list.
  294. * @n: the list_node to delete from the list.
  295. *
  296. * Note that this leaves @n in an undefined state; it can be added to
  297. * another list, but not deleted again.
  298. *
  299. * See also:
  300. * list_del_from(), list_del_init()
  301. *
  302. * Example:
  303. * list_del(&child->list);
  304. * parent->num_children--;
  305. */
  306. #define list_del(n) list_del_(n, LIST_LOC)
  307. static inline void list_del_(struct list_node *n, const char* abortstr)
  308. {
  309. (void)list_debug_node(n, abortstr);
  310. n->next->prev = n->prev;
  311. n->prev->next = n->next;
  312. #ifdef CCAN_LIST_DEBUG
  313. /* Catch use-after-del. */
  314. n->next = n->prev = NULL;
  315. #endif
  316. }
  317. /**
  318. * list_del_init - delete a node, and reset it so it can be deleted again.
  319. * @n: the list_node to be deleted.
  320. *
  321. * list_del(@n) or list_del_init() again after this will be safe,
  322. * which can be useful in some cases.
  323. *
  324. * See also:
  325. * list_del_from(), list_del()
  326. *
  327. * Example:
  328. * list_del_init(&child->list);
  329. * parent->num_children--;
  330. */
  331. #define list_del_init(n) list_del_init_(n, LIST_LOC)
  332. static inline void list_del_init_(struct list_node *n, const char *abortstr)
  333. {
  334. list_del_(n, abortstr);
  335. list_node_init(n);
  336. }
  337. /**
  338. * list_del_from - delete an entry from a known linked list.
  339. * @h: the list_head the node is in.
  340. * @n: the list_node to delete from the list.
  341. *
  342. * This explicitly indicates which list a node is expected to be in,
  343. * which is better documentation and can catch more bugs.
  344. *
  345. * See also: list_del()
  346. *
  347. * Example:
  348. * list_del_from(&parent->children, &child->list);
  349. * parent->num_children--;
  350. */
  351. static inline void list_del_from(struct list_head *h, struct list_node *n)
  352. {
  353. #ifdef CCAN_LIST_DEBUG
  354. {
  355. /* Thorough check: make sure it was in list! */
  356. struct list_node *i;
  357. for (i = h->n.next; i != n; i = i->next)
  358. assert(i != &h->n);
  359. }
  360. #endif /* CCAN_LIST_DEBUG */
  361. /* Quick test that catches a surprising number of bugs. */
  362. assert(!list_empty(h));
  363. list_del(n);
  364. }
  365. /**
  366. * list_swap - swap out an entry from an (unknown) linked list for a new one.
  367. * @o: the list_node to replace from the list.
  368. * @n: the list_node to insert in place of the old one.
  369. *
  370. * Note that this leaves @o in an undefined state; it can be added to
  371. * another list, but not deleted/swapped again.
  372. *
  373. * See also:
  374. * list_del()
  375. *
  376. * Example:
  377. * struct child x1, x2;
  378. * LIST_HEAD(xh);
  379. *
  380. * list_add(&xh, &x1.list);
  381. * list_swap(&x1.list, &x2.list);
  382. */
  383. #define list_swap(o, n) list_swap_(o, n, LIST_LOC)
  384. static inline void list_swap_(struct list_node *o,
  385. struct list_node *n,
  386. const char* abortstr)
  387. {
  388. (void)list_debug_node(o, abortstr);
  389. *n = *o;
  390. n->next->prev = n;
  391. n->prev->next = n;
  392. #ifdef CCAN_LIST_DEBUG
  393. /* Catch use-after-del. */
  394. o->next = o->prev = NULL;
  395. #endif
  396. }
  397. /**
  398. * list_entry - convert a list_node back into the structure containing it.
  399. * @n: the list_node
  400. * @type: the type of the entry
  401. * @member: the list_node member of the type
  402. *
  403. * Example:
  404. * // First list entry is children.next; convert back to child.
  405. * child = list_entry(parent->children.n.next, struct child, list);
  406. *
  407. * See Also:
  408. * list_top(), list_for_each()
  409. */
  410. #define list_entry(n, type, member) container_of(n, type, member)
  411. /**
  412. * list_top - get the first entry in a list
  413. * @h: the list_head
  414. * @type: the type of the entry
  415. * @member: the list_node member of the type
  416. *
  417. * If the list is empty, returns NULL.
  418. *
  419. * Example:
  420. * struct child *first;
  421. * first = list_top(&parent->children, struct child, list);
  422. * if (!first)
  423. * printf("Empty list!\n");
  424. */
  425. #define list_top(h, type, member) \
  426. ((type *)list_top_((h), list_off_(type, member)))
  427. static inline const void *list_top_(const struct list_head *h, size_t off)
  428. {
  429. if (list_empty(h))
  430. return NULL;
  431. return (const char *)h->n.next - off;
  432. }
  433. /**
  434. * list_pop - remove the first entry in a list
  435. * @h: the list_head
  436. * @type: the type of the entry
  437. * @member: the list_node member of the type
  438. *
  439. * If the list is empty, returns NULL.
  440. *
  441. * Example:
  442. * struct child *one;
  443. * one = list_pop(&parent->children, struct child, list);
  444. * if (!one)
  445. * printf("Empty list!\n");
  446. */
  447. #define list_pop(h, type, member) \
  448. ((type *)list_pop_((h), list_off_(type, member)))
  449. static inline const void *list_pop_(const struct list_head *h, size_t off)
  450. {
  451. struct list_node *n;
  452. if (list_empty(h))
  453. return NULL;
  454. n = h->n.next;
  455. list_del(n);
  456. return (const char *)n - off;
  457. }
  458. /**
  459. * list_tail - get the last entry in a list
  460. * @h: the list_head
  461. * @type: the type of the entry
  462. * @member: the list_node member of the type
  463. *
  464. * If the list is empty, returns NULL.
  465. *
  466. * Example:
  467. * struct child *last;
  468. * last = list_tail(&parent->children, struct child, list);
  469. * if (!last)
  470. * printf("Empty list!\n");
  471. */
  472. #define list_tail(h, type, member) \
  473. ((type *)list_tail_((h), list_off_(type, member)))
  474. static inline const void *list_tail_(const struct list_head *h, size_t off)
  475. {
  476. if (list_empty(h))
  477. return NULL;
  478. return (const char *)h->n.prev - off;
  479. }
  480. /**
  481. * list_for_each - iterate through a list.
  482. * @h: the list_head (warning: evaluated multiple times!)
  483. * @i: the structure containing the list_node
  484. * @member: the list_node member of the structure
  485. *
  486. * This is a convenient wrapper to iterate @i over the entire list. It's
  487. * a for loop, so you can break and continue as normal.
  488. *
  489. * Example:
  490. * list_for_each(&parent->children, child, list)
  491. * printf("Name: %s\n", child->name);
  492. */
  493. #define list_for_each(h, i, member) \
  494. list_for_each_off(h, i, list_off_var_(i, member))
  495. /**
  496. * list_for_each_rev - iterate through a list backwards.
  497. * @h: the list_head
  498. * @i: the structure containing the list_node
  499. * @member: the list_node member of the structure
  500. *
  501. * This is a convenient wrapper to iterate @i over the entire list. It's
  502. * a for loop, so you can break and continue as normal.
  503. *
  504. * Example:
  505. * list_for_each_rev(&parent->children, child, list)
  506. * printf("Name: %s\n", child->name);
  507. */
  508. #define list_for_each_rev(h, i, member) \
  509. list_for_each_rev_off(h, i, list_off_var_(i, member))
  510. /**
  511. * list_for_each_rev_safe - iterate through a list backwards,
  512. * maybe during deletion
  513. * @h: the list_head
  514. * @i: the structure containing the list_node
  515. * @nxt: the structure containing the list_node
  516. * @member: the list_node member of the structure
  517. *
  518. * This is a convenient wrapper to iterate @i over the entire list backwards.
  519. * It's a for loop, so you can break and continue as normal. The extra
  520. * variable * @nxt is used to hold the next element, so you can delete @i
  521. * from the list.
  522. *
  523. * Example:
  524. * struct child *next;
  525. * list_for_each_rev_safe(&parent->children, child, next, list) {
  526. * printf("Name: %s\n", child->name);
  527. * }
  528. */
  529. #define list_for_each_rev_safe(h, i, nxt, member) \
  530. list_for_each_rev_safe_off(h, i, nxt, list_off_var_(i, member))
  531. /**
  532. * list_for_each_safe - iterate through a list, maybe during deletion
  533. * @h: the list_head
  534. * @i: the structure containing the list_node
  535. * @nxt: the structure containing the list_node
  536. * @member: the list_node member of the structure
  537. *
  538. * This is a convenient wrapper to iterate @i over the entire list. It's
  539. * a for loop, so you can break and continue as normal. The extra variable
  540. * @nxt is used to hold the next element, so you can delete @i from the list.
  541. *
  542. * Example:
  543. * list_for_each_safe(&parent->children, child, next, list) {
  544. * list_del(&child->list);
  545. * parent->num_children--;
  546. * }
  547. */
  548. #define list_for_each_safe(h, i, nxt, member) \
  549. list_for_each_safe_off(h, i, nxt, list_off_var_(i, member))
  550. /**
  551. * list_next - get the next entry in a list
  552. * @h: the list_head
  553. * @i: a pointer to an entry in the list.
  554. * @member: the list_node member of the structure
  555. *
  556. * If @i was the last entry in the list, returns NULL.
  557. *
  558. * Example:
  559. * struct child *second;
  560. * second = list_next(&parent->children, first, list);
  561. * if (!second)
  562. * printf("No second child!\n");
  563. */
  564. #define list_next(h, i, member) \
  565. ((list_typeof(i))list_entry_or_null(list_debug(h, \
  566. __FILE__ ":" stringify(__LINE__)), \
  567. (i)->member.next, \
  568. list_off_var_((i), member)))
  569. /**
  570. * list_prev - get the previous entry in a list
  571. * @h: the list_head
  572. * @i: a pointer to an entry in the list.
  573. * @member: the list_node member of the structure
  574. *
  575. * If @i was the first entry in the list, returns NULL.
  576. *
  577. * Example:
  578. * first = list_prev(&parent->children, second, list);
  579. * if (!first)
  580. * printf("Can't go back to first child?!\n");
  581. */
  582. #define list_prev(h, i, member) \
  583. ((list_typeof(i))list_entry_or_null(list_debug(h, \
  584. __FILE__ ":" stringify(__LINE__)), \
  585. (i)->member.prev, \
  586. list_off_var_((i), member)))
  587. /**
  588. * list_append_list - empty one list onto the end of another.
  589. * @to: the list to append into
  590. * @from: the list to empty.
  591. *
  592. * This takes the entire contents of @from and moves it to the end of
  593. * @to. After this @from will be empty.
  594. *
  595. * Example:
  596. * struct list_head adopter;
  597. *
  598. * list_append_list(&adopter, &parent->children);
  599. * assert(list_empty(&parent->children));
  600. * parent->num_children = 0;
  601. */
  602. #define list_append_list(t, f) list_append_list_(t, f, \
  603. __FILE__ ":" stringify(__LINE__))
  604. static inline void list_append_list_(struct list_head *to,
  605. struct list_head *from,
  606. const char *abortstr)
  607. {
  608. struct list_node *from_tail = list_debug(from, abortstr)->n.prev;
  609. struct list_node *to_tail = list_debug(to, abortstr)->n.prev;
  610. /* Sew in head and entire list. */
  611. to->n.prev = from_tail;
  612. from_tail->next = &to->n;
  613. to_tail->next = &from->n;
  614. from->n.prev = to_tail;
  615. /* Now remove head. */
  616. list_del(&from->n);
  617. list_head_init(from);
  618. }
  619. /**
  620. * list_prepend_list - empty one list into the start of another.
  621. * @to: the list to prepend into
  622. * @from: the list to empty.
  623. *
  624. * This takes the entire contents of @from and moves it to the start
  625. * of @to. After this @from will be empty.
  626. *
  627. * Example:
  628. * list_prepend_list(&adopter, &parent->children);
  629. * assert(list_empty(&parent->children));
  630. * parent->num_children = 0;
  631. */
  632. #define list_prepend_list(t, f) list_prepend_list_(t, f, LIST_LOC)
  633. static inline void list_prepend_list_(struct list_head *to,
  634. struct list_head *from,
  635. const char *abortstr)
  636. {
  637. struct list_node *from_tail = list_debug(from, abortstr)->n.prev;
  638. struct list_node *to_head = list_debug(to, abortstr)->n.next;
  639. /* Sew in head and entire list. */
  640. to->n.next = &from->n;
  641. from->n.prev = &to->n;
  642. to_head->prev = from_tail;
  643. from_tail->next = to_head;
  644. /* Now remove head. */
  645. list_del(&from->n);
  646. list_head_init(from);
  647. }
  648. /* internal macros, do not use directly */
  649. #define list_for_each_off_dir_(h, i, off, dir) \
  650. for (i = list_node_to_off_(list_debug(h, LIST_LOC)->n.dir, \
  651. (off)); \
  652. list_node_from_off_((void *)i, (off)) != &(h)->n; \
  653. i = list_node_to_off_(list_node_from_off_((void *)i, (off))->dir, \
  654. (off)))
  655. #define list_for_each_safe_off_dir_(h, i, nxt, off, dir) \
  656. for (i = list_node_to_off_(list_debug(h, LIST_LOC)->n.dir, \
  657. (off)), \
  658. nxt = list_node_to_off_(list_node_from_off_(i, (off))->dir, \
  659. (off)); \
  660. list_node_from_off_(i, (off)) != &(h)->n; \
  661. i = nxt, \
  662. nxt = list_node_to_off_(list_node_from_off_(i, (off))->dir, \
  663. (off)))
  664. /**
  665. * list_for_each_off - iterate through a list of memory regions.
  666. * @h: the list_head
  667. * @i: the pointer to a memory region wich contains list node data.
  668. * @off: offset(relative to @i) at which list node data resides.
  669. *
  670. * This is a low-level wrapper to iterate @i over the entire list, used to
  671. * implement all oher, more high-level, for-each constructs. It's a for loop,
  672. * so you can break and continue as normal.
  673. *
  674. * WARNING! Being the low-level macro that it is, this wrapper doesn't know
  675. * nor care about the type of @i. The only assumption made is that @i points
  676. * to a chunk of memory that at some @offset, relative to @i, contains a
  677. * properly filled `struct list_node' which in turn contains pointers to
  678. * memory chunks and it's turtles all the way down. With all that in mind
  679. * remember that given the wrong pointer/offset couple this macro will
  680. * happily churn all you memory until SEGFAULT stops it, in other words
  681. * caveat emptor.
  682. *
  683. * It is worth mentioning that one of legitimate use-cases for that wrapper
  684. * is operation on opaque types with known offset for `struct list_node'
  685. * member(preferably 0), because it allows you not to disclose the type of
  686. * @i.
  687. *
  688. * Example:
  689. * list_for_each_off(&parent->children, child,
  690. * offsetof(struct child, list))
  691. * printf("Name: %s\n", child->name);
  692. */
  693. #define list_for_each_off(h, i, off) \
  694. list_for_each_off_dir_((h),(i),(off),next)
  695. /**
  696. * list_for_each_rev_off - iterate through a list of memory regions backwards
  697. * @h: the list_head
  698. * @i: the pointer to a memory region wich contains list node data.
  699. * @off: offset(relative to @i) at which list node data resides.
  700. *
  701. * See list_for_each_off for details
  702. */
  703. #define list_for_each_rev_off(h, i, off) \
  704. list_for_each_off_dir_((h),(i),(off),prev)
  705. /**
  706. * list_for_each_safe_off - iterate through a list of memory regions, maybe
  707. * during deletion
  708. * @h: the list_head
  709. * @i: the pointer to a memory region wich contains list node data.
  710. * @nxt: the structure containing the list_node
  711. * @off: offset(relative to @i) at which list node data resides.
  712. *
  713. * For details see `list_for_each_off' and `list_for_each_safe'
  714. * descriptions.
  715. *
  716. * Example:
  717. * list_for_each_safe_off(&parent->children, child,
  718. * next, offsetof(struct child, list))
  719. * printf("Name: %s\n", child->name);
  720. */
  721. #define list_for_each_safe_off(h, i, nxt, off) \
  722. list_for_each_safe_off_dir_((h),(i),(nxt),(off),next)
  723. /**
  724. * list_for_each_rev_safe_off - iterate backwards through a list of
  725. * memory regions, maybe during deletion
  726. * @h: the list_head
  727. * @i: the pointer to a memory region wich contains list node data.
  728. * @nxt: the structure containing the list_node
  729. * @off: offset(relative to @i) at which list node data resides.
  730. *
  731. * For details see `list_for_each_rev_off' and `list_for_each_rev_safe'
  732. * descriptions.
  733. *
  734. * Example:
  735. * list_for_each_rev_safe_off(&parent->children, child,
  736. * next, offsetof(struct child, list))
  737. * printf("Name: %s\n", child->name);
  738. */
  739. #define list_for_each_rev_safe_off(h, i, nxt, off) \
  740. list_for_each_safe_off_dir_((h),(i),(nxt),(off),prev)
  741. /* Other -off variants. */
  742. #define list_entry_off(n, type, off) \
  743. ((type *)list_node_from_off_((n), (off)))
  744. #define list_head_off(h, type, off) \
  745. ((type *)list_head_off((h), (off)))
  746. #define list_tail_off(h, type, off) \
  747. ((type *)list_tail_((h), (off)))
  748. #define list_add_off(h, n, off) \
  749. list_add((h), list_node_from_off_((n), (off)))
  750. #define list_del_off(n, off) \
  751. list_del(list_node_from_off_((n), (off)))
  752. #define list_del_from_off(h, n, off) \
  753. list_del_from(h, list_node_from_off_((n), (off)))
  754. /* Offset helper functions so we only single-evaluate. */
  755. static inline void *list_node_to_off_(struct list_node *node, size_t off)
  756. {
  757. return (void *)((char *)node - off);
  758. }
  759. static inline struct list_node *list_node_from_off_(void *ptr, size_t off)
  760. {
  761. return (struct list_node *)((char *)ptr + off);
  762. }
  763. /* Get the offset of the member, but make sure it's a list_node. */
  764. #define list_off_(type, member) \
  765. (container_off(type, member) + \
  766. check_type(((type *)0)->member, struct list_node))
  767. #define list_off_var_(var, member) \
  768. (container_off_var(var, member) + \
  769. check_type(var->member, struct list_node))
  770. #if HAVE_TYPEOF
  771. #define list_typeof(var) typeof(var)
  772. #else
  773. #define list_typeof(var) void *
  774. #endif
  775. /* Returns member, or NULL if at end of list. */
  776. static inline void *list_entry_or_null(const struct list_head *h,
  777. const struct list_node *n,
  778. size_t off)
  779. {
  780. if (n == &h->n)
  781. return NULL;
  782. return (char *)n - off;
  783. }
  784. #endif /* CCAN_LIST_H */