[Spice-devel] [PATCH 2/2] Define and use new SPICE_MAGIC_CONST macro

Frediano Ziglio fziglio at redhat.com
Fri Aug 7 05:29:57 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   | 2 +-
 spice/foreign_menu_prot.h | 2 +-
 spice/macros.h            | 6 ++++++
 spice/protocol.h          | 2 +-
 spice/qxl_dev.h           | 4 ++--
 spice/stats.h             | 2 +-
 spice/vdi_dev.h           | 2 +-
 7 files changed, 13 insertions(+), 7 deletions(-)

diff --git a/spice/controller_prot.h b/spice/controller_prot.h
index bca7804..a21bf57 100644
--- a/spice/controller_prot.h
+++ b/spice/controller_prot.h
@@ -21,7 +21,7 @@
 #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..9dd882d 100644
--- a/spice/foreign_menu_prot.h
+++ b/spice/foreign_menu_prot.h
@@ -21,7 +21,7 @@
 #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 018ff32..4647ee3 100644
--- a/spice/macros.h
+++ b/spice/macros.h
@@ -359,5 +359,11 @@
 #  define SPICE_BYTESWAP64(val) (SPICE_BYTESWAP64_CONSTANT (val))
 #endif /* generic */
 
+#if SPICE_ENDIAN == SPICE_ENDIAN_LITTLE
+#define SPICE_MAGIC_CONST(s) ((uint32_t)(s[0]+s[1]*0x100u+s[2]*0x10000u+s[3]*0x1000000u))
+#else
+#define SPICE_MAGIC_CONST(s) ((uint32_t)(s[3]+s[2]*0x100u+s[1]*0x10000u+s[0]*0x1000000u))
+#endif
+
 
 #endif /* _H_SPICE_MACROS */
diff --git a/spice/protocol.h b/spice/protocol.h
index d3c5962..68f45c3 100644
--- a/spice/protocol.h
+++ b/spice/protocol.h
@@ -35,7 +35,7 @@
 #include <spice/enums.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..30b608e 100644
--- a/spice/qxl_dev.h
+++ b/spice/qxl_dev.h
@@ -54,8 +54,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..93326b5 100644
--- a/spice/stats.h
+++ b/spice/stats.h
@@ -35,7 +35,7 @@
 
 #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..07c3697 100644
--- a/spice/vdi_dev.h
+++ b/spice/vdi_dev.h
@@ -48,7 +48,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.1.0



More information about the Spice-devel mailing list