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.

148 lines
4.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the Water library.
  4. Copyright (c) 2016 ROLI Ltd.
  5. Copyright (C) 2017 Filipe Coelho <falktx@falktx.com>
  6. Permission is granted to use this software under the terms of the ISC license
  7. http://www.isc.org/downloads/software-support-policy/isc-license/
  8. Permission to use, copy, modify, and/or distribute this software for any
  9. purpose with or without fee is hereby granted, provided that the above
  10. copyright notice and this permission notice appear in all copies.
  11. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  12. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  13. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  14. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  15. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  16. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  17. OF THIS SOFTWARE.
  18. ==============================================================================
  19. */
  20. #ifndef WATER_ARRAYALLOCATIONBASE_H_INCLUDED
  21. #define WATER_ARRAYALLOCATIONBASE_H_INCLUDED
  22. #include "../memory/HeapBlock.h"
  23. #include "CarlaUtils.hpp"
  24. namespace water {
  25. //==============================================================================
  26. /**
  27. Implements some basic array storage allocation functions.
  28. This class isn't really for public use - it's used by the other
  29. array classes, but might come in handy for some purposes.
  30. @see Array, OwnedArray, ReferenceCountedArray
  31. */
  32. template <class ElementType>
  33. class ArrayAllocationBase
  34. {
  35. public:
  36. //==============================================================================
  37. /** Creates an empty array. */
  38. ArrayAllocationBase() noexcept
  39. : numAllocated (0)
  40. {
  41. }
  42. /** Destructor. */
  43. ~ArrayAllocationBase() noexcept
  44. {
  45. }
  46. #if WATER_COMPILER_SUPPORTS_MOVE_SEMANTICS
  47. ArrayAllocationBase (ArrayAllocationBase<ElementType>&& other) noexcept
  48. : elements (static_cast<HeapBlock<ElementType>&&> (other.elements)),
  49. numAllocated (other.numAllocated)
  50. {
  51. }
  52. ArrayAllocationBase& operator= (ArrayAllocationBase<ElementType>&& other) noexcept
  53. {
  54. elements = static_cast<HeapBlock<ElementType>&&> (other.elements);
  55. numAllocated = other.numAllocated;
  56. return *this;
  57. }
  58. #endif
  59. //==============================================================================
  60. /** Changes the amount of storage allocated.
  61. This will retain any data currently held in the array, and either add or
  62. remove extra space at the end.
  63. @param numElements the number of elements that are needed
  64. */
  65. bool setAllocatedSize (const int numElements) noexcept
  66. {
  67. if (numAllocated != numElements)
  68. {
  69. if (numElements > 0)
  70. {
  71. if (! elements.realloc ((size_t) numElements))
  72. return false;
  73. }
  74. else
  75. {
  76. elements.free();
  77. }
  78. numAllocated = numElements;
  79. }
  80. return true;
  81. }
  82. /** Increases the amount of storage allocated if it is less than a given amount.
  83. This will retain any data currently held in the array, but will add
  84. extra space at the end to make sure there it's at least as big as the size
  85. passed in. If it's already bigger, no action is taken.
  86. @param minNumElements the minimum number of elements that are needed
  87. */
  88. bool ensureAllocatedSize (const int minNumElements) noexcept
  89. {
  90. if (minNumElements > numAllocated)
  91. return setAllocatedSize ((minNumElements + minNumElements / 2 + 8) & ~7);
  92. return true;
  93. }
  94. /** Minimises the amount of storage allocated so that it's no more than
  95. the given number of elements.
  96. */
  97. bool shrinkToNoMoreThan (const int maxNumElements) noexcept
  98. {
  99. if (maxNumElements < numAllocated)
  100. return setAllocatedSize (maxNumElements);
  101. return true;
  102. }
  103. /** Swap the contents of two objects. */
  104. void swapWith (ArrayAllocationBase <ElementType>& other) noexcept
  105. {
  106. elements.swapWith (other.elements);
  107. std::swap (numAllocated, other.numAllocated);
  108. }
  109. //==============================================================================
  110. HeapBlock<ElementType> elements;
  111. int numAllocated;
  112. private:
  113. CARLA_DECLARE_NON_COPY_CLASS (ArrayAllocationBase)
  114. };
  115. }
  116. #endif // WATER_ARRAYALLOCATIONBASE_H_INCLUDED