OpenASIP  2.0
PagedArray.hh
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.hh
26  *
27  * Declaration of PagedArray class.
28  *
29  * @author Pekka Jääskeläinen 2006 (pjaaskel-no.spam-cs.tut.fi)
30  * @note rating: red
31  */
32 
33 #ifndef TTA_PAGED_ARRAY_HH
34 #define TTA_PAGED_ARRAY_HH
35 
36 #include <vector>
37 #include <stdint.h>
38 
39 #include "BaseType.hh"
40 #include "Exception.hh"
41 
42 #define DEFAULT_PAGE_SIZE 1024
43 #define UNDEFINED_VALUE (0)
44 
45 /// Type used for indexing the array. Currently, limited to 32 bits.
46 typedef uint32_t IndexType;
47 
48 /**
49  * Models the data contained in array.
50  *
51  * The array space is divided into pages which are allocated on-demand,
52  * that is, the first time an index in that page is accessed for a write
53  * operation. Pages are stored in a table from which they are
54  * retrieved using (address / page_size) as index. The offset within the
55  * selected page is given by (address % chunk_size). This should result in
56  * O(1) access.
57  *
58  * This allows modeling large arrays without reserving the memory for the
59  * array if it's not accessed. The idea behind this implementation is borrowed
60  * from common (paged) virtual memory implementations of operating systems.
61  *
62  * Please note that this container does not perform any checking for the
63  * validity of the indices due to efficiency reasons.
64  */
65 template <
66  typename ValueType,
67  int PageSize=DEFAULT_PAGE_SIZE,
68  ValueType DefaultValue=static_cast<ValueType>(UNDEFINED_VALUE)>
69 class PagedArray {
70 public:
71 
72  /// Type for a set of values in a basic array type.
73  typedef ValueType* ValueTable;
74 
75  /// Type for a set of values in a std::vector container.
76  typedef std::vector<ValueType> ValueVector;
77 
78  PagedArray(std::size_t size);
79  virtual ~PagedArray();
80 
81  void write(IndexType index, const ValueTable data, std::size_t size);
82  void writeData(IndexType index, const ValueType& data);
83  ValueType readData(IndexType index);
84  void read(IndexType index, ValueVector& data, size_t size);
85  void read(IndexType index, ValueVector& data);
86  void read(IndexType index, ValueTable data, size_t size);
87  size_t allocatedMemory() const;
88  void clear();
89 
90 private:
91  void deletePages();
92 
93  /// Copying not allowed.
94  PagedArray(const PagedArray&);
95  /// Assignment not allowed.
96  PagedArray& operator=(const PagedArray&);
97 
98  /// Storage for the data pages.
99  /// Created pages are stored in table from which they are found
100  /// with address / size_of_page.
101  ValueType** pageTable_;
102  /// Size of the page table.
103  std::size_t pageTableSize_;
104 };
105 
106 #include "PagedArray.icc"
107 
108 #endif
BaseType.hh
PagedArray::pageTable_
ValueType ** pageTable_
Storage for the data pages. Created pages are stored in table from which they are found with address ...
Definition: PagedArray.hh:101
Exception.hh
PagedArray::ValueVector
std::vector< ValueType > ValueVector
Type for a set of values in a std::vector container.
Definition: PagedArray.hh:76
DEFAULT_PAGE_SIZE
#define DEFAULT_PAGE_SIZE
Definition: PagedArray.hh:42
IndexType
uint32_t IndexType
Type used for indexing the array. Currently, limited to 32 bits.
Definition: PagedArray.hh:46
PagedArray::pageTableSize_
std::size_t pageTableSize_
Size of the page table.
Definition: PagedArray.hh:103
PagedArray.icc
PagedArray
Definition: PagedArray.hh:69
UNDEFINED_VALUE
#define UNDEFINED_VALUE
Definition: PagedArray.hh:43
PagedArray::ValueTable
ValueType * ValueTable
Type for a set of values in a basic array type.
Definition: PagedArray.hh:73