blob: 3d04d91d92751a81131a68643c9b62028cc9950d [file] [log] [blame]
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#ifndef ART_SRC_MEMORY_REGION_H_
4#define ART_SRC_MEMORY_REGION_H_
5
6#include <stdint.h>
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07007#include "globals.h"
8#include "logging.h"
9#include "macros.h"
10#include "memory_region.h"
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070011
Carl Shapiro6b6b5f02011-06-21 15:05:09 -070012namespace art {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070013
14// Memory regions are useful for accessing memory with bounds check in
15// debug mode. They can be safely passed by value and do not assume ownership
16// of the region.
17class MemoryRegion {
18 public:
19 MemoryRegion() : pointer_(NULL), size_(0) {}
20 MemoryRegion(void* pointer, uword size) : pointer_(pointer), size_(size) {}
21
22 void* pointer() const { return pointer_; }
23 size_t size() const { return size_; }
24 size_t size_in_bits() const { return size_ * kBitsPerByte; }
25
26 static size_t pointer_offset() {
27 return OFFSETOF_MEMBER(MemoryRegion, pointer_);
28 }
29
30 byte* start() const { return reinterpret_cast<byte*>(pointer_); }
31 byte* end() const { return start() + size_; }
32
33 template<typename T> T Load(uintptr_t offset) const {
34 return *ComputeInternalPointer<T>(offset);
35 }
36
37 template<typename T> void Store(uintptr_t offset, T value) const {
38 *ComputeInternalPointer<T>(offset) = value;
39 }
40
41 template<typename T> T* PointerTo(uintptr_t offset) const {
42 return ComputeInternalPointer<T>(offset);
43 }
44
45 void CopyFrom(size_t offset, const MemoryRegion& from) const;
46
47 // Compute a sub memory region based on an existing one.
48 void Subregion(const MemoryRegion& from, uintptr_t offset, uintptr_t size) {
49 CHECK_GE(from.size(), size);
50 CHECK_LE(offset, from.size() - size);
51 pointer_ = reinterpret_cast<void*>(from.start() + offset);
52 size_ = size;
53 }
54
55 // Compute an extended memory region based on an existing one.
56 void Extend(const MemoryRegion& region, uintptr_t extra) {
57 pointer_ = region.pointer();
58 size_ = (region.size() + extra);
59 }
60
61 private:
62 template<typename T> T* ComputeInternalPointer(size_t offset) const {
63 CHECK_GE(size(), sizeof(T));
64 CHECK_LE(offset, size() - sizeof(T));
65 return reinterpret_cast<T*>(start() + offset);
66 }
67
68 // Locate the bit with the given offset. Returns a pointer to the byte
69 // containing the bit, and sets bit_mask to the bit within that byte.
70 byte* ComputeBitPointer(uintptr_t bit_offset, byte* bit_mask) const {
71 uintptr_t bit_remainder = (bit_offset & (kBitsPerByte - 1));
72 *bit_mask = (1U << bit_remainder);
73 uintptr_t byte_offset = (bit_offset >> kBitsPerByteLog2);
74 return ComputeInternalPointer<byte>(byte_offset);
75 }
76
77 void* pointer_;
78 size_t size_;
Brian Carlstrom4e777d42011-08-15 13:53:52 -070079
80 DISALLOW_COPY_AND_ASSIGN(MemoryRegion);
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070081};
82
Carl Shapiro6b6b5f02011-06-21 15:05:09 -070083} // namespace art
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070084
85#endif // ART_MEMORY_REGION_H_