Skip to content

Commit

Permalink
cppgc: Fix and merge cppgc samples
Browse files Browse the repository at this point in the history
Both sample are essentially the same up to string constants since
cppgc's default platform started using libplatform.
The only diff between the sample is whether we call
v8::V8::IntializePlatform or cppgc::InitializeProcess.

Drive-by: replace CPPGC_BUILD_IN_V8 with CPPGC_IS_STANDALONE which is
          more descriptive.

Bug: chromium:1056170
Change-Id: I8fdeb59c3345af77f1bccd8b93255ab39b4d3181
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2557516
Commit-Queue: Omer Katz <omerkatz@chromium.org>
Reviewed-by: Michael Lippautz <mlippautz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#71421}
  • Loading branch information
omerktz authored and Commit Bot committed Nov 26, 2020
1 parent aec92ae commit f8fa0ed
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 104 deletions.
41 changes: 15 additions & 26 deletions BUILD.gn
Expand Up @@ -468,8 +468,8 @@ assert(!cppgc_is_standalone || !v8_use_perfetto)
# This config should be applied to code using the cppgc_base.
config("cppgc_base_config") {
defines = []
if (!cppgc_is_standalone) {
defines += [ "CPPGC_BUILD_IN_V8" ]
if (cppgc_is_standalone) {
defines += [ "CPPGC_IS_STANDALONE" ]
}
}

Expand Down Expand Up @@ -4490,6 +4490,7 @@ v8_source_set("cppgc_base") {
"src/heap/cppgc/compactor.h",
"src/heap/cppgc/concurrent-marker.cc",
"src/heap/cppgc/concurrent-marker.h",
"src/heap/cppgc/default-platform.cc",
"src/heap/cppgc/free-list.cc",
"src/heap/cppgc/free-list.h",
"src/heap/cppgc/garbage-collector.h",
Expand Down Expand Up @@ -5125,33 +5126,21 @@ if (want_v8_shell) {
}
}

if (cppgc_is_standalone) {
v8_executable("cppgc_standalone") {
sources = [ "samples/cppgc/cppgc-standalone.cc" ]
v8_executable("cppgc_sample") {
sources = [ "samples/cppgc/cppgc-sample.cc" ]

configs = [
# Note: don't use :internal_config here because this target will get
# the :external_config applied to it by virtue of depending on :cppgc, and
# you can't have both applied to the same target.
":internal_config_base",
]

deps = [ ":cppgc" ]
}
} else {
v8_executable("cppgc_for_v8_embedders") {
sources = [ "samples/cppgc/cppgc-for-v8-embedders.cc" ]

configs = [
# Note: don't use :internal_config here because this target will get
# the :external_config applied to it by virtue of depending on :cppgc, and
# you can't have both applied to the same target.
":internal_config_base",
]
configs = [
# Note: don't use :internal_config here because this target will get
# the :external_config applied to it by virtue of depending on :cppgc, and
# you can't have both applied to the same target.
":internal_config_base",
":cppgc_base_config",
]

deps = [
deps = [ ":cppgc" ]
if (!cppgc_is_standalone) {
deps += [
":v8",
":v8_libplatform",
"//build/win:default_exe_manifest",
]
}
Expand Down
9 changes: 9 additions & 0 deletions include/cppgc/default-platform.h
Expand Up @@ -20,6 +20,15 @@ namespace cppgc {
*/
class V8_EXPORT DefaultPlatform : public Platform {
public:
/**
* Use this method instead of 'cppgc::InitializeProcess' when using
* 'cppgc::DefaultPlatform'. 'cppgc::DefaultPlatform::InitializeProcess'
* will initialize cppgc and v8 if needed (for non-standalone builds).
*
* \param platform DefaultPlatform instance used to initialize cppgc/v8.
*/
static void InitializeProcess(DefaultPlatform* platform);

using IdleTaskSupport = v8::platform::IdleTaskSupport;
explicit DefaultPlatform(
int thread_pool_size = 0,
Expand Down
70 changes: 0 additions & 70 deletions samples/cppgc/cppgc-for-v8-embedders.cc

This file was deleted.

Expand Up @@ -45,9 +45,13 @@ int main(int argc, char* argv[]) {
// Create a default platform that is used by cppgc::Heap for execution and
// backend allocation.
auto cppgc_platform = std::make_shared<cppgc::DefaultPlatform>();
// Initialize the process. This must happen before any
// cppgc::Heap::Create() calls.
cppgc::InitializeProcess(cppgc_platform->GetPageAllocator());
// Initialize the process. This must happen before any cppgc::Heap::Create()
// calls. cppgc::DefaultPlatform::InitializeProcess initializes both cppgc
// and v8 (if cppgc is not used as a standalone) as needed.
// If using a platform other than cppgc::DefaultPlatform, should call
// cppgc::InitializeProcess (for standalone builds) or
// v8::V8::InitializePlatform (for non-standalone builds) directly.
cppgc::DefaultPlatform::InitializeProcess(cppgc_platform.get());
// Create a managed heap.
std::unique_ptr<cppgc::Heap> heap = cppgc::Heap::Create(cppgc_platform);
// Allocate a string rope on the managed heap.
Expand All @@ -56,7 +60,7 @@ int main(int argc, char* argv[]) {
cppgc::MakeGarbageCollected<Rope>(heap->GetAllocationHandle(), "World!"));
// Manually trigger garbage collection. The object greeting is held alive
// through conservative stack scanning.
heap->ForceGarbageCollectionSlow("CppGC stand-alone example", "Testing");
heap->ForceGarbageCollectionSlow("CppGC example", "Testing");
std::cout << *greeting << std::endl;
// Gracefully shutdown the process.
cppgc::ShutdownProcess();
Expand Down
23 changes: 23 additions & 0 deletions src/heap/cppgc/default-platform.cc
@@ -0,0 +1,23 @@
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <include/cppgc/default-platform.h>

#if !CPPGC_IS_STANDALONE
#include <v8.h>
#endif // !CPPGC_IS_STANDALONE

namespace cppgc {

// static
void DefaultPlatform::InitializeProcess(DefaultPlatform* platform) {
#if CPPGC_IS_STANDALONE
cppgc::InitializeProcess(platform->GetPageAllocator());
#else
// v8::V8::InitializePlatform transitively calls cppgc::InitializeProcess.
v8::V8::InitializePlatform(platform->v8_platform_.get());
#endif // CPPGC_IS_STANDALONE
}

} // namespace cppgc
4 changes: 2 additions & 2 deletions src/heap/cppgc/trace-event.h
Expand Up @@ -5,7 +5,7 @@
#ifndef V8_HEAP_CPPGC_TRACE_EVENT_H_
#define V8_HEAP_CPPGC_TRACE_EVENT_H_

#if CPPGC_BUILD_IN_V8
#if !CPPGC_IS_STANDALONE
#include "src/tracing/trace-event.h"
using ConvertableToTraceFormat = v8::ConvertableToTraceFormat;
#else
Expand Down Expand Up @@ -237,6 +237,6 @@ static V8_INLINE uint64_t AddTraceEvent(
} // namespace internal
} // namespace cppgc

#endif // CPPGC_BUILD_IN_V8
#endif // !CPPGC_IS_STANDALONE

#endif // V8_HEAP_CPPGC_TRACE_EVENT_H_
4 changes: 2 additions & 2 deletions test/unittests/heap/cppgc/stats-collector-scopes-unittest.cc
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#if !CPPGC_BUILD_IN_V8
#if CPPGC_IS_STANDALONE

#include "src/heap/cppgc/stats-collector.h"
#include "test/unittests/heap/cppgc/tests.h"
Expand Down Expand Up @@ -303,4 +303,4 @@ TEST_F(CppgcTracingScopesTest, TestIndividualConcurrentScopes) {
} // namespace internal
} // namespace cppgc

#endif // !CPPGC_BUILD_IN_V8
#endif // CPPGC_IS_STANDALONE

0 comments on commit f8fa0ed

Please sign in to comment.