Audio plugin host https://kx.studio/carla
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.

145 lines
4.9KB

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