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/1415910
Reviewed-by: Kai Ninomiya <kainino@chromium.org>
Reviewed-by: Geoff Lang <geofflang@chromium.org>
Commit-Queue: Jamie Madill <jmadill@chromium.org>
diff --git a/src/common/system_utils.h b/src/common/system_utils.h
index e3df59a..2079d56 100644
--- a/src/common/system_utils.h
+++ b/src/common/system_utils.h
@@ -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);
diff --git a/src/common/system_utils_linux.cpp b/src/common/system_utils_linux.cpp
index 266a6ed..f7ea49f 100644
--- a/src/common/system_utils_linux.cpp
+++ b/src/common/system_utils_linux.cpp
@@ -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 @@
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";
diff --git a/src/common/system_utils_mac.cpp b/src/common/system_utils_mac.cpp
index b98e3bf..f3d9891 100644
--- a/src/common/system_utils_mac.cpp
+++ b/src/common/system_utils_mac.cpp
@@ -18,11 +18,7 @@
namespace angle
{
-
-namespace
-{
-
-std::string GetExecutablePathImpl()
+std::string GetExecutablePath()
{
std::string result;
@@ -42,29 +38,13 @@
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";
diff --git a/src/common/system_utils_win.cpp b/src/common/system_utils_win.cpp
index e4c1460..7db4485 100644
--- a/src/common/system_utils_win.cpp
+++ b/src/common/system_utils_win.cpp
@@ -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 @@
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";
diff --git a/src/libANGLE/renderer/vulkan/RendererVk.cpp b/src/libANGLE/renderer/vulkan/RendererVk.cpp
index e48893e..f746742 100644
--- a/src/libANGLE/renderer/vulkan/RendererVk.cpp
+++ b/src/libANGLE/renderer/vulkan/RendererVk.cpp
@@ -366,8 +366,8 @@
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.";