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.

129 lines
4.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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. //==============================================================================
  20. /**
  21. Implements some basic array storage allocation functions.
  22. This class isn't really for public use - it's used by the other
  23. array classes, but might come in handy for some purposes.
  24. It inherits from a critical section class to allow the arrays to use
  25. the "empty base class optimisation" pattern to reduce their footprint.
  26. @see Array, OwnedArray, ReferenceCountedArray
  27. */
  28. template <class ElementType, class TypeOfCriticalSectionToUse>
  29. class ArrayAllocationBase : public TypeOfCriticalSectionToUse
  30. {
  31. public:
  32. //==============================================================================
  33. /** Creates an empty array. */
  34. ArrayAllocationBase() noexcept
  35. : numAllocated (0)
  36. {
  37. }
  38. /** Destructor. */
  39. ~ArrayAllocationBase() noexcept
  40. {
  41. }
  42. ArrayAllocationBase (ArrayAllocationBase<ElementType, TypeOfCriticalSectionToUse>&& other) noexcept
  43. : elements (static_cast<HeapBlock<ElementType>&&> (other.elements)),
  44. numAllocated (other.numAllocated)
  45. {
  46. }
  47. ArrayAllocationBase& operator= (ArrayAllocationBase<ElementType, TypeOfCriticalSectionToUse>&& other) noexcept
  48. {
  49. elements = static_cast<HeapBlock<ElementType>&&> (other.elements);
  50. numAllocated = other.numAllocated;
  51. return *this;
  52. }
  53. //==============================================================================
  54. /** Changes the amount of storage allocated.
  55. This will retain any data currently held in the array, and either add or
  56. remove extra space at the end.
  57. @param numElements the number of elements that are needed
  58. */
  59. void setAllocatedSize (const int numElements)
  60. {
  61. if (numAllocated != numElements)
  62. {
  63. if (numElements > 0)
  64. elements.realloc ((size_t) numElements);
  65. else
  66. elements.free();
  67. numAllocated = numElements;
  68. }
  69. }
  70. /** Increases the amount of storage allocated if it is less than a given amount.
  71. This will retain any data currently held in the array, but will add
  72. extra space at the end to make sure there it's at least as big as the size
  73. passed in. If it's already bigger, no action is taken.
  74. @param minNumElements the minimum number of elements that are needed
  75. */
  76. void ensureAllocatedSize (const int minNumElements)
  77. {
  78. if (minNumElements > numAllocated)
  79. setAllocatedSize ((minNumElements + minNumElements / 2 + 8) & ~7);
  80. jassert (numAllocated <= 0 || elements != nullptr);
  81. }
  82. /** Minimises the amount of storage allocated so that it's no more than
  83. the given number of elements.
  84. */
  85. void shrinkToNoMoreThan (const int maxNumElements)
  86. {
  87. if (maxNumElements < numAllocated)
  88. setAllocatedSize (maxNumElements);
  89. }
  90. /** Swap the contents of two objects. */
  91. void swapWith (ArrayAllocationBase <ElementType, TypeOfCriticalSectionToUse>& other) noexcept
  92. {
  93. elements.swapWith (other.elements);
  94. std::swap (numAllocated, other.numAllocated);
  95. }
  96. //==============================================================================
  97. HeapBlock<ElementType> elements;
  98. int numAllocated;
  99. private:
  100. JUCE_DECLARE_NON_COPYABLE (ArrayAllocationBase)
  101. };
  102. } // namespace juce