Fix: rcuja merge fixes
[userspace-rcu.git] / urcu / hlist.h
1 #ifndef _KCOMPAT_HLIST_H
2 #define _KCOMPAT_HLIST_H
3
4 /*
5 * Kernel sourcecode compatible lightweight single pointer list head useful
6 * for implementing hash tables
7 *
8 * Copyright (C) 2009 Novell Inc.
9 *
10 * Author: Jan Blunck <jblunck@suse.de>
11 *
12 * Copyright (C) 2010-2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
13 *
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU Lesser General Public License version 2.1 as
16 * published by the Free Software Foundation.
17 */
18
19 #include <stddef.h>
20
21 struct cds_hlist_head {
22 struct cds_hlist_node *next;
23 };
24
25 struct cds_hlist_node {
26 struct cds_hlist_node *next, *prev;
27 };
28
29 /* Initialize a new list head. */
30 static inline
31 void CDS_INIT_HLIST_HEAD(struct cds_hlist_head *ptr)
32 {
33 ptr->next = NULL;
34 }
35
36 #define CDS_HLIST_HEAD(name) \
37 struct cds_hlist_head name = { NULL }
38
39 #define CDS_HLIST_HEAD_INIT(name) \
40 { .next = NULL }
41
42 /* Get typed element from list at a given position. */
43 #define cds_hlist_entry(ptr, type, member) \
44 ((type *) ((char *) (ptr) - (unsigned long) (&((type *) 0)->member)))
45
46 /* Get first entry from a list. Assumes the hlist is not empty. */
47 #define cds_hlist_first_entry(ptr, type, member) \
48 cds_list_entry((ptr)->next, type, member)
49
50 static inline
51 int cds_hlist_empty(struct cds_hlist_head *head)
52 {
53 return !head->next;
54 }
55
56 /* Add new element at the head of the list. */
57 static inline
58 void cds_hlist_add_head(struct cds_hlist_node *newp,
59 struct cds_hlist_head *head)
60 {
61 if (head->next)
62 head->next->prev = newp;
63 newp->next = head->next;
64 newp->prev = (struct cds_hlist_node *) head;
65 head->next = newp;
66 }
67
68 /* Remove element from list. */
69 static inline
70 void cds_hlist_del(struct cds_hlist_node *elem)
71 {
72 if (elem->next)
73 elem->next->prev = elem->prev;
74 elem->prev->next = elem->next;
75 }
76
77 #define cds_hlist_for_each(pos, head) \
78 for (pos = (head)->next; pos != NULL; pos = pos->next)
79
80 #define cds_hlist_for_each_safe(pos, p, head) \
81 for (pos = (head)->next; \
82 (pos != NULL) && (p = pos->next, 1); \
83 pos = p)
84
85 /*
86 * cds_hlist_for_each_entry and cds_hlist_for_each_entry_safe take
87 * respectively 4 and 5 arguments, while the Linux kernel APIs take 3,
88 * and 4. We implement cds_hlist_for_each_entry_2() and
89 * cds_hlist_for_each_entry_safe_2() to follow the Linux kernel APIs.
90 */
91 #define cds_hlist_for_each_entry(entry, pos, head, member) \
92 for (pos = (head)->next, \
93 entry = cds_hlist_entry(pos, __typeof__(*entry), member); \
94 pos != NULL; \
95 pos = pos->next, \
96 entry = cds_hlist_entry(pos, __typeof__(*entry), member))
97
98 #define cds_hlist_for_each_entry_safe(entry, pos, p, head, member) \
99 for (pos = (head)->next, \
100 entry = cds_hlist_entry(pos, __typeof__(*entry), member); \
101 (pos != NULL) && (p = pos->next, 1); \
102 pos = p, \
103 entry = cds_hlist_entry(pos, __typeof__(*entry), member))
104
105 #define cds_hlist_for_each_entry_2(entry, head, member) \
106 for (entry = ((head)->next == NULL ? NULL \
107 : cds_hlist_entry((head)->next, __typeof__(*entry), member)); \
108 entry != NULL; \
109 entry = (entry->member.next == NULL ? NULL \
110 : cds_hlist_entry(entry->member.next, __typeof__(*entry), member)))
111
112 #define cds_hlist_for_each_entry_safe_2(entry, e, head, member) \
113 for (entry = ((head)->next == NULL ? NULL \
114 : cds_hlist_entry((head)->next, __typeof__(*entry), member)); \
115 (entry != NULL) && (e = (entry->member.next == NULL ? NULL \
116 : cds_hlist_entry(entry->member.next, \
117 __typeof__(*entry), member)), 1); \
118 entry = e)
119
120 #endif /* _KCOMPAT_HLIST_H */
This page took 0.03152 seconds and 4 git commands to generate.