| 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" |
| Carl Shapiro | 7ec9144 | 2010-10-21 15:35:19 -0700 | [diff] [blame] | 19 | #include "alloc/CardTable.h" |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 20 | #include "alloc/HeapBitmap.h" |
| 21 | #include "alloc/HeapInternal.h" |
| 22 | #include "alloc/HeapSource.h" |
| 23 | #include "alloc/MarkSweep.h" |
| Carl Shapiro | ec805ea | 2010-06-28 16:28:26 -0700 | [diff] [blame] | 24 | #include "alloc/Visit.h" |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 25 | #include <limits.h> // for ULONG_MAX |
| 26 | #include <sys/mman.h> // for madvise(), mmap() |
| The Android Open Source Project | 9940988 | 2009-03-18 22:20:24 -0700 | [diff] [blame] | 27 | #include <errno.h> |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 28 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 29 | #define GC_LOG_TAG LOG_TAG "-gc" |
| 30 | |
| 31 | #if LOG_NDEBUG |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 32 | #define LOGD_GC(...) ((void)0) |
| 33 | #else |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 34 | #define LOGD_GC(...) LOG(LOG_DEBUG, GC_LOG_TAG, __VA_ARGS__) |
| 35 | #endif |
| 36 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 37 | #define LOGE_GC(...) LOG(LOG_ERROR, GC_LOG_TAG, __VA_ARGS__) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 38 | |
| Carl Shapiro | 7ec9144 | 2010-10-21 15:35:19 -0700 | [diff] [blame] | 39 | #define ALIGN_DOWN(x, n) ((size_t)(x) & -(n)) |
| Carl Shapiro | 57ee270 | 2010-08-27 13:06:48 -0700 | [diff] [blame] | 40 | #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 Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 42 | |
| Carl Shapiro | 7ec9144 | 2010-10-21 15:35:19 -0700 | [diff] [blame] | 43 | typedef unsigned long Word; |
| 44 | const size_t kWordSize = sizeof(Word); |
| 45 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 46 | /* Do not cast the result of this to a boolean; the only set bit |
| 47 | * may be > 1<<8. |
| 48 | */ |
| Carl Shapiro | 6343bd0 | 2010-02-16 17:40:19 -0800 | [diff] [blame] | 49 | 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] | 50 | { |
| Carl Shapiro | f373efd | 2010-02-19 00:46:33 -0800 | [diff] [blame] | 51 | return dvmHeapBitmapIsObjectBitSet(ctx->bitmap, obj); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| Carl Shapiro | fdf8052 | 2010-11-09 14:27:02 -0800 | [diff] [blame] | 54 | /* |
| 55 | * Initializes the stack top and advises the mark stack pages as needed. |
| 56 | */ |
| Carl Shapiro | d7400e0 | 2010-09-02 18:24:29 -0700 | [diff] [blame] | 57 | static bool createMarkStack(GcMarkStack *stack) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 58 | { |
| Carl Shapiro | fdf8052 | 2010-11-09 14:27:02 -0800 | [diff] [blame] | 59 | 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 Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 63 | stack->top = stack->base; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 64 | return true; |
| 65 | } |
| 66 | |
| Carl Shapiro | fdf8052 | 2010-11-09 14:27:02 -0800 | [diff] [blame] | 67 | /* |
| 68 | * Assigns NULL to the stack top and advises the mark stack pages as |
| 69 | * not needed. |
| 70 | */ |
| Carl Shapiro | d7400e0 | 2010-09-02 18:24:29 -0700 | [diff] [blame] | 71 | static void destroyMarkStack(GcMarkStack *stack) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 72 | { |
| Carl Shapiro | fdf8052 | 2010-11-09 14:27:02 -0800 | [diff] [blame] | 73 | assert(stack != NULL); |
| 74 | madvise(stack->base, stack->length, MADV_DONTNEED); |
| 75 | stack->top = NULL; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| Carl Shapiro | fdf8052 | 2010-11-09 14:27:02 -0800 | [diff] [blame] | 78 | /* |
| 79 | * Pops an object from the mark stack. |
| 80 | */ |
| 81 | static 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 | */ |
| 94 | static const Object *markStackPop(GcMarkStack *stack) |
| 95 | { |
| Carl Shapiro | fdf8052 | 2010-11-09 14:27:02 -0800 | [diff] [blame] | 96 | assert(stack != NULL); |
| 97 | assert(stack->base < stack->top); |
| 98 | assert(stack->limit > stack->top); |
| 99 | --stack->top; |
| Carl Shapiro | 26a8280 | 2010-12-01 18:54:19 -0800 | [diff] [blame] | 100 | return *stack->top; |
| Carl Shapiro | fdf8052 | 2010-11-09 14:27:02 -0800 | [diff] [blame] | 101 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 102 | |
| Carl Shapiro | d7400e0 | 2010-09-02 18:24:29 -0700 | [diff] [blame] | 103 | bool dvmHeapBeginMarkStep(GcMode mode) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 104 | { |
| Carl Shapiro | b2e78d3 | 2010-08-20 11:34:18 -0700 | [diff] [blame] | 105 | GcMarkContext *ctx = &gDvm.gcHeap->markContext; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 106 | |
| Carl Shapiro | b2e78d3 | 2010-08-20 11:34:18 -0700 | [diff] [blame] | 107 | if (!createMarkStack(&ctx->stack)) { |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 108 | return false; |
| 109 | } |
| Carl Shapiro | b2e78d3 | 2010-08-20 11:34:18 -0700 | [diff] [blame] | 110 | ctx->finger = NULL; |
| Carl Shapiro | fc75f3e | 2010-12-07 11:43:38 -0800 | [diff] [blame^] | 111 | ctx->immuneLimit = (char*)dvmHeapSourceGetImmuneLimit(mode); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 112 | return true; |
| 113 | } |
| 114 | |
| Carl Shapiro | d7400e0 | 2010-09-02 18:24:29 -0700 | [diff] [blame] | 115 | static long setAndReturnMarkBit(GcMarkContext *ctx, const void *obj) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 116 | { |
| Carl Shapiro | f373efd | 2010-02-19 00:46:33 -0800 | [diff] [blame] | 117 | return dvmHeapBitmapSetAndReturnObjectBit(ctx->bitmap, obj); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| Carl Shapiro | d7400e0 | 2010-09-02 18:24:29 -0700 | [diff] [blame] | 120 | static void markObjectNonNull(const Object *obj, GcMarkContext *ctx, |
| 121 | bool checkFinger) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 122 | { |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 123 | assert(ctx != NULL); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 124 | assert(obj != NULL); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 125 | assert(dvmIsValidObject(obj)); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 126 | |
| Carl Shapiro | b31b301 | 2010-05-25 18:35:37 -0700 | [diff] [blame] | 127 | if (obj < (Object *)ctx->immuneLimit) { |
| Carl Shapiro | d25566d | 2010-03-11 20:39:47 -0800 | [diff] [blame] | 128 | assert(isMarked(obj, ctx)); |
| 129 | return; |
| 130 | } |
| Carl Shapiro | 6343bd0 | 2010-02-16 17:40:19 -0800 | [diff] [blame] | 131 | if (!setAndReturnMarkBit(ctx, obj)) { |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 132 | /* This object was not previously marked. |
| 133 | */ |
| Carl Shapiro | 106c5fd | 2010-07-28 14:12:27 -0700 | [diff] [blame] | 134 | if (checkFinger && (void *)obj < ctx->finger) { |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 135 | /* This object will need to go on the mark stack. |
| 136 | */ |
| Carl Shapiro | fdf8052 | 2010-11-09 14:27:02 -0800 | [diff] [blame] | 137 | markStackPush(&ctx->stack, obj); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 138 | } |
| 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 | |
| Carl Shapiro | 6d4ce5e | 2010-12-02 16:16:01 -0800 | [diff] [blame] | 155 | /* |
| 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 Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 159 | */ |
| Carl Shapiro | 6d4ce5e | 2010-12-02 16:16:01 -0800 | [diff] [blame] | 160 | static void rootMarkObjectVisitor(void *addr, RootType type, u4 thread, void *arg) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 161 | { |
| Carl Shapiro | 6d4ce5e | 2010-12-02 16:16:01 -0800 | [diff] [blame] | 162 | Object *obj; |
| Carl Shapiro | fc75f3e | 2010-12-07 11:43:38 -0800 | [diff] [blame^] | 163 | GcMarkContext *ctx; |
| Carl Shapiro | 6d4ce5e | 2010-12-02 16:16:01 -0800 | [diff] [blame] | 164 | |
| 165 | assert(addr != NULL); |
| 166 | assert(arg != NULL); |
| 167 | obj = *(Object **)addr; |
| Carl Shapiro | fc75f3e | 2010-12-07 11:43:38 -0800 | [diff] [blame^] | 168 | ctx = (GcMarkContext *)arg; |
| Carl Shapiro | 6d4ce5e | 2010-12-02 16:16:01 -0800 | [diff] [blame] | 169 | if (obj != NULL) { |
| Carl Shapiro | fc75f3e | 2010-12-07 11:43:38 -0800 | [diff] [blame^] | 170 | markObjectNonNull(obj, ctx, false); |
| Carl Shapiro | 6d4ce5e | 2010-12-02 16:16:01 -0800 | [diff] [blame] | 171 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /* Mark the set of root objects. |
| 175 | * |
| 176 | * Things we need to scan: |
| 177 | * - System classes defined by root classloader |
| 178 | * - For each thread: |
| 179 | * - Interpreted stack, from top to "curFrame" |
| 180 | * - Dalvik registers (args + local vars) |
| 181 | * - JNI local references |
| 182 | * - Automatic VM local references (TrackedAlloc) |
| 183 | * - Associated Thread/VMThread object |
| 184 | * - ThreadGroups (could track & start with these instead of working |
| 185 | * upward from Threads) |
| 186 | * - Exception currently being thrown, if present |
| 187 | * - JNI global references |
| 188 | * - Interned string table |
| 189 | * - Primitive classes |
| 190 | * - Special objects |
| 191 | * - gDvm.outOfMemoryObj |
| 192 | * - Objects allocated with ALLOC_NO_GC |
| 193 | * - Objects pending finalization (but not yet finalized) |
| 194 | * - Objects in debugger object registry |
| 195 | * |
| 196 | * Don't need: |
| 197 | * - Native stack (for in-progress stuff in the VM) |
| 198 | * - The TrackedAlloc stuff watches all native VM references. |
| 199 | */ |
| 200 | void dvmHeapMarkRootSet() |
| 201 | { |
| Barry Hayes | d4f78d3 | 2010-06-08 09:34:42 -0700 | [diff] [blame] | 202 | GcHeap *gcHeap = gDvm.gcHeap; |
| Barry Hayes | 425848f | 2010-05-04 13:32:12 -0700 | [diff] [blame] | 203 | dvmMarkImmuneObjects(gcHeap->markContext.immuneLimit); |
| Carl Shapiro | 6d4ce5e | 2010-12-02 16:16:01 -0800 | [diff] [blame] | 204 | dvmVisitRoots(rootMarkObjectVisitor, &gcHeap->markContext); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | /* |
| Carl Shapiro | 6d4ce5e | 2010-12-02 16:16:01 -0800 | [diff] [blame] | 208 | * Callback applied to root references during root remarking. If the |
| 209 | * root location contains a white reference it is pushed on the mark |
| 210 | * stack and grayed. |
| Carl Shapiro | 106c5fd | 2010-07-28 14:12:27 -0700 | [diff] [blame] | 211 | */ |
| Carl Shapiro | 07018e2 | 2010-10-26 21:07:41 -0700 | [diff] [blame] | 212 | static void markObjectVisitor(void *addr, RootType type, u4 thread, void *arg) |
| Carl Shapiro | 106c5fd | 2010-07-28 14:12:27 -0700 | [diff] [blame] | 213 | { |
| 214 | Object *obj; |
| Carl Shapiro | fc75f3e | 2010-12-07 11:43:38 -0800 | [diff] [blame^] | 215 | GcMarkContext *ctx; |
| Carl Shapiro | 106c5fd | 2010-07-28 14:12:27 -0700 | [diff] [blame] | 216 | |
| 217 | assert(addr != NULL); |
| 218 | assert(arg != NULL); |
| 219 | obj = *(Object **)addr; |
| Carl Shapiro | fc75f3e | 2010-12-07 11:43:38 -0800 | [diff] [blame^] | 220 | ctx = (GcMarkContext *)arg; |
| Carl Shapiro | 106c5fd | 2010-07-28 14:12:27 -0700 | [diff] [blame] | 221 | if (obj != NULL) { |
| Carl Shapiro | fc75f3e | 2010-12-07 11:43:38 -0800 | [diff] [blame^] | 222 | markObjectNonNull(obj, ctx, true); |
| Carl Shapiro | 106c5fd | 2010-07-28 14:12:27 -0700 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | |
| 226 | /* |
| 227 | * Grays all references in the roots. |
| 228 | */ |
| 229 | void dvmHeapReMarkRootSet(void) |
| 230 | { |
| 231 | GcMarkContext *ctx = &gDvm.gcHeap->markContext; |
| 232 | assert(ctx->finger == (void *)ULONG_MAX); |
| 233 | dvmVisitRoots(markObjectVisitor, ctx); |
| 234 | } |
| 235 | |
| 236 | /* |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 237 | * Scans instance fields. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 238 | */ |
| Carl Shapiro | 3e24d33 | 2010-11-29 17:24:50 -0800 | [diff] [blame] | 239 | static void scanFields(const Object *obj, GcMarkContext *ctx) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 240 | { |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 241 | assert(obj != NULL); |
| 242 | assert(obj->clazz != NULL); |
| 243 | assert(ctx != NULL); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 244 | |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 245 | if (obj->clazz->refOffsets != CLASS_WALK_SUPER) { |
| 246 | unsigned int refOffsets = obj->clazz->refOffsets; |
| Barry Hayes | eac47ed | 2009-06-22 11:45:20 -0700 | [diff] [blame] | 247 | while (refOffsets != 0) { |
| Carl Shapiro | 3e24d33 | 2010-11-29 17:24:50 -0800 | [diff] [blame] | 248 | size_t rshift = CLZ(refOffsets); |
| 249 | size_t offset = CLASS_OFFSET_FROM_CLZ(rshift); |
| 250 | Object *ref = dvmGetFieldObject((Object*)obj, offset); |
| 251 | markObject(ref, ctx); |
| Barry Hayes | eac47ed | 2009-06-22 11:45:20 -0700 | [diff] [blame] | 252 | refOffsets &= ~(CLASS_HIGH_BIT >> rshift); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 253 | } |
| Barry Hayes | eac47ed | 2009-06-22 11:45:20 -0700 | [diff] [blame] | 254 | } else { |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 255 | ClassObject *clazz; |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 256 | for (clazz = obj->clazz; clazz != NULL; clazz = clazz->super) { |
| 257 | InstField *field = clazz->ifields; |
| Carl Shapiro | 3e24d33 | 2010-11-29 17:24:50 -0800 | [diff] [blame] | 258 | int i; |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 259 | for (i = 0; i < clazz->ifieldRefCount; ++i, ++field) { |
| 260 | void *addr = BYTE_OFFSET((Object *)obj, field->byteOffset); |
| Carl Shapiro | fc75f3e | 2010-12-07 11:43:38 -0800 | [diff] [blame^] | 261 | Object *ref = (Object *)((JValue *)addr)->l; |
| 262 | markObject(ref, ctx); |
| Barry Hayes | eac47ed | 2009-06-22 11:45:20 -0700 | [diff] [blame] | 263 | } |
| Barry Hayes | eac47ed | 2009-06-22 11:45:20 -0700 | [diff] [blame] | 264 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 268 | /* |
| Carl Shapiro | 3e24d33 | 2010-11-29 17:24:50 -0800 | [diff] [blame] | 269 | * Scans the static fields of a class object. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 270 | */ |
| Carl Shapiro | 3e24d33 | 2010-11-29 17:24:50 -0800 | [diff] [blame] | 271 | static void scanStaticFields(const ClassObject *clazz, GcMarkContext *ctx) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 272 | { |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 273 | int i; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 274 | |
| Carl Shapiro | 3e24d33 | 2010-11-29 17:24:50 -0800 | [diff] [blame] | 275 | assert(clazz != NULL); |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 276 | assert(ctx != NULL); |
| Carl Shapiro | 3e24d33 | 2010-11-29 17:24:50 -0800 | [diff] [blame] | 277 | for (i = 0; i < clazz->sfieldCount; ++i) { |
| 278 | char ch = clazz->sfields[i].field.signature[0]; |
| 279 | if (ch == '[' || ch == 'L') { |
| Carl Shapiro | fc75f3e | 2010-12-07 11:43:38 -0800 | [diff] [blame^] | 280 | Object *obj = (Object *)clazz->sfields[i].value.l; |
| 281 | markObject(obj, ctx); |
| Carl Shapiro | 3e24d33 | 2010-11-29 17:24:50 -0800 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 285 | |
| Carl Shapiro | 3e24d33 | 2010-11-29 17:24:50 -0800 | [diff] [blame] | 286 | /* |
| 287 | * Visit the interfaces of a class object. |
| 288 | */ |
| 289 | static void scanInterfaces(const ClassObject *clazz, GcMarkContext *ctx) |
| 290 | { |
| 291 | int i; |
| 292 | |
| 293 | assert(clazz != NULL); |
| 294 | assert(ctx != NULL); |
| 295 | for (i = 0; i < clazz->interfaceCount; ++i) { |
| 296 | markObject((const Object *)clazz->interfaces[i], ctx); |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | /* |
| 301 | * Scans the header, static field references, and interface |
| 302 | * pointers of a class object. |
| 303 | */ |
| 304 | static void scanClassObject(const Object *obj, GcMarkContext *ctx) |
| 305 | { |
| 306 | const ClassObject *asClass; |
| 307 | |
| 308 | assert(obj != NULL); |
| 309 | assert(obj->clazz == gDvm.classJavaLangClass); |
| 310 | assert(ctx != NULL); |
| 311 | markObject((const Object *)obj->clazz, ctx); |
| 312 | asClass = (const ClassObject *)obj; |
| 313 | if (IS_CLASS_FLAG_SET(asClass, CLASS_ISARRAY)) { |
| 314 | markObject((const Object *)asClass->elementClass, ctx); |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 315 | } |
| Barry Hayes | c49db85 | 2010-05-14 13:43:34 -0700 | [diff] [blame] | 316 | /* Do super and the interfaces contain Objects and not dex idx values? */ |
| Carl Shapiro | 3e24d33 | 2010-11-29 17:24:50 -0800 | [diff] [blame] | 317 | if (asClass->status > CLASS_IDX) { |
| 318 | markObject((const Object *)asClass->super, ctx); |
| Barry Hayes | c49db85 | 2010-05-14 13:43:34 -0700 | [diff] [blame] | 319 | } |
| Carl Shapiro | 3e24d33 | 2010-11-29 17:24:50 -0800 | [diff] [blame] | 320 | markObject((const Object *)asClass->classLoader, ctx); |
| 321 | scanFields(obj, ctx); |
| 322 | scanStaticFields(asClass, ctx); |
| 323 | if (asClass->status > CLASS_IDX) { |
| 324 | scanInterfaces(asClass, ctx); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 325 | } |
| 326 | } |
| 327 | |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 328 | /* |
| 329 | * Scans the header of all array objects. If the array object is |
| 330 | * 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] | 331 | */ |
| Carl Shapiro | 3e24d33 | 2010-11-29 17:24:50 -0800 | [diff] [blame] | 332 | static void scanArrayObject(const Object *obj, GcMarkContext *ctx) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 333 | { |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 334 | assert(obj != NULL); |
| Carl Shapiro | 3e24d33 | 2010-11-29 17:24:50 -0800 | [diff] [blame] | 335 | assert(obj->clazz != NULL); |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 336 | assert(ctx != NULL); |
| Carl Shapiro | 3e24d33 | 2010-11-29 17:24:50 -0800 | [diff] [blame] | 337 | markObject((const Object *)obj->clazz, ctx); |
| 338 | if (IS_CLASS_FLAG_SET(obj->clazz, CLASS_ISOBJECTARRAY)) { |
| 339 | const ArrayObject *array = (const ArrayObject *)obj; |
| 340 | const Object **contents = (const Object **)array->contents; |
| 341 | size_t i; |
| 342 | for (i = 0; i < array->length; ++i) { |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 343 | markObject(contents[i], ctx); |
| 344 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 345 | } |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 346 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 347 | |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 348 | /* |
| Carl Shapiro | 2a6f484 | 2010-07-09 16:50:54 -0700 | [diff] [blame] | 349 | * Returns class flags relating to Reference subclasses. |
| 350 | */ |
| 351 | static int referenceClassFlags(const Object *obj) |
| 352 | { |
| 353 | int flags = CLASS_ISREFERENCE | |
| 354 | CLASS_ISWEAKREFERENCE | |
| 355 | CLASS_ISPHANTOMREFERENCE; |
| 356 | return GET_CLASS_FLAG_GROUP(obj->clazz, flags); |
| 357 | } |
| 358 | |
| 359 | /* |
| 360 | * Returns true if the object derives from SoftReference. |
| 361 | */ |
| 362 | static bool isSoftReference(const Object *obj) |
| 363 | { |
| 364 | return referenceClassFlags(obj) == CLASS_ISREFERENCE; |
| 365 | } |
| 366 | |
| 367 | /* |
| 368 | * Returns true if the object derives from WeakReference. |
| 369 | */ |
| 370 | static bool isWeakReference(const Object *obj) |
| 371 | { |
| 372 | return referenceClassFlags(obj) & CLASS_ISWEAKREFERENCE; |
| 373 | } |
| 374 | |
| 375 | /* |
| 376 | * Returns true if the object derives from PhantomReference. |
| 377 | */ |
| 378 | static bool isPhantomReference(const Object *obj) |
| 379 | { |
| 380 | return referenceClassFlags(obj) & CLASS_ISPHANTOMREFERENCE; |
| 381 | } |
| 382 | |
| 383 | /* |
| 384 | * Adds a reference to the tail of a circular queue of references. |
| 385 | */ |
| 386 | static void enqueuePendingReference(Object *ref, Object **list) |
| 387 | { |
| 388 | size_t offset; |
| 389 | |
| 390 | assert(ref != NULL); |
| 391 | assert(list != NULL); |
| 392 | offset = gDvm.offJavaLangRefReference_pendingNext; |
| 393 | if (*list == NULL) { |
| 394 | dvmSetFieldObject(ref, offset, ref); |
| 395 | *list = ref; |
| 396 | } else { |
| 397 | Object *head = dvmGetFieldObject(*list, offset); |
| 398 | dvmSetFieldObject(ref, offset, head); |
| 399 | dvmSetFieldObject(*list, offset, ref); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | /* |
| Carl Shapiro | a1b03a9 | 2010-07-12 14:02:28 -0700 | [diff] [blame] | 404 | * Removes the reference at the head of a circular queue of |
| 405 | * references. |
| Carl Shapiro | 2a6f484 | 2010-07-09 16:50:54 -0700 | [diff] [blame] | 406 | */ |
| 407 | static Object *dequeuePendingReference(Object **list) |
| 408 | { |
| 409 | Object *ref, *head; |
| 410 | size_t offset; |
| 411 | |
| 412 | assert(list != NULL); |
| 413 | assert(*list != NULL); |
| 414 | offset = gDvm.offJavaLangRefReference_pendingNext; |
| 415 | head = dvmGetFieldObject(*list, offset); |
| 416 | if (*list == head) { |
| 417 | ref = *list; |
| 418 | *list = NULL; |
| 419 | } else { |
| 420 | Object *next = dvmGetFieldObject(head, offset); |
| 421 | dvmSetFieldObject(*list, offset, next); |
| 422 | ref = head; |
| 423 | } |
| 424 | dvmSetFieldObject(ref, offset, NULL); |
| 425 | return ref; |
| 426 | } |
| 427 | |
| 428 | /* |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 429 | * Process the "referent" field in a java.lang.ref.Reference. If the |
| 430 | * referent has not yet been marked, put it on the appropriate list in |
| 431 | * the gcHeap for later processing. |
| 432 | */ |
| Barry Hayes | 697b5a9 | 2010-06-23 11:38:52 -0700 | [diff] [blame] | 433 | static void delayReferenceReferent(Object *obj, GcMarkContext *ctx) |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 434 | { |
| Carl Shapiro | 2a6f484 | 2010-07-09 16:50:54 -0700 | [diff] [blame] | 435 | GcHeap *gcHeap = gDvm.gcHeap; |
| 436 | Object *pending, *referent; |
| 437 | size_t pendingNextOffset, referentOffset; |
| 438 | |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 439 | assert(obj != NULL); |
| Barry Hayes | 697b5a9 | 2010-06-23 11:38:52 -0700 | [diff] [blame] | 440 | assert(obj->clazz != NULL); |
| Carl Shapiro | 2a6f484 | 2010-07-09 16:50:54 -0700 | [diff] [blame] | 441 | assert(IS_CLASS_FLAG_SET(obj->clazz, CLASS_ISREFERENCE)); |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 442 | assert(ctx != NULL); |
| Carl Shapiro | 2a6f484 | 2010-07-09 16:50:54 -0700 | [diff] [blame] | 443 | pendingNextOffset = gDvm.offJavaLangRefReference_pendingNext; |
| 444 | referentOffset = gDvm.offJavaLangRefReference_referent; |
| 445 | pending = dvmGetFieldObject(obj, pendingNextOffset); |
| 446 | referent = dvmGetFieldObject(obj, referentOffset); |
| 447 | if (pending == NULL && referent != NULL && !isMarked(referent, ctx)) { |
| 448 | Object **list = NULL; |
| 449 | if (isSoftReference(obj)) { |
| 450 | list = &gcHeap->softReferences; |
| 451 | } else if (isWeakReference(obj)) { |
| 452 | list = &gcHeap->weakReferences; |
| 453 | } else if (isPhantomReference(obj)) { |
| 454 | list = &gcHeap->phantomReferences; |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 455 | } |
| Carl Shapiro | 2a6f484 | 2010-07-09 16:50:54 -0700 | [diff] [blame] | 456 | assert(list != NULL); |
| 457 | enqueuePendingReference(obj, list); |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 458 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 459 | } |
| 460 | |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 461 | /* |
| 462 | * Scans the header and field references of a data object. |
| 463 | */ |
| Carl Shapiro | 3e24d33 | 2010-11-29 17:24:50 -0800 | [diff] [blame] | 464 | static void scanDataObject(const Object *obj, GcMarkContext *ctx) |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 465 | { |
| 466 | assert(obj != NULL); |
| Carl Shapiro | 3e24d33 | 2010-11-29 17:24:50 -0800 | [diff] [blame] | 467 | assert(obj->clazz != NULL); |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 468 | assert(ctx != NULL); |
| Carl Shapiro | 3e24d33 | 2010-11-29 17:24:50 -0800 | [diff] [blame] | 469 | markObject((const Object *)obj->clazz, ctx); |
| 470 | scanFields(obj, ctx); |
| 471 | if (IS_CLASS_FLAG_SET(obj->clazz, CLASS_ISREFERENCE)) { |
| Barry Hayes | 697b5a9 | 2010-06-23 11:38:52 -0700 | [diff] [blame] | 472 | delayReferenceReferent((Object *)obj, ctx); |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 473 | } |
| 474 | } |
| 475 | |
| 476 | /* |
| 477 | * Scans an object reference. Determines the type of the reference |
| 478 | * and dispatches to a specialized scanning routine. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 479 | */ |
| 480 | static void scanObject(const Object *obj, GcMarkContext *ctx) |
| 481 | { |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 482 | assert(obj != NULL); |
| 483 | assert(ctx != NULL); |
| Barry Hayes | 899cdb7 | 2010-06-08 09:59:12 -0700 | [diff] [blame] | 484 | assert(obj->clazz != NULL); |
| Carl Shapiro | 1a8e21a | 2010-06-08 13:19:57 -0700 | [diff] [blame] | 485 | if (obj->clazz == gDvm.classJavaLangClass) { |
| Carl Shapiro | 3e24d33 | 2010-11-29 17:24:50 -0800 | [diff] [blame] | 486 | scanClassObject(obj, ctx); |
| Carl Shapiro | 1a8e21a | 2010-06-08 13:19:57 -0700 | [diff] [blame] | 487 | } else if (IS_CLASS_FLAG_SET(obj->clazz, CLASS_ISARRAY)) { |
| Carl Shapiro | 3e24d33 | 2010-11-29 17:24:50 -0800 | [diff] [blame] | 488 | scanArrayObject(obj, ctx); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 489 | } else { |
| Carl Shapiro | 3e24d33 | 2010-11-29 17:24:50 -0800 | [diff] [blame] | 490 | scanDataObject(obj, ctx); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 491 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 492 | } |
| 493 | |
| Carl Shapiro | fdf8052 | 2010-11-09 14:27:02 -0800 | [diff] [blame] | 494 | /* |
| 495 | * Scan anything that's on the mark stack. We can't use the bitmaps |
| 496 | * anymore, so use a finger that points past the end of them. |
| 497 | */ |
| 498 | static void processMarkStack(GcMarkContext *ctx) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 499 | { |
| Carl Shapiro | fdf8052 | 2010-11-09 14:27:02 -0800 | [diff] [blame] | 500 | GcMarkStack *stack; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 501 | |
| Carl Shapiro | fdf8052 | 2010-11-09 14:27:02 -0800 | [diff] [blame] | 502 | assert(ctx != NULL); |
| Carl Shapiro | 034fba5 | 2010-11-11 16:39:58 -0800 | [diff] [blame] | 503 | assert(ctx->finger == (void *)ULONG_MAX); |
| Carl Shapiro | fdf8052 | 2010-11-09 14:27:02 -0800 | [diff] [blame] | 504 | stack = &ctx->stack; |
| 505 | assert(stack->top >= stack->base); |
| 506 | while (stack->top > stack->base) { |
| 507 | const Object *obj = markStackPop(stack); |
| 508 | scanObject(obj, ctx); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 509 | } |
| 510 | } |
| 511 | |
| Carl Shapiro | 106c5fd | 2010-07-28 14:12:27 -0700 | [diff] [blame] | 512 | static size_t objectSize(const Object *obj) |
| 513 | { |
| 514 | assert(dvmIsValidObject(obj)); |
| 515 | assert(dvmIsValidObject((Object *)obj->clazz)); |
| 516 | if (IS_CLASS_FLAG_SET(obj->clazz, CLASS_ISARRAY)) { |
| 517 | return dvmArrayObjectSize((ArrayObject *)obj); |
| 518 | } else if (obj->clazz == gDvm.classJavaLangClass) { |
| 519 | return dvmClassObjectSize((ClassObject *)obj); |
| 520 | } else { |
| 521 | return obj->clazz->objectSize; |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | /* |
| Carl Shapiro | 106c5fd | 2010-07-28 14:12:27 -0700 | [diff] [blame] | 526 | * Scans forward to the header of the next marked object between start |
| 527 | * and limit. Returns NULL if no marked objects are in that region. |
| 528 | */ |
| Carl Shapiro | 7ec9144 | 2010-10-21 15:35:19 -0700 | [diff] [blame] | 529 | static Object *nextGrayObject(const u1 *base, const u1 *limit, |
| 530 | const HeapBitmap *markBits) |
| Carl Shapiro | 106c5fd | 2010-07-28 14:12:27 -0700 | [diff] [blame] | 531 | { |
| Carl Shapiro | 7ec9144 | 2010-10-21 15:35:19 -0700 | [diff] [blame] | 532 | const u1 *ptr; |
| Carl Shapiro | 106c5fd | 2010-07-28 14:12:27 -0700 | [diff] [blame] | 533 | |
| 534 | assert(base < limit); |
| 535 | assert(limit - base <= GC_CARD_SIZE); |
| 536 | for (ptr = base; ptr < limit; ptr += HB_OBJECT_ALIGNMENT) { |
| Carl Shapiro | ea10c55 | 2010-09-02 23:32:25 -0700 | [diff] [blame] | 537 | if (dvmHeapBitmapIsObjectBitSet(markBits, ptr)) |
| Carl Shapiro | 106c5fd | 2010-07-28 14:12:27 -0700 | [diff] [blame] | 538 | return (Object *)ptr; |
| 539 | } |
| 540 | return NULL; |
| 541 | } |
| 542 | |
| 543 | /* |
| Carl Shapiro | 7ec9144 | 2010-10-21 15:35:19 -0700 | [diff] [blame] | 544 | * Scans each byte from start below end returning the address of the |
| 545 | * first dirty card. Returns NULL if no dirty card is found. |
| 546 | */ |
| 547 | static const u1 *scanBytesForDirtyCard(const u1 *start, const u1 *end) |
| 548 | { |
| 549 | const u1 *ptr; |
| 550 | |
| 551 | assert(start <= end); |
| 552 | for (ptr = start; ptr < end; ++ptr) { |
| 553 | if (*ptr == GC_CARD_DIRTY) { |
| 554 | return ptr; |
| 555 | } |
| 556 | } |
| 557 | return NULL; |
| 558 | } |
| 559 | |
| 560 | /* |
| 561 | * Like scanBytesForDirtyCard but scans the range from start below end |
| 562 | * by words. Assumes start and end are word aligned. |
| 563 | */ |
| 564 | static const u1 *scanWordsForDirtyCard(const u1 *start, const u1 *end) |
| 565 | { |
| 566 | const u1 *ptr; |
| 567 | |
| 568 | assert((uintptr_t)start % kWordSize == 0); |
| 569 | assert((uintptr_t)end % kWordSize == 0); |
| 570 | assert(start <= end); |
| 571 | for (ptr = start; ptr < end; ptr += kWordSize) { |
| 572 | if (*(const Word *)ptr != 0) { |
| 573 | const u1 *dirty = scanBytesForDirtyCard(ptr, ptr + kWordSize); |
| 574 | if (dirty != NULL) { |
| 575 | return dirty; |
| 576 | } |
| 577 | } |
| 578 | } |
| 579 | return NULL; |
| 580 | } |
| 581 | |
| 582 | /* |
| 583 | * Scans the card table as quickly as possible looking for a dirty |
| 584 | * card. Returns the address of the first dirty card found or NULL if |
| 585 | * no dirty cards were found. |
| 586 | */ |
| 587 | static const u1 *nextDirtyCard(const u1 *start, const u1 *end) |
| 588 | { |
| 589 | const u1 *wstart = (u1 *)ALIGN_UP(start, kWordSize); |
| 590 | const u1 *wend = (u1 *)ALIGN_DOWN(end, kWordSize); |
| 591 | const u1 *ptr, *dirty; |
| 592 | |
| 593 | assert(start <= end); |
| 594 | assert(start <= wstart); |
| 595 | assert(end >= wend); |
| 596 | ptr = start; |
| 597 | if (wstart < end) { |
| 598 | /* Scan the leading unaligned bytes. */ |
| 599 | dirty = scanBytesForDirtyCard(ptr, wstart); |
| 600 | if (dirty != NULL) { |
| 601 | return dirty; |
| 602 | } |
| 603 | /* Scan the range of aligned words. */ |
| 604 | dirty = scanWordsForDirtyCard(wstart, wend); |
| 605 | if (dirty != NULL) { |
| 606 | return dirty; |
| 607 | } |
| 608 | ptr = wend; |
| 609 | } |
| 610 | /* Scan trailing unaligned bytes. */ |
| 611 | dirty = scanBytesForDirtyCard(ptr, end); |
| 612 | if (dirty != NULL) { |
| 613 | return dirty; |
| 614 | } |
| 615 | return NULL; |
| 616 | } |
| 617 | |
| 618 | /* |
| 619 | * Scans range of dirty cards between start and end. A range of dirty |
| 620 | * cards is composed consecutively dirty cards or dirty cards spanned |
| 621 | * by a gray object. Returns the address of a clean card if the scan |
| 622 | * reached a clean card or NULL if the scan reached the end. |
| 623 | */ |
| 624 | const u1 *scanDirtyCards(const u1 *start, const u1 *end, |
| 625 | GcMarkContext *ctx) |
| 626 | { |
| 627 | const HeapBitmap *markBits = ctx->bitmap; |
| 628 | const u1 *card = start, *prevAddr = NULL; |
| 629 | while (card < end) { |
| 630 | if (*card != GC_CARD_DIRTY) { |
| 631 | return card; |
| 632 | } |
| Carl Shapiro | fc75f3e | 2010-12-07 11:43:38 -0800 | [diff] [blame^] | 633 | const u1 *ptr = prevAddr ? prevAddr : (u1*)dvmAddrFromCard(card); |
| Carl Shapiro | 7ec9144 | 2010-10-21 15:35:19 -0700 | [diff] [blame] | 634 | const u1 *limit = ptr + GC_CARD_SIZE; |
| 635 | while (ptr < limit) { |
| 636 | Object *obj = nextGrayObject(ptr, limit, markBits); |
| 637 | if (obj == NULL) { |
| 638 | break; |
| 639 | } |
| 640 | scanObject(obj, ctx); |
| 641 | ptr = (u1*)obj + ALIGN_UP(objectSize(obj), HB_OBJECT_ALIGNMENT); |
| 642 | } |
| 643 | if (ptr < limit) { |
| 644 | /* Ended within the current card, advance to the next card. */ |
| 645 | ++card; |
| 646 | prevAddr = NULL; |
| 647 | } else { |
| 648 | /* Ended past the current card, skip ahead. */ |
| 649 | card = dvmCardFromAddr(ptr); |
| 650 | prevAddr = ptr; |
| 651 | } |
| 652 | } |
| 653 | return NULL; |
| 654 | } |
| 655 | |
| 656 | /* |
| 657 | * Blackens gray objects found on dirty cards. |
| Carl Shapiro | 106c5fd | 2010-07-28 14:12:27 -0700 | [diff] [blame] | 658 | */ |
| 659 | static void scanGrayObjects(GcMarkContext *ctx) |
| 660 | { |
| 661 | GcHeap *h = gDvm.gcHeap; |
| Carl Shapiro | 7ec9144 | 2010-10-21 15:35:19 -0700 | [diff] [blame] | 662 | const u1 *base, *limit, *ptr, *dirty; |
| Carl Shapiro | b8c48ae | 2010-08-12 11:24:44 -0700 | [diff] [blame] | 663 | size_t footprint; |
| Carl Shapiro | 106c5fd | 2010-07-28 14:12:27 -0700 | [diff] [blame] | 664 | |
| Carl Shapiro | b8c48ae | 2010-08-12 11:24:44 -0700 | [diff] [blame] | 665 | footprint = dvmHeapSourceGetValue(HS_FOOTPRINT, NULL, 0); |
| Carl Shapiro | 7ec9144 | 2010-10-21 15:35:19 -0700 | [diff] [blame] | 666 | base = &h->cardTableBase[0]; |
| 667 | limit = dvmCardFromAddr((u1 *)dvmHeapSourceGetBase() + footprint); |
| 668 | assert(limit <= &h->cardTableBase[h->cardTableLength]); |
| 669 | |
| 670 | ptr = base; |
| 671 | for (;;) { |
| 672 | dirty = nextDirtyCard(ptr, limit); |
| 673 | if (dirty == NULL) { |
| 674 | break; |
| Carl Shapiro | 106c5fd | 2010-07-28 14:12:27 -0700 | [diff] [blame] | 675 | } |
| Carl Shapiro | 7ec9144 | 2010-10-21 15:35:19 -0700 | [diff] [blame] | 676 | assert((dirty > ptr) && (dirty < limit)); |
| 677 | ptr = scanDirtyCards(dirty, limit, ctx); |
| 678 | if (ptr == NULL) { |
| 679 | break; |
| 680 | } |
| 681 | assert((ptr > dirty) && (ptr < limit)); |
| Carl Shapiro | 106c5fd | 2010-07-28 14:12:27 -0700 | [diff] [blame] | 682 | } |
| 683 | } |
| 684 | |
| Carl Shapiro | 57ee270 | 2010-08-27 13:06:48 -0700 | [diff] [blame] | 685 | /* |
| 686 | * Callback for scanning each object in the bitmap. The finger is set |
| 687 | * to the address corresponding to the lowest address in the next word |
| 688 | * of bits in the bitmap. |
| 689 | */ |
| Carl Shapiro | 38d710b | 2010-08-31 16:48:31 -0700 | [diff] [blame] | 690 | static void scanBitmapCallback(void *addr, void *finger, void *arg) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 691 | { |
| Carl Shapiro | fc75f3e | 2010-12-07 11:43:38 -0800 | [diff] [blame^] | 692 | GcMarkContext *ctx = (GcMarkContext *)arg; |
| Carl Shapiro | 57ee270 | 2010-08-27 13:06:48 -0700 | [diff] [blame] | 693 | ctx->finger = (void *)finger; |
| Carl Shapiro | fc75f3e | 2010-12-07 11:43:38 -0800 | [diff] [blame^] | 694 | scanObject((Object *)addr, ctx); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | /* Given bitmaps with the root set marked, find and mark all |
| 698 | * reachable objects. When this returns, the entire set of |
| 699 | * live objects will be marked and the mark stack will be empty. |
| 700 | */ |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 701 | void dvmHeapScanMarkedObjects(void) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 702 | { |
| 703 | GcMarkContext *ctx = &gDvm.gcHeap->markContext; |
| 704 | |
| 705 | assert(ctx->finger == NULL); |
| 706 | |
| 707 | /* The bitmaps currently have bits set for the root set. |
| 708 | * Walk across the bitmaps and scan each object. |
| 709 | */ |
| Carl Shapiro | 38d710b | 2010-08-31 16:48:31 -0700 | [diff] [blame] | 710 | dvmHeapBitmapScanWalk(ctx->bitmap, scanBitmapCallback, ctx); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 711 | |
| Carl Shapiro | 034fba5 | 2010-11-11 16:39:58 -0800 | [diff] [blame] | 712 | ctx->finger = (void *)ULONG_MAX; |
| 713 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 714 | /* We've walked the mark bitmaps. Scan anything that's |
| 715 | * left on the mark stack. |
| 716 | */ |
| 717 | processMarkStack(ctx); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 718 | } |
| 719 | |
| Carl Shapiro | 106c5fd | 2010-07-28 14:12:27 -0700 | [diff] [blame] | 720 | void dvmHeapReScanMarkedObjects(void) |
| Carl Shapiro | ec805ea | 2010-06-28 16:28:26 -0700 | [diff] [blame] | 721 | { |
| Carl Shapiro | 106c5fd | 2010-07-28 14:12:27 -0700 | [diff] [blame] | 722 | GcMarkContext *ctx = &gDvm.gcHeap->markContext; |
| Carl Shapiro | ec805ea | 2010-06-28 16:28:26 -0700 | [diff] [blame] | 723 | |
| Carl Shapiro | ec805ea | 2010-06-28 16:28:26 -0700 | [diff] [blame] | 724 | /* |
| Carl Shapiro | f586033 | 2010-06-28 23:02:08 -0700 | [diff] [blame] | 725 | * The finger must have been set to the maximum value to ensure |
| 726 | * that gray objects will be pushed onto the mark stack. |
| Carl Shapiro | ec805ea | 2010-06-28 16:28:26 -0700 | [diff] [blame] | 727 | */ |
| 728 | assert(ctx->finger == (void *)ULONG_MAX); |
| Carl Shapiro | 106c5fd | 2010-07-28 14:12:27 -0700 | [diff] [blame] | 729 | scanGrayObjects(ctx); |
| Carl Shapiro | ec805ea | 2010-06-28 16:28:26 -0700 | [diff] [blame] | 730 | processMarkStack(ctx); |
| 731 | } |
| 732 | |
| Carl Shapiro | 34f5199 | 2010-07-09 17:55:41 -0700 | [diff] [blame] | 733 | /* |
| 734 | * Clear the referent field. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 735 | */ |
| Barry Hayes | 6930a11 | 2009-12-22 11:01:38 -0800 | [diff] [blame] | 736 | static void clearReference(Object *reference) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 737 | { |
| Carl Shapiro | 34f5199 | 2010-07-09 17:55:41 -0700 | [diff] [blame] | 738 | size_t offset = gDvm.offJavaLangRefReference_referent; |
| 739 | dvmSetFieldObject(reference, offset, NULL); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 740 | } |
| 741 | |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 742 | /* |
| 743 | * Returns true if the reference was registered with a reference queue |
| 744 | * and has not yet been enqueued. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 745 | */ |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 746 | static bool isEnqueuable(const Object *reference) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 747 | { |
| Barry Hayes | 6930a11 | 2009-12-22 11:01:38 -0800 | [diff] [blame] | 748 | Object *queue = dvmGetFieldObject(reference, |
| 749 | gDvm.offJavaLangRefReference_queue); |
| 750 | Object *queueNext = dvmGetFieldObject(reference, |
| 751 | gDvm.offJavaLangRefReference_queueNext); |
| Carl Shapiro | 2a6f484 | 2010-07-09 16:50:54 -0700 | [diff] [blame] | 752 | return queue != NULL && queueNext == NULL; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 753 | } |
| 754 | |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 755 | /* |
| 756 | * 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] | 757 | */ |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 758 | static void enqueueReference(Object *ref) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 759 | { |
| Carl Shapiro | 646ba09 | 2010-06-10 15:17:00 -0700 | [diff] [blame] | 760 | assert(ref != NULL); |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 761 | assert(dvmGetFieldObject(ref, gDvm.offJavaLangRefReference_queue) != NULL); |
| 762 | assert(dvmGetFieldObject(ref, gDvm.offJavaLangRefReference_queueNext) == NULL); |
| Carl Shapiro | 646ba09 | 2010-06-10 15:17:00 -0700 | [diff] [blame] | 763 | if (!dvmHeapAddRefToLargeTable(&gDvm.gcHeap->referenceOperations, ref)) { |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 764 | LOGE_HEAP("enqueueReference(): no room for any more " |
| 765 | "reference operations\n"); |
| 766 | dvmAbort(); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 767 | } |
| 768 | } |
| 769 | |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 770 | /* |
| 771 | * Walks the reference list marking any references subject to the |
| 772 | * reference clearing policy. References with a black referent are |
| 773 | * removed from the list. References with white referents biased |
| 774 | * toward saving are blackened and also removed from the list. |
| 775 | */ |
| Carl Shapiro | e8ef2b5 | 2010-12-01 19:32:05 -0800 | [diff] [blame] | 776 | static void preserveSomeSoftReferences(Object **list) |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 777 | { |
| Carl Shapiro | b2e78d3 | 2010-08-20 11:34:18 -0700 | [diff] [blame] | 778 | GcMarkContext *ctx; |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 779 | Object *ref, *referent; |
| Carl Shapiro | 2a6f484 | 2010-07-09 16:50:54 -0700 | [diff] [blame] | 780 | Object *clear; |
| Carl Shapiro | a1b03a9 | 2010-07-12 14:02:28 -0700 | [diff] [blame] | 781 | size_t referentOffset; |
| Carl Shapiro | 2a6f484 | 2010-07-09 16:50:54 -0700 | [diff] [blame] | 782 | size_t counter; |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 783 | bool marked; |
| 784 | |
| Carl Shapiro | b2e78d3 | 2010-08-20 11:34:18 -0700 | [diff] [blame] | 785 | ctx = &gDvm.gcHeap->markContext; |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 786 | referentOffset = gDvm.offJavaLangRefReference_referent; |
| Carl Shapiro | 2a6f484 | 2010-07-09 16:50:54 -0700 | [diff] [blame] | 787 | clear = NULL; |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 788 | counter = 0; |
| Carl Shapiro | 2a6f484 | 2010-07-09 16:50:54 -0700 | [diff] [blame] | 789 | while (*list != NULL) { |
| 790 | ref = dequeuePendingReference(list); |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 791 | referent = dvmGetFieldObject(ref, referentOffset); |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 792 | assert(referent != NULL); |
| Carl Shapiro | b2e78d3 | 2010-08-20 11:34:18 -0700 | [diff] [blame] | 793 | marked = isMarked(referent, ctx); |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 794 | if (!marked && ((++counter) & 1)) { |
| 795 | /* Referent is white and biased toward saving, mark it. */ |
| Carl Shapiro | b2e78d3 | 2010-08-20 11:34:18 -0700 | [diff] [blame] | 796 | markObject(referent, ctx); |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 797 | marked = true; |
| 798 | } |
| Carl Shapiro | 2a6f484 | 2010-07-09 16:50:54 -0700 | [diff] [blame] | 799 | if (!marked) { |
| 800 | /* Referent is white, queue it for clearing. */ |
| 801 | enqueuePendingReference(ref, &clear); |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 802 | } |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 803 | } |
| Carl Shapiro | 2a6f484 | 2010-07-09 16:50:54 -0700 | [diff] [blame] | 804 | *list = clear; |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 805 | /* |
| 806 | * Restart the mark with the newly black references added to the |
| 807 | * root set. |
| 808 | */ |
| Carl Shapiro | b2e78d3 | 2010-08-20 11:34:18 -0700 | [diff] [blame] | 809 | processMarkStack(ctx); |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | /* |
| Carl Shapiro | a1b03a9 | 2010-07-12 14:02:28 -0700 | [diff] [blame] | 813 | * Unlink the reference list clearing references objects with white |
| 814 | * referents. Cleared references registered to a reference queue are |
| 815 | * scheduled for appending by the heap worker thread. |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 816 | */ |
| Carl Shapiro | e8ef2b5 | 2010-12-01 19:32:05 -0800 | [diff] [blame] | 817 | static void clearWhiteReferences(Object **list) |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 818 | { |
| Carl Shapiro | b2e78d3 | 2010-08-20 11:34:18 -0700 | [diff] [blame] | 819 | GcMarkContext *ctx; |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 820 | Object *ref, *referent; |
| Carl Shapiro | a1b03a9 | 2010-07-12 14:02:28 -0700 | [diff] [blame] | 821 | size_t referentOffset; |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 822 | bool doSignal; |
| 823 | |
| Carl Shapiro | b2e78d3 | 2010-08-20 11:34:18 -0700 | [diff] [blame] | 824 | ctx = &gDvm.gcHeap->markContext; |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 825 | referentOffset = gDvm.offJavaLangRefReference_referent; |
| 826 | doSignal = false; |
| 827 | while (*list != NULL) { |
| Carl Shapiro | 2a6f484 | 2010-07-09 16:50:54 -0700 | [diff] [blame] | 828 | ref = dequeuePendingReference(list); |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 829 | referent = dvmGetFieldObject(ref, referentOffset); |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 830 | assert(referent != NULL); |
| Carl Shapiro | b2e78d3 | 2010-08-20 11:34:18 -0700 | [diff] [blame] | 831 | if (!isMarked(referent, ctx)) { |
| Carl Shapiro | a1b03a9 | 2010-07-12 14:02:28 -0700 | [diff] [blame] | 832 | /* Referent is white, clear it. */ |
| Carl Shapiro | 2954074 | 2010-03-26 15:34:39 -0700 | [diff] [blame] | 833 | clearReference(ref); |
| 834 | if (isEnqueuable(ref)) { |
| 835 | enqueueReference(ref); |
| 836 | doSignal = true; |
| 837 | } |
| 838 | } |
| 839 | } |
| 840 | /* |
| 841 | * If we cleared a reference with a reference queue we must notify |
| 842 | * the heap worker to append the reference. |
| 843 | */ |
| 844 | if (doSignal) { |
| 845 | dvmSignalHeapWorker(false); |
| 846 | } |
| 847 | assert(*list == NULL); |
| 848 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 849 | |
| 850 | /* Find unreachable objects that need to be finalized, |
| 851 | * and schedule them for finalization. |
| 852 | */ |
| Carl Shapiro | e8ef2b5 | 2010-12-01 19:32:05 -0800 | [diff] [blame] | 853 | static void scheduleFinalizations(void) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 854 | { |
| 855 | HeapRefTable newPendingRefs; |
| 856 | LargeHeapRefTable *finRefs = gDvm.gcHeap->finalizableRefs; |
| 857 | Object **ref; |
| 858 | Object **lastRef; |
| 859 | size_t totalPendCount; |
| Carl Shapiro | b2e78d3 | 2010-08-20 11:34:18 -0700 | [diff] [blame] | 860 | GcMarkContext *ctx = &gDvm.gcHeap->markContext; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 861 | |
| 862 | /* |
| 863 | * All reachable objects have been marked. |
| 864 | * Any unmarked finalizable objects need to be finalized. |
| 865 | */ |
| 866 | |
| 867 | /* Create a table that the new pending refs will |
| 868 | * be added to. |
| 869 | */ |
| Barry Hayes | d4f78d3 | 2010-06-08 09:34:42 -0700 | [diff] [blame] | 870 | if (!dvmHeapInitHeapRefTable(&newPendingRefs)) { |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 871 | //TODO: mark all finalizable refs and hope that |
| 872 | // we can schedule them next time. Watch out, |
| 873 | // because we may be expecting to free up space |
| 874 | // by calling finalizers. |
| Carl Shapiro | e8ef2b5 | 2010-12-01 19:32:05 -0800 | [diff] [blame] | 875 | LOGE_GC("scheduleFinalizations(): no room for " |
| 876 | "pending finalizations"); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 877 | dvmAbort(); |
| 878 | } |
| 879 | |
| 880 | /* Walk through finalizableRefs and move any unmarked references |
| 881 | * to the list of new pending refs. |
| 882 | */ |
| 883 | totalPendCount = 0; |
| 884 | while (finRefs != NULL) { |
| 885 | Object **gapRef; |
| 886 | size_t newPendCount = 0; |
| 887 | |
| 888 | gapRef = ref = finRefs->refs.table; |
| 889 | lastRef = finRefs->refs.nextEntry; |
| 890 | while (ref < lastRef) { |
| Carl Shapiro | b2e78d3 | 2010-08-20 11:34:18 -0700 | [diff] [blame] | 891 | if (!isMarked(*ref, ctx)) { |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 892 | if (!dvmHeapAddToHeapRefTable(&newPendingRefs, *ref)) { |
| 893 | //TODO: add the current table and allocate |
| 894 | // a new, smaller one. |
| Carl Shapiro | e8ef2b5 | 2010-12-01 19:32:05 -0800 | [diff] [blame] | 895 | LOGE_GC("scheduleFinalizations(): " |
| 896 | "no room for any more pending finalizations: %zd", |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 897 | dvmHeapNumHeapRefTableEntries(&newPendingRefs)); |
| 898 | dvmAbort(); |
| 899 | } |
| 900 | newPendCount++; |
| 901 | } else { |
| 902 | /* This ref is marked, so will remain on finalizableRefs. |
| 903 | */ |
| 904 | if (newPendCount > 0) { |
| 905 | /* Copy it up to fill the holes. |
| 906 | */ |
| 907 | *gapRef++ = *ref; |
| 908 | } else { |
| 909 | /* No holes yet; don't bother copying. |
| 910 | */ |
| 911 | gapRef++; |
| 912 | } |
| 913 | } |
| 914 | ref++; |
| 915 | } |
| 916 | finRefs->refs.nextEntry = gapRef; |
| 917 | //TODO: if the table is empty when we're done, free it. |
| 918 | totalPendCount += newPendCount; |
| 919 | finRefs = finRefs->next; |
| 920 | } |
| Carl Shapiro | e8ef2b5 | 2010-12-01 19:32:05 -0800 | [diff] [blame] | 921 | LOGD_GC("scheduleFinalizations(): %zd finalizers triggered.", |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 922 | totalPendCount); |
| 923 | if (totalPendCount == 0) { |
| 924 | /* No objects required finalization. |
| 925 | * Free the empty temporary table. |
| 926 | */ |
| 927 | dvmClearReferenceTable(&newPendingRefs); |
| 928 | return; |
| 929 | } |
| 930 | |
| 931 | /* Add the new pending refs to the main list. |
| 932 | */ |
| 933 | if (!dvmHeapAddTableToLargeTable(&gDvm.gcHeap->pendingFinalizationRefs, |
| 934 | &newPendingRefs)) |
| 935 | { |
| Carl Shapiro | e8ef2b5 | 2010-12-01 19:32:05 -0800 | [diff] [blame] | 936 | LOGE_GC("scheduleFinalizations(): can't insert new " |
| 937 | "pending finalizations"); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 938 | dvmAbort(); |
| 939 | } |
| 940 | |
| 941 | //TODO: try compacting the main list with a memcpy loop |
| 942 | |
| 943 | /* Mark the refs we just moved; we don't want them or their |
| 944 | * children to get swept yet. |
| 945 | */ |
| 946 | ref = newPendingRefs.table; |
| 947 | lastRef = newPendingRefs.nextEntry; |
| 948 | assert(ref < lastRef); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 949 | while (ref < lastRef) { |
| Barry Hayes | e1bccb9 | 2010-05-18 09:48:37 -0700 | [diff] [blame] | 950 | assert(*ref != NULL); |
| Carl Shapiro | b2e78d3 | 2010-08-20 11:34:18 -0700 | [diff] [blame] | 951 | markObject(*ref, ctx); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 952 | ref++; |
| 953 | } |
| Carl Shapiro | b2e78d3 | 2010-08-20 11:34:18 -0700 | [diff] [blame] | 954 | processMarkStack(ctx); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 955 | dvmSignalHeapWorker(false); |
| 956 | } |
| 957 | |
| Carl Shapiro | e8ef2b5 | 2010-12-01 19:32:05 -0800 | [diff] [blame] | 958 | /* |
| 959 | * Process reference class instances and schedule finalizations. |
| 960 | */ |
| 961 | void dvmHeapProcessReferences(Object **softReferences, bool clearSoftRefs, |
| 962 | Object **weakReferences, |
| 963 | Object **phantomReferences) |
| 964 | { |
| 965 | assert(softReferences != NULL); |
| 966 | assert(weakReferences != NULL); |
| 967 | assert(phantomReferences != NULL); |
| 968 | /* |
| 969 | * Unless we are required to clear soft references with white |
| 970 | * references, preserve some white referents. |
| 971 | */ |
| 972 | if (!clearSoftRefs) { |
| 973 | preserveSomeSoftReferences(softReferences); |
| 974 | } |
| 975 | /* |
| 976 | * Clear all remaining soft and weak references with white |
| 977 | * referents. |
| 978 | */ |
| 979 | clearWhiteReferences(softReferences); |
| 980 | clearWhiteReferences(weakReferences); |
| 981 | /* |
| 982 | * Preserve all white objects with finalize methods and schedule |
| 983 | * them for finalization. |
| 984 | */ |
| 985 | scheduleFinalizations(); |
| 986 | /* |
| 987 | * Clear all f-reachable soft and weak references with white |
| 988 | * referents. |
| 989 | */ |
| 990 | clearWhiteReferences(softReferences); |
| 991 | clearWhiteReferences(weakReferences); |
| 992 | /* |
| 993 | * Clear all phantom references with white referents. |
| 994 | */ |
| 995 | clearWhiteReferences(phantomReferences); |
| 996 | /* |
| 997 | * At this point all reference lists should be empty. |
| 998 | */ |
| 999 | assert(*softReferences == NULL); |
| 1000 | assert(*weakReferences == NULL); |
| 1001 | assert(*phantomReferences == NULL); |
| 1002 | } |
| 1003 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1004 | void dvmHeapFinishMarkStep() |
| 1005 | { |
| Carl Shapiro | b2e78d3 | 2010-08-20 11:34:18 -0700 | [diff] [blame] | 1006 | GcMarkContext *ctx; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1007 | |
| Carl Shapiro | b2e78d3 | 2010-08-20 11:34:18 -0700 | [diff] [blame] | 1008 | ctx = &gDvm.gcHeap->markContext; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1009 | |
| Barry Hayes | 81010a4 | 2010-07-19 14:07:01 -0700 | [diff] [blame] | 1010 | /* The mark bits are now not needed. |
| 1011 | */ |
| 1012 | dvmHeapSourceZeroMarkBitmap(); |
| 1013 | |
| Carl Shapiro | f373efd | 2010-02-19 00:46:33 -0800 | [diff] [blame] | 1014 | /* Clean up everything else associated with the marking process. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1015 | */ |
| Carl Shapiro | b2e78d3 | 2010-08-20 11:34:18 -0700 | [diff] [blame] | 1016 | destroyMarkStack(&ctx->stack); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1017 | |
| Carl Shapiro | b2e78d3 | 2010-08-20 11:34:18 -0700 | [diff] [blame] | 1018 | ctx->finger = NULL; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1019 | } |
| 1020 | |
| Carl Shapiro | 8881a80 | 2010-08-10 15:55:45 -0700 | [diff] [blame] | 1021 | typedef struct { |
| 1022 | size_t numObjects; |
| 1023 | size_t numBytes; |
| 1024 | bool isConcurrent; |
| 1025 | } SweepContext; |
| 1026 | |
| Carl Shapiro | 57ee270 | 2010-08-27 13:06:48 -0700 | [diff] [blame] | 1027 | static void sweepBitmapCallback(size_t numPtrs, void **ptrs, void *arg) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1028 | { |
| Carl Shapiro | fc75f3e | 2010-12-07 11:43:38 -0800 | [diff] [blame^] | 1029 | SweepContext *ctx = (SweepContext *)arg; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1030 | |
| Carl Shapiro | 8881a80 | 2010-08-10 15:55:45 -0700 | [diff] [blame] | 1031 | if (ctx->isConcurrent) { |
| 1032 | dvmLockHeap(); |
| 1033 | } |
| 1034 | ctx->numBytes += dvmHeapSourceFreeList(numPtrs, ptrs); |
| 1035 | ctx->numObjects += numPtrs; |
| 1036 | if (ctx->isConcurrent) { |
| 1037 | dvmUnlockHeap(); |
| 1038 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1039 | } |
| 1040 | |
| Carl Shapiro | 8881a80 | 2010-08-10 15:55:45 -0700 | [diff] [blame] | 1041 | /* |
| 1042 | * Returns true if the given object is unmarked. This assumes that |
| 1043 | * the bitmaps have not yet been swapped. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1044 | */ |
| 1045 | static int isUnmarkedObject(void *object) |
| 1046 | { |
| Carl Shapiro | 6343bd0 | 2010-02-16 17:40:19 -0800 | [diff] [blame] | 1047 | 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] | 1048 | &gDvm.gcHeap->markContext); |
| 1049 | } |
| 1050 | |
| Carl Shapiro | 8881a80 | 2010-08-10 15:55:45 -0700 | [diff] [blame] | 1051 | /* |
| 1052 | * Process all the internal system structures that behave like |
| 1053 | * weakly-held objects. |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1054 | */ |
| Carl Shapiro | 8881a80 | 2010-08-10 15:55:45 -0700 | [diff] [blame] | 1055 | void dvmHeapSweepSystemWeaks(void) |
| 1056 | { |
| 1057 | dvmGcDetachDeadInternedStrings(isUnmarkedObject); |
| 1058 | dvmSweepMonitorList(&gDvm.monitorList, isUnmarkedObject); |
| 1059 | } |
| 1060 | |
| 1061 | /* |
| 1062 | * Walk through the list of objects that haven't been marked and free |
| 1063 | * them. Assumes the bitmaps have been swapped. |
| 1064 | */ |
| 1065 | void dvmHeapSweepUnmarkedObjects(GcMode mode, bool isConcurrent, |
| 1066 | size_t *numObjects, size_t *numBytes) |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1067 | { |
| Carl Shapiro | 5fdab4a | 2010-08-18 21:04:31 -0700 | [diff] [blame] | 1068 | HeapBitmap currMark[HEAP_SOURCE_MAX_HEAP_COUNT]; |
| 1069 | HeapBitmap currLive[HEAP_SOURCE_MAX_HEAP_COUNT]; |
| Carl Shapiro | 8881a80 | 2010-08-10 15:55:45 -0700 | [diff] [blame] | 1070 | SweepContext ctx; |
| Carl Shapiro | d25566d | 2010-03-11 20:39:47 -0800 | [diff] [blame] | 1071 | size_t numBitmaps, numSweepBitmaps; |
| Barry Hayes | e168ebd | 2010-05-07 09:19:46 -0700 | [diff] [blame] | 1072 | size_t i; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1073 | |
| Carl Shapiro | f373efd | 2010-02-19 00:46:33 -0800 | [diff] [blame] | 1074 | numBitmaps = dvmHeapSourceGetNumHeaps(); |
| Carl Shapiro | 5fdab4a | 2010-08-18 21:04:31 -0700 | [diff] [blame] | 1075 | dvmHeapSourceGetObjectBitmaps(currLive, currMark, numBitmaps); |
| Carl Shapiro | d25566d | 2010-03-11 20:39:47 -0800 | [diff] [blame] | 1076 | if (mode == GC_PARTIAL) { |
| 1077 | numSweepBitmaps = 1; |
| Carl Shapiro | 5fdab4a | 2010-08-18 21:04:31 -0700 | [diff] [blame] | 1078 | assert((uintptr_t)gDvm.gcHeap->markContext.immuneLimit == currLive[0].base); |
| Carl Shapiro | d25566d | 2010-03-11 20:39:47 -0800 | [diff] [blame] | 1079 | } else { |
| 1080 | numSweepBitmaps = numBitmaps; |
| 1081 | } |
| Carl Shapiro | 8881a80 | 2010-08-10 15:55:45 -0700 | [diff] [blame] | 1082 | ctx.numObjects = ctx.numBytes = 0; |
| 1083 | ctx.isConcurrent = isConcurrent; |
| Barry Hayes | e168ebd | 2010-05-07 09:19:46 -0700 | [diff] [blame] | 1084 | for (i = 0; i < numSweepBitmaps; i++) { |
| Carl Shapiro | 5fdab4a | 2010-08-18 21:04:31 -0700 | [diff] [blame] | 1085 | HeapBitmap* prevLive = &currMark[i]; |
| 1086 | HeapBitmap* prevMark = &currLive[i]; |
| 1087 | dvmHeapBitmapSweepWalk(prevLive, prevMark, sweepBitmapCallback, &ctx); |
| Barry Hayes | e168ebd | 2010-05-07 09:19:46 -0700 | [diff] [blame] | 1088 | } |
| Carl Shapiro | 8881a80 | 2010-08-10 15:55:45 -0700 | [diff] [blame] | 1089 | *numObjects = ctx.numObjects; |
| 1090 | *numBytes = ctx.numBytes; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1091 | if (gDvm.allocProf.enabled) { |
| Carl Shapiro | 8881a80 | 2010-08-10 15:55:45 -0700 | [diff] [blame] | 1092 | gDvm.allocProf.freeCount += ctx.numObjects; |
| 1093 | gDvm.allocProf.freeSize += ctx.numBytes; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1094 | } |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1095 | } |