7#ifndef _ADDRMAPCACHES_H
8#define _ADDRMAPCACHES_H
14template <
typename Key,
typename Value>
17 const size_t Capacity = 0x10000;
18 using ListIt =
typename std::list<std::pair<Key, Value>>
::iterator;
19 std::list<std::pair<Key, Value>> m_list;
20 std::unordered_map<Key, ListIt> m_map;
25#if THREAD_SAFE == true
26 mutable std::shared_mutex m_mutex;
38#if THREAD_SAFE == true
39 std::unique_lock<std::shared_mutex>
l(m_mutex);
41 auto it = m_map.find(
key);
42 if (
it == m_map.end()) {
48 m_list.splice(m_list.begin(), m_list,
it->second);
49 value =
it->second->second;
61#if THREAD_SAFE == true
62 std::unique_lock<std::shared_mutex>
l(m_mutex);
65 if (m_list.size() >= Capacity) {
66 auto last = m_list.back();
67 m_map.erase(
last.first);
72 m_list.emplace_front(
key, value);
73 m_map[
key] = m_list.begin();
84 uint64_t get_hits()
const override {
return m_hits; }
85 uint64_t get_misses()
const override {
return m_misses; }
86 void reset_stats()
override
105template <
typename Key,
typename Value>
108 const size_t Capacity = 8;
109 using ListIt =
typename std::list<std::pair<Key, Value>>
::iterator;
115 std::vector<Entry> m_cache;
120#if THREAD_SAFE == true
121 mutable std::mutex m_mutex;
125 bool get(
const Key&
key,
Value& value)
override
127#if THREAD_SAFE == true
128 std::lock_guard<std::mutex>
l(m_mutex);
130 for (
const auto& entry : m_cache) {
131 if (
key >= entry.start &&
key < entry.end) {
143#if THREAD_SAFE == true
144 std::lock_guard<std::mutex>
l(m_mutex);
152 if (m_cache.size() >= Capacity) {
153 m_cache.erase(m_cache.begin());
156 m_cache.emplace_back(Entry{ value->address, value->address + value->size, value });
159 void clear()
override { m_cache.clear(); }
162 uint64_t get_hits()
const override {
return m_hits; }
163 uint64_t get_misses()
const override {
return m_misses; }
164 void reset_stats()
override
192template <
typename Key,
typename Value>
195 const size_t Capacity = 8;
206 std::list<CacheEntry> m_list;
209#if THREAD_SAFE == true
210 mutable std::mutex m_mutex;
227#if THREAD_SAFE == true
228 std::lock_guard<std::mutex>
l(m_mutex);
232 for (
auto it = m_list.end();
it != m_list.begin();) {
234 const CacheEntry& entry = *
it;
235 if (
key >= entry.start &&
key < entry.end) {
236 value = entry.target;
237 if (std::next(
it) != m_list.end()) {
238 m_list.splice(m_list.end(), m_list,
it);
261#if THREAD_SAFE == true
262 std::lock_guard<std::mutex>
l(m_mutex);
272 if (m_list.size() >= Capacity) {
277 m_list.emplace_back(value->address, value->address + value->size, value);
281 void clear()
override { m_list.clear(); }
284 uint64_t get_hits()
const override {
return m_hits; }
285 uint64_t get_misses()
const override {
return m_misses; }
286 void reset_stats()
override
Definition addrmap_cache_examples.h:16
void put(const Key &key, const Value &value, uint64_t size) override
Insert or update value in cache with LRU eviction.
Definition addrmap_cache_examples.h:59
bool get(const Key &key, Value &value) override
Retrieve value from cache and update LRU order.
Definition addrmap_cache_examples.h:36
void clear() override
Clear all cached entries.
Definition addrmap_cache_examples.h:77
RegionFIFOCache - A simple FIFO cache for region-based address lookups.
Definition addrmap_cache_examples.h:107
RegionLRUCache - A true LRU cache for region-based address lookups.
Definition addrmap_cache_examples.h:194
bool get(const Key &key, Value &value) override
Retrieve value from cache and update LRU order.
Definition addrmap_cache_examples.h:225
void put(const Key &key, const Value &value, uint64_t size) override
Insert or update region in cache with LRU eviction.
Definition addrmap_cache_examples.h:259
void clear() override
Clear all cached entries.
Definition addrmap_cache_examples.h:281
Tool which reads a Lua configuration file and sets parameters.
Definition biflow.cc:10