Re: [PATCH v2 1/2] stddef: Add offsetofend() variant of struct_offset()

From: Alejandro Colomar

Date: Thu Nov 12 2026 - 06:16:49 EST


Hi Bence,

> Date: 2026-11-12 10:29:33+0100
> From: Bence Csokas <bence.csokas@xxxxxxx>
>
> Many call sites of offsetofend() use it for the purpose of pointer
> artihmetic. Thus, a common usage pattern is:
>
> offsetofend(typeof(*ptr), member)
>
> Capturing this in one macro helps keep lines short and terse, while
> still making the purpose clear. It also helps discourage spelling out
> the type name on every callsite, leading to more maintainable code.
>
> A similar struct_offset() macro already exists, so add an offsetof()
> variant in the same spirit.
>
> Signed-off-by: Bence Csokas <bence.csokas@xxxxxxx>
> ---
> include/linux/overflow.h | 12 ++++++++++++
> 1 file changed, 12 insertions(+)
>
> diff --git a/include/linux/overflow.h b/include/linux/overflow.h
> index d0f04436343e..060dad4b6f3e 100644
> --- a/include/linux/overflow.h
> +++ b/include/linux/overflow.h
> @@ -464,6 +464,18 @@ static __always_inline size_t __must_check size_sub(size_t minuend, size_t subtr
> */
> #define struct_offset(p, member) (offsetof(typeof(*(p)), member))
>
> +/**
> + * struct_offsetofend() - Calculate the end of a member within a struct
> + * @p: Pointer to the struct
> + * @member: Name of the member to get the end offset of
> + *
> + * Calculates the offset of the end of a particular @member of the structure
> + * pointed to by @p.
> + *
> + * Return: number of bytes to the location of @member.
> + */
> +#define struct_offsetofend(p, member) (offsetofend(typeof(*(p)), member))

The outer parentheses are unnecessary (they are unnecessary in
struct_offset() too, but that already exists).


Have a lovely day!
Alex

> +
> /**
> * __DEFINE_FLEX() - helper macro for DEFINE_FLEX() family.
> * Enables caller macro to pass arbitrary trailing expressions
>
> --
> 2.56.1
>
>

--
<https://www.alejandro-colomar.es>

Attachment: signature.asc
Description: PGP signature