• 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 #ifndef COMMON_COMPONENTS_HEAP_SPACE_TO_SPACE_H
16 #define COMMON_COMPONENTS_HEAP_SPACE_TO_SPACE_H
17 
18 #include <assert.h>
19 #include <list>
20 #include <map>
21 #include <set>
22 #include <thread>
23 #include <vector>
24 
25 #include "common_components/heap/allocator/alloc_util.h"
26 #include "common_components/heap/allocator/allocator.h"
27 #include "common_components/heap/allocator/region_list.h"
28 #include "common_components/heap/allocator/region_manager.h"
29 #include "common_components/heap/space/regional_space.h"
30 #include "common_components/heap/space/from_space.h"
31 #include "common_components/mutator/mutator.h"
32 #if defined(COMMON_SANITIZER_SUPPORT)
33 #include "common_components/base/asan_interface.h"
34 #endif
35 
36 namespace common {
37 class OldSpace;
38 // regions for small-sized movable objects, which may be moved during gc.
39 class ToSpace : public RegionalSpace {
40 public:
ToSpace(RegionManager & regionManager)41     ToSpace(RegionManager& regionManager) : RegionalSpace(regionManager),
42         tlToRegionList_("thread local to-regions"),
43         fullToRegionList_("full to-regions") {}
44 
45     void DumpRegionStats() const;
46 
CollectFixTasks(FixHeapTaskList & taskList)47     void CollectFixTasks(FixHeapTaskList &taskList)
48     {
49         FixHeapWorker::CollectFixHeapTasks(taskList, tlToRegionList_, FIX_TO_REGION);
50         FixHeapWorker::CollectFixHeapTasks(taskList, fullToRegionList_, FIX_TO_REGION);
51     }
52 
AddFullRegion(RegionDesc * region)53     void AddFullRegion(RegionDesc* region)
54     {
55         DCHECK_CC(Heap::GetHeap().IsGcStarted());
56         fullToRegionList_.PrependRegion(region, RegionDesc::RegionType::TO_REGION);
57     }
58 
AddThreadLocalRegion(RegionDesc * region)59     void AddThreadLocalRegion(RegionDesc* region)
60     {
61         tlToRegionList_.PrependRegion(region, RegionDesc::RegionType::TO_REGION);
62     }
63 
HandleFullThreadLocalRegion(RegionDesc * region)64     void HandleFullThreadLocalRegion(RegionDesc* region)
65     {
66         DCHECK_CC(Heap::GetHeap().IsGcStarted());
67         DCHECK_CC(region->IsToRegion());
68         tlToRegionList_.DeleteRegion(region);
69         fullToRegionList_.PrependRegion(region, RegionDesc::RegionType::TO_REGION);
70     }
71 
GetAllocatedSize()72     size_t GetAllocatedSize() const
73     {
74         return tlToRegionList_.GetAllocatedSize(false) + fullToRegionList_.GetAllocatedSize();
75     }
76 
GetSurvivedSize()77     size_t GetSurvivedSize() const
78     {
79         return tlToRegionList_.GetAllocatedSize(false) + fullToRegionList_.GetAllocatedSize();
80     }
81 
GetUsedUnitCount()82     size_t GetUsedUnitCount() const
83     {
84         return tlToRegionList_.GetUnitCount() + fullToRegionList_.GetUnitCount();
85     }
86 
87     void GetPromotedTo(OldSpace& mspace);
88 
ClearAllGCInfo()89     void ClearAllGCInfo()
90     {
91         ClearGCInfo(tlToRegionList_);
92         ClearGCInfo(fullToRegionList_);
93     }
94 
GetTlToRegionList()95     RegionList& GetTlToRegionList() noexcept { return tlToRegionList_; }
96 
GetFullToRegionList()97     RegionList& GetFullToRegionList() noexcept { return fullToRegionList_; }
98 
99 private:
ClearGCInfo(RegionList & list)100     void ClearGCInfo(RegionList& list)
101     {
102         RegionList tmp("temp region list");
103         list.CopyListTo(tmp);
104         tmp.VisitAllRegions([](RegionDesc* region) {
105             region->ClearMarkingCopyLine();
106             region->ClearLiveInfo();
107             region->ResetMarkBit();
108         });
109     }
110 
111     // toRegionList is a list of to-space regions produced by gc threads.
112     // when a region is prepended to this list, the region is probably not full, so the statistics
113     // of this region-list are not reliable and need to be updated.
114     RegionList tlToRegionList_;
115     RegionList fullToRegionList_;
116 };
117 } // namespace common
118 #endif // COMMON_COMPONENTS_HEAP_SPACE_TO_SPACE_H
119