TBETable.hh revision 7839
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_SYSTEM_TBETABLE_HH__
30#define __MEM_RUBY_SYSTEM_TBETABLE_HH__
31
32#include <iostream>
33
34#include "base/hashmap.hh"
35#include "mem/ruby/common/Address.hh"
36#include "mem/ruby/common/Global.hh"
37#include "mem/ruby/profiler/Profiler.hh"
38#include "mem/ruby/system/System.hh"
39
40template<class ENTRY>
41class TBETable
42{
43  public:
44    TBETable(int number_of_TBEs)
45        : m_number_of_TBEs(number_of_TBEs)
46    {
47    }
48
49    void
50    printConfig(std::ostream& out)
51    {
52        out << "TBEs_per_TBETable: " << m_number_of_TBEs << std::endl;
53    }
54
55    bool isPresent(const Address& address) const;
56    void allocate(const Address& address);
57    void deallocate(const Address& address);
58    bool
59    areNSlotsAvailable(int n) const
60    {
61        return (m_number_of_TBEs - m_map.size()) >= n;
62    }
63
64    ENTRY* lookup(const Address& address);
65
66    // Print cache contents
67    void print(std::ostream& out) const;
68
69  private:
70    // Private copy constructor and assignment operator
71    TBETable(const TBETable& obj);
72    TBETable& operator=(const TBETable& obj);
73
74    // Data Members (m_prefix)
75    m5::hash_map<Address, ENTRY> m_map;
76
77  private:
78    int m_number_of_TBEs;
79};
80
81template<class ENTRY>
82inline std::ostream&
83operator<<(std::ostream& out, const TBETable<ENTRY>& obj)
84{
85    obj.print(out);
86    out << std::flush;
87    return out;
88}
89
90template<class ENTRY>
91inline bool
92TBETable<ENTRY>::isPresent(const Address& address) const
93{
94    assert(address == line_address(address));
95    assert(m_map.size() <= m_number_of_TBEs);
96    return !!m_map.count(address);
97}
98
99template<class ENTRY>
100inline void
101TBETable<ENTRY>::allocate(const Address& address)
102{
103    assert(!isPresent(address));
104    assert(m_map.size() < m_number_of_TBEs);
105    m_map[address] = ENTRY();
106}
107
108template<class ENTRY>
109inline void
110TBETable<ENTRY>::deallocate(const Address& address)
111{
112    assert(isPresent(address));
113    assert(m_map.size() > 0);
114    m_map.erase(address);
115}
116
117// looks an address up in the cache
118template<class ENTRY>
119inline ENTRY*
120TBETable<ENTRY>::lookup(const Address& address)
121{
122  if(m_map.find(address) != m_map.end()) return &(m_map.find(address)->second);
123  return NULL;
124}
125
126
127template<class ENTRY>
128inline void
129TBETable<ENTRY>::print(std::ostream& out) const
130{
131}
132
133#endif // __MEM_RUBY_SYSTEM_TBETABLE_HH__
134