• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 V8_ZONE_ZONE_TYPE_TRAITS_H_
6 #define V8_ZONE_ZONE_TYPE_TRAITS_H_
7 
8 #include "src/common/globals.h"
9 
10 #ifdef V8_COMPRESS_ZONES
11 #include "src/zone/compressed-zone-ptr.h"
12 #endif
13 
14 namespace v8 {
15 namespace internal {
16 
17 template <typename T>
18 class ZoneList;
19 
20 // ZonePtrList is a ZoneList of pointers to ZoneObjects allocated in the same
21 // zone as the list object.
22 template <typename T>
23 using ZonePtrList = ZoneList<T*>;
24 
25 template <typename T>
26 using FullZonePtr = T*;
27 
28 template <typename T>
29 class CompressedZonePtr;
30 
31 //
32 // ZoneTypeTraits provides type aliases for compressed or full pointer
33 // dependent types based on a static flag. It helps organizing fine-grained
34 // control over which parts of the code base should use compressed zone
35 // pointers.
36 // For example:
37 //   using ZoneNodePtr = typename ZoneTypeTraits<kCompressGraphZone>::Ptr<Node>;
38 //
39 // or
40 //   template <typename T>
41 //   using AstZonePtr = typename ZoneTypeTraits<kCompressAstZone>::Ptr<T>;
42 //
43 template <bool kEnableCompression>
44 struct ZoneTypeTraits;
45 
46 template <>
47 struct ZoneTypeTraits<false> {
48   template <typename T>
49   using Ptr = FullZonePtr<T>;
50 };
51 
52 template <>
53 struct ZoneTypeTraits<true> {
54   template <typename T>
55   using Ptr = CompressedZonePtr<T>;
56 };
57 
58 // This requirement is necessary for being able to use memcopy in containers
59 // of zone pointers.
60 // TODO(ishell): Re-enable once compressed pointers are supported in containers.
61 // static_assert(
62 //     std::is_trivially_copyable<
63 //         ZoneTypeTraits<COMPRESS_ZONES_BOOL>::Ptr<int>>::value,
64 //     "ZoneTypeTraits<COMPRESS_ZONES_BOOL>::Ptr<T> must be trivially
65 //     copyable");
66 
67 //
68 // is_compressed_pointer<T> predicate can be used for checking if T is a
69 // compressed pointer.
70 //
71 template <typename>
72 struct is_compressed_pointer : std::false_type {};
73 
74 template <typename T>
75 struct is_compressed_pointer<CompressedZonePtr<T>> : std::true_type {};
76 
77 template <typename T>
78 struct is_compressed_pointer<const CompressedZonePtr<T>> : std::true_type {};
79 
80 }  // namespace internal
81 }  // namespace v8
82 
83 #endif  // V8_ZONE_ZONE_TYPE_TRAITS_H_
84