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.4KB

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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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) 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. #else
  40. return ::dlopen(filename, RTLD_NOW|RTLD_LOCAL);
  41. #endif
  42. } CARLA_SAFE_EXCEPTION_RETURN("lib_open", nullptr);
  43. }
  44. /*
  45. * Close a previously opened library (must not be null).
  46. * If false is returned, "lib_error" has the error.
  47. */
  48. static inline
  49. bool lib_close(const lib_t lib) noexcept
  50. {
  51. CARLA_SAFE_ASSERT_RETURN(lib != nullptr, false);
  52. try {
  53. #ifdef CARLA_OS_WIN
  54. return ::FreeLibrary(lib);
  55. #else
  56. return (::dlclose(lib) == 0);
  57. #endif
  58. } CARLA_SAFE_EXCEPTION_RETURN("lib_close", false);
  59. }
  60. /*
  61. * Get a library symbol (must not be null).
  62. * Returns null if the symbol is not found.
  63. */
  64. template<typename Func>
  65. static inline
  66. Func lib_symbol(const lib_t 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. try {
  71. #ifdef CARLA_OS_WIN
  72. return (Func)::GetProcAddress(lib, symbol);
  73. #else
  74. return (Func)(uintptr_t)::dlsym(lib, symbol);
  75. #endif
  76. } CARLA_SAFE_EXCEPTION_RETURN("lib_symbol", nullptr);
  77. }
  78. /*
  79. * Return the last operation error ('filename' must not be null).
  80. * May return null.
  81. */
  82. static inline
  83. const char* lib_error(const char* const filename) noexcept
  84. {
  85. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', nullptr);
  86. #ifdef CARLA_OS_WIN
  87. static char libError[2048+1];
  88. carla_zeroChar(libError, 2048+1);
  89. try {
  90. const DWORD winErrorCode = ::GetLastError();
  91. const int winErrorFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS;
  92. LPVOID winErrorString;
  93. ::FormatMessage(winErrorFlags, nullptr, winErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&winErrorString, 0, nullptr);
  94. std::snprintf(libError, 2048, "%s: error code %li: %s", filename, winErrorCode, (const char*)winErrorString);
  95. ::LocalFree(winErrorString);
  96. } CARLA_SAFE_EXCEPTION("lib_error");
  97. return (libError[0] != '\0') ? libError : nullptr;
  98. #else
  99. return ::dlerror();
  100. #endif
  101. }
  102. // -----------------------------------------------------------------------
  103. #endif // CARLA_LIB_UTILS_HPP_INCLUDED