< prev index next >

src/share/vm/gc/serial/defNewGeneration.cpp

Print this page

        

@@ -383,16 +383,37 @@
          new_size_before <= max_new_size,
          "just checking");
   // All space sizes must be multiples of Generation::GenGrain.
   size_t alignment = Generation::GenGrain;
 
-  // Compute desired new generation size based on NewRatio and
-  // NewSizeThreadIncrease
-  size_t desired_new_size = old_size/NewRatio;
-  int threads_count = Threads::number_of_non_daemon_threads();
-  size_t thread_increase_size = threads_count * NewSizeThreadIncrease;
-  desired_new_size = align_size_up(desired_new_size + thread_increase_size, alignment);
+  // To revert previous value when an overflow happens.
+  size_t desired_new_size = new_size_before;
+
+  int threads_count = 0;
+  size_t thread_increase_size = 0;
+
+  // Compute desired new generation size based on NewRatio and NewSizeThreadIncrease
+  if (NewSizeThreadIncrease > 0) {
+    size_t new_size_candidate = old_size / NewRatio;
+
+    // 1. Check an overflow at 'threads_count * NewSizeThreadIncrease'.
+    threads_count = Threads::number_of_non_daemon_threads();
+    if (NewSizeThreadIncrease <= max_uintx / threads_count) {
+      thread_increase_size = threads_count * NewSizeThreadIncrease;
+
+      // 2. Check an overflow at 'new_size_candidate + thread_increase_size'.
+      if (new_size_candidate <= max_uintx - thread_increase_size) {
+        new_size_candidate += thread_increase_size;
+
+        // 3. Check an overflow at 'align_size_up'.
+        size_t aligned_max = ((max_uintx - alignment) & ~(alignment-1));
+        if (new_size_candidate <= aligned_max) {
+          desired_new_size = align_size_up(new_size_candidate, alignment);
+        }
+      }
+    }
+  }
 
   // Adjust new generation size
   desired_new_size = MAX2(MIN2(desired_new_size, max_new_size), min_new_size);
   assert(desired_new_size <= max_new_size, "just checking");
 
< prev index next >