Add mprotect helper function to MemMap class.

Change-Id: I3967b76301d339de987fb8e094adbde4ef18573d
diff --git a/src/dex_file.cc b/src/dex_file.cc
index f18b215..3ba671e 100644
--- a/src/dex_file.cc
+++ b/src/dex_file.cc
@@ -96,9 +96,7 @@
 }
 
 void DexFile::ChangePermissions(int prot) const {
-  if (mprotect(mem_map_->Begin(), mem_map_->Size(), prot) != 0) {
-    PLOG(FATAL) << "Failed to change dex file permissions to " << prot << " for " << GetLocation();
-  }
+  mem_map_->Protect(prot);
 }
 
 const DexFile* DexFile::OpenFile(const std::string& filename,
diff --git a/src/mem_map.cc b/src/mem_map.cc
index ba34d8b..8201fa8 100644
--- a/src/mem_map.cc
+++ b/src/mem_map.cc
@@ -223,4 +223,18 @@
   CHECK_NE(base_size_, 0U);
 };
 
+
+bool MemMap::Protect(int prot) {
+  if (base_begin_ == NULL && base_size_ == 0) {
+    return true;
+  }
+
+  if (mprotect(base_begin_, base_size_, prot) == 0) {
+    return true;
+  }
+
+  PLOG(ERROR) << "mprotect(" << base_begin_ << ", " << base_size_ << ", " << prot << ") failed";
+  return false;
+}
+
 }  // namespace art
diff --git a/src/mem_map.h b/src/mem_map.h
index ce2f4fa..87c45a9 100644
--- a/src/mem_map.h
+++ b/src/mem_map.h
@@ -57,6 +57,8 @@
   // Releases the memory mapping
   ~MemMap();
 
+  bool Protect(int prot);
+
   byte* Begin() const {
     return begin_;
   }