blob: 574a7318ba8cadc33b571bf9be68a862de12d4d3 [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#include "Dalvik.h"
Barry Hayeseac47ed2009-06-22 11:45:20 -070018#include "alloc/clz.h"
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080019#include "alloc/HeapBitmap.h"
20#include "alloc/HeapInternal.h"
21#include "alloc/HeapSource.h"
22#include "alloc/MarkSweep.h"
Carl Shapiroec805ea2010-06-28 16:28:26 -070023#include "alloc/Visit.h"
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080024#include <limits.h> // for ULONG_MAX
25#include <sys/mman.h> // for madvise(), mmap()
The Android Open Source Project99409882009-03-18 22:20:24 -070026#include <errno.h>
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080027
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080028#define GC_LOG_TAG LOG_TAG "-gc"
29
30#if LOG_NDEBUG
31#define LOGV_GC(...) ((void)0)
32#define LOGD_GC(...) ((void)0)
33#else
34#define LOGV_GC(...) LOG(LOG_VERBOSE, GC_LOG_TAG, __VA_ARGS__)
35#define LOGD_GC(...) LOG(LOG_DEBUG, GC_LOG_TAG, __VA_ARGS__)
36#endif
37
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080038#define LOGI_GC(...) LOG(LOG_INFO, GC_LOG_TAG, __VA_ARGS__)
39#define LOGW_GC(...) LOG(LOG_WARN, GC_LOG_TAG, __VA_ARGS__)
40#define LOGE_GC(...) LOG(LOG_ERROR, GC_LOG_TAG, __VA_ARGS__)
41
42#define LOG_SCAN(...) LOGV_GC("SCAN: " __VA_ARGS__)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080043
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080044#define ALIGN_UP_TO_PAGE_SIZE(p) \
Andy McFadden96516932009-10-28 17:39:02 -070045 (((size_t)(p) + (SYSTEM_PAGE_SIZE - 1)) & ~(SYSTEM_PAGE_SIZE - 1))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080046
47/* Do not cast the result of this to a boolean; the only set bit
48 * may be > 1<<8.
49 */
Carl Shapiro6343bd02010-02-16 17:40:19 -080050static inline long isMarked(const void *obj, const GcMarkContext *ctx)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080051{
Carl Shapirof373efd2010-02-19 00:46:33 -080052 return dvmHeapBitmapIsObjectBitSet(ctx->bitmap, obj);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080053}
54
55static bool
56createMarkStack(GcMarkStack *stack)
57{
58 const Object **limit;
Carl Shapiro742c4452010-07-13 18:28:13 -070059 const char *name;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080060 size_t size;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080061
62 /* Create a stack big enough for the worst possible case,
63 * where the heap is perfectly full of the smallest object.
64 * TODO: be better about memory usage; use a smaller stack with
65 * overflow detection and recovery.
66 */
67 size = dvmHeapSourceGetIdealFootprint() * sizeof(Object*) /
68 (sizeof(Object) + HEAP_SOURCE_CHUNK_OVERHEAD);
69 size = ALIGN_UP_TO_PAGE_SIZE(size);
Carl Shapiro742c4452010-07-13 18:28:13 -070070 name = "dalvik-mark-stack";
71 limit = dvmAllocRegion(size, PROT_READ | PROT_WRITE, name);
72 if (limit == NULL) {
73 LOGE_GC("Could not mmap %zd-byte ashmem region '%s'", size, name);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080074 return false;
75 }
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080076 stack->limit = limit;
77 stack->base = (const Object **)((uintptr_t)limit + size);
78 stack->top = stack->base;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080079 return true;
80}
81
82static void
83destroyMarkStack(GcMarkStack *stack)
84{
85 munmap((char *)stack->limit,
86 (uintptr_t)stack->base - (uintptr_t)stack->limit);
87 memset(stack, 0, sizeof(*stack));
88}
89
90#define MARK_STACK_PUSH(stack, obj) \
91 do { \
92 *--(stack).top = (obj); \
93 } while (false)
94
95bool
Carl Shapirod25566d2010-03-11 20:39:47 -080096dvmHeapBeginMarkStep(GcMode mode)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080097{
98 GcMarkContext *mc = &gDvm.gcHeap->markContext;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080099
100 if (!createMarkStack(&mc->stack)) {
101 return false;
102 }
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800103 mc->finger = NULL;
Carl Shapirod25566d2010-03-11 20:39:47 -0800104 mc->immuneLimit = dvmHeapSourceGetImmuneLimit(mode);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800105 return true;
106}
107
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800108static long
Carl Shapiro6343bd02010-02-16 17:40:19 -0800109setAndReturnMarkBit(GcMarkContext *ctx, const void *obj)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800110{
Carl Shapirof373efd2010-02-19 00:46:33 -0800111 return dvmHeapBitmapSetAndReturnObjectBit(ctx->bitmap, obj);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800112}
113
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800114static void
Barry Hayese1bccb92010-05-18 09:48:37 -0700115markObjectNonNull(const Object *obj, GcMarkContext *ctx,
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800116 bool checkFinger, bool forceStack)
117{
Barry Hayese1bccb92010-05-18 09:48:37 -0700118 assert(ctx != NULL);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800119 assert(obj != NULL);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800120 assert(dvmIsValidObject(obj));
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800121
Carl Shapirob31b3012010-05-25 18:35:37 -0700122 if (obj < (Object *)ctx->immuneLimit) {
Carl Shapirod25566d2010-03-11 20:39:47 -0800123 assert(isMarked(obj, ctx));
124 return;
125 }
Carl Shapiro6343bd02010-02-16 17:40:19 -0800126 if (!setAndReturnMarkBit(ctx, obj)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800127 /* This object was not previously marked.
128 */
Carl Shapiro6343bd02010-02-16 17:40:19 -0800129 if (forceStack || (checkFinger && (void *)obj < ctx->finger)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800130 /* This object will need to go on the mark stack.
131 */
132 MARK_STACK_PUSH(ctx->stack, obj);
133 }
134
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800135#if WITH_HPROF
136 if (gDvm.gcHeap->hprofContext != NULL) {
137 hprofMarkRootObject(gDvm.gcHeap->hprofContext, obj, 0);
138 }
139#endif
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800140 }
141}
142
143/* Used to mark objects when recursing. Recursion is done by moving
144 * the finger across the bitmaps in address order and marking child
145 * objects. Any newly-marked objects whose addresses are lower than
146 * the finger won't be visited by the bitmap scan, so those objects
147 * need to be added to the mark stack.
148 */
Barry Hayese1bccb92010-05-18 09:48:37 -0700149static void markObject(const Object *obj, GcMarkContext *ctx)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800150{
Barry Hayese1bccb92010-05-18 09:48:37 -0700151 if (obj != NULL) {
152 markObjectNonNull(obj, ctx, true, false);
153 }
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800154}
155
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800156/* If the object hasn't already been marked, mark it and
157 * schedule it to be scanned for references.
158 *
159 * obj may not be NULL. The macro dvmMarkObject() should
160 * be used in situations where a reference may be NULL.
161 *
162 * This function may only be called when marking the root
Barry Hayese1bccb92010-05-18 09:48:37 -0700163 * set. When recursing, use the internal markObject().
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800164 */
165void
166dvmMarkObjectNonNull(const Object *obj)
167{
Barry Hayese1bccb92010-05-18 09:48:37 -0700168 assert(obj != NULL);
169 markObjectNonNull(obj, &gDvm.gcHeap->markContext, false, false);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800170}
171
172/* Mark the set of root objects.
173 *
174 * Things we need to scan:
175 * - System classes defined by root classloader
176 * - For each thread:
177 * - Interpreted stack, from top to "curFrame"
178 * - Dalvik registers (args + local vars)
179 * - JNI local references
180 * - Automatic VM local references (TrackedAlloc)
181 * - Associated Thread/VMThread object
182 * - ThreadGroups (could track & start with these instead of working
183 * upward from Threads)
184 * - Exception currently being thrown, if present
185 * - JNI global references
186 * - Interned string table
187 * - Primitive classes
188 * - Special objects
189 * - gDvm.outOfMemoryObj
190 * - Objects allocated with ALLOC_NO_GC
191 * - Objects pending finalization (but not yet finalized)
192 * - Objects in debugger object registry
193 *
194 * Don't need:
195 * - Native stack (for in-progress stuff in the VM)
196 * - The TrackedAlloc stuff watches all native VM references.
197 */
198void dvmHeapMarkRootSet()
199{
Barry Hayesd4f78d32010-06-08 09:34:42 -0700200 GcHeap *gcHeap = gDvm.gcHeap;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800201
202 HPROF_SET_GC_SCAN_STATE(HPROF_ROOT_STICKY_CLASS, 0);
203
Carl Shapirod25566d2010-03-11 20:39:47 -0800204 LOG_SCAN("immune objects");
Barry Hayes425848f2010-05-04 13:32:12 -0700205 dvmMarkImmuneObjects(gcHeap->markContext.immuneLimit);
Carl Shapirod25566d2010-03-11 20:39:47 -0800206
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800207 LOG_SCAN("root class loader\n");
208 dvmGcScanRootClassLoader();
209 LOG_SCAN("primitive classes\n");
210 dvmGcScanPrimitiveClasses();
211
212 /* dvmGcScanRootThreadGroups() sets a bunch of
213 * different scan states internally.
214 */
215 HPROF_CLEAR_GC_SCAN_STATE();
216
217 LOG_SCAN("root thread groups\n");
218 dvmGcScanRootThreadGroups();
219
220 HPROF_SET_GC_SCAN_STATE(HPROF_ROOT_INTERNED_STRING, 0);
221
222 LOG_SCAN("interned strings\n");
223 dvmGcScanInternedStrings();
224
225 HPROF_SET_GC_SCAN_STATE(HPROF_ROOT_JNI_GLOBAL, 0);
226
227 LOG_SCAN("JNI global refs\n");
228 dvmGcMarkJniGlobalRefs();
229
230 HPROF_SET_GC_SCAN_STATE(HPROF_ROOT_REFERENCE_CLEANUP, 0);
231
232 LOG_SCAN("pending reference operations\n");
Carl Shapiro646ba092010-06-10 15:17:00 -0700233 dvmHeapMarkLargeTableRefs(gcHeap->referenceOperations);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800234
235 HPROF_SET_GC_SCAN_STATE(HPROF_ROOT_FINALIZING, 0);
236
237 LOG_SCAN("pending finalizations\n");
Carl Shapiro646ba092010-06-10 15:17:00 -0700238 dvmHeapMarkLargeTableRefs(gcHeap->pendingFinalizationRefs);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800239
240 HPROF_SET_GC_SCAN_STATE(HPROF_ROOT_DEBUGGER, 0);
241
242 LOG_SCAN("debugger refs\n");
243 dvmGcMarkDebuggerRefs();
244
245 HPROF_SET_GC_SCAN_STATE(HPROF_ROOT_VM_INTERNAL, 0);
246
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800247 /* Mark any special objects we have sitting around.
248 */
249 LOG_SCAN("special objects\n");
250 dvmMarkObjectNonNull(gDvm.outOfMemoryObj);
251 dvmMarkObjectNonNull(gDvm.internalErrorObj);
Andy McFadden7fc3ce82009-07-14 15:57:23 -0700252 dvmMarkObjectNonNull(gDvm.noClassDefFoundErrorObj);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800253//TODO: scan object references sitting in gDvm; use pointer begin & end
254
255 HPROF_CLEAR_GC_SCAN_STATE();
256}
257
258/*
Barry Hayese1bccb92010-05-18 09:48:37 -0700259 * Nothing past this point is allowed to use dvmMarkObject() or
260 * dvmMarkObjectNonNull(), which are for root-marking only.
261 * Scanning/recursion must use markObject(), which takes the finger
262 * into account.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800263 */
Barry Hayese1bccb92010-05-18 09:48:37 -0700264#undef dvmMarkObject
265#define dvmMarkObject __dont_use_dvmMarkObject__
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800266#define dvmMarkObjectNonNull __dont_use_dvmMarkObjectNonNull__
267
Barry Hayese1bccb92010-05-18 09:48:37 -0700268/*
269 * Scans instance fields.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800270 */
Barry Hayese1bccb92010-05-18 09:48:37 -0700271static void scanInstanceFields(const Object *obj, GcMarkContext *ctx)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800272{
Barry Hayese1bccb92010-05-18 09:48:37 -0700273 assert(obj != NULL);
274 assert(obj->clazz != NULL);
275 assert(ctx != NULL);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800276
Barry Hayese1bccb92010-05-18 09:48:37 -0700277 if (obj->clazz->refOffsets != CLASS_WALK_SUPER) {
278 unsigned int refOffsets = obj->clazz->refOffsets;
Barry Hayeseac47ed2009-06-22 11:45:20 -0700279 while (refOffsets != 0) {
280 const int rshift = CLZ(refOffsets);
281 refOffsets &= ~(CLASS_HIGH_BIT >> rshift);
282 markObject(dvmGetFieldObject((Object*)obj,
Barry Hayese1bccb92010-05-18 09:48:37 -0700283 CLASS_OFFSET_FROM_CLZ(rshift)), ctx);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800284 }
Barry Hayeseac47ed2009-06-22 11:45:20 -0700285 } else {
Barry Hayese1bccb92010-05-18 09:48:37 -0700286 ClassObject *clazz;
287 int i;
288 for (clazz = obj->clazz; clazz != NULL; clazz = clazz->super) {
289 InstField *field = clazz->ifields;
290 for (i = 0; i < clazz->ifieldRefCount; ++i, ++field) {
291 void *addr = BYTE_OFFSET((Object *)obj, field->byteOffset);
292 markObject(((JValue *)addr)->l, ctx);
Barry Hayeseac47ed2009-06-22 11:45:20 -0700293 }
Barry Hayeseac47ed2009-06-22 11:45:20 -0700294 }
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800295 }
296}
297
Barry Hayese1bccb92010-05-18 09:48:37 -0700298/*
299 * Scans the header, static field references, and interface
300 * pointers of a class object.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800301 */
Barry Hayese1bccb92010-05-18 09:48:37 -0700302static void scanClassObject(const ClassObject *obj, GcMarkContext *ctx)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800303{
Barry Hayese1bccb92010-05-18 09:48:37 -0700304 int i;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800305
Barry Hayese1bccb92010-05-18 09:48:37 -0700306 assert(obj != NULL);
307 assert(obj->obj.clazz == gDvm.classJavaLangClass);
308 assert(ctx != NULL);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800309
Barry Hayese1bccb92010-05-18 09:48:37 -0700310 markObject((Object *)obj->obj.clazz, ctx);
311 if (IS_CLASS_FLAG_SET(obj, CLASS_ISARRAY)) {
312 markObject((Object *)obj->elementClass, ctx);
313 }
Barry Hayesc49db852010-05-14 13:43:34 -0700314 /* Do super and the interfaces contain Objects and not dex idx values? */
315 if (obj->status > CLASS_IDX) {
316 markObject((Object *)obj->super, ctx);
317 }
Barry Hayese1bccb92010-05-18 09:48:37 -0700318 markObject(obj->classLoader, ctx);
319 /* Scan static field references. */
320 for (i = 0; i < obj->sfieldCount; ++i) {
321 char ch = obj->sfields[i].field.signature[0];
322 if (ch == '[' || ch == 'L') {
323 markObject(obj->sfields[i].value.l, ctx);
324 }
325 }
326 /* Scan the instance fields. */
327 scanInstanceFields((const Object *)obj, ctx);
328 /* Scan interface references. */
Barry Hayesc49db852010-05-14 13:43:34 -0700329 if (obj->status > CLASS_IDX) {
330 for (i = 0; i < obj->interfaceCount; ++i) {
331 markObject((Object *)obj->interfaces[i], ctx);
332 }
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800333 }
334}
335
Barry Hayese1bccb92010-05-18 09:48:37 -0700336/*
337 * Scans the header of all array objects. If the array object is
338 * specialized to a reference type, scans the array data as well.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800339 */
Barry Hayese1bccb92010-05-18 09:48:37 -0700340static void scanArrayObject(const ArrayObject *obj, GcMarkContext *ctx)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800341{
Barry Hayese1bccb92010-05-18 09:48:37 -0700342 size_t i;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800343
Barry Hayese1bccb92010-05-18 09:48:37 -0700344 assert(obj != NULL);
345 assert(obj->obj.clazz != NULL);
346 assert(ctx != NULL);
347 /* Scan the class object reference. */
348 markObject((Object *)obj->obj.clazz, ctx);
349 if (IS_CLASS_FLAG_SET(obj->obj.clazz, CLASS_ISOBJECTARRAY)) {
350 /* Scan the array contents. */
351 Object **contents = (Object **)obj->contents;
352 for (i = 0; i < obj->length; ++i) {
353 markObject(contents[i], ctx);
354 }
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800355 }
Barry Hayese1bccb92010-05-18 09:48:37 -0700356}
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800357
Barry Hayese1bccb92010-05-18 09:48:37 -0700358/*
Carl Shapiro2a6f4842010-07-09 16:50:54 -0700359 * Returns class flags relating to Reference subclasses.
360 */
361static int referenceClassFlags(const Object *obj)
362{
363 int flags = CLASS_ISREFERENCE |
364 CLASS_ISWEAKREFERENCE |
365 CLASS_ISPHANTOMREFERENCE;
366 return GET_CLASS_FLAG_GROUP(obj->clazz, flags);
367}
368
369/*
370 * Returns true if the object derives from SoftReference.
371 */
372static bool isSoftReference(const Object *obj)
373{
374 return referenceClassFlags(obj) == CLASS_ISREFERENCE;
375}
376
377/*
378 * Returns true if the object derives from WeakReference.
379 */
380static bool isWeakReference(const Object *obj)
381{
382 return referenceClassFlags(obj) & CLASS_ISWEAKREFERENCE;
383}
384
385/*
386 * Returns true if the object derives from PhantomReference.
387 */
388static bool isPhantomReference(const Object *obj)
389{
390 return referenceClassFlags(obj) & CLASS_ISPHANTOMREFERENCE;
391}
392
393/*
394 * Adds a reference to the tail of a circular queue of references.
395 */
396static void enqueuePendingReference(Object *ref, Object **list)
397{
398 size_t offset;
399
400 assert(ref != NULL);
401 assert(list != NULL);
402 offset = gDvm.offJavaLangRefReference_pendingNext;
403 if (*list == NULL) {
404 dvmSetFieldObject(ref, offset, ref);
405 *list = ref;
406 } else {
407 Object *head = dvmGetFieldObject(*list, offset);
408 dvmSetFieldObject(ref, offset, head);
409 dvmSetFieldObject(*list, offset, ref);
410 }
411}
412
413/*
Carl Shapiroa1b03a92010-07-12 14:02:28 -0700414 * Removes the reference at the head of a circular queue of
415 * references.
Carl Shapiro2a6f4842010-07-09 16:50:54 -0700416 */
417static Object *dequeuePendingReference(Object **list)
418{
419 Object *ref, *head;
420 size_t offset;
421
422 assert(list != NULL);
423 assert(*list != NULL);
424 offset = gDvm.offJavaLangRefReference_pendingNext;
425 head = dvmGetFieldObject(*list, offset);
426 if (*list == head) {
427 ref = *list;
428 *list = NULL;
429 } else {
430 Object *next = dvmGetFieldObject(head, offset);
431 dvmSetFieldObject(*list, offset, next);
432 ref = head;
433 }
434 dvmSetFieldObject(ref, offset, NULL);
435 return ref;
436}
437
438/*
Barry Hayese1bccb92010-05-18 09:48:37 -0700439 * Process the "referent" field in a java.lang.ref.Reference. If the
440 * referent has not yet been marked, put it on the appropriate list in
441 * the gcHeap for later processing.
442 */
Barry Hayes697b5a92010-06-23 11:38:52 -0700443static void delayReferenceReferent(Object *obj, GcMarkContext *ctx)
Barry Hayese1bccb92010-05-18 09:48:37 -0700444{
Carl Shapiro2a6f4842010-07-09 16:50:54 -0700445 GcHeap *gcHeap = gDvm.gcHeap;
446 Object *pending, *referent;
447 size_t pendingNextOffset, referentOffset;
448
Barry Hayese1bccb92010-05-18 09:48:37 -0700449 assert(obj != NULL);
Barry Hayes697b5a92010-06-23 11:38:52 -0700450 assert(obj->clazz != NULL);
Carl Shapiro2a6f4842010-07-09 16:50:54 -0700451 assert(IS_CLASS_FLAG_SET(obj->clazz, CLASS_ISREFERENCE));
Barry Hayese1bccb92010-05-18 09:48:37 -0700452 assert(ctx != NULL);
Carl Shapiro2a6f4842010-07-09 16:50:54 -0700453 pendingNextOffset = gDvm.offJavaLangRefReference_pendingNext;
454 referentOffset = gDvm.offJavaLangRefReference_referent;
455 pending = dvmGetFieldObject(obj, pendingNextOffset);
456 referent = dvmGetFieldObject(obj, referentOffset);
457 if (pending == NULL && referent != NULL && !isMarked(referent, ctx)) {
458 Object **list = NULL;
459 if (isSoftReference(obj)) {
460 list = &gcHeap->softReferences;
461 } else if (isWeakReference(obj)) {
462 list = &gcHeap->weakReferences;
463 } else if (isPhantomReference(obj)) {
464 list = &gcHeap->phantomReferences;
Barry Hayese1bccb92010-05-18 09:48:37 -0700465 }
Carl Shapiro2a6f4842010-07-09 16:50:54 -0700466 assert(list != NULL);
467 enqueuePendingReference(obj, list);
Barry Hayese1bccb92010-05-18 09:48:37 -0700468 }
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800469}
470
Barry Hayese1bccb92010-05-18 09:48:37 -0700471/*
472 * Scans the header and field references of a data object.
473 */
Barry Hayes697b5a92010-06-23 11:38:52 -0700474static void scanDataObject(DataObject *obj, GcMarkContext *ctx)
Barry Hayese1bccb92010-05-18 09:48:37 -0700475{
476 assert(obj != NULL);
477 assert(obj->obj.clazz != NULL);
478 assert(ctx != NULL);
479 /* Scan the class object. */
480 markObject((Object *)obj->obj.clazz, ctx);
481 /* Scan the instance fields. */
482 scanInstanceFields((const Object *)obj, ctx);
Barry Hayese1bccb92010-05-18 09:48:37 -0700483 if (IS_CLASS_FLAG_SET(obj->obj.clazz, CLASS_ISREFERENCE)) {
Barry Hayes697b5a92010-06-23 11:38:52 -0700484 delayReferenceReferent((Object *)obj, ctx);
Barry Hayese1bccb92010-05-18 09:48:37 -0700485 }
486}
487
488/*
489 * Scans an object reference. Determines the type of the reference
490 * and dispatches to a specialized scanning routine.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800491 */
492static void scanObject(const Object *obj, GcMarkContext *ctx)
493{
Barry Hayese1bccb92010-05-18 09:48:37 -0700494 assert(obj != NULL);
495 assert(ctx != NULL);
Barry Hayes899cdb72010-06-08 09:59:12 -0700496 assert(obj->clazz != NULL);
Carl Shapiro1a8e21a2010-06-08 13:19:57 -0700497#if WITH_HPROF
498 if (gDvm.gcHeap->hprofContext != NULL) {
499 hprofDumpHeapObject(gDvm.gcHeap->hprofContext, obj);
500 }
501#endif
Barry Hayese1bccb92010-05-18 09:48:37 -0700502 /* Dispatch a type-specific scan routine. */
Carl Shapiro1a8e21a2010-06-08 13:19:57 -0700503 if (obj->clazz == gDvm.classJavaLangClass) {
Barry Hayese1bccb92010-05-18 09:48:37 -0700504 scanClassObject((ClassObject *)obj, ctx);
Carl Shapiro1a8e21a2010-06-08 13:19:57 -0700505 } else if (IS_CLASS_FLAG_SET(obj->clazz, CLASS_ISARRAY)) {
Barry Hayes899cdb72010-06-08 09:59:12 -0700506 scanArrayObject((ArrayObject *)obj, ctx);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800507 } else {
Barry Hayes899cdb72010-06-08 09:59:12 -0700508 scanDataObject((DataObject *)obj, ctx);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800509 }
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800510}
511
512static void
513processMarkStack(GcMarkContext *ctx)
514{
515 const Object **const base = ctx->stack.base;
516
517 /* Scan anything that's on the mark stack.
518 * We can't use the bitmaps anymore, so use
519 * a finger that points past the end of them.
520 */
521 ctx->finger = (void *)ULONG_MAX;
522 while (ctx->stack.top != base) {
523 scanObject(*ctx->stack.top++, ctx);
524 }
525}
526
527#ifndef NDEBUG
528static uintptr_t gLastFinger = 0;
529#endif
530
531static bool
532scanBitmapCallback(size_t numPtrs, void **ptrs, const void *finger, void *arg)
533{
534 GcMarkContext *ctx = (GcMarkContext *)arg;
535 size_t i;
536
537#ifndef NDEBUG
538 assert((uintptr_t)finger >= gLastFinger);
539 gLastFinger = (uintptr_t)finger;
540#endif
541
542 ctx->finger = finger;
543 for (i = 0; i < numPtrs; i++) {
Carl Shapiro6343bd02010-02-16 17:40:19 -0800544 scanObject(*ptrs++, ctx);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800545 }
546
547 return true;
548}
549
550/* Given bitmaps with the root set marked, find and mark all
551 * reachable objects. When this returns, the entire set of
552 * live objects will be marked and the mark stack will be empty.
553 */
Carl Shapiro29540742010-03-26 15:34:39 -0700554void dvmHeapScanMarkedObjects(void)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800555{
556 GcMarkContext *ctx = &gDvm.gcHeap->markContext;
557
558 assert(ctx->finger == NULL);
559
560 /* The bitmaps currently have bits set for the root set.
561 * Walk across the bitmaps and scan each object.
562 */
563#ifndef NDEBUG
564 gLastFinger = 0;
565#endif
Carl Shapirocedcb702010-07-22 20:22:49 -0700566 dvmHeapBitmapWalk(ctx->bitmap, scanBitmapCallback, ctx);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800567
568 /* We've walked the mark bitmaps. Scan anything that's
569 * left on the mark stack.
570 */
571 processMarkStack(ctx);
572
573 LOG_SCAN("done with marked objects\n");
574}
575
Carl Shapiroec805ea2010-06-28 16:28:26 -0700576/*
577 * Callback applied to each gray object to blacken it.
578 */
579static bool dirtyObjectCallback(size_t numPtrs, void **ptrs,
580 const void *finger, void *arg)
581{
Carl Shapiroec805ea2010-06-28 16:28:26 -0700582 size_t i;
583
Carl Shapiroec805ea2010-06-28 16:28:26 -0700584 for (i = 0; i < numPtrs; ++i) {
Carl Shapiroa1b03a92010-07-12 14:02:28 -0700585 scanObject(ptrs[i], arg);
Carl Shapiroec805ea2010-06-28 16:28:26 -0700586 }
587 return true;
588}
589
590/*
591 * Re-mark dirtied objects. Iterates through all blackened objects
592 * looking for references to white objects.
593 */
594void dvmMarkDirtyObjects(void)
595{
596 HeapBitmap markBits[HEAP_SOURCE_MAX_HEAP_COUNT];
597 HeapBitmap liveBits[HEAP_SOURCE_MAX_HEAP_COUNT];
598 GcMarkContext *ctx;
599 size_t numBitmaps;
600 size_t i;
601
602 ctx = &gDvm.gcHeap->markContext;
603 /*
Carl Shapirof5860332010-06-28 23:02:08 -0700604 * The finger must have been set to the maximum value to ensure
605 * that gray objects will be pushed onto the mark stack.
Carl Shapiroec805ea2010-06-28 16:28:26 -0700606 */
607 assert(ctx->finger == (void *)ULONG_MAX);
608 numBitmaps = dvmHeapSourceGetNumHeaps();
609 dvmHeapSourceGetObjectBitmaps(liveBits, markBits, numBitmaps);
610 for (i = 0; i < numBitmaps; i++) {
611 dvmHeapBitmapWalk(&markBits[i], dirtyObjectCallback, ctx);
612 }
613 processMarkStack(ctx);
614}
615
Carl Shapiro34f51992010-07-09 17:55:41 -0700616/*
617 * Clear the referent field.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800618 */
Barry Hayes6930a112009-12-22 11:01:38 -0800619static void clearReference(Object *reference)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800620{
Carl Shapiro34f51992010-07-09 17:55:41 -0700621 size_t offset = gDvm.offJavaLangRefReference_referent;
622 dvmSetFieldObject(reference, offset, NULL);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800623}
624
Carl Shapiro29540742010-03-26 15:34:39 -0700625/*
626 * Returns true if the reference was registered with a reference queue
627 * and has not yet been enqueued.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800628 */
Carl Shapiro29540742010-03-26 15:34:39 -0700629static bool isEnqueuable(const Object *reference)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800630{
Barry Hayes6930a112009-12-22 11:01:38 -0800631 Object *queue = dvmGetFieldObject(reference,
632 gDvm.offJavaLangRefReference_queue);
633 Object *queueNext = dvmGetFieldObject(reference,
634 gDvm.offJavaLangRefReference_queueNext);
Carl Shapiro2a6f4842010-07-09 16:50:54 -0700635 return queue != NULL && queueNext == NULL;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800636}
637
Carl Shapiro29540742010-03-26 15:34:39 -0700638/*
639 * Schedules a reference to be appended to its reference queue.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800640 */
Carl Shapiro29540742010-03-26 15:34:39 -0700641static void enqueueReference(Object *ref)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800642{
Carl Shapiro646ba092010-06-10 15:17:00 -0700643 assert(ref != NULL);
Carl Shapiro29540742010-03-26 15:34:39 -0700644 assert(dvmGetFieldObject(ref, gDvm.offJavaLangRefReference_queue) != NULL);
645 assert(dvmGetFieldObject(ref, gDvm.offJavaLangRefReference_queueNext) == NULL);
Carl Shapiro646ba092010-06-10 15:17:00 -0700646 if (!dvmHeapAddRefToLargeTable(&gDvm.gcHeap->referenceOperations, ref)) {
Carl Shapiro29540742010-03-26 15:34:39 -0700647 LOGE_HEAP("enqueueReference(): no room for any more "
648 "reference operations\n");
649 dvmAbort();
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800650 }
651}
652
Carl Shapiro29540742010-03-26 15:34:39 -0700653/*
654 * Walks the reference list marking any references subject to the
655 * reference clearing policy. References with a black referent are
656 * removed from the list. References with white referents biased
657 * toward saving are blackened and also removed from the list.
658 */
659void dvmHandleSoftRefs(Object **list)
660{
661 GcMarkContext *markContext;
662 Object *ref, *referent;
Carl Shapiro2a6f4842010-07-09 16:50:54 -0700663 Object *clear;
Carl Shapiroa1b03a92010-07-12 14:02:28 -0700664 size_t referentOffset;
Carl Shapiro2a6f4842010-07-09 16:50:54 -0700665 size_t counter;
Carl Shapiro29540742010-03-26 15:34:39 -0700666 bool marked;
667
668 markContext = &gDvm.gcHeap->markContext;
Carl Shapiro29540742010-03-26 15:34:39 -0700669 referentOffset = gDvm.offJavaLangRefReference_referent;
Carl Shapiro2a6f4842010-07-09 16:50:54 -0700670 clear = NULL;
Carl Shapiro29540742010-03-26 15:34:39 -0700671 counter = 0;
Carl Shapiro2a6f4842010-07-09 16:50:54 -0700672 while (*list != NULL) {
673 ref = dequeuePendingReference(list);
Carl Shapiro29540742010-03-26 15:34:39 -0700674 referent = dvmGetFieldObject(ref, referentOffset);
Carl Shapiro29540742010-03-26 15:34:39 -0700675 assert(referent != NULL);
676 marked = isMarked(referent, markContext);
677 if (!marked && ((++counter) & 1)) {
678 /* Referent is white and biased toward saving, mark it. */
Barry Hayese1bccb92010-05-18 09:48:37 -0700679 markObject(referent, markContext);
Carl Shapiro29540742010-03-26 15:34:39 -0700680 marked = true;
681 }
Carl Shapiro2a6f4842010-07-09 16:50:54 -0700682 if (!marked) {
683 /* Referent is white, queue it for clearing. */
684 enqueuePendingReference(ref, &clear);
Carl Shapiro29540742010-03-26 15:34:39 -0700685 }
Carl Shapiro29540742010-03-26 15:34:39 -0700686 }
Carl Shapiro2a6f4842010-07-09 16:50:54 -0700687 *list = clear;
Carl Shapiro29540742010-03-26 15:34:39 -0700688 /*
689 * Restart the mark with the newly black references added to the
690 * root set.
691 */
692 processMarkStack(markContext);
693}
694
695/*
Carl Shapiroa1b03a92010-07-12 14:02:28 -0700696 * Unlink the reference list clearing references objects with white
697 * referents. Cleared references registered to a reference queue are
698 * scheduled for appending by the heap worker thread.
Carl Shapiro29540742010-03-26 15:34:39 -0700699 */
700void dvmClearWhiteRefs(Object **list)
701{
702 GcMarkContext *markContext;
703 Object *ref, *referent;
Carl Shapiroa1b03a92010-07-12 14:02:28 -0700704 size_t referentOffset;
Carl Shapiro29540742010-03-26 15:34:39 -0700705 bool doSignal;
706
707 markContext = &gDvm.gcHeap->markContext;
Carl Shapiro29540742010-03-26 15:34:39 -0700708 referentOffset = gDvm.offJavaLangRefReference_referent;
709 doSignal = false;
710 while (*list != NULL) {
Carl Shapiro2a6f4842010-07-09 16:50:54 -0700711 ref = dequeuePendingReference(list);
Carl Shapiro29540742010-03-26 15:34:39 -0700712 referent = dvmGetFieldObject(ref, referentOffset);
Carl Shapiro29540742010-03-26 15:34:39 -0700713 assert(referent != NULL);
714 if (!isMarked(referent, markContext)) {
Carl Shapiroa1b03a92010-07-12 14:02:28 -0700715 /* Referent is white, clear it. */
Carl Shapiro29540742010-03-26 15:34:39 -0700716 clearReference(ref);
717 if (isEnqueuable(ref)) {
718 enqueueReference(ref);
719 doSignal = true;
720 }
721 }
722 }
723 /*
724 * If we cleared a reference with a reference queue we must notify
725 * the heap worker to append the reference.
726 */
727 if (doSignal) {
728 dvmSignalHeapWorker(false);
729 }
730 assert(*list == NULL);
731}
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800732
733/* Find unreachable objects that need to be finalized,
734 * and schedule them for finalization.
735 */
736void dvmHeapScheduleFinalizations()
737{
738 HeapRefTable newPendingRefs;
739 LargeHeapRefTable *finRefs = gDvm.gcHeap->finalizableRefs;
740 Object **ref;
741 Object **lastRef;
742 size_t totalPendCount;
743 GcMarkContext *markContext = &gDvm.gcHeap->markContext;
744
745 /*
746 * All reachable objects have been marked.
747 * Any unmarked finalizable objects need to be finalized.
748 */
749
750 /* Create a table that the new pending refs will
751 * be added to.
752 */
Barry Hayesd4f78d32010-06-08 09:34:42 -0700753 if (!dvmHeapInitHeapRefTable(&newPendingRefs)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800754 //TODO: mark all finalizable refs and hope that
755 // we can schedule them next time. Watch out,
756 // because we may be expecting to free up space
757 // by calling finalizers.
758 LOGE_GC("dvmHeapScheduleFinalizations(): no room for "
759 "pending finalizations\n");
760 dvmAbort();
761 }
762
763 /* Walk through finalizableRefs and move any unmarked references
764 * to the list of new pending refs.
765 */
766 totalPendCount = 0;
767 while (finRefs != NULL) {
768 Object **gapRef;
769 size_t newPendCount = 0;
770
771 gapRef = ref = finRefs->refs.table;
772 lastRef = finRefs->refs.nextEntry;
773 while (ref < lastRef) {
Carl Shapiro6343bd02010-02-16 17:40:19 -0800774 if (!isMarked(*ref, markContext)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800775 if (!dvmHeapAddToHeapRefTable(&newPendingRefs, *ref)) {
776 //TODO: add the current table and allocate
777 // a new, smaller one.
778 LOGE_GC("dvmHeapScheduleFinalizations(): "
779 "no room for any more pending finalizations: %zd\n",
780 dvmHeapNumHeapRefTableEntries(&newPendingRefs));
781 dvmAbort();
782 }
783 newPendCount++;
784 } else {
785 /* This ref is marked, so will remain on finalizableRefs.
786 */
787 if (newPendCount > 0) {
788 /* Copy it up to fill the holes.
789 */
790 *gapRef++ = *ref;
791 } else {
792 /* No holes yet; don't bother copying.
793 */
794 gapRef++;
795 }
796 }
797 ref++;
798 }
799 finRefs->refs.nextEntry = gapRef;
800 //TODO: if the table is empty when we're done, free it.
801 totalPendCount += newPendCount;
802 finRefs = finRefs->next;
803 }
804 LOGD_GC("dvmHeapScheduleFinalizations(): %zd finalizers triggered.\n",
805 totalPendCount);
806 if (totalPendCount == 0) {
807 /* No objects required finalization.
808 * Free the empty temporary table.
809 */
810 dvmClearReferenceTable(&newPendingRefs);
811 return;
812 }
813
814 /* Add the new pending refs to the main list.
815 */
816 if (!dvmHeapAddTableToLargeTable(&gDvm.gcHeap->pendingFinalizationRefs,
817 &newPendingRefs))
818 {
819 LOGE_GC("dvmHeapScheduleFinalizations(): can't insert new "
820 "pending finalizations\n");
821 dvmAbort();
822 }
823
824 //TODO: try compacting the main list with a memcpy loop
825
826 /* Mark the refs we just moved; we don't want them or their
827 * children to get swept yet.
828 */
829 ref = newPendingRefs.table;
830 lastRef = newPendingRefs.nextEntry;
831 assert(ref < lastRef);
832 HPROF_SET_GC_SCAN_STATE(HPROF_ROOT_FINALIZING, 0);
833 while (ref < lastRef) {
Barry Hayese1bccb92010-05-18 09:48:37 -0700834 assert(*ref != NULL);
835 markObject(*ref, markContext);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800836 ref++;
837 }
838 HPROF_CLEAR_GC_SCAN_STATE();
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800839 processMarkStack(markContext);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800840 dvmSignalHeapWorker(false);
841}
842
843void dvmHeapFinishMarkStep()
844{
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800845 GcMarkContext *markContext;
846
847 markContext = &gDvm.gcHeap->markContext;
848
849 /* The sweep step freed every object that appeared in the
850 * HeapSource bitmaps that didn't appear in the mark bitmaps.
851 * The new state of the HeapSource is exactly the final
852 * mark bitmaps, so swap them in.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800853 */
Carl Shapirof373efd2010-02-19 00:46:33 -0800854 dvmHeapSourceSwapBitmaps();
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800855
Carl Shapirof373efd2010-02-19 00:46:33 -0800856 /* Clean up everything else associated with the marking process.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800857 */
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800858 destroyMarkStack(&markContext->stack);
859
Carl Shapirof373efd2010-02-19 00:46:33 -0800860 markContext->finger = NULL;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800861}
862
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800863static bool
864sweepBitmapCallback(size_t numPtrs, void **ptrs, const void *finger, void *arg)
865{
866 const ClassObject *const classJavaLangClass = gDvm.classJavaLangClass;
Barry Hayes5cbb2302010-02-02 14:07:37 -0800867 const bool overwriteFree = gDvm.overwriteFree;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800868 size_t i;
869
870 for (i = 0; i < numPtrs; i++) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800871 Object *obj;
872
Barry Hayes04174be2010-07-21 11:51:37 -0700873 obj = (Object *)ptrs[i];
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800874
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800875 /* This assumes that java.lang.Class will never go away.
876 * If it can, and we were the last reference to it, it
877 * could have already been swept. However, even in that case,
878 * gDvm.classJavaLangClass should still have a useful
879 * value.
880 */
881 if (obj->clazz == classJavaLangClass) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800882 /* dvmFreeClassInnards() may have already been called,
883 * but it's safe to call on the same ClassObject twice.
884 */
885 dvmFreeClassInnards((ClassObject *)obj);
886 }
887
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800888 /* Overwrite the to-be-freed object to make stale references
889 * more obvious.
890 */
Barry Hayes5cbb2302010-02-02 14:07:37 -0800891 if (overwriteFree) {
Barry Hayes2e3c3e12010-02-22 09:39:10 -0800892 int objlen;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800893 ClassObject *clazz = obj->clazz;
Barry Hayes2e3c3e12010-02-22 09:39:10 -0800894 objlen = dvmHeapSourceChunkSize(obj);
895 memset(obj, 0xa5, objlen);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800896 obj->clazz = (ClassObject *)((uintptr_t)clazz ^ 0xffffffff);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800897 }
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800898 }
Barry Hayesdde8ab02009-05-20 12:10:36 -0700899 // TODO: dvmHeapSourceFreeList has a loop, just like the above
900 // does. Consider collapsing the two loops to save overhead.
Barry Hayes04174be2010-07-21 11:51:37 -0700901 dvmHeapSourceFreeList(numPtrs, ptrs);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800902
903 return true;
904}
905
Carl Shapiro5a6071b2010-01-07 21:35:50 -0800906/* Returns true if the given object is unmarked. Ignores the low bits
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800907 * of the pointer because the intern table may set them.
908 */
909static int isUnmarkedObject(void *object)
910{
Carl Shapiro6343bd02010-02-16 17:40:19 -0800911 return !isMarked((void *)((uintptr_t)object & ~(HB_OBJECT_ALIGNMENT-1)),
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800912 &gDvm.gcHeap->markContext);
913}
914
915/* Walk through the list of objects that haven't been
916 * marked and free them.
917 */
918void
Carl Shapirod25566d2010-03-11 20:39:47 -0800919dvmHeapSweepUnmarkedObjects(GcMode mode, int *numFreed, size_t *sizeFreed)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800920{
Carl Shapirof373efd2010-02-19 00:46:33 -0800921 HeapBitmap markBits[HEAP_SOURCE_MAX_HEAP_COUNT];
Carl Shapirod77f7fd2010-04-05 19:23:31 -0700922 HeapBitmap liveBits[HEAP_SOURCE_MAX_HEAP_COUNT];
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800923 size_t origObjectsAllocated;
924 size_t origBytesAllocated;
Carl Shapirod25566d2010-03-11 20:39:47 -0800925 size_t numBitmaps, numSweepBitmaps;
Barry Hayese168ebd2010-05-07 09:19:46 -0700926 size_t i;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800927
928 /* All reachable objects have been marked.
929 * Detach any unreachable interned strings before
930 * we sweep.
931 */
932 dvmGcDetachDeadInternedStrings(isUnmarkedObject);
933
934 /* Free any known objects that are not marked.
935 */
936 origObjectsAllocated = dvmHeapSourceGetValue(HS_OBJECTS_ALLOCATED, NULL, 0);
937 origBytesAllocated = dvmHeapSourceGetValue(HS_BYTES_ALLOCATED, NULL, 0);
938
Carl Shapiro5a6071b2010-01-07 21:35:50 -0800939 dvmSweepMonitorList(&gDvm.monitorList, isUnmarkedObject);
940
Carl Shapirof373efd2010-02-19 00:46:33 -0800941 numBitmaps = dvmHeapSourceGetNumHeaps();
Carl Shapirod77f7fd2010-04-05 19:23:31 -0700942 dvmHeapSourceGetObjectBitmaps(liveBits, markBits, numBitmaps);
Carl Shapirod25566d2010-03-11 20:39:47 -0800943 if (mode == GC_PARTIAL) {
944 numSweepBitmaps = 1;
Carl Shapirod77f7fd2010-04-05 19:23:31 -0700945 assert((uintptr_t)gDvm.gcHeap->markContext.immuneLimit == liveBits[0].base);
Carl Shapirod25566d2010-03-11 20:39:47 -0800946 } else {
947 numSweepBitmaps = numBitmaps;
948 }
Barry Hayese168ebd2010-05-07 09:19:46 -0700949 for (i = 0; i < numSweepBitmaps; i++) {
950 dvmHeapBitmapXorWalk(&markBits[i], &liveBits[i],
951 sweepBitmapCallback, NULL);
952 }
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800953
954 *numFreed = origObjectsAllocated -
955 dvmHeapSourceGetValue(HS_OBJECTS_ALLOCATED, NULL, 0);
956 *sizeFreed = origBytesAllocated -
957 dvmHeapSourceGetValue(HS_BYTES_ALLOCATED, NULL, 0);
958
959#ifdef WITH_PROFILER
960 if (gDvm.allocProf.enabled) {
961 gDvm.allocProf.freeCount += *numFreed;
962 gDvm.allocProf.freeSize += *sizeFreed;
963 }
964#endif
965}