blob: b949c96dd22bc715fca23b4a23523e6e61d47f6d [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>
Vladimir Marko4df2d802018-09-27 16:42:44 +000027inline uintptr_t ObjPtr<MirrorType>::GetCurrentTrimedCookie() {
28 Thread* self = Thread::Current();
29 if (UNLIKELY(self == nullptr)) {
30 return kCookieMask;
31 }
32 return self->GetPoisonObjectCookie() & kCookieMask;
33}
34
35template<class MirrorType>
Andreas Gampec73cb642017-02-22 10:11:30 -080036inline bool ObjPtr<MirrorType>::IsValid() const {
37 if (!kObjPtrPoisoning || IsNull()) {
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070038 return true;
39 }
Vladimir Marko4df2d802018-09-27 16:42:44 +000040 return GetCookie() == GetCurrentTrimedCookie();
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070041}
42
Andreas Gampec73cb642017-02-22 10:11:30 -080043template<class MirrorType>
44inline void ObjPtr<MirrorType>::AssertValid() const {
45 if (kObjPtrPoisoning) {
Mathieu Chartier9d156d52016-10-06 17:44:26 -070046 CHECK(IsValid()) << "Stale object pointer " << PtrUnchecked() << " , expected cookie "
Vladimir Marko4df2d802018-09-27 16:42:44 +000047 << GetCurrentTrimedCookie() << " but got " << GetCookie();
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070048 }
49}
50
Andreas Gampec73cb642017-02-22 10:11:30 -080051template<class MirrorType>
52inline uintptr_t ObjPtr<MirrorType>::Encode(MirrorType* ptr) {
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070053 uintptr_t ref = reinterpret_cast<uintptr_t>(ptr);
Mathieu Chartier0795f232016-09-27 18:43:30 -070054 DCHECK_ALIGNED(ref, kObjectAlignment);
Andreas Gampec73cb642017-02-22 10:11:30 -080055 if (kObjPtrPoisoning && ref != 0) {
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070056 DCHECK_LE(ref, 0xFFFFFFFFU);
57 ref >>= kObjectAlignmentShift;
58 // Put cookie in high bits.
Vladimir Marko4df2d802018-09-27 16:42:44 +000059 ref |= GetCurrentTrimedCookie() << kCookieShift;
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070060 }
61 return ref;
62}
63
Andreas Gampec73cb642017-02-22 10:11:30 -080064template<class MirrorType>
65inline std::ostream& operator<<(std::ostream& os, ObjPtr<MirrorType> ptr) {
Mathieu Chartier0795f232016-09-27 18:43:30 -070066 // May be used for dumping bad pointers, do not use the checked version.
Mathieu Chartier1cc62e42016-10-03 18:01:28 -070067 return os << ptr.PtrUnchecked();
Mathieu Chartier0795f232016-09-27 18:43:30 -070068}
69
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070070} // namespace art
71
72#endif // ART_RUNTIME_OBJ_PTR_INL_H_