MMKVPredef.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. #ifndef MMKV_SRC_MMKVPREDEF_H
  21. #define MMKV_SRC_MMKVPREDEF_H
  22. // disable encryption & decryption to reduce some code
  23. // #define MMKV_DISABLE_CRYPT
  24. //#define MMKV_DISABLE_FLUTTER
  25. // using POSIX implementation
  26. //#define FORCE_POSIX
  27. #ifdef __cplusplus
  28. #include <string>
  29. #include <vector>
  30. #include <unordered_map>
  31. constexpr auto MMKV_VERSION = "v2.2.2";
  32. #ifdef DEBUG
  33. # define MMKV_DEBUG
  34. #endif
  35. #ifdef NDEBUG
  36. # undef MMKV_DEBUG
  37. #endif
  38. #if __cplusplus>=202002L
  39. # define MMKV_HAS_CPP20
  40. #endif
  41. #ifdef __ANDROID__
  42. # ifdef FORCE_POSIX
  43. # define MMKV_POSIX
  44. # else
  45. # define MMKV_ANDROID
  46. # endif
  47. #elif __OHOS__
  48. # ifdef FORCE_POSIX
  49. # define MMKV_POSIX
  50. # else
  51. # define MMKV_ANDROID
  52. # define MMKV_OHOS
  53. #endif
  54. #elif __APPLE__
  55. # ifdef FORCE_POSIX
  56. # define MMKV_POSIX
  57. # else
  58. # define MMKV_APPLE
  59. # ifdef __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__
  60. # define MMKV_IOS
  61. # elif __ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__
  62. # define MMKV_WATCH
  63. # else
  64. # define MMKV_MAC
  65. # endif
  66. # endif // FORCE_POSIX
  67. #elif __linux__ || __unix__
  68. # define MMKV_POSIX
  69. # if __linux__
  70. # define MMKV_LINUX
  71. # endif
  72. #elif _WIN32
  73. # define MMKV_WIN32
  74. #endif
  75. #ifdef MMKV_WIN32
  76. # if !defined(_WIN32_WINNT)
  77. # define _WIN32_WINNT _WIN32_WINNT_WINXP
  78. # endif
  79. # include <SDKDDKVer.h>
  80. // Exclude rarely-used stuff from Windows headers
  81. # define WIN32_LEAN_AND_MEAN
  82. // Windows Header Files
  83. # include <windows.h>
  84. constexpr auto MMKV_PATH_SLASH = L"\\";
  85. # define MMKV_PATH_FORMAT "%ls"
  86. using MMKVFileHandle_t = HANDLE;
  87. #define MMKVFileHandleInvalidValue INVALID_HANDLE_VALUE
  88. using MMKVPath_t = std::wstring;
  89. extern MMKVPath_t string2MMKVPath_t(const std::string &str);
  90. extern std::string MMKVPath_t2String(const MMKVPath_t &str);
  91. # ifndef MMKV_EMBED_ZLIB
  92. # define MMKV_EMBED_ZLIB 1
  93. # endif
  94. #else // MMKV_WIN32
  95. constexpr auto MMKV_PATH_SLASH = "/";
  96. # define MMKV_PATH_FORMAT "%s"
  97. using MMKVFileHandle_t = int;
  98. constexpr MMKVFileHandle_t MMKVFileHandleInvalidValue = -1;
  99. using MMKVPath_t = std::string;
  100. # define string2MMKVPath_t(str) (str)
  101. # define MMKVPath_t2String(str) (str)
  102. # ifndef MMKV_EMBED_ZLIB
  103. # define MMKV_EMBED_ZLIB 0
  104. # endif
  105. #endif // MMKV_WIN32
  106. #ifdef MMKV_ANDROID
  107. #define MMKV_EXPORT __attribute__((visibility("default")))
  108. #else
  109. #define MMKV_EXPORT
  110. #endif
  111. #ifdef MMKV_APPLE
  112. #ifdef __OBJC__
  113. # import <Foundation/Foundation.h>
  114. using MMKVLog_t = NSString *;
  115. #else
  116. using MMKVLog_t = void *;
  117. #endif
  118. # define MMKV_NAMESPACE_BEGIN namespace mmkv {
  119. # define MMKV_NAMESPACE_END }
  120. # define MMKV_NAMESPACE_PREFIX mmkv
  121. #else
  122. # define MMKV_NAMESPACE_BEGIN
  123. # define MMKV_NAMESPACE_END
  124. # define MMKV_NAMESPACE_PREFIX
  125. using MMKVLog_t = const std::string &;
  126. #endif // MMKV_APPLE
  127. MMKV_NAMESPACE_BEGIN
  128. enum MMKVLogLevel : int {
  129. MMKVLogDebug = 0, // not available for release/product build
  130. MMKVLogInfo = 1, // default level
  131. MMKVLogWarning,
  132. MMKVLogError,
  133. MMKVLogNone, // special level used to disable all log messages
  134. };
  135. enum MMKVRecoverStrategic : int {
  136. OnErrorDiscard = 0,
  137. OnErrorRecover,
  138. };
  139. enum MMKVErrorType : int {
  140. MMKVCRCCheckFail = 0,
  141. MMKVFileLength,
  142. };
  143. enum SyncFlag : bool { MMKV_SYNC = true, MMKV_ASYNC = false };
  144. MMKV_NAMESPACE_END
  145. namespace mmkv {
  146. typedef void (*LogHandler)(MMKVLogLevel level, const char *file, int line, const char *function, MMKVLog_t message);
  147. // by default MMKV will discard all datas on failure
  148. // return `OnErrorRecover` to recover any data from file
  149. typedef MMKVRecoverStrategic (*ErrorHandler)(const std::string &mmapID, MMKVErrorType errorType);
  150. // called when content is changed by other process
  151. // doesn't guarantee real-time notification
  152. typedef void (*ContentChangeHandler)(const std::string &mmapID);
  153. extern MMKV_EXPORT size_t DEFAULT_MMAP_SIZE;
  154. #define DEFAULT_MMAP_ID "mmkv.default"
  155. class MMBuffer;
  156. struct KeyValueHolder;
  157. #ifdef MMKV_DISABLE_CRYPT
  158. using KeyValueHolderCrypt = KeyValueHolder;
  159. #else
  160. struct KeyValueHolderCrypt;
  161. #endif
  162. #ifdef MMKV_APPLE
  163. #ifdef __OBJC__
  164. struct HybridStringCP {
  165. NSString *str;
  166. HybridStringCP(std::string_view cpp);
  167. ~HybridStringCP();
  168. };
  169. struct HybridString {
  170. NSString *str;
  171. HybridString(std::string_view cpp);
  172. ~HybridString();
  173. };
  174. struct KeyHasher {
  175. // enables heterogeneous lookup
  176. using is_transparent = void;
  177. size_t operator()(NSString *key) const { return key.hash; }
  178. };
  179. struct KeyEqualer {
  180. // enables heterogeneous lookup
  181. using is_transparent = void;
  182. bool operator()(NSString *left, NSString *right) const {
  183. if (left == right) {
  184. return true;
  185. }
  186. return ([left isEqualToString:right] == YES);
  187. }
  188. };
  189. using MMKVVector = std::vector<std::pair<NSString *, mmkv::MMBuffer>>;
  190. using MMKVMap = std::unordered_map<NSString *, mmkv::KeyValueHolder, KeyHasher, KeyEqualer>;
  191. using MMKVMapCrypt = std::unordered_map<NSString *, mmkv::KeyValueHolderCrypt, KeyHasher, KeyEqualer>;
  192. #else // type erase for pure C++ users
  193. using MMKVVector = std::vector<std::pair<void *, mmkv::MMBuffer>>;
  194. using MMKVMap = std::unordered_map<void *, mmkv::KeyValueHolder>;
  195. using MMKVMapCrypt = std::unordered_map<void *, mmkv::KeyValueHolderCrypt>;
  196. #endif // __OBJC__
  197. #else // !MMKV_APPLE
  198. struct KeyHasher {
  199. // enables heterogeneous lookup
  200. using is_transparent = void;
  201. std::size_t operator()(const std::string_view& str) const {
  202. return std::hash<std::string_view>{}(str);
  203. }
  204. std::size_t operator()(const std::string& str) const {
  205. return std::hash<std::string>{}(str);
  206. }
  207. };
  208. struct KeyEqualer {
  209. // enables heterogeneous lookup
  210. using is_transparent = void;
  211. bool operator()(const std::string_view& lhs, const std::string_view& rhs) const {
  212. return lhs == rhs;
  213. }
  214. bool operator()(const std::string& lhs, const std::string& rhs) const {
  215. return lhs == rhs;
  216. }
  217. };
  218. using MMKVVector = std::vector<std::pair<std::string, mmkv::MMBuffer>>;
  219. using MMKVMap = std::unordered_map<std::string, mmkv::KeyValueHolder, KeyHasher, KeyEqualer>;
  220. using MMKVMapCrypt = std::unordered_map<std::string, mmkv::KeyValueHolderCrypt, KeyHasher, KeyEqualer>;
  221. #endif // MMKV_APPLE
  222. template <typename T>
  223. void unused(const T &) {}
  224. constexpr size_t AES_KEY_LEN = 16;
  225. constexpr size_t AES_KEY_BITSET_LEN = 128;
  226. } // namespace mmkv
  227. #ifdef MMKV_DEBUG
  228. # include <cassert>
  229. # define MMKV_ASSERT(var) assert(var)
  230. #else
  231. # define MMKV_ASSERT(var) mmkv::unused(var)
  232. #endif
  233. #endif //cplus-plus
  234. #ifndef MMKV_WIN32
  235. # ifndef likely
  236. # define mmkv_unlikely(x) (__builtin_expect(bool(x), 0))
  237. # define mmkv_likely(x) (__builtin_expect(bool(x), 1))
  238. # endif
  239. #else
  240. # ifndef likely
  241. # define mmkv_unlikely(x) (x)
  242. # define mmkv_likely(x) (x)
  243. # endif
  244. #endif
  245. #if defined(__arm__)
  246. #if defined(__ARM_ARCH_7A__)
  247. #if defined(__ARM_NEON__)
  248. #if defined(__ARM_PCS_VFP)
  249. #define MMKV_ABI "armeabi-v7a/NEON (hard-float)"
  250. #else
  251. #define MMKV_ABI "armeabi-v7a/NEON"
  252. #endif
  253. #else
  254. #if defined(__ARM_PCS_VFP)
  255. #define MMKV_ABI "armeabi-v7a (hard-float)"
  256. #else
  257. #define MMKV_ABI "armeabi-v7a"
  258. #endif
  259. #endif
  260. #else
  261. #define MMKV_ABI "armeabi"
  262. #endif
  263. #elif defined(__i386__) || defined(_M_IX86)
  264. #define MMKV_ABI "x86"
  265. #elif defined(__x86_64__) || defined(_M_X64)
  266. #define MMKV_ABI "x86_64"
  267. #elif defined(__mips64)
  268. #define MMKV_ABI "mips64"
  269. #elif defined(__mips__)
  270. #define MMKV_ABI "mips"
  271. #elif defined(__aarch64__) || defined(_M_ARM64)
  272. #define MMKV_ABI "arm64-v8a"
  273. #else
  274. #define MMKV_ABI "unknown"
  275. #endif
  276. #endif //MMKV_SRC_MMKVPREDEF_H