| 222 | } |
| 223 | |
| 224 | void MemoryFile::reloadFromFile(size_t expectedCapacity) { |
| 225 | if (isFileValid()) { |
| 226 | MMKVWarning("calling reloadFromFile while the cache [%s] is still valid", m_diskFile.getUTF8Path().c_str()); |
| 227 | assert(0); |
| 228 | clearMemoryCache(); |
| 229 | } |
| 230 | if (openIfNeeded()) { |
| 231 | FileLock fileLock(m_diskFile.getFd()); |
| 232 | InterProcessLock lock(&fileLock, SharedLockType); |
| 233 | SCOPED_LOCK(&lock); |
| 234 | |
| 235 | mmkv::getFileSize(m_diskFile.getFd(), m_size); |
| 236 | size_t expectedSize = std::max<size_t>(DEFAULT_MMAP_SIZE, roundUp<size_t>(expectedCapacity, DEFAULT_MMAP_SIZE)); |
| 237 | // round up to (n * pagesize) |
| 238 | if (!m_readOnly && (m_size < expectedSize || (m_size % DEFAULT_MMAP_SIZE != 0))) { |
| 239 | InterProcessLock exclusiveLock(&fileLock, ExclusiveLockType); |
| 240 | SCOPED_LOCK(&exclusiveLock); |
| 241 | |
| 242 | size_t roundSize = ((m_size / DEFAULT_MMAP_SIZE) + 1) * DEFAULT_MMAP_SIZE;; |
| 243 | roundSize = std::max<size_t>(expectedSize, roundSize); |
| 244 | truncate(roundSize, &fileLock); |
| 245 | } else { |
| 246 | mmapOrCleanup(&fileLock); |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | void MemoryFile::doCleanMemoryCache(bool forceClean) { |
| 252 | if (m_ptr) { |
nothing calls this directly
no test coverage detected