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.

CarlaLibUtils.hpp 3.5KB

10 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #ifdef CARLA_OS_WIN
  21. typedef HMODULE lib_t;
  22. #else
  23. # include <dlfcn.h>
  24. typedef void* lib_t;
  25. #endif
  26. // -----------------------------------------------------------------------
  27. // library related calls
  28. /*
  29. * Open 'filename' library (must not be null).
  30. * May return null, in which case "lib_error" has the error.
  31. */
  32. static inline
  33. lib_t lib_open(const char* const filename, const bool global = false) noexcept
  34. {
  35. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', nullptr);
  36. try {
  37. #ifdef CARLA_OS_WIN
  38. return ::LoadLibraryA(filename);
  39. // unused
  40. (void)global;
  41. #else
  42. return ::dlopen(filename, RTLD_NOW|(global ? RTLD_GLOBAL : RTLD_LOCAL));
  43. #endif
  44. } CARLA_SAFE_EXCEPTION_RETURN("lib_open", nullptr);
  45. }
  46. /*
  47. * Close a previously opened library (must not be null).
  48. * If false is returned, "lib_error" has the error.
  49. */
  50. static inline
  51. bool lib_close(const lib_t lib) noexcept
  52. {
  53. CARLA_SAFE_ASSERT_RETURN(lib != nullptr, false);
  54. try {
  55. #ifdef CARLA_OS_WIN
  56. return ::FreeLibrary(lib);
  57. #else
  58. return (::dlclose(lib) == 0);
  59. #endif
  60. } CARLA_SAFE_EXCEPTION_RETURN("lib_close", false);
  61. }
  62. /*
  63. * Get a library symbol (must not be null).
  64. * Returns null if the symbol is not found.
  65. */
  66. template<typename Func>
  67. static inline
  68. Func lib_symbol(const lib_t lib, const char* const symbol) noexcept
  69. {
  70. CARLA_SAFE_ASSERT_RETURN(lib != nullptr, nullptr);
  71. CARLA_SAFE_ASSERT_RETURN(symbol != nullptr && symbol[0] != '\0', nullptr);
  72. try {
  73. #ifdef CARLA_OS_WIN
  74. return (Func)::GetProcAddress(lib, symbol);
  75. #else
  76. return (Func)(uintptr_t)::dlsym(lib, symbol);
  77. #endif
  78. } CARLA_SAFE_EXCEPTION_RETURN("lib_symbol", nullptr);
  79. }
  80. /*
  81. * Return the last operation error ('filename' must not be null).
  82. * May return null.
  83. */
  84. static inline
  85. const char* lib_error(const char* const filename) noexcept
  86. {
  87. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', nullptr);
  88. #ifdef CARLA_OS_WIN
  89. static char libError[2048+1];
  90. carla_zeroChars(libError, 2048+1);
  91. try {
  92. const DWORD winErrorCode = ::GetLastError();
  93. const int winErrorFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS;
  94. LPVOID winErrorString;
  95. ::FormatMessage(winErrorFlags, nullptr, winErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&winErrorString, 0, nullptr);
  96. std::snprintf(libError, 2048, "%s: error code %li: %s", filename, winErrorCode, (const char*)winErrorString);
  97. ::LocalFree(winErrorString);
  98. } CARLA_SAFE_EXCEPTION("lib_error");
  99. return (libError[0] != '\0') ? libError : nullptr;
  100. #else
  101. return ::dlerror();
  102. #endif
  103. }
  104. // -----------------------------------------------------------------------
  105. #endif // CARLA_LIB_UTILS_HPP_INCLUDED