a421ad51c85032d5cbec9fd54389cc430b945135
[cascardo/ovs.git] / lib / list.h
1 /*
2  * Copyright (c) 2008, 2009 Nicira Networks.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
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   {
28     struct list *prev;     /* Previous list element. */
29     struct list *next;     /* Next list element. */
30   };
31
32 #define LIST_INITIALIZER(LIST) { LIST, LIST }
33
34 void list_init(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
44 /* List removal. */
45 struct list *list_remove(struct list *);
46 struct list *list_pop_front(struct list *);
47 struct list *list_pop_back(struct list *);
48
49 /* List elements. */
50 struct list *list_front(struct list *);
51 struct list *list_back(struct list *);
52
53 /* List properties. */
54 size_t list_size(const struct list *);
55 bool list_is_empty(const struct list *);
56
57 #define LIST_FOR_EACH(ITER, STRUCT, MEMBER, LIST)                   \
58     for (ITER = CONTAINER_OF((LIST)->next, STRUCT, MEMBER);         \
59          &(ITER)->MEMBER != (LIST);                                 \
60          ITER = CONTAINER_OF((ITER)->MEMBER.next, STRUCT, MEMBER))
61 #define LIST_FOR_EACH_REVERSE(ITER, STRUCT, MEMBER, LIST)           \
62     for (ITER = CONTAINER_OF((LIST)->prev, STRUCT, MEMBER);         \
63          &(ITER)->MEMBER != (LIST);                                 \
64          ITER = CONTAINER_OF((ITER)->MEMBER.prev, STRUCT, MEMBER))
65 #define LIST_FOR_EACH_SAFE(ITER, NEXT, STRUCT, MEMBER, LIST)        \
66     for (ITER = CONTAINER_OF((LIST)->next, STRUCT, MEMBER);         \
67          (NEXT = CONTAINER_OF((ITER)->MEMBER.next, STRUCT, MEMBER), \
68           &(ITER)->MEMBER != (LIST));                               \
69          ITER = NEXT)
70
71 #endif /* list.h */