linkedlists.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2006, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. * Kevin P. Fleming <kpfleming@digium.com>
  8. *
  9. * See http://www.asterisk.org for more information about
  10. * the Asterisk project. Please do not directly contact
  11. * any of the maintainers of this project for assistance;
  12. * the project provides a web site, mailing lists and IRC
  13. * channels for your use.
  14. *
  15. * This program is free software, distributed under the terms of
  16. * the GNU General Public License Version 2. See the LICENSE file
  17. * at the top of the source tree.
  18. */
  19. #ifndef ASTERISK_LINKEDLISTS_H
  20. #define ASTERISK_LINKEDLISTS_H
  21. #include "asterisk/lock.h"
  22. /*!
  23. * \file linkedlists.h
  24. * \brief A set of macros to manage forward-linked lists.
  25. */
  26. /*!
  27. * \brief Locks a list.
  28. * \param head This is a pointer to the list head structure
  29. *
  30. * This macro attempts to place an exclusive lock in the
  31. * list head structure pointed to by head.
  32. * \retval 0 on success
  33. * \retval non-zero on failure
  34. */
  35. #define AST_LIST_LOCK(head) \
  36. ast_mutex_lock(&(head)->lock)
  37. /*!
  38. * \brief Write locks a list.
  39. * \param head This is a pointer to the list head structure
  40. *
  41. * This macro attempts to place an exclusive write lock in the
  42. * list head structure pointed to by head.
  43. * \retval 0 on success
  44. * \retval non-zero on failure
  45. */
  46. #define AST_RWLIST_WRLOCK(head) \
  47. ast_rwlock_wrlock(&(head)->lock)
  48. /*!
  49. * \brief Write locks a list, with timeout.
  50. * \param head This is a pointer to the list head structure
  51. * \param ts Pointer to a timespec structure
  52. *
  53. * This macro attempts to place an exclusive write lock in the
  54. * list head structure pointed to by head.
  55. * \retval 0 on success
  56. * \retval non-zero on failure
  57. *
  58. * \since 1.6.1
  59. */
  60. #define AST_RWLIST_TIMEDWRLOCK(head, ts) ast_rwlock_timedwrlock(&(head)->lock, ts)
  61. /*!
  62. * \brief Read locks a list.
  63. * \param head This is a pointer to the list head structure
  64. *
  65. * This macro attempts to place a read lock in the
  66. * list head structure pointed to by head.
  67. * \retval 0 on success
  68. * \retval non-zero on failure
  69. */
  70. #define AST_RWLIST_RDLOCK(head) \
  71. ast_rwlock_rdlock(&(head)->lock)
  72. /*!
  73. * \brief Read locks a list, with timeout.
  74. * \param head This is a pointer to the list head structure
  75. * \param ts Pointer to a timespec structure
  76. *
  77. * This macro attempts to place a read lock in the
  78. * list head structure pointed to by head.
  79. * \retval 0 on success
  80. * \retval non-zero on failure
  81. *
  82. * \since 1.6.1
  83. */
  84. #define AST_RWLIST_TIMEDRDLOCK(head, ts) \
  85. ast_rwlock_timedrdlock(&(head)->lock, ts)
  86. /*!
  87. * \brief Locks a list, without blocking if the list is locked.
  88. * \param head This is a pointer to the list head structure
  89. *
  90. * This macro attempts to place an exclusive lock in the
  91. * list head structure pointed to by head.
  92. * \retval 0 on success
  93. * \retval non-zero on failure
  94. */
  95. #define AST_LIST_TRYLOCK(head) \
  96. ast_mutex_trylock(&(head)->lock)
  97. /*!
  98. * \brief Write locks a list, without blocking if the list is locked.
  99. * \param head This is a pointer to the list head structure
  100. *
  101. * This macro attempts to place an exclusive write lock in the
  102. * list head structure pointed to by head.
  103. * \retval 0 on success
  104. * \retval non-zero on failure
  105. */
  106. #define AST_RWLIST_TRYWRLOCK(head) \
  107. ast_rwlock_trywrlock(&(head)->lock)
  108. /*!
  109. * \brief Read locks a list, without blocking if the list is locked.
  110. * \param head This is a pointer to the list head structure
  111. *
  112. * This macro attempts to place a read lock in the
  113. * list head structure pointed to by head.
  114. * \retval 0 on success
  115. * \retval non-zero on failure
  116. */
  117. #define AST_RWLIST_TRYRDLOCK(head) \
  118. ast_rwlock_tryrdlock(&(head)->lock)
  119. /*!
  120. * \brief Attempts to unlock a list.
  121. * \param head This is a pointer to the list head structure
  122. *
  123. * This macro attempts to remove an exclusive lock from the
  124. * list head structure pointed to by head. If the list
  125. * was not locked by this thread, this macro has no effect.
  126. */
  127. #define AST_LIST_UNLOCK(head) \
  128. ast_mutex_unlock(&(head)->lock)
  129. /*!
  130. * \brief Attempts to unlock a read/write based list.
  131. * \param head This is a pointer to the list head structure
  132. *
  133. * This macro attempts to remove a read or write lock from the
  134. * list head structure pointed to by head. If the list
  135. * was not locked by this thread, this macro has no effect.
  136. */
  137. #define AST_RWLIST_UNLOCK(head) \
  138. ast_rwlock_unlock(&(head)->lock)
  139. /*!
  140. * \brief Defines a structure to be used to hold a list of specified type.
  141. * \param name This will be the name of the defined structure.
  142. * \param type This is the type of each list entry.
  143. *
  144. * This macro creates a structure definition that can be used
  145. * to hold a list of the entries of type \a type. It does not actually
  146. * declare (allocate) a structure; to do that, either follow this
  147. * macro with the desired name of the instance you wish to declare,
  148. * or use the specified \a name to declare instances elsewhere.
  149. *
  150. * Example usage:
  151. * \code
  152. * static AST_LIST_HEAD(entry_list, entry) entries;
  153. * \endcode
  154. *
  155. * This would define \c struct \c entry_list, and declare an instance of it named
  156. * \a entries, all intended to hold a list of type \c struct \c entry.
  157. */
  158. #define AST_LIST_HEAD(name, type) \
  159. struct name { \
  160. struct type *first; \
  161. struct type *last; \
  162. ast_mutex_t lock; \
  163. }
  164. /*!
  165. * \brief Defines a structure to be used to hold a read/write list of specified type.
  166. * \param name This will be the name of the defined structure.
  167. * \param type This is the type of each list entry.
  168. *
  169. * This macro creates a structure definition that can be used
  170. * to hold a list of the entries of type \a type. It does not actually
  171. * declare (allocate) a structure; to do that, either follow this
  172. * macro with the desired name of the instance you wish to declare,
  173. * or use the specified \a name to declare instances elsewhere.
  174. *
  175. * Example usage:
  176. * \code
  177. * static AST_RWLIST_HEAD(entry_list, entry) entries;
  178. * \endcode
  179. *
  180. * This would define \c struct \c entry_list, and declare an instance of it named
  181. * \a entries, all intended to hold a list of type \c struct \c entry.
  182. */
  183. #define AST_RWLIST_HEAD(name, type) \
  184. struct name { \
  185. struct type *first; \
  186. struct type *last; \
  187. ast_rwlock_t lock; \
  188. }
  189. /*!
  190. * \brief Defines a structure to be used to hold a list of specified type (with no lock).
  191. * \param name This will be the name of the defined structure.
  192. * \param type This is the type of each list entry.
  193. *
  194. * This macro creates a structure definition that can be used
  195. * to hold a list of the entries of type \a type. It does not actually
  196. * declare (allocate) a structure; to do that, either follow this
  197. * macro with the desired name of the instance you wish to declare,
  198. * or use the specified \a name to declare instances elsewhere.
  199. *
  200. * Example usage:
  201. * \code
  202. * static AST_LIST_HEAD_NOLOCK(entry_list, entry) entries;
  203. * \endcode
  204. *
  205. * This would define \c struct \c entry_list, and declare an instance of it named
  206. * \a entries, all intended to hold a list of type \c struct \c entry.
  207. */
  208. #define AST_LIST_HEAD_NOLOCK(name, type) \
  209. struct name { \
  210. struct type *first; \
  211. struct type *last; \
  212. }
  213. /*!
  214. * \brief Defines initial values for a declaration of AST_LIST_HEAD
  215. */
  216. #define AST_LIST_HEAD_INIT_VALUE { \
  217. .first = NULL, \
  218. .last = NULL, \
  219. .lock = AST_MUTEX_INIT_VALUE, \
  220. }
  221. /*!
  222. * \brief Defines initial values for a declaration of AST_RWLIST_HEAD
  223. */
  224. #define AST_RWLIST_HEAD_INIT_VALUE { \
  225. .first = NULL, \
  226. .last = NULL, \
  227. .lock = AST_RWLOCK_INIT_VALUE, \
  228. }
  229. /*!
  230. * \brief Defines initial values for a declaration of AST_LIST_HEAD_NOLOCK
  231. */
  232. #define AST_LIST_HEAD_NOLOCK_INIT_VALUE { \
  233. .first = NULL, \
  234. .last = NULL, \
  235. }
  236. /*!
  237. * \brief Defines a structure to be used to hold a list of specified type, statically initialized.
  238. * \param name This will be the name of the defined structure.
  239. * \param type This is the type of each list entry.
  240. *
  241. * This macro creates a structure definition that can be used
  242. * to hold a list of the entries of type \a type, and allocates an instance
  243. * of it, initialized to be empty.
  244. *
  245. * Example usage:
  246. * \code
  247. * static AST_LIST_HEAD_STATIC(entry_list, entry);
  248. * \endcode
  249. *
  250. * This would define \c struct \c entry_list, intended to hold a list of
  251. * type \c struct \c entry.
  252. */
  253. #if defined(AST_MUTEX_INIT_W_CONSTRUCTORS)
  254. #define AST_LIST_HEAD_STATIC(name, type) \
  255. struct name { \
  256. struct type *first; \
  257. struct type *last; \
  258. ast_mutex_t lock; \
  259. } name; \
  260. static void __attribute__((constructor)) __init_##name(void) \
  261. { \
  262. AST_LIST_HEAD_INIT(&name); \
  263. } \
  264. static void __attribute__((destructor)) __fini_##name(void) \
  265. { \
  266. AST_LIST_HEAD_DESTROY(&name); \
  267. } \
  268. struct __dummy_##name
  269. #else
  270. #define AST_LIST_HEAD_STATIC(name, type) \
  271. struct name { \
  272. struct type *first; \
  273. struct type *last; \
  274. ast_mutex_t lock; \
  275. } name = AST_LIST_HEAD_INIT_VALUE
  276. #endif
  277. /*!
  278. * \brief Defines a structure to be used to hold a read/write list of specified type, statically initialized.
  279. * \param name This will be the name of the defined structure.
  280. * \param type This is the type of each list entry.
  281. *
  282. * This macro creates a structure definition that can be used
  283. * to hold a list of the entries of type \a type, and allocates an instance
  284. * of it, initialized to be empty.
  285. *
  286. * Example usage:
  287. * \code
  288. * static AST_RWLIST_HEAD_STATIC(entry_list, entry);
  289. * \endcode
  290. *
  291. * This would define \c struct \c entry_list, intended to hold a list of
  292. * type \c struct \c entry.
  293. */
  294. #ifndef HAVE_PTHREAD_RWLOCK_INITIALIZER
  295. #define AST_RWLIST_HEAD_STATIC(name, type) \
  296. struct name { \
  297. struct type *first; \
  298. struct type *last; \
  299. ast_rwlock_t lock; \
  300. } name; \
  301. static void __attribute__((constructor)) __init_##name(void) \
  302. { \
  303. AST_RWLIST_HEAD_INIT(&name); \
  304. } \
  305. static void __attribute__((destructor)) __fini_##name(void) \
  306. { \
  307. AST_RWLIST_HEAD_DESTROY(&name); \
  308. } \
  309. struct __dummy_##name
  310. #else
  311. #define AST_RWLIST_HEAD_STATIC(name, type) \
  312. struct name { \
  313. struct type *first; \
  314. struct type *last; \
  315. ast_rwlock_t lock; \
  316. } name = AST_RWLIST_HEAD_INIT_VALUE
  317. #endif
  318. /*!
  319. * \brief Defines a structure to be used to hold a list of specified type, statically initialized.
  320. *
  321. * This is the same as AST_LIST_HEAD_STATIC, except without the lock included.
  322. */
  323. #define AST_LIST_HEAD_NOLOCK_STATIC(name, type) \
  324. struct name { \
  325. struct type *first; \
  326. struct type *last; \
  327. } name = AST_LIST_HEAD_NOLOCK_INIT_VALUE
  328. /*!
  329. * \brief Initializes a list head structure with a specified first entry.
  330. * \param head This is a pointer to the list head structure
  331. * \param entry pointer to the list entry that will become the head of the list
  332. *
  333. * This macro initializes a list head structure by setting the head
  334. * entry to the supplied value and recreating the embedded lock.
  335. */
  336. #define AST_LIST_HEAD_SET(head, entry) do { \
  337. (head)->first = (entry); \
  338. (head)->last = (entry); \
  339. ast_mutex_init(&(head)->lock); \
  340. } while (0)
  341. /*!
  342. * \brief Initializes an rwlist head structure with a specified first entry.
  343. * \param head This is a pointer to the list head structure
  344. * \param entry pointer to the list entry that will become the head of the list
  345. *
  346. * This macro initializes a list head structure by setting the head
  347. * entry to the supplied value and recreating the embedded lock.
  348. */
  349. #define AST_RWLIST_HEAD_SET(head, entry) do { \
  350. (head)->first = (entry); \
  351. (head)->last = (entry); \
  352. ast_rwlock_init(&(head)->lock); \
  353. } while (0)
  354. /*!
  355. * \brief Initializes a list head structure with a specified first entry.
  356. * \param head This is a pointer to the list head structure
  357. * \param entry pointer to the list entry that will become the head of the list
  358. *
  359. * This macro initializes a list head structure by setting the head
  360. * entry to the supplied value.
  361. */
  362. #define AST_LIST_HEAD_SET_NOLOCK(head, entry) do { \
  363. (head)->first = (entry); \
  364. (head)->last = (entry); \
  365. } while (0)
  366. /*!
  367. * \brief Declare a forward link structure inside a list entry.
  368. * \param type This is the type of each list entry.
  369. *
  370. * This macro declares a structure to be used to link list entries together.
  371. * It must be used inside the definition of the structure named in
  372. * \a type, as follows:
  373. *
  374. * \code
  375. * struct list_entry {
  376. ...
  377. AST_LIST_ENTRY(list_entry) list;
  378. * }
  379. * \endcode
  380. *
  381. * The field name \a list here is arbitrary, and can be anything you wish.
  382. */
  383. #define AST_LIST_ENTRY(type) \
  384. struct { \
  385. struct type *next; \
  386. }
  387. #define AST_RWLIST_ENTRY AST_LIST_ENTRY
  388. /*!
  389. * \brief Returns the first entry contained in a list.
  390. * \param head This is a pointer to the list head structure
  391. */
  392. #define AST_LIST_FIRST(head) ((head)->first)
  393. #define AST_RWLIST_FIRST AST_LIST_FIRST
  394. /*!
  395. * \brief Returns the last entry contained in a list.
  396. * \param head This is a pointer to the list head structure
  397. */
  398. #define AST_LIST_LAST(head) ((head)->last)
  399. #define AST_RWLIST_LAST AST_LIST_LAST
  400. /*!
  401. * \brief Returns the next entry in the list after the given entry.
  402. * \param elm This is a pointer to the current entry.
  403. * \param field This is the name of the field (declared using AST_LIST_ENTRY())
  404. * used to link entries of this list together.
  405. */
  406. #define AST_LIST_NEXT(elm, field) ((elm)->field.next)
  407. #define AST_RWLIST_NEXT AST_LIST_NEXT
  408. /*!
  409. * \brief Checks whether the specified list contains any entries.
  410. * \param head This is a pointer to the list head structure
  411. *
  412. * \return zero if the list has entries
  413. * \return non-zero if not.
  414. */
  415. #define AST_LIST_EMPTY(head) (AST_LIST_FIRST(head) == NULL)
  416. #define AST_RWLIST_EMPTY AST_LIST_EMPTY
  417. /*!
  418. * \brief Loops over (traverses) the entries in a list.
  419. * \param head This is a pointer to the list head structure
  420. * \param var This is the name of the variable that will hold a pointer to the
  421. * current list entry on each iteration. It must be declared before calling
  422. * this macro.
  423. * \param field This is the name of the field (declared using AST_LIST_ENTRY())
  424. * used to link entries of this list together.
  425. *
  426. * This macro is use to loop over (traverse) the entries in a list. It uses a
  427. * \a for loop, and supplies the enclosed code with a pointer to each list
  428. * entry as it loops. It is typically used as follows:
  429. * \code
  430. * static AST_LIST_HEAD(entry_list, list_entry) entries;
  431. * ...
  432. * struct list_entry {
  433. ...
  434. AST_LIST_ENTRY(list_entry) list;
  435. * }
  436. * ...
  437. * struct list_entry *current;
  438. * ...
  439. * AST_LIST_TRAVERSE(&entries, current, list) {
  440. (do something with current here)
  441. * }
  442. * \endcode
  443. * \warning If you modify the forward-link pointer contained in the \a current entry while
  444. * inside the loop, the behavior will be unpredictable. At a minimum, the following
  445. * macros will modify the forward-link pointer, and should not be used inside
  446. * AST_LIST_TRAVERSE() against the entry pointed to by the \a current pointer without
  447. * careful consideration of their consequences:
  448. * \li AST_LIST_NEXT() (when used as an lvalue)
  449. * \li AST_LIST_INSERT_AFTER()
  450. * \li AST_LIST_INSERT_HEAD()
  451. * \li AST_LIST_INSERT_TAIL()
  452. * \li AST_LIST_INSERT_SORTALPHA()
  453. */
  454. #define AST_LIST_TRAVERSE(head,var,field) \
  455. for((var) = (head)->first; (var); (var) = (var)->field.next)
  456. #define AST_RWLIST_TRAVERSE AST_LIST_TRAVERSE
  457. /*!
  458. * \brief Loops safely over (traverses) the entries in a list.
  459. * \param head This is a pointer to the list head structure
  460. * \param var This is the name of the variable that will hold a pointer to the
  461. * current list entry on each iteration. It must be declared before calling
  462. * this macro.
  463. * \param field This is the name of the field (declared using AST_LIST_ENTRY())
  464. * used to link entries of this list together.
  465. *
  466. * This macro is used to safely loop over (traverse) the entries in a list. It
  467. * uses a \a for loop, and supplies the enclosed code with a pointer to each list
  468. * entry as it loops. It is typically used as follows:
  469. *
  470. * \code
  471. * static AST_LIST_HEAD(entry_list, list_entry) entries;
  472. * ...
  473. * struct list_entry {
  474. ...
  475. AST_LIST_ENTRY(list_entry) list;
  476. * }
  477. * ...
  478. * struct list_entry *current;
  479. * ...
  480. * AST_LIST_TRAVERSE_SAFE_BEGIN(&entries, current, list) {
  481. (do something with current here)
  482. * }
  483. * AST_LIST_TRAVERSE_SAFE_END;
  484. * \endcode
  485. *
  486. * It differs from AST_LIST_TRAVERSE() in that the code inside the loop can modify
  487. * (or even free, after calling AST_LIST_REMOVE_CURRENT()) the entry pointed to by
  488. * the \a current pointer without affecting the loop traversal.
  489. */
  490. #define AST_LIST_TRAVERSE_SAFE_BEGIN(head, var, field) { \
  491. typeof((head)) __list_head = head; \
  492. typeof(__list_head->first) __list_next; \
  493. typeof(__list_head->first) __list_prev = NULL; \
  494. typeof(__list_head->first) __list_current; \
  495. for ((var) = __list_head->first, \
  496. __list_current = (var), \
  497. __list_next = (var) ? (var)->field.next : NULL; \
  498. (var); \
  499. __list_prev = __list_current, \
  500. (var) = __list_next, \
  501. __list_current = (var), \
  502. __list_next = (var) ? (var)->field.next : NULL, \
  503. (void) __list_prev /* To quiet compiler? */ \
  504. )
  505. #define AST_RWLIST_TRAVERSE_SAFE_BEGIN AST_LIST_TRAVERSE_SAFE_BEGIN
  506. /*!
  507. * \brief Removes the \a current entry from a list during a traversal.
  508. * \param field This is the name of the field (declared using AST_LIST_ENTRY())
  509. * used to link entries of this list together.
  510. *
  511. * \note This macro can \b only be used inside an AST_LIST_TRAVERSE_SAFE_BEGIN()
  512. * block; it is used to unlink the current entry from the list without affecting
  513. * the list traversal (and without having to re-traverse the list to modify the
  514. * previous entry, if any).
  515. */
  516. #define AST_LIST_REMOVE_CURRENT(field) do { \
  517. __list_current->field.next = NULL; \
  518. __list_current = __list_prev; \
  519. if (__list_prev) { \
  520. __list_prev->field.next = __list_next; \
  521. } else { \
  522. __list_head->first = __list_next; \
  523. } \
  524. if (!__list_next) { \
  525. __list_head->last = __list_prev; \
  526. } \
  527. } while (0)
  528. #define AST_RWLIST_REMOVE_CURRENT AST_LIST_REMOVE_CURRENT
  529. /*!
  530. * \brief Move the current list entry to another list.
  531. *
  532. * \note This is a silly macro. It should be done explicitly
  533. * otherwise the field parameter must be the same for the two
  534. * lists.
  535. *
  536. * AST_LIST_REMOVE_CURRENT(field);
  537. * AST_LIST_INSERT_TAIL(newhead, var, other_field);
  538. */
  539. #define AST_LIST_MOVE_CURRENT(newhead, field) do { \
  540. typeof ((newhead)->first) __extracted = __list_current; \
  541. AST_LIST_REMOVE_CURRENT(field); \
  542. AST_LIST_INSERT_TAIL((newhead), __extracted, field); \
  543. } while (0)
  544. #define AST_RWLIST_MOVE_CURRENT AST_LIST_MOVE_CURRENT
  545. /*!
  546. * \brief Inserts a list entry before the current entry during a traversal.
  547. * \param elm This is a pointer to the entry to be inserted.
  548. * \param field This is the name of the field (declared using AST_LIST_ENTRY())
  549. * used to link entries of this list together.
  550. *
  551. * \note This macro can \b only be used inside an AST_LIST_TRAVERSE_SAFE_BEGIN()
  552. * block.
  553. */
  554. #define AST_LIST_INSERT_BEFORE_CURRENT(elm, field) do { \
  555. if (__list_prev) { \
  556. (elm)->field.next = __list_prev->field.next; \
  557. __list_prev->field.next = elm; \
  558. } else { \
  559. (elm)->field.next = __list_head->first; \
  560. __list_head->first = (elm); \
  561. } \
  562. __list_prev = (elm); \
  563. } while (0)
  564. #define AST_RWLIST_INSERT_BEFORE_CURRENT AST_LIST_INSERT_BEFORE_CURRENT
  565. /*!
  566. * \brief Closes a safe loop traversal block.
  567. */
  568. #define AST_LIST_TRAVERSE_SAFE_END }
  569. #define AST_RWLIST_TRAVERSE_SAFE_END AST_LIST_TRAVERSE_SAFE_END
  570. /*!
  571. * \brief Initializes a list head structure.
  572. * \param head This is a pointer to the list head structure
  573. *
  574. * This macro initializes a list head structure by setting the head
  575. * entry to \a NULL (empty list) and recreating the embedded lock.
  576. */
  577. #define AST_LIST_HEAD_INIT(head) { \
  578. (head)->first = NULL; \
  579. (head)->last = NULL; \
  580. ast_mutex_init(&(head)->lock); \
  581. }
  582. /*!
  583. * \brief Initializes an rwlist head structure.
  584. * \param head This is a pointer to the list head structure
  585. *
  586. * This macro initializes a list head structure by setting the head
  587. * entry to \a NULL (empty list) and recreating the embedded lock.
  588. */
  589. #define AST_RWLIST_HEAD_INIT(head) { \
  590. (head)->first = NULL; \
  591. (head)->last = NULL; \
  592. ast_rwlock_init(&(head)->lock); \
  593. }
  594. /*!
  595. * \brief Destroys a list head structure.
  596. * \param head This is a pointer to the list head structure
  597. *
  598. * This macro destroys a list head structure by setting the head
  599. * entry to \a NULL (empty list) and destroying the embedded lock.
  600. * It does not free the structure from memory.
  601. */
  602. #define AST_LIST_HEAD_DESTROY(head) { \
  603. (head)->first = NULL; \
  604. (head)->last = NULL; \
  605. ast_mutex_destroy(&(head)->lock); \
  606. }
  607. /*!
  608. * \brief Destroys an rwlist head structure.
  609. * \param head This is a pointer to the list head structure
  610. *
  611. * This macro destroys a list head structure by setting the head
  612. * entry to \a NULL (empty list) and destroying the embedded lock.
  613. * It does not free the structure from memory.
  614. */
  615. #define AST_RWLIST_HEAD_DESTROY(head) { \
  616. (head)->first = NULL; \
  617. (head)->last = NULL; \
  618. ast_rwlock_destroy(&(head)->lock); \
  619. }
  620. /*!
  621. * \brief Initializes a list head structure.
  622. * \param head This is a pointer to the list head structure
  623. *
  624. * This macro initializes a list head structure by setting the head
  625. * entry to \a NULL (empty list). There is no embedded lock handling
  626. * with this macro.
  627. */
  628. #define AST_LIST_HEAD_INIT_NOLOCK(head) { \
  629. (head)->first = NULL; \
  630. (head)->last = NULL; \
  631. }
  632. /*!
  633. * \brief Inserts a list entry after a given entry.
  634. * \param head This is a pointer to the list head structure
  635. * \param listelm This is a pointer to the entry after which the new entry should
  636. * be inserted.
  637. * \param elm This is a pointer to the entry to be inserted.
  638. * \param field This is the name of the field (declared using AST_LIST_ENTRY())
  639. * used to link entries of this list together.
  640. */
  641. #define AST_LIST_INSERT_AFTER(head, listelm, elm, field) do { \
  642. (elm)->field.next = (listelm)->field.next; \
  643. (listelm)->field.next = (elm); \
  644. if ((head)->last == (listelm)) \
  645. (head)->last = (elm); \
  646. } while (0)
  647. #define AST_RWLIST_INSERT_AFTER AST_LIST_INSERT_AFTER
  648. /*!
  649. * \brief Inserts a list entry at the head of a list.
  650. * \param head This is a pointer to the list head structure
  651. * \param elm This is a pointer to the entry to be inserted.
  652. * \param field This is the name of the field (declared using AST_LIST_ENTRY())
  653. * used to link entries of this list together.
  654. */
  655. #define AST_LIST_INSERT_HEAD(head, elm, field) do { \
  656. (elm)->field.next = (head)->first; \
  657. (head)->first = (elm); \
  658. if (!(head)->last) \
  659. (head)->last = (elm); \
  660. } while (0)
  661. #define AST_RWLIST_INSERT_HEAD AST_LIST_INSERT_HEAD
  662. /*!
  663. * \brief Appends a list entry to the tail of a list.
  664. * \param head This is a pointer to the list head structure
  665. * \param elm This is a pointer to the entry to be appended.
  666. * \param field This is the name of the field (declared using AST_LIST_ENTRY())
  667. * used to link entries of this list together.
  668. *
  669. * Note: The link field in the appended entry is \b not modified, so if it is
  670. * actually the head of a list itself, the entire list will be appended
  671. * temporarily (until the next AST_LIST_INSERT_TAIL is performed).
  672. */
  673. #define AST_LIST_INSERT_TAIL(head, elm, field) do { \
  674. if (!(head)->first) { \
  675. (head)->first = (elm); \
  676. (head)->last = (elm); \
  677. } else { \
  678. (head)->last->field.next = (elm); \
  679. (head)->last = (elm); \
  680. } \
  681. } while (0)
  682. #define AST_RWLIST_INSERT_TAIL AST_LIST_INSERT_TAIL
  683. /*!
  684. * \brief Inserts a list entry into a alphabetically sorted list
  685. * \param head Pointer to the list head structure
  686. * \param elm Pointer to the entry to be inserted
  687. * \param field Name of the list entry field (declared using AST_LIST_ENTRY())
  688. * \param sortfield Name of the field on which the list is sorted
  689. * \since 1.6.1
  690. */
  691. #define AST_LIST_INSERT_SORTALPHA(head, elm, field, sortfield) do { \
  692. if (!(head)->first) { \
  693. (head)->first = (elm); \
  694. (head)->last = (elm); \
  695. } else { \
  696. typeof((head)->first) __cur = (head)->first, __prev = NULL; \
  697. while (__cur && strcmp(__cur->sortfield, elm->sortfield) < 0) { \
  698. __prev = __cur; \
  699. __cur = __cur->field.next; \
  700. } \
  701. if (!__prev) { \
  702. AST_LIST_INSERT_HEAD(head, elm, field); \
  703. } else if (!__cur) { \
  704. AST_LIST_INSERT_TAIL(head, elm, field); \
  705. } else { \
  706. AST_LIST_INSERT_AFTER(head, __prev, elm, field); \
  707. } \
  708. } \
  709. } while (0)
  710. #define AST_RWLIST_INSERT_SORTALPHA AST_LIST_INSERT_SORTALPHA
  711. /*!
  712. * \brief Appends a whole list to the tail of a list.
  713. * \param head This is a pointer to the list head structure
  714. * \param list This is a pointer to the list to be appended.
  715. * \param field This is the name of the field (declared using AST_LIST_ENTRY())
  716. * used to link entries of this list together.
  717. *
  718. * Note: The source list (the \a list parameter) will be empty after
  719. * calling this macro (the list entries are \b moved to the target list).
  720. */
  721. #define AST_LIST_APPEND_LIST(head, list, field) do { \
  722. if (!(list)->first) { \
  723. break; \
  724. } \
  725. if (!(head)->first) { \
  726. (head)->first = (list)->first; \
  727. (head)->last = (list)->last; \
  728. } else { \
  729. (head)->last->field.next = (list)->first; \
  730. (head)->last = (list)->last; \
  731. } \
  732. (list)->first = NULL; \
  733. (list)->last = NULL; \
  734. } while (0)
  735. #define AST_RWLIST_APPEND_LIST AST_LIST_APPEND_LIST
  736. /*!
  737. \brief Inserts a whole list after a specific entry in a list
  738. \param head This is a pointer to the list head structure
  739. \param list This is a pointer to the list to be inserted.
  740. \param elm This is a pointer to the entry after which the new list should
  741. be inserted.
  742. \param field This is the name of the field (declared using AST_LIST_ENTRY())
  743. used to link entries of the lists together.
  744. Note: The source list (the \a list parameter) will be empty after
  745. calling this macro (the list entries are \b moved to the target list).
  746. */
  747. #define AST_LIST_INSERT_LIST_AFTER(head, list, elm, field) do { \
  748. (list)->last->field.next = (elm)->field.next; \
  749. (elm)->field.next = (list)->first; \
  750. if ((head)->last == elm) { \
  751. (head)->last = (list)->last; \
  752. } \
  753. (list)->first = NULL; \
  754. (list)->last = NULL; \
  755. } while(0)
  756. #define AST_RWLIST_INSERT_LIST_AFTER AST_LIST_INSERT_LIST_AFTER
  757. /*!
  758. * \brief Removes and returns the head entry from a list.
  759. * \param head This is a pointer to the list head structure
  760. * \param field This is the name of the field (declared using AST_LIST_ENTRY())
  761. * used to link entries of this list together.
  762. *
  763. * Removes the head entry from the list, and returns a pointer to it.
  764. * This macro is safe to call on an empty list.
  765. */
  766. #define AST_LIST_REMOVE_HEAD(head, field) ({ \
  767. typeof((head)->first) __cur = (head)->first; \
  768. if (__cur) { \
  769. (head)->first = __cur->field.next; \
  770. __cur->field.next = NULL; \
  771. if ((head)->last == __cur) \
  772. (head)->last = NULL; \
  773. } \
  774. __cur; \
  775. })
  776. #define AST_RWLIST_REMOVE_HEAD AST_LIST_REMOVE_HEAD
  777. /*!
  778. * \brief Removes a specific entry from a list.
  779. * \param head This is a pointer to the list head structure
  780. * \param elm This is a pointer to the entry to be removed.
  781. * \param field This is the name of the field (declared using AST_LIST_ENTRY())
  782. * used to link entries of this list together.
  783. * \retval elm if elm was in the list.
  784. * \retval NULL if elm was not in the list or elm was NULL.
  785. * \warning The removed entry is \b not freed.
  786. */
  787. #define AST_LIST_REMOVE(head, elm, field) \
  788. ({ \
  789. typeof(elm) __elm = (elm); \
  790. if (__elm) { \
  791. if ((head)->first == __elm) { \
  792. (head)->first = __elm->field.next; \
  793. __elm->field.next = NULL; \
  794. if ((head)->last == __elm) { \
  795. (head)->last = NULL; \
  796. } \
  797. } else { \
  798. typeof(elm) __prev = (head)->first; \
  799. while (__prev && __prev->field.next != __elm) { \
  800. __prev = __prev->field.next; \
  801. } \
  802. if (__prev) { \
  803. __prev->field.next = __elm->field.next; \
  804. __elm->field.next = NULL; \
  805. if ((head)->last == __elm) { \
  806. (head)->last = __prev; \
  807. } \
  808. } else { \
  809. __elm = NULL; \
  810. } \
  811. } \
  812. } \
  813. __elm; \
  814. })
  815. #define AST_RWLIST_REMOVE AST_LIST_REMOVE
  816. #endif /* _ASTERISK_LINKEDLISTS_H */