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.

131 lines
3.4KB

  1. /*
  2. * Carla library counter
  3. * Copyright (C) 2013-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_COUNTER_HPP_INCLUDED
  18. #define CARLA_LIB_COUNTER_HPP_INCLUDED
  19. #include "CarlaLibUtils.hpp"
  20. #include "CarlaMutex.hpp"
  21. #include "LinkedList.hpp"
  22. // -----------------------------------------------------------------------
  23. class LibCounter
  24. {
  25. public:
  26. LibCounter() noexcept {}
  27. void* open(const char* const filename) noexcept
  28. {
  29. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', nullptr);
  30. // try duplicating filename first, it can throw
  31. const char* dfilename = nullptr;
  32. try {
  33. dfilename = carla_strdup(filename);
  34. }
  35. CARLA_SAFE_EXCEPTION_RETURN("LibCounter::open", nullptr);
  36. const CarlaMutexLocker sl(fMutex);
  37. for (LinkedList<Lib>::Itenerator it = fLibs.begin(); it.valid(); it.next())
  38. {
  39. Lib& lib(it.getValue());
  40. CARLA_SAFE_ASSERT_CONTINUE(lib.count > 0);
  41. CARLA_SAFE_ASSERT_CONTINUE(lib.filename != nullptr);
  42. if (std::strcmp(lib.filename, filename) == 0)
  43. {
  44. ++lib.count;
  45. return lib.lib;
  46. }
  47. }
  48. void* const libPtr(lib_open(filename));
  49. if (libPtr == nullptr)
  50. {
  51. delete[] dfilename;
  52. return nullptr;
  53. }
  54. Lib lib;
  55. lib.lib = libPtr;
  56. lib.filename = dfilename;
  57. lib.count = 1;
  58. if (fLibs.append(lib))
  59. return libPtr;
  60. delete[] dfilename;
  61. return nullptr;
  62. }
  63. bool close(void* const libPtr) noexcept
  64. {
  65. CARLA_SAFE_ASSERT_RETURN(libPtr != nullptr, false);
  66. const CarlaMutexLocker sl(fMutex);
  67. for (LinkedList<Lib>::Itenerator it = fLibs.begin(); it.valid(); it.next())
  68. {
  69. Lib& lib(it.getValue());
  70. CARLA_SAFE_ASSERT_CONTINUE(lib.count > 0);
  71. CARLA_SAFE_ASSERT_CONTINUE(lib.lib != nullptr);
  72. if (lib.lib != libPtr)
  73. continue;
  74. if (--lib.count == 0)
  75. {
  76. if (! lib_close(lib.lib))
  77. carla_stderr("LibCounter::close() failed, reason:\n%s", lib_error(lib.filename));
  78. lib.lib = nullptr;
  79. if (lib.filename != nullptr)
  80. {
  81. delete[] lib.filename;
  82. lib.filename = nullptr;
  83. }
  84. fLibs.remove(it);
  85. }
  86. return true;
  87. }
  88. carla_safe_assert("invalid lib pointer", __FILE__, __LINE__);
  89. return false;
  90. }
  91. private:
  92. struct Lib {
  93. void* lib;
  94. const char* filename;
  95. int count;
  96. };
  97. CarlaMutex fMutex;
  98. LinkedList<Lib> fLibs;
  99. };
  100. // -----------------------------------------------------------------------
  101. #endif // CARLA_LIB_COUNTER_HPP_INCLUDED