blob: 6a784eb061f64766ff6e0ac99cea91e2e41a2284 [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>
Elliott Hughes76160052012-12-12 16:31:20 -080021
Roland Levillainda4d79b2015-03-24 14:36:11 +000022#include "base/casts.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080023#include "base/logging.h"
Elliott Hughes76160052012-12-12 16:31:20 -080024#include "base/macros.h"
Ian Rogersd4c4d952014-10-16 20:31:53 -070025#include "base/value_object.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070026#include "globals.h"
Roland Levillaina2d8ec62015-03-12 15:25:29 +000027#include "utils.h"
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070028
Carl Shapiro6b6b5f02011-06-21 15:05:09 -070029namespace art {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070030
31// Memory regions are useful for accessing memory with bounds check in
32// debug mode. They can be safely passed by value and do not assume ownership
33// of the region.
Ian Rogersd4c4d952014-10-16 20:31:53 -070034class MemoryRegion FINAL : public ValueObject {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070035 public:
Ian Rogersd4c4d952014-10-16 20:31:53 -070036 MemoryRegion() : pointer_(nullptr), size_(0) {}
Andreas Gampe277ccbd2014-11-03 21:36:10 -080037 MemoryRegion(void* pointer_in, uintptr_t size_in) : pointer_(pointer_in), size_(size_in) {}
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070038
39 void* pointer() const { return pointer_; }
40 size_t size() const { return size_; }
41 size_t size_in_bits() const { return size_ * kBitsPerByte; }
42
43 static size_t pointer_offset() {
44 return OFFSETOF_MEMBER(MemoryRegion, pointer_);
45 }
46
Ian Rogers13735952014-10-08 12:43:28 -070047 uint8_t* start() const { return reinterpret_cast<uint8_t*>(pointer_); }
48 uint8_t* end() const { return start() + size_; }
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070049
Roland Levillaina2d8ec62015-03-12 15:25:29 +000050 // Load value of type `T` at `offset`. The memory address corresponding
Roland Levillainbdba92d2015-03-31 12:27:44 +010051 // to `offset` should be word-aligned (on ARM, this is a requirement).
David Brazdilb7656832015-03-30 10:08:19 +010052 template<typename T>
53 ALWAYS_INLINE T Load(uintptr_t offset) const {
Roland Levillainbdba92d2015-03-31 12:27:44 +010054 T* address = ComputeInternalPointer<T>(offset);
55 DCHECK(IsWordAligned(address));
56 return *address;
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070057 }
58
Roland Levillaina2d8ec62015-03-12 15:25:29 +000059 // Store `value` (of type `T`) at `offset`. The memory address
Roland Levillainbdba92d2015-03-31 12:27:44 +010060 // corresponding to `offset` should be word-aligned (on ARM, this is
61 // a requirement).
David Brazdilb7656832015-03-30 10:08:19 +010062 template<typename T>
63 ALWAYS_INLINE void Store(uintptr_t offset, T value) const {
Roland Levillainbdba92d2015-03-31 12:27:44 +010064 T* address = ComputeInternalPointer<T>(offset);
65 DCHECK(IsWordAligned(address));
66 *address = value;
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070067 }
68
Roland Levillaina2d8ec62015-03-12 15:25:29 +000069 // Load value of type `T` at `offset`. The memory address corresponding
70 // to `offset` does not need to be word-aligned.
David Brazdilb7656832015-03-30 10:08:19 +010071 template<typename T>
72 ALWAYS_INLINE T LoadUnaligned(uintptr_t offset) const {
Roland Levillaina2d8ec62015-03-12 15:25:29 +000073 // Equivalent unsigned integer type corresponding to T.
74 typedef typename UnsignedIntegerType<sizeof(T)>::type U;
75 U equivalent_unsigned_integer_value = 0;
76 // Read the value byte by byte in a little-endian fashion.
77 for (size_t i = 0; i < sizeof(U); ++i) {
78 equivalent_unsigned_integer_value +=
79 *ComputeInternalPointer<uint8_t>(offset + i) << (i * kBitsPerByte);
80 }
Roland Levillainda4d79b2015-03-24 14:36:11 +000081 return bit_cast<T, U>(equivalent_unsigned_integer_value);
Roland Levillaina2d8ec62015-03-12 15:25:29 +000082 }
83
84 // Store `value` (of type `T`) at `offset`. The memory address
85 // corresponding to `offset` does not need to be word-aligned.
David Brazdilb7656832015-03-30 10:08:19 +010086 template<typename T>
87 ALWAYS_INLINE void StoreUnaligned(uintptr_t offset, T value) const {
Roland Levillaina2d8ec62015-03-12 15:25:29 +000088 // Equivalent unsigned integer type corresponding to T.
89 typedef typename UnsignedIntegerType<sizeof(T)>::type U;
Roland Levillainda4d79b2015-03-24 14:36:11 +000090 U equivalent_unsigned_integer_value = bit_cast<U, T>(value);
Roland Levillaina2d8ec62015-03-12 15:25:29 +000091 // Write the value byte by byte in a little-endian fashion.
92 for (size_t i = 0; i < sizeof(U); ++i) {
93 *ComputeInternalPointer<uint8_t>(offset + i) =
94 (equivalent_unsigned_integer_value >> (i * kBitsPerByte)) & 0xFF;
95 }
96 }
97
David Brazdilb7656832015-03-30 10:08:19 +010098 template<typename T>
99 ALWAYS_INLINE T* PointerTo(uintptr_t offset) const {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700100 return ComputeInternalPointer<T>(offset);
101 }
102
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100103 // Load a single bit in the region. The bit at offset 0 is the least
104 // significant bit in the first byte.
David Brazdilb7656832015-03-30 10:08:19 +0100105 ALWAYS_INLINE bool LoadBit(uintptr_t bit_offset) const {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100106 uint8_t bit_mask;
107 uint8_t byte = *ComputeBitPointer(bit_offset, &bit_mask);
108 return byte & bit_mask;
109 }
110
David Brazdilb7656832015-03-30 10:08:19 +0100111 ALWAYS_INLINE void StoreBit(uintptr_t bit_offset, bool value) const {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100112 uint8_t bit_mask;
113 uint8_t* byte = ComputeBitPointer(bit_offset, &bit_mask);
114 if (value) {
115 *byte |= bit_mask;
116 } else {
117 *byte &= ~bit_mask;
118 }
119 }
120
Roland Levillaina552e1c2015-03-26 15:01:03 +0000121 // Load `length` bits from the region starting at bit offset `bit_offset`.
122 // The bit at the smallest offset is the least significant bit in the
123 // loaded value. `length` must not be larger than the number of bits
124 // contained in the return value (32).
125 uint32_t LoadBits(uintptr_t bit_offset, size_t length) const {
126 CHECK_LE(length, sizeof(uint32_t) * kBitsPerByte);
127 uint32_t value = 0u;
128 for (size_t i = 0; i < length; ++i) {
129 value |= LoadBit(bit_offset + i) << i;
130 }
131 return value;
132 }
133
134 // Store `value` on `length` bits in the region starting at bit offset
135 // `bit_offset`. The bit at the smallest offset is the least significant
136 // bit of the stored `value`. `value` must not be larger than `length`
137 // bits.
138 void StoreBits(uintptr_t bit_offset, uint32_t value, size_t length) {
139 CHECK_LT(value, 2u << length);
140 for (size_t i = 0; i < length; ++i) {
141 bool ith_bit = value & (1 << i);
142 StoreBit(bit_offset + i, ith_bit);
143 }
144 }
145
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700146 void CopyFrom(size_t offset, const MemoryRegion& from) const;
147
148 // Compute a sub memory region based on an existing one.
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800149 MemoryRegion Subregion(uintptr_t offset, uintptr_t size_in) const {
150 CHECK_GE(this->size(), size_in);
151 CHECK_LE(offset, this->size() - size_in);
152 return MemoryRegion(reinterpret_cast<void*>(start() + offset), size_in);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700153 }
154
155 // Compute an extended memory region based on an existing one.
156 void Extend(const MemoryRegion& region, uintptr_t extra) {
157 pointer_ = region.pointer();
158 size_ = (region.size() + extra);
159 }
160
161 private:
David Brazdilb7656832015-03-30 10:08:19 +0100162 template<typename T>
163 ALWAYS_INLINE T* ComputeInternalPointer(size_t offset) const {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700164 CHECK_GE(size(), sizeof(T));
165 CHECK_LE(offset, size() - sizeof(T));
166 return reinterpret_cast<T*>(start() + offset);
167 }
168
169 // Locate the bit with the given offset. Returns a pointer to the byte
170 // containing the bit, and sets bit_mask to the bit within that byte.
David Brazdilb7656832015-03-30 10:08:19 +0100171 ALWAYS_INLINE uint8_t* ComputeBitPointer(uintptr_t bit_offset, uint8_t* bit_mask) const {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700172 uintptr_t bit_remainder = (bit_offset & (kBitsPerByte - 1));
173 *bit_mask = (1U << bit_remainder);
174 uintptr_t byte_offset = (bit_offset >> kBitsPerByteLog2);
Ian Rogers13735952014-10-08 12:43:28 -0700175 return ComputeInternalPointer<uint8_t>(byte_offset);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700176 }
177
Roland Levillainbdba92d2015-03-31 12:27:44 +0100178 // Is `address` aligned on a machine word?
179 template<typename T> static bool IsWordAligned(const T* address) {
180 // Word alignment in bytes.
181 size_t kWordAlignment = GetInstructionSetPointerSize(kRuntimeISA);
182 return IsAlignedParam(address, kWordAlignment);
183 }
184
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700185 void* pointer_;
186 size_t size_;
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700187};
188
Carl Shapiro6b6b5f02011-06-21 15:05:09 -0700189} // namespace art
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700190
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700191#endif // ART_RUNTIME_MEMORY_REGION_H_