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.

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