gem5  v21.0.0.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
chunk_generator.hh
Go to the documentation of this file.
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 
37 #include <algorithm>
38 #include <cassert>
39 
40 #include "base/intmath.hh"
41 #include "base/types.hh"
42 
57 {
58  private:
70  const Addr startAddr;
72  const Addr chunkSize;
73 
74  public:
84  ChunkGenerator(Addr _startAddr, Addr totalSize, Addr _chunkSize) :
85  startAddr(_startAddr), chunkSize(_chunkSize)
86  {
87  // chunkSize must be a power of two
88  assert(chunkSize == 0 || isPowerOf2(chunkSize));
89 
90  // set up initial chunk.
92 
93  if (chunkSize == 0) { // Special Case, if we see 0, assume no chunking.
94  nextAddr = startAddr + totalSize;
95  } else {
96  // nextAddr should be *next* chunk start.
98  if (curAddr == nextAddr) {
99  // ... even if startAddr is already chunk-aligned
100  nextAddr += chunkSize;
101  }
102  nextAddr = std::min(nextAddr, startAddr + totalSize);
103  }
104 
105  // How many bytes are left between curAddr and the end of this chunk?
107  sizeLeft = totalSize - curSize;
108  nextSize = std::min(sizeLeft, chunkSize);
109  }
110 
116  Addr addr() const { return curAddr; }
122  Addr size() const { return curSize; }
123 
129  Addr complete() const { return curAddr - startAddr; }
130 
138  bool done() const { return curSize == 0; }
139 
146  bool last() const { return sizeLeft == 0; }
147 
154  void
156  {
157  assert(next >= nextAddr);
158 
159  const Addr skipping = std::min(next - nextAddr, sizeLeft);
160 
161  sizeLeft -= skipping;
162  curSize += skipping;
163  nextAddr = next;
164 
165  assert(chunkSize);
166 
167  // nextSize will be enough to get to an alignment boundary,
169  // or if it's already aligned, to the following boundary or the end.
170  if (!nextSize)
171  nextSize = std::min(sizeLeft, chunkSize);
172  }
173 
181  bool
183  {
184  if (last()) {
185  curSize = 0;
186  return false;
187  }
188 
189  curAddr = nextAddr;
190  curSize = nextSize;
191  sizeLeft -= curSize;
192  nextAddr += curSize;
193  nextSize = std::min(sizeLeft, chunkSize);
194  return true;
195  }
196 };
197 
198 #endif // __BASE_CHUNK_GENERATOR_HH__
ChunkGenerator::complete
Addr complete() const
Number of bytes we have already chunked up.
Definition: chunk_generator.hh:129
ChunkGenerator::sizeLeft
Addr sizeLeft
The number of bytes remaining in the region after the current chunk.
Definition: chunk_generator.hh:68
ChunkGenerator::curAddr
Addr curAddr
The starting address of the current chunk.
Definition: chunk_generator.hh:60
ChunkGenerator::nextSize
Addr nextSize
The size of the next chunk (in bytes).
Definition: chunk_generator.hh:66
ChunkGenerator::setNext
void setNext(Addr next)
Grow this chunk to cover additional bytes which are already handled.
Definition: chunk_generator.hh:155
ChunkGenerator::chunkSize
const Addr chunkSize
The maximum chunk size, e.g., the cache block size or page size.
Definition: chunk_generator.hh:72
ChunkGenerator::addr
Addr addr() const
Return starting address of current chunk.
Definition: chunk_generator.hh:116
ChunkGenerator::next
bool next()
Advance generator to next chunk.
Definition: chunk_generator.hh:182
ChunkGenerator::curSize
Addr curSize
The size of the current chunk (in bytes).
Definition: chunk_generator.hh:64
Addr
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:148
ChunkGenerator::startAddr
const Addr startAddr
The start address so we can calculate offset in writing block.
Definition: chunk_generator.hh:70
ChunkGenerator::done
bool done() const
Are we done? That is, did the last call to next() advance past the end of the region?
Definition: chunk_generator.hh:138
types.hh
roundUp
T roundUp(const T &val, const U &align)
This function is used to align addresses in memory.
Definition: intmath.hh:131
ChunkGenerator::size
Addr size() const
Return size in bytes of current chunk.
Definition: chunk_generator.hh:122
ChunkGenerator
This class takes an arbitrary memory region (address/length pair) and generates a series of appropria...
Definition: chunk_generator.hh:56
ChunkGenerator::nextAddr
Addr nextAddr
The starting address of the next chunk (after the current one).
Definition: chunk_generator.hh:62
intmath.hh
ChunkGenerator::last
bool last() const
Is this the last chunk?
Definition: chunk_generator.hh:146
isPowerOf2
bool isPowerOf2(const T &n)
Definition: intmath.hh:102
ChunkGenerator::ChunkGenerator
ChunkGenerator(Addr _startAddr, Addr totalSize, Addr _chunkSize)
Constructor.
Definition: chunk_generator.hh:84

Generated on Tue Mar 23 2021 19:41:23 for gem5 by doxygen 1.8.17