]> wimlib.net Git - wimlib/blobdiff - include/wimlib/unaligned.h
LZX, LZMS: Annotate unaligned memory accesses in x86 filtering
[wimlib] / include / wimlib / unaligned.h
diff --git a/include/wimlib/unaligned.h b/include/wimlib/unaligned.h
new file mode 100644 (file)
index 0000000..d30c9f2
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * unaligned.h
+ *
+ * Inline functions for unaligned memory accesses.
+ *
+ * The author dedicates this file to the public domain.
+ * You can do whatever you want with this file.
+ */
+
+#ifndef _WIMLIB_UNALIGNED_H
+#define _WIMLIB_UNALIGNED_H
+
+#include "compiler.h"
+#include "endianness.h"
+#include "types.h"
+
+#define DEFINE_UNALIGNED_TYPE(type)                            \
+struct type##_unaligned {                                      \
+       type v;                                                 \
+} _packed_attribute;                                           \
+                                                               \
+static inline type                                             \
+load_##type##_unaligned(const void *p)                         \
+{                                                              \
+       return ((const struct type##_unaligned *)p)->v;         \
+}                                                              \
+                                                               \
+static inline void                                             \
+store_##type##_unaligned(type val, void *p)                    \
+{                                                              \
+       ((struct type##_unaligned *)p)->v = val;                \
+}
+
+DEFINE_UNALIGNED_TYPE(le16);
+DEFINE_UNALIGNED_TYPE(le32);
+DEFINE_UNALIGNED_TYPE(le64);
+
+#endif /* _WIMLIB_UNALIGNED_H */