chunk_generator.hh revision 2982
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>
402373SN/A#include "base/intmath.hh"
412423SN/A#include "arch/isa_traits.hh" // for Addr
422373SN/A
432373SN/A/**
442373SN/A * This class takes an arbitrary memory region (address/length pair)
452373SN/A * and generates a series of appropriately (e.g. block- or page-)
462373SN/A * aligned chunks covering the same region.
472373SN/A *
482373SN/A * Example usage:
492373SN/A
502373SN/A\code
512373SN/A    for (ChunkGenerator gen(addr, size, chunkSize); !gen.done(); gen.next()) {
522373SN/A        doSomethingChunky(gen.addr(), gen.size());
532373SN/A    }
542373SN/A\endcode
552373SN/A */
562373SN/Aclass ChunkGenerator
572373SN/A{
582373SN/A  private:
592373SN/A    /** The starting address of the current chunk. */
602373SN/A    Addr curAddr;
612373SN/A    /** The starting address of the next chunk (after the current one). */
622373SN/A    Addr nextAddr;
632373SN/A    /** The size of the current chunk (in bytes). */
642373SN/A    int  curSize;
652373SN/A    /** The number of bytes remaining in the region after the current chunk. */
662373SN/A    int  sizeLeft;
672565SN/A    /** The start address so we can calculate offset in writing block. */
682565SN/A    const Addr startAddr;
692373SN/A    /** The maximum chunk size, e.g., the cache block size or page size. */
702373SN/A    const int chunkSize;
712373SN/A
722373SN/A  public:
732373SN/A    /**
742373SN/A     * Constructor.
752982Sstever@eecs.umich.edu     * @param _startAddr The starting address of the region.
762373SN/A     * @param totalSize The total size of the region.
772373SN/A     * @param _chunkSize The size/alignment of chunks into which
782373SN/A     *    the region should be decomposed.
792373SN/A     */
802565SN/A    ChunkGenerator(Addr _startAddr, int totalSize, int _chunkSize)
812565SN/A        : startAddr(_startAddr), chunkSize(_chunkSize)
822373SN/A    {
832373SN/A        // chunkSize must be a power of two
842418SN/A        assert(chunkSize == 0 || isPowerOf2(chunkSize));
852373SN/A
862373SN/A        // set up initial chunk.
872373SN/A        curAddr = startAddr;
882373SN/A
892415SN/A        if (chunkSize == 0) //Special Case, if we see 0, assume no chuncking
902415SN/A        {
912415SN/A            nextAddr = startAddr + totalSize;
922415SN/A        }
932415SN/A        else
942415SN/A        {
952415SN/A            // nextAddr should be *next* chunk start
962415SN/A            nextAddr = roundUp(startAddr, chunkSize);
972415SN/A            if (curAddr == nextAddr) {
982415SN/A                // ... even if startAddr is already chunk-aligned
992415SN/A                nextAddr += chunkSize;
1002415SN/A            }
1012373SN/A        }
1022373SN/A
1032373SN/A        // how many bytes are left between curAddr and the end of this chunk?
1042373SN/A        int left_in_chunk = nextAddr - curAddr;
1052373SN/A        curSize = std::min(totalSize, left_in_chunk);
1062373SN/A        sizeLeft = totalSize - curSize;
1072373SN/A    }
1082373SN/A
1092373SN/A    /** Return starting address of current chunk. */
1102373SN/A    Addr addr() { return curAddr; }
1112373SN/A    /** Return size in bytes of current chunk. */
1122373SN/A    int  size() { return curSize; }
1132373SN/A
1142565SN/A    /** Number of bytes we have already chunked up. */
1152565SN/A    int complete() { return curAddr - startAddr; }
1162373SN/A    /**
1172373SN/A     * Are we done?  That is, did the last call to next() advance
1182373SN/A     * past the end of the region?
1192373SN/A     * @return True if yes, false if more to go.
1202373SN/A     */
1212373SN/A    bool done() { return (curSize == 0); }
1222373SN/A
1232373SN/A    /**
1242373SN/A     * Advance generator to next chunk.
1252373SN/A     * @return True if successful, false if unsuccessful
1262373SN/A     * (because we were at the last chunk).
1272373SN/A     */
1282373SN/A    bool next()
1292373SN/A    {
1302373SN/A        if (sizeLeft == 0) {
1312373SN/A            curSize = 0;
1322373SN/A            return false;
1332373SN/A        }
1342373SN/A
1352373SN/A        curAddr = nextAddr;
1362373SN/A        curSize = std::min(sizeLeft, chunkSize);
1372373SN/A        sizeLeft -= curSize;
1382373SN/A        nextAddr += curSize;
1392373SN/A        return true;
1402373SN/A    }
1412373SN/A};
1422373SN/A
1432373SN/A#endif // __BASE__CHUNK_GENERATOR_HH__
144