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 @@
namespace angle
{
const char *GetExecutablePath();
const char *GetExecutableDirectory();
std::string GetExecutablePath();
std::string GetExecutableDirectory();
const char *GetSharedLibraryExtension();
Optional<std::string> GetCWD();
bool SetCWD(const char *dirName);
......
......@@ -17,11 +17,7 @@
namespace angle
{
namespace
{
std::string GetExecutablePathImpl()
std::string GetExecutablePath()
{
// 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.
......@@ -37,29 +33,13 @@ std::string GetExecutablePathImpl()
return path;
}
std::string GetExecutableDirectoryImpl()
std::string GetExecutableDirectory()
{
std::string executablePath = GetExecutablePath();
size_t lastPathSepLoc = executablePath.find_last_of("/");
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()
{
return "so";
......
......@@ -18,11 +18,7 @@
namespace angle
{
namespace
{
std::string GetExecutablePathImpl()
std::string GetExecutablePath()
{
std::string result;
......@@ -42,29 +38,13 @@ std::string GetExecutablePathImpl()
return buffer.data();
}
std::string GetExecutableDirectoryImpl()
std::string GetExecutableDirectory()
{
std::string executablePath = GetExecutablePath();
size_t lastPathSepLoc = executablePath.find_last_of("/");
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()
{
return "dylib";
......
......@@ -15,11 +15,7 @@
namespace angle
{
namespace
{
std::string GetExecutablePathImpl()
std::string GetExecutablePath()
{
std::array<char, MAX_PATH> executableFileBuf;
DWORD executablePathLen = GetModuleFileNameA(nullptr, executableFileBuf.data(),
......@@ -27,29 +23,13 @@ std::string GetExecutablePathImpl()
return (executablePathLen > 0 ? std::string(executableFileBuf.data()) : "");
}
std::string GetExecutableDirectoryImpl()
std::string GetExecutableDirectory()
{
std::string executablePath = GetExecutablePath();
size_t lastPathSepLoc = executablePath.find_last_of("\\/");
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()
{
return "dll";
......
......@@ -366,8 +366,8 @@ class ScopedVkLoaderEnvironment : angle::NonCopyable
else
{
mPreviousCWD = cwd.value();
const char *exeDir = angle::GetExecutableDirectory();
mChangedCWD = angle::SetCWD(exeDir);
std::string exeDir = angle::GetExecutableDirectory();
mChangedCWD = angle::SetCWD(exeDir.c_str());
if (!mChangedCWD)
{
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