Skip to content

Commit

Permalink
vm_ptr.h: Improve try_read() (RPCS3#10627)
Browse files Browse the repository at this point in the history
  • Loading branch information
elad335 authored Jul 29, 2021
1 parent 3a6399a commit d85bb3f
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions rpcs3/Emu/Memory/vm_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,16 +206,24 @@ namespace vm
return *this;
}

template <bool = false> requires (std::is_copy_constructible_v<T>)
std::pair<bool, std::conditional_t<std::is_void_v<T>, char, std::remove_const_t<T>>> try_read() const
{
alignas(sizeof(T) >= 16 ? 16 : 8) char buf[sizeof(T)]{};
const bool ok = vm::try_access(vm::cast(m_addr), buf, sizeof(T), false);
return { ok, std::bit_cast<decltype(try_read().second)>(buf) };
}

template <bool = false> requires (!std::is_void_v<T>)
bool try_read(std::conditional_t<std::is_void_v<T>, char, std::remove_const_t<T>>& out) const
{
return vm::try_access(vm::cast(m_addr), &out, sizeof(T), false);
return vm::try_access(vm::cast(m_addr), std::addressof(out), sizeof(T), false);
}

template <bool = false> requires (!std::is_void_v<T> && !std::is_const_v<T>)
bool try_write(const std::conditional_t<std::is_void_v<T>, char, T>& _in) const
{
return vm::try_access(vm::cast(m_addr), const_cast<T*>(&_in), sizeof(T), true);
return vm::try_access(vm::cast(m_addr), const_cast<T*>(std::addressof(_in)), sizeof(T), true);
}

// Don't use
Expand Down

0 comments on commit d85bb3f

Please sign in to comment.