Commit 5204c32a authored by Michael Lippautz's avatar Michael Lippautz Committed by Commit Bot

cppgc: Require object for cppgc::subtle::Resize()

Resize() is not similar to realloc() in that it allocates a new object
when passed a nullptr object.

Avoid corner cases around Resize(nullptr, size) where size may be
problematic if non-null by just requiring a valid object. The caller
can perform the necesary nullptr check.

Bug: chromium:1056170
Change-Id: Ic05972ae67c2968fc3eb002a6302b44e56b41ab4
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2752147Reviewed-by: 's avatarOmer Katz <omerkatz@chromium.org>
Commit-Queue: Michael Lippautz <mlippautz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#73336}
parent 943eb9e4
......@@ -61,11 +61,10 @@ void FreeUnreferencedObject(T* object) {
* on, and false otherwise.
*/
template <typename T>
bool Resize(T* object, AdditionalBytes additional_bytes) {
bool Resize(T& object, AdditionalBytes additional_bytes) {
static_assert(IsGarbageCollectedTypeV<T>,
"Object must be of type GarbageCollected.");
if (!object) return true;
return internal::Resize(object, sizeof(T) + additional_bytes.value);
return internal::Resize(&object, sizeof(T) + additional_bytes.value);
}
} // namespace subtle
......
......@@ -121,11 +121,11 @@ TEST_F(ExplicitManagementTest, GrowAtLAB) {
auto& header = HeapObjectHeader::FromPayload(o);
constexpr size_t size_of_o = sizeof(DynamicallySized);
constexpr size_t kFirstDelta = 8;
EXPECT_TRUE(subtle::Resize(o, AdditionalBytes(kFirstDelta)));
EXPECT_TRUE(subtle::Resize(*o, AdditionalBytes(kFirstDelta)));
EXPECT_EQ(RoundUp<kAllocationGranularity>(size_of_o + kFirstDelta),
header.ObjectSize());
constexpr size_t kSecondDelta = 9;
EXPECT_TRUE(subtle::Resize(o, AdditionalBytes(kSecondDelta)));
EXPECT_TRUE(subtle::Resize(*o, AdditionalBytes(kSecondDelta)));
EXPECT_EQ(RoundUp<kAllocationGranularity>(size_of_o + kSecondDelta),
header.ObjectSize());
// Second round didn't actually grow object because alignment restrictions
......@@ -133,7 +133,7 @@ TEST_F(ExplicitManagementTest, GrowAtLAB) {
EXPECT_EQ(RoundUp<kAllocationGranularity>(size_of_o + kFirstDelta),
RoundUp<kAllocationGranularity>(size_of_o + kSecondDelta));
constexpr size_t kThirdDelta = 16;
EXPECT_TRUE(subtle::Resize(o, AdditionalBytes(kThirdDelta)));
EXPECT_TRUE(subtle::Resize(*o, AdditionalBytes(kThirdDelta)));
EXPECT_EQ(RoundUp<kAllocationGranularity>(size_of_o + kThirdDelta),
header.ObjectSize());
}
......@@ -144,10 +144,10 @@ TEST_F(ExplicitManagementTest, GrowShrinkAtLAB) {
auto& header = HeapObjectHeader::FromPayload(o);
constexpr size_t size_of_o = sizeof(DynamicallySized);
constexpr size_t kDelta = 27;
EXPECT_TRUE(subtle::Resize(o, AdditionalBytes(kDelta)));
EXPECT_TRUE(subtle::Resize(*o, AdditionalBytes(kDelta)));
EXPECT_EQ(RoundUp<kAllocationGranularity>(size_of_o + kDelta),
header.ObjectSize());
EXPECT_TRUE(subtle::Resize(o, AdditionalBytes(0)));
EXPECT_TRUE(subtle::Resize(*o, AdditionalBytes(0)));
EXPECT_EQ(RoundUp<kAllocationGranularity>(size_of_o), header.ObjectSize());
}
......@@ -160,7 +160,7 @@ TEST_F(ExplicitManagementTest, ShrinkFreeList) {
ResetLinearAllocationBuffers();
auto& header = HeapObjectHeader::FromPayload(o);
constexpr size_t size_of_o = sizeof(DynamicallySized);
EXPECT_TRUE(subtle::Resize(o, AdditionalBytes(0)));
EXPECT_TRUE(subtle::Resize(*o, AdditionalBytes(0)));
EXPECT_EQ(RoundUp<kAllocationGranularity>(size_of_o), header.ObjectSize());
EXPECT_TRUE(space->free_list().ContainsForTesting(
{header.PayloadEnd(), ObjectAllocator::kSmallestSpaceSize}));
......@@ -175,7 +175,7 @@ TEST_F(ExplicitManagementTest, ShrinkFreeListBailoutAvoidFragmentation) {
ResetLinearAllocationBuffers();
auto& header = HeapObjectHeader::FromPayload(o);
constexpr size_t size_of_o = sizeof(DynamicallySized);
EXPECT_TRUE(subtle::Resize(o, AdditionalBytes(0)));
EXPECT_TRUE(subtle::Resize(*o, AdditionalBytes(0)));
EXPECT_EQ(RoundUp<kAllocationGranularity>(
size_of_o + ObjectAllocator::kSmallestSpaceSize - 1),
header.ObjectSize());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment