[PATCH spice-common v2] canvas_base: Avoid misaligned access decoding LZ4 data

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Make code faster on platforms not supporting unaligned access by
default.
SPICE_UNALIGNED_CAST is just silencing possible alignment warning and,
if enabled, produces some logs, however in this case we know that the
pointer can be misaligned.
Use packed structures to tell compiler to generate best code possible.
For more details see comment on commit 74e50b57ae05116be4e2 ("Make the
compiler work out better way to write unaligned memory").

Signed-off-by: Frediano Ziglio <fziglio@xxxxxxxxxx>
---
 common/canvas_base.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

Changes since v1:
- extends commit message.

diff --git a/common/canvas_base.c b/common/canvas_base.c
index 6bf6e5d..36546b3 100644
--- a/common/canvas_base.c
+++ b/common/canvas_base.c
@@ -45,6 +45,14 @@
 #include "mem.h"
 #include "macros.h"
 
+#include <spice/start-packed.h>
+typedef struct SPICE_ATTR_PACKED {
+    uint32_t v;
+} uint32_unaligned_t;
+#include <spice/end-packed.h>
+
+#define read_uint32_be(ptr) ntohl(((uint32_unaligned_t *)(ptr))->v)
+
 #define ROUND(_x) ((int)floor((_x) + 0.5))
 
  static inline int fix_to_int(SPICE_FIXED28_4 fixed)
@@ -572,7 +580,7 @@ static pixman_image_t *canvas_get_lz4(CanvasBase *canvas, SpiceImage *image)
 
     do {
         // Read next compressed block
-        enc_size = ntohl(*SPICE_UNALIGNED_CAST(uint32_t *, data));
+        enc_size = read_uint32_be(data);
         data += 4;
         dec_size = LZ4_decompress_safe_continue(stream, (const char *) data,
                                                 (char *) dest, enc_size, available);
-- 
2.17.1

_______________________________________________
Spice-devel mailing list
Spice-devel@xxxxxxxxxxxxxxxxxxxxx
https://lists.freedesktop.org/mailman/listinfo/spice-devel




[Index of Archives]     [Linux Virtualization]     [Linux Virtualization]     [Linux ARM Kernel]     [Linux ARM]     [Linux Omap]     [Fedora ARM]     [IETF Annouce]     [Security]     [Bugtraq]     [Linux OMAP]     [Linux MIPS]     [ECOS]     [Asterisk Internet PBX]     [Linux API]     [Monitors]