Unmaps memory region, decrements reference count, and removes from cache if last reference.
(long address, long len, int memoryTag)
| 246 | * Unmaps memory region, decrements reference count, and removes from cache if last reference. |
| 247 | */ |
| 248 | public void unmap(long address, long len, int memoryTag) { |
| 249 | if (address <= 0 || len <= 0) { |
| 250 | throw CairoException.critical(0) |
| 251 | .put("unmap: invalid address or length [address=" + address + ", len=" + len + ']'); |
| 252 | } |
| 253 | |
| 254 | if (!Files.FS_CACHE_ENABLED) { |
| 255 | unmap0(address, len, memoryTag); |
| 256 | return; |
| 257 | } |
| 258 | |
| 259 | long unmapPtr, unmapLen; |
| 260 | int unmapTag; |
| 261 | |
| 262 | synchronized (this) { |
| 263 | int addrMapIndex = mmapAddrCache.keyIndex(address); |
| 264 | if (addrMapIndex > -1) { |
| 265 | // Not cached |
| 266 | unmap0(address, len, memoryTag); |
| 267 | return; |
| 268 | } |
| 269 | |
| 270 | var record = mmapAddrCache.valueAt(addrMapIndex); |
| 271 | record.count--; |
| 272 | |
| 273 | if (record.count != 0) { |
| 274 | assert record.count > -1; |
| 275 | return; |
| 276 | } |
| 277 | |
| 278 | // Remove the record from the cache, the last usage of the address is unmapped |
| 279 | mmapAddrCache.removeAt(addrMapIndex); |
| 280 | |
| 281 | // Check if the same map record is used for the FD, |
| 282 | // it can be already overwritten by a longer map over the same file |
| 283 | int fdIndex = mmapFileCache.keyIndex(record.fileCacheKey); |
| 284 | if (fdIndex < 0 && mmapFileCache.valueAt(fdIndex) == record) { |
| 285 | mmapFileCache.removeAt(fdIndex); |
| 286 | } |
| 287 | |
| 288 | // Unmap after exiting the lock. |
| 289 | unmapPtr = record.address; |
| 290 | unmapLen = record.length; |
| 291 | unmapTag = record.memoryTag; |
| 292 | record.address = 0; |
| 293 | if (recordPool.size() < MAX_RECORD_POOL_CAPACITY) { |
| 294 | recordPool.push(record); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | // offload the unmap to a single thread to not block everyone under synchronized section |
| 299 | unmap0(unmapPtr, unmapLen, unmapTag); |
| 300 | } |
| 301 | |
| 302 | private static long mmap0(int fd, long len, long offset, int flags, int memoryTag) { |
| 303 | long address = Files.mmap0(fd, len, offset, flags, 0); |