[PATCH v4 1/3] tools: add sizeof_field and offsetof_end macros

Petr Beneš posted 3 patches 3 months ago
There is a newer version of this series
[PATCH v4 1/3] tools: add sizeof_field and offsetof_end macros
Posted by Petr Beneš 3 months ago
From: Petr Beneš <w1benny@gmail.com>

* `sizeof_field` returns the size of a specific struct member
* `offsetof_end` returns the offset to the end of the member within the struct

It will be useful in upcoming layout checks of SMBIOS structs.

Signed-off-by: Petr Beneš <w1benny@gmail.com>
---
 tools/include/xen-tools/common-macros.h | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/tools/include/xen-tools/common-macros.h b/tools/include/xen-tools/common-macros.h
index 0088208c2e..3899e06753 100644
--- a/tools/include/xen-tools/common-macros.h
+++ b/tools/include/xen-tools/common-macros.h
@@ -83,6 +83,11 @@
 #define __packed __attribute__((__packed__))
 #endif
 
+#define sizeof_field(type, member) sizeof(((type *)NULL)->member)
+
+#define offsetof_end(type, member) \
+    (offsetof(type, member) + sizeof_field(type, member))
+
 #define container_of(ptr, type, member) ({              \
     typeof(((type *)0)->member) *mptr__ = (ptr);        \
     (type *)((char *)mptr__ - offsetof(type, member));  \
-- 
2.34.1


Re: [PATCH v4 1/3] tools: add sizeof_field and offsetof_end macros
Posted by Andrew Cooper 2 months, 1 week ago
On 30/07/2025 10:56 am, Petr Beneš wrote:
> From: Petr Beneš <w1benny@gmail.com>
>
> * `sizeof_field` returns the size of a specific struct member
> * `offsetof_end` returns the offset to the end of the member within the struct
>
> It will be useful in upcoming layout checks of SMBIOS structs.
>
> Signed-off-by: Petr Beneš <w1benny@gmail.com>
> ---
>  tools/include/xen-tools/common-macros.h | 5 +++++
>  1 file changed, 5 insertions(+)
>
> diff --git a/tools/include/xen-tools/common-macros.h b/tools/include/xen-tools/common-macros.h
> index 0088208c2e..3899e06753 100644
> --- a/tools/include/xen-tools/common-macros.h
> +++ b/tools/include/xen-tools/common-macros.h
> @@ -83,6 +83,11 @@
>  #define __packed __attribute__((__packed__))
>  #endif
>  
> +#define sizeof_field(type, member) sizeof(((type *)NULL)->member)
> +
> +#define offsetof_end(type, member) \
> +    (offsetof(type, member) + sizeof_field(type, member))

In Xen, we have this called endof_field()  (although I realise doing
this post-dated your patch).

Please could you adjust, and send out a full series.  I see there were
some minor changes needed in patch 2.

Also, please add a final patch which adds to CHANGELOG.md in the root
directly.  You want a bullet point under Added/x86 about this.

~Andrew