Commit | Line | Data |
---|---|---|
63ff4873 MD |
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 | * | |
6734fec1 | 12 | * Copyright (C) 2010-2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
5db941e8 | 13 | * |
63ff4873 MD |
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 | ||
9056e3b8 MD |
19 | #include <stddef.h> |
20 | ||
6734fec1 | 21 | struct cds_hlist_head { |
16aa9ee8 | 22 | struct cds_hlist_node *next; |
63ff4873 MD |
23 | }; |
24 | ||
6734fec1 MD |
25 | struct cds_hlist_node { |
26 | struct cds_hlist_node *next, *prev; | |
63ff4873 MD |
27 | }; |
28 | ||
6734fec1 MD |
29 | /* Initialize a new list head. */ |
30 | static inline | |
31 | void CDS_INIT_HLIST_HEAD(struct cds_hlist_head *ptr) | |
63ff4873 MD |
32 | { |
33 | ptr->next = NULL; | |
34 | } | |
35 | ||
d7a0f2fe MD |
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 | ||
6734fec1 MD |
42 | /* Get typed element from list at a given position. */ |
43 | #define cds_hlist_entry(ptr, type, member) \ | |
63ff4873 MD |
44 | ((type *) ((char *) (ptr) - (unsigned long) (&((type *) 0)->member))) |
45 | ||
6734fec1 MD |
46 | /* Add new element at the head of the list. */ |
47 | static inline | |
48 | void cds_hlist_add_head(struct cds_hlist_node *newp, | |
49 | struct cds_hlist_head *head) | |
63ff4873 MD |
50 | { |
51 | if (head->next) | |
52 | head->next->prev = newp; | |
63ff4873 | 53 | newp->next = head->next; |
6734fec1 | 54 | newp->prev = (struct cds_hlist_node *) head; |
63ff4873 MD |
55 | head->next = newp; |
56 | } | |
57 | ||
6734fec1 MD |
58 | /* Remove element from list. */ |
59 | static inline | |
60 | void cds_hlist_del(struct cds_hlist_node *elem) | |
63ff4873 MD |
61 | { |
62 | if (elem->next) | |
63 | elem->next->prev = elem->prev; | |
63ff4873 MD |
64 | elem->prev->next = elem->next; |
65 | } | |
66 | ||
d7a0f2fe MD |
67 | #define cds_hlist_for_each(pos, head) \ |
68 | for (pos = (head)->next; pos != NULL; pos = pos->next) | |
69 | ||
70 | #define cds_hlist_for_each_safe(pos, p, head) \ | |
71 | for (pos = (head)->next; \ | |
72 | (pos != NULL) && (p = pos->next, 1); \ | |
73 | pos = p) | |
74 | ||
75 | /* | |
76 | * cds_hlist_for_each_entry and cds_hlist_for_each_entry_safe take | |
77 | * respectively 4 and 5 arguments, while the Linux kernel APIs take 3, | |
78 | * and 4. We implement cds_hlist_for_each_entry_2() and | |
79 | * cds_hlist_for_each_entry_safe_2() to follow the Linux kernel APIs. | |
80 | */ | |
6734fec1 MD |
81 | #define cds_hlist_for_each_entry(entry, pos, head, member) \ |
82 | for (pos = (head)->next, \ | |
83 | entry = cds_hlist_entry(pos, __typeof__(*entry), member); \ | |
84 | pos != NULL; \ | |
85 | pos = pos->next, \ | |
86 | entry = cds_hlist_entry(pos, __typeof__(*entry), member)) | |
63ff4873 | 87 | |
6734fec1 MD |
88 | #define cds_hlist_for_each_entry_safe(entry, pos, p, head, member) \ |
89 | for (pos = (head)->next, \ | |
90 | entry = cds_hlist_entry(pos, __typeof__(*entry), member); \ | |
d7a0f2fe | 91 | (pos != NULL) && (p = pos->next, 1); \ |
6734fec1 MD |
92 | pos = p, \ |
93 | entry = cds_hlist_entry(pos, __typeof__(*entry), member)) | |
63ff4873 | 94 | |
d7a0f2fe MD |
95 | #define cds_hlist_for_each_entry_2(entry, head, member) \ |
96 | for (entry = cds_hlist_entry((head)->next, __typeof__(*entry), member); \ | |
97 | &entry->member != NULL; \ | |
98 | entry = cds_hlist_entry(entry->member.next, __typeof__(*entry), member)) | |
99 | ||
100 | #define cds_hlist_for_each_entry_safe_2(entry, e, head, member) \ | |
101 | for (entry = cds_hlist_entry((head)->next, __typeof__(*entry), member); \ | |
102 | (&entry->member != NULL) && (e = cds_hlist_entry(entry->member.next, \ | |
103 | __typeof__(*entry), member), 1); \ | |
104 | entry = e) | |
105 | ||
63ff4873 | 106 | #endif /* _KCOMPAT_HLIST_H */ |