dynlib.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. }
  32. while (cnt && isspace(msg[cnt - 1])) {
  33. --cnt;
  34. }
  35. TString err(msg, 0, cnt);
  36. LocalFree(msg);
  37. return err;
  38. #endif
  39. }
  40. class TDynamicLibrary::TImpl {
  41. private:
  42. inline TImpl(const char* path, int flags)
  43. : Module(DLLOPEN(path, flags))
  44. , Unloadable(true)
  45. {
  46. (void)flags;
  47. if (!Module) {
  48. ythrow yexception() << DLLERR().data();
  49. }
  50. }
  51. class TCreateMutex: public TMutex {
  52. };
  53. public:
  54. static inline TImpl* SafeCreate(const char* path, int flags) {
  55. auto guard = Guard(*Singleton<TCreateMutex>());
  56. return new TImpl(path, flags);
  57. }
  58. inline ~TImpl() {
  59. if (Module && Unloadable) {
  60. DLLCLOSE(Module);
  61. }
  62. }
  63. inline void* SymOptional(const char* name) noexcept {
  64. return (void*)DLLSYM(Module, name);
  65. }
  66. inline void* Sym(const char* name) {
  67. void* symbol = SymOptional(name);
  68. if (symbol == nullptr) {
  69. ythrow yexception() << DLLERR().data();
  70. }
  71. return symbol;
  72. }
  73. inline void SetUnloadable(bool unloadable) {
  74. Unloadable = unloadable;
  75. }
  76. private:
  77. HINSTANCE Module;
  78. bool Unloadable;
  79. };
  80. TDynamicLibrary::TDynamicLibrary() noexcept {
  81. }
  82. TDynamicLibrary::TDynamicLibrary(const TString& path, int flags) {
  83. Open(path.data(), flags);
  84. }
  85. TDynamicLibrary::~TDynamicLibrary() = default;
  86. void TDynamicLibrary::Open(const char* path, int flags) {
  87. Impl_.Reset(TImpl::SafeCreate(path, flags));
  88. }
  89. void TDynamicLibrary::Close() noexcept {
  90. Impl_.Destroy();
  91. }
  92. void* TDynamicLibrary::SymOptional(const char* name) noexcept {
  93. if (!IsLoaded()) {
  94. return nullptr;
  95. }
  96. return Impl_->SymOptional(name);
  97. }
  98. void* TDynamicLibrary::Sym(const char* name) {
  99. if (!IsLoaded()) {
  100. ythrow yexception() << "library not loaded";
  101. }
  102. return Impl_->Sym(name);
  103. }
  104. bool TDynamicLibrary::IsLoaded() const noexcept {
  105. return (bool)Impl_.Get();
  106. }
  107. void TDynamicLibrary::SetUnloadable(bool unloadable) {
  108. Impl_->SetUnloadable(unloadable);
  109. }