From: Roman Kiryanov <r...@google.com> to use the QEMU headers with a C++ compiler.
Signed-off-by: Felix Wu <f...@google.com> Signed-off-by: Roman Kiryanov <r...@google.com> --- include/qemu/atomic.h | 8 ++++++++ include/qemu/atomic.hpp | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 include/qemu/atomic.hpp diff --git a/include/qemu/atomic.h b/include/qemu/atomic.h index 99110abefb..aeaecc440a 100644 --- a/include/qemu/atomic.h +++ b/include/qemu/atomic.h @@ -20,6 +20,13 @@ /* Compiler barrier */ #define barrier() ({ asm volatile("" ::: "memory"); (void)0; }) +#ifdef __cplusplus + +#ifndef typeof_strip_qual +#error Use the typeof_strip_qual(expr) definition from atomic.hpp on C++ builds. +#endif + +#else /* __cpluplus */ /* The variable that receives the old value of an atomically-accessed * variable must be non-qualified, because atomic builtins return values * through a pointer-type argument as in __atomic_load(&var, &old, MODEL). @@ -61,6 +68,7 @@ __builtin_types_compatible_p(typeof(expr), const volatile unsigned short), \ (unsigned short)1, \ (expr)+0)))))) +#endif /* __cpluplus */ #ifndef __ATOMIC_RELAXED #error "Expecting C11 atomic ops" diff --git a/include/qemu/atomic.hpp b/include/qemu/atomic.hpp new file mode 100644 index 0000000000..5844e3d427 --- /dev/null +++ b/include/qemu/atomic.hpp @@ -0,0 +1,38 @@ +/* + * The C++ definition for typeof_strip_qual used in atomic.h. + * + * Copyright (C) 2024 Google, Inc. + * + * Author: Roman Kiryanov <r...@google.com> + * + * This work is licensed under the terms of the GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + * + * See docs/devel/atomics.rst for discussion about the guarantees each + * atomic primitive is meant to provide. + */ + +#ifndef QEMU_ATOMIC_HPP +#define QEMU_ATOMIC_HPP + +#include <type_traits> + +/* Match the integer promotion behavior of typeof_strip_qual, see atomic.h */ +template <class T> struct typeof_strip_qual_cpp { using result = decltype(+T(0)); }; + +template <> struct typeof_strip_qual_cpp<bool> { using result = bool; }; +template <> struct typeof_strip_qual_cpp<signed char> { using result = signed char; }; +template <> struct typeof_strip_qual_cpp<unsigned char> { using result = unsigned char; }; +template <> struct typeof_strip_qual_cpp<signed short> { using result = signed short; }; +template <> struct typeof_strip_qual_cpp<unsigned short> { using result = unsigned short; }; + +#define typeof_strip_qual(expr) \ + typeof_strip_qual_cpp< \ + std::remove_cv< \ + std::remove_reference< \ + decltype(expr) \ + >::type \ + >::type \ + >::result + +#endif /* QEMU_ATOMIC_HPP */ -- 2.45.2.741.gdbec12cfda-goog