5#include "fxjs/gc/heap.h"
10#include "testing/fxgc_unittest.h"
11#include "testing/gtest/include/gtest/gtest.h"
12#include "testing/v8_test_environment.h"
13#include "third_party/base/containers/contains.h"
14#include "v8/include/cppgc/allocation.h"
15#include "v8/include/cppgc/persistent.h"
19class PseudoCollectible :
public cppgc::GarbageCollected<PseudoCollectible> {
21 static void ClearCounts() {
25 static size_t LiveCount() {
return s_live_.size(); }
26 static size_t DeadCount() {
return s_dead_.size(); }
28 PseudoCollectible() { s_live_.insert(
this); }
29 virtual ~PseudoCollectible() {
34 bool IsLive()
const {
return pdfium::Contains(s_live_,
this); }
36 virtual void Trace(cppgc::Visitor* visitor)
const {}
39 static std::set<
const PseudoCollectible*> s_live_;
40 static std::set<
const PseudoCollectible*> s_dead_;
43std::set<
const PseudoCollectible*> PseudoCollectible::s_live_;
44std::set<
const PseudoCollectible*> PseudoCollectible::s_dead_;
46class CollectibleHolder {
48 explicit CollectibleHolder(PseudoCollectible* holdee) : holdee_(holdee) {}
49 ~CollectibleHolder() =
default;
51 PseudoCollectible* holdee()
const {
return holdee_; }
54 cppgc::Persistent<PseudoCollectible> holdee_;
57class Bloater :
public cppgc::GarbageCollected<Bloater> {
59 void Trace(cppgc::Visitor* visitor)
const {}
60 uint8_t bloat_[65536];
72 PseudoCollectible::ClearCounts();
78 FXGCScopedHeap heap1 = FXGC_CreateHeap();
81 FXGCScopedHeap heap2 = FXGC_CreateHeap();
84 FXGCScopedHeap heap3 = FXGC_CreateHeap();
95 FXGCScopedHeap heap1 = FXGC_CreateHeap();
98 auto holder = std::make_unique<CollectibleHolder>(
99 cppgc::MakeGarbageCollected<PseudoCollectible>(
100 heap1->GetAllocationHandle()));
102 EXPECT_TRUE(holder->holdee()->IsLive());
103 EXPECT_EQ(1u, PseudoCollectible::LiveCount());
104 EXPECT_EQ(0u, PseudoCollectible::DeadCount());
106 FXGC_ForceGarbageCollection(heap1.get());
107 EXPECT_EQ(0u, PseudoCollectible::LiveCount());
108 EXPECT_EQ(1u, PseudoCollectible::DeadCount());
112 FXGCScopedHeap heap1 = FXGC_CreateHeap();
115 auto holder = std::make_unique<CollectibleHolder>(
116 cppgc::MakeGarbageCollected<PseudoCollectible>(
117 heap1->GetAllocationHandle()));
119 EXPECT_TRUE(holder->holdee()->IsLive());
120 EXPECT_EQ(1u, PseudoCollectible::LiveCount());
121 EXPECT_EQ(0u, PseudoCollectible::DeadCount());
123 FXGC_ForceGarbageCollection(heap1.get());
124 EXPECT_TRUE(holder->holdee()->IsLive());
125 EXPECT_EQ(1u, PseudoCollectible::LiveCount());
126 EXPECT_EQ(0u, PseudoCollectible::DeadCount());
132 FXGCScopedHeap heap1 = FXGC_CreateHeap();
135 auto holder = std::make_unique<CollectibleHolder>(
136 cppgc::MakeGarbageCollected<PseudoCollectible>(
137 heap1->GetAllocationHandle()));
139 EXPECT_TRUE(holder->holdee()->IsLive());
140 EXPECT_EQ(1u, PseudoCollectible::LiveCount());
141 EXPECT_EQ(0u, PseudoCollectible::DeadCount());
146 EXPECT_FALSE(holder->holdee());
147 EXPECT_EQ(1u, PseudoCollectible::LiveCount());
148 EXPECT_EQ(0u, PseudoCollectible::DeadCount());
153 FXGCScopedHeap heap1 = FXGC_CreateHeap();
156 auto holder = std::make_unique<CollectibleHolder>(
157 cppgc::MakeGarbageCollected<PseudoCollectible>(
158 heap1->GetAllocationHandle()));
160 EXPECT_TRUE(holder->holdee()->IsLive());
161 EXPECT_EQ(1u, PseudoCollectible::LiveCount());
162 EXPECT_EQ(0u, PseudoCollectible::DeadCount());
165 EXPECT_EQ(0u, PseudoCollectible::LiveCount());
166 EXPECT_EQ(1u, PseudoCollectible::DeadCount());
171 for (
int i = 0; i < 100000; ++i) {
172 cppgc::MakeGarbageCollected<Bloater>(heap()->GetAllocationHandle());
~HeapUnitTest() override=default
TEST_F(HeapUnitTest, SeveralHeaps)