DataBlock.hh revision 8090
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
327002Snate@binkert.org#include <iomanip>
337002Snate@binkert.org#include <iostream>
347002Snate@binkert.org
356154Snate@binkert.org#include "mem/ruby/common/Global.hh"
366145Snate@binkert.org
377039Snate@binkert.orgclass DataBlock
387039Snate@binkert.org{
397039Snate@binkert.org  public:
407039Snate@binkert.org    DataBlock()
417039Snate@binkert.org    {
427039Snate@binkert.org        alloc();
437039Snate@binkert.org    }
446145Snate@binkert.org
458090Snilay@cs.wisc.edu    DataBlock(const DataBlock &cp);
466285Snate@binkert.org
477039Snate@binkert.org    ~DataBlock()
487039Snate@binkert.org    {
497039Snate@binkert.org        if (m_alloc)
507039Snate@binkert.org            delete [] m_data;
517039Snate@binkert.org    }
526145Snate@binkert.org
537039Snate@binkert.org    DataBlock& operator=(const DataBlock& obj);
546285Snate@binkert.org
557039Snate@binkert.org    void assign(uint8* data);
566145Snate@binkert.org
577039Snate@binkert.org    void clear();
587039Snate@binkert.org    uint8 getByte(int whichByte) const;
597039Snate@binkert.org    const uint8* getData(int offset, int len) const;
607039Snate@binkert.org    void setByte(int whichByte, uint8 data);
617039Snate@binkert.org    void setData(uint8* data, int offset, int len);
627039Snate@binkert.org    void copyPartial(const DataBlock & dblk, int offset, int len);
637039Snate@binkert.org    bool equal(const DataBlock& obj) const;
647039Snate@binkert.org    void print(std::ostream& out) const;
657039Snate@binkert.org
667039Snate@binkert.org  private:
677039Snate@binkert.org    void alloc();
687039Snate@binkert.org    uint8* m_data;
697039Snate@binkert.org    bool m_alloc;
706145Snate@binkert.org};
716145Snate@binkert.org
727039Snate@binkert.orginline void
737039Snate@binkert.orgDataBlock::assign(uint8* data)
746285Snate@binkert.org{
757039Snate@binkert.org    if (m_alloc) {
767039Snate@binkert.org        delete [] m_data;
777039Snate@binkert.org    }
787039Snate@binkert.org    m_data = data;
797039Snate@binkert.org    m_alloc = false;
806285Snate@binkert.org}
816285Snate@binkert.org
827039Snate@binkert.orginline uint8
837039Snate@binkert.orgDataBlock::getByte(int whichByte) const
846285Snate@binkert.org{
857039Snate@binkert.org    return m_data[whichByte];
866285Snate@binkert.org}
876285Snate@binkert.org
887039Snate@binkert.orginline void
897039Snate@binkert.orgDataBlock::setByte(int whichByte, uint8 data)
906285Snate@binkert.org{
916285Snate@binkert.org    m_data[whichByte] = data;
926285Snate@binkert.org}
936285Snate@binkert.org
947039Snate@binkert.orginline void
957039Snate@binkert.orgDataBlock::copyPartial(const DataBlock & dblk, int offset, int len)
966285Snate@binkert.org{
977039Snate@binkert.org    setData(&dblk.m_data[offset], offset, len);
986285Snate@binkert.org}
996145Snate@binkert.org
1007039Snate@binkert.orginline std::ostream&
1017039Snate@binkert.orgoperator<<(std::ostream& out, const DataBlock& obj)
1026145Snate@binkert.org{
1037039Snate@binkert.org    obj.print(out);
1047039Snate@binkert.org    out << std::flush;
1057039Snate@binkert.org    return out;
1066145Snate@binkert.org}
1076145Snate@binkert.org
1087039Snate@binkert.orginline bool
1097039Snate@binkert.orgoperator==(const DataBlock& obj1,const DataBlock& obj2)
1106145Snate@binkert.org{
1117039Snate@binkert.org    return obj1.equal(obj2);
1126145Snate@binkert.org}
1136145Snate@binkert.org
1147039Snate@binkert.org#endif // __MEM_RUBY_COMMON_DATABLOCK_HH__
115