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

Eric Engestrom eric.engestrom at imgtec.com
Thu Nov 16 11:24:41 UTC 2017


On Wednesday, 2017-11-15 17:16:52 -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.
> ---
>  src/util/macros.h | 16 ++++++++++++++++
>  1 file changed, 16 insertions(+)
> 
> diff --git a/src/util/macros.h b/src/util/macros.h
> index a9a52a1..3fd0ffd 100644
> --- a/src/util/macros.h
> +++ b/src/util/macros.h
> @@ -68,6 +68,22 @@
>  
>  
>  /**
> + * 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 { \
> +      STRUCT s; \
> +      s.FIELD = (MAXVAL); \
> +      assert((int) s.FIELD == (MAXVAL) && "Insufficient bitfield size!"); \

I think this will give a 'set but unused var' warning on non-assert
builds; wrap the macro in `#ifndef NDEBUG`?

> +   } while (0)
> +
> +
> +/**
>   * Unreachable macro. Useful for suppressing "control reaches end of non-void
>   * function" warnings.
>   */
> -- 
> 1.9.1
> 


More information about the mesa-dev mailing list