From: Jérémie Galarneau Date: Fri, 10 Jun 2022 19:10:32 +0000 (-0400) Subject: common: replace container_of with a C++ safe implementation X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=commitdiff_plain;h=0114db0ec2407029052eb61a0189c9b1cd64d520;hp=0114db0ec2407029052eb61a0189c9b1cd64d520 common: replace container_of with a C++ safe implementation As more code moves to a more idiomatic C++ style, structures like typically end up becoming classes that use different access controls, virtual functions, etc. This, in turn, makes them adopt a non standard layout and causes GCC and clang to emit the following warning when container_of is used: error: 'offsetof' within non-standard-layout type 'foo' is conditionally-supported [-Werror=invalid-offsetof] This new implementation of container_of makes use of a pointer to a data member to find the parent's address. The use of ptr_to_member against the null dummy_parent makes me uneasy as it seems equivalent to performing arithmetic on a null pointer, which I understand is undefined behavior (C++11 Standard 5.7.5). However, Boost.Instrusive uses an approach that seems roughly equivalent to lttng::utils::container_of() [1]. It seems like a reasonable compromise that works on all mainstream compilers. [1] https://github.com/boostorg/intrusive/blob/3c5c8cec3f0356a028a4b56ba6cac2256340dab1/include/boost/intrusive/detail/parent_from_member.hpp#L92 Change-Id: Ia6287e1648bce85dfe6de936f17ec5df46ea648d Signed-off-by: Jérémie Galarneau ---