v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
allocation-result.h
Go to the documentation of this file.
1// Copyright 2022 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_HEAP_ALLOCATION_RESULT_H_
6#define V8_HEAP_ALLOCATION_RESULT_H_
7
11
12namespace v8 {
13namespace internal {
14
23
24// The result of an allocation attempt. Either represents a successful
25// allocation that can be turned into an object or a failed attempt.
26class AllocationResult final {
27 public:
29
31 return AllocationResult(heap_object);
32 }
33
34 // Empty constructor creates a failed result. The callsite determines which
35 // GC to invoke based on the requested allocation.
36 AllocationResult() = default;
37
38 bool IsFailure() const { return object_.is_null(); }
39
40 template <typename T>
41 bool To(Tagged<T>* obj) const {
42 if (IsFailure()) return false;
43 *obj = Cast<T>(object_);
44 return true;
45 }
46
51
56
58 DCHECK(!IsFailure());
59 return Cast<HeapObject>(object_).address();
60 }
61
62 explicit operator bool() const { return !IsFailure(); }
63
64 private:
66 : object_(heap_object) {}
67
69};
70
71static_assert(sizeof(AllocationResult) == kSystemPointerSize);
72
73} // namespace internal
74} // namespace v8
75
76#endif // V8_HEAP_ALLOCATION_RESULT_H_
bool To(Tagged< T > *obj) const
static AllocationResult Failure()
Tagged< HeapObject > ToObjectChecked() const
Tagged< HeapObject > ToObject() const
AllocationResult(Tagged< HeapObject > heap_object)
static AllocationResult FromObject(Tagged< HeapObject > heap_object)
V8_INLINE constexpr bool is_null() const
Definition tagged.h:502
constexpr int kSystemPointerSize
Definition globals.h:410
Tagged< To > Cast(Tagged< From > value, const v8::SourceLocation &loc=INIT_SOURCE_LOCATION_IN_DEBUG)
Definition casting.h:150
#define CHECK(condition)
Definition logging.h:124
#define DCHECK(condition)
Definition logging.h:482