RE: [PATCH net-next v3 1/7] overflow: add DEFINE_FLEX() for on-stack allocs

From
David Laight <hidden>
Date
2023-08-17 14:35:33
Also in
intel-wired-lan, linux-hardening
Reply to
Przemek Kitszel, 2023-08-16
From: Przemek Kitszel
Sent: Wednesday, August 16, 2023 3:06 PM

Using underlying array for on-stack storage lets us to declare
known-at-compile-time structures without kzalloc().
Isn't DEFINE_FLEX() a bit misleading?
One thing it isn't is 'flexible' since it has a fixed size.
quoted hunk ↗ jump to hunk
+#define DEFINE_FLEX(type, name, member, count)					\
+	union {									\
+		u8 bytes[struct_size_t(type, member, count)];			\
+		type obj;							\
+	} name##_u __aligned(_Alignof(type)) = {};				\
You shouldn't need the _Alignof() it is the default.
I'm not sure you should be forcing the memset() either.
+	type *name = (type *)&name##_u
How about?
	type *const name = &name_##_u.obj;

You might want to add:
	Static_assert(is_constexpr(count), "DEFINE_FLEX: non-constant count " #count);

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
Thread (21 messages) 21 messages, 7 authors, last reply 2023-08-28
Keyboard shortcuts
hback out one level
jnext message in thread / next series-diff position
kprevious message in thread / previous series-diff position
ldrill in
Escclose help / fold thread tree
?toggle this help