OLD | NEW |
| (Empty) |
1 //===- subzero/src/IceMemoryRegion.h - Memory region ------------*- C++ -*-===// | |
2 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
3 // for details. All rights reserved. Use of this source code is governed by a | |
4 // BSD-style license that can be found in the LICENSE file. | |
5 // | |
6 // Modified by the Subzero authors. | |
7 // | |
8 //===----------------------------------------------------------------------===// | |
9 // | |
10 // The Subzero Code Generator | |
11 // | |
12 // This file is distributed under the University of Illinois Open Source | |
13 // License. See LICENSE.TXT for details. | |
14 // | |
15 //===----------------------------------------------------------------------===// | |
16 // | |
17 // This file declares the MemoryRegion class. It tracks a pointer | |
18 // plus its bounds for bounds-checking in debug mode. | |
19 //===----------------------------------------------------------------------===// | |
20 | |
21 #ifndef SUBZERO_SRC_ICE_MEMORY_REGION_H_ | |
22 #define SUBZERO_SRC_ICE_MEMORY_REGION_H_ | |
23 | |
24 #include "IceDefs.h" | |
25 #include "IceUtils.h" | |
26 | |
27 namespace Ice { | |
28 | |
29 // Memory regions are useful for accessing memory with bounds check in | |
30 // debug mode. They can be safely passed by value and do not assume ownership | |
31 // of the region. | |
32 class MemoryRegion { | |
33 public: | |
34 MemoryRegion(const MemoryRegion &other) = default; | |
35 MemoryRegion &operator=(const MemoryRegion &other) = default; | |
36 MemoryRegion() : pointer_(NULL), size_(0) {} | |
37 MemoryRegion(void *pointer, size_t size) : pointer_(pointer), size_(size) {} | |
38 | |
39 void *pointer() const { return pointer_; } | |
40 size_t size() const { return size_; } | |
41 | |
42 size_t start() const { return reinterpret_cast<size_t>(pointer_); } | |
43 size_t end() const { return start() + size_; } | |
44 | |
45 template <typename T> T Load(size_t offset) const { | |
46 return *ComputeInternalPointer<T>(offset); | |
47 } | |
48 | |
49 template <typename T> void Store(size_t offset, T value) const { | |
50 *ComputeInternalPointer<T>(offset) = value; | |
51 } | |
52 | |
53 template <typename T> T *PointerTo(size_t offset) const { | |
54 return ComputeInternalPointer<T>(offset); | |
55 } | |
56 | |
57 bool Contains(size_t address) const { | |
58 return (address >= start()) && (address < end()); | |
59 } | |
60 | |
61 void CopyFrom(size_t offset, const MemoryRegion &from) const; | |
62 | |
63 // Compute a sub memory region based on an existing one. | |
64 void Subregion(const MemoryRegion &from, size_t offset, size_t size) { | |
65 assert(from.size() >= size); | |
66 assert(offset <= (from.size() - size)); | |
67 pointer_ = reinterpret_cast<void *>(from.start() + offset); | |
68 size_ = size; | |
69 } | |
70 | |
71 // Compute an extended memory region based on an existing one. | |
72 void Extend(const MemoryRegion ®ion, size_t extra) { | |
73 pointer_ = region.pointer(); | |
74 size_ = (region.size() + extra); | |
75 } | |
76 | |
77 private: | |
78 template <typename T> T *ComputeInternalPointer(size_t offset) const { | |
79 assert(size() >= sizeof(T)); | |
80 assert(offset <= size() - sizeof(T)); | |
81 return reinterpret_cast<T *>(start() + offset); | |
82 } | |
83 | |
84 void *pointer_; | |
85 size_t size_; | |
86 }; | |
87 | |
88 } // end of namespace Ice | |
89 | |
90 #endif // SUBZERO_SRC_ICE_MEMORY_REGION_H_ | |
OLD | NEW |