misc: Replace enable_if<>::type with enable_if_t<>.
[gem5.git] / src / sim / proxy_ptr.hh
1 /*
2 * Copyright (c) 2004-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;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #ifndef __SIM_PROXY_PTR_HH__
30 #define __SIM_PROXY_PTR_HH__
31
32 #include <cstdint>
33 #include <memory>
34 #include <type_traits>
35
36 #include "base/logging.hh"
37 #include "base/types.hh"
38 #include "sim/guest_abi.hh"
39
40 template <typename Proxy>
41 class ProxyPtrBuffer
42 {
43 private:
44 std::shared_ptr<Proxy> proxy;
45
46 Addr ptr;
47 size_t size;
48 std::unique_ptr<uint8_t[]> data;
49
50 bool dirty = false;
51
52 void markClean() { dirty = false; }
53
54 public:
55
56 std::shared_ptr<Proxy> getProxy() const { return proxy; }
57
58 void markDirty() { dirty = true; }
59 bool isDirty() { return dirty; }
60
61 template <typename T>
62 T &
63 as()
64 {
65 assert(sizeof(T) <= size);
66 markDirty();
67 return *reinterpret_cast<T *>(data.get());
68 }
69
70 template <typename T>
71 const T &
72 asConst() const
73 {
74 assert(sizeof(T) <= size);
75 return *reinterpret_cast<T *>(data.get());
76 }
77
78 void
79 flush(bool force=false)
80 {
81 if (force || isDirty()) {
82 proxy->writeBlob(ptr, data.get(), size);
83 markClean();
84 }
85 }
86
87 void
88 load()
89 {
90 panic_if(isDirty(), "Overwriting dirty ProxyPtr.");
91 proxy->readBlob(ptr, data.get(), size);
92 }
93
94 Addr addr() const { return ptr; }
95
96 ProxyPtrBuffer(std::shared_ptr<Proxy> _proxy, Addr _ptr, size_t _size) :
97 proxy(_proxy), ptr(_ptr), size(_size), data(new uint8_t[_size])
98 {
99 load();
100 }
101
102 ~ProxyPtrBuffer() { flush(); }
103 };
104
105 template <typename T, typename Proxy>
106 class ConstProxyPtr
107 {
108 protected:
109 std::shared_ptr<Proxy> proxy;
110 std::shared_ptr<ProxyPtrBuffer<Proxy>> buffer;
111
112 template <typename O, typename P>
113 friend class ProxyPtr;
114
115 void nullCheck() const { panic_if(!buffer, "Accessing null ProxyPtr."); }
116
117 void
118 setAddr(Addr ptr)
119 {
120 if (ptr)
121 buffer.reset(new ProxyPtrBuffer<Proxy>(proxy, ptr, sizeof(T)));
122 else
123 buffer.reset((ProxyPtrBuffer<Proxy> *)nullptr);
124 }
125
126 ConstProxyPtr(Addr _ptr, std::shared_ptr<Proxy> _proxy) : proxy(_proxy)
127 {
128 setAddr(_ptr);
129 }
130
131 using CPP = ConstProxyPtr<T, Proxy>;
132
133 public:
134 using Type = T;
135
136 template <typename ...Args,
137 typename std::enable_if_t<std::is_constructible<
138 Proxy, Args&&...>::value, int> = 0>
139 explicit ConstProxyPtr(Addr _ptr, Args&&... args) :
140 proxy(std::make_shared<Proxy>(args...))
141 {
142 setAddr(_ptr);
143 }
144 template <typename ...Args,
145 typename std::enable_if_t<std::is_constructible<
146 Proxy, Args&&...>::value, int> = 0>
147 explicit ConstProxyPtr(Args&&... args) :
148 proxy(std::make_shared<Proxy>(args...))
149 {
150 setAddr(0);
151 }
152
153 template <typename O, typename Enabled=
154 typename std::enable_if_t<std::is_assignable<T *, O *>::value>>
155 ConstProxyPtr(const ConstProxyPtr<O, Proxy> &other) :
156 proxy(other.proxy), buffer(other.buffer)
157 {}
158
159 ConstProxyPtr(const CPP &other) :
160 proxy(other.proxy), buffer(other.buffer)
161 {}
162
163 void
164 load()
165 {
166 nullCheck();
167 buffer->load();
168 }
169
170 Addr addr() const { return buffer ? buffer->addr() : 0; }
171 operator bool() const { return (bool)buffer; }
172
173 template <typename A>
174 typename std::enable_if_t<std::is_integral<A>::value, CPP>
175 operator + (A a) const
176 {
177 return CPP(addr() + a * sizeof(T), proxy);
178 }
179
180 template <typename A>
181 typename std::enable_if_t<std::is_integral<A>::value, CPP>
182 operator - (A a) const
183 {
184 return CPP(addr() - a * sizeof(T), proxy);
185 }
186
187 ptrdiff_t
188 operator - (const CPP &other) const
189 {
190 return (addr() - other.addr()) / sizeof(T);
191 }
192
193 CPP &
194 operator = (const CPP &other)
195 {
196 proxy = other.proxy;
197 buffer = other.buffer;
198 return *this;
199 }
200
201 CPP &
202 operator = (const Addr &a)
203 {
204 setAddr(a);
205 return *this;
206 }
207
208 operator const T *() const
209 {
210 return buffer ? &buffer->template asConst<T>() : nullptr;
211 }
212
213 const T &
214 operator *() const
215 {
216 nullCheck();
217 return buffer->template asConst<T>();
218 }
219 const T *
220 operator ->() const
221 {
222 nullCheck();
223 return &buffer->template asConst<T>();
224 }
225 };
226
227 template <typename T, typename Proxy, typename A>
228 typename std::enable_if_t<std::is_integral<A>::value, ConstProxyPtr<T, Proxy>>
229 operator + (A a, const ConstProxyPtr<T, Proxy> &other)
230 {
231 return other + a;
232 }
233
234 template <typename T, typename Proxy>
235 class ProxyPtr : public ConstProxyPtr<T, Proxy>
236 {
237 protected:
238 using CPP = ConstProxyPtr<T, Proxy>;
239 using PP = ProxyPtr<T, Proxy>;
240
241 ProxyPtr(Addr _ptr, std::shared_ptr<Proxy> _proxy) : CPP(_ptr, _proxy) {}
242
243 public:
244 template <typename ...Args,
245 typename std::enable_if_t<std::is_constructible<
246 Proxy, Args&&...>::value, int> = 0>
247 explicit ProxyPtr(Addr _ptr, Args&&... args) : CPP(_ptr, args...) {}
248 template <typename ...Args,
249 typename std::enable_if_t<std::is_constructible<
250 Proxy, Args&&...>::value, int> = 0>
251 explicit ProxyPtr(Args&&... args) : CPP(0, args...) {}
252
253 template <typename O, typename Enabled=
254 typename std::enable_if_t<std::is_assignable<T *, O *>::value>>
255 ProxyPtr(const ProxyPtr<O, Proxy> &other) : CPP(other) {}
256
257 ProxyPtr(const PP &other) : CPP(other) {}
258 operator bool() const { return (bool)this->buffer; }
259
260 void
261 flush(bool force=false)
262 {
263 this->nullCheck();
264 this->buffer->flush(force);
265 }
266
267 template <typename A>
268 typename std::enable_if_t<std::is_integral<A>::value, PP>
269 operator + (A a) const
270 {
271 return PP(this->addr() + a * sizeof(T), this->proxy);
272 }
273
274 template <typename A>
275 typename std::enable_if_t<std::is_integral<A>::value, PP>
276 operator - (A a) const
277 {
278 return PP(this->addr() - a * sizeof(T), this->proxy);
279 }
280
281 ptrdiff_t
282 operator - (const PP &other) const
283 {
284 return (this->addr() - other.addr()) / sizeof(T);
285 }
286
287 PP &
288 operator = (const PP &other)
289 {
290 this->proxy = other.proxy;
291 this->buffer = other.buffer;
292 return *this;
293 }
294
295 PP &
296 operator = (const Addr &a)
297 {
298 this->setAddr(a);
299 return *this;
300 }
301
302 using CPP::operator const T *;
303 operator T *() const
304 {
305 return this->buffer ? &this->buffer->template as<T>() : nullptr;
306 }
307
308 using CPP::operator *;
309 T &
310 operator *() const
311 {
312 this->nullCheck();
313 return this->buffer->template as<T>();
314 }
315
316 using CPP::operator ->;
317 T *
318 operator ->() const
319 {
320 this->nullCheck();
321 return &this->buffer->template as<T>();
322 }
323 };
324
325 template <typename T, typename Proxy, typename A>
326 typename std::enable_if_t<std::is_integral<A>::value, ProxyPtr<T, Proxy>>
327 operator + (A a, const ProxyPtr<T, Proxy> &other)
328 {
329 return other + a;
330 }
331
332 namespace GuestABI
333 {
334
335 template <typename ABI, typename T, typename Proxy>
336 struct Argument<ABI, ProxyPtr<T, Proxy>>
337 {
338 static ProxyPtr<T, Proxy>
339 get(ThreadContext *tc, typename ABI::State &state)
340 {
341 return ProxyPtr<T, Proxy>(Argument<ABI, Addr>::get(tc, state), tc);
342 }
343 };
344
345 template <typename ABI, typename T, typename Proxy>
346 struct Argument<ABI, ConstProxyPtr<T, Proxy>>
347 {
348 static ConstProxyPtr<T, Proxy>
349 get(ThreadContext *tc, typename ABI::State &state)
350 {
351 return ConstProxyPtr<T, Proxy>(
352 Argument<ABI, Addr>::get(tc, state), tc);
353 }
354 };
355
356 } // namespace GuestABI
357
358 template <typename T, typename Proxy>
359 std::ostream &
360 operator << (std::ostream &os, const ConstProxyPtr<T, Proxy> &vptr)
361 {
362 ccprintf(os, "%#x", vptr.addr());
363 return os;
364 }
365
366 class SETranslatingPortProxy;
367
368 template <typename T>
369 using ConstVPtr = ConstProxyPtr<T, SETranslatingPortProxy>;
370 template <typename T>
371 using VPtr = ProxyPtr<T, SETranslatingPortProxy>;
372
373 #endif // __SIM_PROXY_PTR_HH__