From: Mathieu Desnoyers Date: Mon, 3 Jul 2023 15:17:04 +0000 (-0400) Subject: compiler.h: Introduce caa_container_of_check_null X-Git-Tag: v0.13.4~17 X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=2a17fe10ad97e997981a98463a4cbda9b4a933a5;p=userspace-rcu.git compiler.h: Introduce caa_container_of_check_null The approach taken by caa_unqual_scalar_typeof requires use of _Generic which requires full C11 support. Currently liburcu supports C99. Therefore, this approach is not appropriate for now. Instead, introduce caa_container_of_check_null which returns NULL if the ptr is NULL before offsetting by the member offset. Signed-off-by: Mathieu Desnoyers Change-Id: I0ac1cacc67d83bd3dad6fb6cd2e6595190735441 --- diff --git a/include/urcu/compiler.h b/include/urcu/compiler.h index fb3c8f1..3ba9aec 100644 --- a/include/urcu/compiler.h +++ b/include/urcu/compiler.h @@ -70,6 +70,23 @@ (type *)((char *)__ptr - offsetof(type, member)); \ }) +/* + * caa_container_of_check_null - Get the address of an object containing a field. + * + * @ptr: pointer to the field. + * @type: type of the object. + * @member: name of the field within the object. + * + * Return the address of the object containing the field. Return NULL if + * @ptr is NULL. + */ +#define caa_container_of_check_null(ptr, type, member) \ + __extension__ \ + ({ \ + const __typeof__(((type *) NULL)->member) * __ptr = (ptr); \ + (__ptr) ? (type *)((char *)__ptr - offsetof(type, member)) : NULL; \ + }) + #define CAA_BUILD_BUG_ON_ZERO(cond) (sizeof(struct { int:-!!(cond); })) #define CAA_BUILD_BUG_ON(cond) ((void)CAA_BUILD_BUG_ON_ZERO(cond))