[Spice-devel] [PATCH v3 2/2] Define and use new SPICE_MAGIC_CONST macro
Frediano Ziglio
fziglio at redhat.com
Wed Aug 12 03:42:46 PDT 2015
This macro allow to define magic constants without using weird
memory tweacks.
This remove some possible warning from some compiler and
make code more optimized as compiler is able to compute the
constant.
Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
---
spice/controller_prot.h | 3 ++-
spice/foreign_menu_prot.h | 3 ++-
spice/macros.h | 8 ++++++++
spice/protocol.h | 3 ++-
spice/qxl_dev.h | 5 +++--
spice/stats.h | 3 ++-
spice/vdi_dev.h | 3 ++-
7 files changed, 21 insertions(+), 7 deletions(-)
diff --git a/spice/controller_prot.h b/spice/controller_prot.h
index bca7804..91dbe6f 100644
--- a/spice/controller_prot.h
+++ b/spice/controller_prot.h
@@ -18,10 +18,11 @@
#ifndef _H_CONTROLLER_PROT
#define _H_CONTROLLER_PROT
+#include <spice/macros.h>
#include <spice/types.h>
#include <spice/start-packed.h>
-#define CONTROLLER_MAGIC (*(uint32_t*)"CTRL")
+#define CONTROLLER_MAGIC SPICE_MAGIC_CONST("CTRL")
#define CONTROLLER_VERSION 1
diff --git a/spice/foreign_menu_prot.h b/spice/foreign_menu_prot.h
index f478e2a..2ee2552 100644
--- a/spice/foreign_menu_prot.h
+++ b/spice/foreign_menu_prot.h
@@ -18,10 +18,11 @@
#ifndef _H_FOREIGN_MENU_PROT
#define _H_FOREIGN_MENU_PROT
+#include <spice/macros.h>
#include <spice/types.h>
#include <spice/start-packed.h>
-#define FOREIGN_MENU_MAGIC (*(uint32_t*)"FRGM")
+#define FOREIGN_MENU_MAGIC SPICE_MAGIC_CONST("FRGM")
#define FOREIGN_MENU_VERSION 1
typedef struct SPICE_ATTR_PACKED FrgMenuInitHeader {
diff --git a/spice/macros.h b/spice/macros.h
index 3538989..679d68d 100644
--- a/spice/macros.h
+++ b/spice/macros.h
@@ -414,4 +414,12 @@
#endif
+#if SPICE_ENDIAN == SPICE_ENDIAN_LITTLE
+#define SPICE_MAGIC_CONST(s) \
+ ((uint32_t)((s[0]&0xffu)|((s[1]&0xffu)<<8)|((s[2]&0xff)<<16)|((s[3]&0xffu)<<24)))
+#else
+#define SPICE_MAGIC_CONST(s) \
+ ((uint32_t)((s[3]&0xffu)|((s[2]&0xffu)<<8)|((s[1]&0xff)<<16)|((s[0]&0xffu)<<24)))
+#endif
+
#endif /* _H_SPICE_MACROS */
diff --git a/spice/protocol.h b/spice/protocol.h
index d3c5962..0c265ee 100644
--- a/spice/protocol.h
+++ b/spice/protocol.h
@@ -33,9 +33,10 @@
#include <spice/types.h>
#include <spice/enums.h>
+#include <spice/macros.h>
#include <spice/start-packed.h>
-#define SPICE_MAGIC (*(uint32_t*)"REDQ")
+#define SPICE_MAGIC SPICE_MAGIC_CONST("REDQ")
#define SPICE_VERSION_MAJOR 2
#define SPICE_VERSION_MINOR 2
diff --git a/spice/qxl_dev.h b/spice/qxl_dev.h
index a3e91a5..9e753c4 100644
--- a/spice/qxl_dev.h
+++ b/spice/qxl_dev.h
@@ -36,6 +36,7 @@
#include <spice/barrier.h>
#include <spice/ipc_ring.h>
#include <spice/enums.h>
+#include <spice/macros.h>
#include <spice/start-packed.h>
@@ -54,8 +55,8 @@ enum {
#define QXL_DEVICE_ID_DEVEL 0x01ff
#define QXL_REVISION_DEVEL 0x01
-#define QXL_ROM_MAGIC (*(const uint32_t*)"QXRO")
-#define QXL_RAM_MAGIC (*(const uint32_t*)"QXRA")
+#define QXL_ROM_MAGIC SPICE_MAGIC_CONST("QXRO")
+#define QXL_RAM_MAGIC SPICE_MAGIC_CONST("QXRA")
enum {
QXL_RAM_RANGE_INDEX,
diff --git a/spice/stats.h b/spice/stats.h
index 452d89e..46bbfed 100644
--- a/spice/stats.h
+++ b/spice/stats.h
@@ -31,11 +31,12 @@
#ifndef _H_SPICE_STATS
#define _H_SPICE_STATS
+#include <spice/macros.h>
#include <spice/types.h>
#define SPICE_STAT_SHM_NAME "/spice.%u"
#define SPICE_STAT_NODE_NAME_MAX 20
-#define SPICE_STAT_MAGIC (*(uint32_t*)"STAT")
+#define SPICE_STAT_MAGIC SPICE_MAGIC_CONST("STAT")
#define SPICE_STAT_VERSION 1
enum {
diff --git a/spice/vdi_dev.h b/spice/vdi_dev.h
index def88fc..a27f395 100644
--- a/spice/vdi_dev.h
+++ b/spice/vdi_dev.h
@@ -34,6 +34,7 @@
#include <spice/types.h>
#include <spice/barrier.h>
#include <spice/ipc_ring.h>
+#include <spice/macros.h>
#include <spice/start-packed.h>
@@ -48,7 +49,7 @@
#define VDI_PORT_INTERRUPT (1 << 0)
-#define VDI_PORT_MAGIC (*(uint32_t*)"VDIP")
+#define VDI_PORT_MAGIC SPICE_MAGIC_CONST("VDIP")
typedef struct SPICE_ATTR_PACKED VDIPortPacket {
uint32_t gen;
--
2.4.3
More information about the Spice-devel
mailing list