chunk_generator.hh revision 6227
12373SN/A/* 22373SN/A * Copyright (c) 2001-2005 The Regents of The University of Michigan 32373SN/A * All rights reserved. 42373SN/A * 52373SN/A * Redistribution and use in source and binary forms, with or without 62373SN/A * modification, are permitted provided that the following conditions are 72373SN/A * met: redistributions of source code must retain the above copyright 82373SN/A * notice, this list of conditions and the following disclaimer; 92373SN/A * redistributions in binary form must reproduce the above copyright 102373SN/A * notice, this list of conditions and the following disclaimer in the 112373SN/A * documentation and/or other materials provided with the distribution; 122373SN/A * neither the name of the copyright holders nor the names of its 132373SN/A * contributors may be used to endorse or promote products derived from 142373SN/A * this software without specific prior written permission. 152373SN/A * 162373SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 172373SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 182373SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 192373SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 202373SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 212373SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 222373SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 232373SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 242373SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 252373SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 262373SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 272665Ssaidi@eecs.umich.edu * 282665Ssaidi@eecs.umich.edu * Authors: Steve Reinhardt 292373SN/A */ 302373SN/A 312373SN/A#ifndef __BASE__CHUNK_GENERATOR_HH__ 322373SN/A#define __BASE__CHUNK_GENERATOR_HH__ 332373SN/A 342373SN/A/** 352373SN/A * @file 362373SN/A * Declaration and inline definition of ChunkGenerator object. 372373SN/A */ 382373SN/A 392373SN/A#include <algorithm> 406227Snate@binkert.org 412373SN/A#include "base/intmath.hh" 426227Snate@binkert.org#include "base/types.hh" 432373SN/A 442373SN/A/** 452373SN/A * This class takes an arbitrary memory region (address/length pair) 462373SN/A * and generates a series of appropriately (e.g. block- or page-) 472373SN/A * aligned chunks covering the same region. 482373SN/A * 492373SN/A * Example usage: 502373SN/A 512373SN/A\code 522373SN/A for (ChunkGenerator gen(addr, size, chunkSize); !gen.done(); gen.next()) { 532373SN/A doSomethingChunky(gen.addr(), gen.size()); 542373SN/A } 552373SN/A\endcode 562373SN/A */ 572373SN/Aclass ChunkGenerator 582373SN/A{ 592373SN/A private: 602373SN/A /** The starting address of the current chunk. */ 612373SN/A Addr curAddr; 622373SN/A /** The starting address of the next chunk (after the current one). */ 632373SN/A Addr nextAddr; 642373SN/A /** The size of the current chunk (in bytes). */ 656227Snate@binkert.org unsigned curSize; 662373SN/A /** The number of bytes remaining in the region after the current chunk. */ 676227Snate@binkert.org unsigned sizeLeft; 682565SN/A /** The start address so we can calculate offset in writing block. */ 692565SN/A const Addr startAddr; 702373SN/A /** The maximum chunk size, e.g., the cache block size or page size. */ 716227Snate@binkert.org const unsigned chunkSize; 722373SN/A 732373SN/A public: 742373SN/A /** 752373SN/A * Constructor. 762982Sstever@eecs.umich.edu * @param _startAddr The starting address of the region. 772373SN/A * @param totalSize The total size of the region. 782373SN/A * @param _chunkSize The size/alignment of chunks into which 792373SN/A * the region should be decomposed. 802373SN/A */ 816227Snate@binkert.org ChunkGenerator(Addr _startAddr, unsigned totalSize, unsigned _chunkSize) 822565SN/A : startAddr(_startAddr), chunkSize(_chunkSize) 832373SN/A { 842373SN/A // chunkSize must be a power of two 852418SN/A assert(chunkSize == 0 || isPowerOf2(chunkSize)); 865731SSteve.Reinhardt@amd.com assert(totalSize >= 0); 872373SN/A 882373SN/A // set up initial chunk. 892373SN/A curAddr = startAddr; 902373SN/A 912415SN/A if (chunkSize == 0) //Special Case, if we see 0, assume no chuncking 922415SN/A { 932415SN/A nextAddr = startAddr + totalSize; 942415SN/A } 952415SN/A else 962415SN/A { 972415SN/A // nextAddr should be *next* chunk start 982415SN/A nextAddr = roundUp(startAddr, chunkSize); 992415SN/A if (curAddr == nextAddr) { 1002415SN/A // ... even if startAddr is already chunk-aligned 1012415SN/A nextAddr += chunkSize; 1022415SN/A } 1032373SN/A } 1042373SN/A 1052373SN/A // how many bytes are left between curAddr and the end of this chunk? 1066227Snate@binkert.org unsigned left_in_chunk = nextAddr - curAddr; 1072373SN/A curSize = std::min(totalSize, left_in_chunk); 1082373SN/A sizeLeft = totalSize - curSize; 1092373SN/A } 1102373SN/A 1112373SN/A /** Return starting address of current chunk. */ 1126227Snate@binkert.org Addr addr() const { return curAddr; } 1132373SN/A /** Return size in bytes of current chunk. */ 1146227Snate@binkert.org unsigned size() const { return curSize; } 1152373SN/A 1162565SN/A /** Number of bytes we have already chunked up. */ 1176227Snate@binkert.org unsigned complete() const { return curAddr - startAddr; } 1186227Snate@binkert.org 1192373SN/A /** 1202373SN/A * Are we done? That is, did the last call to next() advance 1212373SN/A * past the end of the region? 1222373SN/A * @return True if yes, false if more to go. 1232373SN/A */ 1246227Snate@binkert.org bool done() const { return (curSize == 0); } 1252373SN/A 1262373SN/A /** 1272373SN/A * Advance generator to next chunk. 1282373SN/A * @return True if successful, false if unsuccessful 1292373SN/A * (because we were at the last chunk). 1302373SN/A */ 1316227Snate@binkert.org bool 1326227Snate@binkert.org next() 1332373SN/A { 1342373SN/A if (sizeLeft == 0) { 1352373SN/A curSize = 0; 1362373SN/A return false; 1372373SN/A } 1382373SN/A 1392373SN/A curAddr = nextAddr; 1402373SN/A curSize = std::min(sizeLeft, chunkSize); 1412373SN/A sizeLeft -= curSize; 1422373SN/A nextAddr += curSize; 1432373SN/A return true; 1442373SN/A } 1452373SN/A}; 1462373SN/A 1472373SN/A#endif // __BASE__CHUNK_GENERATOR_HH__ 148