• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
23 
24 /*
25  * public void gc()
26  *
27  * Initiate a gc.
28  */
Dalvik_java_lang_Runtime_gc(const u4 * args,JValue * pResult)29 static void Dalvik_java_lang_Runtime_gc(const u4* args, JValue* pResult)
30 {
31     UNUSED_PARAMETER(args);
32 
33     dvmCollectGarbage(false);
34     RETURN_VOID();
35 }
36 
37 /*
38  * private static void nativeExit(int code, boolean isExit)
39  *
40  * Runtime.exit() calls this after doing shutdown processing.  Runtime.halt()
41  * uses this as well.
42  */
Dalvik_java_lang_Runtime_nativeExit(const u4 * args,JValue * pResult)43 static void Dalvik_java_lang_Runtime_nativeExit(const u4* args,
44     JValue* pResult)
45 {
46     int status = args[0];
47     bool isExit = (args[1] != 0);
48 
49     if (isExit && gDvm.exitHook != NULL) {
50         dvmChangeStatus(NULL, THREAD_NATIVE);
51         (*gDvm.exitHook)(status);     // not expected to return
52         dvmChangeStatus(NULL, THREAD_RUNNING);
53         LOGW("JNI exit hook returned\n");
54     }
55     LOGD("Calling exit(%d)\n", status);
56     exit(status);
57 }
58 
59 /*
60  * static boolean nativeLoad(String filename, ClassLoader loader)
61  *
62  * Load the specified full path as a dynamic library filled with
63  * JNI-compatible methods.
64  */
Dalvik_java_lang_Runtime_nativeLoad(const u4 * args,JValue * pResult)65 static void Dalvik_java_lang_Runtime_nativeLoad(const u4* args,
66     JValue* pResult)
67 {
68     StringObject* fileNameObj = (StringObject*) args[0];
69     Object* classLoader = (Object*) args[1];
70     char* fileName;
71     int result;
72 
73     if (fileNameObj == NULL)
74         RETURN_INT(false);
75     fileName = dvmCreateCstrFromString(fileNameObj);
76 
77     result = dvmLoadNativeCode(fileName, classLoader);
78 
79     free(fileName);
80     RETURN_INT(result);
81 }
82 
83 /*
84  * public void runFinalization(boolean forced)
85  *
86  * Requests that the VM runs finalizers for objects on the heap. If the
87  * parameter forced is true, then the VM needs to ensure finalization.
88  * Otherwise this only inspires the VM to make a best-effort attempt to
89  * run finalizers before returning, but it's not guaranteed to actually
90  * do anything.
91  */
Dalvik_java_lang_Runtime_runFinalization(const u4 * args,JValue * pResult)92 static void Dalvik_java_lang_Runtime_runFinalization(const u4* args,
93     JValue* pResult)
94 {
95     bool forced = (args[0] != 0);
96 
97     dvmWaitForHeapWorkerIdle();
98     if (forced) {
99         // TODO(Google) Need to explicitly implement this,
100         //              although dvmWaitForHeapWorkerIdle()
101         //              should usually provide the "forced"
102         //              behavior already.
103     }
104 
105     RETURN_VOID();
106 }
107 
108 /*
109  * public void maxMemory()
110  *
111  * Returns GC heap max memory in bytes.
112  */
Dalvik_java_lang_Runtime_maxMemory(const u4 * args,JValue * pResult)113 static void Dalvik_java_lang_Runtime_maxMemory(const u4* args, JValue* pResult)
114 {
115     unsigned int result = gDvm.heapSizeMax;
116     RETURN_LONG(result);
117 }
118 
119 /*
120  * public void totalMemory()
121  *
122  * Returns GC heap total memory in bytes.
123  */
Dalvik_java_lang_Runtime_totalMemory(const u4 * args,JValue * pResult)124 static void Dalvik_java_lang_Runtime_totalMemory(const u4* args,
125     JValue* pResult)
126 {
127     int result = dvmGetHeapDebugInfo(kVirtualHeapSize);
128     RETURN_LONG(result);
129 }
130 
131 /*
132  * public void freeMemory()
133  *
134  * Returns GC heap free memory in bytes.
135  */
Dalvik_java_lang_Runtime_freeMemory(const u4 * args,JValue * pResult)136 static void Dalvik_java_lang_Runtime_freeMemory(const u4* args,
137     JValue* pResult)
138 {
139     int result = dvmGetHeapDebugInfo(kVirtualHeapSize)
140                  - dvmGetHeapDebugInfo(kVirtualHeapAllocated);
141     if (result < 0) {
142         result = 0;
143     }
144     RETURN_LONG(result);
145 }
146 
147 const DalvikNativeMethod dvm_java_lang_Runtime[] = {
148     { "freeMemory",          "()J",
149         Dalvik_java_lang_Runtime_freeMemory },
150     { "gc",                 "()V",
151         Dalvik_java_lang_Runtime_gc },
152     { "maxMemory",          "()J",
153         Dalvik_java_lang_Runtime_maxMemory },
154     { "nativeExit",         "(IZ)V",
155         Dalvik_java_lang_Runtime_nativeExit },
156     { "nativeLoad",         "(Ljava/lang/String;Ljava/lang/ClassLoader;)Z",
157         Dalvik_java_lang_Runtime_nativeLoad },
158     { "runFinalization",    "(Z)V",
159         Dalvik_java_lang_Runtime_runFinalization },
160     { "totalMemory",          "()J",
161         Dalvik_java_lang_Runtime_totalMemory },
162     { NULL, NULL, NULL },
163 };
164 
165