mirror of
https://github.com/cemu-project/Cemu.git
synced 2025-07-09 16:31:19 +12:00
Various smaller code improvements (#1343)
This commit is contained in:
parent
adffd53dbd
commit
8508c62540
13 changed files with 153 additions and 142 deletions
|
@ -4,7 +4,7 @@
|
|||
|
||||
using MPTR = uint32; // generic address in PowerPC memory space
|
||||
|
||||
#define MPTR_NULL (0)
|
||||
#define MPTR_NULL (0)
|
||||
|
||||
using VAddr = uint32; // virtual address
|
||||
using PAddr = uint32; // physical address
|
||||
|
@ -14,137 +14,175 @@ extern uint8* PPCInterpreterGetStackPointer();
|
|||
extern uint8* PPCInterpreter_PushAndReturnStackPointer(sint32 offset);
|
||||
extern void PPCInterpreterModifyStackPointer(sint32 offset);
|
||||
|
||||
class MEMPTRBase {};
|
||||
class MEMPTRBase
|
||||
{
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
template<typename T>
|
||||
class MEMPTR : MEMPTRBase
|
||||
{
|
||||
public:
|
||||
constexpr MEMPTR()
|
||||
: m_value(0) { }
|
||||
public:
|
||||
constexpr MEMPTR() noexcept
|
||||
: m_value(0) {}
|
||||
|
||||
explicit constexpr MEMPTR(uint32 offset)
|
||||
: m_value(offset) { }
|
||||
explicit constexpr MEMPTR(uint32 offset) noexcept
|
||||
: m_value(offset) {}
|
||||
|
||||
explicit constexpr MEMPTR(const uint32be& offset)
|
||||
: m_value(offset) { }
|
||||
explicit constexpr MEMPTR(const uint32be& offset) noexcept
|
||||
: m_value(offset) {}
|
||||
|
||||
constexpr MEMPTR(std::nullptr_t)
|
||||
: m_value(0) { }
|
||||
constexpr MEMPTR(std::nullptr_t) noexcept
|
||||
: m_value(0) {}
|
||||
|
||||
MEMPTR(T* ptr)
|
||||
MEMPTR(T* ptr) noexcept
|
||||
{
|
||||
if (ptr == nullptr)
|
||||
m_value = 0;
|
||||
else
|
||||
{
|
||||
cemu_assert_debug((uint8*)ptr >= memory_base && (uint8*)ptr <= memory_base + 0x100000000);
|
||||
m_value = (uint32)((uintptr_t)ptr - (uintptr_t)memory_base);
|
||||
}
|
||||
{
|
||||
cemu_assert_debug((uint8*)ptr >= memory_base && (uint8*)ptr <= memory_base + 0x100000000);
|
||||
m_value = (uint32)((uintptr_t)ptr - (uintptr_t)memory_base);
|
||||
}
|
||||
}
|
||||
|
||||
constexpr MEMPTR(const MEMPTR& memptr)
|
||||
: m_value(memptr.m_value) { }
|
||||
constexpr MEMPTR(const MEMPTR&) noexcept = default;
|
||||
|
||||
constexpr MEMPTR& operator=(const MEMPTR& memptr)
|
||||
{
|
||||
m_value = memptr.m_value;
|
||||
return *this;
|
||||
}
|
||||
constexpr MEMPTR& operator=(const MEMPTR&) noexcept = default;
|
||||
|
||||
constexpr MEMPTR& operator=(const uint32& offset)
|
||||
constexpr MEMPTR& operator=(const uint32& offset) noexcept
|
||||
{
|
||||
m_value = offset;
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr MEMPTR& operator=(const std::nullptr_t rhs)
|
||||
constexpr MEMPTR& operator=(std::nullptr_t) noexcept
|
||||
{
|
||||
m_value = 0;
|
||||
return *this;
|
||||
}
|
||||
|
||||
MEMPTR& operator=(T* ptr)
|
||||
MEMPTR& operator=(T* ptr) noexcept
|
||||
{
|
||||
if (ptr == nullptr)
|
||||
if (ptr == nullptr)
|
||||
m_value = 0;
|
||||
else
|
||||
{
|
||||
cemu_assert_debug((uint8*)ptr >= memory_base && (uint8*)ptr <= memory_base + 0x100000000);
|
||||
m_value = (uint32)((uintptr_t)ptr - (uintptr_t)memory_base);
|
||||
}
|
||||
{
|
||||
cemu_assert_debug((uint8*)ptr >= memory_base && (uint8*)ptr <= memory_base + 0x100000000);
|
||||
m_value = (uint32)((uintptr_t)ptr - (uintptr_t)memory_base);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool atomic_compare_exchange(T* comparePtr, T* newPtr)
|
||||
bool atomic_compare_exchange(T* comparePtr, T* newPtr) noexcept
|
||||
{
|
||||
MEMPTR<T> mp_compare = comparePtr;
|
||||
MEMPTR<T> mp_new = newPtr;
|
||||
std::atomic<uint32be>* thisValueAtomic = (std::atomic<uint32be>*)&m_value;
|
||||
auto* thisValueAtomic = reinterpret_cast<std::atomic<uint32be>*>(&m_value);
|
||||
return thisValueAtomic->compare_exchange_strong(mp_compare.m_value, mp_new.m_value);
|
||||
}
|
||||
|
||||
explicit constexpr operator bool() const noexcept { return m_value != 0; }
|
||||
|
||||
constexpr operator T*() const noexcept { return GetPtr(); } // allow implicit cast to wrapped pointer type
|
||||
explicit constexpr operator bool() const noexcept
|
||||
{
|
||||
return m_value != 0;
|
||||
}
|
||||
|
||||
// allow implicit cast to wrapped pointer type
|
||||
constexpr operator T*() const noexcept
|
||||
{
|
||||
return GetPtr();
|
||||
}
|
||||
|
||||
template <typename X>
|
||||
explicit operator MEMPTR<X>() const { return MEMPTR<X>(this->m_value); }
|
||||
template<typename X>
|
||||
explicit operator MEMPTR<X>() const noexcept
|
||||
{
|
||||
return MEMPTR<X>(this->m_value);
|
||||
}
|
||||
|
||||
MEMPTR operator+(const MEMPTR& ptr) { return MEMPTR(this->GetMPTR() + ptr.GetMPTR()); }
|
||||
MEMPTR operator-(const MEMPTR& ptr) { return MEMPTR(this->GetMPTR() - ptr.GetMPTR()); }
|
||||
MEMPTR operator+(const MEMPTR& ptr) noexcept
|
||||
{
|
||||
return MEMPTR(this->GetMPTR() + ptr.GetMPTR());
|
||||
}
|
||||
MEMPTR operator-(const MEMPTR& ptr) noexcept
|
||||
{
|
||||
return MEMPTR(this->GetMPTR() - ptr.GetMPTR());
|
||||
}
|
||||
|
||||
MEMPTR operator+(sint32 v)
|
||||
MEMPTR operator+(sint32 v) noexcept
|
||||
{
|
||||
// pointer arithmetic
|
||||
return MEMPTR(this->GetMPTR() + v * 4);
|
||||
}
|
||||
|
||||
MEMPTR operator-(sint32 v)
|
||||
MEMPTR operator-(sint32 v) noexcept
|
||||
{
|
||||
// pointer arithmetic
|
||||
return MEMPTR(this->GetMPTR() - v * 4);
|
||||
}
|
||||
|
||||
MEMPTR& operator+=(sint32 v)
|
||||
MEMPTR& operator+=(sint32 v) noexcept
|
||||
{
|
||||
m_value += v * sizeof(T);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <class Q = T>
|
||||
typename std::enable_if<!std::is_same<Q, void>::value, Q>::type&
|
||||
operator*() const { return *GetPtr(); }
|
||||
template<typename Q = T>
|
||||
std::enable_if_t<!std::is_same_v<Q, void>, Q>& operator*() const noexcept
|
||||
{
|
||||
return *GetPtr();
|
||||
}
|
||||
|
||||
T* operator->() const { return GetPtr(); }
|
||||
constexpr T* operator->() const noexcept
|
||||
{
|
||||
return GetPtr();
|
||||
}
|
||||
|
||||
template <class Q = T>
|
||||
typename std::enable_if<!std::is_same<Q, void>::value, Q>::type&
|
||||
operator[](int index) { return GetPtr()[index]; }
|
||||
template<typename Q = T>
|
||||
std::enable_if_t<!std::is_same_v<Q, void>, Q>& operator[](int index) noexcept
|
||||
{
|
||||
return GetPtr()[index];
|
||||
}
|
||||
|
||||
T* GetPtr() const { return (T*)(m_value == 0 ? nullptr : memory_base + (uint32)m_value); }
|
||||
T* GetPtr() const noexcept
|
||||
{
|
||||
return (T*)(m_value == 0 ? nullptr : memory_base + (uint32)m_value);
|
||||
}
|
||||
|
||||
template <typename C>
|
||||
C* GetPtr() const { return (C*)(GetPtr()); }
|
||||
template<typename C>
|
||||
C* GetPtr() const noexcept
|
||||
{
|
||||
return static_cast<C*>(GetPtr());
|
||||
}
|
||||
|
||||
constexpr uint32 GetMPTR() const { return m_value.value(); }
|
||||
constexpr const uint32be& GetBEValue() const { return m_value; }
|
||||
[[nodiscard]] constexpr uint32 GetMPTR() const noexcept
|
||||
{
|
||||
return m_value.value();
|
||||
}
|
||||
[[nodiscard]] constexpr const uint32be& GetBEValue() const noexcept
|
||||
{
|
||||
return m_value;
|
||||
}
|
||||
|
||||
constexpr bool IsNull() const { return m_value == 0; }
|
||||
[[nodiscard]] constexpr bool IsNull() const noexcept
|
||||
{
|
||||
return m_value == 0;
|
||||
}
|
||||
|
||||
private:
|
||||
private:
|
||||
uint32be m_value;
|
||||
};
|
||||
|
||||
static_assert(sizeof(MEMPTR<void*>) == sizeof(uint32be));
|
||||
static_assert(std::is_trivially_copyable_v<MEMPTR<void*>>);
|
||||
|
||||
#include "StackAllocator.h"
|
||||
#include "SysAllocator.h"
|
||||
|
||||
template <typename T>
|
||||
template<typename T>
|
||||
struct fmt::formatter<MEMPTR<T>> : formatter<string_view>
|
||||
{
|
||||
template <typename FormatContext>
|
||||
auto format(const MEMPTR<T>& v, FormatContext& ctx) const -> format_context::iterator { return fmt::format_to(ctx.out(), "{:#x}", v.GetMPTR()); }
|
||||
template<typename FormatContext>
|
||||
auto format(const MEMPTR<T>& v, FormatContext& ctx) const -> format_context::iterator
|
||||
{
|
||||
return fmt::format_to(ctx.out(), "{:#x}", v.GetMPTR());
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue