react-native-mmkv 3.2.0 → 4.0.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (104) hide show
  1. package/android/CMakeLists.txt +18 -9
  2. package/android/build.gradle +6 -2
  3. package/cpp/{MMKVManagedBuffer.h → ManagedMMBuffer.h} +6 -6
  4. package/cpp/MmkvHostObject.cpp +22 -17
  5. package/cpp/MmkvHostObject.h +1 -2
  6. package/cpp/MmkvTypes.h +50 -0
  7. package/cpp/NativeMmkvModule.cpp +3 -3
  8. package/cpp/NativeMmkvModule.h +1 -8
  9. package/lib/commonjs/MMKV.js.map +1 -1
  10. package/lib/commonjs/Types.js.map +1 -1
  11. package/lib/commonjs/createMMKV.web.js +1 -1
  12. package/lib/commonjs/createMMKV.web.js.map +1 -1
  13. package/lib/module/MMKV.js.map +1 -1
  14. package/lib/module/Types.js.map +1 -1
  15. package/lib/module/createMMKV.web.js +1 -1
  16. package/lib/module/createMMKV.web.js.map +1 -1
  17. package/lib/typescript/src/MMKV.d.ts +1 -1
  18. package/lib/typescript/src/MMKV.d.ts.map +1 -1
  19. package/lib/typescript/src/Types.d.ts +7 -1
  20. package/lib/typescript/src/Types.d.ts.map +1 -1
  21. package/lib/typescript/src/hooks.d.ts +1 -1
  22. package/lib/typescript/src/hooks.d.ts.map +1 -1
  23. package/package.json +22 -27
  24. package/react-native-mmkv.podspec +4 -10
  25. package/react-native.config.js +9 -0
  26. package/src/MMKV.ts +1 -1
  27. package/src/Types.ts +7 -1
  28. package/src/createMMKV.web.ts +1 -1
  29. package/src/hooks.ts +1 -1
  30. package/MMKV/Core/CMakeLists.txt +0 -172
  31. package/MMKV/Core/CodedInputData.cpp +0 -252
  32. package/MMKV/Core/CodedInputData.h +0 -87
  33. package/MMKV/Core/CodedInputDataCrypt.cpp +0 -280
  34. package/MMKV/Core/CodedInputDataCrypt.h +0 -87
  35. package/MMKV/Core/CodedInputDataCrypt_OSX.cpp +0 -62
  36. package/MMKV/Core/CodedInputData_OSX.cpp +0 -92
  37. package/MMKV/Core/CodedOutputData.cpp +0 -186
  38. package/MMKV/Core/CodedOutputData.h +0 -88
  39. package/MMKV/Core/Core.xcodeproj/project.pbxproj +0 -707
  40. package/MMKV/Core/Core.xcodeproj/project.xcworkspace/contents.xcworkspacedata +0 -7
  41. package/MMKV/Core/Core.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +0 -8
  42. package/MMKV/Core/Core.xcodeproj/xcshareddata/xcschemes/Core.xcscheme +0 -67
  43. package/MMKV/Core/Core.xcodeproj/xcshareddata/xcschemes/MMKVWatchCore.xcscheme +0 -67
  44. package/MMKV/Core/InterProcessLock.cpp +0 -186
  45. package/MMKV/Core/InterProcessLock.h +0 -119
  46. package/MMKV/Core/InterProcessLock_Android.cpp +0 -103
  47. package/MMKV/Core/InterProcessLock_Win32.cpp +0 -108
  48. package/MMKV/Core/KeyValueHolder.cpp +0 -236
  49. package/MMKV/Core/KeyValueHolder.h +0 -122
  50. package/MMKV/Core/MMBuffer.cpp +0 -210
  51. package/MMKV/Core/MMBuffer.h +0 -111
  52. package/MMKV/Core/MMKV.cpp +0 -1702
  53. package/MMKV/Core/MMKV.h +0 -595
  54. package/MMKV/Core/MMKVLog.cpp +0 -127
  55. package/MMKV/Core/MMKVLog.h +0 -86
  56. package/MMKV/Core/MMKVLog_Android.cpp +0 -134
  57. package/MMKV/Core/MMKVMetaInfo.hpp +0 -99
  58. package/MMKV/Core/MMKVPredef.h +0 -293
  59. package/MMKV/Core/MMKV_Android.cpp +0 -261
  60. package/MMKV/Core/MMKV_IO.cpp +0 -1905
  61. package/MMKV/Core/MMKV_IO.h +0 -57
  62. package/MMKV/Core/MMKV_OSX.cpp +0 -423
  63. package/MMKV/Core/MMKV_OSX.h +0 -57
  64. package/MMKV/Core/MemoryFile.cpp +0 -603
  65. package/MMKV/Core/MemoryFile.h +0 -194
  66. package/MMKV/Core/MemoryFile_Android.cpp +0 -236
  67. package/MMKV/Core/MemoryFile_Linux.cpp +0 -125
  68. package/MMKV/Core/MemoryFile_OSX.cpp +0 -142
  69. package/MMKV/Core/MemoryFile_Win32.cpp +0 -554
  70. package/MMKV/Core/MiniPBCoder.cpp +0 -672
  71. package/MMKV/Core/MiniPBCoder.h +0 -151
  72. package/MMKV/Core/MiniPBCoder_OSX.cpp +0 -237
  73. package/MMKV/Core/PBEncodeItem.hpp +0 -104
  74. package/MMKV/Core/PBUtility.cpp +0 -61
  75. package/MMKV/Core/PBUtility.h +0 -148
  76. package/MMKV/Core/ScopedLock.hpp +0 -69
  77. package/MMKV/Core/ThreadLock.cpp +0 -75
  78. package/MMKV/Core/ThreadLock.h +0 -81
  79. package/MMKV/Core/ThreadLock_Win32.cpp +0 -89
  80. package/MMKV/Core/aes/AESCrypt.cpp +0 -273
  81. package/MMKV/Core/aes/AESCrypt.h +0 -112
  82. package/MMKV/Core/aes/openssl/openssl_aes-armv4.S +0 -1243
  83. package/MMKV/Core/aes/openssl/openssl_aes.h +0 -130
  84. package/MMKV/Core/aes/openssl/openssl_aes_core.cpp +0 -1044
  85. package/MMKV/Core/aes/openssl/openssl_aes_locl.h +0 -38
  86. package/MMKV/Core/aes/openssl/openssl_aesv8-armx.S +0 -308
  87. package/MMKV/Core/aes/openssl/openssl_arm_arch.h +0 -84
  88. package/MMKV/Core/aes/openssl/openssl_cfb128.cpp +0 -97
  89. package/MMKV/Core/aes/openssl/openssl_md32_common.h +0 -254
  90. package/MMKV/Core/aes/openssl/openssl_md5.h +0 -49
  91. package/MMKV/Core/aes/openssl/openssl_md5_dgst.cpp +0 -166
  92. package/MMKV/Core/aes/openssl/openssl_md5_locl.h +0 -75
  93. package/MMKV/Core/aes/openssl/openssl_md5_one.cpp +0 -30
  94. package/MMKV/Core/aes/openssl/openssl_opensslconf.h +0 -271
  95. package/MMKV/Core/core.vcxproj +0 -288
  96. package/MMKV/Core/core.vcxproj.filters +0 -150
  97. package/MMKV/Core/crc32/Checksum.h +0 -75
  98. package/MMKV/Core/crc32/crc32_armv8.cpp +0 -134
  99. package/MMKV/Core/crc32/zlib/CMakeLists.txt +0 -60
  100. package/MMKV/Core/crc32/zlib/crc32.cpp +0 -55
  101. package/MMKV/Core/crc32/zlib/crc32.h +0 -48
  102. package/MMKV/Core/crc32/zlib/zconf.h +0 -380
  103. package/MMKV/Core/crc32/zlib/zutil.h +0 -25
  104. package/MMKV/README.md +0 -354
@@ -1,236 +0,0 @@
1
- /*
2
- * Tencent is pleased to support the open source community by making
3
- * MMKV available.
4
- *
5
- * Copyright (C) 2020 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
-
21
- #include "KeyValueHolder.h"
22
- #include "PBUtility.h"
23
- #include "aes/AESCrypt.h"
24
- #include <cerrno>
25
- #include <cstring>
26
- #include <stdexcept>
27
-
28
- namespace mmkv {
29
-
30
- KeyValueHolder::KeyValueHolder(uint32_t keyLength, uint32_t valueLength, uint32_t off)
31
- : keySize(static_cast<uint16_t>(keyLength)), valueSize(valueLength), offset(off) {
32
- computedKVSize = keySize + static_cast<uint16_t>(pbRawVarint32Size(keySize));
33
- computedKVSize += static_cast<uint16_t>(pbRawVarint32Size(valueSize));
34
- }
35
-
36
- MMBuffer KeyValueHolder::toMMBuffer(const void *basePtr) const {
37
- auto realPtr = (uint8_t *) basePtr + offset;
38
- realPtr += computedKVSize;
39
- return MMBuffer(realPtr, valueSize, MMBufferNoCopy);
40
- }
41
-
42
- #ifndef MMKV_DISABLE_CRYPT
43
-
44
- KeyValueHolderCrypt::KeyValueHolderCrypt(const void *src, size_t length) {
45
- if (length <= SmallBufferSize()) {
46
- type = KeyValueHolderType_Direct;
47
- paddedSize = static_cast<uint8_t>(length);
48
- memcpy(paddedValue, src, length);
49
- } else {
50
- type = KeyValueHolderType_Memory;
51
- memSize = static_cast<uint32_t>(length);
52
- memPtr = malloc(length);
53
- if (!memPtr) {
54
- throw std::runtime_error(strerror(errno));
55
- }
56
- memcpy(memPtr, src, memSize);
57
- }
58
- }
59
-
60
- KeyValueHolderCrypt::KeyValueHolderCrypt(MMBuffer &&data) {
61
- if (data.type == MMBuffer::MMBufferType_Small) {
62
- static_assert(SmallBufferSize() >= MMBuffer::SmallBufferSize(), "KeyValueHolderCrypt can't hold MMBuffer");
63
-
64
- type = KeyValueHolderType_Direct;
65
- paddedSize = static_cast<uint8_t>(data.length());
66
- memcpy(paddedValue, data.getPtr(), data.length());
67
- } else {
68
- type = KeyValueHolderType_Memory;
69
- memSize = static_cast<uint32_t>(data.length());
70
- # ifdef MMKV_APPLE
71
- if (data.m_data != nil) {
72
- memPtr = malloc(memSize);
73
- if (!memPtr) {
74
- throw std::runtime_error(strerror(errno));
75
- }
76
- memcpy(memPtr, data.getPtr(), memSize);
77
- return;
78
- }
79
- # endif
80
- memPtr = data.getPtr();
81
- data.detach();
82
- }
83
- }
84
-
85
- KeyValueHolderCrypt::KeyValueHolderCrypt(uint32_t keyLength, uint32_t valueLength, uint32_t off)
86
- : type(KeyValueHolderType_Offset), keySize(static_cast<uint16_t>(keyLength)), valueSize(valueLength), offset(off) {
87
-
88
- pbKeyValueSize = static_cast<uint8_t>(pbRawVarint32Size(keySize) + pbRawVarint32Size(valueSize));
89
- }
90
-
91
- KeyValueHolderCrypt::KeyValueHolderCrypt(KeyValueHolderCrypt &&other) noexcept {
92
- this->move(std::move(other));
93
- }
94
-
95
- KeyValueHolderCrypt &KeyValueHolderCrypt::operator=(KeyValueHolderCrypt &&other) noexcept {
96
- if (type == KeyValueHolderType_Memory && memPtr) {
97
- free(memPtr);
98
- }
99
- this->move(std::move(other));
100
- return *this;
101
- }
102
-
103
- void KeyValueHolderCrypt::move(KeyValueHolderCrypt &&other) noexcept {
104
- if (other.type == KeyValueHolderType_Direct || other.type == KeyValueHolderType_Offset) {
105
- memcpy(this, &other, sizeof(other));
106
- } else if (other.type == KeyValueHolderType_Memory) {
107
- type = KeyValueHolderType_Memory;
108
- memSize = other.memSize;
109
- memPtr = other.memPtr;
110
- other.memPtr = nullptr;
111
- }
112
- }
113
-
114
- KeyValueHolderCrypt::~KeyValueHolderCrypt() {
115
- if (type == KeyValueHolderType_Memory && memPtr) {
116
- free(memPtr);
117
- }
118
- }
119
-
120
- uint32_t KeyValueHolderCrypt::realValueSize() const {
121
- switch (type) {
122
- case KeyValueHolderType_Direct:
123
- return paddedSize;
124
- case KeyValueHolderType_Offset:
125
- return valueSize;
126
- case KeyValueHolderType_Memory:
127
- return memSize;
128
- }
129
- return 0;
130
- }
131
-
132
- // get decrypt data with [position, -1)
133
- static MMBuffer decryptBuffer(AESCrypt &crypter, const MMBuffer &inputBuffer, size_t position) {
134
- size_t smallBuffer[16 / sizeof(size_t)];
135
- auto basePtr = (uint8_t *) inputBuffer.getPtr();
136
- auto ptr = basePtr;
137
- for (size_t index = sizeof(smallBuffer); index < position; index += sizeof(smallBuffer)) {
138
- crypter.decrypt(ptr, smallBuffer, sizeof(smallBuffer));
139
- ptr += sizeof(smallBuffer);
140
- }
141
- if (ptr < basePtr + position) {
142
- crypter.decrypt(ptr, smallBuffer, static_cast<size_t>(basePtr + position - ptr));
143
- ptr = basePtr + position;
144
- }
145
- size_t length = inputBuffer.length() - position;
146
- MMBuffer tmp(length);
147
-
148
- auto input = ptr;
149
- auto output = tmp.getPtr();
150
- crypter.decrypt(input, output, length);
151
-
152
- return tmp;
153
- }
154
-
155
- MMBuffer KeyValueHolderCrypt::toMMBuffer(const void *basePtr, const AESCrypt *crypter) const {
156
- if (type == KeyValueHolderType_Direct) {
157
- return MMBuffer((void *) paddedValue, paddedSize, MMBufferNoCopy);
158
- } else if (type == KeyValueHolderType_Memory) {
159
- return MMBuffer(memPtr, memSize, MMBufferNoCopy);
160
- } else {
161
- auto realPtr = (uint8_t *) basePtr + offset;
162
- auto position = static_cast<uint32_t>(pbKeyValueSize + keySize);
163
- auto realSize = position + valueSize;
164
- auto kvBuffer = MMBuffer(realPtr, realSize, MMBufferNoCopy);
165
- auto decrypter = crypter->cloneWithStatus(cryptStatus);
166
- return decryptBuffer(decrypter, kvBuffer, position);
167
- }
168
- }
169
-
170
- #endif // MMKV_DISABLE_CRYPT
171
-
172
- } // namespace mmkv
173
-
174
- #if !defined(MMKV_DISABLE_CRYPT) && defined(MMKV_DEBUG)
175
- # include "CodedInputData.h"
176
- # include "CodedOutputData.h"
177
- # include "MMKVLog.h"
178
- # include <ctime>
179
-
180
- using namespace std;
181
-
182
- namespace mmkv {
183
-
184
- void KeyValueHolderCrypt::testAESToMMBuffer() {
185
- const uint8_t plainText[] = "Hello, OpenSSL-mmkv::KeyValueHolderCrypt::testAESToMMBuffer() with AES CFB 128.";
186
- constexpr size_t textLength = sizeof(plainText) - 1;
187
-
188
- const uint8_t key[] = "TheAESKey";
189
- constexpr size_t keyLength = sizeof(key) - 1;
190
-
191
- uint8_t iv[AES_KEY_LEN];
192
- srand((unsigned) time(nullptr));
193
- for (uint32_t i = 0; i < AES_KEY_LEN; i++) {
194
- iv[i] = (uint8_t) rand();
195
- }
196
- AESCrypt crypt1(key, keyLength, iv, sizeof(iv));
197
-
198
- auto encryptText = new uint8_t[DEFAULT_MMAP_SIZE];
199
- memset(encryptText, 0, DEFAULT_MMAP_SIZE);
200
- CodedOutputData output(encryptText, DEFAULT_MMAP_SIZE);
201
- output.writeData(MMBuffer((void *) key, keyLength, MMBufferNoCopy));
202
- auto lengthOfValue = textLength + pbRawVarint32Size((uint32_t) textLength);
203
- output.writeRawVarint32((int32_t) lengthOfValue);
204
- output.writeData(MMBuffer((void *) plainText, textLength, MMBufferNoCopy));
205
- crypt1.encrypt(encryptText, encryptText, (size_t)(output.curWritePointer() - encryptText));
206
-
207
- AESCrypt decrypt(key, keyLength, iv, sizeof(iv));
208
- uint8_t smallBuffer[32];
209
- decrypt.decrypt(encryptText, smallBuffer, 5);
210
- auto keySize = CodedInputData(smallBuffer, 5).readUInt32();
211
- auto sizeOfKeySize = pbRawVarint32Size(keySize);
212
- auto position = sizeOfKeySize;
213
- decrypt.decrypt(encryptText + 5, smallBuffer + 5, static_cast<size_t>(sizeOfKeySize + keySize - 5));
214
- position += keySize;
215
- decrypt.decrypt(encryptText + position, smallBuffer + position, 5);
216
- auto valueSize = CodedInputData(smallBuffer + position, 5).readUInt32();
217
- // auto sizeOfValueSize = pbRawVarint32Size(valueSize);
218
- KeyValueHolderCrypt kvHolder(keySize, valueSize, 0);
219
- auto rollbackSize = position + 5;
220
- decrypt.statusBeforeDecrypt(encryptText + rollbackSize, smallBuffer + rollbackSize, rollbackSize,
221
- kvHolder.cryptStatus);
222
- auto value = kvHolder.toMMBuffer(encryptText, &decrypt);
223
- # ifdef MMKV_APPLE
224
- MMKVInfo("testAESToMMBuffer: %@", CodedInputData((char *) value.getPtr(), value.length()).readString());
225
- # else
226
- MMKVInfo("testAESToMMBuffer: %s", CodedInputData((char *) value.getPtr(), value.length()).readString().c_str());
227
- # endif
228
- MMKVInfo("MMBuffer::SmallBufferSize() = %u, KeyValueHolderCrypt::SmallBufferSize() = %u",
229
- MMBuffer::SmallBufferSize(), KeyValueHolderCrypt::SmallBufferSize());
230
-
231
- delete[] encryptText;
232
- }
233
-
234
- } // namespace mmkv
235
-
236
- #endif
@@ -1,122 +0,0 @@
1
- /*
2
- * Tencent is pleased to support the open source community by making
3
- * MMKV available.
4
- *
5
- * Copyright (C) 2020 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
-
21
- #ifndef KeyValueHolder_hpp
22
- #define KeyValueHolder_hpp
23
- #ifdef __cplusplus
24
-
25
- #include "MMBuffer.h"
26
- #include "aes/AESCrypt.h"
27
-
28
- namespace mmkv {
29
-
30
- #pragma pack(push, 1)
31
-
32
- struct KeyValueHolder {
33
- uint16_t computedKVSize; // internal use only
34
- uint16_t keySize;
35
- uint32_t valueSize;
36
- uint32_t offset;
37
-
38
- KeyValueHolder() = default;
39
- KeyValueHolder(uint32_t keyLength, uint32_t valueLength, uint32_t offset);
40
-
41
- MMBuffer toMMBuffer(const void *basePtr) const;
42
- };
43
-
44
- #ifndef MMKV_DISABLE_CRYPT
45
-
46
- enum KeyValueHolderType : uint8_t {
47
- KeyValueHolderType_Direct, // store value directly
48
- KeyValueHolderType_Memory, // store value in the heap memory
49
- KeyValueHolderType_Offset, // store value by offset
50
- };
51
-
52
- // kv holder for encrypted mmkv
53
- struct KeyValueHolderCrypt {
54
- KeyValueHolderType type = KeyValueHolderType_Direct;
55
-
56
- union {
57
- // store value by offset
58
- struct {
59
- uint8_t pbKeyValueSize; // size needed to encode keySize & valueSize
60
- uint16_t keySize;
61
- uint32_t valueSize;
62
- uint32_t offset;
63
- AESCryptStatus cryptStatus;
64
- };
65
- // store value directly
66
- struct {
67
- uint8_t paddedSize;
68
- uint8_t paddedValue[1];
69
- };
70
- // store value in the heap memory
71
- struct {
72
- uint32_t memSize;
73
- void *memPtr;
74
- };
75
- };
76
-
77
- static constexpr size_t SmallBufferSize() {
78
- return sizeof(KeyValueHolderCrypt) - offsetof(KeyValueHolderCrypt, paddedValue);
79
- }
80
-
81
- static constexpr size_t MediumBufferSize() {
82
- return 256;
83
- }
84
-
85
- static bool isValueStoredAsOffset(size_t valueSize) { return valueSize > MediumBufferSize(); }
86
-
87
- KeyValueHolderCrypt() = default;
88
- KeyValueHolderCrypt(const void *valuePtr, size_t valueLength);
89
- explicit KeyValueHolderCrypt(MMBuffer &&data);
90
- KeyValueHolderCrypt(uint32_t keyLength, uint32_t valueLength, uint32_t offset);
91
-
92
- KeyValueHolderCrypt(KeyValueHolderCrypt &&other) noexcept;
93
- KeyValueHolderCrypt &operator=(KeyValueHolderCrypt &&other) noexcept;
94
- void move(KeyValueHolderCrypt &&other) noexcept;
95
-
96
- ~KeyValueHolderCrypt();
97
-
98
- uint32_t realValueSize() const;
99
-
100
- MMBuffer toMMBuffer(const void *basePtr, const AESCrypt *crypter) const;
101
-
102
- std::tuple<uint32_t, uint32_t, AESCryptStatus *> toTuple() {
103
- return std::make_tuple(offset, pbKeyValueSize + keySize + valueSize, &cryptStatus);
104
- }
105
-
106
- // those are expensive, just forbid it for possibly misuse
107
- explicit KeyValueHolderCrypt(const KeyValueHolderCrypt &other) = delete;
108
- KeyValueHolderCrypt &operator=(const KeyValueHolderCrypt &other) = delete;
109
-
110
- #ifdef MMKV_DEBUG
111
- static void testAESToMMBuffer();
112
- #endif
113
- };
114
-
115
- #endif // MMKV_DISABLE_CRYPT
116
-
117
- #pragma pack(pop)
118
-
119
- } // namespace mmkv
120
-
121
- #endif
122
- #endif /* KeyValueHolder_hpp */
@@ -1,210 +0,0 @@
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
-
21
- #define NOMINMAX // undefine max/min
22
-
23
- #include "MMBuffer.h"
24
- #include <cerrno>
25
- #include <cstdlib>
26
- #include <cstring>
27
- #include <utility>
28
- #include <stdexcept>
29
- #include <algorithm>
30
-
31
- #ifdef MMKV_APPLE
32
- # if __has_feature(objc_arc)
33
- # error This file must be compiled with MRC. Use -fno-objc-arc flag.
34
- # endif
35
- #endif
36
-
37
- using namespace std;
38
-
39
- namespace mmkv {
40
-
41
- MMBuffer::MMBuffer(size_t length) {
42
- if (length > SmallBufferSize()) {
43
- type = MMBufferType_Normal;
44
- isNoCopy = MMBufferCopy;
45
- size = length;
46
- ptr = malloc(size);
47
- if (!ptr) {
48
- throw std::runtime_error(strerror(errno));
49
- }
50
- #ifdef MMKV_APPLE
51
- m_data = nil;
52
- #endif
53
- } else {
54
- type = MMBufferType_Small;
55
- paddedSize = static_cast<uint8_t>(length);
56
- }
57
- }
58
-
59
- MMBuffer::MMBuffer(void *source, size_t length, MMBufferCopyFlag flag) : isNoCopy(flag) {
60
- if (isNoCopy == MMBufferCopy) {
61
- if (length > SmallBufferSize()) {
62
- type = MMBufferType_Normal;
63
- size = length;
64
- ptr = malloc(size);
65
- if (!ptr) {
66
- throw std::runtime_error(strerror(errno));
67
- }
68
- memcpy(ptr, source, size);
69
- #ifdef MMKV_APPLE
70
- m_data = nil;
71
- #endif
72
- } else {
73
- type = MMBufferType_Small;
74
- paddedSize = static_cast<uint8_t>(length);
75
- memcpy(paddedBuffer, source, length);
76
- }
77
- } else {
78
- type = MMBufferType_Normal;
79
- size = length;
80
- ptr = source;
81
- #ifdef MMKV_APPLE
82
- m_data = nil;
83
- #endif
84
- }
85
- }
86
-
87
- bool MMBuffer::operator==(const MMBuffer& other) const {
88
- if (this->length() != other.length()) {
89
- return false;
90
- }
91
- return !memcmp((uint8_t*)this->getPtr(), (uint8_t*)other.getPtr(), this->length());
92
- }
93
-
94
- #ifdef MMKV_APPLE
95
- MMBuffer::MMBuffer(NSData *data, MMBufferCopyFlag flag)
96
- : type(MMBufferType_Normal), ptr((void *) data.bytes), size(data.length), isNoCopy(flag) {
97
- if (isNoCopy == MMBufferCopy) {
98
- m_data = [data retain];
99
- } else {
100
- m_data = data;
101
- }
102
- }
103
- #endif
104
-
105
- MMBuffer::MMBuffer(MMBuffer &&other) noexcept : type(other.type) {
106
- if (type == MMBufferType_Normal) {
107
- size = other.size;
108
- ptr = other.ptr;
109
- isNoCopy = other.isNoCopy;
110
- #ifdef MMKV_APPLE
111
- m_data = other.m_data;
112
- #endif
113
- other.detach();
114
- } else {
115
- paddedSize = other.paddedSize;
116
- memcpy(paddedBuffer, other.paddedBuffer, paddedSize);
117
- }
118
- }
119
-
120
- MMBuffer::MMBuffer(MMBuffer &&other, size_t length) noexcept : type(other.type) {
121
- if (type == MMBufferType_Normal) {
122
- size = std::min(other.size, length);
123
- ptr = other.ptr;
124
- isNoCopy = other.isNoCopy;
125
- #ifdef MMKV_APPLE
126
- m_data = other.m_data;
127
- #endif
128
- other.detach();
129
- } else {
130
- paddedSize = std::min(other.paddedSize, static_cast<uint8_t>(length));
131
- memcpy(paddedBuffer, other.paddedBuffer, paddedSize);
132
- }
133
- }
134
-
135
- MMBuffer &MMBuffer::operator=(MMBuffer &&other) noexcept {
136
- if (type == MMBufferType_Normal) {
137
- if (other.type == MMBufferType_Normal) {
138
- std::swap(isNoCopy, other.isNoCopy);
139
- std::swap(size, other.size);
140
- std::swap(ptr, other.ptr);
141
- #ifdef MMKV_APPLE
142
- std::swap(m_data, other.m_data);
143
- #endif
144
- } else {
145
- type = MMBufferType_Small;
146
- if (isNoCopy == MMBufferCopy) {
147
- #ifdef MMKV_APPLE
148
- if (m_data) {
149
- [m_data release];
150
- } else if (ptr) {
151
- free(ptr);
152
- }
153
- #else
154
- if (ptr) {
155
- free(ptr);
156
- }
157
- #endif
158
- }
159
- paddedSize = other.paddedSize;
160
- memcpy(paddedBuffer, other.paddedBuffer, paddedSize);
161
- }
162
- } else {
163
- if (other.type == MMBufferType_Normal) {
164
- type = MMBufferType_Normal;
165
- isNoCopy = other.isNoCopy;
166
- size = other.size;
167
- ptr = other.ptr;
168
- #ifdef MMKV_APPLE
169
- m_data = other.m_data;
170
- #endif
171
- other.detach();
172
- } else {
173
- uint8_t tmp[SmallBufferSize()];
174
- memcpy(tmp, other.paddedBuffer, other.paddedSize);
175
- memcpy(other.paddedBuffer, paddedBuffer, paddedSize);
176
- memcpy(paddedBuffer, tmp, other.paddedSize);
177
- std::swap(paddedSize, other.paddedSize);
178
- }
179
- }
180
-
181
- return *this;
182
- }
183
-
184
- MMBuffer::~MMBuffer() {
185
- if (isStoredOnStack()) {
186
- return;
187
- }
188
-
189
- #ifdef MMKV_APPLE
190
- if (m_data) {
191
- if (isNoCopy == MMBufferCopy) {
192
- [m_data release];
193
- }
194
- return;
195
- }
196
- #endif
197
-
198
- if (isNoCopy == MMBufferCopy && ptr) {
199
- free(ptr);
200
- }
201
- }
202
-
203
- void MMBuffer::detach() {
204
- // type = MMBufferType_Small;
205
- // paddedSize = 0;
206
- auto memsetPtr = (size_t *) &type;
207
- *memsetPtr = 0;
208
- }
209
-
210
- } // namespace mmkv
@@ -1,111 +0,0 @@
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
-
21
- #ifndef MMKV_MMBUFFER_H
22
- #define MMKV_MMBUFFER_H
23
- #ifdef __cplusplus
24
-
25
- #include "MMKVPredef.h"
26
-
27
- #include <cstdint>
28
- #include <cstdlib>
29
- #include <cstddef>
30
-
31
- namespace mmkv {
32
-
33
- enum MMBufferCopyFlag : bool {
34
- MMBufferCopy = false,
35
- MMBufferNoCopy = true,
36
- };
37
-
38
- #pragma pack(push, 1)
39
-
40
- #ifndef MMKV_DISABLE_CRYPT
41
- struct KeyValueHolderCrypt;
42
- #endif
43
-
44
- class MMBuffer {
45
- enum MMBufferType : uint8_t {
46
- MMBufferType_Small, // store small buffer in stack memory
47
- MMBufferType_Normal, // store in heap memory
48
- };
49
- MMBufferType type;
50
-
51
- union {
52
- struct {
53
- MMBufferCopyFlag isNoCopy;
54
- size_t size;
55
- void *ptr;
56
- #ifdef MMKV_APPLE
57
- NSData *m_data;
58
- #endif
59
- };
60
- struct {
61
- uint8_t paddedSize;
62
- // make at least 10 bytes to hold all primitive types (negative int32, int64, double etc.) on 32 bit device
63
- // on 64 bit device it's guaranteed larger than 10 bytes
64
- uint8_t paddedBuffer[10];
65
- };
66
- };
67
-
68
- static constexpr size_t SmallBufferSize() {
69
- return sizeof(MMBuffer) - offsetof(MMBuffer, paddedBuffer);
70
- }
71
-
72
- public:
73
- explicit MMBuffer(size_t length = 0);
74
- MMBuffer(void *source, size_t length, MMBufferCopyFlag flag = MMBufferCopy);
75
- #ifdef MMKV_APPLE
76
- explicit MMBuffer(NSData *data, MMBufferCopyFlag flag = MMBufferCopy);
77
- #endif
78
-
79
- MMBuffer(MMBuffer &&other) noexcept;
80
- MMBuffer(MMBuffer &&other, size_t length) noexcept;
81
- MMBuffer &operator=(MMBuffer &&other) noexcept;
82
-
83
- ~MMBuffer();
84
-
85
- bool isStoredOnStack() const { return (type == MMBufferType_Small); }
86
-
87
- void *getPtr() const { return isStoredOnStack() ? (void *) paddedBuffer : ptr; }
88
-
89
- size_t length() const { return isStoredOnStack() ? paddedSize : size; }
90
-
91
- // transfer ownership to others
92
- void detach();
93
-
94
- // compare two MMBuffer
95
- bool operator==(const MMBuffer& other) const;
96
-
97
- // those are expensive, just forbid it for possibly misuse
98
- explicit MMBuffer(const MMBuffer &other) = delete;
99
- MMBuffer &operator=(const MMBuffer &other) = delete;
100
-
101
- #ifndef MMKV_DISABLE_CRYPT
102
- friend KeyValueHolderCrypt;
103
- #endif
104
- };
105
-
106
- #pragma pack(pop)
107
-
108
- } // namespace mmkv
109
-
110
- #endif
111
- #endif //MMKV_MMBUFFER_H