Setting tag sliver-openvswitch-2.2.90-1
[sliver-openvswitch.git] / lib / list.h
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2013 Nicira, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef LIST_H
17 #define LIST_H 1
18
19 /* Doubly linked list. */
20
21 #include <stdbool.h>
22 #include <stddef.h>
23 #include "util.h"
24
25 /* Doubly linked list head or element. */
26 struct list {
27     struct list *prev;     /* Previous list element. */
28     struct list *next;     /* Next list element. */
29 };
30
31 #define LIST_INITIALIZER(LIST) { LIST, LIST }
32
33 void list_init(struct list *);
34 void list_poison(struct list *);
35
36 /* List insertion. */
37 void list_insert(struct list *, struct list *);
38 void list_splice(struct list *before, struct list *first, struct list *last);
39 void list_push_front(struct list *, struct list *);
40 void list_push_back(struct list *, struct list *);
41 void list_replace(struct list *, const struct list *);
42 void list_moved(struct list *);
43 void list_move(struct list *dst, struct list *src);
44
45 /* List removal. */
46 struct list *list_remove(struct list *);
47 struct list *list_pop_front(struct list *);
48 struct list *list_pop_back(struct list *);
49
50 /* List elements. */
51 struct list *list_front(const struct list *);
52 struct list *list_back(const struct list *);
53
54 /* List properties. */
55 size_t list_size(const struct list *);
56 bool list_is_empty(const struct list *);
57 bool list_is_singleton(const struct list *);
58 bool list_is_short(const struct list *);
59
60 #define LIST_FOR_EACH(ITER, MEMBER, LIST)                               \
61     for (ASSIGN_CONTAINER(ITER, (LIST)->next, MEMBER);                  \
62          &(ITER)->MEMBER != (LIST);                                     \
63          ASSIGN_CONTAINER(ITER, (ITER)->MEMBER.next, MEMBER))
64 #define LIST_FOR_EACH_CONTINUE(ITER, MEMBER, LIST)                      \
65     for (ASSIGN_CONTAINER(ITER, (ITER)->MEMBER.next, MEMBER);           \
66          &(ITER)->MEMBER != (LIST);                                     \
67          ASSIGN_CONTAINER(ITER, (ITER)->MEMBER.next, MEMBER))
68 #define LIST_FOR_EACH_REVERSE(ITER, MEMBER, LIST)                       \
69     for (ASSIGN_CONTAINER(ITER, (LIST)->prev, MEMBER);                  \
70          &(ITER)->MEMBER != (LIST);                                     \
71          ASSIGN_CONTAINER(ITER, (ITER)->MEMBER.prev, MEMBER))
72 #define LIST_FOR_EACH_REVERSE_CONTINUE(ITER, MEMBER, LIST)              \
73     for (ASSIGN_CONTAINER(ITER, (ITER)->MEMBER.prev, MEMBER);           \
74          &(ITER)->MEMBER != (LIST);                                     \
75          ASSIGN_CONTAINER(ITER, (ITER)->MEMBER.prev, MEMBER))
76 #define LIST_FOR_EACH_SAFE(ITER, NEXT, MEMBER, LIST)               \
77     for (ASSIGN_CONTAINER(ITER, (LIST)->next, MEMBER);             \
78          (&(ITER)->MEMBER != (LIST)                                \
79           ? ASSIGN_CONTAINER(NEXT, (ITER)->MEMBER.next, MEMBER), 1 \
80           : 0);                                                    \
81          (ITER) = (NEXT))
82
83 #endif /* list.h */