cppgc: Fix and merge cppgc samples
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}
This commit is contained in:
parent
aec92aed6e
commit
f8fa0edf16
41
BUILD.gn
41
BUILD.gn
@ -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" ]
|
||||
}
|
||||
}
|
||||
|
||||
@ -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",
|
||||
@ -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",
|
||||
]
|
||||
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 = [ ":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",
|
||||
]
|
||||
|
||||
deps = [
|
||||
deps = [ ":cppgc" ]
|
||||
if (!cppgc_is_standalone) {
|
||||
deps += [
|
||||
":v8",
|
||||
":v8_libplatform",
|
||||
"//build/win:default_exe_manifest",
|
||||
]
|
||||
}
|
||||
|
@ -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,
|
||||
|
@ -1,70 +0,0 @@
|
||||
// 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/allocation.h>
|
||||
#include <include/cppgc/default-platform.h>
|
||||
#include <include/cppgc/garbage-collected.h>
|
||||
#include <include/cppgc/heap.h>
|
||||
#include <include/cppgc/member.h>
|
||||
#include <include/cppgc/platform.h>
|
||||
#include <include/cppgc/visitor.h>
|
||||
#include <include/v8.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
/**
|
||||
* This sample program shows how to set up a stand-alone cppgc heap as an
|
||||
* embedder of V8. Most importantly, this example shows how to reuse V8's
|
||||
* platform for cppgc.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Simple string rope to illustrate allocation and garbage collection below. The
|
||||
* rope keeps the next parts alive via regular managed reference.
|
||||
*/
|
||||
class Rope final : public cppgc::GarbageCollected<Rope> {
|
||||
public:
|
||||
explicit Rope(std::string part, Rope* next = nullptr)
|
||||
: part_(part), next_(next) {}
|
||||
|
||||
void Trace(cppgc::Visitor* visitor) const { visitor->Trace(next_); }
|
||||
|
||||
private:
|
||||
std::string part_;
|
||||
cppgc::Member<Rope> next_;
|
||||
|
||||
friend std::ostream& operator<<(std::ostream& os, const Rope& rope);
|
||||
};
|
||||
|
||||
std::ostream& operator<<(std::ostream& os, const Rope& rope) {
|
||||
os << rope.part_;
|
||||
if (rope.next_) {
|
||||
os << *rope.next_;
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
// Create a 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());
|
||||
// Create a managed heap.
|
||||
std::unique_ptr<cppgc::Heap> heap = cppgc::Heap::Create(cppgc_platform);
|
||||
// Allocate a string rope on the managed heap.
|
||||
auto* greeting = cppgc::MakeGarbageCollected<Rope>(
|
||||
heap->GetAllocationHandle(), "Hello ",
|
||||
cppgc::MakeGarbageCollected<Rope>(heap->GetAllocationHandle(), "World!"));
|
||||
// Manually trigger garbage collection. The object greeting is held alive
|
||||
// through conservative stack scanning.
|
||||
heap->ForceGarbageCollectionSlow("V8 embedders example", "Testing");
|
||||
std::cout << *greeting << std::endl;
|
||||
// Gracefully shutdown the process.
|
||||
cppgc::ShutdownProcess();
|
||||
return 0;
|
||||
}
|
@ -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.
|
||||
@ -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();
|
23
src/heap/cppgc/default-platform.cc
Normal file
23
src/heap/cppgc/default-platform.cc
Normal file
@ -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
|
@ -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
|
||||
@ -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_
|
||||
|
@ -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"
|
||||
@ -303,4 +303,4 @@ TEST_F(CppgcTracingScopesTest, TestIndividualConcurrentScopes) {
|
||||
} // namespace internal
|
||||
} // namespace cppgc
|
||||
|
||||
#endif // !CPPGC_BUILD_IN_V8
|
||||
#endif // CPPGC_IS_STANDALONE
|
||||
|
Loading…
Reference in New Issue
Block a user