/* * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ #include "Utils.hpp" #ifdef _MSC_VER #include #else #include #endif #include #include #include #include namespace OpenVulkano { void Utils::SetThreadName(const std::string& name) { #ifdef _MSC_VER auto thisThread = ::GetCurrentThread(); std::wstring namew(name.begin(), name.end()); SetThreadDescription(thisThread, namew.c_str()); #elif __APPLE__ pthread_setname_np(name.c_str()); #else pthread_setname_np(pthread_self(), name.c_str()); #endif } std::string Utils::GetThreadName() { #ifdef _MSC_VER auto thisThread = ::GetCurrentThread(); PWSTR threadName; GetThreadDescription(thisThread, &threadName); std::wstring_convert> converter; std::string str = converter.to_bytes(threadName); LocalFree(threadName); return str; #else // NOTE(vb): Accoring to linux man page size of this array must be _at least_ 16 bytes... char threadName[64]; pthread_getname_np(pthread_self(), threadName, sizeof(threadName)); std::string str = threadName; return str; #endif } uint64_t Utils::GetThreadId() { #ifdef _MSC_VER return (uint64_t)::GetThreadId(::GetCurrentThread()); #else return (uint64_t)pthread_self(); #endif } Array Utils::ReadFile(const std::string& filePath, bool emptyOnMissing) { std::ifstream file(filePath, std::ios::ate | std::ios::binary); if (!file.is_open()) { if (emptyOnMissing) return {}; throw std::runtime_error("Failed to open file '" + filePath + "'!"); } const size_t fileSize = static_cast(file.tellg()); Array data(fileSize); file.seekg(0); file.read(data.Data(), fileSize); file.close(); return data; } std::string Utils::PrettyBytes(uint64_t bytes) { static const uint64_t TERABYTES = 1024ULL*1024*1024*1024; static const uint64_t GIGABYTES = 1024ULL*1024*1024; static const uint64_t MEGABYTES = 1024*1024; static const uint64_t KILOBYTES = 1024; int TB, GB, MB, KB; TB = bytes / TERABYTES; bytes -= TB * TERABYTES; GB = bytes / GIGABYTES; bytes -= GB * GIGABYTES; MB = bytes / MEGABYTES; bytes -= MB * MEGABYTES; KB = bytes / KILOBYTES; bytes -= KB * KILOBYTES; std::string result; if(TB) { result = std::to_string(TB) + " TB"; } else if(GB) { result = std::to_string(GB) + " GB"; } else if(MB) { result = std::to_string(MB) + " MB"; } else if(KB) { result = std::to_string(KB) + " KB"; } else { result = std::to_string(bytes) + " B"; } return result; } }