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.

HeapBlock.h 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 void 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 operator void*() const noexcept { return static_cast<void*> (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 const void*() const noexcept { return static_cast<const void*> (data); }
  111. /** Lets you use indirect calls to the first element in the array.
  112. Obviously this will cause problems if the array hasn't been initialised, because it'll
  113. be referencing a null pointer.
  114. */
  115. inline ElementType* operator->() const noexcept { return data; }
  116. /** Returns a reference to one of the data elements.
  117. Obviously there's no bounds-checking here, as this object is just a dumb pointer and
  118. has no idea of the size it currently has allocated.
  119. */
  120. template <typename IndexType>
  121. inline ElementType& operator[] (IndexType index) const noexcept { return data [index]; }
  122. /** Returns a pointer to a data element at an offset from the start of the array.
  123. This is the same as doing pointer arithmetic on the raw pointer itself.
  124. */
  125. template <typename IndexType>
  126. inline ElementType* operator+ (IndexType index) const noexcept { return data + index; }
  127. //==============================================================================
  128. /** Compares the pointer with another pointer.
  129. This can be handy for checking whether this is a null pointer.
  130. */
  131. inline bool operator== (const ElementType* const otherPointer) const noexcept { return otherPointer == data; }
  132. /** Compares the pointer with another pointer.
  133. This can be handy for checking whether this is a null pointer.
  134. */
  135. inline bool operator!= (const ElementType* const otherPointer) const noexcept { return otherPointer != data; }
  136. //==============================================================================
  137. /** Allocates a specified amount of memory.
  138. This uses the normal malloc to allocate an amount of memory for this object.
  139. Any previously allocated memory will be freed by this method.
  140. The number of bytes allocated will be (newNumElements * elementSize). Normally
  141. you wouldn't need to specify the second parameter, but it can be handy if you need
  142. to allocate a size in bytes rather than in terms of the number of elements.
  143. The data that is allocated will be freed when this object is deleted, or when you
  144. call free() or any of the allocation methods.
  145. */
  146. bool malloc (const size_t newNumElements, const size_t elementSize = sizeof (ElementType)) noexcept
  147. {
  148. std::free (data);
  149. data = static_cast<ElementType*> (std::malloc (newNumElements * elementSize));
  150. return data != nullptr;
  151. }
  152. /** Allocates a specified amount of memory and clears it.
  153. This does the same job as the malloc() method, but clears the memory that it allocates.
  154. */
  155. bool calloc (const size_t newNumElements, const size_t elementSize = sizeof (ElementType)) noexcept
  156. {
  157. std::free (data);
  158. data = static_cast<ElementType*> (std::calloc (newNumElements, elementSize));
  159. return data != nullptr;
  160. }
  161. /** Allocates a specified amount of memory and optionally clears it.
  162. This does the same job as either malloc() or calloc(), depending on the
  163. initialiseToZero parameter.
  164. */
  165. bool allocate (const size_t newNumElements, bool initialiseToZero) noexcept
  166. {
  167. std::free (data);
  168. data = static_cast<ElementType*> (initialiseToZero
  169. ? std::calloc (newNumElements, sizeof (ElementType))
  170. : std::malloc (newNumElements * sizeof (ElementType)));
  171. return data != nullptr;
  172. }
  173. /** Re-allocates a specified amount of memory.
  174. The semantics of this method are the same as malloc() and calloc(), but it
  175. uses realloc() to keep as much of the existing data as possible.
  176. */
  177. bool realloc (const size_t newNumElements, const size_t elementSize = sizeof (ElementType)) noexcept
  178. {
  179. data = static_cast<ElementType*> (data == nullptr ? std::malloc (newNumElements * elementSize)
  180. : std::realloc (data, newNumElements * elementSize));
  181. return data != nullptr;
  182. }
  183. /** Frees any currently-allocated data.
  184. This will free the data and reset this object to be a null pointer.
  185. */
  186. void free() noexcept
  187. {
  188. std::free (data);
  189. data = nullptr;
  190. }
  191. /** Swaps this object's data with the data of another HeapBlock.
  192. The two objects simply exchange their data pointers.
  193. */
  194. void swapWith (HeapBlock<ElementType>& other) noexcept
  195. {
  196. std::swap (data, other.data);
  197. }
  198. /** This fills the block with zeros, up to the number of elements specified.
  199. Since the block has no way of knowing its own size, you must make sure that the number of
  200. elements you specify doesn't exceed the allocated size.
  201. */
  202. void clear (size_t numElements) noexcept
  203. {
  204. zeromem (data, sizeof (ElementType) * numElements);
  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_COPY_CLASS(HeapBlock)
  212. };
  213. }
  214. #endif // WATER_HEAPBLOCK_H_INCLUDED