Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix warning #29

Merged
merged 5 commits into from
Sep 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 38 additions & 36 deletions iOS/MMKV/MMKV/MMKV.mm
Original file line number Diff line number Diff line change
Expand Up @@ -385,33 +385,35 @@ - (void)clearMemoryCache {
}

- (BOOL)protectFromBackgroundWritting:(size_t)size writeBlock:(void (^)(MiniCodedOutputData *output))block {
@try {
if (m_isInBackground) {
static const int offset = pbFixed32Size(0);
static const int pagesize = getpagesize();
size_t realOffset = offset + m_actualSize - size;
size_t pageOffset = (realOffset / pagesize) * pagesize;
size_t pointerOffset = realOffset - pageOffset;
size_t mmapSize = offset + m_actualSize - pageOffset;
char *ptr = m_ptr + pageOffset;
if (mlock(ptr, mmapSize) != 0) {
MMKVError(@"fail to mlock [%@], %s", m_mmapID, strerror(errno));
// just fail on this condition, otherwise app will crash anyway
//block(m_output);
return NO;
} else {
MiniCodedOutputData output(ptr + pointerOffset, size);
block(&output);
m_output->seek(size);
}
munlock(ptr, mmapSize);
} else {
block(m_output);
}
} @catch (NSException *exception) {
MMKVError(@"%@", exception);
return NO;
}
m_isInBackground = YES;
if (m_isInBackground) {
static const int offset = pbFixed32Size(0);
static const int pagesize = getpagesize();
size_t realOffset = offset + m_actualSize - size;
size_t pageOffset = (realOffset / pagesize) * pagesize;
size_t pointerOffset = realOffset - pageOffset;
size_t mmapSize = offset + m_actualSize - pageOffset;
char *ptr = m_ptr + pageOffset;
if (mlock(ptr, mmapSize) != 0) {
MMKVError(@"fail to mlock [%@], %s", m_mmapID, strerror(errno));
// just fail on this condition, otherwise app will crash anyway
//block(m_output);
return NO;
} else {
@try {
MiniCodedOutputData output(ptr + pointerOffset, size);
block(&output);
m_output->seek(size);
} @catch (NSException *exception) {
MMKVError(@"%@", exception);
return NO;
} @finally {
munlock(ptr, mmapSize);
}
}
} else {
block(m_output);
}

return YES;
}
Expand Down Expand Up @@ -652,7 +654,7 @@ - (BOOL)checkFileCRCValid {
int offset = pbFixed32Size(0);
m_crcDigest = (uint32_t) crc32(0, (const uint8_t *) m_ptr + offset, (uint32_t) m_actualSize);

// for backwark compatibility
// for backward compatibility
if (!isFileExist(m_crcPath)) {
MMKVInfo(@"crc32 file not found:%@", m_crcPath);
return YES;
Expand Down Expand Up @@ -803,15 +805,15 @@ - (BOOL)reKey:(NSData *)newKey {

- (BOOL)setObject:(id)obj forKey:(NSString *)key {
if (obj == nil || key.length <= 0) {
return FALSE;
return NO;
}
NSData *data = [MiniPBCoder encodeDataWithObject:obj];
return [self setData:data forKey:key];
}

- (BOOL)setBool:(BOOL)value forKey:(NSString *)key {
if (key.length <= 0) {
return FALSE;
return NO;
}
size_t size = pbBoolSize(value);
NSMutableData *data = [NSMutableData dataWithLength:size];
Expand All @@ -823,7 +825,7 @@ - (BOOL)setBool:(BOOL)value forKey:(NSString *)key {

- (BOOL)setInt32:(int32_t)value forKey:(NSString *)key {
if (key.length <= 0) {
return FALSE;
return NO;
}
size_t size = pbInt32Size(value);
NSMutableData *data = [NSMutableData dataWithLength:size];
Expand All @@ -835,7 +837,7 @@ - (BOOL)setInt32:(int32_t)value forKey:(NSString *)key {

- (BOOL)setUInt32:(uint32_t)value forKey:(NSString *)key {
if (key.length <= 0) {
return FALSE;
return NO;
}
size_t size = pbUInt32Size(value);
NSMutableData *data = [NSMutableData dataWithLength:size];
Expand All @@ -847,7 +849,7 @@ - (BOOL)setUInt32:(uint32_t)value forKey:(NSString *)key {

- (BOOL)setInt64:(int64_t)value forKey:(NSString *)key {
if (key.length <= 0) {
return FALSE;
return NO;
}
size_t size = pbInt64Size(value);
NSMutableData *data = [NSMutableData dataWithLength:size];
Expand All @@ -859,7 +861,7 @@ - (BOOL)setInt64:(int64_t)value forKey:(NSString *)key {

- (BOOL)setUInt64:(uint64_t)value forKey:(NSString *)key {
if (key.length <= 0) {
return FALSE;
return NO;
}
size_t size = pbUInt64Size(value);
NSMutableData *data = [NSMutableData dataWithLength:size];
Expand All @@ -871,7 +873,7 @@ - (BOOL)setUInt64:(uint64_t)value forKey:(NSString *)key {

- (BOOL)setFloat:(float)value forKey:(NSString *)key {
if (key.length <= 0) {
return FALSE;
return NO;
}
size_t size = pbFloatSize(value);
NSMutableData *data = [NSMutableData dataWithLength:size];
Expand All @@ -883,7 +885,7 @@ - (BOOL)setFloat:(float)value forKey:(NSString *)key {

- (BOOL)setDouble:(double)value forKey:(NSString *)key {
if (key.length <= 0) {
return FALSE;
return NO;
}
size_t size = pbDoubleSize(value);
NSMutableData *data = [NSMutableData dataWithLength:size];
Expand Down
2 changes: 1 addition & 1 deletion iOS/MMKVDemo/MMKVDemo/DemoSwiftUsage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import Foundation
print("Swift: double = \(mmkv.getDoubleForKey("double"))");

mmkv.setObject("Hello from Swift", forKey: "string");
print("Swift: string = \(mmkv.getObjectOf(NSString.self, forKey:"string"))");
print("Swift: string = \(String(describing: mmkv.getObjectOf(NSString.self, forKey:"string")))");

mmkv.setObject(NSDate(), forKey: "date");
let date = mmkv.getObjectOf(NSDate.self, forKey:"date") as! Date;
Expand Down