Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame^] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
| 3 | #ifndef ART_SRC_MEMORY_REGION_H_ |
| 4 | #define ART_SRC_MEMORY_REGION_H_ |
| 5 | |
| 6 | #include <stdint.h> |
| 7 | #include "src/globals.h" |
| 8 | #include "src/logging.h" |
| 9 | #include "src/macros.h" |
| 10 | #include "src/memory_region.h" |
| 11 | |
| 12 | namespace android { |
| 13 | namespace runtime { |
| 14 | |
| 15 | // Memory regions are useful for accessing memory with bounds check in |
| 16 | // debug mode. They can be safely passed by value and do not assume ownership |
| 17 | // of the region. |
| 18 | class MemoryRegion { |
| 19 | public: |
| 20 | MemoryRegion() : pointer_(NULL), size_(0) {} |
| 21 | MemoryRegion(void* pointer, uword size) : pointer_(pointer), size_(size) {} |
| 22 | |
| 23 | void* pointer() const { return pointer_; } |
| 24 | size_t size() const { return size_; } |
| 25 | size_t size_in_bits() const { return size_ * kBitsPerByte; } |
| 26 | |
| 27 | static size_t pointer_offset() { |
| 28 | return OFFSETOF_MEMBER(MemoryRegion, pointer_); |
| 29 | } |
| 30 | |
| 31 | byte* start() const { return reinterpret_cast<byte*>(pointer_); } |
| 32 | byte* end() const { return start() + size_; } |
| 33 | |
| 34 | template<typename T> T Load(uintptr_t offset) const { |
| 35 | return *ComputeInternalPointer<T>(offset); |
| 36 | } |
| 37 | |
| 38 | template<typename T> void Store(uintptr_t offset, T value) const { |
| 39 | *ComputeInternalPointer<T>(offset) = value; |
| 40 | } |
| 41 | |
| 42 | template<typename T> T* PointerTo(uintptr_t offset) const { |
| 43 | return ComputeInternalPointer<T>(offset); |
| 44 | } |
| 45 | |
| 46 | void CopyFrom(size_t offset, const MemoryRegion& from) const; |
| 47 | |
| 48 | // Compute a sub memory region based on an existing one. |
| 49 | void Subregion(const MemoryRegion& from, uintptr_t offset, uintptr_t size) { |
| 50 | CHECK_GE(from.size(), size); |
| 51 | CHECK_LE(offset, from.size() - size); |
| 52 | pointer_ = reinterpret_cast<void*>(from.start() + offset); |
| 53 | size_ = size; |
| 54 | } |
| 55 | |
| 56 | // Compute an extended memory region based on an existing one. |
| 57 | void Extend(const MemoryRegion& region, uintptr_t extra) { |
| 58 | pointer_ = region.pointer(); |
| 59 | size_ = (region.size() + extra); |
| 60 | } |
| 61 | |
| 62 | private: |
| 63 | template<typename T> T* ComputeInternalPointer(size_t offset) const { |
| 64 | CHECK_GE(size(), sizeof(T)); |
| 65 | CHECK_LE(offset, size() - sizeof(T)); |
| 66 | return reinterpret_cast<T*>(start() + offset); |
| 67 | } |
| 68 | |
| 69 | // Locate the bit with the given offset. Returns a pointer to the byte |
| 70 | // containing the bit, and sets bit_mask to the bit within that byte. |
| 71 | byte* ComputeBitPointer(uintptr_t bit_offset, byte* bit_mask) const { |
| 72 | uintptr_t bit_remainder = (bit_offset & (kBitsPerByte - 1)); |
| 73 | *bit_mask = (1U << bit_remainder); |
| 74 | uintptr_t byte_offset = (bit_offset >> kBitsPerByteLog2); |
| 75 | return ComputeInternalPointer<byte>(byte_offset); |
| 76 | } |
| 77 | |
| 78 | void* pointer_; |
| 79 | size_t size_; |
| 80 | |
| 81 | DISALLOW_COPY_AND_ASSIGN(MemoryRegion); |
| 82 | }; |
| 83 | |
| 84 | } } // namespace android::runtime |
| 85 | |
| 86 | #endif // ART_MEMORY_REGION_H_ |