blob: 429665fc6d7ddfd752beed9fd0c382cd776f1586 [file] [log] [blame]
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "Dalvik.h"
Barry Hayeseac47ed2009-06-22 11:45:20 -070018#include "alloc/clz.h"
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080019#include "alloc/HeapBitmap.h"
20#include "alloc/HeapInternal.h"
21#include "alloc/HeapSource.h"
22#include "alloc/MarkSweep.h"
23#include <limits.h> // for ULONG_MAX
24#include <sys/mman.h> // for madvise(), mmap()
25#include <cutils/ashmem.h>
The Android Open Source Project99409882009-03-18 22:20:24 -070026#include <errno.h>
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080027
28#define GC_DEBUG_PARANOID 2
29#define GC_DEBUG_BASIC 1
30#define GC_DEBUG_OFF 0
31#define GC_DEBUG(l) (GC_DEBUG_LEVEL >= (l))
32
33#if 1
34#define GC_DEBUG_LEVEL GC_DEBUG_PARANOID
35#else
36#define GC_DEBUG_LEVEL GC_DEBUG_OFF
37#endif
38
39#define VERBOSE_GC 0
40
41#define GC_LOG_TAG LOG_TAG "-gc"
42
43#if LOG_NDEBUG
44#define LOGV_GC(...) ((void)0)
45#define LOGD_GC(...) ((void)0)
46#else
47#define LOGV_GC(...) LOG(LOG_VERBOSE, GC_LOG_TAG, __VA_ARGS__)
48#define LOGD_GC(...) LOG(LOG_DEBUG, GC_LOG_TAG, __VA_ARGS__)
49#endif
50
51#if VERBOSE_GC
52#define LOGVV_GC(...) LOGV_GC(__VA_ARGS__)
53#else
54#define LOGVV_GC(...) ((void)0)
55#endif
56
57#define LOGI_GC(...) LOG(LOG_INFO, GC_LOG_TAG, __VA_ARGS__)
58#define LOGW_GC(...) LOG(LOG_WARN, GC_LOG_TAG, __VA_ARGS__)
59#define LOGE_GC(...) LOG(LOG_ERROR, GC_LOG_TAG, __VA_ARGS__)
60
61#define LOG_SCAN(...) LOGV_GC("SCAN: " __VA_ARGS__)
62#define LOG_MARK(...) LOGV_GC("MARK: " __VA_ARGS__)
63#define LOG_SWEEP(...) LOGV_GC("SWEEP: " __VA_ARGS__)
64#define LOG_REF(...) LOGV_GC("REF: " __VA_ARGS__)
65
66#define LOGV_SCAN(...) LOGVV_GC("SCAN: " __VA_ARGS__)
67#define LOGV_MARK(...) LOGVV_GC("MARK: " __VA_ARGS__)
68#define LOGV_SWEEP(...) LOGVV_GC("SWEEP: " __VA_ARGS__)
69#define LOGV_REF(...) LOGVV_GC("REF: " __VA_ARGS__)
70
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080071#define ALIGN_UP_TO_PAGE_SIZE(p) \
Andy McFadden96516932009-10-28 17:39:02 -070072 (((size_t)(p) + (SYSTEM_PAGE_SIZE - 1)) & ~(SYSTEM_PAGE_SIZE - 1))
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080073
74/* Do not cast the result of this to a boolean; the only set bit
75 * may be > 1<<8.
76 */
77static inline long isMarked(const DvmHeapChunk *hc, const GcMarkContext *ctx)
78 __attribute__((always_inline));
79static inline long isMarked(const DvmHeapChunk *hc, const GcMarkContext *ctx)
80{
81 return dvmHeapBitmapIsObjectBitSetInList(ctx->bitmaps, ctx->numBitmaps, hc);
82}
83
84static bool
85createMarkStack(GcMarkStack *stack)
86{
87 const Object **limit;
88 size_t size;
The Android Open Source Project99409882009-03-18 22:20:24 -070089 int fd, err;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -080090
91 /* Create a stack big enough for the worst possible case,
92 * where the heap is perfectly full of the smallest object.
93 * TODO: be better about memory usage; use a smaller stack with
94 * overflow detection and recovery.
95 */
96 size = dvmHeapSourceGetIdealFootprint() * sizeof(Object*) /
97 (sizeof(Object) + HEAP_SOURCE_CHUNK_OVERHEAD);
98 size = ALIGN_UP_TO_PAGE_SIZE(size);
99 fd = ashmem_create_region("dalvik-heap-markstack", size);
100 if (fd < 0) {
The Android Open Source Project99409882009-03-18 22:20:24 -0700101 LOGE_GC("Could not create %d-byte ashmem mark stack: %s\n",
102 size, strerror(errno));
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800103 return false;
104 }
105 limit = (const Object **)mmap(NULL, size, PROT_READ | PROT_WRITE,
106 MAP_PRIVATE, fd, 0);
The Android Open Source Project99409882009-03-18 22:20:24 -0700107 err = errno;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800108 close(fd);
109 if (limit == MAP_FAILED) {
The Android Open Source Project99409882009-03-18 22:20:24 -0700110 LOGE_GC("Could not mmap %d-byte ashmem mark stack: %s\n",
111 size, strerror(err));
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800112 return false;
113 }
114
115 memset(stack, 0, sizeof(*stack));
116 stack->limit = limit;
117 stack->base = (const Object **)((uintptr_t)limit + size);
118 stack->top = stack->base;
119
120 return true;
121}
122
123static void
124destroyMarkStack(GcMarkStack *stack)
125{
126 munmap((char *)stack->limit,
127 (uintptr_t)stack->base - (uintptr_t)stack->limit);
128 memset(stack, 0, sizeof(*stack));
129}
130
131#define MARK_STACK_PUSH(stack, obj) \
132 do { \
133 *--(stack).top = (obj); \
134 } while (false)
135
136bool
137dvmHeapBeginMarkStep()
138{
139 GcMarkContext *mc = &gDvm.gcHeap->markContext;
140 HeapBitmap objectBitmaps[HEAP_SOURCE_MAX_HEAP_COUNT];
141 size_t numBitmaps;
142
143 if (!createMarkStack(&mc->stack)) {
144 return false;
145 }
146
147 numBitmaps = dvmHeapSourceGetObjectBitmaps(objectBitmaps,
148 HEAP_SOURCE_MAX_HEAP_COUNT);
149 if (numBitmaps <= 0) {
150 return false;
151 }
152
153 /* Create mark bitmaps that cover the same ranges as the
154 * current object bitmaps.
155 */
156 if (!dvmHeapBitmapInitListFromTemplates(mc->bitmaps, objectBitmaps,
157 numBitmaps, "mark"))
158 {
159 return false;
160 }
161
162 mc->numBitmaps = numBitmaps;
163 mc->finger = NULL;
164
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800165 return true;
166}
167
168static long setAndReturnMarkBit(GcMarkContext *ctx, const DvmHeapChunk *hc)
169 __attribute__((always_inline));
170static long
171setAndReturnMarkBit(GcMarkContext *ctx, const DvmHeapChunk *hc)
172{
173 return dvmHeapBitmapSetAndReturnObjectBitInList(ctx->bitmaps,
174 ctx->numBitmaps, hc);
175}
176
177static void _markObjectNonNullCommon(const Object *obj, GcMarkContext *ctx,
178 bool checkFinger, bool forceStack)
179 __attribute__((always_inline));
180static void
181_markObjectNonNullCommon(const Object *obj, GcMarkContext *ctx,
182 bool checkFinger, bool forceStack)
183{
184 DvmHeapChunk *hc;
185
186 assert(obj != NULL);
187
188#if GC_DEBUG(GC_DEBUG_PARANOID)
189//TODO: make sure we're locked
190 assert(obj != (Object *)gDvm.unlinkedJavaLangClass);
191 assert(dvmIsValidObject(obj));
192#endif
193
194 hc = ptr2chunk(obj);
195 if (!setAndReturnMarkBit(ctx, hc)) {
196 /* This object was not previously marked.
197 */
198 if (forceStack || (checkFinger && (void *)hc < ctx->finger)) {
199 /* This object will need to go on the mark stack.
200 */
201 MARK_STACK_PUSH(ctx->stack, obj);
202 }
203
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800204#if WITH_HPROF
205 if (gDvm.gcHeap->hprofContext != NULL) {
206 hprofMarkRootObject(gDvm.gcHeap->hprofContext, obj, 0);
207 }
208#endif
209#if DVM_TRACK_HEAP_MARKING
210 gDvm.gcHeap->markCount++;
211 gDvm.gcHeap->markSize += dvmHeapSourceChunkSize((void *)hc) +
212 HEAP_SOURCE_CHUNK_OVERHEAD;
213#endif
214
215 /* obj->clazz can be NULL if we catch an object between
216 * dvmMalloc() and DVM_OBJECT_INIT(). This is ok.
217 */
218 LOGV_MARK("0x%08x %s\n", (uint)obj,
219 obj->clazz == NULL ? "<null class>" : obj->clazz->name);
220 }
221}
222
223/* Used to mark objects when recursing. Recursion is done by moving
224 * the finger across the bitmaps in address order and marking child
225 * objects. Any newly-marked objects whose addresses are lower than
226 * the finger won't be visited by the bitmap scan, so those objects
227 * need to be added to the mark stack.
228 */
229static void
230markObjectNonNull(const Object *obj, GcMarkContext *ctx)
231{
232 _markObjectNonNullCommon(obj, ctx, true, false);
233}
234
235#define markObject(obj, ctx) \
236 do { \
237 Object *MO_obj_ = (Object *)(obj); \
238 if (MO_obj_ != NULL) { \
239 markObjectNonNull(MO_obj_, (ctx)); \
240 } \
241 } while (false)
242
243/* If the object hasn't already been marked, mark it and
244 * schedule it to be scanned for references.
245 *
246 * obj may not be NULL. The macro dvmMarkObject() should
247 * be used in situations where a reference may be NULL.
248 *
249 * This function may only be called when marking the root
250 * set. When recursing, use the internal markObject[NonNull]().
251 */
252void
253dvmMarkObjectNonNull(const Object *obj)
254{
255 _markObjectNonNullCommon(obj, &gDvm.gcHeap->markContext, false, false);
256}
257
258/* Mark the set of root objects.
259 *
260 * Things we need to scan:
261 * - System classes defined by root classloader
262 * - For each thread:
263 * - Interpreted stack, from top to "curFrame"
264 * - Dalvik registers (args + local vars)
265 * - JNI local references
266 * - Automatic VM local references (TrackedAlloc)
267 * - Associated Thread/VMThread object
268 * - ThreadGroups (could track & start with these instead of working
269 * upward from Threads)
270 * - Exception currently being thrown, if present
271 * - JNI global references
272 * - Interned string table
273 * - Primitive classes
274 * - Special objects
275 * - gDvm.outOfMemoryObj
276 * - Objects allocated with ALLOC_NO_GC
277 * - Objects pending finalization (but not yet finalized)
278 * - Objects in debugger object registry
279 *
280 * Don't need:
281 * - Native stack (for in-progress stuff in the VM)
282 * - The TrackedAlloc stuff watches all native VM references.
283 */
284void dvmHeapMarkRootSet()
285{
286 HeapRefTable *refs;
287 GcHeap *gcHeap;
288 Object **op;
289
290 gcHeap = gDvm.gcHeap;
291
292 HPROF_SET_GC_SCAN_STATE(HPROF_ROOT_STICKY_CLASS, 0);
293
294 LOG_SCAN("root class loader\n");
295 dvmGcScanRootClassLoader();
296 LOG_SCAN("primitive classes\n");
297 dvmGcScanPrimitiveClasses();
298
299 /* dvmGcScanRootThreadGroups() sets a bunch of
300 * different scan states internally.
301 */
302 HPROF_CLEAR_GC_SCAN_STATE();
303
304 LOG_SCAN("root thread groups\n");
305 dvmGcScanRootThreadGroups();
306
307 HPROF_SET_GC_SCAN_STATE(HPROF_ROOT_INTERNED_STRING, 0);
308
309 LOG_SCAN("interned strings\n");
310 dvmGcScanInternedStrings();
311
312 HPROF_SET_GC_SCAN_STATE(HPROF_ROOT_JNI_GLOBAL, 0);
313
314 LOG_SCAN("JNI global refs\n");
315 dvmGcMarkJniGlobalRefs();
316
317 HPROF_SET_GC_SCAN_STATE(HPROF_ROOT_REFERENCE_CLEANUP, 0);
318
319 LOG_SCAN("pending reference operations\n");
320 dvmHeapMarkLargeTableRefs(gcHeap->referenceOperations, true);
321
322 HPROF_SET_GC_SCAN_STATE(HPROF_ROOT_FINALIZING, 0);
323
324 LOG_SCAN("pending finalizations\n");
325 dvmHeapMarkLargeTableRefs(gcHeap->pendingFinalizationRefs, false);
326
327 HPROF_SET_GC_SCAN_STATE(HPROF_ROOT_DEBUGGER, 0);
328
329 LOG_SCAN("debugger refs\n");
330 dvmGcMarkDebuggerRefs();
331
332 HPROF_SET_GC_SCAN_STATE(HPROF_ROOT_VM_INTERNAL, 0);
333
334 /* Mark all ALLOC_NO_GC objects.
335 */
336 LOG_SCAN("ALLOC_NO_GC objects\n");
337 refs = &gcHeap->nonCollectableRefs;
338 op = refs->table;
339 while ((uintptr_t)op < (uintptr_t)refs->nextEntry) {
340 dvmMarkObjectNonNull(*(op++));
341 }
342
343 /* Mark any special objects we have sitting around.
344 */
345 LOG_SCAN("special objects\n");
346 dvmMarkObjectNonNull(gDvm.outOfMemoryObj);
347 dvmMarkObjectNonNull(gDvm.internalErrorObj);
Andy McFadden7fc3ce82009-07-14 15:57:23 -0700348 dvmMarkObjectNonNull(gDvm.noClassDefFoundErrorObj);
Andy McFaddenb18992f2009-09-25 10:42:15 -0700349 dvmMarkObject(gDvm.jniWeakGlobalRefQueue);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800350//TODO: scan object references sitting in gDvm; use pointer begin & end
351
352 HPROF_CLEAR_GC_SCAN_STATE();
353}
354
355/*
356 * Nothing past this point is allowed to use dvmMarkObject*().
357 * Scanning/recursion must use markObject*(), which takes the
358 * finger into account.
359 */
360#define dvmMarkObjectNonNull __dont_use_dvmMarkObjectNonNull__
361
362
363/* Mark all of a ClassObject's interfaces.
364 */
365static void markInterfaces(const ClassObject *clazz, GcMarkContext *ctx)
366{
367 ClassObject **interfaces;
368 int interfaceCount;
369 int i;
370
371 /* Mark all interfaces.
372 */
373 interfaces = clazz->interfaces;
374 interfaceCount = clazz->interfaceCount;
375 for (i = 0; i < interfaceCount; i++) {
376 markObjectNonNull((Object *)*interfaces, ctx);
377 interfaces++;
378 }
379}
380
381/* Mark all objects referred to by a ClassObject's static fields.
382 */
383static void scanStaticFields(const ClassObject *clazz, GcMarkContext *ctx)
384{
385 StaticField *f;
386 int i;
387
388 //TODO: Optimize this with a bit vector or something
389 f = clazz->sfields;
390 for (i = 0; i < clazz->sfieldCount; i++) {
391 char c = f->field.signature[0];
392 if (c == '[' || c == 'L') {
393 /* It's an array or class reference.
394 */
395 markObject((Object *)f->value.l, ctx);
396 }
397 f++;
398 }
399}
400
401/* Mark all objects referred to by a DataObject's instance fields.
402 */
403static void scanInstanceFields(const DataObject *obj, ClassObject *clazz,
404 GcMarkContext *ctx)
405{
Barry Hayesd1497482009-10-28 15:32:19 -0700406 if (clazz->refOffsets != CLASS_WALK_SUPER) {
Barry Hayeseac47ed2009-06-22 11:45:20 -0700407 unsigned int refOffsets = clazz->refOffsets;
408 while (refOffsets != 0) {
409 const int rshift = CLZ(refOffsets);
410 refOffsets &= ~(CLASS_HIGH_BIT >> rshift);
411 markObject(dvmGetFieldObject((Object*)obj,
412 CLASS_OFFSET_FROM_CLZ(rshift)), ctx);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800413 }
Barry Hayeseac47ed2009-06-22 11:45:20 -0700414 } else {
415 while (clazz != NULL) {
416 InstField *f;
417 int i;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800418
Barry Hayeseac47ed2009-06-22 11:45:20 -0700419 /* All of the fields that contain object references
420 * are guaranteed to be at the beginning of the ifields list.
421 */
422 f = clazz->ifields;
423 for (i = 0; i < clazz->ifieldRefCount; i++) {
424 /* Mark the array or object reference.
425 * May be NULL.
426 *
427 * Note that, per the comment on struct InstField,
428 * f->byteOffset is the offset from the beginning of
429 * obj, not the offset into obj->instanceData.
430 */
431 markObject(dvmGetFieldObject((Object*)obj, f->byteOffset), ctx);
432 f++;
433 }
434
435 /* This will be NULL when we hit java.lang.Object
436 */
437 clazz = clazz->super;
438 }
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800439 }
440}
441
442/* Mark all objects referred to by the array's contents.
443 */
444static void scanObjectArray(const ArrayObject *array, GcMarkContext *ctx)
445{
446 Object **contents;
447 u4 length;
448 u4 i;
449
450 contents = (Object **)array->contents;
451 length = array->length;
452
453 for (i = 0; i < length; i++) {
454 markObject(*contents, ctx); // may be NULL
455 contents++;
456 }
457}
458
459/* Mark all objects referred to by the ClassObject.
460 */
461static void scanClassObject(const ClassObject *clazz, GcMarkContext *ctx)
462{
463 LOGV_SCAN("---------> %s\n", clazz->name);
464
465 if (IS_CLASS_FLAG_SET(clazz, CLASS_ISARRAY)) {
466 /* We're an array; mark the class object of the contents
467 * of the array.
468 *
469 * Note that we won't necessarily reach the array's element
470 * class by scanning the array contents; the array may be
471 * zero-length, or may only contain null objects.
472 */
473 markObjectNonNull((Object *)clazz->elementClass, ctx);
474 }
475
476 /* We scan these explicitly in case the only remaining
477 * reference to a particular class object is via a data
478 * object; we may not be guaranteed to reach all
479 * live class objects via a classloader.
480 */
481 markObject((Object *)clazz->super, ctx); // may be NULL (java.lang.Object)
482 markObject(clazz->classLoader, ctx); // may be NULL
483
484 scanStaticFields(clazz, ctx);
485 markInterfaces(clazz, ctx);
486}
487
488/* Mark all objects that obj refers to.
489 *
490 * Called on every object in markList.
491 */
492static void scanObject(const Object *obj, GcMarkContext *ctx)
493{
494 ClassObject *clazz;
495
496 assert(dvmIsValidObject(obj));
497 LOGV_SCAN("0x%08x %s\n", (uint)obj, obj->clazz->name);
498
499#if WITH_HPROF
500 if (gDvm.gcHeap->hprofContext != NULL) {
501 hprofDumpHeapObject(gDvm.gcHeap->hprofContext, obj);
502 }
503#endif
504
505 /* Get and mark the class object for this particular instance.
506 */
507 clazz = obj->clazz;
508 if (clazz == NULL) {
509 /* This can happen if we catch an object between
510 * dvmMalloc() and DVM_OBJECT_INIT(). The object
511 * won't contain any references yet, so we can
512 * just skip it.
513 */
514 return;
515 } else if (clazz == gDvm.unlinkedJavaLangClass) {
516 /* This class hasn't been linked yet. We're guaranteed
517 * that the object doesn't contain any references that
518 * aren't already tracked, so we can skip scanning it.
519 *
520 * NOTE: unlinkedJavaLangClass is not on the heap, so
521 * it's very important that we don't try marking it.
522 */
523 return;
524 }
Barry Hayes3592d622009-03-16 16:10:35 -0700525
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800526 assert(dvmIsValidObject((Object *)clazz));
527 markObjectNonNull((Object *)clazz, ctx);
528
529 /* Mark any references in this object.
530 */
531 if (IS_CLASS_FLAG_SET(clazz, CLASS_ISARRAY)) {
532 /* It's an array object.
533 */
534 if (IS_CLASS_FLAG_SET(clazz, CLASS_ISOBJECTARRAY)) {
535 /* It's an array of object references.
536 */
537 scanObjectArray((ArrayObject *)obj, ctx);
538 }
539 // else there's nothing else to scan
540 } else {
541 /* It's a DataObject-compatible object.
542 */
543 scanInstanceFields((DataObject *)obj, clazz, ctx);
544
545 if (IS_CLASS_FLAG_SET(clazz, CLASS_ISREFERENCE)) {
546 GcHeap *gcHeap = gDvm.gcHeap;
547 Object *referent;
548
549 /* It's a subclass of java/lang/ref/Reference.
550 * The fields in this class have been arranged
551 * such that scanInstanceFields() did not actually
552 * mark the "referent" field; we need to handle
553 * it specially.
554 *
555 * If the referent already has a strong mark (isMarked(referent)),
556 * we don't care about its reference status.
557 */
558 referent = dvmGetFieldObject(obj,
559 gDvm.offJavaLangRefReference_referent);
560 if (referent != NULL &&
561 !isMarked(ptr2chunk(referent), &gcHeap->markContext))
562 {
563 u4 refFlags;
564
565 if (gcHeap->markAllReferents) {
566 LOG_REF("Hard-marking a reference\n");
567
568 /* Don't bother with normal reference-following
569 * behavior, just mark the referent. This should
570 * only be used when following objects that just
571 * became scheduled for finalization.
572 */
573 markObjectNonNull(referent, ctx);
574 goto skip_reference;
575 }
576
577 /* See if this reference was handled by a previous GC.
578 */
579 if (dvmGetFieldObject(obj,
580 gDvm.offJavaLangRefReference_vmData) ==
581 SCHEDULED_REFERENCE_MAGIC)
582 {
583 LOG_REF("Skipping scheduled reference\n");
584
585 /* Don't reschedule it, but make sure that its
586 * referent doesn't get collected (in case it's
587 * a PhantomReference and wasn't cleared automatically).
588 */
589 //TODO: Mark these after handling all new refs of
590 // this strength, in case the new refs refer
591 // to the same referent. Not a very common
592 // case, though.
593 markObjectNonNull(referent, ctx);
594 goto skip_reference;
595 }
596
597 /* Find out what kind of reference is pointing
598 * to referent.
599 */
600 refFlags = GET_CLASS_FLAG_GROUP(clazz,
601 CLASS_ISREFERENCE |
602 CLASS_ISWEAKREFERENCE |
603 CLASS_ISPHANTOMREFERENCE);
604
605 /* We use the vmData field of Reference objects
606 * as a next pointer in a singly-linked list.
607 * That way, we don't need to allocate any memory
608 * while we're doing a GC.
609 */
610#define ADD_REF_TO_LIST(list, ref) \
611 do { \
612 Object *ARTL_ref_ = (/*de-const*/Object *)(ref); \
613 dvmSetFieldObject(ARTL_ref_, \
614 gDvm.offJavaLangRefReference_vmData, list); \
615 list = ARTL_ref_; \
616 } while (false)
617
618 /* At this stage, we just keep track of all of
619 * the live references that we've seen. Later,
620 * we'll walk through each of these lists and
621 * deal with the referents.
622 */
623 if (refFlags == CLASS_ISREFERENCE) {
624 /* It's a soft reference. Depending on the state,
625 * we'll attempt to collect all of them, some of
626 * them, or none of them.
627 */
628 if (gcHeap->softReferenceCollectionState ==
629 SR_COLLECT_NONE)
630 {
631 sr_collect_none:
632 markObjectNonNull(referent, ctx);
633 } else if (gcHeap->softReferenceCollectionState ==
634 SR_COLLECT_ALL)
635 {
636 sr_collect_all:
637 ADD_REF_TO_LIST(gcHeap->softReferences, obj);
638 } else {
639 /* We'll only try to collect half of the
640 * referents.
641 */
642 if (gcHeap->softReferenceColor++ & 1) {
643 goto sr_collect_none;
644 }
645 goto sr_collect_all;
646 }
647 } else {
648 /* It's a weak or phantom reference.
649 * Clearing CLASS_ISREFERENCE will reveal which.
650 */
651 refFlags &= ~CLASS_ISREFERENCE;
652 if (refFlags == CLASS_ISWEAKREFERENCE) {
653 ADD_REF_TO_LIST(gcHeap->weakReferences, obj);
654 } else if (refFlags == CLASS_ISPHANTOMREFERENCE) {
655 ADD_REF_TO_LIST(gcHeap->phantomReferences, obj);
656 } else {
657 assert(!"Unknown reference type");
658 }
659 }
660#undef ADD_REF_TO_LIST
661 }
662 }
663
664 skip_reference:
665 /* If this is a class object, mark various other things that
666 * its internals point to.
667 *
668 * All class objects are instances of java.lang.Class,
669 * including the java.lang.Class class object.
670 */
671 if (clazz == gDvm.classJavaLangClass) {
672 scanClassObject((ClassObject *)obj, ctx);
673 }
674 }
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800675}
676
677static void
678processMarkStack(GcMarkContext *ctx)
679{
680 const Object **const base = ctx->stack.base;
681
682 /* Scan anything that's on the mark stack.
683 * We can't use the bitmaps anymore, so use
684 * a finger that points past the end of them.
685 */
686 ctx->finger = (void *)ULONG_MAX;
687 while (ctx->stack.top != base) {
688 scanObject(*ctx->stack.top++, ctx);
689 }
690}
691
692#ifndef NDEBUG
693static uintptr_t gLastFinger = 0;
694#endif
695
696static bool
697scanBitmapCallback(size_t numPtrs, void **ptrs, const void *finger, void *arg)
698{
699 GcMarkContext *ctx = (GcMarkContext *)arg;
700 size_t i;
701
702#ifndef NDEBUG
703 assert((uintptr_t)finger >= gLastFinger);
704 gLastFinger = (uintptr_t)finger;
705#endif
706
707 ctx->finger = finger;
708 for (i = 0; i < numPtrs; i++) {
709 /* The pointers we're getting back are DvmHeapChunks,
710 * not Objects.
711 */
712 scanObject(chunk2ptr(*ptrs++), ctx);
713 }
714
715 return true;
716}
717
718/* Given bitmaps with the root set marked, find and mark all
719 * reachable objects. When this returns, the entire set of
720 * live objects will be marked and the mark stack will be empty.
721 */
722void dvmHeapScanMarkedObjects()
723{
724 GcMarkContext *ctx = &gDvm.gcHeap->markContext;
725
726 assert(ctx->finger == NULL);
727
728 /* The bitmaps currently have bits set for the root set.
729 * Walk across the bitmaps and scan each object.
730 */
731#ifndef NDEBUG
732 gLastFinger = 0;
733#endif
734 dvmHeapBitmapWalkList(ctx->bitmaps, ctx->numBitmaps,
735 scanBitmapCallback, ctx);
736
737 /* We've walked the mark bitmaps. Scan anything that's
738 * left on the mark stack.
739 */
740 processMarkStack(ctx);
741
742 LOG_SCAN("done with marked objects\n");
743}
744
745/** @return true if we need to schedule a call to clear().
746 */
747static bool clearReference(Object *reference)
748{
749 /* This is what the default implementation of Reference.clear()
750 * does. We're required to clear all references to a given
751 * referent atomically, so we can't pop in and out of interp
752 * code each time.
753 *
754 * Also, someone may have subclassed one of the basic Reference
755 * types, overriding clear(). We can't trust the clear()
756 * implementation to call super.clear(); we cannot let clear()
757 * resurrect the referent. If we clear it here, we can safely
758 * call any overriding implementations.
759 */
760 dvmSetFieldObject(reference,
761 gDvm.offJavaLangRefReference_referent, NULL);
762
763#if FANCY_REFERENCE_SUBCLASS
764 /* See if clear() has actually been overridden. If so,
765 * we need to schedule a call to it before calling enqueue().
766 */
767 if (reference->clazz->vtable[gDvm.voffJavaLangRefReference_clear]->clazz !=
768 gDvm.classJavaLangRefReference)
769 {
770 /* clear() has been overridden; return true to indicate
771 * that we need to schedule a call to the real clear()
772 * implementation.
773 */
774 return true;
775 }
776#endif
777
778 return false;
779}
780
781/** @return true if we need to schedule a call to enqueue().
782 */
783static bool enqueueReference(Object *reference)
784{
785#if FANCY_REFERENCE_SUBCLASS
786 /* See if this reference class has overridden enqueue();
787 * if not, we can take a shortcut.
788 */
789 if (reference->clazz->vtable[gDvm.voffJavaLangRefReference_enqueue]->clazz
790 == gDvm.classJavaLangRefReference)
791#endif
792 {
793 Object *queue = dvmGetFieldObject(reference,
794 gDvm.offJavaLangRefReference_queue);
795 Object *queueNext = dvmGetFieldObject(reference,
796 gDvm.offJavaLangRefReference_queueNext);
797 if (queue == NULL || queueNext != NULL) {
798 /* There is no queue, or the reference has already
799 * been enqueued. The Reference.enqueue() method
800 * will do nothing even if we call it.
801 */
802 return false;
803 }
804 }
805
806 /* We need to call enqueue(), but if we called it from
807 * here we'd probably deadlock. Schedule a call.
808 */
809 return true;
810}
811
812/* All objects for stronger reference levels have been
813 * marked before this is called.
814 */
815void dvmHeapHandleReferences(Object *refListHead, enum RefType refType)
816{
817 Object *reference;
818 GcMarkContext *markContext = &gDvm.gcHeap->markContext;
819 const int offVmData = gDvm.offJavaLangRefReference_vmData;
820 const int offReferent = gDvm.offJavaLangRefReference_referent;
821 bool workRequired = false;
822
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800823 reference = refListHead;
824 while (reference != NULL) {
825 Object *next;
826 Object *referent;
827
828 /* Pull the interesting fields out of the Reference object.
829 */
830 next = dvmGetFieldObject(reference, offVmData);
831 referent = dvmGetFieldObject(reference, offReferent);
832
833 //TODO: when handling REF_PHANTOM, unlink any references
834 // that fail this initial if(). We need to re-walk
835 // the list, and it would be nice to avoid the extra
836 // work.
837 if (referent != NULL && !isMarked(ptr2chunk(referent), markContext)) {
838 bool schedClear, schedEnqueue;
839
840 /* This is the strongest reference that refers to referent.
841 * Do the right thing.
842 */
843 switch (refType) {
844 case REF_SOFT:
845 case REF_WEAK:
846 schedClear = clearReference(reference);
847 schedEnqueue = enqueueReference(reference);
848 break;
849 case REF_PHANTOM:
850 /* PhantomReferences are not cleared automatically.
851 * Until someone clears it (or the reference itself
852 * is collected), the referent must remain alive.
853 *
854 * It's necessary to fully mark the referent because
855 * it will still be present during the next GC, and
856 * all objects that it points to must be valid.
857 * (The referent will be marked outside of this loop,
858 * after handing all references of this strength, in
859 * case multiple references point to the same object.)
Andy McFaddenb18992f2009-09-25 10:42:15 -0700860 *
861 * One exception: JNI "weak global" references are handled
862 * as a special case. They're identified by the queue.
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800863 */
Andy McFaddenb18992f2009-09-25 10:42:15 -0700864 if (gDvm.jniWeakGlobalRefQueue != NULL) {
865 Object* queue = dvmGetFieldObject(reference,
866 gDvm.offJavaLangRefReference_queue);
867 if (queue == gDvm.jniWeakGlobalRefQueue) {
868 LOGV("+++ WGR: clearing + not queueing %p:%p\n",
869 reference, referent);
870 schedClear = clearReference(reference);
871 schedEnqueue = false;
872 break;
873 }
874 }
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800875 schedClear = false;
876
877 /* A PhantomReference is only useful with a
878 * queue, but since it's possible to create one
879 * without a queue, we need to check.
880 */
881 schedEnqueue = enqueueReference(reference);
882 break;
883 default:
884 assert(!"Bad reference type");
885 schedClear = false;
886 schedEnqueue = false;
887 break;
888 }
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800889
890 if (schedClear || schedEnqueue) {
891 uintptr_t workBits;
892
893 /* Stuff the clear/enqueue bits in the bottom of
894 * the pointer. Assumes that objects are 8-byte
895 * aligned.
896 *
897 * Note that we are adding the *Reference* (which
898 * is by definition already marked at this point) to
899 * this list; we're not adding the referent (which
900 * has already been cleared).
901 */
902 assert(((intptr_t)reference & 3) == 0);
903 assert(((WORKER_CLEAR | WORKER_ENQUEUE) & ~3) == 0);
904 workBits = (schedClear ? WORKER_CLEAR : 0) |
905 (schedEnqueue ? WORKER_ENQUEUE : 0);
906 if (!dvmHeapAddRefToLargeTable(
907 &gDvm.gcHeap->referenceOperations,
908 (Object *)((uintptr_t)reference | workBits)))
909 {
910 LOGE_HEAP("dvmMalloc(): no room for any more "
911 "reference operations\n");
912 dvmAbort();
913 }
914 workRequired = true;
915 }
916
917 if (refType != REF_PHANTOM) {
918 /* Let later GCs know not to reschedule this reference.
919 */
920 dvmSetFieldObject(reference, offVmData,
921 SCHEDULED_REFERENCE_MAGIC);
922 } // else this is handled later for REF_PHANTOM
923
924 } // else there was a stronger reference to the referent.
925
926 reference = next;
927 }
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800928
929 /* Walk though the reference list again, and mark any non-clear/marked
930 * referents. Only PhantomReferences can have non-clear referents
931 * at this point.
Andy McFaddenb18992f2009-09-25 10:42:15 -0700932 *
933 * (Could skip this for JNI weak globals, since we know they've been
934 * cleared.)
The Android Open Source Projectf6c38712009-03-03 19:28:47 -0800935 */
936 if (refType == REF_PHANTOM) {
937 bool scanRequired = false;
938
939 HPROF_SET_GC_SCAN_STATE(HPROF_ROOT_REFERENCE_CLEANUP, 0);
940 reference = refListHead;
941 while (reference != NULL) {
942 Object *next;
943 Object *referent;
944
945 /* Pull the interesting fields out of the Reference object.
946 */
947 next = dvmGetFieldObject(reference, offVmData);
948 referent = dvmGetFieldObject(reference, offReferent);
949
950 if (referent != NULL && !isMarked(ptr2chunk(referent), markContext)) {
951 markObjectNonNull(referent, markContext);
952 scanRequired = true;
953
954 /* Let later GCs know not to reschedule this reference.
955 */
956 dvmSetFieldObject(reference, offVmData,
957 SCHEDULED_REFERENCE_MAGIC);
958 }
959
960 reference = next;
961 }
962 HPROF_CLEAR_GC_SCAN_STATE();
963
964 if (scanRequired) {
965 processMarkStack(markContext);
966 }
967 }
968
969 if (workRequired) {
970 dvmSignalHeapWorker(false);
971 }
972}
973
974
975/* Find unreachable objects that need to be finalized,
976 * and schedule them for finalization.
977 */
978void dvmHeapScheduleFinalizations()
979{
980 HeapRefTable newPendingRefs;
981 LargeHeapRefTable *finRefs = gDvm.gcHeap->finalizableRefs;
982 Object **ref;
983 Object **lastRef;
984 size_t totalPendCount;
985 GcMarkContext *markContext = &gDvm.gcHeap->markContext;
986
987 /*
988 * All reachable objects have been marked.
989 * Any unmarked finalizable objects need to be finalized.
990 */
991
992 /* Create a table that the new pending refs will
993 * be added to.
994 */
995 if (!dvmHeapInitHeapRefTable(&newPendingRefs, 128)) {
996 //TODO: mark all finalizable refs and hope that
997 // we can schedule them next time. Watch out,
998 // because we may be expecting to free up space
999 // by calling finalizers.
1000 LOGE_GC("dvmHeapScheduleFinalizations(): no room for "
1001 "pending finalizations\n");
1002 dvmAbort();
1003 }
1004
1005 /* Walk through finalizableRefs and move any unmarked references
1006 * to the list of new pending refs.
1007 */
1008 totalPendCount = 0;
1009 while (finRefs != NULL) {
1010 Object **gapRef;
1011 size_t newPendCount = 0;
1012
1013 gapRef = ref = finRefs->refs.table;
1014 lastRef = finRefs->refs.nextEntry;
1015 while (ref < lastRef) {
1016 DvmHeapChunk *hc;
1017
1018 hc = ptr2chunk(*ref);
1019 if (!isMarked(hc, markContext)) {
1020 if (!dvmHeapAddToHeapRefTable(&newPendingRefs, *ref)) {
1021 //TODO: add the current table and allocate
1022 // a new, smaller one.
1023 LOGE_GC("dvmHeapScheduleFinalizations(): "
1024 "no room for any more pending finalizations: %zd\n",
1025 dvmHeapNumHeapRefTableEntries(&newPendingRefs));
1026 dvmAbort();
1027 }
1028 newPendCount++;
1029 } else {
1030 /* This ref is marked, so will remain on finalizableRefs.
1031 */
1032 if (newPendCount > 0) {
1033 /* Copy it up to fill the holes.
1034 */
1035 *gapRef++ = *ref;
1036 } else {
1037 /* No holes yet; don't bother copying.
1038 */
1039 gapRef++;
1040 }
1041 }
1042 ref++;
1043 }
1044 finRefs->refs.nextEntry = gapRef;
1045 //TODO: if the table is empty when we're done, free it.
1046 totalPendCount += newPendCount;
1047 finRefs = finRefs->next;
1048 }
1049 LOGD_GC("dvmHeapScheduleFinalizations(): %zd finalizers triggered.\n",
1050 totalPendCount);
1051 if (totalPendCount == 0) {
1052 /* No objects required finalization.
1053 * Free the empty temporary table.
1054 */
1055 dvmClearReferenceTable(&newPendingRefs);
1056 return;
1057 }
1058
1059 /* Add the new pending refs to the main list.
1060 */
1061 if (!dvmHeapAddTableToLargeTable(&gDvm.gcHeap->pendingFinalizationRefs,
1062 &newPendingRefs))
1063 {
1064 LOGE_GC("dvmHeapScheduleFinalizations(): can't insert new "
1065 "pending finalizations\n");
1066 dvmAbort();
1067 }
1068
1069 //TODO: try compacting the main list with a memcpy loop
1070
1071 /* Mark the refs we just moved; we don't want them or their
1072 * children to get swept yet.
1073 */
1074 ref = newPendingRefs.table;
1075 lastRef = newPendingRefs.nextEntry;
1076 assert(ref < lastRef);
1077 HPROF_SET_GC_SCAN_STATE(HPROF_ROOT_FINALIZING, 0);
1078 while (ref < lastRef) {
1079 markObjectNonNull(*ref, markContext);
1080 ref++;
1081 }
1082 HPROF_CLEAR_GC_SCAN_STATE();
1083
1084 /* Set markAllReferents so that we don't collect referents whose
1085 * only references are in final-reachable objects.
1086 * TODO: eventually provide normal reference behavior by properly
1087 * marking these references.
1088 */
1089 gDvm.gcHeap->markAllReferents = true;
1090 processMarkStack(markContext);
1091 gDvm.gcHeap->markAllReferents = false;
1092
1093 dvmSignalHeapWorker(false);
1094}
1095
1096void dvmHeapFinishMarkStep()
1097{
1098 HeapBitmap *markBitmap;
1099 HeapBitmap objectBitmap;
1100 GcMarkContext *markContext;
1101
1102 markContext = &gDvm.gcHeap->markContext;
1103
1104 /* The sweep step freed every object that appeared in the
1105 * HeapSource bitmaps that didn't appear in the mark bitmaps.
1106 * The new state of the HeapSource is exactly the final
1107 * mark bitmaps, so swap them in.
1108 *
1109 * The old bitmaps will be swapped into the context so that
1110 * we can clean them up.
1111 */
1112 dvmHeapSourceReplaceObjectBitmaps(markContext->bitmaps,
1113 markContext->numBitmaps);
1114
1115 /* Clean up the old HeapSource bitmaps and anything else associated
1116 * with the marking process.
1117 */
1118 dvmHeapBitmapDeleteList(markContext->bitmaps, markContext->numBitmaps);
1119 destroyMarkStack(&markContext->stack);
1120
1121 memset(markContext, 0, sizeof(*markContext));
1122}
1123
1124#if WITH_HPROF && WITH_HPROF_UNREACHABLE
1125static bool
1126hprofUnreachableBitmapCallback(size_t numPtrs, void **ptrs,
1127 const void *finger, void *arg)
1128{
1129 hprof_context_t *hctx = (hprof_context_t *)arg;
1130 size_t i;
1131
1132 for (i = 0; i < numPtrs; i++) {
1133 Object *obj;
1134
1135 /* The pointers we're getting back are DvmHeapChunks, not
1136 * Objects.
1137 */
1138 obj = (Object *)chunk2ptr(*ptrs++);
1139
1140 hprofMarkRootObject(hctx, obj, 0);
1141 hprofDumpHeapObject(hctx, obj);
1142 }
1143
1144 return true;
1145}
1146
1147static void
1148hprofDumpUnmarkedObjects(const HeapBitmap markBitmaps[],
1149 const HeapBitmap objectBitmaps[], size_t numBitmaps)
1150{
1151 hprof_context_t *hctx = gDvm.gcHeap->hprofContext;
1152 if (hctx == NULL) {
1153 return;
1154 }
1155
1156 LOGI("hprof: dumping unreachable objects\n");
1157
1158 HPROF_SET_GC_SCAN_STATE(HPROF_UNREACHABLE, 0);
1159
1160 dvmHeapBitmapXorWalkLists(markBitmaps, objectBitmaps, numBitmaps,
1161 hprofUnreachableBitmapCallback, hctx);
1162
1163 HPROF_CLEAR_GC_SCAN_STATE();
1164}
1165#endif
1166
1167static bool
1168sweepBitmapCallback(size_t numPtrs, void **ptrs, const void *finger, void *arg)
1169{
1170 const ClassObject *const classJavaLangClass = gDvm.classJavaLangClass;
1171 size_t i;
Barry Hayesdde8ab02009-05-20 12:10:36 -07001172 void **origPtrs = ptrs;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001173
1174 for (i = 0; i < numPtrs; i++) {
1175 DvmHeapChunk *hc;
1176 Object *obj;
1177
1178 /* The pointers we're getting back are DvmHeapChunks, not
1179 * Objects.
1180 */
1181 hc = (DvmHeapChunk *)*ptrs++;
1182 obj = (Object *)chunk2ptr(hc);
1183
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001184 /* Free the monitor associated with the object.
1185 */
1186 dvmFreeObjectMonitor(obj);
1187
1188 /* NOTE: Dereferencing clazz is dangerous. If obj was the last
1189 * one to reference its class object, the class object could be
1190 * on the sweep list, and could already have been swept, leaving
1191 * us with a stale pointer.
1192 */
1193 LOGV_SWEEP("FREE: 0x%08x %s\n", (uint)obj, obj->clazz->name);
1194
1195 /* This assumes that java.lang.Class will never go away.
1196 * If it can, and we were the last reference to it, it
1197 * could have already been swept. However, even in that case,
1198 * gDvm.classJavaLangClass should still have a useful
1199 * value.
1200 */
1201 if (obj->clazz == classJavaLangClass) {
1202 LOGV_SWEEP("---------------> %s\n", ((ClassObject *)obj)->name);
1203 /* dvmFreeClassInnards() may have already been called,
1204 * but it's safe to call on the same ClassObject twice.
1205 */
1206 dvmFreeClassInnards((ClassObject *)obj);
1207 }
1208
1209#if 0
1210 /* Overwrite the to-be-freed object to make stale references
1211 * more obvious.
1212 */
1213 {
1214 int chunklen;
1215 ClassObject *clazz = obj->clazz;
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001216 chunklen = dvmHeapSourceChunkSize(hc);
1217 memset(hc, 0xa5, chunklen);
1218 obj->clazz = (ClassObject *)((uintptr_t)clazz ^ 0xffffffff);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001219 }
1220#endif
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001221 }
Barry Hayesdde8ab02009-05-20 12:10:36 -07001222 // TODO: dvmHeapSourceFreeList has a loop, just like the above
1223 // does. Consider collapsing the two loops to save overhead.
1224 dvmHeapSourceFreeList(numPtrs, origPtrs);
The Android Open Source Projectf6c38712009-03-03 19:28:47 -08001225
1226 return true;
1227}
1228
1229/* A function suitable for passing to dvmHashForeachRemove()
1230 * to clear out any unmarked objects. Clears the low bits
1231 * of the pointer because the intern table may set them.
1232 */
1233static int isUnmarkedObject(void *object)
1234{
1235 return !isMarked(ptr2chunk((uintptr_t)object & ~(HB_OBJECT_ALIGNMENT-1)),
1236 &gDvm.gcHeap->markContext);
1237}
1238
1239/* Walk through the list of objects that haven't been
1240 * marked and free them.
1241 */
1242void
1243dvmHeapSweepUnmarkedObjects(int *numFreed, size_t *sizeFreed)
1244{
1245 const HeapBitmap *markBitmaps;
1246 const GcMarkContext *markContext;
1247 HeapBitmap objectBitmaps[HEAP_SOURCE_MAX_HEAP_COUNT];
1248 size_t origObjectsAllocated;
1249 size_t origBytesAllocated;
1250 size_t numBitmaps;
1251
1252 /* All reachable objects have been marked.
1253 * Detach any unreachable interned strings before
1254 * we sweep.
1255 */
1256 dvmGcDetachDeadInternedStrings(isUnmarkedObject);
1257
1258 /* Free any known objects that are not marked.
1259 */
1260 origObjectsAllocated = dvmHeapSourceGetValue(HS_OBJECTS_ALLOCATED, NULL, 0);
1261 origBytesAllocated = dvmHeapSourceGetValue(HS_BYTES_ALLOCATED, NULL, 0);
1262
1263 markContext = &gDvm.gcHeap->markContext;
1264 markBitmaps = markContext->bitmaps;
1265 numBitmaps = dvmHeapSourceGetObjectBitmaps(objectBitmaps,
1266 HEAP_SOURCE_MAX_HEAP_COUNT);
1267#ifndef NDEBUG
1268 if (numBitmaps != markContext->numBitmaps) {
1269 LOGE("heap bitmap count mismatch: %zd != %zd\n",
1270 numBitmaps, markContext->numBitmaps);
1271 dvmAbort();
1272 }
1273#endif
1274
1275#if WITH_HPROF && WITH_HPROF_UNREACHABLE
1276 hprofDumpUnmarkedObjects(markBitmaps, objectBitmaps, numBitmaps);
1277#endif
1278
1279 dvmHeapBitmapXorWalkLists(markBitmaps, objectBitmaps, numBitmaps,
1280 sweepBitmapCallback, NULL);
1281
1282 *numFreed = origObjectsAllocated -
1283 dvmHeapSourceGetValue(HS_OBJECTS_ALLOCATED, NULL, 0);
1284 *sizeFreed = origBytesAllocated -
1285 dvmHeapSourceGetValue(HS_BYTES_ALLOCATED, NULL, 0);
1286
1287#ifdef WITH_PROFILER
1288 if (gDvm.allocProf.enabled) {
1289 gDvm.allocProf.freeCount += *numFreed;
1290 gDvm.allocProf.freeSize += *sizeFreed;
1291 }
1292#endif
1293}