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 | * | |
5db941e8 MD |
12 | * Copyright (C) 2010 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
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 | ||
16aa9ee8 | 21 | struct cds_hlist_head |
63ff4873 | 22 | { |
16aa9ee8 | 23 | struct cds_hlist_node *next; |
63ff4873 MD |
24 | }; |
25 | ||
16aa9ee8 | 26 | struct cds_hlist_node |
63ff4873 | 27 | { |
16aa9ee8 DG |
28 | struct cds_hlist_node *next; |
29 | struct cds_hlist_node *prev; | |
63ff4873 MD |
30 | }; |
31 | ||
32 | /* Initialize a new list head. */ | |
16aa9ee8 | 33 | static inline void CDS_INIT_HLIST_HEAD(struct cds_hlist_head *ptr) |
63ff4873 MD |
34 | { |
35 | ptr->next = NULL; | |
36 | } | |
37 | ||
38 | /* Get typed element from list at a given position. */ | |
16aa9ee8 | 39 | #define cds_hlist_entry(ptr, type, member) \ |
63ff4873 MD |
40 | ((type *) ((char *) (ptr) - (unsigned long) (&((type *) 0)->member))) |
41 | ||
3d8fe307 MD |
42 | /* Get first entry from a list. Assumes the hlist is not empty. */ |
43 | #define cds_hlist_first_entry(ptr, type, member) \ | |
44 | cds_list_entry((ptr)->next, type, member) | |
45 | ||
5f1bf177 MD |
46 | static inline int cds_hlist_empty(struct cds_hlist_head *head) |
47 | { | |
48 | return !head->next; | |
49 | } | |
50 | ||
63ff4873 | 51 | /* Add new element at the head of the list. */ |
16aa9ee8 DG |
52 | static inline void cds_hlist_add_head (struct cds_hlist_node *newp, |
53 | struct cds_hlist_head *head) | |
63ff4873 MD |
54 | { |
55 | if (head->next) | |
56 | head->next->prev = newp; | |
57 | ||
58 | newp->next = head->next; | |
16aa9ee8 | 59 | newp->prev = (struct cds_hlist_node *)head; |
63ff4873 MD |
60 | head->next = newp; |
61 | } | |
62 | ||
63 | /* Remove element from list. */ | |
16aa9ee8 | 64 | static inline void cds_hlist_del (struct cds_hlist_node *elem) |
63ff4873 MD |
65 | { |
66 | if (elem->next) | |
67 | elem->next->prev = elem->prev; | |
68 | ||
69 | elem->prev->next = elem->next; | |
70 | } | |
71 | ||
16aa9ee8 | 72 | #define cds_hlist_for_each_entry(entry, pos, head, member) \ |
63ff4873 | 73 | for (pos = (head)->next, \ |
bdffa73a | 74 | entry = cds_hlist_entry(pos, __typeof__(*entry), member); \ |
63ff4873 MD |
75 | pos != NULL; \ |
76 | pos = pos->next, \ | |
bdffa73a | 77 | entry = cds_hlist_entry(pos, __typeof__(*entry), member)) |
63ff4873 | 78 | |
16aa9ee8 | 79 | #define cds_hlist_for_each_entry_safe(entry, pos, p, head, member) \ |
63ff4873 | 80 | for (pos = (head)->next, \ |
bdffa73a | 81 | entry = cds_hlist_entry(pos, __typeof__(*entry), member); \ |
63ff4873 MD |
82 | (pos != NULL) && ({ p = pos->next; 1;}); \ |
83 | pos = p, \ | |
bdffa73a | 84 | entry = cds_hlist_entry(pos, __typeof__(*entry), member)) |
63ff4873 MD |
85 | |
86 | #endif /* _KCOMPAT_HLIST_H */ |