OpenASIP 2.2
Loading...
Searching...
No Matches
PagedArray.icc
Go to the documentation of this file.
1/*
2 Copyright (c) 2002-2009 Tampere University.
3
4 This file is part of TTA-Based Codesign Environment (TCE).
5
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:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
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.
23 */
24/**
25 * @file PagedArray.icc
26 *
27 * Template definitions of PagedArray class.
28 *
29 * @author Pekka Jääskeläinen 2006 (pekka.jaaskelainen-no.spam-tut.fi)
30 * @note rating: red
31 */
32
33#include "Application.hh"
34
35#include <cmath>
36#include <cstring>
37
38/**
39 * Constructor.
40 *
41 * Initializes the page table.
42 *
43 * @param size The count of elements in the array.
44 */
45template <typename ValueType, int PageSize, ValueType DefaultValue>
46PagedArray<ValueType, PageSize, DefaultValue>::PagedArray(std::size_t size) {
47 pageTableSize_ =
48 static_cast<std::size_t>(
49 std::ceil(static_cast<double>(size) / PageSize));
50
51 pageTable_ = new ValueType*[pageTableSize_];
52 for (std::size_t i = 0; i < pageTableSize_; ++i) {
53 pageTable_[i] = NULL;
54 }
55}
56
57
58/**
59 * Destructor.
60 *
61 * Deallocates tables and cleans up the page table.
62 */
63template <typename ValueType, int PageSize, ValueType DefaultValue>
64PagedArray<ValueType, PageSize, DefaultValue>::~PagedArray() {
65 deletePages();
66 delete[] pageTable_;
67 pageTable_ = NULL;
68}
69
70
71/**
72 * Deletes all allocated memory chunks, thus effectively sets all memory
73 * locations to zero.
74 */
75template <typename ValueType, int PageSize, ValueType DefaultValue>
76void
77PagedArray<ValueType, PageSize, DefaultValue>::deletePages() {
78 for (std::size_t i = 0; i < pageTableSize_; ++i) {
79 if (pageTable_[i] != NULL) {
80 delete[] pageTable_[i];
81 pageTable_[i] = NULL;
82 }
83 }
84}
85
86/**
87 * Reads data to a vector.
88 *
89 * If the number of values exceeds the size of the given container,
90 * then the container is resized.
91 *
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.
95 */
96template <typename ValueType, int PageSize, ValueType DefaultValue>
97void
98PagedArray<ValueType, PageSize, DefaultValue>::read(
99 IndexType index,
100 ValueVector& data,
101 std::size_t size) {
102
103 if (data.size() != size) {
104 data.resize(size);
105 }
106
107 for (size_t i = index; i < index + size; i++) {
108 data[i - index] = readData(i);
109 }
110}
111
112/**
113 * Returns the amount of allocated memory by the array pages.
114 *
115 * This is used for debugging the implementation.
116 *
117 * @return The amount of allocated memory in bytes.
118 */
119template <typename ValueType, int PageSize, ValueType DefaultValue>
120size_t
121PagedArray<ValueType, PageSize, DefaultValue>::allocatedMemory() const {
122
123 size_t countOfChunks = 0;
124
125 for (std::size_t i = 0; i < sizeof(pageTable_); ++i) {
126 if (pageTable_[i] != NULL)
127 ++countOfChunks;
128 }
129 return countOfChunks*PageSize*sizeof(ValueType);
130}
131
132/**
133 * Fills the whole array with the default value.
134 *
135 * This is needed due to some buggy simulated programs which expect
136 * uninitialized data to be zero.
137 */
138template <typename ValueType, int PageSize, ValueType DefaultValue>
139void
140PagedArray<ValueType, PageSize, DefaultValue>::clear() {
141 deletePages();
142}
143
144
145/**
146 * Stores data to the array.
147 *
148 * Does not perform bounds-checking.
149 *
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.
153 */
154template <typename ValueType, int PageSize, ValueType DefaultValue>
155inline void
156PagedArray<ValueType, PageSize, DefaultValue>::write(
157 IndexType index,
158 ValueTable data,
159 std::size_t size) {
160 for (size_t i = index; i < index + size; ++i) {
161 writeData(i, data[i - index]);
162 }
163}
164
165/**
166 * Writes a value to the given index.
167 *
168 * @param index Target index.
169 * @param data Data to write.
170 */
171template <typename ValueType, int PageSize, ValueType DefaultValue>
172inline void
173PagedArray<ValueType, PageSize, DefaultValue>::writeData(
174 IndexType index,
175 const ValueType& data) {
176
177 ValueType* page = pageTable_[index / PageSize];
178 if (page == NULL) {
179 page = new ValueType[PageSize];
180 std::memset(page, 0, PageSize*sizeof(ValueType));
181 pageTable_[index / PageSize] = page;
182 }
183 page[index % PageSize] = data;
184}
185
186/**
187 * Reads a value from a given index.
188 *
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.
194 *
195 * @param index Target index.
196 * @return data
197 */
198template <typename ValueType, int PageSize, ValueType DefaultValue>
199inline ValueType
200PagedArray<ValueType, PageSize, DefaultValue>::readData(IndexType index) {
201 ValueType* page = pageTable_[index / PageSize];
202 if (page == NULL) {
203 return DefaultValue;
204 }
205 return page[index % PageSize];
206}
207
208/**
209 * Reads data to an array.
210 *
211 * A more efficient version.
212 *
213 * @param index Index to read from.
214 * @param data Pointer to array in which the data should stored. Must have
215 * enough space!
216 * @param size Size of chunk to read.
217 */
218template <typename ValueType, int PageSize, ValueType DefaultValue>
219inline void
220PagedArray<ValueType, PageSize, DefaultValue>::read(
221 IndexType index,
222 ValueTable data,
223 std::size_t size) {
224 for (size_t i = index; i < index + size; i++) {
225 data[i - index] = readData(i);
226 }
227}
228
229
230/**
231 * Reads data to a vector.
232 *
233 * The number of minimum addressable units that are read is equal to the
234 * size of the container.
235 *
236 * @param address Memory address to be read from.
237 * @param data Container where read data is stored.
238 */
239template <typename ValueType, int PageSize, ValueType DefaultValue>
240inline void
241PagedArray<ValueType, PageSize, DefaultValue>::read(
242 Word address,
243 ValueVector& data) {
244 read(address, data, data.size());
245}