1 /**
2 * Copyright (c) 2021-2022 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 #include "runtime/mem/gc/heap-space-misc/crossing_map_singleton.h"
17
18 #include "libpandabase/utils/logger.h"
19 #include "runtime/include/panda_vm.h"
20 #include "runtime/include/runtime.h"
21 #include "runtime/mem/gc/heap-space-misc/crossing_map.h"
22
23 namespace panda::mem {
24
25 CrossingMap *CrossingMapSingleton ::instance = nullptr;
26 os::memory::Mutex CrossingMapSingleton::mutex; // NOLINT(fuchsia-statically-constructed-objects)
27
Create()28 bool CrossingMapSingleton::Create()
29 {
30 {
31 os::memory::LockHolder<os::memory::Mutex> lock(mutex);
32
33 InternalAllocatorPtr allocator {InternalAllocator<>::GetInternalAllocatorFromRuntime()};
34 if (instance != nullptr) {
35 LOG(FATAL, RUNTIME) << "CrossingMap is created already";
36 return false;
37 }
38 instance = allocator->New<CrossingMap>(allocator, PoolManager::GetMmapMemPool()->GetMinObjectAddress(),
39 PoolManager::GetMmapMemPool()->GetTotalObjectSize());
40 instance->Initialize();
41 }
42 return true;
43 }
44
45 /* static */
IsCreated()46 bool CrossingMapSingleton::IsCreated()
47 {
48 return instance != nullptr;
49 }
50
51 /* static */
Destroy()52 bool CrossingMapSingleton::Destroy()
53 {
54 CrossingMap *temp_instance;
55 {
56 os::memory::LockHolder<os::memory::Mutex> lock(mutex);
57
58 if (instance == nullptr) {
59 return false;
60 }
61 temp_instance = instance;
62 instance = nullptr;
63 }
64 temp_instance->Destroy();
65 InternalAllocatorPtr allocator {InternalAllocator<>::GetInternalAllocatorFromRuntime()};
66 allocator->Delete(temp_instance);
67 return true;
68 }
69
70 /* static */
AddObject(void * obj_addr,size_t obj_size)71 void CrossingMapSingleton::AddObject(void *obj_addr, size_t obj_size)
72 {
73 GetCrossingMap()->AddObject(obj_addr, obj_size);
74 }
75
76 /* static */
RemoveObject(void * obj_addr,size_t obj_size,void * next_obj_addr,void * prev_obj_addr,size_t prev_obj_size)77 void CrossingMapSingleton::RemoveObject(void *obj_addr, size_t obj_size, void *next_obj_addr, void *prev_obj_addr,
78 size_t prev_obj_size)
79 {
80 GetCrossingMap()->RemoveObject(obj_addr, obj_size, next_obj_addr, prev_obj_addr, prev_obj_size);
81 }
82
83 /* static */
FindFirstObject(void * start_addr,void * end_addr)84 void *CrossingMapSingleton::FindFirstObject(void *start_addr, void *end_addr)
85 {
86 return GetCrossingMap()->FindFirstObject(start_addr, end_addr);
87 }
88
89 /* static */
InitializeCrossingMapForMemory(void * start_addr,size_t size)90 void CrossingMapSingleton::InitializeCrossingMapForMemory(void *start_addr, size_t size)
91 {
92 return GetCrossingMap()->InitializeCrossingMapForMemory(start_addr, size);
93 }
94
95 /* static */
RemoveCrossingMapForMemory(void * start_addr,size_t size)96 void CrossingMapSingleton::RemoveCrossingMapForMemory(void *start_addr, size_t size)
97 {
98 return GetCrossingMap()->RemoveCrossingMapForMemory(start_addr, size);
99 }
100
101 /* static */
GetCrossingMapGranularity()102 size_t CrossingMapSingleton::GetCrossingMapGranularity()
103 {
104 return PANDA_CROSSING_MAP_GRANULARITY;
105 }
106
107 /* static */
MarkCardsAsYoung(const MemRange & mem_range)108 void CrossingMapSingleton::MarkCardsAsYoung(const MemRange &mem_range)
109 {
110 auto card_table = Thread::GetCurrent()->GetVM()->GetGC()->GetCardTable();
111 card_table->MarkCardsAsYoung(mem_range);
112 }
113
114 } // namespace panda::mem
115