Collection of tools useful for audio production
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.

98 lines
2.4KB

  1. /*
  2. * Carla library utils
  3. * Copyright (C) 2011-2012 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * 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 COPYING file
  16. */
  17. #ifndef CARLA_LIBRARY_UTILS_HPP
  18. #define CARLA_LIBRARY_UTILS_HPP
  19. #include "carla_utils.hpp"
  20. #ifndef Q_OS_WIN
  21. # include <dlfcn.h>
  22. #endif
  23. // ------------------------------------------------------------------------------------------------
  24. static inline
  25. void* lib_open(const char* const filename)
  26. {
  27. CARLA_ASSERT(filename);
  28. #ifdef Q_OS_WIN
  29. return LoadLibraryA(filename);
  30. #else
  31. return dlopen(filename, RTLD_NOW|RTLD_LOCAL);
  32. #endif
  33. }
  34. static inline
  35. bool lib_close(void* const lib)
  36. {
  37. CARLA_ASSERT(lib);
  38. if (! lib)
  39. return false;
  40. #ifdef Q_OS_WIN
  41. return FreeLibrary((HMODULE)lib);
  42. #else
  43. return (dlclose(lib) == 0);
  44. #endif
  45. }
  46. static inline
  47. void* lib_symbol(void* const lib, const char* const symbol)
  48. {
  49. CARLA_ASSERT(lib);
  50. CARLA_ASSERT(symbol);
  51. if (! (lib && symbol))
  52. return nullptr;
  53. #ifdef Q_OS_WIN
  54. return (void*)GetProcAddress((HMODULE)lib, symbol);
  55. #else
  56. return dlsym(lib, symbol);
  57. #endif
  58. }
  59. static inline
  60. const char* lib_error(const char* const filename)
  61. {
  62. CARLA_ASSERT(filename);
  63. #ifdef Q_OS_WIN
  64. static char libError[2048];
  65. memset(libError, 0, sizeof(char)*2048);
  66. LPVOID winErrorString;
  67. DWORD winErrorCode = GetLastError();
  68. FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, winErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&winErrorString, 0, nullptr);
  69. snprintf(libError, 2048, "%s: error code %li: %s", filename, winErrorCode, (const char*)winErrorString);
  70. LocalFree(winErrorString);
  71. return libError;
  72. #else
  73. return dlerror();
  74. Q_UNUSED(filename);
  75. #endif
  76. }
  77. // ------------------------------------------------------------------------------------------------
  78. #endif // CARLA_LIBRARY_UTILS_HPP