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