cppgc: Introduce ObjectSizeTrait

Users of padded objects must know the actual object size for
implementing custom finalizers.

Bug: chromium:1056170
Change-Id: I0ddf9066cfece0a8d18a9e6fd985d09449eea92a
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2644941
Commit-Queue: Michael Lippautz <mlippautz@chromium.org>
Reviewed-by: Omer Katz <omerkatz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#72269}
This commit is contained in:
Michael Lippautz 2021-01-22 20:59:37 +01:00 committed by Commit Bot
parent 03cea71144
commit 6d11bcda29
5 changed files with 146 additions and 0 deletions

View File

@ -4595,6 +4595,7 @@ v8_source_set("cppgc_base") {
"include/cppgc/macros.h",
"include/cppgc/member.h",
"include/cppgc/name-provider.h",
"include/cppgc/object-size-trait.h",
"include/cppgc/persistent.h",
"include/cppgc/platform.h",
"include/cppgc/prefinalizer.h",
@ -4650,6 +4651,7 @@ v8_source_set("cppgc_base") {
"src/heap/cppgc/name-trait.cc",
"src/heap/cppgc/object-allocator.cc",
"src/heap/cppgc/object-allocator.h",
"src/heap/cppgc/object-size-trait.cc",
"src/heap/cppgc/object-start-bitmap.h",
"src/heap/cppgc/page-memory.cc",
"src/heap/cppgc/page-memory.h",

View File

@ -0,0 +1,56 @@
// Copyright 2021 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.
#ifndef INCLUDE_CPPGC_OBJECT_SIZE_TRAIT_H_
#define INCLUDE_CPPGC_OBJECT_SIZE_TRAIT_H_
#include "cppgc/type-traits.h"
#include "v8config.h" // NOLINT(build/include_directory)
namespace cppgc {
namespace internal {
struct V8_EXPORT BaseObjectSizeTrait {
protected:
static size_t GetObjectSizeForGarbageCollected(const void*);
static size_t GetObjectSizeForGarbageCollectedMixin(const void*);
};
} // namespace internal
namespace subtle {
/**
* Trait specifying how to get the size of an object that was allocated using
* `MakeGarbageCollected()`. Also supports querying the size with an inner
* pointer to a mixin.
*/
template <typename T, bool = IsGarbageCollectedMixinTypeV<T>>
struct ObjectSizeTrait;
template <typename T>
struct ObjectSizeTrait<T, false> : cppgc::internal::BaseObjectSizeTrait {
static_assert(sizeof(T), "T must be fully defined");
static_assert(IsGarbageCollectedTypeV<T>,
"T must be of type GarbageCollected or GarbageCollectedMixin");
static size_t GetSize(const T& object) {
return GetObjectSizeForGarbageCollected(&object);
}
};
template <typename T>
struct ObjectSizeTrait<T, true> : cppgc::internal::BaseObjectSizeTrait {
static_assert(sizeof(T), "T must be fully defined");
static size_t GetSize(const T& object) {
return GetObjectSizeForGarbageCollectedMixin(&object);
}
};
} // namespace subtle
} // namespace cppgc
#endif // INCLUDE_CPPGC_OBJECT_SIZE_TRAIT_H_

View File

@ -0,0 +1,36 @@
// Copyright 2021 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 "include/cppgc/object-size-trait.h"
#include "src/heap/cppgc/heap-object-header.h"
#include "src/heap/cppgc/heap-page.h"
namespace cppgc {
namespace internal {
// static
size_t BaseObjectSizeTrait::GetObjectSizeForGarbageCollected(
const void* object) {
const auto& header = HeapObjectHeader::FromPayload(object);
return header.IsLargeObject()
? static_cast<const LargePage*>(BasePage::FromPayload(&header))
->PayloadSize()
: header.GetSize();
}
// static
size_t BaseObjectSizeTrait::GetObjectSizeForGarbageCollectedMixin(
const void* address) {
// `address` is guaranteed to be on a normal page because large object mixins
// are not supported.
const auto& header =
BasePage::FromPayload(address)
->ObjectHeaderFromInnerAddress<AccessMode::kAtomic>(address);
DCHECK(!header.IsLargeObject());
return header.GetSize();
}
} // namespace internal
} // namespace cppgc

View File

@ -106,6 +106,7 @@ v8_source_set("cppgc_unittests_sources") {
"heap/cppgc/metric-recorder-unittest.cc",
"heap/cppgc/minor-gc-unittest.cc",
"heap/cppgc/name-trait-unittest.cc",
"heap/cppgc/object-size-trait-unittest.cc",
"heap/cppgc/object-start-bitmap-unittest.cc",
"heap/cppgc/page-memory-unittest.cc",
"heap/cppgc/persistent-family-unittest.cc",

View File

@ -0,0 +1,51 @@
// 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 "include/cppgc/object-size-trait.h"
#include "include/cppgc/allocation.h"
#include "include/cppgc/garbage-collected.h"
#include "src/heap/cppgc/heap.h"
#include "test/unittests/heap/cppgc/tests.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace cppgc {
namespace internal {
namespace {
class ObjectSizeTraitTest : public testing::TestWithHeap {};
class GCed : public GarbageCollected<GCed> {
public:
void Trace(Visitor*) const {}
};
class NotGCed {};
class Mixin : public GarbageCollectedMixin {};
class UnmanagedMixinWithDouble {
protected:
virtual void ForceVTable() {}
};
class GCedWithMixin : public GarbageCollected<GCedWithMixin>,
public UnmanagedMixinWithDouble,
public Mixin {};
} // namespace
TEST_F(ObjectSizeTraitTest, GarbageCollected) {
auto* obj = cppgc::MakeGarbageCollected<GCed>(GetAllocationHandle());
EXPECT_GE(subtle::ObjectSizeTrait<GCed>::GetSize(*obj), sizeof(GCed));
}
TEST_F(ObjectSizeTraitTest, GarbageCollectedMixin) {
auto* obj = cppgc::MakeGarbageCollected<GCedWithMixin>(GetAllocationHandle());
Mixin& mixin = static_cast<Mixin&>(*obj);
EXPECT_NE(static_cast<void*>(&mixin), obj);
EXPECT_GE(subtle::ObjectSizeTrait<Mixin>::GetSize(mixin),
sizeof(GCedWithMixin));
}
} // namespace internal
} // namespace cppgc