-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathMMKV_OSX.cpp
423 lines (372 loc) · 13 KB
/
MMKV_OSX.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*
* Tencent is pleased to support the open source community by making
* MMKV available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company.
* All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of
* the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "MMKVPredef.h"
#ifdef MMKV_APPLE
# include "CodedInputData.h"
# include "CodedOutputData.h"
# include "InterProcessLock.h"
# include "MMKV.h"
# include "MemoryFile.h"
# include "MiniPBCoder.h"
# include "PBUtility.h"
# include "ScopedLock.hpp"
# include "ThreadLock.h"
# include "aes/AESCrypt.h"
# include <sys/utsname.h>
# include <sys/sysctl.h>
# include "MMKV_OSX.h"
# include "MMKVLog.h"
# ifdef MMKV_IOS
# include <sys/mman.h>
# endif
# ifdef __aarch64__
# include "crc32/Checksum.h"
# endif
# if __has_feature(objc_arc)
# error This file must be compiled with MRC. Use -fno-objc-arc flag.
# endif
using namespace std;
using namespace mmkv;
extern ThreadLock *g_instanceLock;
extern MMKVPath_t g_rootDir;
MMKV_NAMESPACE_BEGIN
# ifdef MMKV_IOS
MLockPtr::MLockPtr(void *ptr, size_t size) : m_lockDownSize(0), m_lockedPtr(nullptr) {
if (!ptr || size == 0) {
return;
}
// calc ptr to mlock()
auto writePtr = (size_t) ptr;
auto lockPtr = (writePtr / DEFAULT_MMAP_SIZE) * DEFAULT_MMAP_SIZE;
auto lockDownSize = writePtr - lockPtr + size;
if (mlock((void *) lockPtr, lockDownSize) == 0) {
m_lockedPtr = (uint8_t *) lockPtr;
m_lockDownSize = lockDownSize;
} else {
MMKVError("fail to mlock [%p], %s", m_lockedPtr, strerror(errno));
// just fail on this condition, otherwise app will crash anyway
}
}
MLockPtr::MLockPtr(MLockPtr &&other) : m_lockDownSize(other.m_lockDownSize), m_lockedPtr(other.m_lockedPtr) {
other.m_lockedPtr = nullptr;
}
MLockPtr::~MLockPtr() {
if (m_lockedPtr) {
munlock(m_lockedPtr, m_lockDownSize);
}
}
bool MLockPtr::isMLockPtrEnabled = true;
# endif
# ifdef MMKV_IOS
static bool g_isInBackground = false;
void MMKV::setIsInBackground(bool isInBackground) {
if (!g_instanceLock) {
return;
}
SCOPED_LOCK(g_instanceLock);
g_isInBackground = isInBackground;
MMKVInfo("g_isInBackground:%d", g_isInBackground);
}
bool MMKV::isInBackground() {
if (!g_instanceLock) {
return true;
}
SCOPED_LOCK(g_instanceLock);
return g_isInBackground;
}
pair<bool, MLockPtr> guardForBackgroundWriting(void *ptr, size_t size) {
if (g_isInBackground && MLockPtr::isMLockPtrEnabled) {
MLockPtr mlockPtr(ptr, size);
return make_pair(mlockPtr.isLocked(), std::move(mlockPtr));
} else {
return make_pair(true, MLockPtr(nullptr, 0));
}
}
# endif // MMKV_IOS
bool MMKV::set(NSObject<NSCoding> *__unsafe_unretained obj, MMKVKey_t key) {
return set(obj, key, m_expiredInSeconds);
}
bool MMKV::set(NSObject<NSCoding> *__unsafe_unretained obj, MMKVKey_t key, uint32_t expireDuration) {
if (isKeyEmpty(key)) {
return false;
}
if (!obj) {
removeValueForKey(key);
return true;
}
NSData *tmpData = nil;
if ([obj isKindOfClass:NSString.class]) {
auto str = (NSString *) obj;
tmpData = [str dataUsingEncoding:NSUTF8StringEncoding];
} else if ([obj isKindOfClass:NSData.class]) {
tmpData = (NSData *) obj;
}
if (tmpData) {
// delay write the size needed for encoding tmpData
// avoid memory copying
if (mmkv_likely(!m_enableKeyExpire)) {
return setDataForKey(MMBuffer(tmpData, MMBufferNoCopy), key, true);
} else {
MMBuffer data(tmpData, MMBufferNoCopy);
if (data.length() > 0) {
auto tmp = MMBuffer(pbMMBufferSize(data) + Fixed32Size);
CodedOutputData output(tmp.getPtr(), tmp.length());
output.writeData(data);
auto time = (expireDuration != 0) ? getCurrentTimeInSecond() + expireDuration : 0;
output.writeRawLittleEndian32(UInt32ToInt32(time));
data = std::move(tmp);
}
return setDataForKey(std::move(data), key);
}
} else if ([obj isKindOfClass:NSDate.class]) {
NSDate *oDate = (NSDate *) obj;
double time = oDate.timeIntervalSince1970;
return set(time, key, expireDuration);
} else {
/*if ([object conformsToProtocol:@protocol(NSCoding)])*/ {
@try {
NSError *error = nil;
auto archived = [NSKeyedArchiver archivedDataWithRootObject:obj requiringSecureCoding:NO error:&error];
if (error) {
MMKVError("fail to archive: %@", error);
return false;
}
if (archived.length > 0) {
if (mmkv_likely(!m_enableKeyExpire)) {
return setDataForKey(MMBuffer(archived, MMBufferNoCopy), key);
} else {
MMBuffer data(archived, MMBufferNoCopy);
if (data.length() > 0) {
auto tmp = MMBuffer(data.length() + Fixed32Size);
CodedOutputData output(tmp.getPtr(), tmp.length());
output.writeRawData(data); // NSKeyedArchiver has its own size management
auto time = (expireDuration != 0) ? getCurrentTimeInSecond() + expireDuration : 0;
output.writeRawLittleEndian32(UInt32ToInt32(time));
data = std::move(tmp);
}
return setDataForKey(std::move(data), key);
}
}
} @catch (NSException *exception) {
MMKVError("exception: %@", exception.reason);
}
}
}
return false;
}
static id unSecureUnArchiveObjectWithData(NSData *data) {
@try {
NSError *error = nil;
auto unarchiver = [[NSKeyedUnarchiver alloc] initForReadingFromData:data error:&error];
if (error) {
MMKVError("fail to init unarchiver %@", error);
return nil;
}
unarchiver.requiresSecureCoding = NO;
id result = [unarchiver decodeObjectForKey:NSKeyedArchiveRootObjectKey];
[unarchiver release];
return result;
} @catch (NSException *exception) {
MMKVError("exception: %@", exception.reason);
}
return nil;
}
NSObject *MMKV::getObject(MMKVKey_t key, Class cls) {
if (isKeyEmpty(key) || !cls) {
return nil;
}
SCOPED_LOCK(m_lock);
SCOPED_LOCK(m_sharedProcessLock);
auto data = getDataForKey(key);
if (data.length() > 0) {
if (MiniPBCoder::isCompatibleClass(cls)) {
try {
auto result = MiniPBCoder::decodeObject(data, cls);
return result;
} catch (std::exception &exception) {
MMKVError("%s", exception.what());
} catch (...) {
MMKVError("decode fail");
}
} else {
if ([cls conformsToProtocol:@protocol(NSCoding)]) {
auto tmp = [NSData dataWithBytesNoCopy:data.getPtr() length:data.length() freeWhenDone:NO];
return unSecureUnArchiveObjectWithData(tmp);
}
}
}
return nil;
}
# ifndef MMKV_DISABLE_CRYPT
pair<bool, KeyValueHolder>
MMKV::appendDataWithKey(const MMBuffer &data, MMKVKey_t key, const KeyValueHolderCrypt &kvHolder, bool isDataHolder) {
if (kvHolder.type != KeyValueHolderType_Offset) {
return appendDataWithKey(data, key, isDataHolder);
}
SCOPED_LOCK(m_exclusiveProcessLock);
uint32_t keyLength = kvHolder.keySize;
// size needed to encode the key
size_t rawKeySize = keyLength + pbRawVarint32Size(keyLength);
auto basePtr = (uint8_t *) m_file->getMemory() + Fixed32Size;
MMBuffer keyData(rawKeySize);
AESCrypt decrypter = m_crypter->cloneWithStatus(kvHolder.cryptStatus);
decrypter.decrypt(basePtr + kvHolder.offset, keyData.getPtr(), rawKeySize);
return doAppendDataWithKey(data, keyData, isDataHolder, keyLength);
}
pair<bool, KeyValueHolder>
MMKV::overrideDataWithKey(const MMBuffer &data, MMKVKey_t key, const KeyValueHolderCrypt &kvHolder, bool isDataHolder) {
if (kvHolder.type != KeyValueHolderType_Offset) {
return overrideDataWithKey(data, key, isDataHolder);
}
SCOPED_LOCK(m_exclusiveProcessLock);
uint32_t keyLength = kvHolder.keySize;
// size needed to encode the key
size_t rawKeySize = keyLength + pbRawVarint32Size(keyLength);
auto basePtr = (uint8_t *) m_file->getMemory() + Fixed32Size;
MMBuffer keyData(rawKeySize);
AESCrypt decrypter = m_crypter->cloneWithStatus(kvHolder.cryptStatus);
decrypter.decrypt(basePtr + kvHolder.offset, keyData.getPtr(), rawKeySize);
return doOverrideDataWithKey(data, keyData, isDataHolder, keyLength);
}
# endif
NSArray *MMKV::allKeys(bool filterExpire) {
SCOPED_LOCK(m_lock);
checkLoadData();
if (mmkv_unlikely(filterExpire && m_enableKeyExpire)) {
SCOPED_LOCK(m_exclusiveProcessLock);
fullWriteback(nullptr, true);
}
NSMutableArray *keys = [NSMutableArray array];
if (m_crypter) {
for (const auto &pair : *m_dicCrypt) {
[keys addObject:pair.first];
}
} else {
for (const auto &pair : *m_dic) {
[keys addObject:pair.first];
}
}
return keys;
}
bool MMKV::removeValuesForKeys(NSArray *arrKeys) {
if (isReadOnly()) {
MMKVWarning("[%s] file readonly", m_mmapID.c_str());
return false;
}
if (arrKeys.count == 0) {
return true;
}
if (arrKeys.count == 1) {
return removeValueForKey(arrKeys[0]);
}
SCOPED_LOCK(m_lock);
SCOPED_LOCK(m_exclusiveProcessLock);
checkLoadData();
size_t deleteCount = 0;
if (m_crypter) {
for (NSString *key in arrKeys) {
auto itr = m_dicCrypt->find(key);
if (itr != m_dicCrypt->end()) {
auto oldKey = itr->first;
m_dicCrypt->erase(itr);
[oldKey release];
deleteCount++;
}
}
} else {
for (NSString *key in arrKeys) {
auto itr = m_dic->find(key);
if (itr != m_dic->end()) {
auto oldKey = itr->first;
m_dic->erase(itr);
[oldKey release];
deleteCount++;
}
}
}
if (deleteCount > 0) {
m_hasFullWriteback = false;
return fullWriteback();
}
return true;
}
void MMKV::enumerateKeys(EnumerateBlock block) {
if (block == nil) {
return;
}
SCOPED_LOCK(m_lock);
checkLoadData();
MMKVInfo("enumerate [%s] begin", m_mmapID.c_str());
if (m_crypter) {
for (const auto &pair : *m_dicCrypt) {
BOOL stop = NO;
block(pair.first, &stop);
if (stop) {
break;
}
}
} else {
for (const auto &pair : *m_dic) {
BOOL stop = NO;
block(pair.first, &stop);
if (stop) {
break;
}
}
}
MMKVInfo("enumerate [%s] finish", m_mmapID.c_str());
}
void GetAppleMachineInfo(int &device, int &version) {
device = UnKnown;
version = 0;
# if 0
struct utsname systemInfo = {};
uname(&systemInfo);
std::string machine(systemInfo.machine);
# else
size_t size;
sysctlbyname("hw.machine", nullptr, &size, nullptr, 0);
char *answer = (char *) malloc(size);
sysctlbyname("hw.machine", answer, &size, nullptr, 0);
std::string machine(answer);
free(answer);
# endif
if (machine.find("PowerMac") != std::string::npos || machine.find("Power Macintosh") != std::string::npos) {
device = PowerMac;
} else if (machine.find("Mac") != std::string::npos || machine.find("Macintosh") != std::string::npos) {
device = Mac;
} else if (machine.find("iPhone") != std::string::npos) {
device = iPhone;
} else if (machine.find("iPod") != std::string::npos) {
device = iPod;
} else if (machine.find("iPad") != std::string::npos) {
device = iPad;
} else if (machine.find("AppleTV") != std::string::npos) {
device = AppleTV;
} else if (machine.find("AppleWatch") != std::string::npos) {
device = AppleWatch;
}
auto pos = machine.find_first_of("0123456789");
if (pos != std::string::npos) {
version = std::atoi(machine.substr(pos).c_str());
}
}
MMKV_NAMESPACE_END
#endif // MMKV_APPLE