blob: bd1ae87aeed778133233635155c662c478ec38ff [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
17#ifndef ART_SRC_MEMORY_REGION_H_
18#define ART_SRC_MEMORY_REGION_H_
19
20#include <stdint.h>
Elliott Hughes76160052012-12-12 16:31:20 -080021
22#include "base/macros.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070023#include "globals.h"
24#include "logging.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070025#include "memory_region.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.
32class MemoryRegion {
33 public:
34 MemoryRegion() : pointer_(NULL), size_(0) {}
35 MemoryRegion(void* pointer, uword size) : pointer_(pointer), size_(size) {}
36
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
45 byte* start() const { return reinterpret_cast<byte*>(pointer_); }
46 byte* end() const { return start() + size_; }
47
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
60 void CopyFrom(size_t offset, const MemoryRegion& from) const;
61
62 // Compute a sub memory region based on an existing one.
63 void Subregion(const MemoryRegion& from, uintptr_t offset, uintptr_t size) {
64 CHECK_GE(from.size(), size);
65 CHECK_LE(offset, from.size() - size);
66 pointer_ = reinterpret_cast<void*>(from.start() + offset);
67 size_ = size;
68 }
69
70 // Compute an extended memory region based on an existing one.
71 void Extend(const MemoryRegion& region, uintptr_t extra) {
72 pointer_ = region.pointer();
73 size_ = (region.size() + extra);
74 }
75
76 private:
77 template<typename T> T* ComputeInternalPointer(size_t offset) const {
78 CHECK_GE(size(), sizeof(T));
79 CHECK_LE(offset, size() - sizeof(T));
80 return reinterpret_cast<T*>(start() + offset);
81 }
82
83 // Locate the bit with the given offset. Returns a pointer to the byte
84 // containing the bit, and sets bit_mask to the bit within that byte.
85 byte* ComputeBitPointer(uintptr_t bit_offset, byte* bit_mask) const {
86 uintptr_t bit_remainder = (bit_offset & (kBitsPerByte - 1));
87 *bit_mask = (1U << bit_remainder);
88 uintptr_t byte_offset = (bit_offset >> kBitsPerByteLog2);
89 return ComputeInternalPointer<byte>(byte_offset);
90 }
91
92 void* pointer_;
93 size_t size_;
Brian Carlstrom4e777d42011-08-15 13:53:52 -070094
95 DISALLOW_COPY_AND_ASSIGN(MemoryRegion);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070096};
97
Carl Shapiro6b6b5f02011-06-21 15:05:09 -070098} // namespace art
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070099
100#endif // ART_MEMORY_REGION_H_