@@ -1641,7 +1641,13 @@ typename CacheAllocator<CacheTrait>::WriteHandle
16411641CacheAllocator<CacheTrait>::tryEvictToNextMemoryTier(
16421642 TierId tid, PoolId pid, Item& item) {
16431643 if (item.isChainedItem ()) return {}; // TODO: We do not support ChainedItem yet
1644- if (item.isExpired ()) return acquire (&item);
1644+ if (item.isExpired ()) {
1645+ auto handle = removeIf (item, [](const Item& it) {
1646+ return it.getRefCount () == 0 ;
1647+ });
1648+
1649+ if (handle) { return handle; }
1650+ }
16451651
16461652 TierId nextTier = tid; // TODO - calculate this based on some admission policy
16471653 while (++nextTier < getNumTiers ()) { // try to evict down to the next memory tiers
@@ -3067,16 +3073,12 @@ CacheAllocator<CacheTrait>::evictNormalItem(Item& item,
30673073 // We remove the item from both access and mm containers. It doesn't matter
30683074 // if someone else calls remove on the item at this moment, the item cannot
30693075 // be freed as long as we have the moving bit set.
3070- auto handle = accessContainer_->removeIf (item, std::move (predicate));
3071-
3076+ auto handle = removeIf (item, std::move (predicate));
30723077 if (!handle) {
30733078 return handle;
30743079 }
30753080
3076- XDCHECK_EQ (reinterpret_cast <uintptr_t >(handle.get ()),
3077- reinterpret_cast <uintptr_t >(&item));
30783081 XDCHECK_EQ (1u , handle->getRefCount ());
3079- removeFromMMContainer (item);
30803082
30813083 // now that we are the only handle and we actually removed something from
30823084 // the RAM cache, we enqueue it to nvmcache.
@@ -3188,6 +3190,21 @@ CacheAllocator<CacheTrait>::evictChainedItemForSlabRelease(ChainedItem& child) {
31883190 return parentHandle;
31893191}
31903192
3193+ template <typename CacheTrait>
3194+ template <typename Fn>
3195+ typename CacheAllocator<CacheTrait>::WriteHandle
3196+ CacheAllocator<CacheTrait>::removeIf(Item& item, Fn&& predicate) {
3197+ auto handle = accessContainer_->removeIf (item, std::forward<Fn>(predicate));
3198+
3199+ if (handle) {
3200+ XDCHECK_EQ (reinterpret_cast <uintptr_t >(handle.get ()),
3201+ reinterpret_cast <uintptr_t >(&item));
3202+ removeFromMMContainer (item);
3203+ }
3204+
3205+ return handle;
3206+ }
3207+
31913208template <typename CacheTrait>
31923209bool CacheAllocator<CacheTrait>::removeIfExpired(const ReadHandle& handle) {
31933210 if (!handle) {
@@ -3196,14 +3213,7 @@ bool CacheAllocator<CacheTrait>::removeIfExpired(const ReadHandle& handle) {
31963213
31973214 // We remove the item from both access and mm containers.
31983215 // We want to make sure the caller is the only one holding the handle.
3199- auto removedHandle =
3200- accessContainer_->removeIf (*(handle.getInternal ()), itemExpiryPredicate);
3201- if (removedHandle) {
3202- removeFromMMContainer (*(handle.getInternal ()));
3203- return true ;
3204- }
3205-
3206- return false ;
3216+ return (bool )removeIf (*(handle.getInternal ()), itemExpiryPredicate);
32073217}
32083218
32093219template <typename CacheTrait>
0 commit comments