1 /* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include "window_manager_lru.h" 17 18 namespace OHOS::Rosen { LocalVisit(int32_t key)19bool LruCache::LocalVisit(int32_t key) 20 { 21 if (auto it = cacheMap_.find(key); it != cacheMap_.end()) { 22 cacheList_.splice(cacheList_.begin(), cacheList_, it->second); 23 return true; 24 } 25 return false; 26 } 27 Visit(int32_t key)28bool LruCache::Visit(int32_t key) 29 { 30 std::lock_guard lock(lruCacheMutex_); 31 return LocalVisit(key); 32 } 33 Put(int32_t key)34int32_t LruCache::Put(int32_t key) 35 { 36 int32_t lastRemovedKey = UNDEFINED_REMOVED_KEY; 37 std::lock_guard lock(lruCacheMutex_); 38 if (!LocalVisit(key)) { 39 if (cacheList_.size() >= capacity_) { 40 lastRemovedKey = cacheList_.back(); 41 cacheList_.pop_back(); 42 cacheMap_.erase(lastRemovedKey); 43 } 44 cacheList_.push_front(key); 45 cacheMap_[key] = cacheList_.begin(); 46 } 47 return lastRemovedKey; 48 } 49 Remove(int32_t key)50void LruCache::Remove(int32_t key) 51 { 52 std::lock_guard lock(lruCacheMutex_); 53 if (auto it = cacheMap_.find(key); it != cacheMap_.end()) { 54 cacheList_.erase(it->second); 55 cacheMap_.erase(it); 56 } 57 } 58 } // namespace OHOS::Rosen 59