| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "Dalvik.h" |
| Barry Hayes | eac47ed | 2009-06-22 11:45:20 -0700 | [diff] [blame] | 18 | #include "alloc/clz.h" |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 19 | #include "alloc/HeapBitmap.h" |
| 20 | #include "alloc/HeapInternal.h" |
| 21 | #include "alloc/HeapSource.h" |
| 22 | #include "alloc/MarkSweep.h" |
| Carl Shapiro | ec805ea | 2010-06-28 16:28:26 -0700 | [diff] [blame] | 23 | #include "alloc/Visit.h" |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 24 | #include <limits.h> // for ULONG_MAX |
| 25 | #include <sys/mman.h> // for madvise(), mmap() |
| The Android Open Source Project | 9940988 | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 26 | #include <errno.h> |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 27 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 28 | #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 Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 38 | #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 Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 43 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 44 | #define ALIGN_UP_TO_PAGE_SIZE(p) \ |
| Andy McFadden | 9651693 | 2009-10-28 17:39:02 -0700 | [diff] [blame] | 45 | (((size_t)(p) + (SYSTEM_PAGE_SIZE - 1)) & ~(SYSTEM_PAGE_SIZE - 1)) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 46 | |
| 47 | /* Do not cast the result of this to a boolean; the only set bit |
| 48 | * may be > 1<<8. |
| 49 | */ |
| Carl Shapiro | 6343bd0 | 2010-02-16 17:40:19 -0800 | [diff] [blame] | 50 | static inline long isMarked(const void *obj, const GcMarkContext *ctx) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 51 | { |
| Carl Shapiro | f373efd | 2010-02-19 00:46:33 -0800 | [diff] [blame] | 52 | return dvmHeapBitmapIsObjectBitSet(ctx->bitmap, obj); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | static bool |
| 56 | createMarkStack(GcMarkStack *stack) |
| 57 | { |
| 58 | const Object **limit; |
| Carl Shapiro | 742c445 | 2010-07-13 18:28:13 -0700 | [diff] [blame] | 59 | const char *name; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 60 | size_t size; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 61 | |
| 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 Shapiro | 742c445 | 2010-07-13 18:28:13 -0700 | [diff] [blame] | 70 | 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 Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 74 | return false; |
| 75 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 76 | stack->limit = limit; |
| 77 | stack->base = (const Object **)((uintptr_t)limit + size); |
| 78 | stack->top = stack->base; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 79 | return true; |
| 80 | } |
| 81 | |
| 82 | static void |
| 83 | destroyMarkStack(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 | |
| 95 | bool |
| Carl Shapiro | d25566d | 2010-03-11 20:39:47 -0800 | [diff] [blame] | 96 | dvmHeapBeginMarkStep(GcMode mode) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 97 | { |
| 98 | GcMarkContext *mc = &gDvm.gcHeap->markContext; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 99 | |
| 100 | if (!createMarkStack(&mc->stack)) { |
| 101 | return false; |
| 102 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 103 | mc->finger = NULL; |
| Carl Shapiro | d25566d | 2010-03-11 20:39:47 -0800 | [diff] [blame] | 104 | mc->immuneLimit = dvmHeapSourceGetImmuneLimit(mode); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 105 | return true; |
| 106 | } |
| 107 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 108 | static long |
| Carl Shapiro | 6343bd0 | 2010-02-16 17:40:19 -0800 | [diff] [blame] | 109 | setAndReturnMarkBit(GcMarkContext *ctx, const void *obj) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 110 | { |
| Carl Shapiro | f373efd | 2010-02-19 00:46:33 -0800 | [diff] [blame] | 111 | return dvmHeapBitmapSetAndReturnObjectBit(ctx->bitmap, obj); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 114 | static void |
| Carl Shapiro | 106c5fd | 2010-07-28 14:12:27 -0700 | [diff] [blame] | 115 | markObjectNonNull(const Object *obj, GcMarkContext *ctx, bool checkFinger) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 116 | { |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 117 | assert(ctx != NULL); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 118 | assert(obj != NULL); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 119 | assert(dvmIsValidObject(obj)); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 120 | |
| Carl Shapiro | b31b301 | 2010-05-25 18:35:37 -0700 | [diff] [blame] | 121 | if (obj < (Object *)ctx->immuneLimit) { |
| Carl Shapiro | d25566d | 2010-03-11 20:39:47 -0800 | [diff] [blame] | 122 | assert(isMarked(obj, ctx)); |
| 123 | return; |
| 124 | } |
| Carl Shapiro | 6343bd0 | 2010-02-16 17:40:19 -0800 | [diff] [blame] | 125 | if (!setAndReturnMarkBit(ctx, obj)) { |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 126 | /* This object was not previously marked. |
| 127 | */ |
| Carl Shapiro | 106c5fd | 2010-07-28 14:12:27 -0700 | [diff] [blame] | 128 | if (checkFinger && (void *)obj < ctx->finger) { |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 129 | /* This object will need to go on the mark stack. |
| 130 | */ |
| 131 | MARK_STACK_PUSH(ctx->stack, obj); |
| 132 | } |
| 133 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 134 | #if WITH_HPROF |
| 135 | if (gDvm.gcHeap->hprofContext != NULL) { |
| 136 | hprofMarkRootObject(gDvm.gcHeap->hprofContext, obj, 0); |
| 137 | } |
| 138 | #endif |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 139 | } |
| 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 Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 148 | static void markObject(const Object *obj, GcMarkContext *ctx) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 149 | { |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 150 | if (obj != NULL) { |
| Carl Shapiro | 106c5fd | 2010-07-28 14:12:27 -0700 | [diff] [blame] | 151 | markObjectNonNull(obj, ctx, true); |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 152 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 153 | } |
| 154 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 155 | /* If the object hasn't already been marked, mark it and |
| 156 | * schedule it to be scanned for references. |
| 157 | * |
| 158 | * obj may not be NULL. The macro dvmMarkObject() should |
| 159 | * be used in situations where a reference may be NULL. |
| 160 | * |
| 161 | * This function may only be called when marking the root |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 162 | * set. When recursing, use the internal markObject(). |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 163 | */ |
| 164 | void |
| 165 | dvmMarkObjectNonNull(const Object *obj) |
| 166 | { |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 167 | assert(obj != NULL); |
| Carl Shapiro | 106c5fd | 2010-07-28 14:12:27 -0700 | [diff] [blame] | 168 | markObjectNonNull(obj, &gDvm.gcHeap->markContext, false); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | /* Mark the set of root objects. |
| 172 | * |
| 173 | * Things we need to scan: |
| 174 | * - System classes defined by root classloader |
| 175 | * - For each thread: |
| 176 | * - Interpreted stack, from top to "curFrame" |
| 177 | * - Dalvik registers (args + local vars) |
| 178 | * - JNI local references |
| 179 | * - Automatic VM local references (TrackedAlloc) |
| 180 | * - Associated Thread/VMThread object |
| 181 | * - ThreadGroups (could track & start with these instead of working |
| 182 | * upward from Threads) |
| 183 | * - Exception currently being thrown, if present |
| 184 | * - JNI global references |
| 185 | * - Interned string table |
| 186 | * - Primitive classes |
| 187 | * - Special objects |
| 188 | * - gDvm.outOfMemoryObj |
| 189 | * - Objects allocated with ALLOC_NO_GC |
| 190 | * - Objects pending finalization (but not yet finalized) |
| 191 | * - Objects in debugger object registry |
| 192 | * |
| 193 | * Don't need: |
| 194 | * - Native stack (for in-progress stuff in the VM) |
| 195 | * - The TrackedAlloc stuff watches all native VM references. |
| 196 | */ |
| 197 | void dvmHeapMarkRootSet() |
| 198 | { |
| Barry Hayes | d4f78d3 | 2010-06-08 09:34:42 -0700 | [diff] [blame] | 199 | GcHeap *gcHeap = gDvm.gcHeap; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 200 | |
| 201 | HPROF_SET_GC_SCAN_STATE(HPROF_ROOT_STICKY_CLASS, 0); |
| 202 | |
| Carl Shapiro | d25566d | 2010-03-11 20:39:47 -0800 | [diff] [blame] | 203 | LOG_SCAN("immune objects"); |
| Barry Hayes | 425848f | 2010-05-04 13:32:12 -0700 | [diff] [blame] | 204 | dvmMarkImmuneObjects(gcHeap->markContext.immuneLimit); |
| Carl Shapiro | d25566d | 2010-03-11 20:39:47 -0800 | [diff] [blame] | 205 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 206 | LOG_SCAN("root class loader\n"); |
| 207 | dvmGcScanRootClassLoader(); |
| 208 | LOG_SCAN("primitive classes\n"); |
| 209 | dvmGcScanPrimitiveClasses(); |
| 210 | |
| 211 | /* dvmGcScanRootThreadGroups() sets a bunch of |
| 212 | * different scan states internally. |
| 213 | */ |
| 214 | HPROF_CLEAR_GC_SCAN_STATE(); |
| 215 | |
| 216 | LOG_SCAN("root thread groups\n"); |
| 217 | dvmGcScanRootThreadGroups(); |
| 218 | |
| 219 | HPROF_SET_GC_SCAN_STATE(HPROF_ROOT_INTERNED_STRING, 0); |
| 220 | |
| 221 | LOG_SCAN("interned strings\n"); |
| 222 | dvmGcScanInternedStrings(); |
| 223 | |
| 224 | HPROF_SET_GC_SCAN_STATE(HPROF_ROOT_JNI_GLOBAL, 0); |
| 225 | |
| 226 | LOG_SCAN("JNI global refs\n"); |
| 227 | dvmGcMarkJniGlobalRefs(); |
| 228 | |
| 229 | HPROF_SET_GC_SCAN_STATE(HPROF_ROOT_REFERENCE_CLEANUP, 0); |
| 230 | |
| 231 | LOG_SCAN("pending reference operations\n"); |
| Carl Shapiro | 646ba09 | 2010-06-10 15:17:00 -0700 | [diff] [blame] | 232 | dvmHeapMarkLargeTableRefs(gcHeap->referenceOperations); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 233 | |
| 234 | HPROF_SET_GC_SCAN_STATE(HPROF_ROOT_FINALIZING, 0); |
| 235 | |
| 236 | LOG_SCAN("pending finalizations\n"); |
| Carl Shapiro | 646ba09 | 2010-06-10 15:17:00 -0700 | [diff] [blame] | 237 | dvmHeapMarkLargeTableRefs(gcHeap->pendingFinalizationRefs); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 238 | |
| 239 | HPROF_SET_GC_SCAN_STATE(HPROF_ROOT_DEBUGGER, 0); |
| 240 | |
| 241 | LOG_SCAN("debugger refs\n"); |
| 242 | dvmGcMarkDebuggerRefs(); |
| 243 | |
| 244 | HPROF_SET_GC_SCAN_STATE(HPROF_ROOT_VM_INTERNAL, 0); |
| 245 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 246 | /* Mark any special objects we have sitting around. |
| 247 | */ |
| 248 | LOG_SCAN("special objects\n"); |
| 249 | dvmMarkObjectNonNull(gDvm.outOfMemoryObj); |
| 250 | dvmMarkObjectNonNull(gDvm.internalErrorObj); |
| Andy McFadden | 7fc3ce8 | 2009-07-14 15:57:23 -0700 | [diff] [blame] | 251 | dvmMarkObjectNonNull(gDvm.noClassDefFoundErrorObj); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 252 | //TODO: scan object references sitting in gDvm; use pointer begin & end |
| 253 | |
| 254 | HPROF_CLEAR_GC_SCAN_STATE(); |
| 255 | } |
| 256 | |
| 257 | /* |
| Carl Shapiro | 106c5fd | 2010-07-28 14:12:27 -0700 | [diff] [blame] | 258 | * Callback applied to root references. If the root location contains |
| 259 | * a white reference it is pushed on the mark stack and grayed. |
| 260 | */ |
| 261 | static void markObjectVisitor(void *addr, void *arg) |
| 262 | { |
| 263 | Object *obj; |
| 264 | |
| 265 | assert(addr != NULL); |
| 266 | assert(arg != NULL); |
| 267 | obj = *(Object **)addr; |
| 268 | if (obj != NULL) { |
| 269 | markObjectNonNull(obj, arg, true); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | /* |
| 274 | * Grays all references in the roots. |
| 275 | */ |
| 276 | void dvmHeapReMarkRootSet(void) |
| 277 | { |
| 278 | GcMarkContext *ctx = &gDvm.gcHeap->markContext; |
| 279 | assert(ctx->finger == (void *)ULONG_MAX); |
| 280 | dvmVisitRoots(markObjectVisitor, ctx); |
| 281 | } |
| 282 | |
| 283 | /* |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 284 | * Nothing past this point is allowed to use dvmMarkObject() or |
| 285 | * dvmMarkObjectNonNull(), which are for root-marking only. |
| 286 | * Scanning/recursion must use markObject(), which takes the finger |
| 287 | * into account. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 288 | */ |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 289 | #undef dvmMarkObject |
| 290 | #define dvmMarkObject __dont_use_dvmMarkObject__ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 291 | #define dvmMarkObjectNonNull __dont_use_dvmMarkObjectNonNull__ |
| 292 | |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 293 | /* |
| 294 | * Scans instance fields. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 295 | */ |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 296 | static void scanInstanceFields(const Object *obj, GcMarkContext *ctx) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 297 | { |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 298 | assert(obj != NULL); |
| 299 | assert(obj->clazz != NULL); |
| 300 | assert(ctx != NULL); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 301 | |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 302 | if (obj->clazz->refOffsets != CLASS_WALK_SUPER) { |
| 303 | unsigned int refOffsets = obj->clazz->refOffsets; |
| Barry Hayes | eac47ed | 2009-06-22 11:45:20 -0700 | [diff] [blame] | 304 | while (refOffsets != 0) { |
| 305 | const int rshift = CLZ(refOffsets); |
| 306 | refOffsets &= ~(CLASS_HIGH_BIT >> rshift); |
| 307 | markObject(dvmGetFieldObject((Object*)obj, |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 308 | CLASS_OFFSET_FROM_CLZ(rshift)), ctx); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 309 | } |
| Barry Hayes | eac47ed | 2009-06-22 11:45:20 -0700 | [diff] [blame] | 310 | } else { |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 311 | ClassObject *clazz; |
| 312 | int i; |
| 313 | for (clazz = obj->clazz; clazz != NULL; clazz = clazz->super) { |
| 314 | InstField *field = clazz->ifields; |
| 315 | for (i = 0; i < clazz->ifieldRefCount; ++i, ++field) { |
| 316 | void *addr = BYTE_OFFSET((Object *)obj, field->byteOffset); |
| 317 | markObject(((JValue *)addr)->l, ctx); |
| Barry Hayes | eac47ed | 2009-06-22 11:45:20 -0700 | [diff] [blame] | 318 | } |
| Barry Hayes | eac47ed | 2009-06-22 11:45:20 -0700 | [diff] [blame] | 319 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 323 | /* |
| 324 | * Scans the header, static field references, and interface |
| 325 | * pointers of a class object. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 326 | */ |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 327 | static void scanClassObject(const ClassObject *obj, GcMarkContext *ctx) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 328 | { |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 329 | int i; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 330 | |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 331 | assert(obj != NULL); |
| 332 | assert(obj->obj.clazz == gDvm.classJavaLangClass); |
| 333 | assert(ctx != NULL); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 334 | |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 335 | markObject((Object *)obj->obj.clazz, ctx); |
| 336 | if (IS_CLASS_FLAG_SET(obj, CLASS_ISARRAY)) { |
| 337 | markObject((Object *)obj->elementClass, ctx); |
| 338 | } |
| Barry Hayes | c49db85 | 2010-05-14 13:43:34 -0700 | [diff] [blame] | 339 | /* Do super and the interfaces contain Objects and not dex idx values? */ |
| 340 | if (obj->status > CLASS_IDX) { |
| 341 | markObject((Object *)obj->super, ctx); |
| 342 | } |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 343 | markObject(obj->classLoader, ctx); |
| 344 | /* Scan static field references. */ |
| 345 | for (i = 0; i < obj->sfieldCount; ++i) { |
| 346 | char ch = obj->sfields[i].field.signature[0]; |
| 347 | if (ch == '[' || ch == 'L') { |
| 348 | markObject(obj->sfields[i].value.l, ctx); |
| 349 | } |
| 350 | } |
| 351 | /* Scan the instance fields. */ |
| 352 | scanInstanceFields((const Object *)obj, ctx); |
| 353 | /* Scan interface references. */ |
| Barry Hayes | c49db85 | 2010-05-14 13:43:34 -0700 | [diff] [blame] | 354 | if (obj->status > CLASS_IDX) { |
| 355 | for (i = 0; i < obj->interfaceCount; ++i) { |
| 356 | markObject((Object *)obj->interfaces[i], ctx); |
| 357 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 358 | } |
| 359 | } |
| 360 | |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 361 | /* |
| 362 | * Scans the header of all array objects. If the array object is |
| 363 | * specialized to a reference type, scans the array data as well. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 364 | */ |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 365 | static void scanArrayObject(const ArrayObject *obj, GcMarkContext *ctx) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 366 | { |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 367 | size_t i; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 368 | |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 369 | assert(obj != NULL); |
| 370 | assert(obj->obj.clazz != NULL); |
| 371 | assert(ctx != NULL); |
| 372 | /* Scan the class object reference. */ |
| 373 | markObject((Object *)obj->obj.clazz, ctx); |
| 374 | if (IS_CLASS_FLAG_SET(obj->obj.clazz, CLASS_ISOBJECTARRAY)) { |
| 375 | /* Scan the array contents. */ |
| 376 | Object **contents = (Object **)obj->contents; |
| 377 | for (i = 0; i < obj->length; ++i) { |
| 378 | markObject(contents[i], ctx); |
| 379 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 380 | } |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 381 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 382 | |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 383 | /* |
| Carl Shapiro | 2a6f484 | 2010-07-09 16:50:54 -0700 | [diff] [blame] | 384 | * Returns class flags relating to Reference subclasses. |
| 385 | */ |
| 386 | static int referenceClassFlags(const Object *obj) |
| 387 | { |
| 388 | int flags = CLASS_ISREFERENCE | |
| 389 | CLASS_ISWEAKREFERENCE | |
| 390 | CLASS_ISPHANTOMREFERENCE; |
| 391 | return GET_CLASS_FLAG_GROUP(obj->clazz, flags); |
| 392 | } |
| 393 | |
| 394 | /* |
| 395 | * Returns true if the object derives from SoftReference. |
| 396 | */ |
| 397 | static bool isSoftReference(const Object *obj) |
| 398 | { |
| 399 | return referenceClassFlags(obj) == CLASS_ISREFERENCE; |
| 400 | } |
| 401 | |
| 402 | /* |
| 403 | * Returns true if the object derives from WeakReference. |
| 404 | */ |
| 405 | static bool isWeakReference(const Object *obj) |
| 406 | { |
| 407 | return referenceClassFlags(obj) & CLASS_ISWEAKREFERENCE; |
| 408 | } |
| 409 | |
| 410 | /* |
| 411 | * Returns true if the object derives from PhantomReference. |
| 412 | */ |
| 413 | static bool isPhantomReference(const Object *obj) |
| 414 | { |
| 415 | return referenceClassFlags(obj) & CLASS_ISPHANTOMREFERENCE; |
| 416 | } |
| 417 | |
| 418 | /* |
| 419 | * Adds a reference to the tail of a circular queue of references. |
| 420 | */ |
| 421 | static void enqueuePendingReference(Object *ref, Object **list) |
| 422 | { |
| 423 | size_t offset; |
| 424 | |
| 425 | assert(ref != NULL); |
| 426 | assert(list != NULL); |
| 427 | offset = gDvm.offJavaLangRefReference_pendingNext; |
| 428 | if (*list == NULL) { |
| 429 | dvmSetFieldObject(ref, offset, ref); |
| 430 | *list = ref; |
| 431 | } else { |
| 432 | Object *head = dvmGetFieldObject(*list, offset); |
| 433 | dvmSetFieldObject(ref, offset, head); |
| 434 | dvmSetFieldObject(*list, offset, ref); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | /* |
| Carl Shapiro | a1b03a9 | 2010-07-12 14:02:28 -0700 | [diff] [blame] | 439 | * Removes the reference at the head of a circular queue of |
| 440 | * references. |
| Carl Shapiro | 2a6f484 | 2010-07-09 16:50:54 -0700 | [diff] [blame] | 441 | */ |
| 442 | static Object *dequeuePendingReference(Object **list) |
| 443 | { |
| 444 | Object *ref, *head; |
| 445 | size_t offset; |
| 446 | |
| 447 | assert(list != NULL); |
| 448 | assert(*list != NULL); |
| 449 | offset = gDvm.offJavaLangRefReference_pendingNext; |
| 450 | head = dvmGetFieldObject(*list, offset); |
| 451 | if (*list == head) { |
| 452 | ref = *list; |
| 453 | *list = NULL; |
| 454 | } else { |
| 455 | Object *next = dvmGetFieldObject(head, offset); |
| 456 | dvmSetFieldObject(*list, offset, next); |
| 457 | ref = head; |
| 458 | } |
| 459 | dvmSetFieldObject(ref, offset, NULL); |
| 460 | return ref; |
| 461 | } |
| 462 | |
| 463 | /* |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 464 | * Process the "referent" field in a java.lang.ref.Reference. If the |
| 465 | * referent has not yet been marked, put it on the appropriate list in |
| 466 | * the gcHeap for later processing. |
| 467 | */ |
| Barry Hayes | 697b5a9 | 2010-06-23 11:38:52 -0700 | [diff] [blame] | 468 | static void delayReferenceReferent(Object *obj, GcMarkContext *ctx) |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 469 | { |
| Carl Shapiro | 2a6f484 | 2010-07-09 16:50:54 -0700 | [diff] [blame] | 470 | GcHeap *gcHeap = gDvm.gcHeap; |
| 471 | Object *pending, *referent; |
| 472 | size_t pendingNextOffset, referentOffset; |
| 473 | |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 474 | assert(obj != NULL); |
| Barry Hayes | 697b5a9 | 2010-06-23 11:38:52 -0700 | [diff] [blame] | 475 | assert(obj->clazz != NULL); |
| Carl Shapiro | 2a6f484 | 2010-07-09 16:50:54 -0700 | [diff] [blame] | 476 | assert(IS_CLASS_FLAG_SET(obj->clazz, CLASS_ISREFERENCE)); |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 477 | assert(ctx != NULL); |
| Carl Shapiro | 2a6f484 | 2010-07-09 16:50:54 -0700 | [diff] [blame] | 478 | pendingNextOffset = gDvm.offJavaLangRefReference_pendingNext; |
| 479 | referentOffset = gDvm.offJavaLangRefReference_referent; |
| 480 | pending = dvmGetFieldObject(obj, pendingNextOffset); |
| 481 | referent = dvmGetFieldObject(obj, referentOffset); |
| 482 | if (pending == NULL && referent != NULL && !isMarked(referent, ctx)) { |
| 483 | Object **list = NULL; |
| 484 | if (isSoftReference(obj)) { |
| 485 | list = &gcHeap->softReferences; |
| 486 | } else if (isWeakReference(obj)) { |
| 487 | list = &gcHeap->weakReferences; |
| 488 | } else if (isPhantomReference(obj)) { |
| 489 | list = &gcHeap->phantomReferences; |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 490 | } |
| Carl Shapiro | 2a6f484 | 2010-07-09 16:50:54 -0700 | [diff] [blame] | 491 | assert(list != NULL); |
| 492 | enqueuePendingReference(obj, list); |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 493 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 494 | } |
| 495 | |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 496 | /* |
| 497 | * Scans the header and field references of a data object. |
| 498 | */ |
| Barry Hayes | 697b5a9 | 2010-06-23 11:38:52 -0700 | [diff] [blame] | 499 | static void scanDataObject(DataObject *obj, GcMarkContext *ctx) |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 500 | { |
| 501 | assert(obj != NULL); |
| 502 | assert(obj->obj.clazz != NULL); |
| 503 | assert(ctx != NULL); |
| 504 | /* Scan the class object. */ |
| 505 | markObject((Object *)obj->obj.clazz, ctx); |
| 506 | /* Scan the instance fields. */ |
| 507 | scanInstanceFields((const Object *)obj, ctx); |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 508 | if (IS_CLASS_FLAG_SET(obj->obj.clazz, CLASS_ISREFERENCE)) { |
| Barry Hayes | 697b5a9 | 2010-06-23 11:38:52 -0700 | [diff] [blame] | 509 | delayReferenceReferent((Object *)obj, ctx); |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 510 | } |
| 511 | } |
| 512 | |
| 513 | /* |
| 514 | * Scans an object reference. Determines the type of the reference |
| 515 | * and dispatches to a specialized scanning routine. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 516 | */ |
| 517 | static void scanObject(const Object *obj, GcMarkContext *ctx) |
| 518 | { |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 519 | assert(obj != NULL); |
| 520 | assert(ctx != NULL); |
| Barry Hayes | 899cdb7 | 2010-06-08 09:59:12 -0700 | [diff] [blame] | 521 | assert(obj->clazz != NULL); |
| Carl Shapiro | 1a8e21a | 2010-06-08 13:19:57 -0700 | [diff] [blame] | 522 | #if WITH_HPROF |
| 523 | if (gDvm.gcHeap->hprofContext != NULL) { |
| 524 | hprofDumpHeapObject(gDvm.gcHeap->hprofContext, obj); |
| 525 | } |
| 526 | #endif |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 527 | /* Dispatch a type-specific scan routine. */ |
| Carl Shapiro | 1a8e21a | 2010-06-08 13:19:57 -0700 | [diff] [blame] | 528 | if (obj->clazz == gDvm.classJavaLangClass) { |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 529 | scanClassObject((ClassObject *)obj, ctx); |
| Carl Shapiro | 1a8e21a | 2010-06-08 13:19:57 -0700 | [diff] [blame] | 530 | } else if (IS_CLASS_FLAG_SET(obj->clazz, CLASS_ISARRAY)) { |
| Barry Hayes | 899cdb7 | 2010-06-08 09:59:12 -0700 | [diff] [blame] | 531 | scanArrayObject((ArrayObject *)obj, ctx); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 532 | } else { |
| Barry Hayes | 899cdb7 | 2010-06-08 09:59:12 -0700 | [diff] [blame] | 533 | scanDataObject((DataObject *)obj, ctx); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 534 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | static void |
| 538 | processMarkStack(GcMarkContext *ctx) |
| 539 | { |
| 540 | const Object **const base = ctx->stack.base; |
| 541 | |
| 542 | /* Scan anything that's on the mark stack. |
| 543 | * We can't use the bitmaps anymore, so use |
| 544 | * a finger that points past the end of them. |
| 545 | */ |
| 546 | ctx->finger = (void *)ULONG_MAX; |
| 547 | while (ctx->stack.top != base) { |
| 548 | scanObject(*ctx->stack.top++, ctx); |
| 549 | } |
| 550 | } |
| 551 | |
| Carl Shapiro | 106c5fd | 2010-07-28 14:12:27 -0700 | [diff] [blame] | 552 | static size_t objectSize(const Object *obj) |
| 553 | { |
| 554 | assert(dvmIsValidObject(obj)); |
| 555 | assert(dvmIsValidObject((Object *)obj->clazz)); |
| 556 | if (IS_CLASS_FLAG_SET(obj->clazz, CLASS_ISARRAY)) { |
| 557 | return dvmArrayObjectSize((ArrayObject *)obj); |
| 558 | } else if (obj->clazz == gDvm.classJavaLangClass) { |
| 559 | return dvmClassObjectSize((ClassObject *)obj); |
| 560 | } else { |
| 561 | return obj->clazz->objectSize; |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | /* |
| 566 | * Scans backward to the header of a marked object that spans the |
| 567 | * given address. Returns NULL if there is no spanning marked object. |
| 568 | */ |
| 569 | static Object *previousGrayObject(u1 *start, GcMarkContext *ctx) |
| 570 | { |
| 571 | u1 *end = (u1 *)ctx->bitmap->base; |
| 572 | u1 *ptr; |
| 573 | |
| 574 | assert(start >= end); |
| 575 | for (ptr = start; ptr >= end; ptr -= HB_OBJECT_ALIGNMENT) { |
| 576 | if (dvmIsValidObject((Object *)ptr)) |
| 577 | break; |
| 578 | } |
| 579 | if (ptr < end || !isMarked(ptr, ctx)) { |
| 580 | return NULL; |
| 581 | } else { |
| 582 | Object *obj = (Object *)ptr; |
| 583 | size_t size = objectSize(obj); |
| 584 | if (ptr + size < start) { |
| 585 | return NULL; |
| 586 | } |
| 587 | return obj; |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | /* |
| 592 | * Scans forward to the header of the next marked object between start |
| 593 | * and limit. Returns NULL if no marked objects are in that region. |
| 594 | */ |
| 595 | static Object *nextGrayObject(u1 *base, u1 *limit, GcMarkContext *ctx) |
| 596 | { |
| 597 | u1 *ptr; |
| 598 | |
| 599 | assert(base < limit); |
| 600 | assert(limit - base <= GC_CARD_SIZE); |
| 601 | for (ptr = base; ptr < limit; ptr += HB_OBJECT_ALIGNMENT) { |
| 602 | if (isMarked(ptr, ctx)) |
| 603 | return (Object *)ptr; |
| 604 | } |
| 605 | return NULL; |
| 606 | } |
| 607 | |
| 608 | /* |
| 609 | * Scan the card table looking for objects that have been grayed by |
| 610 | * the mutator. |
| 611 | */ |
| 612 | static void scanGrayObjects(GcMarkContext *ctx) |
| 613 | { |
| 614 | GcHeap *h = gDvm.gcHeap; |
| 615 | u1 *card, *baseCard, *limitCard; |
| 616 | |
| 617 | baseCard = &h->cardTableBase[0]; |
| 618 | limitCard = &h->cardTableBase[h->cardTableLength]; |
| 619 | for (card = baseCard; card != limitCard; ++card) { |
| 620 | if (*card == GC_CARD_DIRTY) { |
| 621 | /* |
| 622 | * The card is dirty. Scan all of the objects that |
| 623 | * intersect with the card address. |
| 624 | */ |
| 625 | u1 *addr = dvmAddrFromCard(card); |
| 626 | /* |
| 627 | * If the last object on the previous card terminates on |
| 628 | * the current card it is gray and must be scanned. |
| 629 | */ |
| 630 | if (!dvmIsValidObject((Object *)addr)) { |
| 631 | Object *prev = previousGrayObject(addr, ctx); |
| 632 | if (prev != NULL) { |
| 633 | scanObject(prev, ctx); |
| 634 | } |
| 635 | } |
| 636 | /* |
| 637 | * Scan through all black objects that start on the |
| 638 | * current card. |
| 639 | */ |
| 640 | u1 *limit = addr + GC_CARD_SIZE; |
| 641 | u1 *next = addr; |
| 642 | while (next < limit) { |
| 643 | Object *obj = nextGrayObject(next, limit, ctx); |
| 644 | if (obj == NULL) |
| 645 | break; |
| 646 | scanObject(obj, ctx); |
| 647 | next = (u1*)obj + HB_OBJECT_ALIGNMENT; |
| 648 | } |
| 649 | } |
| 650 | } |
| 651 | } |
| 652 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 653 | #ifndef NDEBUG |
| 654 | static uintptr_t gLastFinger = 0; |
| 655 | #endif |
| 656 | |
| 657 | static bool |
| 658 | scanBitmapCallback(size_t numPtrs, void **ptrs, const void *finger, void *arg) |
| 659 | { |
| 660 | GcMarkContext *ctx = (GcMarkContext *)arg; |
| 661 | size_t i; |
| 662 | |
| 663 | #ifndef NDEBUG |
| 664 | assert((uintptr_t)finger >= gLastFinger); |
| 665 | gLastFinger = (uintptr_t)finger; |
| 666 | #endif |
| 667 | |
| 668 | ctx->finger = finger; |
| 669 | for (i = 0; i < numPtrs; i++) { |
| Carl Shapiro | 6343bd0 | 2010-02-16 17:40:19 -0800 | [diff] [blame] | 670 | scanObject(*ptrs++, ctx); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | return true; |
| 674 | } |
| 675 | |
| 676 | /* Given bitmaps with the root set marked, find and mark all |
| 677 | * reachable objects. When this returns, the entire set of |
| 678 | * live objects will be marked and the mark stack will be empty. |
| 679 | */ |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 680 | void dvmHeapScanMarkedObjects(void) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 681 | { |
| 682 | GcMarkContext *ctx = &gDvm.gcHeap->markContext; |
| 683 | |
| 684 | assert(ctx->finger == NULL); |
| 685 | |
| 686 | /* The bitmaps currently have bits set for the root set. |
| 687 | * Walk across the bitmaps and scan each object. |
| 688 | */ |
| 689 | #ifndef NDEBUG |
| 690 | gLastFinger = 0; |
| 691 | #endif |
| Carl Shapiro | cedcb70 | 2010-07-22 20:22:49 -0700 | [diff] [blame] | 692 | dvmHeapBitmapWalk(ctx->bitmap, scanBitmapCallback, ctx); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 693 | |
| 694 | /* We've walked the mark bitmaps. Scan anything that's |
| 695 | * left on the mark stack. |
| 696 | */ |
| 697 | processMarkStack(ctx); |
| 698 | |
| 699 | LOG_SCAN("done with marked objects\n"); |
| 700 | } |
| 701 | |
| Carl Shapiro | 106c5fd | 2010-07-28 14:12:27 -0700 | [diff] [blame] | 702 | void dvmHeapReScanMarkedObjects(void) |
| Carl Shapiro | ec805ea | 2010-06-28 16:28:26 -0700 | [diff] [blame] | 703 | { |
| Carl Shapiro | 106c5fd | 2010-07-28 14:12:27 -0700 | [diff] [blame] | 704 | GcMarkContext *ctx = &gDvm.gcHeap->markContext; |
| Carl Shapiro | ec805ea | 2010-06-28 16:28:26 -0700 | [diff] [blame] | 705 | |
| Carl Shapiro | ec805ea | 2010-06-28 16:28:26 -0700 | [diff] [blame] | 706 | /* |
| Carl Shapiro | f586033 | 2010-06-28 23:02:08 -0700 | [diff] [blame] | 707 | * The finger must have been set to the maximum value to ensure |
| 708 | * that gray objects will be pushed onto the mark stack. |
| Carl Shapiro | ec805ea | 2010-06-28 16:28:26 -0700 | [diff] [blame] | 709 | */ |
| 710 | assert(ctx->finger == (void *)ULONG_MAX); |
| Carl Shapiro | 106c5fd | 2010-07-28 14:12:27 -0700 | [diff] [blame] | 711 | scanGrayObjects(ctx); |
| Carl Shapiro | ec805ea | 2010-06-28 16:28:26 -0700 | [diff] [blame] | 712 | processMarkStack(ctx); |
| 713 | } |
| 714 | |
| Carl Shapiro | 34f5199 | 2010-07-09 17:55:41 -0700 | [diff] [blame] | 715 | /* |
| 716 | * Clear the referent field. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 717 | */ |
| Barry Hayes | 6930a11 | 2009-12-22 11:01:38 -0800 | [diff] [blame] | 718 | static void clearReference(Object *reference) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 719 | { |
| Carl Shapiro | 34f5199 | 2010-07-09 17:55:41 -0700 | [diff] [blame] | 720 | size_t offset = gDvm.offJavaLangRefReference_referent; |
| 721 | dvmSetFieldObject(reference, offset, NULL); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 722 | } |
| 723 | |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 724 | /* |
| 725 | * Returns true if the reference was registered with a reference queue |
| 726 | * and has not yet been enqueued. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 727 | */ |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 728 | static bool isEnqueuable(const Object *reference) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 729 | { |
| Barry Hayes | 6930a11 | 2009-12-22 11:01:38 -0800 | [diff] [blame] | 730 | Object *queue = dvmGetFieldObject(reference, |
| 731 | gDvm.offJavaLangRefReference_queue); |
| 732 | Object *queueNext = dvmGetFieldObject(reference, |
| 733 | gDvm.offJavaLangRefReference_queueNext); |
| Carl Shapiro | 2a6f484 | 2010-07-09 16:50:54 -0700 | [diff] [blame] | 734 | return queue != NULL && queueNext == NULL; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 735 | } |
| 736 | |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 737 | /* |
| 738 | * Schedules a reference to be appended to its reference queue. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 739 | */ |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 740 | static void enqueueReference(Object *ref) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 741 | { |
| Carl Shapiro | 646ba09 | 2010-06-10 15:17:00 -0700 | [diff] [blame] | 742 | assert(ref != NULL); |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 743 | assert(dvmGetFieldObject(ref, gDvm.offJavaLangRefReference_queue) != NULL); |
| 744 | assert(dvmGetFieldObject(ref, gDvm.offJavaLangRefReference_queueNext) == NULL); |
| Carl Shapiro | 646ba09 | 2010-06-10 15:17:00 -0700 | [diff] [blame] | 745 | if (!dvmHeapAddRefToLargeTable(&gDvm.gcHeap->referenceOperations, ref)) { |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 746 | LOGE_HEAP("enqueueReference(): no room for any more " |
| 747 | "reference operations\n"); |
| 748 | dvmAbort(); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 749 | } |
| 750 | } |
| 751 | |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 752 | /* |
| 753 | * Walks the reference list marking any references subject to the |
| 754 | * reference clearing policy. References with a black referent are |
| 755 | * removed from the list. References with white referents biased |
| 756 | * toward saving are blackened and also removed from the list. |
| 757 | */ |
| 758 | void dvmHandleSoftRefs(Object **list) |
| 759 | { |
| 760 | GcMarkContext *markContext; |
| 761 | Object *ref, *referent; |
| Carl Shapiro | 2a6f484 | 2010-07-09 16:50:54 -0700 | [diff] [blame] | 762 | Object *clear; |
| Carl Shapiro | a1b03a9 | 2010-07-12 14:02:28 -0700 | [diff] [blame] | 763 | size_t referentOffset; |
| Carl Shapiro | 2a6f484 | 2010-07-09 16:50:54 -0700 | [diff] [blame] | 764 | size_t counter; |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 765 | bool marked; |
| 766 | |
| 767 | markContext = &gDvm.gcHeap->markContext; |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 768 | referentOffset = gDvm.offJavaLangRefReference_referent; |
| Carl Shapiro | 2a6f484 | 2010-07-09 16:50:54 -0700 | [diff] [blame] | 769 | clear = NULL; |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 770 | counter = 0; |
| Carl Shapiro | 2a6f484 | 2010-07-09 16:50:54 -0700 | [diff] [blame] | 771 | while (*list != NULL) { |
| 772 | ref = dequeuePendingReference(list); |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 773 | referent = dvmGetFieldObject(ref, referentOffset); |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 774 | assert(referent != NULL); |
| 775 | marked = isMarked(referent, markContext); |
| 776 | if (!marked && ((++counter) & 1)) { |
| 777 | /* Referent is white and biased toward saving, mark it. */ |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 778 | markObject(referent, markContext); |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 779 | marked = true; |
| 780 | } |
| Carl Shapiro | 2a6f484 | 2010-07-09 16:50:54 -0700 | [diff] [blame] | 781 | if (!marked) { |
| 782 | /* Referent is white, queue it for clearing. */ |
| 783 | enqueuePendingReference(ref, &clear); |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 784 | } |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 785 | } |
| Carl Shapiro | 2a6f484 | 2010-07-09 16:50:54 -0700 | [diff] [blame] | 786 | *list = clear; |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 787 | /* |
| 788 | * Restart the mark with the newly black references added to the |
| 789 | * root set. |
| 790 | */ |
| 791 | processMarkStack(markContext); |
| 792 | } |
| 793 | |
| 794 | /* |
| Carl Shapiro | a1b03a9 | 2010-07-12 14:02:28 -0700 | [diff] [blame] | 795 | * Unlink the reference list clearing references objects with white |
| 796 | * referents. Cleared references registered to a reference queue are |
| 797 | * scheduled for appending by the heap worker thread. |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 798 | */ |
| 799 | void dvmClearWhiteRefs(Object **list) |
| 800 | { |
| 801 | GcMarkContext *markContext; |
| 802 | Object *ref, *referent; |
| Carl Shapiro | a1b03a9 | 2010-07-12 14:02:28 -0700 | [diff] [blame] | 803 | size_t referentOffset; |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 804 | bool doSignal; |
| 805 | |
| 806 | markContext = &gDvm.gcHeap->markContext; |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 807 | referentOffset = gDvm.offJavaLangRefReference_referent; |
| 808 | doSignal = false; |
| 809 | while (*list != NULL) { |
| Carl Shapiro | 2a6f484 | 2010-07-09 16:50:54 -0700 | [diff] [blame] | 810 | ref = dequeuePendingReference(list); |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 811 | referent = dvmGetFieldObject(ref, referentOffset); |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 812 | assert(referent != NULL); |
| 813 | if (!isMarked(referent, markContext)) { |
| Carl Shapiro | a1b03a9 | 2010-07-12 14:02:28 -0700 | [diff] [blame] | 814 | /* Referent is white, clear it. */ |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 815 | clearReference(ref); |
| 816 | if (isEnqueuable(ref)) { |
| 817 | enqueueReference(ref); |
| 818 | doSignal = true; |
| 819 | } |
| 820 | } |
| 821 | } |
| 822 | /* |
| 823 | * If we cleared a reference with a reference queue we must notify |
| 824 | * the heap worker to append the reference. |
| 825 | */ |
| 826 | if (doSignal) { |
| 827 | dvmSignalHeapWorker(false); |
| 828 | } |
| 829 | assert(*list == NULL); |
| 830 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 831 | |
| 832 | /* Find unreachable objects that need to be finalized, |
| 833 | * and schedule them for finalization. |
| 834 | */ |
| 835 | void dvmHeapScheduleFinalizations() |
| 836 | { |
| 837 | HeapRefTable newPendingRefs; |
| 838 | LargeHeapRefTable *finRefs = gDvm.gcHeap->finalizableRefs; |
| 839 | Object **ref; |
| 840 | Object **lastRef; |
| 841 | size_t totalPendCount; |
| 842 | GcMarkContext *markContext = &gDvm.gcHeap->markContext; |
| 843 | |
| 844 | /* |
| 845 | * All reachable objects have been marked. |
| 846 | * Any unmarked finalizable objects need to be finalized. |
| 847 | */ |
| 848 | |
| 849 | /* Create a table that the new pending refs will |
| 850 | * be added to. |
| 851 | */ |
| Barry Hayes | d4f78d3 | 2010-06-08 09:34:42 -0700 | [diff] [blame] | 852 | if (!dvmHeapInitHeapRefTable(&newPendingRefs)) { |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 853 | //TODO: mark all finalizable refs and hope that |
| 854 | // we can schedule them next time. Watch out, |
| 855 | // because we may be expecting to free up space |
| 856 | // by calling finalizers. |
| 857 | LOGE_GC("dvmHeapScheduleFinalizations(): no room for " |
| 858 | "pending finalizations\n"); |
| 859 | dvmAbort(); |
| 860 | } |
| 861 | |
| 862 | /* Walk through finalizableRefs and move any unmarked references |
| 863 | * to the list of new pending refs. |
| 864 | */ |
| 865 | totalPendCount = 0; |
| 866 | while (finRefs != NULL) { |
| 867 | Object **gapRef; |
| 868 | size_t newPendCount = 0; |
| 869 | |
| 870 | gapRef = ref = finRefs->refs.table; |
| 871 | lastRef = finRefs->refs.nextEntry; |
| 872 | while (ref < lastRef) { |
| Carl Shapiro | 6343bd0 | 2010-02-16 17:40:19 -0800 | [diff] [blame] | 873 | if (!isMarked(*ref, markContext)) { |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 874 | if (!dvmHeapAddToHeapRefTable(&newPendingRefs, *ref)) { |
| 875 | //TODO: add the current table and allocate |
| 876 | // a new, smaller one. |
| 877 | LOGE_GC("dvmHeapScheduleFinalizations(): " |
| 878 | "no room for any more pending finalizations: %zd\n", |
| 879 | dvmHeapNumHeapRefTableEntries(&newPendingRefs)); |
| 880 | dvmAbort(); |
| 881 | } |
| 882 | newPendCount++; |
| 883 | } else { |
| 884 | /* This ref is marked, so will remain on finalizableRefs. |
| 885 | */ |
| 886 | if (newPendCount > 0) { |
| 887 | /* Copy it up to fill the holes. |
| 888 | */ |
| 889 | *gapRef++ = *ref; |
| 890 | } else { |
| 891 | /* No holes yet; don't bother copying. |
| 892 | */ |
| 893 | gapRef++; |
| 894 | } |
| 895 | } |
| 896 | ref++; |
| 897 | } |
| 898 | finRefs->refs.nextEntry = gapRef; |
| 899 | //TODO: if the table is empty when we're done, free it. |
| 900 | totalPendCount += newPendCount; |
| 901 | finRefs = finRefs->next; |
| 902 | } |
| 903 | LOGD_GC("dvmHeapScheduleFinalizations(): %zd finalizers triggered.\n", |
| 904 | totalPendCount); |
| 905 | if (totalPendCount == 0) { |
| 906 | /* No objects required finalization. |
| 907 | * Free the empty temporary table. |
| 908 | */ |
| 909 | dvmClearReferenceTable(&newPendingRefs); |
| 910 | return; |
| 911 | } |
| 912 | |
| 913 | /* Add the new pending refs to the main list. |
| 914 | */ |
| 915 | if (!dvmHeapAddTableToLargeTable(&gDvm.gcHeap->pendingFinalizationRefs, |
| 916 | &newPendingRefs)) |
| 917 | { |
| 918 | LOGE_GC("dvmHeapScheduleFinalizations(): can't insert new " |
| 919 | "pending finalizations\n"); |
| 920 | dvmAbort(); |
| 921 | } |
| 922 | |
| 923 | //TODO: try compacting the main list with a memcpy loop |
| 924 | |
| 925 | /* Mark the refs we just moved; we don't want them or their |
| 926 | * children to get swept yet. |
| 927 | */ |
| 928 | ref = newPendingRefs.table; |
| 929 | lastRef = newPendingRefs.nextEntry; |
| 930 | assert(ref < lastRef); |
| 931 | HPROF_SET_GC_SCAN_STATE(HPROF_ROOT_FINALIZING, 0); |
| 932 | while (ref < lastRef) { |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 933 | assert(*ref != NULL); |
| 934 | markObject(*ref, markContext); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 935 | ref++; |
| 936 | } |
| 937 | HPROF_CLEAR_GC_SCAN_STATE(); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 938 | processMarkStack(markContext); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 939 | dvmSignalHeapWorker(false); |
| 940 | } |
| 941 | |
| 942 | void dvmHeapFinishMarkStep() |
| 943 | { |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 944 | GcMarkContext *markContext; |
| 945 | |
| 946 | markContext = &gDvm.gcHeap->markContext; |
| 947 | |
| 948 | /* The sweep step freed every object that appeared in the |
| 949 | * HeapSource bitmaps that didn't appear in the mark bitmaps. |
| 950 | * The new state of the HeapSource is exactly the final |
| 951 | * mark bitmaps, so swap them in. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 952 | */ |
| Carl Shapiro | f373efd | 2010-02-19 00:46:33 -0800 | [diff] [blame] | 953 | dvmHeapSourceSwapBitmaps(); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 954 | |
| Carl Shapiro | f373efd | 2010-02-19 00:46:33 -0800 | [diff] [blame] | 955 | /* Clean up everything else associated with the marking process. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 956 | */ |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 957 | destroyMarkStack(&markContext->stack); |
| 958 | |
| Carl Shapiro | f373efd | 2010-02-19 00:46:33 -0800 | [diff] [blame] | 959 | markContext->finger = NULL; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 960 | } |
| 961 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 962 | static bool |
| 963 | sweepBitmapCallback(size_t numPtrs, void **ptrs, const void *finger, void *arg) |
| 964 | { |
| 965 | const ClassObject *const classJavaLangClass = gDvm.classJavaLangClass; |
| Barry Hayes | 5cbb230 | 2010-02-02 14:07:37 -0800 | [diff] [blame] | 966 | const bool overwriteFree = gDvm.overwriteFree; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 967 | size_t i; |
| 968 | |
| 969 | for (i = 0; i < numPtrs; i++) { |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 970 | Object *obj; |
| 971 | |
| Barry Hayes | 04174be | 2010-07-21 11:51:37 -0700 | [diff] [blame] | 972 | obj = (Object *)ptrs[i]; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 973 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 974 | /* This assumes that java.lang.Class will never go away. |
| 975 | * If it can, and we were the last reference to it, it |
| 976 | * could have already been swept. However, even in that case, |
| 977 | * gDvm.classJavaLangClass should still have a useful |
| 978 | * value. |
| 979 | */ |
| 980 | if (obj->clazz == classJavaLangClass) { |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 981 | /* dvmFreeClassInnards() may have already been called, |
| 982 | * but it's safe to call on the same ClassObject twice. |
| 983 | */ |
| 984 | dvmFreeClassInnards((ClassObject *)obj); |
| 985 | } |
| 986 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 987 | /* Overwrite the to-be-freed object to make stale references |
| 988 | * more obvious. |
| 989 | */ |
| Barry Hayes | 5cbb230 | 2010-02-02 14:07:37 -0800 | [diff] [blame] | 990 | if (overwriteFree) { |
| Barry Hayes | 2e3c3e1 | 2010-02-22 09:39:10 -0800 | [diff] [blame] | 991 | int objlen; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 992 | ClassObject *clazz = obj->clazz; |
| Barry Hayes | 2e3c3e1 | 2010-02-22 09:39:10 -0800 | [diff] [blame] | 993 | objlen = dvmHeapSourceChunkSize(obj); |
| 994 | memset(obj, 0xa5, objlen); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 995 | obj->clazz = (ClassObject *)((uintptr_t)clazz ^ 0xffffffff); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 996 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 997 | } |
| Barry Hayes | dde8ab0 | 2009-05-20 12:10:36 -0700 | [diff] [blame] | 998 | // TODO: dvmHeapSourceFreeList has a loop, just like the above |
| 999 | // does. Consider collapsing the two loops to save overhead. |
| Barry Hayes | 04174be | 2010-07-21 11:51:37 -0700 | [diff] [blame] | 1000 | dvmHeapSourceFreeList(numPtrs, ptrs); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1001 | |
| 1002 | return true; |
| 1003 | } |
| 1004 | |
| Carl Shapiro | 5a6071b | 2010-01-07 21:35:50 -0800 | [diff] [blame] | 1005 | /* Returns true if the given object is unmarked. Ignores the low bits |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1006 | * of the pointer because the intern table may set them. |
| 1007 | */ |
| 1008 | static int isUnmarkedObject(void *object) |
| 1009 | { |
| Carl Shapiro | 6343bd0 | 2010-02-16 17:40:19 -0800 | [diff] [blame] | 1010 | return !isMarked((void *)((uintptr_t)object & ~(HB_OBJECT_ALIGNMENT-1)), |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1011 | &gDvm.gcHeap->markContext); |
| 1012 | } |
| 1013 | |
| 1014 | /* Walk through the list of objects that haven't been |
| 1015 | * marked and free them. |
| 1016 | */ |
| 1017 | void |
| Carl Shapiro | d25566d | 2010-03-11 20:39:47 -0800 | [diff] [blame] | 1018 | dvmHeapSweepUnmarkedObjects(GcMode mode, int *numFreed, size_t *sizeFreed) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1019 | { |
| Carl Shapiro | f373efd | 2010-02-19 00:46:33 -0800 | [diff] [blame] | 1020 | HeapBitmap markBits[HEAP_SOURCE_MAX_HEAP_COUNT]; |
| Carl Shapiro | d77f7fd | 2010-04-05 19:23:31 -0700 | [diff] [blame] | 1021 | HeapBitmap liveBits[HEAP_SOURCE_MAX_HEAP_COUNT]; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1022 | size_t origObjectsAllocated; |
| 1023 | size_t origBytesAllocated; |
| Carl Shapiro | d25566d | 2010-03-11 20:39:47 -0800 | [diff] [blame] | 1024 | size_t numBitmaps, numSweepBitmaps; |
| Barry Hayes | e168ebd | 2010-05-07 09:19:46 -0700 | [diff] [blame] | 1025 | size_t i; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1026 | |
| 1027 | /* All reachable objects have been marked. |
| 1028 | * Detach any unreachable interned strings before |
| 1029 | * we sweep. |
| 1030 | */ |
| 1031 | dvmGcDetachDeadInternedStrings(isUnmarkedObject); |
| 1032 | |
| 1033 | /* Free any known objects that are not marked. |
| 1034 | */ |
| 1035 | origObjectsAllocated = dvmHeapSourceGetValue(HS_OBJECTS_ALLOCATED, NULL, 0); |
| 1036 | origBytesAllocated = dvmHeapSourceGetValue(HS_BYTES_ALLOCATED, NULL, 0); |
| 1037 | |
| Carl Shapiro | 5a6071b | 2010-01-07 21:35:50 -0800 | [diff] [blame] | 1038 | dvmSweepMonitorList(&gDvm.monitorList, isUnmarkedObject); |
| 1039 | |
| Carl Shapiro | f373efd | 2010-02-19 00:46:33 -0800 | [diff] [blame] | 1040 | numBitmaps = dvmHeapSourceGetNumHeaps(); |
| Carl Shapiro | d77f7fd | 2010-04-05 19:23:31 -0700 | [diff] [blame] | 1041 | dvmHeapSourceGetObjectBitmaps(liveBits, markBits, numBitmaps); |
| Carl Shapiro | d25566d | 2010-03-11 20:39:47 -0800 | [diff] [blame] | 1042 | if (mode == GC_PARTIAL) { |
| 1043 | numSweepBitmaps = 1; |
| Carl Shapiro | d77f7fd | 2010-04-05 19:23:31 -0700 | [diff] [blame] | 1044 | assert((uintptr_t)gDvm.gcHeap->markContext.immuneLimit == liveBits[0].base); |
| Carl Shapiro | d25566d | 2010-03-11 20:39:47 -0800 | [diff] [blame] | 1045 | } else { |
| 1046 | numSweepBitmaps = numBitmaps; |
| 1047 | } |
| Barry Hayes | e168ebd | 2010-05-07 09:19:46 -0700 | [diff] [blame] | 1048 | for (i = 0; i < numSweepBitmaps; i++) { |
| 1049 | dvmHeapBitmapXorWalk(&markBits[i], &liveBits[i], |
| 1050 | sweepBitmapCallback, NULL); |
| 1051 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1052 | |
| 1053 | *numFreed = origObjectsAllocated - |
| 1054 | dvmHeapSourceGetValue(HS_OBJECTS_ALLOCATED, NULL, 0); |
| 1055 | *sizeFreed = origBytesAllocated - |
| 1056 | dvmHeapSourceGetValue(HS_BYTES_ALLOCATED, NULL, 0); |
| 1057 | |
| 1058 | #ifdef WITH_PROFILER |
| 1059 | if (gDvm.allocProf.enabled) { |
| 1060 | gDvm.allocProf.freeCount += *numFreed; |
| 1061 | gDvm.allocProf.freeSize += *sizeFreed; |
| 1062 | } |
| 1063 | #endif |
| 1064 | } |