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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. #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. //==============================================================================
  73. bool MemoryBlock::operator== (const MemoryBlock& other) const noexcept
  74. {
  75. return matches (other.data, other.size);
  76. }
  77. bool MemoryBlock::operator!= (const MemoryBlock& other) const noexcept
  78. {
  79. return ! operator== (other);
  80. }
  81. bool MemoryBlock::matches (const void* dataToCompare, size_t dataSize) const noexcept
  82. {
  83. return size == dataSize
  84. && memcmp (data, dataToCompare, size) == 0;
  85. }
  86. //==============================================================================
  87. // this will resize the block to this size
  88. void MemoryBlock::setSize (const size_t newSize, const bool initialiseToZero)
  89. {
  90. if (size != newSize)
  91. {
  92. if (newSize <= 0)
  93. {
  94. reset();
  95. }
  96. else
  97. {
  98. if (data != nullptr)
  99. {
  100. data.realloc (newSize);
  101. if (initialiseToZero && (newSize > size))
  102. zeromem (data + size, newSize - size);
  103. }
  104. else
  105. {
  106. data.allocate (newSize, initialiseToZero);
  107. }
  108. size = newSize;
  109. }
  110. }
  111. }
  112. void MemoryBlock::reset()
  113. {
  114. data.free();
  115. size = 0;
  116. }
  117. void MemoryBlock::ensureSize (const size_t minimumSize, const bool initialiseToZero)
  118. {
  119. if (size < minimumSize)
  120. setSize (minimumSize, initialiseToZero);
  121. }
  122. void MemoryBlock::swapWith (MemoryBlock& other) noexcept
  123. {
  124. std::swap (size, other.size);
  125. data.swapWith (other.data);
  126. }
  127. //==============================================================================
  128. void* MemoryBlock::release () noexcept
  129. {
  130. size = 0;
  131. return data.release();
  132. }
  133. //==============================================================================
  134. void MemoryBlock::fillWith (const uint8 value) noexcept
  135. {
  136. memset (data, (int) value, size);
  137. }
  138. void MemoryBlock::append (const void* const srcData, const size_t numBytes)
  139. {
  140. if (numBytes > 0)
  141. {
  142. wassert (srcData != nullptr); // this must not be null!
  143. const size_t oldSize = size;
  144. setSize (size + numBytes);
  145. memcpy (data + oldSize, srcData, numBytes);
  146. }
  147. }
  148. void MemoryBlock::replaceWith (const void* const srcData, const size_t numBytes)
  149. {
  150. if (numBytes > 0)
  151. {
  152. wassert (srcData != nullptr); // this must not be null!
  153. setSize (numBytes);
  154. memcpy (data, srcData, numBytes);
  155. }
  156. }
  157. void MemoryBlock::insert (const void* const srcData, const size_t numBytes, size_t insertPosition)
  158. {
  159. if (numBytes > 0)
  160. {
  161. wassert (srcData != nullptr); // this must not be null!
  162. insertPosition = jmin (size, insertPosition);
  163. const size_t trailingDataSize = size - insertPosition;
  164. setSize (size + numBytes, false);
  165. if (trailingDataSize > 0)
  166. memmove (data + insertPosition + numBytes,
  167. data + insertPosition,
  168. trailingDataSize);
  169. memcpy (data + insertPosition, srcData, numBytes);
  170. }
  171. }
  172. void MemoryBlock::removeSection (const size_t startByte, const size_t numBytesToRemove)
  173. {
  174. if (startByte + numBytesToRemove >= size)
  175. {
  176. setSize (startByte);
  177. }
  178. else if (numBytesToRemove > 0)
  179. {
  180. memmove (data + startByte,
  181. data + startByte + numBytesToRemove,
  182. size - (startByte + numBytesToRemove));
  183. setSize (size - numBytesToRemove);
  184. }
  185. }
  186. void MemoryBlock::copyFrom (const void* const src, int offset, size_t num) noexcept
  187. {
  188. const char* d = static_cast<const char*> (src);
  189. if (offset < 0)
  190. {
  191. d -= offset;
  192. num += (size_t) -offset;
  193. offset = 0;
  194. }
  195. if ((size_t) offset + num > size)
  196. num = size - (size_t) offset;
  197. if (num > 0)
  198. memcpy (data + offset, d, num);
  199. }
  200. void MemoryBlock::copyTo (void* const dst, int offset, size_t num) const noexcept
  201. {
  202. char* d = static_cast<char*> (dst);
  203. if (offset < 0)
  204. {
  205. zeromem (d, (size_t) -offset);
  206. d -= offset;
  207. num -= (size_t) -offset;
  208. offset = 0;
  209. }
  210. if ((size_t) offset + num > size)
  211. {
  212. const size_t newNum = (size_t) size - (size_t) offset;
  213. zeromem (d + newNum, num - newNum);
  214. num = newNum;
  215. }
  216. if (num > 0)
  217. memcpy (d, data + offset, num);
  218. }
  219. String MemoryBlock::toString() const
  220. {
  221. return String::fromUTF8 (data, (int) size);
  222. }
  223. //==============================================================================
  224. int MemoryBlock::getBitRange (const size_t bitRangeStart, size_t numBits) const noexcept
  225. {
  226. int res = 0;
  227. size_t byte = bitRangeStart >> 3;
  228. size_t offsetInByte = bitRangeStart & 7;
  229. size_t bitsSoFar = 0;
  230. while (numBits > 0 && (size_t) byte < size)
  231. {
  232. const size_t bitsThisTime = jmin (numBits, 8 - offsetInByte);
  233. const int mask = (0xff >> (8 - bitsThisTime)) << offsetInByte;
  234. res |= (((data[byte] & mask) >> offsetInByte) << bitsSoFar);
  235. bitsSoFar += bitsThisTime;
  236. numBits -= bitsThisTime;
  237. ++byte;
  238. offsetInByte = 0;
  239. }
  240. return res;
  241. }
  242. void MemoryBlock::setBitRange (const size_t bitRangeStart, size_t numBits, int bitsToSet) noexcept
  243. {
  244. size_t byte = bitRangeStart >> 3;
  245. size_t offsetInByte = bitRangeStart & 7;
  246. uint32 mask = ~((((uint32) 0xffffffff) << (32 - numBits)) >> (32 - numBits));
  247. while (numBits > 0 && (size_t) byte < size)
  248. {
  249. const size_t bitsThisTime = jmin (numBits, 8 - offsetInByte);
  250. const uint32 tempMask = (mask << offsetInByte) | ~((((uint32) 0xffffffff) >> offsetInByte) << offsetInByte);
  251. const uint32 tempBits = (uint32) bitsToSet << offsetInByte;
  252. data[byte] = (char) (((uint32) data[byte] & tempMask) | tempBits);
  253. ++byte;
  254. numBits -= bitsThisTime;
  255. bitsToSet >>= bitsThisTime;
  256. mask >>= bitsThisTime;
  257. offsetInByte = 0;
  258. }
  259. }
  260. //==============================================================================
  261. void MemoryBlock::loadFromHexString (StringRef hex)
  262. {
  263. ensureSize ((size_t) hex.length() >> 1);
  264. char* dest = data;
  265. CharPointer_UTF8 t (hex.text);
  266. for (;;)
  267. {
  268. int byte = 0;
  269. for (int loop = 2; --loop >= 0;)
  270. {
  271. byte <<= 4;
  272. for (;;)
  273. {
  274. const water_uchar c = t.getAndAdvance();
  275. if (c >= '0' && c <= '9') { byte |= c - '0'; break; }
  276. if (c >= 'a' && c <= 'z') { byte |= c - ('a' - 10); break; }
  277. if (c >= 'A' && c <= 'Z') { byte |= c - ('A' - 10); break; }
  278. if (c == 0)
  279. {
  280. setSize (static_cast<size_t> (dest - data));
  281. return;
  282. }
  283. }
  284. }
  285. *dest++ = (char) byte;
  286. }
  287. }
  288. }