v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
managed-inl.h
Go to the documentation of this file.
1// Copyright 2021 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_OBJECTS_MANAGED_INL_H_
6#define V8_OBJECTS_MANAGED_INL_H_
7
9// Include the non-inl header before the rest of the headers.
10
12
13namespace v8::internal {
14
15namespace detail {
16// Called by either isolate shutdown or the {ManagedObjectFinalizer} in order
17// to actually delete the shared pointer and decrement the shared refcount.
18template <typename CppType>
19static void Destructor(void* ptr) {
20 auto shared_ptr_ptr = reinterpret_cast<std::shared_ptr<CppType>*>(ptr);
21 delete shared_ptr_ptr;
22}
23} // namespace detail
24
25// static
26template <class CppType>
28 Isolate* isolate, size_t estimated_size,
29 std::shared_ptr<CppType> shared_ptr, AllocationType allocation_type) {
31 static_assert(IsManagedExternalPointerType(kTag));
32 auto destructor = new ManagedPtrDestructor(
33 estimated_size, new std::shared_ptr<CppType>{std::move(shared_ptr)},
36 reinterpret_cast<v8::Isolate*>(isolate), estimated_size);
38 Cast<Managed<CppType>>(isolate->factory()->NewForeign<kTag>(
39 reinterpret_cast<Address>(destructor), allocation_type));
40 IndirectHandle<Object> global_handle =
41 isolate->global_handles()->Create(*handle);
42 destructor->global_handle_location_ = global_handle.location();
43 GlobalHandles::MakeWeak(destructor->global_handle_location_, destructor,
46 isolate->RegisterManagedPtrDestructor(destructor);
47 return handle;
48}
49
50// static
51template <class CppType>
53 Isolate* isolate, size_t estimated_size,
54 std::shared_ptr<CppType> shared_ptr) {
55 auto destructor = new ManagedPtrDestructor(
56 estimated_size, new std::shared_ptr<CppType>{std::move(shared_ptr)},
59 reinterpret_cast<v8::Isolate*>(isolate), estimated_size);
61 Cast<TrustedManaged<CppType>>(isolate->factory()->NewTrustedForeign(
62 reinterpret_cast<Address>(destructor)));
63 IndirectHandle<Object> global_handle =
64 isolate->global_handles()->Create(*handle);
65 destructor->global_handle_location_ = global_handle.location();
66 GlobalHandles::MakeWeak(destructor->global_handle_location_, destructor,
69 isolate->RegisterManagedPtrDestructor(destructor);
70 return handle;
71}
72
73} // namespace v8::internal
74
75#endif // V8_OBJECTS_MANAGED_INL_H_
void Increase(Isolate *isolate, size_t size)
Definition api.cc:12371
static void MakeWeak(Address *location, void *parameter, WeakCallbackInfo< void >::Callback weak_callback, v8::WeakCallbackType type)
V8_INLINE Address * location() const
Definition handles.h:80
static DirectHandle< Managed< CppType > > From(Isolate *isolate, size_t estimated_size, std::shared_ptr< CppType > shared_ptr, AllocationType allocation_type=AllocationType::kYoung)
Definition managed-inl.h:27
static DirectHandle< TrustedManaged< CppType > > From(Isolate *isolate, size_t estimated_size, std::shared_ptr< CppType > shared_ptr)
Definition managed-inl.h:52
static void Destructor(void *ptr)
Definition managed-inl.h:19
V8_INLINE IndirectHandle< T > handle(Tagged< T > object, Isolate *isolate)
Definition handles-inl.h:72
static V8_INLINE constexpr bool IsManagedExternalPointerType(ExternalPointerTagRange tag_range)
void ManagedObjectFinalizer(const v8::WeakCallbackInfo< void > &data)
Definition managed.cc:33
Tagged< To > Cast(Tagged< From > value, const v8::SourceLocation &loc=INIT_SOURCE_LOCATION_IN_DEBUG)
Definition casting.h:150
V8_NO_UNIQUE_ADDRESS ExternalMemoryAccounter external_memory_accounter_
Definition managed.h:72