blob: 9c9ff92071b77f6616f27f1abcc1f17618e2b8dc [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
David Sehr1ce2b3b2018-04-05 11:02:03 -070017#ifndef ART_LIBARTBASE_BASE_MEMORY_REGION_H_
18#define ART_LIBARTBASE_BASE_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
Andreas Gampe57943812017-12-06 21:39:13 -080023#include <android-base/logging.h>
24
David Sehr1979c642018-04-26 14:41:18 -070025#include "bit_utils.h"
26#include "casts.h"
27#include "enums.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070028#include "globals.h"
David Sehr1979c642018-04-26 14:41:18 -070029#include "macros.h"
30#include "value_object.h"
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070031
Carl Shapiro6b6b5f02011-06-21 15:05:09 -070032namespace art {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070033
34// Memory regions are useful for accessing memory with bounds check in
35// debug mode. They can be safely passed by value and do not assume ownership
36// of the region.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010037class MemoryRegion final : public ValueObject {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070038 public:
David Srbecky45aa5982016-03-18 02:15:09 +000039 struct ContentEquals {
40 constexpr bool operator()(const MemoryRegion& lhs, const MemoryRegion& rhs) const {
41 return lhs.size() == rhs.size() && memcmp(lhs.begin(), rhs.begin(), lhs.size()) == 0;
42 }
43 };
44
Ian Rogersd4c4d952014-10-16 20:31:53 -070045 MemoryRegion() : pointer_(nullptr), size_(0) {}
Andreas Gampe277ccbd2014-11-03 21:36:10 -080046 MemoryRegion(void* pointer_in, uintptr_t size_in) : pointer_(pointer_in), size_(size_in) {}
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070047
48 void* pointer() const { return pointer_; }
49 size_t size() const { return size_; }
50 size_t size_in_bits() const { return size_ * kBitsPerByte; }
51
52 static size_t pointer_offset() {
53 return OFFSETOF_MEMBER(MemoryRegion, pointer_);
54 }
55
David Srbecky45aa5982016-03-18 02:15:09 +000056 uint8_t* begin() const { return reinterpret_cast<uint8_t*>(pointer_); }
57 uint8_t* end() const { return begin() + size_; }
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070058
Roland Levillaina2d8ec62015-03-12 15:25:29 +000059 // Load value of type `T` at `offset`. The memory address corresponding
Roland Levillainbdba92d2015-03-31 12:27:44 +010060 // to `offset` should be word-aligned (on ARM, this is a requirement).
David Brazdilb7656832015-03-30 10:08:19 +010061 template<typename T>
62 ALWAYS_INLINE T Load(uintptr_t offset) const {
Roland Levillainbdba92d2015-03-31 12:27:44 +010063 T* address = ComputeInternalPointer<T>(offset);
64 DCHECK(IsWordAligned(address));
65 return *address;
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070066 }
67
Roland Levillaina2d8ec62015-03-12 15:25:29 +000068 // Store `value` (of type `T`) at `offset`. The memory address
Roland Levillainbdba92d2015-03-31 12:27:44 +010069 // corresponding to `offset` should be word-aligned (on ARM, this is
70 // a requirement).
David Brazdilb7656832015-03-30 10:08:19 +010071 template<typename T>
72 ALWAYS_INLINE void Store(uintptr_t offset, T value) const {
Roland Levillainbdba92d2015-03-31 12:27:44 +010073 T* address = ComputeInternalPointer<T>(offset);
74 DCHECK(IsWordAligned(address));
75 *address = value;
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070076 }
77
Roland Levillaina2d8ec62015-03-12 15:25:29 +000078 // Load value of type `T` at `offset`. The memory address corresponding
79 // to `offset` does not need to be word-aligned.
David Brazdilb7656832015-03-30 10:08:19 +010080 template<typename T>
81 ALWAYS_INLINE T LoadUnaligned(uintptr_t offset) const {
Roland Levillaina2d8ec62015-03-12 15:25:29 +000082 // Equivalent unsigned integer type corresponding to T.
Vladimir Marko80afd022015-05-19 18:08:00 +010083 typedef typename std::make_unsigned<T>::type U;
Roland Levillaina2d8ec62015-03-12 15:25:29 +000084 U equivalent_unsigned_integer_value = 0;
85 // Read the value byte by byte in a little-endian fashion.
86 for (size_t i = 0; i < sizeof(U); ++i) {
87 equivalent_unsigned_integer_value +=
88 *ComputeInternalPointer<uint8_t>(offset + i) << (i * kBitsPerByte);
89 }
Roland Levillainda4d79b2015-03-24 14:36:11 +000090 return bit_cast<T, U>(equivalent_unsigned_integer_value);
Roland Levillaina2d8ec62015-03-12 15:25:29 +000091 }
92
93 // Store `value` (of type `T`) at `offset`. The memory address
94 // corresponding to `offset` does not need to be word-aligned.
David Brazdilb7656832015-03-30 10:08:19 +010095 template<typename T>
96 ALWAYS_INLINE void StoreUnaligned(uintptr_t offset, T value) const {
Roland Levillaina2d8ec62015-03-12 15:25:29 +000097 // Equivalent unsigned integer type corresponding to T.
Vladimir Marko80afd022015-05-19 18:08:00 +010098 typedef typename std::make_unsigned<T>::type U;
Roland Levillainda4d79b2015-03-24 14:36:11 +000099 U equivalent_unsigned_integer_value = bit_cast<U, T>(value);
Roland Levillaina2d8ec62015-03-12 15:25:29 +0000100 // 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
David Brazdilb7656832015-03-30 10:08:19 +0100107 template<typename T>
108 ALWAYS_INLINE T* PointerTo(uintptr_t offset) const {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700109 return ComputeInternalPointer<T>(offset);
110 }
111
112 void CopyFrom(size_t offset, const MemoryRegion& from) const;
113
David Srbecky45aa5982016-03-18 02:15:09 +0000114 template<class Vector>
115 void CopyFromVector(size_t offset, Vector& vector) const {
116 if (!vector.empty()) {
117 CopyFrom(offset, MemoryRegion(vector.data(), vector.size()));
118 }
119 }
120
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700121 // Compute a sub memory region based on an existing one.
Mingyao Yangccfa8852017-01-18 14:51:59 -0800122 ALWAYS_INLINE MemoryRegion Subregion(uintptr_t offset, uintptr_t size_in) const {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800123 CHECK_GE(this->size(), size_in);
124 CHECK_LE(offset, this->size() - size_in);
David Srbecky45aa5982016-03-18 02:15:09 +0000125 return MemoryRegion(reinterpret_cast<void*>(begin() + offset), size_in);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700126 }
127
128 // Compute an extended memory region based on an existing one.
Mingyao Yangccfa8852017-01-18 14:51:59 -0800129 ALWAYS_INLINE void Extend(const MemoryRegion& region, uintptr_t extra) {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700130 pointer_ = region.pointer();
131 size_ = (region.size() + extra);
132 }
133
134 private:
David Brazdilb7656832015-03-30 10:08:19 +0100135 template<typename T>
136 ALWAYS_INLINE T* ComputeInternalPointer(size_t offset) const {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700137 CHECK_GE(size(), sizeof(T));
138 CHECK_LE(offset, size() - sizeof(T));
David Srbecky45aa5982016-03-18 02:15:09 +0000139 return reinterpret_cast<T*>(begin() + offset);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700140 }
141
142 // Locate the bit with the given offset. Returns a pointer to the byte
143 // containing the bit, and sets bit_mask to the bit within that byte.
David Brazdilb7656832015-03-30 10:08:19 +0100144 ALWAYS_INLINE uint8_t* ComputeBitPointer(uintptr_t bit_offset, uint8_t* bit_mask) const {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700145 uintptr_t bit_remainder = (bit_offset & (kBitsPerByte - 1));
146 *bit_mask = (1U << bit_remainder);
147 uintptr_t byte_offset = (bit_offset >> kBitsPerByteLog2);
Ian Rogers13735952014-10-08 12:43:28 -0700148 return ComputeInternalPointer<uint8_t>(byte_offset);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700149 }
150
Roland Levillainbdba92d2015-03-31 12:27:44 +0100151 // Is `address` aligned on a machine word?
Andreas Gampe542451c2016-07-26 09:02:02 -0700152 template<typename T> static constexpr bool IsWordAligned(const T* address) {
David Sehr1ce2b3b2018-04-05 11:02:03 -0700153 // Word alignment in bytes. Determined from pointer size.
154 return IsAligned<kRuntimePointerSize>(address);
Roland Levillainbdba92d2015-03-31 12:27:44 +0100155 }
156
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700157 void* pointer_;
158 size_t size_;
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700159};
160
Carl Shapiro6b6b5f02011-06-21 15:05:09 -0700161} // namespace art
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700162
David Sehr1ce2b3b2018-04-05 11:02:03 -0700163#endif // ART_LIBARTBASE_BASE_MEMORY_REGION_H_