rpcsx/rpcs3/Emu/Memory/vm_var.h

163 lines
3.4 KiB
C
Raw Normal View History

#pragma once
2016-02-01 22:52:49 +01:00
#include "vm_ptr.h"
namespace vm
{
2016-02-01 22:52:49 +01:00
template<memory_location_t Location = vm::main>
struct page_allocator
{
2016-05-13 16:01:48 +02:00
static inline vm::addr_t alloc(u32 size, u32 align)
{
2016-05-13 16:01:48 +02:00
return vm::cast(vm::alloc(size, Location, std::max<u32>(align, 4096)));
}
2016-02-01 22:52:49 +01:00
static inline void dealloc(u32 addr, u32 size = 0) noexcept
{
return vm::dealloc_verbose_nothrow(addr, Location);
}
};
2015-08-13 15:28:42 +02:00
template<typename T>
2016-02-01 22:52:49 +01:00
struct stack_allocator
{
2016-05-13 16:01:48 +02:00
static inline vm::addr_t alloc(u32 size, u32 align)
{
return vm::cast(T::stack_push(size, align));
}
2016-02-01 22:52:49 +01:00
static inline void dealloc(u32 addr, u32 size) noexcept
{
T::stack_pop_verbose(addr, size);
}
};
2016-02-01 22:52:49 +01:00
// 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:
2016-02-01 22:52:49 +01:00
_var_base()
2016-05-13 16:01:48 +02:00
: pointer(A::alloc(SIZE_32(T), ALIGN_32(T)))
{
}
2016-02-01 22:52:49 +01:00
_var_base(const T& right)
: _var_base()
{
2016-02-01 22:52:49 +01:00
std::memcpy(pointer::get_ptr(), &right, sizeof(T));
}
2016-02-01 22:52:49 +01:00
_var_base(_var_base&& right)
: pointer(right)
{
reinterpret_cast<u32&>(static_cast<pointer&>(right)) = 0;
}
2016-02-01 22:52:49 +01:00
~_var_base()
{
if (pointer::addr()) A::dealloc(pointer::addr(), SIZE_32(T));
}
};
2016-02-01 22:52:49 +01:00
// 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>;
2016-02-01 22:52:49 +01:00
u32 m_size;
public:
_var_base(u32 count)
2016-05-13 16:01:48 +02:00
: pointer(A::alloc(SIZE_32(T) * count, ALIGN_32(T)))
2016-02-01 22:52:49 +01:00
, m_size(SIZE_32(T) * count)
{
}
2016-02-01 22:52:49 +01:00
_var_base(_var_base&& right)
: pointer(right)
2016-06-27 02:22:22 +02:00
, m_size(right.m_size)
{
2016-02-01 22:52:49 +01:00
reinterpret_cast<u32&>(static_cast<pointer&>(right)) = 0;
}
2016-02-01 22:52:49 +01:00
~_var_base()
{
2016-02-01 22:52:49 +01:00
if (pointer::addr()) A::dealloc(pointer::addr(), m_size);
}
// Remove operator ->
T* operator ->() const = delete;
2016-02-01 22:52:49 +01:00
u32 get_count() const
{
2016-02-01 22:52:49 +01:00
return m_size / SIZE_32(T);
}
2015-08-13 15:28:42 +02:00
};
// LE variable
template<typename T, typename A> using varl = _var_base<to_le_t<T>, A>;
// BE variable
template<typename T, typename A> using varb = _var_base<to_be_t<T>, A>;
2015-03-13 16:06:27 +01:00
2015-08-13 15:28:42 +02:00
namespace ps3
{
// BE variable
template<typename T, typename A = stack_allocator<ppu_thread>> using var = varb<T, A>;
2016-02-01 22:52:49 +01:00
// Make BE variable initialized from value
template<typename T, typename A = stack_allocator<ppu_thread>>
2016-05-26 17:06:06 +02:00
inline auto make_var(const T& value)
{
return varb<T, A>(value);
}
// Make char[] variable initialized from std::string
template<typename A = stack_allocator<ppu_thread>>
static auto make_str(const std::string& str)
{
var<char[], A> var_(size32(str) + 1);
std::memcpy(var_.get_ptr(), str.c_str(), str.size() + 1);
return var_;
}
2016-02-01 22:52:49 +01:00
// Global HLE variable
template<typename T>
struct gvar : ptr<T>
{
};
2015-08-13 15:28:42 +02:00
}
2015-08-13 15:28:42 +02:00
namespace psv
{
// LE variable
template<typename T, typename A = stack_allocator<ARMv7Thread>> using var = varl<T, A>;
2016-02-01 22:52:49 +01:00
// Make LE variable initialized from value
template<typename T, typename A = stack_allocator<ARMv7Thread>>
2016-05-26 17:06:06 +02:00
inline auto make_var(const T& value)
{
return var<T, A>(value);
}
// Make char[] variable initialized from std::string
template<typename A = stack_allocator<ARMv7Thread>>
static auto make_str(const std::string& str)
{
var<char[], A> var_(size32(str) + 1);
std::memcpy(var_.get_ptr(), str.c_str(), str.size() + 1);
return var_;
}
2016-02-01 22:52:49 +01:00
// Global HLE variable
template<typename T>
struct gvar : ptr<T>
{
};
}
2015-08-13 15:28:42 +02:00
}