blob: cc18bf4f746cf5bc77725cc50c18c74029b0ae1b [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 Shapiro69759ea2011-07-21 18:13:35 -070016
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_OFFSETS_H_
18#define ART_RUNTIME_OFFSETS_H_
Carl Shapiro69759ea2011-07-21 18:13:35 -070019
Andreas Gampe8764dc32019-01-07 15:20:12 -080020#include <iosfwd>
Ian Rogerscf7f1912014-10-22 22:06:39 -070021
Andreas Gampe542451c2016-07-26 09:02:02 -070022#include "base/enums.h"
Andreas Gampe5a0430d2019-01-04 14:33:57 -080023#include "runtime_globals.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070024
25namespace art {
26
Ian Rogersef7d42f2014-01-06 12:55:46 -080027// Allow the meaning of offsets to be strongly typed.
Carl Shapiro69759ea2011-07-21 18:13:35 -070028class Offset {
29 public:
David Srbecky56de89a2018-10-01 15:32:20 +010030 constexpr explicit Offset(size_t val) : val_(val) {}
31 constexpr int32_t Int32Value() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -070032 return static_cast<int32_t>(val_);
33 }
David Srbecky56de89a2018-10-01 15:32:20 +010034 constexpr uint32_t Uint32Value() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -070035 return static_cast<uint32_t>(val_);
36 }
David Srbecky56de89a2018-10-01 15:32:20 +010037 constexpr size_t SizeValue() const {
Hiroshi Yamauchiaa866f52014-03-21 16:18:30 -070038 return val_;
39 }
Alex Light986914b2019-11-19 01:12:25 +000040 constexpr bool operator==(Offset o) const {
41 return SizeValue() == o.SizeValue();
42 }
Alex Light3a73ffb2021-01-25 14:11:05 +000043 constexpr bool operator!=(Offset o) const {
44 return !(*this == o);
45 }
Hiroshi Yamauchiaa866f52014-03-21 16:18:30 -070046
Carl Shapiro69759ea2011-07-21 18:13:35 -070047 protected:
48 size_t val_;
49};
50std::ostream& operator<<(std::ostream& os, const Offset& offs);
51
Ian Rogersef7d42f2014-01-06 12:55:46 -080052// Offsets relative to the current frame.
Carl Shapiro69759ea2011-07-21 18:13:35 -070053class FrameOffset : public Offset {
54 public:
David Srbecky56de89a2018-10-01 15:32:20 +010055 constexpr explicit FrameOffset(size_t val) : Offset(val) {}
Carl Shapiro69759ea2011-07-21 18:13:35 -070056 bool operator>(FrameOffset other) const { return val_ > other.val_; }
57 bool operator<(FrameOffset other) const { return val_ < other.val_; }
58};
59
Ian Rogersef7d42f2014-01-06 12:55:46 -080060// Offsets relative to the current running thread.
Andreas Gampe542451c2016-07-26 09:02:02 -070061template<PointerSize pointer_size>
Carl Shapiro69759ea2011-07-21 18:13:35 -070062class ThreadOffset : public Offset {
63 public:
David Srbecky56de89a2018-10-01 15:32:20 +010064 constexpr explicit ThreadOffset(size_t val) : Offset(val) {}
Carl Shapiro69759ea2011-07-21 18:13:35 -070065};
66
Andreas Gampe542451c2016-07-26 09:02:02 -070067using ThreadOffset32 = ThreadOffset<PointerSize::k32>;
68using ThreadOffset64 = ThreadOffset<PointerSize::k64>;
69
Ian Rogersef7d42f2014-01-06 12:55:46 -080070// Offsets relative to an object.
Carl Shapiro69759ea2011-07-21 18:13:35 -070071class MemberOffset : public Offset {
72 public:
David Srbecky56de89a2018-10-01 15:32:20 +010073 constexpr explicit MemberOffset(size_t val) : Offset(val) {}
Carl Shapiro69759ea2011-07-21 18:13:35 -070074};
75
76} // namespace art
77
Brian Carlstromfc0e3212013-07-17 14:40:12 -070078#endif // ART_RUNTIME_OFFSETS_H_