v8  8.4.371 (node 14.15.5)
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"
13 #include "cppgc/garbage-collected.h"
14 #include "cppgc/heap.h"
15 #include "cppgc/internal/api-constants.h"
16 #include "cppgc/internal/gc-info.h"
17 
18 namespace cppgc {
19 
20 template <typename T>
21 class MakeGarbageCollectedTraitBase;
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 
48 /**
49  * Base trait that provides utilities for advancers users that have custom
50  * allocation needs (e.g., overriding size). It's expected that users override
51  * MakeGarbageCollectedTrait (see below) and inherit from
52  * MakeGarbageCollectedTraitBase and make use of the low-level primitives
53  * offered to allocate and construct an object.
54  */
55 template <typename T>
56 class MakeGarbageCollectedTraitBase
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:
81  /**
82  * Allocates memory for an object of type T.
83  *
84  * \param heap The heap to allocate this object on.
85  * \param size The size that should be reserved for the object.
86  * \returns the memory to construct an object of type T on.
87  */
88  static void* Allocate(Heap* heap, size_t size) {
89  return SpacePolicy<T, typename SpaceTrait<T>::Space>::Allocate(heap, size);
90  }
91 
92  /**
93  * Marks an object as fully constructed, resulting in precise handling by the
94  * garbage collector.
95  *
96  * \param payload The base pointer the object is allocated at.
97  */
98  static void MarkObjectAsFullyConstructed(const void* payload) {
100  payload);
101  }
102 };
103 
104 /**
105  * Default trait class that specifies how to construct an object of type T.
106  * Advanced users may override how an object is constructed using the utilities
107  * that are provided through MakeGarbageCollectedTraitBase.
108  *
109  * Any trait overriding construction must
110  * - allocate through MakeGarbageCollectedTraitBase<T>::Allocate;
111  * - mark the object as fully constructed using
112  * MakeGarbageCollectedTraitBase<T>::MarkObjectAsFullyConstructed;
113  */
114 template <typename T>
115 class MakeGarbageCollectedTrait : public MakeGarbageCollectedTraitBase<T> {
116  public:
117  template <typename... Args>
118  static T* Call(Heap* heap, Args&&... args) {
119  static_assert(internal::IsGarbageCollectedType<T>::value,
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)...);
127  MakeGarbageCollectedTraitBase<T>::MarkObjectAsFullyConstructed(object);
128  return object;
129  }
130 };
131 
132 /**
133  * Allows users to specify a post-construction callback for specific types. The
134  * callback is invoked on the instance of type T right after it has been
135  * constructed. This can be useful when the callback requires a
136  * fully-constructed object to be able to dispatch to virtual methods.
137  */
138 template <typename T, typename = void>
140  static void Call(T*) {}
141 };
142 
143 /**
144  * Constructs a managed object of type T where T transitively inherits from
145  * GarbageCollected.
146  *
147  * \param args List of arguments with which an instance of T will be
148  * constructed.
149  * \returns an instance of type T.
150  */
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)...);
155  PostConstructionCallbackTrait<T>::Call(object);
156  return object;
157 }
158 
159 } // namespace cppgc
160 
161 #endif // INCLUDE_CPPGC_ALLOCATION_H_