qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Felix Wu <flwu@google.com>
To: peter.maydell@linaro.org
Cc: qemu-devel@nongnu.org, Roman Kiryanov <rkir@google.com>,
	Felix Wu <flwu@google.com>
Subject: [PATCH 1/1] include/qemu: Provide a C++ compatible version of typeof_strip_qual
Date: Mon, 24 Jun 2024 20:56:47 +0000	[thread overview]
Message-ID: <20240624205647.112034-1-flwu@google.com> (raw)

From: Roman Kiryanov <rkir@google.com>

to use the QEMU headers with a C++ compiler.

Signed-off-by: Felix Wu <flwu@google.com>
Signed-off-by: Roman Kiryanov <rkir@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 <rkir@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



             reply	other threads:[~2024-06-24 20:57 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-24 20:56 Felix Wu [this message]
2024-06-25  2:26 ` [PATCH 1/1] include/qemu: Provide a C++ compatible version of typeof_strip_qual Philippe Mathieu-Daudé
2024-06-25  2:32   ` Roman Kiryanov
2024-06-25  6:05     ` Paolo Bonzini
2024-06-25  6:19       ` Philippe Mathieu-Daudé
2024-06-25  9:27         ` Peter Maydell
2024-06-25 10:16           ` Peter Maydell
2024-06-25 10:31             ` Daniel P. Berrangé
2024-06-25  6:07     ` Thomas Huth
2024-06-25  7:52 ` Daniel P. Berrangé

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240624205647.112034-1-flwu@google.com \
    --to=flwu@google.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.org \
    --cc=rkir@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).