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.14.1~21 X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=664d450341874fbbe6bc85176ed3336eaecb9f4c;p=urcu.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 827ec1f..09953f2 100644 --- a/include/urcu/compiler.h +++ b/include/urcu/compiler.h @@ -74,6 +74,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))