2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
7 * Kevin P. Fleming <kpfleming@digium.com>
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.
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.
20 #ifndef ASTERISK_LINKEDLISTS_H
21 #define ASTERISK_LINKEDLISTS_H
23 #include "asterisk/lock.h"
27 \brief A set of macros to manage forward-linked lists.
31 \brief Attempts to lock a list.
32 \param head This is a pointer to the list head structure
34 This macro attempts to place an exclusive lock in the
35 list head structure pointed to by head.
36 Returns non-zero on success, 0 on failure
38 #define AST_LIST_LOCK(head) \
39 ast_mutex_lock(&(head)->lock)
42 \brief Attempts to unlock a list.
43 \param head This is a pointer to the list head structure
45 This macro attempts to remove an exclusive lock from the
46 list head structure pointed to by head. If the list
47 was not locked by this thread, this macro has no effect.
49 #define AST_LIST_UNLOCK(head) \
50 ast_mutex_unlock(&(head)->lock)
53 \brief Defines a structure to be used to hold a list of specified type.
54 \param name This will be the name of the defined structure.
55 \param type This is the type of each list entry.
57 This macro creates a structure definition that can be used
58 to hold a list of the entries of type \a type. It does not actually
59 declare (allocate) a structure; to do that, either follow this
60 macro with the desired name of the instance you wish to declare,
61 or use the specified \a name to declare instances elsewhere.
65 static AST_LIST_HEAD(entry_list, entry) entries;
68 This would define \c struct \c entry_list, and declare an instance of it named
69 \a entries, all intended to hold a list of type \c struct \c entry.
71 #define AST_LIST_HEAD(name, type) \
79 \brief Defines a structure to be used to hold a list of specified type (with no lock).
80 \param name This will be the name of the defined structure.
81 \param type This is the type of each list entry.
83 This macro creates a structure definition that can be used
84 to hold a list of the entries of type \a type. It does not actually
85 declare (allocate) a structure; to do that, either follow this
86 macro with the desired name of the instance you wish to declare,
87 or use the specified \a name to declare instances elsewhere.
91 static AST_LIST_HEAD_NOLOCK(entry_list, entry) entries;
94 This would define \c struct \c entry_list, and declare an instance of it named
95 \a entries, all intended to hold a list of type \c struct \c entry.
97 #define AST_LIST_HEAD_NOLOCK(name, type) \
104 \brief Defines a structure to be used to hold a list of specified type, statically initialized.
105 \param name This will be the name of the defined structure.
106 \param type This is the type of each list entry.
108 This macro creates a structure definition that can be used
109 to hold a list of the entries of type \a type, and allocates an instance
110 of it, initialized to be empty.
114 static AST_LIST_HEAD_STATIC(entry_list, entry);
117 This would define \c struct \c entry_list, intended to hold a list of
118 type \c struct \c entry.
120 #define AST_LIST_HEAD_STATIC(name, type) \
122 struct type *first; \
128 .lock = AST_MUTEX_INIT_VALUE, \
132 \brief Initializes a list head structure with a specified first entry.
133 \param head This is a pointer to the list head structure
134 \param entry pointer to the list entry that will become the head of the list
136 This macro initializes a list head structure by setting the head
137 entry to the supplied value and recreating the embedded lock.
139 #define AST_LIST_HEAD_SET(head, entry) do { \
140 (head)->first = (entry); \
141 (head)->last = (entry); \
142 ast_mutex_init(&(head)->lock); \
146 \brief Initializes a list head structure with a specified first entry.
147 \param head This is a pointer to the list head structure
148 \param entry pointer to the list entry that will become the head of the list
150 This macro initializes a list head structure by setting the head
151 entry to the supplied value.
153 #define AST_LIST_HEAD_SET_NOLOCK(head, entry) do { \
154 (head)->first = (entry); \
155 (head)->last = (entry); \
159 \brief Declare a forward link structure inside a list entry.
160 \param type This is the type of each list entry.
162 This macro declares a structure to be used to link list entries together.
163 It must be used inside the definition of the structure named in
169 AST_LIST_ENTRY(list_entry) list;
173 The field name \a list here is arbitrary, and can be anything you wish.
175 #define AST_LIST_ENTRY(type) \
181 \brief Returns the first entry contained in a list.
182 \param head This is a pointer to the list head structure
184 #define AST_LIST_FIRST(head) ((head)->first)
187 \brief Returns the next entry in the list after the given entry.
188 \param elm This is a pointer to the current entry.
189 \param field This is the name of the field (declared using AST_LIST_ENTRY())
190 used to link entries of this list together.
192 #define AST_LIST_NEXT(elm, field) ((elm)->field.next)
195 \brief Checks whether the specified list contains any entries.
196 \param head This is a pointer to the list head structure
198 Returns non-zero if the list has entries, zero if not.
200 #define AST_LIST_EMPTY(head) (AST_LIST_FIRST(head) == NULL)
203 \brief Loops over (traverses) the entries in a list.
204 \param head This is a pointer to the list head structure
205 \param var This is the name of the variable that will hold a pointer to the
206 current list entry on each iteration. It must be declared before calling
208 \param field This is the name of the field (declared using AST_LIST_ENTRY())
209 used to link entries of this list together.
211 This macro is use to loop over (traverse) the entries in a list. It uses a
212 \a for loop, and supplies the enclosed code with a pointer to each list
213 entry as it loops. It is typically used as follows:
215 static AST_LIST_HEAD(entry_list, list_entry) entries;
219 AST_LIST_ENTRY(list_entry) list;
222 struct list_entry *current;
224 AST_LIST_TRAVERSE(&entries, current, list) {
225 (do something with current here)
228 \warning If you modify the forward-link pointer contained in the \a current entry while
229 inside the loop, the behavior will be unpredictable. At a minimum, the following
230 macros will modify the forward-link pointer, and should not be used inside
231 AST_LIST_TRAVERSE() against the entry pointed to by the \a current pointer without
232 careful consideration of their consequences:
233 \li AST_LIST_NEXT() (when used as an lvalue)
234 \li AST_LIST_INSERT_AFTER()
235 \li AST_LIST_INSERT_HEAD()
236 \li AST_LIST_INSERT_TAIL()
238 #define AST_LIST_TRAVERSE(head,var,field) \
239 for((var) = (head)->first; (var); (var) = (var)->field.next)
242 \brief Loops safely over (traverses) the entries in a list.
243 \param head This is a pointer to the list head structure
244 \param var This is the name of the variable that will hold a pointer to the
245 current list entry on each iteration. It must be declared before calling
247 \param field This is the name of the field (declared using AST_LIST_ENTRY())
248 used to link entries of this list together.
250 This macro is used to safely loop over (traverse) the entries in a list. It
251 uses a \a for loop, and supplies the enclosed code with a pointer to each list
252 entry as it loops. It is typically used as follows:
255 static AST_LIST_HEAD(entry_list, list_entry) entries;
259 AST_LIST_ENTRY(list_entry) list;
262 struct list_entry *current;
264 AST_LIST_TRAVERSE_SAFE_BEGIN(&entries, current, list) {
265 (do something with current here)
267 AST_LIST_TRAVERSE_SAFE_END;
270 It differs from AST_LIST_TRAVERSE() in that the code inside the loop can modify
271 (or even free, after calling AST_LIST_REMOVE_CURRENT()) the entry pointed to by
272 the \a current pointer without affecting the loop traversal.
274 #define AST_LIST_TRAVERSE_SAFE_BEGIN(head, var, field) { \
275 typeof((head)->first) __list_next; \
276 typeof((head)->first) __list_prev = NULL; \
277 for ((var) = (head)->first, __list_next = (var) ? (var)->field.next : NULL; \
279 __list_prev = (var), (var) = __list_next, \
280 __list_next = (var) ? (var)->field.next : NULL \
284 \brief Removes the \a current entry from a list during a traversal.
285 \param head This is a pointer to the list head structure
286 \param field This is the name of the field (declared using AST_LIST_ENTRY())
287 used to link entries of this list together.
289 \note This macro can \b only be used inside an AST_LIST_TRAVERSE_SAFE_BEGIN()
290 block; it is used to unlink the current entry from the list without affecting
291 the list traversal (and without having to re-traverse the list to modify the
292 previous entry, if any).
294 #define AST_LIST_REMOVE_CURRENT(head, field) \
296 __list_prev->field.next = __list_next; \
298 (head)->first = __list_next; \
300 (head)->last = __list_prev;
303 \brief Closes a safe loop traversal block.
305 #define AST_LIST_TRAVERSE_SAFE_END }
308 \brief Initializes a list head structure.
309 \param head This is a pointer to the list head structure
311 This macro initializes a list head structure by setting the head
312 entry to \a NULL (empty list) and recreating the embedded lock.
314 #define AST_LIST_HEAD_INIT(head) { \
315 (head)->first = NULL; \
316 (head)->last = NULL; \
317 ast_mutex_init(&(head)->lock); \
321 \brief Destroys a list head structure.
322 \param head This is a pointer to the list head structure
324 This macro destroys a list head structure by setting the head
325 entry to \a NULL (empty list) and destroying the embedded lock.
326 It does not free the structure from memory.
328 #define AST_LIST_HEAD_DESTROY(head) { \
329 (head)->first = NULL; \
330 (head)->last = NULL; \
331 ast_mutex_destroy(&(head)->lock); \
335 \brief Initializes a list head structure.
336 \param head This is a pointer to the list head structure
338 This macro initializes a list head structure by setting the head
339 entry to \a NULL (empty list) and recreating the embedded lock.
341 #define AST_LIST_HEAD_INIT_NOLOCK(head) { \
342 (head)->first = NULL; \
343 (head)->last = NULL; \
347 \brief Inserts a list entry after a given entry.
348 \param head This is a pointer to the list head structure
349 \param listelm This is a pointer to the entry after which the new entry should
351 \param elm This is a pointer to the entry to be inserted.
352 \param field This is the name of the field (declared using AST_LIST_ENTRY())
353 used to link entries of this list together.
355 #define AST_LIST_INSERT_AFTER(head, listelm, elm, field) do { \
356 (elm)->field.next = (listelm)->field.next; \
357 (listelm)->field.next = (elm); \
358 if ((head)->last == (listelm)) \
359 (head)->last = (elm); \
363 \brief Inserts a list entry at the head of a list.
364 \param head This is a pointer to the list head structure
365 \param elm This is a pointer to the entry to be inserted.
366 \param field This is the name of the field (declared using AST_LIST_ENTRY())
367 used to link entries of this list together.
369 #define AST_LIST_INSERT_HEAD(head, elm, field) do { \
370 (elm)->field.next = (head)->first; \
371 (head)->first = (elm); \
373 (head)->last = (elm); \
377 \brief Appends a list entry to the tail of a list.
378 \param head This is a pointer to the list head structure
379 \param elm This is a pointer to the entry to be appended.
380 \param field This is the name of the field (declared using AST_LIST_ENTRY())
381 used to link entries of this list together.
383 Note: The link field in the appended entry is \b not modified, so if it is
384 actually the head of a list itself, the entire list will be appended.
386 #define AST_LIST_INSERT_TAIL(head, elm, field) do { \
387 if (!(head)->first) { \
388 (head)->first = (elm); \
389 (head)->last = (elm); \
391 (head)->last->field.next = (elm); \
392 (head)->last = (elm); \
397 \brief Removes and returns the head entry from a list.
398 \param head This is a pointer to the list head structure
399 \param field This is the name of the field (declared using AST_LIST_ENTRY())
400 used to link entries of this list together.
402 Removes the head entry from the list, and returns a pointer to it.
403 This macro is safe to call on an empty list.
405 #define AST_LIST_REMOVE_HEAD(head, field) ({ \
406 typeof((head)->first) cur = (head)->first; \
408 (head)->first = cur->field.next; \
409 cur->field.next = NULL; \
410 if ((head)->last == cur) \
411 (head)->last = NULL; \
417 \brief Removes a specific entry from a list.
418 \param head This is a pointer to the list head structure
419 \param elm This is a pointer to the entry to be removed.
420 \param field This is the name of the field (declared using AST_LIST_ENTRY())
421 used to link entries of this list together.
422 \warning The removed entry is \b not freed nor modified in any way.
424 #define AST_LIST_REMOVE(head, elm, field) do { \
425 if ((head)->first == (elm)) { \
426 (head)->first = (elm)->field.next; \
427 if ((head)->last == (elm)) \
428 (head)->last = NULL; \
430 typeof(elm) curelm = (head)->first; \
431 while (curelm->field.next != (elm)) \
432 curelm = curelm->field.next; \
433 curelm->field.next = (elm)->field.next; \
434 if ((head)->last == curelm->field.next) \
435 (head)->last = curelm; \
439 #endif /* _ASTERISK_LINKEDLISTS_H */