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