| |
| |
| |
|
|
| #ifndef INCLUDE_CPPGC_ALLOCATION_H_ |
| #define INCLUDE_CPPGC_ALLOCATION_H_ |
|
|
| #include <atomic> |
| #include <cstddef> |
| #include <cstdint> |
| #include <new> |
| #include <type_traits> |
| #include <utility> |
|
|
| #include "cppgc/custom-space.h" |
| #include "cppgc/internal/api-constants.h" |
| #include "cppgc/internal/gc-info.h" |
| #include "cppgc/type-traits.h" |
| #include "v8config.h" |
|
|
| #if defined(__has_attribute) |
| #if __has_attribute(assume_aligned) |
| #define CPPGC_DEFAULT_ALIGNED \ |
| __attribute__((assume_aligned(api_constants::kDefaultAlignment))) |
| #define CPPGC_DOUBLE_WORD_ALIGNED \ |
| __attribute__((assume_aligned(2 * api_constants::kDefaultAlignment))) |
| #endif |
| #endif |
|
|
| #if !defined(CPPGC_DEFAULT_ALIGNED) |
| #define CPPGC_DEFAULT_ALIGNED |
| #endif |
|
|
| #if !defined(CPPGC_DOUBLE_WORD_ALIGNED) |
| #define CPPGC_DOUBLE_WORD_ALIGNED |
| #endif |
|
|
| namespace cppgc { |
|
|
| |
| |
| |
| class AllocationHandle; |
|
|
| namespace internal { |
|
|
| |
| enum class AlignVal : size_t {}; |
|
|
| class V8_EXPORT MakeGarbageCollectedTraitInternal { |
| protected: |
| static inline void MarkObjectAsFullyConstructed(const void* payload) { |
| |
| 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))); |
| |
| |
| |
| |
| uint16_t value = atomic_mutable_bitfield->load(std::memory_order_relaxed); |
| value |= api_constants::kFullyConstructedBitMask; |
| atomic_mutable_bitfield->store(value, std::memory_order_release); |
| } |
|
|
| |
| |
| |
| |
| template <typename GCInfoType, typename CustomSpace, size_t alignment> |
| struct AllocationDispatcher final { |
| static void* Invoke(AllocationHandle& handle, size_t size) { |
| static_assert(std::is_base_of<CustomSpaceBase, CustomSpace>::value, |
| "Custom space must inherit from CustomSpaceBase."); |
| static_assert( |
| !CustomSpace::kSupportsCompaction, |
| "Custom spaces that support compaction do not support allocating " |
| "objects with non-default (i.e. word-sized) alignment."); |
| return MakeGarbageCollectedTraitInternal::Allocate( |
| handle, size, static_cast<AlignVal>(alignment), |
| internal::GCInfoTrait<GCInfoType>::Index(), CustomSpace::kSpaceIndex); |
| } |
| }; |
|
|
| |
| |
| template <typename GCInfoType> |
| struct AllocationDispatcher<GCInfoType, void, |
| api_constants::kDefaultAlignment> |
| final { |
| static void* Invoke(AllocationHandle& handle, size_t size) { |
| return MakeGarbageCollectedTraitInternal::Allocate( |
| handle, size, internal::GCInfoTrait<GCInfoType>::Index()); |
| } |
| }; |
|
|
| |
| template <typename GCInfoType, size_t alignment> |
| struct AllocationDispatcher<GCInfoType, void, alignment> final { |
| static void* Invoke(AllocationHandle& handle, size_t size) { |
| return MakeGarbageCollectedTraitInternal::Allocate( |
| handle, size, static_cast<AlignVal>(alignment), |
| internal::GCInfoTrait<GCInfoType>::Index()); |
| } |
| }; |
|
|
| |
| template <typename GCInfoType, typename CustomSpace> |
| struct AllocationDispatcher<GCInfoType, CustomSpace, |
| api_constants::kDefaultAlignment> |
| final { |
| static void* Invoke(AllocationHandle& handle, size_t size) { |
| static_assert(std::is_base_of<CustomSpaceBase, CustomSpace>::value, |
| "Custom space must inherit from CustomSpaceBase."); |
| return MakeGarbageCollectedTraitInternal::Allocate( |
| handle, size, internal::GCInfoTrait<GCInfoType>::Index(), |
| CustomSpace::kSpaceIndex); |
| } |
| }; |
|
|
| private: |
| static void* CPPGC_DEFAULT_ALIGNED Allocate(cppgc::AllocationHandle&, size_t, |
| GCInfoIndex); |
| static void* CPPGC_DOUBLE_WORD_ALIGNED Allocate(cppgc::AllocationHandle&, |
| size_t, AlignVal, |
| GCInfoIndex); |
| static void* CPPGC_DEFAULT_ALIGNED Allocate(cppgc::AllocationHandle&, size_t, |
| GCInfoIndex, CustomSpaceIndex); |
| static void* CPPGC_DOUBLE_WORD_ALIGNED Allocate(cppgc::AllocationHandle&, |
| size_t, AlignVal, GCInfoIndex, |
| CustomSpaceIndex); |
|
|
| friend class HeapObjectHeader; |
| }; |
|
|
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| template <typename T> |
| class MakeGarbageCollectedTraitBase |
| : private internal::MakeGarbageCollectedTraitInternal { |
| private: |
| 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"); |
|
|
| protected: |
| |
| |
| |
| |
| |
| |
| |
| |
| V8_INLINE static void* Allocate(AllocationHandle& handle, size_t size) { |
| 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."); |
| static constexpr size_t kWantedAlignment = |
| alignof(T) < internal::api_constants::kDefaultAlignment |
| ? internal::api_constants::kDefaultAlignment |
| : alignof(T); |
| static_assert( |
| kWantedAlignment <= internal::api_constants::kMaxSupportedAlignment, |
| "Requested alignment larger than alignof(std::max_align_t) bytes. " |
| "Please file a bug to possibly get this restriction lifted."); |
| return AllocationDispatcher< |
| typename internal::GCInfoFolding< |
| T, typename T::ParentMostGarbageCollectedType>::ResultType, |
| typename SpaceTrait<T>::Space, kWantedAlignment>::Invoke(handle, size); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| V8_INLINE static void MarkObjectAsFullyConstructed(const void* payload) { |
| internal::MakeGarbageCollectedTraitInternal::MarkObjectAsFullyConstructed( |
| payload); |
| } |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| struct AdditionalBytes { |
| constexpr explicit AdditionalBytes(size_t bytes) : value(bytes) {} |
| const size_t value; |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| template <typename T> |
| class MakeGarbageCollectedTrait : public MakeGarbageCollectedTraitBase<T> { |
| public: |
| template <typename... Args> |
| static T* Call(AllocationHandle& handle, Args&&... args) { |
| void* memory = |
| MakeGarbageCollectedTraitBase<T>::Allocate(handle, sizeof(T)); |
| T* object = ::new (memory) T(std::forward<Args>(args)...); |
| MakeGarbageCollectedTraitBase<T>::MarkObjectAsFullyConstructed(object); |
| return object; |
| } |
|
|
| 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; |
| } |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| template <typename T, typename = void> |
| struct PostConstructionCallbackTrait { |
| static void Call(T*) {} |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| template <typename T, typename... Args> |
| V8_INLINE T* MakeGarbageCollected(AllocationHandle& handle, Args&&... args) { |
| T* object = |
| MakeGarbageCollectedTrait<T>::Call(handle, std::forward<Args>(args)...); |
| PostConstructionCallbackTrait<T>::Call(object); |
| return object; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| template <typename T, typename... Args> |
| V8_INLINE T* MakeGarbageCollected(AllocationHandle& handle, |
| AdditionalBytes additional_bytes, |
| Args&&... args) { |
| T* object = MakeGarbageCollectedTrait<T>::Call(handle, additional_bytes, |
| std::forward<Args>(args)...); |
| PostConstructionCallbackTrait<T>::Call(object); |
| return object; |
| } |
|
|
| } |
|
|
| #undef CPPGC_DEFAULT_ALIGNED |
| #undef CPPGC_DOUBLE_WORD_ALIGNED |
|
|
| #endif |
|
|