5#ifndef INCLUDE_CPPGC_ALLOCATION_H_
6#define INCLUDE_CPPGC_ALLOCATION_H_
22#if defined(__has_attribute)
23#if __has_attribute(assume_aligned)
24#define CPPGC_DEFAULT_ALIGNED \
25 __attribute__((assume_aligned(api_constants::kDefaultAlignment)))
26#define CPPGC_DOUBLE_WORD_ALIGNED \
27 __attribute__((assume_aligned(2 * api_constants::kDefaultAlignment)))
31#if !defined(CPPGC_DEFAULT_ALIGNED)
32#define CPPGC_DEFAULT_ALIGNED
35#if !defined(CPPGC_DOUBLE_WORD_ALIGNED)
36#define CPPGC_DOUBLE_WORD_ALIGNED
44class AllocationHandle;
58 std::atomic_ref<uint16_t> atomic_mutable_bitfield(
59 *
const_cast<uint16_t*
>(
reinterpret_cast<const uint16_t*
>(
60 reinterpret_cast<const uint8_t*
>(payload) -
61 api_constants::kFullyConstructedBitFieldOffsetFromPayload)));
66 uint16_t value = atomic_mutable_bitfield.load(std::memory_order_relaxed);
67 value |= api_constants::kFullyConstructedBitMask;
68 atomic_mutable_bitfield.store(value, std::memory_order_release);
75 template <
typename GCInfoType,
typename CustomSpace,
size_t alignment,
78 static void*
Invoke(AllocationHandle& handle,
size_t size) {
79 static_assert(std::is_base_of_v<CustomSpaceBase, CustomSpace>,
80 "Custom space must inherit from CustomSpaceBase.");
83 "Custom spaces that support compaction do not support allocating "
84 "objects with non-default (i.e. word-sized) alignment.");
86 return MakeGarbageCollectedTraitInternal::Allocate(
87 handle, size,
static_cast<AlignVal>(alignment),
89 CustomSpace::kSpaceIndex, kMixinTag);
91 return MakeGarbageCollectedTraitInternal::Allocate(
92 handle, size,
static_cast<AlignVal>(alignment),
94 CustomSpace::kSpaceIndex);
101 template <
typename GCInfoType, CanContainMixins contain_mixins>
103 api_constants::kDefaultAlignment, contain_mixins>
105 static void*
Invoke(AllocationHandle& handle,
size_t size) {
107 return MakeGarbageCollectedTraitInternal::Allocate(
111 return MakeGarbageCollectedTraitInternal::Allocate(
118 template <
typename GCInfoType,
size_t alignment,
122 static void*
Invoke(AllocationHandle& handle,
size_t size) {
124 return MakeGarbageCollectedTraitInternal::Allocate(
125 handle, size,
static_cast<AlignVal>(alignment),
128 return MakeGarbageCollectedTraitInternal::Allocate(
129 handle, size,
static_cast<AlignVal>(alignment),
136 template <
typename GCInfoType,
typename CustomSpace,
139 api_constants::kDefaultAlignment, contain_mixins>
141 static void*
Invoke(AllocationHandle& handle,
size_t size) {
142 static_assert(std::is_base_of_v<CustomSpaceBase, CustomSpace>,
143 "Custom space must inherit from CustomSpaceBase.");
145 return MakeGarbageCollectedTraitInternal::Allocate(
147 CustomSpace::kSpaceIndex, kMixinTag);
149 return MakeGarbageCollectedTraitInternal::Allocate(
151 CustomSpace::kSpaceIndex);
157 inline const static struct MixinTag {
161 Allocate(cppgc::AllocationHandle&,
size_t,
GCInfoIndex);
170 Allocate(cppgc::AllocationHandle&,
size_t,
GCInfoIndex, MixinTag);
197 "T needs to be a garbage collected object");
198 static_assert(!IsGarbageCollectedWithMixinTypeV<T> ||
200 internal::api_constants::kLargeObjectSizeThreshold,
201 "GarbageCollectedMixin may not be a large object");
214 std::is_base_of_v<typename T::ParentMostGarbageCollectedType, T>,
215 "U of GarbageCollected<U> must be a base of T. Check "
216 "GarbageCollected<T> base class inheritance.");
217 static constexpr size_t kWantedAlignment =
218 alignof(T) < internal::api_constants::kDefaultAlignment
219 ? internal::api_constants::kDefaultAlignment
222 std::is_base_of_v<GarbageCollectedMixin, T> ? CanContainMixins::kYes
223 : CanContainMixins::kNo;
225 kWantedAlignment <= internal::api_constants::kMaxSupportedAlignment,
226 "Requested alignment larger than alignof(std::max_align_t) bytes. "
227 "Please file a bug to possibly get this restriction lifted.");
230 T,
typename T::ParentMostGarbageCollectedType>::ResultType,
232 kMayContainMixins>::Invoke(handle, size);
287 template <
typename... Args>
288 static T*
Call(AllocationHandle& handle, Args&&... args) {
289 static_assert(
sizeof(T) <= std::numeric_limits<uint32_t>::max());
292 T*
object = ::new (memory) T(std::forward<Args>(args)...);
297 template <
typename... Args>
300 static_assert(
sizeof(T) <= std::numeric_limits<uint32_t>::max());
301 const size_t size_to_allocate =
sizeof(T) + additional_bytes.
value;
305#
if !defined(V8_HOST_ARCH_64_BIT)
310 T*
object = ::new (memory) T(std::forward<Args>(args)...);
322template <
typename T,
typename =
void>
335template <
typename T,
typename... Args>
353template <
typename T,
typename... Args>
358 std::forward<Args>(args)...);
365#undef CPPGC_DEFAULT_ALIGNED
366#undef CPPGC_DOUBLE_WORD_ALIGNED
#define CPPGC_DEFAULT_ALIGNED
Definition: allocation.h:32
#define CPPGC_DOUBLE_WORD_ALIGNED
Definition: allocation.h:36
Definition: custom-space.h:49
static constexpr bool kSupportsCompaction
Definition: custom-space.h:55
Definition: garbage-collected.h:93
Definition: allocation.h:194
static void * Allocate(AllocationHandle &handle, size_t size)
Definition: allocation.h:212
static void MarkObjectAsFullyConstructed(const void *payload)
Definition: allocation.h:241
Definition: allocation.h:285
static T * Call(AllocationHandle &handle, Args &&... args)
Definition: allocation.h:288
static T * Call(AllocationHandle &handle, AdditionalBytes additional_bytes, Args &&... args)
Definition: allocation.h:298
Definition: allocation.h:52
friend class HeapObjectHeader
Definition: allocation.h:180
CanContainMixins
Definition: allocation.h:54
static void MarkObjectAsFullyConstructed(const void *payload)
Definition: allocation.h:56
#define CPPGC_CHECK(condition)
Definition: logging.h:45
uint16_t GCInfoIndex
Definition: gc-info.h:20
std::align_val_t AlignVal
Definition: allocation.h:50
Definition: allocation.h:39
T * MakeGarbageCollected(AllocationHandle &handle, Args &&... args)
Definition: allocation.h:336
Definition: allocation.h:269
const uint32_t value
Definition: allocation.h:271
constexpr AdditionalBytes(uint32_t bytes)
Definition: allocation.h:270
Definition: custom-space.h:15
Definition: allocation.h:323
static void Call(T *)
Definition: allocation.h:324
void Space
Definition: custom-space.h:70
Definition: gc-info.h:179
Definition: gc-info.h:145
Definition: type-traits.h:99
static void * Invoke(AllocationHandle &handle, size_t size)
Definition: allocation.h:122
static void * Invoke(AllocationHandle &handle, size_t size)
Definition: allocation.h:105
static void * Invoke(AllocationHandle &handle, size_t size)
Definition: allocation.h:141
Definition: allocation.h:77
static void * Invoke(AllocationHandle &handle, size_t size)
Definition: allocation.h:78
#define V8_EXPORT
Definition: v8config.h:874
#define V8_INLINE
Definition: v8config.h:518