blob: b7e81af806426edc343da2ec506ccea264d2f09a [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
Elliott Hughesbf86d042011-08-31 17:53:14 -070017#include <limits.h>
Elliott Hugheseac76672012-05-24 21:56:51 -070018#include <unistd.h>
Elliott Hughesbf86d042011-08-31 17:53:14 -070019
20#include "heap.h"
21#include "jni_internal.h"
22#include "object.h"
23#include "runtime.h"
Elliott Hughesbf86d042011-08-31 17:53:14 -070024#include "ScopedUtfChars.h"
25
26namespace art {
27
Elliott Hughes0512f022012-03-15 22:10:52 -070028static void Runtime_gc(JNIEnv*, jclass) {
Elliott Hughes34e06962012-04-09 13:55:55 -070029 ScopedThreadStateChange tsc(Thread::Current(), kRunnable);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080030 Runtime::Current()->GetHeap()->CollectGarbage(false);
Elliott Hughesbf86d042011-08-31 17:53:14 -070031}
32
Elliott Hughes1bac54f2012-03-16 12:48:31 -070033static void Runtime_nativeExit(JNIEnv*, jclass, jint status, jboolean isExit) {
Elliott Hughesbf86d042011-08-31 17:53:14 -070034 // isExit is true for System.exit and false for System.halt.
35 if (isExit) {
36 Runtime::Current()->CallExitHook(status);
37 }
38 exit(status);
39}
40
41/*
42 * static String nativeLoad(String filename, ClassLoader loader)
43 *
44 * Load the specified full path as a dynamic library filled with
45 * JNI-compatible methods. Returns null on success, or a failure
46 * message on failure.
47 */
Elliott Hughes0512f022012-03-15 22:10:52 -070048static jstring Runtime_nativeLoad(JNIEnv* env, jclass, jstring javaFilename, jobject javaLoader) {
Elliott Hughesbf86d042011-08-31 17:53:14 -070049 ScopedUtfChars filename(env, javaFilename);
50 if (filename.c_str() == NULL) {
51 return NULL;
52 }
53
54 ClassLoader* classLoader = Decode<ClassLoader*>(env, javaLoader);
55 std::string detail;
56 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
57 bool success = vm->LoadNativeLibrary(filename.c_str(), classLoader, detail);
58 if (success) {
59 return NULL;
60 }
61
62 return env->NewStringUTF(detail.c_str());
63}
64
Elliott Hughes1bac54f2012-03-16 12:48:31 -070065static jlong Runtime_maxMemory(JNIEnv*, jclass) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080066 return Runtime::Current()->GetHeap()->GetMaxMemory();
Elliott Hughesbf86d042011-08-31 17:53:14 -070067}
68
Elliott Hughes1bac54f2012-03-16 12:48:31 -070069static jlong Runtime_totalMemory(JNIEnv*, jclass) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080070 return Runtime::Current()->GetHeap()->GetTotalMemory();
Elliott Hughesbf86d042011-08-31 17:53:14 -070071}
72
Elliott Hughes1bac54f2012-03-16 12:48:31 -070073static jlong Runtime_freeMemory(JNIEnv*, jclass) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080074 return Runtime::Current()->GetHeap()->GetFreeMemory();
Elliott Hughesbf86d042011-08-31 17:53:14 -070075}
76
77static JNINativeMethod gMethods[] = {
Elliott Hugheseac76672012-05-24 21:56:51 -070078 NATIVE_METHOD(Runtime, freeMemory, "()J"),
79 NATIVE_METHOD(Runtime, gc, "()V"),
80 NATIVE_METHOD(Runtime, maxMemory, "()J"),
81 NATIVE_METHOD(Runtime, nativeExit, "(IZ)V"),
82 NATIVE_METHOD(Runtime, nativeLoad, "(Ljava/lang/String;Ljava/lang/ClassLoader;)Ljava/lang/String;"),
83 NATIVE_METHOD(Runtime, totalMemory, "()J"),
Elliott Hughesbf86d042011-08-31 17:53:14 -070084};
85
Elliott Hughesbf86d042011-08-31 17:53:14 -070086void register_java_lang_Runtime(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -070087 REGISTER_NATIVE_METHODS("java/lang/Runtime");
Elliott Hughesbf86d042011-08-31 17:53:14 -070088}
89
90} // namespace art