linkedlists.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. /*!
  22. \brief Defines a structure to be used to hold a list of specified type (with no lock).
  23. \param name This will be the name of the defined structure.
  24. \param type This is the type of each list entry.
  25. This macro creates a structure definition that can be used
  26. to hold a list of the entries of type \a type. It does not actually
  27. declare (allocate) a structure; to do that, either follow this
  28. macro with the desired name of the instance you wish to declare,
  29. or use the specified \a name to declare instances elsewhere.
  30. Example usage:
  31. \code
  32. static AST_LIST_HEAD_NOLOCK(entry_list, entry) entries;
  33. \endcode
  34. This would define \c struct \c entry_list, and declare an instance of it named
  35. \a entries, all intended to hold a list of type \c struct \c entry.
  36. */
  37. #define AST_LIST_HEAD_NOLOCK(name, type) \
  38. struct name { \
  39. struct type *first; \
  40. struct type *last; \
  41. }
  42. /*!
  43. \brief Defines initial values for a declaration of AST_LIST_HEAD_NOLOCK
  44. */
  45. #define AST_LIST_HEAD_NOLOCK_INIT_VALUE { \
  46. .first = NULL, \
  47. .last = NULL, \
  48. }
  49. /*!
  50. \brief Defines a structure to be used to hold a list of specified type, statically initialized.
  51. This is the same as AST_LIST_HEAD_STATIC, except without the lock included.
  52. */
  53. #define AST_LIST_HEAD_NOLOCK_STATIC(name, type) \
  54. struct name { \
  55. struct type *first; \
  56. struct type *last; \
  57. } name = AST_LIST_HEAD_NOLOCK_INIT_VALUE
  58. /*!
  59. \brief Initializes a list head structure with a specified first entry.
  60. \param head This is a pointer to the list head structure
  61. \param entry pointer to the list entry that will become the head of the list
  62. This macro initializes a list head structure by setting the head
  63. entry to the supplied value.
  64. */
  65. #define AST_LIST_HEAD_SET_NOLOCK(head, entry) do { \
  66. (head)->first = (entry); \
  67. (head)->last = (entry); \
  68. } while (0)
  69. /*!
  70. \brief Declare a forward link structure inside a list entry.
  71. \param type This is the type of each list entry.
  72. This macro declares a structure to be used to link list entries together.
  73. It must be used inside the definition of the structure named in
  74. \a type, as follows:
  75. \code
  76. struct list_entry {
  77. ...
  78. AST_LIST_ENTRY(list_entry) list;
  79. }
  80. \endcode
  81. The field name \a list here is arbitrary, and can be anything you wish.
  82. */
  83. #define AST_LIST_ENTRY(type) \
  84. struct { \
  85. struct type *next; \
  86. }
  87. /*!
  88. \brief Returns the first entry contained in a list.
  89. \param head This is a pointer to the list head structure
  90. */
  91. #define AST_LIST_FIRST(head) ((head)->first)
  92. /*!
  93. \brief Returns the last entry contained in a list.
  94. \param head This is a pointer to the list tail structure
  95. */
  96. #define AST_LIST_LAST(head) ((head)->last)
  97. /*!
  98. \brief Returns the next entry in the list after the given entry.
  99. \param elm This is a pointer to the current entry.
  100. \param field This is the name of the field (declared using AST_LIST_ENTRY())
  101. used to link entries of this list together.
  102. */
  103. #define AST_LIST_NEXT(elm, field) ((elm)->field.next)
  104. /*!
  105. \brief Checks whether the specified list contains any entries.
  106. \param head This is a pointer to the list head structure
  107. Returns non-zero if the list has entries, zero if not.
  108. */
  109. #define AST_LIST_EMPTY(head) (AST_LIST_FIRST(head) == NULL)
  110. /*!
  111. \brief Loops over (traverses) the entries in a list.
  112. \param head This is a pointer to the list head structure
  113. \param var This is the name of the variable that will hold a pointer to the
  114. current list entry on each iteration. It must be declared before calling
  115. this macro.
  116. \param field This is the name of the field (declared using AST_LIST_ENTRY())
  117. used to link entries of this list together.
  118. This macro is use to loop over (traverse) the entries in a list. It uses a
  119. \a for loop, and supplies the enclosed code with a pointer to each list
  120. entry as it loops. It is typically used as follows:
  121. \code
  122. static AST_LIST_HEAD(entry_list, list_entry) entries;
  123. ...
  124. struct list_entry {
  125. ...
  126. AST_LIST_ENTRY(list_entry) list;
  127. }
  128. ...
  129. struct list_entry *current;
  130. ...
  131. AST_LIST_TRAVERSE(&entries, current, list) {
  132. (do something with current here)
  133. }
  134. \endcode
  135. \warning If you modify the forward-link pointer contained in the \a current entry while
  136. inside the loop, the behavior will be unpredictable. At a minimum, the following
  137. macros will modify the forward-link pointer, and should not be used inside
  138. AST_LIST_TRAVERSE() against the entry pointed to by the \a current pointer without
  139. careful consideration of their consequences:
  140. \li AST_LIST_NEXT() (when used as an lvalue)
  141. \li AST_LIST_INSERT_AFTER()
  142. \li AST_LIST_INSERT_HEAD()
  143. \li AST_LIST_INSERT_TAIL()
  144. */
  145. #define AST_LIST_TRAVERSE(head,var,field) \
  146. for((var) = (head)->first; (var); (var) = (var)->field.next)
  147. /*!
  148. \brief Loops safely over (traverses) the entries in a list.
  149. \param head This is a pointer to the list head structure
  150. \param var This is the name of the variable that will hold a pointer to the
  151. current list entry on each iteration. It must be declared before calling
  152. this macro.
  153. \param field This is the name of the field (declared using AST_LIST_ENTRY())
  154. used to link entries of this list together.
  155. This macro is used to safely loop over (traverse) the entries in a list. It
  156. uses a \a for loop, and supplies the enclosed code with a pointer to each list
  157. entry as it loops. It is typically used as follows:
  158. \code
  159. static AST_LIST_HEAD(entry_list, list_entry) entries;
  160. ...
  161. struct list_entry {
  162. ...
  163. AST_LIST_ENTRY(list_entry) list;
  164. }
  165. ...
  166. struct list_entry *current;
  167. ...
  168. AST_LIST_TRAVERSE_SAFE_BEGIN(&entries, current, list) {
  169. (do something with current here)
  170. }
  171. AST_LIST_TRAVERSE_SAFE_END;
  172. \endcode
  173. It differs from AST_LIST_TRAVERSE() in that the code inside the loop can modify
  174. (or even free, after calling AST_LIST_REMOVE_CURRENT()) the entry pointed to by
  175. the \a current pointer without affecting the loop traversal.
  176. */
  177. #define AST_LIST_TRAVERSE_SAFE_BEGIN(head, var, field) { \
  178. typeof((head)->first) __list_next; \
  179. typeof((head)->first) __list_prev = NULL; \
  180. typeof((head)->first) __new_prev = NULL; \
  181. for ((var) = (head)->first, __new_prev = (var), \
  182. __list_next = (var) ? (var)->field.next : NULL; \
  183. (var); \
  184. __list_prev = __new_prev, (var) = __list_next, \
  185. __new_prev = (var), \
  186. __list_next = (var) ? (var)->field.next : NULL \
  187. )
  188. /*!
  189. \brief Removes the \a current entry from a list during a traversal.
  190. \param head This is a pointer to the list head structure
  191. \param field This is the name of the field (declared using AST_LIST_ENTRY())
  192. used to link entries of this list together.
  193. \note This macro can \b only be used inside an AST_LIST_TRAVERSE_SAFE_BEGIN()
  194. block; it is used to unlink the current entry from the list without affecting
  195. the list traversal (and without having to re-traverse the list to modify the
  196. previous entry, if any).
  197. */
  198. #define AST_LIST_REMOVE_CURRENT(head, field) do { \
  199. __new_prev->field.next = NULL; \
  200. __new_prev = __list_prev; \
  201. if (__list_prev) \
  202. __list_prev->field.next = __list_next; \
  203. else \
  204. (head)->first = __list_next; \
  205. if (!__list_next) \
  206. (head)->last = __list_prev; \
  207. } while (0)
  208. /*!
  209. \brief Inserts a list entry before the current entry during a traversal.
  210. \param head This is a pointer to the list head structure
  211. \param elm This is a pointer to the entry to be inserted.
  212. \param field This is the name of the field (declared using AST_LIST_ENTRY())
  213. used to link entries of this list together.
  214. \note This macro can \b only be used inside an AST_LIST_TRAVERSE_SAFE_BEGIN()
  215. block.
  216. */
  217. #define AST_LIST_INSERT_BEFORE_CURRENT(head, elm, field) do { \
  218. if (__list_prev) { \
  219. (elm)->field.next = __list_prev->field.next; \
  220. __list_prev->field.next = elm; \
  221. } else { \
  222. (elm)->field.next = (head)->first; \
  223. (head)->first = (elm); \
  224. } \
  225. __new_prev = (elm); \
  226. } while (0)
  227. /*!
  228. \brief Closes a safe loop traversal block.
  229. */
  230. #define AST_LIST_TRAVERSE_SAFE_END }
  231. /*!
  232. \brief Initializes a list head structure.
  233. \param head This is a pointer to the list head structure
  234. This macro initializes a list head structure by setting the head
  235. entry to \a NULL (empty list). There is no embedded lock handling
  236. with this macro.
  237. */
  238. #define AST_LIST_HEAD_INIT_NOLOCK(head) { \
  239. (head)->first = NULL; \
  240. (head)->last = NULL; \
  241. }
  242. /*!
  243. \brief Inserts a list entry after a given entry.
  244. \param head This is a pointer to the list head structure
  245. \param listelm This is a pointer to the entry after which the new entry should
  246. be inserted.
  247. \param elm This is a pointer to the entry to be inserted.
  248. \param field This is the name of the field (declared using AST_LIST_ENTRY())
  249. used to link entries of this list together.
  250. */
  251. #define AST_LIST_INSERT_AFTER(head, listelm, elm, field) do { \
  252. (elm)->field.next = (listelm)->field.next; \
  253. (listelm)->field.next = (elm); \
  254. if ((head)->last == (listelm)) \
  255. (head)->last = (elm); \
  256. } while (0)
  257. /*!
  258. \brief Inserts a list entry at the head of a list.
  259. \param head This is a pointer to the list head structure
  260. \param elm This is a pointer to the entry to be inserted.
  261. \param field This is the name of the field (declared using AST_LIST_ENTRY())
  262. used to link entries of this list together.
  263. */
  264. #define AST_LIST_INSERT_HEAD(head, elm, field) do { \
  265. (elm)->field.next = (head)->first; \
  266. (head)->first = (elm); \
  267. if (!(head)->last) \
  268. (head)->last = (elm); \
  269. } while (0)
  270. /*!
  271. \brief Appends a list entry to the tail of a list.
  272. \param head This is a pointer to the list head structure
  273. \param elm This is a pointer to the entry to be appended.
  274. \param field This is the name of the field (declared using AST_LIST_ENTRY())
  275. used to link entries of this list together.
  276. Note: The link field in the appended entry is \b not modified, so if it is
  277. actually the head of a list itself, the entire list will be appended
  278. temporarily (until the next AST_LIST_INSERT_TAIL is performed).
  279. */
  280. #define AST_LIST_INSERT_TAIL(head, elm, field) do { \
  281. if (!(head)->first) { \
  282. (head)->first = (elm); \
  283. (head)->last = (elm); \
  284. } else { \
  285. (head)->last->field.next = (elm); \
  286. (head)->last = (elm); \
  287. } \
  288. } while (0)
  289. /*!
  290. \brief Removes and returns the head entry from a list.
  291. \param head This is a pointer to the list head structure
  292. \param field This is the name of the field (declared using AST_LIST_ENTRY())
  293. used to link entries of this list together.
  294. Removes the head entry from the list, and returns a pointer to it.
  295. This macro is safe to call on an empty list.
  296. */
  297. #define AST_LIST_REMOVE_HEAD(head, field) ({ \
  298. typeof((head)->first) cur = (head)->first; \
  299. if (cur) { \
  300. (head)->first = cur->field.next; \
  301. cur->field.next = NULL; \
  302. if ((head)->last == cur) \
  303. (head)->last = NULL; \
  304. } \
  305. cur; \
  306. })
  307. /*!
  308. \brief Removes a specific entry from a list.
  309. \param head This is a pointer to the list head structure
  310. \param elm This is a pointer to the entry to be removed.
  311. \param field This is the name of the field (declared using AST_LIST_ENTRY())
  312. used to link entries of this list together.
  313. \warning The removed entry is \b not freed nor modified in any way.
  314. */
  315. #define AST_LIST_REMOVE(head, elm, field) do { \
  316. if ((head)->first == (elm)) { \
  317. (head)->first = (elm)->field.next; \
  318. if ((head)->last == (elm)) \
  319. (head)->last = NULL; \
  320. } else { \
  321. typeof(elm) curelm = (head)->first; \
  322. while (curelm && (curelm->field.next != (elm))) \
  323. curelm = curelm->field.next; \
  324. if (curelm) { \
  325. curelm->field.next = (elm)->field.next; \
  326. if ((head)->last == (elm)) \
  327. (head)->last = curelm; \
  328. } \
  329. } \
  330. (elm)->field.next = NULL; \
  331. } while (0)
  332. #endif /* _ASTERISK_LINKEDLISTS_H */