OSDN Git Service

am 81058aaf: (-s ours) Use bcopy() to move object refs around within an array. DO...
[android-x86/dalvik.git] / vm / native / dalvik_system_VMRuntime.c
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  * dalvik.system.VMRuntime
19  */
20 #include "Dalvik.h"
21 #include "native/InternalNativePriv.h"
22
23 #include <limits.h>
24
25
26 /*
27  * public native float getTargetHeapUtilization()
28  *
29  * Gets the current ideal heap utilization, represented as a number
30  * between zero and one.
31  */
32 static void Dalvik_dalvik_system_VMRuntime_getTargetHeapUtilization(
33     const u4* args, JValue* pResult)
34 {
35     UNUSED_PARAMETER(args);
36
37     RETURN_FLOAT(dvmGetTargetHeapUtilization());
38 }
39
40 /*
41  * native float nativeSetTargetHeapUtilization()
42  *
43  * Sets the current ideal heap utilization, represented as a number
44  * between zero and one.  Returns the old utilization.
45  *
46  * Note that this is NOT static.
47  */
48 static void Dalvik_dalvik_system_VMRuntime_nativeSetTargetHeapUtilization(
49     const u4* args, JValue* pResult)
50 {
51     dvmSetTargetHeapUtilization(dvmU4ToFloat(args[1]));
52
53     RETURN_VOID();
54 }
55
56 /*
57  * public native void gcSoftReferences()
58  *
59  * Does a GC and forces collection of SoftReferences that are
60  * not strongly-reachable.
61  */
62 static void Dalvik_dalvik_system_VMRuntime_gcSoftReferences(const u4* args,
63     JValue* pResult)
64 {
65     dvmCollectGarbage(true);
66
67     RETURN_VOID();
68 }
69
70 /*
71  * public native void runFinalizationSync()
72  *
73  * Does not return until any pending finalizers have been called.
74  * This may or may not happen in the context of the calling thread.
75  * No exceptions will escape.
76  *
77  * Used by zygote, which doesn't have a HeapWorker thread.
78  */
79 static void Dalvik_dalvik_system_VMRuntime_runFinalizationSync(const u4* args,
80     JValue* pResult)
81 {
82     dvmRunFinalizationSync();
83
84     RETURN_VOID();
85 }
86
87 /*
88  * public native void startJitCompilation()
89  *
90  * Callback function from the framework to indicate that an app has gone
91  * through the startup phase and it is time to enable the JIT compiler.
92  */
93 static void Dalvik_dalvik_system_VMRuntime_startJitCompilation(const u4* args,
94     JValue* pResult)
95 {
96 #if defined(WITH_JIT)
97     if (gDvm.executionMode == kExecutionModeJit &&
98         gDvmJit.disableJit == false) {
99         dvmLockMutex(&gDvmJit.compilerLock);
100         gDvmJit.alreadyEnabledViaFramework = true;
101         pthread_cond_signal(&gDvmJit.compilerQueueActivity);
102         dvmUnlockMutex(&gDvmJit.compilerLock);
103     }
104 #endif
105     RETURN_VOID();
106 }
107
108 /*
109  * public native void disableJitCompilation()
110  *
111  * Callback function from the framework to indicate that a VM instance wants to
112  * permanently disable the JIT compiler. Currently only the system server uses
113  * this interface when it detects system-wide safe mode is enabled.
114  */
115 static void Dalvik_dalvik_system_VMRuntime_disableJitCompilation(const u4* args,
116     JValue* pResult)
117 {
118 #if defined(WITH_JIT)
119     if (gDvm.executionMode == kExecutionModeJit) {
120         gDvmJit.disableJit = true;
121     }
122 #endif
123     RETURN_VOID();
124 }
125
126 static void Dalvik_dalvik_system_VMRuntime_newNonMovableArray(const u4* args,
127     JValue* pResult)
128 {
129     ClassObject* elementClass = (ClassObject*) args[1];
130     int length = args[2];
131     ArrayObject* newArray;
132
133     if (elementClass == NULL) {
134         dvmThrowException("Ljava/lang/NullPointerException;", NULL);
135         RETURN_VOID();
136     }
137     if (length < 0) {
138         dvmThrowException("Ljava/lang/NegativeArraySizeException;", NULL);
139         RETURN_VOID();
140     }
141
142     // TODO: right now, we don't have a copying collector, so there's no need
143     // to do anything special here, but we ought to pass the non-movability
144     // through to the allocator.
145     newArray = dvmAllocObjectArray(elementClass, length, ALLOC_DEFAULT);
146     if (newArray == NULL) {
147         assert(dvmCheckException(dvmThreadSelf()));
148         RETURN_VOID();
149     }
150     dvmReleaseTrackedAlloc((Object*) newArray, NULL);
151
152     RETURN_PTR(newArray);
153 }
154
155 static void Dalvik_dalvik_system_VMRuntime_addressOf(const u4* args,
156     JValue* pResult)
157 {
158     ArrayObject* array = (ArrayObject*) args[1];
159     if (!dvmIsArray(array)) {
160         dvmThrowException("Ljava/lang/IllegalArgumentException;", NULL);
161         RETURN_VOID();
162     }
163     // TODO: we should also check that this is a non-movable array.
164     s8 result = (uintptr_t) array->contents;
165     RETURN_LONG(result);
166 }
167
168 static void Dalvik_dalvik_system_VMRuntime_clearGrowthLimit(const u4* args,
169     JValue* pResult)
170 {
171     dvmClearGrowthLimit();
172     RETURN_VOID();
173 }
174
175 const DalvikNativeMethod dvm_dalvik_system_VMRuntime[] = {
176     { "getTargetHeapUtilization", "()F",
177         Dalvik_dalvik_system_VMRuntime_getTargetHeapUtilization },
178     { "nativeSetTargetHeapUtilization", "(F)V",
179         Dalvik_dalvik_system_VMRuntime_nativeSetTargetHeapUtilization },
180     { "gcSoftReferences", "()V",
181         Dalvik_dalvik_system_VMRuntime_gcSoftReferences },
182     { "runFinalizationSync", "()V",
183         Dalvik_dalvik_system_VMRuntime_runFinalizationSync },
184     { "startJitCompilation", "()V",
185         Dalvik_dalvik_system_VMRuntime_startJitCompilation },
186     { "disableJitCompilation", "()V",
187         Dalvik_dalvik_system_VMRuntime_disableJitCompilation },
188     { "newNonMovableArray", "(Ljava/lang/Class;I)Ljava/lang/Object;",
189         Dalvik_dalvik_system_VMRuntime_newNonMovableArray },
190     { "addressOf", "(Ljava/lang/Object;)J",
191         Dalvik_dalvik_system_VMRuntime_addressOf },
192     { "clearGrowthLimit", "()V",
193         Dalvik_dalvik_system_VMRuntime_clearGrowthLimit },
194     { NULL, NULL, NULL },
195 };