mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-21 23:20:20 +00:00
AK: Add load64 and load_pointer to AK::ByteReader
This lets us load misaligned 64 bit integers, and misaligned pointers in a platform agnostic way.
This commit is contained in:
parent
19d34414bc
commit
fac4eab415
Notes:
sideshowbarker
2024-07-18 09:11:17 +09:00
Author: https://github.com/ADKaster Commit: https://github.com/SerenityOS/serenity/commit/fac4eab415c Pull-request: https://github.com/SerenityOS/serenity/pull/8633 Issue: https://github.com/SerenityOS/serenity/issues/7158 Issue: https://github.com/SerenityOS/serenity/issues/8629
1 changed files with 28 additions and 0 deletions
|
@ -49,6 +49,27 @@ struct ByteReader {
|
|||
value = v._32;
|
||||
}
|
||||
|
||||
static void load(const u8* address, u64& value)
|
||||
{
|
||||
union {
|
||||
u64 _64;
|
||||
u8 _8[8];
|
||||
} v { ._64 = 0 };
|
||||
__builtin_memcpy(&v._8, address, 8);
|
||||
value = v._64;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
static T* load_pointer(const u8* address)
|
||||
{
|
||||
if constexpr (sizeof(T*) == 4) {
|
||||
return reinterpret_cast<T*>(load32(address));
|
||||
} else {
|
||||
static_assert(sizeof(T*) == 8, "sizeof(T*) must be either 4 or 8");
|
||||
return reinterpret_cast<T*>(load64(address));
|
||||
}
|
||||
}
|
||||
|
||||
static u16 load16(const u8* address)
|
||||
{
|
||||
u16 value;
|
||||
|
@ -62,6 +83,13 @@ struct ByteReader {
|
|||
load(address, value);
|
||||
return value;
|
||||
}
|
||||
|
||||
static u64 load64(const u8* address)
|
||||
{
|
||||
u64 value;
|
||||
load(address, value);
|
||||
return value;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue