1 /** 2 * Copyright (c) 2021-2024 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 ark::mem { 24 25 /// Represent a set of regions grouped by type. 26 class CollectionSet { 27 public: CollectionSet()28 CollectionSet() 29 { 30 tenuredBegin_ = 0; 31 humongousBegin_ = 0; 32 } 33 34 template <typename Container> CollectionSet(const Container & set)35 explicit CollectionSet(const Container &set) : collectionSet_(set.begin(), set.end()) 36 { 37 tenuredBegin_ = collectionSet_.size(); 38 humongousBegin_ = tenuredBegin_; 39 } 40 CollectionSet(PandaVector<Region * > && youngRegions)41 explicit CollectionSet(PandaVector<Region *> &&youngRegions) : collectionSet_(youngRegions) 42 { 43 tenuredBegin_ = collectionSet_.size(); 44 humongousBegin_ = tenuredBegin_; 45 } 46 47 ~CollectionSet() = default; 48 AddRegion(Region * region)49 void AddRegion(Region *region) 50 { 51 ASSERT(region->HasFlag(RegionFlag::IS_OLD)); 52 collectionSet_.push_back(region); 53 if (!region->HasFlag(RegionFlag::IS_LARGE_OBJECT) && humongousBegin_ != collectionSet_.size()) { 54 std::swap(collectionSet_[humongousBegin_], collectionSet_.back()); 55 ++humongousBegin_; 56 } 57 } 58 begin()59 auto begin() // NOLINT(readability-identifier-naming) 60 { 61 return collectionSet_.begin(); 62 } 63 begin()64 auto begin() const // NOLINT(readability-identifier-naming) 65 { 66 return collectionSet_.begin(); 67 } 68 end()69 auto end() // NOLINT(readability-identifier-naming) 70 { 71 return collectionSet_.end(); 72 } 73 end()74 auto end() const // NOLINT(readability-identifier-naming) 75 { 76 return collectionSet_.end(); 77 } 78 size()79 size_t size() const // NOLINT(readability-identifier-naming) 80 { 81 return collectionSet_.size(); 82 } 83 empty()84 bool empty() const // NOLINT(readability-identifier-naming) 85 { 86 return collectionSet_.empty(); 87 } 88 clear()89 void clear() // NOLINT(readability-identifier-naming) 90 { 91 collectionSet_.clear(); 92 tenuredBegin_ = 0; 93 humongousBegin_ = 0; 94 } 95 Young()96 auto Young() 97 { 98 return Range<PandaVector<Region *>::iterator>(begin(), begin() + tenuredBegin_); 99 } 100 Young()101 auto Young() const 102 { 103 return Range<PandaVector<Region *>::const_iterator>(begin(), begin() + tenuredBegin_); 104 } 105 Tenured()106 auto Tenured() 107 { 108 return Range<PandaVector<Region *>::iterator>(begin() + tenuredBegin_, begin() + humongousBegin_); 109 } 110 Tenured()111 auto Tenured() const 112 { 113 return Range<PandaVector<Region *>::const_iterator>(begin() + tenuredBegin_, begin() + humongousBegin_); 114 } 115 Humongous()116 auto Humongous() 117 { 118 return Range<PandaVector<Region *>::iterator>(begin() + humongousBegin_, end()); 119 } 120 Humongous()121 auto Humongous() const 122 { 123 return Range<PandaVector<Region *>::const_iterator>(begin() + humongousBegin_, end()); 124 } 125 Movable()126 auto Movable() 127 { 128 return Range<PandaVector<Region *>::iterator>(begin(), begin() + humongousBegin_); 129 } 130 Movable()131 auto Movable() const 132 { 133 return Range<PandaVector<Region *>::const_iterator>(begin(), begin() + humongousBegin_); 134 } 135 136 DEFAULT_COPY_SEMANTIC(CollectionSet); 137 DEFAULT_MOVE_SEMANTIC(CollectionSet); 138 139 private: 140 PandaVector<Region *> collectionSet_; 141 size_t tenuredBegin_; 142 size_t humongousBegin_; 143 }; 144 145 } // namespace ark::mem 146 147 #endif // PANDA_RUNTIME_MEM_GC_G1_COLLECTION_SET_H 148