# 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 
- List: netdev
- Page: https://ratatoskr.run/netdev/2023/08/11269836
- Thread: https://ratatoskr.run/netdev/2023/08/11269614/t.md

```text
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.

> +#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)

```
