Skip to content

Truly independent web browser

LibGC: Let a block-allocator test wait for its block to come back

sideshowbarker0713ac89f5df
diff --git a/Libraries/LibGC/BlockAllocator.cpp b/Libraries/LibGC/BlockAllocator.cpp
index 773ba857909..37fb6394903 100644
--- a/Libraries/LibGC/BlockAllocator.cpp
+++ b/Libraries/LibGC/BlockAllocator.cpp
@@ -546,6 +546,12 @@ size_t BlockAllocator::block_count()
return m_blocks.size() + m_returned_blocks.size();
}
+size_t BlockAllocator::returned_block_count_for_testing()
+{
+ MutexLocker locker(m_mutex);
+ return m_returned_blocks.size();
+}
+
HeapRegionBaseCopyRegistration::HeapRegionBaseCopyRegistration(FlatPtr& copy)
: m_copy(&copy)
{
diff --git a/Libraries/LibGC/BlockAllocator.h b/Libraries/LibGC/BlockAllocator.h
index bdcd59da60a..315e2ee31d1 100644
--- a/Libraries/LibGC/BlockAllocator.h
+++ b/Libraries/LibGC/BlockAllocator.h
@@ -36,6 +36,9 @@ public:
size_t block_count();
+ // How many blocks are back in the pool after their pages went back to the system. Tests wait on it.
+ size_t returned_block_count_for_testing();
+
// Gives the pages of the free blocks back to the system right away, rather than leaving them for the system to take
// when it runs short of memory. For a heap that went away, whose blocks may never be reused.
void return_freed_blocks_to_system();
diff --git a/Tests/LibGC/TestBlockAllocator.cpp b/Tests/LibGC/TestBlockAllocator.cpp
index 4862a70af79..f4a2d152718 100644
--- a/Tests/LibGC/TestBlockAllocator.cpp
+++ b/Tests/LibGC/TestBlockAllocator.cpp
@@ -112,9 +112,12 @@ TEST_CASE(pages_of_a_block_that_stays_unused_go_back_to_the_system)
// NB: MADV_FREE leaves the contents until the system runs short of memory, so only a real return zeroes the page.
// Nothing has to collect garbage for this to happen.
+ // The worker zeroes the pages before it puts the block back in the pool, so wait until the block is back: An
+ // allocation before then would get a fresh slot.
auto deadline = MonotonicTime::now() + AK::Duration::from_seconds(30);
- while (block[0] != 0 && MonotonicTime::now() < deadline)
+ while (allocator.returned_block_count_for_testing() == 0 && MonotonicTime::now() < deadline)
(void)Core::System::sleep_ms(1);
+ EXPECT_EQ(allocator.returned_block_count_for_testing(), 1u);
u8 first_byte = block[0];
EXPECT_EQ(first_byte, 0);