blob: a88b0f755755a8e3f0a0e164cbf4e8e2a1326385 [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
Spyridon Mastorakis460f57c2014-12-17 00:44:14 -080023/// @cond include_hidden
24
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070025#include <boost/intrusive/options.hpp>
26#include <boost/intrusive/list.hpp>
27
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070028namespace ns3 {
29namespace ndn {
30namespace ndnSIM {
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070031
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070032/**
33 * @brief Traits for Least Recently Used replacement policy
34 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080035struct lru_policy_traits {
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -080036 /// @brief Name that can be used to identify the policy (for NS-3 object model and logging)
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080037 static std::string
38 GetName()
Alexander Afanasyev9a989702012-06-29 17:44:00 -070039 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080040 return "Lru";
41 }
42
43 struct policy_hook_type : public boost::intrusive::list_member_hook<> {
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070044 };
Alexander Afanasyev9a989702012-06-29 17:44:00 -070045
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080046 template<class Container>
47 struct container_hook {
48 typedef boost::intrusive::member_hook<Container, policy_hook_type, &Container::policy_hook_>
49 type;
50 };
51
52 template<class Base, class Container, class Hook>
53 struct policy {
54 typedef typename boost::intrusive::list<Container, Hook> policy_container;
55
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070056 // could be just typedef
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080057 class type : public policy_container {
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070058 public:
59 typedef Container parent_trie;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080060
61 type(Base& base)
62 : base_(base)
63 , max_size_(100)
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070064 {
65 }
66
67 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080068 update(typename parent_trie::iterator item)
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070069 {
70 // do relocation
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080071 policy_container::splice(policy_container::end(), *this,
72 policy_container::s_iterator_to(*item));
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070073 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080074
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070075 inline bool
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080076 insert(typename parent_trie::iterator item)
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070077 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080078 if (max_size_ != 0 && policy_container::size() >= max_size_) {
79 base_.erase(&(*policy_container::begin()));
80 }
81
82 policy_container::push_back(*item);
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070083 return true;
84 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080085
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070086 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080087 lookup(typename parent_trie::iterator item)
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070088 {
89 // do relocation
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080090 policy_container::splice(policy_container::end(), *this,
91 policy_container::s_iterator_to(*item));
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070092 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -070093
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070094 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080095 erase(typename parent_trie::iterator item)
Alexander Afanasyev78057c32012-07-06 15:18:46 -070096 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080097 policy_container::erase(policy_container::s_iterator_to(*item));
Alexander Afanasyev78057c32012-07-06 15:18:46 -070098 }
99
100 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800101 clear()
102 {
103 policy_container::clear();
104 }
105
106 inline void
107 set_max_size(size_t max_size)
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700108 {
109 max_size_ = max_size;
110 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700111
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700112 inline size_t
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800113 get_max_size() const
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700114 {
115 return max_size_;
116 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700117
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700118 private:
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800119 type()
120 : base_(*((Base*)0)){};
Alexander Afanasyev903062f2012-07-04 18:25:26 -0700121
122 private:
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800123 Base& base_;
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700124 size_t max_size_;
125 };
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700126 };
127};
128
Alexander Afanasyev30cb1172012-07-06 10:47:39 -0700129} // ndnSIM
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700130} // ndn
Alexander Afanasyeve77db792012-08-09 11:10:58 -0700131} // ns3
Alexander Afanasyev30cb1172012-07-06 10:47:39 -0700132
Spyridon Mastorakis460f57c2014-12-17 00:44:14 -0800133/// @endcond
134
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700135#endif