V8  latest master commit
V8 is Google's open source JavaScript engine
allocation.h
Go to the documentation of this file.
1 // Copyright 2020 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef INCLUDE_CPPGC_ALLOCATION_H_
6 #define INCLUDE_CPPGC_ALLOCATION_H_
7 
8 #include <stdint.h>
9 
10 #include <atomic>
11 
12 #include "cppgc/custom-space.h"
14 #include "cppgc/heap.h"
16 #include "cppgc/internal/gc-info.h"
17 
18 namespace cppgc {
19 
20 template <typename T>
22 
23 namespace internal {
24 
26  protected:
27  static inline void MarkObjectAsFullyConstructed(const void* payload) {
28  // See api_constants for an explanation of the constants.
29  std::atomic<uint16_t>* atomic_mutable_bitfield =
30  reinterpret_cast<std::atomic<uint16_t>*>(
31  const_cast<uint16_t*>(reinterpret_cast<const uint16_t*>(
32  reinterpret_cast<const uint8_t*>(payload) -
33  api_constants::kFullyConstructedBitFieldOffsetFromPayload)));
34  uint16_t value = atomic_mutable_bitfield->load(std::memory_order_relaxed);
35  value = value | api_constants::kFullyConstructedBitMask;
36  atomic_mutable_bitfield->store(value, std::memory_order_release);
37  }
38 
39  static void* Allocate(cppgc::Heap* heap, size_t size, GCInfoIndex index);
40  static void* Allocate(cppgc::Heap* heapx, size_t size, GCInfoIndex index,
41  CustomSpaceIndex space_inde);
42 
43  friend class HeapObjectHeader;
44 };
45 
46 } // namespace internal
47 
55 template <typename T>
58  private:
59  template <typename U, typename CustomSpace>
60  struct SpacePolicy {
61  static void* Allocate(Heap* heap, size_t size) {
62  // Custom space.
63  static_assert(std::is_base_of<CustomSpaceBase, CustomSpace>::value,
64  "Custom space must inherit from CustomSpaceBase.");
66  heap, size, internal::GCInfoTrait<T>::Index(),
67  CustomSpace::kSpaceIndex);
68  }
69  };
70 
71  template <typename U>
72  struct SpacePolicy<U, void> {
73  static void* Allocate(Heap* heap, size_t size) {
74  // Default space.
76  heap, size, internal::GCInfoTrait<T>::Index());
77  }
78  };
79 
80  protected:
88  static void* Allocate(Heap* heap, size_t size) {
89  return SpacePolicy<T, typename SpaceTrait<T>::Space>::Allocate(heap, size);
90  }
91 
98  static void MarkObjectAsFullyConstructed(const void* payload) {
100  payload);
101  }
102 };
103 
114 template <typename T>
116  public:
117  template <typename... Args>
118  static T* Call(Heap* heap, Args&&... args) {
120  "T needs to be a garbage collected object");
121  static_assert(
123  sizeof(T) <= internal::api_constants::kLargeObjectSizeThreshold,
124  "GarbageCollectedMixin may not be a large object");
125  void* memory = MakeGarbageCollectedTraitBase<T>::Allocate(heap, sizeof(T));
126  T* object = ::new (memory) T(std::forward<Args>(args)...);
128  return object;
129  }
130 };
131 
138 template <typename T, typename = void>
140  static void Call(T*) {}
141 };
142 
151 template <typename T, typename... Args>
152 T* MakeGarbageCollected(Heap* heap, Args&&... args) {
153  T* object =
154  MakeGarbageCollectedTrait<T>::Call(heap, std::forward<Args>(args)...);
156  return object;
157 }
158 
159 } // namespace cppgc
160 
161 #endif // INCLUDE_CPPGC_ALLOCATION_H_
garbage-collected.h
cppgc::internal::GCInfoIndex
uint16_t GCInfoIndex
Definition: gc-info.h:17
cppgc::PostConstructionCallbackTrait
Definition: allocation.h:139
cppgc::internal::IsGarbageCollectedType
Definition: type-traits.h:83
custom-space.h
cppgc::internal::MakeGarbageCollectedTraitInternal::MarkObjectAsFullyConstructed
static void MarkObjectAsFullyConstructed(const void *payload)
Definition: allocation.h:27
cppgc::internal::GCInfoTrait
Definition: gc-info.h:32
cppgc
Definition: allocation.h:18
cppgc::internal::MakeGarbageCollectedTraitInternal
Definition: allocation.h:25
api-constants.h
cppgc::MakeGarbageCollectedTrait::Call
static T * Call(Heap *heap, Args &&... args)
Definition: allocation.h:118
V8_EXPORT
#define V8_EXPORT
Definition: v8config.h:467
cppgc::Heap
Definition: heap.h:20
cppgc::internal::GCInfoTrait::Index
static GCInfoIndex Index()
Definition: gc-info.h:33
gc-info.h
cppgc::MakeGarbageCollectedTraitBase::Allocate
static void * Allocate(Heap *heap, size_t size)
Definition: allocation.h:88
cppgc::MakeGarbageCollectedTrait
Definition: allocation.h:115
cppgc::MakeGarbageCollectedTraitBase::MarkObjectAsFullyConstructed
static void MarkObjectAsFullyConstructed(const void *payload)
Definition: allocation.h:98
cppgc::internal::IsGarbageCollectedMixinType
Definition: type-traits.h:70
cppgc::MakeGarbageCollected
T * MakeGarbageCollected(Heap *heap, Args &&... args)
Definition: allocation.h:152
cppgc::internal::MakeGarbageCollectedTraitInternal::Allocate
static void * Allocate(cppgc::Heap *heap, size_t size, GCInfoIndex index)
cppgc::MakeGarbageCollectedTraitBase
Definition: allocation.h:21
cppgc::CustomSpaceIndex
Definition: custom-space.h:12
cppgc::PostConstructionCallbackTrait::Call
static void Call(T *)
Definition: allocation.h:140
heap.h