NinjaFlight
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
list.h
Go to the documentation of this file.
1 /*-
2  * Copyright (c) 2011 Felix Fietkau <nbd@openwrt.org>
3  * Copyright (c) 2010 Isilon Systems, Inc.
4  * Copyright (c) 2010 iX Systems, Inc.
5  * Copyright (c) 2010 Panasas, Inc.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  * notice unmodified, this list of conditions, and the following
13  * disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 #ifndef _LINUX_LIST_H_
30 #define _LINUX_LIST_H_
31 
32 #include <stddef.h>
33 #include <stdbool.h>
34 
35 #include "utils.h"
36 
37 #define prefetch(x)
38 
39 struct list_head {
40  struct list_head *next;
41  struct list_head *prev;
42 };
43 
44 #define LIST_HEAD_INIT(name) { &(name), &(name) }
45 #undef LIST_HEAD
46 #define LIST_HEAD(name) struct list_head name = LIST_HEAD_INIT(name)
47 
48 static inline void
49 INIT_LIST_HEAD(struct list_head *list)
50 {
51  list->next = list->prev = list;
52 }
53 
54 static inline bool
55 list_empty(const struct list_head *head)
56 {
57  return (head->next == head);
58 }
59 
60 static inline bool
61 list_is_first(const struct list_head *list,
62  const struct list_head *head)
63 {
64  return list->prev == head;
65 }
66 
67 static inline bool
68 list_is_last(const struct list_head *list,
69  const struct list_head *head)
70 {
71  return list->next == head;
72 }
73 
74 static inline void
75 _list_del(struct list_head *entry)
76 {
77  entry->next->prev = entry->prev;
78  entry->prev->next = entry->next;
79 }
80 
81 static inline void
82 list_del(struct list_head *entry)
83 {
84  _list_del(entry);
85  entry->next = entry->prev = NULL;
86 }
87 
88 static inline void
89 _list_add(struct list_head *_new, struct list_head *prev,
90  struct list_head *next)
91 {
92 
93  next->prev = _new;
94  _new->next = next;
95  _new->prev = prev;
96  prev->next = _new;
97 }
98 
99 static inline void
100 list_del_init(struct list_head *entry)
101 {
102  _list_del(entry);
103  INIT_LIST_HEAD(entry);
104 }
105 
106 #define list_entry(ptr, type, field) container_of(ptr, type, field)
107 #define list_first_entry(ptr, type, field) list_entry((ptr)->next, type, field)
108 #define list_last_entry(ptr, type, field) list_entry((ptr)->prev, type, field)
109 
110 #define list_for_each(p, head) \
111  for (p = (head)->next; p != (head); p = p->next)
112 
113 #define list_for_each_safe(p, n, head) \
114  for (p = (head)->next, n = p->next; p != (head); p = n, n = p->next)
115 
116 #define list_for_each_entry(p, h, field) \
117  for (p = list_first_entry(h, typeof(*p), field); &p->field != (h); \
118  p = list_entry(p->field.next, typeof(*p), field))
119 
120 #define list_for_each_entry_safe(p, n, h, field) \
121  for (p = list_first_entry(h, typeof(*p), field), \
122  n = list_entry(p->field.next, typeof(*p), field); &p->field != (h);\
123  p = n, n = list_entry(n->field.next, typeof(*n), field))
124 
125 #define list_for_each_entry_reverse(p, h, field) \
126  for (p = list_last_entry(h, typeof(*p), field); &p->field != (h); \
127  p = list_entry(p->field.prev, typeof(*p), field))
128 
129 #define list_for_each_prev(p, h) for (p = (h)->prev; p != (h); p = p->prev)
130 #define list_for_each_prev_safe(p, n, h) for (p = (h)->prev, n = p->prev; p != (h); p = n, n = p->prev)
131 
132 static inline void
133 list_add(struct list_head *_new, struct list_head *head)
134 {
135  _list_add(_new, head, head->next);
136 }
137 
138 static inline void
139 list_add_tail(struct list_head *_new, struct list_head *head)
140 {
141  _list_add(_new, head->prev, head);
142 }
143 
144 static inline void
145 list_move(struct list_head *list, struct list_head *head)
146 {
147  _list_del(list);
148  list_add(list, head);
149 }
150 
151 static inline void
152 list_move_tail(struct list_head *entry, struct list_head *head)
153 {
154  _list_del(entry);
155  list_add_tail(entry, head);
156 }
157 
158 static inline void
159 _list_splice(const struct list_head *list, struct list_head *prev,
160  struct list_head *next)
161 {
162  struct list_head *first;
163  struct list_head *last;
164 
165  if (list_empty(list))
166  return;
167 
168  first = list->next;
169  last = list->prev;
170  first->prev = prev;
171  prev->next = first;
172  last->next = next;
173  next->prev = last;
174 }
175 
176 static inline void
177 list_splice(const struct list_head *list, struct list_head *head)
178 {
179  _list_splice(list, head, head->next);
180 }
181 
182 static inline void
183 list_splice_tail(struct list_head *list, struct list_head *head)
184 {
185  _list_splice(list, head->prev, head);
186 }
187 
188 static inline void
189 list_splice_init(struct list_head *list, struct list_head *head)
190 {
191  _list_splice(list, head, head->next);
192  INIT_LIST_HEAD(list);
193 }
194 
195 static inline void
196 list_splice_tail_init(struct list_head *list, struct list_head *head)
197 {
198  _list_splice(list, head->prev, head);
199  INIT_LIST_HEAD(list);
200 }
201 
202 #endif /* _LINUX_LIST_H_ */
struct list_head * next
Definition: list.h:40
Definition: list.h:39
struct list_head * prev
Definition: list.h:41