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) { ...@@ -61,11 +61,10 @@ void FreeUnreferencedObject(T* object) {
* on, and false otherwise. * on, and false otherwise.
*/ */
template <typename T> template <typename T>
bool Resize(T* object, AdditionalBytes additional_bytes) { bool Resize(T& object, AdditionalBytes additional_bytes) {
static_assert(IsGarbageCollectedTypeV<T>, static_assert(IsGarbageCollectedTypeV<T>,
"Object must be of type GarbageCollected."); "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 } // namespace subtle
......
...@@ -121,11 +121,11 @@ TEST_F(ExplicitManagementTest, GrowAtLAB) { ...@@ -121,11 +121,11 @@ TEST_F(ExplicitManagementTest, GrowAtLAB) {
auto& header = HeapObjectHeader::FromPayload(o); auto& header = HeapObjectHeader::FromPayload(o);
constexpr size_t size_of_o = sizeof(DynamicallySized); constexpr size_t size_of_o = sizeof(DynamicallySized);
constexpr size_t kFirstDelta = 8; 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), EXPECT_EQ(RoundUp<kAllocationGranularity>(size_of_o + kFirstDelta),
header.ObjectSize()); header.ObjectSize());
constexpr size_t kSecondDelta = 9; 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), EXPECT_EQ(RoundUp<kAllocationGranularity>(size_of_o + kSecondDelta),
header.ObjectSize()); header.ObjectSize());
// Second round didn't actually grow object because alignment restrictions // Second round didn't actually grow object because alignment restrictions
...@@ -133,7 +133,7 @@ TEST_F(ExplicitManagementTest, GrowAtLAB) { ...@@ -133,7 +133,7 @@ TEST_F(ExplicitManagementTest, GrowAtLAB) {
EXPECT_EQ(RoundUp<kAllocationGranularity>(size_of_o + kFirstDelta), EXPECT_EQ(RoundUp<kAllocationGranularity>(size_of_o + kFirstDelta),
RoundUp<kAllocationGranularity>(size_of_o + kSecondDelta)); RoundUp<kAllocationGranularity>(size_of_o + kSecondDelta));
constexpr size_t kThirdDelta = 16; 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), EXPECT_EQ(RoundUp<kAllocationGranularity>(size_of_o + kThirdDelta),
header.ObjectSize()); header.ObjectSize());
} }
...@@ -144,10 +144,10 @@ TEST_F(ExplicitManagementTest, GrowShrinkAtLAB) { ...@@ -144,10 +144,10 @@ TEST_F(ExplicitManagementTest, GrowShrinkAtLAB) {
auto& header = HeapObjectHeader::FromPayload(o); auto& header = HeapObjectHeader::FromPayload(o);
constexpr size_t size_of_o = sizeof(DynamicallySized); constexpr size_t size_of_o = sizeof(DynamicallySized);
constexpr size_t kDelta = 27; 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), EXPECT_EQ(RoundUp<kAllocationGranularity>(size_of_o + kDelta),
header.ObjectSize()); 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()); EXPECT_EQ(RoundUp<kAllocationGranularity>(size_of_o), header.ObjectSize());
} }
...@@ -160,7 +160,7 @@ TEST_F(ExplicitManagementTest, ShrinkFreeList) { ...@@ -160,7 +160,7 @@ TEST_F(ExplicitManagementTest, ShrinkFreeList) {
ResetLinearAllocationBuffers(); ResetLinearAllocationBuffers();
auto& header = HeapObjectHeader::FromPayload(o); auto& header = HeapObjectHeader::FromPayload(o);
constexpr size_t size_of_o = sizeof(DynamicallySized); 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_EQ(RoundUp<kAllocationGranularity>(size_of_o), header.ObjectSize());
EXPECT_TRUE(space->free_list().ContainsForTesting( EXPECT_TRUE(space->free_list().ContainsForTesting(
{header.PayloadEnd(), ObjectAllocator::kSmallestSpaceSize})); {header.PayloadEnd(), ObjectAllocator::kSmallestSpaceSize}));
...@@ -175,7 +175,7 @@ TEST_F(ExplicitManagementTest, ShrinkFreeListBailoutAvoidFragmentation) { ...@@ -175,7 +175,7 @@ TEST_F(ExplicitManagementTest, ShrinkFreeListBailoutAvoidFragmentation) {
ResetLinearAllocationBuffers(); ResetLinearAllocationBuffers();
auto& header = HeapObjectHeader::FromPayload(o); auto& header = HeapObjectHeader::FromPayload(o);
constexpr size_t size_of_o = sizeof(DynamicallySized); 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>( EXPECT_EQ(RoundUp<kAllocationGranularity>(
size_of_o + ObjectAllocator::kSmallestSpaceSize - 1), size_of_o + ObjectAllocator::kSmallestSpaceSize - 1),
header.ObjectSize()); 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