Fix little macro (bug #3100)
[asterisk/asterisk.git] / include / asterisk / linkedlists.h
1 #ifndef ASTERISK_LINKEDLISTS_H
2 #define ASTERISK_LINKEDLISTS_H
3
4 #include <asterisk/lock.h>
5
6 #define AST_LIST_LOCK(head)                                             \
7         ast_mutex_lock(&(head)->lock) 
8         
9 #define AST_LIST_UNLOCK(head)                                           \
10         ast_mutex_unlock(&(head)->lock)
11
12 #define AST_LIST_HEAD(name, type)                                       \
13 struct name {                                                           \
14         struct type *first;                                             \
15         ast_mutex_t lock;                                               \
16 }
17
18 #define AST_LIST_HEAD_SET(head,entry) do {                              \
19         (head)->first=(entry);                                          \
20         ast_pthread_mutex_init(&(head)->lock,NULL);                             \
21 } while (0)
22
23 #define AST_LIST_ENTRY(type)                                            \
24 struct {                                                                \
25         struct type *next;                                              \
26 }
27  
28 #define AST_LIST_FIRST(head)    ((head)->first)
29
30 #define AST_LIST_NEXT(elm, field)       ((elm)->field.next)
31
32 #define AST_LIST_EMPTY(head)    (AST_LIST_FIRST(head) == NULL)
33
34 #define AST_LIST_TRAVERSE(head,var,field)                               \
35         for((var) = (head)->first; (var); (var) = (var)->field.next)
36
37 #define AST_LIST_HEAD_INIT(head) {                                              \
38         (head)->first = NULL;                                           \
39         ast_pthread_mutex_init(&(head)->lock,NULL);                             \
40 }
41
42 #define AST_LIST_INSERT_AFTER(listelm, elm, field) do {         \
43         (elm)->field.next = (listelm)->field.next;                      \
44         (listelm)->field.next = (elm);                          \
45 } while (0)
46
47 #define AST_LIST_INSERT_HEAD(head, elm, field) do {                     \
48                 (elm)->field.next = (head)->first;                      \
49                 (head)->first = (elm);                                  \
50 } while (0)
51
52 #define AST_LIST_INSERT_TAIL(head, elm, type, field) do {             \
53       struct type *curelm = (head)->first;                            \
54       if(!curelm) {                                                   \
55               AST_LIST_INSERT_HEAD(head, elm, field);                 \
56       } else {                                                        \
57               while ( curelm->field.next!=NULL ) {                    \
58                       curelm=curelm->field.next;                      \
59               }                                                       \
60               AST_LIST_INSERT_AFTER(curelm,elm,field);                \
61       }                                                               \
62 } while (0)
63
64
65 #define AST_LIST_REMOVE_HEAD(head, field) do {                                  \
66                 (head)->first = (head)->first->field.next;                      \
67         } while (0)
68
69 #define AST_LIST_REMOVE(head, elm, type, field) do {                    \
70         if ((head)->first == (elm)) {                                   \
71                 AST_LIST_REMOVE_HEAD((head), field);                    \
72         }                                                               \
73         else {                                                          \
74                 struct type *curelm = (head)->first;                    \
75                 while( curelm->field.next != (elm) )                    \
76                         curelm = curelm->field.next;                    \
77                 curelm->field.next =                                    \
78                     curelm->field.next->field.next;                     \
79         }                                                               \
80 } while (0)
81
82 #endif