blob: 2a55a41861ef607f7e573e270e1c41c269382d5a [file] [log] [blame]
Jan Voung8acded02014-09-22 18:02:25 -07001// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4//
5// Modified by the Subzero authors.
6//
7//===- subzero/src/IceMemoryRegion.h - Memory region ------------*- C++ -*-===//
8//
9// The Subzero Code Generator
10//
11// This file is distributed under the University of Illinois Open Source
12// License. See LICENSE.TXT for details.
13//
14//===----------------------------------------------------------------------===//
15//
16// This file declares the MemoryRegion class. It tracks a pointer
17// plus its bounds for bounds-checking in debug mode.
18//===----------------------------------------------------------------------===//
19
20#ifndef SUBZERO_SRC_ICE_MEMORY_REGION_H_
21#define SUBZERO_SRC_ICE_MEMORY_REGION_H_
22
23#include "IceDefs.h"
24#include "IceUtils.h"
25
26namespace Ice {
27
28// Memory regions are useful for accessing memory with bounds check in
29// debug mode. They can be safely passed by value and do not assume ownership
30// of the region.
31class MemoryRegion {
32public:
33 MemoryRegion() : pointer_(NULL), size_(0) {}
34 MemoryRegion(void *pointer, size_t size) : pointer_(pointer), size_(size) {}
35 MemoryRegion(const MemoryRegion &other) { *this = other; }
36 MemoryRegion &operator=(const MemoryRegion &other) {
37 pointer_ = other.pointer_;
38 size_ = other.size_;
39 return *this;
40 }
41
42 void *pointer() const { return pointer_; }
43 size_t size() const { return size_; }
44
45 size_t start() const { return reinterpret_cast<size_t>(pointer_); }
46 size_t end() const { return start() + size_; }
47
48 template <typename T> T Load(size_t offset) const {
49 return *ComputeInternalPointer<T>(offset);
50 }
51
52 template <typename T> void Store(size_t offset, T value) const {
53 *ComputeInternalPointer<T>(offset) = value;
54 }
55
56 template <typename T> T *PointerTo(size_t offset) const {
57 return ComputeInternalPointer<T>(offset);
58 }
59
60 bool Contains(size_t address) const {
61 return (address >= start()) && (address < end());
62 }
63
64 void CopyFrom(size_t offset, const MemoryRegion &from) const;
65
66 // Compute a sub memory region based on an existing one.
67 void Subregion(const MemoryRegion &from, size_t offset, size_t size) {
68 assert(from.size() >= size);
69 assert(offset <= (from.size() - size));
70 pointer_ = reinterpret_cast<void *>(from.start() + offset);
71 size_ = size;
72 }
73
74 // Compute an extended memory region based on an existing one.
75 void Extend(const MemoryRegion &region, size_t extra) {
76 pointer_ = region.pointer();
77 size_ = (region.size() + extra);
78 }
79
80private:
81 template <typename T> T *ComputeInternalPointer(size_t offset) const {
82 assert(size() >= sizeof(T));
83 assert(offset <= size() - sizeof(T));
84 return reinterpret_cast<T *>(start() + offset);
85 }
86
87 void *pointer_;
88 size_t size_;
89};
90
91} // end of namespace Ice
92
93#endif // SUBZERO_SRC_ICE_MEMORY_REGION_H_