blob: 077d602f75f9aae6d646172d695fa5bb2579331b [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 Afanasyev9a989702012-06-29 17:44:00 -070030struct lru_policy_traits
31{
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070032 struct policy_hook_type : public boost::intrusive::list_member_hook<> {};
Alexander Afanasyev9a989702012-06-29 17:44:00 -070033
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070034 template<class Container>
35 struct container_hook
Alexander Afanasyev9a989702012-06-29 17:44:00 -070036 {
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070037 typedef boost::intrusive::member_hook< Container,
38 policy_hook_type,
39 &Container::policy_hook_ > type;
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070040 };
Alexander Afanasyev9a989702012-06-29 17:44:00 -070041
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070042 template<class Base,
43 class Container,
44 class Hook>
45 struct policy
46 {
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070047 typedef typename boost::intrusive::list< Container, Hook > policy_container;
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070048
49 // could be just typedef
50 class type : public policy_container
Alexander Afanasyev9a989702012-06-29 17:44:00 -070051 {
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070052 public:
53 typedef Container parent_trie;
54
55 type (Base &base)
56 : base_ (base)
57 , max_size_ (100)
58 {
59 }
60
61 inline void
62 update (typename parent_trie::iterator item)
63 {
64 // do relocation
65 policy_container::splice (policy_container::end (),
66 *this,
67 policy_container::s_iterator_to (*item));
68 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -070069
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070070 inline bool
71 insert (typename parent_trie::iterator item)
72 {
73 if (policy_container::size () >= max_size_)
74 {
75 base_.erase (&(*policy_container::begin ()));
76 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -070077
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070078 policy_container::push_back (*item);
79 return true;
80 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -070081
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070082 inline void
83 lookup (typename parent_trie::iterator item)
84 {
85 // do relocation
86 policy_container::splice (policy_container::end (),
87 *this,
88 policy_container::s_iterator_to (*item));
89 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -070090
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070091 inline void
92 erase (typename parent_trie::iterator item)
93 {
94 policy_container::erase (policy_container::s_iterator_to (*item));
95 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -070096
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070097 inline void
Alexander Afanasyev78057c32012-07-06 15:18:46 -070098 clear ()
99 {
100 policy_container::clear ();
101 }
102
103 inline void
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700104 set_max_size (size_t max_size)
105 {
106 max_size_ = max_size;
107 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700108
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700109 inline size_t
110 get_max_size () const
111 {
112 return max_size_;
113 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700114
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700115 private:
Alexander Afanasyev903062f2012-07-04 18:25:26 -0700116 type () : base_(*((Base*)0)) { };
117
118 private:
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700119 Base &base_;
120 size_t max_size_;
121 };
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700122 };
123};
124
Alexander Afanasyev30cb1172012-07-06 10:47:39 -0700125} // ndnSIM
126
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700127#endif