blob: cd091ce47a42633518bd9382809d3f3ecbc47b96 [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{
Alexander Afanasyev903062f2012-07-04 18:25:26 -070026 struct policy_hook_type : public bi::list_member_hook<> {};
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 typedef bi::member_hook< Container,
32 policy_hook_type,
33 &Container::policy_hook_ > type;
34 };
Alexander Afanasyev9a989702012-06-29 17:44:00 -070035
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070036 template<class Base,
37 class Container,
38 class Hook>
39 struct policy
40 {
41 typedef typename bi::list< Container, Hook > policy_container;
42
43 // could be just typedef
44 class type : public policy_container
Alexander Afanasyev9a989702012-06-29 17:44:00 -070045 {
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070046 public:
47 typedef Container parent_trie;
48
49 type (Base &base)
50 : base_ (base)
51 , max_size_ (100)
52 {
53 }
54
55 inline void
56 update (typename parent_trie::iterator item)
57 {
58 // do relocation
59 policy_container::splice (policy_container::end (),
60 *this,
61 policy_container::s_iterator_to (*item));
62 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -070063
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070064 inline bool
65 insert (typename parent_trie::iterator item)
66 {
67 if (policy_container::size () >= max_size_)
68 {
69 base_.erase (&(*policy_container::begin ()));
70 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -070071
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070072 policy_container::push_back (*item);
73 return true;
74 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -070075
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070076 inline void
77 lookup (typename parent_trie::iterator item)
78 {
79 // do relocation
80 policy_container::splice (policy_container::end (),
81 *this,
82 policy_container::s_iterator_to (*item));
83 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -070084
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070085 inline void
86 erase (typename parent_trie::iterator item)
87 {
88 policy_container::erase (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 set_max_size (size_t max_size)
93 {
94 max_size_ = max_size;
95 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -070096
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070097 inline size_t
98 get_max_size () const
99 {
100 return max_size_;
101 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700102
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700103 private:
Alexander Afanasyev903062f2012-07-04 18:25:26 -0700104 type () : base_(*((Base*)0)) { };
105
106 private:
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700107 Base &base_;
108 size_t max_size_;
109 };
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700110 };
111};
112
113#endif