DataBlock.hh revision 9181
16145Snate@binkert.org/* 26145Snate@binkert.org * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood 36145Snate@binkert.org * All rights reserved. 46145Snate@binkert.org * 56145Snate@binkert.org * Redistribution and use in source and binary forms, with or without 66145Snate@binkert.org * modification, are permitted provided that the following conditions are 76145Snate@binkert.org * met: redistributions of source code must retain the above copyright 86145Snate@binkert.org * notice, this list of conditions and the following disclaimer; 96145Snate@binkert.org * redistributions in binary form must reproduce the above copyright 106145Snate@binkert.org * notice, this list of conditions and the following disclaimer in the 116145Snate@binkert.org * documentation and/or other materials provided with the distribution; 126145Snate@binkert.org * neither the name of the copyright holders nor the names of its 136145Snate@binkert.org * contributors may be used to endorse or promote products derived from 146145Snate@binkert.org * this software without specific prior written permission. 156145Snate@binkert.org * 166145Snate@binkert.org * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 176145Snate@binkert.org * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 186145Snate@binkert.org * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 196145Snate@binkert.org * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 206145Snate@binkert.org * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 216145Snate@binkert.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 226145Snate@binkert.org * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 236145Snate@binkert.org * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 246145Snate@binkert.org * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 256145Snate@binkert.org * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 266145Snate@binkert.org * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 276145Snate@binkert.org */ 286145Snate@binkert.org 297039Snate@binkert.org#ifndef __MEM_RUBY_COMMON_DATABLOCK_HH__ 307039Snate@binkert.org#define __MEM_RUBY_COMMON_DATABLOCK_HH__ 316145Snate@binkert.org 329181Spower.jg@gmail.com#include <cassert> 337002Snate@binkert.org#include <iomanip> 347002Snate@binkert.org#include <iostream> 357002Snate@binkert.org 368608Snilay@cs.wisc.edu#include "mem/ruby/common/TypeDefines.hh" 376145Snate@binkert.org 387039Snate@binkert.orgclass DataBlock 397039Snate@binkert.org{ 407039Snate@binkert.org public: 417039Snate@binkert.org DataBlock() 427039Snate@binkert.org { 437039Snate@binkert.org alloc(); 447039Snate@binkert.org } 456145Snate@binkert.org 468090Snilay@cs.wisc.edu DataBlock(const DataBlock &cp); 476285Snate@binkert.org 487039Snate@binkert.org ~DataBlock() 497039Snate@binkert.org { 507039Snate@binkert.org if (m_alloc) 517039Snate@binkert.org delete [] m_data; 527039Snate@binkert.org } 536145Snate@binkert.org 547039Snate@binkert.org DataBlock& operator=(const DataBlock& obj); 556285Snate@binkert.org 567039Snate@binkert.org void assign(uint8* data); 576145Snate@binkert.org 587039Snate@binkert.org void clear(); 597039Snate@binkert.org uint8 getByte(int whichByte) const; 607039Snate@binkert.org const uint8* getData(int offset, int len) const; 617039Snate@binkert.org void setByte(int whichByte, uint8 data); 627039Snate@binkert.org void setData(uint8* data, int offset, int len); 637039Snate@binkert.org void copyPartial(const DataBlock & dblk, int offset, int len); 647039Snate@binkert.org bool equal(const DataBlock& obj) const; 657039Snate@binkert.org void print(std::ostream& out) const; 667039Snate@binkert.org 677039Snate@binkert.org private: 687039Snate@binkert.org void alloc(); 697039Snate@binkert.org uint8* m_data; 707039Snate@binkert.org bool m_alloc; 716145Snate@binkert.org}; 726145Snate@binkert.org 737039Snate@binkert.orginline void 747039Snate@binkert.orgDataBlock::assign(uint8* data) 756285Snate@binkert.org{ 769181Spower.jg@gmail.com assert(data != NULL); 777039Snate@binkert.org if (m_alloc) { 787039Snate@binkert.org delete [] m_data; 797039Snate@binkert.org } 807039Snate@binkert.org m_data = data; 817039Snate@binkert.org m_alloc = false; 826285Snate@binkert.org} 836285Snate@binkert.org 847039Snate@binkert.orginline uint8 857039Snate@binkert.orgDataBlock::getByte(int whichByte) const 866285Snate@binkert.org{ 877039Snate@binkert.org return m_data[whichByte]; 886285Snate@binkert.org} 896285Snate@binkert.org 907039Snate@binkert.orginline void 917039Snate@binkert.orgDataBlock::setByte(int whichByte, uint8 data) 926285Snate@binkert.org{ 936285Snate@binkert.org m_data[whichByte] = data; 946285Snate@binkert.org} 956285Snate@binkert.org 967039Snate@binkert.orginline void 977039Snate@binkert.orgDataBlock::copyPartial(const DataBlock & dblk, int offset, int len) 986285Snate@binkert.org{ 997039Snate@binkert.org setData(&dblk.m_data[offset], offset, len); 1006285Snate@binkert.org} 1016145Snate@binkert.org 1027039Snate@binkert.orginline std::ostream& 1037039Snate@binkert.orgoperator<<(std::ostream& out, const DataBlock& obj) 1046145Snate@binkert.org{ 1057039Snate@binkert.org obj.print(out); 1067039Snate@binkert.org out << std::flush; 1077039Snate@binkert.org return out; 1086145Snate@binkert.org} 1096145Snate@binkert.org 1107039Snate@binkert.orginline bool 1117039Snate@binkert.orgoperator==(const DataBlock& obj1,const DataBlock& obj2) 1126145Snate@binkert.org{ 1137039Snate@binkert.org return obj1.equal(obj2); 1146145Snate@binkert.org} 1156145Snate@binkert.org 1167039Snate@binkert.org#endif // __MEM_RUBY_COMMON_DATABLOCK_HH__ 117