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.

136 lines
4.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_ARRAYALLOCATIONBASE_JUCEHEADER__
  19. #define __JUCE_ARRAYALLOCATIONBASE_JUCEHEADER__
  20. #include "../memory/juce_HeapBlock.h"
  21. //==============================================================================
  22. /**
  23. Implements some basic array storage allocation functions.
  24. This class isn't really for public use - it's used by the other
  25. array classes, but might come in handy for some purposes.
  26. It inherits from a critical section class to allow the arrays to use
  27. the "empty base class optimisation" pattern to reduce their footprint.
  28. @see Array, OwnedArray, ReferenceCountedArray
  29. */
  30. template <class ElementType, class TypeOfCriticalSectionToUse>
  31. class ArrayAllocationBase : public TypeOfCriticalSectionToUse
  32. {
  33. public:
  34. //==============================================================================
  35. /** Creates an empty array. */
  36. ArrayAllocationBase() noexcept
  37. : numAllocated (0)
  38. {
  39. }
  40. /** Destructor. */
  41. ~ArrayAllocationBase() noexcept
  42. {
  43. }
  44. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  45. ArrayAllocationBase (ArrayAllocationBase<ElementType, TypeOfCriticalSectionToUse>&& other) noexcept
  46. : elements (static_cast <HeapBlock <ElementType>&&> (other.elements)),
  47. numAllocated (other.numAllocated)
  48. {
  49. }
  50. ArrayAllocationBase& operator= (ArrayAllocationBase<ElementType, TypeOfCriticalSectionToUse>&& other) noexcept
  51. {
  52. elements = static_cast <HeapBlock <ElementType>&&> (other.elements);
  53. numAllocated = other.numAllocated;
  54. return *this;
  55. }
  56. #endif
  57. //==============================================================================
  58. /** Changes the amount of storage allocated.
  59. This will retain any data currently held in the array, and either add or
  60. remove extra space at the end.
  61. @param numElements the number of elements that are needed
  62. */
  63. void setAllocatedSize (const int numElements)
  64. {
  65. if (numAllocated != numElements)
  66. {
  67. if (numElements > 0)
  68. elements.realloc ((size_t) numElements);
  69. else
  70. elements.free();
  71. numAllocated = numElements;
  72. }
  73. }
  74. /** Increases the amount of storage allocated if it is less than a given amount.
  75. This will retain any data currently held in the array, but will add
  76. extra space at the end to make sure there it's at least as big as the size
  77. passed in. If it's already bigger, no action is taken.
  78. @param minNumElements the minimum number of elements that are needed
  79. */
  80. void ensureAllocatedSize (const int minNumElements)
  81. {
  82. if (minNumElements > numAllocated)
  83. setAllocatedSize ((minNumElements + minNumElements / 2 + 8) & ~7);
  84. }
  85. /** Minimises the amount of storage allocated so that it's no more than
  86. the given number of elements.
  87. */
  88. void shrinkToNoMoreThan (const int maxNumElements)
  89. {
  90. if (maxNumElements < numAllocated)
  91. setAllocatedSize (maxNumElements);
  92. }
  93. /** Swap the contents of two objects. */
  94. void swapWith (ArrayAllocationBase <ElementType, TypeOfCriticalSectionToUse>& other) noexcept
  95. {
  96. elements.swapWith (other.elements);
  97. std::swap (numAllocated, other.numAllocated);
  98. }
  99. //==============================================================================
  100. HeapBlock <ElementType> elements;
  101. int numAllocated;
  102. private:
  103. JUCE_DECLARE_NON_COPYABLE (ArrayAllocationBase);
  104. };
  105. #endif // __JUCE_ARRAYALLOCATIONBASE_JUCEHEADER__