libosmocore  1.6.0
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 
338 #define llist_for_each_entry_safe(pos, n, head, member) \
339  for (pos = llist_entry((head)->next, typeof(*pos), member), \
340  n = llist_entry(pos->member.next, typeof(*pos), member); \
341  &pos->member != (head); \
342  pos = n, n = llist_entry(n->member.next, typeof(*n), member))
343 
348 #define llist_for_each_rcu(pos, head) \
349  for (pos = (head)->next, prefetch(pos->next); pos != (head); \
350  pos = pos->next, ({ smp_read_barrier_depends(); 0;}), prefetch(pos->next))
351 
352 #define __llist_for_each_rcu(pos, head) \
353  for (pos = (head)->next; pos != (head); \
354  pos = pos->next, ({ smp_read_barrier_depends(); 0;}))
355 
361 #define llist_for_each_safe_rcu(pos, n, head) \
362  for (pos = (head)->next, n = pos->next; pos != (head); \
363  pos = n, ({ smp_read_barrier_depends(); 0;}), n = pos->next)
364 
370 #define llist_for_each_entry_rcu(pos, head, member) \
371  for (pos = llist_entry((head)->next, typeof(*pos), member), \
372  prefetch(pos->member.next); \
373  &pos->member != (head); \
374  pos = llist_entry(pos->member.next, typeof(*pos), member), \
375  ({ smp_read_barrier_depends(); 0;}), \
376  prefetch(pos->member.next))
377 
378 
383 #define llist_for_each_continue_rcu(pos, head) \
384  for ((pos) = (pos)->next, prefetch((pos)->next); (pos) != (head); \
385  (pos) = (pos)->next, ({ smp_read_barrier_depends(); 0;}), prefetch((pos)->next))
386 
394 static inline unsigned int llist_count(const struct llist_head *head)
395 {
396  struct llist_head *entry;
397  unsigned int i = 0;
398  llist_for_each(entry, head)
399  i++;
400  return i;
401 }
402 
403 
404 
411 struct hlist_head {
412  struct hlist_node *first;
413 };
414 
415 struct hlist_node {
416  struct hlist_node *next, **pprev;
417 };
418 
419 #define HLIST_HEAD_INIT { .first = NULL }
420 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
421 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
422 static inline void INIT_HLIST_NODE(struct hlist_node *h)
423 {
424  h->next = NULL;
425  h->pprev = NULL;
426 }
427 
428 #define READ_ONCE(x) x
429 #define WRITE_ONCE(a, b) a = b
430 
439 static inline int hlist_unhashed(const struct hlist_node *h)
440 {
441  return !h->pprev;
442 }
443 
452 static inline int hlist_unhashed_lockless(const struct hlist_node *h)
453 {
454  return !READ_ONCE(h->pprev);
455 }
456 
461 static inline int hlist_empty(const struct hlist_head *h)
462 {
463  return !READ_ONCE(h->first);
464 }
465 
466 static inline void __hlist_del(struct hlist_node *n)
467 {
468  struct hlist_node *next = n->next;
469  struct hlist_node **pprev = n->pprev;
470 
471  WRITE_ONCE(*pprev, next);
472  if (next)
474 }
475 
482 static inline void hlist_del(struct hlist_node *n)
483 {
484  __hlist_del(n);
485  n->next = (struct hlist_node *)LLIST_POISON1;
486  n->pprev = (struct hlist_node **)LLIST_POISON2;
487 }
488 
494 static inline void hlist_del_init(struct hlist_node *n)
495 {
496  if (!hlist_unhashed(n)) {
497  __hlist_del(n);
499  }
500 }
501 
509 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
510 {
511  struct hlist_node *first = h->first;
512  WRITE_ONCE(n->next, first);
513  if (first)
514  WRITE_ONCE(first->pprev, &n->next);
515  WRITE_ONCE(h->first, n);
516  WRITE_ONCE(n->pprev, &h->first);
517 }
518 
523 static inline void hlist_add_before(struct hlist_node *n,
524  struct hlist_node *next)
525 {
526  WRITE_ONCE(n->pprev, next->pprev);
527  WRITE_ONCE(n->next, next);
528  WRITE_ONCE(next->pprev, &n->next);
529  WRITE_ONCE(*(n->pprev), n);
530 }
531 
536 static inline void hlist_add_behind(struct hlist_node *n,
537  struct hlist_node *prev)
538 {
539  WRITE_ONCE(n->next, prev->next);
540  WRITE_ONCE(prev->next, n);
541  WRITE_ONCE(n->pprev, &prev->next);
542 
543  if (n->next)
544  WRITE_ONCE(n->next->pprev, &n->next);
545 }
546 
554 static inline void hlist_add_fake(struct hlist_node *n)
555 {
556  n->pprev = &n->next;
557 }
558 
562 static inline bool hlist_fake(struct hlist_node *h)
563 {
564  return h->pprev == &h->next;
565 }
566 
574 static inline bool
576 {
577  return !n->next && n->pprev == &h->first;
578 }
579 
587 static inline void hlist_move_list(struct hlist_head *old,
588  struct hlist_head *_new)
589 {
590  _new->first = old->first;
591  if (_new->first)
592  _new->first->pprev = &_new->first;
593  old->first = NULL;
594 }
595 
596 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
597 
598 #define hlist_for_each(pos, head) \
599  for (pos = (head)->first; pos ; pos = pos->next)
600 
601 #define hlist_for_each_safe(pos, n, head) \
602  for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
603  pos = n)
604 
605 #define hlist_entry_safe(ptr, type, member) \
606  ({ typeof(ptr) ____ptr = (ptr); \
607  ____ptr ? hlist_entry(____ptr, type, member) : NULL; \
608  })
609 
615 #define hlist_for_each_entry(pos, head, member) \
616  for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
617  pos; \
618  pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
619 
624 #define hlist_for_each_entry_continue(pos, member) \
625  for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member);\
626  pos; \
627  pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
628 
633 #define hlist_for_each_entry_from(pos, member) \
634  for (; pos; \
635  pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
636 
643 #define hlist_for_each_entry_safe(pos, n, head, member) \
644  for (pos = hlist_entry_safe((head)->first, typeof(*pos), member);\
645  pos && ({ n = pos->member.next; 1; }); \
646  pos = hlist_entry_safe(n, typeof(*pos), member))
647 
648 
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:394
#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:466
static void hlist_add_behind(struct hlist_node *n, struct hlist_node *prev)
add a new entry after the one specified
Definition: linuxlist.h:536
static void hlist_add_before(struct hlist_node *n, struct hlist_node *next)
add a new entry before the one specified.
Definition: linuxlist.h:523
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:494
static bool hlist_fake(struct hlist_node *h)
Is this node a fake hlist?.
Definition: linuxlist.h:562
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:422
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:575
static void hlist_add_fake(struct hlist_node *n)
create a fake hlist consisting of a single headless node.
Definition: linuxlist.h:554
static int hlist_unhashed(const struct hlist_node *h)
Has node been removed from list and reinitialized?.
Definition: linuxlist.h:439
#define WRITE_ONCE(a, b)
Definition: linuxlist.h:429
static void __llist_splice(struct llist_head *llist, struct llist_head *head)
Definition: linuxlist.h:174
#define READ_ONCE(x)
Definition: linuxlist.h:428
static void hlist_del(struct hlist_node *n)
Delete the specified hlist_node from its list.
Definition: linuxlist.h:482
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:509
#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:452
static void hlist_move_list(struct hlist_head *old, struct hlist_head *_new)
Move an hlist.
Definition: linuxlist.h:587
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:461
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:411
struct hlist_node * first
Definition: linuxlist.h:412
Definition: linuxlist.h:415
struct hlist_node ** pprev
Definition: linuxlist.h:416
struct hlist_node * next
Definition: linuxlist.h:416
(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