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#include "include/cppgc/heap-consistency.h"
6
7#include "include/cppgc/heap.h"
8#include "src/base/logging.h"
9#include "src/heap/cppgc/heap-base.h"
10
11namespace cppgc {
12namespace subtle {
13
14// static
15bool DisallowGarbageCollectionScope::IsGarbageCollectionAllowed(
16    cppgc::HeapHandle& heap_handle) {
17  auto& heap_base = internal::HeapBase::From(heap_handle);
18  return !heap_base.in_disallow_gc_scope();
19}
20
21// static
22void DisallowGarbageCollectionScope::Enter(cppgc::HeapHandle& heap_handle) {
23  auto& heap_base = internal::HeapBase::From(heap_handle);
24  heap_base.disallow_gc_scope_++;
25}
26
27// static
28void DisallowGarbageCollectionScope::Leave(cppgc::HeapHandle& heap_handle) {
29  auto& heap_base = internal::HeapBase::From(heap_handle);
30  DCHECK_GT(heap_base.disallow_gc_scope_, 0);
31  heap_base.disallow_gc_scope_--;
32}
33
34DisallowGarbageCollectionScope::DisallowGarbageCollectionScope(
35    cppgc::HeapHandle& heap_handle)
36    : heap_handle_(heap_handle) {
37  Enter(heap_handle);
38}
39
40DisallowGarbageCollectionScope::~DisallowGarbageCollectionScope() {
41  Leave(heap_handle_);
42}
43
44// static
45void NoGarbageCollectionScope::Enter(cppgc::HeapHandle& heap_handle) {
46  auto& heap_base = internal::HeapBase::From(heap_handle);
47  heap_base.no_gc_scope_++;
48}
49
50// static
51void NoGarbageCollectionScope::Leave(cppgc::HeapHandle& heap_handle) {
52  auto& heap_base = internal::HeapBase::From(heap_handle);
53  DCHECK_GT(heap_base.no_gc_scope_, 0);
54  heap_base.no_gc_scope_--;
55}
56
57NoGarbageCollectionScope::NoGarbageCollectionScope(
58    cppgc::HeapHandle& heap_handle)
59    : heap_handle_(heap_handle) {
60  Enter(heap_handle);
61}
62
63NoGarbageCollectionScope::~NoGarbageCollectionScope() { Leave(heap_handle_); }
64
65}  // namespace subtle
66}  // namespace cppgc
67