From ff45c3957858cdf189b73e11550da06fe4337b8e Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 22 Sep 2020 17:31:53 -0400 Subject: [PATCH] General: Make use of std::nullopt where applicable Allows some implementations to avoid completely zeroing out the internal buffer of the optional, and instead only set the validity byte within the structure. This also makes it consistent how we return empty optionals. --- src/core/arm/dynarmic/arm_dynarmic_cp15.cpp | 6 ++--- src/core/file_sys/content_archive.cpp | 10 ++++---- src/core/file_sys/vfs.cpp | 7 +++--- src/core/file_sys/vfs_offset.cpp | 7 +++--- src/core/file_sys/vfs_static.h | 8 +++--- .../service/nvdrv/devices/nvhost_as_gpu.cpp | 4 +-- src/core/hle/service/nvdrv/nvdrv.h | 2 +- src/core/hle/service/nvflinger/nvflinger.cpp | 6 ++--- src/core/hle/service/sockets/bsd.cpp | 2 +- src/core/loader/nso.cpp | 6 ++--- src/video_core/engines/maxwell_3d.cpp | 2 +- src/video_core/macro/macro.cpp | 2 +- src/video_core/memory_manager.cpp | 6 ++--- .../renderer_opengl/gl_shader_decompiler.cpp | 12 ++++----- src/video_core/shader/ast.h | 25 +++++++------------ src/video_core/shader/track.cpp | 4 +-- src/video_core/texture_cache/surface_base.cpp | 10 +++++--- 17 files changed, 60 insertions(+), 59 deletions(-) diff --git a/src/core/arm/dynarmic/arm_dynarmic_cp15.cpp b/src/core/arm/dynarmic/arm_dynarmic_cp15.cpp index 54556e0f9..caefc09f4 100644 --- a/src/core/arm/dynarmic/arm_dynarmic_cp15.cpp +++ b/src/core/arm/dynarmic/arm_dynarmic_cp15.cpp @@ -34,7 +34,7 @@ std::optional DynarmicCP15::CompileInternalOperation(bool two, unsigne CoprocReg CRm, unsigned opc2) { LOG_CRITICAL(Core_ARM, "CP15: cdp{} p15, {}, {}, {}, {}, {}", two ? "2" : "", opc1, CRd, CRn, CRm, opc2); - return {}; + return std::nullopt; } CallbackOrAccessOneWord DynarmicCP15::CompileSendOneWord(bool two, unsigned opc1, CoprocReg CRn, @@ -115,7 +115,7 @@ std::optional DynarmicCP15::CompileLoadWords(bool two, bool long_trans LOG_CRITICAL(Core_ARM, "CP15: mrrc{}{} p15, {}, [...]", two ? "2" : "", long_transfer ? "l" : "", CRd); } - return {}; + return std::nullopt; } std::optional DynarmicCP15::CompileStoreWords(bool two, bool long_transfer, CoprocReg CRd, @@ -127,7 +127,7 @@ std::optional DynarmicCP15::CompileStoreWords(bool two, bool long_tran LOG_CRITICAL(Core_ARM, "CP15: mrrc{}{} p15, {}, [...]", two ? "2" : "", long_transfer ? "l" : "", CRd); } - return {}; + return std::nullopt; } } // namespace Core diff --git a/src/core/file_sys/content_archive.cpp b/src/core/file_sys/content_archive.cpp index 426fb6bb5..76af47ff9 100644 --- a/src/core/file_sys/content_archive.cpp +++ b/src/core/file_sys/content_archive.cpp @@ -323,7 +323,7 @@ bool NCA::ReadRomFSSection(const NCASectionHeader& section, const NCASectionTabl subsection_buckets.back().entries.push_back({section.bktr.relocation.offset, {0}, ctr_low}); subsection_buckets.back().entries.push_back({size, {0}, 0}); - std::optional key = {}; + std::optional key; if (encrypted) { if (has_rights_id) { status = Loader::ResultStatus::Success; @@ -442,18 +442,18 @@ std::optional NCA::GetTitlekey() { memcpy(rights_id.data(), header.rights_id.data(), 16); if (rights_id == u128{}) { status = Loader::ResultStatus::ErrorInvalidRightsID; - return {}; + return std::nullopt; } auto titlekey = keys.GetKey(Core::Crypto::S128KeyType::Titlekey, rights_id[1], rights_id[0]); if (titlekey == Core::Crypto::Key128{}) { status = Loader::ResultStatus::ErrorMissingTitlekey; - return {}; + return std::nullopt; } if (!keys.HasKey(Core::Crypto::S128KeyType::Titlekek, master_key_id)) { status = Loader::ResultStatus::ErrorMissingTitlekek; - return {}; + return std::nullopt; } Core::Crypto::AESCipher cipher( @@ -477,7 +477,7 @@ VirtualFile NCA::Decrypt(const NCASectionHeader& s_header, VirtualFile in, u64 s case NCASectionCryptoType::BKTR: LOG_TRACE(Crypto, "called with mode=CTR, starting_offset={:016X}", starting_offset); { - std::optional key = {}; + std::optional key; if (has_rights_id) { status = Loader::ResultStatus::Success; key = GetTitlekey(); diff --git a/src/core/file_sys/vfs.cpp b/src/core/file_sys/vfs.cpp index a4c3f67c4..b2f026b6d 100644 --- a/src/core/file_sys/vfs.cpp +++ b/src/core/file_sys/vfs.cpp @@ -169,11 +169,12 @@ VfsDirectory::~VfsDirectory() = default; std::optional VfsFile::ReadByte(std::size_t offset) const { u8 out{}; - std::size_t size = Read(&out, 1, offset); - if (size == 1) + const std::size_t size = Read(&out, sizeof(u8), offset); + if (size == 1) { return out; + } - return {}; + return std::nullopt; } std::vector VfsFile::ReadBytes(std::size_t size, std::size_t offset) const { diff --git a/src/core/file_sys/vfs_offset.cpp b/src/core/file_sys/vfs_offset.cpp index c96f88488..7714d3de5 100644 --- a/src/core/file_sys/vfs_offset.cpp +++ b/src/core/file_sys/vfs_offset.cpp @@ -58,10 +58,11 @@ std::size_t OffsetVfsFile::Write(const u8* data, std::size_t length, std::size_t } std::optional OffsetVfsFile::ReadByte(std::size_t r_offset) const { - if (r_offset < size) - return file->ReadByte(offset + r_offset); + if (r_offset >= size) { + return std::nullopt; + } - return {}; + return file->ReadByte(offset + r_offset); } std::vector OffsetVfsFile::ReadBytes(std::size_t r_size, std::size_t r_offset) const { diff --git a/src/core/file_sys/vfs_static.h b/src/core/file_sys/vfs_static.h index 9f5a90b1b..8b27c30fa 100644 --- a/src/core/file_sys/vfs_static.h +++ b/src/core/file_sys/vfs_static.h @@ -54,9 +54,11 @@ public: } std::optional ReadByte(std::size_t offset) const override { - if (offset < size) - return value; - return {}; + if (offset >= size) { + return std::nullopt; + } + + return value; } std::vector ReadBytes(std::size_t length, std::size_t offset) const override { diff --git a/src/core/hle/service/nvdrv/devices/nvhost_as_gpu.cpp b/src/core/hle/service/nvdrv/devices/nvhost_as_gpu.cpp index d4ba88147..39bd2a45b 100644 --- a/src/core/hle/service/nvdrv/devices/nvhost_as_gpu.cpp +++ b/src/core/hle/service/nvdrv/devices/nvhost_as_gpu.cpp @@ -265,7 +265,7 @@ std::optional nvhost_as_gpu::FindBufferMap(GPUVAddr gp } } - return {}; + return std::nullopt; } void nvhost_as_gpu::AddBufferMap(GPUVAddr gpu_addr, std::size_t size, VAddr cpu_addr, @@ -286,7 +286,7 @@ std::optional nvhost_as_gpu::RemoveBufferMap(GPUVAddr gpu_addr) { return size; } - return {}; + return std::nullopt; } } // namespace Service::Nvidia::Devices diff --git a/src/core/hle/service/nvdrv/nvdrv.h b/src/core/hle/service/nvdrv/nvdrv.h index d7a1bef91..7706a5590 100644 --- a/src/core/hle/service/nvdrv/nvdrv.h +++ b/src/core/hle/service/nvdrv/nvdrv.h @@ -54,7 +54,7 @@ struct EventInterface { } mask = mask >> 1; } - return {}; + return std::nullopt; } void SetEventStatus(const u32 event_id, EventState new_status) { EventState old_status = status[event_id]; diff --git a/src/core/hle/service/nvflinger/nvflinger.cpp b/src/core/hle/service/nvflinger/nvflinger.cpp index f644a460d..c64673dba 100644 --- a/src/core/hle/service/nvflinger/nvflinger.cpp +++ b/src/core/hle/service/nvflinger/nvflinger.cpp @@ -114,7 +114,7 @@ std::optional NVFlinger::OpenDisplay(std::string_view name) { [&](const VI::Display& display) { return display.GetName() == name; }); if (itr == displays.end()) { - return {}; + return std::nullopt; } return itr->GetID(); @@ -124,7 +124,7 @@ std::optional NVFlinger::CreateLayer(u64 display_id) { auto* const display = FindDisplay(display_id); if (display == nullptr) { - return {}; + return std::nullopt; } const u64 layer_id = next_layer_id++; @@ -144,7 +144,7 @@ std::optional NVFlinger::FindBufferQueueId(u64 display_id, u64 layer_id) co const auto* const layer = FindLayer(display_id, layer_id); if (layer == nullptr) { - return {}; + return std::nullopt; } return layer->GetBufferQueue().GetId(); diff --git a/src/core/hle/service/sockets/bsd.cpp b/src/core/hle/service/sockets/bsd.cpp index 7b9dd42d8..a74be9370 100644 --- a/src/core/hle/service/sockets/bsd.cpp +++ b/src/core/hle/service/sockets/bsd.cpp @@ -497,7 +497,7 @@ std::pair BSD::PollImpl(std::vector& write_buffer, std::vector& descriptor = file_descriptors[pollfd.fd]; + const std::optional& descriptor = file_descriptors[pollfd.fd]; if (!descriptor) { LOG_ERROR(Service, "File descriptor handle={} is not allocated", pollfd.fd); pollfd.revents = POLL_NVAL; diff --git a/src/core/loader/nso.cpp b/src/core/loader/nso.cpp index 60373cc5f..1e70f6e11 100644 --- a/src/core/loader/nso.cpp +++ b/src/core/loader/nso.cpp @@ -76,16 +76,16 @@ std::optional AppLoader_NSO::LoadModule(Kernel::Process& process, Core::S bool should_pass_arguments, bool load_into_process, std::optional pm) { if (file.GetSize() < sizeof(NSOHeader)) { - return {}; + return std::nullopt; } NSOHeader nso_header{}; if (sizeof(NSOHeader) != file.ReadObject(&nso_header)) { - return {}; + return std::nullopt; } if (nso_header.magic != Common::MakeMagic('N', 'S', 'O', '0')) { - return {}; + return std::nullopt; } // Build program image diff --git a/src/video_core/engines/maxwell_3d.cpp b/src/video_core/engines/maxwell_3d.cpp index 33854445f..57ebc785f 100644 --- a/src/video_core/engines/maxwell_3d.cpp +++ b/src/video_core/engines/maxwell_3d.cpp @@ -597,7 +597,7 @@ std::optional Maxwell3D::GetQueryResult() { // Deferred. rasterizer->Query(regs.query.QueryAddress(), VideoCore::QueryType::SamplesPassed, system.GPU().GetTicks()); - return {}; + return std::nullopt; default: LOG_DEBUG(HW_GPU, "Unimplemented query select type {}", static_cast(regs.query.query_get.select.Value())); diff --git a/src/video_core/macro/macro.cpp b/src/video_core/macro/macro.cpp index a50e7b4e0..cd21a2112 100644 --- a/src/video_core/macro/macro.cpp +++ b/src/video_core/macro/macro.cpp @@ -36,7 +36,7 @@ void MacroEngine::Execute(Engines::Maxwell3D& maxwell3d, u32 method, } } else { // Macro not compiled, check if it's uploaded and if so, compile it - std::optional mid_method = std::nullopt; + std::optional mid_method; const auto macro_code = uploaded_macro_code.find(method); if (macro_code == uploaded_macro_code.end()) { for (const auto& [method_base, code] : uploaded_macro_code) { diff --git a/src/video_core/memory_manager.cpp b/src/video_core/memory_manager.cpp index 16b2aaa27..02cf53d15 100644 --- a/src/video_core/memory_manager.cpp +++ b/src/video_core/memory_manager.cpp @@ -58,7 +58,7 @@ void MemoryManager::Unmap(GPUVAddr gpu_addr, std::size_t size) { std::optional MemoryManager::AllocateFixed(GPUVAddr gpu_addr, std::size_t size) { for (u64 offset{}; offset < size; offset += page_size) { if (!GetPageEntry(gpu_addr + offset).IsUnmapped()) { - return {}; + return std::nullopt; } } @@ -135,13 +135,13 @@ std::optional MemoryManager::FindFreeRange(std::size_t size, std::size } } - return {}; + return std::nullopt; } std::optional MemoryManager::GpuToCpuAddress(GPUVAddr gpu_addr) const { const auto page_entry{GetPageEntry(gpu_addr)}; if (!page_entry.IsValid()) { - return {}; + return std::nullopt; } return page_entry.ToAddress() + (gpu_addr & page_mask); diff --git a/src/video_core/renderer_opengl/gl_shader_decompiler.cpp b/src/video_core/renderer_opengl/gl_shader_decompiler.cpp index ce3a65122..bbb8fb095 100644 --- a/src/video_core/renderer_opengl/gl_shader_decompiler.cpp +++ b/src/video_core/renderer_opengl/gl_shader_decompiler.cpp @@ -813,7 +813,7 @@ private: const u8 location = static_cast(static_cast(index) * 4 + element); const auto it = transform_feedback.find(location); if (it == transform_feedback.end()) { - return {}; + return std::nullopt; } return it->second.components; } @@ -1295,21 +1295,21 @@ private: switch (element) { case 0: UNIMPLEMENTED(); - return {}; + return std::nullopt; case 1: if (stage == ShaderType::Vertex && !device.HasVertexViewportLayer()) { - return {}; + return std::nullopt; } return {{"gl_Layer", Type::Int}}; case 2: if (stage == ShaderType::Vertex && !device.HasVertexViewportLayer()) { - return {}; + return std::nullopt; } return {{"gl_ViewportIndex", Type::Int}}; case 3: return {{"gl_PointSize", Type::Float}}; } - return {}; + return std::nullopt; case Attribute::Index::FrontColor: return {{"gl_FrontColor"s + GetSwizzle(element), Type::Float}}; case Attribute::Index::FrontSecondaryColor: @@ -1332,7 +1332,7 @@ private: Type::Float}}; } UNIMPLEMENTED_MSG("Unhandled output attribute: {}", static_cast(attribute)); - return {}; + return std::nullopt; } } diff --git a/src/video_core/shader/ast.h b/src/video_core/shader/ast.h index cca13bcde..8e5a22ab3 100644 --- a/src/video_core/shader/ast.h +++ b/src/video_core/shader/ast.h @@ -199,55 +199,48 @@ public: } std::optional GetGotoLabel() const { - auto inner = std::get_if(&data); - if (inner) { + if (const auto* inner = std::get_if(&data)) { return {inner->label}; } - return {}; + return std::nullopt; } Expr GetGotoCondition() const { - auto inner = std::get_if(&data); - if (inner) { + if (const auto* inner = std::get_if(&data)) { return inner->condition; } return nullptr; } void MarkLabelUnused() { - auto inner = std::get_if(&data); - if (inner) { + if (auto* inner = std::get_if(&data)) { inner->unused = true; } } bool IsLabelUnused() const { - auto inner = std::get_if(&data); - if (inner) { + if (const auto* inner = std::get_if(&data)) { return inner->unused; } return true; } std::optional GetLabelIndex() const { - auto inner = std::get_if(&data); - if (inner) { + if (const auto* inner = std::get_if(&data)) { return {inner->index}; } - return {}; + return std::nullopt; } Expr GetIfCondition() const { - auto inner = std::get_if(&data); - if (inner) { + if (const auto* inner = std::get_if(&data)) { return inner->condition; } return nullptr; } void SetGotoCondition(Expr new_condition) { - auto inner = std::get_if(&data); - if (inner) { + if (auto* inner = std::get_if(&data)) { inner->condition = std::move(new_condition); } } diff --git a/src/video_core/shader/track.cpp b/src/video_core/shader/track.cpp index d5ed81442..6be3ea92b 100644 --- a/src/video_core/shader/track.cpp +++ b/src/video_core/shader/track.cpp @@ -205,12 +205,12 @@ std::optional ShaderIR::TrackImmediate(Node tracked, const NodeBlock& code, const auto result = TrackRegister(&std::get(*tracked), code, cursor - 1); const auto& found = result.first; if (!found) { - return {}; + return std::nullopt; } if (const auto immediate = std::get_if(&*found)) { return immediate->GetValue(); } - return {}; + return std::nullopt; } std::pair ShaderIR::TrackRegister(const GprNode* tracked, const NodeBlock& code, diff --git a/src/video_core/texture_cache/surface_base.cpp b/src/video_core/texture_cache/surface_base.cpp index dfcf36e0b..b44c09d71 100644 --- a/src/video_core/texture_cache/surface_base.cpp +++ b/src/video_core/texture_cache/surface_base.cpp @@ -115,20 +115,24 @@ std::optional> SurfaceBaseImpl::GetLayerMipmap( if (gpu_addr == candidate_gpu_addr) { return {{0, 0}}; } + if (candidate_gpu_addr < gpu_addr) { - return {}; + return std::nullopt; } + const auto relative_address{static_cast(candidate_gpu_addr - gpu_addr)}; const auto layer{static_cast(relative_address / layer_size)}; if (layer >= params.depth) { - return {}; + return std::nullopt; } + const GPUVAddr mipmap_address = relative_address - layer_size * layer; const auto mipmap_it = Common::BinaryFind(mipmap_offsets.begin(), mipmap_offsets.end(), mipmap_address); if (mipmap_it == mipmap_offsets.end()) { - return {}; + return std::nullopt; } + const auto level{static_cast(std::distance(mipmap_offsets.begin(), mipmap_it))}; return std::make_pair(layer, level); }