v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
zone-with-name.h
Go to the documentation of this file.
1// Copyright 2024 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_COMPILER_TURBOSHAFT_ZONE_WITH_NAME_H_
6#define V8_COMPILER_TURBOSHAFT_ZONE_WITH_NAME_H_
7
10
12
13// In debug builds, `ZoneWithNamePointer` is a lightweight wrapper around a raw
14// pointer to a zone-allocated object that encodes the identity of the zone (in
15// terms of the zone's name) in its C++ type. This makes it more explicit what
16// the lifetime of the respective object is (aka until the corresponding zone is
17// gone) and provides an additional layer of safety against misuse with other
18// pointer types. Such pointers are typically created by the respective zone.
19// Example:
20//
21// ZoneWithName<kGraphZoneName> graph_zone;
22// ZoneWithNamePointer<Graph, kGraphZoneName> graph = graph_zone.New<Graph>();
23// foo(graph_zone, graph);
24//
25// Both `ZoneWithName` as well as `ZoneWithNamePointer` will implicitly convert
26// to the underlying raw `Zone*` and `Graph*` to make its use as smooth as
27// possible, even when `foo`'s arguments expects raw types. NOTE: In release
28// builds, `ZoneWithNamePointer<T, Name>` is merely an alias to `T*`.
29#if defined(DEBUG) && defined(HAS_CPP_CLASS_TYPES_AS_TEMPLATE_ARGS)
30template <typename T, base::tmp::StringLiteral Name>
31class ZoneWithNamePointerImpl final {
32 public:
33 using pointer_type = T*;
34
35 ZoneWithNamePointerImpl() = default;
36 ZoneWithNamePointerImpl(std::nullptr_t) // NOLINT(runtime/explicit)
37 : ptr_(nullptr) {}
38 explicit ZoneWithNamePointerImpl(pointer_type ptr) : ptr_(ptr) {}
39
40 ZoneWithNamePointerImpl(const ZoneWithNamePointerImpl&) V8_NOEXCEPT = default;
41 ZoneWithNamePointerImpl(ZoneWithNamePointerImpl&&) V8_NOEXCEPT = default;
42 template <typename U>
43 ZoneWithNamePointerImpl(const ZoneWithNamePointerImpl<U, Name>& other)
44 V8_NOEXCEPT // NOLINT(runtime/explicit)
45 requires(std::is_convertible_v<U*, pointer_type>)
46 : ptr_(static_cast<U*>(other)) {}
47 ZoneWithNamePointerImpl& operator=(const ZoneWithNamePointerImpl&)
48 V8_NOEXCEPT = default;
49 ZoneWithNamePointerImpl& operator=(ZoneWithNamePointerImpl&&)
50 V8_NOEXCEPT = default;
51 template <typename U>
52 ZoneWithNamePointerImpl& operator=(
53 const ZoneWithNamePointerImpl<U, Name>& other) V8_NOEXCEPT
54 requires(std::is_convertible_v<U*, pointer_type>)
55 {
56 ptr_ = static_cast<U*>(other);
57 }
58
59 operator pointer_type() const { return get(); } // NOLINT(runtime/explicit)
60 T& operator*() const { return *get(); }
61 pointer_type operator->() { return get(); }
62
63 private:
64 pointer_type get() const { return ptr_; }
65
66 pointer_type ptr_ = pointer_type{};
67};
68
69template <typename T, base::tmp::StringLiteral Name>
70using ZoneWithNamePointer = ZoneWithNamePointerImpl<T, Name>;
71#else
72template <typename T, auto>
74#endif
75
76#ifdef HAS_CPP_CLASS_TYPES_AS_TEMPLATE_ARGS
77template <base::tmp::StringLiteral Name>
78#else
79template <auto Name>
80#endif
81class ZoneWithName final {
82 public:
83 ZoneWithName(ZoneStats* pool, const char* name,
84 bool support_zone_compression = false)
85 : scope_(pool, name, support_zone_compression) {
86#ifdef HAS_CPP_CLASS_TYPES_AS_TEMPLATE_ARGS
87 DCHECK_EQ(std::strcmp(name, Name.c_str()), 0);
88#endif
89 }
90
91 ZoneWithName(const ZoneWithName&) = delete;
93 : scope_(std::move(other.scope_)) {}
96 scope_ = std::move(other.scope_);
97 return *this;
98 }
99
100 template <typename T, typename... Args>
103 get()->template New<T>(std::forward<Args>(args)...)};
104 }
105
106 template <typename T>
109 get()->template AllocateArray<T>(length)};
110 }
111
112 Zone* get() { return scope_.zone(); }
113 operator Zone*() { return get(); } // NOLINT(runtime/explicit)
114 Zone* operator->() { return get(); }
115
116 void Destroy() { scope_.Destroy(); }
117
118 private:
119 // NOTE: `ZoneStats::Scope` actually allocates a new zone.
121};
122
123} // namespace v8::internal::compiler::turboshaft
124
125#endif // V8_COMPILER_TURBOSHAFT_ZONE_WITH_NAME_H_
#define T
ZoneWithName & operator=(const ZoneWithName &)=delete
ZoneWithNamePointer< T, Name > AllocateArray(size_t length)
ZoneWithNamePointer< T, Name > New(Args &&... args)
ZoneWithName & operator=(ZoneWithName &&other) V8_NOEXCEPT
ZoneWithName(ZoneWithName &&other) V8_NOEXCEPT
ZoneWithName(ZoneStats *pool, const char *name, bool support_zone_compression=false)
Node ** ptr_
base::Vector< const DirectHandle< Object > > args
Definition execution.cc:74
STL namespace.
constexpr int U
#define V8_NOEXCEPT
#define DCHECK_EQ(v1, v2)
Definition logging.h:485