libosmocore  0.9.6.256-2956
Osmocom core library
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Modules Pages
linuxlist.h
Go to the documentation of this file.
1 #pragma once
2 
19 #include <stddef.h>
20 
21 #ifndef inline
22 #define inline __inline__
23 #endif
24 
25 static inline void prefetch(const void *x) {;}
26 
33 #define container_of(ptr, type, member) ({ \
34  const typeof( ((type *)0)->member ) *__mptr = (ptr); \
35  (type *)( (char *)__mptr - offsetof(type, member) );})
36 
37 
43 #define LLIST_POISON1 ((void *) 0x00100100)
44 #define LLIST_POISON2 ((void *) 0x00200200)
45 
47 struct llist_head {
49  struct llist_head *next, *prev;
50 };
51 
52 #define LLIST_HEAD_INIT(name) { &(name), &(name) }
53 
59 #define LLIST_HEAD(name) \
60  struct llist_head name = LLIST_HEAD_INIT(name)
61 
63 #define INIT_LLIST_HEAD(ptr) do { \
64  (ptr)->next = (ptr); (ptr)->prev = (ptr); \
65 } while (0)
66 
72 static inline void __llist_add(struct llist_head *_new,
73  struct llist_head *prev,
74  struct llist_head *next)
75 {
76  next->prev = _new;
77  _new->next = next;
78  _new->prev = prev;
79  prev->next = _new;
80 }
81 
89 static inline void llist_add(struct llist_head *_new, struct llist_head *head)
90 {
91  __llist_add(_new, head, head->next);
92 }
93 
101 static inline void llist_add_tail(struct llist_head *_new, struct llist_head *head)
102 {
103  __llist_add(_new, head->prev, head);
104 }
105 
106 /*
107  * Delete a llist entry by making the prev/next entries
108  * point to each other.
109  *
110  * This is only for internal llist manipulation where we know
111  * the prev/next entries already!
112  */
113 static inline void __llist_del(struct llist_head * prev, struct llist_head * next)
114 {
115  next->prev = prev;
116  prev->next = next;
117 }
118 
125 static inline void llist_del(struct llist_head *entry)
126 {
127  __llist_del(entry->prev, entry->next);
128  entry->next = (struct llist_head *)LLIST_POISON1;
129  entry->prev = (struct llist_head *)LLIST_POISON2;
130 }
131 
135 static inline void llist_del_init(struct llist_head *entry)
136 {
137  __llist_del(entry->prev, entry->next);
138  INIT_LLIST_HEAD(entry);
139 }
140 
145 static inline void llist_move(struct llist_head *llist, struct llist_head *head)
146 {
147  __llist_del(llist->prev, llist->next);
148  llist_add(llist, head);
149 }
150 
155 static inline void llist_move_tail(struct llist_head *llist,
156  struct llist_head *head)
157 {
158  __llist_del(llist->prev, llist->next);
159  llist_add_tail(llist, head);
160 }
161 
166 static inline int llist_empty(const struct llist_head *head)
167 {
168  return head->next == head;
169 }
170 
171 static inline void __llist_splice(struct llist_head *llist,
172  struct llist_head *head)
173 {
174  struct llist_head *first = llist->next;
175  struct llist_head *last = llist->prev;
176  struct llist_head *at = head->next;
177 
178  first->prev = head;
179  head->next = first;
180 
181  last->next = at;
182  at->prev = last;
183 }
184 
189 static inline void llist_splice(struct llist_head *llist, struct llist_head *head)
190 {
191  if (!llist_empty(llist))
192  __llist_splice(llist, head);
193 }
194 
201 static inline void llist_splice_init(struct llist_head *llist,
202  struct llist_head *head)
203 {
204  if (!llist_empty(llist)) {
205  __llist_splice(llist, head);
206  INIT_LLIST_HEAD(llist);
207  }
208 }
209 
215 #define llist_entry(ptr, type, member) \
216  container_of(ptr, type, member)
217 
225 #define llist_first_entry(ptr, type, member) \
226  llist_entry((ptr)->next, type, member)
227 
235 #define llist_last_entry(ptr, type, member) \
236  llist_entry((ptr)->prev, type, member)
237 
245 #define llist_first_entry_or_null(ptr, type, member) \
246  (!llist_empty(ptr) ? llist_first_entry(ptr, type, member) : NULL)
247 
252 #define llist_for_each(pos, head) \
253  for (pos = (head)->next, prefetch(pos->next); pos != (head); \
254  pos = pos->next, prefetch(pos->next))
255 
265 #define __llist_for_each(pos, head) \
266  for (pos = (head)->next; pos != (head); pos = pos->next)
267 
272 #define llist_for_each_prev(pos, head) \
273  for (pos = (head)->prev, prefetch(pos->prev); pos != (head); \
274  pos = pos->prev, prefetch(pos->prev))
275 
281 #define llist_for_each_safe(pos, n, head) \
282  for (pos = (head)->next, n = pos->next; pos != (head); \
283  pos = n, n = pos->next)
284 
290 #define llist_for_each_entry(pos, head, member) \
291  for (pos = llist_entry((head)->next, typeof(*pos), member), \
292  prefetch(pos->member.next); \
293  &pos->member != (head); \
294  pos = llist_entry(pos->member.next, typeof(*pos), member), \
295  prefetch(pos->member.next))
296 
302 #define llist_for_each_entry_reverse(pos, head, member) \
303  for (pos = llist_entry((head)->prev, typeof(*pos), member), \
304  prefetch(pos->member.prev); \
305  &pos->member != (head); \
306  pos = llist_entry(pos->member.prev, typeof(*pos), member), \
307  prefetch(pos->member.prev))
308 
315 #define llist_for_each_entry_continue(pos, head, member) \
316  for (pos = llist_entry(pos->member.next, typeof(*pos), member), \
317  prefetch(pos->member.next); \
318  &pos->member != (head); \
319  pos = llist_entry(pos->member.next, typeof(*pos), member), \
320  prefetch(pos->member.next))
321 
329 #define llist_for_each_entry_safe(pos, n, head, member) \
330  for (pos = llist_entry((head)->next, typeof(*pos), member), \
331  n = llist_entry(pos->member.next, typeof(*pos), member); \
332  &pos->member != (head); \
333  pos = n, n = llist_entry(n->member.next, typeof(*n), member))
334 
340 #define llist_for_each_rcu(pos, head) \
341  for (pos = (head)->next, prefetch(pos->next); pos != (head); \
342  pos = pos->next, ({ smp_read_barrier_depends(); 0;}), prefetch(pos->next))
343 
344 #define __llist_for_each_rcu(pos, head) \
345  for (pos = (head)->next; pos != (head); \
346  pos = pos->next, ({ smp_read_barrier_depends(); 0;}))
347 
355 #define llist_for_each_safe_rcu(pos, n, head) \
356  for (pos = (head)->next, n = pos->next; pos != (head); \
357  pos = n, ({ smp_read_barrier_depends(); 0;}), n = pos->next)
358 
365 #define llist_for_each_entry_rcu(pos, head, member) \
366  for (pos = llist_entry((head)->next, typeof(*pos), member), \
367  prefetch(pos->member.next); \
368  &pos->member != (head); \
369  pos = llist_entry(pos->member.next, typeof(*pos), member), \
370  ({ smp_read_barrier_depends(); 0;}), \
371  prefetch(pos->member.next))
372 
373 
380 #define llist_for_each_continue_rcu(pos, head) \
381  for ((pos) = (pos)->next, prefetch((pos)->next); (pos) != (head); \
382  (pos) = (pos)->next, ({ smp_read_barrier_depends(); 0;}), prefetch((pos)->next))
383 
391 static inline unsigned int llist_count(struct llist_head *head)
392 {
393  struct llist_head *entry;
394  unsigned int i = 0;
395  llist_for_each(entry, head)
396  i++;
397  return i;
398 }
399 
struct llist_head * next
Pointer to next and previous item.
Definition: linuxlist.h:49
static void llist_del(struct llist_head *entry)
Delete entry from linked list.
Definition: linuxlist.h:125
static unsigned int llist_count(struct llist_head *head)
count nr of llist items by iterating.
Definition: linuxlist.h:391
static void llist_splice(struct llist_head *llist, struct llist_head *head)
Join two llists.
Definition: linuxlist.h:189
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:89
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:72
#define LLIST_POISON1
Definition: linuxlist.h:43
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:145
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:155
(double) linked list header structure
Definition: linuxlist.h:47
#define INIT_LLIST_HEAD(ptr)
initialize a llist_head to point back to self
Definition: linuxlist.h:63
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:101
static int llist_empty(const struct llist_head *head)
Test whether a linked list is empty.
Definition: linuxlist.h:166
static void llist_splice_init(struct llist_head *llist, struct llist_head *head)
join two llists and reinitialise the emptied llist.
Definition: linuxlist.h:201
#define llist_for_each(pos, head)
Iterate over a linked list.
Definition: linuxlist.h:252
static void llist_del_init(struct llist_head *entry)
Delete entry from linked list and reinitialize it.
Definition: linuxlist.h:135