2020-03-20 14:44:46 +00:00
|
|
|
// 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.
|
|
|
|
|
2020-04-07 21:54:43 +00:00
|
|
|
#ifndef INCLUDE_CPPGC_INTERNAL_FINALIZER_TRAIT_H_
|
|
|
|
#define INCLUDE_CPPGC_INTERNAL_FINALIZER_TRAIT_H_
|
2020-03-20 14:44:46 +00:00
|
|
|
|
|
|
|
#include <type_traits>
|
|
|
|
|
2020-04-20 18:19:29 +00:00
|
|
|
#include "cppgc/type-traits.h"
|
2020-03-27 10:02:58 +00:00
|
|
|
|
2020-03-20 14:44:46 +00:00
|
|
|
namespace cppgc {
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
using FinalizationCallback = void (*)(void*);
|
|
|
|
|
|
|
|
template <typename T, typename = void>
|
|
|
|
struct HasFinalizeGarbageCollectedObject : std::false_type {};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct HasFinalizeGarbageCollectedObject<
|
2022-04-05 16:22:50 +00:00
|
|
|
T,
|
|
|
|
std::void_t<decltype(std::declval<T>().FinalizeGarbageCollectedObject())>>
|
2020-03-20 14:44:46 +00:00
|
|
|
: std::true_type {};
|
|
|
|
|
|
|
|
// The FinalizerTraitImpl specifies how to finalize objects.
|
|
|
|
template <typename T, bool isFinalized>
|
|
|
|
struct FinalizerTraitImpl;
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct FinalizerTraitImpl<T, true> {
|
|
|
|
private:
|
|
|
|
// Dispatch to custom FinalizeGarbageCollectedObject().
|
|
|
|
struct Custom {
|
|
|
|
static void Call(void* obj) {
|
|
|
|
static_cast<T*>(obj)->FinalizeGarbageCollectedObject();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Dispatch to regular destructor.
|
|
|
|
struct Destructor {
|
|
|
|
static void Call(void* obj) { static_cast<T*>(obj)->~T(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
using FinalizeImpl =
|
|
|
|
std::conditional_t<HasFinalizeGarbageCollectedObject<T>::value, Custom,
|
|
|
|
Destructor>;
|
|
|
|
|
|
|
|
public:
|
|
|
|
static void Finalize(void* obj) {
|
|
|
|
static_assert(sizeof(T), "T must be fully defined");
|
|
|
|
FinalizeImpl::Call(obj);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct FinalizerTraitImpl<T, false> {
|
|
|
|
static void Finalize(void* obj) {
|
|
|
|
static_assert(sizeof(T), "T must be fully defined");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// The FinalizerTrait is used to determine if a type requires finalization and
|
|
|
|
// what finalization means.
|
|
|
|
template <typename T>
|
|
|
|
struct FinalizerTrait {
|
|
|
|
private:
|
|
|
|
// Object has a finalizer if it has
|
|
|
|
// - a custom FinalizeGarbageCollectedObject method, or
|
|
|
|
// - a destructor.
|
|
|
|
static constexpr bool kNonTrivialFinalizer =
|
|
|
|
internal::HasFinalizeGarbageCollectedObject<T>::value ||
|
|
|
|
!std::is_trivially_destructible<typename std::remove_cv<T>::type>::value;
|
|
|
|
|
|
|
|
static void Finalize(void* obj) {
|
|
|
|
internal::FinalizerTraitImpl<T, kNonTrivialFinalizer>::Finalize(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2021-08-16 07:27:58 +00:00
|
|
|
static constexpr bool HasFinalizer() { return kNonTrivialFinalizer; }
|
cppgc: Optimize GCInfo setup
In Blink's version of Oilpan, GCInfo objects would reside in .bss and
a table would translate between an index and the .bss address. Upon
retrieving a GCInfoIndex, the slow path merely passes a .bss pointer
to a slow path setup method to create the table mapping.
In cppgc, we set up GCInfo entries directly in the table. This is
slightly faster for actually using GCInfo objects as there's no
indirection between table and .bss, and it also saves one pointer (the
indirection) per type that is set up. The downside of this approach is
that individual components of a GCInfo objects, that are all
type-dependent, need to be passed to the conditional setup method.
Since GCInfo indices must be retrieved on each allocation, this
pollutes the fast path with additional instructions.
However, GCInfo components are actually known at compile-time for many
objects. In such cases, we can use a compile-time static dispatch to
encode the known parameters in different functions. This saves around
40KiB of memory on ChromePublic.apk and also creates a more compact
fast path for allocation.
Bug: chromium:1238884, chromium:1056170
Change-Id: Iedd809a8baefcc02f131d2b2c77d341b0abe43bb
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3094007
Reviewed-by: Anton Bikineev <bikineev@chromium.org>
Reviewed-by: Omer Katz <omerkatz@chromium.org>
Commit-Queue: Michael Lippautz <mlippautz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#76291}
2021-08-13 08:01:26 +00:00
|
|
|
|
2020-03-20 14:44:46 +00:00
|
|
|
// The callback used to finalize an object of type T.
|
|
|
|
static constexpr FinalizationCallback kCallback =
|
|
|
|
kNonTrivialFinalizer ? Finalize : nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
constexpr FinalizationCallback FinalizerTrait<T>::kCallback;
|
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace cppgc
|
|
|
|
|
2020-04-07 21:54:43 +00:00
|
|
|
#endif // INCLUDE_CPPGC_INTERNAL_FINALIZER_TRAIT_H_
|