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.

165 lines
4.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. ~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. } CARLA_SAFE_EXCEPTION_RETURN("LibCounter::open", nullptr);
  56. const CarlaMutexLocker cml(fMutex);
  57. for (LinkedList<Lib>::Itenerator it = fLibs.begin(); it.valid(); it.next())
  58. {
  59. Lib& lib(it.getValue());
  60. CARLA_SAFE_ASSERT_CONTINUE(lib.count > 0);
  61. CARLA_SAFE_ASSERT_CONTINUE(lib.filename != nullptr);
  62. if (std::strcmp(lib.filename, filename) == 0)
  63. {
  64. // will not be needed
  65. delete[] dfilename;
  66. ++lib.count;
  67. return lib.lib;
  68. }
  69. }
  70. void* const libPtr(lib_open(filename));
  71. if (libPtr == nullptr)
  72. {
  73. delete[] dfilename;
  74. return nullptr;
  75. }
  76. Lib lib;
  77. lib.lib = libPtr;
  78. lib.filename = dfilename;
  79. lib.count = 1;
  80. lib.canDelete = canDelete;
  81. if (fLibs.append(lib))
  82. return libPtr;
  83. delete[] dfilename;
  84. return nullptr;
  85. }
  86. bool close(void* const libPtr) noexcept
  87. {
  88. CARLA_SAFE_ASSERT_RETURN(libPtr != nullptr, false);
  89. const CarlaMutexLocker cml(fMutex);
  90. for (LinkedList<Lib>::Itenerator it = fLibs.begin(); it.valid(); it.next())
  91. {
  92. Lib& lib(it.getValue());
  93. CARLA_SAFE_ASSERT_CONTINUE(lib.count > 0);
  94. CARLA_SAFE_ASSERT_CONTINUE(lib.lib != nullptr);
  95. if (lib.lib != libPtr)
  96. continue;
  97. if (lib.count == 1 && ! lib.canDelete)
  98. return true;
  99. if (--lib.count == 0)
  100. {
  101. if (! lib_close(lib.lib))
  102. carla_stderr("LibCounter::close() failed, reason:\n%s", lib_error(lib.filename));
  103. lib.lib = nullptr;
  104. if (lib.filename != nullptr)
  105. {
  106. delete[] lib.filename;
  107. lib.filename = nullptr;
  108. }
  109. fLibs.remove(it);
  110. }
  111. return true;
  112. }
  113. carla_safe_assert("invalid lib pointer", __FILE__, __LINE__);
  114. return false;
  115. }
  116. private:
  117. struct Lib {
  118. void* lib;
  119. const char* filename;
  120. int count;
  121. bool canDelete;
  122. };
  123. CarlaMutex fMutex;
  124. LinkedList<Lib> fLibs;
  125. };
  126. // -----------------------------------------------------------------------
  127. #endif // CARLA_LIB_COUNTER_HPP_INCLUDED