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.

263 lines
8.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the Water library.
  4. Copyright (c) 2016 ROLI Ltd.
  5. Copyright (C) 2017-2019 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. #if WATER_COMPILER_SUPPORTS_MOVE_SEMANTICS
  36. private:
  37. #if defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 5
  38. template <typename T>
  39. struct IsTriviallyCopyable : std::integral_constant<bool, false> {};
  40. #else
  41. template <typename T>
  42. using IsTriviallyCopyable = std::is_trivially_copyable<T>;
  43. #endif
  44. template <typename T>
  45. using TriviallyCopyableVoid = typename std::enable_if<IsTriviallyCopyable<T>::value, void>::type;
  46. template <typename T>
  47. using TriviallyCopyableBool = typename std::enable_if<IsTriviallyCopyable<T>::value, bool>::type;
  48. template <typename T>
  49. using NonTriviallyCopyableVoid = typename std::enable_if<! IsTriviallyCopyable<T>::value, void>::type;
  50. template <typename T>
  51. using NonTriviallyCopyableBool = typename std::enable_if<! IsTriviallyCopyable<T>::value, bool>::type;
  52. #endif // WATER_COMPILER_SUPPORTS_MOVE_SEMANTICS
  53. public:
  54. //==============================================================================
  55. /** Creates an empty array. */
  56. ArrayAllocationBase() noexcept
  57. : elements(),
  58. numAllocated(0)
  59. {
  60. }
  61. /** Destructor. */
  62. ~ArrayAllocationBase() noexcept
  63. {
  64. }
  65. #if WATER_COMPILER_SUPPORTS_MOVE_SEMANTICS
  66. ArrayAllocationBase (ArrayAllocationBase<ElementType>&& other) noexcept
  67. : elements (static_cast<HeapBlock<ElementType>&&> (other.elements)),
  68. numAllocated (other.numAllocated) {}
  69. ArrayAllocationBase& operator= (ArrayAllocationBase<ElementType>&& other) noexcept
  70. {
  71. elements = static_cast<HeapBlock<ElementType>&&> (other.elements);
  72. numAllocated = other.numAllocated;
  73. return *this;
  74. }
  75. #endif
  76. //==============================================================================
  77. /** Changes the amount of storage allocated.
  78. This will retain any data currently held in the array, and either add or
  79. remove extra space at the end.
  80. @param numNewElements the number of elements that are needed
  81. */
  82. #if WATER_COMPILER_SUPPORTS_MOVE_SEMANTICS
  83. template <typename T = ElementType> TriviallyCopyableBool<T>
  84. #else
  85. bool
  86. #endif
  87. setAllocatedSize (const size_t numNewElements) noexcept
  88. {
  89. if (numAllocated != numNewElements)
  90. {
  91. if (numNewElements > 0)
  92. {
  93. if (! elements.realloc ((size_t) numNewElements))
  94. return false;
  95. }
  96. else
  97. {
  98. elements.free();
  99. }
  100. numAllocated = numNewElements;
  101. }
  102. return true;
  103. }
  104. #if WATER_COMPILER_SUPPORTS_MOVE_SEMANTICS
  105. template <typename T = ElementType>
  106. NonTriviallyCopyableBool<T> setAllocatedSize (const size_t numNewElements) noexcept
  107. {
  108. if (numAllocated == numNewElements)
  109. return true;
  110. if (numNewElements > 0)
  111. {
  112. HeapBlock<ElementType> newElements;
  113. if (! newElements.malloc (numNewElements))
  114. return false;
  115. size_t i = 0;
  116. for (; i < numAllocated && i < numNewElements; ++i)
  117. {
  118. new (newElements + i) ElementType (std::move (elements[i]));
  119. elements[i].~ElementType();
  120. }
  121. for (; i < numNewElements; ++i)
  122. new (newElements + i) ElementType ();
  123. elements = std::move (newElements);
  124. }
  125. else
  126. {
  127. elements.free();
  128. }
  129. numAllocated = numNewElements;
  130. return true;
  131. }
  132. #endif
  133. /** Increases the amount of storage allocated if it is less than a given amount.
  134. This will retain any data currently held in the array, but will add
  135. extra space at the end to make sure there it's at least as big as the size
  136. passed in. If it's already bigger, no action is taken.
  137. @param minNumElements the minimum number of elements that are needed
  138. */
  139. bool ensureAllocatedSize (const size_t minNumElements) noexcept
  140. {
  141. if (minNumElements > numAllocated)
  142. return setAllocatedSize ((minNumElements + minNumElements / 2U + 8U) & ~7U);
  143. return true;
  144. }
  145. /** Minimises the amount of storage allocated so that it's no more than
  146. the given number of elements.
  147. */
  148. bool shrinkToNoMoreThan (const size_t maxNumElements) noexcept
  149. {
  150. if (maxNumElements < numAllocated)
  151. return setAllocatedSize (maxNumElements);
  152. return true;
  153. }
  154. /** Swap the contents of two objects. */
  155. void swapWith (ArrayAllocationBase <ElementType>& other) noexcept
  156. {
  157. elements.swapWith (other.elements);
  158. std::swap (numAllocated, other.numAllocated);
  159. }
  160. #if WATER_COMPILER_SUPPORTS_MOVE_SEMANTICS
  161. template <typename T = ElementType> TriviallyCopyableVoid<T>
  162. #else
  163. void
  164. #endif
  165. moveMemory (ElementType* target, const ElementType* source, const size_t numElements) noexcept
  166. {
  167. CARLA_SAFE_ASSERT_RETURN(target != nullptr,);
  168. CARLA_SAFE_ASSERT_RETURN(source != nullptr,);
  169. CARLA_SAFE_ASSERT_RETURN(target != source,);
  170. CARLA_SAFE_ASSERT_RETURN(numElements != 0,);
  171. std::memmove (target, source, ((size_t) numElements) * sizeof (ElementType));
  172. }
  173. #if WATER_COMPILER_SUPPORTS_MOVE_SEMANTICS
  174. template <typename T = ElementType>
  175. NonTriviallyCopyableVoid<T> moveMemory (ElementType* target, const ElementType* source, const size_t numElements) noexcept
  176. {
  177. CARLA_SAFE_ASSERT_RETURN(target != nullptr,);
  178. CARLA_SAFE_ASSERT_RETURN(source != nullptr,);
  179. CARLA_SAFE_ASSERT_RETURN(target != source,);
  180. CARLA_SAFE_ASSERT_RETURN(numElements != 0,);
  181. if (target > source)
  182. {
  183. for (size_t i = 0; i < numElements; ++i)
  184. {
  185. moveElement (target, std::move (*source));
  186. ++target;
  187. ++source;
  188. }
  189. }
  190. else
  191. {
  192. for (size_t i = 0; i < numElements; ++i)
  193. {
  194. moveElement (target, std::move (*source));
  195. --target;
  196. --source;
  197. }
  198. }
  199. }
  200. void moveElement (ElementType* destination, const ElementType&& source)
  201. {
  202. destination->~ElementType();
  203. new (destination) ElementType (std::move (source));
  204. }
  205. #endif
  206. //==============================================================================
  207. HeapBlock<ElementType> elements;
  208. size_t numAllocated;
  209. private:
  210. CARLA_DECLARE_NON_COPY_CLASS (ArrayAllocationBase)
  211. };
  212. }
  213. #endif // WATER_ARRAYALLOCATIONBASE_H_INCLUDED