00001 // 00002 // Copyright (c) 2005-2008 Kenichi Watanabe. 00003 // Copyright (c) 2005-2008 Yasuhiro Watari. 00004 // Copyright (c) 2005-2008 Hironori Ichibayashi. 00005 // Copyright (c) 2008-2009 Kazuo Horio. 00006 // Copyright (c) 2009-2013 Naruki Kurata. 00007 // Copyright (c) 2005-2013 Ryota Shioya. 00008 // Copyright (c) 2005-2013 Masahiro Goshima. 00009 // 00010 // This software is provided 'as-is', without any express or implied 00011 // warranty. In no event will the authors be held liable for any damages 00012 // arising from the use of this software. 00013 // 00014 // Permission is granted to anyone to use this software for any purpose, 00015 // including commercial applications, and to alter it and redistribute it 00016 // freely, subject to the following restrictions: 00017 // 00018 // 1. The origin of this software must not be misrepresented; you must not 00019 // claim that you wrote the original software. If you use this software 00020 // in a product, an acknowledgment in the product documentation would be 00021 // appreciated but is not required. 00022 // 00023 // 2. Altered source versions must be plainly marked as such, and must not be 00024 // misrepresented as being the original software. 00025 // 00026 // 3. This notice may not be removed or altered from any source 00027 // distribution. 00028 // 00029 // 00030 00031 00032 #ifndef SIM_MEMORY_CACHE_CACHE_EXTRA_STATE_TABLE_H 00033 #define SIM_MEMORY_CACHE_CACHE_EXTRA_STATE_TABLE_H 00034 00035 #include "Sim/Memory/Cache/Cache.h" 00036 00037 namespace Onikiri 00038 { 00039 00040 // 00041 // A mechanism adding extra information to cache lines. 00042 // Information corresponding to a line that is pointed by setassoc_table::iterator 00043 // is get from / set to this table. 00044 // Cache/PrefetcherBase classes use this table and they are examples of usage. 00045 // 00046 00047 class Cache; 00048 00049 template < typename ValueType, typename ContainerType = std::vector<ValueType> > 00050 class CacheExtraStateTable 00051 { 00052 00053 public: 00054 typedef typename ContainerType::reference ReferenceType; 00055 typedef typename ContainerType::const_reference ConstReferenceType; 00056 typedef typename CacheTable::iterator iterator; 00057 typedef typename CacheTable::const_iterator const_iterator; 00058 00059 protected: 00060 ContainerType m_table; 00061 size_t m_indexCount; 00062 size_t m_wayCount; 00063 00064 template < typename T > 00065 size_t GetTableIndex( const T& i ) const 00066 { 00067 ASSERT( 00068 m_wayCount > 0 && m_indexCount > 0, 00069 "CacheExtraStateTable is not initialized." 00070 ); 00071 ASSERT( 00072 i.way() < m_wayCount && i.index() < m_indexCount, 00073 "The iterator points out of range." 00074 ); 00075 return i.way() + i.index() * m_wayCount; 00076 } 00077 00078 public: 00079 void Resize( Cache* cache, const ValueType& initValue = ValueType() ) 00080 { 00081 m_indexCount = cache->GetIndexCount(); 00082 m_wayCount = cache->GetWayCount(); 00083 m_table.resize( m_indexCount * m_wayCount, initValue ); 00084 } 00085 00086 CacheExtraStateTable( Cache* cache ) 00087 { 00088 Resize( cache ); 00089 } 00090 00091 CacheExtraStateTable() : 00092 m_indexCount(0), 00093 m_wayCount(0) 00094 { 00095 } 00096 00097 00098 ReferenceType operator[]( const iterator& i ) 00099 { 00100 return m_table[ GetTableIndex(i) ]; 00101 } 00102 00103 ConstReferenceType operator[]( const iterator& i ) const 00104 { 00105 return m_table[ GetTableIndex(i) ]; 00106 } 00107 00108 ReferenceType operator[]( const const_iterator& i ) 00109 { 00110 return m_table[ GetTableIndex(i) ]; 00111 } 00112 00113 ConstReferenceType operator[]( const const_iterator& i ) const 00114 { 00115 return m_table[ GetTableIndex(i) ]; 00116 } 00117 00118 }; 00119 00120 }; // namespace Onikiri 00121 00122 #endif // SIM_MEMORY_CACHE_CACHE_EXTRA_STATE_TABLE_H 00123