Directory restructuring of object.h

Break object.h into constituent files.
Reduce number of #includes in other GC header files.
Introduce -inl.h files to avoid mirror files #include-ing each other.
Check invariants of verifier RegTypes for all constructors.

Change-Id: Iecf1171c02910ac152d52947330ef456df4043bc
diff --git a/src/gc/card_table.h b/src/gc/card_table.h
index 8f1bc92..842fcc3 100644
--- a/src/gc/card_table.h
+++ b/src/gc/card_table.h
@@ -17,19 +17,18 @@
 #ifndef ART_SRC_GC_CARDTABLE_H_
 #define ART_SRC_GC_CARDTABLE_H_
 
-#include "base/logging.h"
 #include "globals.h"
+#include "locks.h"
 #include "mem_map.h"
-#include "space_bitmap.h"
 #include "UniquePtr.h"
-#include "utils.h"
 
 namespace art {
-
+namespace mirror {
+class Object;
+}  // namespace mirror
 class Heap;
 class ContinuousSpace;
 class SpaceBitmap;
-class Object;
 
 // Maintain a card table from the the write barrier. All writes of
 // non-NULL values to heap addresses should go through an entry in
@@ -50,12 +49,12 @@
   }
 
   // Is the object on a dirty card?
-  bool IsDirty(const Object* obj) const {
+  bool IsDirty(const mirror::Object* obj) const {
     return GetCard(obj) == kCardDirty;
   }
 
   // Return the state of the card at an address.
-  byte GetCard(const Object* obj) const {
+  byte GetCard(const mirror::Object* obj) const {
     return *CardFromAddr(obj);
   }
 
@@ -88,71 +87,7 @@
    */
   template <typename Visitor, typename ModifiedVisitor>
   void ModifyCardsAtomic(byte* scan_begin, byte* scan_end, const Visitor& visitor,
-                      const ModifiedVisitor& modified = VoidFunctor()) {
-    byte* card_cur = CardFromAddr(scan_begin);
-    byte* card_end = CardFromAddr(scan_end);
-    CheckCardValid(card_cur);
-    CheckCardValid(card_end);
-
-    // Handle any unaligned cards at the start.
-    while (!IsAligned<sizeof(word)>(card_cur) && card_cur < card_end) {
-      byte expected, new_value;
-      do {
-        expected = *card_cur;
-        new_value = visitor(expected);
-      } while (expected != new_value && UNLIKELY(byte_cas(expected, new_value, card_cur) != 0));
-      if (expected != new_value) {
-        modified(card_cur, expected, new_value);
-      }
-      ++card_cur;
-    }
-
-    // Handle unaligned cards at the end.
-    while (!IsAligned<sizeof(word)>(card_end) && card_end > card_cur) {
-      --card_end;
-      byte expected, new_value;
-      do {
-        expected = *card_end;
-        new_value = visitor(expected);
-      } while (expected != new_value && UNLIKELY(byte_cas(expected, new_value, card_end) != 0));
-      if (expected != new_value) {
-        modified(card_cur, expected, new_value);
-      }
-    }
-
-    // Now we have the words, we can process words in parallel.
-    uintptr_t* word_cur = reinterpret_cast<uintptr_t*>(card_cur);
-    uintptr_t* word_end = reinterpret_cast<uintptr_t*>(card_end);
-    uintptr_t expected_word;
-    uintptr_t new_word;
-
-    // TODO: Parallelize.
-    while (word_cur < word_end) {
-      while ((expected_word = *word_cur) != 0) {
-        new_word =
-            (visitor((expected_word >> 0) & 0xFF) << 0) |
-            (visitor((expected_word >> 8) & 0xFF) << 8) |
-            (visitor((expected_word >> 16) & 0xFF) << 16) |
-            (visitor((expected_word >> 24) & 0xFF) << 24);
-        if (new_word == expected_word) {
-          // No need to do a cas.
-          break;
-        }
-        if (LIKELY(android_atomic_cas(expected_word, new_word,
-                                      reinterpret_cast<int32_t*>(word_cur)) == 0)) {
-          for (size_t i = 0; i < sizeof(uintptr_t); ++i) {
-            const byte expected_byte = (expected_word >> (8 * i)) & 0xFF;
-            const byte new_byte = (new_word >> (8 * i)) & 0xFF;
-            if (expected_byte != new_byte) {
-              modified(reinterpret_cast<byte*>(word_cur) + i, expected_byte, new_byte);
-            }
-          }
-          break;
-        }
-      }
-      ++word_cur;
-    }
-  }
+                         const ModifiedVisitor& modified);
 
   // For every dirty at least minumum age between begin and end invoke the visitor with the
   // specified argument.
@@ -161,67 +96,7 @@
             const Visitor& visitor, const FingerVisitor& finger_visitor,
             const byte minimum_age = kCardDirty) const
       EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
-      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
-    DCHECK(bitmap->HasAddress(scan_begin));
-    DCHECK(bitmap->HasAddress(scan_end - 1));  // scan_end is the byte after the last byte we scan.
-    byte* card_cur = CardFromAddr(scan_begin);
-    byte* card_end = CardFromAddr(scan_end);
-    CheckCardValid(card_cur);
-    CheckCardValid(card_end);
-
-    // Handle any unaligned cards at the start.
-    while (!IsAligned<sizeof(word)>(card_cur) && card_cur < card_end) {
-      if (*card_cur >= minimum_age) {
-        uintptr_t start = reinterpret_cast<uintptr_t>(AddrFromCard(card_cur));
-        uintptr_t end = start + kCardSize;
-        bitmap->VisitMarkedRange(start, end, visitor, finger_visitor);
-      }
-      ++card_cur;
-    }
-
-    byte* aligned_end = card_end -
-        (reinterpret_cast<uintptr_t>(card_end) & (sizeof(uintptr_t) - 1));
-
-    // Now we have the words, we can send these to be processed in parallel.
-    uintptr_t* word_cur = reinterpret_cast<uintptr_t*>(card_cur);
-    uintptr_t* word_end = reinterpret_cast<uintptr_t*>(aligned_end);
-
-    // TODO: Parallelize
-    while (word_cur < word_end) {
-      // Find the first dirty card.
-      while (*word_cur == 0 && word_cur < word_end) {
-        word_cur++;
-      }
-      if (word_cur >= word_end) {
-        break;
-      }
-      uintptr_t start_word = *word_cur;
-      for (size_t i = 0; i < sizeof(uintptr_t); ++i) {
-        if ((start_word & 0xFF) >= minimum_age) {
-          byte* card = reinterpret_cast<byte*>(word_cur) + i;
-          const byte card_byte = *card;
-          DCHECK(card_byte == (start_word & 0xFF) || card_byte == kCardDirty)
-              << "card " << static_cast<size_t>(card_byte) << " word " << (start_word & 0xFF);
-          uintptr_t start = reinterpret_cast<uintptr_t>(AddrFromCard(card));
-          uintptr_t end = start + kCardSize;
-          bitmap->VisitMarkedRange(start, end, visitor, finger_visitor);
-        }
-        start_word >>= 8;
-      }
-      ++word_cur;
-    }
-
-    // Handle any unaligned cards at the end.
-    card_cur = reinterpret_cast<byte*>(word_end);
-    while (card_cur < card_end) {
-      if (*card_cur >= minimum_age) {
-        uintptr_t start = reinterpret_cast<uintptr_t>(AddrFromCard(card_cur));
-        uintptr_t end = start + kCardSize;
-        bitmap->VisitMarkedRange(start, end, visitor, finger_visitor);
-      }
-      ++card_cur;
-    }
-  }
+      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
 
   // Assertion used to check the given address is covered by the card table
   void CheckAddrIsInCardTable(const byte* addr) const;
@@ -233,40 +108,14 @@
   void ClearSpaceCards(ContinuousSpace* space);
 
   // Returns the first address in the heap which maps to this card.
-  void* AddrFromCard(const byte *card_addr) const {
-    DCHECK(IsValidCard(card_addr))
-      << " card_addr: " << reinterpret_cast<const void*>(card_addr)
-      << " begin: " << reinterpret_cast<void*>(mem_map_->Begin() + offset_)
-      << " end: " << reinterpret_cast<void*>(mem_map_->End());
-    uintptr_t offset = card_addr - biased_begin_;
-    return reinterpret_cast<void*>(offset << kCardShift);
-  }
+  void* AddrFromCard(const byte *card_addr) const;
 
   // Returns the address of the relevant byte in the card table, given an address on the heap.
-  byte* CardFromAddr(const void *addr) const {
-    byte *card_addr = biased_begin_ + (reinterpret_cast<uintptr_t>(addr) >> kCardShift);
-    // Sanity check the caller was asking for address covered by the card table
-    DCHECK(IsValidCard(card_addr)) << "addr: " << addr
-        << " card_addr: " << reinterpret_cast<void*>(card_addr);
-    return card_addr;
-  }
+  byte* CardFromAddr(const void *addr) const;
 
   bool AddrIsInCardTable(const void* addr) const;
 
  private:
-  static int byte_cas(byte old_value, byte new_value, byte* address) {
-    // Little endian means most significant byte is on the left.
-    const size_t shift = reinterpret_cast<uintptr_t>(address) % sizeof(uintptr_t);
-    // Align the address down.
-    address -= shift;
-    int32_t* word_address = reinterpret_cast<int32_t*>(address);
-    // Word with the byte we are trying to cas cleared.
-    const int32_t cur_word = *word_address & ~(0xFF << shift);
-    const int32_t old_word = cur_word | (static_cast<int32_t>(old_value) << shift);
-    const int32_t new_word = cur_word | (static_cast<int32_t>(new_value) << shift);
-    return android_atomic_cas(old_word, new_word, word_address);
-  }
-
   CardTable(MemMap* begin, byte* biased_begin, size_t offset);
 
   // Returns true iff the card table address is within the bounds of the card table.
@@ -276,12 +125,7 @@
     return card_addr >= begin && card_addr < end;
   }
 
-  void CheckCardValid(byte* card) const {
-    DCHECK(IsValidCard(card))
-        << " card_addr: " << reinterpret_cast<const void*>(card)
-        << " begin: " << reinterpret_cast<void*>(mem_map_->Begin() + offset_)
-        << " end: " << reinterpret_cast<void*>(mem_map_->End());
-  }
+  void CheckCardValid(byte* card) const;
 
   // Verifies that all gray objects are on a dirty card.
   void VerifyCardTable();