| | |
| | |
| | |
| |
|
| | #ifndef INCLUDE_CPPGC_ALLOCATION_H_ |
| | #define INCLUDE_CPPGC_ALLOCATION_H_ |
| |
|
| | #include <stdint.h> |
| |
|
| | #include <atomic> |
| |
|
| | #include "cppgc/custom-space.h" |
| | #include "cppgc/garbage-collected.h" |
| | #include "cppgc/heap.h" |
| | #include "cppgc/internal/api-constants.h" |
| | #include "cppgc/internal/gc-info.h" |
| |
|
| | namespace cppgc { |
| |
|
| | template <typename T> |
| | class MakeGarbageCollectedTraitBase; |
| |
|
| | namespace internal { |
| |
|
| | 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 = value | api_constants::kFullyConstructedBitMask; |
| | atomic_mutable_bitfield->store(value, std::memory_order_release); |
| | } |
| |
|
| | static void* Allocate(cppgc::Heap* heap, size_t size, GCInfoIndex index); |
| | static void* Allocate(cppgc::Heap* heapx, size_t size, GCInfoIndex index, |
| | CustomSpaceIndex space_inde); |
| |
|
| | friend class HeapObjectHeader; |
| | }; |
| |
|
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | template <typename T> |
| | class MakeGarbageCollectedTraitBase |
| | : private internal::MakeGarbageCollectedTraitInternal { |
| | private: |
| | template <typename U, typename CustomSpace> |
| | struct SpacePolicy { |
| | static void* Allocate(Heap* heap, size_t size) { |
| | |
| | static_assert(std::is_base_of<CustomSpaceBase, CustomSpace>::value, |
| | "Custom space must inherit from CustomSpaceBase."); |
| | return internal::MakeGarbageCollectedTraitInternal::Allocate( |
| | heap, size, internal::GCInfoTrait<T>::Index(), |
| | CustomSpace::kSpaceIndex); |
| | } |
| | }; |
| |
|
| | template <typename U> |
| | struct SpacePolicy<U, void> { |
| | static void* Allocate(Heap* heap, size_t size) { |
| | |
| | return internal::MakeGarbageCollectedTraitInternal::Allocate( |
| | heap, size, internal::GCInfoTrait<T>::Index()); |
| | } |
| | }; |
| |
|
| | protected: |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | static void* Allocate(Heap* heap, size_t size) { |
| | return SpacePolicy<T, typename SpaceTrait<T>::Space>::Allocate(heap, size); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | static void MarkObjectAsFullyConstructed(const void* payload) { |
| | internal::MakeGarbageCollectedTraitInternal::MarkObjectAsFullyConstructed( |
| | payload); |
| | } |
| | }; |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | template <typename T> |
| | class MakeGarbageCollectedTrait : public MakeGarbageCollectedTraitBase<T> { |
| | public: |
| | template <typename... Args> |
| | static T* Call(Heap* heap, Args&&... args) { |
| | static_assert(internal::IsGarbageCollectedType<T>::value, |
| | "T needs to be a garbage collected object"); |
| | static_assert( |
| | !internal::IsGarbageCollectedMixinType<T>::value || |
| | sizeof(T) <= internal::api_constants::kLargeObjectSizeThreshold, |
| | "GarbageCollectedMixin may not be a large object"); |
| | void* memory = MakeGarbageCollectedTraitBase<T>::Allocate(heap, sizeof(T)); |
| | 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> |
| | T* MakeGarbageCollected(Heap* heap, Args&&... args) { |
| | T* object = |
| | MakeGarbageCollectedTrait<T>::Call(heap, std::forward<Args>(args)...); |
| | PostConstructionCallbackTrait<T>::Call(object); |
| | return object; |
| | } |
| |
|
| | } |
| |
|
| | #endif |
| |
|