P4C
The P4 Compiler
Loading...
Searching...
No Matches
alloc.h
1
19#ifndef BACKENDS_TOFINO_BF_P4C_COMMON_ALLOC_H_
20#define BACKENDS_TOFINO_BF_P4C_COMMON_ALLOC_H_
21
22#include <stdlib.h>
23
24#include <stdexcept>
25#include <utility>
26
27namespace BFN {
28
29template <class T>
31 int size_;
32 T *data;
33 Alloc1Dbase() = delete;
34 Alloc1Dbase(const Alloc1Dbase &) = delete;
35 Alloc1Dbase &operator=(const Alloc1Dbase &) = delete;
36 Alloc1Dbase &operator=(Alloc1Dbase &&) = delete;
37
38 public:
39 explicit Alloc1Dbase(int sz) : size_(sz) { data = sz ? new T[sz]{} : nullptr; }
40 Alloc1Dbase(Alloc1Dbase &&a) noexcept : size_(a.size_), data(a.data) { a.data = 0; }
41 virtual ~Alloc1Dbase() { delete[] data; }
42
43 typedef T *iterator;
44 typedef T *const_iterator;
45 T &operator[](int i) {
46 if (i < 0 || i >= size_) throw std::out_of_range("Alloc1D");
47 return data[i];
48 }
49 const T &operator[](int i) const {
50 if (i < 0 || i >= size_) throw std::out_of_range("Alloc1D");
51 return data[i];
52 }
53 bool operator==(const Alloc1Dbase<T> &t) const {
54 return std::equal(data, data + size_, t.data, t.data + t.size_);
55 }
56 bool operator!=(const Alloc1Dbase<T> &t) const { return !(*this == t); }
57
58 int size() const { return size_; }
59 void clear() { std::fill(data, data + size_, T()); }
60 T *begin() { return data; }
61 T *end() { return data + size_; }
62};
63
64template <class T, int S>
65class Alloc1D : public Alloc1Dbase<T> {
66 public:
67 Alloc1D() : Alloc1Dbase<T>(S) {}
68 Alloc1Dbase<T> &base() { return *this; }
69 bool operator!=(const Alloc1D<T, S> &t) const { return Alloc1Dbase<T>::operator!=(t); }
70};
71
72template <class T>
73class Alloc3Dbase;
74
75template <class T>
77 int nrows, ncols;
78 T *data;
79 template <class U>
80 class rowref {
81 U *row;
82 int ncols;
83 friend class Alloc2Dbase;
84 friend class Alloc3Dbase<U>;
85 rowref(U *r, int c) : row(r), ncols(c) {}
86
87 public:
88 typedef U *iterator;
89 typedef const U *const_iterator;
90 U &operator[](int i) const {
91 if (i < 0 || i >= ncols) throw std::out_of_range("Alloc2D");
92 return row[i];
93 }
94 U *begin() const { return row; }
95 U *end() const { return row + ncols; }
96 };
97 Alloc2Dbase() = delete;
98 Alloc2Dbase(const Alloc2Dbase &) = delete;
99 Alloc2Dbase &operator=(const Alloc2Dbase &) = delete;
100 Alloc2Dbase &operator=(Alloc2Dbase &&) = delete;
101 friend class Alloc3Dbase<T>;
102
103 public:
104 Alloc2Dbase(int r, int c) : nrows(r), ncols(c) {
105 size_t sz = r * c;
106 data = sz ? new T[sz]{} : nullptr;
107 }
108 Alloc2Dbase(Alloc2Dbase &&a) noexcept : nrows(a.nrows), ncols(a.ncols), data(a.data) {
109 a.data = 0;
110 }
111 virtual ~Alloc2Dbase() { delete[] data; }
112
113 rowref<T> operator[](int i) {
114 if (i < 0 || i >= nrows) throw std::out_of_range("Alloc2D");
115 return {data + i * ncols, ncols};
116 }
117 rowref<const T> operator[](int i) const {
118 if (i < 0 || i >= nrows) throw std::out_of_range("Alloc2D");
119 return {data + i * ncols, ncols};
120 }
121 T &at(int i, int j) {
122 if (i < 0 || i >= nrows || j < 0 || j >= ncols) throw std::out_of_range("Alloc2D");
123 return data[i * ncols + j];
124 }
125 const T &at(int i, int j) const {
126 if (i < 0 || i >= nrows || j < 0 || j >= ncols) throw std::out_of_range("Alloc2D");
127 return data[i * ncols + j];
128 }
129 T &operator[](std::pair<int, int> i) {
130 if (i.first < 0 || i.first >= nrows || i.second < 0 || i.second >= ncols)
131 throw std::out_of_range("Alloc2D");
132 return data[i.first * ncols + i.second];
133 }
134 const T &operator[](std::pair<int, int> i) const {
135 if (i.first < 0 || i.first >= nrows || i.second < 0 || i.second >= ncols)
136 throw std::out_of_range("Alloc2D");
137 return data[i.first * ncols + i.second];
138 }
139 bool operator==(const Alloc2Dbase<T> &t) const {
140 int sz = nrows * ncols;
141 if (nrows != t.nrows || ncols != t.ncols) return false;
142 return std::equal(data, data + sz, t.data);
143 }
144 bool operator!=(const Alloc2Dbase<T> &t) const { return !(*this == t); }
145
146 int rows() const { return nrows; }
147 int cols() const { return ncols; }
148 void clear() { std::fill(data, data + nrows * ncols, T()); }
149};
150
151template <class T, int R, int C>
152class Alloc2D : public Alloc2Dbase<T> {
153 public:
154 Alloc2D() : Alloc2Dbase<T>(R, C) {}
155 Alloc2Dbase<T> &base() { return *this; }
156};
157
158template <class T>
160 int nmats, nrows, ncols;
161 T *data;
162 template <class U>
163 class matref {
164 U *matrix;
165 int nrows, ncols;
166 friend class Alloc3Dbase;
167
168 public:
169 typename Alloc2Dbase<T>::template rowref<U> operator[](int i) const {
170 if (i < 0 || i >= nrows) throw std::out_of_range("Alloc3D");
171 return {matrix + i * ncols, ncols};
172 }
173 U &operator[](std::pair<int, int> i) const {
174 if (i.first < 0 || i.first >= nrows || i.second < 0 || i.second >= ncols)
175 throw std::out_of_range("Alloc3D");
176 return matrix[i.first * ncols + i.second];
177 }
178 };
179 Alloc3Dbase() = delete;
180 Alloc3Dbase(const Alloc3Dbase &) = delete;
181 Alloc3Dbase &operator=(const Alloc3Dbase &) = delete;
182 Alloc3Dbase &operator=(Alloc3Dbase &&) = delete;
183
184 public:
185 Alloc3Dbase(int m, int r, int c) : nmats(m), nrows(r), ncols(c) {
186 size_t sz = m * r * c;
187 data = sz ? new T[sz]{} : nullptr;
188 }
189 Alloc3Dbase(Alloc3Dbase &&a) noexcept
190 : nmats(a.nmats), nrows(a.nrows), ncols(a.ncols), data(a.data) {
191 a.data = 0;
192 }
193 virtual ~Alloc3Dbase() { delete[] data; }
194
195 matref<T> operator[](int i) {
196 if (i < 0 || i >= nmats) throw std::out_of_range("Alloc3D");
197 return {data + i * nrows * ncols, nrows, ncols};
198 }
199 matref<const T> operator[](int i) const {
200 if (i < 0 || i >= nmats) throw std::out_of_range("Alloc3D");
201 return {data + i * nrows * ncols, nrows, ncols};
202 }
203 T &at(int i, int j, int k) {
204 if (i < 0 || i >= nmats || j < 0 || j >= nrows || k < 0 || k >= ncols)
205 throw std::out_of_range("Alloc3D");
206 return data[i * nrows * ncols + j * ncols + k];
207 }
208 const T &at(int i, int j, int k) const {
209 if (i < 0 || i >= nmats || j < 0 || j >= nrows || k < 0 || k >= ncols)
210 throw std::out_of_range("Alloc3D");
211 return data[i * nrows * ncols + j * ncols + k];
212 }
213 T &operator[](std::tuple<int, int, int> i) {
214 if (std::get<0>(i) < 0 || std::get<0>(i) >= nmats || std::get<1>(i) < 0 ||
215 std::get<1>(i) >= nrows || std::get<2>(i) < 0 || std::get<2>(i) >= ncols)
216 throw std::out_of_range("Alloc3D");
217 return data[std::get<0>(i) * nrows * ncols + std::get<1>(i) * ncols + std::get<2>(i)];
218 }
219 const T &operator[](std::tuple<int, int, int> i) const {
220 if (std::get<0>(i) < 0 || std::get<0>(i) >= nmats || std::get<1>(i) < 0 ||
221 std::get<1>(i) >= nrows || std::get<2>(i) < 0 || std::get<2>(i) >= ncols)
222 throw std::out_of_range("Alloc3D");
223 return data[std::get<0>(i) * nrows * ncols + std::get<1>(i) * ncols + std::get<2>(i)];
224 }
225 bool operator==(const Alloc3Dbase<T> &t) const {
226 int sz = nmats * nrows * ncols;
227 if (nmats != t.nmats || nrows != t.nrows || ncols != t.ncols) return false;
228 return std::equal(data, data + sz, t.data);
229 }
230 bool operator!=(const Alloc3Dbase<T> &t) const { return !(*this == t); }
231
232 int matrixes() const { return nmats; }
233 int rows() const { return nrows; }
234 int cols() const { return ncols; }
235 void clear() { std::fill(data, data + nmats * nrows * ncols, T()); }
236};
237
238template <class T, int B, int R, int C>
239class Alloc3D : public Alloc3Dbase<T> {
240 public:
241 Alloc3D() : Alloc3Dbase<T>(B, R, C) {}
242 Alloc3Dbase<T> &base() { return *this; }
243};
244
245} // namespace BFN
246
247#endif /* BACKENDS_TOFINO_BF_P4C_COMMON_ALLOC_H_ */
Definition alloc.h:65
Definition alloc.h:30
Definition alloc.h:152
Definition alloc.h:76
Definition alloc.h:239
Definition alloc.h:159
The namespace encapsulating Barefoot/Intel-specific stuff.
Definition add_t2na_meta.cpp:21