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:28 +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 fb3c8f160dc580aa8ede55e1ad0fa525e5942ade..3ba9aeca0c075b10d36e116112b5a359c59b16ff 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.026338 seconds and 4 git commands to generate.