From 3857a82744508415786c59dc9b8f88bc4ffbda04 Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Thu, 27 Sep 2018 16:04:17 +0800 Subject: [PATCH] Make CRC-32 file length to 4 Byte Make CRC-32 file length to 4B adjust spacing adjust spacing --- iOS/MMKV/MMKV/MMKV.mm | 27 ++++++++++++++------------- iOS/MMKV/MMKV/MemoryFile.h | 1 - iOS/MMKV/MMKV/MemoryFile.mm | 2 -- 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/iOS/MMKV/MMKV/MMKV.mm b/iOS/MMKV/MMKV/MMKV.mm index fb6d4c8c..ddf90e54 100644 --- a/iOS/MMKV/MMKV/MMKV.mm +++ b/iOS/MMKV/MMKV/MMKV.mm @@ -221,14 +221,15 @@ - (void)loadFromFile { if (fstat(m_fd, &st) != -1) { 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; - } - } + // 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; + 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)); @@ -314,7 +315,7 @@ - (void)clearAll { if (m_ptr != nullptr && m_ptr != MAP_FAILED) { // for truncate - size_t size = std::min(DEFAULT_FILE_SIZE, m_size); + size_t size = std::min(DEFAULT_MMAP_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)); @@ -326,10 +327,10 @@ - (void)clearAll { m_ptr = nullptr; 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 (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 (close(m_fd) != 0) { diff --git a/iOS/MMKV/MMKV/MemoryFile.h b/iOS/MMKV/MMKV/MemoryFile.h index 11d0c361..e68896d1 100644 --- a/iOS/MMKV/MMKV/MemoryFile.h +++ b/iOS/MMKV/MMKV/MemoryFile.h @@ -25,7 +25,6 @@ #import extern const int DEFAULT_MMAP_SIZE; -extern const int DEFAULT_FILE_SIZE; class MemoryFile { NSString *m_name; diff --git a/iOS/MMKV/MMKV/MemoryFile.mm b/iOS/MMKV/MMKV/MemoryFile.mm index 0afa01dd..f35ab2c0 100644 --- a/iOS/MMKV/MMKV/MemoryFile.mm +++ b/iOS/MMKV/MMKV/MemoryFile.mm @@ -31,8 +31,6 @@ 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