diff options
Diffstat (limited to 'deps/v8/test/unittests/heap/cppgc/object-start-bitmap-unittest.cc')
-rw-r--r-- | deps/v8/test/unittests/heap/cppgc/object-start-bitmap-unittest.cc | 208 |
1 files changed, 108 insertions, 100 deletions
diff --git a/deps/v8/test/unittests/heap/cppgc/object-start-bitmap-unittest.cc b/deps/v8/test/unittests/heap/cppgc/object-start-bitmap-unittest.cc index 1625a3a586..fd8fcd8d54 100644 --- a/deps/v8/test/unittests/heap/cppgc/object-start-bitmap-unittest.cc +++ b/deps/v8/test/unittests/heap/cppgc/object-start-bitmap-unittest.cc @@ -6,6 +6,7 @@ #include "include/cppgc/allocation.h" #include "src/base/macros.h" +#include "src/base/page-allocator.h" #include "src/heap/cppgc/globals.h" #include "src/heap/cppgc/heap-object-header.h" #include "src/heap/cppgc/page-memory.h" @@ -18,164 +19,171 @@ namespace internal { namespace { -bool IsEmpty(const ObjectStartBitmap& bitmap) { - size_t count = 0; - bitmap.Iterate([&count](Address) { count++; }); - return count == 0; -} - -// Abstraction for objects that hides ObjectStartBitmap::kGranularity and -// the base address as getting either of it wrong will result in failed DCHECKs. -class Object { +class PageWithBitmap final { public: - static Address kBaseOffset; + PageWithBitmap() + : base_(allocator_.AllocatePages( + nullptr, kPageSize, kPageSize, + v8::base::PageAllocator::Permission::kReadWrite)), + bitmap_(new(base_) ObjectStartBitmap) {} + + PageWithBitmap(const PageWithBitmap&) = delete; + PageWithBitmap& operator=(const PageWithBitmap&) = delete; + + ~PageWithBitmap() { allocator_.FreePages(base_, kPageSize); } + + ObjectStartBitmap& bitmap() const { return *bitmap_; } + + void* base() const { return base_; } + size_t size() const { return kPageSize; } + + v8::base::PageAllocator allocator_; + void* base_; + ObjectStartBitmap* bitmap_; +}; - explicit Object(size_t number) : number_(number) { - const size_t max_entries = ObjectStartBitmap::MaxEntries(); - EXPECT_GE(max_entries, number_); +class ObjectStartBitmapTest : public ::testing::Test { + protected: + void AllocateObject(size_t object_position) { + bitmap().SetBit(ObjectAddress(object_position)); } - Address address() const { - return kBaseOffset + ObjectStartBitmap::Granularity() * number_; + void FreeObject(size_t object_position) { + bitmap().ClearBit(ObjectAddress(object_position)); } - HeapObjectHeader* header() const { - return reinterpret_cast<HeapObjectHeader*>(address()); + bool CheckObjectAllocated(size_t object_position) { + return bitmap().CheckBit(ObjectAddress(object_position)); } - // Allow implicitly converting Object to Address. - operator Address() const { return address(); } + Address ObjectAddress(size_t pos) const { + return reinterpret_cast<Address>(reinterpret_cast<uintptr_t>(page.base()) + + pos * ObjectStartBitmap::Granularity()); + } + + HeapObjectHeader* ObjectHeader(size_t pos) const { + return reinterpret_cast<HeapObjectHeader*>(ObjectAddress(pos)); + } + + ObjectStartBitmap& bitmap() const { return page.bitmap(); } + + bool IsEmpty() const { + size_t count = 0; + bitmap().Iterate([&count](Address) { count++; }); + return count == 0; + } private: - const size_t number_; + PageWithBitmap page; }; -Address Object::kBaseOffset = reinterpret_cast<Address>(0x4000); - } // namespace -TEST(ObjectStartBitmapTest, MoreThanZeroEntriesPossible) { +TEST_F(ObjectStartBitmapTest, MoreThanZeroEntriesPossible) { const size_t max_entries = ObjectStartBitmap::MaxEntries(); EXPECT_LT(0u, max_entries); } -TEST(ObjectStartBitmapTest, InitialEmpty) { - ObjectStartBitmap bitmap(Object::kBaseOffset); - EXPECT_TRUE(IsEmpty(bitmap)); -} +TEST_F(ObjectStartBitmapTest, InitialEmpty) { EXPECT_TRUE(IsEmpty()); } -TEST(ObjectStartBitmapTest, SetBitImpliesNonEmpty) { - ObjectStartBitmap bitmap(Object::kBaseOffset); - bitmap.SetBit(Object(0)); - EXPECT_FALSE(IsEmpty(bitmap)); +TEST_F(ObjectStartBitmapTest, SetBitImpliesNonEmpty) { + AllocateObject(0); + EXPECT_FALSE(IsEmpty()); } -TEST(ObjectStartBitmapTest, SetBitCheckBit) { - ObjectStartBitmap bitmap(Object::kBaseOffset); - Object object(7); - bitmap.SetBit(object); - EXPECT_TRUE(bitmap.CheckBit(object)); +TEST_F(ObjectStartBitmapTest, SetBitCheckBit) { + constexpr size_t object_num = 7; + AllocateObject(object_num); + EXPECT_TRUE(CheckObjectAllocated(object_num)); } -TEST(ObjectStartBitmapTest, SetBitClearbitCheckBit) { - ObjectStartBitmap bitmap(Object::kBaseOffset); - Object object(77); - bitmap.SetBit(object); - bitmap.ClearBit(object); - EXPECT_FALSE(bitmap.CheckBit(object)); +TEST_F(ObjectStartBitmapTest, SetBitClearbitCheckBit) { + constexpr size_t object_num = 77; + AllocateObject(object_num); + FreeObject(object_num); + EXPECT_FALSE(CheckObjectAllocated(object_num)); } -TEST(ObjectStartBitmapTest, SetBitClearBitImpliesEmpty) { - ObjectStartBitmap bitmap(Object::kBaseOffset); - Object object(123); - bitmap.SetBit(object); - bitmap.ClearBit(object); - EXPECT_TRUE(IsEmpty(bitmap)); +TEST_F(ObjectStartBitmapTest, SetBitClearBitImpliesEmpty) { + constexpr size_t object_num = 123; + AllocateObject(object_num); + FreeObject(object_num); + EXPECT_TRUE(IsEmpty()); } -TEST(ObjectStartBitmapTest, AdjacentObjectsAtBegin) { - ObjectStartBitmap bitmap(Object::kBaseOffset); - Object object0(0); - Object object1(1); - bitmap.SetBit(object0); - bitmap.SetBit(object1); - EXPECT_FALSE(bitmap.CheckBit(Object(3))); +TEST_F(ObjectStartBitmapTest, AdjacentObjectsAtBegin) { + AllocateObject(0); + AllocateObject(1); + EXPECT_FALSE(CheckObjectAllocated(3)); size_t count = 0; - bitmap.Iterate([&count, object0, object1](Address current) { + bitmap().Iterate([&count, this](Address current) { if (count == 0) { - EXPECT_EQ(object0.address(), current); + EXPECT_EQ(ObjectAddress(0), current); } else if (count == 1) { - EXPECT_EQ(object1.address(), current); + EXPECT_EQ(ObjectAddress(1), current); } count++; }); EXPECT_EQ(2u, count); } -TEST(ObjectStartBitmapTest, AdjacentObjectsAtEnd) { - ObjectStartBitmap bitmap(Object::kBaseOffset); - const size_t last_entry_index = ObjectStartBitmap::MaxEntries() - 1; - Object object0(last_entry_index - 1); - Object object1(last_entry_index); - bitmap.SetBit(object0); - bitmap.SetBit(object1); - EXPECT_FALSE(bitmap.CheckBit(Object(last_entry_index - 2))); +TEST_F(ObjectStartBitmapTest, AdjacentObjectsAtEnd) { + static constexpr size_t last_entry_index = + ObjectStartBitmap::MaxEntries() - 1; + AllocateObject(last_entry_index); + AllocateObject(last_entry_index - 1); + EXPECT_FALSE(CheckObjectAllocated(last_entry_index - 2)); size_t count = 0; - bitmap.Iterate([&count, object0, object1](Address current) { + bitmap().Iterate([&count, this](Address current) { if (count == 0) { - EXPECT_EQ(object0.address(), current); + EXPECT_EQ(ObjectAddress(last_entry_index - 1), current); } else if (count == 1) { - EXPECT_EQ(object1.address(), current); + EXPECT_EQ(ObjectAddress(last_entry_index), current); } count++; }); EXPECT_EQ(2u, count); } -TEST(ObjectStartBitmapTest, FindHeaderExact) { - ObjectStartBitmap bitmap(Object::kBaseOffset); - Object object(654); - bitmap.SetBit(object); - EXPECT_EQ(object.header(), bitmap.FindHeader(object.address())); +TEST_F(ObjectStartBitmapTest, FindHeaderExact) { + constexpr size_t object_num = 654; + AllocateObject(object_num); + EXPECT_EQ(ObjectHeader(object_num), + bitmap().FindHeader(ObjectAddress(object_num))); } -TEST(ObjectStartBitmapTest, FindHeaderApproximate) { +TEST_F(ObjectStartBitmapTest, FindHeaderApproximate) { static const size_t kInternalDelta = 37; - ObjectStartBitmap bitmap(Object::kBaseOffset); - Object object(654); - bitmap.SetBit(object); - EXPECT_EQ(object.header(), - bitmap.FindHeader(object.address() + kInternalDelta)); + constexpr size_t object_num = 654; + AllocateObject(object_num); + EXPECT_EQ(ObjectHeader(object_num), + bitmap().FindHeader(ObjectAddress(object_num) + kInternalDelta)); } -TEST(ObjectStartBitmapTest, FindHeaderIteratingWholeBitmap) { - ObjectStartBitmap bitmap(Object::kBaseOffset); - Object object_to_find(Object(0)); - Address hint_index = Object(ObjectStartBitmap::MaxEntries() - 1); - bitmap.SetBit(object_to_find); - EXPECT_EQ(object_to_find.header(), bitmap.FindHeader(hint_index)); +TEST_F(ObjectStartBitmapTest, FindHeaderIteratingWholeBitmap) { + AllocateObject(0); + Address hint_index = ObjectAddress(ObjectStartBitmap::MaxEntries() - 1); + EXPECT_EQ(ObjectHeader(0), bitmap().FindHeader(hint_index)); } -TEST(ObjectStartBitmapTest, FindHeaderNextCell) { +TEST_F(ObjectStartBitmapTest, FindHeaderNextCell) { // This white box test makes use of the fact that cells are of type uint8_t. const size_t kCellSize = sizeof(uint8_t); - ObjectStartBitmap bitmap(Object::kBaseOffset); - Object object_to_find(Object(kCellSize - 1)); - Address hint = Object(kCellSize); - bitmap.SetBit(Object(0)); - bitmap.SetBit(object_to_find); - EXPECT_EQ(object_to_find.header(), bitmap.FindHeader(hint)); + AllocateObject(0); + AllocateObject(kCellSize - 1); + Address hint = ObjectAddress(kCellSize); + EXPECT_EQ(ObjectHeader(kCellSize - 1), bitmap().FindHeader(hint)); } -TEST(ObjectStartBitmapTest, FindHeaderSameCell) { +TEST_F(ObjectStartBitmapTest, FindHeaderSameCell) { // This white box test makes use of the fact that cells are of type uint8_t. const size_t kCellSize = sizeof(uint8_t); - ObjectStartBitmap bitmap(Object::kBaseOffset); - Object object_to_find(Object(kCellSize - 1)); - bitmap.SetBit(Object(0)); - bitmap.SetBit(object_to_find); - EXPECT_EQ(object_to_find.header(), - bitmap.FindHeader(object_to_find.address())); + AllocateObject(0); + AllocateObject(kCellSize - 1); + Address hint = ObjectAddress(kCellSize); + EXPECT_EQ(ObjectHeader(kCellSize - 1), bitmap().FindHeader(hint)); + EXPECT_EQ(ObjectHeader(kCellSize - 1), + bitmap().FindHeader(ObjectAddress(kCellSize - 1))); } } // namespace internal |