DataBlock.hh revision 7002
113206Sgabeblack@google.com 213206Sgabeblack@google.com/* 313206Sgabeblack@google.com * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood 413206Sgabeblack@google.com * All rights reserved. 513206Sgabeblack@google.com * 613206Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without 713206Sgabeblack@google.com * modification, are permitted provided that the following conditions are 813206Sgabeblack@google.com * met: redistributions of source code must retain the above copyright 913206Sgabeblack@google.com * notice, this list of conditions and the following disclaimer; 1013206Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright 1113206Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the 1213206Sgabeblack@google.com * documentation and/or other materials provided with the distribution; 1313206Sgabeblack@google.com * neither the name of the copyright holders nor the names of its 1413206Sgabeblack@google.com * contributors may be used to endorse or promote products derived from 1513206Sgabeblack@google.com * this software without specific prior written permission. 1613206Sgabeblack@google.com * 1713206Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1813206Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1913206Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 2013206Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2113206Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2213206Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2313206Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2413206Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2513206Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2613206Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 2713206Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2813206Sgabeblack@google.com */ 2913206Sgabeblack@google.com 3013206Sgabeblack@google.com#ifndef DATABLOCK_H 3113206Sgabeblack@google.com#define DATABLOCK_H 3213206Sgabeblack@google.com 3313206Sgabeblack@google.com#include <iomanip> 3413206Sgabeblack@google.com#include <iostream> 3513206Sgabeblack@google.com 3613206Sgabeblack@google.com#include "mem/ruby/common/Global.hh" 3713206Sgabeblack@google.com#include "mem/ruby/system/System.hh" 3813206Sgabeblack@google.com#include "mem/gems_common/Vector.hh" 3913260Sgabeblack@google.com 4013206Sgabeblack@google.comclass DataBlock { 4113206Sgabeblack@google.com public: 4213206Sgabeblack@google.com // Constructors 4313206Sgabeblack@google.com DataBlock() {alloc();} 4413206Sgabeblack@google.com DataBlock(const DataBlock & cp) { 4513206Sgabeblack@google.com m_data = new uint8[RubySystem::getBlockSizeBytes()]; 4613206Sgabeblack@google.com memcpy(m_data, cp.m_data, RubySystem::getBlockSizeBytes()); 4713206Sgabeblack@google.com m_alloc = true; 4813206Sgabeblack@google.com } 4913206Sgabeblack@google.com 5013206Sgabeblack@google.com // Destructor 5113206Sgabeblack@google.com ~DataBlock() { 5213206Sgabeblack@google.com if(m_alloc) { 5313206Sgabeblack@google.com delete [] m_data; 5413206Sgabeblack@google.com } 5513206Sgabeblack@google.com } 5613206Sgabeblack@google.com 5713206Sgabeblack@google.com DataBlock& operator=(const DataBlock& obj); 5813206Sgabeblack@google.com 5913206Sgabeblack@google.com // Public Methods 6013206Sgabeblack@google.com void assign(uint8* data); 6113206Sgabeblack@google.com 6213206Sgabeblack@google.com void clear(); 6313206Sgabeblack@google.com uint8 getByte(int whichByte) const; 6413206Sgabeblack@google.com const uint8* getData(int offset, int len) const; 6513206Sgabeblack@google.com void setByte(int whichByte, uint8 data); 6613206Sgabeblack@google.com void setData(uint8* data, int offset, int len); 6713206Sgabeblack@google.com void copyPartial(const DataBlock & dblk, int offset, int len); 6813206Sgabeblack@google.com bool equal(const DataBlock& obj) const; 6913206Sgabeblack@google.com void print(std::ostream& out) const; 7013206Sgabeblack@google.com 7113206Sgabeblack@google.comprivate: 7213206Sgabeblack@google.com void alloc(); 7313206Sgabeblack@google.com // Data Members (m_ prefix) 7413206Sgabeblack@google.com uint8* m_data; 7513206Sgabeblack@google.com bool m_alloc; 7613206Sgabeblack@google.com}; 7713206Sgabeblack@google.com 7813206Sgabeblack@google.com// Output operator declaration 7913206Sgabeblack@google.comstd::ostream& operator<<(std::ostream& out, const DataBlock& obj); 8013206Sgabeblack@google.com 8113206Sgabeblack@google.combool operator==(const DataBlock& obj1, const DataBlock& obj2); 8213206Sgabeblack@google.com 8313206Sgabeblack@google.com// inline functions for speed 8413206Sgabeblack@google.com 8513206Sgabeblack@google.cominline 8613206Sgabeblack@google.comvoid DataBlock::assign(uint8* data) 8713206Sgabeblack@google.com{ 8813260Sgabeblack@google.com if (m_alloc) { 8913260Sgabeblack@google.com delete [] m_data; 9013260Sgabeblack@google.com } 9113260Sgabeblack@google.com m_data = data; 9213260Sgabeblack@google.com m_alloc = false; 9313260Sgabeblack@google.com} 9413260Sgabeblack@google.com 9513260Sgabeblack@google.cominline 9613208Sgabeblack@google.comvoid DataBlock::alloc() 9713208Sgabeblack@google.com{ 9813206Sgabeblack@google.com m_data = new uint8[RubySystem::getBlockSizeBytes()]; 9913206Sgabeblack@google.com m_alloc = true; 10013206Sgabeblack@google.com clear(); 10113206Sgabeblack@google.com} 10213260Sgabeblack@google.com 10313206Sgabeblack@google.cominline 10413206Sgabeblack@google.comvoid DataBlock::clear() 10513206Sgabeblack@google.com{ 10613206Sgabeblack@google.com memset(m_data, 0, RubySystem::getBlockSizeBytes()); 10713206Sgabeblack@google.com} 10813206Sgabeblack@google.com 10913206Sgabeblack@google.cominline 11013206Sgabeblack@google.combool DataBlock::equal(const DataBlock& obj) const 11113206Sgabeblack@google.com{ 11213206Sgabeblack@google.com return !memcmp(m_data, obj.m_data, RubySystem::getBlockSizeBytes()); 11313206Sgabeblack@google.com} 11413260Sgabeblack@google.com 11513206Sgabeblack@google.cominline 11613206Sgabeblack@google.comvoid DataBlock::print(std::ostream& out) const 11713206Sgabeblack@google.com{ 11813206Sgabeblack@google.com using namespace std; 11913206Sgabeblack@google.com 12013206Sgabeblack@google.com int size = RubySystem::getBlockSizeBytes(); 12113206Sgabeblack@google.com out << "[ "; 12213206Sgabeblack@google.com for (int i = 0; i < size; i++) { 12313206Sgabeblack@google.com out << setw(2) << setfill('0') << hex << "0x" << (int)m_data[i] << " "; 12413206Sgabeblack@google.com out << setfill(' '); 12513206Sgabeblack@google.com } 12613206Sgabeblack@google.com out << dec << "]" << flush; 12713206Sgabeblack@google.com} 12813206Sgabeblack@google.com 12913260Sgabeblack@google.cominline 13013206Sgabeblack@google.comuint8 DataBlock::getByte(int whichByte) const 13113206Sgabeblack@google.com{ 13213206Sgabeblack@google.com return m_data[whichByte]; 13313206Sgabeblack@google.com} 13413260Sgabeblack@google.com 13513260Sgabeblack@google.cominline 13613260Sgabeblack@google.comconst uint8* DataBlock::getData(int offset, int len) const 13713260Sgabeblack@google.com{ 13813260Sgabeblack@google.com assert(offset + len <= RubySystem::getBlockSizeBytes()); 13913260Sgabeblack@google.com return &m_data[offset]; 14013260Sgabeblack@google.com} 14113260Sgabeblack@google.com 14213260Sgabeblack@google.cominline 14313260Sgabeblack@google.comvoid DataBlock::setByte(int whichByte, uint8 data) 14413260Sgabeblack@google.com{ 14513260Sgabeblack@google.com m_data[whichByte] = data; 14613260Sgabeblack@google.com} 14713260Sgabeblack@google.com 14813260Sgabeblack@google.cominline 14913260Sgabeblack@google.comvoid DataBlock::setData(uint8* data, int offset, int len) 15013260Sgabeblack@google.com{ 15113260Sgabeblack@google.com assert(offset + len <= RubySystem::getBlockSizeBytes()); 15213260Sgabeblack@google.com memcpy(&m_data[offset], data, len); 15313260Sgabeblack@google.com} 15413260Sgabeblack@google.com 15513260Sgabeblack@google.cominline 15613260Sgabeblack@google.comvoid DataBlock::copyPartial(const DataBlock & dblk, int offset, int len) 15713206Sgabeblack@google.com{ 15813206Sgabeblack@google.com setData(&dblk.m_data[offset], offset, len); 15913207Sgabeblack@google.com} 16013206Sgabeblack@google.com 16113206Sgabeblack@google.com// ******************* Definitions ******************* 16213206Sgabeblack@google.com 16313206Sgabeblack@google.com// Output operator definition 16413206Sgabeblack@google.comextern inline 16513206Sgabeblack@google.comstd::ostream& operator<<(std::ostream& out, const DataBlock& obj) 16613206Sgabeblack@google.com{ 16713206Sgabeblack@google.com obj.print(out); 16813206Sgabeblack@google.com out << std::flush; 16913206Sgabeblack@google.com return out; 17013206Sgabeblack@google.com} 17113206Sgabeblack@google.com 17213206Sgabeblack@google.comextern inline 17313206Sgabeblack@google.combool operator==(const DataBlock& obj1,const DataBlock& obj2) 17413206Sgabeblack@google.com{ 17513207Sgabeblack@google.com return (obj1.equal(obj2)); 17613207Sgabeblack@google.com} 17713207Sgabeblack@google.com 17813207Sgabeblack@google.com#endif //DATABLOCK_H 17913207Sgabeblack@google.com