blob: 3dfcf9e2cf3a645beceec9b4a3c4c1acfbe2f8f3 [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) {
36 CHECK(IsValid()) << "Stale object pointer " << DecodeUnchecked() << " , expected cookie "
37 << 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);
44 if (kPoison && ref != 0) {
45 DCHECK_LE(ref, 0xFFFFFFFFU);
46 ref >>= kObjectAlignmentShift;
47 // Put cookie in high bits.
48 Thread* self = Thread::Current();
49 DCHECK(self != nullptr);
50 ref |= self->GetPoisonObjectCookie() << kCookieShift;
51 }
52 return ref;
53}
54
55} // namespace art
56
57#endif // ART_RUNTIME_OBJ_PTR_INL_H_