From 143b7f1af13f186f38718af00b3bddf3af8390e2 Mon Sep 17 00:00:00 2001 From: Mathieu Desnoyers Date: Tue, 20 Apr 2021 16:15:39 -0400 Subject: [PATCH] Fix: list.h: use parenthesis around macro parameters, caa_container_of() The coding style followed across liburcu is to use parenthesis around macro parameters when it would otherwise lead to unexpected results due to priority of operators. Fix list.h to follow this coding style. Use caa_container_of() for cds_list_entry rather than open-code the pointer arithmetic. Signed-off-by: Mathieu Desnoyers Change-Id: I17052dc0418ad205cf262a6e5b91e591c37462cb --- include/urcu/list.h | 47 +++++++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/include/urcu/list.h b/include/urcu/list.h index 4aaf869..f3b8678 100644 --- a/include/urcu/list.h +++ b/include/urcu/list.h @@ -25,6 +25,8 @@ #ifndef _CDS_LIST_H #define _CDS_LIST_H 1 +#include + /* * The definitions of this file are adopted from those which can be * found in the Linux kernel headers to enable people familiar with the @@ -121,8 +123,7 @@ void cds_list_splice(struct cds_list_head *add, struct cds_list_head *head) } /* Get typed element from list at a given position. */ -#define cds_list_entry(ptr, type, member) \ - ((type *) ((char *) (ptr) - (unsigned long) (&((type *) 0)->member))) +#define cds_list_entry(ptr, type, member) caa_container_of(ptr, type, member) /* Get first entry from a list. */ @@ -131,54 +132,54 @@ void cds_list_splice(struct cds_list_head *add, struct cds_list_head *head) /* Iterate forward over the elements of the list. */ #define cds_list_for_each(pos, head) \ - for (pos = (head)->next; pos != (head); pos = pos->next) + for (pos = (head)->next; (pos) != (head); pos = (pos)->next) /* * Iterate forward over the elements list. The list elements can be * removed from the list while doing this. */ #define cds_list_for_each_safe(pos, p, head) \ - for (pos = (head)->next, p = pos->next; \ - pos != (head); \ - pos = p, p = pos->next) + for (pos = (head)->next, p = (pos)->next; \ + (pos) != (head); \ + pos = (p), p = (pos)->next) /* Iterate backward over the elements of the list. */ #define cds_list_for_each_prev(pos, head) \ - for (pos = (head)->prev; pos != (head); pos = pos->prev) + for (pos = (head)->prev; (pos) != (head); pos = (pos)->prev) /* * Iterate backwards over the elements list. The list elements can be * removed from the list while doing this. */ #define cds_list_for_each_prev_safe(pos, p, head) \ - for (pos = (head)->prev, p = pos->prev; \ - pos != (head); \ - pos = p, p = pos->prev) + for (pos = (head)->prev, p = (pos)->prev; \ + (pos) != (head); \ + pos = (p), p = (pos)->prev) #define cds_list_for_each_entry(pos, head, member) \ - for (pos = cds_list_entry((head)->next, __typeof__(*pos), member); \ - &pos->member != (head); \ - pos = cds_list_entry(pos->member.next, __typeof__(*pos), member)) + for (pos = cds_list_entry((head)->next, __typeof__(*(pos)), member); \ + &(pos)->member != (head); \ + pos = cds_list_entry((pos)->member.next, __typeof__(*(pos)), member)) #define cds_list_for_each_entry_reverse(pos, head, member) \ - for (pos = cds_list_entry((head)->prev, __typeof__(*pos), member); \ - &pos->member != (head); \ - pos = cds_list_entry(pos->member.prev, __typeof__(*pos), member)) + for (pos = cds_list_entry((head)->prev, __typeof__(*(pos)), member); \ + &(pos)->member != (head); \ + pos = cds_list_entry((pos)->member.prev, __typeof__(*(pos)), member)) #define cds_list_for_each_entry_safe(pos, p, head, member) \ - for (pos = cds_list_entry((head)->next, __typeof__(*pos), member), \ - p = cds_list_entry(pos->member.next, __typeof__(*pos), member); \ - &pos->member != (head); \ - pos = p, p = cds_list_entry(pos->member.next, __typeof__(*pos), member)) + for (pos = cds_list_entry((head)->next, __typeof__(*(pos)), member), \ + p = cds_list_entry((pos)->member.next, __typeof__(*(pos)), member); \ + &(pos)->member != (head); \ + pos = (p), p = cds_list_entry((pos)->member.next, __typeof__(*(pos)), member)) /* * Same as cds_list_for_each_entry_safe, but starts from "pos" which should * point to an entry within the list. */ #define cds_list_for_each_entry_safe_from(pos, p, head, member) \ - for (p = cds_list_entry(pos->member.next, __typeof__(*pos), member); \ - &pos->member != (head); \ - pos = p, p = cds_list_entry(pos->member.next, __typeof__(*pos), member)) + for (p = cds_list_entry((pos)->member.next, __typeof__(*(pos)), member); \ + &(pos)->member != (head); \ + pos = (p), p = cds_list_entry((pos)->member.next, __typeof__(*(pos)), member)) static inline int cds_list_empty(struct cds_list_head *head) -- 2.34.1