blob: 750256cce3485c5d13cef0ec3b0491e4453886d5 [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"
Carl Shapiro7ec91442010-10-21 15:35:19 -070019#include "alloc/CardTable.h"
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080020#include "alloc/HeapBitmap.h"
21#include "alloc/HeapInternal.h"
22#include "alloc/HeapSource.h"
23#include "alloc/MarkSweep.h"
Carl Shapiroec805ea2010-06-28 16:28:26 -070024#include "alloc/Visit.h"
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080025#include <limits.h> // for ULONG_MAX
26#include <sys/mman.h> // for madvise(), mmap()
The Android Open Source Project99409882009-03-18 22:20:24 -070027#include <errno.h>
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080028
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080029#define GC_LOG_TAG LOG_TAG "-gc"
30
31#if LOG_NDEBUG
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080032#define LOGD_GC(...) ((void)0)
33#else
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080034#define LOGD_GC(...) LOG(LOG_DEBUG, GC_LOG_TAG, __VA_ARGS__)
35#endif
36
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080037#define LOGE_GC(...) LOG(LOG_ERROR, GC_LOG_TAG, __VA_ARGS__)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080038
Carl Shapiro7ec91442010-10-21 15:35:19 -070039#define ALIGN_DOWN(x, n) ((size_t)(x) & -(n))
Carl Shapiro57ee2702010-08-27 13:06:48 -070040#define ALIGN_UP(x, n) (((size_t)(x) + (n) - 1) & ~((n) - 1))
41#define ALIGN_UP_TO_PAGE_SIZE(p) ALIGN_UP(p, SYSTEM_PAGE_SIZE)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080042
Carl Shapiro7ec91442010-10-21 15:35:19 -070043typedef unsigned long Word;
44const size_t kWordSize = sizeof(Word);
45
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080046/* Do not cast the result of this to a boolean; the only set bit
47 * may be > 1<<8.
48 */
Carl Shapiro6343bd02010-02-16 17:40:19 -080049static inline long isMarked(const void *obj, const GcMarkContext *ctx)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080050{
Carl Shapirof373efd2010-02-19 00:46:33 -080051 return dvmHeapBitmapIsObjectBitSet(ctx->bitmap, obj);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080052}
53
Carl Shapirofdf80522010-11-09 14:27:02 -080054/*
55 * Initializes the stack top and advises the mark stack pages as needed.
56 */
Carl Shapirod7400e02010-09-02 18:24:29 -070057static bool createMarkStack(GcMarkStack *stack)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080058{
Carl Shapirofdf80522010-11-09 14:27:02 -080059 assert(stack != NULL);
60 size_t length = dvmHeapSourceGetIdealFootprint() * sizeof(Object*) /
61 (sizeof(Object) + HEAP_SOURCE_CHUNK_OVERHEAD);
62 madvise(stack->base, length, MADV_NORMAL);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080063 stack->top = stack->base;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080064 return true;
65}
66
Carl Shapirofdf80522010-11-09 14:27:02 -080067/*
68 * Assigns NULL to the stack top and advises the mark stack pages as
69 * not needed.
70 */
Carl Shapirod7400e02010-09-02 18:24:29 -070071static void destroyMarkStack(GcMarkStack *stack)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080072{
Carl Shapirofdf80522010-11-09 14:27:02 -080073 assert(stack != NULL);
74 madvise(stack->base, stack->length, MADV_DONTNEED);
75 stack->top = NULL;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080076}
77
Carl Shapirofdf80522010-11-09 14:27:02 -080078/*
79 * Pops an object from the mark stack.
80 */
81static void markStackPush(GcMarkStack *stack, const Object *obj)
82{
83 assert(stack != NULL);
84 assert(stack->base <= stack->top);
85 assert(stack->limit > stack->top);
86 assert(obj != NULL);
87 *stack->top = obj;
88 ++stack->top;
89}
90
91/*
92 * Pushes an object on the mark stack.
93 */
94static const Object *markStackPop(GcMarkStack *stack)
95{
Carl Shapirofdf80522010-11-09 14:27:02 -080096 assert(stack != NULL);
97 assert(stack->base < stack->top);
98 assert(stack->limit > stack->top);
99 --stack->top;
Carl Shapiro26a82802010-12-01 18:54:19 -0800100 return *stack->top;
Carl Shapirofdf80522010-11-09 14:27:02 -0800101}
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800102
Carl Shapirod7400e02010-09-02 18:24:29 -0700103bool dvmHeapBeginMarkStep(GcMode mode)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800104{
Carl Shapirob2e78d32010-08-20 11:34:18 -0700105 GcMarkContext *ctx = &gDvm.gcHeap->markContext;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800106
Carl Shapirob2e78d32010-08-20 11:34:18 -0700107 if (!createMarkStack(&ctx->stack)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800108 return false;
109 }
Carl Shapirob2e78d32010-08-20 11:34:18 -0700110 ctx->finger = NULL;
111 ctx->immuneLimit = dvmHeapSourceGetImmuneLimit(mode);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800112 return true;
113}
114
Carl Shapirod7400e02010-09-02 18:24:29 -0700115static long setAndReturnMarkBit(GcMarkContext *ctx, const void *obj)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800116{
Carl Shapirof373efd2010-02-19 00:46:33 -0800117 return dvmHeapBitmapSetAndReturnObjectBit(ctx->bitmap, obj);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800118}
119
Carl Shapirod7400e02010-09-02 18:24:29 -0700120static void markObjectNonNull(const Object *obj, GcMarkContext *ctx,
121 bool checkFinger)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800122{
Barry Hayese1bccb92010-05-18 09:48:37 -0700123 assert(ctx != NULL);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800124 assert(obj != NULL);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800125 assert(dvmIsValidObject(obj));
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800126
Carl Shapirob31b3012010-05-25 18:35:37 -0700127 if (obj < (Object *)ctx->immuneLimit) {
Carl Shapirod25566d2010-03-11 20:39:47 -0800128 assert(isMarked(obj, ctx));
129 return;
130 }
Carl Shapiro6343bd02010-02-16 17:40:19 -0800131 if (!setAndReturnMarkBit(ctx, obj)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800132 /* This object was not previously marked.
133 */
Carl Shapiro106c5fd2010-07-28 14:12:27 -0700134 if (checkFinger && (void *)obj < ctx->finger) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800135 /* This object will need to go on the mark stack.
136 */
Carl Shapirofdf80522010-11-09 14:27:02 -0800137 markStackPush(&ctx->stack, obj);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800138 }
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800139 }
140}
141
142/* Used to mark objects when recursing. Recursion is done by moving
143 * the finger across the bitmaps in address order and marking child
144 * objects. Any newly-marked objects whose addresses are lower than
145 * the finger won't be visited by the bitmap scan, so those objects
146 * need to be added to the mark stack.
147 */
Barry Hayese1bccb92010-05-18 09:48:37 -0700148static void markObject(const Object *obj, GcMarkContext *ctx)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800149{
Barry Hayese1bccb92010-05-18 09:48:37 -0700150 if (obj != NULL) {
Carl Shapiro106c5fd2010-07-28 14:12:27 -0700151 markObjectNonNull(obj, ctx, true);
Barry Hayese1bccb92010-05-18 09:48:37 -0700152 }
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800153}
154
Carl Shapiro6d4ce5e2010-12-02 16:16:01 -0800155/*
156 * Callback applied to root references during the initial root
157 * marking. Visited roots are always marked but are only pushed on
158 * the mark stack if their address is below the finger.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800159 */
Carl Shapiro6d4ce5e2010-12-02 16:16:01 -0800160static void rootMarkObjectVisitor(void *addr, RootType type, u4 thread, void *arg)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800161{
Carl Shapiro6d4ce5e2010-12-02 16:16:01 -0800162 Object *obj;
163
164 assert(addr != NULL);
165 assert(arg != NULL);
166 obj = *(Object **)addr;
167 if (obj != NULL) {
168 markObjectNonNull(obj, arg, false);
169 }
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;
Barry Hayes425848f2010-05-04 13:32:12 -0700201 dvmMarkImmuneObjects(gcHeap->markContext.immuneLimit);
Carl Shapiro6d4ce5e2010-12-02 16:16:01 -0800202 dvmVisitRoots(rootMarkObjectVisitor, &gcHeap->markContext);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800203}
204
205/*
Carl Shapiro6d4ce5e2010-12-02 16:16:01 -0800206 * Callback applied to root references during root remarking. If the
207 * root location contains a white reference it is pushed on the mark
208 * stack and grayed.
Carl Shapiro106c5fd2010-07-28 14:12:27 -0700209 */
Carl Shapiro07018e22010-10-26 21:07:41 -0700210static void markObjectVisitor(void *addr, RootType type, u4 thread, void *arg)
Carl Shapiro106c5fd2010-07-28 14:12:27 -0700211{
212 Object *obj;
213
214 assert(addr != NULL);
215 assert(arg != NULL);
216 obj = *(Object **)addr;
217 if (obj != NULL) {
218 markObjectNonNull(obj, arg, true);
219 }
220}
221
222/*
223 * Grays all references in the roots.
224 */
225void dvmHeapReMarkRootSet(void)
226{
227 GcMarkContext *ctx = &gDvm.gcHeap->markContext;
228 assert(ctx->finger == (void *)ULONG_MAX);
229 dvmVisitRoots(markObjectVisitor, ctx);
230}
231
232/*
Barry Hayese1bccb92010-05-18 09:48:37 -0700233 * Scans instance fields.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800234 */
Carl Shapiro3e24d332010-11-29 17:24:50 -0800235static void scanFields(const Object *obj, GcMarkContext *ctx)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800236{
Barry Hayese1bccb92010-05-18 09:48:37 -0700237 assert(obj != NULL);
238 assert(obj->clazz != NULL);
239 assert(ctx != NULL);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800240
Barry Hayese1bccb92010-05-18 09:48:37 -0700241 if (obj->clazz->refOffsets != CLASS_WALK_SUPER) {
242 unsigned int refOffsets = obj->clazz->refOffsets;
Barry Hayeseac47ed2009-06-22 11:45:20 -0700243 while (refOffsets != 0) {
Carl Shapiro3e24d332010-11-29 17:24:50 -0800244 size_t rshift = CLZ(refOffsets);
245 size_t offset = CLASS_OFFSET_FROM_CLZ(rshift);
246 Object *ref = dvmGetFieldObject((Object*)obj, offset);
247 markObject(ref, ctx);
Barry Hayeseac47ed2009-06-22 11:45:20 -0700248 refOffsets &= ~(CLASS_HIGH_BIT >> rshift);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800249 }
Barry Hayeseac47ed2009-06-22 11:45:20 -0700250 } else {
Barry Hayese1bccb92010-05-18 09:48:37 -0700251 ClassObject *clazz;
Barry Hayese1bccb92010-05-18 09:48:37 -0700252 for (clazz = obj->clazz; clazz != NULL; clazz = clazz->super) {
253 InstField *field = clazz->ifields;
Carl Shapiro3e24d332010-11-29 17:24:50 -0800254 int i;
Barry Hayese1bccb92010-05-18 09:48:37 -0700255 for (i = 0; i < clazz->ifieldRefCount; ++i, ++field) {
256 void *addr = BYTE_OFFSET((Object *)obj, field->byteOffset);
257 markObject(((JValue *)addr)->l, ctx);
Barry Hayeseac47ed2009-06-22 11:45:20 -0700258 }
Barry Hayeseac47ed2009-06-22 11:45:20 -0700259 }
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800260 }
261}
262
Barry Hayese1bccb92010-05-18 09:48:37 -0700263/*
Carl Shapiro3e24d332010-11-29 17:24:50 -0800264 * Scans the static fields of a class object.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800265 */
Carl Shapiro3e24d332010-11-29 17:24:50 -0800266static void scanStaticFields(const ClassObject *clazz, GcMarkContext *ctx)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800267{
Barry Hayese1bccb92010-05-18 09:48:37 -0700268 int i;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800269
Carl Shapiro3e24d332010-11-29 17:24:50 -0800270 assert(clazz != NULL);
Barry Hayese1bccb92010-05-18 09:48:37 -0700271 assert(ctx != NULL);
Carl Shapiro3e24d332010-11-29 17:24:50 -0800272 for (i = 0; i < clazz->sfieldCount; ++i) {
273 char ch = clazz->sfields[i].field.signature[0];
274 if (ch == '[' || ch == 'L') {
275 markObject(clazz->sfields[i].value.l, ctx);
276 }
277 }
278}
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800279
Carl Shapiro3e24d332010-11-29 17:24:50 -0800280/*
281 * Visit the interfaces of a class object.
282 */
283static void scanInterfaces(const ClassObject *clazz, GcMarkContext *ctx)
284{
285 int i;
286
287 assert(clazz != NULL);
288 assert(ctx != NULL);
289 for (i = 0; i < clazz->interfaceCount; ++i) {
290 markObject((const Object *)clazz->interfaces[i], ctx);
291 }
292}
293
294/*
295 * Scans the header, static field references, and interface
296 * pointers of a class object.
297 */
298static void scanClassObject(const Object *obj, GcMarkContext *ctx)
299{
300 const ClassObject *asClass;
301
302 assert(obj != NULL);
303 assert(obj->clazz == gDvm.classJavaLangClass);
304 assert(ctx != NULL);
305 markObject((const Object *)obj->clazz, ctx);
306 asClass = (const ClassObject *)obj;
307 if (IS_CLASS_FLAG_SET(asClass, CLASS_ISARRAY)) {
308 markObject((const Object *)asClass->elementClass, ctx);
Barry Hayese1bccb92010-05-18 09:48:37 -0700309 }
Barry Hayesc49db852010-05-14 13:43:34 -0700310 /* Do super and the interfaces contain Objects and not dex idx values? */
Carl Shapiro3e24d332010-11-29 17:24:50 -0800311 if (asClass->status > CLASS_IDX) {
312 markObject((const Object *)asClass->super, ctx);
Barry Hayesc49db852010-05-14 13:43:34 -0700313 }
Carl Shapiro3e24d332010-11-29 17:24:50 -0800314 markObject((const Object *)asClass->classLoader, ctx);
315 scanFields(obj, ctx);
316 scanStaticFields(asClass, ctx);
317 if (asClass->status > CLASS_IDX) {
318 scanInterfaces(asClass, ctx);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800319 }
320}
321
Barry Hayese1bccb92010-05-18 09:48:37 -0700322/*
323 * Scans the header of all array objects. If the array object is
324 * specialized to a reference type, scans the array data as well.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800325 */
Carl Shapiro3e24d332010-11-29 17:24:50 -0800326static void scanArrayObject(const Object *obj, GcMarkContext *ctx)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800327{
Barry Hayese1bccb92010-05-18 09:48:37 -0700328 assert(obj != NULL);
Carl Shapiro3e24d332010-11-29 17:24:50 -0800329 assert(obj->clazz != NULL);
Barry Hayese1bccb92010-05-18 09:48:37 -0700330 assert(ctx != NULL);
Carl Shapiro3e24d332010-11-29 17:24:50 -0800331 markObject((const Object *)obj->clazz, ctx);
332 if (IS_CLASS_FLAG_SET(obj->clazz, CLASS_ISOBJECTARRAY)) {
333 const ArrayObject *array = (const ArrayObject *)obj;
334 const Object **contents = (const Object **)array->contents;
335 size_t i;
336 for (i = 0; i < array->length; ++i) {
Barry Hayese1bccb92010-05-18 09:48:37 -0700337 markObject(contents[i], ctx);
338 }
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800339 }
Barry Hayese1bccb92010-05-18 09:48:37 -0700340}
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800341
Barry Hayese1bccb92010-05-18 09:48:37 -0700342/*
Carl Shapiro2a6f4842010-07-09 16:50:54 -0700343 * Returns class flags relating to Reference subclasses.
344 */
345static int referenceClassFlags(const Object *obj)
346{
347 int flags = CLASS_ISREFERENCE |
348 CLASS_ISWEAKREFERENCE |
349 CLASS_ISPHANTOMREFERENCE;
350 return GET_CLASS_FLAG_GROUP(obj->clazz, flags);
351}
352
353/*
354 * Returns true if the object derives from SoftReference.
355 */
356static bool isSoftReference(const Object *obj)
357{
358 return referenceClassFlags(obj) == CLASS_ISREFERENCE;
359}
360
361/*
362 * Returns true if the object derives from WeakReference.
363 */
364static bool isWeakReference(const Object *obj)
365{
366 return referenceClassFlags(obj) & CLASS_ISWEAKREFERENCE;
367}
368
369/*
370 * Returns true if the object derives from PhantomReference.
371 */
372static bool isPhantomReference(const Object *obj)
373{
374 return referenceClassFlags(obj) & CLASS_ISPHANTOMREFERENCE;
375}
376
377/*
378 * Adds a reference to the tail of a circular queue of references.
379 */
380static void enqueuePendingReference(Object *ref, Object **list)
381{
382 size_t offset;
383
384 assert(ref != NULL);
385 assert(list != NULL);
386 offset = gDvm.offJavaLangRefReference_pendingNext;
387 if (*list == NULL) {
388 dvmSetFieldObject(ref, offset, ref);
389 *list = ref;
390 } else {
391 Object *head = dvmGetFieldObject(*list, offset);
392 dvmSetFieldObject(ref, offset, head);
393 dvmSetFieldObject(*list, offset, ref);
394 }
395}
396
397/*
Carl Shapiroa1b03a92010-07-12 14:02:28 -0700398 * Removes the reference at the head of a circular queue of
399 * references.
Carl Shapiro2a6f4842010-07-09 16:50:54 -0700400 */
401static Object *dequeuePendingReference(Object **list)
402{
403 Object *ref, *head;
404 size_t offset;
405
406 assert(list != NULL);
407 assert(*list != NULL);
408 offset = gDvm.offJavaLangRefReference_pendingNext;
409 head = dvmGetFieldObject(*list, offset);
410 if (*list == head) {
411 ref = *list;
412 *list = NULL;
413 } else {
414 Object *next = dvmGetFieldObject(head, offset);
415 dvmSetFieldObject(*list, offset, next);
416 ref = head;
417 }
418 dvmSetFieldObject(ref, offset, NULL);
419 return ref;
420}
421
422/*
Barry Hayese1bccb92010-05-18 09:48:37 -0700423 * Process the "referent" field in a java.lang.ref.Reference. If the
424 * referent has not yet been marked, put it on the appropriate list in
425 * the gcHeap for later processing.
426 */
Barry Hayes697b5a92010-06-23 11:38:52 -0700427static void delayReferenceReferent(Object *obj, GcMarkContext *ctx)
Barry Hayese1bccb92010-05-18 09:48:37 -0700428{
Carl Shapiro2a6f4842010-07-09 16:50:54 -0700429 GcHeap *gcHeap = gDvm.gcHeap;
430 Object *pending, *referent;
431 size_t pendingNextOffset, referentOffset;
432
Barry Hayese1bccb92010-05-18 09:48:37 -0700433 assert(obj != NULL);
Barry Hayes697b5a92010-06-23 11:38:52 -0700434 assert(obj->clazz != NULL);
Carl Shapiro2a6f4842010-07-09 16:50:54 -0700435 assert(IS_CLASS_FLAG_SET(obj->clazz, CLASS_ISREFERENCE));
Barry Hayese1bccb92010-05-18 09:48:37 -0700436 assert(ctx != NULL);
Carl Shapiro2a6f4842010-07-09 16:50:54 -0700437 pendingNextOffset = gDvm.offJavaLangRefReference_pendingNext;
438 referentOffset = gDvm.offJavaLangRefReference_referent;
439 pending = dvmGetFieldObject(obj, pendingNextOffset);
440 referent = dvmGetFieldObject(obj, referentOffset);
441 if (pending == NULL && referent != NULL && !isMarked(referent, ctx)) {
442 Object **list = NULL;
443 if (isSoftReference(obj)) {
444 list = &gcHeap->softReferences;
445 } else if (isWeakReference(obj)) {
446 list = &gcHeap->weakReferences;
447 } else if (isPhantomReference(obj)) {
448 list = &gcHeap->phantomReferences;
Barry Hayese1bccb92010-05-18 09:48:37 -0700449 }
Carl Shapiro2a6f4842010-07-09 16:50:54 -0700450 assert(list != NULL);
451 enqueuePendingReference(obj, list);
Barry Hayese1bccb92010-05-18 09:48:37 -0700452 }
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800453}
454
Barry Hayese1bccb92010-05-18 09:48:37 -0700455/*
456 * Scans the header and field references of a data object.
457 */
Carl Shapiro3e24d332010-11-29 17:24:50 -0800458static void scanDataObject(const Object *obj, GcMarkContext *ctx)
Barry Hayese1bccb92010-05-18 09:48:37 -0700459{
460 assert(obj != NULL);
Carl Shapiro3e24d332010-11-29 17:24:50 -0800461 assert(obj->clazz != NULL);
Barry Hayese1bccb92010-05-18 09:48:37 -0700462 assert(ctx != NULL);
Carl Shapiro3e24d332010-11-29 17:24:50 -0800463 markObject((const Object *)obj->clazz, ctx);
464 scanFields(obj, ctx);
465 if (IS_CLASS_FLAG_SET(obj->clazz, CLASS_ISREFERENCE)) {
Barry Hayes697b5a92010-06-23 11:38:52 -0700466 delayReferenceReferent((Object *)obj, ctx);
Barry Hayese1bccb92010-05-18 09:48:37 -0700467 }
468}
469
470/*
471 * Scans an object reference. Determines the type of the reference
472 * and dispatches to a specialized scanning routine.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800473 */
474static void scanObject(const Object *obj, GcMarkContext *ctx)
475{
Barry Hayese1bccb92010-05-18 09:48:37 -0700476 assert(obj != NULL);
477 assert(ctx != NULL);
Barry Hayes899cdb72010-06-08 09:59:12 -0700478 assert(obj->clazz != NULL);
Carl Shapiro1a8e21a2010-06-08 13:19:57 -0700479 if (obj->clazz == gDvm.classJavaLangClass) {
Carl Shapiro3e24d332010-11-29 17:24:50 -0800480 scanClassObject(obj, ctx);
Carl Shapiro1a8e21a2010-06-08 13:19:57 -0700481 } else if (IS_CLASS_FLAG_SET(obj->clazz, CLASS_ISARRAY)) {
Carl Shapiro3e24d332010-11-29 17:24:50 -0800482 scanArrayObject(obj, ctx);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800483 } else {
Carl Shapiro3e24d332010-11-29 17:24:50 -0800484 scanDataObject(obj, ctx);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800485 }
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800486}
487
Carl Shapirofdf80522010-11-09 14:27:02 -0800488/*
489 * Scan anything that's on the mark stack. We can't use the bitmaps
490 * anymore, so use a finger that points past the end of them.
491 */
492static void processMarkStack(GcMarkContext *ctx)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800493{
Carl Shapirofdf80522010-11-09 14:27:02 -0800494 GcMarkStack *stack;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800495
Carl Shapirofdf80522010-11-09 14:27:02 -0800496 assert(ctx != NULL);
Carl Shapiro034fba52010-11-11 16:39:58 -0800497 assert(ctx->finger == (void *)ULONG_MAX);
Carl Shapirofdf80522010-11-09 14:27:02 -0800498 stack = &ctx->stack;
499 assert(stack->top >= stack->base);
500 while (stack->top > stack->base) {
501 const Object *obj = markStackPop(stack);
502 scanObject(obj, ctx);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800503 }
504}
505
Carl Shapiro106c5fd2010-07-28 14:12:27 -0700506static size_t objectSize(const Object *obj)
507{
508 assert(dvmIsValidObject(obj));
509 assert(dvmIsValidObject((Object *)obj->clazz));
510 if (IS_CLASS_FLAG_SET(obj->clazz, CLASS_ISARRAY)) {
511 return dvmArrayObjectSize((ArrayObject *)obj);
512 } else if (obj->clazz == gDvm.classJavaLangClass) {
513 return dvmClassObjectSize((ClassObject *)obj);
514 } else {
515 return obj->clazz->objectSize;
516 }
517}
518
519/*
Carl Shapiro106c5fd2010-07-28 14:12:27 -0700520 * Scans forward to the header of the next marked object between start
521 * and limit. Returns NULL if no marked objects are in that region.
522 */
Carl Shapiro7ec91442010-10-21 15:35:19 -0700523static Object *nextGrayObject(const u1 *base, const u1 *limit,
524 const HeapBitmap *markBits)
Carl Shapiro106c5fd2010-07-28 14:12:27 -0700525{
Carl Shapiro7ec91442010-10-21 15:35:19 -0700526 const u1 *ptr;
Carl Shapiro106c5fd2010-07-28 14:12:27 -0700527
528 assert(base < limit);
529 assert(limit - base <= GC_CARD_SIZE);
530 for (ptr = base; ptr < limit; ptr += HB_OBJECT_ALIGNMENT) {
Carl Shapiroea10c552010-09-02 23:32:25 -0700531 if (dvmHeapBitmapIsObjectBitSet(markBits, ptr))
Carl Shapiro106c5fd2010-07-28 14:12:27 -0700532 return (Object *)ptr;
533 }
534 return NULL;
535}
536
537/*
Carl Shapiro7ec91442010-10-21 15:35:19 -0700538 * Scans each byte from start below end returning the address of the
539 * first dirty card. Returns NULL if no dirty card is found.
540 */
541static const u1 *scanBytesForDirtyCard(const u1 *start, const u1 *end)
542{
543 const u1 *ptr;
544
545 assert(start <= end);
546 for (ptr = start; ptr < end; ++ptr) {
547 if (*ptr == GC_CARD_DIRTY) {
548 return ptr;
549 }
550 }
551 return NULL;
552}
553
554/*
555 * Like scanBytesForDirtyCard but scans the range from start below end
556 * by words. Assumes start and end are word aligned.
557 */
558static const u1 *scanWordsForDirtyCard(const u1 *start, const u1 *end)
559{
560 const u1 *ptr;
561
562 assert((uintptr_t)start % kWordSize == 0);
563 assert((uintptr_t)end % kWordSize == 0);
564 assert(start <= end);
565 for (ptr = start; ptr < end; ptr += kWordSize) {
566 if (*(const Word *)ptr != 0) {
567 const u1 *dirty = scanBytesForDirtyCard(ptr, ptr + kWordSize);
568 if (dirty != NULL) {
569 return dirty;
570 }
571 }
572 }
573 return NULL;
574}
575
576/*
577 * Scans the card table as quickly as possible looking for a dirty
578 * card. Returns the address of the first dirty card found or NULL if
579 * no dirty cards were found.
580 */
581static const u1 *nextDirtyCard(const u1 *start, const u1 *end)
582{
583 const u1 *wstart = (u1 *)ALIGN_UP(start, kWordSize);
584 const u1 *wend = (u1 *)ALIGN_DOWN(end, kWordSize);
585 const u1 *ptr, *dirty;
586
587 assert(start <= end);
588 assert(start <= wstart);
589 assert(end >= wend);
590 ptr = start;
591 if (wstart < end) {
592 /* Scan the leading unaligned bytes. */
593 dirty = scanBytesForDirtyCard(ptr, wstart);
594 if (dirty != NULL) {
595 return dirty;
596 }
597 /* Scan the range of aligned words. */
598 dirty = scanWordsForDirtyCard(wstart, wend);
599 if (dirty != NULL) {
600 return dirty;
601 }
602 ptr = wend;
603 }
604 /* Scan trailing unaligned bytes. */
605 dirty = scanBytesForDirtyCard(ptr, end);
606 if (dirty != NULL) {
607 return dirty;
608 }
609 return NULL;
610}
611
612/*
613 * Scans range of dirty cards between start and end. A range of dirty
614 * cards is composed consecutively dirty cards or dirty cards spanned
615 * by a gray object. Returns the address of a clean card if the scan
616 * reached a clean card or NULL if the scan reached the end.
617 */
618const u1 *scanDirtyCards(const u1 *start, const u1 *end,
619 GcMarkContext *ctx)
620{
621 const HeapBitmap *markBits = ctx->bitmap;
622 const u1 *card = start, *prevAddr = NULL;
623 while (card < end) {
624 if (*card != GC_CARD_DIRTY) {
625 return card;
626 }
627 const u1 *ptr = prevAddr ? prevAddr : dvmAddrFromCard(card);
628 const u1 *limit = ptr + GC_CARD_SIZE;
629 while (ptr < limit) {
630 Object *obj = nextGrayObject(ptr, limit, markBits);
631 if (obj == NULL) {
632 break;
633 }
634 scanObject(obj, ctx);
635 ptr = (u1*)obj + ALIGN_UP(objectSize(obj), HB_OBJECT_ALIGNMENT);
636 }
637 if (ptr < limit) {
638 /* Ended within the current card, advance to the next card. */
639 ++card;
640 prevAddr = NULL;
641 } else {
642 /* Ended past the current card, skip ahead. */
643 card = dvmCardFromAddr(ptr);
644 prevAddr = ptr;
645 }
646 }
647 return NULL;
648}
649
650/*
651 * Blackens gray objects found on dirty cards.
Carl Shapiro106c5fd2010-07-28 14:12:27 -0700652 */
653static void scanGrayObjects(GcMarkContext *ctx)
654{
655 GcHeap *h = gDvm.gcHeap;
Carl Shapiro7ec91442010-10-21 15:35:19 -0700656 const u1 *base, *limit, *ptr, *dirty;
Carl Shapirob8c48ae2010-08-12 11:24:44 -0700657 size_t footprint;
Carl Shapiro106c5fd2010-07-28 14:12:27 -0700658
Carl Shapirob8c48ae2010-08-12 11:24:44 -0700659 footprint = dvmHeapSourceGetValue(HS_FOOTPRINT, NULL, 0);
Carl Shapiro7ec91442010-10-21 15:35:19 -0700660 base = &h->cardTableBase[0];
661 limit = dvmCardFromAddr((u1 *)dvmHeapSourceGetBase() + footprint);
662 assert(limit <= &h->cardTableBase[h->cardTableLength]);
663
664 ptr = base;
665 for (;;) {
666 dirty = nextDirtyCard(ptr, limit);
667 if (dirty == NULL) {
668 break;
Carl Shapiro106c5fd2010-07-28 14:12:27 -0700669 }
Carl Shapiro7ec91442010-10-21 15:35:19 -0700670 assert((dirty > ptr) && (dirty < limit));
671 ptr = scanDirtyCards(dirty, limit, ctx);
672 if (ptr == NULL) {
673 break;
674 }
675 assert((ptr > dirty) && (ptr < limit));
Carl Shapiro106c5fd2010-07-28 14:12:27 -0700676 }
677}
678
Carl Shapiro57ee2702010-08-27 13:06:48 -0700679/*
680 * Callback for scanning each object in the bitmap. The finger is set
681 * to the address corresponding to the lowest address in the next word
682 * of bits in the bitmap.
683 */
Carl Shapiro38d710b2010-08-31 16:48:31 -0700684static void scanBitmapCallback(void *addr, void *finger, void *arg)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800685{
Carl Shapiro006346e2010-07-29 20:39:50 -0700686 GcMarkContext *ctx = arg;
Carl Shapiro57ee2702010-08-27 13:06:48 -0700687 ctx->finger = (void *)finger;
688 scanObject(addr, ctx);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800689}
690
691/* Given bitmaps with the root set marked, find and mark all
692 * reachable objects. When this returns, the entire set of
693 * live objects will be marked and the mark stack will be empty.
694 */
Carl Shapiro29540742010-03-26 15:34:39 -0700695void dvmHeapScanMarkedObjects(void)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800696{
697 GcMarkContext *ctx = &gDvm.gcHeap->markContext;
698
699 assert(ctx->finger == NULL);
700
701 /* The bitmaps currently have bits set for the root set.
702 * Walk across the bitmaps and scan each object.
703 */
Carl Shapiro38d710b2010-08-31 16:48:31 -0700704 dvmHeapBitmapScanWalk(ctx->bitmap, scanBitmapCallback, ctx);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800705
Carl Shapiro034fba52010-11-11 16:39:58 -0800706 ctx->finger = (void *)ULONG_MAX;
707
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800708 /* We've walked the mark bitmaps. Scan anything that's
709 * left on the mark stack.
710 */
711 processMarkStack(ctx);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800712}
713
Carl Shapiro106c5fd2010-07-28 14:12:27 -0700714void dvmHeapReScanMarkedObjects(void)
Carl Shapiroec805ea2010-06-28 16:28:26 -0700715{
Carl Shapiro106c5fd2010-07-28 14:12:27 -0700716 GcMarkContext *ctx = &gDvm.gcHeap->markContext;
Carl Shapiroec805ea2010-06-28 16:28:26 -0700717
Carl Shapiroec805ea2010-06-28 16:28:26 -0700718 /*
Carl Shapirof5860332010-06-28 23:02:08 -0700719 * The finger must have been set to the maximum value to ensure
720 * that gray objects will be pushed onto the mark stack.
Carl Shapiroec805ea2010-06-28 16:28:26 -0700721 */
722 assert(ctx->finger == (void *)ULONG_MAX);
Carl Shapiro106c5fd2010-07-28 14:12:27 -0700723 scanGrayObjects(ctx);
Carl Shapiroec805ea2010-06-28 16:28:26 -0700724 processMarkStack(ctx);
725}
726
Carl Shapiro34f51992010-07-09 17:55:41 -0700727/*
728 * Clear the referent field.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800729 */
Barry Hayes6930a112009-12-22 11:01:38 -0800730static void clearReference(Object *reference)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800731{
Carl Shapiro34f51992010-07-09 17:55:41 -0700732 size_t offset = gDvm.offJavaLangRefReference_referent;
733 dvmSetFieldObject(reference, offset, NULL);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800734}
735
Carl Shapiro29540742010-03-26 15:34:39 -0700736/*
737 * Returns true if the reference was registered with a reference queue
738 * and has not yet been enqueued.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800739 */
Carl Shapiro29540742010-03-26 15:34:39 -0700740static bool isEnqueuable(const Object *reference)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800741{
Barry Hayes6930a112009-12-22 11:01:38 -0800742 Object *queue = dvmGetFieldObject(reference,
743 gDvm.offJavaLangRefReference_queue);
744 Object *queueNext = dvmGetFieldObject(reference,
745 gDvm.offJavaLangRefReference_queueNext);
Carl Shapiro2a6f4842010-07-09 16:50:54 -0700746 return queue != NULL && queueNext == NULL;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800747}
748
Carl Shapiro29540742010-03-26 15:34:39 -0700749/*
750 * Schedules a reference to be appended to its reference queue.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800751 */
Carl Shapiro29540742010-03-26 15:34:39 -0700752static void enqueueReference(Object *ref)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800753{
Carl Shapiro646ba092010-06-10 15:17:00 -0700754 assert(ref != NULL);
Carl Shapiro29540742010-03-26 15:34:39 -0700755 assert(dvmGetFieldObject(ref, gDvm.offJavaLangRefReference_queue) != NULL);
756 assert(dvmGetFieldObject(ref, gDvm.offJavaLangRefReference_queueNext) == NULL);
Carl Shapiro646ba092010-06-10 15:17:00 -0700757 if (!dvmHeapAddRefToLargeTable(&gDvm.gcHeap->referenceOperations, ref)) {
Carl Shapiro29540742010-03-26 15:34:39 -0700758 LOGE_HEAP("enqueueReference(): no room for any more "
759 "reference operations\n");
760 dvmAbort();
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800761 }
762}
763
Carl Shapiro29540742010-03-26 15:34:39 -0700764/*
765 * Walks the reference list marking any references subject to the
766 * reference clearing policy. References with a black referent are
767 * removed from the list. References with white referents biased
768 * toward saving are blackened and also removed from the list.
769 */
Carl Shapiroe8ef2b52010-12-01 19:32:05 -0800770static void preserveSomeSoftReferences(Object **list)
Carl Shapiro29540742010-03-26 15:34:39 -0700771{
Carl Shapirob2e78d32010-08-20 11:34:18 -0700772 GcMarkContext *ctx;
Carl Shapiro29540742010-03-26 15:34:39 -0700773 Object *ref, *referent;
Carl Shapiro2a6f4842010-07-09 16:50:54 -0700774 Object *clear;
Carl Shapiroa1b03a92010-07-12 14:02:28 -0700775 size_t referentOffset;
Carl Shapiro2a6f4842010-07-09 16:50:54 -0700776 size_t counter;
Carl Shapiro29540742010-03-26 15:34:39 -0700777 bool marked;
778
Carl Shapirob2e78d32010-08-20 11:34:18 -0700779 ctx = &gDvm.gcHeap->markContext;
Carl Shapiro29540742010-03-26 15:34:39 -0700780 referentOffset = gDvm.offJavaLangRefReference_referent;
Carl Shapiro2a6f4842010-07-09 16:50:54 -0700781 clear = NULL;
Carl Shapiro29540742010-03-26 15:34:39 -0700782 counter = 0;
Carl Shapiro2a6f4842010-07-09 16:50:54 -0700783 while (*list != NULL) {
784 ref = dequeuePendingReference(list);
Carl Shapiro29540742010-03-26 15:34:39 -0700785 referent = dvmGetFieldObject(ref, referentOffset);
Carl Shapiro29540742010-03-26 15:34:39 -0700786 assert(referent != NULL);
Carl Shapirob2e78d32010-08-20 11:34:18 -0700787 marked = isMarked(referent, ctx);
Carl Shapiro29540742010-03-26 15:34:39 -0700788 if (!marked && ((++counter) & 1)) {
789 /* Referent is white and biased toward saving, mark it. */
Carl Shapirob2e78d32010-08-20 11:34:18 -0700790 markObject(referent, ctx);
Carl Shapiro29540742010-03-26 15:34:39 -0700791 marked = true;
792 }
Carl Shapiro2a6f4842010-07-09 16:50:54 -0700793 if (!marked) {
794 /* Referent is white, queue it for clearing. */
795 enqueuePendingReference(ref, &clear);
Carl Shapiro29540742010-03-26 15:34:39 -0700796 }
Carl Shapiro29540742010-03-26 15:34:39 -0700797 }
Carl Shapiro2a6f4842010-07-09 16:50:54 -0700798 *list = clear;
Carl Shapiro29540742010-03-26 15:34:39 -0700799 /*
800 * Restart the mark with the newly black references added to the
801 * root set.
802 */
Carl Shapirob2e78d32010-08-20 11:34:18 -0700803 processMarkStack(ctx);
Carl Shapiro29540742010-03-26 15:34:39 -0700804}
805
806/*
Carl Shapiroa1b03a92010-07-12 14:02:28 -0700807 * Unlink the reference list clearing references objects with white
808 * referents. Cleared references registered to a reference queue are
809 * scheduled for appending by the heap worker thread.
Carl Shapiro29540742010-03-26 15:34:39 -0700810 */
Carl Shapiroe8ef2b52010-12-01 19:32:05 -0800811static void clearWhiteReferences(Object **list)
Carl Shapiro29540742010-03-26 15:34:39 -0700812{
Carl Shapirob2e78d32010-08-20 11:34:18 -0700813 GcMarkContext *ctx;
Carl Shapiro29540742010-03-26 15:34:39 -0700814 Object *ref, *referent;
Carl Shapiroa1b03a92010-07-12 14:02:28 -0700815 size_t referentOffset;
Carl Shapiro29540742010-03-26 15:34:39 -0700816 bool doSignal;
817
Carl Shapirob2e78d32010-08-20 11:34:18 -0700818 ctx = &gDvm.gcHeap->markContext;
Carl Shapiro29540742010-03-26 15:34:39 -0700819 referentOffset = gDvm.offJavaLangRefReference_referent;
820 doSignal = false;
821 while (*list != NULL) {
Carl Shapiro2a6f4842010-07-09 16:50:54 -0700822 ref = dequeuePendingReference(list);
Carl Shapiro29540742010-03-26 15:34:39 -0700823 referent = dvmGetFieldObject(ref, referentOffset);
Carl Shapiro29540742010-03-26 15:34:39 -0700824 assert(referent != NULL);
Carl Shapirob2e78d32010-08-20 11:34:18 -0700825 if (!isMarked(referent, ctx)) {
Carl Shapiroa1b03a92010-07-12 14:02:28 -0700826 /* Referent is white, clear it. */
Carl Shapiro29540742010-03-26 15:34:39 -0700827 clearReference(ref);
828 if (isEnqueuable(ref)) {
829 enqueueReference(ref);
830 doSignal = true;
831 }
832 }
833 }
834 /*
835 * If we cleared a reference with a reference queue we must notify
836 * the heap worker to append the reference.
837 */
838 if (doSignal) {
839 dvmSignalHeapWorker(false);
840 }
841 assert(*list == NULL);
842}
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800843
844/* Find unreachable objects that need to be finalized,
845 * and schedule them for finalization.
846 */
Carl Shapiroe8ef2b52010-12-01 19:32:05 -0800847static void scheduleFinalizations(void)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800848{
849 HeapRefTable newPendingRefs;
850 LargeHeapRefTable *finRefs = gDvm.gcHeap->finalizableRefs;
851 Object **ref;
852 Object **lastRef;
853 size_t totalPendCount;
Carl Shapirob2e78d32010-08-20 11:34:18 -0700854 GcMarkContext *ctx = &gDvm.gcHeap->markContext;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800855
856 /*
857 * All reachable objects have been marked.
858 * Any unmarked finalizable objects need to be finalized.
859 */
860
861 /* Create a table that the new pending refs will
862 * be added to.
863 */
Barry Hayesd4f78d32010-06-08 09:34:42 -0700864 if (!dvmHeapInitHeapRefTable(&newPendingRefs)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800865 //TODO: mark all finalizable refs and hope that
866 // we can schedule them next time. Watch out,
867 // because we may be expecting to free up space
868 // by calling finalizers.
Carl Shapiroe8ef2b52010-12-01 19:32:05 -0800869 LOGE_GC("scheduleFinalizations(): no room for "
870 "pending finalizations");
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800871 dvmAbort();
872 }
873
874 /* Walk through finalizableRefs and move any unmarked references
875 * to the list of new pending refs.
876 */
877 totalPendCount = 0;
878 while (finRefs != NULL) {
879 Object **gapRef;
880 size_t newPendCount = 0;
881
882 gapRef = ref = finRefs->refs.table;
883 lastRef = finRefs->refs.nextEntry;
884 while (ref < lastRef) {
Carl Shapirob2e78d32010-08-20 11:34:18 -0700885 if (!isMarked(*ref, ctx)) {
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800886 if (!dvmHeapAddToHeapRefTable(&newPendingRefs, *ref)) {
887 //TODO: add the current table and allocate
888 // a new, smaller one.
Carl Shapiroe8ef2b52010-12-01 19:32:05 -0800889 LOGE_GC("scheduleFinalizations(): "
890 "no room for any more pending finalizations: %zd",
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800891 dvmHeapNumHeapRefTableEntries(&newPendingRefs));
892 dvmAbort();
893 }
894 newPendCount++;
895 } else {
896 /* This ref is marked, so will remain on finalizableRefs.
897 */
898 if (newPendCount > 0) {
899 /* Copy it up to fill the holes.
900 */
901 *gapRef++ = *ref;
902 } else {
903 /* No holes yet; don't bother copying.
904 */
905 gapRef++;
906 }
907 }
908 ref++;
909 }
910 finRefs->refs.nextEntry = gapRef;
911 //TODO: if the table is empty when we're done, free it.
912 totalPendCount += newPendCount;
913 finRefs = finRefs->next;
914 }
Carl Shapiroe8ef2b52010-12-01 19:32:05 -0800915 LOGD_GC("scheduleFinalizations(): %zd finalizers triggered.",
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800916 totalPendCount);
917 if (totalPendCount == 0) {
918 /* No objects required finalization.
919 * Free the empty temporary table.
920 */
921 dvmClearReferenceTable(&newPendingRefs);
922 return;
923 }
924
925 /* Add the new pending refs to the main list.
926 */
927 if (!dvmHeapAddTableToLargeTable(&gDvm.gcHeap->pendingFinalizationRefs,
928 &newPendingRefs))
929 {
Carl Shapiroe8ef2b52010-12-01 19:32:05 -0800930 LOGE_GC("scheduleFinalizations(): can't insert new "
931 "pending finalizations");
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800932 dvmAbort();
933 }
934
935 //TODO: try compacting the main list with a memcpy loop
936
937 /* Mark the refs we just moved; we don't want them or their
938 * children to get swept yet.
939 */
940 ref = newPendingRefs.table;
941 lastRef = newPendingRefs.nextEntry;
942 assert(ref < lastRef);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800943 while (ref < lastRef) {
Barry Hayese1bccb92010-05-18 09:48:37 -0700944 assert(*ref != NULL);
Carl Shapirob2e78d32010-08-20 11:34:18 -0700945 markObject(*ref, ctx);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800946 ref++;
947 }
Carl Shapirob2e78d32010-08-20 11:34:18 -0700948 processMarkStack(ctx);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800949 dvmSignalHeapWorker(false);
950}
951
Carl Shapiroe8ef2b52010-12-01 19:32:05 -0800952/*
953 * Process reference class instances and schedule finalizations.
954 */
955void dvmHeapProcessReferences(Object **softReferences, bool clearSoftRefs,
956 Object **weakReferences,
957 Object **phantomReferences)
958{
959 assert(softReferences != NULL);
960 assert(weakReferences != NULL);
961 assert(phantomReferences != NULL);
962 /*
963 * Unless we are required to clear soft references with white
964 * references, preserve some white referents.
965 */
966 if (!clearSoftRefs) {
967 preserveSomeSoftReferences(softReferences);
968 }
969 /*
970 * Clear all remaining soft and weak references with white
971 * referents.
972 */
973 clearWhiteReferences(softReferences);
974 clearWhiteReferences(weakReferences);
975 /*
976 * Preserve all white objects with finalize methods and schedule
977 * them for finalization.
978 */
979 scheduleFinalizations();
980 /*
981 * Clear all f-reachable soft and weak references with white
982 * referents.
983 */
984 clearWhiteReferences(softReferences);
985 clearWhiteReferences(weakReferences);
986 /*
987 * Clear all phantom references with white referents.
988 */
989 clearWhiteReferences(phantomReferences);
990 /*
991 * At this point all reference lists should be empty.
992 */
993 assert(*softReferences == NULL);
994 assert(*weakReferences == NULL);
995 assert(*phantomReferences == NULL);
996}
997
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800998void dvmHeapFinishMarkStep()
999{
Carl Shapirob2e78d32010-08-20 11:34:18 -07001000 GcMarkContext *ctx;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001001
Carl Shapirob2e78d32010-08-20 11:34:18 -07001002 ctx = &gDvm.gcHeap->markContext;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001003
Barry Hayes81010a42010-07-19 14:07:01 -07001004 /* The mark bits are now not needed.
1005 */
1006 dvmHeapSourceZeroMarkBitmap();
1007
Carl Shapirof373efd2010-02-19 00:46:33 -08001008 /* Clean up everything else associated with the marking process.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001009 */
Carl Shapirob2e78d32010-08-20 11:34:18 -07001010 destroyMarkStack(&ctx->stack);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001011
Carl Shapirob2e78d32010-08-20 11:34:18 -07001012 ctx->finger = NULL;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001013}
1014
Carl Shapiro8881a802010-08-10 15:55:45 -07001015typedef struct {
1016 size_t numObjects;
1017 size_t numBytes;
1018 bool isConcurrent;
1019} SweepContext;
1020
Carl Shapiro57ee2702010-08-27 13:06:48 -07001021static void sweepBitmapCallback(size_t numPtrs, void **ptrs, void *arg)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001022{
Carl Shapirob9b23952010-08-10 17:20:15 -07001023 SweepContext *ctx = arg;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001024
Carl Shapiro8881a802010-08-10 15:55:45 -07001025 if (ctx->isConcurrent) {
1026 dvmLockHeap();
1027 }
1028 ctx->numBytes += dvmHeapSourceFreeList(numPtrs, ptrs);
1029 ctx->numObjects += numPtrs;
1030 if (ctx->isConcurrent) {
1031 dvmUnlockHeap();
1032 }
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001033}
1034
Carl Shapiro8881a802010-08-10 15:55:45 -07001035/*
1036 * Returns true if the given object is unmarked. This assumes that
1037 * the bitmaps have not yet been swapped.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001038 */
1039static int isUnmarkedObject(void *object)
1040{
Carl Shapiro6343bd02010-02-16 17:40:19 -08001041 return !isMarked((void *)((uintptr_t)object & ~(HB_OBJECT_ALIGNMENT-1)),
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001042 &gDvm.gcHeap->markContext);
1043}
1044
Carl Shapiro8881a802010-08-10 15:55:45 -07001045/*
1046 * Process all the internal system structures that behave like
1047 * weakly-held objects.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001048 */
Carl Shapiro8881a802010-08-10 15:55:45 -07001049void dvmHeapSweepSystemWeaks(void)
1050{
1051 dvmGcDetachDeadInternedStrings(isUnmarkedObject);
1052 dvmSweepMonitorList(&gDvm.monitorList, isUnmarkedObject);
1053}
1054
1055/*
1056 * Walk through the list of objects that haven't been marked and free
1057 * them. Assumes the bitmaps have been swapped.
1058 */
1059void dvmHeapSweepUnmarkedObjects(GcMode mode, bool isConcurrent,
1060 size_t *numObjects, size_t *numBytes)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001061{
Carl Shapiro5fdab4a2010-08-18 21:04:31 -07001062 HeapBitmap currMark[HEAP_SOURCE_MAX_HEAP_COUNT];
1063 HeapBitmap currLive[HEAP_SOURCE_MAX_HEAP_COUNT];
Carl Shapiro8881a802010-08-10 15:55:45 -07001064 SweepContext ctx;
Carl Shapirod25566d2010-03-11 20:39:47 -08001065 size_t numBitmaps, numSweepBitmaps;
Barry Hayese168ebd2010-05-07 09:19:46 -07001066 size_t i;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001067
Carl Shapirof373efd2010-02-19 00:46:33 -08001068 numBitmaps = dvmHeapSourceGetNumHeaps();
Carl Shapiro5fdab4a2010-08-18 21:04:31 -07001069 dvmHeapSourceGetObjectBitmaps(currLive, currMark, numBitmaps);
Carl Shapirod25566d2010-03-11 20:39:47 -08001070 if (mode == GC_PARTIAL) {
1071 numSweepBitmaps = 1;
Carl Shapiro5fdab4a2010-08-18 21:04:31 -07001072 assert((uintptr_t)gDvm.gcHeap->markContext.immuneLimit == currLive[0].base);
Carl Shapirod25566d2010-03-11 20:39:47 -08001073 } else {
1074 numSweepBitmaps = numBitmaps;
1075 }
Carl Shapiro8881a802010-08-10 15:55:45 -07001076 ctx.numObjects = ctx.numBytes = 0;
1077 ctx.isConcurrent = isConcurrent;
Barry Hayese168ebd2010-05-07 09:19:46 -07001078 for (i = 0; i < numSweepBitmaps; i++) {
Carl Shapiro5fdab4a2010-08-18 21:04:31 -07001079 HeapBitmap* prevLive = &currMark[i];
1080 HeapBitmap* prevMark = &currLive[i];
1081 dvmHeapBitmapSweepWalk(prevLive, prevMark, sweepBitmapCallback, &ctx);
Barry Hayese168ebd2010-05-07 09:19:46 -07001082 }
Carl Shapiro8881a802010-08-10 15:55:45 -07001083 *numObjects = ctx.numObjects;
1084 *numBytes = ctx.numBytes;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001085 if (gDvm.allocProf.enabled) {
Carl Shapiro8881a802010-08-10 15:55:45 -07001086 gDvm.allocProf.freeCount += ctx.numObjects;
1087 gDvm.allocProf.freeSize += ctx.numBytes;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001088 }
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001089}