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.

116 lines
2.9KB

  1. /*
  2. * Carla library counter
  3. * Copyright (C) 2013 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 "RtList.hpp"
  22. // -----------------------------------------------------------------------
  23. class LibCounter
  24. {
  25. public:
  26. LibCounter() {}
  27. void* open(const char* const filename)
  28. {
  29. CARLA_SAFE_ASSERT_RETURN(filename != nullptr, nullptr);
  30. const CarlaMutex::ScopedLocker sl(mutex);
  31. for (NonRtList<Lib>::Itenerator it = libs.begin(); it.valid(); it.next())
  32. {
  33. Lib& lib(*it);
  34. CARLA_ASSERT(lib.count > 0);
  35. CARLA_SAFE_ASSERT_CONTINUE(lib.filename != nullptr);
  36. if (std::strcmp(lib.filename, filename) == 0)
  37. {
  38. ++lib.count;
  39. return lib.lib;
  40. }
  41. }
  42. void* const libPtr(lib_open(filename));
  43. if (libPtr == nullptr)
  44. return nullptr;
  45. Lib lib;
  46. lib.lib = libPtr;
  47. lib.filename = carla_strdup(filename);
  48. lib.count = 1;
  49. libs.append(lib);
  50. return libPtr;
  51. }
  52. bool close(void* const libPtr)
  53. {
  54. CARLA_SAFE_ASSERT_RETURN(libPtr != nullptr, false);
  55. const CarlaMutex::ScopedLocker sl(mutex);
  56. for (NonRtList<Lib>::Itenerator it = libs.begin(); it.valid(); it.next())
  57. {
  58. Lib& lib(*it);
  59. CARLA_ASSERT(lib.count > 0);
  60. CARLA_SAFE_ASSERT_CONTINUE(lib.lib != nullptr);
  61. if (lib.lib != libPtr)
  62. continue;
  63. if (--lib.count == 0)
  64. {
  65. if (lib.filename != nullptr)
  66. {
  67. delete[] lib.filename;
  68. lib.filename = nullptr;
  69. }
  70. lib_close(lib.lib);
  71. lib.lib = nullptr;
  72. libs.remove(it);
  73. }
  74. return true;
  75. }
  76. CARLA_ASSERT(false); // invalid pointer
  77. return false;
  78. }
  79. private:
  80. struct Lib {
  81. void* lib;
  82. const char* filename;
  83. int count;
  84. };
  85. CarlaMutex mutex;
  86. NonRtList<Lib> libs;
  87. };
  88. // -----------------------------------------------------------------------
  89. #endif // CARLA_LIB_COUNTER_HPP_INCLUDED