react-native-mmkv 3.3.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 (87) 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 +13 -8
  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/package.json +22 -23
  10. package/react-native-mmkv.podspec +3 -9
  11. package/react-native.config.js +9 -0
  12. package/MMKV/Core/CMakeLists.txt +0 -172
  13. package/MMKV/Core/CodedInputData.cpp +0 -252
  14. package/MMKV/Core/CodedInputData.h +0 -87
  15. package/MMKV/Core/CodedInputDataCrypt.cpp +0 -280
  16. package/MMKV/Core/CodedInputDataCrypt.h +0 -87
  17. package/MMKV/Core/CodedInputDataCrypt_OSX.cpp +0 -62
  18. package/MMKV/Core/CodedInputData_OSX.cpp +0 -92
  19. package/MMKV/Core/CodedOutputData.cpp +0 -186
  20. package/MMKV/Core/CodedOutputData.h +0 -88
  21. package/MMKV/Core/Core.xcodeproj/project.pbxproj +0 -707
  22. package/MMKV/Core/Core.xcodeproj/project.xcworkspace/contents.xcworkspacedata +0 -7
  23. package/MMKV/Core/Core.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +0 -8
  24. package/MMKV/Core/Core.xcodeproj/xcshareddata/xcschemes/Core.xcscheme +0 -67
  25. package/MMKV/Core/Core.xcodeproj/xcshareddata/xcschemes/MMKVWatchCore.xcscheme +0 -67
  26. package/MMKV/Core/InterProcessLock.cpp +0 -186
  27. package/MMKV/Core/InterProcessLock.h +0 -119
  28. package/MMKV/Core/InterProcessLock_Android.cpp +0 -103
  29. package/MMKV/Core/InterProcessLock_Win32.cpp +0 -108
  30. package/MMKV/Core/KeyValueHolder.cpp +0 -236
  31. package/MMKV/Core/KeyValueHolder.h +0 -122
  32. package/MMKV/Core/MMBuffer.cpp +0 -210
  33. package/MMKV/Core/MMBuffer.h +0 -111
  34. package/MMKV/Core/MMKV.cpp +0 -1702
  35. package/MMKV/Core/MMKV.h +0 -595
  36. package/MMKV/Core/MMKVLog.cpp +0 -127
  37. package/MMKV/Core/MMKVLog.h +0 -86
  38. package/MMKV/Core/MMKVLog_Android.cpp +0 -134
  39. package/MMKV/Core/MMKVMetaInfo.hpp +0 -99
  40. package/MMKV/Core/MMKVPredef.h +0 -293
  41. package/MMKV/Core/MMKV_Android.cpp +0 -261
  42. package/MMKV/Core/MMKV_IO.cpp +0 -1905
  43. package/MMKV/Core/MMKV_IO.h +0 -57
  44. package/MMKV/Core/MMKV_OSX.cpp +0 -423
  45. package/MMKV/Core/MMKV_OSX.h +0 -57
  46. package/MMKV/Core/MemoryFile.cpp +0 -603
  47. package/MMKV/Core/MemoryFile.h +0 -194
  48. package/MMKV/Core/MemoryFile_Android.cpp +0 -236
  49. package/MMKV/Core/MemoryFile_Linux.cpp +0 -125
  50. package/MMKV/Core/MemoryFile_OSX.cpp +0 -142
  51. package/MMKV/Core/MemoryFile_Win32.cpp +0 -554
  52. package/MMKV/Core/MiniPBCoder.cpp +0 -672
  53. package/MMKV/Core/MiniPBCoder.h +0 -151
  54. package/MMKV/Core/MiniPBCoder_OSX.cpp +0 -237
  55. package/MMKV/Core/PBEncodeItem.hpp +0 -104
  56. package/MMKV/Core/PBUtility.cpp +0 -61
  57. package/MMKV/Core/PBUtility.h +0 -148
  58. package/MMKV/Core/ScopedLock.hpp +0 -69
  59. package/MMKV/Core/ThreadLock.cpp +0 -75
  60. package/MMKV/Core/ThreadLock.h +0 -81
  61. package/MMKV/Core/ThreadLock_Win32.cpp +0 -89
  62. package/MMKV/Core/aes/AESCrypt.cpp +0 -273
  63. package/MMKV/Core/aes/AESCrypt.h +0 -112
  64. package/MMKV/Core/aes/openssl/openssl_aes-armv4.S +0 -1243
  65. package/MMKV/Core/aes/openssl/openssl_aes.h +0 -130
  66. package/MMKV/Core/aes/openssl/openssl_aes_core.cpp +0 -1044
  67. package/MMKV/Core/aes/openssl/openssl_aes_locl.h +0 -38
  68. package/MMKV/Core/aes/openssl/openssl_aesv8-armx.S +0 -308
  69. package/MMKV/Core/aes/openssl/openssl_arm_arch.h +0 -84
  70. package/MMKV/Core/aes/openssl/openssl_cfb128.cpp +0 -97
  71. package/MMKV/Core/aes/openssl/openssl_md32_common.h +0 -254
  72. package/MMKV/Core/aes/openssl/openssl_md5.h +0 -49
  73. package/MMKV/Core/aes/openssl/openssl_md5_dgst.cpp +0 -166
  74. package/MMKV/Core/aes/openssl/openssl_md5_locl.h +0 -75
  75. package/MMKV/Core/aes/openssl/openssl_md5_one.cpp +0 -30
  76. package/MMKV/Core/aes/openssl/openssl_opensslconf.h +0 -271
  77. package/MMKV/Core/core.vcxproj +0 -288
  78. package/MMKV/Core/core.vcxproj.filters +0 -150
  79. package/MMKV/Core/crc32/Checksum.h +0 -75
  80. package/MMKV/Core/crc32/crc32_armv8.cpp +0 -134
  81. package/MMKV/Core/crc32/zlib/CMakeLists.txt +0 -60
  82. package/MMKV/Core/crc32/zlib/crc32.cpp +0 -55
  83. package/MMKV/Core/crc32/zlib/crc32.h +0 -48
  84. package/MMKV/Core/crc32/zlib/zconf.h +0 -380
  85. package/MMKV/Core/crc32/zlib/zutil.h +0 -25
  86. package/MMKV/LICENSE.TXT +0 -193
  87. package/MMKV/README.md +0 -354
@@ -1,293 +0,0 @@
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
- #ifndef MMKV_SRC_MMKVPREDEF_H
22
- #define MMKV_SRC_MMKVPREDEF_H
23
-
24
- // disable encryption & decryption to reduce some code
25
- // #define MMKV_DISABLE_CRYPT
26
- //#define MMKV_DISABLE_FLUTTER
27
-
28
- // using POSIX implementation
29
- //#define FORCE_POSIX
30
-
31
- #ifdef __cplusplus
32
-
33
- #include <string>
34
- #include <vector>
35
- #include <unordered_map>
36
-
37
- constexpr auto MMKV_VERSION = "v2.0.0";
38
-
39
- #ifdef DEBUG
40
- # define MMKV_DEBUG
41
- #endif
42
-
43
- #ifdef NDEBUG
44
- # undef MMKV_DEBUG
45
- #endif
46
-
47
- #if __cplusplus>=202002L
48
- # define MMKV_HAS_CPP20
49
- #endif
50
-
51
- #ifdef __ANDROID__
52
- # ifdef FORCE_POSIX
53
- # define MMKV_POSIX
54
- # else
55
- # define MMKV_ANDROID
56
- # endif
57
- #elif __OHOS__
58
- # ifdef FORCE_POSIX
59
- # define MMKV_POSIX
60
- # else
61
- # define MMKV_ANDROID
62
- # define MMKV_OHOS
63
- #endif
64
- #elif __APPLE__
65
- # ifdef FORCE_POSIX
66
- # define MMKV_POSIX
67
- # else
68
- # define MMKV_APPLE
69
- # ifdef __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__
70
- # define MMKV_IOS
71
- # elif __ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__
72
- # define MMKV_WATCH
73
- # else
74
- # define MMKV_MAC
75
- # endif
76
- # endif // FORCE_POSIX
77
- #elif __linux__ || __unix__
78
- # define MMKV_POSIX
79
- # if __linux__
80
- # define MMKV_LINUX
81
- # endif
82
- #elif _WIN32
83
- # define MMKV_WIN32
84
- #endif
85
-
86
- #ifdef MMKV_WIN32
87
- # if !defined(_WIN32_WINNT)
88
- # define _WIN32_WINNT _WIN32_WINNT_WINXP
89
- # endif
90
-
91
- # include <SDKDDKVer.h>
92
- // Exclude rarely-used stuff from Windows headers
93
- # define WIN32_LEAN_AND_MEAN
94
- // Windows Header Files
95
- # include <windows.h>
96
-
97
- constexpr auto MMKV_PATH_SLASH = L"\\";
98
- # define MMKV_PATH_FORMAT "%ls"
99
- using MMKVFileHandle_t = HANDLE;
100
- using MMKVPath_t = std::wstring;
101
- extern MMKVPath_t string2MMKVPath_t(const std::string &str);
102
- extern std::string MMKVPath_t2String(const MMKVPath_t &str);
103
-
104
- # ifndef MMKV_EMBED_ZLIB
105
- # define MMKV_EMBED_ZLIB 1
106
- # endif
107
-
108
- #else // MMKV_WIN32
109
-
110
- constexpr auto MMKV_PATH_SLASH = "/";
111
- # define MMKV_PATH_FORMAT "%s"
112
- using MMKVFileHandle_t = int;
113
- using MMKVPath_t = std::string;
114
- # define string2MMKVPath_t(str) (str)
115
- # define MMKVPath_t2String(str) (str)
116
-
117
- # ifndef MMKV_EMBED_ZLIB
118
- # define MMKV_EMBED_ZLIB 0
119
- # endif
120
-
121
- #endif // MMKV_WIN32
122
-
123
- #ifdef MMKV_APPLE
124
- # import <Foundation/Foundation.h>
125
- # define MMKV_NAMESPACE_BEGIN namespace mmkv {
126
- # define MMKV_NAMESPACE_END }
127
- # define MMKV_NAMESPACE_PREFIX mmkv
128
- using MMKVLog_t = NSString *;
129
- #else
130
- # define MMKV_NAMESPACE_BEGIN
131
- # define MMKV_NAMESPACE_END
132
- # define MMKV_NAMESPACE_PREFIX
133
- using MMKVLog_t = const std::string &;
134
- #endif // MMKV_APPLE
135
-
136
- MMKV_NAMESPACE_BEGIN
137
-
138
- enum MMKVLogLevel : int {
139
- MMKVLogDebug = 0, // not available for release/product build
140
- MMKVLogInfo = 1, // default level
141
- MMKVLogWarning,
142
- MMKVLogError,
143
- MMKVLogNone, // special level used to disable all log messages
144
- };
145
-
146
- enum MMKVRecoverStrategic : int {
147
- OnErrorDiscard = 0,
148
- OnErrorRecover,
149
- };
150
-
151
- enum MMKVErrorType : int {
152
- MMKVCRCCheckFail = 0,
153
- MMKVFileLength,
154
- };
155
-
156
- enum SyncFlag : bool { MMKV_SYNC = true, MMKV_ASYNC = false };
157
-
158
- MMKV_NAMESPACE_END
159
-
160
- namespace mmkv {
161
-
162
- typedef void (*LogHandler)(MMKVLogLevel level, const char *file, int line, const char *function, MMKVLog_t message);
163
-
164
- // by default MMKV will discard all datas on failure
165
- // return `OnErrorRecover` to recover any data from file
166
- typedef MMKVRecoverStrategic (*ErrorHandler)(const std::string &mmapID, MMKVErrorType errorType);
167
-
168
- // called when content is changed by other process
169
- // doesn't guarantee real-time notification
170
- typedef void (*ContentChangeHandler)(const std::string &mmapID);
171
-
172
- extern size_t DEFAULT_MMAP_SIZE;
173
- #define DEFAULT_MMAP_ID "mmkv.default"
174
-
175
- class MMBuffer;
176
- struct KeyValueHolder;
177
-
178
- #ifdef MMKV_DISABLE_CRYPT
179
- using KeyValueHolderCrypt = KeyValueHolder;
180
- #else
181
- struct KeyValueHolderCrypt;
182
- #endif
183
-
184
- #ifdef MMKV_APPLE
185
- struct KeyHasher {
186
- size_t operator()(NSString *key) const { return key.hash; }
187
- };
188
-
189
- struct KeyEqualer {
190
- bool operator()(NSString *left, NSString *right) const {
191
- if (left == right) {
192
- return true;
193
- }
194
- return ([left isEqualToString:right] == YES);
195
- }
196
- };
197
-
198
- using MMKVVector = std::vector<std::pair<NSString *, mmkv::MMBuffer>>;
199
- using MMKVMap = std::unordered_map<NSString *, mmkv::KeyValueHolder, KeyHasher, KeyEqualer>;
200
- using MMKVMapCrypt = std::unordered_map<NSString *, mmkv::KeyValueHolderCrypt, KeyHasher, KeyEqualer>;
201
- #else
202
- struct KeyHasher {
203
- // enables heterogeneous lookup
204
- using is_transparent = void;
205
-
206
- std::size_t operator()(const std::string_view& str) const {
207
- return std::hash<std::string_view>{}(str);
208
- }
209
-
210
- std::size_t operator()(const std::string& str) const {
211
- return std::hash<std::string>{}(str);
212
- }
213
- };
214
-
215
- struct KeyEqualer {
216
- // enables heterogeneous lookup
217
- using is_transparent = void;
218
-
219
- bool operator()(const std::string_view& lhs, const std::string_view& rhs) const {
220
- return lhs == rhs;
221
- }
222
-
223
- bool operator()(const std::string& lhs, const std::string& rhs) const {
224
- return lhs == rhs;
225
- }
226
- };
227
- using MMKVVector = std::vector<std::pair<std::string, mmkv::MMBuffer>>;
228
- using MMKVMap = std::unordered_map<std::string, mmkv::KeyValueHolder, KeyHasher, KeyEqualer>;
229
- using MMKVMapCrypt = std::unordered_map<std::string, mmkv::KeyValueHolderCrypt, KeyHasher, KeyEqualer>;
230
- #endif // MMKV_APPLE
231
-
232
- template <typename T>
233
- void unused(const T &) {}
234
-
235
- constexpr size_t AES_KEY_LEN = 16;
236
- constexpr size_t AES_KEY_BITSET_LEN = 128;
237
-
238
- } // namespace mmkv
239
-
240
- #ifdef MMKV_DEBUG
241
- # include <cassert>
242
- # define MMKV_ASSERT(var) assert(var)
243
- #else
244
- # define MMKV_ASSERT(var) mmkv::unused(var)
245
- #endif
246
-
247
- #endif //cplus-plus
248
-
249
- #ifndef MMKV_WIN32
250
- # ifndef likely
251
- # define mmkv_unlikely(x) (__builtin_expect(bool(x), 0))
252
- # define mmkv_likely(x) (__builtin_expect(bool(x), 1))
253
- # endif
254
- #else
255
- # ifndef likely
256
- # define mmkv_unlikely(x) (x)
257
- # define mmkv_likely(x) (x)
258
- # endif
259
- #endif
260
-
261
- #if defined(__arm__)
262
- #if defined(__ARM_ARCH_7A__)
263
- #if defined(__ARM_NEON__)
264
- #if defined(__ARM_PCS_VFP)
265
- #define MMKV_ABI "armeabi-v7a/NEON (hard-float)"
266
- #else
267
- #define MMKV_ABI "armeabi-v7a/NEON"
268
- #endif
269
- #else
270
- #if defined(__ARM_PCS_VFP)
271
- #define MMKV_ABI "armeabi-v7a (hard-float)"
272
- #else
273
- #define MMKV_ABI "armeabi-v7a"
274
- #endif
275
- #endif
276
- #else
277
- #define MMKV_ABI "armeabi"
278
- #endif
279
- #elif defined(__i386__) || defined(_M_IX86)
280
- #define MMKV_ABI "x86"
281
- #elif defined(__x86_64__) || defined(_M_X64)
282
- #define MMKV_ABI "x86_64"
283
- #elif defined(__mips64)
284
- #define MMKV_ABI "mips64"
285
- #elif defined(__mips__)
286
- #define MMKV_ABI "mips"
287
- #elif defined(__aarch64__) || defined(_M_ARM64)
288
- #define MMKV_ABI "arm64-v8a"
289
- #else
290
- #define MMKV_ABI "unknown"
291
- #endif
292
-
293
- #endif //MMKV_SRC_MMKVPREDEF_H
@@ -1,261 +0,0 @@
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 "MMKV.h"
22
- // #include <bits/alltypes.h>
23
-
24
- #ifdef MMKV_ANDROID
25
-
26
- # include "InterProcessLock.h"
27
- # include "KeyValueHolder.h"
28
- # include "MMKVLog.h"
29
- # include "MMKVMetaInfo.hpp"
30
- # include "MemoryFile.h"
31
- # include "ScopedLock.hpp"
32
- # include "ThreadLock.h"
33
- # include <unistd.h>
34
- # include "MMKV_IO.h"
35
-
36
- using namespace std;
37
- using namespace mmkv;
38
-
39
- extern unordered_map<string, MMKV *> *g_instanceDic;
40
- extern ThreadLock *g_instanceLock;
41
-
42
- MMKV::MMKV(const string &mmapID, int size, MMKVMode mode, string *cryptKey, string *rootPath, size_t expectedCapacity)
43
- : m_mmapID((mode & MMKV_BACKUP) ? mmapID : mmapedKVKey(mmapID, rootPath)) // historically Android mistakenly use mmapKey as mmapID
44
- , m_mode(mode)
45
- , m_path(mappedKVPathWithID(m_mmapID, mode, rootPath))
46
- , m_crcPath(crcPathWithID(m_mmapID, mode, rootPath))
47
- , m_dic(nullptr)
48
- , m_dicCrypt(nullptr)
49
- , m_expectedCapacity(std::max<size_t>(DEFAULT_MMAP_SIZE, roundUp<size_t>(expectedCapacity, DEFAULT_MMAP_SIZE)))
50
- , m_file(new MemoryFile(m_path, size, (mode & MMKV_ASHMEM) ? MMFILE_TYPE_ASHMEM : MMFILE_TYPE_FILE, m_expectedCapacity, isReadOnly()))
51
- , m_metaFile(new MemoryFile(m_crcPath, DEFAULT_MMAP_SIZE, m_file->m_fileType, 0, isReadOnly()))
52
- , m_metaInfo(new MMKVMetaInfo())
53
- , m_crypter(nullptr)
54
- , m_lock(new ThreadLock())
55
- , m_fileLock(new FileLock(m_metaFile->getFd(), (mode & MMKV_ASHMEM)))
56
- , m_sharedProcessLock(new InterProcessLock(m_fileLock, SharedLockType))
57
- , m_exclusiveProcessLock(new InterProcessLock(m_fileLock, ExclusiveLockType)) {
58
- m_actualSize = 0;
59
- m_output = nullptr;
60
-
61
- // force use fcntl(), otherwise will conflict with MemoryFile::reloadFromFile()
62
- m_fileModeLock = new FileLock(m_file->getFd(), true);
63
- m_sharedProcessModeLock = new InterProcessLock(m_fileModeLock, SharedLockType);
64
- m_exclusiveProcessModeLock = nullptr;
65
-
66
- # ifndef MMKV_DISABLE_CRYPT
67
- if (cryptKey && cryptKey->length() > 0) {
68
- m_dicCrypt = new MMKVMapCrypt();
69
- m_crypter = new AESCrypt(cryptKey->data(), cryptKey->length());
70
- } else
71
- # endif
72
- {
73
- m_dic = new MMKVMap();
74
- }
75
-
76
- m_needLoadFromFile = true;
77
- m_hasFullWriteback = false;
78
-
79
- m_crcDigest = 0;
80
-
81
- m_sharedProcessLock->m_enable = isMultiProcess();
82
- m_exclusiveProcessLock->m_enable = isMultiProcess();
83
-
84
- // sensitive zone
85
- /*{
86
- SCOPED_LOCK(m_sharedProcessLock);
87
- loadFromFile();
88
- }*/
89
- }
90
-
91
- MMKV::MMKV(const string &mmapID, int ashmemFD, int ashmemMetaFD, string *cryptKey)
92
- : m_mmapID(mmapID)
93
- , m_mode(MMKV_ASHMEM)
94
- , m_path(mappedKVPathWithID(m_mmapID, MMKV_ASHMEM, nullptr))
95
- , m_crcPath(crcPathWithID(m_mmapID, MMKV_ASHMEM, nullptr))
96
- , m_dic(nullptr)
97
- , m_dicCrypt(nullptr)
98
- , m_file(new MemoryFile(ashmemFD))
99
- , m_metaFile(new MemoryFile(ashmemMetaFD))
100
- , m_metaInfo(new MMKVMetaInfo())
101
- , m_crypter(nullptr)
102
- , m_lock(new ThreadLock())
103
- , m_fileLock(new FileLock(m_metaFile->getFd(), true))
104
- , m_sharedProcessLock(new InterProcessLock(m_fileLock, SharedLockType))
105
- , m_exclusiveProcessLock(new InterProcessLock(m_fileLock, ExclusiveLockType)) {
106
-
107
- m_actualSize = 0;
108
- m_output = nullptr;
109
-
110
- // force use fcntl(), otherwise will conflict with MemoryFile::reloadFromFile()
111
- m_fileModeLock = new FileLock(m_file->getFd(), true);
112
- m_sharedProcessModeLock = new InterProcessLock(m_fileModeLock, SharedLockType);
113
- m_exclusiveProcessModeLock = nullptr;
114
-
115
- # ifndef MMKV_DISABLE_CRYPT
116
- if (cryptKey && cryptKey->length() > 0) {
117
- m_dicCrypt = new MMKVMapCrypt();
118
- m_crypter = new AESCrypt(cryptKey->data(), cryptKey->length());
119
- } else
120
- # endif
121
- {
122
- m_dic = new MMKVMap();
123
- }
124
-
125
- m_needLoadFromFile = true;
126
- m_hasFullWriteback = false;
127
-
128
- m_crcDigest = 0;
129
-
130
- m_sharedProcessLock->m_enable = true;
131
- m_exclusiveProcessLock->m_enable = true;
132
-
133
- // sensitive zone
134
- /*{
135
- SCOPED_LOCK(m_sharedProcessLock);
136
- loadFromFile();
137
- }*/
138
- }
139
-
140
- MMKV *MMKV::mmkvWithID(const string &mmapID, int size, MMKVMode mode, string *cryptKey, string *rootPath, size_t expectedCapacity) {
141
- if (mmapID.empty() || !g_instanceLock) {
142
- return nullptr;
143
- }
144
- SCOPED_LOCK(g_instanceLock);
145
-
146
- auto mmapKey = mmapedKVKey(mmapID, rootPath);
147
- auto itr = g_instanceDic->find(mmapKey);
148
- if (itr != g_instanceDic->end()) {
149
- MMKV *kv = itr->second;
150
- return kv;
151
- }
152
- if (rootPath) {
153
- if (!isFileExist(*rootPath)) {
154
- if (!mkPath(*rootPath)) {
155
- return nullptr;
156
- }
157
- }
158
- MMKVInfo("prepare to load %s (id %s) from rootPath %zu", mmapID.c_str(), mmapKey.c_str(), rootPath->c_str());
159
- }
160
- auto kv = new MMKV(mmapID, size, mode, cryptKey, rootPath, expectedCapacity);
161
- (*g_instanceDic)[mmapKey] = kv;
162
- return kv;
163
- }
164
-
165
- MMKV *MMKV::mmkvWithAshmemFD(const string &mmapID, int fd, int metaFD, string *cryptKey) {
166
-
167
- if (fd < 0 || !g_instanceLock) {
168
- return nullptr;
169
- }
170
- SCOPED_LOCK(g_instanceLock);
171
-
172
- auto itr = g_instanceDic->find(mmapID);
173
- if (itr != g_instanceDic->end()) {
174
- MMKV *kv = itr->second;
175
- # ifndef MMKV_DISABLE_CRYPT
176
- kv->checkReSetCryptKey(fd, metaFD, cryptKey);
177
- # endif
178
- return kv;
179
- }
180
- auto kv = new MMKV(mmapID, fd, metaFD, cryptKey);
181
- (*g_instanceDic)[mmapID] = kv;
182
- return kv;
183
- }
184
-
185
- int MMKV::ashmemFD() {
186
- return (m_file->m_fileType & mmkv::MMFILE_TYPE_ASHMEM) ? m_file->getFd() : -1;
187
- }
188
-
189
- int MMKV::ashmemMetaFD() {
190
- return (m_file->m_fileType & mmkv::MMFILE_TYPE_ASHMEM) ? m_metaFile->getFd() : -1;
191
- }
192
-
193
- # ifndef MMKV_DISABLE_CRYPT
194
- void MMKV::checkReSetCryptKey(int fd, int metaFD, string *cryptKey) {
195
- SCOPED_LOCK(m_lock);
196
-
197
- checkReSetCryptKey(cryptKey);
198
-
199
- if (m_file->m_fileType & MMFILE_TYPE_ASHMEM) {
200
- if (m_file->getFd() != fd) {
201
- ::close(fd);
202
- }
203
- if (m_metaFile->getFd() != metaFD) {
204
- ::close(metaFD);
205
- }
206
- }
207
- }
208
- # endif // MMKV_DISABLE_CRYPT
209
-
210
- bool MMKV::checkProcessMode() {
211
- // avoid exception on open() error
212
- if (!m_file->isFileValid()) {
213
- return true;
214
- }
215
-
216
- if (isMultiProcess()) {
217
- if (!m_exclusiveProcessModeLock) {
218
- m_exclusiveProcessModeLock = new InterProcessLock(m_fileModeLock, ExclusiveLockType);
219
- }
220
- // avoid multiple processes get shared lock at the same time, https://github.com/Tencent/MMKV/issues/523
221
- auto tryAgain = false;
222
- auto exclusiveLocked = m_exclusiveProcessModeLock->try_lock(&tryAgain);
223
- if (exclusiveLocked) {
224
- return true;
225
- }
226
- auto shareLocked = m_sharedProcessModeLock->try_lock();
227
- if (!shareLocked) {
228
- // this call will fail on most case, just do it to make sure
229
- m_exclusiveProcessModeLock->try_lock();
230
- return true;
231
- } else {
232
- if (!tryAgain) {
233
- // something wrong with the OS/filesystem, let's try again
234
- exclusiveLocked = m_exclusiveProcessModeLock->try_lock(&tryAgain);
235
- if (!exclusiveLocked && !tryAgain) {
236
- // still something wrong, we have to give up and assume it passed the test
237
- MMKVWarning("Got a shared lock, but fail to exclusive lock [%s], assume it's ok", m_mmapID.c_str());
238
- exclusiveLocked = true;
239
- }
240
- }
241
- if (!exclusiveLocked) {
242
- MMKVError("Got a shared lock, but fail to exclusive lock [%s]", m_mmapID.c_str());
243
- }
244
- return exclusiveLocked;
245
- }
246
- } else {
247
- auto tryAgain = false;
248
- auto shareLocked = m_sharedProcessModeLock->try_lock(&tryAgain);
249
- if (!shareLocked && !tryAgain) {
250
- // something wrong with the OS/filesystem, we have to give up and assume it passed the test
251
- MMKVWarning("Fail to shared lock [%s], assume it's ok", m_mmapID.c_str());
252
- shareLocked = true;
253
- }
254
- if (!shareLocked) {
255
- MMKVError("Fail to share lock [%s]", m_mmapID.c_str());
256
- }
257
- return shareLocked;
258
- }
259
- }
260
-
261
- #endif // MMKV_ANDROID