2 Copyright (c) 2002-2009 Tampere University.
4 This file is part of TTA-Based Codesign Environment (TCE).
6 Permission is hereby granted, free of charge, to any person obtaining a
7 copy of this software and associated documentation files (the "Software"),
8 to deal in the Software without restriction, including without limitation
9 the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 and/or sell copies of the Software, and to permit persons to whom the
11 Software is furnished to do so, subject to the following conditions:
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 DEALINGS IN THE SOFTWARE.
25 * @file PagedArray.icc
27 * Template definitions of PagedArray class.
29 * @author Pekka Jääskeläinen 2006 (pekka.jaaskelainen-no.spam-tut.fi)
33 #include "Application.hh"
41 * Initializes the page table.
43 * @param size The count of elements in the array.
45 template <typename ValueType, int PageSize, ValueType DefaultValue>
46 PagedArray<ValueType, PageSize, DefaultValue>::PagedArray(std::size_t size) {
48 static_cast<std::size_t>(
49 std::ceil(static_cast<double>(size) / PageSize));
51 pageTable_ = new ValueType*[pageTableSize_];
52 for (std::size_t i = 0; i < pageTableSize_; ++i) {
61 * Deallocates tables and cleans up the page table.
63 template <typename ValueType, int PageSize, ValueType DefaultValue>
64 PagedArray<ValueType, PageSize, DefaultValue>::~PagedArray() {
72 * Deletes all allocated memory chunks, thus effectively sets all memory
75 template <typename ValueType, int PageSize, ValueType DefaultValue>
77 PagedArray<ValueType, PageSize, DefaultValue>::deletePages() {
78 for (std::size_t i = 0; i < pageTableSize_; ++i) {
79 if (pageTable_[i] != NULL) {
80 delete[] pageTable_[i];
87 * Reads data to a vector.
89 * If the number of values exceeds the size of the given container,
90 * then the container is resized.
92 * @param index Index to be read from.
93 * @param data Container where read data is stored.
94 * @param size Number of minimum addressable units to read.
96 template <typename ValueType, int PageSize, ValueType DefaultValue>
98 PagedArray<ValueType, PageSize, DefaultValue>::read(
103 if (data.size() != size) {
107 for (size_t i = index; i < index + size; i++) {
108 data[i - index] = readData(i);
113 * Returns the amount of allocated memory by the array pages.
115 * This is used for debugging the implementation.
117 * @return The amount of allocated memory in bytes.
119 template <typename ValueType, int PageSize, ValueType DefaultValue>
121 PagedArray<ValueType, PageSize, DefaultValue>::allocatedMemory() const {
123 size_t countOfChunks = 0;
125 for (std::size_t i = 0; i < sizeof(pageTable_); ++i) {
126 if (pageTable_[i] != NULL)
129 return countOfChunks*PageSize*sizeof(ValueType);
133 * Fills the whole array with the default value.
135 * This is needed due to some buggy simulated programs which expect
136 * uninitialized data to be zero.
138 template <typename ValueType, int PageSize, ValueType DefaultValue>
140 PagedArray<ValueType, PageSize, DefaultValue>::clear() {
146 * Stores data to the array.
148 * Does not perform bounds-checking.
150 * @param index The index of the data.
151 * @param data Data to be stored in a traditional table.
152 * @param size Size of the data to store.
154 template <typename ValueType, int PageSize, ValueType DefaultValue>
156 PagedArray<ValueType, PageSize, DefaultValue>::write(
160 for (size_t i = index; i < index + size; ++i) {
161 writeData(i, data[i - index]);
166 * Writes a value to the given index.
168 * @param index Target index.
169 * @param data Data to write.
171 template <typename ValueType, int PageSize, ValueType DefaultValue>
173 PagedArray<ValueType, PageSize, DefaultValue>::writeData(
175 const ValueType& data) {
177 ValueType* page = pageTable_[index / PageSize];
179 page = new ValueType[PageSize];
180 std::memset(page, 0, PageSize*sizeof(ValueType));
181 pageTable_[index / PageSize] = page;
183 page[index % PageSize] = data;
187 * Reads a value from a given index.
189 * If the page to be read is not yet allocated, doesn't allocate it,
190 * but returns the default value instead. This way we avoid memory consumption
191 * explosion for example in simulator/debugger when user wants to browse
192 * through the memory space (which is mostly uninitialized at least in
193 * sequential code) in the memory window.
195 * @param index Target index.
198 template <typename ValueType, int PageSize, ValueType DefaultValue>
200 PagedArray<ValueType, PageSize, DefaultValue>::readData(IndexType index) {
201 ValueType* page = pageTable_[index / PageSize];
205 return page[index % PageSize];
209 * Reads data to an array.
211 * A more efficient version.
213 * @param index Index to read from.
214 * @param data Pointer to array in which the data should stored. Must have
216 * @param size Size of chunk to read.
218 template <typename ValueType, int PageSize, ValueType DefaultValue>
220 PagedArray<ValueType, PageSize, DefaultValue>::read(
224 for (size_t i = index; i < index + size; i++) {
225 data[i - index] = readData(i);
231 * Reads data to a vector.
233 * The number of minimum addressable units that are read is equal to the
234 * size of the container.
236 * @param address Memory address to be read from.
237 * @param data Container where read data is stored.
239 template <typename ValueType, int PageSize, ValueType DefaultValue>
241 PagedArray<ValueType, PageSize, DefaultValue>::read(
244 read(address, data, data.size());