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.

139 lines
5.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the juce_core module of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. or without fee is hereby granted, provided that the above copyright notice and this
  7. permission notice appear in all copies.
  8. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  9. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  10. NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  11. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  12. IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. ------------------------------------------------------------------------------
  15. NOTE! This permissive ISC license applies ONLY to files within the juce_core module!
  16. All other JUCE modules are covered by a dual GPL/commercial license, so if you are
  17. using any other modules, be sure to check that you also comply with their license.
  18. For more details, visit www.juce.com
  19. ==============================================================================
  20. */
  21. #ifndef JUCE_ARRAYALLOCATIONBASE_H_INCLUDED
  22. #define JUCE_ARRAYALLOCATIONBASE_H_INCLUDED
  23. //==============================================================================
  24. /**
  25. Implements some basic array storage allocation functions.
  26. This class isn't really for public use - it's used by the other
  27. array classes, but might come in handy for some purposes.
  28. It inherits from a critical section class to allow the arrays to use
  29. the "empty base class optimisation" pattern to reduce their footprint.
  30. @see Array, OwnedArray, ReferenceCountedArray
  31. */
  32. template <class ElementType, class TypeOfCriticalSectionToUse>
  33. class ArrayAllocationBase : public TypeOfCriticalSectionToUse
  34. {
  35. public:
  36. //==============================================================================
  37. /** Creates an empty array. */
  38. ArrayAllocationBase() noexcept
  39. : numAllocated (0)
  40. {
  41. }
  42. /** Destructor. */
  43. ~ArrayAllocationBase() noexcept
  44. {
  45. }
  46. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  47. ArrayAllocationBase (ArrayAllocationBase<ElementType, TypeOfCriticalSectionToUse>&& other) noexcept
  48. : elements (static_cast <HeapBlock <ElementType>&&> (other.elements)),
  49. numAllocated (other.numAllocated)
  50. {
  51. }
  52. ArrayAllocationBase& operator= (ArrayAllocationBase<ElementType, TypeOfCriticalSectionToUse>&& other) noexcept
  53. {
  54. elements = static_cast <HeapBlock <ElementType>&&> (other.elements);
  55. numAllocated = other.numAllocated;
  56. return *this;
  57. }
  58. #endif
  59. //==============================================================================
  60. /** Changes the amount of storage allocated.
  61. This will retain any data currently held in the array, and either add or
  62. remove extra space at the end.
  63. @param numElements the number of elements that are needed
  64. */
  65. void setAllocatedSize (const int numElements)
  66. {
  67. if (numAllocated != numElements)
  68. {
  69. if (numElements > 0)
  70. elements.realloc ((size_t) numElements);
  71. else
  72. elements.free();
  73. numAllocated = numElements;
  74. }
  75. }
  76. /** Increases the amount of storage allocated if it is less than a given amount.
  77. This will retain any data currently held in the array, but will add
  78. extra space at the end to make sure there it's at least as big as the size
  79. passed in. If it's already bigger, no action is taken.
  80. @param minNumElements the minimum number of elements that are needed
  81. */
  82. void ensureAllocatedSize (const int minNumElements)
  83. {
  84. if (minNumElements > numAllocated)
  85. setAllocatedSize ((minNumElements + minNumElements / 2 + 8) & ~7);
  86. jassert (numAllocated <= 0 || elements != nullptr);
  87. }
  88. /** Minimises the amount of storage allocated so that it's no more than
  89. the given number of elements.
  90. */
  91. void shrinkToNoMoreThan (const int maxNumElements)
  92. {
  93. if (maxNumElements < numAllocated)
  94. setAllocatedSize (maxNumElements);
  95. }
  96. /** Swap the contents of two objects. */
  97. void swapWith (ArrayAllocationBase <ElementType, TypeOfCriticalSectionToUse>& other) noexcept
  98. {
  99. elements.swapWith (other.elements);
  100. std::swap (numAllocated, other.numAllocated);
  101. }
  102. //==============================================================================
  103. HeapBlock <ElementType> elements;
  104. int numAllocated;
  105. private:
  106. JUCE_DECLARE_NON_COPYABLE (ArrayAllocationBase)
  107. };
  108. #endif // JUCE_ARRAYALLOCATIONBASE_H_INCLUDED