blob: 9ebe584210a6068bb77eaedff61e274f99e3db55 [file] [log] [blame]
Avi Drissman468e51b62022-09-13 20:47:011// Copyright 2021 The Chromium Authors
Michael Lippautzca279472021-04-23 16:13:522// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "gin/public/cppgc.h"
Daniel Hosseinian554068b2021-07-08 18:35:106
7#include "base/check_op.h"
Anton Bikineev2bc9146d2024-07-08 10:17:148#include "base/feature_list.h"
Anton Bikineev687200d2024-11-13 14:33:209#include "build/build_config.h"
Anton Bikineev2bc9146d2024-07-08 10:17:1410#include "gin/gin_features.h"
Michael Lippautzca279472021-04-23 16:13:5211#include "gin/public/v8_platform.h"
12#include "v8/include/cppgc/platform.h"
13
14namespace gin {
15
Daniel Hosseinian554068b2021-07-08 18:35:1016namespace {
17
18int g_init_count = 0;
19
20} // namespace
21
Michael Lippautzca279472021-04-23 16:13:5222void InitializeCppgcFromV8Platform() {
Anton Bikineev687200d2024-11-13 14:33:2023#if BUILDFLAG(IS_ANDROID)
24 // Keep the cage size at 4GB on Android, since some vendors can configure the
25 // kernel to have address space limited to 2^39 or 2^36 bits, which is more
26 // prone to address space exhaustion.
27 static constexpr size_t kCageSize =
Anton Bikineev2bc9146d2024-07-08 10:17:1428 static_cast<size_t>(4) * 1024 * 1024 * 1024;
Anton Bikineev687200d2024-11-13 14:33:2029#else
30 static constexpr size_t kCageSize =
Anton Bikineev2bc9146d2024-07-08 10:17:1431 static_cast<size_t>(16) * 1024 * 1024 * 1024;
Anton Bikineev687200d2024-11-13 14:33:2032#endif
Michael Lippautzca279472021-04-23 16:13:5233
Anton Bikineev2bc9146d2024-07-08 10:17:1434 DCHECK_GE(g_init_count, 0);
35 if (g_init_count++ > 0) {
36 return;
37 }
38
Anton Bikineev2bc9146d2024-07-08 10:17:1439 cppgc::InitializeProcess(gin::V8Platform::Get()->GetPageAllocator(),
Anton Bikineev687200d2024-11-13 14:33:2040 kCageSize);
Daniel Hosseinian554068b2021-07-08 18:35:1041}
Michael Lippautzca279472021-04-23 16:13:5242
Daniel Hosseinian554068b2021-07-08 18:35:1043void MaybeShutdownCppgc() {
44 DCHECK_GT(g_init_count, 0);
45 if (--g_init_count > 0)
46 return;
47
48 cppgc::ShutdownProcess();
Michael Lippautzca279472021-04-23 16:13:5249}
50
51} // namespace gin