core/memory: Migrate over ReadCString() to the Memory class

This only had one usage spot, so this is fairly straightforward to
convert over.
This commit is contained in:
Lioncash 2019-11-26 15:48:19 -05:00
parent 3f08e8d8d4
commit b2165c6b35
3 changed files with 40 additions and 18 deletions

View File

@ -332,7 +332,9 @@ static ResultCode UnmapMemory(Core::System& system, VAddr dst_addr, VAddr src_ad
/// Connect to an OS service given the port name, returns the handle to the port to out /// Connect to an OS service given the port name, returns the handle to the port to out
static ResultCode ConnectToNamedPort(Core::System& system, Handle* out_handle, static ResultCode ConnectToNamedPort(Core::System& system, Handle* out_handle,
VAddr port_name_address) { VAddr port_name_address) {
if (!system.Memory().IsValidVirtualAddress(port_name_address)) { auto& memory = system.Memory();
if (!memory.IsValidVirtualAddress(port_name_address)) {
LOG_ERROR(Kernel_SVC, LOG_ERROR(Kernel_SVC,
"Port Name Address is not a valid virtual address, port_name_address=0x{:016X}", "Port Name Address is not a valid virtual address, port_name_address=0x{:016X}",
port_name_address); port_name_address);
@ -341,7 +343,7 @@ static ResultCode ConnectToNamedPort(Core::System& system, Handle* out_handle,
static constexpr std::size_t PortNameMaxLength = 11; static constexpr std::size_t PortNameMaxLength = 11;
// Read 1 char beyond the max allowed port name to detect names that are too long. // Read 1 char beyond the max allowed port name to detect names that are too long.
std::string port_name = Memory::ReadCString(port_name_address, PortNameMaxLength + 1); const std::string port_name = memory.ReadCString(port_name_address, PortNameMaxLength + 1);
if (port_name.size() > PortNameMaxLength) { if (port_name.size() > PortNameMaxLength) {
LOG_ERROR(Kernel_SVC, "Port name is too long, expected {} but got {}", PortNameMaxLength, LOG_ERROR(Kernel_SVC, "Port name is too long, expected {} but got {}", PortNameMaxLength,
port_name.size()); port_name.size());

View File

@ -210,6 +210,21 @@ struct Memory::Impl {
return nullptr; return nullptr;
} }
std::string ReadCString(VAddr vaddr, std::size_t max_length) {
std::string string;
string.reserve(max_length);
for (std::size_t i = 0; i < max_length; ++i) {
const char c = Read8(vaddr);
if (c == '\0') {
break;
}
string.push_back(c);
++vaddr;
}
string.shrink_to_fit();
return string;
}
/** /**
* Maps a region of pages as a specific type. * Maps a region of pages as a specific type.
* *
@ -299,6 +314,10 @@ const u8* Memory::GetPointer(VAddr vaddr) const {
return impl->GetPointer(vaddr); return impl->GetPointer(vaddr);
} }
std::string Memory::ReadCString(VAddr vaddr, std::size_t max_length) {
return impl->ReadCString(vaddr, max_length);
}
void SetCurrentPageTable(Kernel::Process& process) { void SetCurrentPageTable(Kernel::Process& process) {
current_page_table = &process.VMManager().page_table; current_page_table = &process.VMManager().page_table;
@ -315,20 +334,6 @@ bool IsKernelVirtualAddress(const VAddr vaddr) {
return KERNEL_REGION_VADDR <= vaddr && vaddr < KERNEL_REGION_END; return KERNEL_REGION_VADDR <= vaddr && vaddr < KERNEL_REGION_END;
} }
std::string ReadCString(VAddr vaddr, std::size_t max_length) {
std::string string;
string.reserve(max_length);
for (std::size_t i = 0; i < max_length; ++i) {
char c = Read8(vaddr);
if (c == '\0')
break;
string.push_back(c);
++vaddr;
}
string.shrink_to_fit();
return string;
}
void RasterizerMarkRegionCached(VAddr vaddr, u64 size, bool cached) { void RasterizerMarkRegionCached(VAddr vaddr, u64 size, bool cached) {
if (vaddr == 0) { if (vaddr == 0) {
return; return;

View File

@ -152,6 +152,23 @@ public:
*/ */
const u8* GetPointer(VAddr vaddr) const; const u8* GetPointer(VAddr vaddr) const;
/**
* Reads a null-terminated string from the given virtual address.
* This function will continually read characters until either:
*
* - A null character ('\0') is reached.
* - max_length characters have been read.
*
* @note The final null-terminating character (if found) is not included
* in the returned string.
*
* @param vaddr The address to begin reading the string from.
* @param max_length The maximum length of the string to read in characters.
*
* @returns The read string.
*/
std::string ReadCString(VAddr vaddr, std::size_t max_length);
private: private:
struct Impl; struct Impl;
std::unique_ptr<Impl> impl; std::unique_ptr<Impl> impl;
@ -182,8 +199,6 @@ void WriteBlock(VAddr dest_addr, const void* src_buffer, std::size_t size);
void ZeroBlock(const Kernel::Process& process, VAddr dest_addr, std::size_t size); void ZeroBlock(const Kernel::Process& process, VAddr dest_addr, std::size_t size);
void CopyBlock(VAddr dest_addr, VAddr src_addr, std::size_t size); void CopyBlock(VAddr dest_addr, VAddr src_addr, std::size_t size);
std::string ReadCString(VAddr vaddr, std::size_t max_length);
/** /**
* Mark each page touching the region as cached. * Mark each page touching the region as cached.
*/ */