allocation.h 5.93 KB
Newer Older
1 2 3 4 5 6 7 8
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef INCLUDE_CPPGC_ALLOCATION_H_
#define INCLUDE_CPPGC_ALLOCATION_H_

#include <stdint.h>
9

10 11
#include <atomic>

12
#include "cppgc/custom-space.h"
13 14 15
#include "cppgc/garbage-collected.h"
#include "cppgc/internal/api-constants.h"
#include "cppgc/internal/gc-info.h"
16 17

namespace cppgc {
18 19 20 21

template <typename T>
class MakeGarbageCollectedTraitBase;

22 23 24 25 26 27 28 29 30
namespace internal {
class ObjectAllocator;
}  // namespace internal

/**
 * AllocationHandle is used to allocate garbage-collected objects.
 */
class AllocationHandle;

31 32
namespace internal {

33 34 35 36 37 38 39 40 41 42 43 44 45 46
class V8_EXPORT MakeGarbageCollectedTraitInternal {
 protected:
  static inline void MarkObjectAsFullyConstructed(const void* payload) {
    // See api_constants for an explanation of the constants.
    std::atomic<uint16_t>* atomic_mutable_bitfield =
        reinterpret_cast<std::atomic<uint16_t>*>(
            const_cast<uint16_t*>(reinterpret_cast<const uint16_t*>(
                reinterpret_cast<const uint8_t*>(payload) -
                api_constants::kFullyConstructedBitFieldOffsetFromPayload)));
    uint16_t value = atomic_mutable_bitfield->load(std::memory_order_relaxed);
    value = value | api_constants::kFullyConstructedBitMask;
    atomic_mutable_bitfield->store(value, std::memory_order_release);
  }

47 48 49 50
  static void* Allocate(cppgc::AllocationHandle& handle, size_t size,
                        GCInfoIndex index);
  static void* Allocate(cppgc::AllocationHandle& handle, size_t size,
                        GCInfoIndex index, CustomSpaceIndex space_index);
51 52 53

  friend class HeapObjectHeader;
};
54 55

}  // namespace internal
56

57 58 59 60 61 62 63
/**
 * Base trait that provides utilities for advancers users that have custom
 * allocation needs (e.g., overriding size). It's expected that users override
 * MakeGarbageCollectedTrait (see below) and inherit from
 * MakeGarbageCollectedTraitBase and make use of the low-level primitives
 * offered to allocate and construct an object.
 */
64 65 66
template <typename T>
class MakeGarbageCollectedTraitBase
    : private internal::MakeGarbageCollectedTraitInternal {
67 68 69
 private:
  template <typename U, typename CustomSpace>
  struct SpacePolicy {
70
    static void* Allocate(AllocationHandle& handle, size_t size) {
71 72 73 74
      // Custom space.
      static_assert(std::is_base_of<CustomSpaceBase, CustomSpace>::value,
                    "Custom space must inherit from CustomSpaceBase.");
      return internal::MakeGarbageCollectedTraitInternal::Allocate(
75
          handle, size, internal::GCInfoTrait<T>::Index(),
76 77 78 79 80 81
          CustomSpace::kSpaceIndex);
    }
  };

  template <typename U>
  struct SpacePolicy<U, void> {
82
    static void* Allocate(AllocationHandle& handle, size_t size) {
83 84
      // Default space.
      return internal::MakeGarbageCollectedTraitInternal::Allocate(
85
          handle, size, internal::GCInfoTrait<T>::Index());
86 87 88
    }
  };

89
 protected:
90 91 92
  /**
   * Allocates memory for an object of type T.
   *
93 94
   * \param handle AllocationHandle identifying the heap to allocate the object
   *   on.
95 96 97
   * \param size The size that should be reserved for the object.
   * \returns the memory to construct an object of type T on.
   */
98
  static void* Allocate(AllocationHandle& handle, size_t size) {
99 100
    return SpacePolicy<T, typename SpaceTrait<T>::Space>::Allocate(handle,
                                                                   size);
101 102
  }

103 104 105 106 107 108
  /**
   * Marks an object as fully constructed, resulting in precise handling by the
   * garbage collector.
   *
   * \param payload The base pointer the object is allocated at.
   */
109 110 111 112 113 114
  static void MarkObjectAsFullyConstructed(const void* payload) {
    internal::MakeGarbageCollectedTraitInternal::MarkObjectAsFullyConstructed(
        payload);
  }
};

115 116 117 118 119 120 121 122 123 124
/**
 * Default trait class that specifies how to construct an object of type T.
 * Advanced users may override how an object is constructed using the utilities
 * that are provided through MakeGarbageCollectedTraitBase.
 *
 * Any trait overriding construction must
 * - allocate through MakeGarbageCollectedTraitBase<T>::Allocate;
 * - mark the object as fully constructed using
 *   MakeGarbageCollectedTraitBase<T>::MarkObjectAsFullyConstructed;
 */
125 126 127 128
template <typename T>
class MakeGarbageCollectedTrait : public MakeGarbageCollectedTraitBase<T> {
 public:
  template <typename... Args>
129
  static T* Call(AllocationHandle& handle, Args&&... args) {
130 131
    static_assert(internal::IsGarbageCollectedType<T>::value,
                  "T needs to be a garbage collected object");
132 133 134 135
    static_assert(
        !internal::IsGarbageCollectedMixinType<T>::value ||
            sizeof(T) <= internal::api_constants::kLargeObjectSizeThreshold,
        "GarbageCollectedMixin may not be a large object");
136 137
    void* memory =
        MakeGarbageCollectedTraitBase<T>::Allocate(handle, sizeof(T));
138 139 140 141 142 143
    T* object = ::new (memory) T(std::forward<Args>(args)...);
    MakeGarbageCollectedTraitBase<T>::MarkObjectAsFullyConstructed(object);
    return object;
  }
};

144 145 146 147 148 149 150 151 152 153 154
/**
 * Allows users to specify a post-construction callback for specific types. The
 * callback is invoked on the instance of type T right after it has been
 * constructed. This can be useful when the callback requires a
 * fully-constructed object to be able to dispatch to virtual methods.
 */
template <typename T, typename = void>
struct PostConstructionCallbackTrait {
  static void Call(T*) {}
};

155 156 157 158 159 160 161 162
/**
 * Constructs a managed object of type T where T transitively inherits from
 * GarbageCollected.
 *
 * \param args List of arguments with which an instance of T will be
 *   constructed.
 * \returns an instance of type T.
 */
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 168
  PostConstructionCallbackTrait<T>::Call(object);
  return object;
169 170
}

171 172 173
}  // namespace cppgc

#endif  // INCLUDE_CPPGC_ALLOCATION_H_