blob: e8c300e06438de24b069780f351a97ad467a5e40 [file] [log] [blame]
Eric Dumazetbbaffac2008-11-16 19:37:55 -08001#ifndef _LINUX_LIST_NULLS_H
2#define _LINUX_LIST_NULLS_H
3
4/*
5 * Special version of lists, where end of list is not a NULL pointer,
6 * but a 'nulls' marker, which can have many different values.
7 * (up to 2^31 different values guaranteed on all platforms)
8 *
9 * In the standard hlist, termination of a list is the NULL pointer.
10 * In this special 'nulls' variant, we use the fact that objects stored in
11 * a list are aligned on a word (4 or 8 bytes alignment).
12 * We therefore use the last significant bit of 'ptr' :
13 * Set to 1 : This is a 'nulls' end-of-list marker (ptr >> 1)
14 * Set to 0 : This is a pointer to some object (ptr)
15 */
16
17struct hlist_nulls_head {
18 struct hlist_nulls_node *first;
19};
20
21struct hlist_nulls_node {
22 struct hlist_nulls_node *next, **pprev;
23};
Thomas Graff89bd6f2015-01-02 23:00:21 +010024#define NULLS_MARKER(value) (1UL | (((long)value) << 1))
Eric Dumazetbbaffac2008-11-16 19:37:55 -080025#define INIT_HLIST_NULLS_HEAD(ptr, nulls) \
Thomas Graff89bd6f2015-01-02 23:00:21 +010026 ((ptr)->first = (struct hlist_nulls_node *) NULLS_MARKER(nulls))
Eric Dumazetbbaffac2008-11-16 19:37:55 -080027
28#define hlist_nulls_entry(ptr, type, member) container_of(ptr,type,member)
29/**
30 * ptr_is_a_nulls - Test if a ptr is a nulls
31 * @ptr: ptr to be tested
32 *
33 */
34static inline int is_a_nulls(const struct hlist_nulls_node *ptr)
35{
36 return ((unsigned long)ptr & 1);
37}
38
39/**
40 * get_nulls_value - Get the 'nulls' value of the end of chain
41 * @ptr: end of chain
42 *
43 * Should be called only if is_a_nulls(ptr);
44 */
45static inline unsigned long get_nulls_value(const struct hlist_nulls_node *ptr)
46{
47 return ((unsigned long)ptr) >> 1;
48}
49
50static inline int hlist_nulls_unhashed(const struct hlist_nulls_node *h)
51{
52 return !h->pprev;
53}
54
55static inline int hlist_nulls_empty(const struct hlist_nulls_head *h)
56{
57 return is_a_nulls(h->first);
58}
59
Pablo Neira Ayusod219dce2009-06-13 12:28:57 +020060static inline void hlist_nulls_add_head(struct hlist_nulls_node *n,
61 struct hlist_nulls_head *h)
62{
63 struct hlist_nulls_node *first = h->first;
64
65 n->next = first;
66 n->pprev = &h->first;
67 h->first = n;
68 if (!is_a_nulls(first))
69 first->pprev = &n->next;
70}
71
Eric Dumazetbbaffac2008-11-16 19:37:55 -080072static inline void __hlist_nulls_del(struct hlist_nulls_node *n)
73{
74 struct hlist_nulls_node *next = n->next;
75 struct hlist_nulls_node **pprev = n->pprev;
76 *pprev = next;
77 if (!is_a_nulls(next))
78 next->pprev = pprev;
79}
80
Pablo Neira Ayusod219dce2009-06-13 12:28:57 +020081static inline void hlist_nulls_del(struct hlist_nulls_node *n)
82{
83 __hlist_nulls_del(n);
84 n->pprev = LIST_POISON2;
85}
86
Eric Dumazetbbaffac2008-11-16 19:37:55 -080087/**
88 * hlist_nulls_for_each_entry - iterate over list of given type
89 * @tpos: the type * to use as a loop cursor.
90 * @pos: the &struct hlist_node to use as a loop cursor.
91 * @head: the head for your list.
92 * @member: the name of the hlist_node within the struct.
93 *
94 */
95#define hlist_nulls_for_each_entry(tpos, pos, head, member) \
96 for (pos = (head)->first; \
97 (!is_a_nulls(pos)) && \
98 ({ tpos = hlist_nulls_entry(pos, typeof(*tpos), member); 1;}); \
99 pos = pos->next)
100
101/**
102 * hlist_nulls_for_each_entry_from - iterate over a hlist continuing from current point
103 * @tpos: the type * to use as a loop cursor.
104 * @pos: the &struct hlist_node to use as a loop cursor.
105 * @member: the name of the hlist_node within the struct.
106 *
107 */
108#define hlist_nulls_for_each_entry_from(tpos, pos, member) \
109 for (; (!is_a_nulls(pos)) && \
110 ({ tpos = hlist_nulls_entry(pos, typeof(*tpos), member); 1;}); \
111 pos = pos->next)
112
113#endif