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.

94 lines
2.2KB

  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_H
  18. #define CARLA_LIBRARY_UTILS_H
  19. #include "carla_utils.h"
  20. #ifndef Q_OS_WIN
  21. # include <dlfcn.h>
  22. #endif
  23. static inline
  24. void* lib_open(const char* const filename)
  25. {
  26. CARLA_ASSERT(filename);
  27. #ifdef Q_OS_WIN
  28. return LoadLibraryA(filename);
  29. #else
  30. return dlopen(filename, RTLD_NOW|RTLD_LOCAL);
  31. #endif
  32. }
  33. static inline
  34. bool lib_close(void* const lib)
  35. {
  36. CARLA_ASSERT(lib);
  37. if (! lib)
  38. return false;
  39. #ifdef Q_OS_WIN
  40. return FreeLibrary((HMODULE)lib);
  41. #else
  42. return (dlclose(lib) == 0);
  43. #endif
  44. }
  45. static inline
  46. void* lib_symbol(void* const lib, const char* const symbol)
  47. {
  48. CARLA_ASSERT(lib);
  49. CARLA_ASSERT(symbol);
  50. if (! lib)
  51. return nullptr;
  52. #ifdef Q_OS_WIN
  53. return (void*)GetProcAddress((HMODULE)lib, symbol);
  54. #else
  55. return dlsym(lib, symbol);
  56. #endif
  57. }
  58. static inline
  59. const char* lib_error(const char* const filename)
  60. {
  61. CARLA_ASSERT(filename);
  62. #ifdef Q_OS_WIN
  63. static char libError[2048];
  64. memset(libError, 0, sizeof(char)*2048);
  65. LPVOID winErrorString;
  66. DWORD winErrorCode = GetLastError();
  67. FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, winErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&winErrorString, 0, nullptr);
  68. snprintf(libError, 2048, "%s: error code %i: %s", filename, winErrorCode, (const char*)winErrorString);
  69. LocalFree(winErrorString);
  70. return libError;
  71. #else
  72. return dlerror();
  73. Q_UNUSED(filename);
  74. #endif
  75. }
  76. #endif // CARLA_LIBRARY_UTILS_H