• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef ECMASCRIPT_MEM_REGION_INL_H
17 #define ECMASCRIPT_MEM_REGION_INL_H
18 
19 #include "ecmascript/mem/region.h"
20 
21 #include "ecmascript/mem/heap.h"
22 #include "ecmascript/mem/native_area_allocator.h"
23 #include "ecmascript/mem/remembered_set.h"
24 #include "ecmascript/mem/space.h"
25 
26 namespace panda::ecmascript {
SetSpace(Space * space)27 inline void Region::SetSpace(Space *space)
28 {
29     space_ = space;
30 }
31 
CreateRememberedSet()32 RememberedSet *Region::CreateRememberedSet()
33 {
34     auto setSize = RememberedSet::GetSizeInByte(GetCapacity());
35     auto setAddr = const_cast<NativeAreaAllocator *>(heap_->GetNativeAreaAllocator())->Allocate(setSize);
36     uintptr_t setData = ToUintPtr(setAddr);
37     auto ret = new RememberedSet(ToUintPtr(this), GetCapacity(), setData);
38     ret->ClearAllBits();
39     return ret;
40 }
41 
GetOrCreateCrossRegionRememberedSet()42 RememberedSet *Region::GetOrCreateCrossRegionRememberedSet()
43 {
44     if (UNLIKELY(crossRegionSet_ == nullptr)) {
45         os::memory::LockHolder lock(lock_);
46         if (crossRegionSet_ == nullptr) {
47             crossRegionSet_ = CreateRememberedSet();
48         }
49     }
50     return crossRegionSet_;
51 }
52 
GetOrCreateOldToNewRememberedSet()53 RememberedSet *Region::GetOrCreateOldToNewRememberedSet()
54 {
55     if (UNLIKELY(oldToNewSet_ == nullptr)) {
56         os::memory::LockHolder lock(lock_);
57         if (oldToNewSet_ == nullptr) {
58             oldToNewSet_ = CreateRememberedSet();
59         }
60     }
61     return oldToNewSet_;
62 }
63 
InsertCrossRegionRememberedSet(uintptr_t addr)64 void Region::InsertCrossRegionRememberedSet(uintptr_t addr)
65 {
66     auto set = GetOrCreateCrossRegionRememberedSet();
67     set->Insert(addr);
68 }
69 
AtomicInsertCrossRegionRememberedSet(uintptr_t addr)70 void Region::AtomicInsertCrossRegionRememberedSet(uintptr_t addr)
71 {
72     auto set = GetOrCreateCrossRegionRememberedSet();
73     set->AtomicInsert(addr);
74 }
75 
InsertOldToNewRememberedSet(uintptr_t addr)76 void Region::InsertOldToNewRememberedSet(uintptr_t addr)
77 {
78     auto set = GetOrCreateOldToNewRememberedSet();
79     set->Insert(addr);
80 }
81 
AtomicInsertOldToNewRememberedSet(uintptr_t addr)82 void Region::AtomicInsertOldToNewRememberedSet(uintptr_t addr)
83 {
84     auto set = GetOrCreateOldToNewRememberedSet();
85     set->AtomicInsert(addr);
86 }
87 
GetWorkList()88 WorkerHelper *Region::GetWorkList() const
89 {
90     return heap_->GetWorkList();
91 }
92 
DeleteMarkBitmap()93 void Region::DeleteMarkBitmap()
94 {
95     if (markBitmap_ != nullptr) {
96         auto size = RangeBitmap::GetBitMapSizeInByte(GetCapacity());
97         const_cast<NativeAreaAllocator *>(
98             heap_->GetNativeAreaAllocator())->Free(markBitmap_->GetBitMap().Data(), size);
99         delete markBitmap_;
100         markBitmap_ = nullptr;
101     }
102 }
103 
DeleteCrossRegionRememberedSet()104 void Region::DeleteCrossRegionRememberedSet()
105 {
106     if (crossRegionSet_ != nullptr) {
107         auto size = RememberedSet::GetSizeInByte(GetCapacity());
108         const_cast<NativeAreaAllocator *>(heap_->GetNativeAreaAllocator())->Free(
109             crossRegionSet_->GetBitMap().Data(), size);
110         delete crossRegionSet_;
111         crossRegionSet_ = nullptr;
112     }
113 }
114 
DeleteOldToNewRememberedSet()115 void Region::DeleteOldToNewRememberedSet()
116 {
117     if (oldToNewSet_ != nullptr) {
118         auto size = RememberedSet::GetSizeInByte(GetCapacity());
119         const_cast<NativeAreaAllocator *>(heap_->GetNativeAreaAllocator())->Free(
120             oldToNewSet_->GetBitMap().Data(), size);
121         delete oldToNewSet_;
122         oldToNewSet_ = nullptr;
123     }
124 }
125 
ClearMarkBitmap()126 void Region::ClearMarkBitmap()
127 {
128     if (markBitmap_ != nullptr) {
129         markBitmap_->ClearAllBits();
130     }
131 }
132 
ClearCrossRegionRememberedSet()133 void Region::ClearCrossRegionRememberedSet()
134 {
135     if (crossRegionSet_ != nullptr) {
136         crossRegionSet_->ClearAllBits();
137     }
138 }
139 
IsMarking()140 bool Region::IsMarking() const
141 {
142     return !heap_->GetJSThread()->IsReadyToMark();
143 }
144 }  // namespace panda::ecmascript
145 #endif  // ECMASCRIPT_MEM_REGION_INL_H
146