From e8b92838feda8fa01b1323bd90f220c39078a499 Mon Sep 17 00:00:00 2001 From: Stefan Hajnoczi Date: Tue, 3 Jul 2018 01:02:46 -0400 Subject: [PATCH] compiler: add a sizeof_field() macro Determining the size of a field is useful when you don't have a struct variable handy. Open-coding this is ugly. This patch adds the sizeof_field() macro, which is similar to typeof_field(). Existing instances are updated to use the macro. Backports commit f18793b096e69c7acfce66cded483ba9fc01762a from qemu --- qemu/include/qemu/compiler.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/qemu/include/qemu/compiler.h b/qemu/include/qemu/compiler.h index 387bbdd8..41fa3d5d 100644 --- a/qemu/include/qemu/compiler.h +++ b/qemu/include/qemu/compiler.h @@ -28,6 +28,7 @@ #endif #endif +#define sizeof_field(type, field) sizeof(((type *)0)->field) static double rint( double x ) { @@ -166,6 +167,8 @@ static union MSVC_FLOAT_HACK __NAN = {{0x00, 0x00, 0xC0, 0x7F}}; #endif #endif +#define sizeof_field(type, field) sizeof(((type *)0)->field) + /* Convert from a base type to a parent type, with compile time checking. */ #ifdef __GNUC__ #define DO_UPCAST(type, field, dev) ( __extension__ ( { \