5#ifndef INCLUDE_CPPGC_ALLOCATION_H_
6#define INCLUDE_CPPGC_ALLOCATION_H_
15#include "cppgc/custom-space.h"
16#include "cppgc/internal/api-constants.h"
17#include "cppgc/internal/gc-info.h"
18#include "cppgc/type-traits.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)))
31#define CPPGC_DEFAULT_ALIGNED
35#define CPPGC_DOUBLE_WORD_ALIGNED
41
42
43class AllocationHandle;
47using AlignVal = std::align_val_t;
53 std::atomic<uint16_t>* atomic_mutable_bitfield =
54 reinterpret_cast<std::atomic<uint16_t>*>(
55 const_cast<uint16_t*>(
reinterpret_cast<
const uint16_t*>(
56 reinterpret_cast<
const uint8_t*>(payload) -
57 api_constants::kFullyConstructedBitFieldOffsetFromPayload)));
62 uint16_t value = atomic_mutable_bitfield->load(std::memory_order_relaxed);
64 atomic_mutable_bitfield->store(value, std::memory_order_release);
71 template <
typename GCInfoType,
typename CustomSpace, size_t alignment>
72 struct AllocationDispatcher
final {
73 static void*
Invoke(AllocationHandle& handle, size_t size) {
75 "Custom space must inherit from CustomSpaceBase.");
77 !CustomSpace::kSupportsCompaction,
78 "Custom spaces that support compaction do not support allocating "
79 "objects with non-default (i.e. word-sized) alignment.");
80 return MakeGarbageCollectedTraitInternal::Allocate(
81 handle, size,
static_cast<AlignVal>(alignment),
82 internal::GCInfoTrait<GCInfoType>::Index(), CustomSpace::kSpaceIndex);
99 template <
typename GCInfoType, size_t alignment>
100 struct AllocationDispatcher<GCInfoType,
void, alignment>
final {
101 static void*
Invoke(AllocationHandle& handle, size_t size) {
102 return MakeGarbageCollectedTraitInternal::Allocate(
103 handle, size,
static_cast<AlignVal>(alignment),
104 internal::GCInfoTrait<GCInfoType>::Index());
115 "Custom space must inherit from CustomSpaceBase.");
124 Allocate(
cppgc::AllocationHandle&, size_t, GCInfoIndex);
126 Allocate(
cppgc::AllocationHandle&, size_t, AlignVal, GCInfoIndex);
130 Allocate(
cppgc::AllocationHandle&, size_t, AlignVal, GCInfoIndex,
133 friend class HeapObjectHeader;
139
140
141
142
143
144
150 "T needs to be a garbage collected object");
151 static_assert(!IsGarbageCollectedWithMixinTypeV<T> ||
153 internal::api_constants::kLargeObjectSizeThreshold,
154 "GarbageCollectedMixin may not be a large object");
158
159
160
161
162
163
164
167 std::is_base_of_v<
typename T::ParentMostGarbageCollectedType, T>,
168 "U of GarbageCollected<U> must be a base of T. Check "
169 "GarbageCollected<T> base class inheritance.");
170 static constexpr size_t kWantedAlignment =
171 alignof(T) < internal::api_constants::kDefaultAlignment
172 ? internal::api_constants::kDefaultAlignment
175 kWantedAlignment <= internal::api_constants::kMaxSupportedAlignment,
176 "Requested alignment larger than alignof(std::max_align_t) bytes. "
177 "Please file a bug to possibly get this restriction lifted.");
178 return AllocationDispatcher<
179 typename internal::GCInfoFolding<
180 T,
typename T::ParentMostGarbageCollectedType>::ResultType,
181 typename SpaceTrait<T>::Space, kWantedAlignment>::Invoke(handle, size);
185
186
187
188
189
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
224
225
226
227
228
229
230
231
232
236 template <
typename... Args>
237 static T*
Call(AllocationHandle& handle, Args&&... args) {
240 T* object = ::
new (memory) T(std::forward<Args>(args)...);
245 template <
typename... Args>
249 handle,
sizeof(T) + additional_bytes.value);
250 T* object = ::
new (memory) T(std::forward<Args>(args)...);
257
258
259
260
261
262template <
typename T,
typename =
void>
268
269
270
271
272
273
274
275template <
typename T,
typename... Args>
279 PostConstructionCallbackTrait<T>::Call(object);
284
285
286
287
288
289
290
291
292
293template <
typename T,
typename... Args>
298 std::forward<Args>(args)...);
299 PostConstructionCallbackTrait<T>::Call(object);
305#undef CPPGC_DEFAULT_ALIGNED
306#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)
V8_INLINE T * MakeGarbageCollected(AllocationHandle &handle, AdditionalBytes additional_bytes, Args &&... args)
constexpr AdditionalBytes(size_t bytes)
static void * Invoke(AllocationHandle &handle, size_t size)
static void * Invoke(AllocationHandle &handle, size_t size)