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.

107 lines
2.8KB

  1. /*
  2. * Carla library utils
  3. * Copyright (C) 2011-2013 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)
  31. {
  32. CARLA_SAFE_ASSERT_RETURN(filename != nullptr, nullptr);
  33. #ifdef CARLA_OS_WIN
  34. return (void*)LoadLibraryA(filename);
  35. #else
  36. return dlopen(filename, RTLD_NOW|RTLD_LOCAL);
  37. #endif
  38. }
  39. /*
  40. * Close a previously opened library (must not be null).
  41. * If false is returned,"lib_error" has the error.
  42. */
  43. static inline
  44. bool lib_close(void* const lib)
  45. {
  46. CARLA_SAFE_ASSERT_RETURN(lib != nullptr, false);
  47. #ifdef CARLA_OS_WIN
  48. return FreeLibrary((HMODULE)lib);
  49. #else
  50. return (dlclose(lib) == 0);
  51. #endif
  52. }
  53. /*
  54. * Get a library symbol (must not be null).
  55. * May return null if the symbol is not found.
  56. */
  57. static inline
  58. void* lib_symbol(void* const lib, const char* const symbol)
  59. {
  60. CARLA_SAFE_ASSERT_RETURN(lib != nullptr, nullptr);
  61. CARLA_SAFE_ASSERT_RETURN(symbol != nullptr, nullptr);
  62. #ifdef CARLA_OS_WIN
  63. return (void*)GetProcAddress((HMODULE)lib, symbol);
  64. #else
  65. return dlsym(lib, symbol);
  66. #endif
  67. }
  68. /*
  69. * Return the last operation error ('filename' must not be null).
  70. */
  71. static inline
  72. const char* lib_error(const char* const filename)
  73. {
  74. CARLA_SAFE_ASSERT_RETURN(filename != nullptr, nullptr);
  75. #ifdef CARLA_OS_WIN
  76. static char libError[2048+1];
  77. carla_zeroChar(libError, 2048+1);
  78. LPVOID winErrorString;
  79. DWORD winErrorCode = GetLastError();
  80. FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, winErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&winErrorString, 0, nullptr);
  81. std::snprintf(libError, 2048, "%s: error code %li: %s", filename, winErrorCode, (const char*)winErrorString);
  82. LocalFree(winErrorString);
  83. return libError;
  84. #else
  85. return dlerror();
  86. #endif
  87. }
  88. // -----------------------------------------------------------------------
  89. #endif // CARLA_LIB_UTILS_HPP_INCLUDED