blob: 3019e95d9d018dd0a2e5064cc5720d7abb8bd51b [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 Hughes28c7bfd2012-06-01 12:45:40 -070033static void Runtime_nativeExit(JNIEnv*, jclass, jint status) {
34 Runtime::Current()->CallExitHook(status);
Elliott Hughesbf86d042011-08-31 17:53:14 -070035 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 Hughes0512f022012-03-15 22:10:52 -070045static jstring Runtime_nativeLoad(JNIEnv* env, jclass, jstring javaFilename, jobject javaLoader) {
Elliott Hughesbf86d042011-08-31 17:53:14 -070046 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 Hughes1bac54f2012-03-16 12:48:31 -070062static jlong Runtime_maxMemory(JNIEnv*, jclass) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080063 return Runtime::Current()->GetHeap()->GetMaxMemory();
Elliott Hughesbf86d042011-08-31 17:53:14 -070064}
65
Elliott Hughes1bac54f2012-03-16 12:48:31 -070066static jlong Runtime_totalMemory(JNIEnv*, jclass) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080067 return Runtime::Current()->GetHeap()->GetTotalMemory();
Elliott Hughesbf86d042011-08-31 17:53:14 -070068}
69
Elliott Hughes1bac54f2012-03-16 12:48:31 -070070static jlong Runtime_freeMemory(JNIEnv*, jclass) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080071 return Runtime::Current()->GetHeap()->GetFreeMemory();
Elliott Hughesbf86d042011-08-31 17:53:14 -070072}
73
74static JNINativeMethod gMethods[] = {
Elliott Hugheseac76672012-05-24 21:56:51 -070075 NATIVE_METHOD(Runtime, freeMemory, "()J"),
76 NATIVE_METHOD(Runtime, gc, "()V"),
77 NATIVE_METHOD(Runtime, maxMemory, "()J"),
Elliott Hughes28c7bfd2012-06-01 12:45:40 -070078 NATIVE_METHOD(Runtime, nativeExit, "(I)V"),
Elliott Hugheseac76672012-05-24 21:56:51 -070079 NATIVE_METHOD(Runtime, nativeLoad, "(Ljava/lang/String;Ljava/lang/ClassLoader;)Ljava/lang/String;"),
80 NATIVE_METHOD(Runtime, totalMemory, "()J"),
Elliott Hughesbf86d042011-08-31 17:53:14 -070081};
82
Elliott Hughesbf86d042011-08-31 17:53:14 -070083void register_java_lang_Runtime(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -070084 REGISTER_NATIVE_METHODS("java/lang/Runtime");
Elliott Hughesbf86d042011-08-31 17:53:14 -070085}
86
87} // namespace art