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.

171 lines
5.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the Water library.
  4. Copyright (c) 2016 ROLI Ltd.
  5. Copyright (C) 2017 Filipe Coelho <falktx@falktx.com>
  6. Permission is granted to use this software under the terms of the ISC license
  7. http://www.isc.org/downloads/software-support-policy/isc-license/
  8. Permission to use, copy, modify, and/or distribute this software for any
  9. purpose with or without fee is hereby granted, provided that the above
  10. copyright notice and this permission notice appear in all copies.
  11. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  12. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  13. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  14. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  15. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  16. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  17. OF THIS SOFTWARE.
  18. ==============================================================================
  19. */
  20. #include "StringPool.h"
  21. #include "../misc/Time.h"
  22. namespace water {
  23. static const int minNumberOfStringsForGarbageCollection = 300;
  24. static const uint32 garbageCollectionInterval = 30000;
  25. StringPool::StringPool() noexcept : lastGarbageCollectionTime (0) {}
  26. StringPool::~StringPool() {}
  27. struct StartEndString
  28. {
  29. StartEndString (String::CharPointerType s, String::CharPointerType e) noexcept : start (s), end (e) {}
  30. operator String() const { return String (start, end); }
  31. String::CharPointerType start, end;
  32. };
  33. static int compareStrings (const String& s1, const String& s2) noexcept { return s1.compare (s2); }
  34. static int compareStrings (CharPointer_UTF8 s1, const String& s2) noexcept { return s1.compare (s2.getCharPointer()); }
  35. static int compareStrings (const StartEndString& string1, const String& string2) noexcept
  36. {
  37. String::CharPointerType s1 (string1.start), s2 (string2.getCharPointer());
  38. for (;;)
  39. {
  40. const int c1 = s1 < string1.end ? (int) s1.getAndAdvance() : 0;
  41. const int c2 = (int) s2.getAndAdvance();
  42. const int diff = c1 - c2;
  43. if (diff != 0) return diff < 0 ? -1 : 1;
  44. if (c1 == 0) break;
  45. }
  46. return 0;
  47. }
  48. template <typename NewStringType>
  49. static String addPooledString (Array<String>& strings, const NewStringType& newString)
  50. {
  51. int start = 0;
  52. int end = strings.size();
  53. while (start < end)
  54. {
  55. const String& startString = strings.getReference (start);
  56. const int startComp = compareStrings (newString, startString);
  57. if (startComp == 0)
  58. return startString;
  59. const int halfway = (start + end) / 2;
  60. if (halfway == start)
  61. {
  62. if (startComp > 0)
  63. ++start;
  64. break;
  65. }
  66. const String& halfwayString = strings.getReference (halfway);
  67. const int halfwayComp = compareStrings (newString, halfwayString);
  68. if (halfwayComp == 0)
  69. return halfwayString;
  70. if (halfwayComp > 0)
  71. start = halfway;
  72. else
  73. end = halfway;
  74. }
  75. strings.insert (start, newString);
  76. return strings.getReference (start);
  77. }
  78. String StringPool::getPooledString (const char* const newString)
  79. {
  80. if (newString == nullptr || *newString == 0)
  81. return String();
  82. const CarlaRecursiveMutexLocker sl (lock);
  83. garbageCollectIfNeeded();
  84. return addPooledString (strings, CharPointer_UTF8 (newString));
  85. }
  86. String StringPool::getPooledString (String::CharPointerType start, String::CharPointerType end)
  87. {
  88. if (start.isEmpty() || start == end)
  89. return String();
  90. const CarlaRecursiveMutexLocker sl (lock);
  91. garbageCollectIfNeeded();
  92. return addPooledString (strings, StartEndString (start, end));
  93. }
  94. String StringPool::getPooledString (StringRef newString)
  95. {
  96. if (newString.isEmpty())
  97. return String();
  98. const CarlaRecursiveMutexLocker sl (lock);
  99. garbageCollectIfNeeded();
  100. return addPooledString (strings, newString.text);
  101. }
  102. String StringPool::getPooledString (const String& newString)
  103. {
  104. if (newString.isEmpty())
  105. return String();
  106. const CarlaRecursiveMutexLocker sl (lock);
  107. garbageCollectIfNeeded();
  108. return addPooledString (strings, newString);
  109. }
  110. void StringPool::garbageCollectIfNeeded()
  111. {
  112. if (strings.size() > minNumberOfStringsForGarbageCollection
  113. && Time::getApproximateMillisecondCounter() > lastGarbageCollectionTime + garbageCollectionInterval)
  114. garbageCollect();
  115. }
  116. void StringPool::garbageCollect()
  117. {
  118. const CarlaRecursiveMutexLocker sl (lock);
  119. for (int i = strings.size(); --i >= 0;)
  120. if (strings.getReference(i).getReferenceCount() == 1)
  121. strings.remove (i);
  122. lastGarbageCollectionTime = Time::getApproximateMillisecondCounter();
  123. }
  124. StringPool& StringPool::getGlobalPool() noexcept
  125. {
  126. static StringPool pool;
  127. return pool;
  128. }
  129. }