Skip to content

Truly independent web browser

LibGC: Stop counting references to the null WeakImpl

Andreas Klingba8a70588d49
diff --git a/Libraries/LibGC/Weak.h b/Libraries/LibGC/Weak.h
index 54a5518025f..6e71264464a 100644
--- a/Libraries/LibGC/Weak.h
+++ b/Libraries/LibGC/Weak.h
@@ -17,10 +17,21 @@ class WeakBlock;
class WeakImpl {
public:
+ enum class State {
+ Allocated,
+ Freelist,
+ // The state of the null impl, which all threads share, so it does not count references.
+ Null,
+ };
+
// NOTE: Null GC::Weaks point at this WeakImpl. This allows Weak to always chase the impl pointer without null-checking it.
static GC_API WeakImpl the_null_weak_impl;
WeakImpl() = default;
+ explicit constexpr WeakImpl(State state)
+ : m_state(state)
+ {
+ }
WeakImpl(void* ptr)
: m_ptr(ptr)
{
@@ -32,9 +43,15 @@ public:
bool operator==(WeakImpl const& other) const { return m_ptr == other.m_ptr; }
bool operator!=(WeakImpl const& other) const { return m_ptr != other.m_ptr; }
- void ref() const { ++m_ref_count; }
+ void ref() const
+ {
+ if (m_state != State::Null)
+ ++m_ref_count;
+ }
void unref() const
{
+ if (m_state == State::Null)
+ return;
VERIFY(m_ref_count);
--m_ref_count;
}
@@ -42,11 +59,6 @@ public:
size_t ref_count() const { return m_ref_count; }
static constexpr size_t value_offset() { return offsetof(WeakImpl, m_ptr); }
- enum class State {
- Allocated,
- Freelist,
- };
-
void set_state(State state) { m_state = state; }
State state() const { return m_state; }
diff --git a/Libraries/LibGC/WeakBlock.cpp b/Libraries/LibGC/WeakBlock.cpp
index 39f43aedd35..a4cc81dd773 100644
--- a/Libraries/LibGC/WeakBlock.cpp
+++ b/Libraries/LibGC/WeakBlock.cpp
@@ -10,7 +10,7 @@
namespace GC {
-WeakImpl WeakImpl::the_null_weak_impl;
+constinit WeakImpl WeakImpl::the_null_weak_impl { WeakImpl::State::Null };
WeakBlock* WeakBlock::create()
{