blob: f8288011a19a0a951921f27b65bfe4650e23dbaa [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
27namespace ndnSIM
28{
29
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070030/**
31 * @brief Traits for Least Recently Used replacement policy
32 */
Alexander Afanasyev9a989702012-06-29 17:44:00 -070033struct lru_policy_traits
34{
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070035 struct policy_hook_type : public boost::intrusive::list_member_hook<> {};
Alexander Afanasyev9a989702012-06-29 17:44:00 -070036
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070037 template<class Container>
38 struct container_hook
Alexander Afanasyev9a989702012-06-29 17:44:00 -070039 {
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070040 typedef boost::intrusive::member_hook< Container,
41 policy_hook_type,
42 &Container::policy_hook_ > type;
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070043 };
Alexander Afanasyev9a989702012-06-29 17:44:00 -070044
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070045 template<class Base,
46 class Container,
47 class Hook>
48 struct policy
49 {
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070050 typedef typename boost::intrusive::list< Container, Hook > policy_container;
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070051
52 // could be just typedef
53 class type : public policy_container
Alexander Afanasyev9a989702012-06-29 17:44:00 -070054 {
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070055 public:
56 typedef Container parent_trie;
57
58 type (Base &base)
59 : base_ (base)
60 , max_size_ (100)
61 {
62 }
63
64 inline void
65 update (typename parent_trie::iterator item)
66 {
67 // do relocation
68 policy_container::splice (policy_container::end (),
69 *this,
70 policy_container::s_iterator_to (*item));
71 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -070072
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070073 inline bool
74 insert (typename parent_trie::iterator item)
75 {
Alexander Afanasyevbd6f3f42012-07-26 17:50:17 -070076 if (max_size_ != 0 && policy_container::size () >= max_size_)
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070077 {
78 base_.erase (&(*policy_container::begin ()));
79 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -070080
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070081 policy_container::push_back (*item);
82 return true;
83 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -070084
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070085 inline void
86 lookup (typename parent_trie::iterator item)
87 {
88 // do relocation
89 policy_container::splice (policy_container::end (),
90 *this,
91 policy_container::s_iterator_to (*item));
92 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -070093
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070094 inline void
95 erase (typename parent_trie::iterator item)
96 {
97 policy_container::erase (policy_container::s_iterator_to (*item));
98 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -070099
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700100 inline void
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700101 clear ()
102 {
103 policy_container::clear ();
104 }
105
106 inline void
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700107 set_max_size (size_t max_size)
108 {
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
113 get_max_size () const
114 {
115 return max_size_;
116 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700117
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700118 private:
Alexander Afanasyev903062f2012-07-04 18:25:26 -0700119 type () : base_(*((Base*)0)) { };
120
121 private:
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700122 Base &base_;
123 size_t max_size_;
124 };
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700125 };
126};
127
Alexander Afanasyev30cb1172012-07-06 10:47:39 -0700128} // ndnSIM
129
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700130#endif