From 98fe5f82c58f76dbc9730f9b82257993c8e77cc9 Mon Sep 17 00:00:00 2001 From: Mat M Date: Tue, 28 Apr 2020 15:43:52 -0400 Subject: [PATCH] memory: Make getter functions const qualified where applicable (#5251) Many of these functions are capable of being used within const contexts, so we can apply the const qualifier in some cases and add const based overloads for others, which makes the interface a little bit more flexible and const-correct. --- src/core/memory.cpp | 36 ++++++++++++++++++++++++++++++------ src/core/memory.h | 23 ++++++++++++++--------- 2 files changed, 44 insertions(+), 15 deletions(-) diff --git a/src/core/memory.cpp b/src/core/memory.cpp index 0306868fd..9f78242fc 100644 --- a/src/core/memory.cpp +++ b/src/core/memory.cpp @@ -266,7 +266,7 @@ void MemorySystem::UnmapRegion(PageTable& page_table, VAddr base, u32 size) { MapPages(page_table, base / PAGE_SIZE, size / PAGE_SIZE, nullptr, PageType::Unmapped); } -MemoryRef MemorySystem::GetPointerForRasterizerCache(VAddr addr) { +MemoryRef MemorySystem::GetPointerForRasterizerCache(VAddr addr) const { if (addr >= LINEAR_HEAP_VADDR && addr < LINEAR_HEAP_VADDR_END) { return {impl->fcram_mem, addr - LINEAR_HEAP_VADDR}; } @@ -394,7 +394,7 @@ bool IsValidVirtualAddress(const Kernel::Process& process, const VAddr vaddr) { return false; } -bool MemorySystem::IsValidPhysicalAddress(const PAddr paddr) { +bool MemorySystem::IsValidPhysicalAddress(const PAddr paddr) const { return GetPhysicalPointer(paddr) != nullptr; } @@ -414,6 +414,21 @@ u8* MemorySystem::GetPointer(const VAddr vaddr) { return nullptr; } +const u8* MemorySystem::GetPointer(const VAddr vaddr) const { + const u8* page_pointer = impl->current_page_table->pointers[vaddr >> PAGE_BITS]; + if (page_pointer) { + return page_pointer + (vaddr & PAGE_MASK); + } + + if (impl->current_page_table->attributes[vaddr >> PAGE_BITS] == + PageType::RasterizerCachedMemory) { + return GetPointerForRasterizerCache(vaddr); + } + + LOG_ERROR(HW_Memory, "unknown GetPointer @ 0x{:08x}", vaddr); + return nullptr; +} + std::string MemorySystem::ReadCString(VAddr vaddr, std::size_t max_length) { std::string string; string.reserve(max_length); @@ -432,7 +447,11 @@ u8* MemorySystem::GetPhysicalPointer(PAddr address) { return GetPhysicalRef(address); } -MemoryRef MemorySystem::GetPhysicalRef(PAddr address) { +const u8* MemorySystem::GetPhysicalPointer(PAddr address) const { + return GetPhysicalRef(address); +} + +MemoryRef MemorySystem::GetPhysicalRef(PAddr address) const { struct MemoryArea { PAddr paddr_base; u32 size; @@ -911,17 +930,22 @@ void WriteMMIO(MMIORegionPointer mmio_handler, VAddr addr, const u64 data) mmio_handler->Write64(addr, data); } -u32 MemorySystem::GetFCRAMOffset(const u8* pointer) { +u32 MemorySystem::GetFCRAMOffset(const u8* pointer) const { ASSERT(pointer >= impl->fcram.get() && pointer <= impl->fcram.get() + Memory::FCRAM_N3DS_SIZE); return static_cast(pointer - impl->fcram.get()); } -u8* MemorySystem::GetFCRAMPointer(u32 offset) { +u8* MemorySystem::GetFCRAMPointer(std::size_t offset) { ASSERT(offset <= Memory::FCRAM_N3DS_SIZE); return impl->fcram.get() + offset; } -MemoryRef MemorySystem::GetFCRAMRef(u32 offset) { +const u8* MemorySystem::GetFCRAMPointer(std::size_t offset) const { + ASSERT(offset <= Memory::FCRAM_N3DS_SIZE); + return impl->fcram.get() + offset; +} + +MemoryRef MemorySystem::GetFCRAMRef(std::size_t offset) const { ASSERT(offset <= Memory::FCRAM_N3DS_SIZE); return MemoryRef(impl->fcram_mem, offset); } diff --git a/src/core/memory.h b/src/core/memory.h index e8e714952..01b3777b0 100644 --- a/src/core/memory.h +++ b/src/core/memory.h @@ -339,25 +339,30 @@ public: std::string ReadCString(VAddr vaddr, std::size_t max_length); - /** - * Gets a pointer to the memory region beginning at the specified physical address. - */ + /// Gets a pointer to the memory region beginning at the specified physical address. u8* GetPhysicalPointer(PAddr address); - MemoryRef GetPhysicalRef(PAddr address); + /// Gets a pointer to the memory region beginning at the specified physical address. + const u8* GetPhysicalPointer(PAddr address) const; + + MemoryRef GetPhysicalRef(PAddr address) const; u8* GetPointer(VAddr vaddr); + const u8* GetPointer(VAddr vaddr) const; - bool IsValidPhysicalAddress(PAddr paddr); + bool IsValidPhysicalAddress(PAddr paddr) const; /// Gets offset in FCRAM from a pointer inside FCRAM range - u32 GetFCRAMOffset(const u8* pointer); + u32 GetFCRAMOffset(const u8* pointer) const; /// Gets pointer in FCRAM with given offset - u8* GetFCRAMPointer(u32 offset); + u8* GetFCRAMPointer(std::size_t offset); + + /// Gets pointer in FCRAM with given offset + const u8* GetFCRAMPointer(std::size_t offset) const; /// Gets a serializable ref to FCRAM with the given offset - MemoryRef GetFCRAMRef(u32 offset); + MemoryRef GetFCRAMRef(std::size_t offset) const; /** * Mark each page touching the region as cached. @@ -385,7 +390,7 @@ private: * Since the cache only happens on linear heap or VRAM, we know the exact physical address and * pointer of such virtual address */ - MemoryRef GetPointerForRasterizerCache(VAddr addr); + MemoryRef GetPointerForRasterizerCache(VAddr addr) const; void MapPages(PageTable& page_table, u32 base, u32 size, MemoryRef memory, PageType type);