Nicolas Geoffray | 32c9ea5 | 2015-06-12 14:52:33 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | #include "art_method-inl.h" |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame^] | 18 | #include "base/enums.h" |
Nicolas Geoffray | 32c9ea5 | 2015-06-12 14:52:33 +0100 | [diff] [blame] | 19 | #include "jni.h" |
| 20 | #include "scoped_thread_state_change.h" |
| 21 | #include "stack.h" |
| 22 | #include "thread.h" |
| 23 | |
| 24 | namespace art { |
| 25 | |
| 26 | namespace { |
| 27 | |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 28 | extern "C" JNIEXPORT jobject JNICALL Java_Main_cloneResolvedMethods(JNIEnv* env, |
| 29 | jclass, |
| 30 | jclass cls) { |
Nicolas Geoffray | 32c9ea5 | 2015-06-12 14:52:33 +0100 | [diff] [blame] | 31 | ScopedObjectAccess soa(Thread::Current()); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 32 | mirror::DexCache* dex_cache = soa.Decode<mirror::Class*>(cls)->GetDexCache(); |
| 33 | size_t num_methods = dex_cache->NumResolvedMethods(); |
| 34 | ArtMethod** methods = dex_cache->GetResolvedMethods(); |
| 35 | CHECK_EQ(num_methods != 0u, methods != nullptr); |
| 36 | if (num_methods == 0u) { |
| 37 | return nullptr; |
| 38 | } |
| 39 | jarray array; |
| 40 | if (sizeof(void*) == 4) { |
| 41 | array = env->NewIntArray(num_methods); |
| 42 | } else { |
| 43 | array = env->NewLongArray(num_methods); |
| 44 | } |
| 45 | CHECK(array != nullptr); |
| 46 | mirror::PointerArray* pointer_array = soa.Decode<mirror::PointerArray*>(array); |
| 47 | for (size_t i = 0; i != num_methods; ++i) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame^] | 48 | ArtMethod* method = mirror::DexCache::GetElementPtrSize(methods, i, kRuntimePointerSize); |
| 49 | pointer_array->SetElementPtrSize(i, method, kRuntimePointerSize); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 50 | } |
| 51 | return array; |
Nicolas Geoffray | 32c9ea5 | 2015-06-12 14:52:33 +0100 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | extern "C" JNIEXPORT void JNICALL Java_Main_restoreResolvedMethods( |
| 55 | JNIEnv*, jclass, jclass cls, jobject old_cache) { |
| 56 | ScopedObjectAccess soa(Thread::Current()); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 57 | mirror::DexCache* dex_cache = soa.Decode<mirror::Class*>(cls)->GetDexCache(); |
| 58 | size_t num_methods = dex_cache->NumResolvedMethods(); |
| 59 | ArtMethod** methods = soa.Decode<mirror::Class*>(cls)->GetDexCache()->GetResolvedMethods(); |
| 60 | CHECK_EQ(num_methods != 0u, methods != nullptr); |
Nicolas Geoffray | 32c9ea5 | 2015-06-12 14:52:33 +0100 | [diff] [blame] | 61 | mirror::PointerArray* old = soa.Decode<mirror::PointerArray*>(old_cache); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 62 | CHECK_EQ(methods != nullptr, old != nullptr); |
| 63 | CHECK_EQ(num_methods, static_cast<size_t>(old->GetLength())); |
| 64 | for (size_t i = 0; i != num_methods; ++i) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame^] | 65 | ArtMethod* method = old->GetElementPtrSize<ArtMethod*>(i, kRuntimePointerSize); |
| 66 | mirror::DexCache::SetElementPtrSize(methods, i, method, kRuntimePointerSize); |
Nicolas Geoffray | 32c9ea5 | 2015-06-12 14:52:33 +0100 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
| 70 | } // namespace |
| 71 | |
| 72 | } // namespace art |