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.

166 lines
4.5KB

  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. ~LibCounter() noexcept
  28. {
  29. // might have some leftovers
  30. for (LinkedList<Lib>::Itenerator it = fLibs.begin(); it.valid(); it.next())
  31. {
  32. Lib& lib(it.getValue());
  33. CARLA_SAFE_ASSERT_CONTINUE(lib.count > 0);
  34. CARLA_SAFE_ASSERT_CONTINUE(lib.lib != nullptr);
  35. // all libs should be closed by now except those explicitly marked non-delete
  36. CARLA_SAFE_ASSERT(! lib.canDelete);
  37. if (! lib_close(lib.lib))
  38. carla_stderr("LibCounter cleanup failed, reason:\n%s", lib_error(lib.filename));
  39. lib.lib = nullptr;
  40. if (lib.filename != nullptr)
  41. {
  42. delete[] lib.filename;
  43. lib.filename = nullptr;
  44. }
  45. }
  46. fLibs.clear();
  47. }
  48. void* open(const char* const filename, const bool canDelete = true) noexcept
  49. {
  50. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', nullptr);
  51. // try duplicating filename first, it can throw
  52. const char* dfilename = nullptr;
  53. try {
  54. dfilename = carla_strdup(filename);
  55. }
  56. CARLA_SAFE_EXCEPTION_RETURN("LibCounter::open", nullptr);
  57. const CarlaMutexLocker cml(fMutex);
  58. for (LinkedList<Lib>::Itenerator it = fLibs.begin(); it.valid(); it.next())
  59. {
  60. Lib& lib(it.getValue());
  61. CARLA_SAFE_ASSERT_CONTINUE(lib.count > 0);
  62. CARLA_SAFE_ASSERT_CONTINUE(lib.filename != nullptr);
  63. if (std::strcmp(lib.filename, filename) == 0)
  64. {
  65. // will not be needed
  66. delete[] dfilename;
  67. ++lib.count;
  68. return lib.lib;
  69. }
  70. }
  71. void* const libPtr(lib_open(filename));
  72. if (libPtr == nullptr)
  73. {
  74. delete[] dfilename;
  75. return nullptr;
  76. }
  77. Lib lib;
  78. lib.lib = libPtr;
  79. lib.filename = dfilename;
  80. lib.count = 1;
  81. lib.canDelete = canDelete;
  82. if (fLibs.append(lib))
  83. return libPtr;
  84. delete[] dfilename;
  85. return nullptr;
  86. }
  87. bool close(void* const libPtr) noexcept
  88. {
  89. CARLA_SAFE_ASSERT_RETURN(libPtr != nullptr, false);
  90. const CarlaMutexLocker cml(fMutex);
  91. for (LinkedList<Lib>::Itenerator it = fLibs.begin(); it.valid(); it.next())
  92. {
  93. Lib& lib(it.getValue());
  94. CARLA_SAFE_ASSERT_CONTINUE(lib.count > 0);
  95. CARLA_SAFE_ASSERT_CONTINUE(lib.lib != nullptr);
  96. if (lib.lib != libPtr)
  97. continue;
  98. if (lib.count == 1 && ! lib.canDelete)
  99. return true;
  100. if (--lib.count == 0)
  101. {
  102. if (! lib_close(lib.lib))
  103. carla_stderr("LibCounter::close() failed, reason:\n%s", lib_error(lib.filename));
  104. lib.lib = nullptr;
  105. if (lib.filename != nullptr)
  106. {
  107. delete[] lib.filename;
  108. lib.filename = nullptr;
  109. }
  110. fLibs.remove(it);
  111. }
  112. return true;
  113. }
  114. carla_safe_assert("invalid lib pointer", __FILE__, __LINE__);
  115. return false;
  116. }
  117. private:
  118. struct Lib {
  119. void* lib;
  120. const char* filename;
  121. int count;
  122. bool canDelete;
  123. };
  124. CarlaMutex fMutex;
  125. LinkedList<Lib> fLibs;
  126. };
  127. // -----------------------------------------------------------------------
  128. #endif // CARLA_LIB_COUNTER_HPP_INCLUDED