MMBuffer.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Tencent is pleased to support the open source community by making
  3. * MMKV available.
  4. *
  5. * Copyright (C) 2018 THL A29 Limited, a Tencent company.
  6. * All rights reserved.
  7. *
  8. * Licensed under the BSD 3-Clause License (the "License"); you may not use
  9. * this file except in compliance with the License. You may obtain a copy of
  10. * the License at
  11. *
  12. * https://opensource.org/licenses/BSD-3-Clause
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. #define NOMINMAX // undefine max/min
  21. #include "MMBuffer.h"
  22. #include <cerrno>
  23. #include <cstdlib>
  24. #include <cstring>
  25. #include <utility>
  26. #include <stdexcept>
  27. #include <algorithm>
  28. #ifdef MMKV_APPLE
  29. # if __has_feature(objc_arc)
  30. # error This file must be compiled with MRC. Use -fno-objc-arc flag.
  31. # endif
  32. #endif
  33. using namespace std;
  34. namespace mmkv {
  35. MMBuffer::MMBuffer(size_t length) {
  36. if (length > SmallBufferSize()) {
  37. type = MMBufferType_Normal;
  38. isNoCopy = MMBufferCopy;
  39. size = length;
  40. ptr = malloc(size);
  41. if (!ptr) {
  42. throw std::runtime_error(strerror(errno));
  43. }
  44. #ifdef MMKV_APPLE
  45. m_data = nil;
  46. #endif
  47. } else {
  48. type = MMBufferType_Small;
  49. paddedSize = static_cast<uint8_t>(length);
  50. }
  51. }
  52. MMBuffer::MMBuffer(void *source, size_t length, MMBufferCopyFlag flag) : isNoCopy(flag) {
  53. if (isNoCopy == MMBufferCopy) {
  54. if (length > SmallBufferSize()) {
  55. type = MMBufferType_Normal;
  56. size = length;
  57. ptr = malloc(size);
  58. if (!ptr) {
  59. throw std::runtime_error(strerror(errno));
  60. }
  61. memcpy(ptr, source, size);
  62. #ifdef MMKV_APPLE
  63. m_data = nil;
  64. #endif
  65. } else {
  66. type = MMBufferType_Small;
  67. paddedSize = static_cast<uint8_t>(length);
  68. memcpy(paddedBuffer, source, length);
  69. }
  70. } else {
  71. type = MMBufferType_Normal;
  72. size = length;
  73. ptr = source;
  74. #ifdef MMKV_APPLE
  75. m_data = nil;
  76. #endif
  77. }
  78. }
  79. bool MMBuffer::operator==(const MMBuffer& other) const {
  80. if (this->length() != other.length()) {
  81. return false;
  82. }
  83. return !memcmp((uint8_t*)this->getPtr(), (uint8_t*)other.getPtr(), this->length());
  84. }
  85. #ifdef MMKV_APPLE
  86. MMBuffer::MMBuffer(NSData *data, MMBufferCopyFlag flag)
  87. : type(MMBufferType_Normal), ptr((void *) data.bytes), size(data.length), isNoCopy(flag) {
  88. if (isNoCopy == MMBufferCopy) {
  89. m_data = [data retain];
  90. } else {
  91. m_data = data;
  92. }
  93. }
  94. #endif
  95. MMBuffer::MMBuffer(MMBuffer &&other) noexcept : type(other.type) {
  96. if (type == MMBufferType_Normal) {
  97. size = other.size;
  98. ptr = other.ptr;
  99. isNoCopy = other.isNoCopy;
  100. #ifdef MMKV_APPLE
  101. m_data = other.m_data;
  102. #endif
  103. other.detach();
  104. } else {
  105. paddedSize = other.paddedSize;
  106. memcpy(paddedBuffer, other.paddedBuffer, paddedSize);
  107. }
  108. }
  109. MMBuffer::MMBuffer(MMBuffer &&other, size_t length) noexcept : type(other.type) {
  110. if (type == MMBufferType_Normal) {
  111. size = std::min(other.size, length);
  112. ptr = other.ptr;
  113. isNoCopy = other.isNoCopy;
  114. #ifdef MMKV_APPLE
  115. m_data = other.m_data;
  116. #endif
  117. other.detach();
  118. } else {
  119. paddedSize = std::min(other.paddedSize, static_cast<uint8_t>(length));
  120. memcpy(paddedBuffer, other.paddedBuffer, paddedSize);
  121. }
  122. }
  123. MMBuffer &MMBuffer::operator=(MMBuffer &&other) noexcept {
  124. if (type == MMBufferType_Normal) {
  125. if (other.type == MMBufferType_Normal) {
  126. std::swap(isNoCopy, other.isNoCopy);
  127. std::swap(size, other.size);
  128. std::swap(ptr, other.ptr);
  129. #ifdef MMKV_APPLE
  130. std::swap(m_data, other.m_data);
  131. #endif
  132. } else {
  133. type = MMBufferType_Small;
  134. if (isNoCopy == MMBufferCopy) {
  135. #ifdef MMKV_APPLE
  136. if (m_data) {
  137. [m_data release];
  138. } else if (ptr) {
  139. free(ptr);
  140. }
  141. #else
  142. if (ptr) {
  143. free(ptr);
  144. }
  145. #endif
  146. }
  147. paddedSize = other.paddedSize;
  148. memcpy(paddedBuffer, other.paddedBuffer, paddedSize);
  149. }
  150. } else {
  151. if (other.type == MMBufferType_Normal) {
  152. type = MMBufferType_Normal;
  153. isNoCopy = other.isNoCopy;
  154. size = other.size;
  155. ptr = other.ptr;
  156. #ifdef MMKV_APPLE
  157. m_data = other.m_data;
  158. #endif
  159. other.detach();
  160. } else {
  161. uint8_t tmp[SmallBufferSize()];
  162. memcpy(tmp, other.paddedBuffer, other.paddedSize);
  163. memcpy(other.paddedBuffer, paddedBuffer, paddedSize);
  164. memcpy(paddedBuffer, tmp, other.paddedSize);
  165. std::swap(paddedSize, other.paddedSize);
  166. }
  167. }
  168. return *this;
  169. }
  170. MMBuffer::~MMBuffer() {
  171. if (isStoredOnStack()) {
  172. return;
  173. }
  174. #ifdef MMKV_APPLE
  175. if (m_data) {
  176. if (isNoCopy == MMBufferCopy) {
  177. [m_data release];
  178. }
  179. return;
  180. }
  181. #endif
  182. if (isNoCopy == MMBufferCopy && ptr) {
  183. free(ptr);
  184. }
  185. }
  186. void MMBuffer::detach() {
  187. // type = MMBufferType_Small;
  188. // paddedSize = 0;
  189. auto memsetPtr = (size_t *) &type;
  190. *memsetPtr = 0;
  191. }
  192. } // namespace mmkv