Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
3.2KB

  1. /*
  2. * Carla library utils
  3. * Copyright (C) 2011-2014 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #ifndef CARLA_LIB_UTILS_HPP_INCLUDED
  18. #define CARLA_LIB_UTILS_HPP_INCLUDED
  19. #include "CarlaUtils.hpp"
  20. #ifndef CARLA_OS_WIN
  21. # include <dlfcn.h>
  22. #endif
  23. // -----------------------------------------------------------------------
  24. // library related calls
  25. /*
  26. * Open 'filename' library (must not be null).
  27. * May return null, in which case "lib_error" has the error.
  28. */
  29. static inline
  30. void* lib_open(const char* const filename) noexcept
  31. {
  32. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', nullptr);
  33. void* ret = nullptr;
  34. try {
  35. #ifdef CARLA_OS_WIN
  36. ret = (void*)LoadLibraryA(filename);
  37. #else
  38. ret = dlopen(filename, RTLD_NOW|RTLD_LOCAL);
  39. #endif
  40. } catch(...) {}
  41. return ret;
  42. }
  43. /*
  44. * Close a previously opened library (must not be null).
  45. * If false is returned,"lib_error" has the error.
  46. */
  47. static inline
  48. bool lib_close(void* const lib) noexcept
  49. {
  50. CARLA_SAFE_ASSERT_RETURN(lib != nullptr, false);
  51. bool ret = false;
  52. try {
  53. #ifdef CARLA_OS_WIN
  54. ret = FreeLibrary((HMODULE)lib);
  55. #else
  56. ret = (dlclose(lib) == 0);
  57. #endif
  58. } catch(...) {}
  59. return ret;
  60. }
  61. /*
  62. * Get a library symbol (must not be null).
  63. * May return null if the symbol is not found.
  64. */
  65. static inline
  66. void* lib_symbol(void* const lib, const char* const symbol) noexcept
  67. {
  68. CARLA_SAFE_ASSERT_RETURN(lib != nullptr, nullptr);
  69. CARLA_SAFE_ASSERT_RETURN(symbol != nullptr && symbol[0] != '\0', nullptr);
  70. #ifdef CARLA_OS_WIN
  71. return (void*)GetProcAddress((HMODULE)lib, symbol);
  72. #else
  73. return dlsym(lib, symbol);
  74. #endif
  75. }
  76. /*
  77. * Return the last operation error ('filename' must not be null).
  78. * May return null.
  79. */
  80. static inline
  81. const char* lib_error(const char* const filename) noexcept
  82. {
  83. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', nullptr);
  84. #ifdef CARLA_OS_WIN
  85. static char libError[2048+1];
  86. carla_zeroChar(libError, 2048+1);
  87. try {
  88. LPVOID winErrorString;
  89. DWORD winErrorCode = GetLastError();
  90. FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, winErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&winErrorString, 0, nullptr);
  91. std::snprintf(libError, 2048, "%s: error code %li: %s", filename, winErrorCode, (const char*)winErrorString);
  92. LocalFree(winErrorString);
  93. } catch(...) {}
  94. return (libError[0] != '\0') ? libError : nullptr;
  95. #else
  96. return dlerror();
  97. #endif
  98. }
  99. // -----------------------------------------------------------------------
  100. #endif // CARLA_LIB_UTILS_HPP_INCLUDED