blob: 6dc850e83f87a249c3156547ab0b412413ac4ffa [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 Rogers00f7d0e2012-07-19 15:28:27 -070025#include "scoped_thread_state_change.h"
Elliott Hughesbf86d042011-08-31 17:53:14 -070026#include "ScopedUtfChars.h"
27
28namespace art {
29
Ian Rogers00f7d0e2012-07-19 15:28:27 -070030static void Runtime_gc(JNIEnv*, jclass) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080031 Runtime::Current()->GetHeap()->CollectGarbage(false);
Elliott Hughesbf86d042011-08-31 17:53:14 -070032}
33
Elliott Hughes28c7bfd2012-06-01 12:45:40 -070034static void Runtime_nativeExit(JNIEnv*, jclass, jint status) {
35 Runtime::Current()->CallExitHook(status);
Elliott Hughesbf86d042011-08-31 17:53:14 -070036 exit(status);
37}
38
39/*
40 * static String nativeLoad(String filename, ClassLoader loader)
41 *
42 * Load the specified full path as a dynamic library filled with
43 * JNI-compatible methods. Returns null on success, or a failure
44 * message on failure.
45 */
Elliott Hughes0512f022012-03-15 22:10:52 -070046static jstring Runtime_nativeLoad(JNIEnv* env, jclass, jstring javaFilename, jobject javaLoader) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070047 ScopedObjectAccess soa(env);
Elliott Hughesbf86d042011-08-31 17:53:14 -070048 ScopedUtfChars filename(env, javaFilename);
49 if (filename.c_str() == NULL) {
50 return NULL;
51 }
52
Ian Rogers00f7d0e2012-07-19 15:28:27 -070053 ClassLoader* classLoader = soa.Decode<ClassLoader*>(javaLoader);
Elliott Hughesbf86d042011-08-31 17:53:14 -070054 std::string detail;
55 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
56 bool success = vm->LoadNativeLibrary(filename.c_str(), classLoader, detail);
57 if (success) {
58 return NULL;
59 }
60
61 return env->NewStringUTF(detail.c_str());
62}
63
Elliott Hughes1bac54f2012-03-16 12:48:31 -070064static jlong Runtime_maxMemory(JNIEnv*, jclass) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080065 return Runtime::Current()->GetHeap()->GetMaxMemory();
Elliott Hughesbf86d042011-08-31 17:53:14 -070066}
67
Elliott Hughes1bac54f2012-03-16 12:48:31 -070068static jlong Runtime_totalMemory(JNIEnv*, jclass) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080069 return Runtime::Current()->GetHeap()->GetTotalMemory();
Elliott Hughesbf86d042011-08-31 17:53:14 -070070}
71
Elliott Hughes1bac54f2012-03-16 12:48:31 -070072static jlong Runtime_freeMemory(JNIEnv*, jclass) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080073 return Runtime::Current()->GetHeap()->GetFreeMemory();
Elliott Hughesbf86d042011-08-31 17:53:14 -070074}
75
76static JNINativeMethod gMethods[] = {
Elliott Hugheseac76672012-05-24 21:56:51 -070077 NATIVE_METHOD(Runtime, freeMemory, "()J"),
78 NATIVE_METHOD(Runtime, gc, "()V"),
79 NATIVE_METHOD(Runtime, maxMemory, "()J"),
Elliott Hughes28c7bfd2012-06-01 12:45:40 -070080 NATIVE_METHOD(Runtime, nativeExit, "(I)V"),
Elliott Hugheseac76672012-05-24 21:56:51 -070081 NATIVE_METHOD(Runtime, nativeLoad, "(Ljava/lang/String;Ljava/lang/ClassLoader;)Ljava/lang/String;"),
82 NATIVE_METHOD(Runtime, totalMemory, "()J"),
Elliott Hughesbf86d042011-08-31 17:53:14 -070083};
84
Elliott Hughesbf86d042011-08-31 17:53:14 -070085void register_java_lang_Runtime(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -070086 REGISTER_NATIVE_METHODS("java/lang/Runtime");
Elliott Hughesbf86d042011-08-31 17:53:14 -070087}
88
89} // namespace art