summary |
shortlog |
log |
commit | commitdiff |
tree
raw |
patch |
inline | side by side (from parent 1:
bdd51ae)
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.
#ifndef _WIMLIB_UNALIGNED_H
#define _WIMLIB_UNALIGNED_H
#ifndef _WIMLIB_UNALIGNED_H
#define _WIMLIB_UNALIGNED_H
#include "wimlib/compiler.h"
#include "wimlib/endianness.h"
#include "wimlib/types.h"
#define DEFINE_UNALIGNED_TYPE(type) \
#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) \
{ \
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 \
} \
\
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);
}
DEFINE_UNALIGNED_TYPE(u16);