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.

135 lines
4.8KB

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