libosmocore  1.9.0.196-9975.202404302026
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 
261 #define llist_for_each(pos, head) \
262  for (pos = (head)->next, prefetch(pos->next); pos != (head); \
263  pos = pos->next, prefetch(pos->next))
264 
274 #define __llist_for_each(pos, head) \
275  for (pos = (head)->next; pos != (head); pos = pos->next)
276 
281 #define llist_for_each_prev(pos, head) \
282  for (pos = (head)->prev, prefetch(pos->prev); pos != (head); \
283  pos = pos->prev, prefetch(pos->prev))
284 
290 #define llist_for_each_safe(pos, n, head) \
291  for (pos = (head)->next, n = pos->next; pos != (head); \
292  pos = n, n = pos->next)
293 
299 #define llist_for_each_entry(pos, head, member) \
300  for (pos = llist_entry((head)->next, typeof(*pos), member), \
301  prefetch(pos->member.next); \
302  &pos->member != (head); \
303  pos = llist_entry(pos->member.next, typeof(*pos), member), \
304  prefetch(pos->member.next))
305 
311 #define llist_for_each_entry_reverse(pos, head, member) \
312  for (pos = llist_entry((head)->prev, typeof(*pos), member), \
313  prefetch(pos->member.prev); \
314  &pos->member != (head); \
315  pos = llist_entry(pos->member.prev, typeof(*pos), member), \
316  prefetch(pos->member.prev))
317 
324 #define llist_for_each_entry_continue(pos, head, member) \
325  for (pos = llist_entry(pos->member.next, typeof(*pos), member), \
326  prefetch(pos->member.next); \
327  &pos->member != (head); \
328  pos = llist_entry(pos->member.next, typeof(*pos), member), \
329  prefetch(pos->member.next))
330 
337 #define llist_for_each_entry_safe(pos, n, head, member) \
338  for (pos = llist_entry((head)->next, typeof(*pos), member), \
339  n = llist_entry(pos->member.next, typeof(*pos), member); \
340  &pos->member != (head); \
341  pos = n, n = llist_entry(n->member.next, typeof(*n), member))
342 
347 #define llist_for_each_rcu(pos, head) \
348  for (pos = (head)->next, prefetch(pos->next); pos != (head); \
349  pos = pos->next, ({ smp_read_barrier_depends(); 0;}), prefetch(pos->next))
350 
351 #define __llist_for_each_rcu(pos, head) \
352  for (pos = (head)->next; pos != (head); \
353  pos = pos->next, ({ smp_read_barrier_depends(); 0;}))
354 
360 #define llist_for_each_safe_rcu(pos, n, head) \
361  for (pos = (head)->next, n = pos->next; pos != (head); \
362  pos = n, ({ smp_read_barrier_depends(); 0;}), n = pos->next)
363 
369 #define llist_for_each_entry_rcu(pos, head, member) \
370  for (pos = llist_entry((head)->next, typeof(*pos), member), \
371  prefetch(pos->member.next); \
372  &pos->member != (head); \
373  pos = llist_entry(pos->member.next, typeof(*pos), member), \
374  ({ smp_read_barrier_depends(); 0;}), \
375  prefetch(pos->member.next))
376 
377 
382 #define llist_for_each_continue_rcu(pos, head) \
383  for ((pos) = (pos)->next, prefetch((pos)->next); (pos) != (head); \
384  (pos) = (pos)->next, ({ smp_read_barrier_depends(); 0;}), prefetch((pos)->next))
385 
393 static inline unsigned int llist_count(const struct llist_head *head)
394 {
395  struct llist_head *entry;
396  unsigned int i = 0;
397  llist_for_each(entry, head)
398  i++;
399  return i;
400 }
401 
402 
403 
410 struct hlist_head {
411  struct hlist_node *first;
412 };
413 
414 struct hlist_node {
415  struct hlist_node *next, **pprev;
416 };
417 
418 #define HLIST_HEAD_INIT { .first = NULL }
419 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
420 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
421 static inline void INIT_HLIST_NODE(struct hlist_node *h)
422 {
423  h->next = NULL;
424  h->pprev = NULL;
425 }
426 
427 #define READ_ONCE(x) x
428 #define WRITE_ONCE(a, b) a = b
429 
438 static inline int hlist_unhashed(const struct hlist_node *h)
439 {
440  return !h->pprev;
441 }
442 
451 static inline int hlist_unhashed_lockless(const struct hlist_node *h)
452 {
453  return !READ_ONCE(h->pprev);
454 }
455 
460 static inline int hlist_empty(const struct hlist_head *h)
461 {
462  return !READ_ONCE(h->first);
463 }
464 
465 static inline void __hlist_del(struct hlist_node *n)
466 {
467  struct hlist_node *next = n->next;
468  struct hlist_node **pprev = n->pprev;
469 
470  WRITE_ONCE(*pprev, next);
471  if (next)
473 }
474 
481 static inline void hlist_del(struct hlist_node *n)
482 {
483  __hlist_del(n);
484  n->next = (struct hlist_node *)LLIST_POISON1;
485  n->pprev = (struct hlist_node **)LLIST_POISON2;
486 }
487 
493 static inline void hlist_del_init(struct hlist_node *n)
494 {
495  if (!hlist_unhashed(n)) {
496  __hlist_del(n);
498  }
499 }
500 
508 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
509 {
510  struct hlist_node *first = h->first;
511  WRITE_ONCE(n->next, first);
512  if (first)
513  WRITE_ONCE(first->pprev, &n->next);
514  WRITE_ONCE(h->first, n);
515  WRITE_ONCE(n->pprev, &h->first);
516 }
517 
522 static inline void hlist_add_before(struct hlist_node *n,
523  struct hlist_node *next)
524 {
525  WRITE_ONCE(n->pprev, next->pprev);
526  WRITE_ONCE(n->next, next);
527  WRITE_ONCE(next->pprev, &n->next);
528  WRITE_ONCE(*(n->pprev), n);
529 }
530 
535 static inline void hlist_add_behind(struct hlist_node *n,
536  struct hlist_node *prev)
537 {
538  WRITE_ONCE(n->next, prev->next);
539  WRITE_ONCE(prev->next, n);
540  WRITE_ONCE(n->pprev, &prev->next);
541 
542  if (n->next)
543  WRITE_ONCE(n->next->pprev, &n->next);
544 }
545 
553 static inline void hlist_add_fake(struct hlist_node *n)
554 {
555  n->pprev = &n->next;
556 }
557 
561 static inline bool hlist_fake(struct hlist_node *h)
562 {
563  return h->pprev == &h->next;
564 }
565 
573 static inline bool
575 {
576  return !n->next && n->pprev == &h->first;
577 }
578 
586 static inline void hlist_move_list(struct hlist_head *old,
587  struct hlist_head *_new)
588 {
589  _new->first = old->first;
590  if (_new->first)
591  _new->first->pprev = &_new->first;
592  old->first = NULL;
593 }
594 
595 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
596 
597 #define hlist_for_each(pos, head) \
598  for (pos = (head)->first; pos ; pos = pos->next)
599 
600 #define hlist_for_each_safe(pos, n, head) \
601  for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
602  pos = n)
603 
604 #define hlist_entry_safe(ptr, type, member) \
605  ({ typeof(ptr) ____ptr = (ptr); \
606  ____ptr ? hlist_entry(____ptr, type, member) : NULL; \
607  })
608 
614 #define hlist_for_each_entry(pos, head, member) \
615  for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
616  pos; \
617  pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
618 
623 #define hlist_for_each_entry_continue(pos, member) \
624  for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member);\
625  pos; \
626  pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
627 
632 #define hlist_for_each_entry_from(pos, member) \
633  for (; pos; \
634  pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
635 
642 #define hlist_for_each_entry_safe(pos, n, head, member) \
643  for (pos = hlist_entry_safe((head)->first, typeof(*pos), member);\
644  pos && ({ n = pos->member.next; 1; }); \
645  pos = hlist_entry_safe(n, typeof(*pos), member))
646 
647 
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:393
#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:465
static void hlist_add_behind(struct hlist_node *n, struct hlist_node *prev)
add a new entry after the one specified
Definition: linuxlist.h:535
static void hlist_add_before(struct hlist_node *n, struct hlist_node *next)
add a new entry before the one specified.
Definition: linuxlist.h:522
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:493
static bool hlist_fake(struct hlist_node *h)
Is this node a fake hlist?.
Definition: linuxlist.h:561
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:421
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:574
static void hlist_add_fake(struct hlist_node *n)
create a fake hlist consisting of a single headless node.
Definition: linuxlist.h:553
static int hlist_unhashed(const struct hlist_node *h)
Has node been removed from list and reinitialized?.
Definition: linuxlist.h:438
#define WRITE_ONCE(a, b)
Definition: linuxlist.h:428
static void __llist_splice(struct llist_head *llist, struct llist_head *head)
Definition: linuxlist.h:174
#define READ_ONCE(x)
Definition: linuxlist.h:427
static void hlist_del(struct hlist_node *n)
Delete the specified hlist_node from its list.
Definition: linuxlist.h:481
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:508
#define llist_for_each(pos, head)
Iterate over a linked list.
Definition: linuxlist.h:261
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:451
static void hlist_move_list(struct hlist_head *old, struct hlist_head *_new)
Move an hlist.
Definition: linuxlist.h:586
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:460
static void __llist_del(struct llist_head *prev, struct llist_head *next)
Definition: linuxlist.h:116
struct gad_raw_head h
Double linked lists with a single pointer list head.
Definition: linuxlist.h:410
struct hlist_node * first
Definition: linuxlist.h:411
Definition: linuxlist.h:414
struct hlist_node ** pprev
Definition: linuxlist.h:415
struct hlist_node * next
Definition: linuxlist.h:415
(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