Simplify JIT memory region creation.

Have JitCodeCache only call JitMemoryRegion::Initialize.

Test: m
Change-Id: Idc6f75bf7ebb94fb2dfd0a73b02d8e0befefa85e
diff --git a/runtime/jit/jit_code_cache.cc b/runtime/jit/jit_code_cache.cc
index 56a2b6a..2edf770 100644
--- a/runtime/jit/jit_code_cache.cc
+++ b/runtime/jit/jit_code_cache.cc
@@ -178,6 +178,7 @@
     }
   }
 
+  size_t initial_capacity = Runtime::Current()->GetJITOptions()->GetCodeCacheInitialCapacity();
   // Check whether the provided max capacity in options is below 1GB.
   size_t max_capacity = Runtime::Current()->GetJITOptions()->GetCodeCacheMaxCapacity();
   // We need to have 32 bit offsets from method headers in code cache which point to things
@@ -191,24 +192,22 @@
     return nullptr;
   }
 
-  size_t initial_capacity = Runtime::Current()->GetJITOptions()->GetCodeCacheInitialCapacity();
-
-  std::unique_ptr<JitCodeCache> jit_code_cache(new JitCodeCache());
-
   MutexLock mu(Thread::Current(), *Locks::jit_lock_);
-  jit_code_cache->private_region_.InitializeState(initial_capacity, max_capacity);
-
-  // Zygote should never collect code to share the memory with the children.
-  if (is_zygote) {
-    jit_code_cache->garbage_collect_code_ = false;
-  }
-
-  if (!jit_code_cache->private_region_.InitializeMappings(
-        rwx_memory_allowed, is_zygote, error_msg)) {
+  JitMemoryRegion region;
+  if (!region.Initialize(initial_capacity,
+                         max_capacity,
+                         rwx_memory_allowed,
+                         is_zygote,
+                         error_msg)) {
     return nullptr;
   }
 
-  jit_code_cache->private_region_.InitializeSpaces();
+  std::unique_ptr<JitCodeCache> jit_code_cache(new JitCodeCache());
+  if (is_zygote) {
+    // Zygote should never collect code to share the memory with the children.
+    jit_code_cache->garbage_collect_code_ = false;
+  }
+  jit_code_cache->private_region_ = std::move(region);
 
   VLOG(jit) << "Created jit code cache: initial capacity="
             << PrettySize(initial_capacity)
@@ -1833,17 +1832,14 @@
 
   size_t initial_capacity = Runtime::Current()->GetJITOptions()->GetCodeCacheInitialCapacity();
   size_t max_capacity = Runtime::Current()->GetJITOptions()->GetCodeCacheMaxCapacity();
-
-  private_region_.InitializeState(initial_capacity, max_capacity);
-
   std::string error_msg;
-  if (!private_region_.InitializeMappings(
-          /* rwx_memory_allowed= */ !is_system_server, is_zygote, &error_msg)) {
-    LOG(WARNING) << "Could not reset JIT state after zygote fork: " << error_msg;
-    return;
+  if (!private_region_.Initialize(initial_capacity,
+                                  max_capacity,
+                                  /* rwx_memory_allowed= */ !is_system_server,
+                                  is_zygote,
+                                  &error_msg)) {
+    LOG(WARNING) << "Could not create private region after zygote fork: " << error_msg;
   }
-
-  private_region_.InitializeSpaces();
 }
 
 }  // namespace jit
diff --git a/runtime/jit/jit_memory_region.cc b/runtime/jit/jit_memory_region.cc
index 3c08ff6..df61aaa 100644
--- a/runtime/jit/jit_memory_region.cc
+++ b/runtime/jit/jit_memory_region.cc
@@ -40,11 +40,22 @@
 // TODO: Make this variable?
 static constexpr size_t kCodeAndDataCapacityDivider = 2;
 
-bool JitMemoryRegion::InitializeMappings(bool rwx_memory_allowed,
-                                         bool is_zygote,
-                                         std::string* error_msg) {
+bool JitMemoryRegion::Initialize(size_t initial_capacity,
+                                 size_t max_capacity,
+                                 bool rwx_memory_allowed,
+                                 bool is_zygote,
+                                 std::string* error_msg) {
   ScopedTrace trace(__PRETTY_FUNCTION__);
 
+  CHECK_GE(max_capacity, initial_capacity);
+  CHECK(max_capacity <= 1 * GB) << "The max supported size for JIT code cache is 1GB";
+  // Align both capacities to page size, as that's the unit mspaces use.
+  initial_capacity_ = RoundDown(initial_capacity, 2 * kPageSize);
+  max_capacity_ = RoundDown(max_capacity, 2 * kPageSize);
+  current_capacity_ = initial_capacity,
+  data_end_ = initial_capacity / kCodeAndDataCapacityDivider;
+  exec_end_ = initial_capacity - data_end_;
+
   const size_t capacity = max_capacity_;
   const size_t data_capacity = capacity / kCodeAndDataCapacityDivider;
   const size_t exec_capacity = capacity - data_capacity;
@@ -202,21 +213,9 @@
   data_pages_ = std::move(data_pages);
   exec_pages_ = std::move(exec_pages);
   non_exec_pages_ = std::move(non_exec_pages);
-  return true;
-}
 
-void JitMemoryRegion::InitializeState(size_t initial_capacity, size_t max_capacity) {
-  CHECK_GE(max_capacity, initial_capacity);
-  CHECK(max_capacity <= 1 * GB) << "The max supported size for JIT code cache is 1GB";
-  // Align both capacities to page size, as that's the unit mspaces use.
-  initial_capacity_ = RoundDown(initial_capacity, 2 * kPageSize);
-  max_capacity_ = RoundDown(max_capacity, 2 * kPageSize);
-  current_capacity_ = initial_capacity,
-  data_end_ = initial_capacity / kCodeAndDataCapacityDivider;
-  exec_end_ = initial_capacity - data_end_;
-}
+  // Now that the pages are initialized, initialize the spaces.
 
-void JitMemoryRegion::InitializeSpaces() {
   // Initialize the data heap
   data_mspace_ = create_mspace_with_base(data_pages_.Begin(), data_end_, false /*locked*/);
   CHECK(data_mspace_ != nullptr) << "create_mspace_with_base (data) failed";
@@ -242,6 +241,8 @@
     exec_mspace_ = nullptr;
     SetFootprintLimit(initial_capacity_);
   }
+
+  return true;
 }
 
 void JitMemoryRegion::SetFootprintLimit(size_t new_footprint) {
diff --git a/runtime/jit/jit_memory_region.h b/runtime/jit/jit_memory_region.h
index b4a0b75..1512cad 100644
--- a/runtime/jit/jit_memory_region.h
+++ b/runtime/jit/jit_memory_region.h
@@ -40,17 +40,25 @@
 class JitMemoryRegion {
  public:
   JitMemoryRegion()
-      : used_memory_for_code_(0),
-        used_memory_for_data_(0) {}
+      : initial_capacity_(0),
+        max_capacity_(0),
+        current_capacity_(0),
+        data_end_(0),
+        exec_end_(0),
+        used_memory_for_code_(0),
+        used_memory_for_data_(0),
+        exec_pages_(),
+        non_exec_pages_(),
+        data_mspace_(nullptr),
+        exec_mspace_(nullptr) {}
 
-  void InitializeState(size_t initial_capacity, size_t max_capacity)
+  bool Initialize(size_t initial_capacity,
+                  size_t max_capacity,
+                  bool rwx_memory_allowed,
+                  bool is_zygote,
+                  std::string* error_msg)
       REQUIRES(Locks::jit_lock_);
 
-  bool InitializeMappings(bool rwx_memory_allowed, bool is_zygote, std::string* error_msg)
-      REQUIRES(Locks::jit_lock_);
-
-  void InitializeSpaces() REQUIRES(Locks::jit_lock_);
-
   // Try to increase the current capacity of the code cache. Return whether we
   // succeeded at doing so.
   bool IncreaseCodeCacheCapacity() REQUIRES(Locks::jit_lock_);