gem5  v20.0.0.2
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 
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:
79  ChunkGenerator(Addr _startAddr, unsigned totalSize, unsigned _chunkSize)
80  : startAddr(_startAddr), chunkSize(_chunkSize)
81  {
82  // chunkSize must be a power of two
83  assert(chunkSize == 0 || isPowerOf2(chunkSize));
84 
85  // set up initial chunk.
86  curAddr = startAddr;
87 
88  if (chunkSize == 0) //Special Case, if we see 0, assume no chuncking
89  {
90  nextAddr = startAddr + totalSize;
91  }
92  else
93  {
94  // nextAddr should be *next* chunk start
95  nextAddr = roundUp(startAddr, chunkSize);
96  if (curAddr == nextAddr) {
97  // ... even if startAddr is already chunk-aligned
98  nextAddr += chunkSize;
99  }
100  }
101 
102  // how many bytes are left between curAddr and the end of this chunk?
103  unsigned left_in_chunk = nextAddr - curAddr;
104  curSize = std::min(totalSize, left_in_chunk);
105  sizeLeft = totalSize - curSize;
106  }
107 
109  Addr addr() const { return curAddr; }
111  unsigned size() const { return curSize; }
112 
114  unsigned complete() const { return curAddr - startAddr; }
115 
121  bool done() const { return (curSize == 0); }
122 
127  bool last() const { return (sizeLeft == 0); }
128 
134  bool
136  {
137  if (sizeLeft == 0) {
138  curSize = 0;
139  return false;
140  }
141 
142  curAddr = nextAddr;
143  curSize = std::min(sizeLeft, chunkSize);
144  sizeLeft -= curSize;
145  nextAddr += curSize;
146  return true;
147  }
148 };
149 
150 #endif // __BASE__CHUNK_GENERATOR_HH__
bool next()
Advance generator to next chunk.
Addr curAddr
The starting address of the current chunk.
bool last() const
Is this the last chunk?
T roundUp(const T &val, const U &align)
This function is used to align addresses in memory.
Definition: intmath.hh:114
unsigned curSize
The size of the current chunk (in bytes).
unsigned sizeLeft
The number of bytes remaining in the region after the current chunk.
unsigned size() const
Return size in bytes of current chunk.
This class takes an arbitrary memory region (address/length pair) and generates a series of appropria...
ChunkGenerator(Addr _startAddr, unsigned totalSize, unsigned _chunkSize)
Constructor.
const Addr startAddr
The start address so we can calculate offset in writing block.
bool isPowerOf2(const T &n)
Definition: intmath.hh:90
Defines global host-dependent types: Counter, Tick, and (indirectly) {int,uint}{8,16,32,64}_t.
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:140
bool done() const
Are we done? That is, did the last call to next() advance past the end of the region?
Addr nextAddr
The starting address of the next chunk (after the current one).
unsigned complete() const
Number of bytes we have already chunked up.
const unsigned chunkSize
The maximum chunk size, e.g., the cache block size or page size.
Addr addr() const
Return starting address of current chunk.

Generated on Mon Jun 8 2020 15:45:07 for gem5 by doxygen 1.8.13