Commit c0c8df48 authored by Michael Lippautz's avatar Michael Lippautz Committed by Commit Bot

heap: Trivial code health cleanups around ThreadState

- Move trivial initializers to declaration site
- Move Visit* methods to private section
- Remove unused parameter around Persistent APIs

Bug: 982754
Change-Id: Iec4102f1466a8b65251aa69892d041f450ac069e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1743636
Commit-Queue: Michael Lippautz <mlippautz@chromium.org>
Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Cr-Commit-Position: refs/heads/master@{#685265}
parent 9230e75a
...@@ -220,10 +220,11 @@ class PersistentBase { ...@@ -220,10 +220,11 @@ class PersistentBase {
// clean LSan leak reports or to register a thread-local persistent // clean LSan leak reports or to register a thread-local persistent
// needing to be cleared out before the thread is terminated. // needing to be cleared out before the thread is terminated.
PersistentBase* RegisterAsStaticReference() { PersistentBase* RegisterAsStaticReference() {
CHECK_EQ(weaknessConfiguration, kNonWeakPersistentConfiguration); static_assert(weaknessConfiguration == kNonWeakPersistentConfiguration,
"Can only register non-weak Persistent references as static "
"references.");
if (PersistentNode* node = persistent_node_.Get()) { if (PersistentNode* node = persistent_node_.Get()) {
DCHECK(ThreadState::Current()); ThreadState::Current()->RegisterStaticPersistentNode(node);
ThreadState::Current()->RegisterStaticPersistentNode(node, nullptr);
LEAK_SANITIZER_IGNORE_OBJECT(this); LEAK_SANITIZER_IGNORE_OBJECT(this);
} }
return this; return this;
......
...@@ -29,7 +29,7 @@ PersistentRegion::~PersistentRegion() { ...@@ -29,7 +29,7 @@ PersistentRegion::~PersistentRegion() {
} }
int PersistentRegion::NumberOfPersistents() { int PersistentRegion::NumberOfPersistents() {
int persistent_count = 0; size_t persistent_count = 0;
for (PersistentNodeSlots* slots = slots_; slots; slots = slots->next_) { for (PersistentNodeSlots* slots = slots_; slots; slots = slots->next_) {
for (int i = 0; i < PersistentNodeSlots::kSlotCount; ++i) { for (int i = 0; i < PersistentNodeSlots::kSlotCount; ++i) {
if (!slots->slot_[i].IsUnused()) if (!slots->slot_[i].IsUnused())
...@@ -56,17 +56,10 @@ void PersistentRegion::EnsurePersistentNodeSlots(void* self, ...@@ -56,17 +56,10 @@ void PersistentRegion::EnsurePersistentNodeSlots(void* self,
slots_ = slots; slots_ = slots;
} }
void PersistentRegion::ReleasePersistentNode( void PersistentRegion::ReleasePersistentNode(PersistentNode* persistent_node) {
PersistentNode* persistent_node,
ThreadState::PersistentClearCallback callback) {
DCHECK(!persistent_node->IsUnused()); DCHECK(!persistent_node->IsUnused());
// 'self' is in use, containing the persistent wrapper object. // 'self' is in use, containing the persistent wrapper object.
void* self = persistent_node->Self(); void* self = persistent_node->Self();
if (callback) {
(*callback)(self);
DCHECK(persistent_node->IsUnused());
return;
}
Persistent<DummyGCBase>* persistent = Persistent<DummyGCBase>* persistent =
reinterpret_cast<Persistent<DummyGCBase>*>(self); reinterpret_cast<Persistent<DummyGCBase>*>(self);
persistent->Clear(); persistent->Clear();
...@@ -80,7 +73,7 @@ void PersistentRegion::ReleasePersistentNode( ...@@ -80,7 +73,7 @@ void PersistentRegion::ReleasePersistentNode(
void PersistentRegion::TracePersistentNodes(Visitor* visitor, void PersistentRegion::TracePersistentNodes(Visitor* visitor,
ShouldTraceCallback should_trace) { ShouldTraceCallback should_trace) {
free_list_head_ = nullptr; free_list_head_ = nullptr;
int persistent_count = 0; size_t persistent_count = 0;
PersistentNodeSlots** prev_next = &slots_; PersistentNodeSlots** prev_next = &slots_;
PersistentNodeSlots* slots = slots_; PersistentNodeSlots* slots = slots_;
while (slots) { while (slots) {
...@@ -142,7 +135,7 @@ void PersistentRegion::PrepareForThreadStateTermination() { ...@@ -142,7 +135,7 @@ void PersistentRegion::PrepareForThreadStateTermination() {
slots = slots->next_; slots = slots->next_;
} }
#if DCHECK_IS_ON() #if DCHECK_IS_ON()
DCHECK_EQ(persistent_count_, 0); DCHECK_EQ(persistent_count_, 0u);
#endif #endif
} }
...@@ -197,7 +190,7 @@ void CrossThreadPersistentRegion::UnpoisonCrossThreadPersistents() { ...@@ -197,7 +190,7 @@ void CrossThreadPersistentRegion::UnpoisonCrossThreadPersistents() {
#if DCHECK_IS_ON() #if DCHECK_IS_ON()
ProcessHeap::CrossThreadPersistentMutex().AssertAcquired(); ProcessHeap::CrossThreadPersistentMutex().AssertAcquired();
#endif #endif
int persistent_count = 0; size_t persistent_count = 0;
for (PersistentNodeSlots* slots = persistent_region_.slots_; slots; for (PersistentNodeSlots* slots = persistent_region_.slots_; slots;
slots = slots->next_) { slots = slots->next_) {
for (int i = 0; i < PersistentNodeSlots::kSlotCount; ++i) { for (int i = 0; i < PersistentNodeSlots::kSlotCount; ++i) {
......
...@@ -161,15 +161,7 @@ class PLATFORM_EXPORT PersistentRegion final { ...@@ -161,15 +161,7 @@ class PLATFORM_EXPORT PersistentRegion final {
USING_FAST_MALLOC(PersistentRegion); USING_FAST_MALLOC(PersistentRegion);
public: public:
PersistentRegion() PersistentRegion() = default;
: free_list_head_(nullptr),
slots_(nullptr)
#if DCHECK_IS_ON()
,
persistent_count_(0)
#endif
{
}
~PersistentRegion(); ~PersistentRegion();
PersistentNode* AllocatePersistentNode(void* self, TraceCallback trace) { PersistentNode* AllocatePersistentNode(void* self, TraceCallback trace) {
...@@ -188,7 +180,7 @@ class PLATFORM_EXPORT PersistentRegion final { ...@@ -188,7 +180,7 @@ class PLATFORM_EXPORT PersistentRegion final {
void FreePersistentNode(PersistentNode* persistent_node) { void FreePersistentNode(PersistentNode* persistent_node) {
#if DCHECK_IS_ON() #if DCHECK_IS_ON()
DCHECK_GT(persistent_count_, 0); DCHECK_GT(persistent_count_, 0u);
#endif #endif
persistent_node->SetFreeListNext(free_list_head_); persistent_node->SetFreeListNext(free_list_head_);
free_list_head_ = persistent_node; free_list_head_ = persistent_node;
...@@ -201,8 +193,7 @@ class PLATFORM_EXPORT PersistentRegion final { ...@@ -201,8 +193,7 @@ class PLATFORM_EXPORT PersistentRegion final {
return true; return true;
} }
void ReleasePersistentNode(PersistentNode*, void ReleasePersistentNode(PersistentNode*);
ThreadState::PersistentClearCallback);
using ShouldTraceCallback = bool (*)(Visitor*, PersistentNode*); using ShouldTraceCallback = bool (*)(Visitor*, PersistentNode*);
void TracePersistentNodes( void TracePersistentNodes(
Visitor*, Visitor*,
...@@ -215,10 +206,10 @@ class PLATFORM_EXPORT PersistentRegion final { ...@@ -215,10 +206,10 @@ class PLATFORM_EXPORT PersistentRegion final {
void EnsurePersistentNodeSlots(void*, TraceCallback); void EnsurePersistentNodeSlots(void*, TraceCallback);
PersistentNode* free_list_head_; PersistentNode* free_list_head_ = nullptr;
PersistentNodeSlots* slots_; PersistentNodeSlots* slots_ = nullptr;
#if DCHECK_IS_ON() #if DCHECK_IS_ON()
int persistent_count_; size_t persistent_count_ = 0;
#endif #endif
}; };
...@@ -272,7 +263,6 @@ class PLATFORM_EXPORT CrossThreadPersistentRegion final { ...@@ -272,7 +263,6 @@ class PLATFORM_EXPORT CrossThreadPersistentRegion final {
#endif #endif
private: private:
// We don't make CrossThreadPersistentRegion inherit from PersistentRegion // We don't make CrossThreadPersistentRegion inherit from PersistentRegion
// because we don't want to virtualize performance-sensitive methods // because we don't want to virtualize performance-sensitive methods
// such as PersistentRegion::allocate/freePersistentNode. // such as PersistentRegion::allocate/freePersistentNode.
......
...@@ -130,13 +130,9 @@ ThreadState::ThreadState() ...@@ -130,13 +130,9 @@ ThreadState::ThreadState()
persistent_region_(std::make_unique<PersistentRegion>()), persistent_region_(std::make_unique<PersistentRegion>()),
weak_persistent_region_(std::make_unique<PersistentRegion>()), weak_persistent_region_(std::make_unique<PersistentRegion>()),
start_of_stack_(reinterpret_cast<Address*>(WTF::GetStackStart())), start_of_stack_(reinterpret_cast<Address*>(WTF::GetStackStart())),
gc_state_(kNoGCScheduled),
gc_phase_(GCPhase::kNone),
reason_for_scheduled_gc_(BlinkGC::GCReason::kForcedGCForTesting),
#if defined(ADDRESS_SANITIZER) #if defined(ADDRESS_SANITIZER)
asan_fake_stack_(__asan_get_current_fake_stack()), asan_fake_stack_(__asan_get_current_fake_stack()),
#endif #endif
reported_memory_to_v8_(0),
sweeper_scheduler_(base::MakeRefCounted<WorkerPoolTaskRunner>()) { sweeper_scheduler_(base::MakeRefCounted<WorkerPoolTaskRunner>()) {
DCHECK(CheckThread()); DCHECK(CheckThread());
DCHECK(!**thread_specific_); DCHECK(!**thread_specific_);
...@@ -1173,24 +1169,21 @@ void ThreadState::LeaveStaticReferenceRegistrationDisabledScope() { ...@@ -1173,24 +1169,21 @@ void ThreadState::LeaveStaticReferenceRegistrationDisabledScope() {
static_persistent_registration_disabled_count_--; static_persistent_registration_disabled_count_--;
} }
void ThreadState::RegisterStaticPersistentNode( void ThreadState::RegisterStaticPersistentNode(PersistentNode* node) {
PersistentNode* node,
PersistentClearCallback callback) {
if (static_persistent_registration_disabled_count_) if (static_persistent_registration_disabled_count_)
return; return;
DCHECK(!static_persistents_.Contains(node)); DCHECK(!static_persistents_.Contains(node));
static_persistents_.insert(node, callback); static_persistents_.insert(node);
} }
void ThreadState::ReleaseStaticPersistentNodes() { void ThreadState::ReleaseStaticPersistentNodes() {
HashMap<PersistentNode*, ThreadState::PersistentClearCallback> HashSet<PersistentNode*> static_persistents;
static_persistents;
static_persistents.swap(static_persistents_); static_persistents.swap(static_persistents_);
PersistentRegion* persistent_region = GetPersistentRegion(); PersistentRegion* persistent_region = GetPersistentRegion();
for (const auto& it : static_persistents) for (PersistentNode* it : static_persistents)
persistent_region->ReleasePersistentNode(it.key, it.value); persistent_region->ReleasePersistentNode(it);
} }
void ThreadState::FreePersistentNode(PersistentRegion* persistent_region, void ThreadState::FreePersistentNode(PersistentRegion* persistent_region,
......
...@@ -338,15 +338,6 @@ class PLATFORM_EXPORT ThreadState final : private RAILModeObserver { ...@@ -338,15 +338,6 @@ class PLATFORM_EXPORT ThreadState final : private RAILModeObserver {
return weak_persistent_region_.get(); return weak_persistent_region_.get();
} }
// Visit all non-weak persistents allocated on this thread.
void VisitPersistents(Visitor*);
// Visit all weak persistents allocated on this thread.
void VisitWeakPersistents(Visitor*);
// Visit all DOM wrappers allocatd on this thread.
void VisitDOMWrappers(Visitor*);
struct GCSnapshotInfo { struct GCSnapshotInfo {
STACK_ALLOCATED(); STACK_ALLOCATED();
...@@ -360,12 +351,9 @@ class PLATFORM_EXPORT ThreadState final : private RAILModeObserver { ...@@ -360,12 +351,9 @@ class PLATFORM_EXPORT ThreadState final : private RAILModeObserver {
Vector<size_t> dead_size; Vector<size_t> dead_size;
}; };
void FreePersistentNode(PersistentRegion*, PersistentNode*); void RegisterStaticPersistentNode(PersistentNode*);
using PersistentClearCallback = void (*)(void*);
void RegisterStaticPersistentNode(PersistentNode*, PersistentClearCallback);
void ReleaseStaticPersistentNodes(); void ReleaseStaticPersistentNodes();
void FreePersistentNode(PersistentRegion*, PersistentNode*);
v8::Isolate* GetIsolate() const { return isolate_; } v8::Isolate* GetIsolate() const { return isolate_; }
...@@ -520,6 +508,15 @@ class PLATFORM_EXPORT ThreadState final : private RAILModeObserver { ...@@ -520,6 +508,15 @@ class PLATFORM_EXPORT ThreadState final : private RAILModeObserver {
Address*, Address*,
Address*); Address*);
// Visit all non-weak persistents allocated on this thread.
void VisitPersistents(Visitor*);
// Visit all weak persistents allocated on this thread.
void VisitWeakPersistents(Visitor*);
// Visit all DOM wrappers allocatd on this thread.
void VisitDOMWrappers(Visitor*);
// ShouldForceConservativeGC // ShouldForceConservativeGC
// implements the heuristics that are used to determine when to collect // implements the heuristics that are used to determine when to collect
// garbage. // garbage.
...@@ -602,9 +599,10 @@ class PLATFORM_EXPORT ThreadState final : private RAILModeObserver { ...@@ -602,9 +599,10 @@ class PLATFORM_EXPORT ThreadState final : private RAILModeObserver {
base::TimeDelta next_incremental_marking_step_duration_; base::TimeDelta next_incremental_marking_step_duration_;
base::TimeDelta previous_incremental_marking_time_left_; base::TimeDelta previous_incremental_marking_time_left_;
GCState gc_state_; GCState gc_state_ = GCState::kNoGCScheduled;
GCPhase gc_phase_; GCPhase gc_phase_ = GCPhase::kNone;
BlinkGC::GCReason reason_for_scheduled_gc_; BlinkGC::GCReason reason_for_scheduled_gc_ =
BlinkGC::GCReason::kForcedGCForTesting;
using PreFinalizerCallback = bool (*)(void*); using PreFinalizerCallback = bool (*)(void*);
using PreFinalizer = std::pair<void*, PreFinalizerCallback>; using PreFinalizer = std::pair<void*, PreFinalizerCallback>;
...@@ -630,10 +628,9 @@ class PLATFORM_EXPORT ThreadState final : private RAILModeObserver { ...@@ -630,10 +628,9 @@ class PLATFORM_EXPORT ThreadState final : private RAILModeObserver {
// references that either have to be cleared upon the thread // references that either have to be cleared upon the thread
// detaching from Oilpan and shutting down or references we // detaching from Oilpan and shutting down or references we
// have to clear before initiating LSan's leak detection. // have to clear before initiating LSan's leak detection.
HashMap<PersistentNode*, PersistentClearCallback> static_persistents_; HashSet<PersistentNode*> static_persistents_;
size_t reported_memory_to_v8_;
size_t reported_memory_to_v8_ = 0;
int gc_age_ = 0; int gc_age_ = 0;
struct GCData { struct GCData {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment