blob: 1b657b1801deff98c91f9c9b1c859f27e7f981c6 [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
Ian Rogers365c1022012-06-22 15:05:28 -070020#include "class_loader.h"
Elliott Hughesbf86d042011-08-31 17:53:14 -070021#include "heap.h"
22#include "jni_internal.h"
23#include "object.h"
24#include "runtime.h"
Ian Rogers365c1022012-06-22 15:05:28 -070025#include "scoped_jni_thread_state.h"
Elliott Hughesbf86d042011-08-31 17:53:14 -070026#include "ScopedUtfChars.h"
27
28namespace art {
29
Ian Rogers365c1022012-06-22 15:05:28 -070030static void Runtime_gc(JNIEnv* env, jclass) {
31 ScopedJniThreadState ts(env);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080032 Runtime::Current()->GetHeap()->CollectGarbage(false);
Elliott Hughesbf86d042011-08-31 17:53:14 -070033}
34
Elliott Hughes28c7bfd2012-06-01 12:45:40 -070035static void Runtime_nativeExit(JNIEnv*, jclass, jint status) {
36 Runtime::Current()->CallExitHook(status);
Elliott Hughesbf86d042011-08-31 17:53:14 -070037 exit(status);
38}
39
40/*
41 * static String nativeLoad(String filename, ClassLoader loader)
42 *
43 * Load the specified full path as a dynamic library filled with
44 * JNI-compatible methods. Returns null on success, or a failure
45 * message on failure.
46 */
Elliott Hughes0512f022012-03-15 22:10:52 -070047static jstring Runtime_nativeLoad(JNIEnv* env, jclass, jstring javaFilename, jobject javaLoader) {
Ian Rogers365c1022012-06-22 15:05:28 -070048 ScopedJniThreadState ts(env);
Elliott Hughesbf86d042011-08-31 17:53:14 -070049 ScopedUtfChars filename(env, javaFilename);
50 if (filename.c_str() == NULL) {
51 return NULL;
52 }
53
Ian Rogers365c1022012-06-22 15:05:28 -070054 ClassLoader* classLoader = ts.Decode<ClassLoader*>(javaLoader);
Elliott Hughesbf86d042011-08-31 17:53:14 -070055 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"),
Elliott Hughes28c7bfd2012-06-01 12:45:40 -070081 NATIVE_METHOD(Runtime, nativeExit, "(I)V"),
Elliott Hugheseac76672012-05-24 21:56:51 -070082 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