chunk_generator.hh revision 2632:1bb2f91485ea
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 start address so we can calculate offset in writing block. */
66    const Addr startAddr;
67    /** The maximum chunk size, e.g., the cache block size or page size. */
68    const int chunkSize;
69
70  public:
71    /**
72     * Constructor.
73     * @param startAddr The starting address of the region.
74     * @param totalSize The total size of the region.
75     * @param _chunkSize The size/alignment of chunks into which
76     *    the region should be decomposed.
77     */
78    ChunkGenerator(Addr _startAddr, int totalSize, int _chunkSize)
79        : startAddr(_startAddr), chunkSize(_chunkSize)
80    {
81        // chunkSize must be a power of two
82        assert(chunkSize == 0 || isPowerOf2(chunkSize));
83
84        // set up initial chunk.
85        curAddr = startAddr;
86
87        if (chunkSize == 0) //Special Case, if we see 0, assume no chuncking
88        {
89            nextAddr = startAddr + totalSize;
90        }
91        else
92        {
93            // nextAddr should be *next* chunk start
94            nextAddr = roundUp(startAddr, chunkSize);
95            if (curAddr == nextAddr) {
96                // ... even if startAddr is already chunk-aligned
97                nextAddr += chunkSize;
98            }
99        }
100
101        // how many bytes are left between curAddr and the end of this chunk?
102        int left_in_chunk = nextAddr - curAddr;
103        curSize = std::min(totalSize, left_in_chunk);
104        sizeLeft = totalSize - curSize;
105    }
106
107    /** Return starting address of current chunk. */
108    Addr addr() { return curAddr; }
109    /** Return size in bytes of current chunk. */
110    int  size() { return curSize; }
111
112    /** Number of bytes we have already chunked up. */
113    int complete() { return curAddr - startAddr; }
114    /**
115     * Are we done?  That is, did the last call to next() advance
116     * past the end of the region?
117     * @return True if yes, false if more to go.
118     */
119    bool done() { return (curSize == 0); }
120
121    /**
122     * Advance generator to next chunk.
123     * @return True if successful, false if unsuccessful
124     * (because we were at the last chunk).
125     */
126    bool next()
127    {
128        if (sizeLeft == 0) {
129            curSize = 0;
130            return false;
131        }
132
133        curAddr = nextAddr;
134        curSize = std::min(sizeLeft, chunkSize);
135        sizeLeft -= curSize;
136        nextAddr += curSize;
137        return true;
138    }
139};
140
141#endif // __BASE__CHUNK_GENERATOR_HH__
142