chunk_generator.hh revision 6227
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 * Authors: Steve Reinhardt 29 */ 30 31#ifndef __BASE__CHUNK_GENERATOR_HH__ 32#define __BASE__CHUNK_GENERATOR_HH__ 33 34/** 35 * @file 36 * Declaration and inline definition of ChunkGenerator object. 37 */ 38 39#include <algorithm> 40 41#include "base/intmath.hh" 42#include "base/types.hh" 43 44/** 45 * This class takes an arbitrary memory region (address/length pair) 46 * and generates a series of appropriately (e.g. block- or page-) 47 * aligned chunks covering the same region. 48 * 49 * Example usage: 50 51\code 52 for (ChunkGenerator gen(addr, size, chunkSize); !gen.done(); gen.next()) { 53 doSomethingChunky(gen.addr(), gen.size()); 54 } 55\endcode 56 */ 57class ChunkGenerator 58{ 59 private: 60 /** The starting address of the current chunk. */ 61 Addr curAddr; 62 /** The starting address of the next chunk (after the current one). */ 63 Addr nextAddr; 64 /** The size of the current chunk (in bytes). */ 65 unsigned curSize; 66 /** The number of bytes remaining in the region after the current chunk. */ 67 unsigned sizeLeft; 68 /** The start address so we can calculate offset in writing block. */ 69 const Addr startAddr; 70 /** The maximum chunk size, e.g., the cache block size or page size. */ 71 const unsigned chunkSize; 72 73 public: 74 /** 75 * Constructor. 76 * @param _startAddr The starting address of the region. 77 * @param totalSize The total size of the region. 78 * @param _chunkSize The size/alignment of chunks into which 79 * the region should be decomposed. 80 */ 81 ChunkGenerator(Addr _startAddr, unsigned totalSize, unsigned _chunkSize) 82 : startAddr(_startAddr), chunkSize(_chunkSize) 83 { 84 // chunkSize must be a power of two 85 assert(chunkSize == 0 || isPowerOf2(chunkSize)); 86 assert(totalSize >= 0); 87 88 // set up initial chunk. 89 curAddr = startAddr; 90 91 if (chunkSize == 0) //Special Case, if we see 0, assume no chuncking 92 { 93 nextAddr = startAddr + totalSize; 94 } 95 else 96 { 97 // nextAddr should be *next* chunk start 98 nextAddr = roundUp(startAddr, chunkSize); 99 if (curAddr == nextAddr) { 100 // ... even if startAddr is already chunk-aligned 101 nextAddr += chunkSize; 102 } 103 } 104 105 // how many bytes are left between curAddr and the end of this chunk? 106 unsigned left_in_chunk = nextAddr - curAddr; 107 curSize = std::min(totalSize, left_in_chunk); 108 sizeLeft = totalSize - curSize; 109 } 110 111 /** Return starting address of current chunk. */ 112 Addr addr() const { return curAddr; } 113 /** Return size in bytes of current chunk. */ 114 unsigned size() const { return curSize; } 115 116 /** Number of bytes we have already chunked up. */ 117 unsigned complete() const { return curAddr - startAddr; } 118 119 /** 120 * Are we done? That is, did the last call to next() advance 121 * past the end of the region? 122 * @return True if yes, false if more to go. 123 */ 124 bool done() const { return (curSize == 0); } 125 126 /** 127 * Advance generator to next chunk. 128 * @return True if successful, false if unsuccessful 129 * (because we were at the last chunk). 130 */ 131 bool 132 next() 133 { 134 if (sizeLeft == 0) { 135 curSize = 0; 136 return false; 137 } 138 139 curAddr = nextAddr; 140 curSize = std::min(sizeLeft, chunkSize); 141 sizeLeft -= curSize; 142 nextAddr += curSize; 143 return true; 144 } 145}; 146 147#endif // __BASE__CHUNK_GENERATOR_HH__ 148