[PATCH 0/2] stddef: Simplify TRAILING_OVERLAP() and improve docs

Uwe Kleine-König posted 2 patches 2 weeks, 6 days ago
include/linux/stddef.h | 14 ++++++++++++--
1 file changed, 12 insertions(+), 2 deletions(-)
[PATCH 0/2] stddef: Simplify TRAILING_OVERLAP() and improve docs
Posted by Uwe Kleine-König 2 weeks, 6 days ago
Hello,

as a follup to the discussion I had with Gustavo in reply to
https://lore.kernel.org/linux-pwm/aJtRPZpc-Lv-C6zD@kspp here comes my
suggestion to improve TRAILING_OVERLAP() for wider audience.

While working at it, I wonder if __packed should also better be part of the
macro to ensure that


	struct myfamstruct {
		char c;
		char payload[];
	};

	TRAILING_OVERLAP(struct myfamstruct, mfs, payload,
		unsigned int payload_as_int;
	) fs;

does the right thing. This however is orthogonal and better discussed
separately from this patch set.

Best regards
Uwe

Uwe Kleine-König (2):
  stddef: Add a comment about why TRAILING_OVERLAP() exists
  stddef: Stop using FAM parameter of TRAILING_OVERLAP()

 include/linux/stddef.h | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)


base-commit: 8f5ae30d69d7543eee0d70083daf4de8fe15d585
-- 
2.50.1

Re: [PATCH 0/2] stddef: Simplify TRAILING_OVERLAP() and improve docs
Posted by Gustavo A. R. Silva 2 weeks, 6 days ago

On 9/12/25 12:28, Uwe Kleine-König wrote:
> Hello,
> 
> as a follup to the discussion I had with Gustavo in reply to
> https://lore.kernel.org/linux-pwm/aJtRPZpc-Lv-C6zD@kspp here comes my
> suggestion to improve TRAILING_OVERLAP() for wider audience.
> 
> While working at it, I wonder if __packed should also better be part of the
> macro to ensure that

I have patch ready for this:

diff --git a/include/linux/stddef.h b/include/linux/stddef.h
index dab49e2ec8c0..b20ff76778d5 100644
--- a/include/linux/stddef.h
+++ b/include/linux/stddef.h
@@ -93,6 +93,14 @@ enum {
  #define DECLARE_FLEX_ARRAY(TYPE, NAME) \
         __DECLARE_FLEX_ARRAY(TYPE, NAME)

+#define __TRAILING_OVERLAP(TYPE, NAME, FAM, MEMBERS, ATTRS)                            \
+       union {                                                                 \
+               TYPE NAME;                                                      \
+               struct {                                                        \
+                       unsigned char __offset_to_##FAM[offsetof(TYPE, FAM)];   \
+                       MEMBERS                                                 \
+               } ATTRS;                                                                \
+       }
  /**
   * TRAILING_OVERLAP() - Overlap a flexible-array member with trailing members.
   *

Thanks
-Gustavo