DEFINE_FLEX(9)

πŸ“¦ version 7.3.1, in nixpkgs unstable

πŸ’… 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_by member πŸ”’
  • 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 of NAME members ⭐
  • 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