react-native-mmkv 3.0.0-beta.2 → 3.0.0-beta.4

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 (56) hide show
  1. package/MMKV/Core/CMakeLists.txt +12 -4
  2. package/MMKV/Core/Core.xcodeproj/project.pbxproj +13 -4
  3. package/MMKV/Core/Core.xcodeproj/xcshareddata/xcschemes/Core.xcscheme +1 -1
  4. package/MMKV/Core/Core.xcodeproj/xcshareddata/xcschemes/MMKVWatchCore.xcscheme +1 -1
  5. package/MMKV/Core/MMKV.cpp +3 -4
  6. package/MMKV/Core/MMKVLog_Android.cpp +56 -1
  7. package/MMKV/Core/MMKVPredef.h +8 -1
  8. package/MMKV/Core/MMKV_Android.cpp +3 -3
  9. package/MMKV/Core/MMKV_IO.cpp +1 -1
  10. package/MMKV/Core/MemoryFile.cpp +33 -2
  11. package/MMKV/Core/MemoryFile.h +1 -0
  12. package/MMKV/Core/MemoryFile_Android.cpp +13 -3
  13. package/MMKV/Core/MemoryFile_Linux.cpp +8 -3
  14. package/MMKV/Core/MemoryFile_Win32.cpp +3 -3
  15. package/MMKV/Core/ThreadLock.cpp +9 -5
  16. package/MMKV/Core/ThreadLock.h +2 -3
  17. package/MMKV/Core/aes/AESCrypt.h +1 -0
  18. package/MMKV/Core/aes/openssl/openssl_aes.h +12 -0
  19. package/MMKV/Core/aes/openssl/openssl_aes_core.cpp +4 -4
  20. package/MMKV/Core/core.vcxproj +102 -0
  21. package/MMKV/Core/crc32/Checksum.h +5 -0
  22. package/MMKV/Core/crc32/crc32_armv8.cpp +3 -1
  23. package/MMKV/Core/crc32/zlib/CMakeLists.txt +60 -0
  24. package/MMKV/LICENSE.TXT +193 -0
  25. package/MMKV/README.md +74 -10
  26. package/README.md +12 -5
  27. package/cpp/MmkvHostObject.cpp +1 -0
  28. package/cpp/NativeMmkvModule.cpp +2 -5
  29. package/lib/commonjs/LazyTurboModule.js +26 -9
  30. package/lib/commonjs/LazyTurboModule.js.map +1 -1
  31. package/lib/commonjs/MMKV.js +1 -1
  32. package/lib/commonjs/MMKV.js.map +1 -1
  33. package/lib/commonjs/PlatformChecker.js +3 -3
  34. package/lib/commonjs/PlatformChecker.js.map +1 -1
  35. package/lib/commonjs/index.js +19 -0
  36. package/lib/commonjs/index.js.map +1 -1
  37. package/lib/module/LazyTurboModule.js +26 -9
  38. package/lib/module/LazyTurboModule.js.map +1 -1
  39. package/lib/module/MMKV.js +2 -2
  40. package/lib/module/MMKV.js.map +1 -1
  41. package/lib/module/PlatformChecker.js +2 -2
  42. package/lib/module/PlatformChecker.js.map +1 -1
  43. package/lib/module/index.js +1 -0
  44. package/lib/module/index.js.map +1 -1
  45. package/lib/typescript/src/LazyTurboModule.d.ts +3 -0
  46. package/lib/typescript/src/LazyTurboModule.d.ts.map +1 -1
  47. package/lib/typescript/src/PlatformChecker.d.ts +1 -1
  48. package/lib/typescript/src/PlatformChecker.d.ts.map +1 -1
  49. package/lib/typescript/src/index.d.ts +1 -0
  50. package/lib/typescript/src/index.d.ts.map +1 -1
  51. package/package.json +5 -5
  52. package/react-native.config.js +1 -1
  53. package/src/LazyTurboModule.ts +48 -12
  54. package/src/MMKV.ts +2 -2
  55. package/src/PlatformChecker.ts +4 -2
  56. package/src/index.ts +2 -0
@@ -47,8 +47,14 @@ ELSEIF("${ANDROID_ABI}" STREQUAL "armeabi")
47
47
  ENDIF()
48
48
 
49
49
  #include(CMakePrintHelpers)
50
- #cmake_print_variables(CMAKE_SYSTEM_PROCESSOR)
51
- IF(UNIX AND (NOT APPLE))
50
+ #cmake_print_variables(CMAKE_SYSTEM_PROCESSOR OHOS OHOS_ARCH CFLAGS ASM_OPTIONS CMAKE_ASM_FLAGS)
51
+
52
+ IF(OHOS)
53
+ IF("${OHOS_ARCH}" STREQUAL "arm64-v8a")
54
+ SET(ASM_OPTIONS "-x assembler-with-cpp")
55
+ SET(CMAKE_ASM_FLAGS "${CFLAGS} ${ASM_OPTIONS} -march=armv8+crypto -D__MUSL__")
56
+ ENDIF()
57
+ ELSEIF(UNIX AND (NOT APPLE))
52
58
  IF("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "aarch64")
53
59
  SET(ASM_OPTIONS "-x assembler-with-cpp")
54
60
  SET(CMAKE_ASM_FLAGS "${CFLAGS} ${ASM_OPTIONS} -march=armv8-a+crypto")
@@ -131,9 +137,11 @@ add_library(core
131
137
  MMKVPredef.h
132
138
  )
133
139
 
134
- IF (NOT MSVC)
140
+ IF (MSVC OR (CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64"))
135
141
  # .S files is not supported by MSVC.
136
- target_sources(core PRIVATE
142
+ # x86_64 asm not supported in OHOS
143
+ ELSE()
144
+ target_sources(core PRIVATE
137
145
  aes/openssl/openssl_aesv8-armx.S
138
146
  aes/openssl/openssl_aes-armv4.S)
139
147
  ENDIF()
@@ -3,7 +3,7 @@
3
3
  archiveVersion = 1;
4
4
  classes = {
5
5
  };
6
- objectVersion = 50;
6
+ objectVersion = 54;
7
7
  objects = {
8
8
 
9
9
  /* Begin PBXBuildFile section */
@@ -349,7 +349,8 @@
349
349
  CB9563D023AB2D9500ACCD39 /* Project object */ = {
350
350
  isa = PBXProject;
351
351
  attributes = {
352
- LastUpgradeCheck = 1420;
352
+ BuildIndependentTargetsInParallel = YES;
353
+ LastUpgradeCheck = 1530;
353
354
  ORGANIZATIONNAME = lingol;
354
355
  TargetAttributes = {
355
356
  CB9563D723AB2D9500ACCD39 = {
@@ -448,6 +449,7 @@
448
449
  isa = XCBuildConfiguration;
449
450
  buildSettings = {
450
451
  ALWAYS_SEARCH_USER_PATHS = NO;
452
+ ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES;
451
453
  CLANG_ANALYZER_NONNULL = YES;
452
454
  CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
453
455
  CLANG_CXX_LANGUAGE_STANDARD = "gnu++14";
@@ -481,6 +483,7 @@
481
483
  DEBUG_INFORMATION_FORMAT = dwarf;
482
484
  ENABLE_STRICT_OBJC_MSGSEND = YES;
483
485
  ENABLE_TESTABILITY = YES;
486
+ ENABLE_USER_SCRIPT_SANDBOXING = YES;
484
487
  GCC_C_LANGUAGE_STANDARD = gnu11;
485
488
  GCC_DYNAMIC_NO_PIC = NO;
486
489
  GCC_NO_COMMON_BLOCKS = YES;
@@ -495,7 +498,7 @@
495
498
  GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
496
499
  GCC_WARN_UNUSED_FUNCTION = YES;
497
500
  GCC_WARN_UNUSED_VARIABLE = YES;
498
- IPHONEOS_DEPLOYMENT_TARGET = 11.0;
501
+ IPHONEOS_DEPLOYMENT_TARGET = 12.0;
499
502
  MACOSX_DEPLOYMENT_TARGET = 10.13;
500
503
  MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
501
504
  MTL_FAST_MATH = YES;
@@ -511,6 +514,7 @@
511
514
  isa = XCBuildConfiguration;
512
515
  buildSettings = {
513
516
  ALWAYS_SEARCH_USER_PATHS = NO;
517
+ ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES;
514
518
  CLANG_ANALYZER_NONNULL = YES;
515
519
  CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
516
520
  CLANG_CXX_LANGUAGE_STANDARD = "gnu++14";
@@ -544,6 +548,7 @@
544
548
  DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
545
549
  ENABLE_NS_ASSERTIONS = NO;
546
550
  ENABLE_STRICT_OBJC_MSGSEND = YES;
551
+ ENABLE_USER_SCRIPT_SANDBOXING = YES;
547
552
  GCC_C_LANGUAGE_STANDARD = gnu11;
548
553
  GCC_NO_COMMON_BLOCKS = YES;
549
554
  GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
@@ -552,7 +557,7 @@
552
557
  GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
553
558
  GCC_WARN_UNUSED_FUNCTION = YES;
554
559
  GCC_WARN_UNUSED_VARIABLE = YES;
555
- IPHONEOS_DEPLOYMENT_TARGET = 11.0;
560
+ IPHONEOS_DEPLOYMENT_TARGET = 12.0;
556
561
  MACOSX_DEPLOYMENT_TARGET = 10.13;
557
562
  MTL_ENABLE_DEBUG_INFO = NO;
558
563
  MTL_FAST_MATH = YES;
@@ -580,6 +585,7 @@
580
585
  GCC_WARN_SHADOW = YES;
581
586
  GCC_WARN_SIGN_COMPARE = YES;
582
587
  GCC_WARN_UNUSED_PARAMETER = NO;
588
+ MACOSX_DEPLOYMENT_TARGET = "$(RECOMMENDED_MACOSX_DEPLOYMENT_TARGET)";
583
589
  OTHER_LDFLAGS = "-ObjC";
584
590
  PRODUCT_NAME = "$(TARGET_NAME)";
585
591
  SDKROOT = "";
@@ -606,6 +612,7 @@
606
612
  GCC_WARN_SHADOW = YES;
607
613
  GCC_WARN_SIGN_COMPARE = YES;
608
614
  GCC_WARN_UNUSED_PARAMETER = NO;
615
+ MACOSX_DEPLOYMENT_TARGET = "$(RECOMMENDED_MACOSX_DEPLOYMENT_TARGET)";
609
616
  OTHER_LDFLAGS = "-ObjC";
610
617
  PRODUCT_NAME = "$(TARGET_NAME)";
611
618
  SDKROOT = "";
@@ -630,6 +637,7 @@
630
637
  GCC_WARN_SHADOW = YES;
631
638
  GCC_WARN_SIGN_COMPARE = YES;
632
639
  GCC_WARN_UNUSED_PARAMETER = NO;
640
+ MACOSX_DEPLOYMENT_TARGET = "$(RECOMMENDED_MACOSX_DEPLOYMENT_TARGET)";
633
641
  OTHER_LDFLAGS = "-ObjC";
634
642
  PRODUCT_NAME = MMKVCore;
635
643
  SDKROOT = watchos;
@@ -655,6 +663,7 @@
655
663
  GCC_WARN_SHADOW = YES;
656
664
  GCC_WARN_SIGN_COMPARE = YES;
657
665
  GCC_WARN_UNUSED_PARAMETER = NO;
666
+ MACOSX_DEPLOYMENT_TARGET = "$(RECOMMENDED_MACOSX_DEPLOYMENT_TARGET)";
658
667
  OTHER_LDFLAGS = "-ObjC";
659
668
  PRODUCT_NAME = MMKVCore;
660
669
  SDKROOT = watchos;
@@ -1,6 +1,6 @@
1
1
  <?xml version="1.0" encoding="UTF-8"?>
2
2
  <Scheme
3
- LastUpgradeVersion = "1420"
3
+ LastUpgradeVersion = "1530"
4
4
  version = "1.3">
5
5
  <BuildAction
6
6
  parallelizeBuildables = "YES"
@@ -1,6 +1,6 @@
1
1
  <?xml version="1.0" encoding="UTF-8"?>
2
2
  <Scheme
3
- LastUpgradeVersion = "1420"
3
+ LastUpgradeVersion = "1530"
4
4
  version = "1.3">
5
5
  <BuildAction
6
6
  parallelizeBuildables = "YES"
@@ -40,10 +40,9 @@
40
40
  #include <cstdio>
41
41
  #include <cstring>
42
42
  #include <unordered_set>
43
- //#include <unistd.h>
44
43
  #include <cassert>
45
44
 
46
- #if defined(__aarch64__) && defined(__linux)
45
+ #if defined(__aarch64__) && defined(__linux__) && !defined (MMKV_OHOS)
47
46
  # include <asm/hwcap.h>
48
47
  # include <sys/auxv.h>
49
48
  #endif
@@ -167,7 +166,7 @@ void initialize() {
167
166
  MMKVInfo("version %s, page size %d, arch %s", MMKV_VERSION, DEFAULT_MMAP_SIZE, MMKV_ABI);
168
167
 
169
168
  // get CPU status of ARMv8 extensions (CRC32, AES)
170
- #if defined(__aarch64__) && defined(__linux__)
169
+ #if defined(__aarch64__) && defined(__linux__) && !defined (MMKV_OHOS)
171
170
  auto hwcaps = getauxval(AT_HWCAP);
172
171
  # ifndef MMKV_DISABLE_CRYPT
173
172
  if (hwcaps & HWCAP_AES) {
@@ -188,7 +187,7 @@ void initialize() {
188
187
  MMKVInfo("armv8 CRC32 instructions is not supported");
189
188
  }
190
189
  # endif // MMKV_USE_ARMV8_CRC32
191
- #endif // __aarch64__ && defined(__linux__)
190
+ #endif // __aarch64__ && defined(__linux__) && !defined (MMKV_OHOS)
192
191
 
193
192
  #if defined(MMKV_DEBUG) && !defined(MMKV_DISABLE_CRYPT)
194
193
  // AESCrypt::testAESCrypt();
@@ -21,7 +21,62 @@
21
21
  #include "MMKVLog.h"
22
22
 
23
23
  #ifdef ENABLE_MMKV_LOG
24
- # ifdef MMKV_ANDROID
24
+ # ifdef MMKV_OHOS
25
+ # include <hilog/log.h>
26
+ # include <cstdarg>
27
+ # include <string>
28
+
29
+ using namespace std;
30
+
31
+ constexpr auto APP_NAME = "MMKV";
32
+
33
+ static LogLevel MMKVLogLevelDesc(MMKVLogLevel level) {
34
+ switch (level) {
35
+ case MMKVLogDebug:
36
+ return LOG_DEBUG;
37
+ case MMKVLogInfo:
38
+ return LOG_INFO;
39
+ case MMKVLogWarning:
40
+ return LOG_WARN;
41
+ case MMKVLogError:
42
+ return LOG_ERROR;
43
+ default:
44
+ return LOG_INFO;
45
+ }
46
+ }
47
+
48
+ void _MMKVLogWithLevel(MMKVLogLevel level, const char *filename, const char *func, int line, const char *format, ...) {
49
+ if (level >= g_currentLogLevel) {
50
+ string message;
51
+ char buffer[16];
52
+
53
+ va_list args;
54
+ va_start(args, format);
55
+ auto length = std::vsnprintf(buffer, sizeof(buffer), format, args);
56
+ va_end(args);
57
+
58
+ if (length < 0) { // something wrong
59
+ message = {};
60
+ } else if (length < sizeof(buffer)) {
61
+ message = string(buffer, static_cast<unsigned long>(length));
62
+ } else {
63
+ message.resize(static_cast<unsigned long>(length), '\0');
64
+ va_start(args, format);
65
+ std::vsnprintf(const_cast<char *>(message.data()), static_cast<size_t>(length) + 1, format, args);
66
+ va_end(args);
67
+ }
68
+
69
+ if (g_logHandler) {
70
+ g_logHandler(level, filename, line, func, message);
71
+ } else {
72
+ auto desc = MMKVLogLevelDesc(level);
73
+ OH_LOG_Print(LOG_APP, desc, 0, APP_NAME, "<%{public}s:%{public}d::%{public}s> %{public}s",
74
+ filename, line, func, message.c_str());
75
+ }
76
+ }
77
+ }
78
+
79
+ # elif defined(MMKV_ANDROID)
25
80
  # include <android/log.h>
26
81
  # include <cstdarg>
27
82
  # include <string>
@@ -34,7 +34,7 @@
34
34
  #include <vector>
35
35
  #include <unordered_map>
36
36
 
37
- constexpr auto MMKV_VERSION = "v1.3.4";
37
+ constexpr auto MMKV_VERSION = "v1.3.5";
38
38
 
39
39
  #ifdef DEBUG
40
40
  # define MMKV_DEBUG
@@ -50,6 +50,13 @@ constexpr auto MMKV_VERSION = "v1.3.4";
50
50
  # else
51
51
  # define MMKV_ANDROID
52
52
  # endif
53
+ #elif __OHOS__
54
+ # ifdef FORCE_POSIX
55
+ # define MMKV_POSIX
56
+ # else
57
+ # define MMKV_ANDROID
58
+ # define MMKV_OHOS
59
+ #endif
53
60
  #elif __APPLE__
54
61
  # ifdef FORCE_POSIX
55
62
  # define MMKV_POSIX
@@ -19,6 +19,7 @@
19
19
  */
20
20
 
21
21
  #include "MMKV.h"
22
+ // #include <bits/alltypes.h>
22
23
 
23
24
  #ifdef MMKV_ANDROID
24
25
 
@@ -136,8 +137,7 @@ MMKV::MMKV(const string &mmapID, int ashmemFD, int ashmemMetaFD, string *cryptKe
136
137
  }*/
137
138
  }
138
139
 
139
- MMKV *MMKV::mmkvWithID(const string &mmapID, int size, MMKVMode mode, string *cryptKey, string *rootPath,
140
- size_t expectedCapacity) {
140
+ MMKV *MMKV::mmkvWithID(const string &mmapID, int size, MMKVMode mode, string *cryptKey, string *rootPath, size_t expectedCapacity) {
141
141
  if (mmapID.empty()) {
142
142
  return nullptr;
143
143
  }
@@ -155,7 +155,7 @@ MMKV *MMKV::mmkvWithID(const string &mmapID, int size, MMKVMode mode, string *cr
155
155
  return nullptr;
156
156
  }
157
157
  }
158
- MMKVInfo("prepare to load %s (id %s) from rootPath %s", mmapID.c_str(), mmapKey.c_str(), rootPath->c_str());
158
+ MMKVInfo("prepare to load %s (id %s) from rootPath %zu", mmapID.c_str(), mmapKey.c_str(), rootPath->c_str());
159
159
  }
160
160
  auto kv = new MMKV(mmapID, size, mode, cryptKey, rootPath, expectedCapacity);
161
161
  (*g_instanceDic)[mmapKey] = kv;
@@ -156,7 +156,7 @@ void MMKV::partialLoadFromFile() {
156
156
  size_t addedSize = m_actualSize - position;
157
157
  auto basePtr = (uint8_t *) m_file->getMemory() + Fixed32Size;
158
158
  // incremental update crc digest
159
- m_crcDigest = (uint32_t) CRC32(m_crcDigest, basePtr + position, addedSize);
159
+ m_crcDigest = (uint32_t) CRC32(m_crcDigest, basePtr + position, (z_size_t) addedSize);
160
160
  if (m_crcDigest == m_metaInfo->m_crcDigest) {
161
161
  MMBuffer inputBuffer(basePtr, m_actualSize, MMBufferNoCopy);
162
162
  #ifndef MMKV_DISABLE_CRYPT
@@ -267,10 +267,10 @@ bool isFileExist(const string &nsFilePath) {
267
267
  return false;
268
268
  }
269
269
 
270
- struct stat temp = {};
271
- return lstat(nsFilePath.c_str(), &temp) == 0;
270
+ return access(nsFilePath.c_str(), F_OK) == 0;
272
271
  }
273
272
 
273
+ #ifndef MMKV_APPLE
274
274
  extern bool mkPath(const MMKVPath_t &str) {
275
275
  char *path = strdup(str.c_str());
276
276
 
@@ -307,6 +307,19 @@ LContinue:
307
307
 
308
308
  return true;
309
309
  }
310
+ #else
311
+ // avoid using so-called privacy API
312
+ extern bool mkPath(const MMKVPath_t &str) {
313
+ auto path = [NSString stringWithUTF8String:str.c_str()];
314
+ NSError *error = nil;
315
+ auto ret = [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error];
316
+ if (!ret) {
317
+ MMKVWarning("%s", error.localizedDescription.UTF8String);
318
+ return false;
319
+ }
320
+ return true;
321
+ }
322
+ #endif
310
323
 
311
324
  MMBuffer *readWholeFile(const MMKVPath_t &path) {
312
325
  MMBuffer *buffer = nullptr;
@@ -360,6 +373,7 @@ bool zeroFillFile(int fd, size_t startPos, size_t size) {
360
373
  return true;
361
374
  }
362
375
 
376
+ #ifndef MMKV_APPLE
363
377
  bool getFileSize(int fd, size_t &size) {
364
378
  struct stat st = {};
365
379
  if (fstat(fd, &st) != -1) {
@@ -368,6 +382,23 @@ bool getFileSize(int fd, size_t &size) {
368
382
  }
369
383
  return false;
370
384
  }
385
+ #else
386
+ // avoid using so-called privacy API
387
+ bool getFileSize(int fd, size_t &size) {
388
+ auto cur = lseek(fd, 0, SEEK_CUR);
389
+ if (cur == -1) {
390
+ return false;
391
+ }
392
+ auto end = lseek(fd, 0, SEEK_END);
393
+ if (end == -1) {
394
+ return false;
395
+ }
396
+ size = (size_t) end;
397
+
398
+ lseek(fd, cur, SEEK_SET);
399
+ return true;
400
+ }
401
+ #endif
371
402
 
372
403
  size_t getPageSize() {
373
404
  return static_cast<size_t>(getpagesize());
@@ -23,6 +23,7 @@
23
23
  #ifdef __cplusplus
24
24
 
25
25
  #include "MMKVPredef.h"
26
+ #include <cstdint>
26
27
  #include <functional>
27
28
 
28
29
  #ifdef MMKV_ANDROID
@@ -114,9 +114,12 @@ constexpr auto ASHMEM_IOC = 0x77;
114
114
  # define ASHMEM_SET_SIZE _IOW(ASHMEM_IOC, 3, size_t)
115
115
  # define ASHMEM_GET_SIZE _IO(ASHMEM_IOC, 4)
116
116
 
117
+ #ifndef MMKV_OHOS
117
118
  int g_android_api = __ANDROID_API_L__;
119
+ #endif
118
120
  std::string g_android_tmpDir = "/data/local/tmp/";
119
121
 
122
+ #ifndef MMKV_OHOS
120
123
  void *loadLibrary() {
121
124
  auto name = "libandroid.so";
122
125
  static auto handle = dlopen(name, RTLD_LAZY | RTLD_LOCAL);
@@ -127,9 +130,13 @@ void *loadLibrary() {
127
130
  }
128
131
 
129
132
  typedef int (*AShmem_create_t)(const char *name, size_t size);
133
+ typedef size_t (*AShmem_getSize_t)(int fd);
134
+
135
+ #endif
130
136
 
131
137
  int ASharedMemory_create(const char *name, size_t size) {
132
138
  int fd = -1;
139
+ #ifndef MMKV_OHOS
133
140
  if (g_android_api >= __ANDROID_API_O__) {
134
141
  static auto handle = loadLibrary();
135
142
  static AShmem_create_t funcPtr =
@@ -143,6 +150,7 @@ int ASharedMemory_create(const char *name, size_t size) {
143
150
  MMKVWarning("fail to locate ASharedMemory_create() from loading libandroid.so");
144
151
  }
145
152
  }
153
+ #endif
146
154
  if (fd < 0) {
147
155
  fd = open(ASHMEM_NAME_DEF, O_RDWR | O_CLOEXEC);
148
156
  if (fd < 0) {
@@ -158,10 +166,9 @@ int ASharedMemory_create(const char *name, size_t size) {
158
166
  return fd;
159
167
  }
160
168
 
161
- typedef size_t (*AShmem_getSize_t)(int fd);
162
-
163
169
  size_t ASharedMemory_getSize(int fd) {
164
170
  size_t size = 0;
171
+ #ifndef MMKV_OHOS
165
172
  if (g_android_api >= __ANDROID_API_O__) {
166
173
  static auto handle = loadLibrary();
167
174
  static AShmem_getSize_t funcPtr =
@@ -175,6 +182,7 @@ size_t ASharedMemory_getSize(int fd) {
175
182
  MMKVWarning("fail to locate ASharedMemory_create() from loading libandroid.so");
176
183
  }
177
184
  }
185
+ #endif
178
186
  if (size == 0) {
179
187
  int tmp = ioctl(fd, ASHMEM_GET_SIZE, nullptr);
180
188
  if (tmp < 0) {
@@ -190,9 +198,11 @@ string ASharedMemory_getName(int fd) {
190
198
  // Android Q doesn't have ASharedMemory_getName()
191
199
  // I've make a request to Google, https://issuetracker.google.com/issues/130741665
192
200
  // There's nothing we can do before it's supported officially by Google
193
- if (g_android_api >= __ANDROID_API_Q__) {
201
+ #ifndef MMKV_OHOS
202
+ if (g_android_api >= __ANDROID_API_O__) {
194
203
  return "";
195
204
  }
205
+ #endif
196
206
 
197
207
  char name[ASHMEM_NAME_LEN] = {0};
198
208
  if (ioctl(fd, ASHMEM_GET_NAME, name) != 0) {
@@ -54,20 +54,25 @@ bool tryAtomicRename(const MMKVPath_t &srcPath, const MMKVPath_t &dstPath) {
54
54
  bool renamed = false;
55
55
 
56
56
  // try renameat2() first
57
- #ifdef SYS_renameat2
57
+ #if defined(SYS_renameat2) && !defined(MMKV_OHOS)
58
58
  #ifdef MMKV_ANDROID
59
59
  static auto g_renameat2 = (renameat2_t) dlsym(RTLD_DEFAULT, "renameat2");
60
60
  if (g_renameat2) {
61
61
  renamed = (g_renameat2(AT_FDCWD, srcPath.c_str(), AT_FDCWD, dstPath.c_str(), RENAME_EXCHANGE) == 0);
62
62
  }
63
+ if (!renamed && errno != ENOENT) {
64
+ MMKVWarning("fail on renameat2() [%s] to [%s], %d(%s)", srcPath.c_str(), dstPath.c_str(), errno,
65
+ strerror(errno));
66
+ }
63
67
  #endif
64
68
  if (!renamed) {
65
69
  renamed = (syscall(SYS_renameat2, AT_FDCWD, srcPath.c_str(), AT_FDCWD, dstPath.c_str(), RENAME_EXCHANGE) == 0);
66
70
  }
67
71
  if (!renamed && errno != ENOENT) {
68
- MMKVError("fail on renameat2() [%s] to [%s], %d(%s)", srcPath.c_str(), dstPath.c_str(), errno, strerror(errno));
72
+ MMKVWarning("fail on syscall(SYS_renameat2) [%s] to [%s], %d(%s)", srcPath.c_str(), dstPath.c_str(), errno,
73
+ strerror(errno));
69
74
  }
70
- #endif // SYS_renameat2
75
+ #endif // SYS_renameat2 && !MMKV_OHOS
71
76
 
72
77
  if (!renamed) {
73
78
  if (::rename(srcPath.c_str(), dstPath.c_str()) != 0) {
@@ -282,7 +282,7 @@ MMBuffer *readWholeFile(const MMKVPath_t &nsFilePath) {
282
282
  buffer = new MMBuffer(static_cast<size_t>(fileLength));
283
283
  SetFilePointer(fd, 0, 0, FILE_BEGIN);
284
284
  DWORD readSize = 0;
285
- if (ReadFile(fd, buffer->getPtr(), fileLength, &readSize, nullptr)) {
285
+ if (ReadFile(fd, buffer->getPtr(), (DWORD) fileLength, &readSize, nullptr)) {
286
286
  //fileSize = readSize;
287
287
  } else {
288
288
  MMKVWarning("fail to read %ls: %d", nsFilePath.c_str(), GetLastError());
@@ -323,7 +323,7 @@ bool zeroFillFile(MMKVFileHandle_t file, size_t startPos, size_t size) {
323
323
  }
324
324
  if (size > 0) {
325
325
  DWORD bytesWritten = 0;
326
- if (!WriteFile(file, zeros, size, &bytesWritten, nullptr)) {
326
+ if (!WriteFile(file, zeros, (DWORD) size, &bytesWritten, nullptr)) {
327
327
  MMKVError("fail to write fd[%p], error:%d", file, GetLastError());
328
328
  return false;
329
329
  }
@@ -407,7 +407,7 @@ bool copyFileContent(const MMKVPath_t &srcPath, MMKVFileHandle_t dstFD, bool nee
407
407
  // the Win32 platform don't have sendfile()/fcopyfile() equivalent, do it the hard way
408
408
  while (true) {
409
409
  DWORD sizeRead = 0;
410
- if (!ReadFile(srcFile.getFd(), buffer, bufferSize, &sizeRead, nullptr)) {
410
+ if (!ReadFile(srcFile.getFd(), buffer, (DWORD) bufferSize, &sizeRead, nullptr)) {
411
411
  MMKVError("fail to read %ls: %d", srcPath.c_str(), GetLastError());
412
412
  goto errorOut;
413
413
  }
@@ -27,7 +27,7 @@ using namespace std;
27
27
 
28
28
  namespace mmkv {
29
29
 
30
- ThreadLock::ThreadLock() {
30
+ ThreadLock::ThreadLock() : m_lock({}) {
31
31
  pthread_mutexattr_t attr;
32
32
  pthread_mutexattr_init(&attr);
33
33
  pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
@@ -38,13 +38,13 @@ ThreadLock::ThreadLock() {
38
38
  }
39
39
 
40
40
  ThreadLock::~ThreadLock() {
41
+ #ifdef MMKV_OHOS
42
+ // OHOS's pthread is buggy, check for POC: https://gist.github.com/lingol/622af352e090e0490ebacfe3a38b9221
43
+ pthread_mutex_unlock(&m_lock);
44
+ #endif
41
45
  pthread_mutex_destroy(&m_lock);
42
46
  }
43
47
 
44
- void ThreadLock::initialize() {
45
- return;
46
- }
47
-
48
48
  void ThreadLock::lock() {
49
49
  auto ret = pthread_mutex_lock(&m_lock);
50
50
  if (ret != 0) {
@@ -64,6 +64,10 @@ bool ThreadLock::try_lock() {
64
64
  return (ret == 0);
65
65
  }
66
66
 
67
+ void ThreadLock::initialize() {
68
+ return;
69
+ }
70
+
67
71
  void ThreadLock::ThreadOnce(ThreadOnceToken_t *onceToken, void (*callback)()) {
68
72
  pthread_once(onceToken, callback);
69
73
  }
@@ -20,6 +20,7 @@
20
20
 
21
21
  #ifndef MMKV_THREADLOCK_H
22
22
  #define MMKV_THREADLOCK_H
23
+
23
24
  #ifdef __cplusplus
24
25
 
25
26
  #include "MMKVPredef.h"
@@ -29,8 +30,7 @@
29
30
  # define MMKV_USING_PTHREAD 1
30
31
  #endif
31
32
 
32
- #if MMKV_USING_PTHREAD
33
- #else
33
+ #ifndef MMKV_USING_PTHREAD
34
34
  # include <atomic>
35
35
  #endif
36
36
 
@@ -45,7 +45,6 @@ using ThreadOnceToken_t = std::atomic<ThreadOnceTokenEnum>;
45
45
  #endif
46
46
 
47
47
  class ThreadLock {
48
- private:
49
48
  #if MMKV_USING_PTHREAD
50
49
  pthread_mutex_t m_lock;
51
50
  #else
@@ -24,6 +24,7 @@
24
24
 
25
25
  #include "../MMKVPredef.h"
26
26
  #include <cstddef>
27
+ #include <cstdint>
27
28
 
28
29
  #ifdef MMKV_DISABLE_CRYPT
29
30
 
@@ -13,6 +13,7 @@
13
13
 
14
14
  #include "openssl_opensslconf.h"
15
15
  #include "openssl_arm_arch.h"
16
+ #include <cstdint>
16
17
  #include <stddef.h>
17
18
  #include "../../MMKVPredef.h"
18
19
 
@@ -77,6 +78,15 @@ extern "C" int openssl_aes_armv8_set_decrypt_key(const uint8_t *userKey, const i
77
78
  extern "C" void openssl_aes_armv8_encrypt(const uint8_t *in, uint8_t *out, const void *key);
78
79
  extern "C" void openssl_aes_armv8_decrypt(const uint8_t *in, uint8_t *out, const void *key);
79
80
 
81
+ #ifdef MMKV_OHOS
82
+
83
+ #define AES_set_encrypt_key(userKey, bits, key) openssl_aes_armv8_set_encrypt_key(userKey, bits, key)
84
+ #define AES_set_decrypt_key(userKey, bits, key) openssl_aes_armv8_set_decrypt_key(userKey, bits, key)
85
+ #define AES_encrypt(in, out, key) openssl_aes_armv8_encrypt(in, out, key)
86
+ #define AES_decrypt(in, out, key) openssl_aes_armv8_decrypt(in, out, key)
87
+
88
+ #else
89
+
80
90
  typedef int (*aes_set_encrypt_t)(const uint8_t *userKey, const int bits, void *key);
81
91
  typedef int (*aes_set_decrypt_t)(const uint8_t *userKey, const int bits, void *key);
82
92
  typedef void (*aes_encrypt_t)(const uint8_t *in, uint8_t *out, const void *key);
@@ -96,6 +106,8 @@ extern aes_decrypt_t AES_decrypt;
96
106
 
97
107
  } // namespace openssl
98
108
 
109
+ #endif // !MMKV_OHOS
110
+
99
111
  #endif // __ARM_MAX_ARCH__ <= 7
100
112
 
101
113
  #endif // __linux__
@@ -45,16 +45,16 @@
45
45
 
46
46
  namespace openssl {
47
47
 
48
- #if (__ARM_MAX_ARCH__ > 7) && defined(__linux__)
48
+ #if (__ARM_MAX_ARCH__ > 7) && defined(__linux__) && !defined(MMKV_OHOS)
49
49
 
50
50
  aes_set_encrypt_t AES_set_encrypt_key = openssl::AES_C_set_encrypt_key;
51
51
  aes_set_decrypt_t AES_set_decrypt_key = openssl::AES_C_set_decrypt_key;
52
52
  aes_encrypt_t AES_encrypt = openssl::AES_C_encrypt;
53
53
  aes_encrypt_t AES_decrypt = openssl::AES_C_decrypt;
54
54
 
55
- #endif // (__ARM_MAX_ARCH__ > 7 && defined(__linux__)
55
+ #endif // (__ARM_MAX_ARCH__ > 7 && defined(__linux__) && !defined(MMKV_OHOS)
56
56
 
57
- #if (__ARM_MAX_ARCH__ <= 0) || (__ARM_MAX_ARCH__ > 7 && defined(__linux__))
57
+ #if (__ARM_MAX_ARCH__ <= 0) || (__ARM_MAX_ARCH__ > 7 && defined(__linux__) && !defined(MMKV_OHOS))
58
58
 
59
59
  /*-
60
60
  Te0[x] = S [x].[02, 01, 01, 03];
@@ -1037,7 +1037,7 @@ void AES_C_decrypt(const uint8_t *in, uint8_t *out, const void *k) {
1037
1037
  PUTU32(out + 12, s3);
1038
1038
  }
1039
1039
 
1040
- #endif // (__ARM_MAX_ARCH__ < 0) || (__ARM_MAX_ARCH__ > 7 && defined(__linux__))
1040
+ #endif // (__ARM_MAX_ARCH__ < 0) || (__ARM_MAX_ARCH__ > 7 && defined(__linux__) && !defined(MMKV_OHOS))
1041
1041
 
1042
1042
  } // namespace openssl
1043
1043