• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #include "base/common.h"
16 #ifdef _WIN64
17 #include <errhandlingapi.h>
18 #include <handleapi.h>
19 #include <memoryapi.h>
20 #endif
21 
22 #include "common_components/base/immortal_wrapper.h"
23 #include "common_components/heap/allocator/region_desc.h"
24 #include "common_components/heap/collector/region_bitmap.h"
25 #include "common_components/heap/collector/copy_data_manager.h"
26 #include "common_components/platform/os.h"
27 
28 namespace common {
29 
30 static ImmortalWrapper<HeapBitmapManager> forwardDataManager;
GetHeapBitmapManager()31 HeapBitmapManager& HeapBitmapManager::GetHeapBitmapManager() { return *forwardDataManager; }
32 
InitializeHeapBitmap()33 void HeapBitmapManager::InitializeHeapBitmap()
34 {
35     DCHECK_CC(!initialized);
36     size_t maxHeapBytes = Heap::GetHeap().GetMaxCapacity();
37     size_t heapBitmapSize = RoundUp(GetHeapBitmapSize(maxHeapBytes), COMMON_PAGE_SIZE);
38     allHeapBitmapSize_ = heapBitmapSize;
39 
40 #ifdef _WIN64
41     void* startAddress = VirtualAlloc(NULL, allHeapBitmapSize_, MEM_RESERVE, PAGE_READWRITE);
42     if (startAddress == NULL) { //LCOV_EXCL_BR_LINE
43         LOG_COMMON(FATAL) << "failed to initialize HeapBitmapManager";
44         UNREACHABLE_CC();
45     }
46 #else
47     void* startAddress = mmap(nullptr, allHeapBitmapSize_, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
48     if (startAddress == MAP_FAILED) { //LCOV_EXCL_BR_LINE
49         LOG_COMMON(FATAL) << "failed to initialize HeapBitmapManager";
50         UNREACHABLE_CC();
51     } else {
52 #ifndef __APPLE__
53         (void)madvise(startAddress, allHeapBitmapSize_, MADV_NOHUGEPAGE);
54 #endif
55     }
56 #endif
57 
58     heapBitmapStart_ = reinterpret_cast<uintptr_t>(startAddress);
59     heapBitmap_[0].InitializeMemory(heapBitmapStart_, heapBitmapSize, regionUnitCount_);
60 
61     os::PrctlSetVMA(startAddress, allHeapBitmapSize_, "ArkTS Heap CMCGC HeapBitMap");
62     initialized = true;
63 }
64 
DestroyHeapBitmap()65 void HeapBitmapManager::DestroyHeapBitmap()
66 {
67 #ifdef _WIN64
68     if (!VirtualFree(reinterpret_cast<void*>(heapBitmapStart_), 0, MEM_RELEASE)) {
69         LOG_COMMON(ERROR) << "VirtualFree error for HeapBitmapManager";
70     }
71 #else
72     if (munmap(reinterpret_cast<void*>(heapBitmapStart_), allHeapBitmapSize_) != 0) {
73         LOG_COMMON(ERROR) << "munmap error for HeapBitmapManager";
74     }
75 #endif
76     initialized = false;
77 }
78 
79 } // namespace common
80