diff options
author | Hendiadyoin1 <leon2002.la@gmail.com> | 2021-07-13 17:17:12 +0200 |
---|---|---|
committer | Ali Mohammad Pur <Ali.mpfard@gmail.com> | 2021-07-14 11:26:34 +0430 |
commit | d761c5024b20cb09a4052b3f0aaff60ea9d03b5e (patch) | |
tree | a73bf6643363bb2d49076fe6a531640ecb4415cc /AK | |
parent | b98e741237d6f3335a7c86d849818a4c0ab0d3a3 (diff) | |
download | serenity-d761c5024b20cb09a4052b3f0aaff60ea9d03b5e.zip |
AK: Generalize ByteReader
Also use it instead of CPU.h's possibly_unaligned_data interface
Diffstat (limited to 'AK')
-rw-r--r-- | AK/ByteReader.h | 59 |
1 files changed, 10 insertions, 49 deletions
diff --git a/AK/ByteReader.h b/AK/ByteReader.h index c0da52fe9b..71d451f76d 100644 --- a/AK/ByteReader.h +++ b/AK/ByteReader.h @@ -6,68 +6,29 @@ #pragma once +#include <AK/StdLibExtraDetails.h> #include <AK/Types.h> namespace AK { struct ByteReader { - static void store(u8* address, u16 value) - { - union { - u16 _16; - u8 _8[2]; - } const v { ._16 = value }; - __builtin_memcpy(address, v._8, 2); - } - - static void store(u8* address, u32 value) - { - union { - u32 _32; - u8 _8[4]; - } const v { ._32 = value }; - __builtin_memcpy(address, v._8, 4); - } - - static void load(const u8* address, u16& value) - { - union { - u16 _16; - u8 _8[2]; - } v { ._16 = 0 }; - __builtin_memcpy(&v._8, address, 2); - value = v._16; - } - - static void load(const u8* address, u32& value) + template<typename T> + requires(IsTriviallyCopyable<T>) static void store(u8* addr, T value) { - union { - u32 _32; - u8 _8[4]; - } v { ._32 = 0 }; - __builtin_memcpy(&v._8, address, 4); - value = v._32; + __builtin_memcpy(addr, &value, sizeof(T)); } - - static void load(const u8* address, u64& value) + template<typename T> + requires(IsTriviallyConstructible<T>) static void load(const u8* addr, T& value) { - union { - u64 _64; - u8 _8[8]; - } v { ._64 = 0 }; - __builtin_memcpy(&v._8, address, 8); - value = v._64; + __builtin_memcpy(&value, addr, sizeof(T)); } 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)); - } + FlatPtr value; + load<FlatPtr>(address, value); + return reinterpret_cast<T*>(value); } static u16 load16(const u8* address) |