[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH] Use unaligned memory accesses unconditionally
From: |
Fangrui Song |
Subject: |
[PATCH] Use unaligned memory accesses unconditionally |
Date: |
Wed, 2 Aug 2023 17:18:46 +0000 |
From: Fangrui Song <i@maskray.me>
Currently ALIGNMENT_WORKAROUND is only defined for __ia64__ and __arm__.
However, -fsanitize=alignment (part of UndefinedBehaviorSanitizer) will
give errors for other architectures like x86. Modern compilers are able
to optimize the memory access, so let's just use unaligned memory
accesses unconditionally.
---
Makefile | 1 -
config.h | 5 -----
types.h | 12 ------------
3 files changed, 18 deletions(-)
diff --git a/Makefile b/Makefile
index 7aa729d..3e83805 100644
--- a/Makefile
+++ b/Makefile
@@ -25,7 +25,6 @@ CFLAGS += -W -Wall -Wshadow -Wstrict-prototypes
-Wpointer-arith -Wcast-qual \
CFLAGS += -D_FILE_OFFSET_BITS=64
#CFLAGS += -DBIGENDIAN
-#CFLAGS += -DALIGNMENT_WORKAROUND
# Pass linker flags here (can be set from environment too)
LDFLAGS ?=
diff --git a/config.h b/config.h
index 0a1af7d..416d4a6 100644
--- a/config.h
+++ b/config.h
@@ -21,11 +21,6 @@
#define USE_MMAP
#endif
-/* Use memory alignment workaround or not */
-#if defined(__ia64__) || defined(__arm__)
-#define ALIGNMENT_WORKAROUND
-#endif
-
/* Avoid unaligned memcpy on /dev/mem */
#ifdef __aarch64__
#define USE_SLOW_MEMCPY
diff --git a/types.h b/types.h
index 51c32d7..d445cd6 100644
--- a/types.h
+++ b/types.h
@@ -12,10 +12,6 @@ typedef unsigned int u32;
* You may use the following defines to adjust the type definitions
* depending on the architecture:
* - Define BIGENDIAN on big-endian systems.
- * - Define ALIGNMENT_WORKAROUND if your system doesn't support
- * non-aligned memory access. In this case, we use a slower, but safer,
- * memory access method. This should be done automatically in config.h
- * for architectures which need it.
*/
#ifdef BIGENDIAN
@@ -30,7 +26,6 @@ typedef struct {
} u64;
#endif
-#if defined(ALIGNMENT_WORKAROUND) || defined(BIGENDIAN)
static inline u64 U64(u32 low, u32 high)
{
u64 self;
@@ -40,20 +35,13 @@ static inline u64 U64(u32 low, u32 high)
return self;
}
-#endif
/*
* Per SMBIOS v2.8.0 and later, all structures assume a little-endian
* ordering convention.
*/
-#if defined(ALIGNMENT_WORKAROUND) || defined(BIGENDIAN)
#define WORD(x) (u16)((x)[0] + ((x)[1] << 8))
#define DWORD(x) (u32)((x)[0] + ((x)[1] << 8) + ((x)[2] << 16) + ((x)[3] <<
24))
#define QWORD(x) (U64(DWORD(x), DWORD(x + 4)))
-#else /* ALIGNMENT_WORKAROUND || BIGENDIAN */
-#define WORD(x) (u16)(*(const u16 *)(x))
-#define DWORD(x) (u32)(*(const u32 *)(x))
-#define QWORD(x) (*(const u64 *)(x))
-#endif /* ALIGNMENT_WORKAROUND || BIGENDIAN */
#endif
--
2.41.0.585.gd2178a4bd4-goog
- [PATCH] Use unaligned memory accesses unconditionally,
Fangrui Song <=