java_lang_Runtime.cc revision 53b8b09fc80329539585dcf43657bc5f4ecefdff
1/*
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
17#include <dlfcn.h>
18#include <limits.h>
19#include <unistd.h>
20
21#include "gc/heap.h"
22#include "jni_internal.h"
23#include "mirror/class_loader.h"
24#include "runtime.h"
25#include "scoped_thread_state_change.h"
26#include "ScopedUtfChars.h"
27#include "sirt_ref-inl.h"
28
29namespace art {
30
31static void Runtime_gc(JNIEnv*, jclass) {
32  if (Runtime::Current()->IsExplicitGcDisabled()) {
33      LOG(INFO) << "Explicit GC skipped.";
34      return;
35  }
36  Runtime::Current()->GetHeap()->CollectGarbage(false);
37}
38
39static void Runtime_nativeExit(JNIEnv*, jclass, jint status) {
40  Runtime::Current()->CallExitHook(status);
41  exit(status);
42}
43
44static jstring Runtime_nativeLoad(JNIEnv* env, jclass, jstring javaFilename, jobject javaLoader, jstring javaLdLibraryPath) {
45  ScopedUtfChars filename(env, javaFilename);
46  if (filename.c_str() == NULL) {
47    return NULL;
48  }
49
50  if (javaLdLibraryPath != NULL) {
51    ScopedUtfChars ldLibraryPath(env, javaLdLibraryPath);
52    if (ldLibraryPath.c_str() == NULL) {
53      return NULL;
54    }
55    void* sym = dlsym(RTLD_DEFAULT, "android_update_LD_LIBRARY_PATH");
56    if (sym != NULL) {
57      typedef void (*Fn)(const char*);
58      Fn android_update_LD_LIBRARY_PATH = reinterpret_cast<Fn>(sym);
59      (*android_update_LD_LIBRARY_PATH)(ldLibraryPath.c_str());
60    } else {
61      LOG(ERROR) << "android_update_LD_LIBRARY_PATH not found; .so dependencies will not work!";
62    }
63  }
64
65  std::string detail;
66  {
67    ScopedObjectAccess soa(env);
68    SirtRef<mirror::ClassLoader> classLoader(soa.Self(),
69                                             soa.Decode<mirror::ClassLoader*>(javaLoader));
70    JavaVMExt* vm = Runtime::Current()->GetJavaVM();
71    bool success = vm->LoadNativeLibrary(filename.c_str(), classLoader, &detail);
72    if (success) {
73      return nullptr;
74    }
75  }
76
77  // Don't let a pending exception from JNI_OnLoad cause a CheckJNI issue with NewStringUTF.
78  env->ExceptionClear();
79  return env->NewStringUTF(detail.c_str());
80}
81
82static jlong Runtime_maxMemory(JNIEnv*, jclass) {
83  return Runtime::Current()->GetHeap()->GetMaxMemory();
84}
85
86static jlong Runtime_totalMemory(JNIEnv*, jclass) {
87  return Runtime::Current()->GetHeap()->GetTotalMemory();
88}
89
90static jlong Runtime_freeMemory(JNIEnv*, jclass) {
91  return Runtime::Current()->GetHeap()->GetFreeMemory();
92}
93
94static JNINativeMethod gMethods[] = {
95  NATIVE_METHOD(Runtime, freeMemory, "!()J"),
96  NATIVE_METHOD(Runtime, gc, "()V"),
97  NATIVE_METHOD(Runtime, maxMemory, "!()J"),
98  NATIVE_METHOD(Runtime, nativeExit, "(I)V"),
99  NATIVE_METHOD(Runtime, nativeLoad, "(Ljava/lang/String;Ljava/lang/ClassLoader;Ljava/lang/String;)Ljava/lang/String;"),
100  NATIVE_METHOD(Runtime, totalMemory, "!()J"),
101};
102
103void register_java_lang_Runtime(JNIEnv* env) {
104  REGISTER_NATIVE_METHODS("java/lang/Runtime");
105}
106
107}  // namespace art
108