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
diff --git a/vm/alloc/CardTable.cpp b/vm/alloc/CardTable.cpp
index 533d5bc..d29bfa5 100644
--- a/vm/alloc/CardTable.cpp
+++ b/vm/alloc/CardTable.cpp
@@ -152,8 +152,7 @@
     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 @@
     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 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 097efae..3dd1c1b 100644
--- a/vm/alloc/Copying.cpp
+++ b/vm/alloc/Copying.cpp
@@ -245,9 +245,7 @@
 
 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 *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 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 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 @@
         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 @@
         }
         /* 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 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 @@
 
 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 @@
     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 @@
 
 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 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 scavengeClassObject(ClassObject *obj)
 {
-    int i;
-
     LOG_SCAV("scavengeClassObject(obj=%p)", obj);
     assert(obj != NULL);
     assert(obj->obj.clazz != NULL);
@@ -929,14 +905,14 @@
     /* 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 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 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 @@
     } 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 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 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 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 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 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 @@
 
             const RegisterMap* pMap;
             const u1* regVector;
-            int i;
 
             Method* nonConstMethod = (Method*) method;  // quiet gcc
             pMap = dvmGetExpandedRegisterMap(nonConstMethod);
@@ -1705,7 +1656,7 @@
                  * 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 @@
     Method *method;
     const char *shorty;
     Object *obj;
-    int i;
 
     saveArea = NULL;
     framePtr = (const u4 *)thread->curFrame;
@@ -1841,7 +1791,7 @@
                 }
             }
             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 @@
                 /*
                  * 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 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 @@
     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 @@
 
 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 ffeff62..0ffd3b0 100644
--- a/vm/alloc/HeapBitmap.cpp
+++ b/vm/alloc/HeapBitmap.cpp
@@ -100,14 +100,11 @@
 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 @@
     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 @@
                             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 @@
          */
         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 50303b7..0f47fba 100644
--- a/vm/alloc/HeapSource.cpp
+++ b/vm/alloc/HeapSource.cpp
@@ -234,11 +234,10 @@
 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 @@
     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 @@
                              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 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 @@
     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 @@
             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 @@
             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 @@
 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 @@
     /* 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 @@
                   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 9b527cb..6fbde9c 100644
--- a/vm/alloc/MarkSweep.cpp
+++ b/vm/alloc/MarkSweep.cpp
@@ -196,15 +196,13 @@
     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 @@
             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 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 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 @@
     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 @@
     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 @@
     SweepContext ctx;
     HeapBitmap *prevLive, *prevMark;
     size_t numHeaps, numSweepHeaps;
-    size_t i;
 
     numHeaps = dvmHeapSourceGetNumHeaps();
     dvmHeapSourceGetRegions(base, max, NULL, numHeaps);
@@ -1080,7 +1071,7 @@
     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 6ca1439..b329595 100644
--- a/vm/alloc/Visit.cpp
+++ b/vm/alloc/Visit.cpp
@@ -36,12 +36,10 @@
 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 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 @@
     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 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 @@
                  * 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 @@
                  * 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 eb889af..479c3c0 100644
--- a/vm/alloc/VisitInlines.h
+++ b/vm/alloc/VisitInlines.h
@@ -35,11 +35,11 @@
             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 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 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 @@
     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 8ec0100..25100c7 100644
--- a/vm/hprof/HprofHeap.cpp
+++ b/vm/hprof/HprofHeap.cpp
@@ -282,10 +282,9 @@
 
         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 @@
                 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 @@
                                    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 @@
 
             /* 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 @@
              */
             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 b84b298..1543233 100644
--- a/vm/hprof/HprofOutput.cpp
+++ b/vm/hprof/HprofOutput.cpp
@@ -251,19 +251,15 @@
 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 @@
 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 @@
 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);
     }