blob: 07af16226aed590b3223298a2b3ad7fce6635702 [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
30namespace {
31
32void Runtime_gc(JNIEnv*, jclass) {
Brian Carlstromb82b6872011-10-26 17:18:07 -070033 ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080034 Runtime::Current()->GetHeap()->CollectGarbage(false);
Elliott Hughesbf86d042011-08-31 17:53:14 -070035}
36
37void Runtime_nativeExit(JNIEnv* env, jclass, jint status, jboolean isExit) {
38 // isExit is true for System.exit and false for System.halt.
39 if (isExit) {
40 Runtime::Current()->CallExitHook(status);
41 }
42 exit(status);
43}
44
45/*
46 * static String nativeLoad(String filename, ClassLoader loader)
47 *
48 * Load the specified full path as a dynamic library filled with
49 * JNI-compatible methods. Returns null on success, or a failure
50 * message on failure.
51 */
52jstring Runtime_nativeLoad(JNIEnv* env, jclass, jstring javaFilename, jobject javaLoader) {
53 ScopedUtfChars filename(env, javaFilename);
54 if (filename.c_str() == NULL) {
55 return NULL;
56 }
57
58 ClassLoader* classLoader = Decode<ClassLoader*>(env, javaLoader);
59 std::string detail;
60 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
61 bool success = vm->LoadNativeLibrary(filename.c_str(), classLoader, detail);
62 if (success) {
63 return NULL;
64 }
65
66 return env->NewStringUTF(detail.c_str());
67}
68
69jlong Runtime_maxMemory(JNIEnv* env, jclass) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080070 return Runtime::Current()->GetHeap()->GetMaxMemory();
Elliott Hughesbf86d042011-08-31 17:53:14 -070071}
72
73jlong Runtime_totalMemory(JNIEnv* env, jclass) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080074 return Runtime::Current()->GetHeap()->GetTotalMemory();
Elliott Hughesbf86d042011-08-31 17:53:14 -070075}
76
77jlong Runtime_freeMemory(JNIEnv* env, jclass) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080078 return Runtime::Current()->GetHeap()->GetFreeMemory();
Elliott Hughesbf86d042011-08-31 17:53:14 -070079}
80
81static JNINativeMethod gMethods[] = {
82 NATIVE_METHOD(Runtime, freeMemory, "()J"),
83 NATIVE_METHOD(Runtime, gc, "()V"),
84 NATIVE_METHOD(Runtime, maxMemory, "()J"),
85 NATIVE_METHOD(Runtime, nativeExit, "(IZ)V"),
86 NATIVE_METHOD(Runtime, nativeLoad, "(Ljava/lang/String;Ljava/lang/ClassLoader;)Ljava/lang/String;"),
87 NATIVE_METHOD(Runtime, totalMemory, "()J"),
88};
89
90} // namespace
91
92void register_java_lang_Runtime(JNIEnv* env) {
93 jniRegisterNativeMethods(env, "java/lang/Runtime", gMethods, NELEM(gMethods));
94}
95
96} // namespace art