blob: a2bd399434a649119a799173fb928d6b2566f5af [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
17#include <unistd.h>
18#include <limits.h>
19
20#include "heap.h"
21#include "jni_internal.h"
22#include "object.h"
23#include "runtime.h"
24
25#include "JniConstants.h" // Last to avoid problems with LOG redefinition.
26#include "ScopedUtfChars.h"
27
28namespace art {
29
30namespace {
31
32void Runtime_gc(JNIEnv*, jclass) {
Elliott Hughesbf86d042011-08-31 17:53:14 -070033 Heap::CollectGarbage();
34}
35
36void Runtime_nativeExit(JNIEnv* env, jclass, jint status, jboolean isExit) {
37 // isExit is true for System.exit and false for System.halt.
38 if (isExit) {
39 Runtime::Current()->CallExitHook(status);
40 }
41 exit(status);
42}
43
44/*
45 * static String nativeLoad(String filename, ClassLoader loader)
46 *
47 * Load the specified full path as a dynamic library filled with
48 * JNI-compatible methods. Returns null on success, or a failure
49 * message on failure.
50 */
51jstring Runtime_nativeLoad(JNIEnv* env, jclass, jstring javaFilename, jobject javaLoader) {
52 ScopedUtfChars filename(env, javaFilename);
53 if (filename.c_str() == NULL) {
54 return NULL;
55 }
56
57 ClassLoader* classLoader = Decode<ClassLoader*>(env, javaLoader);
58 std::string detail;
59 JavaVMExt* vm = Runtime::Current()->GetJavaVM();
60 bool success = vm->LoadNativeLibrary(filename.c_str(), classLoader, detail);
61 if (success) {
62 return NULL;
63 }
64
65 return env->NewStringUTF(detail.c_str());
66}
67
68jlong Runtime_maxMemory(JNIEnv* env, jclass) {
69 return Heap::GetMaxMemory();
70}
71
72jlong Runtime_totalMemory(JNIEnv* env, jclass) {
73 return Heap::GetTotalMemory();
74}
75
76jlong Runtime_freeMemory(JNIEnv* env, jclass) {
77 return Heap::GetFreeMemory();
78}
79
80static JNINativeMethod gMethods[] = {
81 NATIVE_METHOD(Runtime, freeMemory, "()J"),
82 NATIVE_METHOD(Runtime, gc, "()V"),
83 NATIVE_METHOD(Runtime, maxMemory, "()J"),
84 NATIVE_METHOD(Runtime, nativeExit, "(IZ)V"),
85 NATIVE_METHOD(Runtime, nativeLoad, "(Ljava/lang/String;Ljava/lang/ClassLoader;)Ljava/lang/String;"),
86 NATIVE_METHOD(Runtime, totalMemory, "()J"),
87};
88
89} // namespace
90
91void register_java_lang_Runtime(JNIEnv* env) {
92 jniRegisterNativeMethods(env, "java/lang/Runtime", gMethods, NELEM(gMethods));
93}
94
95} // namespace art