blob: 77a65cabd7b295f719a4fd43b8619d473b710583 [file] [log] [blame]
Alexander Afanasyev9a989702012-06-29 17:44:00 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2011 University of California, Los Angeles
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
19 */
20
21#ifndef LRU_POLICY_H_
22#define LRU_POLICY_H_
23
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070024#include <boost/intrusive/options.hpp>
25#include <boost/intrusive/list.hpp>
26
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070027namespace ns3 {
28namespace ndn {
29namespace ndnSIM {
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070030
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070031/**
32 * @brief Traits for Least Recently Used replacement policy
33 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080034struct lru_policy_traits {
Alexander Afanasyev991a0cc2012-12-10 11:38:19 -080035 /// @brief Name that can be used to identify the policy (for NS-3 object model and logging)
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080036 static std::string
37 GetName()
Alexander Afanasyev9a989702012-06-29 17:44:00 -070038 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080039 return "Lru";
40 }
41
42 struct policy_hook_type : public boost::intrusive::list_member_hook<> {
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070043 };
Alexander Afanasyev9a989702012-06-29 17:44:00 -070044
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080045 template<class Container>
46 struct container_hook {
47 typedef boost::intrusive::member_hook<Container, policy_hook_type, &Container::policy_hook_>
48 type;
49 };
50
51 template<class Base, class Container, class Hook>
52 struct policy {
53 typedef typename boost::intrusive::list<Container, Hook> policy_container;
54
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070055 // could be just typedef
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080056 class type : public policy_container {
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070057 public:
58 typedef Container parent_trie;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080059
60 type(Base& base)
61 : base_(base)
62 , max_size_(100)
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070063 {
64 }
65
66 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080067 update(typename parent_trie::iterator item)
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070068 {
69 // do relocation
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080070 policy_container::splice(policy_container::end(), *this,
71 policy_container::s_iterator_to(*item));
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070072 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080073
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070074 inline bool
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080075 insert(typename parent_trie::iterator item)
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070076 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080077 if (max_size_ != 0 && policy_container::size() >= max_size_) {
78 base_.erase(&(*policy_container::begin()));
79 }
80
81 policy_container::push_back(*item);
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070082 return true;
83 }
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080084
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070085 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080086 lookup(typename parent_trie::iterator item)
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070087 {
88 // do relocation
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080089 policy_container::splice(policy_container::end(), *this,
90 policy_container::s_iterator_to(*item));
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070091 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -070092
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070093 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080094 erase(typename parent_trie::iterator item)
Alexander Afanasyev78057c32012-07-06 15:18:46 -070095 {
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080096 policy_container::erase(policy_container::s_iterator_to(*item));
Alexander Afanasyev78057c32012-07-06 15:18:46 -070097 }
98
99 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800100 clear()
101 {
102 policy_container::clear();
103 }
104
105 inline void
106 set_max_size(size_t max_size)
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700107 {
108 max_size_ = max_size;
109 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700110
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700111 inline size_t
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800112 get_max_size() const
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700113 {
114 return max_size_;
115 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700116
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700117 private:
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800118 type()
119 : base_(*((Base*)0)){};
Alexander Afanasyev903062f2012-07-04 18:25:26 -0700120
121 private:
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800122 Base& base_;
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700123 size_t max_size_;
124 };
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700125 };
126};
127
Alexander Afanasyev30cb1172012-07-06 10:47:39 -0700128} // ndnSIM
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700129} // ndn
Alexander Afanasyeve77db792012-08-09 11:10:58 -0700130} // ns3
Alexander Afanasyev30cb1172012-07-06 10:47:39 -0700131
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700132#endif