*/
#include <stddef.h>
+#include <urcu/compiler.h>
struct cds_hlist_head {
struct cds_hlist_node *next;
{ .next = NULL }
/* Get typed element from list at a given position. */
-#define cds_hlist_entry(ptr, type, member) \
- ((type *) ((char *) (ptr) - (unsigned long) (&((type *) 0)->member)))
+#define cds_hlist_entry(ptr, type, member) caa_container_of(ptr, type, member)
+
+/* Get typed element from list at a given position, keeping NULL pointers. */
+#define cds_hlist_entry_safe(ptr, type, member) \
+ ({ \
+ __typeof__(ptr) ____ret = ptr; \
+ ____ret ? cds_hlist_entry(____ret, type, member) : NULL; \
+ })
/* Add new element at the head of the list. */
static inline
entry = cds_hlist_entry(pos, __typeof__(*entry), member))
#define cds_hlist_for_each_entry_2(entry, head, member) \
- for (entry = ((head)->next == NULL ? NULL \
- : cds_hlist_entry((head)->next, __typeof__(*entry), member)); \
+ for (entry = cds_hlist_entry_safe((head)->next, __typeof__(*entry), member); \
entry != NULL; \
- entry = (entry->member.next == NULL ? NULL \
- : cds_hlist_entry(entry->member.next, __typeof__(*entry), member)))
+ entry = cds_hlist_entry_safe(entry->member.next, __typeof__(*entry), member))
#define cds_hlist_for_each_entry_safe_2(entry, e, head, member) \
- for (entry = ((head)->next == NULL ? NULL \
- : cds_hlist_entry((head)->next, __typeof__(*entry), member)); \
- (entry != NULL) && (e = (entry->member.next == NULL ? NULL \
- : cds_hlist_entry(entry->member.next, \
+ for (entry = cds_hlist_entry_safe((head)->next, __typeof__(*entry), member); \
+ (entry != NULL) && (e = (cds_hlist_entry_safe(entry->member.next, \
__typeof__(*entry), member)), 1); \
entry = e)
entry = cds_hlist_entry(pos, __typeof__(*entry), member))
#define cds_hlist_for_each_entry_rcu_2(entry, head, member) \
- for (entry = cds_hlist_entry(rcu_dereference((head)->next), \
+ for (entry = cds_hlist_entry_safe(rcu_dereference((head)->next), \
__typeof__(*entry), member); \
- &entry->member != NULL; \
- entry = cds_hlist_entry(rcu_dereference(entry->member.next), \
+ entry != NULL; \
+ entry = cds_hlist_entry_safe(rcu_dereference(entry->member.next), \
__typeof__(*entry), member))
#endif /* _URCU_RCUHLIST_H */