blob: d0be6dc98129547f746ace0db4b3a8d12f1ca860 [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
20#include "obj_ptr.h"
21#include "thread-inl.h"
22
23namespace art {
24
25template<class MirrorType, bool kPoison>
26inline bool ObjPtr<MirrorType, kPoison>::IsValid() const {
27 if (!kPoison || IsNull()) {
28 return true;
29 }
30 return GetCookie() == TrimCookie(Thread::Current()->GetPoisonObjectCookie());
31}
32
33template<class MirrorType, bool kPoison>
34inline void ObjPtr<MirrorType, kPoison>::AssertValid() const {
35 if (kPoison) {
Mathieu Chartier9d156d52016-10-06 17:44:26 -070036 CHECK(IsValid()) << "Stale object pointer " << PtrUnchecked() << " , expected cookie "
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070037 << TrimCookie(Thread::Current()->GetPoisonObjectCookie()) << " but got " << GetCookie();
38 }
39}
40
41template<class MirrorType, bool kPoison>
42inline uintptr_t ObjPtr<MirrorType, kPoison>::Encode(MirrorType* ptr) {
43 uintptr_t ref = reinterpret_cast<uintptr_t>(ptr);
Mathieu Chartier0795f232016-09-27 18:43:30 -070044 DCHECK_ALIGNED(ref, kObjectAlignment);
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070045 if (kPoison && ref != 0) {
46 DCHECK_LE(ref, 0xFFFFFFFFU);
47 ref >>= kObjectAlignmentShift;
48 // Put cookie in high bits.
49 Thread* self = Thread::Current();
50 DCHECK(self != nullptr);
51 ref |= self->GetPoisonObjectCookie() << kCookieShift;
52 }
53 return ref;
54}
55
Mathieu Chartier0795f232016-09-27 18:43:30 -070056template<class MirrorType, bool kPoison>
57inline std::ostream& operator<<(std::ostream& os, ObjPtr<MirrorType, kPoison> ptr) {
58 // May be used for dumping bad pointers, do not use the checked version.
Mathieu Chartier1cc62e42016-10-03 18:01:28 -070059 return os << ptr.PtrUnchecked();
Mathieu Chartier0795f232016-09-27 18:43:30 -070060}
61
Mathieu Chartiera59d9b22016-09-26 18:13:17 -070062} // namespace art
63
64#endif // ART_RUNTIME_OBJ_PTR_INL_H_