blob: 44703b70c37f269148620a9bf7e43f2b2c9624b0 [file] [log] [blame]
Elliott Hughesbf86d042011-08-31 17:53:14 -07001/*
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
28namespace art {
29
Elliott Hughes0512f022012-03-15 22:10:52 -070030static void Runtime_gc(JNIEnv*, jclass) {
Brian Carlstromb82b6872011-10-26 17:18:07 -070031 ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080032 Runtime::Current()->GetHeap()->CollectGarbage(false);
Elliott Hughesbf86d042011-08-31 17:53:14 -070033}
34
Elliott Hughes1bac54f2012-03-16 12:48:31 -070035static void Runtime_nativeExit(JNIEnv*, jclass, jint status, jboolean isExit) {
Elliott Hughesbf86d042011-08-31 17:53:14 -070036 // 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 Hughes0512f022012-03-15 22:10:52 -070050static jstring Runtime_nativeLoad(JNIEnv* env, jclass, jstring javaFilename, jobject javaLoader) {
Elliott Hughesbf86d042011-08-31 17:53:14 -070051 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 Hughes1bac54f2012-03-16 12:48:31 -070067static jlong Runtime_maxMemory(JNIEnv*, jclass) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080068 return Runtime::Current()->GetHeap()->GetMaxMemory();
Elliott Hughesbf86d042011-08-31 17:53:14 -070069}
70
Elliott Hughes1bac54f2012-03-16 12:48:31 -070071static jlong Runtime_totalMemory(JNIEnv*, jclass) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080072 return Runtime::Current()->GetHeap()->GetTotalMemory();
Elliott Hughesbf86d042011-08-31 17:53:14 -070073}
74
Elliott Hughes1bac54f2012-03-16 12:48:31 -070075static jlong Runtime_freeMemory(JNIEnv*, jclass) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080076 return Runtime::Current()->GetHeap()->GetFreeMemory();
Elliott Hughesbf86d042011-08-31 17:53:14 -070077}
78
79static 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 Hughesbf86d042011-08-31 17:53:14 -070088void register_java_lang_Runtime(JNIEnv* env) {
89 jniRegisterNativeMethods(env, "java/lang/Runtime", gMethods, NELEM(gMethods));
90}
91
92} // namespace art