java_lang_Runtime.cpp revision e8e1ddccd616e8226b7cc1e4e9fdb327429249e8
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/* 18 * java.lang.Runtime 19 */ 20#include "Dalvik.h" 21#include "native/InternalNativePriv.h" 22#include <unistd.h> 23#include <limits.h> 24 25/* 26 * public void gc() 27 * 28 * Initiate a gc. 29 */ 30static void Dalvik_java_lang_Runtime_gc(const u4* args, JValue* pResult) 31{ 32 UNUSED_PARAMETER(args); 33 34 dvmCollectGarbage(); 35 RETURN_VOID(); 36} 37 38/* 39 * private static void nativeExit(int code, boolean isExit) 40 * 41 * Runtime.exit() calls this after doing shutdown processing. Runtime.halt() 42 * uses this as well. 43 */ 44static void Dalvik_java_lang_Runtime_nativeExit(const u4* args, 45 JValue* pResult) 46{ 47 int status = args[0]; 48 bool isExit = (args[1] != 0); 49 50 if (isExit && gDvm.exitHook != NULL) { 51 dvmChangeStatus(NULL, THREAD_NATIVE); 52 (*gDvm.exitHook)(status); // not expected to return 53 dvmChangeStatus(NULL, THREAD_RUNNING); 54 ALOGW("JNI exit hook returned"); 55 } 56 ALOGD("Calling exit(%d)", status); 57#if defined(WITH_JIT) && defined(WITH_JIT_TUNING) 58 dvmCompilerDumpStats(); 59#endif 60 exit(status); 61} 62 63/* 64 * static String nativeLoad(String filename, ClassLoader loader) 65 * 66 * Load the specified full path as a dynamic library filled with 67 * JNI-compatible methods. Returns null on success, or a failure 68 * message on failure. 69 */ 70static void Dalvik_java_lang_Runtime_nativeLoad(const u4* args, 71 JValue* pResult) 72{ 73 StringObject* fileNameObj = (StringObject*) args[0]; 74 Object* classLoader = (Object*) args[1]; 75 char* fileName = NULL; 76 StringObject* result = NULL; 77 char* reason = NULL; 78 bool success; 79 80 assert(fileNameObj != NULL); 81 fileName = dvmCreateCstrFromString(fileNameObj); 82 83 success = dvmLoadNativeCode(fileName, classLoader, &reason); 84 if (!success) { 85 const char* msg = (reason != NULL) ? reason : "unknown failure"; 86 result = dvmCreateStringFromCstr(msg); 87 dvmReleaseTrackedAlloc((Object*) result, NULL); 88 } 89 90 free(reason); 91 free(fileName); 92 RETURN_PTR(result); 93} 94 95/* 96 * public long maxMemory() 97 * 98 * Returns GC heap max memory in bytes. 99 */ 100static void Dalvik_java_lang_Runtime_maxMemory(const u4* args, JValue* pResult) 101{ 102 RETURN_LONG(dvmGetHeapDebugInfo(kVirtualHeapMaximumSize)); 103} 104 105/* 106 * public long totalMemory() 107 * 108 * Returns GC heap total memory in bytes. 109 */ 110static void Dalvik_java_lang_Runtime_totalMemory(const u4* args, 111 JValue* pResult) 112{ 113 RETURN_LONG(dvmGetHeapDebugInfo(kVirtualHeapSize)); 114} 115 116/* 117 * public long freeMemory() 118 * 119 * Returns GC heap free memory in bytes. 120 */ 121static void Dalvik_java_lang_Runtime_freeMemory(const u4* args, 122 JValue* pResult) 123{ 124 size_t size = dvmGetHeapDebugInfo(kVirtualHeapSize); 125 size_t allocated = dvmGetHeapDebugInfo(kVirtualHeapAllocated); 126 long long result = size - allocated; 127 if (result < 0) { 128 result = 0; 129 } 130 RETURN_LONG(result); 131} 132 133const DalvikNativeMethod dvm_java_lang_Runtime[] = { 134 { "freeMemory", "()J", 135 Dalvik_java_lang_Runtime_freeMemory }, 136 { "gc", "()V", 137 Dalvik_java_lang_Runtime_gc }, 138 { "maxMemory", "()J", 139 Dalvik_java_lang_Runtime_maxMemory }, 140 { "nativeExit", "(IZ)V", 141 Dalvik_java_lang_Runtime_nativeExit }, 142 { "nativeLoad", "(Ljava/lang/String;Ljava/lang/ClassLoader;)Ljava/lang/String;", 143 Dalvik_java_lang_Runtime_nativeLoad }, 144 { "totalMemory", "()J", 145 Dalvik_java_lang_Runtime_totalMemory }, 146 { NULL, NULL, NULL }, 147}; 148