MMBuffer.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. #ifdef MMKV_APPLE
  80. MMBuffer::MMBuffer(NSData *data, MMBufferCopyFlag flag)
  81. : type(MMBufferType_Normal), ptr((void *) data.bytes), size(data.length), isNoCopy(flag) {
  82. if (isNoCopy == MMBufferCopy) {
  83. m_data = [data retain];
  84. } else {
  85. m_data = data;
  86. }
  87. }
  88. #endif
  89. MMBuffer::MMBuffer(MMBuffer &&other) noexcept : type(other.type) {
  90. if (type == MMBufferType_Normal) {
  91. size = other.size;
  92. ptr = other.ptr;
  93. isNoCopy = other.isNoCopy;
  94. #ifdef MMKV_APPLE
  95. m_data = other.m_data;
  96. #endif
  97. other.detach();
  98. } else {
  99. paddedSize = other.paddedSize;
  100. memcpy(paddedBuffer, other.paddedBuffer, paddedSize);
  101. }
  102. }
  103. MMBuffer::MMBuffer(MMBuffer &&other, size_t length) noexcept : type(other.type) {
  104. if (type == MMBufferType_Normal) {
  105. size = std::min(other.size, length);
  106. ptr = other.ptr;
  107. isNoCopy = other.isNoCopy;
  108. #ifdef MMKV_APPLE
  109. m_data = other.m_data;
  110. #endif
  111. other.detach();
  112. } else {
  113. paddedSize = std::min(other.paddedSize, static_cast<uint8_t>(length));
  114. memcpy(paddedBuffer, other.paddedBuffer, paddedSize);
  115. }
  116. }
  117. MMBuffer &MMBuffer::operator=(MMBuffer &&other) noexcept {
  118. if (type == MMBufferType_Normal) {
  119. if (other.type == MMBufferType_Normal) {
  120. std::swap(isNoCopy, other.isNoCopy);
  121. std::swap(size, other.size);
  122. std::swap(ptr, other.ptr);
  123. #ifdef MMKV_APPLE
  124. std::swap(m_data, other.m_data);
  125. #endif
  126. } else {
  127. type = MMBufferType_Small;
  128. if (isNoCopy == MMBufferCopy) {
  129. #ifdef MMKV_APPLE
  130. if (m_data) {
  131. [m_data release];
  132. } else if (ptr) {
  133. free(ptr);
  134. }
  135. #else
  136. if (ptr) {
  137. free(ptr);
  138. }
  139. #endif
  140. }
  141. paddedSize = other.paddedSize;
  142. memcpy(paddedBuffer, other.paddedBuffer, paddedSize);
  143. }
  144. } else {
  145. if (other.type == MMBufferType_Normal) {
  146. type = MMBufferType_Normal;
  147. isNoCopy = other.isNoCopy;
  148. size = other.size;
  149. ptr = other.ptr;
  150. #ifdef MMKV_APPLE
  151. m_data = other.m_data;
  152. #endif
  153. other.detach();
  154. } else {
  155. uint8_t tmp[SmallBufferSize()];
  156. memcpy(tmp, other.paddedBuffer, other.paddedSize);
  157. memcpy(other.paddedBuffer, paddedBuffer, paddedSize);
  158. memcpy(paddedBuffer, tmp, other.paddedSize);
  159. std::swap(paddedSize, other.paddedSize);
  160. }
  161. }
  162. return *this;
  163. }
  164. MMBuffer::~MMBuffer() {
  165. if (isStoredOnStack()) {
  166. return;
  167. }
  168. #ifdef MMKV_APPLE
  169. if (m_data) {
  170. if (isNoCopy == MMBufferCopy) {
  171. [m_data release];
  172. }
  173. return;
  174. }
  175. #endif
  176. if (isNoCopy == MMBufferCopy && ptr) {
  177. free(ptr);
  178. }
  179. }
  180. void MMBuffer::detach() {
  181. // type = MMBufferType_Small;
  182. // paddedSize = 0;
  183. auto memsetPtr = (size_t *) &type;
  184. *memsetPtr = 0;
  185. }
  186. } // namespace mmkv