diff mbox series

[2/5] qemu/qarray.h: weak scalar type check in QARRAY_CREATE()

Message ID a3f07198ba9e12b45ef38b45fa543e9b597ee70f.1629578875.git.qemu_oss@crudebyte.com (mailing list archive)
State New, archived
Headers show
Series introduce QArray | expand

Commit Message

Christian Schoenebeck Aug. 21, 2021, 8:30 p.m. UTC
Unfortunately something like

  _Static_assert(typeof(a) == typeof(b), "type mismatch");

is currently not suported by C. So for the time being at least
check that the size of the scalar types match at compile time.

Signed-off-by: Christian Schoenebeck <qemu_oss@crudebyte.com>
---
 include/qemu/qarray.h | 6 ++++++
 1 file changed, 6 insertions(+)

Comments

Richard Henderson Aug. 22, 2021, 4:11 a.m. UTC | #1
On 8/21/21 1:30 PM, Christian Schoenebeck wrote:
> Unfortunately something like
> 
>    _Static_assert(typeof(a) == typeof(b), "type mismatch");
> 
> is currently not suported by C. So for the time being at least
> check that the size of the scalar types match at compile time.

Did you try
_Static_assert(__builtin_types_compatible_p(X, Y), "type mismatch");


r~

> 
> Signed-off-by: Christian Schoenebeck <qemu_oss@crudebyte.com>
> ---
>   include/qemu/qarray.h | 6 ++++++
>   1 file changed, 6 insertions(+)
> 
> diff --git a/include/qemu/qarray.h b/include/qemu/qarray.h
> index 230a556e81..2cb8656c5d 100644
> --- a/include/qemu/qarray.h
> +++ b/include/qemu/qarray.h
> @@ -27,6 +27,8 @@
>   #ifndef QEMU_QARRAY_H
>   #define QEMU_QARRAY_H
>   
> +#include "qemu/compiler.h"
> +
>   /**
>    * QArray provides a mechanism to access arrays in common C-style (e.g. by
>    * square bracket [] operator) in conjunction with reference variables that
> @@ -143,6 +145,10 @@
>    * @param len - amount of array elements to be allocated immediately
>    */
>   #define QARRAY_CREATE(scalar_type, auto_var, len) \
> +    QEMU_BUILD_BUG_MSG( \
> +        sizeof(scalar_type) != sizeof(*auto_var), \
> +        "QArray scalar type mismatch" \
> +    ); \
>       qarray_create_##scalar_type((&auto_var), len)
>   
>   #endif /* QEMU_QARRAY_H */
>
Christian Schoenebeck Aug. 22, 2021, 12:16 p.m. UTC | #2
On Sonntag, 22. August 2021 06:11:58 CEST Richard Henderson wrote:
> On 8/21/21 1:30 PM, Christian Schoenebeck wrote:
> > Unfortunately something like
> > 
> >    _Static_assert(typeof(a) == typeof(b), "type mismatch");
> > 
> > is currently not suported by C. So for the time being at least
> > check that the size of the scalar types match at compile time.
> 
> Did you try
> _Static_assert(__builtin_types_compatible_p(X, Y), "type mismatch");
> 
> 
> r~

Ah, you are right. I was trying it, but now as you pointed me at it again, I 
realized I was just missing something. The specific use case here is like:

struct Foo {
  ...
} Foo;

Foo *var;

_Static_assert(__builtin_types_compatible_p(Foo, typeof(*var)),
               "type mismatch");

So I was missing the typeof() keyword to deduce the scalar type from the 
passed variable.

I'll send a v2.

Thanks!

Best regards,
Christian Schoenebeck
diff mbox series

Patch

diff --git a/include/qemu/qarray.h b/include/qemu/qarray.h
index 230a556e81..2cb8656c5d 100644
--- a/include/qemu/qarray.h
+++ b/include/qemu/qarray.h
@@ -27,6 +27,8 @@ 
 #ifndef QEMU_QARRAY_H
 #define QEMU_QARRAY_H
 
+#include "qemu/compiler.h"
+
 /**
  * QArray provides a mechanism to access arrays in common C-style (e.g. by
  * square bracket [] operator) in conjunction with reference variables that
@@ -143,6 +145,10 @@ 
  * @param len - amount of array elements to be allocated immediately
  */
 #define QARRAY_CREATE(scalar_type, auto_var, len) \
+    QEMU_BUILD_BUG_MSG( \
+        sizeof(scalar_type) != sizeof(*auto_var), \
+        "QArray scalar type mismatch" \
+    ); \
     qarray_create_##scalar_type((&auto_var), len)
 
 #endif /* QEMU_QARRAY_H */