Skip to content

Commit

Permalink
Make CRC-32 file length to 4 Byte
Browse files Browse the repository at this point in the history
Make CRC-32 file length to 4B

adjust spacing

adjust spacing
  • Loading branch information
zhongwuzw committed Sep 27, 2018
1 parent 7444dd1 commit 3857a82
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 16 deletions.
27 changes: 14 additions & 13 deletions iOS/MMKV/MMKV/MMKV.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -314,7 +315,7 @@ - (void)clearAll {

if (m_ptr != nullptr && m_ptr != MAP_FAILED) {
// for truncate
size_t size = std::min<size_t>(DEFAULT_FILE_SIZE, m_size);
size_t size = std::min<size_t>(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));
Expand All @@ -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) {
Expand Down
1 change: 0 additions & 1 deletion iOS/MMKV/MMKV/MemoryFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#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: 0 additions & 2 deletions iOS/MMKV/MMKV/MemoryFile.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3857a82

Please sign in to comment.