diff options
| author | Yong He <yonghe@outlook.com> | 2021-03-05 10:58:08 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-03-05 10:58:08 -0800 |
| commit | dc7110858ecdb7c7567de360787b9adc4defa04a (patch) | |
| tree | 16b43018b1544c4d418ca0c68d63c0dc84745c56 /source/core | |
| parent | a5ac4999b4dea546a7ef824669ab1809224b6448 (diff) | |
Cache stdlib when creating global session. (#1736)
* Cache stdlib when creating global session.
* Fix
* Fix
Diffstat (limited to 'source/core')
| -rw-r--r-- | source/core/slang-shared-library.cpp | 58 | ||||
| -rw-r--r-- | source/core/slang-shared-library.h | 7 |
2 files changed, 65 insertions, 0 deletions
diff --git a/source/core/slang-shared-library.cpp b/source/core/slang-shared-library.cpp index 746e14e4c..8d362fd89 100644 --- a/source/core/slang-shared-library.cpp +++ b/source/core/slang-shared-library.cpp @@ -5,6 +5,18 @@ #include "slang-io.h" #include "slang-string-util.h" +#if defined(_WIN32) +#define WIN32_LEAN_AND_MEAN +#include <Windows.h> +#elif defined(__linux__) +#include <dlfcn.h> +#endif +#include <sys/stat.h> +#include <sys/types.h> +#ifndef WIN32 +# include <unistd.h> +#endif + namespace Slang { @@ -87,4 +99,50 @@ void* DefaultSharedLibrary::findSymbolAddressByName(char const* name) return SharedLibrary::findSymbolAddressByName(m_sharedLibraryHandle, name); } + +String SharedLibraryUtils::getSharedLibraryFileName(void* symbolInLib) +{ +#if defined(_WIN32) + HMODULE moduleHandle; + GetModuleHandleExA( + GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, + (LPCSTR)symbolInLib, + &moduleHandle); + const int maxLength = 1024; + wchar_t filenameBuffer[maxLength]; + auto length = GetModuleFileNameW(moduleHandle, filenameBuffer, maxLength); + if (length == maxLength) + { + // Insufficient buffer, return empty. + return String(); + } + return String::fromWString(filenameBuffer); + +#elif defined(__linux__) + Dl_info dllInfo; + if (!dladdr(symbolInLib, &dllInfo)) + { + return String(); + } + return dllInfo.dli_fname; + +#else + return String(); +#endif +} + +uint64_t SharedLibraryUtils::getSharedLibraryTimestamp(void* symbolInLib) +{ + auto fileName = getSharedLibraryFileName(symbolInLib); + if (fileName.getLength() == 0) + return 0; + struct stat result; + if (stat(fileName.getBuffer(), &result) == 0) + { + auto mod_time = result.st_mtime; + return (uint64_t)mod_time; + } + return 0; +} + } diff --git a/source/core/slang-shared-library.h b/source/core/slang-shared-library.h index c29f16289..c33f1f41b 100644 --- a/source/core/slang-shared-library.h +++ b/source/core/slang-shared-library.h @@ -94,6 +94,13 @@ protected: String m_path; }; +class SharedLibraryUtils +{ +public: + static String getSharedLibraryFileName(void* symbolInLib); + static uint64_t getSharedLibraryTimestamp(void* symbolInLib); +}; + } #endif // SLANG_SHARED_LIBRARY_H_INCLUDED |
