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.

265 lines
8.6KB

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