Merge "Fix broken card table asserts."
diff --git a/vm/alloc/CardTable.cpp b/vm/alloc/CardTable.cpp
index 2c81fd1..28fe58e 100644
--- a/vm/alloc/CardTable.cpp
+++ b/vm/alloc/CardTable.cpp
@@ -54,9 +54,12 @@
     void *allocBase;
     u1 *biasedBase;
     GcHeap *gcHeap = gDvm.gcHeap;
+    int offset;
     void *heapBase = dvmHeapSourceGetBase();
     assert(gcHeap != NULL);
     assert(heapBase != NULL);
+    /* All zeros is the correct initial value; all clean. */
+    assert(GC_CARD_CLEAN == 0);
 
     /* Set up the card table */
     length = heapMaximumSize / GC_CARD_SIZE;
@@ -69,17 +72,11 @@
     gcHeap->cardTableBase = (u1*)allocBase;
     gcHeap->cardTableLength = growthLimit / GC_CARD_SIZE;
     gcHeap->cardTableMaxLength = length;
-    gcHeap->cardTableOffset = 0;
-    /* All zeros is the correct initial value; all clean. */
-    assert(GC_CARD_CLEAN == 0);
-
     biasedBase = (u1 *)((uintptr_t)allocBase -
-                        ((uintptr_t)heapBase >> GC_CARD_SHIFT));
-    if (((uintptr_t)biasedBase & 0xff) != GC_CARD_DIRTY) {
-        int offset = GC_CARD_DIRTY - ((uintptr_t)biasedBase & 0xff);
-        gcHeap->cardTableOffset = offset + (offset < 0 ? 0x100 : 0);
-        biasedBase += gcHeap->cardTableOffset;
-    }
+                       ((uintptr_t)heapBase >> GC_CARD_SHIFT));
+    offset = GC_CARD_DIRTY - ((uintptr_t)biasedBase & 0xff);
+    gcHeap->cardTableOffset = offset + (offset < 0 ? 0x100 : 0);
+    biasedBase += gcHeap->cardTableOffset;
     assert(((uintptr_t)biasedBase & 0xff) == GC_CARD_DIRTY);
     gDvm.biasedCardTableBase = biasedBase;
 
@@ -165,7 +162,7 @@
 }
 
 /*
- * Returns the address of the relevent byte in the card table, given
+ * Returns the address of the relevant byte in the card table, given
  * an address on the heap.
  */
 u1 *dvmCardFromAddr(const void *addr)
diff --git a/vm/alloc/MarkSweep.cpp b/vm/alloc/MarkSweep.cpp
index eb739e5..2781a7c 100644
--- a/vm/alloc/MarkSweep.cpp
+++ b/vm/alloc/MarkSweep.cpp
@@ -558,8 +558,9 @@
     const u1 *base, *limit, *ptr, *dirty;
 
     base = &h->cardTableBase[0];
-    limit = dvmCardFromAddr((u1 *)dvmHeapSourceGetLimit());
-    assert(limit <= &h->cardTableBase[h->cardTableLength]);
+    // The limit is the card one after the last accessible card.
+    limit = dvmCardFromAddr((u1 *)dvmHeapSourceGetLimit() - GC_CARD_SIZE) + 1;
+    assert(limit <= &base[h->cardTableOffset + h->cardTableLength]);
 
     ptr = base;
     for (;;) {