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 
11 namespace cppgc {
12 namespace subtle {
13 
14 // static
IsGarbageCollectionAllowed( cppgc::HeapHandle& heap_handle)15 bool 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
Enter(cppgc::HeapHandle& heap_handle)22 void 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
Leave(cppgc::HeapHandle& heap_handle)28 void 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 
DisallowGarbageCollectionScope( cppgc::HeapHandle& heap_handle)34 DisallowGarbageCollectionScope::DisallowGarbageCollectionScope(
35     cppgc::HeapHandle& heap_handle)
36     : heap_handle_(heap_handle) {
37   Enter(heap_handle);
38 }
39 
~DisallowGarbageCollectionScope()40 DisallowGarbageCollectionScope::~DisallowGarbageCollectionScope() {
41   Leave(heap_handle_);
42 }
43 
44 // static
Enter(cppgc::HeapHandle& heap_handle)45 void 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
Leave(cppgc::HeapHandle& heap_handle)51 void 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 
NoGarbageCollectionScope( cppgc::HeapHandle& heap_handle)57 NoGarbageCollectionScope::NoGarbageCollectionScope(
58     cppgc::HeapHandle& heap_handle)
59     : heap_handle_(heap_handle) {
60   Enter(heap_handle);
61 }
62 
~NoGarbageCollectionScope()63 NoGarbageCollectionScope::~NoGarbageCollectionScope() { Leave(heap_handle_); }
64 
65 }  // namespace subtle
66 }  // namespace cppgc
67