summaryrefslogtreecommitdiffstats
path: root/src/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/common')
-rw-r--r--src/common/file_util.cpp449
-rw-r--r--src/common/file_util.h73
-rw-r--r--src/common/scope_exit.h2
-rw-r--r--src/common/telemetry.h4
-rw-r--r--src/common/x64/xbyak_abi.h20
5 files changed, 148 insertions, 400 deletions
diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp
index 18fbfa25b..a286b9341 100644
--- a/src/common/file_util.cpp
+++ b/src/common/file_util.cpp
@@ -3,6 +3,7 @@
// Refer to the license.txt file included.
#include <array>
+#include <filesystem>
#include <limits>
#include <memory>
#include <sstream>
@@ -67,290 +68,128 @@
#include <algorithm>
#include <sys/stat.h>
-#ifndef S_ISDIR
-#define S_ISDIR(m) (((m)&S_IFMT) == S_IFDIR)
-#endif
-
-// This namespace has various generic functions related to files and paths.
-// The code still needs a ton of cleanup.
-// REMEMBER: strdup considered harmful!
namespace Common::FS {
+namespace fs = std::filesystem;
-// Remove any ending forward slashes from directory paths
-// Modifies argument.
-static void StripTailDirSlashes(std::string& fname) {
- if (fname.length() <= 1) {
- return;
- }
-
- std::size_t i = fname.length();
- while (i > 0 && fname[i - 1] == DIR_SEP_CHR) {
- --i;
- }
- fname.resize(i);
+bool Exists(const fs::path& path) {
+ std::error_code ec;
+ return fs::exists(path, ec);
}
-bool Exists(const std::string& filename) {
- struct stat file_info;
-
- std::string copy(filename);
- StripTailDirSlashes(copy);
-
-#ifdef _WIN32
- // Windows needs a slash to identify a driver root
- if (copy.size() != 0 && copy.back() == ':')
- copy += DIR_SEP_CHR;
-
- int result = _wstat64(Common::UTF8ToUTF16W(copy).c_str(), &file_info);
-#else
- int result = stat(copy.c_str(), &file_info);
-#endif
-
- return (result == 0);
+bool IsDirectory(const fs::path& path) {
+ std::error_code ec;
+ return fs::is_directory(path, ec);
}
-bool IsDirectory(const std::string& filename) {
- struct stat file_info;
-
- std::string copy(filename);
- StripTailDirSlashes(copy);
+bool Delete(const fs::path& path) {
+ LOG_TRACE(Common_Filesystem, "file {}", path.string());
-#ifdef _WIN32
- // Windows needs a slash to identify a driver root
- if (copy.size() != 0 && copy.back() == ':')
- copy += DIR_SEP_CHR;
-
- int result = _wstat64(Common::UTF8ToUTF16W(copy).c_str(), &file_info);
-#else
- int result = stat(copy.c_str(), &file_info);
-#endif
-
- if (result < 0) {
- LOG_DEBUG(Common_Filesystem, "stat failed on {}: {}", filename, GetLastErrorMsg());
- return false;
+ // Return true because we care about the file no
+ // being there, not the actual delete.
+ if (!Exists(path)) {
+ LOG_DEBUG(Common_Filesystem, "{} does not exist", path.string());
+ return true;
}
- return S_ISDIR(file_info.st_mode);
+ std::error_code ec;
+ return fs::remove(path, ec);
}
-bool Delete(const std::string& filename) {
- LOG_TRACE(Common_Filesystem, "file {}", filename);
+bool CreateDir(const fs::path& path) {
+ LOG_TRACE(Common_Filesystem, "directory {}", path.string());
- // Return true because we care about the file no
- // being there, not the actual delete.
- if (!Exists(filename)) {
- LOG_DEBUG(Common_Filesystem, "{} does not exist", filename);
+ if (Exists(path)) {
+ LOG_DEBUG(Common_Filesystem, "path exists {}", path.string());
return true;
}
- // We can't delete a directory
- if (IsDirectory(filename)) {
- LOG_ERROR(Common_Filesystem, "Failed: {} is a directory", filename);
- return false;
- }
+ std::error_code ec;
+ const bool success = fs::create_directory(path, ec);
-#ifdef _WIN32
- if (!DeleteFileW(Common::UTF8ToUTF16W(filename).c_str())) {
- LOG_ERROR(Common_Filesystem, "DeleteFile failed on {}: {}", filename, GetLastErrorMsg());
- return false;
- }
-#else
- if (unlink(filename.c_str()) == -1) {
- LOG_ERROR(Common_Filesystem, "unlink failed on {}: {}", filename, GetLastErrorMsg());
+ if (!success) {
+ LOG_ERROR(Common_Filesystem, "Unable to create directory: {}", ec.message());
return false;
}
-#endif
return true;
}
-bool CreateDir(const std::string& path) {
- LOG_TRACE(Common_Filesystem, "directory {}", path);
-#ifdef _WIN32
- if (::CreateDirectoryW(Common::UTF8ToUTF16W(path).c_str(), nullptr))
- return true;
- DWORD error = GetLastError();
- if (error == ERROR_ALREADY_EXISTS) {
- LOG_DEBUG(Common_Filesystem, "CreateDirectory failed on {}: already exists", path);
- return true;
- }
- LOG_ERROR(Common_Filesystem, "CreateDirectory failed on {}: {}", path, error);
- return false;
-#else
- if (mkdir(path.c_str(), 0755) == 0)
- return true;
-
- int err = errno;
+bool CreateDirs(const fs::path& path) {
+ LOG_TRACE(Common_Filesystem, "path {}", path.string());
- if (err == EEXIST) {
- LOG_DEBUG(Common_Filesystem, "mkdir failed on {}: already exists", path);
+ if (Exists(path)) {
+ LOG_DEBUG(Common_Filesystem, "path exists {}", path.string());
return true;
}
- LOG_ERROR(Common_Filesystem, "mkdir failed on {}: {}", path, strerror(err));
- return false;
-#endif
-}
+ std::error_code ec;
+ const bool success = fs::create_directories(path, ec);
-bool CreateFullPath(const std::string& fullPath) {
- int panicCounter = 100;
- LOG_TRACE(Common_Filesystem, "path {}", fullPath);
-
- if (Exists(fullPath)) {
- LOG_DEBUG(Common_Filesystem, "path exists {}", fullPath);
- return true;
+ if (!success) {
+ LOG_ERROR(Common_Filesystem, "Unable to create directories: {}", ec.message());
+ return false;
}
- std::size_t position = 0;
- while (true) {
- // Find next sub path
- position = fullPath.find(DIR_SEP_CHR, position);
+ return true;
+}
- // we're done, yay!
- if (position == fullPath.npos)
- return true;
+bool CreateFullPath(const fs::path& path) {
+ LOG_TRACE(Common_Filesystem, "path {}", path);
- // Include the '/' so the first call is CreateDir("/") rather than CreateDir("")
- std::string const subPath(fullPath.substr(0, position + 1));
- if (!IsDirectory(subPath) && !CreateDir(subPath)) {
- LOG_ERROR(Common, "CreateFullPath: directory creation failed");
- return false;
- }
+ // Removes trailing slashes and turns any '\' into '/'
+ const auto new_path = SanitizePath(path.string(), DirectorySeparator::ForwardSlash);
- // A safety check
- panicCounter--;
- if (panicCounter <= 0) {
- LOG_ERROR(Common, "CreateFullPath: directory structure is too deep");
- return false;
- }
- position++;
+ if (new_path.rfind('.') == std::string::npos) {
+ // The path is a directory
+ return CreateDirs(new_path);
+ } else {
+ // The path is a file
+ // Creates directory preceding the last '/'
+ return CreateDirs(new_path.substr(0, new_path.rfind('/')));
}
}
-bool DeleteDir(const std::string& filename) {
- LOG_TRACE(Common_Filesystem, "directory {}", filename);
+bool Rename(const fs::path& src, const fs::path& dst) {
+ LOG_TRACE(Common_Filesystem, "{} --> {}", src.string(), dst.string());
- // check if a directory
- if (!IsDirectory(filename)) {
- LOG_ERROR(Common_Filesystem, "Not a directory {}", filename);
+ std::error_code ec;
+ fs::rename(src, dst, ec);
+
+ if (ec) {
+ LOG_ERROR(Common_Filesystem, "Unable to rename file from {} to {}: {}", src.string(),
+ dst.string(), ec.message());
return false;
}
-#ifdef _WIN32
- if (::RemoveDirectoryW(Common::UTF8ToUTF16W(filename).c_str()))
- return true;
-#else
- if (rmdir(filename.c_str()) == 0)
- return true;
-#endif
- LOG_ERROR(Common_Filesystem, "failed {}: {}", filename, GetLastErrorMsg());
-
- return false;
-}
-
-bool Rename(const std::string& srcFilename, const std::string& destFilename) {
- LOG_TRACE(Common_Filesystem, "{} --> {}", srcFilename, destFilename);
-#ifdef _WIN32
- if (_wrename(Common::UTF8ToUTF16W(srcFilename).c_str(),
- Common::UTF8ToUTF16W(destFilename).c_str()) == 0)
- return true;
-#else
- if (rename(srcFilename.c_str(), destFilename.c_str()) == 0)
- return true;
-#endif
- LOG_ERROR(Common_Filesystem, "failed {} --> {}: {}", srcFilename, destFilename,
- GetLastErrorMsg());
- return false;
+ return true;
}
-bool Copy(const std::string& srcFilename, const std::string& destFilename) {
- LOG_TRACE(Common_Filesystem, "{} --> {}", srcFilename, destFilename);
-#ifdef _WIN32
- if (CopyFileW(Common::UTF8ToUTF16W(srcFilename).c_str(),
- Common::UTF8ToUTF16W(destFilename).c_str(), FALSE))
- return true;
+bool Copy(const fs::path& src, const fs::path& dst) {
+ LOG_TRACE(Common_Filesystem, "{} --> {}", src.string(), dst.string());
- LOG_ERROR(Common_Filesystem, "failed {} --> {}: {}", srcFilename, destFilename,
- GetLastErrorMsg());
- return false;
-#else
- using CFilePointer = std::unique_ptr<FILE, decltype(&std::fclose)>;
+ std::error_code ec;
+ const bool success = fs::copy_file(src, dst, fs::copy_options::overwrite_existing, ec);
- // Open input file
- CFilePointer input{fopen(srcFilename.c_str(), "rb"), std::fclose};
- if (!input) {
- LOG_ERROR(Common_Filesystem, "opening input failed {} --> {}: {}", srcFilename,
- destFilename, GetLastErrorMsg());
+ if (!success) {
+ LOG_ERROR(Common_Filesystem, "Unable to copy file {} to {}: {}", src.string(), dst.string(),
+ ec.message());
return false;
}
- // open output file
- CFilePointer output{fopen(destFilename.c_str(), "wb"), std::fclose};
- if (!output) {
- LOG_ERROR(Common_Filesystem, "opening output failed {} --> {}: {}", srcFilename,
- destFilename, GetLastErrorMsg());
- return false;
- }
-
- // copy loop
- std::array<char, 1024> buffer;
- while (!feof(input.get())) {
- // read input
- std::size_t rnum = fread(buffer.data(), sizeof(char), buffer.size(), input.get());
- if (rnum != buffer.size()) {
- if (ferror(input.get()) != 0) {
- LOG_ERROR(Common_Filesystem, "failed reading from source, {} --> {}: {}",
- srcFilename, destFilename, GetLastErrorMsg());
- return false;
- }
- }
-
- // write output
- std::size_t wnum = fwrite(buffer.data(), sizeof(char), rnum, output.get());
- if (wnum != rnum) {
- LOG_ERROR(Common_Filesystem, "failed writing to output, {} --> {}: {}", srcFilename,
- destFilename, GetLastErrorMsg());
- return false;
- }
- }
-
return true;
-#endif
}
-u64 GetSize(const std::string& filename) {
- if (!Exists(filename)) {
- LOG_ERROR(Common_Filesystem, "failed {}: No such file", filename);
- return 0;
- }
+u64 GetSize(const fs::path& path) {
+ std::error_code ec;
+ const auto size = fs::file_size(path, ec);
- if (IsDirectory(filename)) {
- LOG_ERROR(Common_Filesystem, "failed {}: is a directory", filename);
+ if (ec) {
+ LOG_ERROR(Common_Filesystem, "Unable to retrieve file size ({}): {}", path.string(),
+ ec.message());
return 0;
}
- struct stat buf;
-#ifdef _WIN32
- if (_wstat64(Common::UTF8ToUTF16W(filename).c_str(), &buf) == 0)
-#else
- if (stat(filename.c_str(), &buf) == 0)
-#endif
- {
- LOG_TRACE(Common_Filesystem, "{}: {}", filename, buf.st_size);
- return buf.st_size;
- }
-
- LOG_ERROR(Common_Filesystem, "Stat failed {}: {}", filename, GetLastErrorMsg());
- return 0;
-}
-
-u64 GetSize(const int fd) {
- struct stat buf;
- if (fstat(fd, &buf) != 0) {
- LOG_ERROR(Common_Filesystem, "GetSize: stat failed {}: {}", fd, GetLastErrorMsg());
- return 0;
- }
- return buf.st_size;
+ return size;
}
u64 GetSize(FILE* f) {
@@ -400,7 +239,7 @@ bool ForeachDirectoryEntry(u64* num_entries_out, const std::string& directory,
}
// windows loop
do {
- const std::string virtual_name(Common::UTF16ToUTF8(ffd.cFileName));
+ const std::string virtual_name = std::filesystem::path(ffd.cFileName).string();
#else
DIR* dirp = opendir(directory.c_str());
if (!dirp)
@@ -438,132 +277,58 @@ bool ForeachDirectoryEntry(u64* num_entries_out, const std::string& directory,
return true;
}
-u64 ScanDirectoryTree(const std::string& directory, FSTEntry& parent_entry,
- unsigned int recursion) {
- const auto callback = [recursion, &parent_entry](u64* num_entries_out,
- const std::string& directory,
- const std::string& virtual_name) -> bool {
- FSTEntry entry;
- entry.virtualName = virtual_name;
- entry.physicalName = directory + DIR_SEP + virtual_name;
-
- if (IsDirectory(entry.physicalName)) {
- entry.isDirectory = true;
- // is a directory, lets go inside if we didn't recurse to often
- if (recursion > 0) {
- entry.size = ScanDirectoryTree(entry.physicalName, entry, recursion - 1);
- *num_entries_out += entry.size;
- } else {
- entry.size = 0;
- }
- } else { // is a file
- entry.isDirectory = false;
- entry.size = GetSize(entry.physicalName);
- }
- (*num_entries_out)++;
-
- // Push into the tree
- parent_entry.children.push_back(std::move(entry));
- return true;
- };
-
- u64 num_entries;
- return ForeachDirectoryEntry(&num_entries, directory, callback) ? num_entries : 0;
-}
-
-bool DeleteDirRecursively(const std::string& directory, unsigned int recursion) {
- const auto callback = [recursion](u64*, const std::string& directory,
- const std::string& virtual_name) {
- const std::string new_path = directory + DIR_SEP_CHR + virtual_name;
+bool DeleteDirRecursively(const fs::path& path) {
+ std::error_code ec;
+ fs::remove_all(path, ec);
- if (IsDirectory(new_path)) {
- if (recursion == 0) {
- return false;
- }
- return DeleteDirRecursively(new_path, recursion - 1);
- }
- return Delete(new_path);
- };
-
- if (!ForeachDirectoryEntry(nullptr, directory, callback))
+ if (ec) {
+ LOG_ERROR(Common_Filesystem, "Unable to completely delete directory {}: {}", path.string(),
+ ec.message());
return false;
+ }
- // Delete the outermost directory
- DeleteDir(directory);
return true;
}
-void CopyDir([[maybe_unused]] const std::string& source_path,
- [[maybe_unused]] const std::string& dest_path) {
-#ifndef _WIN32
- if (source_path == dest_path) {
- return;
- }
- if (!Exists(source_path)) {
- return;
- }
- if (!Exists(dest_path)) {
- CreateFullPath(dest_path);
- }
+void CopyDir(const fs::path& src, const fs::path& dst) {
+ constexpr auto copy_flags = fs::copy_options::skip_existing | fs::copy_options::recursive;
+
+ std::error_code ec;
+ fs::copy(src, dst, copy_flags, ec);
- DIR* dirp = opendir(source_path.c_str());
- if (!dirp) {
+ if (ec) {
+ LOG_ERROR(Common_Filesystem, "Error copying directory {} to {}: {}", src.string(),
+ dst.string(), ec.message());
return;
}
- while (struct dirent* result = readdir(dirp)) {
- const std::string virtualName(result->d_name);
- // check for "." and ".."
- if (((virtualName[0] == '.') && (virtualName[1] == '\0')) ||
- ((virtualName[0] == '.') && (virtualName[1] == '.') && (virtualName[2] == '\0'))) {
- continue;
- }
-
- std::string source, dest;
- source = source_path + virtualName;
- dest = dest_path + virtualName;
- if (IsDirectory(source)) {
- source += '/';
- dest += '/';
- if (!Exists(dest)) {
- CreateFullPath(dest);
- }
- CopyDir(source, dest);
- } else if (!Exists(dest)) {
- Copy(source, dest);
- }
- }
- closedir(dirp);
-#endif
+ LOG_TRACE(Common_Filesystem, "Successfully copied directory.");
}
-std::optional<std::string> GetCurrentDir() {
-// Get the current working directory (getcwd uses malloc)
-#ifdef _WIN32
- wchar_t* dir = _wgetcwd(nullptr, 0);
- if (!dir) {
-#else
- char* dir = getcwd(nullptr, 0);
- if (!dir) {
-#endif
- LOG_ERROR(Common_Filesystem, "GetCurrentDirectory failed: {}", GetLastErrorMsg());
+std::optional<fs::path> GetCurrentDir() {
+ std::error_code ec;
+ auto path = fs::current_path(ec);
+
+ if (ec) {
+ LOG_ERROR(Common_Filesystem, "Unable to retrieve current working directory: {}",
+ ec.message());
return std::nullopt;
}
-#ifdef _WIN32
- std::string strDir = Common::UTF16ToUTF8(dir);
-#else
- std::string strDir = dir;
-#endif
- free(dir);
- return strDir;
+
+ return {std::move(path)};
}
-bool SetCurrentDir(const std::string& directory) {
-#ifdef _WIN32
- return _wchdir(Common::UTF8ToUTF16W(directory).c_str()) == 0;
-#else
- return chdir(directory.c_str()) == 0;
-#endif
+bool SetCurrentDir(const fs::path& path) {
+ std::error_code ec;
+ fs::current_path(path, ec);
+
+ if (ec) {
+ LOG_ERROR(Common_Filesystem, "Unable to set {} as working directory: {}", path.string(),
+ ec.message());
+ return false;
+ }
+
+ return true;
}
#if defined(__APPLE__)
diff --git a/src/common/file_util.h b/src/common/file_util.h
index 840cde2a6..cf006cc9d 100644
--- a/src/common/file_util.h
+++ b/src/common/file_util.h
@@ -6,6 +6,7 @@
#include <array>
#include <cstdio>
+#include <filesystem>
#include <fstream>
#include <functional>
#include <limits>
@@ -38,48 +39,40 @@ enum class UserPath {
UserDir,
};
-// FileSystem tree node/
-struct FSTEntry {
- bool isDirectory;
- u64 size; // file length or number of entries from children
- std::string physicalName; // name on disk
- std::string virtualName; // name in FST names table
- std::vector<FSTEntry> children;
-};
-
-// Returns true if file filename exists
-[[nodiscard]] bool Exists(const std::string& filename);
+// Returns true if the path exists
+[[nodiscard]] bool Exists(const std::filesystem::path& path);
-// Returns true if filename is a directory
-[[nodiscard]] bool IsDirectory(const std::string& filename);
+// Returns true if path is a directory
+[[nodiscard]] bool IsDirectory(const std::filesystem::path& path);
// Returns the size of filename (64bit)
-[[nodiscard]] u64 GetSize(const std::string& filename);
-
-// Overloaded GetSize, accepts file descriptor
-[[nodiscard]] u64 GetSize(int fd);
+[[nodiscard]] u64 GetSize(const std::filesystem::path& path);
// Overloaded GetSize, accepts FILE*
[[nodiscard]] u64 GetSize(FILE* f);
// Returns true if successful, or path already exists.
-bool CreateDir(const std::string& filename);
+bool CreateDir(const std::filesystem::path& path);
-// Creates the full path of fullPath returns true on success
-bool CreateFullPath(const std::string& fullPath);
+// Create all directories in path
+// Returns true if successful, or path already exists.
+[[nodiscard("Directory creation can fail and must be tested")]] bool CreateDirs(
+ const std::filesystem::path& path);
-// Deletes a given filename, return true on success
-// Doesn't supports deleting a directory
-bool Delete(const std::string& filename);
+// Creates directories in path. Returns true on success.
+[[deprecated("This function is deprecated, use CreateDirs")]] bool CreateFullPath(
+ const std::filesystem::path& path);
-// Deletes a directory filename, returns true on success
-bool DeleteDir(const std::string& filename);
+// Deletes a given file at the path.
+// This will also delete empty directories.
+// Return true on success
+bool Delete(const std::filesystem::path& path);
-// renames file srcFilename to destFilename, returns true on success
-bool Rename(const std::string& srcFilename, const std::string& destFilename);
+// Renames file src to dst, returns true on success
+bool Rename(const std::filesystem::path& src, const std::filesystem::path& dst);
-// copies file srcFilename to destFilename, returns true on success
-bool Copy(const std::string& srcFilename, const std::string& destFilename);
+// copies file src to dst, returns true on success
+bool Copy(const std::filesystem::path& src, const std::filesystem::path& dst);
// creates an empty file filename, returns true on success
bool CreateEmptyFile(const std::string& filename);
@@ -106,27 +99,17 @@ using DirectoryEntryCallable = std::function<bool(
bool ForeachDirectoryEntry(u64* num_entries_out, const std::string& directory,
DirectoryEntryCallable callback);
-/**
- * Scans the directory tree, storing the results.
- * @param directory the parent directory to start scanning from
- * @param parent_entry FSTEntry where the filesystem tree results will be stored.
- * @param recursion Number of children directories to read before giving up.
- * @return the total number of files/directories found
- */
-u64 ScanDirectoryTree(const std::string& directory, FSTEntry& parent_entry,
- unsigned int recursion = 0);
-
-// deletes the given directory and anything under it. Returns true on success.
-bool DeleteDirRecursively(const std::string& directory, unsigned int recursion = 256);
+// Deletes the given path and anything under it. Returns true on success.
+bool DeleteDirRecursively(const std::filesystem::path& path);
// Returns the current directory
-[[nodiscard]] std::optional<std::string> GetCurrentDir();
+[[nodiscard]] std::optional<std::filesystem::path> GetCurrentDir();
// Create directory and copy contents (does not overwrite existing files)
-void CopyDir(const std::string& source_path, const std::string& dest_path);
+void CopyDir(const std::filesystem::path& src, const std::filesystem::path& dst);
-// Set the current directory to given directory
-bool SetCurrentDir(const std::string& directory);
+// Set the current directory to given path
+bool SetCurrentDir(const std::filesystem::path& path);
// Returns a pointer to a string with a yuzu data dir in the user's home
// directory. To be used in "multi-user" mode (that is, installed).
diff --git a/src/common/scope_exit.h b/src/common/scope_exit.h
index 68ef5f197..fa46cb394 100644
--- a/src/common/scope_exit.h
+++ b/src/common/scope_exit.h
@@ -10,7 +10,7 @@
namespace detail {
template <typename Func>
struct ScopeExitHelper {
- explicit ScopeExitHelper(Func&& func) : func(std::move(func)) {}
+ explicit ScopeExitHelper(Func&& func_) : func(std::move(func_)) {}
~ScopeExitHelper() {
if (active) {
func();
diff --git a/src/common/telemetry.h b/src/common/telemetry.h
index a50c5d1de..49186e848 100644
--- a/src/common/telemetry.h
+++ b/src/common/telemetry.h
@@ -52,8 +52,8 @@ public:
template <typename T>
class Field : public FieldInterface {
public:
- Field(FieldType type, std::string name, T value)
- : name(std::move(name)), type(type), value(std::move(value)) {}
+ Field(FieldType type_, std::string name_, T value_)
+ : name(std::move(name_)), type(type_), value(std::move(value_)) {}
Field(const Field&) = default;
Field& operator=(const Field&) = default;
diff --git a/src/common/x64/xbyak_abi.h b/src/common/x64/xbyak_abi.h
index 26e4bfda5..c2c9b6134 100644
--- a/src/common/x64/xbyak_abi.h
+++ b/src/common/x64/xbyak_abi.h
@@ -11,25 +11,25 @@
namespace Common::X64 {
-constexpr std::size_t RegToIndex(const Xbyak::Reg& reg) {
+constexpr size_t RegToIndex(const Xbyak::Reg& reg) {
using Kind = Xbyak::Reg::Kind;
ASSERT_MSG((reg.getKind() & (Kind::REG | Kind::XMM)) != 0,
"RegSet only support GPRs and XMM registers.");
ASSERT_MSG(reg.getIdx() < 16, "RegSet only supports XXM0-15.");
- return reg.getIdx() + (reg.getKind() == Kind::REG ? 0 : 16);
+ return static_cast<size_t>(reg.getIdx()) + (reg.getKind() == Kind::REG ? 0 : 16);
}
-constexpr Xbyak::Reg64 IndexToReg64(std::size_t reg_index) {
+constexpr Xbyak::Reg64 IndexToReg64(size_t reg_index) {
ASSERT(reg_index < 16);
return Xbyak::Reg64(static_cast<int>(reg_index));
}
-constexpr Xbyak::Xmm IndexToXmm(std::size_t reg_index) {
+constexpr Xbyak::Xmm IndexToXmm(size_t reg_index) {
ASSERT(reg_index >= 16 && reg_index < 32);
return Xbyak::Xmm(static_cast<int>(reg_index - 16));
}
-constexpr Xbyak::Reg IndexToReg(std::size_t reg_index) {
+constexpr Xbyak::Reg IndexToReg(size_t reg_index) {
if (reg_index < 16) {
return IndexToReg64(reg_index);
} else {
@@ -182,7 +182,7 @@ inline size_t ABI_PushRegistersAndAdjustStack(Xbyak::CodeGenerator& code, std::b
size_t rsp_alignment, size_t needed_frame_size = 0) {
auto frame_info = ABI_CalculateFrameSize(regs, rsp_alignment, needed_frame_size);
- for (std::size_t i = 0; i < regs.size(); ++i) {
+ for (size_t i = 0; i < regs.size(); ++i) {
if (regs[i] && ABI_ALL_GPRS[i]) {
code.push(IndexToReg64(i));
}
@@ -192,7 +192,7 @@ inline size_t ABI_PushRegistersAndAdjustStack(Xbyak::CodeGenerator& code, std::b
code.sub(code.rsp, frame_info.subtraction);
}
- for (std::size_t i = 0; i < regs.size(); ++i) {
+ for (size_t i = 0; i < regs.size(); ++i) {
if (regs[i] && ABI_ALL_XMMS[i]) {
code.movaps(code.xword[code.rsp + frame_info.xmm_offset], IndexToXmm(i));
frame_info.xmm_offset += 0x10;
@@ -206,7 +206,7 @@ inline void ABI_PopRegistersAndAdjustStack(Xbyak::CodeGenerator& code, std::bits
size_t rsp_alignment, size_t needed_frame_size = 0) {
auto frame_info = ABI_CalculateFrameSize(regs, rsp_alignment, needed_frame_size);
- for (std::size_t i = 0; i < regs.size(); ++i) {
+ for (size_t i = 0; i < regs.size(); ++i) {
if (regs[i] && ABI_ALL_XMMS[i]) {
code.movaps(IndexToXmm(i), code.xword[code.rsp + frame_info.xmm_offset]);
frame_info.xmm_offset += 0x10;
@@ -218,8 +218,8 @@ inline void ABI_PopRegistersAndAdjustStack(Xbyak::CodeGenerator& code, std::bits
}
// GPRs need to be popped in reverse order
- for (std::size_t j = 0; j < regs.size(); ++j) {
- const std::size_t i = regs.size() - j - 1;
+ for (size_t j = 0; j < regs.size(); ++j) {
+ const size_t i = regs.size() - j - 1;
if (regs[i] && ABI_ALL_GPRS[i]) {
code.pop(IndexToReg64(i));
}