Revisions to "First pass of ImageWriter"

Cleanup from accidental commit of:

    commit db4d54081f09abcbe97ffdf615874f2809a9e777
    Author: Brian Carlstrom <bdc@google.com>
    Date:   Tue Aug 9 12:18:28 2011 -0700

        First pass of ImageWriter

Change-Id: I0168c05d229e0c7f8059dc1ab1b46dc2dc426c53
diff --git a/src/dex_file.cc b/src/dex_file.cc
index 0ccdba4..22e4430 100644
--- a/src/dex_file.cc
+++ b/src/dex_file.cc
@@ -155,10 +155,10 @@
   if (OS::FileExists(adjacent_dex_filename.c_str())) {
     DexFile* adjacent_dex_file = DexFile::OpenFile(adjacent_dex_filename);
     if (adjacent_dex_file != NULL) {
-        // We don't verify anything in this case, because we aren't in
-        // the cache and typically the file is in the readonly /system
-        // area, so if something is wrong, there is nothing we can do.
-        return adjacent_dex_file;
+      // We don't verify anything in this case, because we aren't in
+      // the cache and typically the file is in the readonly /system
+      // area, so if something is wrong, there is nothing we can do.
+      return adjacent_dex_file;
     }
     return NULL;
   }
diff --git a/src/file_test.cc b/src/file_test.cc
index d019d09..4afc834 100644
--- a/src/file_test.cc
+++ b/src/file_test.cc
@@ -13,7 +13,7 @@
 
 TEST_F(FileTest, Read) {
   std::string filename = GetLibCoreDexFileName();
-  scoped_ptr<File> file(OS::OpenBinaryFile(filename.c_str(), false));
+  scoped_ptr<File> file(OS::OpenFile(filename.c_str(), false));
   ASSERT_TRUE(file != NULL);
   EXPECT_STREQ(filename.c_str(), file->name());
   char buffer[3];
@@ -27,7 +27,7 @@
 
 TEST_F(FileTest, FileLength) {
   std::string filename = GetLibCoreDexFileName();
-  scoped_ptr<File> file(OS::OpenBinaryFile(filename.c_str(), false));
+  scoped_ptr<File> file(OS::OpenFile(filename.c_str(), false));
   ASSERT_TRUE(file != NULL);
   EXPECT_NE(0, file->Length());
 }
@@ -35,7 +35,7 @@
 
 TEST_F(FileTest, FilePosition) {
   std::string filename = GetLibCoreDexFileName();
-  scoped_ptr<File> file(OS::OpenBinaryFile(filename.c_str(), false));
+  scoped_ptr<File> file(OS::OpenFile(filename.c_str(), false));
   ASSERT_TRUE(file != NULL);
   char buf[4];
   EXPECT_TRUE(file->ReadFully(buf, 2));
diff --git a/src/image_writer.cc b/src/image_writer.cc
index 6ac5803..7240c3d 100644
--- a/src/image_writer.cc
+++ b/src/image_writer.cc
@@ -23,11 +23,11 @@
   CalculateNewObjectOffsets();
   CopyAndFixupObjects();
 
-  scoped_ptr<File> file(OS::OpenBinaryFile(filename, true));
+  scoped_ptr<File> file(OS::OpenFile(filename, true));
   if (file == NULL) {
     return false;
   }
-  return file->WriteFully(mem_map_->GetAddress(), top_);
+  return file->WriteFully(image_->GetAddress(), image_top_);
 }
 
 bool ImageWriter::Init(Space* space) {
@@ -35,31 +35,32 @@
   int prot = PROT_READ | PROT_WRITE;
   int flags = MAP_PRIVATE | MAP_ANONYMOUS;
   size_t length = RoundUp(size, kPageSize);
-  mem_map_.reset(MemMap::Map(length, prot, flags));
-  if (mem_map_ == NULL) {
+  image_.reset(MemMap::Map(length, prot, flags));
+  if (image_ == NULL) {
     PLOG(ERROR) << "mmap failed";
     return false;
   }
   return true;
 }
 
-void ImageWriter::CalculateNewObjectOffsets() {
-  HeapBitmap* heap_bitmap = Heap::GetLiveBits();
-  DCHECK(heap_bitmap != NULL);
-  DCHECK_EQ(0U, top_);
-  top_ += sizeof(uint64_t);  // leave a header, ensures objects have non-zero offset for DCHECKs
-  heap_bitmap->Walk(CalculateNewObjectOffsetsCallback, this);
-  DCHECK_LT(top_, mem_map_->GetLength());
-  // Note that top_ is left at end of used space
-}
-
 void ImageWriter::CalculateNewObjectOffsetsCallback(Object *obj, void *arg) {
   DCHECK(obj != NULL);
   DCHECK(arg != NULL);
   ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
-  image_writer->SetImageOffset(obj, image_writer->top_);
-  image_writer->top_ += RoundUp(obj->Size(), 8);  // 64-bit alignment
-  DCHECK_LT(image_writer->top_, image_writer->mem_map_->GetLength());
+  image_writer->SetImageOffset(obj, image_writer->image_top_);
+  image_writer->image_top_ += RoundUp(obj->Size(), 8);  // 64-bit alignment
+  DCHECK_LT(image_writer->image_top_, image_writer->image_->GetLength());
+}
+
+void ImageWriter::CalculateNewObjectOffsets() {
+  HeapBitmap* heap_bitmap = Heap::GetLiveBits();
+  DCHECK(heap_bitmap != NULL);
+  DCHECK_EQ(0U, image_top_);
+  // leave a header, ensures objects have non-zero offset for DCHECKs
+  image_top_ += 8; // 64-bit-alignment
+  heap_bitmap->Walk(CalculateNewObjectOffsetsCallback, this);
+  DCHECK_LT(image_top_, image_->GetLength());
+  // Note that top_ is left at end of used space
 }
 
 void ImageWriter::CopyAndFixupObjects() {
@@ -74,10 +75,10 @@
   ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
 
   size_t offset = image_writer->GetImageOffset(obj);
-  byte* dst = image_writer->mem_map_->GetAddress() + offset;
+  byte* dst = image_writer->image_->GetAddress() + offset;
   byte* src = reinterpret_cast<byte*>(obj);
   size_t n = obj->Size();
-  DCHECK_LT(offset + n, image_writer->mem_map_->GetLength());
+  DCHECK_LT(offset + n, image_writer->image_->GetLength());
   memcpy(dst, src, n);
   Object* copy = reinterpret_cast<Object*>(dst);
   image_writer->FixupObject(obj, copy);
diff --git a/src/image_writer.h b/src/image_writer.h
index ac4a557..0af45c6 100644
--- a/src/image_writer.h
+++ b/src/image_writer.h
@@ -13,10 +13,11 @@
 
 namespace art {
 
+// Write a Space built during compilation for use during execution.
 class ImageWriter {
 
  public:
-  ImageWriter() : top_(0), image_base_(NULL) {};
+  ImageWriter() : image_top_(0), image_base_(NULL) {};
   bool Write(Space* space, const char* filename, byte* image_base);
   ~ImageWriter() {};
 
@@ -54,10 +55,10 @@
   void FixupInstanceFields(Object* orig, Object* copy);
 
   // memory mapped for generating the image
-  scoped_ptr<MemMap> mem_map_;
+  scoped_ptr<MemMap> image_;
 
-  // Offset to the free space in mem_map_ 
-  size_t top_;
+  // Offset to the free space in image_
+  size_t image_top_;
 
   // Target base address for the output image
   byte* image_base_;
diff --git a/src/jni_internal.cc b/src/jni_internal.cc
index 0e9cb84..71d1204 100644
--- a/src/jni_internal.cc
+++ b/src/jni_internal.cc
@@ -1890,7 +1890,7 @@
 };
 
 void MonitorEnterHelper(JNIEnv* env, jobject obj) {
-  env = Thread::Current()->GetJniEnv();  // XXX bdc do not commit workaround
+  // env = Thread::Current()->GetJniEnv();  // uncomment this if you want your tests to pass
   CHECK_EQ(Thread::Current()->GetJniEnv(), env);
   MonitorEnter(env, obj);  // Ignore the result.
 }
diff --git a/src/mem_map.h b/src/mem_map.h
index 4625f91..9357b9b 100644
--- a/src/mem_map.h
+++ b/src/mem_map.h
@@ -67,7 +67,7 @@
 
   ~MemMap() {
     Unmap();
-  };
+  }
 
   // Release a memory mapping, returning true on success or it was previously unmapped.
   bool Unmap() {
diff --git a/src/memory_region.h b/src/memory_region.h
index f1a3ec5..3d04d91 100644
--- a/src/memory_region.h
+++ b/src/memory_region.h
@@ -76,6 +76,8 @@
 
   void* pointer_;
   size_t size_;
+
+  DISALLOW_COPY_AND_ASSIGN(MemoryRegion);
 };
 
 }  // namespace art
diff --git a/src/os.h b/src/os.h
index 440c997..dda5a30 100644
--- a/src/os.h
+++ b/src/os.h
@@ -13,8 +13,7 @@
  public:
 
   // Open a file. The returned file must be deleted by the caller.
-  static File* OpenBinaryFile(const char* name, bool writable);
-  static File* OpenTextFile(const char* name, bool writable);
+  static File* OpenFile(const char* name, bool writable);
 
   // Create a file from an already open file descriptor
   static File* FileFromFd(const char* name, int fd);
diff --git a/src/os_linux.cc b/src/os_linux.cc
index da8ea38..8f5f56a 100644
--- a/src/os_linux.cc
+++ b/src/os_linux.cc
@@ -11,7 +11,7 @@
 
 namespace art {
 
-File* OS::OpenBinaryFile(const char* name, bool writable) {
+File* OS::OpenFile(const char* name, bool writable) {
   int flags = O_RDONLY;
   if (writable) {
     flags = (O_RDWR | O_CREAT | O_TRUNC);
@@ -23,10 +23,6 @@
   return new LinuxFile(name, fd, true);
 }
 
-File* OS::OpenTextFile(const char* name, bool writable) {
-  return OpenBinaryFile(name, writable);
-}
-
 File* OS::FileFromFd(const char* name, int fd) {
   return new LinuxFile(name, fd, false);
 }