Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 16 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_MEMORY_REGION_H_ |
| 18 | #define ART_RUNTIME_MEMORY_REGION_H_ |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 19 | |
| 20 | #include <stdint.h> |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 21 | |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 22 | #include "base/logging.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 23 | #include "base/macros.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 24 | #include "globals.h" |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 25 | |
Carl Shapiro | 6b6b5f0 | 2011-06-21 15:05:09 -0700 | [diff] [blame] | 26 | namespace art { |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 27 | |
| 28 | // Memory regions are useful for accessing memory with bounds check in |
| 29 | // debug mode. They can be safely passed by value and do not assume ownership |
| 30 | // of the region. |
| 31 | class MemoryRegion { |
| 32 | public: |
| 33 | MemoryRegion() : pointer_(NULL), size_(0) {} |
| 34 | MemoryRegion(void* pointer, uword size) : pointer_(pointer), size_(size) {} |
| 35 | |
| 36 | void* pointer() const { return pointer_; } |
| 37 | size_t size() const { return size_; } |
| 38 | size_t size_in_bits() const { return size_ * kBitsPerByte; } |
| 39 | |
| 40 | static size_t pointer_offset() { |
| 41 | return OFFSETOF_MEMBER(MemoryRegion, pointer_); |
| 42 | } |
| 43 | |
| 44 | byte* start() const { return reinterpret_cast<byte*>(pointer_); } |
| 45 | byte* end() const { return start() + size_; } |
| 46 | |
| 47 | template<typename T> T Load(uintptr_t offset) const { |
| 48 | return *ComputeInternalPointer<T>(offset); |
| 49 | } |
| 50 | |
| 51 | template<typename T> void Store(uintptr_t offset, T value) const { |
| 52 | *ComputeInternalPointer<T>(offset) = value; |
| 53 | } |
| 54 | |
| 55 | template<typename T> T* PointerTo(uintptr_t offset) const { |
| 56 | return ComputeInternalPointer<T>(offset); |
| 57 | } |
| 58 | |
| 59 | void CopyFrom(size_t offset, const MemoryRegion& from) const; |
| 60 | |
| 61 | // Compute a sub memory region based on an existing one. |
| 62 | void Subregion(const MemoryRegion& from, uintptr_t offset, uintptr_t size) { |
| 63 | CHECK_GE(from.size(), size); |
| 64 | CHECK_LE(offset, from.size() - size); |
| 65 | pointer_ = reinterpret_cast<void*>(from.start() + offset); |
| 66 | size_ = size; |
| 67 | } |
| 68 | |
| 69 | // Compute an extended memory region based on an existing one. |
| 70 | void Extend(const MemoryRegion& region, uintptr_t extra) { |
| 71 | pointer_ = region.pointer(); |
| 72 | size_ = (region.size() + extra); |
| 73 | } |
| 74 | |
| 75 | private: |
| 76 | template<typename T> T* ComputeInternalPointer(size_t offset) const { |
| 77 | CHECK_GE(size(), sizeof(T)); |
| 78 | CHECK_LE(offset, size() - sizeof(T)); |
| 79 | return reinterpret_cast<T*>(start() + offset); |
| 80 | } |
| 81 | |
| 82 | // Locate the bit with the given offset. Returns a pointer to the byte |
| 83 | // containing the bit, and sets bit_mask to the bit within that byte. |
| 84 | byte* ComputeBitPointer(uintptr_t bit_offset, byte* bit_mask) const { |
| 85 | uintptr_t bit_remainder = (bit_offset & (kBitsPerByte - 1)); |
| 86 | *bit_mask = (1U << bit_remainder); |
| 87 | uintptr_t byte_offset = (bit_offset >> kBitsPerByteLog2); |
| 88 | return ComputeInternalPointer<byte>(byte_offset); |
| 89 | } |
| 90 | |
| 91 | void* pointer_; |
| 92 | size_t size_; |
Brian Carlstrom | 4e777d4 | 2011-08-15 13:53:52 -0700 | [diff] [blame] | 93 | |
| 94 | DISALLOW_COPY_AND_ASSIGN(MemoryRegion); |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 95 | }; |
| 96 | |
Carl Shapiro | 6b6b5f0 | 2011-06-21 15:05:09 -0700 | [diff] [blame] | 97 | } // namespace art |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 98 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 99 | #endif // ART_RUNTIME_MEMORY_REGION_H_ |