Commit bab0302b by Jamie Madill Committed by Commit Bot

Make path utils return std::string.

This avoids potential issues with returning local variables. It also solves potential threading conflicts by returning the string value instead of a pointer. The code should be optimized with RVO with a modern c++ compiler. Bug: angleproject:2601 Change-Id: I8a01702d2675a17dd060f27920105efab0c49454 Reviewed-on: https://chromium-review.googlesource.com/c/1415910Reviewed-by: 's avatarKai Ninomiya <kainino@chromium.org> Reviewed-by: 's avatarGeoff Lang <geofflang@chromium.org> Commit-Queue: Jamie Madill <jmadill@chromium.org>
parent 465d6090
...@@ -14,8 +14,8 @@ ...@@ -14,8 +14,8 @@
namespace angle namespace angle
{ {
const char *GetExecutablePath(); std::string GetExecutablePath();
const char *GetExecutableDirectory(); std::string GetExecutableDirectory();
const char *GetSharedLibraryExtension(); const char *GetSharedLibraryExtension();
Optional<std::string> GetCWD(); Optional<std::string> GetCWD();
bool SetCWD(const char *dirName); bool SetCWD(const char *dirName);
......
...@@ -17,11 +17,7 @@ ...@@ -17,11 +17,7 @@
namespace angle namespace angle
{ {
std::string GetExecutablePath()
namespace
{
std::string GetExecutablePathImpl()
{ {
// We cannot use lstat to get the size of /proc/self/exe as it always returns 0 // We cannot use lstat to get the size of /proc/self/exe as it always returns 0
// so we just use a big buffer and hope the path fits in it. // so we just use a big buffer and hope the path fits in it.
...@@ -37,29 +33,13 @@ std::string GetExecutablePathImpl() ...@@ -37,29 +33,13 @@ std::string GetExecutablePathImpl()
return path; return path;
} }
std::string GetExecutableDirectoryImpl() std::string GetExecutableDirectory()
{ {
std::string executablePath = GetExecutablePath(); std::string executablePath = GetExecutablePath();
size_t lastPathSepLoc = executablePath.find_last_of("/"); size_t lastPathSepLoc = executablePath.find_last_of("/");
return (lastPathSepLoc != std::string::npos) ? executablePath.substr(0, lastPathSepLoc) : ""; return (lastPathSepLoc != std::string::npos) ? executablePath.substr(0, lastPathSepLoc) : "";
} }
} // anonymous namespace
const char *GetExecutablePath()
{
// TODO(jmadill): Make global static string thread-safe.
const static std::string &exePath = GetExecutablePathImpl();
return exePath.c_str();
}
const char *GetExecutableDirectory()
{
// TODO(jmadill): Make global static string thread-safe.
const static std::string &exeDir = GetExecutableDirectoryImpl();
return exeDir.c_str();
}
const char *GetSharedLibraryExtension() const char *GetSharedLibraryExtension()
{ {
return "so"; return "so";
......
...@@ -18,11 +18,7 @@ ...@@ -18,11 +18,7 @@
namespace angle namespace angle
{ {
std::string GetExecutablePath()
namespace
{
std::string GetExecutablePathImpl()
{ {
std::string result; std::string result;
...@@ -42,29 +38,13 @@ std::string GetExecutablePathImpl() ...@@ -42,29 +38,13 @@ std::string GetExecutablePathImpl()
return buffer.data(); return buffer.data();
} }
std::string GetExecutableDirectoryImpl() std::string GetExecutableDirectory()
{ {
std::string executablePath = GetExecutablePath(); std::string executablePath = GetExecutablePath();
size_t lastPathSepLoc = executablePath.find_last_of("/"); size_t lastPathSepLoc = executablePath.find_last_of("/");
return (lastPathSepLoc != std::string::npos) ? executablePath.substr(0, lastPathSepLoc) : ""; return (lastPathSepLoc != std::string::npos) ? executablePath.substr(0, lastPathSepLoc) : "";
} }
} // anonymous namespace
const char *GetExecutablePath()
{
// TODO(jmadill): Make global static string thread-safe.
const static std::string &exePath = GetExecutablePathImpl();
return exePath.c_str();
}
const char *GetExecutableDirectory()
{
// TODO(jmadill): Make global static string thread-safe.
const static std::string &exeDir = GetExecutableDirectoryImpl();
return exeDir.c_str();
}
const char *GetSharedLibraryExtension() const char *GetSharedLibraryExtension()
{ {
return "dylib"; return "dylib";
......
...@@ -15,11 +15,7 @@ ...@@ -15,11 +15,7 @@
namespace angle namespace angle
{ {
std::string GetExecutablePath()
namespace
{
std::string GetExecutablePathImpl()
{ {
std::array<char, MAX_PATH> executableFileBuf; std::array<char, MAX_PATH> executableFileBuf;
DWORD executablePathLen = GetModuleFileNameA(nullptr, executableFileBuf.data(), DWORD executablePathLen = GetModuleFileNameA(nullptr, executableFileBuf.data(),
...@@ -27,29 +23,13 @@ std::string GetExecutablePathImpl() ...@@ -27,29 +23,13 @@ std::string GetExecutablePathImpl()
return (executablePathLen > 0 ? std::string(executableFileBuf.data()) : ""); return (executablePathLen > 0 ? std::string(executableFileBuf.data()) : "");
} }
std::string GetExecutableDirectoryImpl() std::string GetExecutableDirectory()
{ {
std::string executablePath = GetExecutablePath(); std::string executablePath = GetExecutablePath();
size_t lastPathSepLoc = executablePath.find_last_of("\\/"); size_t lastPathSepLoc = executablePath.find_last_of("\\/");
return (lastPathSepLoc != std::string::npos) ? executablePath.substr(0, lastPathSepLoc) : ""; return (lastPathSepLoc != std::string::npos) ? executablePath.substr(0, lastPathSepLoc) : "";
} }
} // anonymous namespace
const char *GetExecutablePath()
{
// TODO(jmadill): Make global static string thread-safe.
const static std::string &exePath = GetExecutablePathImpl();
return exePath.c_str();
}
const char *GetExecutableDirectory()
{
// TODO(jmadill): Make global static string thread-safe.
const static std::string &exeDir = GetExecutableDirectoryImpl();
return exeDir.c_str();
}
const char *GetSharedLibraryExtension() const char *GetSharedLibraryExtension()
{ {
return "dll"; return "dll";
......
...@@ -366,8 +366,8 @@ class ScopedVkLoaderEnvironment : angle::NonCopyable ...@@ -366,8 +366,8 @@ class ScopedVkLoaderEnvironment : angle::NonCopyable
else else
{ {
mPreviousCWD = cwd.value(); mPreviousCWD = cwd.value();
const char *exeDir = angle::GetExecutableDirectory(); std::string exeDir = angle::GetExecutableDirectory();
mChangedCWD = angle::SetCWD(exeDir); mChangedCWD = angle::SetCWD(exeDir.c_str());
if (!mChangedCWD) if (!mChangedCWD)
{ {
ERR() << "Error setting CWD for Vulkan layers init."; ERR() << "Error setting CWD for Vulkan layers init.";
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment