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