v8/test/unittests/heap/cppgc/object-start-bitmap-unittest.cc
Omer Katz cc6dfd5d67 Fix CountPopulation non-builtin implementation
The existing non-builtin implementation is returning wrong results.
For example, given the value 63 as a uint8_t it returns 38 (should be 6).

The new implementation follows the naive algorithm presented in figure 5-1
in Hacker's Delight section 5-1.
Note that the algorithm in the book is designed for 32 bit numbers, so we
extended it to support 64 bit as well.

Bug: chromium:1056170
Change-Id: I8fed9c449f80b01b8cc93d339529c0e1e0863fc0
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2199345
Reviewed-by: Clemens Backes <clemensb@chromium.org>
Reviewed-by: Michael Lippautz <mlippautz@chromium.org>
Reviewed-by: Anton Bikineev <bikineev@chromium.org>
Commit-Queue: Omer Katz <omerkatz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#67801}
2020-05-14 14:24:13 +00:00

185 lines
5.5 KiB
C++

// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/heap/cppgc/object-start-bitmap.h"
#include "include/cppgc/allocation.h"
#include "src/base/macros.h"
#include "src/heap/cppgc/globals.h"
#include "src/heap/cppgc/heap-object-header-inl.h"
#include "src/heap/cppgc/heap-object-header.h"
#include "src/heap/cppgc/object-start-bitmap-inl.h"
#include "src/heap/cppgc/page-memory-inl.h"
#include "src/heap/cppgc/raw-heap.h"
#include "test/unittests/heap/cppgc/tests.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace cppgc {
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 {
public:
static Address kBaseOffset;
explicit Object(size_t number) : number_(number) {
const size_t max_entries = ObjectStartBitmap::MaxEntries();
EXPECT_GE(max_entries, number_);
}
Address address() const {
return kBaseOffset + ObjectStartBitmap::Granularity() * number_;
}
HeapObjectHeader* header() const {
return reinterpret_cast<HeapObjectHeader*>(address());
}
// Allow implicitly converting Object to Address.
operator Address() const { return address(); }
private:
const size_t number_;
};
Address Object::kBaseOffset = reinterpret_cast<Address>(0x4000);
} // namespace
TEST(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(ObjectStartBitmapTest, SetBitImpliesNonEmpty) {
ObjectStartBitmap bitmap(Object::kBaseOffset);
bitmap.SetBit(Object(0));
EXPECT_FALSE(IsEmpty(bitmap));
}
TEST(ObjectStartBitmapTest, SetBitCheckBit) {
ObjectStartBitmap bitmap(Object::kBaseOffset);
Object object(7);
bitmap.SetBit(object);
EXPECT_TRUE(bitmap.CheckBit(object));
}
TEST(ObjectStartBitmapTest, SetBitClearbitCheckBit) {
ObjectStartBitmap bitmap(Object::kBaseOffset);
Object object(77);
bitmap.SetBit(object);
bitmap.ClearBit(object);
EXPECT_FALSE(bitmap.CheckBit(object));
}
TEST(ObjectStartBitmapTest, SetBitClearBitImpliesEmpty) {
ObjectStartBitmap bitmap(Object::kBaseOffset);
Object object(123);
bitmap.SetBit(object);
bitmap.ClearBit(object);
EXPECT_TRUE(IsEmpty(bitmap));
}
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)));
size_t count = 0;
bitmap.Iterate([&count, object0, object1](Address current) {
if (count == 0) {
EXPECT_EQ(object0.address(), current);
} else if (count == 1) {
EXPECT_EQ(object1.address(), 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)));
size_t count = 0;
bitmap.Iterate([&count, object0, object1](Address current) {
if (count == 0) {
EXPECT_EQ(object0.address(), current);
} else if (count == 1) {
EXPECT_EQ(object1.address(), 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(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));
}
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(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));
}
TEST(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()));
}
} // namespace internal
} // namespace cppgc