aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorGao Xiang <hsiangkao@linux.alibaba.com>2023-07-10 02:25:08 +0800
committerGao Xiang <hsiangkao@linux.alibaba.com>2023-07-10 15:48:28 +0800
commitbc99c763e3fe4aeb5a02383d954caca4a2e1aa29 (patch)
treeeb87f9cbc18b4e9d3d715636fc0f9049d07a25df /include
parent5de439566bc5b5b7e78fc3eac2c79ceae9d27f62 (diff)
downloaderofs-utils-bc99c763e3fe4aeb5a02383d954caca4a2e1aa29.tar.gz
erofs-utils: switch to effective unaligned access
In order to prepare for LZ77 matchfinder. Note that erofs_memcmp2() is still not quite effective. Signed-off-by: Gao Xiang <hsiangkao@linux.alibaba.com> Link: https://lore.kernel.org/r/20230709182511.96954-2-hsiangkao@linux.alibaba.com
Diffstat (limited to 'include')
-rw-r--r--include/erofs/defs.h24
-rw-r--r--include/erofs/internal.h2
2 files changed, 22 insertions, 4 deletions
diff --git a/include/erofs/defs.h b/include/erofs/defs.h
index e5aa23c..44af557 100644
--- a/include/erofs/defs.h
+++ b/include/erofs/defs.h
@@ -179,9 +179,29 @@ typedef int64_t s64;
#define __maybe_unused __attribute__((__unused__))
#endif
-static inline u32 get_unaligned_le32(const u8 *p)
+#define __packed __attribute__((__packed__))
+
+#define __get_unaligned_t(type, ptr) ({ \
+ const struct { type x; } __packed *__pptr = (typeof(__pptr))(ptr); \
+ __pptr->x; \
+})
+
+#define __put_unaligned_t(type, val, ptr) do { \
+ struct { type x; } __packed *__pptr = (typeof(__pptr))(ptr); \
+ __pptr->x = (val); \
+} while (0)
+
+#define get_unaligned(ptr) __get_unaligned_t(typeof(*(ptr)), (ptr))
+#define put_unaligned(val, ptr) __put_unaligned_t(typeof(*(ptr)), (val), (ptr))
+
+static inline u32 get_unaligned_le32(const void *p)
+{
+ return le32_to_cpu(__get_unaligned_t(__le32, p));
+}
+
+static inline void put_unaligned_le32(u32 val, void *p)
{
- return p[0] | p[1] << 8 | p[2] << 16 | p[3] << 24;
+ __put_unaligned_t(__le32, cpu_to_le32(val), p);
}
/**
diff --git a/include/erofs/internal.h b/include/erofs/internal.h
index ab964d4..aad2115 100644
--- a/include/erofs/internal.h
+++ b/include/erofs/internal.h
@@ -17,8 +17,6 @@ extern "C"
typedef unsigned short umode_t;
-#define __packed __attribute__((__packed__))
-
#include "erofs_fs.h"
#include <fcntl.h>
#include <sys/types.h> /* for off_t definition */