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

Brian Paul brianp at vmware.com
Thu Nov 16 00:16:52 UTC 2017


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!"); \
+   } 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