diff options
Diffstat (limited to '')
-rw-r--r-- | src/common/atomic_ops.cpp | 37 | ||||
-rw-r--r-- | src/common/atomic_ops.h | 10 | ||||
-rw-r--r-- | src/common/concepts.h | 10 | ||||
-rw-r--r-- | src/common/hex_util.cpp | 34 | ||||
-rw-r--r-- | src/common/hex_util.h | 29 | ||||
-rw-r--r-- | src/common/logging/backend.cpp | 22 | ||||
-rw-r--r-- | src/common/logging/backend.h | 14 |
7 files changed, 70 insertions, 86 deletions
diff --git a/src/common/atomic_ops.cpp b/src/common/atomic_ops.cpp index 1098e21ff..1612d0e67 100644 --- a/src/common/atomic_ops.cpp +++ b/src/common/atomic_ops.cpp @@ -14,50 +14,55 @@ namespace Common { #if _MSC_VER -bool AtomicCompareAndSwap(u8 volatile* pointer, u8 value, u8 expected) { - u8 result = _InterlockedCompareExchange8((char*)pointer, value, expected); +bool AtomicCompareAndSwap(volatile u8* pointer, u8 value, u8 expected) { + const u8 result = + _InterlockedCompareExchange8(reinterpret_cast<volatile char*>(pointer), value, expected); return result == expected; } -bool AtomicCompareAndSwap(u16 volatile* pointer, u16 value, u16 expected) { - u16 result = _InterlockedCompareExchange16((short*)pointer, value, expected); +bool AtomicCompareAndSwap(volatile u16* pointer, u16 value, u16 expected) { + const u16 result = + _InterlockedCompareExchange16(reinterpret_cast<volatile short*>(pointer), value, expected); return result == expected; } -bool AtomicCompareAndSwap(u32 volatile* pointer, u32 value, u32 expected) { - u32 result = _InterlockedCompareExchange((long*)pointer, value, expected); +bool AtomicCompareAndSwap(volatile u32* pointer, u32 value, u32 expected) { + const u32 result = + _InterlockedCompareExchange(reinterpret_cast<volatile long*>(pointer), value, expected); return result == expected; } -bool AtomicCompareAndSwap(u64 volatile* pointer, u64 value, u64 expected) { - u64 result = _InterlockedCompareExchange64((__int64*)pointer, value, expected); +bool AtomicCompareAndSwap(volatile u64* pointer, u64 value, u64 expected) { + const u64 result = _InterlockedCompareExchange64(reinterpret_cast<volatile __int64*>(pointer), + value, expected); return result == expected; } -bool AtomicCompareAndSwap(u64 volatile* pointer, u128 value, u128 expected) { - return _InterlockedCompareExchange128((__int64*)pointer, value[1], value[0], - (__int64*)expected.data()) != 0; +bool AtomicCompareAndSwap(volatile u64* pointer, u128 value, u128 expected) { + return _InterlockedCompareExchange128(reinterpret_cast<volatile __int64*>(pointer), value[1], + value[0], + reinterpret_cast<__int64*>(expected.data())) != 0; } #else -bool AtomicCompareAndSwap(u8 volatile* pointer, u8 value, u8 expected) { +bool AtomicCompareAndSwap(volatile u8* pointer, u8 value, u8 expected) { return __sync_bool_compare_and_swap(pointer, expected, value); } -bool AtomicCompareAndSwap(u16 volatile* pointer, u16 value, u16 expected) { +bool AtomicCompareAndSwap(volatile u16* pointer, u16 value, u16 expected) { return __sync_bool_compare_and_swap(pointer, expected, value); } -bool AtomicCompareAndSwap(u32 volatile* pointer, u32 value, u32 expected) { +bool AtomicCompareAndSwap(volatile u32* pointer, u32 value, u32 expected) { return __sync_bool_compare_and_swap(pointer, expected, value); } -bool AtomicCompareAndSwap(u64 volatile* pointer, u64 value, u64 expected) { +bool AtomicCompareAndSwap(volatile u64* pointer, u64 value, u64 expected) { return __sync_bool_compare_and_swap(pointer, expected, value); } -bool AtomicCompareAndSwap(u64 volatile* pointer, u128 value, u128 expected) { +bool AtomicCompareAndSwap(volatile u64* pointer, u128 value, u128 expected) { unsigned __int128 value_a; unsigned __int128 expected_a; std::memcpy(&value_a, value.data(), sizeof(u128)); diff --git a/src/common/atomic_ops.h b/src/common/atomic_ops.h index e6181d521..8d6b73c00 100644 --- a/src/common/atomic_ops.h +++ b/src/common/atomic_ops.h @@ -8,10 +8,10 @@ namespace Common { -bool AtomicCompareAndSwap(u8 volatile* pointer, u8 value, u8 expected); -bool AtomicCompareAndSwap(u16 volatile* pointer, u16 value, u16 expected); -bool AtomicCompareAndSwap(u32 volatile* pointer, u32 value, u32 expected); -bool AtomicCompareAndSwap(u64 volatile* pointer, u64 value, u64 expected); -bool AtomicCompareAndSwap(u64 volatile* pointer, u128 value, u128 expected); +bool AtomicCompareAndSwap(volatile u8* pointer, u8 value, u8 expected); +bool AtomicCompareAndSwap(volatile u16* pointer, u16 value, u16 expected); +bool AtomicCompareAndSwap(volatile u32* pointer, u32 value, u32 expected); +bool AtomicCompareAndSwap(volatile u64* pointer, u64 value, u64 expected); +bool AtomicCompareAndSwap(volatile u64* pointer, u128 value, u128 expected); } // namespace Common diff --git a/src/common/concepts.h b/src/common/concepts.h index db5fb373d..54252e778 100644 --- a/src/common/concepts.h +++ b/src/common/concepts.h @@ -23,10 +23,12 @@ concept IsSTLContainer = requires(T t) { t.size(); }; -// Check if type T is derived from T2 -template <typename T, typename T2> -concept IsBaseOf = requires { - std::is_base_of_v<T, T2>; +// TODO: Replace with std::derived_from when the <concepts> header +// is available on all supported platforms. +template <typename Derived, typename Base> +concept DerivedFrom = requires { + std::is_base_of_v<Base, Derived>; + std::is_convertible_v<const volatile Derived*, const volatile Base*>; }; } // namespace Common diff --git a/src/common/hex_util.cpp b/src/common/hex_util.cpp index c2f6cf0f6..74f52dd11 100644 --- a/src/common/hex_util.cpp +++ b/src/common/hex_util.cpp @@ -3,21 +3,9 @@ // Refer to the license.txt file included. #include "common/hex_util.h" -#include "common/logging/log.h" namespace Common { -u8 ToHexNibble(char c1) { - if (c1 >= 65 && c1 <= 70) - return c1 - 55; - if (c1 >= 97 && c1 <= 102) - return c1 - 87; - if (c1 >= 48 && c1 <= 57) - return c1 - 48; - LOG_ERROR(Common, "Invalid hex digit: 0x{:02X}", c1); - return 0; -} - std::vector<u8> HexStringToVector(std::string_view str, bool little_endian) { std::vector<u8> out(str.size() / 2); if (little_endian) { @@ -30,26 +18,4 @@ std::vector<u8> HexStringToVector(std::string_view str, bool little_endian) { return out; } -std::array<u8, 16> operator""_array16(const char* str, std::size_t len) { - if (len != 32) { - LOG_ERROR(Common, - "Attempting to parse string to array that is not of correct size (expected=32, " - "actual={}).", - len); - return {}; - } - return HexStringToArray<16>(str); -} - -std::array<u8, 32> operator""_array32(const char* str, std::size_t len) { - if (len != 64) { - LOG_ERROR(Common, - "Attempting to parse string to array that is not of correct size (expected=64, " - "actual={}).", - len); - return {}; - } - return HexStringToArray<32>(str); -} - } // namespace Common diff --git a/src/common/hex_util.h b/src/common/hex_util.h index bb4736f96..a0a0e78a4 100644 --- a/src/common/hex_util.h +++ b/src/common/hex_util.h @@ -14,19 +14,31 @@ namespace Common { -u8 ToHexNibble(char c1); +constexpr u8 ToHexNibble(char c) { + if (c >= 65 && c <= 70) { + return c - 55; + } + + if (c >= 97 && c <= 102) { + return c - 87; + } + + return c - 48; +} std::vector<u8> HexStringToVector(std::string_view str, bool little_endian); template <std::size_t Size, bool le = false> -std::array<u8, Size> HexStringToArray(std::string_view str) { +constexpr std::array<u8, Size> HexStringToArray(std::string_view str) { std::array<u8, Size> out{}; if constexpr (le) { - for (std::size_t i = 2 * Size - 2; i <= 2 * Size; i -= 2) + for (std::size_t i = 2 * Size - 2; i <= 2 * Size; i -= 2) { out[i / 2] = (ToHexNibble(str[i]) << 4) | ToHexNibble(str[i + 1]); + } } else { - for (std::size_t i = 0; i < 2 * Size; i += 2) + for (std::size_t i = 0; i < 2 * Size; i += 2) { out[i / 2] = (ToHexNibble(str[i]) << 4) | ToHexNibble(str[i + 1]); + } } return out; } @@ -48,7 +60,12 @@ std::string HexToString(const ContiguousContainer& data, bool upper = true) { return out; } -std::array<u8, 0x10> operator"" _array16(const char* str, std::size_t len); -std::array<u8, 0x20> operator"" _array32(const char* str, std::size_t len); +constexpr std::array<u8, 16> AsArray(const char (&data)[17]) { + return HexStringToArray<16>(data); +} + +constexpr std::array<u8, 32> AsArray(const char (&data)[65]) { + return HexStringToArray<32>(data); +} } // namespace Common diff --git a/src/common/logging/backend.cpp b/src/common/logging/backend.cpp index 04bc3128f..62cfde397 100644 --- a/src/common/logging/backend.cpp +++ b/src/common/logging/backend.cpp @@ -113,19 +113,19 @@ private: Entry CreateEntry(Class log_class, Level log_level, const char* filename, unsigned int line_nr, const char* function, std::string message) const { using std::chrono::duration_cast; + using std::chrono::microseconds; using std::chrono::steady_clock; - Entry entry; - entry.timestamp = - duration_cast<std::chrono::microseconds>(steady_clock::now() - time_origin); - entry.log_class = log_class; - entry.log_level = log_level; - entry.filename = filename; - entry.line_num = line_nr; - entry.function = function; - entry.message = std::move(message); - - return entry; + return { + .timestamp = duration_cast<microseconds>(steady_clock::now() - time_origin), + .log_class = log_class, + .log_level = log_level, + .filename = filename, + .line_num = line_nr, + .function = function, + .message = std::move(message), + .final_entry = false, + }; } std::mutex writing_mutex; diff --git a/src/common/logging/backend.h b/src/common/logging/backend.h index fc338c70d..e5d702568 100644 --- a/src/common/logging/backend.h +++ b/src/common/logging/backend.h @@ -21,19 +21,13 @@ class Filter; */ struct Entry { std::chrono::microseconds timestamp; - Class log_class; - Level log_level; - const char* filename; - unsigned int line_num; + Class log_class{}; + Level log_level{}; + const char* filename = nullptr; + unsigned int line_num = 0; std::string function; std::string message; bool final_entry = false; - - Entry() = default; - Entry(Entry&& o) = default; - - Entry& operator=(Entry&& o) = default; - Entry& operator=(const Entry& o) = default; }; /** |