Skip to content

Commit 847b1d1

Browse files
committed
[JSC] BlockDirectory's bits should be guarded by a mutate-grow lock
https://bugs.webkit.org/show_bug.cgi?id=325474 rdar://188567814 Reviewed by Yusuke Suzuki and Dan Hecht. BlockDirectory's bits have relied on the mutator being the only thread that allocates from or sweeps a directory's blocks, so most mutator-side reads and writes went unlocked and m_bitvectorLock mainly guarded the few places where other threads touch the bits. That assumption stands in the way of sweeping concurrently with the mutator, and of a global GC with multiple mutators allocating from the same directory. This patch starts making those cases possible. m_bitvectorLock is now a MutateGrowLock, which is a thin renaming of WTF::ReadWriteLock. Per-block bit updates take the shared mutate() side, and only operations that resize or restructure the bit vectors (addBlock, removeBlock) take the exclusive grow() side. A block's inUse bit acts as a per-block lock, claimInUse() is the acquire and releaseInUse() release it. This could be error-prone so a Clang thread safety analysis tracks ownership of the claimed block through a new BlockOwnership capability. Right now the capability model only ASSERTs that some thread is holding the inUse bit but if there bugs in the future we can add proper thread id tracking. Because shared holders can write different bits of the same word at the same time, every per-bit accessor is a relaxed atomic operation. This is generally safe because of the inUse ownership mechanism. Bulk vector rewrites stay plain and still require the exclusive lock. Additionally, many of the cursors need to be turned into a new VersionedField. VersionedField is largely ported from libpas with a more C++ style interface. It works by attaching a version number to the contents of the field. The field supports moving in either direction, however, it's only possible to move one way without failure. IsoCellSet used to borrow the directory's lock to keep its per-block bits and its blocks-with-bits vector in sync. That no longer fits. addBlock and removeBlock now call into IsoCellSet while already holding grow(). IsoCellSet::add() runs on marker threads and needs mutual exclusion that the shared side doesn't provide. Taking grow() there would stall allocation and sweeping for a structure the directory doesn't own. IsoCellSet now has its own lock for installing, dropping and resizing those bits. To avoid deadlocking, a thread can't take the m_bitvectorLock after taking the IsoCellSet lock. In the future we may end up with a MutateGrowLock in IsoCellSets too. BlockDirectory::sweep() is also renamed sweepAll() to distinguish it from per-block sweeping, as a drive by change. Tests: JSC Covered by existing tests. No behavior change. Tools/TestWebKitAPI/Tests/WTF/VersionedField.cpp Canonical link: https://commits.webkit.org/323819@main
1 parent 2565fa1 commit 847b1d1

22 files changed

Lines changed: 773 additions & 238 deletions

‎Source/JavaScriptCore/heap/BlockDirectory.cpp‎

Lines changed: 131 additions & 104 deletions
Large diffs are not rendered by default.

‎Source/JavaScriptCore/heap/BlockDirectory.h‎

Lines changed: 102 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2012-2024 Apple Inc. All rights reserved.
2+
* Copyright (C) 2012-2024, 2026 Apple Inc. All rights reserved.
33
*
44
* Redistribution and use in source and binary forms, with or without
55
* modification, are permitted provided that the following conditions
@@ -35,8 +35,10 @@
3535
#include <wtf/DataLog.h>
3636
#include <wtf/DebugHeap.h>
3737
#include <wtf/Lock.h>
38+
#include <wtf/ReadWriteLock.h>
3839
#include <wtf/SharedTask.h>
3940
#include <wtf/Vector.h>
41+
#include <wtf/VersionedField.h>
4042

4143
namespace JSC {
4244

@@ -48,6 +50,20 @@ class LLIntOffsetsExtractor;
4850

4951
DECLARE_ALLOCATOR_WITH_HEAP_IDENTIFIER(BlockDirectory);
5052

53+
// ReadWriteLock with its methods/types renamed for how BlockDirectory uses them; see the comment above m_bits.
54+
class WTF_CAPABILITY_LOCK MutateGrowLock : private ReadWriteLock {
55+
public:
56+
void mutateLock() WTF_ACQUIRES_SHARED_LOCK() { readLock(); }
57+
void mutateUnlock() WTF_RELEASES_SHARED_LOCK() { readUnlock(); }
58+
void growLock() WTF_ACQUIRES_LOCK() { writeLock(); }
59+
void growUnlock() WTF_RELEASES_LOCK() { writeUnlock(); }
60+
61+
using MutateLockView = WTF::ReadLockView;
62+
using GrowLockView = WTF::WriteLockView;
63+
MutateLockView& mutate() WTF_RETURNS_LOCK(*this) { return read(); }
64+
GrowLockView& grow() WTF_RETURNS_LOCK(*this) { return write(); }
65+
};
66+
5167
class BlockDirectory {
5268
WTF_MAKE_NONCOPYABLE(BlockDirectory);
5369
WTF_DEPRECATED_MAKE_FAST_ALLOCATED_WITH_HEAP_IDENTIFIER(BlockDirectory, BlockDirectory);
@@ -67,7 +83,7 @@ class BlockDirectory {
6783
void endMarking();
6884
void LEGACY_NODELETE snapshotUnsweptForEdenCollection();
6985
void LEGACY_NODELETE snapshotUnsweptForFullCollection();
70-
void sweep();
86+
void sweepAll();
7187
void shrink();
7288
void assertNoUnswept();
7389
size_t cellSize() const { return m_cellSize; }
@@ -90,49 +106,93 @@ class BlockDirectory {
90106

91107
#if ASSERT_ENABLED
92108
JS_EXPORT_PRIVATE void assertIsMutatorOrMutatorIsStopped() const WTF_ASSERTS_ACQUIRED_SHARED_LOCK(m_bitvectorLock);
93-
void assertSweeperIsSuspended() const WTF_ASSERTS_ACQUIRED_LOCK(m_bitvectorLock);
94109
#else
95110
ALWAYS_INLINE void assertIsMutatorOrMutatorIsStopped() const WTF_ASSERTS_ACQUIRED_SHARED_LOCK(m_bitvectorLock) { }
96-
ALWAYS_INLINE void assertSweeperIsSuspended() const WTF_ASSERTS_ACQUIRED_LOCK(m_bitvectorLock) { }
97111
#endif
98-
// This feels like it shouldn't be needed to go from assertIsMutatorOrMutatorIsStopped -> Locker but Clang's seems to think it is necessary
99-
// to release the capability.
100-
ALWAYS_INLINE void releaseAssertAcquiredBitVectorLock() const WTF_RELEASES_SHARED_CAPABILITY(m_bitvectorLock) WTF_IGNORES_THREAD_SAFETY_ANALYSIS { }
112+
MutateGrowLock& bitvectorLock() LIFETIME_BOUND WTF_RETURNS_LOCK(m_bitvectorLock) { return m_bitvectorLock; }
101113

102-
Lock& bitvectorLock() LIFETIME_BOUND WTF_RETURNS_LOCK(m_bitvectorLock) { return m_bitvectorLock; }
114+
void assertInUse(size_t index) const WTF_REQUIRES_SHARED_LOCK(m_bitvectorLock) WTF_ASSERTS_ACQUIRED_CAPABILITY(m_blockOwnership)
115+
{
116+
ASSERT_UNUSED(index, isInUse(index));
117+
}
103118

104-
#define BLOCK_DIRECTORY_BIT_ACCESSORS(lowerBitName, capitalBitName) \
105-
bool is ## capitalBitName(size_t index) const WTF_REQUIRES_SHARED_LOCK(m_bitvectorLock) { return m_bits.is ## capitalBitName(index); } \
119+
#if ASSERT_ENABLED
120+
JS_EXPORT_PRIVATE void assertWorldIsStopped() const WTF_ASSERTS_ACQUIRED_LOCK(m_bitvectorLock) WTF_ASSERTS_ACQUIRED_CAPABILITY(m_blockOwnership);
121+
#else
122+
ALWAYS_INLINE void assertWorldIsStopped() const WTF_ASSERTS_ACQUIRED_LOCK(m_bitvectorLock) WTF_ASSERTS_ACQUIRED_CAPABILITY(m_blockOwnership) { }
123+
#endif
124+
125+
// HeapCell::notifyNeedsDestruction() may set a block's destructible bit without holding its inUse bit.
126+
// It's only used in MayNeedDestruction subspaces, where the owner clears the bit only when it sweeps an
127+
// empty block. A cell being notified is live, so its block isn't empty and the two can't race.
128+
void assertMayNotifyNeedsDestruction() const WTF_ASSERTS_ACQUIRED_CAPABILITY(m_blockOwnership) { }
129+
130+
// Writing a BlockOwned bit also requires m_blockOwnership. Bulk reads don't: a reader racing with the
131+
// owner either fails to claim the block or fails the recheck after claiming it.
132+
#define BLOCK_DIRECTORY_BIT_OWNERSHIP_BlockOwned WTF_REQUIRES_LOCK(m_blockOwnership)
133+
#define BLOCK_DIRECTORY_BIT_OWNERSHIP_Unowned
134+
#define BLOCK_DIRECTORY_BIT_ACCESSORS(lowerBitName, capitalBitName, ownership) \
135+
bool is ## capitalBitName(size_t index) const WTF_REQUIRES_SHARED_LOCK(m_bitvectorLock) { return m_bits.bit<BlockDirectoryBits::Kind::capitalBitName>(index).concurrentGet(std::memory_order_relaxed); } \
106136
bool is ## capitalBitName(MarkedBlock::Handle* block) const WTF_REQUIRES_SHARED_LOCK(m_bitvectorLock) { return is ## capitalBitName(block->index()); } \
107137
BlockDirectoryBits::BlockDirectoryBitVectorView<BlockDirectoryBits::Kind::capitalBitName> lowerBitName ## BitsView() const WTF_REQUIRES_SHARED_LOCK(m_bitvectorLock) { return m_bits.lowerBitName(); } \
108138
\
109-
void setIs ## capitalBitName(size_t index, bool value) WTF_REQUIRES_LOCK(m_bitvectorLock) { m_bits.setIs ## capitalBitName(index, value); } \
110-
void setIs ## capitalBitName(MarkedBlock::Handle* block, bool value) WTF_REQUIRES_LOCK(m_bitvectorLock) { setIs ## capitalBitName(block->index(), value); } \
139+
void setIs ## capitalBitName(size_t index, bool value) WTF_REQUIRES_SHARED_LOCK(m_bitvectorLock) BLOCK_DIRECTORY_BIT_OWNERSHIP_ ## ownership { m_bits.bit<BlockDirectoryBits::Kind::capitalBitName>(index).concurrentSet(value, std::memory_order_relaxed); } \
140+
void setIs ## capitalBitName(MarkedBlock::Handle* block, bool value) WTF_REQUIRES_SHARED_LOCK(m_bitvectorLock) BLOCK_DIRECTORY_BIT_OWNERSHIP_ ## ownership { setIs ## capitalBitName(block->index(), value); } \
141+
bool testAndClearIs ## capitalBitName(size_t index) WTF_REQUIRES_SHARED_LOCK(m_bitvectorLock) BLOCK_DIRECTORY_BIT_OWNERSHIP_ ## ownership { return m_bits.bit<BlockDirectoryBits::Kind::capitalBitName>(index).concurrentTestAndClear(std::memory_order_relaxed); } \
111142
BlockDirectoryBits::BlockDirectoryBitVectorRef<BlockDirectoryBits::Kind::capitalBitName> lowerBitName ## Bits() WTF_REQUIRES_LOCK(m_bitvectorLock) { return m_bits.lowerBitName(); }
112143

113144
FOR_EACH_BLOCK_DIRECTORY_BIT(BLOCK_DIRECTORY_BIT_ACCESSORS)
114145
#undef BLOCK_DIRECTORY_BIT_ACCESSORS
146+
#undef BLOCK_DIRECTORY_BIT_OWNERSHIP_Unowned
147+
#undef BLOCK_DIRECTORY_BIT_OWNERSHIP_BlockOwned
148+
149+
// The inUse bit acts as a per-block lock; holding it allows writing that block's BlockOwned bits.
150+
// Callers that pick a block by scanning the bits must recheck those bits after this returns true.
151+
[[nodiscard]] bool claimInUse(size_t index) WTF_REQUIRES_SHARED_LOCK(m_bitvectorLock)
152+
{
153+
return !m_bits.bit<BlockDirectoryBits::Kind::InUse>(index).concurrentTestAndSet(std::memory_order_acquire);
154+
}
155+
156+
void claimInUseOrCrash(size_t index) WTF_REQUIRES_SHARED_LOCK(m_bitvectorLock) WTF_ASSERTS_ACQUIRED_CAPABILITY(m_blockOwnership)
157+
{
158+
bool claimed = claimInUse(index);
159+
RELEASE_ASSERT(claimed);
160+
}
161+
162+
void releaseInUse(size_t index) WTF_REQUIRES_SHARED_LOCK(m_bitvectorLock)
163+
{
164+
m_bits.bit<BlockDirectoryBits::Kind::InUse>(index).concurrentSet(false, std::memory_order_release);
165+
}
115166

116167
// A destructible block still owes its old owner a destructor pass over every one of its cells,
117168
// and whoever took it would have to pay that inline, so it stays with the sweeper until the bit
118169
// says the destructors have run.
119-
auto stealableBits() const WTF_REQUIRES_SHARED_LOCK(m_bitvectorLock) { return emptyBitsView() & ~destructibleBitsView() & ~inUseBitsView(); }
170+
auto stealableBits() const WTF_REQUIRES_SHARED_LOCK(m_bitvectorLock) { return emptyBitsView() & ~destructibleBitsView(); }
120171
bool isStealable(size_t index) const WTF_REQUIRES_SHARED_LOCK(m_bitvectorLock) { return stealableBits()[index]; }
121172

122173
template<typename Func>
123174
void forEachBitVector(NOESCAPE const Func& func) WTF_REQUIRES_LOCK(m_bitvectorLock)
124175
{
125-
#define BLOCK_DIRECTORY_BIT_CALLBACK(lowerBitName, capitalBitName) \
126-
func(m_bits.lowerBitName());
176+
#define BLOCK_DIRECTORY_BIT_CALLBACK(lowerBitName, capitalBitName, ownership) \
177+
func(lowerBitName ## Bits());
127178
FOR_EACH_BLOCK_DIRECTORY_BIT(BLOCK_DIRECTORY_BIT_CALLBACK);
128179
#undef BLOCK_DIRECTORY_BIT_CALLBACK
129180
}
130-
181+
182+
void clearBitsForRemovedBlock(size_t index) WTF_REQUIRES_LOCK(m_bitvectorLock) WTF_REQUIRES_LOCK(m_blockOwnership)
183+
{
184+
// We don't have to clear inUse last here because we're holding the exclusive growth lock.
185+
#define BLOCK_DIRECTORY_BIT_CLEAR(lowerBitName, capitalBitName, ownership) \
186+
lowerBitName##Bits()[index] = false;
187+
FOR_EACH_BLOCK_DIRECTORY_BIT(BLOCK_DIRECTORY_BIT_CLEAR)
188+
#undef BLOCK_DIRECTORY_BIT_CLEAR
189+
}
190+
131191
template<typename Func>
132192
void forEachBitVectorWithName(NOESCAPE const Func& func) const WTF_REQUIRES_SHARED_LOCK(m_bitvectorLock)
133193
{
134-
#define BLOCK_DIRECTORY_BIT_CALLBACK(lowerBitName, capitalBitName) \
135-
func(m_bits.lowerBitName(), #capitalBitName);
194+
#define BLOCK_DIRECTORY_BIT_CALLBACK(lowerBitName, capitalBitName, ownership) \
195+
func(lowerBitName ## BitsView(), #capitalBitName);
136196
FOR_EACH_BLOCK_DIRECTORY_BIT(BLOCK_DIRECTORY_BIT_CALLBACK);
137197
#undef BLOCK_DIRECTORY_BIT_CALLBACK
138198
}
@@ -145,8 +205,8 @@ class BlockDirectory {
145205

146206
MarkedBlock::Handle* findEmptyBlockToSteal();
147207

148-
// Callers must already have cleared the block's in-use bit.
149-
void noteBlockMayBeStealable(unsigned index) WTF_REQUIRES_LOCK(m_bitvectorLock);
208+
// Call while holding the block's inUse bit; this releases it.
209+
void releaseAndNoteIfStealable(unsigned index) WTF_REQUIRES_SHARED_LOCK(m_bitvectorLock);
150210

151211
inline MarkedBlock::Handle* findBlockToSweep();
152212
MarkedBlock::Handle* findBlockToSweep(unsigned& unsweptCursor);
@@ -155,7 +215,7 @@ class BlockDirectory {
155215
MarkedBlock::Handle* LEGACY_NODELETE findMarkedBlockHandleDebug(MarkedBlock*);
156216

157217
void didFinishUsingBlock(MarkedBlock::Handle*);
158-
void didFinishUsingBlock(AbstractLocker&, MarkedBlock::Handle*) WTF_REQUIRES_LOCK(m_bitvectorLock);
218+
void didFinishUsingBlock(AbstractLocker&, MarkedBlock::Handle*) WTF_REQUIRES_SHARED_LOCK(m_bitvectorLock);
159219

160220
Subspace* subspace() const { return m_subspace; }
161221
MarkedSpace& LEGACY_NODELETE markedSpace() const;
@@ -179,10 +239,26 @@ class BlockDirectory {
179239
Vector<std::pair<MarkedBlock::Handle*, MarkedBlock*>> m_blocks;
180240
Vector<unsigned> m_freeBlockIndices;
181241

182-
// Mutator uses this to guard resizing the bitvectors. Those things in the GC that may run
183-
// concurrently to the mutator must lock this when accessing the bitvectors.
242+
// How the bits are synchronized:
243+
//
244+
// - m_bitvectorLock.grow() (exclusive) is taken only to resize or restructure the vectors: addBlock()
245+
// and removeBlock(). Everything else that touches the bits holds m_bitvectorLock.mutate() (shared),
246+
// which keeps the storage from moving. Neither side is recursive.
247+
// - A block's inUse bit is a per-block lock: claimInUse() (acquire) and releaseInUse() (release).
248+
// Only its holder may write that block's BlockOwned bits, and releasing must be its last write.
249+
// Callers that choose a block by scanning must recheck the bits after claiming it.
250+
// - Shared holders can write different bits of the same word at once, so single-bit accessors are
251+
// relaxed atomics; the inUse acquire/release is what orders an owner's writes for the next owner.
252+
// - Unowned bits: inUse itself, and the marking bits, which any marker thread may set under mutate().
253+
// - Bulk rewrites of whole vectors are plain stores and happen only with the world stopped
254+
// (assertWorldIsStopped()).
255+
// - Exception: notifyNeedsDestruction() may set a block's destructible bit without holding inUse;
256+
// see assertMayNotifyNeedsDestruction().
257+
// - Exception: growth only happens on the mutator, so the mutator, or anyone while the world is
258+
// stopped, may read the bits without mutate(); see assertIsMutatorOrMutatorIsStopped().
184259
BlockDirectoryBits m_bits WTF_GUARDED_BY_LOCK(m_bitvectorLock); // Don't access this directly use one of the accessors above.
185-
Lock m_bitvectorLock;
260+
MutateGrowLock m_bitvectorLock;
261+
[[no_unique_address]] BlockOwnership m_blockOwnership;
186262
Lock m_localAllocatorsLock;
187263
CellAttributes m_attributes;
188264

@@ -191,8 +267,8 @@ class BlockDirectory {
191267
// After you do something to a block based on one of these cursors, you clear the bit in the
192268
// corresponding bitvector and leave the cursor where it was. We can use unsigned instead of size_t since
193269
// this number is bound by capacity of Vector m_blocks, which must be within unsigned.
194-
unsigned m_emptyCursor { 0 };
195-
unsigned m_unsweptCursor { 0 }; // Points to the next block that is a candidate for incremental sweeping.
270+
VersionedField m_emptyCursor { 0 };
271+
VersionedField m_unsweptCursor { 0 }; // Points to the next block that is a candidate for incremental sweeping.
196272

197273
// FIXME: All of these should probably be references.
198274
// https://bugs.webkit.org/show_bug.cgi?id=166988

‎Source/JavaScriptCore/heap/BlockDirectoryBits.h‎

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019-2024 Apple Inc. All rights reserved.
2+
* Copyright (C) 2019-2024, 2026 Apple Inc. All rights reserved.
33
*
44
* Redistribution and use in source and binary forms, with or without
55
* modification, are permitted provided that the following conditions
@@ -26,27 +26,32 @@
2626
#pragma once
2727

2828
#include <array>
29+
#include <wtf/Atomics.h>
2930
#include <wtf/FastBitVector.h>
3031
#include <wtf/TZoneMalloc.h>
32+
#include <wtf/ThreadSafetyAnalysis.h>
3133
#include <wtf/Vector.h>
3234

3335
WTF_ALLOW_UNSAFE_BUFFER_USAGE_BEGIN
3436

3537
namespace JSC {
3638

39+
// Acquiring this capability means that the thread in question may write BlockOwned bits.
40+
class WTF_CAPABILITY("inUse block ownership") BlockOwnership { };
41+
3742
#define FOR_EACH_BLOCK_DIRECTORY_BIT(macro) \
38-
macro(live, Live) /* The set of block indices that have actual blocks. */\
39-
macro(empty, Empty) /* The set of all blocks that have no live objects and are not free listed. */ \
40-
macro(allocated, Allocated) /* The set of all blocks that are full of live objects. */\
41-
macro(canAllocate, CanAllocate) /* The set of all blocks are live and not retired (i.e. are more than minMarkedBlockUtilization full). Note: This also implies they wouldn't be allocated. */ \
42-
macro(destructible, Destructible) /* The set of all blocks that may have destructors to run. */\
43-
macro(eden, Eden) /* The set of all blocks that have new objects since the last GC. */\
44-
macro(unswept, Unswept) /* The set of all blocks that could be swept by the incremental sweeper. */\
45-
macro(inUse, InUse) /* This tells us if a block is currently being allocated from or swept. This acts like a lock bit. */\
43+
macro(live, Live, BlockOwned) /* The set of block indices that have actual blocks. */\
44+
macro(empty, Empty, BlockOwned) /* The set of all blocks that have no live objects and are not free listed. */ \
45+
macro(allocated, Allocated, BlockOwned) /* The set of all blocks that are full of live objects. */\
46+
macro(canAllocate, CanAllocate, BlockOwned) /* The set of all blocks are live and not retired (i.e. are more than minMarkedBlockUtilization full). Note: This also implies they wouldn't be allocated. */ \
47+
macro(destructible, Destructible, BlockOwned) /* The set of all blocks that may have destructors to run. */\
48+
macro(eden, Eden, BlockOwned) /* The set of all blocks that have new objects since the last GC. */\
49+
macro(unswept, Unswept, BlockOwned) /* The set of all blocks that could be swept by the incremental sweeper. */\
50+
macro(inUse, InUse, Unowned) /* This tells us if a block is currently being allocated from or swept. This acts like a lock bit. */\
4651
\
47-
/* These are computed during marking. */\
48-
macro(markingNotEmpty, MarkingNotEmpty) /* The set of all blocks that are not empty. */ \
49-
macro(markingRetired, MarkingRetired) /* The set of all blocks that are retired. */
52+
/* These are computed during marking, by any marker thread. */\
53+
macro(markingNotEmpty, MarkingNotEmpty, Unowned) /* The set of all blocks that are not empty. */ \
54+
macro(markingRetired, MarkingRetired, Unowned) /* The set of all blocks that are retired. */
5055

5156
class BlockDirectoryBits {
5257
WTF_MAKE_TZONE_ALLOCATED(BlockDirectoryBits);
@@ -56,12 +61,12 @@ class BlockDirectoryBits {
5661
static constexpr unsigned indexMask = (1U << segmentShift) - 1;
5762
static_assert((1 << segmentShift) == bitsPerSegment);
5863

59-
#define BLOCK_DIRECTORY_BIT_KIND_COUNT(lowerBitName, capitalBitName) + 1
64+
#define BLOCK_DIRECTORY_BIT_KIND_COUNT(lowerBitName, capitalBitName, ownership) + 1
6065
static constexpr unsigned numberOfBlockDirectoryBitKinds = 0 FOR_EACH_BLOCK_DIRECTORY_BIT(BLOCK_DIRECTORY_BIT_KIND_COUNT);
6166
#undef BLOCK_DIRECTORY_BIT_KIND_COUNT
6267

6368
enum class Kind {
64-
#define BLOCK_DIRECTORY_BIT_KIND_DECLARATION(lowerBitName, capitalBitName) \
69+
#define BLOCK_DIRECTORY_BIT_KIND_DECLARATION(lowerBitName, capitalBitName, ownership) \
6570
capitalBitName,
6671
FOR_EACH_BLOCK_DIRECTORY_BIT(BLOCK_DIRECTORY_BIT_KIND_DECLARATION)
6772
#undef BLOCK_DIRECTORY_BIT_KIND_DECLARATION
@@ -120,6 +125,8 @@ class BlockDirectoryBits {
120125
template<Kind kind>
121126
using BlockDirectoryBitVectorView = WTF::FastBitVectorImpl<BlockDirectoryBitVectorWordView<kind>>;
122127

128+
// FIXME: We should be able to combine this with API changes to FastBitVectorImpl so we don't need
129+
// this subclass.
123130
template<Kind kind>
124131
class BlockDirectoryBitVectorRef final : public BlockDirectoryBitVectorView<kind> {
125132
public:
@@ -165,22 +172,39 @@ class BlockDirectoryBits {
165172
}
166173
};
167174

168-
#define BLOCK_DIRECTORY_BIT_ACCESSORS(lowerBitName, capitalBitName) \
169-
bool is ## capitalBitName(size_t index) const \
170-
{ \
171-
return lowerBitName()[index]; \
172-
} \
173-
void setIs ## capitalBitName(size_t index, bool value) \
174-
{ \
175-
lowerBitName()[index] = value; \
176-
} \
175+
template<Kind kind>
176+
BlockDirectoryBitVectorRef<kind> words()
177+
{
178+
return BlockDirectoryBitVectorRef<kind>(BlockDirectoryBitVectorWordView<kind>(m_segments.span().data(), m_numBits));
179+
}
180+
181+
template<Kind kind>
182+
BlockDirectoryBitVectorView<kind> words() const
183+
{
184+
return BlockDirectoryBitVectorView<kind>(BlockDirectoryBitVectorWordView<kind>(m_segments.span().data(), m_numBits));
185+
}
186+
187+
template<Kind kind>
188+
WTF::FastBitReference bit(size_t index)
189+
{
190+
return words<kind>().at(index);
191+
}
192+
193+
template<Kind kind>
194+
const WTF::FastBitReference bit(size_t index) const
195+
{
196+
// The view's operator[] is a plain load; callers need FastBitReference::concurrentGet().
197+
return const_cast<BlockDirectoryBits*>(this)->bit<kind>(index);
198+
}
199+
200+
#define BLOCK_DIRECTORY_BIT_ACCESSORS(lowerBitName, capitalBitName, ownership) \
177201
BlockDirectoryBitVectorView<Kind::capitalBitName> lowerBitName() const \
178202
{ \
179-
return BlockDirectoryBitVectorView<Kind::capitalBitName>(BlockDirectoryBitVectorWordView<Kind::capitalBitName>(m_segments.span().data(), m_numBits)); \
203+
return words<Kind::capitalBitName>(); \
180204
} \
181205
BlockDirectoryBitVectorRef<Kind::capitalBitName> lowerBitName() \
182206
{ \
183-
return BlockDirectoryBitVectorRef<Kind::capitalBitName>(BlockDirectoryBitVectorWordView<Kind::capitalBitName>(m_segments.span().data(), m_numBits)); \
207+
return words<Kind::capitalBitName>(); \
184208
}
185209
FOR_EACH_BLOCK_DIRECTORY_BIT(BLOCK_DIRECTORY_BIT_ACCESSORS)
186210
#undef BLOCK_DIRECTORY_BIT_ACCESSORS

0 commit comments

Comments
 (0)