diff options
author | Carl Shapiro <cshapiro@google.com> | 2011-04-08 16:40:54 -0700 |
---|---|---|
committer | Carl Shapiro <cshapiro@google.com> | 2011-04-08 16:40:54 -0700 |
commit | f9fa8c14c7ef87b4318d606bfc5132df7b77b17c (patch) | |
tree | c61fd2ae4c6523905f665932f6b0fe53b81ff5f7 | |
parent | 6540f02d5827fd3190d7603e93e7352729a8976d (diff) | |
download | android_dalvik-f9fa8c14c7ef87b4318d606bfc5132df7b77b17c.tar.gz android_dalvik-f9fa8c14c7ef87b4318d606bfc5132df7b77b17c.tar.bz2 android_dalvik-f9fa8c14c7ef87b4318d606bfc5132df7b77b17c.zip |
Fix for loops and eliminate some other undesirable idioms.
Previously, we were using a non-standard C dialect which did not permit
the nicer variable scoping in loops. Now that we are using C++ in the
collector we can guarantee that feature is available. This change also
rearranges some of the surrounding code to take advantage of the more
flexible scoping rules.
Change-Id: I9be35794cc12bcbc0d5299fe387d4bc406481075
-rw-r--r-- | vm/alloc/CardTable.cpp | 10 | ||||
-rw-r--r-- | vm/alloc/Copying.cpp | 152 | ||||
-rw-r--r-- | vm/alloc/HeapBitmap.cpp | 28 | ||||
-rw-r--r-- | vm/alloc/HeapSource.cpp | 40 | ||||
-rw-r--r-- | vm/alloc/MarkSweep.cpp | 31 | ||||
-rw-r--r-- | vm/alloc/Visit.cpp | 29 | ||||
-rw-r--r-- | vm/alloc/VisitInlines.h | 19 | ||||
-rw-r--r-- | vm/hprof/HprofHeap.cpp | 17 | ||||
-rw-r--r-- | vm/hprof/HprofOutput.cpp | 30 |
9 files changed, 119 insertions, 237 deletions
diff --git a/vm/alloc/CardTable.cpp b/vm/alloc/CardTable.cpp index 533d5bc8a..d29bfa58b 100644 --- a/vm/alloc/CardTable.cpp +++ b/vm/alloc/CardTable.cpp @@ -152,8 +152,7 @@ static void moveCardsToModUnion(u1 *base, u1 *limit) u1 *limitCard = dvmCardFromAddr(limit); u4 *bits = (u4*)h->modUnionTableBase; u1 *heapBase = (u1*)dvmHeapSourceGetBase(); - u1 *card; - for (card = baseCard; card < limitCard; ++card) { + for (u1 *card = baseCard; card < limitCard; ++card) { if (*card == GC_CARD_CLEAN) { continue; } @@ -174,8 +173,7 @@ void dvmClearCardTable(void) uintptr_t limit[HEAP_SOURCE_MAX_HEAP_COUNT]; size_t numHeaps = dvmHeapSourceGetNumHeaps(); dvmHeapSourceGetRegions(base, NULL, limit, numHeaps); - size_t i; - for (i = 0; i < numHeaps; ++i) { + for (size_t i = 0; i < numHeaps; ++i) { if (i != 0) { moveCardsToModUnion((u1*)base[i], (u1*)limit[i]); } else { @@ -381,9 +379,7 @@ static bool isWeakInternedString(const Object *obj) static bool isPushedOnMarkStack(const Object *obj) { GcMarkStack *stack = &gDvm.gcHeap->markContext.stack; - const Object **ptr; - - for (ptr = stack->base; ptr < stack->top; ++ptr) { + for (const Object **ptr = stack->base; ptr < stack->top; ++ptr) { if (*ptr == obj) { return true; } diff --git a/vm/alloc/Copying.cpp b/vm/alloc/Copying.cpp index 097efae78..3dd1c1bc3 100644 --- a/vm/alloc/Copying.cpp +++ b/vm/alloc/Copying.cpp @@ -245,9 +245,7 @@ static unsigned long alignUp(unsigned long x, unsigned long n) static void describeBlocks(const HeapSource *heapSource) { - size_t i; - - for (i = 0; i < heapSource->totalBlocks; ++i) { + for (size_t i = 0; i < heapSource->totalBlocks; ++i) { if ((i % 32) == 0) putchar('\n'); printf("%d ", heapSource->blockSpace[i]); } @@ -260,12 +258,9 @@ static void describeBlocks(const HeapSource *heapSource) static void *virtualAlloc(size_t length) { - void *addr; - int flags, prot; - - flags = MAP_PRIVATE | MAP_ANONYMOUS; - prot = PROT_READ | PROT_WRITE; - addr = mmap(NULL, length, prot, flags, -1, 0); + int flags = MAP_PRIVATE | MAP_ANONYMOUS; + int prot = PROT_READ | PROT_WRITE; + void *addr = mmap(NULL, length, prot, flags, -1, 0); if (addr == MAP_FAILED) { LOGE_HEAP("mmap: %s", strerror(errno)); addr = NULL; @@ -275,11 +270,9 @@ static void *virtualAlloc(size_t length) static void virtualFree(void *addr, size_t length) { - int res; - assert(addr != NULL); assert((uintptr_t)addr % SYSTEM_PAGE_SIZE == 0); - res = munmap(addr, length); + int res = munmap(addr, length); if (res == -1) { LOGE_HEAP("munmap: %s", strerror(errno)); } @@ -302,12 +295,8 @@ static int isValidAddress(const HeapSource *heapSource, const u1 *addr) */ static void *allocateBlocks(HeapSource *heapSource, size_t blocks) { - void *addr; - size_t allocBlocks, totalBlocks; - size_t i, j; - - allocBlocks = heapSource->allocBlocks; - totalBlocks = heapSource->totalBlocks; + size_t allocBlocks = heapSource->allocBlocks; + size_t totalBlocks = heapSource->totalBlocks; /* Check underflow. */ assert(blocks != 0); /* Check overflow. */ @@ -315,9 +304,9 @@ static void *allocateBlocks(HeapSource *heapSource, size_t blocks) return NULL; } /* Scan block map. */ - for (i = 0; i < totalBlocks; ++i) { + for (size_t i = 0; i < totalBlocks; ++i) { /* Check fit. */ - for (j = 0; j < blocks; ++j) { /* runs over totalBlocks */ + for (size_t j = 0; j < blocks; ++j) { /* runs over totalBlocks */ if (heapSource->blockSpace[i+j] != BLOCK_FREE) { break; } @@ -329,11 +318,11 @@ static void *allocateBlocks(HeapSource *heapSource, size_t blocks) } /* Fit, allocate. */ heapSource->blockSpace[i] = BLOCK_TO_SPACE; /* why to-space? */ - for (j = 1; j < blocks; ++j) { + for (size_t j = 1; j < blocks; ++j) { heapSource->blockSpace[i+j] = BLOCK_CONTINUED; } heapSource->allocBlocks += blocks; - addr = &heapSource->blockBase[i*BLOCK_SIZE]; + void *addr = &heapSource->blockBase[i*BLOCK_SIZE]; memset(addr, 0, blocks*BLOCK_SIZE); /* Collecting? */ if (heapSource->queueHead != QUEUE_TAIL) { @@ -376,24 +365,20 @@ static u1 *blockToAddress(const HeapSource *heapSource, size_t block) static void clearBlock(HeapSource *heapSource, size_t block) { - u1 *addr; - size_t i; - assert(heapSource != NULL); assert(block < heapSource->totalBlocks); - addr = heapSource->blockBase + block*BLOCK_SIZE; + u1 *addr = heapSource->blockBase + block*BLOCK_SIZE; memset(addr, 0xCC, BLOCK_SIZE); - for (i = 0; i < BLOCK_SIZE; i += 8) { + for (size_t i = 0; i < BLOCK_SIZE; i += 8) { dvmHeapBitmapClearObjectBit(&heapSource->allocBits, addr + i); } } static void clearFromSpace(HeapSource *heapSource) { - size_t i, count; - assert(heapSource != NULL); - i = count = 0; + size_t i = 0; + size_t count = 0; while (i < heapSource->totalBlocks) { if (heapSource->blockSpace[i] != BLOCK_FROM_SPACE) { ++i; @@ -786,10 +771,7 @@ size_t dvmGetExternalBytesAllocated(void) void dvmHeapSourceFlip(void) { - HeapSource *heapSource; - size_t i; - - heapSource = gDvm.gcHeap->heapSource; + HeapSource *heapSource = gDvm.gcHeap->heapSource; /* Reset the block queue. */ heapSource->allocBlocks = 0; @@ -802,7 +784,7 @@ void dvmHeapSourceFlip(void) heapSource->allocLimit = NULL; /* Whiten all allocated blocks. */ - for (i = 0; i < heapSource->totalBlocks; ++i) { + for (size_t i = 0; i < heapSource->totalBlocks; ++i) { if (heapSource->blockSpace[i] == BLOCK_TO_SPACE) { heapSource->blockSpace[i] = BLOCK_FROM_SPACE; } @@ -811,9 +793,7 @@ void dvmHeapSourceFlip(void) static void room(size_t *alloc, size_t *avail, size_t *total) { - HeapSource *heapSource; - - heapSource = gDvm.gcHeap->heapSource; + HeapSource *heapSource = gDvm.gcHeap->heapSource; *total = heapSource->totalBlocks*BLOCK_SIZE; *alloc = heapSource->allocBlocks*BLOCK_SIZE; *avail = *total - *alloc; @@ -857,10 +837,8 @@ static void pinObject(const Object *obj) static size_t sumHeapBitmap(const HeapBitmap *bitmap) { - size_t i, sum; - - sum = 0; - for (i = 0; i < bitmap->bitsLen >> 2; ++i) { + size_t sum = 0; + for (size_t i = 0; i < bitmap->bitsLen >> 2; ++i) { sum += CLZ(bitmap->bits[i]); } return sum; @@ -908,8 +886,6 @@ static void* getPermanentString(const StringObject *obj) */ static void scavengeClassObject(ClassObject *obj) { - int i; - LOG_SCAV("scavengeClassObject(obj=%p)", obj); assert(obj != NULL); assert(obj->obj.clazz != NULL); @@ -929,14 +905,14 @@ static void scavengeClassObject(ClassObject *obj) /* Scavenge the class loader. */ scavengeReference(&obj->classLoader); /* Scavenge static fields. */ - for (i = 0; i < obj->sfieldCount; ++i) { + for (int i = 0; i < obj->sfieldCount; ++i) { char ch = obj->sfields[i].field.signature[0]; if (ch == '[' || ch == 'L') { scavengeReference((Object **)(void *)&obj->sfields[i].value.l); } } /* Scavenge interface class objects. */ - for (i = 0; i < obj->interfaceCount; ++i) { + for (int i = 0; i < obj->interfaceCount; ++i) { scavengeReference((Object **) &obj->interfaces[i]); } } @@ -946,19 +922,17 @@ static void scavengeClassObject(ClassObject *obj) */ static size_t scavengeArrayObject(ArrayObject *array) { - size_t i, length; - LOG_SCAV("scavengeArrayObject(array=%p)", array); /* Scavenge the class object. */ assert(toSpaceContains(array)); assert(array != NULL); assert(array->obj.clazz != NULL); scavengeReference((Object **) array); - length = dvmArrayObjectSize(array); + size_t length = dvmArrayObjectSize(array); /* Scavenge the array contents. */ if (IS_CLASS_FLAG_SET(array->obj.clazz, CLASS_ISOBJECTARRAY)) { Object **contents = (Object **)array->contents; - for (i = 0; i < array->length; ++i) { + for (size_t i = 0; i < array->length; ++i) { scavengeReference(&contents[i]); } } @@ -1280,16 +1254,13 @@ static void scavengeReferenceObject(Object *obj) */ static void scavengeDataObject(Object *obj) { - ClassObject *clazz; - int i; - // LOG_SCAV("scavengeDataObject(obj=%p)", obj); assert(obj != NULL); assert(obj->clazz != NULL); assert(obj->clazz->objectSize != 0); assert(toSpaceContains(obj)); /* Scavenge the class object. */ - clazz = obj->clazz; + ClassObject *clazz = obj->clazz; scavengeReference((Object **) obj); /* Scavenge instance fields. */ if (clazz->refOffsets != CLASS_WALK_SUPER) { @@ -1304,7 +1275,7 @@ static void scavengeDataObject(Object *obj) } else { for (; clazz != NULL; clazz = clazz->super) { InstField *field = clazz->ifields; - for (i = 0; i < clazz->ifieldRefCount; ++i, ++field) { + for (int i = 0; i < clazz->ifieldRefCount; ++i, ++field) { size_t offset = field->byteOffset; Object **ref = (Object **)((u1 *)obj + offset); scavengeReference(ref); @@ -1443,18 +1414,14 @@ static void scavengeObject(Object *obj) static void pinHashTableEntries(HashTable *table) { - HashEntry *entry; - void *obj; - int i; - LOG_PIN(">>> pinHashTableEntries(table=%p)", table); if (table == NULL) { return; } dvmHashTableLock(table); - for (i = 0; i < table->tableSize; ++i) { - entry = &table->pEntries[i]; - obj = entry->data; + for (int i = 0; i < table->tableSize; ++i) { + HashEntry *entry = &table->pEntries[i]; + void *obj = entry->data; if (obj == NULL || obj == HASH_TOMBSTONE) { continue; } @@ -1466,11 +1433,8 @@ static void pinHashTableEntries(HashTable *table) static void pinPrimitiveClasses(void) { - size_t length; - size_t i; - - length = ARRAYSIZE(gDvm.primitiveClass); - for (i = 0; i < length; i++) { + size_t length = ARRAYSIZE(gDvm.primitiveClass); + for (size_t i = 0; i < length; i++) { if (gDvm.primitiveClass[i] != NULL) { pinObject((Object *)gDvm.primitiveClass[i]); } @@ -1484,19 +1448,14 @@ static void pinPrimitiveClasses(void) */ static void scavengeInternedStrings(void) { - HashTable *table; - HashEntry *entry; - Object *obj; - int i; - - table = gDvm.internedStrings; + HashTable *table = gDvm.internedStrings; if (table == NULL) { return; } dvmHashTableLock(table); - for (i = 0; i < table->tableSize; ++i) { - entry = &table->pEntries[i]; - obj = (Object *)entry->data; + for (int i = 0; i < table->tableSize; ++i) { + HashEntry *entry = &table->pEntries[i]; + Object *obj = (Object *)entry->data; if (obj == NULL || obj == HASH_TOMBSTONE) { continue; } else if (!isPermanentString((StringObject *)obj)) { @@ -1512,19 +1471,14 @@ static void scavengeInternedStrings(void) static void pinInternedStrings(void) { - HashTable *table; - HashEntry *entry; - Object *obj; - int i; - - table = gDvm.internedStrings; + HashTable *table = gDvm.internedStrings; if (table == NULL) { return; } dvmHashTableLock(table); - for (i = 0; i < table->tableSize; ++i) { - entry = &table->pEntries[i]; - obj = (Object *)entry->data; + for (int i = 0; i < table->tableSize; ++i) { + HashEntry *entry = &table->pEntries[i]; + Object *obj = (Object *)entry->data; if (obj == NULL || obj == HASH_TOMBSTONE) { continue; } else if (isPermanentString((StringObject *)obj)) { @@ -1544,12 +1498,10 @@ static void pinInternedStrings(void) */ static void pinReferenceTable(const ReferenceTable *table) { - Object **entry; - assert(table != NULL); assert(table->table != NULL); assert(table->nextEntry != NULL); - for (entry = table->table; entry < table->nextEntry; ++entry) { + for (Object **entry = table->table; entry < table->nextEntry; ++entry) { assert(entry != NULL); assert(!isForward(*entry)); pinObject(*entry); @@ -1656,7 +1608,6 @@ static void scavengeThreadStack(Thread *thread) const RegisterMap* pMap; const u1* regVector; - int i; Method* nonConstMethod = (Method*) method; // quiet gcc pMap = dvmGetExpandedRegisterMap(nonConstMethod); @@ -1705,7 +1656,7 @@ static void scavengeThreadStack(Thread *thread) * A '1' bit indicates a live reference. */ u2 bits = 1 << 1; - for (i = method->registersSize - 1; i >= 0; i--) { + for (int i = method->registersSize - 1; i >= 0; i--) { u4 rval = *framePtr; bits >>= 1; @@ -1799,7 +1750,6 @@ static void pinThreadStack(const Thread *thread) Method *method; const char *shorty; Object *obj; - int i; saveArea = NULL; framePtr = (const u4 *)thread->curFrame; @@ -1841,7 +1791,7 @@ static void pinThreadStack(const Thread *thread) } } shorty = method->shorty+1; // skip return value - for (i = method->registersSize - 1; i >= 0; i--, framePtr++) { + for (int i = method->registersSize - 1; i >= 0; i--, framePtr++) { switch (*shorty++) { case 'L': obj = (Object *)*framePtr; @@ -1878,7 +1828,7 @@ static void pinThreadStack(const Thread *thread) /* * No register info for this frame, conservatively pin. */ - for (i = 0; i < method->registersSize; ++i) { + for (int i = 0; i < method->registersSize; ++i) { u4 regValue = framePtr[i]; if (regValue != 0 && (regValue & 0x3) == 0 && dvmIsValidObject((Object *)regValue)) { pinObject((Object *)regValue); @@ -2084,13 +2034,9 @@ static void scavengeBlockQueue(void) */ static void verifyNewSpace(void) { - HeapSource *heapSource; - size_t i; - size_t c0, c1, c2, c7; - - c0 = c1 = c2 = c7 = 0; - heapSource = gDvm.gcHeap->heapSource; - for (i = 0; i < heapSource->totalBlocks; ++i) { + HeapSource *heapSource = gDvm.gcHeap->heapSource; + size_t c0 = 0, c1 = 0, c2 = 0, c7 = 0; + for (size_t i = 0; i < heapSource->totalBlocks; ++i) { switch (heapSource->blockSpace[i]) { case BLOCK_FREE: ++c0; break; case BLOCK_TO_SPACE: ++c1; break; @@ -2102,7 +2048,7 @@ static void verifyNewSpace(void) LOG_VER("Block Demographics: " "Free=%zu,ToSpace=%zu,FromSpace=%zu,Continued=%zu", c0, c1, c2, c7); - for (i = 0; i < heapSource->totalBlocks; ++i) { + for (size_t i = 0; i < heapSource->totalBlocks; ++i) { if (heapSource->blockSpace[i] != BLOCK_TO_SPACE) { continue; } @@ -2112,9 +2058,7 @@ static void verifyNewSpace(void) void describeHeap(void) { - HeapSource *heapSource; - - heapSource = gDvm.gcHeap->heapSource; + HeapSource *heapSource = gDvm.gcHeap->heapSource; describeBlocks(heapSource); } diff --git a/vm/alloc/HeapBitmap.cpp b/vm/alloc/HeapBitmap.cpp index ffeff623e..0ffd3b03b 100644 --- a/vm/alloc/HeapBitmap.cpp +++ b/vm/alloc/HeapBitmap.cpp @@ -100,14 +100,11 @@ bool dvmHeapBitmapCoversAddress(const HeapBitmap *hb, const void *obj) void dvmHeapBitmapWalk(const HeapBitmap *bitmap, BitmapCallback *callback, void *arg) { - uintptr_t end; - uintptr_t i; - assert(bitmap != NULL); assert(bitmap->bits != NULL); assert(callback != NULL); - end = HB_OFFSET_TO_INDEX(bitmap->max - bitmap->base); - for (i = 0; i <= end; ++i) { + uintptr_t end = HB_OFFSET_TO_INDEX(bitmap->max - bitmap->base); + for (uintptr_t i = 0; i <= end; ++i) { unsigned long word = bitmap->bits[i]; if (UNLIKELY(word != 0)) { unsigned long highBit = 1 << (HB_BITS_PER_WORD - 1); @@ -145,8 +142,7 @@ void dvmHeapBitmapScanWalk(HeapBitmap *bitmap, assert(base >= bitmap->base); assert(max <= bitmap->max); uintptr_t end = HB_OFFSET_TO_INDEX(max - base); - uintptr_t i; - for (i = 0; i <= end; ++i) { + for (uintptr_t i = 0; i <= end; ++i) { unsigned long word = bitmap->bits[i]; if (UNLIKELY(word != 0)) { unsigned long highBit = 1 << (HB_BITS_PER_WORD - 1); @@ -174,12 +170,6 @@ void dvmHeapBitmapSweepWalk(const HeapBitmap *liveHb, const HeapBitmap *markHb, uintptr_t base, uintptr_t max, BitmapSweepCallback *callback, void *callbackArg) { - void *pointerBuf[4 * HB_BITS_PER_WORD]; - void **pb = pointerBuf; - size_t i; - size_t start, end; - unsigned long *live, *mark; - assert(liveHb != NULL); assert(liveHb->bits != NULL); assert(markHb != NULL); @@ -195,11 +185,13 @@ void dvmHeapBitmapSweepWalk(const HeapBitmap *liveHb, const HeapBitmap *markHb, */ return; } - start = HB_OFFSET_TO_INDEX(base - liveHb->base); - end = HB_OFFSET_TO_INDEX(max - liveHb->base); - live = liveHb->bits; - mark = markHb->bits; - for (i = start; i <= end; i++) { + void *pointerBuf[4 * HB_BITS_PER_WORD]; + void **pb = pointerBuf; + size_t start = HB_OFFSET_TO_INDEX(base - liveHb->base); + size_t end = HB_OFFSET_TO_INDEX(max - liveHb->base); + unsigned long *live = liveHb->bits; + unsigned long *mark = markHb->bits; + for (size_t i = start; i <= end; i++) { unsigned long garbage = live[i] & ~mark[i]; if (UNLIKELY(garbage != 0)) { unsigned long highBit = 1 << (HB_BITS_PER_WORD - 1); diff --git a/vm/alloc/HeapSource.cpp b/vm/alloc/HeapSource.cpp index 50303b7cb..0f47fbae2 100644 --- a/vm/alloc/HeapSource.cpp +++ b/vm/alloc/HeapSource.cpp @@ -234,11 +234,10 @@ static size_t oldHeapOverhead(const HeapSource *hs, bool includeActive) static Heap *ptr2heap(const HeapSource *hs, const void *ptr) { const size_t numHeaps = hs->numHeaps; - size_t i; //TODO: unroll this to HEAP_SOURCE_MAX_HEAP_COUNT if (ptr != NULL) { - for (i = 0; i < numHeaps; i++) { + for (size_t i = 0; i < numHeaps; i++) { const Heap *const heap = &hs->heaps[i]; if ((const char *)ptr >= heap->base && (const char *)ptr < heap->limit) { @@ -659,12 +658,11 @@ dvmHeapSourceGetValue(enum HeapSourceValueSpec spec, size_t perHeapStats[], HeapSource *hs = gHs; size_t value = 0; size_t total = 0; - size_t i; HS_BOILERPLATE(); assert(arrayLen >= hs->numHeaps || perHeapStats == NULL); - for (i = 0; i < hs->numHeaps; i++) { + for (size_t i = 0; i < hs->numHeaps; i++) { Heap *const heap = &hs->heaps[i]; switch (spec) { @@ -696,12 +694,11 @@ void dvmHeapSourceGetRegions(uintptr_t *base, uintptr_t *max, uintptr_t *limit, size_t numHeaps) { HeapSource *hs = gHs; - size_t i; HS_BOILERPLATE(); assert(numHeaps <= hs->numHeaps); - for (i = 0; i < numHeaps; ++i) { + for (size_t i = 0; i < numHeaps; ++i) { base[i] = (uintptr_t)hs->heaps[i].base; if (max != NULL) { max[i] = MIN((uintptr_t)hs->heaps[i].limit - 1, hs->markBits.max); @@ -750,9 +747,6 @@ void dvmHeapSourceZeroMarkBitmap(void) void dvmMarkImmuneObjects(const char *immuneLimit) { - char *dst, *src; - size_t i, index, length; - /* * Copy the contents of the live bit vector for immune object * range into the mark bit vector. @@ -766,17 +760,17 @@ void dvmMarkImmuneObjects(const char *immuneLimit) assert(gHs->heaps[0].base >= immuneLimit); assert(gHs->heaps[0].limit > immuneLimit); - for (i = 1; i < gHs->numHeaps; ++i) { + for (size_t i = 1; i < gHs->numHeaps; ++i) { if (gHs->heaps[i].base < immuneLimit) { assert(gHs->heaps[i].limit <= immuneLimit); /* Compute the number of words to copy in the bitmap. */ - index = HB_OFFSET_TO_INDEX( + size_t index = HB_OFFSET_TO_INDEX( (uintptr_t)gHs->heaps[i].base - gHs->liveBits.base); /* Compute the starting offset in the live and mark bits. */ - src = (char *)(gHs->liveBits.bits + index); - dst = (char *)(gHs->markBits.bits + index); + char *src = (char *)(gHs->liveBits.bits + index); + char *dst = (char *)(gHs->markBits.bits + index); /* Compute the number of bytes of the live bitmap to copy. */ - length = HB_OFFSET_TO_BYTE_INDEX( + size_t length = HB_OFFSET_TO_BYTE_INDEX( gHs->heaps[i].limit - gHs->heaps[i].base); /* Do the copy. */ memcpy(dst, src, length); @@ -960,9 +954,7 @@ size_t dvmHeapSourceFreeList(size_t numPtrs, void **ptrs) assert(ptr2heap(gHs, ptrs[0]) == heap); countFree(heap, ptrs[0], &numBytes); void *merged = ptrs[0]; - - size_t i; - for (i = 1; i < numPtrs; i++) { + for (size_t i = 1; i < numPtrs; i++) { assert(merged != NULL); assert(ptrs[i] != NULL); assert((intptr_t)merged < (intptr_t)ptrs[i]); @@ -981,8 +973,7 @@ size_t dvmHeapSourceFreeList(size_t numPtrs, void **ptrs) mspace_free(msp, merged); } else { // This is not an 'active heap'. Only do the accounting. - size_t i; - for (i = 0; i < numPtrs; i++) { + for (size_t i = 0; i < numPtrs; i++) { assert(ptrs[i] != NULL); assert(ptr2heap(gHs, ptrs[i]) == heap); countFree(heap, ptrs[i], &numBytes); @@ -1402,15 +1393,13 @@ void dvmHeapSourceTrim(size_t bytesTrimmed[], size_t arrayLen) { HeapSource *hs = gHs; - size_t nativeBytes, heapBytes; - size_t i; HS_BOILERPLATE(); assert(arrayLen >= hs->numHeaps); - heapBytes = 0; - for (i = 0; i < hs->numHeaps; i++) { + size_t heapBytes = 0; + for (size_t i = 0; i < hs->numHeaps; i++) { Heap *heap = &hs->heaps[i]; /* Return the wilderness chunk to the system. @@ -1428,7 +1417,7 @@ dvmHeapSourceTrim(size_t bytesTrimmed[], size_t arrayLen) /* Same for the native heap. */ dlmalloc_trim(0); - nativeBytes = 0; + size_t nativeBytes = 0; dlmalloc_walk_free_pages(releasePagesInRange, &nativeBytes); LOGD_HEAP("madvised %zd (GC) + %zd (native) = %zd total bytes\n", @@ -1446,14 +1435,13 @@ dvmHeapSourceWalk(void(*callback)(const void *chunkptr, size_t chunklen, void *arg) { HeapSource *hs = gHs; - size_t i; HS_BOILERPLATE(); /* Walk the heaps from oldest to newest. */ //TODO: do this in address order - for (i = hs->numHeaps; i > 0; --i) { + for (size_t i = hs->numHeaps; i > 0; --i) { mspace_walk_heap(hs->heaps[i-1].msp, callback, arg); } } diff --git a/vm/alloc/MarkSweep.cpp b/vm/alloc/MarkSweep.cpp index 9b527cbc5..6fbde9c79 100644 --- a/vm/alloc/MarkSweep.cpp +++ b/vm/alloc/MarkSweep.cpp @@ -196,15 +196,13 @@ static void visitModUnionTable(Visitor *visitor, u1 *base, u1 *limit, void *arg) assert(byteLength <= gDvm.gcHeap->modUnionTableLength); assert(byteLength % sizeof(*bits) == 0); size_t wordLength = byteLength / sizeof(*bits); - size_t i; - for (i = 0; i < wordLength; ++i) { + for (size_t i = 0; i < wordLength; ++i) { if (bits[i] == 0) { continue; } u4 word = bits[i]; bits[i] = 0; - size_t j = 0; - for (j = 0; j < sizeof(u4)*CHAR_BIT; ++j) { + for (size_t j = 0; j < sizeof(u4)*CHAR_BIT; ++j) { if (word & (1 << j)) { /* compute the base of the card */ size_t offset = (i*sizeof(u4)*CHAR_BIT + j) * GC_CARD_SIZE; @@ -407,11 +405,11 @@ static void scanFields(const Object *obj, GcMarkContext *ctx) refOffsets &= ~(CLASS_HIGH_BIT >> rshift); } } else { - ClassObject *clazz; - for (clazz = obj->clazz; clazz != NULL; clazz = clazz->super) { + for (ClassObject *clazz = obj->clazz; + clazz != NULL; + clazz = clazz->super) { InstField *field = clazz->ifields; - int i; - for (i = 0; i < clazz->ifieldRefCount; ++i, ++field) { + for (int i = 0; i < clazz->ifieldRefCount; ++i, ++field) { void *addr = BYTE_OFFSET((Object *)obj, field->byteOffset); Object *ref = (Object *)((JValue *)addr)->l; markObject(ref, ctx); @@ -425,11 +423,9 @@ static void scanFields(const Object *obj, GcMarkContext *ctx) */ static void scanStaticFields(const ClassObject *clazz, GcMarkContext *ctx) { - int i; - assert(clazz != NULL); assert(ctx != NULL); - for (i = 0; i < clazz->sfieldCount; ++i) { + for (int i = 0; i < clazz->sfieldCount; ++i) { char ch = clazz->sfields[i].field.signature[0]; if (ch == '[' || ch == 'L') { Object *obj = (Object *)clazz->sfields[i].value.l; @@ -443,11 +439,9 @@ static void scanStaticFields(const ClassObject *clazz, GcMarkContext *ctx) */ static void scanInterfaces(const ClassObject *clazz, GcMarkContext *ctx) { - int i; - assert(clazz != NULL); assert(ctx != NULL); - for (i = 0; i < clazz->interfaceCount; ++i) { + for (int i = 0; i < clazz->interfaceCount; ++i) { markObject((const Object *)clazz->interfaces[i], ctx); } } @@ -493,8 +487,7 @@ static void scanArrayObject(const Object *obj, GcMarkContext *ctx) if (IS_CLASS_FLAG_SET(obj->clazz, CLASS_ISOBJECTARRAY)) { const ArrayObject *array = (const ArrayObject *)obj; const Object **contents = (const Object **)(void *)array->contents; - size_t i; - for (i = 0; i < array->length; ++i) { + for (size_t i = 0; i < array->length; ++i) { markObject(contents[i], ctx); } } @@ -1035,8 +1028,7 @@ void sweepWeakJniGlobals(void) Object **entry = table->table; GcMarkContext *ctx = &gDvm.gcHeap->markContext; int numEntries = dvmIndirectRefTableEntries(table); - int i; - for (i = 0; i < numEntries; ++i) { + for (int i = 0; i < numEntries; ++i) { if (entry[i] != NULL && !isMarked(entry[i], ctx)) { entry[i] = NULL; } @@ -1066,7 +1058,6 @@ void dvmHeapSweepUnmarkedObjects(bool isPartial, bool isConcurrent, SweepContext ctx; HeapBitmap *prevLive, *prevMark; size_t numHeaps, numSweepHeaps; - size_t i; numHeaps = dvmHeapSourceGetNumHeaps(); dvmHeapSourceGetRegions(base, max, NULL, numHeaps); @@ -1080,7 +1071,7 @@ void dvmHeapSweepUnmarkedObjects(bool isPartial, bool isConcurrent, ctx.isConcurrent = isConcurrent; prevLive = dvmHeapSourceGetMarkBits(); prevMark = dvmHeapSourceGetLiveBits(); - for (i = 0; i < numSweepHeaps; ++i) { + for (size_t i = 0; i < numSweepHeaps; ++i) { dvmHeapBitmapSweepWalk(prevLive, prevMark, base[i], max[i], sweepBitmapCallback, &ctx); } diff --git a/vm/alloc/Visit.cpp b/vm/alloc/Visit.cpp index 6ca143992..b329595eb 100644 --- a/vm/alloc/Visit.cpp +++ b/vm/alloc/Visit.cpp @@ -36,12 +36,10 @@ void dvmVisitObject(Visitor *visitor, Object *obj, void *arg) static void visitHashTable(RootVisitor *visitor, HashTable *table, RootType type, void *arg) { - int i; - assert(visitor != NULL); assert(table != NULL); dvmHashTableLock(table); - for (i = 0; i < table->tableSize; ++i) { + for (int i = 0; i < table->tableSize; ++i) { HashEntry *entry = &table->pEntries[i]; if (entry->data != NULL && entry->data != HASH_TOMBSTONE) { (*visitor)(&entry->data, 0, type, arg); @@ -56,11 +54,9 @@ static void visitHashTable(RootVisitor *visitor, HashTable *table, static void visitReferenceTable(RootVisitor *visitor, ReferenceTable *table, u4 threadId, RootType type, void *arg) { - Object **entry; - assert(visitor != NULL); assert(table != NULL); - for (entry = table->table; entry < table->nextEntry; ++entry) { + for (Object **entry = table->table; entry < table->nextEntry; ++entry) { assert(entry != NULL); (*visitor)(entry, threadId, type, arg); } @@ -76,8 +72,7 @@ static void visitIndirectRefTable(RootVisitor *visitor, IndirectRefTable *table, assert(table != NULL); Object **entry = table->table; int numEntries = dvmIndirectRefTableEntries(table); - int i; - for (i = 0; i < numEntries; ++i) { + for (int i = 0; i < numEntries; ++i) { (*visitor)(&entry[i], threadId, type, arg); } } @@ -88,23 +83,19 @@ static void visitIndirectRefTable(RootVisitor *visitor, IndirectRefTable *table, */ static void visitThreadStack(RootVisitor *visitor, Thread *thread, void *arg) { - const StackSaveArea *saveArea; - u4 *fp; - u4 threadId; - assert(visitor != NULL); assert(thread != NULL); - threadId = thread->threadId; - fp = (u4 *)thread->curFrame; - for (; fp != NULL; fp = (u4 *)saveArea->prevFrame) { + u4 threadId = thread->threadId; + const StackSaveArea *saveArea; + for (u4 *fp = (u4 *)thread->curFrame; + fp != NULL; + fp = (u4 *)saveArea->prevFrame) { Method *method; saveArea = SAVEAREA_FROM_FP(fp); method = (Method *)saveArea->method; if (method != NULL && !dvmIsNativeMethod(method)) { const RegisterMap* pMap = dvmGetExpandedRegisterMap(method); const u1* regVector = NULL; - size_t i; - if (pMap != NULL) { /* found map, get registers for this address */ int addr = saveArea->xtra.currentPc - method->insns; @@ -116,7 +107,7 @@ static void visitThreadStack(RootVisitor *visitor, Thread *thread, void *arg) * info for the current PC. Perform a conservative * scan. */ - for (i = 0; i < method->registersSize; ++i) { + for (size_t i = 0; i < method->registersSize; ++i) { if (dvmIsValidObject((Object *)fp[i])) { (*visitor)(&fp[i], threadId, ROOT_JAVA_FRAME, arg); } @@ -131,7 +122,7 @@ static void visitThreadStack(RootVisitor *visitor, Thread *thread, void *arg) * A '1' bit indicates a live reference. */ u2 bits = 1 << 1; - for (i = 0; i < method->registersSize; ++i) { + for (size_t i = 0; i < method->registersSize; ++i) { bits >>= 1; if (bits == 1) { /* set bit 9 so we can tell when we're empty */ diff --git a/vm/alloc/VisitInlines.h b/vm/alloc/VisitInlines.h index eb889af57..479c3c00e 100644 --- a/vm/alloc/VisitInlines.h +++ b/vm/alloc/VisitInlines.h @@ -35,11 +35,11 @@ static void visitFields(Visitor *visitor, Object *obj, void *arg) refOffsets &= ~(CLASS_HIGH_BIT >> rshift); } } else { - ClassObject *clazz; - for (clazz = obj->clazz; clazz != NULL; clazz = clazz->super) { + for (ClassObject *clazz = obj->clazz; + clazz != NULL; + clazz = clazz->super) { InstField *field = clazz->ifields; - int i; - for (i = 0; i < clazz->ifieldRefCount; ++i, ++field) { + for (int i = 0; i < clazz->ifieldRefCount; ++i, ++field) { size_t offset = field->byteOffset; Object **ref = (Object **)BYTE_OFFSET(obj, offset); (*visitor)(ref, arg); @@ -54,11 +54,9 @@ static void visitFields(Visitor *visitor, Object *obj, void *arg) static void visitStaticFields(Visitor *visitor, ClassObject *clazz, void *arg) { - int i; - assert(visitor != NULL); assert(clazz != NULL); - for (i = 0; i < clazz->sfieldCount; ++i) { + for (int i = 0; i < clazz->sfieldCount; ++i) { char ch = clazz->sfields[i].field.signature[0]; if (ch == '[' || ch == 'L') { (*visitor)(&clazz->sfields[i].value.l, arg); @@ -72,11 +70,9 @@ static void visitStaticFields(Visitor *visitor, ClassObject *clazz, static void visitInterfaces(Visitor *visitor, ClassObject *clazz, void *arg) { - int i; - assert(visitor != NULL); assert(clazz != NULL); - for (i = 0; i < clazz->interfaceCount; ++i) { + for (int i = 0; i < clazz->interfaceCount; ++i) { (*visitor)(&clazz->interfaces[i], arg); } } @@ -121,8 +117,7 @@ static void visitArrayObject(Visitor *visitor, Object *obj, void *arg) if (IS_CLASS_FLAG_SET(obj->clazz, CLASS_ISOBJECTARRAY)) { ArrayObject *array = (ArrayObject *)obj; Object **contents = (Object **)(void *)array->contents; - size_t i; - for (i = 0; i < array->length; ++i) { + for (size_t i = 0; i < array->length; ++i) { (*visitor)(&contents[i], arg); } } diff --git a/vm/hprof/HprofHeap.cpp b/vm/hprof/HprofHeap.cpp index 8ec0100ee..25100c786 100644 --- a/vm/hprof/HprofHeap.cpp +++ b/vm/hprof/HprofHeap.cpp @@ -282,10 +282,9 @@ hprofDumpHeapObject(hprof_context_t *ctx, const Object *obj) if (clazz == gDvm.classJavaLangClass) { const ClassObject *thisClass = (const ClassObject *)obj; - int i, sFieldCount, iFieldCount; /* obj is a ClassObject. */ - sFieldCount = thisClass->sfieldCount; + int sFieldCount = thisClass->sfieldCount; if (sFieldCount != 0) { int byteLength = sFieldCount*sizeof(StaticField); /* Create a byte array to reflect the allocation of the @@ -296,7 +295,7 @@ hprofDumpHeapObject(hprof_context_t *ctx, const Object *obj) hprofAddU4ToRecord(rec, stackTraceSerialNumber(obj)); hprofAddU4ToRecord(rec, byteLength); hprofAddU1ToRecord(rec, hprof_basic_byte); - for (i = 0; i < byteLength; i++) { + for (int i = 0; i < byteLength; i++) { hprofAddU1ToRecord(rec, 0); } } @@ -331,7 +330,7 @@ hprofDumpHeapObject(hprof_context_t *ctx, const Object *obj) hprofLookupStringId(STATIC_OVERHEAD_NAME)); hprofAddU1ToRecord(rec, hprof_basic_object); hprofAddIdToRecord(rec, CLASS_STATICS_ID(obj)); - for (i = 0; i < sFieldCount; i++) { + for (int i = 0; i < sFieldCount; i++) { hprof_basic_type t; size_t size; const StaticField *f = &thisClass->sfields[i]; @@ -355,9 +354,9 @@ hprofDumpHeapObject(hprof_context_t *ctx, const Object *obj) /* Instance fields for this class (no superclass fields) */ - iFieldCount = thisClass->ifieldCount; + int iFieldCount = thisClass->ifieldCount; hprofAddU2ToRecord(rec, (u2)iFieldCount); - for (i = 0; i < iFieldCount; i++) { + for (int i = 0; i < iFieldCount; i++) { const InstField *f = &thisClass->ifields[i]; hprof_basic_type t; @@ -444,10 +443,8 @@ hprofDumpHeapObject(hprof_context_t *ctx, const Object *obj) */ sclass = clazz; while (sclass != NULL) { - int i, ifieldCount; - - ifieldCount = sclass->ifieldCount; - for (i = 0; i < ifieldCount; i++) { + int ifieldCount = sclass->ifieldCount; + for (int i = 0; i < ifieldCount; i++) { const InstField *f = &sclass->ifields[i]; hprof_basic_type t; size_t size; diff --git a/vm/hprof/HprofOutput.cpp b/vm/hprof/HprofOutput.cpp index b84b29852..154323354 100644 --- a/vm/hprof/HprofOutput.cpp +++ b/vm/hprof/HprofOutput.cpp @@ -251,19 +251,15 @@ hprofAddUtf8StringToRecord(hprof_record_t *rec, const char *str) int hprofAddU2ListToRecord(hprof_record_t *rec, const u2 *values, size_t numValues) { - unsigned char *insert; - size_t i; - int err; - - err = guaranteeRecordAppend(rec, numValues * 2); + int err = guaranteeRecordAppend(rec, numValues * 2); if (err != 0) { return err; } //xxx this can be way smarter //xxx also, don't do this bytewise if aligned and on a matching-endian arch - insert = rec->body + rec->length; - for (i = 0; i < numValues; i++) { + unsigned char *insert = rec->body + rec->length; + for (size_t i = 0; i < numValues; i++) { U2_TO_BUF_BE(insert, 0, *values++); insert += sizeof(*values); } @@ -281,19 +277,15 @@ hprofAddU2ToRecord(hprof_record_t *rec, u2 value) int hprofAddU4ListToRecord(hprof_record_t *rec, const u4 *values, size_t numValues) { - unsigned char *insert; - size_t i; - int err; - - err = guaranteeRecordAppend(rec, numValues * 4); + int err = guaranteeRecordAppend(rec, numValues * 4); if (err != 0) { return err; } //xxx this can be way smarter //xxx also, don't do this bytewise if aligned and on a matching-endian arch - insert = rec->body + rec->length; - for (i = 0; i < numValues; i++) { + unsigned char *insert = rec->body + rec->length; + for (size_t i = 0; i < numValues; i++) { U4_TO_BUF_BE(insert, 0, *values++); insert += sizeof(*values); } @@ -311,19 +303,15 @@ hprofAddU4ToRecord(hprof_record_t *rec, u4 value) int hprofAddU8ListToRecord(hprof_record_t *rec, const u8 *values, size_t numValues) { - unsigned char *insert; - size_t i; - int err; - - err = guaranteeRecordAppend(rec, numValues * 8); + int err = guaranteeRecordAppend(rec, numValues * 8); if (err != 0) { return err; } //xxx this can be way smarter //xxx also, don't do this bytewise if aligned and on a matching-endian arch - insert = rec->body + rec->length; - for (i = 0; i < numValues; i++) { + unsigned char *insert = rec->body + rec->length; + for (size_t i = 0; i < numValues; i++) { U8_TO_BUF_BE(insert, 0, *values++); insert += sizeof(*values); } |