11cb0ef41Sopenharmony_ci// Copyright 2020 the V8 project authors. All rights reserved. 21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be 31cb0ef41Sopenharmony_ci// found in the LICENSE file. 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ci#ifndef V8_HEAP_CPPGC_PREFINALIZER_HANDLER_H_ 61cb0ef41Sopenharmony_ci#define V8_HEAP_CPPGC_PREFINALIZER_HANDLER_H_ 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ci#include <utility> 91cb0ef41Sopenharmony_ci#include <vector> 101cb0ef41Sopenharmony_ci 111cb0ef41Sopenharmony_ci#include "include/cppgc/prefinalizer.h" 121cb0ef41Sopenharmony_ci 131cb0ef41Sopenharmony_cinamespace cppgc { 141cb0ef41Sopenharmony_cinamespace internal { 151cb0ef41Sopenharmony_ci 161cb0ef41Sopenharmony_ciclass HeapBase; 171cb0ef41Sopenharmony_ci 181cb0ef41Sopenharmony_cistruct PreFinalizer final { 191cb0ef41Sopenharmony_ci using Callback = PrefinalizerRegistration::Callback; 201cb0ef41Sopenharmony_ci 211cb0ef41Sopenharmony_ci void* object; 221cb0ef41Sopenharmony_ci Callback callback; 231cb0ef41Sopenharmony_ci 241cb0ef41Sopenharmony_ci bool operator==(const PreFinalizer& other) const; 251cb0ef41Sopenharmony_ci}; 261cb0ef41Sopenharmony_ci 271cb0ef41Sopenharmony_ciclass PreFinalizerHandler final { 281cb0ef41Sopenharmony_ci public: 291cb0ef41Sopenharmony_ci explicit PreFinalizerHandler(HeapBase& heap); 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_ci void RegisterPrefinalizer(PreFinalizer pre_finalizer); 321cb0ef41Sopenharmony_ci 331cb0ef41Sopenharmony_ci void InvokePreFinalizers(); 341cb0ef41Sopenharmony_ci 351cb0ef41Sopenharmony_ci bool IsInvokingPreFinalizers() const { return is_invoking_; } 361cb0ef41Sopenharmony_ci 371cb0ef41Sopenharmony_ci void NotifyAllocationInPrefinalizer(size_t); 381cb0ef41Sopenharmony_ci size_t ExtractBytesAllocatedInPrefinalizers() { 391cb0ef41Sopenharmony_ci return std::exchange(bytes_allocated_in_prefinalizers, 0); 401cb0ef41Sopenharmony_ci } 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_ci private: 431cb0ef41Sopenharmony_ci // Checks that the current thread is the thread that created the heap. 441cb0ef41Sopenharmony_ci bool CurrentThreadIsCreationThread(); 451cb0ef41Sopenharmony_ci 461cb0ef41Sopenharmony_ci // Pre-finalizers are called in the reverse order in which they are 471cb0ef41Sopenharmony_ci // registered by the constructors (including constructors of Mixin 481cb0ef41Sopenharmony_ci // objects) for an object, by processing the ordered_pre_finalizers_ 491cb0ef41Sopenharmony_ci // back-to-front. 501cb0ef41Sopenharmony_ci std::vector<PreFinalizer> ordered_pre_finalizers_; 511cb0ef41Sopenharmony_ci std::vector<PreFinalizer>* current_ordered_pre_finalizers_; 521cb0ef41Sopenharmony_ci 531cb0ef41Sopenharmony_ci HeapBase& heap_; 541cb0ef41Sopenharmony_ci bool is_invoking_ = false; 551cb0ef41Sopenharmony_ci#ifdef DEBUG 561cb0ef41Sopenharmony_ci int creation_thread_id_; 571cb0ef41Sopenharmony_ci#endif 581cb0ef41Sopenharmony_ci 591cb0ef41Sopenharmony_ci // Counter of bytes allocated during prefinalizers. 601cb0ef41Sopenharmony_ci size_t bytes_allocated_in_prefinalizers = 0u; 611cb0ef41Sopenharmony_ci}; 621cb0ef41Sopenharmony_ci 631cb0ef41Sopenharmony_ci} // namespace internal 641cb0ef41Sopenharmony_ci} // namespace cppgc 651cb0ef41Sopenharmony_ci 661cb0ef41Sopenharmony_ci#endif // V8_HEAP_CPPGC_PREFINALIZER_HANDLER_H_ 67