Skip to content

Commit

Permalink
Remove page size truncate of data and crc file
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongwuzw committed Sep 26, 2018
1 parent c556078 commit 7444dd1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 24 deletions.
49 changes: 25 additions & 24 deletions iOS/MMKV/MMKV/MMKV.mm
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
static NSRecursiveLock *g_instanceLock;

#define DEFAULT_MMAP_ID @"mmkv.default"
#define CRC_FILE_SIZE 4

@implementation MMKV {
NSRecursiveLock *m_lock;
Expand Down Expand Up @@ -150,7 +151,7 @@ - (void)dealloc {
munmap(m_ptr, m_size);
m_ptr = nullptr;
}
if (m_fd > 0) {
if (m_fd >= 0) {
close(m_fd);
m_fd = -1;
}
Expand All @@ -167,7 +168,7 @@ - (void)dealloc {
munmap(m_crcPtr, pbFixed32Size(0));
m_crcPtr = nullptr;
}
if (m_crcFd > 0) {
if (m_crcFd >= 0) {
close(m_crcFd);
m_crcFd = -1;
}
Expand Down Expand Up @@ -212,22 +213,22 @@ - (void)didBecomeActive {

- (void)loadFromFile {
m_fd = open(m_path.UTF8String, O_RDWR, S_IRWXU);
if (m_fd <= 0) {
if (m_fd < 0) {
MMKVError(@"fail to open:%@, %s", m_path, strerror(errno));
} else {
m_size = 0;
struct stat st = {};
if (fstat(m_fd, &st) != -1) {
m_size = (size_t) st.st_size;
}
// round up to (n * pagesize)
if (m_size < DEFAULT_MMAP_SIZE || (m_size % DEFAULT_MMAP_SIZE != 0)) {
m_size = ((m_size / DEFAULT_MMAP_SIZE) + 1) * DEFAULT_MMAP_SIZE;
if (ftruncate(m_fd, m_size) != 0) {
MMKVError(@"fail to truncate [%@] to size %zu, %s", m_mmapID, m_size, strerror(errno));
m_size = (size_t) st.st_size;
}
}
if (m_size == 0) {
m_size = DEFAULT_FILE_SIZE;
// If ftruncate faild and m_size = 0, we should just return because len parameter of mmap must not be 0.
if (ftruncate(m_fd, DEFAULT_FILE_SIZE) != 0) {
MMKVError(@"fail to truncate [%@] to size %zu, %s", m_mmapID, m_size, strerror(errno));
return;
}
}
m_ptr = (char *) mmap(nullptr, m_size, PROT_READ | PROT_WRITE, MAP_SHARED, m_fd, 0);
if (m_ptr == MAP_FAILED) {
MMKVError(@"fail to mmap [%@], %s", m_mmapID, strerror(errno));
Expand Down Expand Up @@ -313,7 +314,7 @@ - (void)clearAll {

if (m_ptr != nullptr && m_ptr != MAP_FAILED) {
// for truncate
size_t size = std::min<size_t>(DEFAULT_MMAP_SIZE, m_size);
size_t size = std::min<size_t>(DEFAULT_FILE_SIZE, m_size);
memset(m_ptr, 0, size);
if (msync(m_ptr, size, MS_SYNC) != 0) {
MMKVError(@"fail to msync [%@]:%s", m_mmapID, strerror(errno));
Expand All @@ -324,18 +325,18 @@ - (void)clearAll {
}
m_ptr = nullptr;

if (m_fd > 0) {
if (m_size != DEFAULT_MMAP_SIZE) {
MMKVInfo(@"truncating [%@] from %zu to %d", m_mmapID, m_size, DEFAULT_MMAP_SIZE);
if (ftruncate(m_fd, DEFAULT_MMAP_SIZE) != 0) {
MMKVError(@"fail to truncate [%@] to size %d, %s", m_mmapID, DEFAULT_MMAP_SIZE, strerror(errno));
if (m_fd >= 0) {
if (m_size != DEFAULT_FILE_SIZE) {
MMKVInfo(@"truncating [%@] from %zu to %d", m_mmapID, m_size, DEFAULT_FILE_SIZE);
if (ftruncate(m_fd, DEFAULT_FILE_SIZE) != 0) {
MMKVError(@"fail to truncate [%@] to size %d, %s", m_mmapID, DEFAULT_FILE_SIZE, strerror(errno));
}
}
if (close(m_fd) != 0) {
MMKVError(@"fail to close [%@], %s", m_mmapID, strerror(errno));
}
}
m_fd = 0;
m_fd = -1;
m_size = 0;
m_actualSize = 0;
m_crcDigest = 0;
Expand Down Expand Up @@ -370,12 +371,12 @@ - (void)clearMemoryCache {
}
m_ptr = nullptr;

if (m_fd > 0) {
if (m_fd >= 0) {
if (close(m_fd) != 0) {
MMKVError(@"fail to close [%@], %s", m_mmapID, strerror(errno));
}
}
m_fd = 0;
m_fd = -1;
m_size = 0;
m_actualSize = 0;

Expand Down Expand Up @@ -641,7 +642,7 @@ - (BOOL)fullWriteback {
}

- (BOOL)isFileValid {
if (m_fd > 0 && m_size > 0 && m_output != nullptr && m_ptr != nullptr && m_ptr != MAP_FAILED) {
if (m_fd >= 0 && m_size > 0 && m_output != nullptr && m_ptr != nullptr && m_ptr != MAP_FAILED) {
return YES;
}
// MMKVWarning(@"[%@] file not valid", m_mmapID);
Expand Down Expand Up @@ -722,7 +723,7 @@ - (void)prepareCRCFile {
createFile(m_crcPath);
}
m_crcFd = open(m_crcPath.UTF8String, O_RDWR, S_IRWXU);
if (m_crcFd <= 0) {
if (m_crcFd < 0) {
MMKVError(@"fail to open:%@, %s", m_crcPath, strerror(errno));
removeFile(m_crcPath);
} else {
Expand All @@ -731,8 +732,8 @@ - (void)prepareCRCFile {
if (fstat(m_crcFd, &st) != -1) {
size = (size_t) st.st_size;
}
int fileLegth = DEFAULT_MMAP_SIZE;
if (size < fileLegth) {
int fileLegth = CRC_FILE_SIZE;
if (size != fileLegth) {
size = fileLegth;
if (ftruncate(m_crcFd, size) != 0) {
MMKVError(@"fail to truncate [%@] to size %zu, %s", m_crcPath, size, strerror(errno));
Expand Down
1 change: 1 addition & 0 deletions iOS/MMKV/MMKV/MemoryFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#import <Foundation/Foundation.h>

extern const int DEFAULT_MMAP_SIZE;
extern const int DEFAULT_FILE_SIZE;

class MemoryFile {
NSString *m_name;
Expand Down
2 changes: 2 additions & 0 deletions iOS/MMKV/MMKV/MemoryFile.mm
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
using namespace std;

const int DEFAULT_MMAP_SIZE = getpagesize();
// Truncate file if size equal to 0
const int DEFAULT_FILE_SIZE = DEFAULT_MMAP_SIZE;
// 1MB per segment
constexpr uint32_t SegmentSize = 1024 * 1024;
// count of segments in memory
Expand Down

0 comments on commit 7444dd1

Please sign in to comment.