blob: 14e49f73eb59a43afff62dfcf4f536b06f992899 [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 Afanasyev9a989702012-06-29 17:44:00 -070024struct lru_policy_traits
25{
26 typedef bi::list_member_hook<> policy_hook_type;
Alexander Afanasyev9a989702012-06-29 17:44:00 -070027
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070028 template<class Container>
29 struct container_hook
Alexander Afanasyev9a989702012-06-29 17:44:00 -070030 {
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070031 // could be class/struct implementation
32 typedef bi::member_hook< Container,
33 policy_hook_type,
34 &Container::policy_hook_ > type;
35 };
Alexander Afanasyev9a989702012-06-29 17:44:00 -070036
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070037 template<class Base,
38 class Container,
39 class Hook>
40 struct policy
41 {
42 typedef typename bi::list< Container, Hook > policy_container;
43
44 // could be just typedef
45 class type : public policy_container
Alexander Afanasyev9a989702012-06-29 17:44:00 -070046 {
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070047 public:
48 typedef Container parent_trie;
49
50 type (Base &base)
51 : base_ (base)
52 , max_size_ (100)
53 {
54 }
55
56 inline void
57 update (typename parent_trie::iterator item)
58 {
59 // do relocation
60 policy_container::splice (policy_container::end (),
61 *this,
62 policy_container::s_iterator_to (*item));
63 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -070064
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070065 inline bool
66 insert (typename parent_trie::iterator item)
67 {
68 if (policy_container::size () >= max_size_)
69 {
70 base_.erase (&(*policy_container::begin ()));
71 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -070072
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070073 policy_container::push_back (*item);
74 return true;
75 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -070076
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070077 inline void
78 lookup (typename parent_trie::iterator item)
79 {
80 // do relocation
81 policy_container::splice (policy_container::end (),
82 *this,
83 policy_container::s_iterator_to (*item));
84 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -070085
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070086 inline void
87 erase (typename parent_trie::iterator item)
88 {
89 policy_container::erase (policy_container::s_iterator_to (*item));
90 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -070091
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070092 inline void
93 set_max_size (size_t max_size)
94 {
95 max_size_ = max_size;
96 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -070097
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070098 inline size_t
99 get_max_size () const
100 {
101 return max_size_;
102 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700103
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700104 private:
105 Base &base_;
106 size_t max_size_;
107 };
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700108 };
109};
110
111#endif