Line 28... |
Line 28... |
|
|
#include <cstring>
|
#include <cstring>
|
|
|
#include "MemCache.h"
|
#include "MemCache.h"
|
|
|
|
|
//! Constructor
|
//! Constructor
|
|
|
//! Allocate a closed hash table of the specified size and clear it.
|
//! Allocate a closed hash table of the specified size and clear it.
|
|
|
//! @param[in] _tableSize The desire hash table size. A prime number is
|
//! @param[in] _tableSize The desire hash table size. A prime number is
|
//! recommended.
|
//! recommended.
|
|
|
MemCache::MemCache (int _tableSize) :
|
MemCache::MemCache(int _tableSize):tableSize(_tableSize)
|
tableSize (_tableSize)
|
|
{
|
{
|
tabIsValid = new bool [tableSize];
|
tabIsValid = new bool [tableSize];
|
tabKeyAddr = new uint32_t [tableSize];
|
tabKeyAddr = new uint32_t [tableSize];
|
tabValue = new uint32_t [tableSize];
|
tabValue = new uint32_t [tableSize];
|
|
|
clear ();
|
clear ();
|
|
|
} // MemCache ()
|
} // MemCache ()
|
|
|
|
|
//! Destructor
|
//! Destructor
|
|
|
//! Free the hash table arrays
|
//! Free the hash table arrays
|
|
|
MemCache::~MemCache ()
|
MemCache::~MemCache ()
|
Line 60... |
Line 57... |
delete [] tabKeyAddr;
|
delete [] tabKeyAddr;
|
delete [] tabValue;
|
delete [] tabValue;
|
|
|
} // ~MemCache ()
|
} // ~MemCache ()
|
|
|
|
|
//! Empty the hash table
|
//! Empty the hash table
|
|
|
//! Only need to worry about the validity field
|
//! Only need to worry about the validity field
|
void
|
void
|
MemCache::clear ()
|
MemCache::clear ()
|
{
|
{
|
memset (tabIsValid, false, sizeof (tabIsValid));
|
memset (tabIsValid, false, sizeof (tabIsValid));
|
|
|
} // clear ()
|
} // clear ()
|
|
|
|
|
//! Write a new value into the hash table
|
//! Write a new value into the hash table
|
|
|
//! Will trash anything already there.
|
//! Will trash anything already there.
|
|
|
//! @param[in] addr The address being written to
|
//! @param[in] addr The address being written to
|
//! @param[in] value The value to write
|
//! @param[in] value The value to write
|
void
|
void MemCache::write(uint32_t addr, uint32_t value)
|
MemCache::write (uint32_t addr,
|
|
uint32_t value)
|
|
{
|
{
|
int keyAddr = addr % tableSize;
|
int keyAddr = addr % tableSize;
|
|
|
tabIsValid[keyAddr] = true;
|
tabIsValid[keyAddr] = true;
|
tabKeyAddr[keyAddr] = addr;
|
tabKeyAddr[keyAddr] = addr;
|
tabValue[keyAddr] = value;
|
tabValue[keyAddr] = value;
|
|
|
} // write ()
|
} // write ()
|
|
|
|
|
//! Try to read a value from the hash table
|
//! Try to read a value from the hash table
|
|
|
//! The entry must be valid and the address must match
|
//! The entry must be valid and the address must match
|
|
|
//! @param[in] addr The address being read from
|
//! @param[in] addr The address being read from
|
//! @param[out] value The value read, if there was one there
|
//! @param[out] value The value read, if there was one there
|
|
|
//! @return True if the value was found in the hash table
|
//! @return True if the value was found in the hash table
|
|
|
bool
|
bool MemCache::read(uint32_t addr, uint32_t & value)
|
MemCache::read (uint32_t addr,
|
|
uint32_t &value)
|
|
{
|
{
|
int keyAddr = addr % tableSize;
|
int keyAddr = addr % tableSize;
|
|
|
if (tabIsValid[keyAddr] & (tabKeyAddr[keyAddr] == addr))
|
if (tabIsValid[keyAddr] & (tabKeyAddr[keyAddr] == addr)) {
|
{
|
|
value = tabValue[keyAddr];
|
value = tabValue[keyAddr];
|
return true;
|
return true;
|
}
|
} else {
|
else
|
|
{
|
|
return false;
|
return false;
|
}
|
}
|
} // read ()
|
} // read ()
|
|
|
No newline at end of file
|
No newline at end of file
|