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