hlist: implement cds_hlist_empty
[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 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 {
23 struct cds_hlist_node *next;
24 };
25
26 struct cds_hlist_node
27 {
28 struct cds_hlist_node *next;
29 struct cds_hlist_node *prev;
30 };
31
32 /* Initialize a new list head. */
33 static inline void CDS_INIT_HLIST_HEAD(struct cds_hlist_head *ptr)
34 {
35 ptr->next = NULL;
36 }
37
38 /* Get typed element from list at a given position. */
39 #define cds_hlist_entry(ptr, type, member) \
40 ((type *) ((char *) (ptr) - (unsigned long) (&((type *) 0)->member)))
41
42 static inline int cds_hlist_empty(struct cds_hlist_head *head)
43 {
44 return !head->next;
45 }
46
47 /* Add new element at the head of the list. */
48 static inline void cds_hlist_add_head (struct cds_hlist_node *newp,
49 struct cds_hlist_head *head)
50 {
51 if (head->next)
52 head->next->prev = newp;
53
54 newp->next = head->next;
55 newp->prev = (struct cds_hlist_node *)head;
56 head->next = newp;
57 }
58
59 /* Remove element from list. */
60 static inline void cds_hlist_del (struct cds_hlist_node *elem)
61 {
62 if (elem->next)
63 elem->next->prev = elem->prev;
64
65 elem->prev->next = elem->next;
66 }
67
68 #define cds_hlist_for_each_entry(entry, pos, head, member) \
69 for (pos = (head)->next, \
70 entry = cds_hlist_entry(pos, __typeof__(*entry), member); \
71 pos != NULL; \
72 pos = pos->next, \
73 entry = cds_hlist_entry(pos, __typeof__(*entry), member))
74
75 #define cds_hlist_for_each_entry_safe(entry, pos, p, head, member) \
76 for (pos = (head)->next, \
77 entry = cds_hlist_entry(pos, __typeof__(*entry), member); \
78 (pos != NULL) && ({ p = pos->next; 1;}); \
79 pos = p, \
80 entry = cds_hlist_entry(pos, __typeof__(*entry), member))
81
82 #endif /* _KCOMPAT_HLIST_H */
This page took 0.031559 seconds and 5 git commands to generate.