blob: fc790a3700fe61a7181a993335fedabcdcb524ae [file] [log] [blame]
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2011-2015 Regents of the University of California.
Alexander Afanasyev9a989702012-06-29 17:44:00 -07004 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08005 * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and
6 * contributors.
Alexander Afanasyev9a989702012-06-29 17:44:00 -07007 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08008 * ndnSIM is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
Alexander Afanasyev9a989702012-06-29 17:44:00 -070011 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080012 * ndnSIM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
Alexander Afanasyev9a989702012-06-29 17:44:00 -070015 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080016 * You should have received a copy of the GNU General Public License along with
17 * ndnSIM, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 **/
Alexander Afanasyev9a989702012-06-29 17:44:00 -070019
20#ifndef LRU_POLICY_H_
21#define LRU_POLICY_H_
22
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070023#include <boost/intrusive/options.hpp>
24#include <boost/intrusive/list.hpp>
25
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070026namespace ns3 {
27namespace ndn {
28namespace ndnSIM {
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070029
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070030/**
31 * @brief Traits for Least Recently Used replacement policy
32 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080033struct lru_policy_traits {
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -080034 /// @brief Name that can be used to identify the policy (for NS-3 object model and logging)
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080035 static std::string
36 GetName()
Alexander Afanasyev9a989702012-06-29 17:44:00 -070037 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080038 return "Lru";
39 }
40
41 struct policy_hook_type : public boost::intrusive::list_member_hook<> {
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070042 };
Alexander Afanasyev9a989702012-06-29 17:44:00 -070043
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080044 template<class Container>
45 struct container_hook {
46 typedef boost::intrusive::member_hook<Container, policy_hook_type, &Container::policy_hook_>
47 type;
48 };
49
50 template<class Base, class Container, class Hook>
51 struct policy {
52 typedef typename boost::intrusive::list<Container, Hook> policy_container;
53
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070054 // could be just typedef
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080055 class type : public policy_container {
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070056 public:
57 typedef Container parent_trie;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080058
59 type(Base& base)
60 : base_(base)
61 , max_size_(100)
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070062 {
63 }
64
65 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080066 update(typename parent_trie::iterator item)
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070067 {
68 // do relocation
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080069 policy_container::splice(policy_container::end(), *this,
70 policy_container::s_iterator_to(*item));
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070071 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080072
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070073 inline bool
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080074 insert(typename parent_trie::iterator item)
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070075 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080076 if (max_size_ != 0 && policy_container::size() >= max_size_) {
77 base_.erase(&(*policy_container::begin()));
78 }
79
80 policy_container::push_back(*item);
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070081 return true;
82 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080083
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070084 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080085 lookup(typename parent_trie::iterator item)
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070086 {
87 // do relocation
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080088 policy_container::splice(policy_container::end(), *this,
89 policy_container::s_iterator_to(*item));
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070090 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -070091
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070092 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080093 erase(typename parent_trie::iterator item)
Alexander Afanasyev78057c32012-07-06 15:18:46 -070094 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080095 policy_container::erase(policy_container::s_iterator_to(*item));
Alexander Afanasyev78057c32012-07-06 15:18:46 -070096 }
97
98 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080099 clear()
100 {
101 policy_container::clear();
102 }
103
104 inline void
105 set_max_size(size_t max_size)
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700106 {
107 max_size_ = max_size;
108 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700109
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700110 inline size_t
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800111 get_max_size() const
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700112 {
113 return max_size_;
114 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700115
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700116 private:
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800117 type()
118 : base_(*((Base*)0)){};
Alexander Afanasyev903062f2012-07-04 18:25:26 -0700119
120 private:
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800121 Base& base_;
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700122 size_t max_size_;
123 };
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700124 };
125};
126
Alexander Afanasyev30cb1172012-07-06 10:47:39 -0700127} // ndnSIM
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700128} // ndn
Alexander Afanasyeve77db792012-08-09 11:10:58 -0700129} // ns3
Alexander Afanasyev30cb1172012-07-06 10:47:39 -0700130
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700131#endif