From 82677e3dd820ddf5740e31a1f14d43df1b0988fe Mon Sep 17 00:00:00 2001 From: MerryMage Date: Mon, 25 Jan 2016 22:13:31 +0000 Subject: [PATCH] Memory: Implemented InjectIntoMemory and ExtractFromMemory --- src/core/memory.h | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/core/memory.h b/src/core/memory.h index e6efd7b2d..841d7c4a5 100644 --- a/src/core/memory.h +++ b/src/core/memory.h @@ -140,6 +140,38 @@ u8* GetPointer(VAddr virtual_address); */ std::string GetString(const VAddr vaddr, const u32 size); +/** +* Extracts a POD type from memory. Returns false if address is invalid. +*/ +template +bool ExtractFromMemory(VAddr address, T& object) { + static_assert(std::is_standard_layout::value, "Type must have standard layout"); + + const u8* memory = GetPointer(address); + if (!memory) { + return false; + } + + std::memcpy(&object, memory, sizeof(T)); + return true; +} + +/** +* Injects a POD type into memory. Returns false if address is invalid. +*/ +template +bool InjectIntoMemory(VAddr address, const T& object) { + static_assert(std::is_standard_layout::value, "Type must have standard layout"); + + u8* memory = GetPointer(address); + if (!memory) { + return false; + } + + std::memcpy(memory, &object, sizeof(T)); + return true; +} + /** * Converts a virtual address inside a region with 1:1 mapping to physical memory to a physical * address. This should be used by services to translate addresses for use by the hardware.