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.

MemoryBlock.cpp 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. ==============================================================================
  3. This file is part of the Water library.
  4. Copyright (c) 2016 ROLI Ltd.
  5. Copyright (C) 2017 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. #include "MemoryBlock.h"
  21. namespace water {
  22. MemoryBlock::MemoryBlock() noexcept
  23. : size (0)
  24. {
  25. }
  26. MemoryBlock::MemoryBlock (const size_t initialSize, const bool initialiseToZero)
  27. {
  28. if (initialSize > 0)
  29. {
  30. size = initialSize;
  31. data.allocate (initialSize, initialiseToZero);
  32. }
  33. else
  34. {
  35. size = 0;
  36. }
  37. }
  38. MemoryBlock::MemoryBlock (const MemoryBlock& other)
  39. : size (other.size)
  40. {
  41. if (size > 0)
  42. {
  43. wassert (other.data != nullptr);
  44. data.malloc (size);
  45. memcpy (data, other.data, size);
  46. }
  47. }
  48. MemoryBlock::MemoryBlock (const void* const dataToInitialiseFrom, const size_t sizeInBytes)
  49. : size (sizeInBytes)
  50. {
  51. wassert (((ssize_t) sizeInBytes) >= 0);
  52. if (size > 0)
  53. {
  54. wassert (dataToInitialiseFrom != nullptr); // non-zero size, but a zero pointer passed-in?
  55. data.malloc (size);
  56. if (dataToInitialiseFrom != nullptr)
  57. memcpy (data, dataToInitialiseFrom, size);
  58. }
  59. }
  60. MemoryBlock::~MemoryBlock() noexcept
  61. {
  62. }
  63. MemoryBlock& MemoryBlock::operator= (const MemoryBlock& other)
  64. {
  65. if (this != &other)
  66. {
  67. setSize (other.size, false);
  68. memcpy (data, other.data, size);
  69. }
  70. return *this;
  71. }
  72. #if WATER_COMPILER_SUPPORTS_MOVE_SEMANTICS
  73. MemoryBlock::MemoryBlock (MemoryBlock&& other) noexcept
  74. : data (static_cast<HeapBlock<char>&&> (other.data)),
  75. size (other.size)
  76. {
  77. }
  78. MemoryBlock& MemoryBlock::operator= (MemoryBlock&& other) noexcept
  79. {
  80. data = static_cast<HeapBlock<char>&&> (other.data);
  81. size = other.size;
  82. return *this;
  83. }
  84. #endif
  85. //==============================================================================
  86. bool MemoryBlock::operator== (const MemoryBlock& other) const noexcept
  87. {
  88. return matches (other.data, other.size);
  89. }
  90. bool MemoryBlock::operator!= (const MemoryBlock& other) const noexcept
  91. {
  92. return ! operator== (other);
  93. }
  94. bool MemoryBlock::matches (const void* dataToCompare, size_t dataSize) const noexcept
  95. {
  96. return size == dataSize
  97. && memcmp (data, dataToCompare, size) == 0;
  98. }
  99. //==============================================================================
  100. // this will resize the block to this size
  101. void MemoryBlock::setSize (const size_t newSize, const bool initialiseToZero)
  102. {
  103. if (size != newSize)
  104. {
  105. if (newSize <= 0)
  106. {
  107. reset();
  108. }
  109. else
  110. {
  111. if (data != nullptr)
  112. {
  113. data.realloc (newSize);
  114. if (initialiseToZero && (newSize > size))
  115. zeromem (data + size, newSize - size);
  116. }
  117. else
  118. {
  119. data.allocate (newSize, initialiseToZero);
  120. }
  121. size = newSize;
  122. }
  123. }
  124. }
  125. void MemoryBlock::reset()
  126. {
  127. data.free();
  128. size = 0;
  129. }
  130. void MemoryBlock::ensureSize (const size_t minimumSize, const bool initialiseToZero)
  131. {
  132. if (size < minimumSize)
  133. setSize (minimumSize, initialiseToZero);
  134. }
  135. void MemoryBlock::swapWith (MemoryBlock& other) noexcept
  136. {
  137. std::swap (size, other.size);
  138. data.swapWith (other.data);
  139. }
  140. //==============================================================================
  141. void MemoryBlock::fillWith (const uint8 value) noexcept
  142. {
  143. memset (data, (int) value, size);
  144. }
  145. void MemoryBlock::append (const void* const srcData, const size_t numBytes)
  146. {
  147. if (numBytes > 0)
  148. {
  149. wassert (srcData != nullptr); // this must not be null!
  150. const size_t oldSize = size;
  151. setSize (size + numBytes);
  152. memcpy (data + oldSize, srcData, numBytes);
  153. }
  154. }
  155. void MemoryBlock::replaceWith (const void* const srcData, const size_t numBytes)
  156. {
  157. if (numBytes > 0)
  158. {
  159. wassert (srcData != nullptr); // this must not be null!
  160. setSize (numBytes);
  161. memcpy (data, srcData, numBytes);
  162. }
  163. }
  164. void MemoryBlock::insert (const void* const srcData, const size_t numBytes, size_t insertPosition)
  165. {
  166. if (numBytes > 0)
  167. {
  168. wassert (srcData != nullptr); // this must not be null!
  169. insertPosition = jmin (size, insertPosition);
  170. const size_t trailingDataSize = size - insertPosition;
  171. setSize (size + numBytes, false);
  172. if (trailingDataSize > 0)
  173. memmove (data + insertPosition + numBytes,
  174. data + insertPosition,
  175. trailingDataSize);
  176. memcpy (data + insertPosition, srcData, numBytes);
  177. }
  178. }
  179. void MemoryBlock::removeSection (const size_t startByte, const size_t numBytesToRemove)
  180. {
  181. if (startByte + numBytesToRemove >= size)
  182. {
  183. setSize (startByte);
  184. }
  185. else if (numBytesToRemove > 0)
  186. {
  187. memmove (data + startByte,
  188. data + startByte + numBytesToRemove,
  189. size - (startByte + numBytesToRemove));
  190. setSize (size - numBytesToRemove);
  191. }
  192. }
  193. void MemoryBlock::copyFrom (const void* const src, int offset, size_t num) noexcept
  194. {
  195. const char* d = static_cast<const char*> (src);
  196. if (offset < 0)
  197. {
  198. d -= offset;
  199. num += (size_t) -offset;
  200. offset = 0;
  201. }
  202. if ((size_t) offset + num > size)
  203. num = size - (size_t) offset;
  204. if (num > 0)
  205. memcpy (data + offset, d, num);
  206. }
  207. void MemoryBlock::copyTo (void* const dst, int offset, size_t num) const noexcept
  208. {
  209. char* d = static_cast<char*> (dst);
  210. if (offset < 0)
  211. {
  212. zeromem (d, (size_t) -offset);
  213. d -= offset;
  214. num -= (size_t) -offset;
  215. offset = 0;
  216. }
  217. if ((size_t) offset + num > size)
  218. {
  219. const size_t newNum = (size_t) size - (size_t) offset;
  220. zeromem (d + newNum, num - newNum);
  221. num = newNum;
  222. }
  223. if (num > 0)
  224. memcpy (d, data + offset, num);
  225. }
  226. String MemoryBlock::toString() const
  227. {
  228. return String::fromUTF8 (data, (int) size);
  229. }
  230. //==============================================================================
  231. int MemoryBlock::getBitRange (const size_t bitRangeStart, size_t numBits) const noexcept
  232. {
  233. int res = 0;
  234. size_t byte = bitRangeStart >> 3;
  235. size_t offsetInByte = bitRangeStart & 7;
  236. size_t bitsSoFar = 0;
  237. while (numBits > 0 && (size_t) byte < size)
  238. {
  239. const size_t bitsThisTime = jmin (numBits, 8 - offsetInByte);
  240. const int mask = (0xff >> (8 - bitsThisTime)) << offsetInByte;
  241. res |= (((data[byte] & mask) >> offsetInByte) << bitsSoFar);
  242. bitsSoFar += bitsThisTime;
  243. numBits -= bitsThisTime;
  244. ++byte;
  245. offsetInByte = 0;
  246. }
  247. return res;
  248. }
  249. void MemoryBlock::setBitRange (const size_t bitRangeStart, size_t numBits, int bitsToSet) noexcept
  250. {
  251. size_t byte = bitRangeStart >> 3;
  252. size_t offsetInByte = bitRangeStart & 7;
  253. uint32 mask = ~((((uint32) 0xffffffff) << (32 - numBits)) >> (32 - numBits));
  254. while (numBits > 0 && (size_t) byte < size)
  255. {
  256. const size_t bitsThisTime = jmin (numBits, 8 - offsetInByte);
  257. const uint32 tempMask = (mask << offsetInByte) | ~((((uint32) 0xffffffff) >> offsetInByte) << offsetInByte);
  258. const uint32 tempBits = (uint32) bitsToSet << offsetInByte;
  259. data[byte] = (char) (((uint32) data[byte] & tempMask) | tempBits);
  260. ++byte;
  261. numBits -= bitsThisTime;
  262. bitsToSet >>= bitsThisTime;
  263. mask >>= bitsThisTime;
  264. offsetInByte = 0;
  265. }
  266. }
  267. //==============================================================================
  268. void MemoryBlock::loadFromHexString (StringRef hex)
  269. {
  270. ensureSize ((size_t) hex.length() >> 1);
  271. char* dest = data;
  272. String::CharPointerType t (hex.text);
  273. for (;;)
  274. {
  275. int byte = 0;
  276. for (int loop = 2; --loop >= 0;)
  277. {
  278. byte <<= 4;
  279. for (;;)
  280. {
  281. const water_uchar c = t.getAndAdvance();
  282. if (c >= '0' && c <= '9') { byte |= c - '0'; break; }
  283. if (c >= 'a' && c <= 'z') { byte |= c - ('a' - 10); break; }
  284. if (c >= 'A' && c <= 'Z') { byte |= c - ('A' - 10); break; }
  285. if (c == 0)
  286. {
  287. setSize (static_cast<size_t> (dest - data));
  288. return;
  289. }
  290. }
  291. }
  292. *dest++ = (char) byte;
  293. }
  294. }
  295. }