From dc7110858ecdb7c7567de360787b9adc4defa04a Mon Sep 17 00:00:00 2001 From: Yong He Date: Fri, 5 Mar 2021 10:58:08 -0800 Subject: Cache stdlib when creating global session. (#1736) * Cache stdlib when creating global session. * Fix * Fix --- source/core/slang-shared-library.cpp | 58 ++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) (limited to 'source/core/slang-shared-library.cpp') 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 +#elif defined(__linux__) +#include +#endif +#include +#include +#ifndef WIN32 +# include +#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; +} + } -- cgit v1.2.3