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