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)