dynlib.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include "dynlib.h"
  2. #include "guard.h"
  3. #include "mutex.h"
  4. #include <util/generic/singleton.h>
  5. #include <util/generic/yexception.h>
  6. #ifdef _win32_
  7. #include "winint.h"
  8. #define DLLOPEN(path, flags) LoadLibrary(path)
  9. #define DLLCLOSE(hndl) FreeLibrary(hndl)
  10. #define DLLSYM(hndl, name) GetProcAddress(hndl, name)
  11. #else
  12. #include <dlfcn.h>
  13. #ifndef RTLD_GLOBAL
  14. #define RTLD_GLOBAL (0)
  15. #endif
  16. using HINSTANCE = void*;
  17. #define DLLOPEN(path, flags) dlopen(path, flags)
  18. #define DLLCLOSE(hndl) dlclose(hndl)
  19. #define DLLSYM(hndl, name) dlsym(hndl, name)
  20. #endif
  21. inline TString DLLERR() {
  22. #ifdef _unix_
  23. return dlerror();
  24. #endif
  25. #ifdef _win32_
  26. char* msg = 0;
  27. DWORD cnt = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  28. nullptr, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (char*)&msg, 0, nullptr);
  29. if (!msg)
  30. return "DLLERR() unknown error";
  31. while (cnt && isspace(msg[cnt - 1]))
  32. --cnt;
  33. TString err(msg, 0, cnt);
  34. LocalFree(msg);
  35. return err;
  36. #endif
  37. }
  38. class TDynamicLibrary::TImpl {
  39. private:
  40. inline TImpl(const char* path, int flags)
  41. : Module(DLLOPEN(path, flags))
  42. , Unloadable(true)
  43. {
  44. (void)flags;
  45. if (!Module) {
  46. ythrow yexception() << DLLERR().data();
  47. }
  48. }
  49. class TCreateMutex: public TMutex {
  50. };
  51. public:
  52. static inline TImpl* SafeCreate(const char* path, int flags) {
  53. auto guard = Guard(*Singleton<TCreateMutex>());
  54. return new TImpl(path, flags);
  55. }
  56. inline ~TImpl() {
  57. if (Module && Unloadable) {
  58. DLLCLOSE(Module);
  59. }
  60. }
  61. inline void* SymOptional(const char* name) noexcept {
  62. return (void*)DLLSYM(Module, name);
  63. }
  64. inline void* Sym(const char* name) {
  65. void* symbol = SymOptional(name);
  66. if (symbol == nullptr) {
  67. ythrow yexception() << DLLERR().data();
  68. }
  69. return symbol;
  70. }
  71. inline void SetUnloadable(bool unloadable) {
  72. Unloadable = unloadable;
  73. }
  74. private:
  75. HINSTANCE Module;
  76. bool Unloadable;
  77. };
  78. TDynamicLibrary::TDynamicLibrary() noexcept {
  79. }
  80. TDynamicLibrary::TDynamicLibrary(const TString& path, int flags) {
  81. Open(path.data(), flags);
  82. }
  83. TDynamicLibrary::~TDynamicLibrary() = default;
  84. void TDynamicLibrary::Open(const char* path, int flags) {
  85. Impl_.Reset(TImpl::SafeCreate(path, flags));
  86. }
  87. void TDynamicLibrary::Close() noexcept {
  88. Impl_.Destroy();
  89. }
  90. void* TDynamicLibrary::SymOptional(const char* name) noexcept {
  91. if (!IsLoaded()) {
  92. return nullptr;
  93. }
  94. return Impl_->SymOptional(name);
  95. }
  96. void* TDynamicLibrary::Sym(const char* name) {
  97. if (!IsLoaded()) {
  98. ythrow yexception() << "library not loaded";
  99. }
  100. return Impl_->Sym(name);
  101. }
  102. bool TDynamicLibrary::IsLoaded() const noexcept {
  103. return (bool)Impl_.Get();
  104. }
  105. void TDynamicLibrary::SetUnloadable(bool unloadable) {
  106. Impl_->SetUnloadable(unloadable);
  107. }