Refactor UserDefault, handle error & fix can't enlarge problem.

This commit is contained in:
halx99 2020-02-06 11:55:28 +08:00
parent a91953ac1a
commit 062c99844e
2 changed files with 5 additions and 4 deletions

View File

@ -239,7 +239,7 @@ void UserDefault::setStringForKey(const char* pKey, const std::string & value)
auto count = yasio::endian::ntohv(*(udflen_t*)_rwmmap->data());
*(udflen_t*)_rwmmap->data() = yasio::endian::htonv(count + 1);
// append entities
// append entity
::memcpy(_rwmmap->data() + sizeof(udflen_t) + _realSize, obs.data(), obs.length());
_realSize += obs.length();
}
@ -331,14 +331,15 @@ void UserDefault::flush()
obs.write_v(item.second);
}
std::error_code error;
if (obs.length() > _curMapSize) {
_rwmmap->unmap();
std::error_code error;
_curMapSize <<= 1; // X2
posix_fsetsize(_fd, _curMapSize);
_rwmmap->map(posix_fd2fh(_fd), 0, _curMapSize, error);
}
if (_rwmmap->is_mapped())
if (!error && _rwmmap->is_mapped())
{ // mapping status is good
::memcpy(_rwmmap->data(), obs.data(), obs.length());
_realSize = obs.length() - sizeof(udflen_t);

View File

@ -231,7 +231,7 @@ private:
int _fd = -1; // the file handle for data persistence
std::shared_ptr<mio::mmap_sink> _rwmmap;
int _curMapSize = 4096; // init mapsize is 4K
int _realSize = 0; // real data size
int _realSize = 0; // real data size without key/value entities count field
bool _initialized = false;
};