xref: /aosp_15_r20/external/ublksrv/qcow2/lrucache.hpp (revision 94c4a1e103eb1715230460aab379dff275992c20)
1 /*
2  * File:   lrucache.hpp
3  * Author: Alexander Ponomarev
4  *
5  * Created on June 20, 2013, 5:09 PM
6  */
7 
8 /*
9  * Redistribution and use in source and binary forms, with or without
10    modification, are permitted provided that the following conditions are met:
11 
12  * Redistributions of source code must retain the above copyright notice, this
13    list of conditions and the following disclaimer.
14 
15  * Redistributions in binary form must reproduce the above copyright notice,
16    this list of conditions and the following disclaimer in the documentation
17    and/or other materials provided with the distribution.
18 
19  * Neither the name of lamerman nor the names of its
20    contributors may be used to endorse or promote products derived from
21    this software without specific prior written permission.
22 
23  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
27  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34 
35 #ifndef _LRUCACHE_HPP_INCLUDED_
36 #define	_LRUCACHE_HPP_INCLUDED_
37 
38 #include <unordered_map>
39 #include <list>
40 #include <cstddef>
41 #include <stdexcept>
42 
43 namespace cache {
44 
45 template<typename key_t, typename value_t>
46 class lru_cache {
47 public:
48 	typedef typename std::pair<key_t, value_t> key_value_pair_t;
49 	typedef typename std::list<key_value_pair_t>::iterator list_iterator_t;
50 
lru_cache(size_t max_size)51 	lru_cache(size_t max_size) :
52 		_max_size(max_size) {
53 	}
54 
remove_last()55 	value_t remove_last() {
56 		auto last = _cache_items_list.end();
57 		last--;
58 		auto t = last->second;
59 		_cache_items_map.erase(last->first);
60 		_cache_items_list.pop_back();
61 
62 		return t;
63 	}
64 
put(const key_t & key,const value_t & value)65 	value_t put(const key_t& key, const value_t& value) {
66 		auto it = _cache_items_map.find(key);
67 		_cache_items_list.push_front(key_value_pair_t(key, value));
68 		if (it != _cache_items_map.end()) {
69 			_cache_items_list.erase(it->second);
70 			_cache_items_map.erase(it);
71 		}
72 		_cache_items_map[key] = _cache_items_list.begin();
73 
74 		if (_cache_items_map.size() > _max_size) {
75 			auto t = remove_last();
76 			return t;
77 		} else {
78 			//throw std::range_error("no cache dropped from put");
79 			return nullptr;
80 		}
81 	}
82 
83 	//just retrieve value without updating position in the lru list
__get(const key_t & key)84 	value_t __get(const key_t& key) {
85 		auto it = _cache_items_map.find(key);
86 		if (it == _cache_items_map.end())
87 			return nullptr;
88 		else
89 			return it->second->second;
90 	}
91 
get(const key_t & key)92 	value_t get(const key_t& key) {
93 		auto it = _cache_items_map.find(key);
94 		if (it == _cache_items_map.end()) {
95 			//throw std::range_error("There is no such key in cache");
96 			return nullptr;
97 		} else {
98 			_cache_items_list.splice(_cache_items_list.begin(), _cache_items_list, it->second);
99 			return it->second->second;
100 		}
101 	}
102 
exists(const key_t & key) const103 	bool exists(const key_t& key) const {
104 		return _cache_items_map.find(key) != _cache_items_map.end();
105 	}
106 
size() const107 	size_t size() const {
108 		return _cache_items_map.size();
109 	}
110 
get_lru_list_ro() const111 	const std::list<key_value_pair_t>& get_lru_list_ro() const {
112 		return _cache_items_list;
113 	}
114 
115 private:
116 	std::list<key_value_pair_t> _cache_items_list;
117 	std::unordered_map<key_t, list_iterator_t> _cache_items_map;
118 	size_t _max_size;
119 };
120 
121 } // namespace cache
122 
123 #endif	/* _LRUCACHE_HPP_INCLUDED_ */
124 
125