From 05231d8b08f7d473a4c4cf7640227f41de44ac23 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 20 Jul 2018 21:40:13 -0400 Subject: [PATCH 1/4] vfs: Amend constness on pointers in WriteBytes() and WriteArrays() member functions to be const qualified These functions don't modify the data being pointed to, so these can be pointers to const data --- src/core/file_sys/vfs.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/core/file_sys/vfs.h b/src/core/file_sys/vfs.h index a5213e0cc..edd689c68 100644 --- a/src/core/file_sys/vfs.h +++ b/src/core/file_sys/vfs.h @@ -93,7 +93,7 @@ struct VfsFile : NonCopyable { // Writes an array of type T, size number_elements to offset in file. // Returns the number of bytes (sizeof(T)*number_elements) written successfully. template - size_t WriteArray(T* data, size_t number_elements, size_t offset = 0) { + size_t WriteArray(const T* data, size_t number_elements, size_t offset = 0) { static_assert(std::is_trivially_copyable::value, "Data type must be trivially copyable."); @@ -103,10 +103,10 @@ struct VfsFile : NonCopyable { // Writes size bytes starting at memory location data to offset in file. // Returns the number of bytes written successfully. template - size_t WriteBytes(T* data, size_t size, size_t offset = 0) { + size_t WriteBytes(const T* data, size_t size, size_t offset = 0) { static_assert(std::is_trivially_copyable::value, "Data type must be trivially copyable."); - return Write(reinterpret_cast(data), size, offset); + return Write(reinterpret_cast(data), size, offset); } // Writes one object of type T to offset in file. From dd09439feec14362b8d88c6fad662c9996426b67 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 20 Jul 2018 21:45:20 -0400 Subject: [PATCH 2/4] vfs: Use variable template variants of std::is_trivially_copyable Provides the same behavior, but with less writing --- src/core/file_sys/vfs.h | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/src/core/file_sys/vfs.h b/src/core/file_sys/vfs.h index edd689c68..d108ab1f4 100644 --- a/src/core/file_sys/vfs.h +++ b/src/core/file_sys/vfs.h @@ -59,8 +59,7 @@ struct VfsFile : NonCopyable { // Returns the number of bytes (sizeof(T)*number_elements) read successfully. template size_t ReadArray(T* data, size_t number_elements, size_t offset = 0) const { - static_assert(std::is_trivially_copyable::value, - "Data type must be trivially copyable."); + static_assert(std::is_trivially_copyable_v, "Data type must be trivially copyable."); return Read(reinterpret_cast(data), number_elements * sizeof(T), offset); } @@ -69,8 +68,7 @@ struct VfsFile : NonCopyable { // Returns the number of bytes read successfully. template size_t ReadBytes(T* data, size_t size, size_t offset = 0) const { - static_assert(std::is_trivially_copyable::value, - "Data type must be trivially copyable."); + static_assert(std::is_trivially_copyable_v, "Data type must be trivially copyable."); return Read(reinterpret_cast(data), size, offset); } @@ -78,8 +76,7 @@ struct VfsFile : NonCopyable { // Returns the number of bytes read successfully (sizeof(T)). template size_t ReadObject(T* data, size_t offset = 0) const { - static_assert(std::is_trivially_copyable::value, - "Data type must be trivially copyable."); + static_assert(std::is_trivially_copyable_v, "Data type must be trivially copyable."); return Read(reinterpret_cast(data), sizeof(T), offset); } @@ -94,9 +91,7 @@ struct VfsFile : NonCopyable { // Returns the number of bytes (sizeof(T)*number_elements) written successfully. template size_t WriteArray(const T* data, size_t number_elements, size_t offset = 0) { - static_assert(std::is_trivially_copyable::value, - "Data type must be trivially copyable."); - + static_assert(std::is_trivially_copyable_v, "Data type must be trivially copyable."); return Write(data, number_elements * sizeof(T), offset); } @@ -104,8 +99,7 @@ struct VfsFile : NonCopyable { // Returns the number of bytes written successfully. template size_t WriteBytes(const T* data, size_t size, size_t offset = 0) { - static_assert(std::is_trivially_copyable::value, - "Data type must be trivially copyable."); + static_assert(std::is_trivially_copyable_v, "Data type must be trivially copyable."); return Write(reinterpret_cast(data), size, offset); } @@ -113,8 +107,7 @@ struct VfsFile : NonCopyable { // Returns the number of bytes written successfully (sizeof(T)). template size_t WriteObject(const T& data, size_t offset = 0) { - static_assert(std::is_trivially_copyable::value, - "Data type must be trivially copyable."); + static_assert(std::is_trivially_copyable_v, "Data type must be trivially copyable."); return Write(&data, sizeof(T), offset); } From 894b0de0f2cd15655726ae885b44b030711328a3 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 20 Jul 2018 21:51:28 -0400 Subject: [PATCH 3/4] vfs: Make WriteBytes() overload taking a std::vector pass the std::vector by const reference Given the data is intended to be directly written, there's no need to take the std::vector by value and copy the data. --- src/core/file_sys/vfs.cpp | 2 +- src/core/file_sys/vfs.h | 2 +- src/core/file_sys/vfs_offset.cpp | 2 +- src/core/file_sys/vfs_offset.h | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/core/file_sys/vfs.cpp b/src/core/file_sys/vfs.cpp index 16c8ad90b..3f690f12a 100644 --- a/src/core/file_sys/vfs.cpp +++ b/src/core/file_sys/vfs.cpp @@ -42,7 +42,7 @@ bool VfsFile::WriteByte(u8 data, size_t offset) { return Write(&data, 1, offset) == 1; } -size_t VfsFile::WriteBytes(std::vector data, size_t offset) { +size_t VfsFile::WriteBytes(const std::vector& data, size_t offset) { return Write(data.data(), data.size(), offset); } diff --git a/src/core/file_sys/vfs.h b/src/core/file_sys/vfs.h index d108ab1f4..db3c77eac 100644 --- a/src/core/file_sys/vfs.h +++ b/src/core/file_sys/vfs.h @@ -85,7 +85,7 @@ struct VfsFile : NonCopyable { virtual bool WriteByte(u8 data, size_t offset = 0); // Writes a vector of bytes to offset in file and returns the number of bytes successfully // written. - virtual size_t WriteBytes(std::vector data, size_t offset = 0); + virtual size_t WriteBytes(const std::vector& data, size_t offset = 0); // Writes an array of type T, size number_elements to offset in file. // Returns the number of bytes (sizeof(T)*number_elements) written successfully. diff --git a/src/core/file_sys/vfs_offset.cpp b/src/core/file_sys/vfs_offset.cpp index 31fdd9aa1..38ec4e0f9 100644 --- a/src/core/file_sys/vfs_offset.cpp +++ b/src/core/file_sys/vfs_offset.cpp @@ -75,7 +75,7 @@ bool OffsetVfsFile::WriteByte(u8 data, size_t r_offset) { return false; } -size_t OffsetVfsFile::WriteBytes(std::vector data, size_t r_offset) { +size_t OffsetVfsFile::WriteBytes(const std::vector& data, size_t r_offset) { return file->Write(data.data(), TrimToFit(data.size(), r_offset), offset + r_offset); } diff --git a/src/core/file_sys/vfs_offset.h b/src/core/file_sys/vfs_offset.h index 2e16e47eb..ded4827f5 100644 --- a/src/core/file_sys/vfs_offset.h +++ b/src/core/file_sys/vfs_offset.h @@ -28,7 +28,7 @@ struct OffsetVfsFile : public VfsFile { std::vector ReadBytes(size_t size, size_t offset) const override; std::vector ReadAllBytes() const override; bool WriteByte(u8 data, size_t offset) override; - size_t WriteBytes(std::vector data, size_t offset) override; + size_t WriteBytes(const std::vector& data, size_t offset) override; bool Rename(const std::string& name) override; From 3e0727df1b9354952dc7690dddd3a8239bcdadda Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 20 Jul 2018 22:02:53 -0400 Subject: [PATCH 4/4] vfs_offset: Simplify TrimToFit() We can simply use std::clamp() here, instead of using an equivalent with std::max() and std::min(). --- src/core/file_sys/vfs_offset.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/core/file_sys/vfs_offset.cpp b/src/core/file_sys/vfs_offset.cpp index 38ec4e0f9..217e02235 100644 --- a/src/core/file_sys/vfs_offset.cpp +++ b/src/core/file_sys/vfs_offset.cpp @@ -2,6 +2,7 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. +#include #include #include "core/file_sys/vfs_offset.h" @@ -88,7 +89,7 @@ size_t OffsetVfsFile::GetOffset() const { } size_t OffsetVfsFile::TrimToFit(size_t r_size, size_t r_offset) const { - return std::max(std::min(size - r_offset, r_size), 0); + return std::clamp(r_size, size_t{0}, size - r_offset); } } // namespace FileSys