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 2.9KB

10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 "LinkedList.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 && filename[0] != '\0', nullptr);
  30. const CarlaMutexLocker sl(fMutex);
  31. for (LinkedList<Lib>::Itenerator it = fLibs.begin(); it.valid(); it.next())
  32. {
  33. Lib& lib(it.getValue());
  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. fLibs.append(lib);
  50. return libPtr;
  51. }
  52. bool close(void* const libPtr)
  53. {
  54. CARLA_SAFE_ASSERT_RETURN(libPtr != nullptr, false);
  55. const CarlaMutexLocker sl(fMutex);
  56. for (LinkedList<Lib>::Itenerator it = fLibs.begin(); it.valid(); it.next())
  57. {
  58. Lib& lib(it.getValue());
  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. fLibs.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 fMutex;
  86. LinkedList<Lib> fLibs;
  87. };
  88. // -----------------------------------------------------------------------
  89. #endif // CARLA_LIB_COUNTER_HPP_INCLUDED