blob: f1e3b5053b1748cf086aec8fef15a0a9ff4034e4 [file] [log] [blame]
Mathieu Chartiera59d9b22016-09-26 18:13:17 -07001/*
2 * Copyright (C) 2016 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_OBJ_PTR_INL_H_
18#define ART_RUNTIME_OBJ_PTR_INL_H_
19
Andreas Gampe3b7dc352017-06-06 20:02:03 -070020#include "base/bit_utils.h"
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070021#include "obj_ptr.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070022#include "thread-current-inl.h"
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070023
24namespace art {
25
Andreas Gampec73cb642017-02-22 10:11:30 -080026template<class MirrorType>
27inline bool ObjPtr<MirrorType>::IsValid() const {
28 if (!kObjPtrPoisoning || IsNull()) {
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070029 return true;
30 }
31 return GetCookie() == TrimCookie(Thread::Current()->GetPoisonObjectCookie());
32}
33
Andreas Gampec73cb642017-02-22 10:11:30 -080034template<class MirrorType>
35inline void ObjPtr<MirrorType>::AssertValid() const {
36 if (kObjPtrPoisoning) {
Mathieu Chartier9d156d52016-10-06 17:44:26 -070037 CHECK(IsValid()) << "Stale object pointer " << PtrUnchecked() << " , expected cookie "
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070038 << TrimCookie(Thread::Current()->GetPoisonObjectCookie()) << " but got " << GetCookie();
39 }
40}
41
Andreas Gampec73cb642017-02-22 10:11:30 -080042template<class MirrorType>
43inline uintptr_t ObjPtr<MirrorType>::Encode(MirrorType* ptr) {
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070044 uintptr_t ref = reinterpret_cast<uintptr_t>(ptr);
Mathieu Chartier0795f232016-09-27 18:43:30 -070045 DCHECK_ALIGNED(ref, kObjectAlignment);
Andreas Gampec73cb642017-02-22 10:11:30 -080046 if (kObjPtrPoisoning && ref != 0) {
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070047 DCHECK_LE(ref, 0xFFFFFFFFU);
48 ref >>= kObjectAlignmentShift;
49 // Put cookie in high bits.
50 Thread* self = Thread::Current();
51 DCHECK(self != nullptr);
52 ref |= self->GetPoisonObjectCookie() << kCookieShift;
53 }
54 return ref;
55}
56
Andreas Gampec73cb642017-02-22 10:11:30 -080057template<class MirrorType>
58inline std::ostream& operator<<(std::ostream& os, ObjPtr<MirrorType> ptr) {
Mathieu Chartier0795f232016-09-27 18:43:30 -070059 // May be used for dumping bad pointers, do not use the checked version.
Mathieu Chartier1cc62e42016-10-03 18:01:28 -070060 return os << ptr.PtrUnchecked();
Mathieu Chartier0795f232016-09-27 18:43:30 -070061}
62
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070063} // namespace art
64
65#endif // ART_RUNTIME_OBJ_PTR_INL_H_