code: Use std::span where appropriate (#6658)

* code: Use std::span when possible

* code: Prefix memcpy and memcmp with std::
This commit is contained in:
GPUCode 2023-07-07 01:52:40 +03:00 committed by GitHub
parent 4ccd9f24fb
commit cf9bb90ae3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
106 changed files with 362 additions and 329 deletions

View file

@ -149,7 +149,7 @@ static THREEDSX_Error Load3DSXFile(FileUtil::IOFile& file, u32 base_addr,
return ERROR_READ;
// BSS clear
memset((char*)loadinfo.seg_ptrs[2] + hdr.data_seg_size - hdr.bss_size, 0, hdr.bss_size);
std::memset((char*)loadinfo.seg_ptrs[2] + hdr.data_seg_size - hdr.bss_size, 0, hdr.bss_size);
// Relocate the segments
for (unsigned int current_segment = 0; current_segment < NUM_SEGMENTS; ++current_segment) {

View file

@ -327,7 +327,7 @@ std::shared_ptr<CodeSet> ElfReader::LoadInto(u32 vaddr) {
codeset_segment->addr = segment_addr;
codeset_segment->size = aligned_size;
memcpy(&program_image[current_image_position], GetSegmentPtr(i), p->p_filesz);
std::memcpy(&program_image[current_image_position], GetSegmentPtr(i), p->p_filesz);
current_image_position += aligned_size;
}
}

View file

@ -171,7 +171,7 @@ void AppLoader_NCCH::ParseRegionLockoutInfo(u64 program_id) {
std::vector<u8> smdh_buffer;
if (ReadIcon(smdh_buffer) == ResultStatus::Success && smdh_buffer.size() >= sizeof(SMDH)) {
SMDH smdh;
memcpy(&smdh, smdh_buffer.data(), sizeof(SMDH));
std::memcpy(&smdh, smdh_buffer.data(), sizeof(SMDH));
u32 region_lockout = smdh.region_lockout;
constexpr u32 REGION_COUNT = 7;
std::vector<u32> regions;
@ -185,15 +185,20 @@ void AppLoader_NCCH::ParseRegionLockoutInfo(u64 program_id) {
} else {
const auto region = Core::GetSystemTitleRegion(program_id);
if (region.has_value()) {
cfg->SetPreferredRegionCodes({region.value()});
const std::array regions{region.value()};
cfg->SetPreferredRegionCodes(regions);
}
}
}
bool AppLoader_NCCH::IsGbaVirtualConsole(const std::vector<u8>& code) {
const u32* gbaVcHeader = reinterpret_cast<const u32*>(code.data() + code.size() - 0x10);
return code.size() >= 0x10 && gbaVcHeader[0] == MakeMagic('.', 'C', 'A', 'A') &&
gbaVcHeader[1] == 1;
bool AppLoader_NCCH::IsGbaVirtualConsole(std::span<const u8> code) {
if (code.size() < 0x10) [[unlikely]] {
return false;
}
u32 gbaVcHeader[2];
std::memcpy(gbaVcHeader, code.data() + code.size() - 0x10, sizeof(gbaVcHeader));
return gbaVcHeader[0] == MakeMagic('.', 'C', 'A', 'A') && gbaVcHeader[1] == 1;
}
ResultStatus AppLoader_NCCH::Load(std::shared_ptr<Kernel::Process>& process) {
@ -317,7 +322,7 @@ ResultStatus AppLoader_NCCH::ReadTitle(std::string& title) {
return ResultStatus::ErrorInvalidFormat;
}
memcpy(&smdh, data.data(), sizeof(Loader::SMDH));
std::memcpy(&smdh, data.data(), sizeof(Loader::SMDH));
const auto& short_title = smdh.GetShortTitle(SMDH::TitleLanguage::English);
auto title_end = std::find(short_title.begin(), short_title.end(), u'\0');

View file

@ -78,7 +78,7 @@ private:
void ParseRegionLockoutInfo(u64 program_id);
/// Detects whether the NCCH contains GBA Virtual Console.
bool IsGbaVirtualConsole(const std::vector<u8>& code);
bool IsGbaVirtualConsole(std::span<const u8> code);
FileSys::NCCHContainer base_ncch;
FileSys::NCCHContainer update_ncch;

View file

@ -11,12 +11,13 @@
namespace Loader {
bool IsValidSMDH(const std::vector<u8>& smdh_data) {
if (smdh_data.size() < sizeof(Loader::SMDH))
bool IsValidSMDH(std::span<const u8> smdh_data) {
if (smdh_data.size() < sizeof(Loader::SMDH)) {
return false;
}
u32 magic;
memcpy(&magic, smdh_data.data(), sizeof(u32));
std::memcpy(&magic, smdh_data.data(), sizeof(u32));
return Loader::MakeMagic('S', 'M', 'D', 'H') == magic;
}

View file

@ -5,6 +5,7 @@
#pragma once
#include <array>
#include <span>
#include <vector>
#include "common/common_funcs.h"
#include "common/common_types.h"
@ -17,7 +18,7 @@ namespace Loader {
* @param smdh_data data buffer to test
* @return bool test result
*/
bool IsValidSMDH(const std::vector<u8>& smdh_data);
bool IsValidSMDH(std::span<const u8> smdh_data);
/// SMDH data structure that contains titles, icons etc. See https://www.3dbrew.org/wiki/SMDH
struct SMDH {