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 | * | |
12 | * This program is free software; you can redistribute it and/or modify it | |
13 | * under the terms of the GNU Lesser General Public License version 2.1 as | |
14 | * published by the Free Software Foundation. | |
15 | */ | |
16 | ||
17 | struct hlist_head | |
18 | { | |
19 | struct hlist_node *next; | |
20 | }; | |
21 | ||
22 | struct hlist_node | |
23 | { | |
24 | struct hlist_node *next; | |
25 | struct hlist_node *prev; | |
26 | }; | |
27 | ||
28 | /* Initialize a new list head. */ | |
29 | static inline void INIT_HLIST_HEAD(struct hlist_head *ptr) | |
30 | { | |
31 | ptr->next = NULL; | |
32 | } | |
33 | ||
34 | /* Get typed element from list at a given position. */ | |
35 | #define hlist_entry(ptr, type, member) \ | |
36 | ((type *) ((char *) (ptr) - (unsigned long) (&((type *) 0)->member))) | |
37 | ||
38 | /* Add new element at the head of the list. */ | |
39 | static inline void hlist_add_head (struct hlist_node *newp, | |
40 | struct hlist_head *head) | |
41 | { | |
42 | if (head->next) | |
43 | head->next->prev = newp; | |
44 | ||
45 | newp->next = head->next; | |
46 | newp->prev = (struct hlist_node *)head; | |
47 | head->next = newp; | |
48 | } | |
49 | ||
50 | /* Remove element from list. */ | |
51 | static inline void hlist_del (struct hlist_node *elem) | |
52 | { | |
53 | if (elem->next) | |
54 | elem->next->prev = elem->prev; | |
55 | ||
56 | elem->prev->next = elem->next; | |
57 | } | |
58 | ||
59 | #define hlist_for_each_entry(entry, pos, head, member) \ | |
60 | for (pos = (head)->next, \ | |
61 | entry = hlist_entry(pos, typeof(*entry), member); \ | |
62 | pos != NULL; \ | |
63 | pos = pos->next, \ | |
64 | entry = hlist_entry(pos, typeof(*entry), member)) | |
65 | ||
66 | #define hlist_for_each_entry_safe(entry, pos, p, head, member) \ | |
67 | for (pos = (head)->next, \ | |
68 | entry = hlist_entry(pos, typeof(*entry), member); \ | |
69 | (pos != NULL) && ({ p = pos->next; 1;}); \ | |
70 | pos = p, \ | |
71 | entry = hlist_entry(pos, typeof(*entry), member)) | |
72 | ||
73 | #endif /* _KCOMPAT_HLIST_H */ |