blob: b3820be26cae0d30e4813cc185d2ac75cfb78ad9 [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
Elliott Hughes07ed66b2012-12-12 18:34:25 -080022#include "base/logging.h"
Elliott Hughes76160052012-12-12 16:31:20 -080023#include "base/macros.h"
Ian Rogersd4c4d952014-10-16 20:31:53 -070024#include "base/value_object.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070025#include "globals.h"
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070026
Carl Shapiro6b6b5f02011-06-21 15:05:09 -070027namespace art {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070028
29// Memory regions are useful for accessing memory with bounds check in
30// debug mode. They can be safely passed by value and do not assume ownership
31// of the region.
Ian Rogersd4c4d952014-10-16 20:31:53 -070032class MemoryRegion FINAL : public ValueObject {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070033 public:
Ian Rogersd4c4d952014-10-16 20:31:53 -070034 MemoryRegion() : pointer_(nullptr), size_(0) {}
Andreas Gampe277ccbd2014-11-03 21:36:10 -080035 MemoryRegion(void* pointer_in, uintptr_t size_in) : pointer_(pointer_in), size_(size_in) {}
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070036
37 void* pointer() const { return pointer_; }
38 size_t size() const { return size_; }
39 size_t size_in_bits() const { return size_ * kBitsPerByte; }
40
41 static size_t pointer_offset() {
42 return OFFSETOF_MEMBER(MemoryRegion, pointer_);
43 }
44
Ian Rogers13735952014-10-08 12:43:28 -070045 uint8_t* start() const { return reinterpret_cast<uint8_t*>(pointer_); }
46 uint8_t* end() const { return start() + size_; }
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070047
48 template<typename T> T Load(uintptr_t offset) const {
49 return *ComputeInternalPointer<T>(offset);
50 }
51
52 template<typename T> void Store(uintptr_t offset, T value) const {
53 *ComputeInternalPointer<T>(offset) = value;
54 }
55
56 template<typename T> T* PointerTo(uintptr_t offset) const {
57 return ComputeInternalPointer<T>(offset);
58 }
59
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010060 // Load a single bit in the region. The bit at offset 0 is the least
61 // significant bit in the first byte.
62 bool LoadBit(uintptr_t bit_offset) const {
63 uint8_t bit_mask;
64 uint8_t byte = *ComputeBitPointer(bit_offset, &bit_mask);
65 return byte & bit_mask;
66 }
67
68 void StoreBit(uintptr_t bit_offset, bool value) const {
69 uint8_t bit_mask;
70 uint8_t* byte = ComputeBitPointer(bit_offset, &bit_mask);
71 if (value) {
72 *byte |= bit_mask;
73 } else {
74 *byte &= ~bit_mask;
75 }
76 }
77
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070078 void CopyFrom(size_t offset, const MemoryRegion& from) const;
79
80 // Compute a sub memory region based on an existing one.
Andreas Gampe277ccbd2014-11-03 21:36:10 -080081 MemoryRegion Subregion(uintptr_t offset, uintptr_t size_in) const {
82 CHECK_GE(this->size(), size_in);
83 CHECK_LE(offset, this->size() - size_in);
84 return MemoryRegion(reinterpret_cast<void*>(start() + offset), size_in);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070085 }
86
87 // Compute an extended memory region based on an existing one.
88 void Extend(const MemoryRegion& region, uintptr_t extra) {
89 pointer_ = region.pointer();
90 size_ = (region.size() + extra);
91 }
92
93 private:
94 template<typename T> T* ComputeInternalPointer(size_t offset) const {
95 CHECK_GE(size(), sizeof(T));
96 CHECK_LE(offset, size() - sizeof(T));
97 return reinterpret_cast<T*>(start() + offset);
98 }
99
100 // Locate the bit with the given offset. Returns a pointer to the byte
101 // containing the bit, and sets bit_mask to the bit within that byte.
Ian Rogers13735952014-10-08 12:43:28 -0700102 uint8_t* ComputeBitPointer(uintptr_t bit_offset, uint8_t* bit_mask) const {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700103 uintptr_t bit_remainder = (bit_offset & (kBitsPerByte - 1));
104 *bit_mask = (1U << bit_remainder);
105 uintptr_t byte_offset = (bit_offset >> kBitsPerByteLog2);
Ian Rogers13735952014-10-08 12:43:28 -0700106 return ComputeInternalPointer<uint8_t>(byte_offset);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700107 }
108
109 void* pointer_;
110 size_t size_;
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700111};
112
Carl Shapiro6b6b5f02011-06-21 15:05:09 -0700113} // namespace art
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700114
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700115#endif // ART_RUNTIME_MEMORY_REGION_H_