diff options
author | Anton Samokhvalov <pg83@yandex.ru> | 2022-02-10 16:45:15 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:15 +0300 |
commit | 72cb13b4aff9bc9cf22e49251bc8fd143f82538f (patch) | |
tree | da2c34829458c7d4e74bdfbdf85dff449e9e7fb8 /util/system/dynlib.cpp | |
parent | 778e51ba091dc39e7b7fcab2b9cf4dbedfb6f2b5 (diff) | |
download | ydb-72cb13b4aff9bc9cf22e49251bc8fd143f82538f.tar.gz |
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 1 of 2.
Diffstat (limited to 'util/system/dynlib.cpp')
-rw-r--r-- | util/system/dynlib.cpp | 224 |
1 files changed, 112 insertions, 112 deletions
diff --git a/util/system/dynlib.cpp b/util/system/dynlib.cpp index 9d2541c25f..d77605723f 100644 --- a/util/system/dynlib.cpp +++ b/util/system/dynlib.cpp @@ -1,138 +1,138 @@ -#include "dynlib.h" - -#include "guard.h" -#include "mutex.h" +#include "dynlib.h" + +#include "guard.h" +#include "mutex.h" #include <util/generic/singleton.h> -#include <util/generic/yexception.h> - -#ifdef _win32_ - #include "winint.h" - - #define DLLOPEN(path, flags) LoadLibrary(path) - #define DLLCLOSE(hndl) FreeLibrary(hndl) - #define DLLSYM(hndl, name) GetProcAddress(hndl, name) -#else - #include <dlfcn.h> - - #ifndef RTLD_GLOBAL - #define RTLD_GLOBAL (0) - #endif - +#include <util/generic/yexception.h> + +#ifdef _win32_ + #include "winint.h" + + #define DLLOPEN(path, flags) LoadLibrary(path) + #define DLLCLOSE(hndl) FreeLibrary(hndl) + #define DLLSYM(hndl, name) GetProcAddress(hndl, name) +#else + #include <dlfcn.h> + + #ifndef RTLD_GLOBAL + #define RTLD_GLOBAL (0) + #endif + using HINSTANCE = void*; - - #define DLLOPEN(path, flags) dlopen(path, flags) - #define DLLCLOSE(hndl) dlclose(hndl) - #define DLLSYM(hndl, name) dlsym(hndl, name) -#endif - + + #define DLLOPEN(path, flags) dlopen(path, flags) + #define DLLCLOSE(hndl) dlclose(hndl) + #define DLLSYM(hndl, name) dlsym(hndl, name) +#endif + inline TString DLLERR() { -#ifdef _unix_ - return dlerror(); -#endif - -#ifdef _win32_ - char* msg = 0; - DWORD cnt = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, +#ifdef _unix_ + return dlerror(); +#endif + +#ifdef _win32_ + char* msg = 0; + DWORD cnt = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (char*)&msg, 0, nullptr); - if (!msg) - return "DLLERR() unknown error"; - while (cnt && isspace(msg[cnt - 1])) + if (!msg) + return "DLLERR() unknown error"; + while (cnt && isspace(msg[cnt - 1])) --cnt; TString err(msg, 0, cnt); - LocalFree(msg); - return err; -#endif -} - -class TDynamicLibrary::TImpl { -private: - inline TImpl(const char* path, int flags) - : Module(DLLOPEN(path, flags)) - , Unloadable(true) - { - (void)flags; - - if (!Module) { + LocalFree(msg); + return err; +#endif +} + +class TDynamicLibrary::TImpl { +private: + inline TImpl(const char* path, int flags) + : Module(DLLOPEN(path, flags)) + , Unloadable(true) + { + (void)flags; + + if (!Module) { ythrow yexception() << DLLERR().data(); - } - } - - class TCreateMutex: public TMutex { - }; - -public: - static inline TImpl* SafeCreate(const char* path, int flags) { - auto guard = Guard(*Singleton<TCreateMutex>()); - - return new TImpl(path, flags); - } + } + } + + class TCreateMutex: public TMutex { + }; + +public: + static inline TImpl* SafeCreate(const char* path, int flags) { + auto guard = Guard(*Singleton<TCreateMutex>()); + + return new TImpl(path, flags); + } inline ~TImpl() { - if (Module && Unloadable) { - DLLCLOSE(Module); - } - } - + if (Module && Unloadable) { + DLLCLOSE(Module); + } + } + inline void* SymOptional(const char* name) noexcept { - return (void*)DLLSYM(Module, name); - } - - inline void* Sym(const char* name) { - void* symbol = SymOptional(name); - + return (void*)DLLSYM(Module, name); + } + + inline void* Sym(const char* name) { + void* symbol = SymOptional(name); + if (symbol == nullptr) { ythrow yexception() << DLLERR().data(); - } - - return symbol; - } - - inline void SetUnloadable(bool unloadable) { - Unloadable = unloadable; - } - -private: - HINSTANCE Module; - bool Unloadable; -}; - + } + + return symbol; + } + + inline void SetUnloadable(bool unloadable) { + Unloadable = unloadable; + } + +private: + HINSTANCE Module; + bool Unloadable; +}; + TDynamicLibrary::TDynamicLibrary() noexcept { -} - +} + TDynamicLibrary::TDynamicLibrary(const TString& path, int flags) { Open(path.data(), flags); -} - +} + TDynamicLibrary::~TDynamicLibrary() = default; - + void TDynamicLibrary::Open(const char* path, int flags) { Impl_.Reset(TImpl::SafeCreate(path, flags)); -} - +} + void TDynamicLibrary::Close() noexcept { - Impl_.Destroy(); -} - + Impl_.Destroy(); +} + void* TDynamicLibrary::SymOptional(const char* name) noexcept { - if (!IsLoaded()) { + if (!IsLoaded()) { return nullptr; - } - - return Impl_->SymOptional(name); -} - -void* TDynamicLibrary::Sym(const char* name) { - if (!IsLoaded()) { + } + + return Impl_->SymOptional(name); +} + +void* TDynamicLibrary::Sym(const char* name) { + if (!IsLoaded()) { ythrow yexception() << "library not loaded"; - } - - return Impl_->Sym(name); -} - + } + + return Impl_->Sym(name); +} + bool TDynamicLibrary::IsLoaded() const noexcept { - return (bool)Impl_.Get(); -} + return (bool)Impl_.Get(); +} void TDynamicLibrary::SetUnloadable(bool unloadable) { - Impl_->SetUnloadable(unloadable); + Impl_->SetUnloadable(unloadable); } |