Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef _LINUX_LIST_NULLS_H
0003 #define _LINUX_LIST_NULLS_H
0004 
0005 #include <linux/poison.h>
0006 #include <linux/const.h>
0007 
0008 /*
0009  * Special version of lists, where end of list is not a NULL pointer,
0010  * but a 'nulls' marker, which can have many different values.
0011  * (up to 2^31 different values guaranteed on all platforms)
0012  *
0013  * In the standard hlist, termination of a list is the NULL pointer.
0014  * In this special 'nulls' variant, we use the fact that objects stored in
0015  * a list are aligned on a word (4 or 8 bytes alignment).
0016  * We therefore use the last significant bit of 'ptr' :
0017  * Set to 1 : This is a 'nulls' end-of-list marker (ptr >> 1)
0018  * Set to 0 : This is a pointer to some object (ptr)
0019  */
0020 
0021 struct hlist_nulls_head {
0022     struct hlist_nulls_node *first;
0023 };
0024 
0025 struct hlist_nulls_node {
0026     struct hlist_nulls_node *next, **pprev;
0027 };
0028 #define NULLS_MARKER(value) (1UL | (((long)value) << 1))
0029 #define INIT_HLIST_NULLS_HEAD(ptr, nulls) \
0030     ((ptr)->first = (struct hlist_nulls_node *) NULLS_MARKER(nulls))
0031 
0032 #define hlist_nulls_entry(ptr, type, member) container_of(ptr,type,member)
0033 
0034 #define hlist_nulls_entry_safe(ptr, type, member) \
0035     ({ typeof(ptr) ____ptr = (ptr); \
0036        !is_a_nulls(____ptr) ? hlist_nulls_entry(____ptr, type, member) : NULL; \
0037     })
0038 /**
0039  * ptr_is_a_nulls - Test if a ptr is a nulls
0040  * @ptr: ptr to be tested
0041  *
0042  */
0043 static inline int is_a_nulls(const struct hlist_nulls_node *ptr)
0044 {
0045     return ((unsigned long)ptr & 1);
0046 }
0047 
0048 /**
0049  * get_nulls_value - Get the 'nulls' value of the end of chain
0050  * @ptr: end of chain
0051  *
0052  * Should be called only if is_a_nulls(ptr);
0053  */
0054 static inline unsigned long get_nulls_value(const struct hlist_nulls_node *ptr)
0055 {
0056     return ((unsigned long)ptr) >> 1;
0057 }
0058 
0059 /**
0060  * hlist_nulls_unhashed - Has node been removed and reinitialized?
0061  * @h: Node to be checked
0062  *
0063  * Not that not all removal functions will leave a node in unhashed state.
0064  * For example, hlist_del_init_rcu() leaves the node in unhashed state,
0065  * but hlist_nulls_del() does not.
0066  */
0067 static inline int hlist_nulls_unhashed(const struct hlist_nulls_node *h)
0068 {
0069     return !h->pprev;
0070 }
0071 
0072 /**
0073  * hlist_nulls_unhashed_lockless - Has node been removed and reinitialized?
0074  * @h: Node to be checked
0075  *
0076  * Not that not all removal functions will leave a node in unhashed state.
0077  * For example, hlist_del_init_rcu() leaves the node in unhashed state,
0078  * but hlist_nulls_del() does not.  Unlike hlist_nulls_unhashed(), this
0079  * function may be used locklessly.
0080  */
0081 static inline int hlist_nulls_unhashed_lockless(const struct hlist_nulls_node *h)
0082 {
0083     return !READ_ONCE(h->pprev);
0084 }
0085 
0086 static inline int hlist_nulls_empty(const struct hlist_nulls_head *h)
0087 {
0088     return is_a_nulls(READ_ONCE(h->first));
0089 }
0090 
0091 static inline void hlist_nulls_add_head(struct hlist_nulls_node *n,
0092                     struct hlist_nulls_head *h)
0093 {
0094     struct hlist_nulls_node *first = h->first;
0095 
0096     n->next = first;
0097     WRITE_ONCE(n->pprev, &h->first);
0098     h->first = n;
0099     if (!is_a_nulls(first))
0100         WRITE_ONCE(first->pprev, &n->next);
0101 }
0102 
0103 static inline void __hlist_nulls_del(struct hlist_nulls_node *n)
0104 {
0105     struct hlist_nulls_node *next = n->next;
0106     struct hlist_nulls_node **pprev = n->pprev;
0107 
0108     WRITE_ONCE(*pprev, next);
0109     if (!is_a_nulls(next))
0110         WRITE_ONCE(next->pprev, pprev);
0111 }
0112 
0113 static inline void hlist_nulls_del(struct hlist_nulls_node *n)
0114 {
0115     __hlist_nulls_del(n);
0116     WRITE_ONCE(n->pprev, LIST_POISON2);
0117 }
0118 
0119 /**
0120  * hlist_nulls_for_each_entry   - iterate over list of given type
0121  * @tpos:   the type * to use as a loop cursor.
0122  * @pos:    the &struct hlist_node to use as a loop cursor.
0123  * @head:   the head for your list.
0124  * @member: the name of the hlist_node within the struct.
0125  *
0126  */
0127 #define hlist_nulls_for_each_entry(tpos, pos, head, member)            \
0128     for (pos = (head)->first;                          \
0129          (!is_a_nulls(pos)) &&                         \
0130         ({ tpos = hlist_nulls_entry(pos, typeof(*tpos), member); 1;}); \
0131          pos = pos->next)
0132 
0133 /**
0134  * hlist_nulls_for_each_entry_from - iterate over a hlist continuing from current point
0135  * @tpos:   the type * to use as a loop cursor.
0136  * @pos:    the &struct hlist_node to use as a loop cursor.
0137  * @member: the name of the hlist_node within the struct.
0138  *
0139  */
0140 #define hlist_nulls_for_each_entry_from(tpos, pos, member)  \
0141     for (; (!is_a_nulls(pos)) &&                \
0142         ({ tpos = hlist_nulls_entry(pos, typeof(*tpos), member); 1;}); \
0143          pos = pos->next)
0144 
0145 #endif