libosmocore  1.5.1
Osmocom core library
linuxlist.h
Go to the documentation of this file.
1 
12 #pragma once
13 
18 #include <stddef.h>
19 #include <stdbool.h>
20 
21 #ifndef inline
22 #define inline __inline__
23 #endif
24 
25 static inline void prefetch(const void *x) {;}
26 
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 
54 #define LLIST_HEAD_INIT(name) { &(name), &(name) }
55 
59 #define LLIST_HEAD(name) \
60  struct llist_head name = LLIST_HEAD_INIT(name)
61 
65 #define INIT_LLIST_HEAD(ptr) do { \
66  (ptr)->next = (ptr); (ptr)->prev = (ptr); \
67 } while (0)
68 
69 /*
70  * Insert a new entry between two known consecutive entries.
71  *
72  * This is only for internal llist manipulation where we know
73  * the prev/next entries already!
74  */
75 static inline void __llist_add(struct llist_head *_new,
76  struct llist_head *prev,
77  struct llist_head *next)
78 {
79  next->prev = _new;
80  _new->next = next;
81  _new->prev = prev;
82  prev->next = _new;
83 }
84 
92 static inline void llist_add(struct llist_head *_new, struct llist_head *head)
93 {
94  __llist_add(_new, head, head->next);
95 }
96 
104 static inline void llist_add_tail(struct llist_head *_new, struct llist_head *head)
105 {
106  __llist_add(_new, head->prev, head);
107 }
108 
109 /*
110  * Delete a llist entry by making the prev/next entries
111  * point to each other.
112  *
113  * This is only for internal llist manipulation where we know
114  * the prev/next entries already!
115  */
116 static inline void __llist_del(struct llist_head * prev, struct llist_head * next)
117 {
118  next->prev = prev;
119  prev->next = next;
120 }
121 
128 static inline void llist_del(struct llist_head *entry)
129 {
130  __llist_del(entry->prev, entry->next);
131  entry->next = (struct llist_head *)LLIST_POISON1;
132  entry->prev = (struct llist_head *)LLIST_POISON2;
133 }
134 
138 static inline void llist_del_init(struct llist_head *entry)
139 {
140  __llist_del(entry->prev, entry->next);
141  INIT_LLIST_HEAD(entry);
142 }
143 
148 static inline void llist_move(struct llist_head *llist, struct llist_head *head)
149 {
150  __llist_del(llist->prev, llist->next);
151  llist_add(llist, head);
152 }
153 
158 static inline void llist_move_tail(struct llist_head *llist,
159  struct llist_head *head)
160 {
161  __llist_del(llist->prev, llist->next);
162  llist_add_tail(llist, head);
163 }
164 
169 static inline int llist_empty(const struct llist_head *head)
170 {
171  return head->next == head;
172 }
173 
174 static inline void __llist_splice(struct llist_head *llist,
175  struct llist_head *head)
176 {
177  struct llist_head *first = llist->next;
178  struct llist_head *last = llist->prev;
179  struct llist_head *at = head->next;
180 
181  first->prev = head;
182  head->next = first;
183 
184  last->next = at;
185  at->prev = last;
186 }
187 
192 static inline void llist_splice(struct llist_head *llist, struct llist_head *head)
193 {
194  if (!llist_empty(llist))
195  __llist_splice(llist, head);
196 }
197 
204 static inline void llist_splice_init(struct llist_head *llist,
205  struct llist_head *head)
206 {
207  if (!llist_empty(llist)) {
208  __llist_splice(llist, head);
209  INIT_LLIST_HEAD(llist);
210  }
211 }
212 
218 #define llist_entry(ptr, type, member) \
219  container_of(ptr, type, member)
220 
228 #define llist_first_entry(ptr, type, member) \
229  llist_entry((ptr)->next, type, member)
230 
238 #define llist_last_entry(ptr, type, member) \
239  llist_entry((ptr)->prev, type, member)
240 
248 #define llist_first_entry_or_null(ptr, type, member) \
249  (!llist_empty(ptr) ? llist_first_entry(ptr, type, member) : NULL)
250 
255 #define llist_for_each(pos, head) \
256  for (pos = (head)->next, prefetch(pos->next); pos != (head); \
257  pos = pos->next, prefetch(pos->next))
258 
268 #define __llist_for_each(pos, head) \
269  for (pos = (head)->next; pos != (head); pos = pos->next)
270 
275 #define llist_for_each_prev(pos, head) \
276  for (pos = (head)->prev, prefetch(pos->prev); pos != (head); \
277  pos = pos->prev, prefetch(pos->prev))
278 
284 #define llist_for_each_safe(pos, n, head) \
285  for (pos = (head)->next, n = pos->next; pos != (head); \
286  pos = n, n = pos->next)
287 
293 #define llist_for_each_entry(pos, head, member) \
294  for (pos = llist_entry((head)->next, typeof(*pos), member), \
295  prefetch(pos->member.next); \
296  &pos->member != (head); \
297  pos = llist_entry(pos->member.next, typeof(*pos), member), \
298  prefetch(pos->member.next))
299 
305 #define llist_for_each_entry_reverse(pos, head, member) \
306  for (pos = llist_entry((head)->prev, typeof(*pos), member), \
307  prefetch(pos->member.prev); \
308  &pos->member != (head); \
309  pos = llist_entry(pos->member.prev, typeof(*pos), member), \
310  prefetch(pos->member.prev))
311 
318 #define llist_for_each_entry_continue(pos, head, member) \
319  for (pos = llist_entry(pos->member.next, typeof(*pos), member), \
320  prefetch(pos->member.next); \
321  &pos->member != (head); \
322  pos = llist_entry(pos->member.next, typeof(*pos), member), \
323  prefetch(pos->member.next))
324 
332 #define llist_for_each_entry_safe(pos, n, head, member) \
333  for (pos = llist_entry((head)->next, typeof(*pos), member), \
334  n = llist_entry(pos->member.next, typeof(*pos), member); \
335  &pos->member != (head); \
336  pos = n, n = llist_entry(n->member.next, typeof(*n), member))
337 
342 #define llist_for_each_rcu(pos, head) \
343  for (pos = (head)->next, prefetch(pos->next); pos != (head); \
344  pos = pos->next, ({ smp_read_barrier_depends(); 0;}), prefetch(pos->next))
345 
346 #define __llist_for_each_rcu(pos, head) \
347  for (pos = (head)->next; pos != (head); \
348  pos = pos->next, ({ smp_read_barrier_depends(); 0;}))
349 
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 
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 
377 #define llist_for_each_continue_rcu(pos, head) \
378  for ((pos) = (pos)->next, prefetch((pos)->next); (pos) != (head); \
379  (pos) = (pos)->next, ({ smp_read_barrier_depends(); 0;}), prefetch((pos)->next))
380 
388 static inline unsigned int llist_count(const struct llist_head *head)
389 {
390  struct llist_head *entry;
391  unsigned int i = 0;
392  llist_for_each(entry, head)
393  i++;
394  return i;
395 }
396 
397 
398 
405 struct hlist_head {
406  struct hlist_node *first;
407 };
408 
409 struct hlist_node {
410  struct hlist_node *next, **pprev;
411 };
412 
413 #define HLIST_HEAD_INIT { .first = NULL }
414 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
415 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
416 static inline void INIT_HLIST_NODE(struct hlist_node *h)
417 {
418  h->next = NULL;
419  h->pprev = NULL;
420 }
421 
422 #define READ_ONCE(x) x
423 #define WRITE_ONCE(a, b) a = b
424 
433 static inline int hlist_unhashed(const struct hlist_node *h)
434 {
435  return !h->pprev;
436 }
437 
446 static inline int hlist_unhashed_lockless(const struct hlist_node *h)
447 {
448  return !READ_ONCE(h->pprev);
449 }
450 
455 static inline int hlist_empty(const struct hlist_head *h)
456 {
457  return !READ_ONCE(h->first);
458 }
459 
460 static inline void __hlist_del(struct hlist_node *n)
461 {
462  struct hlist_node *next = n->next;
463  struct hlist_node **pprev = n->pprev;
464 
465  WRITE_ONCE(*pprev, next);
466  if (next)
468 }
469 
476 static inline void hlist_del(struct hlist_node *n)
477 {
478  __hlist_del(n);
479  n->next = (struct hlist_node *)LLIST_POISON1;
480  n->pprev = (struct hlist_node **)LLIST_POISON2;
481 }
482 
488 static inline void hlist_del_init(struct hlist_node *n)
489 {
490  if (!hlist_unhashed(n)) {
491  __hlist_del(n);
493  }
494 }
495 
503 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
504 {
505  struct hlist_node *first = h->first;
506  WRITE_ONCE(n->next, first);
507  if (first)
508  WRITE_ONCE(first->pprev, &n->next);
509  WRITE_ONCE(h->first, n);
510  WRITE_ONCE(n->pprev, &h->first);
511 }
512 
517 static inline void hlist_add_before(struct hlist_node *n,
518  struct hlist_node *next)
519 {
520  WRITE_ONCE(n->pprev, next->pprev);
521  WRITE_ONCE(n->next, next);
522  WRITE_ONCE(next->pprev, &n->next);
523  WRITE_ONCE(*(n->pprev), n);
524 }
525 
530 static inline void hlist_add_behind(struct hlist_node *n,
531  struct hlist_node *prev)
532 {
533  WRITE_ONCE(n->next, prev->next);
534  WRITE_ONCE(prev->next, n);
535  WRITE_ONCE(n->pprev, &prev->next);
536 
537  if (n->next)
538  WRITE_ONCE(n->next->pprev, &n->next);
539 }
540 
548 static inline void hlist_add_fake(struct hlist_node *n)
549 {
550  n->pprev = &n->next;
551 }
552 
556 static inline bool hlist_fake(struct hlist_node *h)
557 {
558  return h->pprev == &h->next;
559 }
560 
568 static inline bool
570 {
571  return !n->next && n->pprev == &h->first;
572 }
573 
581 static inline void hlist_move_list(struct hlist_head *old,
582  struct hlist_head *_new)
583 {
584  _new->first = old->first;
585  if (_new->first)
586  _new->first->pprev = &_new->first;
587  old->first = NULL;
588 }
589 
590 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
591 
592 #define hlist_for_each(pos, head) \
593  for (pos = (head)->first; pos ; pos = pos->next)
594 
595 #define hlist_for_each_safe(pos, n, head) \
596  for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
597  pos = n)
598 
599 #define hlist_entry_safe(ptr, type, member) \
600  ({ typeof(ptr) ____ptr = (ptr); \
601  ____ptr ? hlist_entry(____ptr, type, member) : NULL; \
602  })
603 
609 #define hlist_for_each_entry(pos, head, member) \
610  for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
611  pos; \
612  pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
613 
618 #define hlist_for_each_entry_continue(pos, member) \
619  for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member);\
620  pos; \
621  pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
622 
627 #define hlist_for_each_entry_from(pos, member) \
628  for (; pos; \
629  pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
630 
637 #define hlist_for_each_entry_safe(pos, n, head, member) \
638  for (pos = hlist_entry_safe((head)->first, typeof(*pos), member);\
639  pos && ({ n = pos->member.next; 1; }); \
640  pos = hlist_entry_safe(n, typeof(*pos), member))
641 
642 
hlist_unhashed_lockless
static int hlist_unhashed_lockless(const struct hlist_node *h)
Version of hlist_unhashed for lockless use.
Definition: linuxlist.h:446
LLIST_POISON1
#define LLIST_POISON1
These are non-NULL pointers that will result in page faults under normal circumstances,...
Definition: linuxlist.h:42
hlist_unhashed
static int hlist_unhashed(const struct hlist_node *h)
Has node been removed from list and reinitialized?.
Definition: linuxlist.h:433
n
write Write running configuration to or terminal n Write configuration to the copy running config startup Copy configuration n Copy running config to n Copy running config to startup write Write running configuration to or terminal n Write to terminal n
llist_splice
static void llist_splice(struct llist_head *llist, struct llist_head *head)
Join two linked lists.
Definition: linuxlist.h:192
llist_for_each
#define llist_for_each(pos, head)
Iterate over a linked list.
Definition: linuxlist.h:255
llist_head::next
struct llist_head * next
Pointer to next and previous item.
Definition: linuxlist.h:48
hlist_node
Definition: linuxlist.h:409
hlist_is_singular_node
static bool hlist_is_singular_node(struct hlist_node *n, struct hlist_head *h)
is node the only element of the specified hlist?.
Definition: linuxlist.h:569
hlist_del
static void hlist_del(struct hlist_node *n)
Delete the specified hlist_node from its list.
Definition: linuxlist.h:476
llist_del_init
static void llist_del_init(struct llist_head *entry)
Delete a single entry from a linked list and reinitialize it.
Definition: linuxlist.h:138
INIT_HLIST_NODE
static void INIT_HLIST_NODE(struct hlist_node *h)
Definition: linuxlist.h:416
INIT_LLIST_HEAD
#define INIT_LLIST_HEAD(ptr)
Initialize a llist_head to point back to itself.
Definition: linuxlist.h:65
hlist_add_head
static void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
add a new entry at the beginning of the hlist.
Definition: linuxlist.h:503
llist_del
static void llist_del(struct llist_head *entry)
Delete a single entry from a linked list.
Definition: linuxlist.h:128
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:148
hlist_node::pprev
struct hlist_node ** pprev
Definition: linuxlist.h:410
__llist_del
static void __llist_del(struct llist_head *prev, struct llist_head *next)
Definition: linuxlist.h:116
__llist_add
static void __llist_add(struct llist_head *_new, struct llist_head *prev, struct llist_head *next)
Definition: linuxlist.h:75
hlist_del_init
static void hlist_del_init(struct hlist_node *n)
Delete the specified hlist_node from its list and initialize.
Definition: linuxlist.h:488
hlist_head
Double linked lists with a single pointer list head.
Definition: linuxlist.h:405
hlist_move_list
static void hlist_move_list(struct hlist_head *old, struct hlist_head *_new)
Move an hlist.
Definition: linuxlist.h:581
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:158
h
struct gad_raw_head h
LLIST_POISON2
#define LLIST_POISON2
Definition: linuxlist.h:43
llist_head
(double) linked list header structure
Definition: linuxlist.h:46
llist_count
static unsigned int llist_count(const struct llist_head *head)
Count number of llist items by iterating.
Definition: linuxlist.h:388
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:92
hlist_node::next
struct hlist_node * next
Definition: linuxlist.h:410
hlist_add_fake
static void hlist_add_fake(struct hlist_node *n)
create a fake hlist consisting of a single headless node.
Definition: linuxlist.h:548
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:104
__llist_splice
static void __llist_splice(struct llist_head *llist, struct llist_head *head)
Definition: linuxlist.h:174
__hlist_del
static void __hlist_del(struct hlist_node *n)
Definition: linuxlist.h:460
READ_ONCE
#define READ_ONCE(x)
Definition: linuxlist.h:422
WRITE_ONCE
#define WRITE_ONCE(a, b)
Definition: linuxlist.h:423
llist_empty
static int llist_empty(const struct llist_head *head)
Test whether a linked list is empty.
Definition: linuxlist.h:169
hlist_add_before
static void hlist_add_before(struct hlist_node *n, struct hlist_node *next)
add a new entry before the one specified.
Definition: linuxlist.h:517
llist_head::prev
struct llist_head * prev
Definition: linuxlist.h:48
hlist_empty
static int hlist_empty(const struct hlist_head *h)
Is the specified hlist_head structure an empty hlist?.
Definition: linuxlist.h:455
hlist_add_behind
static void hlist_add_behind(struct hlist_node *n, struct hlist_node *prev)
add a new entry after the one specified
Definition: linuxlist.h:530
prefetch
static void prefetch(const void *x)
Definition: linuxlist.h:25
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:204
hlist_head::first
struct hlist_node * first
Definition: linuxlist.h:406
hlist_fake
static bool hlist_fake(struct hlist_node *h)
Is this node a fake hlist?.
Definition: linuxlist.h:556