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.

269 lines
8.6KB

  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 < numNewElements; ++i)
  117. {
  118. if (i < numAllocated)
  119. new (newElements + i) ElementType (std::move (elements[i]));
  120. else
  121. new (newElements + i) ElementType ();
  122. }
  123. for (; i < numAllocated; ++i)
  124. elements[i].~ElementType();
  125. elements = std::move (newElements);
  126. }
  127. else
  128. {
  129. elements.free();
  130. }
  131. numAllocated = numNewElements;
  132. return true;
  133. }
  134. #endif
  135. /** Increases the amount of storage allocated if it is less than a given amount.
  136. This will retain any data currently held in the array, but will add
  137. extra space at the end to make sure there it's at least as big as the size
  138. passed in. If it's already bigger, no action is taken.
  139. @param minNumElements the minimum number of elements that are needed
  140. */
  141. bool ensureAllocatedSize (const size_t minNumElements) noexcept
  142. {
  143. if (minNumElements > numAllocated)
  144. return setAllocatedSize ((minNumElements + minNumElements / 2U + 8U) & ~7U);
  145. return true;
  146. }
  147. /** Minimises the amount of storage allocated so that it's no more than
  148. the given number of elements.
  149. */
  150. bool shrinkToNoMoreThan (const size_t maxNumElements) noexcept
  151. {
  152. if (maxNumElements < numAllocated)
  153. return setAllocatedSize (maxNumElements);
  154. return true;
  155. }
  156. /** Swap the contents of two objects. */
  157. void swapWith (ArrayAllocationBase <ElementType>& other) noexcept
  158. {
  159. elements.swapWith (other.elements);
  160. std::swap (numAllocated, other.numAllocated);
  161. }
  162. #if WATER_COMPILER_SUPPORTS_MOVE_SEMANTICS
  163. template <typename T = ElementType> TriviallyCopyableVoid<T>
  164. #else
  165. void
  166. #endif
  167. moveMemory (ElementType* target, const ElementType* source, const size_t numElements) noexcept
  168. {
  169. CARLA_SAFE_ASSERT_RETURN(target != nullptr,);
  170. CARLA_SAFE_ASSERT_RETURN(source != nullptr,);
  171. CARLA_SAFE_ASSERT_RETURN(target != source,);
  172. CARLA_SAFE_ASSERT_RETURN(numElements != 0,);
  173. std::memmove (target, source, ((size_t) numElements) * sizeof (ElementType));
  174. }
  175. #if WATER_COMPILER_SUPPORTS_MOVE_SEMANTICS
  176. template <typename T = ElementType>
  177. NonTriviallyCopyableVoid<T> moveMemory (ElementType* target, const ElementType* source, const size_t numElements) noexcept
  178. {
  179. CARLA_SAFE_ASSERT_RETURN(target != nullptr,);
  180. CARLA_SAFE_ASSERT_RETURN(source != nullptr,);
  181. CARLA_SAFE_ASSERT_RETURN(target != source,);
  182. CARLA_SAFE_ASSERT_RETURN(numElements != 0,);
  183. if (target > source)
  184. {
  185. for (size_t i = 0; i < numElements; ++i)
  186. {
  187. moveElement (target, std::move (*source));
  188. ++target;
  189. ++source;
  190. }
  191. (--source)->~ElementType();
  192. }
  193. else
  194. {
  195. for (size_t i = 0; i < numElements; ++i)
  196. {
  197. moveElement (target, std::move (*source));
  198. --target;
  199. --source;
  200. }
  201. (++source)->~ElementType();
  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. size_t numAllocated;
  213. private:
  214. CARLA_DECLARE_NON_COPY_CLASS (ArrayAllocationBase)
  215. };
  216. }
  217. #endif // WATER_ARRAYALLOCATIONBASE_H_INCLUDED