libosmocore  1.14.2.4-2a26b.202609021800
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 
30 #define container_of(ptr, type, member) ({ \
31  const typeof( ((type *)0)->member ) *__mptr = (ptr); \
32  (type *)( (char *)__mptr - offsetof(type, member) );})
33 
34 
40 #define LLIST_POISON1 ((void *) 0x00100100)
41 #define LLIST_POISON2 ((void *) 0x00200200)
42 
44 struct llist_head {
46  struct llist_head *next, *prev;
47 };
48 
52 #define LLIST_HEAD_INIT(name) { &(name), &(name) }
53 
57 #define LLIST_HEAD(name) \
58  struct llist_head name = LLIST_HEAD_INIT(name)
59 
63 #define INIT_LLIST_HEAD(ptr) do { \
64  (ptr)->next = (ptr); (ptr)->prev = (ptr); \
65 } while (0)
66 
67 /*
68  * Insert a new entry between two known consecutive entries.
69  *
70  * This is only for internal llist manipulation where we know
71  * the prev/next entries already!
72  */
73 static inline void __llist_add(struct llist_head *_new,
74  struct llist_head *prev,
75  struct llist_head *next)
76 {
77  next->prev = _new;
78  _new->next = next;
79  _new->prev = prev;
80  prev->next = _new;
81 }
82 
90 static inline void llist_add(struct llist_head *_new, struct llist_head *head)
91 {
92  __llist_add(_new, head, head->next);
93 }
94 
102 static inline void llist_add_tail(struct llist_head *_new, struct llist_head *head)
103 {
104  __llist_add(_new, head->prev, head);
105 }
106 
107 /*
108  * Delete a llist entry by making the prev/next entries
109  * point to each other.
110  *
111  * This is only for internal llist manipulation where we know
112  * the prev/next entries already!
113  */
114 static inline void __llist_del(struct llist_head * prev, struct llist_head * next)
115 {
116  next->prev = prev;
117  prev->next = next;
118 }
119 
126 static inline void llist_del(struct llist_head *entry)
127 {
128  __llist_del(entry->prev, entry->next);
129  entry->next = (struct llist_head *)LLIST_POISON1;
130  entry->prev = (struct llist_head *)LLIST_POISON2;
131 }
132 
136 static inline void llist_del_init(struct llist_head *entry)
137 {
138  __llist_del(entry->prev, entry->next);
139  INIT_LLIST_HEAD(entry);
140 }
141 
146 static inline void llist_move(struct llist_head *llist, struct llist_head *head)
147 {
148  __llist_del(llist->prev, llist->next);
149  llist_add(llist, head);
150 }
151 
156 static inline void llist_move_tail(struct llist_head *llist,
157  struct llist_head *head)
158 {
159  __llist_del(llist->prev, llist->next);
160  llist_add_tail(llist, head);
161 }
162 
167 static inline int llist_empty(const struct llist_head *head)
168 {
169  return head->next == head;
170 }
171 
172 static inline void __llist_splice(struct llist_head *llist,
173  struct llist_head *head)
174 {
175  struct llist_head *first = llist->next;
176  struct llist_head *last = llist->prev;
177  struct llist_head *at = head->next;
178 
179  first->prev = head;
180  head->next = first;
181 
182  last->next = at;
183  at->prev = last;
184 }
185 
190 static inline void llist_splice(struct llist_head *llist, struct llist_head *head)
191 {
192  if (!llist_empty(llist))
193  __llist_splice(llist, head);
194 }
195 
202 static inline void llist_splice_init(struct llist_head *llist,
203  struct llist_head *head)
204 {
205  if (!llist_empty(llist)) {
206  __llist_splice(llist, head);
207  INIT_LLIST_HEAD(llist);
208  }
209 }
210 
216 #define llist_entry(ptr, type, member) \
217  container_of(ptr, type, member)
218 
230 #define __llist_member(pos, member) \
231  ((struct llist_head *)((char *)(pos) + offsetof(typeof(*(pos)), member)))
232 
240 #define llist_first_entry(ptr, type, member) \
241  llist_entry((ptr)->next, type, member)
242 
250 #define llist_last_entry(ptr, type, member) \
251  llist_entry((ptr)->prev, type, member)
252 
257 #define llist_last(head) (head)->prev
258 
266 #define llist_first_entry_or_null(ptr, type, member) \
267  (!llist_empty(ptr) ? llist_first_entry(ptr, type, member) : NULL)
268 
276 #define llist_last_entry_or_null(ptr, type, member) \
277  (!llist_empty(ptr) ? llist_last_entry(ptr, type, member) : NULL)
278 
283 #define llist_for_each(pos, head) \
284  for (pos = (head)->next; pos != (head); pos = pos->next)
285 
294 #define __llist_for_each(pos, head) \
295  for (pos = (head)->next; pos != (head); pos = pos->next)
296 
301 #define llist_for_each_prev(pos, head) \
302  for (pos = (head)->prev; pos != (head); pos = pos->prev)
303 
309 #define llist_for_each_safe(pos, n, head) \
310  for (pos = (head)->next, n = pos->next; pos != (head); \
311  pos = n, n = pos->next)
312 
318 #define llist_for_each_entry(pos, head, member) \
319  for (pos = llist_entry((head)->next, typeof(*pos), member); \
320  __llist_member(pos, member) != (head); \
321  pos = llist_entry(__llist_member(pos, member)->next, typeof(*pos), member))
322 
328 #define llist_for_each_entry_reverse(pos, head, member) \
329  for (pos = llist_entry((head)->prev, typeof(*pos), member); \
330  __llist_member(pos, member) != (head); \
331  pos = llist_entry(__llist_member(pos, member)->prev, typeof(*pos), member))
332 
339 #define llist_for_each_entry_continue(pos, head, member) \
340  for (pos = llist_entry(pos->member.next, typeof(*pos), member); \
341  __llist_member(pos, member) != (head); \
342  pos = llist_entry(__llist_member(pos, member)->next, typeof(*pos), member))
343 
350 #define llist_for_each_entry_safe(pos, n, head, member) \
351  for (pos = llist_entry((head)->next, typeof(*pos), member), \
352  n = llist_entry(__llist_member(pos, member)->next, typeof(*pos), member); \
353  __llist_member(pos, member) != (head); \
354  pos = n, n = llist_entry(__llist_member(n, member)->next, typeof(*n), member))
355 
360 #define llist_for_each_rcu(pos, head) \
361  for (pos = (head)->next; pos != (head); \
362  pos = pos->next, ({ smp_read_barrier_depends(); 0;}))
363 
364 #define __llist_for_each_rcu(pos, head) \
365  for (pos = (head)->next; pos != (head); \
366  pos = pos->next, ({ smp_read_barrier_depends(); 0;}))
367 
373 #define llist_for_each_safe_rcu(pos, n, head) \
374  for (pos = (head)->next, n = pos->next; pos != (head); \
375  pos = n, ({ smp_read_barrier_depends(); 0;}), n = pos->next)
376 
382 #define llist_for_each_entry_rcu(pos, head, member) \
383  for (pos = llist_entry((head)->next, typeof(*pos), member); \
384  __llist_member(pos, member) != (head); \
385  pos = llist_entry(__llist_member(pos, member)->next, typeof(*pos), member), \
386  ({ smp_read_barrier_depends(); 0;}))
387 
388 
393 #define llist_for_each_continue_rcu(pos, head) \
394  for ((pos) = (pos)->next; (pos) != (head); \
395  (pos) = (pos)->next, ({ smp_read_barrier_depends(); 0;}))
396 
404 static inline unsigned int llist_count(const struct llist_head *head)
405 {
406  struct llist_head *entry;
407  unsigned int i = 0;
408  llist_for_each(entry, head)
409  i++;
410  return i;
411 }
412 
413 
414 
421 struct hlist_head {
422  struct hlist_node *first;
423 };
424 
425 struct hlist_node {
426  struct hlist_node *next, **pprev;
427 };
428 
429 #define HLIST_HEAD_INIT { .first = NULL }
430 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
431 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
432 static inline void INIT_HLIST_NODE(struct hlist_node *h)
433 {
434  h->next = NULL;
435  h->pprev = NULL;
436 }
437 
438 #define READ_ONCE(x) x
439 #define WRITE_ONCE(a, b) a = b
440 
449 static inline int hlist_unhashed(const struct hlist_node *h)
450 {
451  return !h->pprev;
452 }
453 
462 static inline int hlist_unhashed_lockless(const struct hlist_node *h)
463 {
464  return !READ_ONCE(h->pprev);
465 }
466 
471 static inline int hlist_empty(const struct hlist_head *h)
472 {
473  return !READ_ONCE(h->first);
474 }
475 
476 static inline void __hlist_del(struct hlist_node *n)
477 {
478  struct hlist_node *next = n->next;
479  struct hlist_node **pprev = n->pprev;
480 
481  WRITE_ONCE(*pprev, next);
482  if (next)
484 }
485 
492 static inline void hlist_del(struct hlist_node *n)
493 {
494  __hlist_del(n);
495  n->next = (struct hlist_node *)LLIST_POISON1;
496  n->pprev = (struct hlist_node **)LLIST_POISON2;
497 }
498 
504 static inline void hlist_del_init(struct hlist_node *n)
505 {
506  if (!hlist_unhashed(n)) {
507  __hlist_del(n);
509  }
510 }
511 
519 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
520 {
521  struct hlist_node *first = h->first;
522  WRITE_ONCE(n->next, first);
523  if (first)
524  WRITE_ONCE(first->pprev, &n->next);
525  WRITE_ONCE(h->first, n);
526  WRITE_ONCE(n->pprev, &h->first);
527 }
528 
533 static inline void hlist_add_before(struct hlist_node *n,
534  struct hlist_node *next)
535 {
536  WRITE_ONCE(n->pprev, next->pprev);
537  WRITE_ONCE(n->next, next);
538  WRITE_ONCE(next->pprev, &n->next);
539  WRITE_ONCE(*(n->pprev), n);
540 }
541 
546 static inline void hlist_add_behind(struct hlist_node *n,
547  struct hlist_node *prev)
548 {
549  WRITE_ONCE(n->next, prev->next);
550  WRITE_ONCE(prev->next, n);
551  WRITE_ONCE(n->pprev, &prev->next);
552 
553  if (n->next)
554  WRITE_ONCE(n->next->pprev, &n->next);
555 }
556 
564 static inline void hlist_add_fake(struct hlist_node *n)
565 {
566  n->pprev = &n->next;
567 }
568 
572 static inline bool hlist_fake(struct hlist_node *h)
573 {
574  return h->pprev == &h->next;
575 }
576 
584 static inline bool
586 {
587  return !n->next && n->pprev == &h->first;
588 }
589 
597 static inline void hlist_move_list(struct hlist_head *old,
598  struct hlist_head *_new)
599 {
600  _new->first = old->first;
601  if (_new->first)
602  _new->first->pprev = &_new->first;
603  old->first = NULL;
604 }
605 
606 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
607 
608 #define hlist_for_each(pos, head) \
609  for (pos = (head)->first; pos ; pos = pos->next)
610 
611 #define hlist_for_each_safe(pos, n, head) \
612  for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
613  pos = n)
614 
615 #define hlist_entry_safe(ptr, type, member) \
616  ({ typeof(ptr) ____ptr = (ptr); \
617  ____ptr ? hlist_entry(____ptr, type, member) : NULL; \
618  })
619 
625 #define hlist_for_each_entry(pos, head, member) \
626  for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
627  pos; \
628  pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
629 
634 #define hlist_for_each_entry_continue(pos, member) \
635  for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member);\
636  pos; \
637  pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
638 
643 #define hlist_for_each_entry_from(pos, member) \
644  for (; pos; \
645  pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
646 
653 #define hlist_for_each_entry_safe(pos, n, head, member) \
654  for (pos = hlist_entry_safe((head)->first, typeof(*pos), member);\
655  pos && ({ n = pos->member.next; 1; }); \
656  pos = hlist_entry_safe(n, typeof(*pos), member))
657 
658 
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:404
#define LLIST_POISON1
These are non-NULL pointers that will result in page faults under normal circumstances,...
Definition: linuxlist.h:40
#define LLIST_POISON2
Definition: linuxlist.h:41
static void __hlist_del(struct hlist_node *n)
Definition: linuxlist.h:476
static void hlist_add_behind(struct hlist_node *n, struct hlist_node *prev)
add a new entry after the one specified
Definition: linuxlist.h:546
static void hlist_add_before(struct hlist_node *n, struct hlist_node *next)
add a new entry before the one specified.
Definition: linuxlist.h:533
static void __llist_add(struct llist_head *_new, struct llist_head *prev, struct llist_head *next)
Definition: linuxlist.h:73
static void llist_splice(struct llist_head *llist, struct llist_head *head)
Join two linked lists.
Definition: linuxlist.h:190
static void hlist_del_init(struct hlist_node *n)
Delete the specified hlist_node from its list and initialize.
Definition: linuxlist.h:504
static bool hlist_fake(struct hlist_node *h)
Is this node a fake hlist?.
Definition: linuxlist.h:572
static void llist_del_init(struct llist_head *entry)
Delete a single entry from a linked list and reinitialize it.
Definition: linuxlist.h:136
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:156
static void INIT_HLIST_NODE(struct hlist_node *h)
Definition: linuxlist.h:432
static void llist_splice_init(struct llist_head *llist, struct llist_head *head)
Join two llists and reinitialise the emptied llist.
Definition: linuxlist.h:202
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:90
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:585
static void hlist_add_fake(struct hlist_node *n)
create a fake hlist consisting of a single headless node.
Definition: linuxlist.h:564
static int hlist_unhashed(const struct hlist_node *h)
Has node been removed from list and reinitialized?.
Definition: linuxlist.h:449
#define WRITE_ONCE(a, b)
Definition: linuxlist.h:439
static void __llist_splice(struct llist_head *llist, struct llist_head *head)
Definition: linuxlist.h:172
#define READ_ONCE(x)
Definition: linuxlist.h:438
static void hlist_del(struct hlist_node *n)
Delete the specified hlist_node from its list.
Definition: linuxlist.h:492
static int llist_empty(const struct llist_head *head)
Test whether a linked list is empty.
Definition: linuxlist.h:167
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:146
static void llist_del(struct llist_head *entry)
Delete a single entry from a linked list.
Definition: linuxlist.h:126
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:519
#define llist_for_each(pos, head)
Iterate over a linked list.
Definition: linuxlist.h:283
static int hlist_unhashed_lockless(const struct hlist_node *h)
Version of hlist_unhashed for lockless use.
Definition: linuxlist.h:462
static void hlist_move_list(struct hlist_head *old, struct hlist_head *_new)
Move an hlist.
Definition: linuxlist.h:597
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:102
#define INIT_LLIST_HEAD(ptr)
Initialize a llist_head to point back to itself.
Definition: linuxlist.h:63
static int hlist_empty(const struct hlist_head *h)
Is the specified hlist_head structure an empty hlist?.
Definition: linuxlist.h:471
static void __llist_del(struct llist_head *prev, struct llist_head *next)
Definition: linuxlist.h:114
struct gad_raw_head h
Double linked lists with a single pointer list head.
Definition: linuxlist.h:421
struct hlist_node * first
Definition: linuxlist.h:422
Definition: linuxlist.h:425
struct hlist_node ** pprev
Definition: linuxlist.h:426
struct hlist_node * next
Definition: linuxlist.h:426
(double) linked list header structure
Definition: linuxlist.h:44
struct llist_head * next
Pointer to next and previous item.
Definition: linuxlist.h:46
struct llist_head * prev
Definition: linuxlist.h:46