ilist.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. /*-------------------------------------------------------------------------
  2. *
  3. * ilist.h
  4. * integrated/inline doubly- and singly-linked lists
  5. *
  6. * These list types are useful when there are only a predetermined set of
  7. * lists that an object could be in. List links are embedded directly into
  8. * the objects, and thus no extra memory management overhead is required.
  9. * (Of course, if only a small proportion of existing objects are in a list,
  10. * the link fields in the remainder would be wasted space. But usually,
  11. * it saves space to not have separately-allocated list nodes.)
  12. *
  13. * None of the functions here allocate any memory; they just manipulate
  14. * externally managed memory. The APIs for singly and doubly linked lists
  15. * are identical as far as capabilities of both allow.
  16. *
  17. * Each list has a list header, which exists even when the list is empty.
  18. * An empty singly-linked list has a NULL pointer in its header.
  19. * There are two kinds of empty doubly linked lists: those that have been
  20. * initialized to NULL, and those that have been initialized to circularity.
  21. * (If a dlist is modified and then all its elements are deleted, it will be
  22. * in the circular state.) We prefer circular dlists because there are some
  23. * operations that can be done without branches (and thus faster) on lists
  24. * that use circular representation. However, it is often convenient to
  25. * initialize list headers to zeroes rather than setting them up with an
  26. * explicit initialization function, so we also allow the other case.
  27. *
  28. * EXAMPLES
  29. *
  30. * Here's a simple example demonstrating how this can be used. Let's assume
  31. * we want to store information about the tables contained in a database.
  32. *
  33. * #include "lib/ilist.h"
  34. *
  35. * // Define struct for the databases including a list header that will be
  36. * // used to access the nodes in the table list later on.
  37. * typedef struct my_database
  38. * {
  39. * char *datname;
  40. * dlist_head tables;
  41. * // ...
  42. * } my_database;
  43. *
  44. * // Define struct for the tables. Note the list_node element which stores
  45. * // prev/next list links. The list_node element need not be first.
  46. * typedef struct my_table
  47. * {
  48. * char *tablename;
  49. * dlist_node list_node;
  50. * perm_t permissions;
  51. * // ...
  52. * } my_table;
  53. *
  54. * // create a database
  55. * my_database *db = create_database();
  56. *
  57. * // and add a few tables to its table list
  58. * dlist_push_head(&db->tables, &create_table(db, "a")->list_node);
  59. * ...
  60. * dlist_push_head(&db->tables, &create_table(db, "b")->list_node);
  61. *
  62. *
  63. * To iterate over the table list, we allocate an iterator variable and use
  64. * a specialized looping construct. Inside a dlist_foreach, the iterator's
  65. * 'cur' field can be used to access the current element. iter.cur points to
  66. * a 'dlist_node', but most of the time what we want is the actual table
  67. * information; dlist_container() gives us that, like so:
  68. *
  69. * dlist_iter iter;
  70. * dlist_foreach(iter, &db->tables)
  71. * {
  72. * my_table *tbl = dlist_container(my_table, list_node, iter.cur);
  73. * printf("we have a table: %s in database %s\n",
  74. * tbl->tablename, db->datname);
  75. * }
  76. *
  77. *
  78. * While a simple iteration is useful, we sometimes also want to manipulate
  79. * the list while iterating. There is a different iterator element and looping
  80. * construct for that. Suppose we want to delete tables that meet a certain
  81. * criterion:
  82. *
  83. * dlist_mutable_iter miter;
  84. * dlist_foreach_modify(miter, &db->tables)
  85. * {
  86. * my_table *tbl = dlist_container(my_table, list_node, miter.cur);
  87. *
  88. * if (!tbl->to_be_deleted)
  89. * continue; // don't touch this one
  90. *
  91. * // unlink the current table from the linked list
  92. * dlist_delete(miter.cur);
  93. * // as these lists never manage memory, we can still access the table
  94. * // after it's been unlinked
  95. * drop_table(db, tbl);
  96. * }
  97. *
  98. *
  99. * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
  100. * Portions Copyright (c) 1994, Regents of the University of California
  101. *
  102. * IDENTIFICATION
  103. * src/include/lib/ilist.h
  104. *-------------------------------------------------------------------------
  105. */
  106. #ifndef ILIST_H
  107. #define ILIST_H
  108. /*
  109. * Enable for extra debugging. This is rather expensive, so it's not enabled by
  110. * default even when USE_ASSERT_CHECKING.
  111. */
  112. /* #define ILIST_DEBUG */
  113. /*
  114. * Node of a doubly linked list.
  115. *
  116. * Embed this in structs that need to be part of a doubly linked list.
  117. */
  118. typedef struct dlist_node dlist_node;
  119. struct dlist_node
  120. {
  121. dlist_node *prev;
  122. dlist_node *next;
  123. };
  124. /*
  125. * Head of a doubly linked list.
  126. *
  127. * Non-empty lists are internally circularly linked. Circular lists have the
  128. * advantage of not needing any branches in the most common list manipulations.
  129. * An empty list can also be represented as a pair of NULL pointers, making
  130. * initialization easier.
  131. */
  132. typedef struct dlist_head
  133. {
  134. /*
  135. * head.next either points to the first element of the list; to &head if
  136. * it's a circular empty list; or to NULL if empty and not circular.
  137. *
  138. * head.prev either points to the last element of the list; to &head if
  139. * it's a circular empty list; or to NULL if empty and not circular.
  140. */
  141. dlist_node head;
  142. } dlist_head;
  143. /*
  144. * Doubly linked list iterator.
  145. *
  146. * Used as state in dlist_foreach() and dlist_reverse_foreach(). To get the
  147. * current element of the iteration use the 'cur' member.
  148. *
  149. * Iterations using this are *not* allowed to change the list while iterating!
  150. *
  151. * NB: We use an extra "end" field here to avoid multiple evaluations of
  152. * arguments in the dlist_foreach() macro.
  153. */
  154. typedef struct dlist_iter
  155. {
  156. dlist_node *cur; /* current element */
  157. dlist_node *end; /* last node we'll iterate to */
  158. } dlist_iter;
  159. /*
  160. * Doubly linked list iterator allowing some modifications while iterating.
  161. *
  162. * Used as state in dlist_foreach_modify(). To get the current element of the
  163. * iteration use the 'cur' member.
  164. *
  165. * Iterations using this are only allowed to change the list at the current
  166. * point of iteration. It is fine to delete the current node, but it is *not*
  167. * fine to insert or delete adjacent nodes.
  168. *
  169. * NB: We need a separate type for mutable iterations so that we can store
  170. * the 'next' node of the current node in case it gets deleted or modified.
  171. */
  172. typedef struct dlist_mutable_iter
  173. {
  174. dlist_node *cur; /* current element */
  175. dlist_node *next; /* next node we'll iterate to */
  176. dlist_node *end; /* last node we'll iterate to */
  177. } dlist_mutable_iter;
  178. /*
  179. * Node of a singly linked list.
  180. *
  181. * Embed this in structs that need to be part of a singly linked list.
  182. */
  183. typedef struct slist_node slist_node;
  184. struct slist_node
  185. {
  186. slist_node *next;
  187. };
  188. /*
  189. * Head of a singly linked list.
  190. *
  191. * Singly linked lists are not circularly linked, in contrast to doubly linked
  192. * lists; we just set head.next to NULL if empty. This doesn't incur any
  193. * additional branches in the usual manipulations.
  194. */
  195. typedef struct slist_head
  196. {
  197. slist_node head;
  198. } slist_head;
  199. /*
  200. * Singly linked list iterator.
  201. *
  202. * Used as state in slist_foreach(). To get the current element of the
  203. * iteration use the 'cur' member.
  204. *
  205. * It's allowed to modify the list while iterating, with the exception of
  206. * deleting the iterator's current node; deletion of that node requires
  207. * care if the iteration is to be continued afterward. (Doing so and also
  208. * deleting or inserting adjacent list elements might misbehave; also, if
  209. * the user frees the current node's storage, continuing the iteration is
  210. * not safe.)
  211. *
  212. * NB: this wouldn't really need to be an extra struct, we could use an
  213. * slist_node * directly. We prefer a separate type for consistency.
  214. */
  215. typedef struct slist_iter
  216. {
  217. slist_node *cur;
  218. } slist_iter;
  219. /*
  220. * Singly linked list iterator allowing some modifications while iterating.
  221. *
  222. * Used as state in slist_foreach_modify(). To get the current element of the
  223. * iteration use the 'cur' member.
  224. *
  225. * The only list modification allowed while iterating is to remove the current
  226. * node via slist_delete_current() (*not* slist_delete()). Insertion or
  227. * deletion of nodes adjacent to the current node would misbehave.
  228. */
  229. typedef struct slist_mutable_iter
  230. {
  231. slist_node *cur; /* current element */
  232. slist_node *next; /* next node we'll iterate to */
  233. slist_node *prev; /* prev node, for deletions */
  234. } slist_mutable_iter;
  235. /* Static initializers */
  236. #define DLIST_STATIC_INIT(name) {{&(name).head, &(name).head}}
  237. #define SLIST_STATIC_INIT(name) {{NULL}}
  238. /* Prototypes for functions too big to be inline */
  239. /* Caution: this is O(n); consider using slist_delete_current() instead */
  240. extern void slist_delete(slist_head *head, slist_node *node);
  241. #ifdef ILIST_DEBUG
  242. extern void dlist_check(dlist_head *head);
  243. extern void slist_check(slist_head *head);
  244. #else
  245. /*
  246. * These seemingly useless casts to void are here to keep the compiler quiet
  247. * about the argument being unused in many functions in a non-debug compile,
  248. * in which functions the only point of passing the list head pointer is to be
  249. * able to run these checks.
  250. */
  251. #define dlist_check(head) ((void) (head))
  252. #define slist_check(head) ((void) (head))
  253. #endif /* ILIST_DEBUG */
  254. /* doubly linked list implementation */
  255. /*
  256. * Initialize a doubly linked list.
  257. * Previous state will be thrown away without any cleanup.
  258. */
  259. static inline void
  260. dlist_init(dlist_head *head)
  261. {
  262. head->head.next = head->head.prev = &head->head;
  263. }
  264. /*
  265. * Is the list empty?
  266. *
  267. * An empty list has either its first 'next' pointer set to NULL, or to itself.
  268. */
  269. static inline bool
  270. dlist_is_empty(dlist_head *head)
  271. {
  272. dlist_check(head);
  273. return head->head.next == NULL || head->head.next == &(head->head);
  274. }
  275. /*
  276. * Insert a node at the beginning of the list.
  277. */
  278. static inline void
  279. dlist_push_head(dlist_head *head, dlist_node *node)
  280. {
  281. if (head->head.next == NULL) /* convert NULL header to circular */
  282. dlist_init(head);
  283. node->next = head->head.next;
  284. node->prev = &head->head;
  285. node->next->prev = node;
  286. head->head.next = node;
  287. dlist_check(head);
  288. }
  289. /*
  290. * Insert a node at the end of the list.
  291. */
  292. static inline void
  293. dlist_push_tail(dlist_head *head, dlist_node *node)
  294. {
  295. if (head->head.next == NULL) /* convert NULL header to circular */
  296. dlist_init(head);
  297. node->next = &head->head;
  298. node->prev = head->head.prev;
  299. node->prev->next = node;
  300. head->head.prev = node;
  301. dlist_check(head);
  302. }
  303. /*
  304. * Insert a node after another *in the same list*
  305. */
  306. static inline void
  307. dlist_insert_after(dlist_node *after, dlist_node *node)
  308. {
  309. node->prev = after;
  310. node->next = after->next;
  311. after->next = node;
  312. node->next->prev = node;
  313. }
  314. /*
  315. * Insert a node before another *in the same list*
  316. */
  317. static inline void
  318. dlist_insert_before(dlist_node *before, dlist_node *node)
  319. {
  320. node->prev = before->prev;
  321. node->next = before;
  322. before->prev = node;
  323. node->prev->next = node;
  324. }
  325. /*
  326. * Delete 'node' from its list (it must be in one).
  327. */
  328. static inline void
  329. dlist_delete(dlist_node *node)
  330. {
  331. node->prev->next = node->next;
  332. node->next->prev = node->prev;
  333. }
  334. /*
  335. * Remove and return the first node from a list (there must be one).
  336. */
  337. static inline dlist_node *
  338. dlist_pop_head_node(dlist_head *head)
  339. {
  340. dlist_node *node;
  341. Assert(!dlist_is_empty(head));
  342. node = head->head.next;
  343. dlist_delete(node);
  344. return node;
  345. }
  346. /*
  347. * Move element from its current position in the list to the head position in
  348. * the same list.
  349. *
  350. * Undefined behaviour if 'node' is not already part of the list.
  351. */
  352. static inline void
  353. dlist_move_head(dlist_head *head, dlist_node *node)
  354. {
  355. /* fast path if it's already at the head */
  356. if (head->head.next == node)
  357. return;
  358. dlist_delete(node);
  359. dlist_push_head(head, node);
  360. dlist_check(head);
  361. }
  362. /*
  363. * Check whether 'node' has a following node.
  364. * Caution: unreliable if 'node' is not in the list.
  365. */
  366. static inline bool
  367. dlist_has_next(dlist_head *head, dlist_node *node)
  368. {
  369. return node->next != &head->head;
  370. }
  371. /*
  372. * Check whether 'node' has a preceding node.
  373. * Caution: unreliable if 'node' is not in the list.
  374. */
  375. static inline bool
  376. dlist_has_prev(dlist_head *head, dlist_node *node)
  377. {
  378. return node->prev != &head->head;
  379. }
  380. /*
  381. * Return the next node in the list (there must be one).
  382. */
  383. static inline dlist_node *
  384. dlist_next_node(dlist_head *head, dlist_node *node)
  385. {
  386. Assert(dlist_has_next(head, node));
  387. return node->next;
  388. }
  389. /*
  390. * Return previous node in the list (there must be one).
  391. */
  392. static inline dlist_node *
  393. dlist_prev_node(dlist_head *head, dlist_node *node)
  394. {
  395. Assert(dlist_has_prev(head, node));
  396. return node->prev;
  397. }
  398. /* internal support function to get address of head element's struct */
  399. static inline void *
  400. dlist_head_element_off(dlist_head *head, size_t off)
  401. {
  402. Assert(!dlist_is_empty(head));
  403. return (char *) head->head.next - off;
  404. }
  405. /*
  406. * Return the first node in the list (there must be one).
  407. */
  408. static inline dlist_node *
  409. dlist_head_node(dlist_head *head)
  410. {
  411. return (dlist_node *) dlist_head_element_off(head, 0);
  412. }
  413. /* internal support function to get address of tail element's struct */
  414. static inline void *
  415. dlist_tail_element_off(dlist_head *head, size_t off)
  416. {
  417. Assert(!dlist_is_empty(head));
  418. return (char *) head->head.prev - off;
  419. }
  420. /*
  421. * Return the last node in the list (there must be one).
  422. */
  423. static inline dlist_node *
  424. dlist_tail_node(dlist_head *head)
  425. {
  426. return (dlist_node *) dlist_tail_element_off(head, 0);
  427. }
  428. /*
  429. * Return the containing struct of 'type' where 'membername' is the dlist_node
  430. * pointed at by 'ptr'.
  431. *
  432. * This is used to convert a dlist_node * back to its containing struct.
  433. */
  434. #define dlist_container(type, membername, ptr) \
  435. (AssertVariableIsOfTypeMacro(ptr, dlist_node *), \
  436. AssertVariableIsOfTypeMacro(((type *) NULL)->membername, dlist_node), \
  437. ((type *) ((char *) (ptr) - offsetof(type, membername))))
  438. /*
  439. * Return the address of the first element in the list.
  440. *
  441. * The list must not be empty.
  442. */
  443. #define dlist_head_element(type, membername, lhead) \
  444. (AssertVariableIsOfTypeMacro(((type *) NULL)->membername, dlist_node), \
  445. (type *) dlist_head_element_off(lhead, offsetof(type, membername)))
  446. /*
  447. * Return the address of the last element in the list.
  448. *
  449. * The list must not be empty.
  450. */
  451. #define dlist_tail_element(type, membername, lhead) \
  452. (AssertVariableIsOfTypeMacro(((type *) NULL)->membername, dlist_node), \
  453. ((type *) dlist_tail_element_off(lhead, offsetof(type, membername))))
  454. /*
  455. * Iterate through the list pointed at by 'lhead' storing the state in 'iter'.
  456. *
  457. * Access the current element with iter.cur.
  458. *
  459. * It is *not* allowed to manipulate the list during iteration.
  460. */
  461. #define dlist_foreach(iter, lhead) \
  462. for (AssertVariableIsOfTypeMacro(iter, dlist_iter), \
  463. AssertVariableIsOfTypeMacro(lhead, dlist_head *), \
  464. (iter).end = &(lhead)->head, \
  465. (iter).cur = (iter).end->next ? (iter).end->next : (iter).end; \
  466. (iter).cur != (iter).end; \
  467. (iter).cur = (iter).cur->next)
  468. /*
  469. * Iterate through the list pointed at by 'lhead' storing the state in 'iter'.
  470. *
  471. * Access the current element with iter.cur.
  472. *
  473. * Iterations using this are only allowed to change the list at the current
  474. * point of iteration. It is fine to delete the current node, but it is *not*
  475. * fine to insert or delete adjacent nodes.
  476. */
  477. #define dlist_foreach_modify(iter, lhead) \
  478. for (AssertVariableIsOfTypeMacro(iter, dlist_mutable_iter), \
  479. AssertVariableIsOfTypeMacro(lhead, dlist_head *), \
  480. (iter).end = &(lhead)->head, \
  481. (iter).cur = (iter).end->next ? (iter).end->next : (iter).end, \
  482. (iter).next = (iter).cur->next; \
  483. (iter).cur != (iter).end; \
  484. (iter).cur = (iter).next, (iter).next = (iter).cur->next)
  485. /*
  486. * Iterate through the list in reverse order.
  487. *
  488. * It is *not* allowed to manipulate the list during iteration.
  489. */
  490. #define dlist_reverse_foreach(iter, lhead) \
  491. for (AssertVariableIsOfTypeMacro(iter, dlist_iter), \
  492. AssertVariableIsOfTypeMacro(lhead, dlist_head *), \
  493. (iter).end = &(lhead)->head, \
  494. (iter).cur = (iter).end->prev ? (iter).end->prev : (iter).end; \
  495. (iter).cur != (iter).end; \
  496. (iter).cur = (iter).cur->prev)
  497. /* singly linked list implementation */
  498. /*
  499. * Initialize a singly linked list.
  500. * Previous state will be thrown away without any cleanup.
  501. */
  502. static inline void
  503. slist_init(slist_head *head)
  504. {
  505. head->head.next = NULL;
  506. }
  507. /*
  508. * Is the list empty?
  509. */
  510. static inline bool
  511. slist_is_empty(slist_head *head)
  512. {
  513. slist_check(head);
  514. return head->head.next == NULL;
  515. }
  516. /*
  517. * Insert a node at the beginning of the list.
  518. */
  519. static inline void
  520. slist_push_head(slist_head *head, slist_node *node)
  521. {
  522. node->next = head->head.next;
  523. head->head.next = node;
  524. slist_check(head);
  525. }
  526. /*
  527. * Insert a node after another *in the same list*
  528. */
  529. static inline void
  530. slist_insert_after(slist_node *after, slist_node *node)
  531. {
  532. node->next = after->next;
  533. after->next = node;
  534. }
  535. /*
  536. * Remove and return the first node from a list (there must be one).
  537. */
  538. static inline slist_node *
  539. slist_pop_head_node(slist_head *head)
  540. {
  541. slist_node *node;
  542. Assert(!slist_is_empty(head));
  543. node = head->head.next;
  544. head->head.next = node->next;
  545. slist_check(head);
  546. return node;
  547. }
  548. /*
  549. * Check whether 'node' has a following node.
  550. */
  551. static inline bool
  552. slist_has_next(slist_head *head, slist_node *node)
  553. {
  554. slist_check(head);
  555. return node->next != NULL;
  556. }
  557. /*
  558. * Return the next node in the list (there must be one).
  559. */
  560. static inline slist_node *
  561. slist_next_node(slist_head *head, slist_node *node)
  562. {
  563. Assert(slist_has_next(head, node));
  564. return node->next;
  565. }
  566. /* internal support function to get address of head element's struct */
  567. static inline void *
  568. slist_head_element_off(slist_head *head, size_t off)
  569. {
  570. Assert(!slist_is_empty(head));
  571. return (char *) head->head.next - off;
  572. }
  573. /*
  574. * Return the first node in the list (there must be one).
  575. */
  576. static inline slist_node *
  577. slist_head_node(slist_head *head)
  578. {
  579. return (slist_node *) slist_head_element_off(head, 0);
  580. }
  581. /*
  582. * Delete the list element the iterator currently points to.
  583. *
  584. * Caution: this modifies iter->cur, so don't use that again in the current
  585. * loop iteration.
  586. */
  587. static inline void
  588. slist_delete_current(slist_mutable_iter *iter)
  589. {
  590. /*
  591. * Update previous element's forward link. If the iteration is at the
  592. * first list element, iter->prev will point to the list header's "head"
  593. * field, so we don't need a special case for that.
  594. */
  595. iter->prev->next = iter->next;
  596. /*
  597. * Reset cur to prev, so that prev will continue to point to the prior
  598. * valid list element after slist_foreach_modify() advances to the next.
  599. */
  600. iter->cur = iter->prev;
  601. }
  602. /*
  603. * Return the containing struct of 'type' where 'membername' is the slist_node
  604. * pointed at by 'ptr'.
  605. *
  606. * This is used to convert a slist_node * back to its containing struct.
  607. */
  608. #define slist_container(type, membername, ptr) \
  609. (AssertVariableIsOfTypeMacro(ptr, slist_node *), \
  610. AssertVariableIsOfTypeMacro(((type *) NULL)->membername, slist_node), \
  611. ((type *) ((char *) (ptr) - offsetof(type, membername))))
  612. /*
  613. * Return the address of the first element in the list.
  614. *
  615. * The list must not be empty.
  616. */
  617. #define slist_head_element(type, membername, lhead) \
  618. (AssertVariableIsOfTypeMacro(((type *) NULL)->membername, slist_node), \
  619. (type *) slist_head_element_off(lhead, offsetof(type, membername)))
  620. /*
  621. * Iterate through the list pointed at by 'lhead' storing the state in 'iter'.
  622. *
  623. * Access the current element with iter.cur.
  624. *
  625. * It's allowed to modify the list while iterating, with the exception of
  626. * deleting the iterator's current node; deletion of that node requires
  627. * care if the iteration is to be continued afterward. (Doing so and also
  628. * deleting or inserting adjacent list elements might misbehave; also, if
  629. * the user frees the current node's storage, continuing the iteration is
  630. * not safe.)
  631. */
  632. #define slist_foreach(iter, lhead) \
  633. for (AssertVariableIsOfTypeMacro(iter, slist_iter), \
  634. AssertVariableIsOfTypeMacro(lhead, slist_head *), \
  635. (iter).cur = (lhead)->head.next; \
  636. (iter).cur != NULL; \
  637. (iter).cur = (iter).cur->next)
  638. /*
  639. * Iterate through the list pointed at by 'lhead' storing the state in 'iter'.
  640. *
  641. * Access the current element with iter.cur.
  642. *
  643. * The only list modification allowed while iterating is to remove the current
  644. * node via slist_delete_current() (*not* slist_delete()). Insertion or
  645. * deletion of nodes adjacent to the current node would misbehave.
  646. */
  647. #define slist_foreach_modify(iter, lhead) \
  648. for (AssertVariableIsOfTypeMacro(iter, slist_mutable_iter), \
  649. AssertVariableIsOfTypeMacro(lhead, slist_head *), \
  650. (iter).prev = &(lhead)->head, \
  651. (iter).cur = (iter).prev->next, \
  652. (iter).next = (iter).cur ? (iter).cur->next : NULL; \
  653. (iter).cur != NULL; \
  654. (iter).prev = (iter).cur, \
  655. (iter).cur = (iter).next, \
  656. (iter).next = (iter).next ? (iter).next->next : NULL)
  657. #endif /* ILIST_H */