blob: f55dff7a500c04d610affe85c6ed8be35baec390 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 Shapiroa5d5cfd2011-06-21 12:46:59 -070016
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_MEMORY_REGION_H_
18#define ART_RUNTIME_MEMORY_REGION_H_
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070019
20#include <stdint.h>
Vladimir Marko80afd022015-05-19 18:08:00 +010021#include <type_traits>
Elliott Hughes76160052012-12-12 16:31:20 -080022
Vladimir Marko80afd022015-05-19 18:08:00 +010023#include "arch/instruction_set.h"
24#include "base/bit_utils.h"
Roland Levillainda4d79b2015-03-24 14:36:11 +000025#include "base/casts.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080026#include "base/logging.h"
Elliott Hughes76160052012-12-12 16:31:20 -080027#include "base/macros.h"
Ian Rogersd4c4d952014-10-16 20:31:53 -070028#include "base/value_object.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070029#include "globals.h"
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070030
Carl Shapiro6b6b5f02011-06-21 15:05:09 -070031namespace art {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070032
33// Memory regions are useful for accessing memory with bounds check in
34// debug mode. They can be safely passed by value and do not assume ownership
35// of the region.
Ian Rogersd4c4d952014-10-16 20:31:53 -070036class MemoryRegion FINAL : public ValueObject {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070037 public:
Ian Rogersd4c4d952014-10-16 20:31:53 -070038 MemoryRegion() : pointer_(nullptr), size_(0) {}
Andreas Gampe277ccbd2014-11-03 21:36:10 -080039 MemoryRegion(void* pointer_in, uintptr_t size_in) : pointer_(pointer_in), size_(size_in) {}
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070040
41 void* pointer() const { return pointer_; }
42 size_t size() const { return size_; }
43 size_t size_in_bits() const { return size_ * kBitsPerByte; }
44
45 static size_t pointer_offset() {
46 return OFFSETOF_MEMBER(MemoryRegion, pointer_);
47 }
48
Ian Rogers13735952014-10-08 12:43:28 -070049 uint8_t* start() const { return reinterpret_cast<uint8_t*>(pointer_); }
50 uint8_t* end() const { return start() + size_; }
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070051
Roland Levillaina2d8ec62015-03-12 15:25:29 +000052 // Load value of type `T` at `offset`. The memory address corresponding
Roland Levillainbdba92d2015-03-31 12:27:44 +010053 // to `offset` should be word-aligned (on ARM, this is a requirement).
David Brazdilb7656832015-03-30 10:08:19 +010054 template<typename T>
55 ALWAYS_INLINE T Load(uintptr_t offset) const {
Roland Levillainbdba92d2015-03-31 12:27:44 +010056 T* address = ComputeInternalPointer<T>(offset);
57 DCHECK(IsWordAligned(address));
58 return *address;
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070059 }
60
Roland Levillaina2d8ec62015-03-12 15:25:29 +000061 // Store `value` (of type `T`) at `offset`. The memory address
Roland Levillainbdba92d2015-03-31 12:27:44 +010062 // corresponding to `offset` should be word-aligned (on ARM, this is
63 // a requirement).
David Brazdilb7656832015-03-30 10:08:19 +010064 template<typename T>
65 ALWAYS_INLINE void Store(uintptr_t offset, T value) const {
Roland Levillainbdba92d2015-03-31 12:27:44 +010066 T* address = ComputeInternalPointer<T>(offset);
67 DCHECK(IsWordAligned(address));
68 *address = value;
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070069 }
70
Roland Levillaina2d8ec62015-03-12 15:25:29 +000071 // Load value of type `T` at `offset`. The memory address corresponding
72 // to `offset` does not need to be word-aligned.
David Brazdilb7656832015-03-30 10:08:19 +010073 template<typename T>
74 ALWAYS_INLINE T LoadUnaligned(uintptr_t offset) const {
Roland Levillaina2d8ec62015-03-12 15:25:29 +000075 // Equivalent unsigned integer type corresponding to T.
Vladimir Marko80afd022015-05-19 18:08:00 +010076 typedef typename std::make_unsigned<T>::type U;
Roland Levillaina2d8ec62015-03-12 15:25:29 +000077 U equivalent_unsigned_integer_value = 0;
78 // Read the value byte by byte in a little-endian fashion.
79 for (size_t i = 0; i < sizeof(U); ++i) {
80 equivalent_unsigned_integer_value +=
81 *ComputeInternalPointer<uint8_t>(offset + i) << (i * kBitsPerByte);
82 }
Roland Levillainda4d79b2015-03-24 14:36:11 +000083 return bit_cast<T, U>(equivalent_unsigned_integer_value);
Roland Levillaina2d8ec62015-03-12 15:25:29 +000084 }
85
86 // Store `value` (of type `T`) at `offset`. The memory address
87 // corresponding to `offset` does not need to be word-aligned.
David Brazdilb7656832015-03-30 10:08:19 +010088 template<typename T>
89 ALWAYS_INLINE void StoreUnaligned(uintptr_t offset, T value) const {
Roland Levillaina2d8ec62015-03-12 15:25:29 +000090 // Equivalent unsigned integer type corresponding to T.
Vladimir Marko80afd022015-05-19 18:08:00 +010091 typedef typename std::make_unsigned<T>::type U;
Roland Levillainda4d79b2015-03-24 14:36:11 +000092 U equivalent_unsigned_integer_value = bit_cast<U, T>(value);
Roland Levillaina2d8ec62015-03-12 15:25:29 +000093 // Write the value byte by byte in a little-endian fashion.
94 for (size_t i = 0; i < sizeof(U); ++i) {
95 *ComputeInternalPointer<uint8_t>(offset + i) =
96 (equivalent_unsigned_integer_value >> (i * kBitsPerByte)) & 0xFF;
97 }
98 }
99
David Brazdilb7656832015-03-30 10:08:19 +0100100 template<typename T>
101 ALWAYS_INLINE T* PointerTo(uintptr_t offset) const {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700102 return ComputeInternalPointer<T>(offset);
103 }
104
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100105 // Load a single bit in the region. The bit at offset 0 is the least
106 // significant bit in the first byte.
David Brazdilb7656832015-03-30 10:08:19 +0100107 ALWAYS_INLINE bool LoadBit(uintptr_t bit_offset) const {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100108 uint8_t bit_mask;
109 uint8_t byte = *ComputeBitPointer(bit_offset, &bit_mask);
110 return byte & bit_mask;
111 }
112
David Brazdilb7656832015-03-30 10:08:19 +0100113 ALWAYS_INLINE void StoreBit(uintptr_t bit_offset, bool value) const {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100114 uint8_t bit_mask;
115 uint8_t* byte = ComputeBitPointer(bit_offset, &bit_mask);
116 if (value) {
117 *byte |= bit_mask;
118 } else {
119 *byte &= ~bit_mask;
120 }
121 }
122
Roland Levillaina552e1c2015-03-26 15:01:03 +0000123 // Load `length` bits from the region starting at bit offset `bit_offset`.
124 // The bit at the smallest offset is the least significant bit in the
125 // loaded value. `length` must not be larger than the number of bits
126 // contained in the return value (32).
Mathieu Chartier3ceedc02017-01-25 11:11:02 -0800127 ALWAYS_INLINE uint32_t LoadBits(uintptr_t bit_offset, size_t length) const {
128 DCHECK_LE(length, BitSizeOf<uint32_t>());
129 DCHECK_LE(bit_offset + length, size_in_bits());
130 if (UNLIKELY(length == 0)) {
131 // Do not touch any memory if the range is empty.
132 return 0;
133 }
134 const uint8_t* address = start() + bit_offset / kBitsPerByte;
135 const uint32_t shift = bit_offset & (kBitsPerByte - 1);
136 // Load the value (reading only the strictly needed bytes).
137 const uint32_t load_bit_count = shift + length;
138 uint32_t value = address[0] >> shift;
139 if (load_bit_count > 8) {
140 value |= static_cast<uint32_t>(address[1]) << (8 - shift);
141 if (load_bit_count > 16) {
142 value |= static_cast<uint32_t>(address[2]) << (16 - shift);
143 if (load_bit_count > 24) {
144 value |= static_cast<uint32_t>(address[3]) << (24 - shift);
145 if (load_bit_count > 32) {
146 value |= static_cast<uint32_t>(address[4]) << (32 - shift);
147 }
148 }
149 }
150 }
151 // Clear unwanted most significant bits.
152 uint32_t clear_bit_count = BitSizeOf(value) - length;
153 value = (value << clear_bit_count) >> clear_bit_count;
Roland Levillaina552e1c2015-03-26 15:01:03 +0000154 for (size_t i = 0; i < length; ++i) {
Mathieu Chartier3ceedc02017-01-25 11:11:02 -0800155 DCHECK_EQ((value >> i) & 1, LoadBit(bit_offset + i));
Roland Levillaina552e1c2015-03-26 15:01:03 +0000156 }
157 return value;
158 }
159
160 // Store `value` on `length` bits in the region starting at bit offset
161 // `bit_offset`. The bit at the smallest offset is the least significant
162 // bit of the stored `value`. `value` must not be larger than `length`
163 // bits.
Mathieu Chartier3ceedc02017-01-25 11:11:02 -0800164 void StoreBits(uintptr_t bit_offset, uint32_t value, size_t length);
Roland Levillaina552e1c2015-03-26 15:01:03 +0000165
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700166 void CopyFrom(size_t offset, const MemoryRegion& from) const;
167
168 // Compute a sub memory region based on an existing one.
Mingyao Yangccfa8852017-01-18 14:51:59 -0800169 ALWAYS_INLINE MemoryRegion Subregion(uintptr_t offset, uintptr_t size_in) const {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800170 CHECK_GE(this->size(), size_in);
171 CHECK_LE(offset, this->size() - size_in);
172 return MemoryRegion(reinterpret_cast<void*>(start() + offset), size_in);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700173 }
174
175 // Compute an extended memory region based on an existing one.
Mingyao Yangccfa8852017-01-18 14:51:59 -0800176 ALWAYS_INLINE void Extend(const MemoryRegion& region, uintptr_t extra) {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700177 pointer_ = region.pointer();
178 size_ = (region.size() + extra);
179 }
180
181 private:
David Brazdilb7656832015-03-30 10:08:19 +0100182 template<typename T>
183 ALWAYS_INLINE T* ComputeInternalPointer(size_t offset) const {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700184 CHECK_GE(size(), sizeof(T));
185 CHECK_LE(offset, size() - sizeof(T));
186 return reinterpret_cast<T*>(start() + offset);
187 }
188
189 // Locate the bit with the given offset. Returns a pointer to the byte
190 // containing the bit, and sets bit_mask to the bit within that byte.
David Brazdilb7656832015-03-30 10:08:19 +0100191 ALWAYS_INLINE uint8_t* ComputeBitPointer(uintptr_t bit_offset, uint8_t* bit_mask) const {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700192 uintptr_t bit_remainder = (bit_offset & (kBitsPerByte - 1));
193 *bit_mask = (1U << bit_remainder);
194 uintptr_t byte_offset = (bit_offset >> kBitsPerByteLog2);
Ian Rogers13735952014-10-08 12:43:28 -0700195 return ComputeInternalPointer<uint8_t>(byte_offset);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700196 }
197
Roland Levillainbdba92d2015-03-31 12:27:44 +0100198 // Is `address` aligned on a machine word?
Andreas Gampe542451c2016-07-26 09:02:02 -0700199 template<typename T> static constexpr bool IsWordAligned(const T* address) {
Roland Levillainbdba92d2015-03-31 12:27:44 +0100200 // Word alignment in bytes.
Andreas Gampe542451c2016-07-26 09:02:02 -0700201 size_t kWordAlignment = static_cast<size_t>(GetInstructionSetPointerSize(kRuntimeISA));
Roland Levillainbdba92d2015-03-31 12:27:44 +0100202 return IsAlignedParam(address, kWordAlignment);
203 }
204
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700205 void* pointer_;
206 size_t size_;
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700207};
208
Carl Shapiro6b6b5f02011-06-21 15:05:09 -0700209} // namespace art
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700210
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700211#endif // ART_RUNTIME_MEMORY_REGION_H_