v8  8.6.395 (node 15.0.1)
V8 is Google's open source JavaScript engine
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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/internal/api-constants.h"
15 #include "cppgc/internal/gc-info.h"
16 
17 namespace cppgc {
18 
19 template <typename T>
20 class MakeGarbageCollectedTraitBase;
21 
22 namespace internal {
23 class ObjectAllocator;
24 } // namespace internal
25 
26 /**
27  * AllocationHandle is used to allocate garbage-collected objects.
28  */
29 class AllocationHandle;
30 
31 namespace internal {
32 
34  protected:
35  static inline void MarkObjectAsFullyConstructed(const void* payload) {
36  // See api_constants for an explanation of the constants.
37  std::atomic<uint16_t>* atomic_mutable_bitfield =
38  reinterpret_cast<std::atomic<uint16_t>*>(
39  const_cast<uint16_t*>(reinterpret_cast<const uint16_t*>(
40  reinterpret_cast<const uint8_t*>(payload) -
41  api_constants::kFullyConstructedBitFieldOffsetFromPayload)));
42  uint16_t value = atomic_mutable_bitfield->load(std::memory_order_relaxed);
43  value = value | api_constants::kFullyConstructedBitMask;
44  atomic_mutable_bitfield->store(value, std::memory_order_release);
45  }
46 
47  static void* Allocate(cppgc::AllocationHandle& handle, size_t size,
48  GCInfoIndex index);
49  static void* Allocate(cppgc::AllocationHandle& handle, size_t size,
50  GCInfoIndex index, CustomSpaceIndex space_index);
51 
52  friend class HeapObjectHeader;
53 };
54 
55 } // namespace internal
56 
57 /**
58  * Base trait that provides utilities for advancers users that have custom
59  * allocation needs (e.g., overriding size). It's expected that users override
60  * MakeGarbageCollectedTrait (see below) and inherit from
61  * MakeGarbageCollectedTraitBase and make use of the low-level primitives
62  * offered to allocate and construct an object.
63  */
64 template <typename T>
65 class MakeGarbageCollectedTraitBase
67  private:
68  template <typename U, typename CustomSpace>
69  struct SpacePolicy {
70  static void* Allocate(AllocationHandle& handle, size_t size) {
71  // Custom space.
72  static_assert(std::is_base_of<CustomSpaceBase, CustomSpace>::value,
73  "Custom space must inherit from CustomSpaceBase.");
75  handle, size, internal::GCInfoTrait<T>::Index(),
76  CustomSpace::kSpaceIndex);
77  }
78  };
79 
80  template <typename U>
81  struct SpacePolicy<U, void> {
82  static void* Allocate(AllocationHandle& handle, size_t size) {
83  // Default space.
85  handle, size, internal::GCInfoTrait<T>::Index());
86  }
87  };
88 
89  protected:
90  /**
91  * Allocates memory for an object of type T.
92  *
93  * \param handle AllocationHandle identifying the heap to allocate the object
94  * on.
95  * \param size The size that should be reserved for the object.
96  * \returns the memory to construct an object of type T on.
97  */
98  static void* Allocate(AllocationHandle& handle, size_t size) {
99  return SpacePolicy<T, typename SpaceTrait<T>::Space>::Allocate(handle,
100  size);
101  }
102 
103  /**
104  * Marks an object as fully constructed, resulting in precise handling by the
105  * garbage collector.
106  *
107  * \param payload The base pointer the object is allocated at.
108  */
109  static void MarkObjectAsFullyConstructed(const void* payload) {
111  payload);
112  }
113 };
114 
115 /**
116  * Default trait class that specifies how to construct an object of type T.
117  * Advanced users may override how an object is constructed using the utilities
118  * that are provided through MakeGarbageCollectedTraitBase.
119  *
120  * Any trait overriding construction must
121  * - allocate through MakeGarbageCollectedTraitBase<T>::Allocate;
122  * - mark the object as fully constructed using
123  * MakeGarbageCollectedTraitBase<T>::MarkObjectAsFullyConstructed;
124  */
125 template <typename T>
126 class MakeGarbageCollectedTrait : public MakeGarbageCollectedTraitBase<T> {
127  public:
128  template <typename... Args>
129  static T* Call(AllocationHandle& handle, Args&&... args) {
130  static_assert(internal::IsGarbageCollectedType<T>::value,
131  "T needs to be a garbage collected object");
132  static_assert(
134  sizeof(T) <= internal::api_constants::kLargeObjectSizeThreshold,
135  "GarbageCollectedMixin may not be a large object");
136  void* memory =
137  MakeGarbageCollectedTraitBase<T>::Allocate(handle, sizeof(T));
138  T* object = ::new (memory) T(std::forward<Args>(args)...);
139  MakeGarbageCollectedTraitBase<T>::MarkObjectAsFullyConstructed(object);
140  return object;
141  }
142 };
143 
144 /**
145  * Allows users to specify a post-construction callback for specific types. The
146  * callback is invoked on the instance of type T right after it has been
147  * constructed. This can be useful when the callback requires a
148  * fully-constructed object to be able to dispatch to virtual methods.
149  */
150 template <typename T, typename = void>
152  static void Call(T*) {}
153 };
154 
155 /**
156  * Constructs a managed object of type T where T transitively inherits from
157  * GarbageCollected.
158  *
159  * \param args List of arguments with which an instance of T will be
160  * constructed.
161  * \returns an instance of type T.
162  */
163 template <typename T, typename... Args>
164 T* MakeGarbageCollected(AllocationHandle& handle, Args&&... args) {
165  T* object =
166  MakeGarbageCollectedTrait<T>::Call(handle, std::forward<Args>(args)...);
167  PostConstructionCallbackTrait<T>::Call(object);
168  return object;
169 }
170 
171 } // namespace cppgc
172 
173 #endif // INCLUDE_CPPGC_ALLOCATION_H_