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" |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 24 | #include "base/value_object.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 25 | #include "globals.h" |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 26 | #include "utils.h" |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 27 | |
Carl Shapiro | 6b6b5f0 | 2011-06-21 15:05:09 -0700 | [diff] [blame] | 28 | namespace art { |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 29 | |
| 30 | // Memory regions are useful for accessing memory with bounds check in |
| 31 | // debug mode. They can be safely passed by value and do not assume ownership |
| 32 | // of the region. |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 33 | class MemoryRegion FINAL : public ValueObject { |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 34 | public: |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 35 | MemoryRegion() : pointer_(nullptr), size_(0) {} |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 36 | MemoryRegion(void* pointer_in, uintptr_t size_in) : pointer_(pointer_in), size_(size_in) {} |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 37 | |
| 38 | void* pointer() const { return pointer_; } |
| 39 | size_t size() const { return size_; } |
| 40 | size_t size_in_bits() const { return size_ * kBitsPerByte; } |
| 41 | |
| 42 | static size_t pointer_offset() { |
| 43 | return OFFSETOF_MEMBER(MemoryRegion, pointer_); |
| 44 | } |
| 45 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 46 | uint8_t* start() const { return reinterpret_cast<uint8_t*>(pointer_); } |
| 47 | uint8_t* end() const { return start() + size_; } |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 48 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 49 | // Load value of type `T` at `offset`. The memory address corresponding |
| 50 | // to `offset` should be word-aligned. |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 51 | template<typename T> T Load(uintptr_t offset) const { |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 52 | // TODO: DCHECK that the address is word-aligned. |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 53 | return *ComputeInternalPointer<T>(offset); |
| 54 | } |
| 55 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 56 | // Store `value` (of type `T`) at `offset`. The memory address |
| 57 | // corresponding to `offset` should be word-aligned. |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 58 | template<typename T> void Store(uintptr_t offset, T value) const { |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 59 | // TODO: DCHECK that the address is word-aligned. |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 60 | *ComputeInternalPointer<T>(offset) = value; |
| 61 | } |
| 62 | |
Roland Levillain | a2d8ec6 | 2015-03-12 15:25:29 +0000 | [diff] [blame] | 63 | // TODO: Local hack to prevent name clashes between two conflicting |
| 64 | // implementations of bit_cast: |
| 65 | // - art::bit_cast<Destination, Source> runtime/base/casts.h, and |
| 66 | // - art::bit_cast<Source, Destination> from runtime/utils.h. |
| 67 | // Remove this when these routines have been merged. |
| 68 | template<typename Source, typename Destination> |
| 69 | static Destination local_bit_cast(Source in) { |
| 70 | static_assert(sizeof(Source) <= sizeof(Destination), |
| 71 | "Size of Source not <= size of Destination"); |
| 72 | union { |
| 73 | Source u; |
| 74 | Destination v; |
| 75 | } tmp; |
| 76 | tmp.u = in; |
| 77 | return tmp.v; |
| 78 | } |
| 79 | |
| 80 | // Load value of type `T` at `offset`. The memory address corresponding |
| 81 | // to `offset` does not need to be word-aligned. |
| 82 | template<typename T> T LoadUnaligned(uintptr_t offset) const { |
| 83 | // Equivalent unsigned integer type corresponding to T. |
| 84 | typedef typename UnsignedIntegerType<sizeof(T)>::type U; |
| 85 | U equivalent_unsigned_integer_value = 0; |
| 86 | // Read the value byte by byte in a little-endian fashion. |
| 87 | for (size_t i = 0; i < sizeof(U); ++i) { |
| 88 | equivalent_unsigned_integer_value += |
| 89 | *ComputeInternalPointer<uint8_t>(offset + i) << (i * kBitsPerByte); |
| 90 | } |
| 91 | return local_bit_cast<U, T>(equivalent_unsigned_integer_value); |
| 92 | } |
| 93 | |
| 94 | // Store `value` (of type `T`) at `offset`. The memory address |
| 95 | // corresponding to `offset` does not need to be word-aligned. |
| 96 | template<typename T> void StoreUnaligned(uintptr_t offset, T value) const { |
| 97 | // Equivalent unsigned integer type corresponding to T. |
| 98 | typedef typename UnsignedIntegerType<sizeof(T)>::type U; |
| 99 | U equivalent_unsigned_integer_value = local_bit_cast<T, U>(value); |
| 100 | // Write the value byte by byte in a little-endian fashion. |
| 101 | for (size_t i = 0; i < sizeof(U); ++i) { |
| 102 | *ComputeInternalPointer<uint8_t>(offset + i) = |
| 103 | (equivalent_unsigned_integer_value >> (i * kBitsPerByte)) & 0xFF; |
| 104 | } |
| 105 | } |
| 106 | |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 107 | template<typename T> T* PointerTo(uintptr_t offset) const { |
| 108 | return ComputeInternalPointer<T>(offset); |
| 109 | } |
| 110 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 111 | // Load a single bit in the region. The bit at offset 0 is the least |
| 112 | // significant bit in the first byte. |
| 113 | bool LoadBit(uintptr_t bit_offset) const { |
| 114 | uint8_t bit_mask; |
| 115 | uint8_t byte = *ComputeBitPointer(bit_offset, &bit_mask); |
| 116 | return byte & bit_mask; |
| 117 | } |
| 118 | |
| 119 | void StoreBit(uintptr_t bit_offset, bool value) const { |
| 120 | uint8_t bit_mask; |
| 121 | uint8_t* byte = ComputeBitPointer(bit_offset, &bit_mask); |
| 122 | if (value) { |
| 123 | *byte |= bit_mask; |
| 124 | } else { |
| 125 | *byte &= ~bit_mask; |
| 126 | } |
| 127 | } |
| 128 | |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 129 | void CopyFrom(size_t offset, const MemoryRegion& from) const; |
| 130 | |
| 131 | // Compute a sub memory region based on an existing one. |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 132 | MemoryRegion Subregion(uintptr_t offset, uintptr_t size_in) const { |
| 133 | CHECK_GE(this->size(), size_in); |
| 134 | CHECK_LE(offset, this->size() - size_in); |
| 135 | return MemoryRegion(reinterpret_cast<void*>(start() + offset), size_in); |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | // Compute an extended memory region based on an existing one. |
| 139 | void Extend(const MemoryRegion& region, uintptr_t extra) { |
| 140 | pointer_ = region.pointer(); |
| 141 | size_ = (region.size() + extra); |
| 142 | } |
| 143 | |
| 144 | private: |
| 145 | template<typename T> T* ComputeInternalPointer(size_t offset) const { |
| 146 | CHECK_GE(size(), sizeof(T)); |
| 147 | CHECK_LE(offset, size() - sizeof(T)); |
| 148 | return reinterpret_cast<T*>(start() + offset); |
| 149 | } |
| 150 | |
| 151 | // Locate the bit with the given offset. Returns a pointer to the byte |
| 152 | // containing the bit, and sets bit_mask to the bit within that byte. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 153 | uint8_t* ComputeBitPointer(uintptr_t bit_offset, uint8_t* bit_mask) const { |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 154 | uintptr_t bit_remainder = (bit_offset & (kBitsPerByte - 1)); |
| 155 | *bit_mask = (1U << bit_remainder); |
| 156 | uintptr_t byte_offset = (bit_offset >> kBitsPerByteLog2); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 157 | return ComputeInternalPointer<uint8_t>(byte_offset); |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | void* pointer_; |
| 161 | size_t size_; |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 162 | }; |
| 163 | |
Carl Shapiro | 6b6b5f0 | 2011-06-21 15:05:09 -0700 | [diff] [blame] | 164 | } // namespace art |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 165 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 166 | #endif // ART_RUNTIME_MEMORY_REGION_H_ |