| 258 | } |
| 259 | |
| 260 | void MemoryFile::reloadFromFile(size_t expectedCapacity) { |
| 261 | # ifdef MMKV_ANDROID |
| 262 | if (m_fileType == MMFILE_TYPE_ASHMEM) { |
| 263 | return; |
| 264 | } |
| 265 | # endif |
| 266 | if (isFileValid()) { |
| 267 | MMKVWarning("calling reloadFromFile while the cache [%s] is still valid", m_diskFile.m_path.c_str()); |
| 268 | MMKV_ASSERT(0); |
| 269 | doCleanMemoryCache(false); |
| 270 | } |
| 271 | |
| 272 | if (openIfNeeded()) { |
| 273 | FileLock fileLock(m_diskFile.m_fd); |
| 274 | InterProcessLock lock(&fileLock, SharedLockType); |
| 275 | SCOPED_LOCK(&lock); |
| 276 | |
| 277 | mmkv::getFileSize(m_diskFile.m_fd, m_size); |
| 278 | size_t expectedSize = std::max<size_t>(DEFAULT_MMAP_SIZE, roundUp<size_t>(expectedCapacity, DEFAULT_MMAP_SIZE)); |
| 279 | // round up to (n * pagesize) |
| 280 | if (!m_readOnly && (m_size < expectedSize || (m_size % DEFAULT_MMAP_SIZE != 0))) { |
| 281 | InterProcessLock exclusiveLock(&fileLock, ExclusiveLockType); |
| 282 | SCOPED_LOCK(&exclusiveLock); |
| 283 | |
| 284 | size_t roundSize = ((m_size / DEFAULT_MMAP_SIZE) + 1) * DEFAULT_MMAP_SIZE;; |
| 285 | roundSize = std::max<size_t>(expectedSize, roundSize); |
| 286 | truncate(roundSize, &fileLock); |
| 287 | } else { |
| 288 | mmapOrCleanup(&fileLock); |
| 289 | } |
| 290 | # ifdef MMKV_IOS |
| 291 | if (!m_readOnly) { |
| 292 | tryResetFileProtection(m_diskFile.m_path); |
| 293 | } |
| 294 | # endif |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | void MemoryFile::doCleanMemoryCache(bool forceClean) { |
| 299 | # ifdef MMKV_ANDROID |
no test coverage detected