2020-03-25 15:59:13 +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.
|
|
|
|
|
|
|
|
#ifndef INCLUDE_CPPGC_ALLOCATION_H_
|
|
|
|
#define INCLUDE_CPPGC_ALLOCATION_H_
|
|
|
|
|
|
|
|
#include <atomic>
|
2021-05-20 09:36:52 +00:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <new>
|
2021-04-07 18:45:22 +00:00
|
|
|
#include <type_traits>
|
2021-08-09 12:26:55 +00:00
|
|
|
#include <utility>
|
2020-03-25 15:59:13 +00:00
|
|
|
|
2020-05-07 20:36:03 +00:00
|
|
|
#include "cppgc/custom-space.h"
|
2020-04-20 18:19:29 +00:00
|
|
|
#include "cppgc/internal/api-constants.h"
|
|
|
|
#include "cppgc/internal/gc-info.h"
|
2021-05-20 09:36:52 +00:00
|
|
|
#include "cppgc/type-traits.h"
|
|
|
|
#include "v8config.h" // NOLINT(build/include_directory)
|
2020-03-25 15:59:13 +00:00
|
|
|
|
|
|
|
namespace cppgc {
|
2020-03-27 10:02:58 +00:00
|
|
|
|
2020-06-10 22:28:41 +00:00
|
|
|
/**
|
|
|
|
* AllocationHandle is used to allocate garbage-collected objects.
|
|
|
|
*/
|
|
|
|
class AllocationHandle;
|
|
|
|
|
2020-03-25 15:59:13 +00:00
|
|
|
namespace internal {
|
|
|
|
|
2020-03-27 10:02:58 +00:00
|
|
|
class V8_EXPORT MakeGarbageCollectedTraitInternal {
|
|
|
|
protected:
|
|
|
|
static inline void MarkObjectAsFullyConstructed(const void* payload) {
|
|
|
|
// See api_constants for an explanation of the constants.
|
|
|
|
std::atomic<uint16_t>* atomic_mutable_bitfield =
|
|
|
|
reinterpret_cast<std::atomic<uint16_t>*>(
|
|
|
|
const_cast<uint16_t*>(reinterpret_cast<const uint16_t*>(
|
|
|
|
reinterpret_cast<const uint8_t*>(payload) -
|
|
|
|
api_constants::kFullyConstructedBitFieldOffsetFromPayload)));
|
2021-08-12 12:37:25 +00:00
|
|
|
// It's safe to split use load+store here (instead of a read-modify-write
|
|
|
|
// operation), since it's guaranteed that this 16-bit bitfield is only
|
|
|
|
// modified by a single thread. This is cheaper in terms of code bloat (on
|
|
|
|
// ARM) and performance.
|
|
|
|
uint16_t value = atomic_mutable_bitfield->load(std::memory_order_relaxed);
|
|
|
|
value |= api_constants::kFullyConstructedBitMask;
|
|
|
|
atomic_mutable_bitfield->store(value, std::memory_order_release);
|
2020-03-27 10:02:58 +00:00
|
|
|
}
|
|
|
|
|
2021-03-17 19:49:57 +00:00
|
|
|
template <typename U, typename CustomSpace>
|
|
|
|
struct SpacePolicy {
|
|
|
|
static void* Allocate(AllocationHandle& handle, size_t size) {
|
|
|
|
// Custom space.
|
|
|
|
static_assert(std::is_base_of<CustomSpaceBase, CustomSpace>::value,
|
|
|
|
"Custom space must inherit from CustomSpaceBase.");
|
|
|
|
return MakeGarbageCollectedTraitInternal::Allocate(
|
|
|
|
handle, size, internal::GCInfoTrait<U>::Index(),
|
|
|
|
CustomSpace::kSpaceIndex);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename U>
|
|
|
|
struct SpacePolicy<U, void> {
|
|
|
|
static void* Allocate(AllocationHandle& handle, size_t size) {
|
|
|
|
// Default space.
|
|
|
|
return MakeGarbageCollectedTraitInternal::Allocate(
|
|
|
|
handle, size, internal::GCInfoTrait<U>::Index());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
2020-06-22 08:42:56 +00:00
|
|
|
static void* Allocate(cppgc::AllocationHandle& handle, size_t size,
|
|
|
|
GCInfoIndex index);
|
|
|
|
static void* Allocate(cppgc::AllocationHandle& handle, size_t size,
|
|
|
|
GCInfoIndex index, CustomSpaceIndex space_index);
|
2020-03-27 10:02:58 +00:00
|
|
|
|
|
|
|
friend class HeapObjectHeader;
|
|
|
|
};
|
2020-03-25 15:59:13 +00:00
|
|
|
|
|
|
|
} // namespace internal
|
2020-03-27 10:02:58 +00:00
|
|
|
|
2020-04-14 12:02:39 +00:00
|
|
|
/**
|
|
|
|
* Base trait that provides utilities for advancers users that have custom
|
|
|
|
* allocation needs (e.g., overriding size). It's expected that users override
|
|
|
|
* MakeGarbageCollectedTrait (see below) and inherit from
|
|
|
|
* MakeGarbageCollectedTraitBase and make use of the low-level primitives
|
|
|
|
* offered to allocate and construct an object.
|
|
|
|
*/
|
2020-03-27 10:02:58 +00:00
|
|
|
template <typename T>
|
|
|
|
class MakeGarbageCollectedTraitBase
|
|
|
|
: private internal::MakeGarbageCollectedTraitInternal {
|
2020-05-07 20:36:03 +00:00
|
|
|
private:
|
2021-01-25 11:24:26 +00:00
|
|
|
static_assert(internal::IsGarbageCollectedType<T>::value,
|
|
|
|
"T needs to be a garbage collected object");
|
|
|
|
static_assert(!IsGarbageCollectedWithMixinTypeV<T> ||
|
|
|
|
sizeof(T) <=
|
|
|
|
internal::api_constants::kLargeObjectSizeThreshold,
|
|
|
|
"GarbageCollectedMixin may not be a large object");
|
|
|
|
|
2020-03-27 10:02:58 +00:00
|
|
|
protected:
|
2020-04-14 12:02:39 +00:00
|
|
|
/**
|
|
|
|
* Allocates memory for an object of type T.
|
|
|
|
*
|
2020-06-10 22:28:41 +00:00
|
|
|
* \param handle AllocationHandle identifying the heap to allocate the object
|
|
|
|
* on.
|
2020-04-14 12:02:39 +00:00
|
|
|
* \param size The size that should be reserved for the object.
|
|
|
|
* \returns the memory to construct an object of type T on.
|
|
|
|
*/
|
2021-03-17 19:49:57 +00:00
|
|
|
V8_INLINE static void* Allocate(AllocationHandle& handle, size_t size) {
|
2021-04-07 18:45:22 +00:00
|
|
|
static_assert(
|
|
|
|
std::is_base_of<typename T::ParentMostGarbageCollectedType, T>::value,
|
|
|
|
"U of GarbageCollected<U> must be a base of T. Check "
|
|
|
|
"GarbageCollected<T> base class inheritance.");
|
2021-03-11 09:31:57 +00:00
|
|
|
return SpacePolicy<
|
|
|
|
typename internal::GCInfoFolding<
|
|
|
|
T, typename T::ParentMostGarbageCollectedType>::ResultType,
|
|
|
|
typename SpaceTrait<T>::Space>::Allocate(handle, size);
|
2020-03-27 10:02:58 +00:00
|
|
|
}
|
|
|
|
|
2020-04-14 12:02:39 +00:00
|
|
|
/**
|
|
|
|
* Marks an object as fully constructed, resulting in precise handling by the
|
|
|
|
* garbage collector.
|
|
|
|
*
|
|
|
|
* \param payload The base pointer the object is allocated at.
|
|
|
|
*/
|
2021-03-17 19:49:57 +00:00
|
|
|
V8_INLINE static void MarkObjectAsFullyConstructed(const void* payload) {
|
2020-03-27 10:02:58 +00:00
|
|
|
internal::MakeGarbageCollectedTraitInternal::MarkObjectAsFullyConstructed(
|
|
|
|
payload);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-10-20 22:22:52 +00:00
|
|
|
/**
|
2020-11-11 16:58:24 +00:00
|
|
|
* Passed to MakeGarbageCollected to specify how many bytes should be appended
|
|
|
|
* to the allocated object.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* \code
|
|
|
|
* class InlinedArray final : public GarbageCollected<InlinedArray> {
|
|
|
|
* public:
|
|
|
|
* explicit InlinedArray(size_t bytes) : size(bytes), byte_array(this + 1) {}
|
|
|
|
* void Trace(Visitor*) const {}
|
|
|
|
|
|
|
|
* size_t size;
|
|
|
|
* char* byte_array;
|
|
|
|
* };
|
|
|
|
*
|
|
|
|
* auto* inlined_array = MakeGarbageCollected<InlinedArray(
|
|
|
|
* GetAllocationHandle(), AdditionalBytes(4), 4);
|
|
|
|
* for (size_t i = 0; i < 4; i++) {
|
|
|
|
* Process(inlined_array->byte_array[i]);
|
|
|
|
* }
|
|
|
|
* \endcode
|
2020-10-20 22:22:52 +00:00
|
|
|
*/
|
|
|
|
struct AdditionalBytes {
|
2020-11-11 16:58:24 +00:00
|
|
|
constexpr explicit AdditionalBytes(size_t bytes) : value(bytes) {}
|
2020-10-20 22:22:52 +00:00
|
|
|
const size_t value;
|
|
|
|
};
|
|
|
|
|
2020-04-14 12:02:39 +00:00
|
|
|
/**
|
|
|
|
* Default trait class that specifies how to construct an object of type T.
|
|
|
|
* Advanced users may override how an object is constructed using the utilities
|
|
|
|
* that are provided through MakeGarbageCollectedTraitBase.
|
|
|
|
*
|
|
|
|
* Any trait overriding construction must
|
2020-10-19 08:12:55 +00:00
|
|
|
* - allocate through `MakeGarbageCollectedTraitBase<T>::Allocate`;
|
2020-04-14 12:02:39 +00:00
|
|
|
* - mark the object as fully constructed using
|
2020-10-19 08:12:55 +00:00
|
|
|
* `MakeGarbageCollectedTraitBase<T>::MarkObjectAsFullyConstructed`;
|
2020-04-14 12:02:39 +00:00
|
|
|
*/
|
2020-03-27 10:02:58 +00:00
|
|
|
template <typename T>
|
|
|
|
class MakeGarbageCollectedTrait : public MakeGarbageCollectedTraitBase<T> {
|
|
|
|
public:
|
|
|
|
template <typename... Args>
|
2020-06-22 08:42:56 +00:00
|
|
|
static T* Call(AllocationHandle& handle, Args&&... args) {
|
2020-06-10 22:28:41 +00:00
|
|
|
void* memory =
|
|
|
|
MakeGarbageCollectedTraitBase<T>::Allocate(handle, sizeof(T));
|
2020-03-27 10:02:58 +00:00
|
|
|
T* object = ::new (memory) T(std::forward<Args>(args)...);
|
|
|
|
MakeGarbageCollectedTraitBase<T>::MarkObjectAsFullyConstructed(object);
|
|
|
|
return object;
|
|
|
|
}
|
2020-10-20 22:22:52 +00:00
|
|
|
|
|
|
|
template <typename... Args>
|
|
|
|
static T* Call(AllocationHandle& handle, AdditionalBytes additional_bytes,
|
|
|
|
Args&&... args) {
|
|
|
|
void* memory = MakeGarbageCollectedTraitBase<T>::Allocate(
|
|
|
|
handle, sizeof(T) + additional_bytes.value);
|
|
|
|
T* object = ::new (memory) T(std::forward<Args>(args)...);
|
|
|
|
MakeGarbageCollectedTraitBase<T>::MarkObjectAsFullyConstructed(object);
|
|
|
|
return object;
|
|
|
|
}
|
2020-03-27 10:02:58 +00:00
|
|
|
};
|
|
|
|
|
2020-05-05 07:12:03 +00:00
|
|
|
/**
|
|
|
|
* Allows users to specify a post-construction callback for specific types. The
|
|
|
|
* callback is invoked on the instance of type T right after it has been
|
|
|
|
* constructed. This can be useful when the callback requires a
|
|
|
|
* fully-constructed object to be able to dispatch to virtual methods.
|
|
|
|
*/
|
|
|
|
template <typename T, typename = void>
|
|
|
|
struct PostConstructionCallbackTrait {
|
|
|
|
static void Call(T*) {}
|
|
|
|
};
|
|
|
|
|
2020-04-14 12:02:39 +00:00
|
|
|
/**
|
|
|
|
* Constructs a managed object of type T where T transitively inherits from
|
|
|
|
* GarbageCollected.
|
|
|
|
*
|
|
|
|
* \param args List of arguments with which an instance of T will be
|
|
|
|
* constructed.
|
|
|
|
* \returns an instance of type T.
|
|
|
|
*/
|
2020-03-27 10:02:58 +00:00
|
|
|
template <typename T, typename... Args>
|
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
|
|
|
V8_INLINE T* MakeGarbageCollected(AllocationHandle& handle, Args&&... args) {
|
2020-05-05 07:12:03 +00:00
|
|
|
T* object =
|
2020-06-10 22:28:41 +00:00
|
|
|
MakeGarbageCollectedTrait<T>::Call(handle, std::forward<Args>(args)...);
|
2020-05-05 07:12:03 +00:00
|
|
|
PostConstructionCallbackTrait<T>::Call(object);
|
|
|
|
return object;
|
2020-03-27 10:02:58 +00:00
|
|
|
}
|
|
|
|
|
2020-10-20 22:22:52 +00:00
|
|
|
/**
|
|
|
|
* Constructs a managed object of type T where T transitively inherits from
|
|
|
|
* GarbageCollected. Created objects will have additional bytes appended to
|
|
|
|
* it. Allocated memory would suffice for `sizeof(T) + additional_bytes`.
|
|
|
|
*
|
|
|
|
* \param additional_bytes Denotes how many bytes to append to T.
|
|
|
|
* \param args List of arguments with which an instance of T will be
|
|
|
|
* constructed.
|
|
|
|
* \returns an instance of type T.
|
|
|
|
*/
|
|
|
|
template <typename T, typename... Args>
|
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
|
|
|
V8_INLINE T* MakeGarbageCollected(AllocationHandle& handle,
|
|
|
|
AdditionalBytes additional_bytes,
|
|
|
|
Args&&... args) {
|
2020-10-20 22:22:52 +00:00
|
|
|
T* object = MakeGarbageCollectedTrait<T>::Call(handle, additional_bytes,
|
|
|
|
std::forward<Args>(args)...);
|
|
|
|
PostConstructionCallbackTrait<T>::Call(object);
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
2020-03-25 15:59:13 +00:00
|
|
|
} // namespace cppgc
|
|
|
|
|
|
|
|
#endif // INCLUDE_CPPGC_ALLOCATION_H_
|