Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 17 | #include <limits.h> |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 18 | #include <unistd.h> |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 19 | |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 20 | #include "class_loader.h" |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 21 | #include "heap.h" |
| 22 | #include "jni_internal.h" |
| 23 | #include "object.h" |
| 24 | #include "runtime.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 25 | #include "scoped_thread_state_change.h" |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 26 | #include "ScopedUtfChars.h" |
| 27 | |
| 28 | namespace art { |
| 29 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 30 | static void Runtime_gc(JNIEnv*, jclass) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 31 | Runtime::Current()->GetHeap()->CollectGarbage(false); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 32 | } |
| 33 | |
Elliott Hughes | 28c7bfd | 2012-06-01 12:45:40 -0700 | [diff] [blame] | 34 | static void Runtime_nativeExit(JNIEnv*, jclass, jint status) { |
| 35 | Runtime::Current()->CallExitHook(status); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 36 | exit(status); |
| 37 | } |
| 38 | |
| 39 | /* |
| 40 | * static String nativeLoad(String filename, ClassLoader loader) |
| 41 | * |
| 42 | * Load the specified full path as a dynamic library filled with |
| 43 | * JNI-compatible methods. Returns null on success, or a failure |
| 44 | * message on failure. |
| 45 | */ |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 46 | static jstring Runtime_nativeLoad(JNIEnv* env, jclass, jstring javaFilename, jobject javaLoader) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 47 | ScopedObjectAccess soa(env); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 48 | ScopedUtfChars filename(env, javaFilename); |
| 49 | if (filename.c_str() == NULL) { |
| 50 | return NULL; |
| 51 | } |
| 52 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 53 | ClassLoader* classLoader = soa.Decode<ClassLoader*>(javaLoader); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 54 | std::string detail; |
| 55 | JavaVMExt* vm = Runtime::Current()->GetJavaVM(); |
| 56 | bool success = vm->LoadNativeLibrary(filename.c_str(), classLoader, detail); |
| 57 | if (success) { |
| 58 | return NULL; |
| 59 | } |
| 60 | |
| 61 | return env->NewStringUTF(detail.c_str()); |
| 62 | } |
| 63 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 64 | static jlong Runtime_maxMemory(JNIEnv*, jclass) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 65 | return Runtime::Current()->GetHeap()->GetMaxMemory(); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 68 | static jlong Runtime_totalMemory(JNIEnv*, jclass) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 69 | return Runtime::Current()->GetHeap()->GetTotalMemory(); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 72 | static jlong Runtime_freeMemory(JNIEnv*, jclass) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 73 | return Runtime::Current()->GetHeap()->GetFreeMemory(); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | static JNINativeMethod gMethods[] = { |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 77 | NATIVE_METHOD(Runtime, freeMemory, "()J"), |
| 78 | NATIVE_METHOD(Runtime, gc, "()V"), |
| 79 | NATIVE_METHOD(Runtime, maxMemory, "()J"), |
Elliott Hughes | 28c7bfd | 2012-06-01 12:45:40 -0700 | [diff] [blame] | 80 | NATIVE_METHOD(Runtime, nativeExit, "(I)V"), |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 81 | NATIVE_METHOD(Runtime, nativeLoad, "(Ljava/lang/String;Ljava/lang/ClassLoader;)Ljava/lang/String;"), |
| 82 | NATIVE_METHOD(Runtime, totalMemory, "()J"), |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 83 | }; |
| 84 | |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 85 | void register_java_lang_Runtime(JNIEnv* env) { |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 86 | REGISTER_NATIVE_METHODS("java/lang/Runtime"); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | } // namespace art |