1
0
mirror of https://github.com/RPCS3/rpcs3.git synced 2024-11-26 04:32:35 +01:00

Partial commit: Memory

This commit is contained in:
Nekotekina 2016-02-02 00:52:49 +03:00
parent b85fc50854
commit 0c7f763889
6 changed files with 422 additions and 607 deletions

View File

@ -18,7 +18,7 @@ bool VirtualMemoryBlock::IsInMyRange(const u32 addr, const u32 size)
u32 VirtualMemoryBlock::Map(u32 realaddr, u32 size)
{
assert(size);
Expects(size);
for (u32 addr = m_range_start; addr <= m_range_start + m_range_size - 1 - GetReservedAmount() - size;)
{
@ -48,7 +48,7 @@ u32 VirtualMemoryBlock::Map(u32 realaddr, u32 size)
bool VirtualMemoryBlock::Map(u32 realaddr, u32 size, u32 addr)
{
assert(size);
Expects(size);
if (!IsInMyRange(addr, size))
{

View File

@ -24,6 +24,8 @@
namespace vm
{
thread_local u64 g_tls_fault_count{};
template<std::size_t Size> struct mapped_ptr_deleter
{
void operator ()(void* ptr)
@ -93,7 +95,7 @@ namespace vm
class reservation_mutex_t
{
std::atomic<bool> m_lock{ false };
atomic_t<bool> m_lock{ false };
std::thread::id m_owner{};
std::condition_variable m_cv;
@ -164,7 +166,7 @@ namespace vm
std::mutex g_waiter_list_mutex;
waiter_t* _add_waiter(named_thread_t& thread, u32 addr, u32 size)
waiter_t* _add_waiter(named_thread& thread, u32 addr, u32 size)
{
std::lock_guard<std::mutex> lock(g_waiter_list_mutex);
@ -255,7 +257,7 @@ namespace vm
return true;
}
waiter_lock_t::waiter_lock_t(named_thread_t& thread, u32 addr, u32 size)
waiter_lock_t::waiter_lock_t(named_thread& thread, u32 addr, u32 size)
: m_waiter(_add_waiter(thread, addr, size))
, m_lock(thread.mutex, std::adopt_lock) // must be locked in _add_waiter
{
@ -312,6 +314,12 @@ namespace vm
}
}
access_violation::access_violation(u64 addr, const char* cause)
: std::runtime_error(fmt::exception("Access violation %s address 0x%llx", cause, addr))
{
g_tls_fault_count &= ~(1ull << 63);
}
void notify_at(u32 addr, u32 size)
{
const u64 align = 0x80000000ull >> cntlz32(size);
@ -353,7 +361,7 @@ namespace vm
void start()
{
// start notification thread
thread_ctrl::spawn(PURE_EXPR("vm::start thread"s), []()
thread_ctrl::spawn("vm::start thread", []()
{
while (!Emu.IsStopped())
{
@ -654,8 +662,8 @@ namespace vm
{
_reservation_break(i * 4096);
const u8 f1 = g_pages[i]._or(flags_set & ~flags_inv) & (page_writable | page_readable);
g_pages[i]._and_not(flags_clear & ~flags_inv);
const u8 f1 = g_pages[i].fetch_or(flags_set & ~flags_inv) & (page_writable | page_readable);
g_pages[i].fetch_and(~(flags_clear & ~flags_inv));
const u8 f2 = (g_pages[i] ^= flags_inv) & (page_writable | page_readable);
if (f1 != f2)
@ -1065,13 +1073,13 @@ namespace vm
u32 stack_push(u32 size, u32 align_v)
{
if (auto cpu = get_current_cpu_thread()) switch (cpu->get_type())
if (auto cpu = get_current_cpu_thread()) switch (cpu->type)
{
case CPU_THREAD_PPU:
case cpu_type::ppu:
{
PPUThread& context = static_cast<PPUThread&>(*cpu);
const u32 old_pos = VM_CAST(context.GPR[1]);
const u32 old_pos = vm::cast(context.GPR[1], HERE);
context.GPR[1] -= align(size + 4, 8); // room minimal possible size
context.GPR[1] &= ~(align_v - 1); // fix stack alignment
@ -1083,12 +1091,12 @@ namespace vm
{
const u32 addr = static_cast<u32>(context.GPR[1]);
vm::ps3::_ref<nse_t<u32>>(addr + size) = old_pos;
std::memset(vm::base(addr), 0, size);
return addr;
}
}
case CPU_THREAD_SPU:
case CPU_THREAD_RAW_SPU:
case cpu_type::spu:
{
SPUThread& context = static_cast<SPUThread&>(*cpu);
@ -1108,9 +1116,9 @@ namespace vm
}
}
case CPU_THREAD_ARMv7:
case cpu_type::arm:
{
ARMv7Context& context = static_cast<ARMv7Thread&>(*cpu);
ARMv7Thread& context = static_cast<ARMv7Thread&>(*cpu);
const u32 old_pos = context.SP;
context.SP -= align(size + 4, 4); // room minimal possible size
@ -1129,63 +1137,63 @@ namespace vm
default:
{
throw EXCEPTION("Invalid thread type (%d)", cpu->get_type());
throw EXCEPTION("Invalid thread type (%u)", cpu->type);
}
}
throw EXCEPTION("Invalid thread");
}
void stack_pop(u32 addr, u32 size)
void stack_pop_verbose(u32 addr, u32 size) noexcept
{
if (auto cpu = get_current_cpu_thread()) switch (cpu->get_type())
if (auto cpu = get_current_cpu_thread()) switch (cpu->type)
{
case CPU_THREAD_PPU:
case cpu_type::ppu:
{
PPUThread& context = static_cast<PPUThread&>(*cpu);
if (context.GPR[1] != addr)
{
throw EXCEPTION("Stack inconsistency (addr=0x%x, SP=0x%llx, size=0x%x)", addr, context.GPR[1], size);
LOG_ERROR(MEMORY, "Stack inconsistency (addr=0x%x, SP=0x%llx, size=0x%x)", addr, context.GPR[1], size);
return;
}
context.GPR[1] = vm::ps3::_ref<nse_t<u32>>(context.GPR[1] + size);
return;
}
case CPU_THREAD_SPU:
case CPU_THREAD_RAW_SPU:
case cpu_type::spu:
{
SPUThread& context = static_cast<SPUThread&>(*cpu);
if (context.gpr[1]._u32[3] + context.offset != addr)
{
throw EXCEPTION("Stack inconsistency (addr=0x%x, SP=LS:0x%05x, size=0x%x)", addr, context.gpr[1]._u32[3], size);
LOG_ERROR(MEMORY, "Stack inconsistency (addr=0x%x, SP=LS:0x%05x, size=0x%x)", addr, context.gpr[1]._u32[3], size);
return;
}
context.gpr[1]._u32[3] = vm::ps3::_ref<nse_t<u32>>(context.gpr[1]._u32[3] + context.offset + size);
return;
}
case CPU_THREAD_ARMv7:
case cpu_type::arm:
{
ARMv7Context& context = static_cast<ARMv7Thread&>(*cpu);
ARMv7Thread& context = static_cast<ARMv7Thread&>(*cpu);
if (context.SP != addr)
{
throw EXCEPTION("Stack inconsistency (addr=0x%x, SP=0x%x, size=0x%x)", addr, context.SP, size);
LOG_ERROR(MEMORY, "Stack inconsistency (addr=0x%x, SP=0x%x, size=0x%x)", addr, context.SP, size);
return;
}
context.SP = vm::psv::_ref<nse_t<u32>>(context.SP + size);
return;
}
default:
{
throw EXCEPTION("Invalid thread type (%d)", cpu->get_type());
}
}
}
throw EXCEPTION("Invalid thread");
[[noreturn]] void throw_access_violation(u64 addr, const char* cause)
{
throw access_violation(addr, cause);
}
}

View File

@ -1,6 +1,7 @@
#pragma once
#include "Utilities/Thread.h"
#include <map>
namespace vm
{
@ -30,28 +31,31 @@ namespace vm
page_allocated = (1 << 7),
};
struct access_violation : std::runtime_error
{
access_violation(u64 addr, const char* cause);
};
[[noreturn]] void throw_access_violation(u64 addr, const char* cause);
struct waiter_t
{
u32 addr = 0;
u32 mask = ~0;
named_thread_t* thread = nullptr;
named_thread* thread = nullptr;
std::function<bool()> pred;
waiter_t() = default;
waiter_t* reset(u32 addr, u32 size, named_thread_t& thread)
waiter_t* reset(u32 addr, u32 size, named_thread& thread)
{
this->addr = addr;
this->mask = ~(size - 1);
this->thread = &thread;
// must be null at this point
if (pred)
{
throw EXCEPTION("Unexpected");
}
Ensures(!pred);
return this;
}
@ -64,7 +68,7 @@ namespace vm
std::unique_lock<std::mutex> m_lock;
public:
waiter_lock_t(named_thread_t& thread, u32 addr, u32 size);
waiter_lock_t(named_thread& thread, u32 addr, u32 size);
waiter_t* operator ->() const
{
@ -77,7 +81,8 @@ namespace vm
};
// Wait until pred() returns true, addr must be aligned to size which must be a power of 2, pred() may be called by any thread
template<typename F, typename... Args> auto wait_op(named_thread_t& thread, u32 addr, u32 size, F pred, Args&&... args) -> decltype(static_cast<void>(pred(args...)))
template<typename F, typename... Args>
auto wait_op(named_thread& thread, u32 addr, u32 size, F pred, Args&&... args) -> decltype(static_cast<void>(pred(args...)))
{
// return immediately if condition passed (optimistic case)
if (pred(args...)) return;
@ -207,7 +212,7 @@ namespace vm
return res;
}
throw EXCEPTION("Not a virtual memory pointer (%p)", real_ptr);
throw fmt::exception("Not a virtual memory pointer (%p)", real_ptr);
}
// Convert pointer-to-member to a vm address compatible offset
@ -216,38 +221,43 @@ namespace vm
return static_cast<u32>(reinterpret_cast<std::uintptr_t>(&reinterpret_cast<char const volatile&>(reinterpret_cast<T*>(0ull)->*member_ptr)));
}
template<typename T> struct cast_ptr
template<typename T>
struct cast_impl
{
static_assert(std::is_same<T, u32>::value, "Unsupported VM_CAST() type");
static_assert(std::is_same<T, u32>::value, "vm::cast() error: unsupported type");
};
template<> struct cast_ptr<u32>
template<>
struct cast_impl<u32>
{
static u32 cast(const u32 addr, const char* file, int line, const char* func)
static u32 cast(const u32& addr, const char* loc)
{
return addr;
}
};
template<> struct cast_ptr<u64>
template<>
struct cast_impl<u64>
{
static u32 cast(const u64 addr, const char* file, int line, const char* func)
static u32 cast(const u64& addr, const char* loc)
{
return static_cast<u32>(addr) == addr ? static_cast<u32>(addr) : throw fmt::exception(file, line, func, "VM_CAST failed (addr=0x%llx)", addr);
return fmt::narrow<u32>("Memory address out of range: 0x%llx%s", addr, loc);
}
};
template<typename T, bool Se> struct cast_ptr<se_t<T, Se>>
template<typename T, bool Se>
struct cast_impl<se_t<T, Se>>
{
static u32 cast(const se_t<T, Se>& addr, const char* file, int line, const char* func)
static u32 cast(const se_t<T, Se>& addr, const char* loc)
{
return cast_ptr<T>::cast(addr, file, line, func);
return cast_impl<T>::cast(addr, loc);
}
};
template<typename T> u32 impl_cast(const T& addr, const char* file, int line, const char* func)
template<typename T>
u32 cast(const T& addr, const char* loc)
{
return cast_ptr<T>::cast(addr, file, line, func);
return cast_impl<T>::cast(addr, loc);
}
// Convert specified PS3/PSV virtual memory address to a pointer for common access
@ -392,12 +402,10 @@ namespace vm
}
void close();
}
#include "vm_ptr.h"
u32 stack_push(u32 size, u32 align_v);
void stack_pop_verbose(u32 addr, u32 size) noexcept;
namespace vm
{
class stack
{
u32 m_begin;
@ -418,21 +426,20 @@ namespace vm
u32 alloc_new_page()
{
assert(m_position + m_page_size < (int)m_size);
Expects(m_position + m_page_size < (int)m_size);
m_position += (int)m_page_size;
return m_begin + m_position;
}
u32 dealloc_new_page()
{
assert(m_position - m_page_size > 0);
Expects(m_position - m_page_size > 0);
m_position -= (int)m_page_size;
return m_begin + m_position;
}
};
u32 stack_push(u32 size, u32 align_v);
void stack_pop(u32 addr, u32 size);
extern thread_local u64 g_tls_fault_count;
}
#include "vm_var.h"

View File

@ -7,14 +7,12 @@ class ARMv7Thread;
namespace vm
{
// helper SFINAE type for vm::_ptr_base comparison operators (enables comparison between equal types and between any type and void*)
template<typename T1, typename T2, typename RT = void> using if_comparable_t = std::enable_if_t<
std::is_void<T1>::value ||
std::is_void<T2>::value ||
std::is_same<std::remove_cv_t<T1>, std::remove_cv_t<T2>>::value,
RT>;
// SFINAE helper type for vm::_ptr_base comparison operators (enables comparison between equal types and between any type and void*)
template<typename T1, typename T2, typename RT = void>
using if_comparable_t = std::enable_if_t<std::is_void<T1>::value || std::is_void<T2>::value || std::is_same<std::remove_cv_t<T1>, std::remove_cv_t<T2>>::value, RT>;
template<typename T, typename AT = u32> class _ptr_base
template<typename T, typename AT = u32>
class _ptr_base
{
AT m_addr;
@ -37,84 +35,85 @@ namespace vm
return m_addr;
}
// get vm pointer to a struct member
template<typename MT, typename T2, typename = if_comparable_t<T, T2>> _ptr_base<MT> ptr(MT T2::*const mptr) const
void set(addr_type addr)
{
return{ VM_CAST(m_addr) + get_offset(mptr), vm::addr };
this->m_addr = addr;
}
// get vm pointer to a struct member with array subscription
template<typename MT, typename T2, typename ET = std::remove_extent_t<MT>, typename = if_comparable_t<T, T2>> _ptr_base<ET> ptr(MT T2::*const mptr, u32 index) const
{
return{ VM_CAST(m_addr) + get_offset(mptr) + SIZE_32(ET) * index, vm::addr };
}
// get vm reference to a struct member
template<typename MT, typename T2, typename = if_comparable_t<T, T2>> _ref_base<MT> ref(MT T2::*const mptr) const
{
return{ VM_CAST(m_addr) + get_offset(mptr), vm::addr };
}
// get vm reference to a struct member with array subscription
template<typename MT, typename T2, typename ET = std::remove_extent_t<MT>, typename = if_comparable_t<T, T2>> _ref_base<ET> ref(MT T2::*const mptr, u32 index) const
{
return{ VM_CAST(m_addr) + get_offset(mptr) + SIZE_32(ET) * index, vm::addr };
}
// get vm reference
_ref_base<T, u32> ref() const
{
return{ VM_CAST(m_addr), vm::addr };
}
/*[[deprecated("Use constructor instead")]]*/ void set(addr_type value)
{
m_addr = value;
}
/*[[deprecated("Use constructor instead")]]*/ static _ptr_base make(addr_type addr)
static constexpr _ptr_base make(addr_type addr)
{
return{ addr, vm::addr };
}
// Enable only the conversions which are originally possible between pointer types
template<typename T2, typename AT2, typename = std::enable_if_t<std::is_convertible<T*, T2*>::value>>
operator _ptr_base<T2, AT2>() const
{
return{ vm::cast(m_addr, HERE), vm::addr };
}
explicit constexpr operator bool() const
{
return m_addr != 0;
}
// Get vm pointer to a struct member
template<typename MT, typename T2, typename = if_comparable_t<T, T2>>
_ptr_base<MT> ptr(MT T2::*const mptr) const
{
return{ vm::cast(m_addr, HERE) + get_offset(mptr), vm::addr };
}
// Get vm pointer to a struct member with array subscription
template<typename MT, typename T2, typename ET = std::remove_extent_t<MT>, typename = if_comparable_t<T, T2>>
_ptr_base<ET> ptr(MT T2::*const mptr, u32 index) const
{
return{ vm::cast(m_addr, HERE) + get_offset(mptr) + SIZE_32(ET) * index, vm::addr };
}
// Get vm reference to a struct member
template<typename MT, typename T2, typename = if_comparable_t<T, T2>>
_ref_base<MT> ref(MT T2::*const mptr) const
{
return{ vm::cast(m_addr, HERE) + get_offset(mptr), vm::addr };
}
// Get vm reference to a struct member with array subscription
template<typename MT, typename T2, typename ET = std::remove_extent_t<MT>, typename = if_comparable_t<T, T2>>
_ref_base<ET> ref(MT T2::*const mptr, u32 index) const
{
return{ vm::cast(m_addr, HERE) + get_offset(mptr) + SIZE_32(ET) * index, vm::addr };
}
// Get vm reference
_ref_base<T, u32> ref() const
{
return{ vm::cast(m_addr, HERE), vm::addr };
}
T* get_ptr() const
{
return static_cast<T*>(vm::base(VM_CAST(m_addr)));
return static_cast<T*>(vm::base(vm::cast(m_addr, HERE)));
}
T* get_ptr_priv() const
{
return static_cast<T*>(vm::base_priv(VM_CAST(m_addr)));
return static_cast<T*>(vm::base_priv(vm::cast(m_addr, HERE)));
}
T* operator ->() const
{
static_assert(!std::is_void<T>::value, "vm::_ptr_base<> error: operator-> is not available for void pointers");
return get_ptr();
}
std::add_lvalue_reference_t<T> operator *() const
{
return *static_cast<T*>(vm::base(vm::cast(m_addr, HERE)));
}
std::add_lvalue_reference_t<T> operator [](u32 index) const
{
static_assert(!std::is_void<T>::value, "vm::_ptr_base<> error: operator[] is not available for void pointers");
return *static_cast<T*>(vm::base(VM_CAST(m_addr) + SIZE_32(T) * index));
}
// enable only the conversions which are originally possible between pointer types
template<typename T2, typename AT2, typename = std::enable_if_t<std::is_convertible<T*, T2*>::value>> operator _ptr_base<T2, AT2>() const
{
return{ VM_CAST(m_addr), vm::addr };
}
//template<typename T2, typename = std::enable_if_t<std::is_convertible<T*, T2*>::value>> explicit operator T2*() const
//{
// return get_ptr();
//}
explicit operator bool() const
{
return m_addr != 0;
return *static_cast<T*>(vm::base(vm::cast(m_addr, HERE) + SIZE_32(T) * index));
}
// Test address for arbitrary alignment: (addr & (align - 1)) == 0
@ -126,8 +125,6 @@ namespace vm
// Test address alignment using alignof(T)
bool aligned() const
{
static_assert(!std::is_void<T>::value, "vm::_ptr_base<> error: aligned() is not available for void pointers");
return aligned(ALIGN_32(T));
}
@ -137,62 +134,74 @@ namespace vm
return !aligned(align);
}
// pointer increment (postfix)
_ptr_base<T, u32> operator +() const
{
return{ vm::cast(m_addr, HERE), vm::addr };
}
_ptr_base<T, u32> operator +(u32 count) const
{
return{ vm::cast(m_addr, HERE) + count * SIZE_32(T), vm::addr };
}
_ptr_base<T, u32> operator -(u32 count) const
{
return{ vm::cast(m_addr, HERE) - count * SIZE_32(T), vm::addr };
}
friend _ptr_base<T, u32> operator +(u32 count, const _ptr_base& ptr)
{
return{ vm::cast(ptr.m_addr, HERE) + count * SIZE_32(T), vm::addr };
}
// Pointer difference operator
template<typename T2, typename AT2>
std::enable_if_t<std::is_object<T2>::value && std::is_same<CV T, CV T2>::value, s32> operator -(const _ptr_base<T2, AT2>& right) const
{
return static_cast<s32>(vm::cast(m_addr, HERE) - vm::cast(right.m_addr, HERE)) / SIZE_32(T);
}
_ptr_base operator ++(int)
{
static_assert(!std::is_void<T>::value, "vm::_ptr_base<> error: operator++ is not available for void pointers");
const addr_type result = m_addr;
m_addr = VM_CAST(m_addr) + SIZE_32(T);
m_addr = vm::cast(m_addr, HERE) + SIZE_32(T);
return{ result, vm::addr };
}
// pointer increment (prefix)
_ptr_base& operator ++()
{
static_assert(!std::is_void<T>::value, "vm::_ptr_base<> error: operator++ is not available for void pointers");
m_addr = VM_CAST(m_addr) + SIZE_32(T);
m_addr = vm::cast(m_addr, HERE) + SIZE_32(T);
return *this;
}
// pointer decrement (postfix)
_ptr_base operator --(int)
{
static_assert(!std::is_void<T>::value, "vm::_ptr_base<> error: operator-- is not available for void pointers");
const addr_type result = m_addr;
m_addr = VM_CAST(m_addr) - SIZE_32(T);
m_addr = vm::cast(m_addr, HERE) - SIZE_32(T);
return{ result, vm::addr };
}
// pointer decrement (prefix)
_ptr_base& operator --()
{
static_assert(!std::is_void<T>::value, "vm::_ptr_base<> error: operator-- is not available for void pointers");
m_addr = VM_CAST(m_addr) - SIZE_32(T);
m_addr = vm::cast(m_addr, HERE) - SIZE_32(T);
return *this;
}
_ptr_base& operator +=(s32 count)
{
static_assert(!std::is_void<T>::value, "vm::_ptr_base<> error: operator+= is not available for void pointers");
m_addr = VM_CAST(m_addr) + count * SIZE_32(T);
m_addr = vm::cast(m_addr, HERE) + count * SIZE_32(T);
return *this;
}
_ptr_base& operator -=(s32 count)
{
static_assert(!std::is_void<T>::value, "vm::_ptr_base<> error: operator-= is not available for void pointers");
m_addr = VM_CAST(m_addr) - count * SIZE_32(T);
m_addr = vm::cast(m_addr, HERE) - count * SIZE_32(T);
return *this;
}
};
template<typename AT, typename RT, typename... T> class _ptr_base<RT(T...), AT>
template<typename AT, typename RT, typename... T>
class _ptr_base<RT(T...), AT>
{
AT m_addr;
@ -211,38 +220,43 @@ namespace vm
return m_addr;
}
/*[[deprecated("Use constructor instead")]]*/ void set(addr_type value)
void set(addr_type addr)
{
m_addr = value;
m_addr = addr;
}
/*[[deprecated("Use constructor instead")]]*/ static _ptr_base make(addr_type addr)
static constexpr _ptr_base make(addr_type addr)
{
return{ addr, vm::addr };
}
// defined in CB_FUNC.h, passing context is mandatory
RT operator()(PPUThread& CPU, T... args) const;
// defined in ARMv7Callback.h, passing context is mandatory
RT operator()(ARMv7Thread& context, T... args) const;
// conversion to another function pointer
template<typename AT2> operator _ptr_base<RT(T...), AT2>() const
// Conversion to another function pointer
template<typename AT2>
operator _ptr_base<RT(T...), AT2>() const
{
return{ VM_CAST(m_addr), vm::addr };
return{ vm::cast(m_addr, HERE), vm::addr };
}
explicit operator bool() const
explicit constexpr operator bool() const
{
return m_addr != 0;
}
_ptr_base<RT(T...), u32> operator +() const
{
return{ vm::cast(m_addr, HERE), vm::addr };
}
// Callback; defined in PPUCallback.h, passing context is mandatory
RT operator()(PPUThread& ppu, T... args) const;
// Callback; defined in ARMv7Callback.h, passing context is mandatory
RT operator()(ARMv7Thread& cpu, T... args) const;
};
template<typename AT, typename RT, typename... T> class _ptr_base<RT(*)(T...), AT>
template<typename AT, typename RT, typename... T>
class _ptr_base<RT(*)(T...), AT>
{
AT m_addr;
static_assert(!sizeof(AT), "vm::_ptr_base<> error: use RT(T...) format for functions instead of RT(*)(T...)");
};
@ -266,19 +280,19 @@ namespace vm
namespace ps3
{
// default pointer for PS3 HLE functions (Native endianness pointer to BE data)
// Default pointer type for PS3 HLE functions (Native endianness pointer to BE data)
template<typename T, typename AT = u32> using ptr = ptrb<T, AT>;
// default pointer to pointer for PS3 HLE functions (Native endianness pointer to BE pointer to BE data)
// Default pointer to pointer type for PS3 HLE functions (Native endianness pointer to BE pointer to BE data)
template<typename T, typename AT = u32, typename AT2 = u32> using pptr = ptr<ptr<T, AT2>, AT>;
// default pointer for PS3 HLE structures (BE pointer to BE data)
// Default pointer type for PS3 HLE structures (BE pointer to BE data)
template<typename T, typename AT = u32> using bptr = bptrb<T, AT>;
// default pointer to pointer for PS3 HLE structures (BE pointer to BE pointer to BE data)
// Default pointer to pointer type for PS3 HLE structures (BE pointer to BE pointer to BE data)
template<typename T, typename AT = u32, typename AT2 = u32> using bpptr = bptr<ptr<T, AT2>, AT>;
// native endianness pointer to const BE data
// Native endianness pointer to const BE data
template<typename T, typename AT = u32> using cptr = ptr<const T, AT>;
// BE pointer to const BE data
@ -287,34 +301,36 @@ namespace vm
template<typename T, typename AT = u32> using cpptr = pptr<const T, AT>;
template<typename T, typename AT = u32> using bcpptr = bpptr<const T, AT>;
// perform static_cast (for example, vm::ptr<void> to vm::ptr<char>)
template<typename CT, typename T, typename AT, typename = decltype(static_cast<to_be_t<CT>*>(std::declval<T*>()))> inline _ptr_base<to_be_t<CT>> static_ptr_cast(const _ptr_base<T, AT>& other)
// Perform static_cast (for example, vm::ptr<void> to vm::ptr<char>)
template<typename CT, typename T, typename AT, typename = decltype(static_cast<to_be_t<CT>*>(std::declval<T*>()))>
inline _ptr_base<to_be_t<CT>> static_ptr_cast(const _ptr_base<T, AT>& other)
{
return{ VM_CAST(other.addr()), vm::addr };
return{ vm::cast(other.addr(), HERE), vm::addr };
}
// perform const_cast (for example, vm::cptr<char> to vm::ptr<char>)
template<typename CT, typename T, typename AT, typename = decltype(const_cast<to_be_t<CT>*>(std::declval<T*>()))> inline _ptr_base<to_be_t<CT>> const_ptr_cast(const _ptr_base<T, AT>& other)
// Perform const_cast (for example, vm::cptr<char> to vm::ptr<char>)
template<typename CT, typename T, typename AT, typename = decltype(const_cast<to_be_t<CT>*>(std::declval<T*>()))>
inline _ptr_base<to_be_t<CT>> const_ptr_cast(const _ptr_base<T, AT>& other)
{
return{ VM_CAST(other.addr()), vm::addr };
return{ vm::cast(other.addr(), HERE), vm::addr };
}
}
namespace psv
{
// default pointer for PSV HLE functions (Native endianness pointer to LE data)
// Default pointer type for PSV HLE functions (Native endianness pointer to LE data)
template<typename T> using ptr = ptrl<T>;
// default pointer to pointer for PSV HLE functions (Native endianness pointer to LE pointer to LE data)
// Default pointer to pointer type for PSV HLE functions (Native endianness pointer to LE pointer to LE data)
template<typename T> using pptr = ptr<ptr<T>>;
// default pointer for PSV HLE structures (LE pointer to LE data)
// Default pointer type for PSV HLE structures (LE pointer to LE data)
template<typename T> using lptr = lptrl<T>;
// default pointer to pointer for PSV HLE structures (LE pointer to LE pointer to LE data)
// Default pointer to pointer type for PSV HLE structures (LE pointer to LE pointer to LE data)
template<typename T> using lpptr = lptr<ptr<T>>;
// native endianness pointer to const LE data
// Native endianness pointer to const LE data
template<typename T> using cptr = ptr<const T>;
// LE pointer to const LE data
@ -323,198 +339,168 @@ namespace vm
template<typename T> using cpptr = pptr<const T>;
template<typename T> using lcpptr = lpptr<const T>;
// perform static_cast (for example, vm::ptr<void> to vm::ptr<char>)
template<typename CT, typename T, typename AT, typename = decltype(static_cast<to_le_t<CT>*>(std::declval<T*>()))> inline _ptr_base<to_le_t<CT>> static_ptr_cast(const _ptr_base<T, AT>& other)
// Perform static_cast (for example, vm::ptr<void> to vm::ptr<char>)
template<typename CT, typename T, typename AT, typename = decltype(static_cast<to_le_t<CT>*>(std::declval<T*>()))>
inline _ptr_base<to_le_t<CT>> static_ptr_cast(const _ptr_base<T, AT>& other)
{
return{ VM_CAST(other.addr()), vm::addr };
return{ vm::cast(other.addr(), HERE), vm::addr };
}
// perform const_cast (for example, vm::cptr<char> to vm::ptr<char>)
template<typename CT, typename T, typename AT, typename = decltype(const_cast<to_le_t<CT>*>(std::declval<T*>()))> inline _ptr_base<to_le_t<CT>> const_ptr_cast(const _ptr_base<T, AT>& other)
// Perform const_cast (for example, vm::cptr<char> to vm::ptr<char>)
template<typename CT, typename T, typename AT, typename = decltype(const_cast<to_le_t<CT>*>(std::declval<T*>()))>
inline _ptr_base<to_le_t<CT>> const_ptr_cast(const _ptr_base<T, AT>& other)
{
return{ VM_CAST(other.addr()), vm::addr };
return{ vm::cast(other.addr(), HERE), vm::addr };
}
}
struct null_t
{
template<typename T, typename AT> operator _ptr_base<T, AT>() const
template<typename T, typename AT>
constexpr operator _ptr_base<T, AT>() const
{
return{ 0, vm::addr };
return _ptr_base<T, AT>{ 0, vm::addr };
}
template<typename T, typename AT>
friend constexpr bool operator ==(const null_t&, const _ptr_base<T, AT>& ptr)
{
return !ptr;
}
template<typename T, typename AT>
friend constexpr bool operator ==(const _ptr_base<T, AT>& ptr, const null_t&)
{
return !ptr;
}
template<typename T, typename AT>
friend constexpr bool operator !=(const null_t&, const _ptr_base<T, AT>& ptr)
{
return ptr.operator bool();
}
template<typename T, typename AT>
friend constexpr bool operator !=(const _ptr_base<T, AT>& ptr, const null_t&)
{
return ptr.operator bool();
}
template<typename T, typename AT>
friend constexpr bool operator <(const null_t&, const _ptr_base<T, AT>& ptr)
{
return ptr.operator bool();
}
template<typename T, typename AT>
friend constexpr bool operator <(const _ptr_base<T, AT>&, const null_t&)
{
return false;
}
template<typename T, typename AT>
friend constexpr bool operator <=(const null_t&, const _ptr_base<T, AT>&)
{
return true;
}
template<typename T, typename AT>
friend constexpr bool operator <=(const _ptr_base<T, AT>& ptr, const null_t&)
{
return !ptr.operator bool();
}
template<typename T, typename AT>
friend constexpr bool operator >(const null_t&, const _ptr_base<T, AT>&)
{
return false;
}
template<typename T, typename AT>
friend constexpr bool operator >(const _ptr_base<T, AT>& ptr, const null_t&)
{
return ptr.operator bool();
}
template<typename T, typename AT>
friend constexpr bool operator >=(const null_t&, const _ptr_base<T, AT>& ptr)
{
return !ptr;
}
template<typename T, typename AT>
friend constexpr bool operator >=(const _ptr_base<T, AT>&, const null_t&)
{
return true;
}
};
// vm::null is convertible to any vm::ptr type as null pointer in virtual memory
// Null pointer convertible to any vm::ptr* type
static null_t null;
// Call wait_op() for specified vm pointer
template<typename T, typename AT, typename F, typename... Args> inline auto wait_op(named_thread_t& thread, const _ptr_base<T, AT>& ptr, F pred, Args&&... args) -> decltype(static_cast<void>(pred(args...)))
template<typename T, typename AT, typename F, typename... Args>
static inline auto wait_op(named_thread& thread, const _ptr_base<T, AT>& ptr, F pred, Args&&... args) -> decltype(static_cast<void>(pred(args...)))
{
return wait_op(thread, ptr.addr(), SIZE_32(T), std::move(pred), std::forward<Args>(args)...);
}
// Call notify_at() for specified vm pointer
template<typename T, typename AT> inline void notify_at(const vm::_ptr_base<T, AT>& ptr)
template<typename T, typename AT>
inline void notify_at(const vm::_ptr_base<T, AT>& ptr)
{
return notify_at(ptr.addr(), SIZE_32(T));
}
}
// unary plus operator for vm::_ptr_base (always available)
template<typename T, typename AT> inline vm::_ptr_base<T> operator +(const vm::_ptr_base<T, AT>& ptr)
{
return ptr;
}
// indirection operator for vm::_ptr_base
template<typename T, typename AT> inline std::enable_if_t<std::is_object<T>::value, T&> operator *(const vm::_ptr_base<T, AT>& ptr)
{
return *ptr.get_ptr();
}
// addition operator for vm::_ptr_base (pointer + integer)
template<typename T, typename AT> inline std::enable_if_t<std::is_object<T>::value, vm::_ptr_base<T>> operator +(const vm::_ptr_base<T, AT>& ptr, u32 count)
{
return{ VM_CAST(ptr.addr()) + count * SIZE_32(T), vm::addr };
}
// addition operator for vm::_ptr_base (integer + pointer)
template<typename T, typename AT> inline std::enable_if_t<std::is_object<T>::value, vm::_ptr_base<T>> operator +(u32 count, const vm::_ptr_base<T, AT>& ptr)
{
return{ VM_CAST(ptr.addr()) + count * SIZE_32(T), vm::addr };
}
// subtraction operator for vm::_ptr_base (pointer - integer)
template<typename T, typename AT> inline std::enable_if_t<std::is_object<T>::value, vm::_ptr_base<T>> operator -(const vm::_ptr_base<T, AT>& ptr, u32 count)
{
return{ VM_CAST(ptr.addr()) - count * SIZE_32(T), vm::addr };
}
// pointer difference operator for vm::_ptr_base
template<typename T1, typename AT1, typename T2, typename AT2> inline std::enable_if_t<
std::is_object<T1>::value &&
std::is_object<T2>::value &&
std::is_same<std::remove_cv_t<T1>, std::remove_cv_t<T2>>::value,
s32> operator -(const vm::_ptr_base<T1, AT1>& left, const vm::_ptr_base<T2, AT2>& right)
{
return static_cast<s32>(VM_CAST(left.addr()) - VM_CAST(right.addr())) / SIZE_32(T1);
}
// comparison operator for vm::_ptr_base (pointer1 == pointer2)
template<typename T1, typename AT1, typename T2, typename AT2> inline vm::if_comparable_t<T1, T2, bool> operator ==(const vm::_ptr_base<T1, AT1>& left, const vm::_ptr_base<T2, AT2>& right)
template<typename T1, typename AT1, typename T2, typename AT2>
inline vm::if_comparable_t<T1, T2, bool> operator ==(const vm::_ptr_base<T1, AT1>& left, const vm::_ptr_base<T2, AT2>& right)
{
return left.addr() == right.addr();
}
template<typename T, typename AT> inline bool operator ==(const vm::null_t&, const vm::_ptr_base<T, AT>& ptr)
{
return !ptr.operator bool();
}
template<typename T, typename AT> inline bool operator ==(const vm::_ptr_base<T, AT>& ptr, const vm::null_t&)
{
return !ptr.operator bool();
}
// comparison operator for vm::_ptr_base (pointer1 != pointer2)
template<typename T1, typename AT1, typename T2, typename AT2> inline vm::if_comparable_t<T1, T2, bool> operator !=(const vm::_ptr_base<T1, AT1>& left, const vm::_ptr_base<T2, AT2>& right)
template<typename T1, typename AT1, typename T2, typename AT2>
inline vm::if_comparable_t<T1, T2, bool> operator !=(const vm::_ptr_base<T1, AT1>& left, const vm::_ptr_base<T2, AT2>& right)
{
return left.addr() != right.addr();
}
template<typename T, typename AT> inline bool operator !=(const vm::null_t&, const vm::_ptr_base<T, AT>& ptr)
{
return ptr.operator bool();
}
template<typename T, typename AT> inline bool operator !=(const vm::_ptr_base<T, AT>& ptr, const vm::null_t&)
{
return ptr.operator bool();
}
// comparison operator for vm::_ptr_base (pointer1 < pointer2)
template<typename T1, typename AT1, typename T2, typename AT2> inline vm::if_comparable_t<T1, T2, bool> operator <(const vm::_ptr_base<T1, AT1>& left, const vm::_ptr_base<T2, AT2>& right)
template<typename T1, typename AT1, typename T2, typename AT2>
inline vm::if_comparable_t<T1, T2, bool> operator <(const vm::_ptr_base<T1, AT1>& left, const vm::_ptr_base<T2, AT2>& right)
{
return left.addr() < right.addr();
}
template<typename T, typename AT> inline bool operator <(const vm::null_t&, const vm::_ptr_base<T, AT>& ptr)
{
return ptr.operator bool();
}
template<typename T, typename AT> inline bool operator <(const vm::_ptr_base<T, AT>&, const vm::null_t&)
{
return false;
}
// comparison operator for vm::_ptr_base (pointer1 <= pointer2)
template<typename T1, typename AT1, typename T2, typename AT2> inline vm::if_comparable_t<T1, T2, bool> operator <=(const vm::_ptr_base<T1, AT1>& left, const vm::_ptr_base<T2, AT2>& right)
template<typename T1, typename AT1, typename T2, typename AT2>
inline vm::if_comparable_t<T1, T2, bool> operator <=(const vm::_ptr_base<T1, AT1>& left, const vm::_ptr_base<T2, AT2>& right)
{
return left.addr() <= right.addr();
}
template<typename T, typename AT> inline bool operator <=(const vm::null_t&, const vm::_ptr_base<T, AT>&)
{
return true;
}
template<typename T, typename AT> inline bool operator <=(const vm::_ptr_base<T, AT>& ptr, const vm::null_t&)
{
return !ptr.operator bool();
}
// comparison operator for vm::_ptr_base (pointer1 > pointer2)
template<typename T1, typename AT1, typename T2, typename AT2> inline vm::if_comparable_t<T1, T2, bool> operator >(const vm::_ptr_base<T1, AT1>& left, const vm::_ptr_base<T2, AT2>& right)
template<typename T1, typename AT1, typename T2, typename AT2>
inline vm::if_comparable_t<T1, T2, bool> operator >(const vm::_ptr_base<T1, AT1>& left, const vm::_ptr_base<T2, AT2>& right)
{
return left.addr() > right.addr();
}
template<typename T, typename AT> inline bool operator >(const vm::null_t&, const vm::_ptr_base<T, AT>&)
{
return false;
}
template<typename T, typename AT> inline bool operator >(const vm::_ptr_base<T, AT>& ptr, const vm::null_t&)
{
return ptr.operator bool();
}
// comparison operator for vm::_ptr_base (pointer1 >= pointer2)
template<typename T1, typename AT1, typename T2, typename AT2> inline vm::if_comparable_t<T1, T2, bool> operator >=(const vm::_ptr_base<T1, AT1>& left, const vm::_ptr_base<T2, AT2>& right)
template<typename T1, typename AT1, typename T2, typename AT2>
inline vm::if_comparable_t<T1, T2, bool> operator >=(const vm::_ptr_base<T1, AT1>& left, const vm::_ptr_base<T2, AT2>& right)
{
return left.addr() >= right.addr();
}
template<typename T, typename AT> inline bool operator >=(const vm::null_t&, const vm::_ptr_base<T, AT>& ptr)
{
return !ptr.operator bool();
}
template<typename T, typename AT> inline bool operator >=(const vm::_ptr_base<T, AT>&, const vm::null_t&)
{
return true;
}
// external specialization for to_se<> (change AT endianness to BE/LE)
template<typename T, typename AT, bool Se> struct to_se<vm::_ptr_base<T, AT>, Se>
// Change AT endianness to BE/LE
template<typename T, typename AT, bool Se>
struct to_se<vm::_ptr_base<T, AT>, Se>
{
using type = vm::_ptr_base<T, typename to_se<AT, Se>::type>;
};
// external specialization for to_ne<> (change AT endianness to native)
template<typename T, typename AT> struct to_ne<vm::_ptr_base<T, AT>>
{
using type = vm::_ptr_base<T, typename to_ne<AT>::type>;
};
namespace fmt
{
// external specialization for fmt::format function
template<typename T, typename AT> struct unveil<vm::_ptr_base<T, AT>, false>
// Format pointer
template<typename T, typename AT>
struct unveil<vm::_ptr_base<T, AT>, void>
{
using result_type = typename unveil<AT>::result_type;
@ -524,37 +510,3 @@ namespace fmt
}
};
}
// external specializations for PPU GPR (SC_FUNC.h, CB_FUNC.h)
template<typename T, bool is_enum> struct cast_ppu_gpr;
template<typename T, typename AT> struct cast_ppu_gpr<vm::_ptr_base<T, AT>, false>
{
static inline u64 to_gpr(const vm::_ptr_base<T, AT>& value)
{
return cast_ppu_gpr<AT, std::is_enum<AT>::value>::to_gpr(value.addr());
}
static inline vm::_ptr_base<T, AT> from_gpr(const u64 reg)
{
return{ cast_ppu_gpr<AT, std::is_enum<AT>::value>::from_gpr(reg), vm::addr };
}
};
// external specializations for ARMv7 GPR
template<typename T, bool is_enum> struct cast_armv7_gpr;
template<typename T, typename AT> struct cast_armv7_gpr<vm::_ptr_base<T, AT>, false>
{
static inline u32 to_gpr(const vm::_ptr_base<T, AT>& value)
{
return cast_armv7_gpr<AT, std::is_enum<AT>::value>::to_gpr(value.addr());
}
static inline vm::_ptr_base<T, AT> from_gpr(const u32 reg)
{
return{ cast_armv7_gpr<AT, std::is_enum<AT>::value>::from_gpr(reg), vm::addr };
}
};

View File

@ -5,9 +5,11 @@ namespace vm
// Tag which allows to construct vm objects from the address value
static struct addr_tag_t {} constexpr addr{};
template<typename T, typename AT> class _ptr_base;
template<typename T, typename AT>
class _ptr_base;
template<typename T, typename AT = u32> class _ref_base
template<typename T, typename AT = u32>
class _ref_base
{
AT m_addr;
@ -36,16 +38,16 @@ namespace vm
T& get_ref() const
{
return *static_cast<T*>(vm::base(VM_CAST(m_addr)));
return *static_cast<T*>(vm::base(vm::cast(m_addr, HERE)));
}
// convert to vm pointer
vm::_ptr_base<T, u32> ptr() const
{
return{ VM_CAST(m_addr), vm::addr };
return{ vm::cast(m_addr, HERE), vm::addr };
}
operator to_ne_t<T>() const
operator simple_t<T>() const
{
return get_ref();
}
@ -60,7 +62,7 @@ namespace vm
return get_ref() = right.get_ref();
}
T& operator =(const T& right) const
T& operator =(const simple_t<T>& right) const
{
return get_ref() = right;
}
@ -85,52 +87,62 @@ namespace vm
return --get_ref();
}
template<typename T2> decltype(auto) operator +=(const T2& right)
template<typename T2>
decltype(auto) operator +=(const T2& right)
{
return get_ref() += right;
}
template<typename T2> decltype(auto) operator -=(const T2& right)
template<typename T2>
decltype(auto) operator -=(const T2& right)
{
return get_ref() -= right;
}
template<typename T2> decltype(auto) operator *=(const T2& right)
template<typename T2>
decltype(auto) operator *=(const T2& right)
{
return get_ref() *= right;
}
template<typename T2> decltype(auto) operator /=(const T2& right)
template<typename T2>
decltype(auto) operator /=(const T2& right)
{
return get_ref() /= right;
}
template<typename T2> decltype(auto) operator %=(const T2& right)
template<typename T2>
decltype(auto) operator %=(const T2& right)
{
return get_ref() %= right;
}
template<typename T2> decltype(auto) operator &=(const T2& right)
template<typename T2>
decltype(auto) operator &=(const T2& right)
{
return get_ref() &= right;
}
template<typename T2> decltype(auto) operator |=(const T2& right)
template<typename T2>
decltype(auto) operator |=(const T2& right)
{
return get_ref() |= right;
}
template<typename T2> decltype(auto) operator ^=(const T2& right)
template<typename T2>
decltype(auto) operator ^=(const T2& right)
{
return get_ref() ^= right;
}
template<typename T2> decltype(auto) operator <<=(const T2& right)
template<typename T2>
decltype(auto) operator <<=(const T2& right)
{
return get_ref() <<= right;
}
template<typename T2> decltype(auto) operator >>=(const T2& right)
template<typename T2>
decltype(auto) operator >>=(const T2& right)
{
return get_ref() >>= right;
}
@ -173,65 +185,19 @@ namespace vm
}
}
// external specialization for to_se<> (change AT's endianness to BE/LE)
template<typename T, typename AT, bool Se> struct to_se<vm::_ref_base<T, AT>, Se>
// Change AT endianness to BE/LE
template<typename T, typename AT, bool Se>
struct to_se<vm::_ref_base<T, AT>, Se>
{
using type = vm::_ref_base<T, typename to_se<AT, Se>::type>;
};
// external specialization for to_ne<> (change AT's endianness to native)
template<typename T, typename AT> struct to_ne<vm::_ref_base<T, AT>>
{
using type = vm::_ref_base<T, typename to_ne<AT>::type>;
};
namespace fmt
{
// external specialization for fmt::format function
template<typename T, typename AT> struct unveil<vm::_ref_base<T, AT>, false>
// Forbid formatting
template<typename T, typename AT>
struct unveil<vm::_ref_base<T, AT>, void>
{
using result_type = typename unveil<AT>::result_type;
static inline result_type get_value(const vm::_ref_base<T, AT>& arg)
{
return unveil<AT>::get_value(arg.addr());
}
static_assert(!sizeof(T), "vm::_ref_base<>: ambiguous format argument");
};
}
// external specializations for PPU GPR (SC_FUNC.h, CB_FUNC.h)
template<typename T, bool is_enum> struct cast_ppu_gpr;
template<typename T, typename AT> struct cast_ppu_gpr<vm::_ref_base<T, AT>, false>
{
static inline u64 to_gpr(const vm::_ref_base<T, AT>& value)
{
return cast_ppu_gpr<AT, std::is_enum<AT>::value>::to_gpr(value.addr());
}
static inline vm::_ref_base<T, AT> from_gpr(const u64 reg)
{
return{ cast_ppu_gpr<AT, std::is_enum<AT>::value>::from_gpr(reg), vm::addr };
}
};
// external specializations for ARMv7 GPR
template<typename T, bool is_enum> struct cast_armv7_gpr;
template<typename T, typename AT> struct cast_armv7_gpr<vm::_ref_base<T, AT>, false>
{
static inline u32 to_gpr(const vm::_ref_base<T, AT>& value)
{
return cast_armv7_gpr<AT, std::is_enum<AT>::value>::to_gpr(value.addr());
}
static inline vm::_ref_base<T, AT> from_gpr(const u32 reg)
{
return{ cast_armv7_gpr<AT, std::is_enum<AT>::value>::from_gpr(reg), vm::addr };
}
};

View File

@ -1,268 +1,150 @@
#pragma once
#include "vm_ptr.h"
namespace vm
{
template<memory_location_t Location = vm::main> class page_alloc_t
template<memory_location_t Location = vm::main>
struct page_allocator
{
u32 m_addr;
public:
static inline u32 alloc(u32 size, u32 align)
{
return vm::alloc(size, Location, std::max<u32>(align, 4096));
}
static inline void dealloc(u32 addr, u32 size) noexcept
static inline void dealloc(u32 addr, u32 size = 0) noexcept
{
return vm::dealloc_verbose_nothrow(addr, Location);
}
page_alloc_t()
: m_addr(0)
{
}
page_alloc_t(u32 size, u32 align)
: m_addr(alloc(size, align))
{
}
page_alloc_t(page_alloc_t&& other)
: m_addr(other.m_addr)
{
other.m_addr = 0;
}
~page_alloc_t()
{
if (m_addr)
{
dealloc(m_addr, 0);
}
}
page_alloc_t& operator =(page_alloc_t&& other)
{
std::swap(m_addr, other.m_addr);
return *this;
}
u32 get_addr() const
{
return m_addr;
}
};
class stack_alloc_t
struct stack_allocator
{
u32 m_addr;
u32 m_size;
public:
static inline u32 alloc(u32 size, u32 align)
{
return vm::stack_push(size, align);
}
static inline void dealloc(u32 addr, u32 size)
static inline void dealloc(u32 addr, u32 size) noexcept
{
if (!std::uncaught_exception()) // Don't call during stack unwinding
{
vm::stack_pop(addr, size);
}
}
stack_alloc_t(u32 size, u32 align)
: m_addr(alloc(size, align))
, m_size(size)
{
}
~stack_alloc_t() noexcept(false) // Allow exceptions
{
dealloc(m_addr, m_size);
}
stack_alloc_t(const stack_alloc_t&) = delete; // Delete copy/move constructors and copy/move operators
u32 get_addr() const
{
return m_addr;
vm::stack_pop_verbose(addr, size);
}
};
// _var_base prototype (T - data type, A - allocation traits)
template<typename T, typename A> class _var_base;
// _var_base general specialization (single object of type T)
template<typename T, typename A> class _var_base final : public _ptr_base<T, const u32>
// Variable general specialization
template<typename T, typename A>
class _var_base final : public _ptr_base<T, const u32>
{
using pointer = _ptr_base<T, const u32>;
public:
// Call the constructor with specified arguments
template<typename... Args, typename = std::enable_if_t<std::is_constructible<T, Args...>::value>> _var_base(Args&&... args)
_var_base()
: pointer(A::alloc(SIZE_32(T), ALIGN_32(T)), vm::addr)
{
#include "restore_new.h"
new(pointer::get_ptr()) T(std::forward<Args>(args)...);
#include "define_new_memleakdetect.h"
}
_var_base(const _var_base&) = delete; // Delete copy/move constructors and copy/move operators
~_var_base() noexcept(noexcept(std::declval<T&>().~T()) && noexcept(A::dealloc(0, 0)))
_var_base(const T& right)
: _var_base()
{
// Call the destructor
pointer::get_ptr()->~T();
// Deallocate memory
A::dealloc(pointer::addr(), SIZE_32(T));
std::memcpy(pointer::get_ptr(), &right, sizeof(T));
}
// Remove operator []
std::add_lvalue_reference_t<T> operator [](u32 index) const = delete;
_var_base(_var_base&& right)
: pointer(right)
{
reinterpret_cast<u32&>(static_cast<pointer&>(right)) = 0;
}
~_var_base()
{
if (pointer::addr()) A::dealloc(pointer::addr(), SIZE_32(T));
}
};
// _var_base unknown length array specialization
template<typename T, typename A> class _var_base<T[], A> final : public _ptr_base<T, const u32>
// Dynamic length array variable
template<typename T, typename A>
class _var_base<T[], A> final : public _ptr_base<T, const u32>
{
using pointer = _ptr_base<T, const u32>;
u32 m_count;
u32 m_size;
public:
// Call the default constructor for each element
_var_base(u32 count)
: pointer(A::alloc(SIZE_32(T) * count, ALIGN_32(T)), vm::addr)
, m_count(count)
, m_size(SIZE_32(T) * count)
{
#include "restore_new.h"
new(pointer::get_ptr()) T[count]();
#include "define_new_memleakdetect.h"
}
// Call the constructor for each element using [it, it + count)
template<typename T2> _var_base(u32 count, T2 it)
: pointer(A::alloc(SIZE_32(T) * count, ALIGN_32(T)), vm::addr)
, m_count(count)
_var_base(_var_base&& right)
: pointer(right)
{
#include "restore_new.h"
for (u32 i = 0; i < m_count; i++, it++) new(pointer::get_ptr() + i) T(*it);
#include "define_new_memleakdetect.h"
reinterpret_cast<u32&>(static_cast<pointer&>(right)) = 0;
}
_var_base(const _var_base&) = delete; // Delete copy/move constructors and copy/move operators
~_var_base() noexcept(noexcept(std::declval<T&>().~T()) && noexcept(A::dealloc(0, 0)))
~_var_base()
{
// Call the destructor for each element
for (u32 i = m_count - 1; ~i; i--) pointer::operator [](i).~T();
// Deallocate memory
A::dealloc(pointer::addr(), SIZE_32(T) * m_count);
if (pointer::addr()) A::dealloc(pointer::addr(), m_size);
}
// Remove operator ->
T* operator ->() const = delete;
u32 get_count() const
{
return m_count;
return m_size / SIZE_32(T);
}
std::add_lvalue_reference_t<T> at(u32 index) const
{
if (index >= m_count) throw EXCEPTION("Out of range (0x%x >= 0x%x)", index, m_count);
return pointer::operator [](index);
}
// Remove operator ->
T* operator ->() const = delete;
};
// _var_base fixed length array specialization
template<typename T, typename A, u32 N> class _var_base<T[N], A> final : public _ptr_base<T, const u32>
{
using pointer = _ptr_base<T, const u32>;
public:
// Call the default constructor for each element
_var_base()
: pointer(A::alloc(SIZE_32(T) * N, ALIGN_32(T)), vm::addr)
{
#include "restore_new.h"
new(pointer::get_ptr()) T[N]();
#include "define_new_memleakdetect.h"
}
// Call the constructor for each element using array
template<typename T2> _var_base(const T2(&array)[N])
: pointer(A::alloc(SIZE_32(T) * N, ALIGN_32(T)), vm::addr)
{
#include "restore_new.h"
for (u32 i = 0; i < N; i++) new(pointer::get_ptr() + i) T(array[i]);
#include "define_new_memleakdetect.h"
}
_var_base(const _var_base&) = delete; // Delete copy/move constructors and copy/move operators
~_var_base() noexcept(noexcept(std::declval<T&>().~T()) && noexcept(A::dealloc(0, 0)))
{
// Call the destructor for each element
for (u32 i = N - 1; ~i; i--) pointer::operator [](i).~T();
// Deallocate memory
A::dealloc(pointer::addr(), SIZE_32(T) * N);
}
constexpr u32 get_count() const
{
return N;
}
std::add_lvalue_reference_t<T> at(u32 index) const
{
if (index >= N) throw EXCEPTION("Out of range (0x%x > 0x%x)", index, N);
return pointer::operator [](index);
}
// Remove operator ->
T* operator ->() const = delete;
};
// LE variable
template<typename T, typename A = vm::stack_alloc_t> using varl = _var_base<to_le_t<T>, A>;
template<typename T, typename A = stack_allocator> using varl = _var_base<to_le_t<T>, A>;
// BE variable
template<typename T, typename A = vm::stack_alloc_t> using varb = _var_base<to_be_t<T>, A>;
template<typename T, typename A = stack_allocator> using varb = _var_base<to_be_t<T>, A>;
namespace ps3
{
// BE variable
template<typename T, typename A = vm::stack_alloc_t> using var = varb<T, A>;
template<typename T, typename A = stack_allocator> using var = varb<T, A>;
// BE variable initialized from value
template<typename T> inline varb<T, vm::stack_alloc_t> make_var(const T& value)
// Make BE variable initialized from value
template<typename T> inline auto make_var(const T& value)
{
return{ value };
varb<T, stack_allocator> var(value);
return var;
}
// Global HLE variable
template<typename T>
struct gvar : ptr<T>
{
};
}
namespace psv
{
// LE variable
template<typename T, typename A = vm::stack_alloc_t> using var = varl<T, A>;
template<typename T, typename A = stack_allocator> using var = varl<T, A>;
// LE variable initialized from value
template<typename T> inline varl<T, vm::stack_alloc_t> make_var(const T& value)
// Make LE variable initialized from value
template<typename T> inline auto make_var(const T& value)
{
return{ value };
varl<T, stack_allocator> var(value);
return var;
}
// Global HLE variable
template<typename T>
struct gvar : ptr<T>
{
};
}
static _var_base<char[], vm::stack_alloc_t> make_str(const std::string& str)
// Make char[] variable initialized from std::string
static auto make_str(const std::string& str)
{
return{ size32(str) + 1, str.data() };
_var_base<char[], stack_allocator> var(size32(str) + 1);
std::memcpy(var.get_ptr(), str.c_str(), str.size() + 1);
return var;
}
}