• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef PANDA_RUNTIME_MEM_GC_G1_COLLECTION_SET_H
16 #define PANDA_RUNTIME_MEM_GC_G1_COLLECTION_SET_H
17 
18 #include "libpandabase/macros.h"
19 #include "libpandabase/utils/range.h"
20 #include "runtime/mem/region_space.h"
21 #include "runtime/include/mem/panda_containers.h"
22 
23 namespace panda::mem {
24 
25 /**
26  * Represent a set of regions grouped by type.
27  */
28 class CollectionSet {
29 public:
CollectionSet()30     CollectionSet()
31     {
32         tenured_begin_ = 0;
33         humongous_begin_ = 0;
34     }
35 
CollectionSet(PandaVector<Region * > && young_regions)36     explicit CollectionSet(PandaVector<Region *> &&young_regions) : collection_set_(young_regions)
37     {
38         tenured_begin_ = collection_set_.size();
39         humongous_begin_ = tenured_begin_;
40     }
41 
42     ~CollectionSet() = default;
43 
AddRegion(Region * region)44     void AddRegion(Region *region)
45     {
46         ASSERT(region->HasFlag(RegionFlag::IS_OLD));
47         collection_set_.push_back(region);
48         if (!region->HasFlag(RegionFlag::IS_LARGE_OBJECT) && humongous_begin_ != collection_set_.size()) {
49             std::swap(collection_set_[humongous_begin_], collection_set_.back());
50             ++humongous_begin_;
51         }
52     }
53 
begin()54     auto begin()
55     {
56         return collection_set_.begin();
57     }
58 
begin()59     auto begin() const
60     {
61         return collection_set_.begin();
62     }
63 
end()64     auto end()
65     {
66         return collection_set_.end();
67     }
68 
end()69     auto end() const
70     {
71         return collection_set_.end();
72     }
73 
size()74     size_t size() const
75     {
76         return collection_set_.size();
77     }
78 
empty()79     bool empty() const
80     {
81         return collection_set_.empty();
82     }
83 
Young()84     auto Young()
85     {
86         return Range<PandaVector<Region *>::iterator>(begin(), begin() + tenured_begin_);
87     }
88 
Young()89     auto Young() const
90     {
91         return Range<PandaVector<Region *>::const_iterator>(begin(), begin() + tenured_begin_);
92     }
93 
Tenured()94     auto Tenured()
95     {
96         return Range<PandaVector<Region *>::iterator>(begin() + tenured_begin_, begin() + humongous_begin_);
97     }
98 
Tenured()99     auto Tenured() const
100     {
101         return Range<PandaVector<Region *>::const_iterator>(begin() + tenured_begin_, begin() + humongous_begin_);
102     }
103 
Humongous()104     auto Humongous()
105     {
106         return Range<PandaVector<Region *>::iterator>(begin() + humongous_begin_, end());
107     }
108 
Humongous()109     auto Humongous() const
110     {
111         return Range<PandaVector<Region *>::const_iterator>(begin() + humongous_begin_, end());
112     }
113 
Movable()114     auto Movable()
115     {
116         return Range<PandaVector<Region *>::iterator>(begin(), begin() + humongous_begin_);
117     }
118 
Movable()119     auto Movable() const
120     {
121         return Range<PandaVector<Region *>::const_iterator>(begin(), begin() + humongous_begin_);
122     }
123 
124     DEFAULT_COPY_SEMANTIC(CollectionSet);
125     DEFAULT_MOVE_SEMANTIC(CollectionSet);
126 
127 private:
128     PandaVector<Region *> collection_set_;
129     size_t tenured_begin_;
130     size_t humongous_begin_;
131 };
132 
133 }  // namespace panda::mem
134 
135 #endif  // PANDA_RUNTIME_MEM_GC_G1_COLLECTION_SET_H
136