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 "jni.h"
18 
19 #include <iostream>
20 
21 #include "art_method.h"
22 #include "jit/jit.h"
23 #include "linear_alloc.h"
24 #include "nativehelper/ScopedUtfChars.h"
25 #include "runtime.h"
26 #include "scoped_thread_state_change-inl.h"
27 #include "thread-current-inl.h"
28 
29 namespace art {
30 namespace {
31 
Java_Main_getArtMethod(JNIEnv * env,jclass,jobject java_method)32 extern "C" JNIEXPORT jlong JNICALL Java_Main_getArtMethod(JNIEnv* env,
33                                                           jclass,
34                                                           jobject java_method) {
35   ScopedObjectAccess soa(env);
36   ArtMethod* method = ArtMethod::FromReflectedMethod(soa, java_method);
37   return static_cast<jlong>(reinterpret_cast<uintptr_t>(method));
38 }
39 
Java_Main_reuseArenaOfMethod(JNIEnv *,jclass,jlong art_method)40 extern "C" JNIEXPORT void JNICALL Java_Main_reuseArenaOfMethod(JNIEnv*,
41                                                                jclass,
42                                                                jlong art_method) {
43   // Create a new allocation and use it to request a specified amount of arenas.
44   // Hopefully one of them is a reused one, the one that covers the art_method pointer.
45   std::unique_ptr<LinearAlloc> alloc(Runtime::Current()->CreateLinearAlloc());
46   do {
47     // Ask for a byte - it's sufficient to get an arena and not have issues with size.
48     alloc->Alloc(Thread::Current(), 1);
49   } while (!alloc->Contains(reinterpret_cast<void*>(static_cast<uintptr_t>(art_method))));
50 }
51 
52 }  // namespace
53 }  // namespace art
54