compiler.h: Introduce caa_container_of_check_null
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Mon, 3 Jul 2023 15:17:04 +0000 (11:17 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Mon, 3 Jul 2023 15:32:17 +0000 (11:32 -0400)
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 <mathieu.desnoyers@efficios.com>
Change-Id: I0ac1cacc67d83bd3dad6fb6cd2e6595190735441

include/urcu/compiler.h

index 827ec1fd6eee0d00d5e863f9268fa89b7ae2d59e..09953f2c2b96eeb5dfd27e04b43825a717d1e38c 100644 (file)
                (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))
 
This page took 0.027145 seconds and 4 git commands to generate.