1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include "src/traced/probes/filesystem/lru_inode_cache.h" 18 19 namespace perfetto { 20 21 InodeMapValue* LRUInodeCache::Get(const InodeKey& k) { 22 const auto& map_it = map_.find(k); 23 if (map_it == map_.end()) { 24 return nullptr; 25 } 26 auto list_entry = map_it->second; 27 // Bump this item to the front of the cache. 28 // We can borrow both elements of the pair stored in the list because 29 // insert does not need them. 30 Insert(map_it, std::move(list_entry->first), std::move(list_entry->second)); 31 return &list_.begin()->second; 32 } 33 34 void LRUInodeCache::Insert(InodeKey k, InodeMapValue v) { 35 auto it = map_.find(k); 36 return Insert(it, std::move(k), std::move(v)); 37 } 38 39 void LRUInodeCache::Insert(typename MapType::iterator map_it, 40 InodeKey k, 41 InodeMapValue v) { 42 list_.emplace_front(k, std::move(v)); 43 if (map_it != map_.end()) { 44 ListIteratorType& list_it = map_it->second; 45 list_.erase(list_it); 46 list_it = list_.begin(); 47 } else { 48 map_.emplace(std::move(k), list_.begin()); 49 } 50 51 if (map_.size() > capacity_) { 52 auto list_last_it = list_.end(); 53 list_last_it--; 54 map_.erase(list_last_it->first); 55 list_.erase(list_last_it); 56 } 57 } 58 } // namespace perfetto 59