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.

277 lines
11KB

  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_HEAPBLOCK_H_INCLUDED
  21. #define WATER_HEAPBLOCK_H_INCLUDED
  22. #include "Memory.h"
  23. #include "../maths/MathsFunctions.h"
  24. namespace water {
  25. //==============================================================================
  26. /**
  27. Very simple container class to hold a pointer to some data on the heap.
  28. When you need to allocate some heap storage for something, always try to use
  29. this class instead of allocating the memory directly using malloc/free.
  30. A HeapBlock<char> object can be treated in pretty much exactly the same way
  31. as an char*, but as long as you allocate it on the stack or as a class member,
  32. it's almost impossible for it to leak memory.
  33. It also makes your code much more concise and readable than doing the same thing
  34. using direct allocations,
  35. E.g. instead of this:
  36. @code
  37. int* temp = (int*) malloc (1024 * sizeof (int));
  38. memcpy (temp, xyz, 1024 * sizeof (int));
  39. free (temp);
  40. temp = (int*) calloc (2048 * sizeof (int));
  41. temp[0] = 1234;
  42. memcpy (foobar, temp, 2048 * sizeof (int));
  43. free (temp);
  44. @endcode
  45. ..you could just write this:
  46. @code
  47. HeapBlock<int> temp (1024);
  48. memcpy (temp, xyz, 1024 * sizeof (int));
  49. temp.calloc (2048);
  50. temp[0] = 1234;
  51. memcpy (foobar, temp, 2048 * sizeof (int));
  52. @endcode
  53. The class is extremely lightweight, containing only a pointer to the
  54. data, and exposes malloc/realloc/calloc/free methods that do the same jobs
  55. as their less object-oriented counterparts. Despite adding safety, you probably
  56. won't sacrifice any performance by using this in place of normal pointers.
  57. @see Array, OwnedArray, MemoryBlock
  58. */
  59. template <class ElementType>
  60. class HeapBlock
  61. {
  62. public:
  63. //==============================================================================
  64. /** Creates a HeapBlock which is initially just a null pointer.
  65. After creation, you can resize the array using the malloc(), calloc(),
  66. or realloc() methods.
  67. */
  68. HeapBlock() noexcept : data (nullptr)
  69. {
  70. }
  71. /** Destructor.
  72. This will free the data, if any has been allocated.
  73. */
  74. ~HeapBlock() noexcept
  75. {
  76. std::free (data);
  77. }
  78. #if WATER_COMPILER_SUPPORTS_MOVE_SEMANTICS
  79. HeapBlock (HeapBlock&& other) noexcept
  80. : data (other.data)
  81. {
  82. other.data = nullptr;
  83. }
  84. HeapBlock& operator= (HeapBlock&& other) noexcept
  85. {
  86. std::swap (data, other.data);
  87. return *this;
  88. }
  89. #endif
  90. //==============================================================================
  91. /** Returns a raw pointer to the allocated data.
  92. This may be a null pointer if the data hasn't yet been allocated, or if it has been
  93. freed by calling the free() method.
  94. */
  95. inline operator ElementType*() const noexcept { return data; }
  96. /** Returns a raw pointer to the allocated data.
  97. This may be a null pointer if the data hasn't yet been allocated, or if it has been
  98. freed by calling the free() method.
  99. */
  100. inline ElementType* getData() const noexcept { return data; }
  101. /** Returns a raw pointer to the allocated data.
  102. This may be a null pointer if the data hasn't yet been allocated, or if it has been
  103. freed by calling the free() method.
  104. */
  105. inline const ElementType* getConstData() const noexcept { return data; }
  106. /** Returns a void pointer to the allocated data.
  107. This may be a null pointer if the data hasn't yet been allocated, or if it has been
  108. freed by calling the free() method.
  109. */
  110. inline operator void*() const noexcept { return static_cast<void*> (data); }
  111. /** Returns a void pointer to the allocated data.
  112. This may be a null pointer if the data hasn't yet been allocated, or if it has been
  113. freed by calling the free() method.
  114. */
  115. inline operator const void*() const noexcept { return static_cast<const void*> (data); }
  116. /** Lets you use indirect calls to the first element in the array.
  117. Obviously this will cause problems if the array hasn't been initialised, because it'll
  118. be referencing a null pointer.
  119. */
  120. inline ElementType* operator->() const noexcept { return data; }
  121. /** Returns a reference to one of the data elements.
  122. Obviously there's no bounds-checking here, as this object is just a dumb pointer and
  123. has no idea of the size it currently has allocated.
  124. */
  125. template <typename IndexType>
  126. inline ElementType& operator[] (IndexType index) const noexcept { return data [index]; }
  127. /** Returns a pointer to a data element at an offset from the start of the array.
  128. This is the same as doing pointer arithmetic on the raw pointer itself.
  129. */
  130. template <typename IndexType>
  131. inline ElementType* operator+ (IndexType index) const noexcept { return data + index; }
  132. //==============================================================================
  133. /** Compares the pointer with another pointer.
  134. This can be handy for checking whether this is a null pointer.
  135. */
  136. inline bool operator== (const ElementType* const otherPointer) const noexcept { return otherPointer == data; }
  137. /** Compares the pointer with another pointer.
  138. This can be handy for checking whether this is a null pointer.
  139. */
  140. inline bool operator!= (const ElementType* const otherPointer) const noexcept { return otherPointer != data; }
  141. //==============================================================================
  142. /** Allocates a specified amount of memory.
  143. This uses the normal malloc to allocate an amount of memory for this object.
  144. Any previously allocated memory will be freed by this method.
  145. The number of bytes allocated will be (newNumElements * elementSize). Normally
  146. you wouldn't need to specify the second parameter, but it can be handy if you need
  147. to allocate a size in bytes rather than in terms of the number of elements.
  148. The data that is allocated will be freed when this object is deleted, or when you
  149. call free() or any of the allocation methods.
  150. */
  151. bool malloc (const size_t newNumElements, const size_t elementSize = sizeof (ElementType)) noexcept
  152. {
  153. std::free (data);
  154. data = static_cast<ElementType*> (std::malloc (newNumElements * elementSize));
  155. return data != nullptr;
  156. }
  157. /** Allocates a specified amount of memory and clears it.
  158. This does the same job as the malloc() method, but clears the memory that it allocates.
  159. */
  160. bool calloc (const size_t newNumElements, const size_t elementSize = sizeof (ElementType)) noexcept
  161. {
  162. std::free (data);
  163. data = static_cast<ElementType*> (std::calloc (newNumElements, elementSize));
  164. return data != nullptr;
  165. }
  166. /** Allocates a specified amount of memory and optionally clears it.
  167. This does the same job as either malloc() or calloc(), depending on the
  168. initialiseToZero parameter.
  169. */
  170. bool allocate (const size_t newNumElements, bool initialiseToZero) noexcept
  171. {
  172. std::free (data);
  173. data = static_cast<ElementType*> (initialiseToZero
  174. ? std::calloc (newNumElements, sizeof (ElementType))
  175. : std::malloc (newNumElements * sizeof (ElementType)));
  176. return data != nullptr;
  177. }
  178. /** Re-allocates a specified amount of memory.
  179. The semantics of this method are the same as malloc() and calloc(), but it
  180. uses realloc() to keep as much of the existing data as possible.
  181. */
  182. bool realloc (const size_t newNumElements, const size_t elementSize = sizeof (ElementType)) noexcept
  183. {
  184. data = static_cast<ElementType*> (data == nullptr ? std::malloc (newNumElements * elementSize)
  185. : std::realloc (data, newNumElements * elementSize));
  186. return data != nullptr;
  187. }
  188. /** Frees any currently-allocated data.
  189. This will free the data and reset this object to be a null pointer.
  190. */
  191. void free() noexcept
  192. {
  193. std::free (data);
  194. data = nullptr;
  195. }
  196. /** Swaps this object's data with the data of another HeapBlock.
  197. The two objects simply exchange their data pointers.
  198. */
  199. void swapWith (HeapBlock<ElementType>& other) noexcept
  200. {
  201. std::swap (data, other.data);
  202. }
  203. /** This fills the block with zeros, up to the number of elements specified.
  204. Since the block has no way of knowing its own size, you must make sure that the number of
  205. elements you specify doesn't exceed the allocated size.
  206. */
  207. void clear (size_t numElements) noexcept
  208. {
  209. zeromem (data, sizeof (ElementType) * numElements);
  210. }
  211. /** Release this object's data ownership, returning the data pointer. */
  212. ElementType* release() noexcept
  213. {
  214. ElementType* const r = data;
  215. data = nullptr;
  216. return r;
  217. }
  218. /** This typedef can be used to get the type of the heapblock's elements. */
  219. typedef ElementType Type;
  220. private:
  221. //==============================================================================
  222. ElementType* data;
  223. CARLA_DECLARE_NON_COPY_CLASS(HeapBlock)
  224. };
  225. }
  226. #endif // WATER_HEAPBLOCK_H_INCLUDED