libosmocore  1.14.2
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 
245 #define llist_last(head) (head)->prev
246 
254 #define llist_first_entry_or_null(ptr, type, member) \
255  (!llist_empty(ptr) ? llist_first_entry(ptr, type, member) : NULL)
256 
264 #define llist_last_entry_or_null(ptr, type, member) \
265  (!llist_empty(ptr) ? llist_last_entry(ptr, type, member) : NULL)
266 
271 #define llist_for_each(pos, head) \
272  for (pos = (head)->next, prefetch(pos->next); pos != (head); \
273  pos = pos->next, prefetch(pos->next))
274 
284 #define __llist_for_each(pos, head) \
285  for (pos = (head)->next; pos != (head); pos = pos->next)
286 
291 #define llist_for_each_prev(pos, head) \
292  for (pos = (head)->prev, prefetch(pos->prev); pos != (head); \
293  pos = pos->prev, prefetch(pos->prev))
294 
300 #define llist_for_each_safe(pos, n, head) \
301  for (pos = (head)->next, n = pos->next; pos != (head); \
302  pos = n, n = pos->next)
303 
309 #define llist_for_each_entry(pos, head, member) \
310  for (pos = llist_entry((head)->next, typeof(*pos), member), \
311  prefetch(pos->member.next); \
312  &pos->member != (head); \
313  pos = llist_entry(pos->member.next, typeof(*pos), member), \
314  prefetch(pos->member.next))
315 
321 #define llist_for_each_entry_reverse(pos, head, member) \
322  for (pos = llist_entry((head)->prev, typeof(*pos), member), \
323  prefetch(pos->member.prev); \
324  &pos->member != (head); \
325  pos = llist_entry(pos->member.prev, typeof(*pos), member), \
326  prefetch(pos->member.prev))
327 
334 #define llist_for_each_entry_continue(pos, head, member) \
335  for (pos = llist_entry(pos->member.next, typeof(*pos), member), \
336  prefetch(pos->member.next); \
337  &pos->member != (head); \
338  pos = llist_entry(pos->member.next, typeof(*pos), member), \
339  prefetch(pos->member.next))
340 
347 #define llist_for_each_entry_safe(pos, n, head, member) \
348  for (pos = llist_entry((head)->next, typeof(*pos), member), \
349  n = llist_entry(pos->member.next, typeof(*pos), member); \
350  &pos->member != (head); \
351  pos = n, n = llist_entry(n->member.next, typeof(*n), member))
352 
357 #define llist_for_each_rcu(pos, head) \
358  for (pos = (head)->next, prefetch(pos->next); pos != (head); \
359  pos = pos->next, ({ smp_read_barrier_depends(); 0;}), prefetch(pos->next))
360 
361 #define __llist_for_each_rcu(pos, head) \
362  for (pos = (head)->next; pos != (head); \
363  pos = pos->next, ({ smp_read_barrier_depends(); 0;}))
364 
370 #define llist_for_each_safe_rcu(pos, n, head) \
371  for (pos = (head)->next, n = pos->next; pos != (head); \
372  pos = n, ({ smp_read_barrier_depends(); 0;}), n = pos->next)
373 
379 #define llist_for_each_entry_rcu(pos, head, member) \
380  for (pos = llist_entry((head)->next, typeof(*pos), member), \
381  prefetch(pos->member.next); \
382  &pos->member != (head); \
383  pos = llist_entry(pos->member.next, typeof(*pos), member), \
384  ({ smp_read_barrier_depends(); 0;}), \
385  prefetch(pos->member.next))
386 
387 
392 #define llist_for_each_continue_rcu(pos, head) \
393  for ((pos) = (pos)->next, prefetch((pos)->next); (pos) != (head); \
394  (pos) = (pos)->next, ({ smp_read_barrier_depends(); 0;}), prefetch((pos)->next))
395 
403 static inline unsigned int llist_count(const struct llist_head *head)
404 {
405  struct llist_head *entry;
406  unsigned int i = 0;
407  llist_for_each(entry, head)
408  i++;
409  return i;
410 }
411 
412 
413 
420 struct hlist_head {
421  struct hlist_node *first;
422 };
423 
424 struct hlist_node {
425  struct hlist_node *next, **pprev;
426 };
427 
428 #define HLIST_HEAD_INIT { .first = NULL }
429 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
430 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
431 static inline void INIT_HLIST_NODE(struct hlist_node *h)
432 {
433  h->next = NULL;
434  h->pprev = NULL;
435 }
436 
437 #define READ_ONCE(x) x
438 #define WRITE_ONCE(a, b) a = b
439 
448 static inline int hlist_unhashed(const struct hlist_node *h)
449 {
450  return !h->pprev;
451 }
452 
461 static inline int hlist_unhashed_lockless(const struct hlist_node *h)
462 {
463  return !READ_ONCE(h->pprev);
464 }
465 
470 static inline int hlist_empty(const struct hlist_head *h)
471 {
472  return !READ_ONCE(h->first);
473 }
474 
475 static inline void __hlist_del(struct hlist_node *n)
476 {
477  struct hlist_node *next = n->next;
478  struct hlist_node **pprev = n->pprev;
479 
480  WRITE_ONCE(*pprev, next);
481  if (next)
483 }
484 
491 static inline void hlist_del(struct hlist_node *n)
492 {
493  __hlist_del(n);
494  n->next = (struct hlist_node *)LLIST_POISON1;
495  n->pprev = (struct hlist_node **)LLIST_POISON2;
496 }
497 
503 static inline void hlist_del_init(struct hlist_node *n)
504 {
505  if (!hlist_unhashed(n)) {
506  __hlist_del(n);
508  }
509 }
510 
518 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
519 {
520  struct hlist_node *first = h->first;
521  WRITE_ONCE(n->next, first);
522  if (first)
523  WRITE_ONCE(first->pprev, &n->next);
524  WRITE_ONCE(h->first, n);
525  WRITE_ONCE(n->pprev, &h->first);
526 }
527 
532 static inline void hlist_add_before(struct hlist_node *n,
533  struct hlist_node *next)
534 {
535  WRITE_ONCE(n->pprev, next->pprev);
536  WRITE_ONCE(n->next, next);
537  WRITE_ONCE(next->pprev, &n->next);
538  WRITE_ONCE(*(n->pprev), n);
539 }
540 
545 static inline void hlist_add_behind(struct hlist_node *n,
546  struct hlist_node *prev)
547 {
548  WRITE_ONCE(n->next, prev->next);
549  WRITE_ONCE(prev->next, n);
550  WRITE_ONCE(n->pprev, &prev->next);
551 
552  if (n->next)
553  WRITE_ONCE(n->next->pprev, &n->next);
554 }
555 
563 static inline void hlist_add_fake(struct hlist_node *n)
564 {
565  n->pprev = &n->next;
566 }
567 
571 static inline bool hlist_fake(struct hlist_node *h)
572 {
573  return h->pprev == &h->next;
574 }
575 
583 static inline bool
585 {
586  return !n->next && n->pprev == &h->first;
587 }
588 
596 static inline void hlist_move_list(struct hlist_head *old,
597  struct hlist_head *_new)
598 {
599  _new->first = old->first;
600  if (_new->first)
601  _new->first->pprev = &_new->first;
602  old->first = NULL;
603 }
604 
605 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
606 
607 #define hlist_for_each(pos, head) \
608  for (pos = (head)->first; pos ; pos = pos->next)
609 
610 #define hlist_for_each_safe(pos, n, head) \
611  for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
612  pos = n)
613 
614 #define hlist_entry_safe(ptr, type, member) \
615  ({ typeof(ptr) ____ptr = (ptr); \
616  ____ptr ? hlist_entry(____ptr, type, member) : NULL; \
617  })
618 
624 #define hlist_for_each_entry(pos, head, member) \
625  for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
626  pos; \
627  pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
628 
633 #define hlist_for_each_entry_continue(pos, member) \
634  for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member);\
635  pos; \
636  pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
637 
642 #define hlist_for_each_entry_from(pos, member) \
643  for (; pos; \
644  pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
645 
652 #define hlist_for_each_entry_safe(pos, n, head, member) \
653  for (pos = hlist_entry_safe((head)->first, typeof(*pos), member);\
654  pos && ({ n = pos->member.next; 1; }); \
655  pos = hlist_entry_safe(n, typeof(*pos), member))
656 
657 
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
static unsigned int llist_count(const struct llist_head *head)
Count number of llist items by iterating.
Definition: linuxlist.h:403
#define LLIST_POISON1
These are non-NULL pointers that will result in page faults under normal circumstances,...
Definition: linuxlist.h:42
#define LLIST_POISON2
Definition: linuxlist.h:43
static void __hlist_del(struct hlist_node *n)
Definition: linuxlist.h:475
static void hlist_add_behind(struct hlist_node *n, struct hlist_node *prev)
add a new entry after the one specified
Definition: linuxlist.h:545
static void hlist_add_before(struct hlist_node *n, struct hlist_node *next)
add a new entry before the one specified.
Definition: linuxlist.h:532
static void __llist_add(struct llist_head *_new, struct llist_head *prev, struct llist_head *next)
Definition: linuxlist.h:75
static void llist_splice(struct llist_head *llist, struct llist_head *head)
Join two linked lists.
Definition: linuxlist.h:192
static void hlist_del_init(struct hlist_node *n)
Delete the specified hlist_node from its list and initialize.
Definition: linuxlist.h:503
static bool hlist_fake(struct hlist_node *h)
Is this node a fake hlist?.
Definition: linuxlist.h:571
static void llist_del_init(struct llist_head *entry)
Delete a single entry from a linked list and reinitialize it.
Definition: linuxlist.h:138
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
static void INIT_HLIST_NODE(struct hlist_node *h)
Definition: linuxlist.h:431
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
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
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:584
static void hlist_add_fake(struct hlist_node *n)
create a fake hlist consisting of a single headless node.
Definition: linuxlist.h:563
static int hlist_unhashed(const struct hlist_node *h)
Has node been removed from list and reinitialized?.
Definition: linuxlist.h:448
#define WRITE_ONCE(a, b)
Definition: linuxlist.h:438
static void __llist_splice(struct llist_head *llist, struct llist_head *head)
Definition: linuxlist.h:174
#define READ_ONCE(x)
Definition: linuxlist.h:437
static void hlist_del(struct hlist_node *n)
Delete the specified hlist_node from its list.
Definition: linuxlist.h:491
static int llist_empty(const struct llist_head *head)
Test whether a linked list is empty.
Definition: linuxlist.h:169
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
static void llist_del(struct llist_head *entry)
Delete a single entry from a linked list.
Definition: linuxlist.h:128
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:518
#define llist_for_each(pos, head)
Iterate over a linked list.
Definition: linuxlist.h:271
static void prefetch(const void *x)
Definition: linuxlist.h:25
static int hlist_unhashed_lockless(const struct hlist_node *h)
Version of hlist_unhashed for lockless use.
Definition: linuxlist.h:461
static void hlist_move_list(struct hlist_head *old, struct hlist_head *_new)
Move an hlist.
Definition: linuxlist.h:596
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
#define INIT_LLIST_HEAD(ptr)
Initialize a llist_head to point back to itself.
Definition: linuxlist.h:65
static int hlist_empty(const struct hlist_head *h)
Is the specified hlist_head structure an empty hlist?.
Definition: linuxlist.h:470
static void __llist_del(struct llist_head *prev, struct llist_head *next)
Definition: linuxlist.h:116
struct gad_raw_head h
uint32_t x
Definition: jhash.h:0
Double linked lists with a single pointer list head.
Definition: linuxlist.h:420
struct hlist_node * first
Definition: linuxlist.h:421
Definition: linuxlist.h:424
struct hlist_node ** pprev
Definition: linuxlist.h:425
struct hlist_node * next
Definition: linuxlist.h:425
(double) linked list header structure
Definition: linuxlist.h:46
struct llist_head * next
Pointer to next and previous item.
Definition: linuxlist.h:48
struct llist_head * prev
Definition: linuxlist.h:48