chunk_generator.hh revision 2423
1/* 2 * Copyright (c) 2001-2005 The Regents of The University of Michigan 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer; 9 * redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution; 12 * neither the name of the copyright holders nor the names of its 13 * contributors may be used to endorse or promote products derived from 14 * this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29#ifndef __BASE__CHUNK_GENERATOR_HH__ 30#define __BASE__CHUNK_GENERATOR_HH__ 31 32/** 33 * @file 34 * Declaration and inline definition of ChunkGenerator object. 35 */ 36 37#include <algorithm> 38#include "base/intmath.hh" 39#include "arch/isa_traits.hh" // for Addr 40 41/** 42 * This class takes an arbitrary memory region (address/length pair) 43 * and generates a series of appropriately (e.g. block- or page-) 44 * aligned chunks covering the same region. 45 * 46 * Example usage: 47 48\code 49 for (ChunkGenerator gen(addr, size, chunkSize); !gen.done(); gen.next()) { 50 doSomethingChunky(gen.addr(), gen.size()); 51 } 52\endcode 53 */ 54class ChunkGenerator 55{ 56 private: 57 /** The starting address of the current chunk. */ 58 Addr curAddr; 59 /** The starting address of the next chunk (after the current one). */ 60 Addr nextAddr; 61 /** The size of the current chunk (in bytes). */ 62 int curSize; 63 /** The number of bytes remaining in the region after the current chunk. */ 64 int sizeLeft; 65 /** The maximum chunk size, e.g., the cache block size or page size. */ 66 const int chunkSize; 67 68 public: 69 /** 70 * Constructor. 71 * @param startAddr The starting address of the region. 72 * @param totalSize The total size of the region. 73 * @param _chunkSize The size/alignment of chunks into which 74 * the region should be decomposed. 75 */ 76 ChunkGenerator(Addr startAddr, int totalSize, int _chunkSize) 77 : chunkSize(_chunkSize) 78 { 79 // chunkSize must be a power of two 80 assert(chunkSize == 0 || isPowerOf2(chunkSize)); 81 82 // set up initial chunk. 83 curAddr = startAddr; 84 85 if (chunkSize == 0) //Special Case, if we see 0, assume no chuncking 86 { 87 nextAddr = startAddr + totalSize; 88 } 89 else 90 { 91 // nextAddr should be *next* chunk start 92 nextAddr = roundUp(startAddr, chunkSize); 93 if (curAddr == nextAddr) { 94 // ... even if startAddr is already chunk-aligned 95 nextAddr += chunkSize; 96 } 97 } 98 99 // how many bytes are left between curAddr and the end of this chunk? 100 int left_in_chunk = nextAddr - curAddr; 101 curSize = std::min(totalSize, left_in_chunk); 102 sizeLeft = totalSize - curSize; 103 } 104 105 /** Return starting address of current chunk. */ 106 Addr addr() { return curAddr; } 107 /** Return size in bytes of current chunk. */ 108 int size() { return curSize; } 109 110 /** 111 * Are we done? That is, did the last call to next() advance 112 * past the end of the region? 113 * @return True if yes, false if more to go. 114 */ 115 bool done() { return (curSize == 0); } 116 117 /** 118 * Advance generator to next chunk. 119 * @return True if successful, false if unsuccessful 120 * (because we were at the last chunk). 121 */ 122 bool next() 123 { 124 if (sizeLeft == 0) { 125 curSize = 0; 126 return false; 127 } 128 129 curAddr = nextAddr; 130 curSize = std::min(sizeLeft, chunkSize); 131 sizeLeft -= curSize; 132 nextAddr += curSize; 133 return true; 134 } 135}; 136 137#endif // __BASE__CHUNK_GENERATOR_HH__ 138