cppgc: Introduce feature for 16GB cage size

The CL introduces a disabled-by-default feature named
'V8CppGCEnableLargerCage'.

Bug: 343959927
Change-Id: I1a39b9d6cc222c02973c95d87227f8e1c6e85a17
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5677043
Reviewed-by: Michael Lippautz <mlippautz@chromium.org>
Commit-Queue: Anton Bikineev <bikineev@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1324126}
diff --git a/gin/cppgc.cc b/gin/cppgc.cc
index 9661a974d..15a5026 100644
--- a/gin/cppgc.cc
+++ b/gin/cppgc.cc
@@ -5,6 +5,8 @@
 #include "gin/public/cppgc.h"
 
 #include "base/check_op.h"
+#include "base/feature_list.h"
+#include "gin/gin_features.h"
 #include "gin/public/v8_platform.h"
 #include "v8/include/cppgc/platform.h"
 
@@ -17,11 +19,28 @@
 }  // namespace
 
 void InitializeCppgcFromV8Platform() {
-  DCHECK_GE(g_init_count, 0);
-  if (g_init_count++ > 0)
-    return;
+  static constexpr size_t kRegularCageSize =
+      static_cast<size_t>(4) * 1024 * 1024 * 1024;
+  static constexpr size_t kLargerCageSize =
+      static_cast<size_t>(16) * 1024 * 1024 * 1024;
 
-  cppgc::InitializeProcess(gin::V8Platform::Get()->GetPageAllocator());
+  DCHECK_GE(g_init_count, 0);
+  if (g_init_count++ > 0) {
+    return;
+  }
+
+  size_t desired_cage_size = kRegularCageSize;
+  auto overridden_state = base::FeatureList::GetStateIfOverridden(
+      features::kV8CppGCEnableLargerCage);
+  if (overridden_state.has_value()) {
+    if (overridden_state.value()) {
+      desired_cage_size = kLargerCageSize;
+    } else {
+    }
+  }
+
+  cppgc::InitializeProcess(gin::V8Platform::Get()->GetPageAllocator(),
+                           desired_cage_size);
 }
 
 void MaybeShutdownCppgc() {
diff --git a/gin/gin_features.cc b/gin/gin_features.cc
index c811907..0615063 100644
--- a/gin/gin_features.cc
+++ b/gin/gin_features.cc
@@ -209,6 +209,11 @@
              "V8SlowHistogramsNoTurbofan",
              base::FEATURE_DISABLED_BY_DEFAULT);
 
+// Enable 16GB heap reservation for Oilpan.
+BASE_FEATURE(kV8CppGCEnableLargerCage,
+             "V8CppGCEnableLargerCage",
+             kFeatureDefaultStateControlledByV8);
+
 BASE_FEATURE(kV8DelayMemoryReducer,
              "V8DelayMemoryReducer",
              base::FEATURE_ENABLED_BY_DEFAULT);
diff --git a/gin/gin_features.h b/gin/gin_features.h
index fcc12fb..721b05ac 100644
--- a/gin/gin_features.h
+++ b/gin/gin_features.h
@@ -28,6 +28,7 @@
 GIN_EXPORT BASE_DECLARE_FEATURE(kV8BaselineBatchCompilation);
 GIN_EXPORT BASE_DECLARE_FEATURE(kV8CodeMemoryWriteProtection);
 GIN_EXPORT BASE_DECLARE_FEATURE(kV8ConcurrentSparkplugHighPriorityThreads);
+GIN_EXPORT BASE_DECLARE_FEATURE(kV8CppGCEnableLargerCage);
 GIN_EXPORT BASE_DECLARE_FEATURE(kV8DelayMemoryReducer);
 GIN_EXPORT BASE_DECLARE_FEATURE(kV8ConcurrentMarkingHighPriorityThreads);
 GIN_EXPORT BASE_DECLARE_FEATURE(kV8DecommitPooledPages);