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;
57 std::atomic_ref<uint16_t> atomic_mutable_bitfield(
58 *
const_cast<uint16_t*
>(
reinterpret_cast<const uint16_t*
>(
59 reinterpret_cast<const uint8_t*
>(payload) -
60 api_constants::kFullyConstructedBitFieldOffsetFromPayload)));
65 uint16_t value = atomic_mutable_bitfield.load(std::memory_order_relaxed);
66 value |= api_constants::kFullyConstructedBitMask;
67 atomic_mutable_bitfield.store(value, std::memory_order_release);
74 template <
typename GCInfoType,
typename CustomSpace,
size_t alignment,
77 static void*
Invoke(AllocationHandle& handle,
size_t size) {
78 static_assert(std::is_base_of_v<CustomSpaceBase, CustomSpace>,
79 "Custom space must inherit from CustomSpaceBase.");
82 "Custom spaces that support compaction do not support allocating "
83 "objects with non-default (i.e. word-sized) alignment.");
85 return MakeGarbageCollectedTraitInternal::Allocate(
86 handle, size,
static_cast<AlignVal>(alignment),
88 CustomSpace::kSpaceIndex, kMixinTag);
90 return MakeGarbageCollectedTraitInternal::Allocate(
91 handle, size,
static_cast<AlignVal>(alignment),
93 CustomSpace::kSpaceIndex);
100 template <
typename GCInfoType, CanContainMixins contain_mixins>
102 api_constants::kDefaultAlignment, contain_mixins>
104 static void*
Invoke(AllocationHandle& handle,
size_t size) {
106 return MakeGarbageCollectedTraitInternal::Allocate(
110 return MakeGarbageCollectedTraitInternal::Allocate(
117 template <
typename GCInfoType,
size_t alignment,
121 static void*
Invoke(AllocationHandle& handle,
size_t size) {
123 return MakeGarbageCollectedTraitInternal::Allocate(
124 handle, size,
static_cast<AlignVal>(alignment),
127 return MakeGarbageCollectedTraitInternal::Allocate(
128 handle, size,
static_cast<AlignVal>(alignment),
135 template <
typename GCInfoType,
typename CustomSpace,
138 api_constants::kDefaultAlignment, contain_mixins>
140 static void*
Invoke(AllocationHandle& handle,
size_t size) {
141 static_assert(std::is_base_of_v<CustomSpaceBase, CustomSpace>,
142 "Custom space must inherit from CustomSpaceBase.");
144 return MakeGarbageCollectedTraitInternal::Allocate(
146 CustomSpace::kSpaceIndex, kMixinTag);
148 return MakeGarbageCollectedTraitInternal::Allocate(
150 CustomSpace::kSpaceIndex);
156 inline const static struct MixinTag {
160 Allocate(cppgc::AllocationHandle&,
size_t,
GCInfoIndex);
169 Allocate(cppgc::AllocationHandle&,
size_t,
GCInfoIndex, MixinTag);
196 "T needs to be a garbage collected object");
197 static_assert(!IsGarbageCollectedWithMixinTypeV<T> ||
199 internal::api_constants::kLargeObjectSizeThreshold,
200 "GarbageCollectedMixin may not be a large object");
213 std::is_base_of_v<typename T::ParentMostGarbageCollectedType, T>,
214 "U of GarbageCollected<U> must be a base of T. Check "
215 "GarbageCollected<T> base class inheritance.");
216 static constexpr size_t kWantedAlignment =
217 alignof(T) < internal::api_constants::kDefaultAlignment
218 ? internal::api_constants::kDefaultAlignment
221 std::is_base_of_v<GarbageCollectedMixin, T> ? CanContainMixins::kYes
222 : CanContainMixins::kNo;
224 kWantedAlignment <= internal::api_constants::kMaxSupportedAlignment,
225 "Requested alignment larger than alignof(std::max_align_t) bytes. "
226 "Please file a bug to possibly get this restriction lifted.");
229 T,
typename T::ParentMostGarbageCollectedType>::ResultType,
231 kMayContainMixins>::Invoke(handle, size);
286 template <
typename... Args>
287 static T*
Call(AllocationHandle& handle, Args&&... args) {
290 T*
object = ::new (memory) T(std::forward<Args>(args)...);
295 template <
typename... Args>
299 handle,
sizeof(T) + additional_bytes.
value);
300 T*
object = ::new (memory) T(std::forward<Args>(args)...);
312template <
typename T,
typename =
void>
325template <
typename T,
typename... Args>
343template <
typename T,
typename... Args>
348 std::forward<Args>(args)...);
355#undef CPPGC_DEFAULT_ALIGNED
356#undef CPPGC_DOUBLE_WORD_ALIGNED
#define CPPGC_DEFAULT_ALIGNED
Definition: allocation.h:31
#define CPPGC_DOUBLE_WORD_ALIGNED
Definition: allocation.h:35
Definition: custom-space.h:49
static constexpr bool kSupportsCompaction
Definition: custom-space.h:55
Definition: garbage-collected.h:93
Definition: allocation.h:193
static void * Allocate(AllocationHandle &handle, size_t size)
Definition: allocation.h:211
static void MarkObjectAsFullyConstructed(const void *payload)
Definition: allocation.h:240
Definition: allocation.h:284
static T * Call(AllocationHandle &handle, Args &&... args)
Definition: allocation.h:287
static T * Call(AllocationHandle &handle, AdditionalBytes additional_bytes, Args &&... args)
Definition: allocation.h:296
Definition: allocation.h:51
friend class HeapObjectHeader
Definition: allocation.h:179
CanContainMixins
Definition: allocation.h:53
static void MarkObjectAsFullyConstructed(const void *payload)
Definition: allocation.h:55
uint16_t GCInfoIndex
Definition: gc-info.h:21
std::align_val_t AlignVal
Definition: allocation.h:49
Definition: allocation.h:38
T * MakeGarbageCollected(AllocationHandle &handle, Args &&... args)
Definition: allocation.h:326
Definition: allocation.h:268
constexpr AdditionalBytes(size_t bytes)
Definition: allocation.h:269
const size_t value
Definition: allocation.h:270
Definition: custom-space.h:15
Definition: allocation.h:313
static void Call(T *)
Definition: allocation.h:314
void Space
Definition: custom-space.h:70
Definition: gc-info.h:109
Definition: type-traits.h:99
static void * Invoke(AllocationHandle &handle, size_t size)
Definition: allocation.h:121
static void * Invoke(AllocationHandle &handle, size_t size)
Definition: allocation.h:104
static void * Invoke(AllocationHandle &handle, size_t size)
Definition: allocation.h:140
Definition: allocation.h:76
static void * Invoke(AllocationHandle &handle, size_t size)
Definition: allocation.h:77
#define V8_EXPORT
Definition: v8config.h:854
#define V8_INLINE
Definition: v8config.h:508