1/*
2 * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#ifndef __MEM_RUBY_STRUCTURES_TBETABLE_HH__
30#define __MEM_RUBY_STRUCTURES_TBETABLE_HH__
31
32#include <iostream>
33#include <unordered_map>
34
35#include "mem/ruby/common/Address.hh"
36
37template<class ENTRY>
38class TBETable
39{
40  public:
41    TBETable(int number_of_TBEs)
42        : m_number_of_TBEs(number_of_TBEs)
43    {
44    }
45
46    bool isPresent(Addr address) const;
47    void allocate(Addr address);
48    void deallocate(Addr address);
49    bool
50    areNSlotsAvailable(int n, Tick current_time) const
51    {
52        return (m_number_of_TBEs - m_map.size()) >= n;
53    }
54
55    ENTRY *lookup(Addr address);
56
57    // Print cache contents
58    void print(std::ostream& out) const;
59
60  private:
61    // Private copy constructor and assignment operator
62    TBETable(const TBETable& obj);
63    TBETable& operator=(const TBETable& obj);
64
65    // Data Members (m_prefix)
66    std::unordered_map<Addr, ENTRY> m_map;
67
68  private:
69    int m_number_of_TBEs;
70};
71
72template<class ENTRY>
73inline std::ostream&
74operator<<(std::ostream& out, const TBETable<ENTRY>& obj)
75{
76    obj.print(out);
77    out << std::flush;
78    return out;
79}
80
81template<class ENTRY>
82inline bool
83TBETable<ENTRY>::isPresent(Addr address) const
84{
85    assert(address == makeLineAddress(address));
86    assert(m_map.size() <= m_number_of_TBEs);
87    return !!m_map.count(address);
88}
89
90template<class ENTRY>
91inline void
92TBETable<ENTRY>::allocate(Addr address)
93{
94    assert(!isPresent(address));
95    assert(m_map.size() < m_number_of_TBEs);
96    m_map[address] = ENTRY();
97}
98
99template<class ENTRY>
100inline void
101TBETable<ENTRY>::deallocate(Addr address)
102{
103    assert(isPresent(address));
104    assert(m_map.size() > 0);
105    m_map.erase(address);
106}
107
108// looks an address up in the cache
109template<class ENTRY>
110inline ENTRY*
111TBETable<ENTRY>::lookup(Addr address)
112{
113  if (m_map.find(address) != m_map.end()) return &(m_map.find(address)->second);
114  return NULL;
115}
116
117
118template<class ENTRY>
119inline void
120TBETable<ENTRY>::print(std::ostream& out) const
121{
122}
123
124#endif // __MEM_RUBY_STRUCTURES_TBETABLE_HH__
125