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));
862373SN/A
872373SN/A        // set up initial chunk.
882373SN/A        curAddr = startAddr;
892373SN/A
902415SN/A        if (chunkSize == 0) //Special Case, if we see 0, assume no chuncking
912415SN/A        {
922415SN/A            nextAddr = startAddr + totalSize;
932415SN/A        }
942415SN/A        else
952415SN/A        {
962415SN/A            // nextAddr should be *next* chunk start
972415SN/A            nextAddr = roundUp(startAddr, chunkSize);
982415SN/A            if (curAddr == nextAddr) {
992415SN/A                // ... even if startAddr is already chunk-aligned
1002415SN/A                nextAddr += chunkSize;
1012415SN/A            }
1022373SN/A        }
1032373SN/A
1042373SN/A        // how many bytes are left between curAddr and the end of this chunk?
1056227Snate@binkert.org        unsigned left_in_chunk = nextAddr - curAddr;
1062373SN/A        curSize = std::min(totalSize, left_in_chunk);
1072373SN/A        sizeLeft = totalSize - curSize;
1082373SN/A    }
1092373SN/A
1102373SN/A    /** Return starting address of current chunk. */
1116227Snate@binkert.org    Addr addr() const { return curAddr; }
1122373SN/A    /** Return size in bytes of current chunk. */
1136227Snate@binkert.org    unsigned size() const { return curSize; }
1142373SN/A
1152565SN/A    /** Number of bytes we have already chunked up. */
1166227Snate@binkert.org    unsigned complete() const { return curAddr - startAddr; }
1176227Snate@binkert.org
1182373SN/A    /**
1192373SN/A     * Are we done?  That is, did the last call to next() advance
1202373SN/A     * past the end of the region?
1212373SN/A     * @return True if yes, false if more to go.
1222373SN/A     */
1236227Snate@binkert.org    bool done() const { return (curSize == 0); }
1242373SN/A
1252373SN/A    /**
12610792Sbrandon.potter@amd.com     * Is this the last chunk?
12710792Sbrandon.potter@amd.com     * @return True if yes, false if more to go.
12810792Sbrandon.potter@amd.com     */
12910792Sbrandon.potter@amd.com    bool last() const { return (sizeLeft == 0); }
13010792Sbrandon.potter@amd.com
13110792Sbrandon.potter@amd.com    /**
1322373SN/A     * Advance generator to next chunk.
1332373SN/A     * @return True if successful, false if unsuccessful
1342373SN/A     * (because we were at the last chunk).
1352373SN/A     */
1366227Snate@binkert.org    bool
1376227Snate@binkert.org    next()
1382373SN/A    {
1392373SN/A        if (sizeLeft == 0) {
1402373SN/A            curSize = 0;
1412373SN/A            return false;
1422373SN/A        }
1432373SN/A
1442373SN/A        curAddr = nextAddr;
1452373SN/A        curSize = std::min(sizeLeft, chunkSize);
1462373SN/A        sizeLeft -= curSize;
1472373SN/A        nextAddr += curSize;
1482373SN/A        return true;
1492373SN/A    }
1502373SN/A};
1512373SN/A
1522373SN/A#endif // __BASE__CHUNK_GENERATOR_HH__
153