mirror of
https://github.com/RPCS3/rpcs3.git
synced 2025-07-10 08:51:28 +12:00
vm::var improved, cleanup
Mostly vm::var initialization introduced. Added vm::make_var function.
This commit is contained in:
parent
cc02a147d3
commit
a974ee009e
116 changed files with 2763 additions and 3019 deletions
|
@ -161,11 +161,7 @@ inline u128 sync_fetch_and_add(volatile u128* dest, u128 value)
|
|||
old.lo = dest->lo;
|
||||
old.hi = dest->hi;
|
||||
|
||||
u128 _new;
|
||||
_new.lo = old.lo + value.lo;
|
||||
_new.hi = old.hi + value.hi + (_new.lo < value.lo);
|
||||
|
||||
if (sync_bool_compare_and_swap(dest, old, _new)) return old;
|
||||
if (sync_bool_compare_and_swap(dest, old, old + value)) return old;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -199,11 +195,7 @@ inline u128 sync_fetch_and_sub(volatile u128* dest, u128 value)
|
|||
old.lo = dest->lo;
|
||||
old.hi = dest->hi;
|
||||
|
||||
u128 _new;
|
||||
_new.lo = old.lo - value.lo;
|
||||
_new.hi = old.hi - value.hi - (old.lo < value.lo);
|
||||
|
||||
if (sync_bool_compare_and_swap(dest, old, _new)) return old;
|
||||
if (sync_bool_compare_and_swap(dest, old, old - value)) return old;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -237,11 +229,7 @@ inline u128 sync_fetch_and_or(volatile u128* dest, u128 value)
|
|||
old.lo = dest->lo;
|
||||
old.hi = dest->hi;
|
||||
|
||||
u128 _new;
|
||||
_new.lo = old.lo | value.lo;
|
||||
_new.hi = old.hi | value.hi;
|
||||
|
||||
if (sync_bool_compare_and_swap(dest, old, _new)) return old;
|
||||
if (sync_bool_compare_and_swap(dest, old, old | value)) return old;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -275,11 +263,7 @@ inline u128 sync_fetch_and_and(volatile u128* dest, u128 value)
|
|||
old.lo = dest->lo;
|
||||
old.hi = dest->hi;
|
||||
|
||||
u128 _new;
|
||||
_new.lo = old.lo & value.lo;
|
||||
_new.hi = old.hi & value.hi;
|
||||
|
||||
if (sync_bool_compare_and_swap(dest, old, _new)) return old;
|
||||
if (sync_bool_compare_and_swap(dest, old, old & value)) return old;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -313,11 +297,7 @@ inline u128 sync_fetch_and_xor(volatile u128* dest, u128 value)
|
|||
old.lo = dest->lo;
|
||||
old.hi = dest->hi;
|
||||
|
||||
u128 _new;
|
||||
_new.lo = old.lo ^ value.lo;
|
||||
_new.hi = old.hi ^ value.hi;
|
||||
|
||||
if (sync_bool_compare_and_swap(dest, old, _new)) return old;
|
||||
if (sync_bool_compare_and_swap(dest, old, old ^ value)) return old;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -355,17 +335,17 @@ template<typename T> struct atomic_storage<T, 16>
|
|||
|
||||
template<typename T> using atomic_storage_t = typename atomic_storage<T>::type;
|
||||
|
||||
// result wrapper to deal with void result type
|
||||
// atomic result wrapper; implements special behaviour for void result type
|
||||
template<typename T, typename RT, typename VT> struct atomic_op_result_t
|
||||
{
|
||||
RT result;
|
||||
|
||||
template<typename... Args> inline atomic_op_result_t(T func, VT& var, Args&&... args)
|
||||
template<typename... Args> atomic_op_result_t(T func, VT& var, Args&&... args)
|
||||
: result(std::move(func(var, std::forward<Args>(args)...)))
|
||||
{
|
||||
}
|
||||
|
||||
inline RT move()
|
||||
RT move()
|
||||
{
|
||||
return std::move(result);
|
||||
}
|
||||
|
@ -376,13 +356,13 @@ template<typename T, typename VT> struct atomic_op_result_t<T, void, VT>
|
|||
{
|
||||
VT result;
|
||||
|
||||
template<typename... Args> inline atomic_op_result_t(T func, VT& var, Args&&... args)
|
||||
template<typename... Args> atomic_op_result_t(T func, VT& var, Args&&... args)
|
||||
: result(var)
|
||||
{
|
||||
func(var, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
inline VT move()
|
||||
VT move()
|
||||
{
|
||||
return std::move(result);
|
||||
}
|
||||
|
@ -393,12 +373,12 @@ template<typename CT, typename... FArgs, typename RT, typename VT> struct atomic
|
|||
{
|
||||
RT result;
|
||||
|
||||
template<typename... Args> inline atomic_op_result_t(RT(CT::*func)(FArgs...), VT& var, Args&&... args)
|
||||
template<typename... Args> atomic_op_result_t(RT(CT::*func)(FArgs...), VT& var, Args&&... args)
|
||||
: result(std::move((var.*func)(std::forward<Args>(args)...)))
|
||||
{
|
||||
}
|
||||
|
||||
inline RT move()
|
||||
RT move()
|
||||
{
|
||||
return std::move(result);
|
||||
}
|
||||
|
@ -409,18 +389,19 @@ template<typename CT, typename... FArgs, typename VT> struct atomic_op_result_t<
|
|||
{
|
||||
VT result;
|
||||
|
||||
template<typename... Args> inline atomic_op_result_t(void(CT::*func)(FArgs...), VT& var, Args&&... args)
|
||||
template<typename... Args> atomic_op_result_t(void(CT::*func)(FArgs...), VT& var, Args&&... args)
|
||||
: result(var)
|
||||
{
|
||||
(var.*func)(std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
inline VT move()
|
||||
VT move()
|
||||
{
|
||||
return std::move(result);
|
||||
}
|
||||
};
|
||||
|
||||
// Atomic type with lock-free and standard layout guarantees (and appropriate limitations)
|
||||
template<typename T> class atomic_t
|
||||
{
|
||||
using type = std::remove_cv_t<T>;
|
||||
|
@ -466,18 +447,14 @@ public:
|
|||
|
||||
atomic_t(const atomic_t&) = delete;
|
||||
|
||||
atomic_t(atomic_t&&) = delete;
|
||||
|
||||
inline atomic_t(type value)
|
||||
atomic_t(type value)
|
||||
: m_data(to_subtype(value))
|
||||
{
|
||||
}
|
||||
|
||||
atomic_t& operator =(const atomic_t&) = delete;
|
||||
|
||||
atomic_t& operator =(atomic_t&&) = delete;
|
||||
|
||||
inline atomic_t& operator =(type value)
|
||||
atomic_t& operator =(type value)
|
||||
{
|
||||
return write_relaxed(m_data, to_subtype(value)), *this;
|
||||
}
|
||||
|
@ -500,31 +477,31 @@ public:
|
|||
}
|
||||
|
||||
// Atomically compare data with cmp, replace with exch if equal, return previous data value anyway
|
||||
inline const type compare_and_swap(const type& cmp, const type& exch) volatile
|
||||
type compare_and_swap(const type& cmp, const type& exch) volatile
|
||||
{
|
||||
return from_subtype(sync_val_compare_and_swap(&m_data, to_subtype(cmp), to_subtype(exch)));
|
||||
}
|
||||
|
||||
// Atomically compare data with cmp, replace with exch if equal, return true if data was replaced
|
||||
inline bool compare_and_swap_test(const type& cmp, const type& exch) volatile
|
||||
bool compare_and_swap_test(const type& cmp, const type& exch) volatile
|
||||
{
|
||||
return sync_bool_compare_and_swap(&m_data, to_subtype(cmp), to_subtype(exch));
|
||||
}
|
||||
|
||||
// Atomically replace data with exch, return previous data value
|
||||
inline const type exchange(const type& exch) volatile
|
||||
type exchange(const type& exch) volatile
|
||||
{
|
||||
return from_subtype(sync_lock_test_and_set(&m_data, to_subtype(exch)));
|
||||
}
|
||||
|
||||
// Atomically read data, possibly without memory barrier (not for 128 bit)
|
||||
inline const type load() const volatile
|
||||
type load() const volatile
|
||||
{
|
||||
return from_subtype(read_relaxed(m_data));
|
||||
}
|
||||
|
||||
// Atomically write data, possibly without memory barrier (not for 128 bit)
|
||||
inline void store(const type& value) volatile
|
||||
void store(const type& value) volatile
|
||||
{
|
||||
write_relaxed(m_data, to_subtype(value));
|
||||
}
|
||||
|
@ -550,40 +527,40 @@ public:
|
|||
}
|
||||
|
||||
// Atomic bitwise OR, returns previous data
|
||||
inline const type _or(const type& right) volatile
|
||||
type _or(const type& right) volatile
|
||||
{
|
||||
return from_subtype(sync_fetch_and_or(&m_data, to_subtype(right)));
|
||||
}
|
||||
|
||||
// Atomic bitwise AND, returns previous data
|
||||
inline const type _and(const type& right) volatile
|
||||
type _and(const type& right) volatile
|
||||
{
|
||||
return from_subtype(sync_fetch_and_and(&m_data, to_subtype(right)));
|
||||
}
|
||||
|
||||
// Atomic bitwise AND NOT (inverts right argument), returns previous data
|
||||
inline const type _and_not(const type& right) volatile
|
||||
type _and_not(const type& right) volatile
|
||||
{
|
||||
return from_subtype(sync_fetch_and_and(&m_data, ~to_subtype(right)));
|
||||
}
|
||||
|
||||
// Atomic bitwise XOR, returns previous data
|
||||
inline const type _xor(const type& right) volatile
|
||||
type _xor(const type& right) volatile
|
||||
{
|
||||
return from_subtype(sync_fetch_and_xor(&m_data, to_subtype(right)));
|
||||
}
|
||||
|
||||
inline const type operator |=(const type& right) volatile
|
||||
type operator |=(const type& right) volatile
|
||||
{
|
||||
return from_subtype(sync_fetch_and_or(&m_data, to_subtype(right)) | to_subtype(right));
|
||||
}
|
||||
|
||||
inline const type operator &=(const type& right) volatile
|
||||
type operator &=(const type& right) volatile
|
||||
{
|
||||
return from_subtype(sync_fetch_and_and(&m_data, to_subtype(right)) & to_subtype(right));
|
||||
}
|
||||
|
||||
inline const type operator ^=(const type& right) volatile
|
||||
type operator ^=(const type& right) volatile
|
||||
{
|
||||
return from_subtype(sync_fetch_and_xor(&m_data, to_subtype(right)) ^ to_subtype(right));
|
||||
}
|
||||
|
@ -697,9 +674,11 @@ template<typename T, typename T2> inline std::enable_if_t<IS_INTEGRAL(T) && std:
|
|||
});
|
||||
}
|
||||
|
||||
template<typename T> using atomic_be_t = atomic_t<be_t<T>>; // Atomic BE Type (for PS3 virtual memory)
|
||||
// Atomic BE Type (for PS3 virtual memory)
|
||||
template<typename T> using atomic_be_t = atomic_t<be_t<T>>;
|
||||
|
||||
template<typename T> using atomic_le_t = atomic_t<le_t<T>>; // Atomic LE Type (for PSV virtual memory)
|
||||
// Atomic LE Type (for PSV virtual memory)
|
||||
template<typename T> using atomic_le_t = atomic_t<le_t<T>>;
|
||||
|
||||
// Algorithm for std::atomic; similar to atomic_t::atomic_op()
|
||||
template<typename T, typename F, typename... Args, typename RT = std::result_of_t<F(T&, Args...)>> auto atomic_op(std::atomic<T>& var, F func, Args&&... args) -> decltype(atomic_op_result_t<F, RT, T>::result)
|
||||
|
|
|
@ -8,34 +8,34 @@
|
|||
|
||||
#define IS_LE_MACHINE // only draft
|
||||
|
||||
template<typename T, std::size_t N, std::size_t M> class masked_array_t // array type accessed as (index ^ M)
|
||||
{
|
||||
T m_data[N];
|
||||
|
||||
public:
|
||||
T& operator [](std::size_t index)
|
||||
{
|
||||
return m_data[index ^ M];
|
||||
}
|
||||
|
||||
const T& operator [](std::size_t index) const
|
||||
{
|
||||
return m_data[index ^ M];
|
||||
}
|
||||
|
||||
T& at(std::size_t index)
|
||||
{
|
||||
return (index ^ M) < N ? m_data[index ^ M] : throw std::out_of_range("Masked array");
|
||||
}
|
||||
|
||||
const T& at(std::size_t index) const
|
||||
{
|
||||
return (index ^ M) < N ? m_data[index ^ M] : throw std::out_of_range("Masked array");
|
||||
}
|
||||
};
|
||||
|
||||
union v128
|
||||
{
|
||||
template<typename T, std::size_t N, std::size_t M> class masked_array_t // array type accessed as (index ^ M)
|
||||
{
|
||||
T m_data[N];
|
||||
|
||||
public:
|
||||
T& operator [](std::size_t index)
|
||||
{
|
||||
return m_data[index ^ M];
|
||||
}
|
||||
|
||||
const T& operator [](std::size_t index) const
|
||||
{
|
||||
return m_data[index ^ M];
|
||||
}
|
||||
|
||||
T& at(std::size_t index)
|
||||
{
|
||||
return (index ^ M) < N ? m_data[index ^ M] : throw std::out_of_range(__FUNCTION__);
|
||||
}
|
||||
|
||||
const T& at(std::size_t index) const
|
||||
{
|
||||
return (index ^ M) < N ? m_data[index ^ M] : throw std::out_of_range(__FUNCTION__);
|
||||
}
|
||||
};
|
||||
|
||||
#ifdef IS_LE_MACHINE
|
||||
template<typename T, std::size_t N = 16 / sizeof(T)> using normal_array_t = masked_array_t<T, N, 0>;
|
||||
template<typename T, std::size_t N = 16 / sizeof(T)> using reversed_array_t = masked_array_t<T, N, N - 1>;
|
||||
|
@ -90,12 +90,12 @@ union v128
|
|||
{
|
||||
}
|
||||
|
||||
inline operator bool() const
|
||||
operator bool() const
|
||||
{
|
||||
return (data & mask) != 0;
|
||||
}
|
||||
|
||||
inline bit_element& operator =(const bool right)
|
||||
bit_element& operator =(const bool right)
|
||||
{
|
||||
if (right)
|
||||
{
|
||||
|
@ -108,7 +108,7 @@ union v128
|
|||
return *this;
|
||||
}
|
||||
|
||||
inline bit_element& operator =(const bit_element& right)
|
||||
bit_element& operator =(const bit_element& right)
|
||||
{
|
||||
if (right)
|
||||
{
|
||||
|
@ -144,14 +144,14 @@ union v128
|
|||
|
||||
bit_element at(u32 index)
|
||||
{
|
||||
if (index >= 128) throw std::out_of_range("Bit element");
|
||||
if (index >= 128) throw std::out_of_range(__FUNCTION__);
|
||||
|
||||
return operator[](index);
|
||||
}
|
||||
|
||||
bool at(u32 index) const
|
||||
{
|
||||
if (index >= 128) throw std::out_of_range("Bit element");
|
||||
if (index >= 128) throw std::out_of_range(__FUNCTION__);
|
||||
|
||||
return operator[](index);
|
||||
}
|
||||
|
@ -320,12 +320,12 @@ union v128
|
|||
return _u64[0] != right._u64[0] || _u64[1] != right._u64[1];
|
||||
}
|
||||
|
||||
inline bool is_any_1() const // check if any bit is 1
|
||||
bool is_any_1() const // check if any bit is 1
|
||||
{
|
||||
return _u64[0] || _u64[1];
|
||||
}
|
||||
|
||||
inline bool is_any_0() const // check if any bit is 0
|
||||
bool is_any_0() const // check if any bit is 0
|
||||
{
|
||||
return ~_u64[0] || ~_u64[1];
|
||||
}
|
||||
|
@ -486,14 +486,14 @@ template<typename T1, typename T2> struct se_convert
|
|||
}
|
||||
};
|
||||
|
||||
static struct se_raw_tag_t {} const se_raw{};
|
||||
static struct se_raw_tag_t {} constexpr se_raw{};
|
||||
|
||||
template<typename T, bool Se = true> class se_t;
|
||||
|
||||
// se_t with switched endianness
|
||||
template<typename T> class se_t<T, true>
|
||||
{
|
||||
using type = std::remove_cv_t<T>;
|
||||
using type = typename std::remove_cv<T>::type;
|
||||
using stype = se_storage_t<type>;
|
||||
using storage = se_storage<type>;
|
||||
|
||||
|
@ -506,7 +506,7 @@ template<typename T> class se_t<T, true>
|
|||
static_assert(!std::is_enum<type>::value, "se_t<> error: invalid type (enumeration), use integral type instead");
|
||||
static_assert(alignof(type) == alignof(stype), "se_t<> error: unexpected alignment");
|
||||
|
||||
template<typename T2, bool = std::is_integral<T2>::value> struct bool_converter
|
||||
template<typename T2, typename = void> struct bool_converter
|
||||
{
|
||||
static inline bool to_bool(const se_t<T2>& value)
|
||||
{
|
||||
|
@ -514,7 +514,7 @@ template<typename T> class se_t<T, true>
|
|||
}
|
||||
};
|
||||
|
||||
template<typename T2> struct bool_converter<T2, true>
|
||||
template<typename T2> struct bool_converter<T2, std::enable_if_t<std::is_integral<T2>::value>>
|
||||
{
|
||||
static inline bool to_bool(const se_t<T2>& value)
|
||||
{
|
||||
|
@ -527,78 +527,78 @@ public:
|
|||
|
||||
se_t(const se_t& right) = default;
|
||||
|
||||
inline se_t(type value)
|
||||
se_t(type value)
|
||||
: m_data(storage::to(value))
|
||||
{
|
||||
}
|
||||
|
||||
// construct directly from raw data (don't use)
|
||||
inline se_t(const stype& raw_value, const se_raw_tag_t&)
|
||||
constexpr se_t(const stype& raw_value, const se_raw_tag_t&)
|
||||
: m_data(raw_value)
|
||||
{
|
||||
}
|
||||
|
||||
inline type value() const
|
||||
type value() const
|
||||
{
|
||||
return storage::from(m_data);
|
||||
}
|
||||
|
||||
// access underlying raw data (don't use)
|
||||
inline const stype& raw_data() const noexcept
|
||||
constexpr const stype& raw_data() const noexcept
|
||||
{
|
||||
return m_data;
|
||||
}
|
||||
|
||||
se_t& operator =(const se_t&) = default;
|
||||
|
||||
inline se_t& operator =(type value)
|
||||
se_t& operator =(type value)
|
||||
{
|
||||
return m_data = storage::to(value), *this;
|
||||
}
|
||||
|
||||
inline operator type() const
|
||||
operator type() const
|
||||
{
|
||||
return storage::from(m_data);
|
||||
}
|
||||
|
||||
// optimization
|
||||
explicit inline operator bool() const
|
||||
explicit operator bool() const
|
||||
{
|
||||
return bool_converter<type>::to_bool(*this);
|
||||
}
|
||||
|
||||
// optimization
|
||||
template<typename T2> inline std::enable_if_t<IS_BINARY_COMPARABLE(T, T2), se_t&> operator &=(const se_t<T2>& right)
|
||||
template<typename T2> std::enable_if_t<IS_BINARY_COMPARABLE(T, T2), se_t&> operator &=(const se_t<T2>& right)
|
||||
{
|
||||
return m_data &= right.raw_data(), *this;
|
||||
}
|
||||
|
||||
// optimization
|
||||
template<typename CT> inline std::enable_if_t<IS_INTEGRAL(T) && std::is_convertible<CT, T>::value, se_t&> operator &=(CT right)
|
||||
template<typename CT> std::enable_if_t<IS_INTEGRAL(T) && std::is_convertible<CT, T>::value, se_t&> operator &=(CT right)
|
||||
{
|
||||
return m_data &= storage::to(right), *this;
|
||||
}
|
||||
|
||||
// optimization
|
||||
template<typename T2> inline std::enable_if_t<IS_BINARY_COMPARABLE(T, T2), se_t&> operator |=(const se_t<T2>& right)
|
||||
template<typename T2> std::enable_if_t<IS_BINARY_COMPARABLE(T, T2), se_t&> operator |=(const se_t<T2>& right)
|
||||
{
|
||||
return m_data |= right.raw_data(), *this;
|
||||
}
|
||||
|
||||
// optimization
|
||||
template<typename CT> inline std::enable_if_t<IS_INTEGRAL(T) && std::is_convertible<CT, T>::value, se_t&> operator |=(CT right)
|
||||
template<typename CT> std::enable_if_t<IS_INTEGRAL(T) && std::is_convertible<CT, T>::value, se_t&> operator |=(CT right)
|
||||
{
|
||||
return m_data |= storage::to(right), *this;
|
||||
}
|
||||
|
||||
// optimization
|
||||
template<typename T2> inline std::enable_if_t<IS_BINARY_COMPARABLE(T, T2), se_t&> operator ^=(const se_t<T2>& right)
|
||||
template<typename T2> std::enable_if_t<IS_BINARY_COMPARABLE(T, T2), se_t&> operator ^=(const se_t<T2>& right)
|
||||
{
|
||||
return m_data ^= right.raw_data(), *this;
|
||||
}
|
||||
|
||||
// optimization
|
||||
template<typename CT> inline std::enable_if_t<IS_INTEGRAL(T) && std::is_convertible<CT, T>::value, se_t&> operator ^=(CT right)
|
||||
template<typename CT> std::enable_if_t<IS_INTEGRAL(T) && std::is_convertible<CT, T>::value, se_t&> operator ^=(CT right)
|
||||
{
|
||||
return m_data ^= storage::to(right), *this;
|
||||
}
|
||||
|
@ -607,7 +607,7 @@ public:
|
|||
// se_t with native endianness
|
||||
template<typename T> class se_t<T, false>
|
||||
{
|
||||
using type = std::remove_cv_t<T>;
|
||||
using type = typename std::remove_cv<T>::type;
|
||||
|
||||
type m_data;
|
||||
|
||||
|
@ -622,39 +622,39 @@ public:
|
|||
|
||||
se_t(const se_t&) = default;
|
||||
|
||||
inline se_t(type value)
|
||||
constexpr se_t(type value)
|
||||
: m_data(value)
|
||||
{
|
||||
}
|
||||
|
||||
inline type value() const
|
||||
type value() const
|
||||
{
|
||||
return m_data;
|
||||
}
|
||||
|
||||
se_t& operator =(const se_t& value) = default;
|
||||
|
||||
inline se_t& operator =(type value)
|
||||
se_t& operator =(type value)
|
||||
{
|
||||
return m_data = value, *this;
|
||||
}
|
||||
|
||||
inline operator type() const
|
||||
operator type() const
|
||||
{
|
||||
return m_data;
|
||||
}
|
||||
|
||||
template<typename CT> inline std::enable_if_t<IS_INTEGRAL(T) && std::is_convertible<CT, T>::value, se_t&> operator &=(const CT& right)
|
||||
template<typename CT> std::enable_if_t<IS_INTEGRAL(T) && std::is_convertible<CT, T>::value, se_t&> operator &=(const CT& right)
|
||||
{
|
||||
return m_data &= right, *this;
|
||||
}
|
||||
|
||||
template<typename CT> inline std::enable_if_t<IS_INTEGRAL(T) && std::is_convertible<CT, T>::value, se_t&> operator |=(const CT& right)
|
||||
template<typename CT> std::enable_if_t<IS_INTEGRAL(T) && std::is_convertible<CT, T>::value, se_t&> operator |=(const CT& right)
|
||||
{
|
||||
return m_data |= right, *this;
|
||||
}
|
||||
|
||||
template<typename CT> inline std::enable_if_t<IS_INTEGRAL(T) && std::is_convertible<CT, T>::value, se_t&> operator ^=(const CT& right)
|
||||
template<typename CT> std::enable_if_t<IS_INTEGRAL(T) && std::is_convertible<CT, T>::value, se_t&> operator ^=(const CT& right)
|
||||
{
|
||||
return m_data ^= right, *this;
|
||||
}
|
||||
|
@ -837,107 +837,77 @@ template<typename T> using be_t = se_t<T, false>;
|
|||
template<typename T> using le_t = se_t<T, true>;
|
||||
#endif
|
||||
|
||||
template<typename T> struct is_be_t : public std::integral_constant<bool, false>
|
||||
|
||||
template<typename T, bool Se, typename = void> struct to_se
|
||||
{
|
||||
using type = typename std::conditional<std::is_arithmetic<T>::value || std::is_enum<T>::value, se_t<T, Se>, T>::type;
|
||||
};
|
||||
|
||||
template<typename T> struct is_be_t<be_t<T>> : public std::integral_constant<bool, true>
|
||||
template<typename T, bool Se> struct to_se<const T, Se, std::enable_if_t<!std::is_array<T>::value>> // move const qualifier
|
||||
{
|
||||
using type = const typename to_se<T, Se>::type;
|
||||
};
|
||||
|
||||
template<typename T> struct is_be_t<const T> : public std::integral_constant<bool, is_be_t<T>::value>
|
||||
template<typename T, bool Se> struct to_se<volatile T, Se, std::enable_if_t<!std::is_array<T>::value && !std::is_const<T>::value>> // move volatile qualifier
|
||||
{
|
||||
using type = volatile typename to_se<T, Se>::type;
|
||||
};
|
||||
|
||||
template<typename T> struct is_be_t<volatile T> : public std::integral_constant<bool, is_be_t<T>::value>
|
||||
template<typename T, bool Se> struct to_se<T[], Se>
|
||||
{
|
||||
using type = typename to_se<T, Se>::type[];
|
||||
};
|
||||
|
||||
// to_be_t helper struct
|
||||
template<typename T> struct to_be
|
||||
template<typename T, bool Se, std::size_t N> struct to_se<T[N], Se>
|
||||
{
|
||||
using type = std::conditional_t<std::is_arithmetic<T>::value || std::is_enum<T>::value || std::is_same<T, v128>::value || std::is_same<T, u128>::value, be_t<T>, T>;
|
||||
using type = typename to_se<T, Se>::type[N];
|
||||
};
|
||||
|
||||
// be_t<T> if possible, T otherwise
|
||||
template<typename T> using to_be_t = typename to_be<T>::type;
|
||||
template<bool Se> struct to_se<u128, Se> { using type = se_t<u128, Se>; };
|
||||
template<bool Se> struct to_se<v128, Se> { using type = se_t<v128, Se>; };
|
||||
template<bool Se> struct to_se<bool, Se> { using type = bool; };
|
||||
template<bool Se> struct to_se<char, Se> { using type = char; };
|
||||
template<bool Se> struct to_se<u8, Se> { using type = u8; };
|
||||
template<bool Se> struct to_se<s8, Se> { using type = s8; };
|
||||
|
||||
template<typename T> struct to_be<const T> // move const qualifier
|
||||
{
|
||||
using type = const to_be_t<T>;
|
||||
};
|
||||
#ifdef IS_LE_MACHINE
|
||||
template<typename T> using to_be_t = typename to_se<T, true>::type;
|
||||
template<typename T> using to_le_t = typename to_se<T, false>::type;
|
||||
#else
|
||||
template<typename T> using to_be_t = typename to_se<T, false>::type;
|
||||
template<typename T> using to_le_t = typename to_se<T, true>::type;
|
||||
#endif
|
||||
|
||||
template<typename T> struct to_be<volatile T> // move volatile qualifier
|
||||
{
|
||||
using type = volatile to_be_t<T>;
|
||||
};
|
||||
|
||||
template<> struct to_be<void> { using type = void; };
|
||||
template<> struct to_be<bool> { using type = bool; };
|
||||
template<> struct to_be<char> { using type = char; };
|
||||
template<> struct to_be<u8> { using type = u8; };
|
||||
template<> struct to_be<s8> { using type = s8; };
|
||||
|
||||
template<typename T> struct is_le_t : public std::integral_constant<bool, false>
|
||||
{
|
||||
};
|
||||
|
||||
template<typename T> struct is_le_t<le_t<T>> : public std::integral_constant<bool, true>
|
||||
{
|
||||
};
|
||||
|
||||
template<typename T> struct is_le_t<const T> : public std::integral_constant<bool, is_le_t<T>::value>
|
||||
{
|
||||
};
|
||||
|
||||
template<typename T> struct is_le_t<volatile T> : public std::integral_constant<bool, is_le_t<T>::value>
|
||||
{
|
||||
};
|
||||
|
||||
template<typename T> struct to_le
|
||||
{
|
||||
using type = std::conditional_t<std::is_arithmetic<T>::value || std::is_enum<T>::value || std::is_same<T, v128>::value || std::is_same<T, u128>::value, le_t<T>, T>;
|
||||
};
|
||||
|
||||
// le_t<T> if possible, T otherwise
|
||||
template<typename T> using to_le_t = typename to_le<T>::type;
|
||||
|
||||
template<typename T> struct to_le<const T> // move const qualifier
|
||||
{
|
||||
using type = const to_le_t<T>;
|
||||
};
|
||||
|
||||
template<typename T> struct to_le<volatile T> // move volatile qualifier
|
||||
{
|
||||
using type = volatile to_le_t<T>;
|
||||
};
|
||||
|
||||
template<> struct to_le<void> { using type = void; };
|
||||
template<> struct to_le<bool> { using type = bool; };
|
||||
template<> struct to_le<char> { using type = char; };
|
||||
template<> struct to_le<u8> { using type = u8; };
|
||||
template<> struct to_le<s8> { using type = s8; };
|
||||
|
||||
// to_ne_t helper struct
|
||||
template<typename T> struct to_ne
|
||||
template<typename T, typename = void> struct to_ne
|
||||
{
|
||||
using type = T;
|
||||
};
|
||||
|
||||
template<typename T, bool Se> struct to_ne<se_t<T, Se>>
|
||||
{
|
||||
using type = T;
|
||||
using type = typename std::remove_cv<T>::type;
|
||||
};
|
||||
|
||||
template<typename T> struct to_ne<const T, std::enable_if_t<!std::is_array<T>::value>> // move const qualifier
|
||||
{
|
||||
using type = const typename to_ne<T>::type;
|
||||
};
|
||||
|
||||
template<typename T> struct to_ne<volatile T, std::enable_if_t<!std::is_array<T>::value && !std::is_const<T>::value>> // move volatile qualifier
|
||||
{
|
||||
using type = volatile typename to_ne<T>::type;
|
||||
};
|
||||
|
||||
template<typename T> struct to_ne<T[]>
|
||||
{
|
||||
using type = typename to_ne<T>::type[];
|
||||
};
|
||||
|
||||
template<typename T, std::size_t N> struct to_ne<T[N]>
|
||||
{
|
||||
using type = typename to_ne<T>::type[N];
|
||||
};
|
||||
|
||||
// restore native endianness for T: returns T for be_t<T> or le_t<T>, T otherwise
|
||||
template<typename T> using to_ne_t = typename to_ne<T>::type;
|
||||
|
||||
template<typename T> struct to_ne<const T> // move const qualifier
|
||||
{
|
||||
using type = const to_ne_t<T>;
|
||||
};
|
||||
|
||||
template<typename T> struct to_ne<volatile T> // move volatile qualifier
|
||||
{
|
||||
using type = volatile to_ne_t<T>;
|
||||
};
|
||||
|
|
140
Utilities/BitField.h
Normal file
140
Utilities/BitField.h
Normal file
|
@ -0,0 +1,140 @@
|
|||
#pragma once
|
||||
|
||||
// BitField access helper class (N bits from I position), intended to be put in union
|
||||
template<typename T, u32 I, u32 N> class bf_t
|
||||
{
|
||||
// Checks
|
||||
static_assert(I < sizeof(T) * 8, "bf_t<> error: I out of bounds");
|
||||
static_assert(N < sizeof(T) * 8, "bf_t<> error: N out of bounds");
|
||||
static_assert(I + N <= sizeof(T) * 8, "bf_t<> error: values out of bounds");
|
||||
|
||||
// Underlying data type
|
||||
using type = typename std::remove_cv<T>::type;
|
||||
|
||||
// Underlying value type (native endianness)
|
||||
using vtype = typename to_ne<type>::type;
|
||||
|
||||
// Mask of size N
|
||||
constexpr static vtype s_mask = (static_cast<vtype>(1) << N) - 1;
|
||||
|
||||
// Underlying data member
|
||||
type m_data;
|
||||
|
||||
// Conversion operator helper (uses SFINAE)
|
||||
template<typename T2, typename = void> struct converter {};
|
||||
|
||||
template<typename T2> struct converter<T2, std::enable_if_t<std::is_unsigned<T2>::value>>
|
||||
{
|
||||
// Load unsigned value
|
||||
static inline T2 convert(const type& data)
|
||||
{
|
||||
return (data >> I) & s_mask;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T2> struct converter<T2, std::enable_if_t<std::is_signed<T2>::value>>
|
||||
{
|
||||
// Load signed value (sign-extended)
|
||||
static inline T2 convert(const type& data)
|
||||
{
|
||||
return data << (sizeof(T) * 8 - I - N) >> (sizeof(T) * 8 - N);
|
||||
}
|
||||
};
|
||||
|
||||
public:
|
||||
// Assignment operator (store bitfield value)
|
||||
bf_t& operator =(vtype value)
|
||||
{
|
||||
m_data = (m_data & ~(s_mask << I)) | (value & s_mask) << I;
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Conversion operator (load bitfield value)
|
||||
operator vtype() const
|
||||
{
|
||||
return converter<vtype>::convert(m_data);
|
||||
}
|
||||
|
||||
// Get raw data with mask applied
|
||||
type unshifted() const
|
||||
{
|
||||
return (m_data & (s_mask << I));
|
||||
}
|
||||
|
||||
// Optimized bool conversion
|
||||
explicit operator bool() const
|
||||
{
|
||||
return unshifted() != 0;
|
||||
}
|
||||
|
||||
// Postfix increment operator
|
||||
vtype operator ++(int)
|
||||
{
|
||||
vtype result = *this;
|
||||
*this = result + 1;
|
||||
return result;
|
||||
}
|
||||
|
||||
// Prefix increment operator
|
||||
bf_t& operator ++()
|
||||
{
|
||||
return *this = *this + 1;
|
||||
}
|
||||
|
||||
// Postfix decrement operator
|
||||
vtype operator --(int)
|
||||
{
|
||||
vtype result = *this;
|
||||
*this = result - 1;
|
||||
return result;
|
||||
}
|
||||
|
||||
// Prefix decrement operator
|
||||
bf_t& operator --()
|
||||
{
|
||||
return *this = *this - 1;
|
||||
}
|
||||
|
||||
// Addition assignment operator
|
||||
bf_t& operator +=(vtype right)
|
||||
{
|
||||
return *this = *this + right;
|
||||
}
|
||||
|
||||
// Subtraction assignment operator
|
||||
bf_t& operator -=(vtype right)
|
||||
{
|
||||
return *this = *this - right;
|
||||
}
|
||||
|
||||
// Multiplication assignment operator
|
||||
bf_t& operator *=(vtype right)
|
||||
{
|
||||
return *this = *this * right;
|
||||
}
|
||||
|
||||
// Bitwise AND assignment operator
|
||||
bf_t& operator &=(vtype right)
|
||||
{
|
||||
m_data &= (right & s_mask) << I;
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Bitwise OR assignment operator
|
||||
bf_t& operator |=(vtype right)
|
||||
{
|
||||
m_data |= (right & s_mask) << I;
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Bitwise XOR assignment operator
|
||||
bf_t& operator ^=(vtype right)
|
||||
{
|
||||
m_data ^= (right & s_mask) << I;
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T, u32 I, u32 N> using bf_be_t = bf_t<be_t<T>, I, N>;
|
||||
|
||||
template<typename T, u32 I, u32 N> using bf_le_t = bf_t<le_t<T>, I, N>;
|
|
@ -7,8 +7,6 @@
|
|||
|
||||
#define GET_API_ERROR static_cast<u64>(GetLastError())
|
||||
|
||||
static_assert(fs::file::null == intptr_t(INVALID_HANDLE_VALUE) && fs::dir::null == fs::file::null, "Check fs::file::null definition");
|
||||
|
||||
std::unique_ptr<wchar_t[]> to_wchar(const std::string& source)
|
||||
{
|
||||
const auto length = source.size() + 1; // size + null terminator
|
||||
|
@ -443,7 +441,7 @@ bool fs::file::open(const std::string& filename, u32 mode)
|
|||
return false;
|
||||
}
|
||||
|
||||
m_fd = (intptr_t)CreateFileW(to_wchar(filename).get(), access, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, disp, FILE_ATTRIBUTE_NORMAL, NULL);
|
||||
m_fd = (std::intptr_t)CreateFileW(to_wchar(filename).get(), access, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, disp, FILE_ATTRIBUTE_NORMAL, NULL);
|
||||
#else
|
||||
int flags = 0;
|
||||
|
||||
|
@ -598,11 +596,6 @@ u64 fs::file::write(const void* buffer, u64 count) const
|
|||
#endif
|
||||
}
|
||||
|
||||
u64 fs::file::write(const std::string &string) const
|
||||
{
|
||||
return write(string.data(), string.size());
|
||||
}
|
||||
|
||||
u64 fs::file::seek(s64 offset, fsm seek_mode) const
|
||||
{
|
||||
g_tls_error = fse::ok;
|
||||
|
@ -659,66 +652,34 @@ u64 fs::file::size() const
|
|||
|
||||
fs::dir::~dir()
|
||||
{
|
||||
if (m_dd != null)
|
||||
if (m_path)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
FindClose((HANDLE)m_dd);
|
||||
if (m_dd != -1) FindClose((HANDLE)m_dd);
|
||||
#else
|
||||
::closedir((DIR*)m_dd);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
void fs::dir::import(handle_type dd, const std::string& path)
|
||||
{
|
||||
if (m_dd != null)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
FindClose((HANDLE)m_dd);
|
||||
#else
|
||||
::closedir((DIR*)m_dd);
|
||||
#endif
|
||||
}
|
||||
|
||||
g_tls_error = fse::ok;
|
||||
|
||||
m_dd = dd;
|
||||
|
||||
#ifdef _WIN32
|
||||
m_path = to_wchar(path);
|
||||
#else
|
||||
m_path.reset(new char[path.size() + 1]);
|
||||
memcpy(m_path.get(), path.c_str(), path.size() + 1);
|
||||
#endif
|
||||
}
|
||||
|
||||
bool fs::dir::open(const std::string& dirname)
|
||||
{
|
||||
if (m_dd != null)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
FindClose((HANDLE)m_dd);
|
||||
#else
|
||||
::closedir((DIR*)m_dd);
|
||||
#endif
|
||||
}
|
||||
this->close();
|
||||
|
||||
g_tls_error = fse::ok;
|
||||
|
||||
m_dd = null;
|
||||
|
||||
m_path.reset();
|
||||
|
||||
if (!is_dir(dirname))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
m_path = to_wchar(dirname + "/*");
|
||||
#else
|
||||
m_path.reset(new char[dirname.size() + 1]);
|
||||
memcpy(m_path.get(), dirname.c_str(), dirname.size() + 1);
|
||||
std::memcpy(m_path.get(), dirname.c_str(), dirname.size() + 1);
|
||||
|
||||
#ifdef _WIN32
|
||||
m_dd = -1;
|
||||
#else
|
||||
m_dd = (std::intptr_t)::opendir(m_path.get());
|
||||
#endif
|
||||
|
||||
return true;
|
||||
|
@ -728,46 +689,26 @@ bool fs::dir::close()
|
|||
{
|
||||
g_tls_error = fse::ok;
|
||||
|
||||
if (m_dd == null)
|
||||
if (!m_path)
|
||||
{
|
||||
if (m_path)
|
||||
{
|
||||
m_path.reset();
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
auto dd = m_dd;
|
||||
m_dd = null;
|
||||
|
||||
m_path.reset();
|
||||
|
||||
#ifdef _WIN32
|
||||
return FindClose((HANDLE)dd);
|
||||
CHECK_ASSERTION(m_dd == -1 || FindClose((HANDLE)m_dd));
|
||||
#else
|
||||
return !::closedir((DIR*)dd);
|
||||
CHECK_ASSERTION(!::closedir((DIR*)m_dd));
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool fs::dir::get_first(std::string& name, stat_t& info)
|
||||
bool fs::dir::read(std::string& name, stat_t& info)
|
||||
{
|
||||
if (m_dd != null) // close previous handle
|
||||
{
|
||||
#ifdef _WIN32
|
||||
FindClose((HANDLE)m_dd);
|
||||
#else
|
||||
::closedir((DIR*)m_dd);
|
||||
#endif
|
||||
}
|
||||
|
||||
g_tls_error = fse::ok;
|
||||
|
||||
m_dd = null;
|
||||
|
||||
if (!m_path)
|
||||
{
|
||||
return false;
|
||||
|
@ -776,43 +717,16 @@ bool fs::dir::get_first(std::string& name, stat_t& info)
|
|||
#ifdef _WIN32
|
||||
WIN32_FIND_DATAW found;
|
||||
|
||||
m_dd = (intptr_t)FindFirstFileW(m_path.get(), &found);
|
||||
|
||||
if (m_dd == null)
|
||||
if (m_dd == -1)
|
||||
{
|
||||
return false;
|
||||
m_dd = (std::intptr_t)FindFirstFileW(to_wchar(m_path.get() + "/*"s).get(), &found);
|
||||
|
||||
if (m_dd == -1)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
to_utf8(name, found.cFileName);
|
||||
|
||||
info.is_directory = (found.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
|
||||
info.is_writable = (found.dwFileAttributes & FILE_ATTRIBUTE_READONLY) == 0;
|
||||
info.size = ((u64)found.nFileSizeHigh << 32) | (u64)found.nFileSizeLow;
|
||||
info.atime = to_time_t(found.ftLastAccessTime);
|
||||
info.mtime = to_time_t(found.ftLastWriteTime);
|
||||
info.ctime = to_time_t(found.ftCreationTime);
|
||||
|
||||
return true;
|
||||
#else
|
||||
m_dd = (intptr_t)::opendir(m_path.get());
|
||||
|
||||
return get_next(name, info);
|
||||
#endif
|
||||
}
|
||||
|
||||
bool fs::dir::get_next(std::string& name, stat_t& info)
|
||||
{
|
||||
g_tls_error = fse::ok;
|
||||
|
||||
if (m_dd == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
WIN32_FIND_DATAW found;
|
||||
|
||||
if (!FindNextFileW((HANDLE)m_dd, &found))
|
||||
else if (!FindNextFileW((HANDLE)m_dd, &found))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
|
163
Utilities/File.h
163
Utilities/File.h
|
@ -26,16 +26,6 @@ enum class fse : u32 // filesystem (file or dir) error
|
|||
invalid_arguments,
|
||||
};
|
||||
|
||||
enum : u32 // obsolete flags
|
||||
{
|
||||
o_read = fom::read,
|
||||
o_write = fom::write,
|
||||
o_append = fom::append,
|
||||
o_create = fom::create,
|
||||
o_trunc = fom::trunc,
|
||||
o_excl = fom::excl,
|
||||
};
|
||||
|
||||
namespace fs
|
||||
{
|
||||
thread_local extern fse g_tls_error;
|
||||
|
@ -50,95 +40,156 @@ namespace fs
|
|||
s64 ctime;
|
||||
};
|
||||
|
||||
// Get file information
|
||||
bool stat(const std::string& path, stat_t& info);
|
||||
|
||||
// Check whether a file or a directory exists (not recommended, use is_file() or is_dir() instead)
|
||||
bool exists(const std::string& path);
|
||||
|
||||
// Check whether the file exists and is NOT a directory
|
||||
bool is_file(const std::string& file);
|
||||
|
||||
// Check whether the directory exists and is NOT a file
|
||||
bool is_dir(const std::string& dir);
|
||||
|
||||
// Delete empty directory
|
||||
bool remove_dir(const std::string& dir);
|
||||
|
||||
// Create directory
|
||||
bool create_dir(const std::string& dir);
|
||||
|
||||
// Create directories
|
||||
bool create_path(const std::string& path);
|
||||
|
||||
// Rename (move) file or directory
|
||||
bool rename(const std::string& from, const std::string& to);
|
||||
|
||||
// Copy file contents
|
||||
bool copy_file(const std::string& from, const std::string& to, bool overwrite);
|
||||
|
||||
// Delete file
|
||||
bool remove_file(const std::string& file);
|
||||
|
||||
// Change file size (possibly appending zeros)
|
||||
bool truncate_file(const std::string& file, u64 length);
|
||||
|
||||
struct file final
|
||||
class file final
|
||||
{
|
||||
using handle_type = std::intptr_t;
|
||||
|
||||
static const handle_type null = -1;
|
||||
constexpr static handle_type null = -1;
|
||||
|
||||
private:
|
||||
handle_type m_fd = null;
|
||||
|
||||
public:
|
||||
file() = default;
|
||||
|
||||
explicit file(const std::string& filename, u32 mode = fom::read)
|
||||
{
|
||||
open(filename, mode);
|
||||
}
|
||||
|
||||
file(file&& other)
|
||||
: m_fd(other.m_fd)
|
||||
{
|
||||
other.m_fd = null;
|
||||
}
|
||||
|
||||
file& operator =(file&& right)
|
||||
{
|
||||
std::swap(m_fd, right.m_fd);
|
||||
return *this;
|
||||
}
|
||||
|
||||
~file();
|
||||
explicit file(const std::string& filename, u32 mode = fom::read) { open(filename, mode); }
|
||||
|
||||
file(const file&) = delete;
|
||||
file(file&&) = delete; // possibly TODO
|
||||
// Check whether the handle is valid (opened file)
|
||||
bool is_opened() const
|
||||
{
|
||||
return m_fd != null;
|
||||
}
|
||||
|
||||
file& operator =(const file&) = delete;
|
||||
file& operator =(file&&) = delete; // possibly TODO
|
||||
|
||||
operator bool() const { return m_fd != null; }
|
||||
|
||||
void import(handle_type fd) { this->~file(); m_fd = fd; }
|
||||
// Check whether the handle is valid (opened file)
|
||||
explicit operator bool() const
|
||||
{
|
||||
return is_opened();
|
||||
}
|
||||
|
||||
// Open specified file with specified mode
|
||||
bool open(const std::string& filename, u32 mode = fom::read);
|
||||
bool is_opened() const { return m_fd != null; }
|
||||
bool trunc(u64 size) const; // change file size (possibly appending zero bytes)
|
||||
bool stat(stat_t& info) const; // get file info
|
||||
|
||||
// Change file size (possibly appending zero bytes)
|
||||
bool trunc(u64 size) const;
|
||||
|
||||
// Get file information
|
||||
bool stat(stat_t& info) const;
|
||||
|
||||
// Close the file explicitly (destructor automatically closes the file)
|
||||
bool close();
|
||||
|
||||
// Read the data from the file and return the amount of data written in buffer
|
||||
u64 read(void* buffer, u64 count) const;
|
||||
|
||||
// Write the data to the file and return the amount of data actually written
|
||||
u64 write(const void* buffer, u64 count) const;
|
||||
u64 write(const std::string &string) const;
|
||||
|
||||
// Write std::string
|
||||
u64 write(const std::string& string) const { return write(string.data(), string.size()); }
|
||||
|
||||
// Move file pointer
|
||||
u64 seek(s64 offset, fsm seek_mode = fsm::begin) const;
|
||||
|
||||
// Get file size
|
||||
u64 size() const;
|
||||
};
|
||||
|
||||
struct dir final
|
||||
class dir final
|
||||
{
|
||||
#ifdef _WIN32
|
||||
using handle_type = intptr_t;
|
||||
using name_type = std::unique_ptr<wchar_t[]>;
|
||||
|
||||
static const handle_type null = -1;
|
||||
#else
|
||||
using handle_type = intptr_t;
|
||||
using name_type = std::unique_ptr<char[]>;
|
||||
|
||||
static const handle_type null = 0;
|
||||
#endif
|
||||
|
||||
private:
|
||||
handle_type m_dd = null;
|
||||
name_type m_path;
|
||||
std::unique_ptr<char[]> m_path;
|
||||
std::intptr_t m_dd; // handle (aux)
|
||||
|
||||
public:
|
||||
dir() = default;
|
||||
|
||||
explicit dir(const std::string& dirname)
|
||||
{
|
||||
open(dirname);
|
||||
}
|
||||
|
||||
dir(dir&& other)
|
||||
: m_dd(other.m_dd)
|
||||
, m_path(std::move(other.m_path))
|
||||
{
|
||||
}
|
||||
|
||||
dir& operator =(dir&& right)
|
||||
{
|
||||
m_dd = right.m_dd;
|
||||
m_path = std::move(right.m_path);
|
||||
return *this;
|
||||
}
|
||||
|
||||
~dir();
|
||||
explicit dir(const std::string& dirname) { open(dirname); }
|
||||
|
||||
dir(const dir&) = delete;
|
||||
dir(dir&&) = delete; // possibly TODO
|
||||
// Check whether the handle is valid (opened directory)
|
||||
bool is_opened() const
|
||||
{
|
||||
return m_path.operator bool();
|
||||
}
|
||||
|
||||
dir& operator =(const dir&) = delete;
|
||||
dir& operator =(dir&&) = delete; // possibly TODO
|
||||
|
||||
operator bool() const { return m_path.operator bool(); }
|
||||
|
||||
void import(handle_type dd, const std::string& path);
|
||||
// Check whether the handle is valid (opened directory)
|
||||
explicit operator bool() const
|
||||
{
|
||||
return is_opened();
|
||||
}
|
||||
|
||||
// Open specified directory
|
||||
bool open(const std::string& dirname);
|
||||
bool is_opened() const { return *this; }
|
||||
|
||||
// Close the directory explicitly (destructor automatically closes the directory)
|
||||
bool close();
|
||||
|
||||
bool get_first(std::string& name, stat_t& info);
|
||||
//bool get_first(std::string& name);
|
||||
bool get_next(std::string& name, stat_t& info);
|
||||
//bool get_next(std::string& name);
|
||||
// Get next directory entry (UTF-8 name and file stat)
|
||||
bool read(std::string& name, stat_t& info);
|
||||
};
|
||||
}
|
||||
|
|
176
Utilities/GNU.h
176
Utilities/GNU.h
|
@ -45,16 +45,16 @@
|
|||
#define _byteswap_uint64(x) __builtin_bswap64(x)
|
||||
#define INFINITE 0xFFFFFFFF
|
||||
|
||||
inline uint64_t __umulh(uint64_t a, uint64_t b)
|
||||
inline std::uint64_t __umulh(std::uint64_t a, std::uint64_t b)
|
||||
{
|
||||
uint64_t result;
|
||||
std::uint64_t result;
|
||||
__asm__("mulq %[b]" : "=d" (result) : [a] "a" (a), [b] "rm" (b));
|
||||
return result;
|
||||
}
|
||||
|
||||
inline int64_t __mulh(int64_t a, int64_t b)
|
||||
inline std::int64_t __mulh(std::int64_t a, std::int64_t b)
|
||||
{
|
||||
int64_t result;
|
||||
std::int64_t result;
|
||||
__asm__("imulq %[b]" : "=d" (result) : [a] "a" (a), [b] "rm" (b));
|
||||
return result;
|
||||
}
|
||||
|
@ -81,202 +81,186 @@ int clock_gettime(clockid_t clk_id, struct timespec *tp);
|
|||
#endif /* __GNUG__ */
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
|
||||
// Unsigned 128-bit integer implementation
|
||||
struct alignas(16) uint128_t
|
||||
struct alignas(16) u128
|
||||
{
|
||||
uint64_t lo, hi;
|
||||
std::uint64_t lo, hi;
|
||||
|
||||
uint128_t() = default;
|
||||
u128() = default;
|
||||
|
||||
uint128_t(uint64_t l)
|
||||
u128(const u128&) = default;
|
||||
|
||||
u128(std::uint64_t l)
|
||||
: lo(l)
|
||||
, hi(0)
|
||||
{
|
||||
}
|
||||
|
||||
[[deprecated("Not implemented")]] inline uint128_t operator +(const uint128_t& r) const
|
||||
u128 operator +(const u128& r) const
|
||||
{
|
||||
return{};
|
||||
}
|
||||
|
||||
inline uint128_t operator +(uint64_t r) const
|
||||
{
|
||||
uint128_t value;
|
||||
value.lo = lo + r;
|
||||
value.hi = value.lo < r ? hi + 1 : hi;
|
||||
u128 value;
|
||||
_addcarry_u64(_addcarry_u64(0, r.lo, lo, &value.lo), r.hi, hi, &value.hi);
|
||||
return value;
|
||||
}
|
||||
|
||||
[[deprecated("Not implemented")]] inline uint128_t operator -(const uint128_t& r) const
|
||||
friend u128 operator +(const u128& l, std::uint64_t r)
|
||||
{
|
||||
return{};
|
||||
}
|
||||
|
||||
inline uint128_t operator -(uint64_t r) const
|
||||
{
|
||||
uint128_t value;
|
||||
value.lo = lo - r;
|
||||
value.hi = lo < r ? hi - 1 : hi;
|
||||
u128 value;
|
||||
_addcarry_u64(_addcarry_u64(0, r, l.lo, &value.lo), l.hi, 0, &value.hi);
|
||||
return value;
|
||||
}
|
||||
|
||||
inline uint128_t operator +() const
|
||||
friend u128 operator +(std::uint64_t l, const u128& r)
|
||||
{
|
||||
u128 value;
|
||||
_addcarry_u64(_addcarry_u64(0, r.lo, l, &value.lo), 0, r.hi, &value.hi);
|
||||
return value;
|
||||
}
|
||||
|
||||
u128 operator -(const u128& r) const
|
||||
{
|
||||
u128 value;
|
||||
_subborrow_u64(_subborrow_u64(0, r.lo, lo, &value.lo), r.hi, hi, &value.hi);
|
||||
return value;
|
||||
}
|
||||
|
||||
friend u128 operator -(const u128& l, std::uint64_t r)
|
||||
{
|
||||
u128 value;
|
||||
_subborrow_u64(_subborrow_u64(0, r, l.lo, &value.lo), 0, l.hi, &value.hi);
|
||||
return value;
|
||||
}
|
||||
|
||||
friend u128 operator -(std::uint64_t l, const u128& r)
|
||||
{
|
||||
u128 value;
|
||||
_subborrow_u64(_subborrow_u64(0, r.lo, l, &value.lo), r.hi, 0, &value.hi);
|
||||
return value;
|
||||
}
|
||||
|
||||
u128 operator +() const
|
||||
{
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline uint128_t operator -() const
|
||||
u128 operator -() const
|
||||
{
|
||||
uint128_t value;
|
||||
value.lo = ~lo + 1;
|
||||
value.hi = lo ? ~hi : ~hi + 1;
|
||||
u128 value;
|
||||
_subborrow_u64(_subborrow_u64(0, lo, 0, &value.lo), hi, 0, &value.hi);
|
||||
return value;
|
||||
}
|
||||
|
||||
inline uint128_t& operator ++()
|
||||
u128& operator ++()
|
||||
{
|
||||
if (!++lo) ++hi;
|
||||
_addcarry_u64(_addcarry_u64(0, 1, lo, &lo), 0, hi, &hi);
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline uint128_t operator ++(int)
|
||||
u128 operator ++(int)
|
||||
{
|
||||
uint128_t value = *this;
|
||||
if (!++lo) ++hi;
|
||||
u128 value = *this;
|
||||
_addcarry_u64(_addcarry_u64(0, 1, lo, &lo), 0, hi, &hi);
|
||||
return value;
|
||||
}
|
||||
|
||||
inline uint128_t& operator --()
|
||||
u128& operator --()
|
||||
{
|
||||
if (!lo--) hi--;
|
||||
_subborrow_u64(_subborrow_u64(0, 1, lo, &lo), 0, hi, &hi);
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline uint128_t operator --(int)
|
||||
u128 operator --(int)
|
||||
{
|
||||
uint128_t value = *this;
|
||||
if (!lo--) hi--;
|
||||
u128 value = *this;
|
||||
_subborrow_u64(_subborrow_u64(0, 1, lo, &lo), 0, hi, &hi);
|
||||
return value;
|
||||
}
|
||||
|
||||
inline uint128_t operator ~() const
|
||||
u128 operator ~() const
|
||||
{
|
||||
uint128_t value;
|
||||
u128 value;
|
||||
value.lo = ~lo;
|
||||
value.hi = ~hi;
|
||||
return value;
|
||||
}
|
||||
|
||||
inline uint128_t operator &(const uint128_t& r) const
|
||||
u128 operator &(const u128& r) const
|
||||
{
|
||||
uint128_t value;
|
||||
u128 value;
|
||||
value.lo = lo & r.lo;
|
||||
value.hi = hi & r.hi;
|
||||
return value;
|
||||
}
|
||||
|
||||
inline uint128_t operator |(const uint128_t& r) const
|
||||
u128 operator |(const u128& r) const
|
||||
{
|
||||
uint128_t value;
|
||||
u128 value;
|
||||
value.lo = lo | r.lo;
|
||||
value.hi = hi | r.hi;
|
||||
return value;
|
||||
}
|
||||
|
||||
inline uint128_t operator ^(const uint128_t& r) const
|
||||
u128 operator ^(const u128& r) const
|
||||
{
|
||||
uint128_t value;
|
||||
u128 value;
|
||||
value.lo = lo ^ r.lo;
|
||||
value.hi = hi ^ r.hi;
|
||||
return value;
|
||||
}
|
||||
|
||||
[[deprecated("Not implemented")]] inline uint128_t& operator +=(const uint128_t& r)
|
||||
u128& operator +=(const u128& r)
|
||||
{
|
||||
_addcarry_u64(_addcarry_u64(0, r.lo, lo, &lo), r.hi, hi, &hi);
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline uint128_t& operator +=(uint64_t r)
|
||||
u128& operator +=(uint64_t r)
|
||||
{
|
||||
hi = (lo += r) < r ? hi + 1 : hi;
|
||||
_addcarry_u64(_addcarry_u64(0, r, lo, &lo), 0, hi, &hi);
|
||||
return *this;
|
||||
}
|
||||
|
||||
[[deprecated("Not implemented")]] inline uint128_t& operator -=(const uint128_t& r)
|
||||
{
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline uint128_t& operator &=(const uint128_t& r)
|
||||
u128& operator &=(const u128& r)
|
||||
{
|
||||
lo &= r.lo;
|
||||
hi &= r.hi;
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline uint128_t& operator |=(const uint128_t& r)
|
||||
u128& operator |=(const u128& r)
|
||||
{
|
||||
lo |= r.lo;
|
||||
hi |= r.hi;
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline uint128_t& operator ^=(const uint128_t& r)
|
||||
u128& operator ^=(const u128& r)
|
||||
{
|
||||
lo ^= r.lo;
|
||||
hi ^= r.hi;
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
using __uint128_t = uint128_t;
|
||||
#endif
|
||||
|
||||
inline uint32_t cntlz32(uint32_t arg)
|
||||
inline std::uint32_t cntlz32(std::uint32_t arg)
|
||||
{
|
||||
#if defined(_MSC_VER)
|
||||
unsigned long res;
|
||||
if (!_BitScanReverse(&res, arg))
|
||||
{
|
||||
return 32;
|
||||
}
|
||||
else
|
||||
{
|
||||
return res ^ 31;
|
||||
}
|
||||
return _BitScanReverse(&res, arg) ? res ^ 31 : 32;
|
||||
#else
|
||||
if (arg)
|
||||
{
|
||||
return __builtin_clzll((uint64_t)arg) - 32;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 32;
|
||||
}
|
||||
return arg ? __builtin_clzll(arg) - 32 : 32;
|
||||
#endif
|
||||
}
|
||||
|
||||
inline uint64_t cntlz64(uint64_t arg)
|
||||
inline std::uint64_t cntlz64(std::uint64_t arg)
|
||||
{
|
||||
#if defined(_MSC_VER)
|
||||
unsigned long res;
|
||||
if (!_BitScanReverse64(&res, arg))
|
||||
{
|
||||
return 64;
|
||||
}
|
||||
else
|
||||
{
|
||||
return res ^ 63;
|
||||
}
|
||||
return _BitScanReverse64(&res, arg) ? res ^ 63 : 64;
|
||||
#else
|
||||
if (arg)
|
||||
{
|
||||
return __builtin_clzll(arg);
|
||||
}
|
||||
else
|
||||
{
|
||||
return 64;
|
||||
}
|
||||
return arg ? __builtin_clzll(arg) : 64;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
|
|
@ -79,7 +79,6 @@ namespace Log
|
|||
LogChannel();
|
||||
LogChannel(const std::string& name);
|
||||
LogChannel(LogChannel& other) = delete;
|
||||
LogChannel& operator = (LogChannel& other) = delete;
|
||||
void log(const LogMessage &msg);
|
||||
void addListener(std::shared_ptr<LogListener> listener);
|
||||
void removeListener(std::shared_ptr<LogListener> listener);
|
||||
|
|
|
@ -27,19 +27,19 @@ public:
|
|||
~sleep_queue_entry_t();
|
||||
|
||||
// add thread to the sleep queue
|
||||
inline void enter()
|
||||
void enter()
|
||||
{
|
||||
add_entry();
|
||||
}
|
||||
|
||||
// remove thread from the sleep queue
|
||||
inline void leave()
|
||||
void leave()
|
||||
{
|
||||
remove_entry();
|
||||
}
|
||||
|
||||
// check whether the thread exists in the sleep queue
|
||||
inline explicit operator bool() const
|
||||
explicit operator bool() const
|
||||
{
|
||||
return find();
|
||||
}
|
||||
|
|
|
@ -15,9 +15,13 @@ std::string v128::to_xyzw() const
|
|||
return fmt::format("x: %g y: %g z: %g w: %g", _f[3], _f[2], _f[1], _f[0]);
|
||||
}
|
||||
|
||||
std::string fmt::to_hex(u64 value, size_t count)
|
||||
std::string fmt::to_hex(u64 value, u64 count)
|
||||
{
|
||||
assert(count - 1 < 16);
|
||||
if (count - 1 >= 16)
|
||||
{
|
||||
throw EXCEPTION("Invalid count: 0x%llx", count);
|
||||
}
|
||||
|
||||
count = std::max<u64>(count, 16 - cntlz64(value) / 4);
|
||||
|
||||
char res[16] = {};
|
||||
|
|
|
@ -141,7 +141,7 @@ namespace fmt
|
|||
return src;
|
||||
}
|
||||
|
||||
std::string to_hex(u64 value, size_t count = 1);
|
||||
std::string to_hex(u64 value, u64 count = 1);
|
||||
std::string to_udec(u64 value);
|
||||
std::string to_sdec(s64 value);
|
||||
|
||||
|
|
|
@ -901,7 +901,7 @@ bool handle_access_violation(u32 addr, bool is_writing, x64_context* context)
|
|||
return false;
|
||||
}
|
||||
|
||||
memcpy(vm::priv_ptr(addr), XMMREG(context, reg - X64R_XMM0), 16);
|
||||
std::memcpy(vm::base_priv(addr), XMMREG(context, reg - X64R_XMM0), 16);
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -911,7 +911,7 @@ bool handle_access_violation(u32 addr, bool is_writing, x64_context* context)
|
|||
return false;
|
||||
}
|
||||
|
||||
memcpy(vm::priv_ptr(addr), ®_value, d_size);
|
||||
std::memcpy(vm::base_priv(addr), ®_value, d_size);
|
||||
break;
|
||||
}
|
||||
case X64OP_MOVS:
|
||||
|
@ -922,7 +922,7 @@ bool handle_access_violation(u32 addr, bool is_writing, x64_context* context)
|
|||
return false;
|
||||
}
|
||||
|
||||
if (vm::get_ptr(addr) != (void*)RDI(context))
|
||||
if (vm::base(addr) != (void*)RDI(context))
|
||||
{
|
||||
LOG_ERROR(MEMORY, "X64OP_MOVS: rdi=0x%llx, rsi=0x%llx, addr=0x%x", (u64)RDI(context), (u64)RSI(context), addr);
|
||||
return false;
|
||||
|
@ -935,8 +935,8 @@ bool handle_access_violation(u32 addr, bool is_writing, x64_context* context)
|
|||
u64 value;
|
||||
|
||||
// copy data
|
||||
memcpy(&value, (void*)RSI(context), d_size);
|
||||
memcpy(vm::priv_ptr(a_addr), &value, d_size);
|
||||
std::memcpy(&value, (void*)RSI(context), d_size);
|
||||
std::memcpy(vm::base_priv(a_addr), &value, d_size);
|
||||
|
||||
// shift pointers
|
||||
if (EFLAGS(context) & 0x400 /* direction flag */)
|
||||
|
@ -977,7 +977,7 @@ bool handle_access_violation(u32 addr, bool is_writing, x64_context* context)
|
|||
return false;
|
||||
}
|
||||
|
||||
if (vm::get_ptr(addr) != (void*)RDI(context))
|
||||
if (vm::base(addr) != (void*)RDI(context))
|
||||
{
|
||||
LOG_ERROR(MEMORY, "X64OP_STOS: rdi=0x%llx, addr=0x%x", (u64)RDI(context), addr);
|
||||
return false;
|
||||
|
@ -994,7 +994,7 @@ bool handle_access_violation(u32 addr, bool is_writing, x64_context* context)
|
|||
while (a_addr >> 12 == addr >> 12)
|
||||
{
|
||||
// fill data with value
|
||||
memcpy(vm::priv_ptr(a_addr), &value, d_size);
|
||||
std::memcpy(vm::base_priv(a_addr), &value, d_size);
|
||||
|
||||
// shift pointers
|
||||
if (EFLAGS(context) & 0x400 /* direction flag */)
|
||||
|
@ -1035,10 +1035,10 @@ bool handle_access_violation(u32 addr, bool is_writing, x64_context* context)
|
|||
|
||||
switch (d_size)
|
||||
{
|
||||
case 1: reg_value = vm::priv_ref<atomic_t<u8>>(addr).exchange((u8)reg_value); break;
|
||||
case 2: reg_value = vm::priv_ref<atomic_t<u16>>(addr).exchange((u16)reg_value); break;
|
||||
case 4: reg_value = vm::priv_ref<atomic_t<u32>>(addr).exchange((u32)reg_value); break;
|
||||
case 8: reg_value = vm::priv_ref<atomic_t<u64>>(addr).exchange((u64)reg_value); break;
|
||||
case 1: reg_value = sync_lock_test_and_set((u8*)vm::base_priv(addr), (u8)reg_value); break;
|
||||
case 2: reg_value = sync_lock_test_and_set((u16*)vm::base_priv(addr), (u16)reg_value); break;
|
||||
case 4: reg_value = sync_lock_test_and_set((u32*)vm::base_priv(addr), (u32)reg_value); break;
|
||||
case 8: reg_value = sync_lock_test_and_set((u64*)vm::base_priv(addr), (u64)reg_value); break;
|
||||
default: return false;
|
||||
}
|
||||
|
||||
|
@ -1058,10 +1058,10 @@ bool handle_access_violation(u32 addr, bool is_writing, x64_context* context)
|
|||
|
||||
switch (d_size)
|
||||
{
|
||||
case 1: old_value = vm::priv_ref<atomic_t<u8>>(addr).compare_and_swap((u8)cmp_value, (u8)reg_value); break;
|
||||
case 2: old_value = vm::priv_ref<atomic_t<u16>>(addr).compare_and_swap((u16)cmp_value, (u16)reg_value); break;
|
||||
case 4: old_value = vm::priv_ref<atomic_t<u32>>(addr).compare_and_swap((u32)cmp_value, (u32)reg_value); break;
|
||||
case 8: old_value = vm::priv_ref<atomic_t<u64>>(addr).compare_and_swap((u64)cmp_value, (u64)reg_value); break;
|
||||
case 1: old_value = sync_val_compare_and_swap((u8*)vm::base_priv(addr), (u8)cmp_value, (u8)reg_value); break;
|
||||
case 2: old_value = sync_val_compare_and_swap((u16*)vm::base_priv(addr), (u16)cmp_value, (u16)reg_value); break;
|
||||
case 4: old_value = sync_val_compare_and_swap((u32*)vm::base_priv(addr), (u32)cmp_value, (u32)reg_value); break;
|
||||
case 8: old_value = sync_val_compare_and_swap((u64*)vm::base_priv(addr), (u64)cmp_value, (u64)reg_value); break;
|
||||
default: return false;
|
||||
}
|
||||
|
||||
|
@ -1081,10 +1081,10 @@ bool handle_access_violation(u32 addr, bool is_writing, x64_context* context)
|
|||
|
||||
switch (d_size)
|
||||
{
|
||||
case 1: value = vm::priv_ref<atomic_t<u8>>(addr) &= (u8)value; break;
|
||||
case 2: value = vm::priv_ref<atomic_t<u16>>(addr) &= (u16)value; break;
|
||||
case 4: value = vm::priv_ref<atomic_t<u32>>(addr) &= (u32)value; break;
|
||||
case 8: value = vm::priv_ref<atomic_t<u64>>(addr) &= value; break;
|
||||
case 1: value &= sync_fetch_and_and((u8*)vm::base_priv(addr), (u8)value); break;
|
||||
case 2: value &= sync_fetch_and_and((u16*)vm::base_priv(addr), (u16)value); break;
|
||||
case 4: value &= sync_fetch_and_and((u32*)vm::base_priv(addr), (u32)value); break;
|
||||
case 8: value &= sync_fetch_and_and((u64*)vm::base_priv(addr), (u64)value); break;
|
||||
default: return false;
|
||||
}
|
||||
|
||||
|
@ -1114,7 +1114,7 @@ bool handle_access_violation(u32 addr, bool is_writing, x64_context* context)
|
|||
|
||||
void _se_translator(unsigned int u, EXCEPTION_POINTERS* pExp)
|
||||
{
|
||||
const u64 addr64 = (u64)pExp->ExceptionRecord->ExceptionInformation[1] - (u64)vm::g_base_addr;
|
||||
const u64 addr64 = (u64)pExp->ExceptionRecord->ExceptionInformation[1] - (u64)vm::base(0);
|
||||
const bool is_writing = pExp->ExceptionRecord->ExceptionInformation[0] != 0;
|
||||
|
||||
if (u == EXCEPTION_ACCESS_VIOLATION && (u32)addr64 == addr64)
|
||||
|
@ -1122,12 +1122,14 @@ void _se_translator(unsigned int u, EXCEPTION_POINTERS* pExp)
|
|||
throw EXCEPTION("Access violation %s location 0x%llx", is_writing ? "writing" : "reading", addr64);
|
||||
}
|
||||
|
||||
//__int2c(); // if it crashed there, check the callstack for the actual source of the crash
|
||||
__debugbreak(); // if it reached there, there should probably be a possibility to check the callstack
|
||||
|
||||
throw EXCEPTION("Fatal error occured %s location %p at %p", is_writing ? "writing" : "reading", pExp->ExceptionRecord->ExceptionInformation[1], pExp->ExceptionRecord->ExceptionAddress);
|
||||
}
|
||||
|
||||
const PVOID exception_handler = (atexit([]{ RemoveVectoredExceptionHandler(exception_handler); }), AddVectoredExceptionHandler(1, [](PEXCEPTION_POINTERS pExp) -> LONG
|
||||
{
|
||||
const u64 addr64 = (u64)pExp->ExceptionRecord->ExceptionInformation[1] - (u64)vm::g_base_addr;
|
||||
const u64 addr64 = (u64)pExp->ExceptionRecord->ExceptionInformation[1] - (u64)vm::base(0);
|
||||
const bool is_writing = pExp->ExceptionRecord->ExceptionInformation[0] != 0;
|
||||
|
||||
if (pExp->ExceptionRecord->ExceptionCode == EXCEPTION_ACCESS_VIOLATION &&
|
||||
|
@ -1154,7 +1156,7 @@ const auto exception_filter = SetUnhandledExceptionFilter([](PEXCEPTION_POINTERS
|
|||
|
||||
void signal_handler(int sig, siginfo_t* info, void* uct)
|
||||
{
|
||||
const u64 addr64 = (u64)info->si_addr - (u64)vm::g_base_addr;
|
||||
const u64 addr64 = (u64)info->si_addr - (u64)vm::base(0);
|
||||
|
||||
#ifdef __APPLE__
|
||||
const bool is_writing = ((ucontext_t*)uct)->uc_mcontext->__es.__err & 0x2;
|
||||
|
@ -1203,14 +1205,14 @@ std::string thread_ctrl_t::get_name() const
|
|||
|
||||
named_thread_t::named_thread_t(std::function<std::string()> name, std::function<void()> func)
|
||||
{
|
||||
start(std::move(name), func);
|
||||
start(std::move(name), std::move(func));
|
||||
}
|
||||
|
||||
named_thread_t::~named_thread_t()
|
||||
{
|
||||
if (m_thread)
|
||||
{
|
||||
std::printf("Fatal: thread '%s' is neither joined nor detached\n", this->get_name().c_str());
|
||||
std::printf("Fatal: thread neither joined nor detached\n");
|
||||
std::terminate();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,20 +2,20 @@
|
|||
|
||||
const class thread_ctrl_t* get_current_thread_ctrl();
|
||||
|
||||
// named thread control class
|
||||
// Named thread control class
|
||||
class thread_ctrl_t final
|
||||
{
|
||||
friend class named_thread_t;
|
||||
|
||||
template<typename T> friend void current_thread_register_atexit(T);
|
||||
|
||||
// thread handler
|
||||
// Thread handler
|
||||
std::thread m_thread;
|
||||
|
||||
// name getter
|
||||
// Name getter
|
||||
const std::function<std::string()> m_name;
|
||||
|
||||
// functions executed at thread exit (temporarily)
|
||||
// Functions executed at thread exit (temporarily)
|
||||
std::vector<std::function<void()>> m_atexit;
|
||||
|
||||
public:
|
||||
|
@ -24,11 +24,13 @@ public:
|
|||
{
|
||||
}
|
||||
|
||||
// get thread name
|
||||
thread_ctrl_t(const thread_ctrl_t&) = delete;
|
||||
|
||||
// Get thread name
|
||||
std::string get_name() const;
|
||||
};
|
||||
|
||||
// register function at thread exit (temporarily)
|
||||
// Register function at thread exit (temporarily)
|
||||
template<typename T> void current_thread_register_atexit(T func)
|
||||
{
|
||||
extern thread_local thread_ctrl_t* g_tls_this_thread;
|
||||
|
@ -38,73 +40,69 @@ template<typename T> void current_thread_register_atexit(T func)
|
|||
|
||||
class named_thread_t
|
||||
{
|
||||
// pointer to managed resource (shared with actual thread)
|
||||
// Pointer to managed resource (shared with actual thread)
|
||||
std::shared_ptr<thread_ctrl_t> m_thread;
|
||||
|
||||
public:
|
||||
// thread mutex for external use
|
||||
// Thread mutex for external use
|
||||
std::mutex mutex;
|
||||
|
||||
// thread condition variable for external use
|
||||
// Thread condition variable for external use
|
||||
std::condition_variable cv;
|
||||
|
||||
public:
|
||||
// initialize in empty state
|
||||
// Initialize in empty state
|
||||
named_thread_t() = default;
|
||||
|
||||
// create named thread
|
||||
// Create named thread
|
||||
named_thread_t(std::function<std::string()> name, std::function<void()> func);
|
||||
|
||||
// destructor, will terminate if thread is neither joined nor detached
|
||||
virtual ~named_thread_t();
|
||||
|
||||
// Deleted copy/move constructors + copy/move operators
|
||||
named_thread_t(const named_thread_t&) = delete;
|
||||
|
||||
named_thread_t& operator =(const named_thread_t&) = delete;
|
||||
// Destructor, calls std::terminate if the thread is neither joined nor detached
|
||||
virtual ~named_thread_t();
|
||||
|
||||
public:
|
||||
// get thread name
|
||||
// Get thread name
|
||||
std::string get_name() const;
|
||||
|
||||
// create named thread (current state must be empty)
|
||||
// Create named thread (current state must be empty)
|
||||
void start(std::function<std::string()> name, std::function<void()> func);
|
||||
|
||||
// detach thread -> empty state
|
||||
// Detach thread -> empty state
|
||||
void detach();
|
||||
|
||||
// join thread -> empty state
|
||||
// Join thread -> empty state
|
||||
void join();
|
||||
|
||||
// check if not empty
|
||||
// Check whether the thread is not in "empty state"
|
||||
bool joinable() const { return m_thread.operator bool(); }
|
||||
|
||||
// check whether it is the current running thread
|
||||
// Check whether it is the currently running thread
|
||||
bool is_current() const;
|
||||
|
||||
// get internal thread pointer
|
||||
// Get internal thread pointer
|
||||
const thread_ctrl_t* get_thread_ctrl() const { return m_thread.get(); }
|
||||
};
|
||||
|
||||
class autojoin_thread_t final : private named_thread_t
|
||||
// Wrapper for named_thread_t, joins automatically in the destructor
|
||||
class autojoin_thread_t final
|
||||
{
|
||||
public:
|
||||
using named_thread_t::mutex;
|
||||
using named_thread_t::cv;
|
||||
named_thread_t m_thread;
|
||||
|
||||
public:
|
||||
autojoin_thread_t() = delete;
|
||||
|
||||
autojoin_thread_t(std::function<std::string()> name, std::function<void()> func)
|
||||
: m_thread(std::move(name), std::move(func))
|
||||
{
|
||||
start(std::move(name), std::move(func));
|
||||
}
|
||||
|
||||
virtual ~autojoin_thread_t() override
|
||||
{
|
||||
join();
|
||||
}
|
||||
autojoin_thread_t(const autojoin_thread_t&) = delete;
|
||||
|
||||
using named_thread_t::is_current;
|
||||
~autojoin_thread_t() noexcept(false) // Allow exceptions
|
||||
{
|
||||
m_thread.join();
|
||||
}
|
||||
};
|
||||
|
||||
extern const std::function<bool()> SQUEUE_ALWAYS_EXIT;
|
||||
|
|
|
@ -38,4 +38,4 @@ namespace memory_helper
|
|||
munmap(pointer, size);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue