• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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  */
16 
17 #ifndef ART_RUNTIME_OFFSETS_H_
18 #define ART_RUNTIME_OFFSETS_H_
19 
20 #include <iosfwd>
21 
22 #include "base/enums.h"
23 #include "runtime_globals.h"
24 
25 namespace art {
26 
27 // Allow the meaning of offsets to be strongly typed.
28 class Offset {
29  public:
Offset(size_t val)30   constexpr explicit Offset(size_t val) : val_(val) {}
Int32Value()31   constexpr int32_t Int32Value() const {
32     return static_cast<int32_t>(val_);
33   }
Uint32Value()34   constexpr uint32_t Uint32Value() const {
35     return static_cast<uint32_t>(val_);
36   }
SizeValue()37   constexpr size_t SizeValue() const {
38     return val_;
39   }
40   Offset& operator+=(const size_t rhs) {
41     val_ += rhs;
42     return *this;
43   }
44   constexpr bool operator==(Offset o) const {
45     return SizeValue() == o.SizeValue();
46   }
47   constexpr bool operator!=(Offset o) const {
48     return !(*this == o);
49   }
50   constexpr bool operator<(Offset o) const {
51     return SizeValue() < o.SizeValue();
52   }
53   constexpr bool operator<=(Offset o) const {
54     return !(*this > o);
55   }
56   constexpr bool operator>(Offset o) const {
57     return o < *this;
58   }
59   constexpr bool operator>=(Offset o) const {
60     return !(*this < o);
61   }
62 
63  protected:
64   size_t val_;
65 };
66 std::ostream& operator<<(std::ostream& os, const Offset& offs);
67 
68 // Offsets relative to the current frame.
69 class FrameOffset : public Offset {
70  public:
FrameOffset(size_t val)71   constexpr explicit FrameOffset(size_t val) : Offset(val) {}
72   bool operator>(FrameOffset other) const { return val_ > other.val_; }
73   bool operator<(FrameOffset other) const { return val_ < other.val_; }
74 };
75 
76 // Offsets relative to the current running thread.
77 template<PointerSize pointer_size>
78 class ThreadOffset : public Offset {
79  public:
ThreadOffset(size_t val)80   constexpr explicit ThreadOffset(size_t val) : Offset(val) {}
81 };
82 
83 using ThreadOffset32 = ThreadOffset<PointerSize::k32>;
84 using ThreadOffset64 = ThreadOffset<PointerSize::k64>;
85 
86 // Offsets relative to an object.
87 class MemberOffset : public Offset {
88  public:
MemberOffset(size_t val)89   constexpr explicit MemberOffset(size_t val) : Offset(val) {}
90 };
91 
92 }  // namespace art
93 
94 #endif  // ART_RUNTIME_OFFSETS_H_
95