blob: d197b7340f017a9940f9395f9c5ae0e829e210db [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 Hughesc8fece32013-01-02 11:27:23 -080017#include <dlfcn.h>
Elliott Hughesbf86d042011-08-31 17:53:14 -070018#include <limits.h>
Elliott Hugheseac76672012-05-24 21:56:51 -070019#include <unistd.h>
Elliott Hughesbf86d042011-08-31 17:53:14 -070020
Ian Rogers365c1022012-06-22 15:05:28 -070021#include "class_loader.h"
Elliott Hughesbf86d042011-08-31 17:53:14 -070022#include "heap.h"
23#include "jni_internal.h"
24#include "object.h"
25#include "runtime.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070026#include "scoped_thread_state_change.h"
Elliott Hughesbf86d042011-08-31 17:53:14 -070027#include "ScopedUtfChars.h"
28
29namespace art {
30
Ian Rogers00f7d0e2012-07-19 15:28:27 -070031static void Runtime_gc(JNIEnv*, jclass) {
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
Elliott Hughesc8fece32013-01-02 11:27:23 -080040static jstring Runtime_nativeLoad(JNIEnv* env, jclass, jstring javaFilename, jobject javaLoader, jstring javaLdLibraryPath) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070041 ScopedObjectAccess soa(env);
Elliott Hughesbf86d042011-08-31 17:53:14 -070042 ScopedUtfChars filename(env, javaFilename);
43 if (filename.c_str() == NULL) {
44 return NULL;
45 }
46
Elliott Hughesc8fece32013-01-02 11:27:23 -080047 if (javaLdLibraryPath != NULL) {
48 ScopedUtfChars ldLibraryPath(env, javaLdLibraryPath);
49 if (ldLibraryPath.c_str() == NULL) {
50 return NULL;
51 }
52 void* sym = dlsym(RTLD_DEFAULT, "android_update_LD_LIBRARY_PATH");
53 if (sym != NULL) {
54 typedef void (*Fn)(const char*);
55 Fn android_update_LD_LIBRARY_PATH = reinterpret_cast<Fn>(sym);
56 (*android_update_LD_LIBRARY_PATH)(ldLibraryPath.c_str());
57 } else {
58 LOG(ERROR) << "android_update_LD_LIBRARY_PATH not found; .so dependencies will not work!";
59 }
60 }
61
Ian Rogers00f7d0e2012-07-19 15:28:27 -070062 ClassLoader* classLoader = soa.Decode<ClassLoader*>(javaLoader);
Elliott Hughesbf86d042011-08-31 17:53:14 -070063 std::string detail;
64 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
65 bool success = vm->LoadNativeLibrary(filename.c_str(), classLoader, detail);
66 if (success) {
67 return NULL;
68 }
69
70 return env->NewStringUTF(detail.c_str());
71}
72
Elliott Hughes1bac54f2012-03-16 12:48:31 -070073static jlong Runtime_maxMemory(JNIEnv*, jclass) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080074 return Runtime::Current()->GetHeap()->GetMaxMemory();
Elliott Hughesbf86d042011-08-31 17:53:14 -070075}
76
Elliott Hughes1bac54f2012-03-16 12:48:31 -070077static jlong Runtime_totalMemory(JNIEnv*, jclass) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080078 return Runtime::Current()->GetHeap()->GetTotalMemory();
Elliott Hughesbf86d042011-08-31 17:53:14 -070079}
80
Elliott Hughes1bac54f2012-03-16 12:48:31 -070081static jlong Runtime_freeMemory(JNIEnv*, jclass) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080082 return Runtime::Current()->GetHeap()->GetFreeMemory();
Elliott Hughesbf86d042011-08-31 17:53:14 -070083}
84
85static JNINativeMethod gMethods[] = {
Elliott Hugheseac76672012-05-24 21:56:51 -070086 NATIVE_METHOD(Runtime, freeMemory, "()J"),
87 NATIVE_METHOD(Runtime, gc, "()V"),
88 NATIVE_METHOD(Runtime, maxMemory, "()J"),
Elliott Hughes28c7bfd2012-06-01 12:45:40 -070089 NATIVE_METHOD(Runtime, nativeExit, "(I)V"),
Elliott Hughesc8fece32013-01-02 11:27:23 -080090 NATIVE_METHOD(Runtime, nativeLoad, "(Ljava/lang/String;Ljava/lang/ClassLoader;Ljava/lang/String;)Ljava/lang/String;"),
Elliott Hugheseac76672012-05-24 21:56:51 -070091 NATIVE_METHOD(Runtime, totalMemory, "()J"),
Elliott Hughesbf86d042011-08-31 17:53:14 -070092};
93
Elliott Hughesbf86d042011-08-31 17:53:14 -070094void register_java_lang_Runtime(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -070095 REGISTER_NATIVE_METHODS("java/lang/Runtime");
Elliott Hughesbf86d042011-08-31 17:53:14 -070096}
97
98} // namespace art