react-native-mmkv-dz 2.5.1

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 (138) hide show
  1. package/LICENSE +219 -0
  2. package/MMKV/CHANGELOG.md +553 -0
  3. package/MMKV/Core/CMakeLists.txt +153 -0
  4. package/MMKV/Core/CodedInputData.cpp +228 -0
  5. package/MMKV/Core/CodedInputData.h +83 -0
  6. package/MMKV/Core/CodedInputDataCrypt.cpp +280 -0
  7. package/MMKV/Core/CodedInputDataCrypt.h +87 -0
  8. package/MMKV/Core/CodedInputDataCrypt_OSX.cpp +62 -0
  9. package/MMKV/Core/CodedInputData_OSX.cpp +92 -0
  10. package/MMKV/Core/CodedOutputData.cpp +174 -0
  11. package/MMKV/Core/CodedOutputData.h +82 -0
  12. package/MMKV/Core/Core.xcodeproj/project.pbxproj +702 -0
  13. package/MMKV/Core/Core.xcodeproj/project.xcworkspace/contents.xcworkspacedata +7 -0
  14. package/MMKV/Core/Core.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +8 -0
  15. package/MMKV/Core/Core.xcodeproj/xcshareddata/xcschemes/Core.xcscheme +67 -0
  16. package/MMKV/Core/Core.xcodeproj/xcshareddata/xcschemes/MMKVWatchCore.xcscheme +67 -0
  17. package/MMKV/Core/InterProcessLock.cpp +186 -0
  18. package/MMKV/Core/InterProcessLock.h +119 -0
  19. package/MMKV/Core/InterProcessLock_Android.cpp +103 -0
  20. package/MMKV/Core/InterProcessLock_Win32.cpp +108 -0
  21. package/MMKV/Core/KeyValueHolder.cpp +236 -0
  22. package/MMKV/Core/KeyValueHolder.h +118 -0
  23. package/MMKV/Core/MMBuffer.cpp +185 -0
  24. package/MMKV/Core/MMBuffer.h +107 -0
  25. package/MMKV/Core/MMKV.cpp +1418 -0
  26. package/MMKV/Core/MMKV.h +386 -0
  27. package/MMKV/Core/MMKVLog.cpp +127 -0
  28. package/MMKV/Core/MMKVLog.h +86 -0
  29. package/MMKV/Core/MMKVLog_Android.cpp +79 -0
  30. package/MMKV/Core/MMKVMetaInfo.hpp +81 -0
  31. package/MMKV/Core/MMKVPredef.h +245 -0
  32. package/MMKV/Core/MMKV_Android.cpp +259 -0
  33. package/MMKV/Core/MMKV_IO.cpp +1119 -0
  34. package/MMKV/Core/MMKV_IO.h +57 -0
  35. package/MMKV/Core/MMKV_OSX.cpp +347 -0
  36. package/MMKV/Core/MMKV_OSX.h +51 -0
  37. package/MMKV/Core/MemoryFile.cpp +537 -0
  38. package/MMKV/Core/MemoryFile.h +182 -0
  39. package/MMKV/Core/MemoryFile_Android.cpp +211 -0
  40. package/MMKV/Core/MemoryFile_Linux.cpp +120 -0
  41. package/MMKV/Core/MemoryFile_OSX.cpp +142 -0
  42. package/MMKV/Core/MemoryFile_Win32.cpp +536 -0
  43. package/MMKV/Core/MiniPBCoder.cpp +366 -0
  44. package/MMKV/Core/MiniPBCoder.h +129 -0
  45. package/MMKV/Core/MiniPBCoder_OSX.cpp +228 -0
  46. package/MMKV/Core/PBEncodeItem.hpp +86 -0
  47. package/MMKV/Core/PBUtility.cpp +61 -0
  48. package/MMKV/Core/PBUtility.h +153 -0
  49. package/MMKV/Core/ScopedLock.hpp +69 -0
  50. package/MMKV/Core/ThreadLock.cpp +68 -0
  51. package/MMKV/Core/ThreadLock.h +78 -0
  52. package/MMKV/Core/ThreadLock_Win32.cpp +89 -0
  53. package/MMKV/Core/aes/AESCrypt.cpp +256 -0
  54. package/MMKV/Core/aes/AESCrypt.h +107 -0
  55. package/MMKV/Core/aes/openssl/openssl_aes-armv4.S +1231 -0
  56. package/MMKV/Core/aes/openssl/openssl_aes.h +118 -0
  57. package/MMKV/Core/aes/openssl/openssl_aes_core.cpp +1044 -0
  58. package/MMKV/Core/aes/openssl/openssl_aes_locl.h +38 -0
  59. package/MMKV/Core/aes/openssl/openssl_aesv8-armx.S +308 -0
  60. package/MMKV/Core/aes/openssl/openssl_arm_arch.h +84 -0
  61. package/MMKV/Core/aes/openssl/openssl_cfb128.cpp +97 -0
  62. package/MMKV/Core/aes/openssl/openssl_md32_common.h +254 -0
  63. package/MMKV/Core/aes/openssl/openssl_md5.h +49 -0
  64. package/MMKV/Core/aes/openssl/openssl_md5_dgst.cpp +166 -0
  65. package/MMKV/Core/aes/openssl/openssl_md5_locl.h +75 -0
  66. package/MMKV/Core/aes/openssl/openssl_md5_one.cpp +30 -0
  67. package/MMKV/Core/aes/openssl/openssl_opensslconf.h +271 -0
  68. package/MMKV/Core/core.vcxproj +186 -0
  69. package/MMKV/Core/core.vcxproj.filters +150 -0
  70. package/MMKV/Core/crc32/Checksum.h +67 -0
  71. package/MMKV/Core/crc32/crc32_armv8.cpp +132 -0
  72. package/MMKV/Core/crc32/zlib/crc32.cpp +55 -0
  73. package/MMKV/Core/crc32/zlib/crc32.h +48 -0
  74. package/MMKV/Core/crc32/zlib/zconf.h +380 -0
  75. package/MMKV/Core/crc32/zlib/zutil.h +25 -0
  76. package/MMKV/LICENSE.TXT +193 -0
  77. package/MMKV/README.md +288 -0
  78. package/README.md +221 -0
  79. package/android/CMakeLists.txt +71 -0
  80. package/android/build.gradle +371 -0
  81. package/android/gradle.properties +4 -0
  82. package/android/src/main/AndroidManifest.xml +4 -0
  83. package/android/src/main/cpp/MmkvHostObject.cpp +302 -0
  84. package/android/src/main/cpp/MmkvHostObject.h +26 -0
  85. package/android/src/main/cpp/cpp-adapter.cpp +65 -0
  86. package/android/src/main/java/com/reactnativemmkv/MmkvModule.java +49 -0
  87. package/android/src/main/java/com/reactnativemmkv/MmkvPackage.java +26 -0
  88. package/cpp/TypedArray.cpp +341 -0
  89. package/cpp/TypedArray.h +175 -0
  90. package/ios/JSIUtils.h +50 -0
  91. package/ios/JSIUtils.mm +194 -0
  92. package/ios/Mmkv.xcodeproj/project.pbxproj +291 -0
  93. package/ios/MmkvHostObject.h +27 -0
  94. package/ios/MmkvHostObject.mm +299 -0
  95. package/ios/MmkvModule.h +5 -0
  96. package/ios/MmkvModule.mm +73 -0
  97. package/lib/commonjs/MMKV.js +146 -0
  98. package/lib/commonjs/MMKV.js.map +1 -0
  99. package/lib/commonjs/PlatformChecker.js +16 -0
  100. package/lib/commonjs/PlatformChecker.js.map +1 -0
  101. package/lib/commonjs/createMMKV.js +66 -0
  102. package/lib/commonjs/createMMKV.js.map +1 -0
  103. package/lib/commonjs/createMMKV.mock.js +40 -0
  104. package/lib/commonjs/createMMKV.mock.js.map +1 -0
  105. package/lib/commonjs/createMMKV.web.js +77 -0
  106. package/lib/commonjs/createMMKV.web.js.map +1 -0
  107. package/lib/commonjs/createTextEncoder.js +24 -0
  108. package/lib/commonjs/createTextEncoder.js.map +1 -0
  109. package/lib/commonjs/hooks.js +200 -0
  110. package/lib/commonjs/hooks.js.map +1 -0
  111. package/lib/commonjs/index.js +32 -0
  112. package/lib/commonjs/index.js.map +1 -0
  113. package/lib/module/MMKV.js +134 -0
  114. package/lib/module/MMKV.js.map +1 -0
  115. package/lib/module/PlatformChecker.js +9 -0
  116. package/lib/module/PlatformChecker.js.map +1 -0
  117. package/lib/module/createMMKV.js +55 -0
  118. package/lib/module/createMMKV.js.map +1 -0
  119. package/lib/module/createMMKV.mock.js +31 -0
  120. package/lib/module/createMMKV.mock.js.map +1 -0
  121. package/lib/module/createMMKV.web.js +67 -0
  122. package/lib/module/createMMKV.web.js.map +1 -0
  123. package/lib/module/createTextEncoder.js +17 -0
  124. package/lib/module/createTextEncoder.js.map +1 -0
  125. package/lib/module/hooks.js +181 -0
  126. package/lib/module/hooks.js.map +1 -0
  127. package/lib/module/index.js +3 -0
  128. package/lib/module/index.js.map +1 -0
  129. package/lib/typescript/MMKV.d.ts +137 -0
  130. package/lib/typescript/PlatformChecker.d.ts +1 -0
  131. package/lib/typescript/createMMKV.d.ts +6 -0
  132. package/lib/typescript/createMMKV.mock.d.ts +2 -0
  133. package/lib/typescript/createMMKV.web.d.ts +2 -0
  134. package/lib/typescript/createTextEncoder.d.ts +1 -0
  135. package/lib/typescript/hooks.d.ts +81 -0
  136. package/lib/typescript/index.d.ts +2 -0
  137. package/package.json +168 -0
  138. package/react-native-mmkv.podspec +32 -0
@@ -0,0 +1,228 @@
1
+ /*
2
+ * Tencent is pleased to support the open source community by making
3
+ * MMKV available.
4
+ *
5
+ * Copyright (C) 2019 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 "MiniPBCoder.h"
22
+
23
+ #ifdef MMKV_APPLE
24
+
25
+ # include "CodedInputData.h"
26
+ # include "CodedInputDataCrypt.h"
27
+ # include "CodedOutputData.h"
28
+ # include "MMBuffer.h"
29
+ # include "PBEncodeItem.hpp"
30
+ # include "PBUtility.h"
31
+ # include <string>
32
+ # include <vector>
33
+
34
+ # if __has_feature(objc_arc)
35
+ # error This file must be compiled with MRC. Use -fno-objc-arc flag.
36
+ # endif
37
+
38
+ using namespace std;
39
+
40
+ namespace mmkv {
41
+
42
+ size_t MiniPBCoder::prepareObjectForEncode(__unsafe_unretained NSObject *obj) {
43
+ if (!obj) {
44
+ return m_encodeItems->size();
45
+ }
46
+ m_encodeItems->push_back(PBEncodeItem());
47
+ PBEncodeItem *encodeItem = &(m_encodeItems->back());
48
+ size_t index = m_encodeItems->size() - 1;
49
+
50
+ if ([obj isKindOfClass:[NSString class]]) {
51
+ NSString *str = (NSString *) obj;
52
+ encodeItem->type = PBEncodeItemType_NSString;
53
+ NSData *buffer = [[str dataUsingEncoding:NSUTF8StringEncoding] retain];
54
+ encodeItem->value.tmpObjectValue = (__bridge void *) buffer;
55
+ encodeItem->valueSize = static_cast<uint32_t>(buffer.length);
56
+ } else if ([obj isKindOfClass:[NSDate class]]) {
57
+ NSDate *oDate = (NSDate *) obj;
58
+ encodeItem->type = PBEncodeItemType_NSDate;
59
+ encodeItem->value.objectValue = (__bridge void *) oDate;
60
+ encodeItem->valueSize = pbDoubleSize();
61
+ encodeItem->compiledSize = encodeItem->valueSize;
62
+ return index; // double has fixed compilesize
63
+ } else if ([obj isKindOfClass:[NSData class]]) {
64
+ NSData *oData = (NSData *) obj;
65
+ encodeItem->type = PBEncodeItemType_NSData;
66
+ encodeItem->value.objectValue = (__bridge void *) oData;
67
+ encodeItem->valueSize = static_cast<uint32_t>(oData.length);
68
+ } else {
69
+ m_encodeItems->pop_back();
70
+ MMKVError("%@ not recognized", NSStringFromClass(obj.class));
71
+ return m_encodeItems->size();
72
+ }
73
+ encodeItem->compiledSize = pbRawVarint32Size(encodeItem->valueSize) + encodeItem->valueSize;
74
+
75
+ return index;
76
+ }
77
+
78
+ void MiniPBCoder::decodeOneMap(MMKVMap &dic, size_t position, bool greedy) {
79
+ auto block = [position, this](MMKVMap &dictionary) {
80
+ if (position) {
81
+ m_inputData->seek(position);
82
+ } else {
83
+ m_inputData->readInt32();
84
+ }
85
+ while (!m_inputData->isAtEnd()) {
86
+ KeyValueHolder kvHolder;
87
+ const auto &key = m_inputData->readString(kvHolder);
88
+ if (key.length > 0) {
89
+ m_inputData->readData(kvHolder);
90
+ auto itr = dictionary.find(key);
91
+ if (itr != dictionary.end()) {
92
+ if (kvHolder.valueSize > 0) {
93
+ itr->second = move(kvHolder);
94
+ } else {
95
+ auto oldKey = itr->first;
96
+ dictionary.erase(itr);
97
+ [oldKey release];
98
+ }
99
+ } else {
100
+ if (kvHolder.valueSize > 0) {
101
+ dictionary.emplace(key, move(kvHolder));
102
+ [key retain];
103
+ }
104
+ }
105
+ }
106
+ }
107
+ };
108
+
109
+ if (greedy) {
110
+ try {
111
+ block(dic);
112
+ } catch (std::exception &exception) {
113
+ MMKVError("%s", exception.what());
114
+ }
115
+ } else {
116
+ try {
117
+ MMKVMap tmpDic;
118
+ block(tmpDic);
119
+ dic.swap(tmpDic);
120
+ for (auto &pair : tmpDic) {
121
+ [pair.first release];
122
+ }
123
+ } catch (std::exception &exception) {
124
+ MMKVError("%s", exception.what());
125
+ }
126
+ }
127
+ }
128
+
129
+ # ifndef MMKV_DISABLE_CRYPT
130
+
131
+ void MiniPBCoder::decodeOneMap(MMKVMapCrypt &dic, size_t position, bool greedy) {
132
+ auto block = [position, this](MMKVMapCrypt &dictionary) {
133
+ if (position) {
134
+ m_inputDataDecrpt->seek(position);
135
+ } else {
136
+ m_inputDataDecrpt->readInt32();
137
+ }
138
+ while (!m_inputDataDecrpt->isAtEnd()) {
139
+ KeyValueHolderCrypt kvHolder;
140
+ const auto &key = m_inputDataDecrpt->readString(kvHolder);
141
+ if (key.length > 0) {
142
+ m_inputDataDecrpt->readData(kvHolder);
143
+ auto itr = dictionary.find(key);
144
+ if (itr != dictionary.end()) {
145
+ if (kvHolder.realValueSize() > 0) {
146
+ itr->second = move(kvHolder);
147
+ } else {
148
+ auto oldKey = itr->first;
149
+ dictionary.erase(itr);
150
+ [oldKey release];
151
+ }
152
+ } else {
153
+ if (kvHolder.realValueSize() > 0) {
154
+ dictionary.emplace(key, move(kvHolder));
155
+ [key retain];
156
+ }
157
+ }
158
+ }
159
+ }
160
+ };
161
+
162
+ if (greedy) {
163
+ try {
164
+ block(dic);
165
+ } catch (std::exception &exception) {
166
+ MMKVError("%s", exception.what());
167
+ }
168
+ } else {
169
+ try {
170
+ MMKVMapCrypt tmpDic;
171
+ block(tmpDic);
172
+ dic.swap(tmpDic);
173
+ for (auto &pair : tmpDic) {
174
+ [pair.first release];
175
+ }
176
+ } catch (std::exception &exception) {
177
+ MMKVError("%s", exception.what());
178
+ }
179
+ }
180
+ }
181
+
182
+ # endif // MMKV_DISABLE_CRYPT
183
+
184
+ NSObject *MiniPBCoder::decodeObject(const MMBuffer &oData, Class cls) {
185
+ if (!cls || oData.length() == 0) {
186
+ return nil;
187
+ }
188
+ CodedInputData input(oData.getPtr(), oData.length());
189
+ if (cls == [NSString class]) {
190
+ return input.readString();
191
+ } else if (cls == [NSMutableString class]) {
192
+ return [NSMutableString stringWithString:input.readString()];
193
+ } else if (cls == [NSData class]) {
194
+ return input.readNSData();
195
+ } else if (cls == [NSMutableData class]) {
196
+ return [NSMutableData dataWithData:input.readNSData()];
197
+ } else if (cls == [NSDate class]) {
198
+ return [NSDate dateWithTimeIntervalSince1970:input.readDouble()];
199
+ } else {
200
+ MMKVError("%@ not recognized", NSStringFromClass(cls));
201
+ }
202
+
203
+ return nil;
204
+ }
205
+
206
+ bool MiniPBCoder::isCompatibleClass(Class cls) {
207
+ if (cls == [NSString class]) {
208
+ return true;
209
+ }
210
+ if (cls == [NSMutableString class]) {
211
+ return true;
212
+ }
213
+ if (cls == [NSData class]) {
214
+ return true;
215
+ }
216
+ if (cls == [NSMutableData class]) {
217
+ return true;
218
+ }
219
+ if (cls == [NSDate class]) {
220
+ return true;
221
+ }
222
+
223
+ return false;
224
+ }
225
+
226
+ } // namespace mmkv
227
+
228
+ #endif // MMKV_APPLE
@@ -0,0 +1,86 @@
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_PBENCODEITEM_HPP
22
+ #define MMKV_PBENCODEITEM_HPP
23
+ #ifdef __cplusplus
24
+
25
+ #include "MMKVPredef.h"
26
+
27
+ #include "MMBuffer.h"
28
+ #include <cstdint>
29
+ #include <memory.h>
30
+
31
+ namespace mmkv {
32
+
33
+ enum PBEncodeItemType {
34
+ PBEncodeItemType_None,
35
+ PBEncodeItemType_Data,
36
+ PBEncodeItemType_Container,
37
+ #ifndef MMKV_APPLE
38
+ PBEncodeItemType_String,
39
+ #else
40
+ PBEncodeItemType_NSString,
41
+ PBEncodeItemType_NSData,
42
+ PBEncodeItemType_NSDate,
43
+ #endif
44
+ };
45
+
46
+ struct PBEncodeItem {
47
+ PBEncodeItemType type;
48
+ uint32_t compiledSize;
49
+ uint32_t valueSize;
50
+ union {
51
+ const MMBuffer *bufferValue;
52
+ #ifndef MMKV_APPLE
53
+ const std::string *strValue;
54
+ #else
55
+ void *objectValue;
56
+ void *tmpObjectValue; // this object should be released on dealloc
57
+ #endif
58
+ } value;
59
+
60
+ PBEncodeItem() : type(PBEncodeItemType_None), compiledSize(0), valueSize(0) { memset(&value, 0, sizeof(value)); }
61
+
62
+ #ifndef MMKV_APPLE
63
+ // opt std::vector.push_back() on slow_path
64
+ PBEncodeItem(PBEncodeItem &&other) = default;
65
+ #else
66
+ // opt std::vector.push_back() on slow_path
67
+ PBEncodeItem(PBEncodeItem &&other)
68
+ : type(other.type), compiledSize(other.compiledSize), valueSize(other.valueSize), value(other.value) {
69
+ // omit unnecessary CFRetain() & CFRelease()
70
+ other.type = PBEncodeItemType_None;
71
+ }
72
+
73
+ ~PBEncodeItem() {
74
+ if (type == PBEncodeItemType_NSString) {
75
+ if (value.tmpObjectValue) {
76
+ CFRelease(value.tmpObjectValue);
77
+ }
78
+ }
79
+ }
80
+ #endif // MMKV_APPLE
81
+ };
82
+
83
+ } // namespace mmkv
84
+
85
+ #endif
86
+ #endif //MMKV_PBENCODEITEM_HPP
@@ -0,0 +1,61 @@
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
+ #include "PBUtility.h"
22
+
23
+ namespace mmkv {
24
+
25
+ uint32_t pbRawVarint32Size(uint32_t value) {
26
+ if ((value & (0xffffffff << 7)) == 0) {
27
+ return 1;
28
+ } else if ((value & (0xffffffff << 14)) == 0) {
29
+ return 2;
30
+ } else if ((value & (0xffffffff << 21)) == 0) {
31
+ return 3;
32
+ } else if ((value & (0xffffffff << 28)) == 0) {
33
+ return 4;
34
+ }
35
+ return 5;
36
+ }
37
+
38
+ uint32_t pbUInt64Size(uint64_t value) {
39
+ if ((value & (0xffffffffffffffffL << 7)) == 0) {
40
+ return 1;
41
+ } else if ((value & (0xffffffffffffffffL << 14)) == 0) {
42
+ return 2;
43
+ } else if ((value & (0xffffffffffffffffL << 21)) == 0) {
44
+ return 3;
45
+ } else if ((value & (0xffffffffffffffffL << 28)) == 0) {
46
+ return 4;
47
+ } else if ((value & (0xffffffffffffffffL << 35)) == 0) {
48
+ return 5;
49
+ } else if ((value & (0xffffffffffffffffL << 42)) == 0) {
50
+ return 6;
51
+ } else if ((value & (0xffffffffffffffffL << 49)) == 0) {
52
+ return 7;
53
+ } else if ((value & (0xffffffffffffffffL << 56)) == 0) {
54
+ return 8;
55
+ } else if ((value & (0xffffffffffffffffL << 63)) == 0) {
56
+ return 9;
57
+ }
58
+ return 10;
59
+ }
60
+
61
+ } // namespace mmkv
@@ -0,0 +1,153 @@
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_PBUTILITY_H
22
+ #define MMKV_PBUTILITY_H
23
+ #ifdef __cplusplus
24
+
25
+ #include "MMKVPredef.h"
26
+
27
+ #include <cstdint>
28
+
29
+ #ifndef MMKV_WIN32
30
+ # ifndef likely
31
+ # define unlikely(x) (__builtin_expect(bool(x), 0))
32
+ # define likely(x) (__builtin_expect(bool(x), 1))
33
+ # endif
34
+ #else
35
+ # ifndef likely
36
+ # define unlikely(x) (x)
37
+ # define likely(x) (x)
38
+ # endif
39
+ #endif
40
+
41
+ namespace mmkv {
42
+
43
+ template <typename T, typename P>
44
+ union Converter {
45
+ static_assert(sizeof(T) == sizeof(P), "size not match");
46
+ T first;
47
+ P second;
48
+ };
49
+
50
+ static inline int64_t Float64ToInt64(double v) {
51
+ Converter<double, int64_t> converter;
52
+ converter.first = v;
53
+ return converter.second;
54
+ }
55
+
56
+ static inline int32_t Float32ToInt32(float v) {
57
+ Converter<float, int32_t> converter;
58
+ converter.first = v;
59
+ return converter.second;
60
+ }
61
+
62
+ static inline double Int64ToFloat64(int64_t v) {
63
+ Converter<double, int64_t> converter;
64
+ converter.second = v;
65
+ return converter.first;
66
+ }
67
+
68
+ static inline float Int32ToFloat32(int32_t v) {
69
+ Converter<float, int32_t> converter;
70
+ converter.second = v;
71
+ return converter.first;
72
+ }
73
+
74
+ static inline uint64_t Int64ToUInt64(int64_t v) {
75
+ Converter<int64_t, uint64_t> converter;
76
+ converter.first = v;
77
+ return converter.second;
78
+ }
79
+
80
+ static inline int64_t UInt64ToInt64(uint64_t v) {
81
+ Converter<int64_t, uint64_t> converter;
82
+ converter.second = v;
83
+ return converter.first;
84
+ }
85
+
86
+ static inline uint32_t Int32ToUInt32(int32_t v) {
87
+ Converter<int32_t, uint32_t> converter;
88
+ converter.first = v;
89
+ return converter.second;
90
+ }
91
+
92
+ static inline int32_t UInt32ToInt32(uint32_t v) {
93
+ Converter<int32_t, uint32_t> converter;
94
+ converter.second = v;
95
+ return converter.first;
96
+ }
97
+
98
+ static inline int32_t logicalRightShift32(int32_t value, uint32_t spaces) {
99
+ return UInt32ToInt32((Int32ToUInt32(value) >> spaces));
100
+ }
101
+
102
+ static inline int64_t logicalRightShift64(int64_t value, uint32_t spaces) {
103
+ return UInt64ToInt64((Int64ToUInt64(value) >> spaces));
104
+ }
105
+
106
+ constexpr uint32_t LittleEdian32Size = 4;
107
+
108
+ constexpr uint32_t pbFloatSize() {
109
+ return LittleEdian32Size;
110
+ }
111
+
112
+ constexpr uint32_t pbFixed32Size() {
113
+ return LittleEdian32Size;
114
+ }
115
+
116
+ constexpr uint32_t LittleEdian64Size = 8;
117
+
118
+ constexpr uint32_t pbDoubleSize() {
119
+ return LittleEdian64Size;
120
+ }
121
+
122
+ constexpr uint32_t pbBoolSize() {
123
+ return 1;
124
+ }
125
+
126
+ extern uint32_t pbRawVarint32Size(uint32_t value);
127
+
128
+ static inline uint32_t pbRawVarint32Size(int32_t value) {
129
+ return pbRawVarint32Size(Int32ToUInt32(value));
130
+ }
131
+
132
+ extern uint32_t pbUInt64Size(uint64_t value);
133
+
134
+ static inline uint32_t pbInt64Size(int64_t value) {
135
+ return pbUInt64Size(Int64ToUInt64(value));
136
+ }
137
+
138
+ static inline uint32_t pbInt32Size(int32_t value) {
139
+ if (value >= 0) {
140
+ return pbRawVarint32Size(value);
141
+ } else {
142
+ return 10;
143
+ }
144
+ }
145
+
146
+ static inline uint32_t pbUInt32Size(uint32_t value) {
147
+ return pbRawVarint32Size(value);
148
+ }
149
+
150
+ } // namespace mmkv
151
+
152
+ #endif
153
+ #endif //MMKV_PBUTILITY_H
@@ -0,0 +1,69 @@
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_SCOPEDLOCK_HPP
22
+ #define MMKV_SCOPEDLOCK_HPP
23
+ #ifdef __cplusplus
24
+
25
+ namespace mmkv {
26
+
27
+ template <typename T>
28
+ class ScopedLock {
29
+ T *m_lock;
30
+
31
+ void lock() {
32
+ if (m_lock) {
33
+ m_lock->lock();
34
+ }
35
+ }
36
+
37
+ void unlock() {
38
+ if (m_lock) {
39
+ m_lock->unlock();
40
+ }
41
+ }
42
+
43
+ public:
44
+ explicit ScopedLock(T *oLock) : m_lock(oLock) {
45
+ MMKV_ASSERT(m_lock);
46
+ lock();
47
+ }
48
+
49
+ ~ScopedLock() {
50
+ unlock();
51
+ m_lock = nullptr;
52
+ }
53
+
54
+ // just forbid it for possibly misuse
55
+ explicit ScopedLock(const ScopedLock<T> &other) = delete;
56
+ ScopedLock &operator=(const ScopedLock<T> &other) = delete;
57
+ };
58
+
59
+ } // namespace mmkv
60
+
61
+ #include <type_traits>
62
+
63
+ #define SCOPED_LOCK(lock) _SCOPEDLOCK(lock, __COUNTER__)
64
+ #define _SCOPEDLOCK(lock, counter) __SCOPEDLOCK(lock, counter)
65
+ #define __SCOPEDLOCK(lock, counter) \
66
+ mmkv::ScopedLock<std::remove_pointer<decltype(lock)>::type> __scopedLock##counter(lock)
67
+
68
+ #endif
69
+ #endif //MMKV_SCOPEDLOCK_HPP
@@ -0,0 +1,68 @@
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
+ #include "ThreadLock.h"
22
+ #include "MMKVLog.h"
23
+
24
+ #if MMKV_USING_PTHREAD
25
+
26
+ using namespace std;
27
+
28
+ namespace mmkv {
29
+
30
+ ThreadLock::ThreadLock() {
31
+ pthread_mutexattr_t attr;
32
+ pthread_mutexattr_init(&attr);
33
+ pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
34
+
35
+ pthread_mutex_init(&m_lock, &attr);
36
+
37
+ pthread_mutexattr_destroy(&attr);
38
+ }
39
+
40
+ ThreadLock::~ThreadLock() {
41
+ pthread_mutex_destroy(&m_lock);
42
+ }
43
+
44
+ void ThreadLock::initialize() {
45
+ return;
46
+ }
47
+
48
+ void ThreadLock::lock() {
49
+ auto ret = pthread_mutex_lock(&m_lock);
50
+ if (ret != 0) {
51
+ MMKVError("fail to lock %p, ret=%d, errno=%s", &m_lock, ret, strerror(errno));
52
+ }
53
+ }
54
+
55
+ void ThreadLock::unlock() {
56
+ auto ret = pthread_mutex_unlock(&m_lock);
57
+ if (ret != 0) {
58
+ MMKVError("fail to unlock %p, ret=%d, errno=%s", &m_lock, ret, strerror(errno));
59
+ }
60
+ }
61
+
62
+ void ThreadLock::ThreadOnce(ThreadOnceToken_t *onceToken, void (*callback)()) {
63
+ pthread_once(onceToken, callback);
64
+ }
65
+
66
+ } // namespace mmkv
67
+
68
+ #endif // MMKV_USING_PTHREAD