The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. static const int minNumberOfStringsForGarbageCollection = 300;
  20. static const uint32 garbageCollectionInterval = 30000;
  21. StringPool::StringPool() noexcept : lastGarbageCollectionTime (0) {}
  22. struct StartEndString
  23. {
  24. StartEndString (String::CharPointerType s, String::CharPointerType e) noexcept : start (s), end (e) {}
  25. operator String() const { return String (start, end); }
  26. String::CharPointerType start, end;
  27. };
  28. static int compareStrings (const String& s1, const String& s2) noexcept { return s1.compare (s2); }
  29. static int compareStrings (CharPointer_UTF8 s1, const String& s2) noexcept { return s1.compare (s2.getCharPointer()); }
  30. static int compareStrings (const StartEndString& string1, const String& string2) noexcept
  31. {
  32. String::CharPointerType s1 (string1.start), s2 (string2.getCharPointer());
  33. for (;;)
  34. {
  35. const int c1 = s1 < string1.end ? (int) s1.getAndAdvance() : 0;
  36. const int c2 = (int) s2.getAndAdvance();
  37. const int diff = c1 - c2;
  38. if (diff != 0) return diff < 0 ? -1 : 1;
  39. if (c1 == 0) break;
  40. }
  41. return 0;
  42. }
  43. template <typename NewStringType>
  44. static String addPooledString (Array<String>& strings, const NewStringType& newString)
  45. {
  46. int start = 0;
  47. int end = strings.size();
  48. while (start < end)
  49. {
  50. const String& startString = strings.getReference (start);
  51. const int startComp = compareStrings (newString, startString);
  52. if (startComp == 0)
  53. return startString;
  54. const int halfway = (start + end) / 2;
  55. if (halfway == start)
  56. {
  57. if (startComp > 0)
  58. ++start;
  59. break;
  60. }
  61. const String& halfwayString = strings.getReference (halfway);
  62. const int halfwayComp = compareStrings (newString, halfwayString);
  63. if (halfwayComp == 0)
  64. return halfwayString;
  65. if (halfwayComp > 0)
  66. start = halfway;
  67. else
  68. end = halfway;
  69. }
  70. strings.insert (start, newString);
  71. return strings.getReference (start);
  72. }
  73. String StringPool::getPooledString (const char* const newString)
  74. {
  75. if (newString == nullptr || *newString == 0)
  76. return {};
  77. const ScopedLock sl (lock);
  78. garbageCollectIfNeeded();
  79. return addPooledString (strings, CharPointer_UTF8 (newString));
  80. }
  81. String StringPool::getPooledString (String::CharPointerType start, String::CharPointerType end)
  82. {
  83. if (start.isEmpty() || start == end)
  84. return {};
  85. const ScopedLock sl (lock);
  86. garbageCollectIfNeeded();
  87. return addPooledString (strings, StartEndString (start, end));
  88. }
  89. String StringPool::getPooledString (StringRef newString)
  90. {
  91. if (newString.isEmpty())
  92. return {};
  93. const ScopedLock sl (lock);
  94. garbageCollectIfNeeded();
  95. return addPooledString (strings, newString.text);
  96. }
  97. String StringPool::getPooledString (const String& newString)
  98. {
  99. if (newString.isEmpty())
  100. return {};
  101. const ScopedLock sl (lock);
  102. garbageCollectIfNeeded();
  103. return addPooledString (strings, newString);
  104. }
  105. void StringPool::garbageCollectIfNeeded()
  106. {
  107. if (strings.size() > minNumberOfStringsForGarbageCollection
  108. && Time::getApproximateMillisecondCounter() > lastGarbageCollectionTime + garbageCollectionInterval)
  109. garbageCollect();
  110. }
  111. void StringPool::garbageCollect()
  112. {
  113. const ScopedLock sl (lock);
  114. for (int i = strings.size(); --i >= 0;)
  115. if (strings.getReference (i).getReferenceCount() == 1)
  116. strings.remove (i);
  117. lastGarbageCollectionTime = Time::getApproximateMillisecondCounter();
  118. }
  119. StringPool& StringPool::getGlobalPool() noexcept
  120. {
  121. static StringPool pool;
  122. return pool;
  123. }
  124. } // namespace juce