gem5  v20.1.0.0
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 
39 #include "base/intmath.hh"
40 #include "base/types.hh"
41 
56 {
57  private:
63  unsigned curSize;
65  unsigned sizeLeft;
67  const Addr startAddr;
69  const unsigned chunkSize;
70 
71  public:
81  ChunkGenerator(Addr _startAddr, unsigned totalSize, unsigned _chunkSize)
82  : startAddr(_startAddr), chunkSize(_chunkSize)
83  {
84  // chunkSize must be a power of two
85  assert(chunkSize == 0 || isPowerOf2(chunkSize));
86 
87  // set up initial chunk.
89 
90  if (chunkSize == 0) //Special Case, if we see 0, assume no chuncking
91  {
92  nextAddr = startAddr + totalSize;
93  }
94  else
95  {
96  // nextAddr should be *next* chunk start
98  if (curAddr == nextAddr) {
99  // ... even if startAddr is already chunk-aligned
100  nextAddr += chunkSize;
101  }
102  }
103 
104  // how many bytes are left between curAddr and the end of this chunk?
105  unsigned left_in_chunk = nextAddr - curAddr;
106  curSize = std::min(totalSize, left_in_chunk);
107  sizeLeft = totalSize - curSize;
108  }
109 
115  Addr addr() const { return curAddr; }
121  unsigned size() const { return curSize; }
122 
128  unsigned complete() const { return curAddr - startAddr; }
129 
137  bool done() const { return (curSize == 0); }
138 
145  bool last() const { return (sizeLeft == 0); }
146 
154  bool
156  {
157  if (sizeLeft == 0) {
158  curSize = 0;
159  return false;
160  }
161 
162  curAddr = nextAddr;
163  curSize = std::min(sizeLeft, chunkSize);
164  sizeLeft -= curSize;
165  nextAddr += curSize;
166  return true;
167  }
168 };
169 
170 #endif // __BASE__CHUNK_GENERATOR_HH__
ChunkGenerator::chunkSize
const unsigned chunkSize
The maximum chunk size, e.g., the cache block size or page size.
Definition: chunk_generator.hh:69
ChunkGenerator::complete
unsigned complete() const
Number of bytes we have already chunked up.
Definition: chunk_generator.hh:128
ChunkGenerator::curAddr
Addr curAddr
The starting address of the current chunk.
Definition: chunk_generator.hh:59
ChunkGenerator::curSize
unsigned curSize
The size of the current chunk (in bytes).
Definition: chunk_generator.hh:63
ChunkGenerator::size
unsigned size() const
Return size in bytes of current chunk.
Definition: chunk_generator.hh:121
ChunkGenerator::addr
Addr addr() const
Return starting address of current chunk.
Definition: chunk_generator.hh:115
ChunkGenerator::sizeLeft
unsigned sizeLeft
The number of bytes remaining in the region after the current chunk.
Definition: chunk_generator.hh:65
ChunkGenerator::next
bool next()
Advance generator to next chunk.
Definition: chunk_generator.hh:155
Addr
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:142
ChunkGenerator::startAddr
const Addr startAddr
The start address so we can calculate offset in writing block.
Definition: chunk_generator.hh:67
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:137
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
This class takes an arbitrary memory region (address/length pair) and generates a series of appropria...
Definition: chunk_generator.hh:55
ChunkGenerator::nextAddr
Addr nextAddr
The starting address of the next chunk (after the current one).
Definition: chunk_generator.hh:61
intmath.hh
ChunkGenerator::last
bool last() const
Is this the last chunk?
Definition: chunk_generator.hh:145
isPowerOf2
bool isPowerOf2(const T &n)
Definition: intmath.hh:102
ChunkGenerator::ChunkGenerator
ChunkGenerator(Addr _startAddr, unsigned totalSize, unsigned _chunkSize)
Constructor.
Definition: chunk_generator.hh:81

Generated on Wed Sep 30 2020 14:02:07 for gem5 by doxygen 1.8.17