8077417: Cleanup of Universe::initialize_heap()
authorpliden
Thu, 16 Apr 2015 15:31:20 +0200
changeset 30260 a7bfa4e20897
parent 30259 4caf50ef60fd
child 30261 faa43d41b739
8077417: Cleanup of Universe::initialize_heap() Reviewed-by: stefank, kbarrett
hotspot/src/share/vm/gc_implementation/parallelScavenge/parallelScavengeHeap.cpp
hotspot/src/share/vm/gc_implementation/parallelScavenge/parallelScavengeHeap.hpp
hotspot/src/share/vm/memory/universe.cpp
hotspot/src/share/vm/memory/universe.hpp
--- a/hotspot/src/share/vm/gc_implementation/parallelScavenge/parallelScavengeHeap.cpp	Thu Apr 16 10:38:06 2015 +0000
+++ b/hotspot/src/share/vm/gc_implementation/parallelScavenge/parallelScavengeHeap.cpp	Thu Apr 16 15:31:20 2015 +0200
@@ -54,10 +54,6 @@
 jint ParallelScavengeHeap::initialize() {
   CollectedHeap::pre_initialize();
 
-  // Initialize collector policy
-  _collector_policy = new GenerationSizer();
-  _collector_policy->initialize_all();
-
   const size_t heap_size = _collector_policy->max_heap_byte_size();
 
   ReservedSpace heap_rs = Universe::reserve_heap(heap_size, _collector_policy->heap_alignment());
--- a/hotspot/src/share/vm/gc_implementation/parallelScavenge/parallelScavengeHeap.hpp	Thu Apr 16 10:38:06 2015 +0000
+++ b/hotspot/src/share/vm/gc_implementation/parallelScavenge/parallelScavengeHeap.hpp	Thu Apr 16 15:31:20 2015 +0200
@@ -74,7 +74,8 @@
   HeapWord* mem_allocate_old_gen(size_t size);
 
  public:
-  ParallelScavengeHeap() : CollectedHeap(), _death_march_count(0) { }
+  ParallelScavengeHeap(GenerationSizer* policy) :
+    CollectedHeap(), _collector_policy(policy), _death_march_count(0) { }
 
   // For use by VM operations
   enum CollectionType {
--- a/hotspot/src/share/vm/memory/universe.cpp	Thu Apr 16 10:38:06 2015 +0000
+++ b/hotspot/src/share/vm/memory/universe.cpp	Thu Apr 16 15:31:20 2015 +0200
@@ -687,6 +687,15 @@
   return JNI_OK;
 }
 
+template <class Heap, class Policy>
+jint Universe::create_heap() {
+  assert(_collectedHeap == NULL, "Heap already created");
+  Policy* policy = new Policy();
+  policy->initialize_all();
+  _collectedHeap = new Heap(policy);
+  return _collectedHeap->initialize();
+}
+
 // Choose the heap base address and oop encoding mode
 // when compressed oops are used:
 // Unscaled  - Use 32-bits oops without encoding when
@@ -696,50 +705,37 @@
 // HeapBased - Use compressed oops with heap base + encoding.
 
 jint Universe::initialize_heap() {
-
-  if (UseParallelGC) {
-#if INCLUDE_ALL_GCS
-    Universe::_collectedHeap = new ParallelScavengeHeap();
-#else  // INCLUDE_ALL_GCS
-    fatal("UseParallelGC not supported in this VM.");
-#endif // INCLUDE_ALL_GCS
-
-  } else if (UseG1GC) {
-#if INCLUDE_ALL_GCS
-    G1CollectorPolicyExt* g1p = new G1CollectorPolicyExt();
-    g1p->initialize_all();
-    G1CollectedHeap* g1h = new G1CollectedHeap(g1p);
-    Universe::_collectedHeap = g1h;
-#else  // INCLUDE_ALL_GCS
-    fatal("UseG1GC not supported in java kernel vm.");
-#endif // INCLUDE_ALL_GCS
+  jint status = JNI_ERR;
 
-  } else {
-    GenCollectorPolicy *gc_policy;
+#if !INCLUDE_ALL_GCS
+  if (UseParallelGC) {
+    fatal("UseParallelGC not supported in this VM.");
+  } else if (UseG1GC) {
+    fatal("UseG1GC not supported in this VM.");
+  } else if (UseConcMarkSweepGC) {
+    fatal("UseConcMarkSweepGC not supported in this VM.");
+  }
+#else
+  if (UseParallelGC) {
+    status = Universe::create_heap<ParallelScavengeHeap, GenerationSizer>();
+  } else if (UseG1GC) {
+    status = Universe::create_heap<G1CollectedHeap, G1CollectorPolicyExt>();
+  } else if (UseConcMarkSweepGC) {
+    status = Universe::create_heap<GenCollectedHeap, ConcurrentMarkSweepPolicy>();
+  }
+#endif
+  else { // UseSerialGC
+    // Don't assert that UseSerialGC is set here because there are cases
+    // where no GC it set and we then fall back to using SerialGC.
+    status = Universe::create_heap<GenCollectedHeap, MarkSweepPolicy>();
+  }
 
-    if (UseSerialGC) {
-      gc_policy = new MarkSweepPolicy();
-    } else if (UseConcMarkSweepGC) {
-#if INCLUDE_ALL_GCS
-      gc_policy = new ConcurrentMarkSweepPolicy();
-#else  // INCLUDE_ALL_GCS
-      fatal("UseConcMarkSweepGC not supported in this VM.");
-#endif // INCLUDE_ALL_GCS
-    } else { // default old generation
-      gc_policy = new MarkSweepPolicy();
-    }
-    gc_policy->initialize_all();
-
-    Universe::_collectedHeap = new GenCollectedHeap(gc_policy);
+  if (status != JNI_OK) {
+    return status;
   }
 
   ThreadLocalAllocBuffer::set_max_size(Universe::heap()->max_tlab_size());
 
-  jint status = Universe::heap()->initialize();
-  if (status != JNI_OK) {
-    return status;
-  }
-
 #ifdef _LP64
   if (UseCompressedOops) {
     // Subtract a page because something can get allocated at heap base.
@@ -1063,7 +1059,7 @@
 
   MemoryService::add_metaspace_memory_pools();
 
-  MemoryService::set_universe_heap(Universe::_collectedHeap);
+  MemoryService::set_universe_heap(Universe::heap());
 #if INCLUDE_CDS
   SharedClassUtil::initialize(CHECK_false);
 #endif
--- a/hotspot/src/share/vm/memory/universe.hpp	Thu Apr 16 10:38:06 2015 +0000
+++ b/hotspot/src/share/vm/memory/universe.hpp	Thu Apr 16 15:31:20 2015 +0200
@@ -214,6 +214,7 @@
   static size_t _heap_capacity_at_last_gc;
   static size_t _heap_used_at_last_gc;
 
+  template <class Heap, class Policy> static jint create_heap();
   static jint initialize_heap();
   static void initialize_basic_type_mirrors(TRAPS);
   static void fixup_mirrors(TRAPS);