[Mesa-dev] [PATCH] util: add new ASSERT_BITFIELD_SIZE() macro (v3)

Eric Engestrom eric.engestrom at imgtec.com
Fri Nov 17 11:59:58 UTC 2017


On Thursday, 2017-11-16 11:17:57 -0700, Brian Paul wrote:
> For checking that bitfields are large enough to hold the largest
> expected value.
> 
> v2: move into existing util/macros.h header where STATIC_ASSERT() lives.
> v3: add MAYBE_UNUSED to variable declaration

Good shout on the MAYBE_UNUSED :)
Reviewed-by: Eric Engestrom <eric.engestrom at imgtec.com>

Could you add one (or more) user of this macro as well? I don't like
introducing unused functions/macros, it's too easy to forget about them
and let them bitrot (although it's unlikely this one would bitrot).

> ---
>  src/util/macros.h | 17 +++++++++++++++++
>  1 file changed, 17 insertions(+)
> 
> diff --git a/src/util/macros.h b/src/util/macros.h
> index a9a52a1..aa36253 100644
> --- a/src/util/macros.h
> +++ b/src/util/macros.h
> @@ -241,6 +241,23 @@ do {                       \
>  #define ATTRIBUTE_NOINLINE
>  #endif
>  
> +
> +/**
> + * Check that STRUCT::FIELD can hold MAXVAL.  We use a lot of bitfields
> + * in Mesa/gallium.  We have to be sure they're of sufficient size to
> + * hold the largest expected value.
> + * Note that with MSVC, enums are signed and enum bitfields need one extra
> + * high bit (always zero) to ensure the max value is handled correctly.
> + * This macro will detect that with MSVC, but not GCC.
> + */
> +#define ASSERT_BITFIELD_SIZE(STRUCT, FIELD, MAXVAL) \
> +   do { \
> +      MAYBE_UNUSED STRUCT s;                \
> +      s.FIELD = (MAXVAL); \
> +      assert((int) s.FIELD == (MAXVAL) && "Insufficient bitfield size!"); \
> +   } while (0)
> +
> +
>  /** Compute ceiling of integer quotient of A divided by B. */
>  #define DIV_ROUND_UP( A, B )  ( (A) % (B) == 0 ? (A)/(B) : (A)/(B)+1 )
>  
> -- 
> 1.9.1
> 


More information about the mesa-dev mailing list