From 2c18108561c0c3a8076e1ae29d76ce372e0fcb34 Mon Sep 17 00:00:00 2001 From: Eric Biggers Date: Thu, 21 May 2020 22:35:29 -0700 Subject: [PATCH] Use memcpy() for unaligned accesses For unaligned memory accesses, with modern compilers memcpy() is compiled just as efficiently as __attribute__((packed)). This also avoids using a nonstandard extension and potentially running into the gcc 10 bug https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94994. --- include/wimlib/unaligned.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/include/wimlib/unaligned.h b/include/wimlib/unaligned.h index 473f5e25..5700e405 100644 --- a/include/wimlib/unaligned.h +++ b/include/wimlib/unaligned.h @@ -21,25 +21,25 @@ #ifndef _WIMLIB_UNALIGNED_H #define _WIMLIB_UNALIGNED_H +#include + #include "wimlib/compiler.h" #include "wimlib/endianness.h" #include "wimlib/types.h" #define DEFINE_UNALIGNED_TYPE(type) \ -struct type##_unaligned { \ - type v; \ -} _packed_attribute _may_alias_attribute; \ - \ static forceinline type \ load_##type##_unaligned(const void *p) \ { \ - return ((const struct type##_unaligned *)p)->v; \ + type v; \ + memcpy(&v, p, sizeof(v)); \ + return v; \ } \ \ static forceinline void \ -store_##type##_unaligned(type val, void *p) \ +store_##type##_unaligned(type v, void *p) \ { \ - ((struct type##_unaligned *)p)->v = val; \ + memcpy(p, &v, sizeof(v)); \ } DEFINE_UNALIGNED_TYPE(u16); -- 2.43.0