Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-22 10:42:25

0001 // Copyright 2020 the V8 project authors. All rights reserved.
0002 // Use of this source code is governed by a BSD-style license that can be
0003 // found in the LICENSE file.
0004 
0005 #ifndef INCLUDE_CPPGC_CUSTOM_SPACE_H_
0006 #define INCLUDE_CPPGC_CUSTOM_SPACE_H_
0007 
0008 #include <stddef.h>
0009 
0010 namespace cppgc {
0011 
0012 /**
0013  * Index identifying a custom space.
0014  */
0015 struct CustomSpaceIndex {
0016   constexpr CustomSpaceIndex(size_t value) : value(value) {}  // NOLINT
0017   size_t value;
0018 };
0019 
0020 /**
0021  * Top-level base class for custom spaces. Users must inherit from CustomSpace
0022  * below.
0023  */
0024 class CustomSpaceBase {
0025  public:
0026   virtual ~CustomSpaceBase() = default;
0027   virtual CustomSpaceIndex GetCustomSpaceIndex() const = 0;
0028   virtual bool IsCompactable() const = 0;
0029 };
0030 
0031 /**
0032  * Base class custom spaces should directly inherit from. The class inheriting
0033  * from `CustomSpace` must define `kSpaceIndex` as unique space index. These
0034  * indices need for form a sequence starting at 0.
0035  *
0036  * Example:
0037  * \code
0038  * class CustomSpace1 : public CustomSpace<CustomSpace1> {
0039  *  public:
0040  *   static constexpr CustomSpaceIndex kSpaceIndex = 0;
0041  * };
0042  * class CustomSpace2 : public CustomSpace<CustomSpace2> {
0043  *  public:
0044  *   static constexpr CustomSpaceIndex kSpaceIndex = 1;
0045  * };
0046  * \endcode
0047  */
0048 template <typename ConcreteCustomSpace>
0049 class CustomSpace : public CustomSpaceBase {
0050  public:
0051   /**
0052    * Compaction is only supported on spaces that manually manage slots
0053    * recording.
0054    */
0055   static constexpr bool kSupportsCompaction = false;
0056 
0057   CustomSpaceIndex GetCustomSpaceIndex() const final {
0058     return ConcreteCustomSpace::kSpaceIndex;
0059   }
0060   bool IsCompactable() const final {
0061     return ConcreteCustomSpace::kSupportsCompaction;
0062   }
0063 };
0064 
0065 /**
0066  * User-overridable trait that allows pinning types to custom spaces.
0067  */
0068 template <typename T, typename = void>
0069 struct SpaceTrait {
0070   using Space = void;
0071 };
0072 
0073 namespace internal {
0074 
0075 template <typename CustomSpace>
0076 struct IsAllocatedOnCompactableSpaceImpl {
0077   static constexpr bool value = CustomSpace::kSupportsCompaction;
0078 };
0079 
0080 template <>
0081 struct IsAllocatedOnCompactableSpaceImpl<void> {
0082   // Non-custom spaces are by default not compactable.
0083   static constexpr bool value = false;
0084 };
0085 
0086 template <typename T>
0087 struct IsAllocatedOnCompactableSpace {
0088  public:
0089   static constexpr bool value =
0090       IsAllocatedOnCompactableSpaceImpl<typename SpaceTrait<T>::Space>::value;
0091 };
0092 
0093 }  // namespace internal
0094 
0095 }  // namespace cppgc
0096 
0097 #endif  // INCLUDE_CPPGC_CUSTOM_SPACE_H_