Deleted Added
sdiff udiff text old ( 5731:453f320129a1 ) new ( 6227:a17798f2a52c )
full compact
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;

--- 23 unchanged lines hidden (view full) ---

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

--- 6 unchanged lines hidden (view full) ---

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 assert(totalSize >= 0);
86
87 // set up initial chunk.
88 curAddr = startAddr;

--- 8 unchanged lines hidden (view full) ---

97 nextAddr = roundUp(startAddr, chunkSize);
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 int left_in_chunk = nextAddr - curAddr;
106 curSize = std::min(totalSize, left_in_chunk);
107 sizeLeft = totalSize - curSize;
108 }
109
110 /** Return starting address of current chunk. */
111 Addr addr() { return curAddr; }
112 /** Return size in bytes of current chunk. */
113 int size() { return curSize; }
114
115 /** Number of bytes we have already chunked up. */
116 int complete() { return curAddr - startAddr; }
117 /**
118 * Are we done? That is, did the last call to next() advance
119 * past the end of the region?
120 * @return True if yes, false if more to go.
121 */
122 bool done() { return (curSize == 0); }
123
124 /**
125 * Advance generator to next chunk.
126 * @return True if successful, false if unsuccessful
127 * (because we were at the last chunk).
128 */
129 bool next()
130 {
131 if (sizeLeft == 0) {
132 curSize = 0;
133 return false;
134 }
135
136 curAddr = nextAddr;
137 curSize = std::min(sizeLeft, chunkSize);
138 sizeLeft -= curSize;
139 nextAddr += curSize;
140 return true;
141 }
142};
143
144#endif // __BASE__CHUNK_GENERATOR_HH__