Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 1 | /* -*- 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 Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 24 | #include <boost/intrusive/options.hpp> |
| 25 | #include <boost/intrusive/list.hpp> |
| 26 | |
| 27 | namespace ndnSIM |
| 28 | { |
| 29 | |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 30 | struct lru_policy_traits |
| 31 | { |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 32 | struct policy_hook_type : public boost::intrusive::list_member_hook<> {}; |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 33 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 34 | template<class Container> |
| 35 | struct container_hook |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 36 | { |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 37 | typedef boost::intrusive::member_hook< Container, |
| 38 | policy_hook_type, |
| 39 | &Container::policy_hook_ > type; |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 40 | }; |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 41 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 42 | template<class Base, |
| 43 | class Container, |
| 44 | class Hook> |
| 45 | struct policy |
| 46 | { |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 47 | typedef typename boost::intrusive::list< Container, Hook > policy_container; |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 48 | |
| 49 | // could be just typedef |
| 50 | class type : public policy_container |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 51 | { |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 52 | 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 Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 69 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 70 | 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 Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 77 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 78 | policy_container::push_back (*item); |
| 79 | return true; |
| 80 | } |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 81 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 82 | 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 Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 90 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 91 | inline void |
| 92 | erase (typename parent_trie::iterator item) |
| 93 | { |
| 94 | policy_container::erase (policy_container::s_iterator_to (*item)); |
| 95 | } |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 96 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 97 | inline void |
| 98 | set_max_size (size_t max_size) |
| 99 | { |
| 100 | max_size_ = max_size; |
| 101 | } |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 102 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 103 | inline size_t |
| 104 | get_max_size () const |
| 105 | { |
| 106 | return max_size_; |
| 107 | } |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 108 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 109 | private: |
Alexander Afanasyev | 903062f | 2012-07-04 18:25:26 -0700 | [diff] [blame] | 110 | type () : base_(*((Base*)0)) { }; |
| 111 | |
| 112 | private: |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 113 | Base &base_; |
| 114 | size_t max_size_; |
| 115 | }; |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 116 | }; |
| 117 | }; |
| 118 | |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 119 | } // ndnSIM |
| 120 | |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 121 | #endif |