Pass the memory region to allocate into to the compiler / allocation.

Test: test.py
Bug: 119800099
Change-Id: Ie3cba5abe3dd4f8756af5ecfd6c26320de314fe8
diff --git a/compiler/compiler.h b/compiler/compiler.h
index a496c6c..e363e70 100644
--- a/compiler/compiler.h
+++ b/compiler/compiler.h
@@ -29,6 +29,7 @@
 namespace jit {
 class JitCodeCache;
 class JitLogger;
+class JitMemoryRegion;
 }  // namespace jit
 namespace mirror {
 class ClassLoader;
@@ -41,7 +42,6 @@
 class CompilerOptions;
 class DexFile;
 template<class T> class Handle;
-class OatWriter;
 class Thread;
 
 class Compiler {
@@ -73,6 +73,7 @@
 
   virtual bool JitCompile(Thread* self ATTRIBUTE_UNUSED,
                           jit::JitCodeCache* code_cache ATTRIBUTE_UNUSED,
+                          jit::JitMemoryRegion* region ATTRIBUTE_UNUSED,
                           ArtMethod* method ATTRIBUTE_UNUSED,
                           bool baseline ATTRIBUTE_UNUSED,
                           bool osr ATTRIBUTE_UNUSED,
diff --git a/compiler/jit/jit_compiler.cc b/compiler/jit/jit_compiler.cc
index b42e9f2..f19de4e 100644
--- a/compiler/jit/jit_compiler.cc
+++ b/compiler/jit/jit_compiler.cc
@@ -129,11 +129,11 @@
 }
 
 extern "C" bool jit_compile_method(
-    void* handle, ArtMethod* method, Thread* self, bool baseline, bool osr)
+    void* handle, JitMemoryRegion* region, ArtMethod* method, Thread* self, bool baseline, bool osr)
     REQUIRES_SHARED(Locks::mutator_lock_) {
   auto* jit_compiler = reinterpret_cast<JitCompiler*>(handle);
   DCHECK(jit_compiler != nullptr);
-  return jit_compiler->CompileMethod(self, method, baseline, osr);
+  return jit_compiler->CompileMethod(self, region, method, baseline, osr);
 }
 
 extern "C" void jit_types_loaded(void* handle, mirror::Class** types, size_t count)
@@ -181,7 +181,8 @@
   }
 }
 
-bool JitCompiler::CompileMethod(Thread* self, ArtMethod* method, bool baseline, bool osr) {
+bool JitCompiler::CompileMethod(
+    Thread* self, JitMemoryRegion* region, ArtMethod* method, bool baseline, bool osr) {
   SCOPED_TRACE << "JIT compiling " << method->PrettyMethod();
 
   DCHECK(!method->IsProxyMethod());
@@ -198,7 +199,8 @@
     TimingLogger::ScopedTiming t2("Compiling", &logger);
     JitCodeCache* const code_cache = runtime->GetJit()->GetCodeCache();
     uint64_t start_ns = NanoTime();
-    success = compiler_->JitCompile(self, code_cache, method, baseline, osr, jit_logger_.get());
+    success = compiler_->JitCompile(
+        self, code_cache, region, method, baseline, osr, jit_logger_.get());
     uint64_t duration_ns = NanoTime() - start_ns;
     VLOG(jit) << "Compilation of "
               << method->PrettyMethod()
diff --git a/compiler/jit/jit_compiler.h b/compiler/jit/jit_compiler.h
index d008de4..06315a5 100644
--- a/compiler/jit/jit_compiler.h
+++ b/compiler/jit/jit_compiler.h
@@ -22,7 +22,6 @@
 namespace art {
 
 class ArtMethod;
-class CompiledMethod;
 class Compiler;
 class CompilerOptions;
 class Thread;
@@ -30,6 +29,7 @@
 namespace jit {
 
 class JitLogger;
+class JitMemoryRegion;
 
 class JitCompiler {
  public:
@@ -37,7 +37,8 @@
   virtual ~JitCompiler();
 
   // Compilation entrypoint. Returns whether the compilation succeeded.
-  bool CompileMethod(Thread* self, ArtMethod* method, bool baseline, bool osr)
+  bool CompileMethod(
+      Thread* self, JitMemoryRegion* region, ArtMethod* method, bool baseline, bool osr)
       REQUIRES_SHARED(Locks::mutator_lock_);
 
   const CompilerOptions& GetCompilerOptions() const {
diff --git a/compiler/optimizing/optimizing_compiler.cc b/compiler/optimizing/optimizing_compiler.cc
index c799b12..9da282b 100644
--- a/compiler/optimizing/optimizing_compiler.cc
+++ b/compiler/optimizing/optimizing_compiler.cc
@@ -295,6 +295,7 @@
 
   bool JitCompile(Thread* self,
                   jit::JitCodeCache* code_cache,
+                  jit::JitMemoryRegion* region,
                   ArtMethod* method,
                   bool baseline,
                   bool osr,
@@ -1248,6 +1249,7 @@
 
 bool OptimizingCompiler::JitCompile(Thread* self,
                                     jit::JitCodeCache* code_cache,
+                                    jit::JitMemoryRegion* region,
                                     ArtMethod* method,
                                     bool baseline,
                                     bool osr,
@@ -1282,6 +1284,7 @@
     uint8_t* stack_map_data = nullptr;
     uint8_t* roots_data = nullptr;
     uint32_t data_size = code_cache->ReserveData(self,
+                                                 region,
                                                  stack_map.size(),
                                                  /* number_of_roots= */ 0,
                                                  method,
@@ -1295,6 +1298,7 @@
 
     const void* code = code_cache->CommitCode(
         self,
+        region,
         method,
         stack_map_data,
         roots_data,
@@ -1306,6 +1310,7 @@
         /* has_should_deoptimize_flag= */ false,
         cha_single_implementation_list);
     if (code == nullptr) {
+      code_cache->ClearData(self, region, stack_map_data, roots_data);
       return false;
     }
 
@@ -1379,6 +1384,7 @@
   uint8_t* stack_map_data = nullptr;
   uint8_t* roots_data = nullptr;
   uint32_t data_size = code_cache->ReserveData(self,
+                                               region,
                                                stack_map.size(),
                                                number_of_roots,
                                                method,
@@ -1400,6 +1406,7 @@
 
   const void* code = code_cache->CommitCode(
       self,
+      region,
       method,
       stack_map_data,
       roots_data,
@@ -1413,7 +1420,7 @@
 
   if (code == nullptr) {
     MaybeRecordStat(compilation_stats_.get(), MethodCompilationStat::kJitOutOfMemoryForCommit);
-    code_cache->ClearData(self, stack_map_data, roots_data);
+    code_cache->ClearData(self, region, stack_map_data, roots_data);
     return false;
   }