5#ifndef INCLUDE_CPPGC_ALLOCATION_H_
6#define INCLUDE_CPPGC_ALLOCATION_H_
21#if defined(__has_attribute)
22#if __has_attribute(assume_aligned)
23#define CPPGC_DEFAULT_ALIGNED \
24 __attribute__((assume_aligned(api_constants::kDefaultAlignment)))
25#define CPPGC_DOUBLE_WORD_ALIGNED \
26 __attribute__((assume_aligned(2 * api_constants::kDefaultAlignment)))
30#if !defined(CPPGC_DEFAULT_ALIGNED)
31#define CPPGC_DEFAULT_ALIGNED
34#if !defined(CPPGC_DOUBLE_WORD_ALIGNED)
35#define CPPGC_DOUBLE_WORD_ALIGNED
43class AllocationHandle;
54 std::atomic<uint16_t>* atomic_mutable_bitfield =
55 reinterpret_cast<std::atomic<uint16_t>*
>(
56 const_cast<uint16_t*
>(
reinterpret_cast<const uint16_t*
>(
57 reinterpret_cast<const uint8_t*
>(payload) -
58 api_constants::kFullyConstructedBitFieldOffsetFromPayload)));
63 uint16_t value = atomic_mutable_bitfield->load(std::memory_order_relaxed);
64 value |= api_constants::kFullyConstructedBitMask;
65 atomic_mutable_bitfield->store(value, std::memory_order_release);
72 template <
typename GCInfoType,
typename CustomSpace,
size_t alignment>
74 static void*
Invoke(AllocationHandle& handle,
size_t size) {
75 static_assert(std::is_base_of<CustomSpaceBase, CustomSpace>::value,
76 "Custom space must inherit from CustomSpaceBase.");
78 !CustomSpace::kSupportsCompaction,
79 "Custom spaces that support compaction do not support allocating "
80 "objects with non-default (i.e. word-sized) alignment.");
81 return MakeGarbageCollectedTraitInternal::Allocate(
82 handle, size,
static_cast<AlignVal>(alignment),
83 internal::GCInfoTrait<GCInfoType>::Index(), CustomSpace::kSpaceIndex);
89 template <
typename GCInfoType>
91 api_constants::kDefaultAlignment>
93 static void*
Invoke(AllocationHandle& handle,
size_t size) {
94 return MakeGarbageCollectedTraitInternal::Allocate(
95 handle, size, internal::GCInfoTrait<GCInfoType>::Index());
100 template <
typename GCInfoType,
size_t alignment>
102 static void*
Invoke(AllocationHandle& handle,
size_t size) {
103 return MakeGarbageCollectedTraitInternal::Allocate(
104 handle, size,
static_cast<AlignVal>(alignment),
105 internal::GCInfoTrait<GCInfoType>::Index());
110 template <
typename GCInfoType,
typename CustomSpace>
112 api_constants::kDefaultAlignment>
114 static void*
Invoke(AllocationHandle& handle,
size_t size) {
115 static_assert(std::is_base_of<CustomSpaceBase, CustomSpace>::value,
116 "Custom space must inherit from CustomSpaceBase.");
117 return MakeGarbageCollectedTraitInternal::Allocate(
118 handle, size, internal::GCInfoTrait<GCInfoType>::Index(),
119 CustomSpace::kSpaceIndex);
135 friend class HeapObjectHeader;
152 "T needs to be a garbage collected object");
153 static_assert(!IsGarbageCollectedWithMixinTypeV<T> ||
155 internal::api_constants::kLargeObjectSizeThreshold,
156 "GarbageCollectedMixin may not be a large object");
169 std::is_base_of<typename T::ParentMostGarbageCollectedType, T>::value,
170 "U of GarbageCollected<U> must be a base of T. Check "
171 "GarbageCollected<T> base class inheritance.");
172 static constexpr size_t kWantedAlignment =
173 alignof(T) < internal::api_constants::kDefaultAlignment
174 ? internal::api_constants::kDefaultAlignment
177 kWantedAlignment <= internal::api_constants::kMaxSupportedAlignment,
178 "Requested alignment larger than alignof(std::max_align_t) bytes. "
179 "Please file a bug to possibly get this restriction lifted.");
182 T,
typename T::ParentMostGarbageCollectedType>::ResultType,
238 template <
typename... Args>
239 static T*
Call(AllocationHandle& handle, Args&&... args) {
242 T*
object = ::new (memory) T(std::forward<Args>(args)...);
247 template <
typename... Args>
251 handle,
sizeof(T) + additional_bytes.
value);
252 T*
object = ::new (memory) T(std::forward<Args>(args)...);
264template <
typename T,
typename =
void>
277template <
typename T,
typename... Args>
295template <
typename T,
typename... Args>
300 std::forward<Args>(args)...);
307#undef CPPGC_DEFAULT_ALIGNED
308#undef CPPGC_DOUBLE_WORD_ALIGNED
#define CPPGC_DEFAULT_ALIGNED
#define CPPGC_DOUBLE_WORD_ALIGNED
static V8_INLINE void MarkObjectAsFullyConstructed(const void *payload)
static V8_INLINE void * Allocate(AllocationHandle &handle, size_t size)
static T * Call(AllocationHandle &handle, AdditionalBytes additional_bytes, Args &&... args)
static T * Call(AllocationHandle &handle, Args &&... args)
static void MarkObjectAsFullyConstructed(const void *payload)
V8_INLINE T * MakeGarbageCollected(AllocationHandle &handle, Args &&... args)
constexpr AdditionalBytes(size_t bytes)
static void * Invoke(AllocationHandle &handle, size_t size)
static void * Invoke(AllocationHandle &handle, size_t size)
static void * Invoke(AllocationHandle &handle, size_t size)
static void * Invoke(AllocationHandle &handle, size_t size)