libosmocore  0.12.0
Osmocom core library
linuxlist.h
Go to the documentation of this file.
1 
12 #pragma once
13 
18 #include <stddef.h>
19 
20 #ifndef inline
21 #define inline __inline__
22 #endif
23 
24 static inline void prefetch(const void *x) {;}
25 
32 #define container_of(ptr, type, member) ({ \
33  const typeof( ((type *)0)->member ) *__mptr = (ptr); \
34  (type *)( (char *)__mptr - offsetof(type, member) );})
35 
36 
42 #define LLIST_POISON1 ((void *) 0x00100100)
43 #define LLIST_POISON2 ((void *) 0x00200200)
44 
46 struct llist_head {
48  struct llist_head *next, *prev;
49 };
50 
51 #define LLIST_HEAD_INIT(name) { &(name), &(name) }
52 
58 #define LLIST_HEAD(name) \
59  struct llist_head name = LLIST_HEAD_INIT(name)
60 
62 #define INIT_LLIST_HEAD(ptr) do { \
63  (ptr)->next = (ptr); (ptr)->prev = (ptr); \
64 } while (0)
65 
71 static inline void __llist_add(struct llist_head *_new,
72  struct llist_head *prev,
73  struct llist_head *next)
74 {
75  next->prev = _new;
76  _new->next = next;
77  _new->prev = prev;
78  prev->next = _new;
79 }
80 
88 static inline void llist_add(struct llist_head *_new, struct llist_head *head)
89 {
90  __llist_add(_new, head, head->next);
91 }
92 
100 static inline void llist_add_tail(struct llist_head *_new, struct llist_head *head)
101 {
102  __llist_add(_new, head->prev, head);
103 }
104 
105 /*
106  * Delete a llist entry by making the prev/next entries
107  * point to each other.
108  *
109  * This is only for internal llist manipulation where we know
110  * the prev/next entries already!
111  */
112 static inline void __llist_del(struct llist_head * prev, struct llist_head * next)
113 {
114  next->prev = prev;
115  prev->next = next;
116 }
117 
124 static inline void llist_del(struct llist_head *entry)
125 {
126  __llist_del(entry->prev, entry->next);
127  entry->next = (struct llist_head *)LLIST_POISON1;
128  entry->prev = (struct llist_head *)LLIST_POISON2;
129 }
130 
134 static inline void llist_del_init(struct llist_head *entry)
135 {
136  __llist_del(entry->prev, entry->next);
137  INIT_LLIST_HEAD(entry);
138 }
139 
144 static inline void llist_move(struct llist_head *llist, struct llist_head *head)
145 {
146  __llist_del(llist->prev, llist->next);
147  llist_add(llist, head);
148 }
149 
154 static inline void llist_move_tail(struct llist_head *llist,
155  struct llist_head *head)
156 {
157  __llist_del(llist->prev, llist->next);
158  llist_add_tail(llist, head);
159 }
160 
165 static inline int llist_empty(const struct llist_head *head)
166 {
167  return head->next == head;
168 }
169 
170 static inline void __llist_splice(struct llist_head *llist,
171  struct llist_head *head)
172 {
173  struct llist_head *first = llist->next;
174  struct llist_head *last = llist->prev;
175  struct llist_head *at = head->next;
176 
177  first->prev = head;
178  head->next = first;
179 
180  last->next = at;
181  at->prev = last;
182 }
183 
188 static inline void llist_splice(struct llist_head *llist, struct llist_head *head)
189 {
190  if (!llist_empty(llist))
191  __llist_splice(llist, head);
192 }
193 
200 static inline void llist_splice_init(struct llist_head *llist,
201  struct llist_head *head)
202 {
203  if (!llist_empty(llist)) {
204  __llist_splice(llist, head);
205  INIT_LLIST_HEAD(llist);
206  }
207 }
208 
214 #define llist_entry(ptr, type, member) \
215  container_of(ptr, type, member)
216 
224 #define llist_first_entry(ptr, type, member) \
225  llist_entry((ptr)->next, type, member)
226 
234 #define llist_last_entry(ptr, type, member) \
235  llist_entry((ptr)->prev, type, member)
236 
244 #define llist_first_entry_or_null(ptr, type, member) \
245  (!llist_empty(ptr) ? llist_first_entry(ptr, type, member) : NULL)
246 
251 #define llist_for_each(pos, head) \
252  for (pos = (head)->next, prefetch(pos->next); pos != (head); \
253  pos = pos->next, prefetch(pos->next))
254 
264 #define __llist_for_each(pos, head) \
265  for (pos = (head)->next; pos != (head); pos = pos->next)
266 
271 #define llist_for_each_prev(pos, head) \
272  for (pos = (head)->prev, prefetch(pos->prev); pos != (head); \
273  pos = pos->prev, prefetch(pos->prev))
274 
280 #define llist_for_each_safe(pos, n, head) \
281  for (pos = (head)->next, n = pos->next; pos != (head); \
282  pos = n, n = pos->next)
283 
289 #define llist_for_each_entry(pos, head, member) \
290  for (pos = llist_entry((head)->next, typeof(*pos), member), \
291  prefetch(pos->member.next); \
292  &pos->member != (head); \
293  pos = llist_entry(pos->member.next, typeof(*pos), member), \
294  prefetch(pos->member.next))
295 
301 #define llist_for_each_entry_reverse(pos, head, member) \
302  for (pos = llist_entry((head)->prev, typeof(*pos), member), \
303  prefetch(pos->member.prev); \
304  &pos->member != (head); \
305  pos = llist_entry(pos->member.prev, typeof(*pos), member), \
306  prefetch(pos->member.prev))
307 
314 #define llist_for_each_entry_continue(pos, head, member) \
315  for (pos = llist_entry(pos->member.next, typeof(*pos), member), \
316  prefetch(pos->member.next); \
317  &pos->member != (head); \
318  pos = llist_entry(pos->member.next, typeof(*pos), member), \
319  prefetch(pos->member.next))
320 
328 #define llist_for_each_entry_safe(pos, n, head, member) \
329  for (pos = llist_entry((head)->next, typeof(*pos), member), \
330  n = llist_entry(pos->member.next, typeof(*pos), member); \
331  &pos->member != (head); \
332  pos = n, n = llist_entry(n->member.next, typeof(*n), member))
333 
339 #define llist_for_each_rcu(pos, head) \
340  for (pos = (head)->next, prefetch(pos->next); pos != (head); \
341  pos = pos->next, ({ smp_read_barrier_depends(); 0;}), prefetch(pos->next))
342 
343 #define __llist_for_each_rcu(pos, head) \
344  for (pos = (head)->next; pos != (head); \
345  pos = pos->next, ({ smp_read_barrier_depends(); 0;}))
346 
354 #define llist_for_each_safe_rcu(pos, n, head) \
355  for (pos = (head)->next, n = pos->next; pos != (head); \
356  pos = n, ({ smp_read_barrier_depends(); 0;}), n = pos->next)
357 
364 #define llist_for_each_entry_rcu(pos, head, member) \
365  for (pos = llist_entry((head)->next, typeof(*pos), member), \
366  prefetch(pos->member.next); \
367  &pos->member != (head); \
368  pos = llist_entry(pos->member.next, typeof(*pos), member), \
369  ({ smp_read_barrier_depends(); 0;}), \
370  prefetch(pos->member.next))
371 
372 
379 #define llist_for_each_continue_rcu(pos, head) \
380  for ((pos) = (pos)->next, prefetch((pos)->next); (pos) != (head); \
381  (pos) = (pos)->next, ({ smp_read_barrier_depends(); 0;}), prefetch((pos)->next))
382 
390 static inline unsigned int llist_count(struct llist_head *head)
391 {
392  struct llist_head *entry;
393  unsigned int i = 0;
394  llist_for_each(entry, head)
395  i++;
396  return i;
397 }
398 
LLIST_POISON1
#define LLIST_POISON1
These are non-NULL pointers that will result in page faults under normal circumstances,...
Definition: linuxlist.h:42
llist_splice
static void llist_splice(struct llist_head *llist, struct llist_head *head)
Join two llists.
Definition: linuxlist.h:188
llist_for_each
#define llist_for_each(pos, head)
Iterate over a linked list.
Definition: linuxlist.h:251
llist_head::next
struct llist_head * next
Pointer to next and previous item.
Definition: linuxlist.h:48
llist_del_init
static void llist_del_init(struct llist_head *entry)
Delete entry from linked list and reinitialize it.
Definition: linuxlist.h:134
INIT_LLIST_HEAD
#define INIT_LLIST_HEAD(ptr)
initialize a llist_head to point back to self
Definition: linuxlist.h:62
llist_del
static void llist_del(struct llist_head *entry)
Delete entry from linked list.
Definition: linuxlist.h:124
llist_move
static void llist_move(struct llist_head *llist, struct llist_head *head)
Delete from one llist and add as another's head.
Definition: linuxlist.h:144
__llist_del
static void __llist_del(struct llist_head *prev, struct llist_head *next)
Definition: linuxlist.h:112
__llist_add
static void __llist_add(struct llist_head *_new, struct llist_head *prev, struct llist_head *next)
Insert a new entry between two known consecutive entries.
Definition: linuxlist.h:71
llist_move_tail
static void llist_move_tail(struct llist_head *llist, struct llist_head *head)
Delete from one llist and add as another's tail.
Definition: linuxlist.h:154
LLIST_POISON2
#define LLIST_POISON2
Definition: linuxlist.h:43
llist_head
(double) linked list header structure
Definition: linuxlist.h:46
llist_add
static void llist_add(struct llist_head *_new, struct llist_head *head)
add a new entry into a linked list (at head)
Definition: linuxlist.h:88
llist_add_tail
static void llist_add_tail(struct llist_head *_new, struct llist_head *head)
add a new entry into a linked list (at tail)
Definition: linuxlist.h:100
__llist_splice
static void __llist_splice(struct llist_head *llist, struct llist_head *head)
Definition: linuxlist.h:170
llist_empty
static int llist_empty(const struct llist_head *head)
Test whether a linked list is empty.
Definition: linuxlist.h:165
llist_head::prev
struct llist_head * prev
Definition: linuxlist.h:48
llist_count
static unsigned int llist_count(struct llist_head *head)
count nr of llist items by iterating.
Definition: linuxlist.h:390
prefetch
static void prefetch(const void *x)
Definition: linuxlist.h:24
llist_splice_init
static void llist_splice_init(struct llist_head *llist, struct llist_head *head)
join two llists and reinitialise the emptied llist.
Definition: linuxlist.h:200