aboutsummaryrefslogtreecommitdiffstats
path: root/include/asterisk/linkedlists.h
blob: 01067d5f07ee4dcba6e75a7d6a15cd0185f1bf0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#ifndef ASTERISK_LINKEDLISTS_H
#define ASTERISK_LINKEDLISTS_H

#include <pthread.h>
#include <asterisk/lock.h>

#define AST_LIST_LOCK(head)						\
	ast_mutex_lock(&head->lock) 
	
#define AST_LIST_UNLOCK(head) 						\
	ast_mutex_unlock(&head->lock)

#define AST_LIST_HEAD(name, type)					\
struct name {								\
	struct type *first;						\
	ast_mutex_t lock;						\
}

#define AST_LIST_HEAD_INITIALIZER(head)					\
	{ NULL, AST_MUTEX_INITIALIZER }
	
#define AST_LIST_HEAD_SET(head,entry) do {				\
	(head)->first=(entry);						\
	ast_pthread_mutex_init(&(head)->lock,NULL);				\
} while (0)

#define AST_LIST_ENTRY(type)						\
struct {								\
	struct type *next;						\
}
 
#define	AST_LIST_FIRST(head)	((head)->first)

#define AST_LIST_NEXT(elm, field)	((elm)->field.next)

#define	AST_LIST_EMPTY(head)	(AST_LIST_FIRST(head) == NULL)

#define AST_LIST_TRAVERSE(head,var,field) 				\
	for((var) = (head)->first; (var); (var) = (var)->field.next)

#define AST_LIST_HEAD_INIT(head) {						\
	(head)->first = NULL;						\
	ast_pthread_mutex_init(&(head)->lock,NULL);				\
}

#define AST_LIST_INSERT_AFTER(listelm, elm, field) do {		\
	(elm)->field.next = (listelm)->field.next;			\
	(listelm)->field.next = (elm);				\
} while (0)

#define AST_LIST_INSERT_HEAD(head, elm, field) do {			\
		(elm)->field.next = (head)->first;			\
		(head)->first = (elm);					\
} while (0)

#define AST_LIST_INSERT_TAIL(head, elm, type, field) do {             \
      struct type *curelm = (head)->first;                            \
      if(!curelm) {                                                   \
              AST_LIST_INSERT_HEAD(head, elm, field);                 \
      } else {                                                        \
              while ( curelm->field.next!=NULL ) {                    \
                      curelm=curelm->field.next;                      \
              }                                                       \
              AST_LIST_INSERT_AFTER(curelm,elm,field);                \
      }                                                               \
} while (0)


#define AST_LIST_REMOVE_HEAD(head, field) do {					\
		(head)->first = (head)->first->field.next;			\
	} while (0)

#define AST_LIST_REMOVE(head, elm, type, field) do {			\
	if ((head)->first == (elm)) {					\
		AST_LIST_REMOVE_HEAD((head), field);			\
	}								\
	else {								\
		struct type *curelm = (head)->first;			\
		while( curelm->field.next != (elm) )			\
			curelm = curelm->field.next;			\
		curelm->field.next =					\
		    curelm->field.next->field.next;			\
	}								\
} while (0)

#endif