blob: f0964459135b8693dc1b39f7ebaeb7d17e304e8c [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 Gampe8764dc32019-01-07 15:20:12 -080020#include <ostream>
21
Andreas Gampe3b7dc352017-06-06 20:02:03 -070022#include "base/bit_utils.h"
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070023#include "obj_ptr.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070024#include "thread-current-inl.h"
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070025
26namespace art {
27
Andreas Gampec73cb642017-02-22 10:11:30 -080028template<class MirrorType>
Vladimir Marko4df2d802018-09-27 16:42:44 +000029inline uintptr_t ObjPtr<MirrorType>::GetCurrentTrimedCookie() {
30 Thread* self = Thread::Current();
31 if (UNLIKELY(self == nullptr)) {
32 return kCookieMask;
33 }
34 return self->GetPoisonObjectCookie() & kCookieMask;
35}
36
37template<class MirrorType>
Andreas Gampec73cb642017-02-22 10:11:30 -080038inline bool ObjPtr<MirrorType>::IsValid() const {
39 if (!kObjPtrPoisoning || IsNull()) {
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070040 return true;
41 }
Vladimir Marko4df2d802018-09-27 16:42:44 +000042 return GetCookie() == GetCurrentTrimedCookie();
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070043}
44
Andreas Gampec73cb642017-02-22 10:11:30 -080045template<class MirrorType>
46inline void ObjPtr<MirrorType>::AssertValid() const {
47 if (kObjPtrPoisoning) {
Mathieu Chartier9d156d52016-10-06 17:44:26 -070048 CHECK(IsValid()) << "Stale object pointer " << PtrUnchecked() << " , expected cookie "
Vladimir Marko4df2d802018-09-27 16:42:44 +000049 << GetCurrentTrimedCookie() << " but got " << GetCookie();
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070050 }
51}
52
Andreas Gampec73cb642017-02-22 10:11:30 -080053template<class MirrorType>
54inline uintptr_t ObjPtr<MirrorType>::Encode(MirrorType* ptr) {
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070055 uintptr_t ref = reinterpret_cast<uintptr_t>(ptr);
Mathieu Chartier0795f232016-09-27 18:43:30 -070056 DCHECK_ALIGNED(ref, kObjectAlignment);
Andreas Gampec73cb642017-02-22 10:11:30 -080057 if (kObjPtrPoisoning && ref != 0) {
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070058 DCHECK_LE(ref, 0xFFFFFFFFU);
59 ref >>= kObjectAlignmentShift;
60 // Put cookie in high bits.
Vladimir Marko4df2d802018-09-27 16:42:44 +000061 ref |= GetCurrentTrimedCookie() << kCookieShift;
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070062 }
63 return ref;
64}
65
Andreas Gampec73cb642017-02-22 10:11:30 -080066template<class MirrorType>
67inline std::ostream& operator<<(std::ostream& os, ObjPtr<MirrorType> ptr) {
Mathieu Chartier0795f232016-09-27 18:43:30 -070068 // May be used for dumping bad pointers, do not use the checked version.
Mathieu Chartier1cc62e42016-10-03 18:01:28 -070069 return os << ptr.PtrUnchecked();
Mathieu Chartier0795f232016-09-27 18:43:30 -070070}
71
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070072} // namespace art
73
74#endif // ART_RUNTIME_OBJ_PTR_INL_H_