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
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the Water library.
  4. Copyright (c) 2016 ROLI Ltd.
  5. Copyright (C) 2017-2022 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. //==============================================================================
  79. /** Returns a raw pointer to the allocated data.
  80. This may be a null pointer if the data hasn't yet been allocated, or if it has been
  81. freed by calling the free() method.
  82. */
  83. inline operator ElementType*() const noexcept { return data; }
  84. /** Returns a raw pointer to the allocated data.
  85. This may be a null pointer if the data hasn't yet been allocated, or if it has been
  86. freed by calling the free() method.
  87. */
  88. inline ElementType* getData() const noexcept { return data; }
  89. /** Returns a raw pointer to the allocated data.
  90. This may be a null pointer if the data hasn't yet been allocated, or if it has been
  91. freed by calling the free() method.
  92. */
  93. inline const ElementType* getConstData() const noexcept { return data; }
  94. /** Returns a void pointer to the allocated data.
  95. This may be a null pointer if the data hasn't yet been allocated, or if it has been
  96. freed by calling the free() method.
  97. */
  98. inline operator void*() const noexcept { return static_cast<void*> (data); }
  99. /** Returns a void pointer to the allocated data.
  100. This may be a null pointer if the data hasn't yet been allocated, or if it has been
  101. freed by calling the free() method.
  102. */
  103. inline operator const void*() const noexcept { return static_cast<const void*> (data); }
  104. /** Lets you use indirect calls to the first element in the array.
  105. Obviously this will cause problems if the array hasn't been initialised, because it'll
  106. be referencing a null pointer.
  107. */
  108. inline ElementType* operator->() const noexcept { return data; }
  109. /** Returns a reference to one of the data elements.
  110. Obviously there's no bounds-checking here, as this object is just a dumb pointer and
  111. has no idea of the size it currently has allocated.
  112. */
  113. template <typename IndexType>
  114. inline ElementType& operator[] (IndexType index) const noexcept { return data [index]; }
  115. /** Returns a pointer to a data element at an offset from the start of the array.
  116. This is the same as doing pointer arithmetic on the raw pointer itself.
  117. */
  118. template <typename IndexType>
  119. inline ElementType* operator+ (IndexType index) const noexcept { return data + index; }
  120. //==============================================================================
  121. /** Compares the pointer with another pointer.
  122. This can be handy for checking whether this is a null pointer.
  123. */
  124. inline bool operator== (const ElementType* const otherPointer) const noexcept { return otherPointer == data; }
  125. /** Compares the pointer with another pointer.
  126. This can be handy for checking whether this is a null pointer.
  127. */
  128. inline bool operator!= (const ElementType* const otherPointer) const noexcept { return otherPointer != data; }
  129. //==============================================================================
  130. /** Allocates a specified amount of memory.
  131. This uses the normal malloc to allocate an amount of memory for this object.
  132. Any previously allocated memory will be freed by this method.
  133. The number of bytes allocated will be (newNumElements * elementSize). Normally
  134. you wouldn't need to specify the second parameter, but it can be handy if you need
  135. to allocate a size in bytes rather than in terms of the number of elements.
  136. The data that is allocated will be freed when this object is deleted, or when you
  137. call free() or any of the allocation methods.
  138. */
  139. bool malloc (const size_t newNumElements, const size_t elementSize = sizeof (ElementType)) noexcept
  140. {
  141. std::free (data);
  142. data = static_cast<ElementType*> (std::malloc (newNumElements * elementSize));
  143. return data != nullptr;
  144. }
  145. /** Allocates a specified amount of memory and clears it.
  146. This does the same job as the malloc() method, but clears the memory that it allocates.
  147. */
  148. bool calloc (const size_t newNumElements, const size_t elementSize = sizeof (ElementType)) noexcept
  149. {
  150. std::free (data);
  151. data = static_cast<ElementType*> (std::calloc (newNumElements, elementSize));
  152. return data != nullptr;
  153. }
  154. /** Allocates a specified amount of memory and optionally clears it.
  155. This does the same job as either malloc() or calloc(), depending on the
  156. initialiseToZero parameter.
  157. */
  158. bool allocate (const size_t newNumElements, bool initialiseToZero) noexcept
  159. {
  160. std::free (data);
  161. data = static_cast<ElementType*> (initialiseToZero
  162. ? std::calloc (newNumElements, sizeof (ElementType))
  163. : std::malloc (newNumElements * sizeof (ElementType)));
  164. return data != nullptr;
  165. }
  166. /** Re-allocates a specified amount of memory.
  167. The semantics of this method are the same as malloc() and calloc(), but it
  168. uses realloc() to keep as much of the existing data as possible.
  169. */
  170. bool realloc (const size_t newNumElements, const size_t elementSize = sizeof (ElementType)) noexcept
  171. {
  172. data = static_cast<ElementType*> (data == nullptr ? std::malloc (newNumElements * elementSize)
  173. : std::realloc (data, newNumElements * elementSize));
  174. return data != nullptr;
  175. }
  176. /** Frees any currently-allocated data.
  177. This will free the data and reset this object to be a null pointer.
  178. */
  179. void free() noexcept
  180. {
  181. std::free (data);
  182. data = nullptr;
  183. }
  184. /** Swaps this object's data with the data of another HeapBlock.
  185. The two objects simply exchange their data pointers.
  186. */
  187. void swapWith (HeapBlock<ElementType>& other) noexcept
  188. {
  189. std::swap (data, other.data);
  190. }
  191. /** This fills the block with zeros, up to the number of elements specified.
  192. Since the block has no way of knowing its own size, you must make sure that the number of
  193. elements you specify doesn't exceed the allocated size.
  194. */
  195. void clear (size_t numElements) noexcept
  196. {
  197. zeromem (data, sizeof (ElementType) * numElements);
  198. }
  199. /** Release this object's data ownership, returning the data pointer. */
  200. ElementType* release() noexcept
  201. {
  202. ElementType* const r = data;
  203. data = nullptr;
  204. return r;
  205. }
  206. /** This typedef can be used to get the type of the heapblock's elements. */
  207. typedef ElementType Type;
  208. private:
  209. //==============================================================================
  210. ElementType* data;
  211. CARLA_DECLARE_NON_COPYABLE(HeapBlock)
  212. };
  213. }
  214. #endif // WATER_HEAPBLOCK_H_INCLUDED