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 | ||
6734fec1 MD |
36 | /* Get typed element from list at a given position. */ |
37 | #define cds_hlist_entry(ptr, type, member) \ | |
63ff4873 MD |
38 | ((type *) ((char *) (ptr) - (unsigned long) (&((type *) 0)->member))) |
39 | ||
6734fec1 MD |
40 | /* Add new element at the head of the list. */ |
41 | static inline | |
42 | void cds_hlist_add_head(struct cds_hlist_node *newp, | |
43 | struct cds_hlist_head *head) | |
63ff4873 MD |
44 | { |
45 | if (head->next) | |
46 | head->next->prev = newp; | |
63ff4873 | 47 | newp->next = head->next; |
6734fec1 | 48 | newp->prev = (struct cds_hlist_node *) head; |
63ff4873 MD |
49 | head->next = newp; |
50 | } | |
51 | ||
6734fec1 MD |
52 | /* Remove element from list. */ |
53 | static inline | |
54 | void cds_hlist_del(struct cds_hlist_node *elem) | |
63ff4873 MD |
55 | { |
56 | if (elem->next) | |
57 | elem->next->prev = elem->prev; | |
63ff4873 MD |
58 | elem->prev->next = elem->next; |
59 | } | |
60 | ||
6734fec1 MD |
61 | #define cds_hlist_for_each_entry(entry, pos, head, member) \ |
62 | for (pos = (head)->next, \ | |
63 | entry = cds_hlist_entry(pos, __typeof__(*entry), member); \ | |
64 | pos != NULL; \ | |
65 | pos = pos->next, \ | |
66 | entry = cds_hlist_entry(pos, __typeof__(*entry), member)) | |
63ff4873 | 67 | |
6734fec1 MD |
68 | #define cds_hlist_for_each_entry_safe(entry, pos, p, head, member) \ |
69 | for (pos = (head)->next, \ | |
70 | entry = cds_hlist_entry(pos, __typeof__(*entry), member); \ | |
71 | (pos != NULL) && ({ p = pos->next; 1;}); \ | |
72 | pos = p, \ | |
73 | entry = cds_hlist_entry(pos, __typeof__(*entry), member)) | |
63ff4873 MD |
74 | |
75 | #endif /* _KCOMPAT_HLIST_H */ |