DEFINE_FLEX(9)
π DEFINE_FLEX(9) β the Kernel API Manual one, bestie
okay so real talk, this is DEFINE_FLEX from section 9 of the kernel API manual, and honestly? it's giving "make me a struct on the stack with a flexible array at the end and zero it out for me" β¨ and we're gonna learn it together π«Ά
β¨ NAME β¨
DEFINE_FLEX β define an on-stack instance of structure with a trailing flexible array member. literally that's the whole vibe π
πΈ SYNOPSIS πΈ
DEFINE_FLEX (TYPE , NAME , MEMBER , COUNTER , COUNT );
π ARGUMENTS π
TYPEβ the structure type name, and yes bestie, that includes the"struct"keyword, don't forget it πNAMEβ the name for the variable you're defining π·οΈMEMBERβ the name of the array member πCOUNTERβ the name of the__counted_bymember π’COUNTβ how many elements are in the array; it must be compile-time const, no cap, this one's strict π
π¦ DESCRIPTION π¦
So it defines a zeroed, on-stack instance of your TYPE structure, with a trailing flexible array member π β the whole thing just appears, zeroed and ready, main character energy fr fr.
Then when you wanna know stuff about it, the tooling is right there:
- use
__struct_size(NAME)to get the compile-time size of it afterwards π - use
__member_size(NAME->member)to get the compile-time size ofNAMEmembers β - use
STACK_FLEX_ARRAY_SIZE(name, member)to get the compile-time number of elements in the array member π
it's iconic, it's convenient, it's on the stack, periodt π
π₯ SEE ALSO π₯
the kernel file ./include/linux/overflow.h β and omg the friend group is HUGE, ok here we go, every single one of them:
check_add_overflow(9), wrapping_add(9), wrapping_assign_add(9), check_sub_overflow(9), wrapping_sub(9), wrapping_assign_sub(9), check_mul_overflow(9), wrapping_mul(9), check_shl_overflow(9), overflows_type(9), range_overflows(9), range_overflows_t(9), range_end_overflows(9), range_end_overflows_t(9), castable_to_type(9), size_mul(9), size_add(9), size_sub(9), array_size(9), array3_size(9), flex_array_size(9), struct_size(9), struct_size_t(9), struct_offset(9), __DEFINE_FLEX(9), _DEFINE_FLEX(9), DEFINE_RAW_FLEX(9), STACK_FLEX_ARRAY_SIZE(9), typeof_flex_counter(9), overflows_flex_counter_type(9), __set_flex_counter(9)
like damn, that's a whole group chat ππ
and that's the page, bestie π§ this came from ./include/linux and the lovely Kernel API Manual, dated October 2026 β catch you on the next one, mwah π
π
π¦ where this page comes from
- nixpkgs unstable: linux-manual 7.3.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms