72720111ec6d8c0f3f6218d69d4d8651cbdf9179
[openvswitch] / include / list.h
1 #ifndef LIST_H
2 #define LIST_H 1
3
4 /* Doubly linked list. */
5
6 #include <stdbool.h>
7 #include <stddef.h>
8 #include "util.h"
9
10 /* Doubly linked list head or element. */
11 struct list
12   {
13     struct list *prev;     /* Previous list element. */
14     struct list *next;     /* Next list element. */
15   };
16
17 #define LIST_INITIALIZER(LIST) { LIST, LIST }
18
19 void list_init(struct list *);
20
21 /* List insertion. */
22 void list_insert(struct list *, struct list *);
23 void list_splice(struct list *before, struct list *first, struct list *last);
24 void list_push_front(struct list *, struct list *);
25 void list_push_back(struct list *, struct list *);
26 void list_replace(struct list *, const struct list *);
27
28 /* List removal. */
29 struct list *list_remove(struct list *);
30 struct list *list_pop_front(struct list *);
31 struct list *list_pop_back(struct list *);
32
33 /* List elements. */
34 struct list *list_front(struct list *);
35 struct list *list_back(struct list *);
36
37 /* List properties. */
38 size_t list_size(const struct list *);
39 bool list_is_empty(const struct list *);
40
41 #define LIST_ELEM__(ELEM, STRUCT, MEMBER, LIST)                 \
42         (ELEM != LIST ? CONTAINER_OF(ELEM, STRUCT, MEMBER) : NULL)
43 #define LIST_FOR_EACH(ITER, STRUCT, MEMBER, LIST)                   \
44     for (ITER = LIST_ELEM__((LIST)->next, STRUCT, MEMBER, LIST);    \
45          ITER != NULL;                                              \
46          ITER = LIST_ELEM__((ITER)->MEMBER.next, STRUCT, MEMBER, LIST))
47 #define LIST_FOR_EACH_SAFE(ITER, NEXT, STRUCT, MEMBER, LIST)        \
48     for (ITER = LIST_ELEM__((LIST)->next, STRUCT, MEMBER, LIST);    \
49          (ITER != NULL                                              \
50           ? (NEXT = LIST_ELEM__((ITER)->MEMBER.next,                \
51                                 STRUCT, MEMBER, LIST), 1)           \
52           : 0);                                                     \
53          ITER = NEXT)
54
55 #endif /* list.h */