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 */ 16 17 #ifndef ART_RUNTIME_MEMORY_REGION_H_ 18 #define ART_RUNTIME_MEMORY_REGION_H_ 19 20 #include <stdint.h> 21 22 #include "base/logging.h" 23 #include "base/macros.h" 24 #include "globals.h" 25 26 namespace art { 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: MemoryRegion()33 MemoryRegion() : pointer_(NULL), size_(0) {} MemoryRegion(void * pointer,uword size)34 MemoryRegion(void* pointer, uword size) : pointer_(pointer), size_(size) {} 35 pointer()36 void* pointer() const { return pointer_; } size()37 size_t size() const { return size_; } size_in_bits()38 size_t size_in_bits() const { return size_ * kBitsPerByte; } 39 pointer_offset()40 static size_t pointer_offset() { 41 return OFFSETOF_MEMBER(MemoryRegion, pointer_); 42 } 43 start()44 byte* start() const { return reinterpret_cast<byte*>(pointer_); } end()45 byte* end() const { return start() + size_; } 46 Load(uintptr_t offset)47 template<typename T> T Load(uintptr_t offset) const { 48 return *ComputeInternalPointer<T>(offset); 49 } 50 Store(uintptr_t offset,T value)51 template<typename T> void Store(uintptr_t offset, T value) const { 52 *ComputeInternalPointer<T>(offset) = value; 53 } 54 PointerTo(uintptr_t offset)55 template<typename T> T* PointerTo(uintptr_t offset) const { 56 return ComputeInternalPointer<T>(offset); 57 } 58 59 // Load a single bit in the region. The bit at offset 0 is the least 60 // significant bit in the first byte. LoadBit(uintptr_t bit_offset)61 bool LoadBit(uintptr_t bit_offset) const { 62 uint8_t bit_mask; 63 uint8_t byte = *ComputeBitPointer(bit_offset, &bit_mask); 64 return byte & bit_mask; 65 } 66 StoreBit(uintptr_t bit_offset,bool value)67 void StoreBit(uintptr_t bit_offset, bool value) const { 68 uint8_t bit_mask; 69 uint8_t* byte = ComputeBitPointer(bit_offset, &bit_mask); 70 if (value) { 71 *byte |= bit_mask; 72 } else { 73 *byte &= ~bit_mask; 74 } 75 } 76 77 void CopyFrom(size_t offset, const MemoryRegion& from) const; 78 79 // Compute a sub memory region based on an existing one. Subregion(uintptr_t offset,uintptr_t size)80 MemoryRegion Subregion(uintptr_t offset, uintptr_t size) const { 81 CHECK_GE(this->size(), size); 82 CHECK_LE(offset, this->size() - size); 83 return MemoryRegion(reinterpret_cast<void*>(start() + offset), size); 84 } 85 86 // Compute an extended memory region based on an existing one. Extend(const MemoryRegion & region,uintptr_t extra)87 void Extend(const MemoryRegion& region, uintptr_t extra) { 88 pointer_ = region.pointer(); 89 size_ = (region.size() + extra); 90 } 91 92 private: ComputeInternalPointer(size_t offset)93 template<typename T> T* ComputeInternalPointer(size_t offset) const { 94 CHECK_GE(size(), sizeof(T)); 95 CHECK_LE(offset, size() - sizeof(T)); 96 return reinterpret_cast<T*>(start() + offset); 97 } 98 99 // Locate the bit with the given offset. Returns a pointer to the byte 100 // containing the bit, and sets bit_mask to the bit within that byte. ComputeBitPointer(uintptr_t bit_offset,byte * bit_mask)101 byte* ComputeBitPointer(uintptr_t bit_offset, byte* bit_mask) const { 102 uintptr_t bit_remainder = (bit_offset & (kBitsPerByte - 1)); 103 *bit_mask = (1U << bit_remainder); 104 uintptr_t byte_offset = (bit_offset >> kBitsPerByteLog2); 105 return ComputeInternalPointer<byte>(byte_offset); 106 } 107 108 void* pointer_; 109 size_t size_; 110 }; 111 112 } // namespace art 113 114 #endif // ART_RUNTIME_MEMORY_REGION_H_ 115