blob: 33f45e86f67854b375fc57e97f8296520d5d099b [file] [log] [blame]
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001/*
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 * Garbage-collecting memory allocator.
18 */
19#include "Dalvik.h"
20#include "alloc/Heap.h"
21#include "alloc/HeapInternal.h"
Carl Shapiro41eb6e92010-08-17 13:30:48 -070022#include "alloc/HeapSource.h"
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080023
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080024/*
25 * Initialize the GC universe.
26 *
27 * We're currently using a memory-mapped arena to keep things off of the
28 * main heap. This needs to be replaced with something real.
29 */
30bool dvmGcStartup(void)
31{
32 dvmInitMutex(&gDvm.gcHeapLock);
33
34 return dvmHeapStartup();
35}
36
37/*
38 * Post-zygote heap initialization, including starting
39 * the HeapWorker thread.
40 */
41bool dvmGcStartupAfterZygote(void)
42{
Carl Shapiroec805ea2010-06-28 16:28:26 -070043 return dvmHeapStartupAfterZygote();
44}
45
46/*
47 * Shutdown the threads internal to the garbage collector.
48 */
49void dvmGcThreadShutdown(void)
50{
Carl Shapiroec805ea2010-06-28 16:28:26 -070051 dvmHeapThreadShutdown();
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080052}
53
54/*
55 * Shut the GC down.
56 */
57void dvmGcShutdown(void)
58{
59 //TODO: grab and destroy the lock
60 dvmHeapShutdown();
61}
62
63/*
64 * Do any last-minute preparation before we call fork() for the first time.
65 */
66bool dvmGcPreZygoteFork(void)
67{
68 return dvmHeapSourceStartupBeforeFork();
69}
70
Carl Shapiroce87bfe2011-03-30 19:35:34 -070071bool dvmGcStartupClasses(void)
72{
73 {
74 const char *klassName = "Ljava/lang/ref/ReferenceQueueThread;";
75 ClassObject *klass = dvmFindSystemClass(klassName);
76 if (klass == NULL) {
77 return false;
78 }
79 const char *methodName = "startReferenceQueue";
80 Method *method = dvmFindDirectMethodByDescriptor(klass, methodName, "()V");
81 if (method == NULL) {
82 return false;
83 }
84 Thread *self = dvmThreadSelf();
85 assert(self != NULL);
86 JValue unusedResult;
87 dvmCallMethod(self, method, NULL, &unusedResult);
88 }
89 {
90 const char *klassName = "Ljava/lang/FinalizerThread;";
91 ClassObject *klass = dvmFindSystemClass(klassName);
92 if (klass == NULL) {
93 return false;
94 }
95 const char *methodName = "startFinalizer";
96 Method *method = dvmFindDirectMethodByDescriptor(klass, methodName, "()V");
97 if (method == NULL) {
98 return false;
99 }
100 Thread *self = dvmThreadSelf();
101 assert(self != NULL);
102 JValue unusedResult;
103 dvmCallMethod(self, method, NULL, &unusedResult);
104 }
105
106 return true;
107}
108
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800109/*
Andy McFadden7fc3ce82009-07-14 15:57:23 -0700110 * Create a "stock instance" of an exception class.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800111 */
Andy McFadden7fc3ce82009-07-14 15:57:23 -0700112static Object* createStockException(const char* descriptor, const char* msg)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800113{
Andy McFadden7fc3ce82009-07-14 15:57:23 -0700114 Thread* self = dvmThreadSelf();
115 StringObject* msgStr = NULL;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800116 ClassObject* clazz;
117 Method* init;
118 Object* obj;
119
Andy McFadden7fc3ce82009-07-14 15:57:23 -0700120 /* find class, initialize if necessary */
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800121 clazz = dvmFindSystemClass(descriptor);
122 if (clazz == NULL) {
123 LOGE("Unable to find %s\n", descriptor);
124 return NULL;
125 }
126
Andy McFadden7fc3ce82009-07-14 15:57:23 -0700127 init = dvmFindDirectMethodByDescriptor(clazz, "<init>",
128 "(Ljava/lang/String;)V");
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800129 if (init == NULL) {
Andy McFadden7fc3ce82009-07-14 15:57:23 -0700130 LOGE("Unable to find String-arg constructor for %s\n", descriptor);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800131 return NULL;
132 }
133
134 obj = dvmAllocObject(clazz, ALLOC_DEFAULT);
135 if (obj == NULL)
136 return NULL;
137
Andy McFadden7fc3ce82009-07-14 15:57:23 -0700138 if (msg == NULL) {
139 msgStr = NULL;
140 } else {
Barry Hayes81f3ebe2010-06-15 16:17:37 -0700141 msgStr = dvmCreateStringFromCstr(msg);
Andy McFadden7fc3ce82009-07-14 15:57:23 -0700142 if (msgStr == NULL) {
143 LOGW("Could not allocate message string \"%s\"\n", msg);
144 dvmReleaseTrackedAlloc(obj, self);
145 return NULL;
146 }
147 }
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800148
Andy McFadden7fc3ce82009-07-14 15:57:23 -0700149 JValue unused;
150 dvmCallMethod(self, init, obj, &unused, msgStr);
151 if (dvmCheckException(self)) {
152 dvmReleaseTrackedAlloc((Object*) msgStr, self);
153 dvmReleaseTrackedAlloc(obj, self);
154 return NULL;
155 }
156
157 dvmReleaseTrackedAlloc((Object*) msgStr, self); // okay if msgStr NULL
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800158 return obj;
159}
160
161/*
Andy McFadden7fc3ce82009-07-14 15:57:23 -0700162 * Create some "stock" exceptions. These can be thrown when the system is
163 * too screwed up to allocate and initialize anything, or when we don't
164 * need a meaningful stack trace.
165 *
166 * We can't do this during the initial startup because we need to execute
167 * the constructors.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800168 */
Andy McFadden7fc3ce82009-07-14 15:57:23 -0700169bool dvmCreateStockExceptions(void)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800170{
171 /*
172 * Pre-allocate some throwables. These need to be explicitly added
Andy McFadden7fc3ce82009-07-14 15:57:23 -0700173 * to the GC's root set (see dvmHeapMarkRootSet()).
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800174 */
Andy McFadden7fc3ce82009-07-14 15:57:23 -0700175 gDvm.outOfMemoryObj = createStockException("Ljava/lang/OutOfMemoryError;",
176 "[memory exhausted]");
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800177 dvmReleaseTrackedAlloc(gDvm.outOfMemoryObj, NULL);
Andy McFadden7fc3ce82009-07-14 15:57:23 -0700178 gDvm.internalErrorObj = createStockException("Ljava/lang/InternalError;",
179 "[pre-allocated]");
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800180 dvmReleaseTrackedAlloc(gDvm.internalErrorObj, NULL);
Andy McFadden7fc3ce82009-07-14 15:57:23 -0700181 gDvm.noClassDefFoundErrorObj =
Andy McFadden4c691d12009-12-10 15:11:18 -0800182 createStockException("Ljava/lang/NoClassDefFoundError;",
183 "[generic]");
Andy McFadden7fc3ce82009-07-14 15:57:23 -0700184 dvmReleaseTrackedAlloc(gDvm.noClassDefFoundErrorObj, NULL);
185
186 if (gDvm.outOfMemoryObj == NULL || gDvm.internalErrorObj == NULL ||
187 gDvm.noClassDefFoundErrorObj == NULL)
188 {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800189 LOGW("Unable to create stock exceptions\n");
190 return false;
191 }
192
193 return true;
194}
195
196
197/*
198 * Create an instance of the specified class.
199 *
200 * Returns NULL and throws an exception on failure.
201 */
202Object* dvmAllocObject(ClassObject* clazz, int flags)
203{
204 Object* newObj;
205
Carl Shapiro3475f9c2011-03-21 13:35:24 -0700206 assert(clazz != NULL);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800207 assert(dvmIsClassInitialized(clazz) || dvmIsClassInitializing(clazz));
208
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800209 /* allocate on GC heap; memory is zeroed out */
Carl Shapirofc75f3e2010-12-07 11:43:38 -0800210 newObj = (Object*)dvmMalloc(clazz->objectSize, flags);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800211 if (newObj != NULL) {
212 DVM_OBJECT_INIT(newObj, clazz);
Andy McFadden6af2ddd2011-02-16 16:50:40 -0800213 dvmTrackAllocation(clazz, clazz->objectSize); /* notify DDMS */
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800214 }
215
216 return newObj;
217}
218
219/*
220 * Create a copy of an object, for Object.clone().
221 *
222 * We use the size actually allocated, rather than obj->clazz->objectSize,
223 * because the latter doesn't work for array objects.
224 */
Andy McFadden0f27ad72011-02-22 13:56:47 -0800225Object* dvmCloneObject(Object* obj, int flags)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800226{
Andy McFadden6af2ddd2011-02-16 16:50:40 -0800227 ClassObject* clazz;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800228 Object* copy;
Andy McFadden6af2ddd2011-02-16 16:50:40 -0800229 size_t size;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800230
231 assert(dvmIsValidObject(obj));
Andy McFadden6af2ddd2011-02-16 16:50:40 -0800232 clazz = obj->clazz;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800233
234 /* Class.java shouldn't let us get here (java.lang.Class is final
235 * and does not implement Clonable), but make extra sure.
236 * A memcpy() clone will wreak havoc on a ClassObject's "innards".
237 */
Andy McFadden6af2ddd2011-02-16 16:50:40 -0800238 assert(clazz != gDvm.classJavaLangClass);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800239
Andy McFadden6af2ddd2011-02-16 16:50:40 -0800240 if (IS_CLASS_FLAG_SET(clazz, CLASS_ISARRAY)) {
Carl Shapirobfe4dcc2010-04-16 17:55:27 -0700241 size = dvmArrayObjectSize((ArrayObject *)obj);
242 } else {
Andy McFadden6af2ddd2011-02-16 16:50:40 -0800243 size = clazz->objectSize;
Carl Shapirobfe4dcc2010-04-16 17:55:27 -0700244 }
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800245
Andy McFadden0f27ad72011-02-22 13:56:47 -0800246 copy = (Object*)dvmMalloc(size, flags);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800247 if (copy == NULL)
248 return NULL;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800249
Andy McFadden6af2ddd2011-02-16 16:50:40 -0800250 /* We assume that memcpy will copy obj by words. */
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800251 memcpy(copy, obj, size);
252 DVM_LOCK_INIT(&copy->lock);
Barry Hayes364f9d92010-06-11 16:12:47 -0700253 dvmWriteBarrierObject(copy);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800254
Carl Shapiro39721ef2011-02-22 16:51:27 -0800255 /* Mark the clone as finalizable if appropriate. */
Andy McFadden6af2ddd2011-02-16 16:50:40 -0800256 if (IS_CLASS_FLAG_SET(clazz, CLASS_ISFINALIZABLE)) {
257 dvmSetFinalizable(copy);
258 }
259
260 dvmTrackAllocation(clazz, size); /* notify DDMS */
261
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800262 return copy;
263}
264
265
266/*
267 * Track an object that was allocated internally and isn't yet part of the
268 * VM root set.
269 *
270 * We could do this per-thread or globally. If it's global we don't have
271 * to do the thread lookup but we do have to synchronize access to the list.
272 *
Andy McFaddenbd74b4b2010-09-22 12:37:49 -0700273 * "obj" must not be NULL.
274 *
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800275 * NOTE: "obj" is not a fully-formed object; in particular, obj->clazz will
276 * usually be NULL since we're being called from dvmMalloc().
277 */
278void dvmAddTrackedAlloc(Object* obj, Thread* self)
279{
280 if (self == NULL)
281 self = dvmThreadSelf();
282
Andy McFaddenbd74b4b2010-09-22 12:37:49 -0700283 assert(obj != NULL);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800284 assert(self != NULL);
285 if (!dvmAddToReferenceTable(&self->internalLocalRefTable, obj)) {
286 LOGE("threadid=%d: unable to add %p to internal ref table\n",
287 self->threadId, obj);
288 dvmDumpThread(self, false);
289 dvmAbort();
290 }
291}
292
293/*
294 * Stop tracking an object.
295 *
296 * We allow attempts to delete NULL "obj" so that callers don't have to wrap
297 * calls with "if != NULL".
298 */
299void dvmReleaseTrackedAlloc(Object* obj, Thread* self)
300{
301 if (obj == NULL)
302 return;
303
304 if (self == NULL)
305 self = dvmThreadSelf();
306 assert(self != NULL);
307
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800308 if (!dvmRemoveFromReferenceTable(&self->internalLocalRefTable,
309 self->internalLocalRefTable.table, obj))
310 {
311 LOGE("threadid=%d: failed to remove %p from internal ref table\n",
312 self->threadId, obj);
313 dvmAbort();
314 }
315}
316
317
318/*
319 * Explicitly initiate garbage collection.
320 */
Carl Shapirobc3ba012011-02-09 14:20:14 -0800321void dvmCollectGarbage(void)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800322{
Carl Shapiro821fd062011-01-19 12:56:14 -0800323 if (gDvm.disableExplicitGc) {
324 return;
325 }
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800326 dvmLockHeap();
Carl Shapiro039167e2010-12-20 18:33:24 -0800327 dvmWaitForConcurrentGcToComplete();
Carl Shapirocc6f5112011-01-26 17:25:27 -0800328 dvmCollectGarbageInternal(GC_EXPLICIT);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800329 dvmUnlockHeap();
330}
Carl Shapiro41eb6e92010-08-17 13:30:48 -0700331
332typedef struct {
333 const ClassObject *clazz;
334 size_t count;
Carl Shapirof331a602010-11-04 15:12:09 -0700335} CountContext;
Carl Shapiro41eb6e92010-08-17 13:30:48 -0700336
Carl Shapiro57ee2702010-08-27 13:06:48 -0700337static void countInstancesOfClassCallback(void *ptr, void *arg)
Carl Shapiro41eb6e92010-08-17 13:30:48 -0700338{
Carl Shapirofc75f3e2010-12-07 11:43:38 -0800339 CountContext *ctx = (CountContext *)arg;
340 const Object *obj = (const Object *)ptr;
Carl Shapiro41eb6e92010-08-17 13:30:48 -0700341
342 assert(ctx != NULL);
Carl Shapiro57ee2702010-08-27 13:06:48 -0700343 if (obj->clazz == ctx->clazz) {
344 ctx->count += 1;
Carl Shapiro41eb6e92010-08-17 13:30:48 -0700345 }
346}
347
348size_t dvmCountInstancesOfClass(const ClassObject *clazz)
349{
Carl Shapirof331a602010-11-04 15:12:09 -0700350 CountContext ctx = { clazz, 0 };
Carl Shapiro41eb6e92010-08-17 13:30:48 -0700351 dvmLockHeap();
Carl Shapiroa4313fe2011-01-24 12:31:09 -0800352 HeapBitmap *bitmap = dvmHeapSourceGetLiveBits();
Carl Shapiro41eb6e92010-08-17 13:30:48 -0700353 dvmHeapBitmapWalk(bitmap, countInstancesOfClassCallback, &ctx);
354 dvmUnlockHeap();
355 return ctx.count;
356}
Carl Shapirof331a602010-11-04 15:12:09 -0700357
358static void countAssignableInstancesOfClassCallback(void *ptr, void *arg)
359{
Carl Shapirofc75f3e2010-12-07 11:43:38 -0800360 CountContext *ctx = (CountContext *)arg;
361 const Object *obj = (const Object *)ptr;
Carl Shapirof331a602010-11-04 15:12:09 -0700362
363 assert(ctx != NULL);
Carl Shapiro2efc1262011-02-02 17:54:26 -0800364 if (obj->clazz != NULL && dvmInstanceof(obj->clazz, ctx->clazz)) {
Carl Shapirof331a602010-11-04 15:12:09 -0700365 ctx->count += 1;
366 }
367}
368
369size_t dvmCountAssignableInstancesOfClass(const ClassObject *clazz)
370{
371 CountContext ctx = { clazz, 0 };
Carl Shapirof331a602010-11-04 15:12:09 -0700372 dvmLockHeap();
Carl Shapiroa4313fe2011-01-24 12:31:09 -0800373 HeapBitmap *bitmap = dvmHeapSourceGetLiveBits();
Carl Shapirof331a602010-11-04 15:12:09 -0700374 dvmHeapBitmapWalk(bitmap, countAssignableInstancesOfClassCallback, &ctx);
375 dvmUnlockHeap();
376 return ctx.count;
377}
Carl Shapiro2494c502011-03-11 17:54:58 -0800378
379bool dvmIsHeapAddress(void *address)
380{
381 return dvmHeapSourceContainsAddress(address);
382}