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.

CarlaLibCounter.hpp 4.5KB

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