chunk_generator.hh revision 2373
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 "targetarch/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(isPowerOf2(chunkSize)); 81 82 // set up initial chunk. 83 curAddr = startAddr; 84 85 // nextAddr should be *next* chunk start 86 nextAddr = roundUp(startAddr, chunkSize); 87 if (curAddr == nextAddr) { 88 // ... even if startAddr is already chunk-aligned 89 nextAddr += chunkSize; 90 } 91 92 // how many bytes are left between curAddr and the end of this chunk? 93 int left_in_chunk = nextAddr - curAddr; 94 curSize = std::min(totalSize, left_in_chunk); 95 sizeLeft = totalSize - curSize; 96 } 97 98 /** Return starting address of current chunk. */ 99 Addr addr() { return curAddr; } 100 /** Return size in bytes of current chunk. */ 101 int size() { return curSize; } 102 103 /** 104 * Are we done? That is, did the last call to next() advance 105 * past the end of the region? 106 * @return True if yes, false if more to go. 107 */ 108 bool done() { return (curSize == 0); } 109 110 /** 111 * Advance generator to next chunk. 112 * @return True if successful, false if unsuccessful 113 * (because we were at the last chunk). 114 */ 115 bool next() 116 { 117 if (sizeLeft == 0) { 118 curSize = 0; 119 return false; 120 } 121 122 curAddr = nextAddr; 123 curSize = std::min(sizeLeft, chunkSize); 124 sizeLeft -= curSize; 125 nextAddr += curSize; 126 return true; 127 } 128}; 129 130#endif // __BASE__CHUNK_GENERATOR_HH__ 131