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