1ab2ce84d4995f05c38c5ebfefc6683b244a36260Alexey Grebenkin/*
2ab2ce84d4995f05c38c5ebfefc6683b244a36260Alexey Grebenkin * Copyright (C) 2015 The Android Open Source Project
3ab2ce84d4995f05c38c5ebfefc6683b244a36260Alexey Grebenkin *
4ab2ce84d4995f05c38c5ebfefc6683b244a36260Alexey Grebenkin * Licensed under the Apache License, Version 2.0 (the "License");
5ab2ce84d4995f05c38c5ebfefc6683b244a36260Alexey Grebenkin * you may not use this file except in compliance with the License.
6ab2ce84d4995f05c38c5ebfefc6683b244a36260Alexey Grebenkin * You may obtain a copy of the License at
7ab2ce84d4995f05c38c5ebfefc6683b244a36260Alexey Grebenkin *
8ab2ce84d4995f05c38c5ebfefc6683b244a36260Alexey Grebenkin *      http://www.apache.org/licenses/LICENSE-2.0
9ab2ce84d4995f05c38c5ebfefc6683b244a36260Alexey Grebenkin *
10ab2ce84d4995f05c38c5ebfefc6683b244a36260Alexey Grebenkin * Unless required by applicable law or agreed to in writing, software
11ab2ce84d4995f05c38c5ebfefc6683b244a36260Alexey Grebenkin * distributed under the License is distributed on an "AS IS" BASIS,
12ab2ce84d4995f05c38c5ebfefc6683b244a36260Alexey Grebenkin * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13ab2ce84d4995f05c38c5ebfefc6683b244a36260Alexey Grebenkin * See the License for the specific language governing permissions and
14ab2ce84d4995f05c38c5ebfefc6683b244a36260Alexey Grebenkin * limitations under the License.
15ab2ce84d4995f05c38c5ebfefc6683b244a36260Alexey Grebenkin */
16ab2ce84d4995f05c38c5ebfefc6683b244a36260Alexey Grebenkin
17ab2ce84d4995f05c38c5ebfefc6683b244a36260Alexey Grebenkin#include "jni.h"
18ab2ce84d4995f05c38c5ebfefc6683b244a36260Alexey Grebenkin
19ab2ce84d4995f05c38c5ebfefc6683b244a36260Alexey Grebenkin#include <iostream>
20ab2ce84d4995f05c38c5ebfefc6683b244a36260Alexey Grebenkin
21ab2ce84d4995f05c38c5ebfefc6683b244a36260Alexey Grebenkin#include "art_method.h"
22ab2ce84d4995f05c38c5ebfefc6683b244a36260Alexey Grebenkin#include "jit/jit.h"
23ab2ce84d4995f05c38c5ebfefc6683b244a36260Alexey Grebenkin#include "linear_alloc.h"
24ab2ce84d4995f05c38c5ebfefc6683b244a36260Alexey Grebenkin#include "nativehelper/ScopedUtfChars.h"
25ab2ce84d4995f05c38c5ebfefc6683b244a36260Alexey Grebenkin#include "runtime.h"
26ab2ce84d4995f05c38c5ebfefc6683b244a36260Alexey Grebenkin#include "scoped_thread_state_change-inl.h"
27ab2ce84d4995f05c38c5ebfefc6683b244a36260Alexey Grebenkin#include "thread-current-inl.h"
28ab2ce84d4995f05c38c5ebfefc6683b244a36260Alexey Grebenkin
29ab2ce84d4995f05c38c5ebfefc6683b244a36260Alexey Grebenkinnamespace art {
30ab2ce84d4995f05c38c5ebfefc6683b244a36260Alexey Grebenkinnamespace {
31ab2ce84d4995f05c38c5ebfefc6683b244a36260Alexey Grebenkin
32ab2ce84d4995f05c38c5ebfefc6683b244a36260Alexey Grebenkinextern "C" JNIEXPORT jlong JNICALL Java_Main_getArtMethod(JNIEnv* env,
33ab2ce84d4995f05c38c5ebfefc6683b244a36260Alexey Grebenkin                                                          jclass,
34ab2ce84d4995f05c38c5ebfefc6683b244a36260Alexey Grebenkin                                                          jobject java_method) {
35ab2ce84d4995f05c38c5ebfefc6683b244a36260Alexey Grebenkin  ScopedObjectAccess soa(env);
36ab2ce84d4995f05c38c5ebfefc6683b244a36260Alexey Grebenkin  ArtMethod* method = ArtMethod::FromReflectedMethod(soa, java_method);
37ab2ce84d4995f05c38c5ebfefc6683b244a36260Alexey Grebenkin  return static_cast<jlong>(reinterpret_cast<uintptr_t>(method));
38ab2ce84d4995f05c38c5ebfefc6683b244a36260Alexey Grebenkin}
39ab2ce84d4995f05c38c5ebfefc6683b244a36260Alexey Grebenkin
40499b8f89e1c7cb096eca17740d238d40d63f9e85Nicolas Geoffrayextern "C" JNIEXPORT void JNICALL Java_Main_reuseArenaOfMethod(JNIEnv*,
41499b8f89e1c7cb096eca17740d238d40d63f9e85Nicolas Geoffray                                                               jclass,
42499b8f89e1c7cb096eca17740d238d40d63f9e85Nicolas Geoffray                                                               jlong art_method) {
43ab2ce84d4995f05c38c5ebfefc6683b244a36260Alexey Grebenkin  // Create a new allocation and use it to request a specified amount of arenas.
44ab2ce84d4995f05c38c5ebfefc6683b244a36260Alexey Grebenkin  // Hopefully one of them is a reused one, the one that covers the art_method pointer.
45ab2ce84d4995f05c38c5ebfefc6683b244a36260Alexey Grebenkin  std::unique_ptr<LinearAlloc> alloc(Runtime::Current()->CreateLinearAlloc());
46499b8f89e1c7cb096eca17740d238d40d63f9e85Nicolas Geoffray  do {
47ab2ce84d4995f05c38c5ebfefc6683b244a36260Alexey Grebenkin    // Ask for a byte - it's sufficient to get an arena and not have issues with size.
48ab2ce84d4995f05c38c5ebfefc6683b244a36260Alexey Grebenkin    alloc->Alloc(Thread::Current(), 1);
49499b8f89e1c7cb096eca17740d238d40d63f9e85Nicolas Geoffray  } while (!alloc->Contains(reinterpret_cast<void*>(static_cast<uintptr_t>(art_method))));
50ab2ce84d4995f05c38c5ebfefc6683b244a36260Alexey Grebenkin}
51ab2ce84d4995f05c38c5ebfefc6683b244a36260Alexey Grebenkin
52ab2ce84d4995f05c38c5ebfefc6683b244a36260Alexey Grebenkin}  // namespace
53ab2ce84d4995f05c38c5ebfefc6683b244a36260Alexey Grebenkin}  // namespace art
54