-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an implementation of a true LRU cache replacement policy
- Loading branch information
Showing
3 changed files
with
86 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// <LRUReplacement.hpp> -*- C++ -*- | ||
|
||
//! | ||
//! \file LRUReplacement.hpp | ||
//! \brief Provides a simple true LRU implementation | ||
//! | ||
|
||
#pragma once | ||
|
||
#include <vector> | ||
#include <list> | ||
|
||
#include "ReplacementIF.hpp" | ||
|
||
namespace sparta::cache | ||
{ | ||
// This class models true LRU with an std::list of way indices. The head of the list is LRU, and | ||
// the tail is MRU. An std::vector of iterators to the list is used to provide constant-time | ||
// index -> iterator lookup. A way is placed at LRU or MRU by moving its respective iterator to | ||
// the beginning or end of the list. | ||
class LRUReplacement : public ReplacementIF | ||
{ | ||
public: | ||
explicit LRUReplacement(const uint32_t num_ways) : ReplacementIF(num_ways) | ||
{ | ||
for (uint32_t i = 0; i < num_ways; ++i) | ||
{ | ||
way_map_.emplace_back(lru_stack_.emplace(lru_stack_.end(), i)); | ||
} | ||
} | ||
|
||
ReplacementIF* clone() const override | ||
{ | ||
return new LRUReplacement(num_ways_); | ||
} | ||
|
||
void touchLRU(uint32_t way) override | ||
{ | ||
auto lru_it = way_map_[way]; | ||
lru_stack_.splice(lru_stack_.begin(), lru_stack_, lru_it); | ||
} | ||
|
||
void touchLRU(uint32_t way, const std::vector<uint32_t> & way_order) override | ||
{ | ||
sparta_assert(false, "Not implemented"); | ||
} | ||
|
||
void touchMRU(uint32_t way) override | ||
{ | ||
auto lru_it = way_map_[way]; | ||
lru_stack_.splice(lru_stack_.end(), lru_stack_, lru_it); | ||
} | ||
|
||
void touchMRU(uint32_t way, const std::vector<uint32_t> & way_order) override | ||
{ | ||
sparta_assert(false, "Not implemented"); | ||
} | ||
|
||
uint32_t getLRUWay() const override { return lru_stack_.front(); } | ||
|
||
uint32_t getLRUWay(const std::vector<uint32_t> & way_order) override | ||
{ | ||
sparta_assert(false, "Not implemented"); | ||
} | ||
|
||
uint32_t getMRUWay() const override { return lru_stack_.back(); } | ||
|
||
uint32_t getMRUWay(const std::vector<uint32_t> & way_order) override | ||
{ | ||
sparta_assert(false, "Not implemented"); | ||
} | ||
|
||
void lockWay(uint32_t way) override | ||
{ | ||
sparta_assert(way < num_ways_); | ||
sparta_assert(false, "Not implemented"); | ||
} | ||
|
||
private: | ||
std::list<uint32_t> lru_stack_; | ||
std::vector<std::list<uint32_t>::const_iterator> way_map_; | ||
}; | ||
} // namespace shinro |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters