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 #ifndef PANDA_MEM_GC_G1_REM_SET_H 17 #define PANDA_MEM_GC_G1_REM_SET_H 18 19 #include "runtime/mem/gc/card_table.h" 20 21 namespace panda::mem { 22 23 namespace test { 24 class RemSetTest; 25 } // namespace test 26 27 using CardPtr = CardTable::CardPtr; 28 using CardList = PandaVector<CardPtr>; 29 30 class RemSetLockConfig { 31 public: 32 using CommonLock = os::memory::RecursiveMutex; 33 using DummyLock = os::memory::DummyLock; 34 }; 35 36 class Region; 37 38 /** 39 * \brief Set in the Region. To record the regions and cards reference to this region. 40 */ 41 template <typename LockConfigT = RemSetLockConfig::CommonLock> 42 class RemSet { 43 public: 44 explicit RemSet(Region *region, CardTable *card_table); 45 46 ~RemSet(); 47 48 NO_COPY_SEMANTIC(RemSet); 49 NO_MOVE_SEMANTIC(RemSet); 50 51 template <bool need_lock = true> 52 void AddRef(const ObjectHeader *from_obj_addr); 53 54 template <bool need_lock = true, typename ObjectVisitor> 55 void VisitMarkedCards(const ObjectVisitor &object_visitor); 56 57 template <bool need_lock = true, typename CardVisitor> 58 void ProceedMarkedCards(const CardVisitor &card_visitor); 59 60 void Clear(); 61 GetRegion()62 Region *GetRegion() 63 { 64 return region_; 65 } 66 67 template <bool need_lock = true> 68 CardList *GetCardList(Region *region); 69 70 template <bool need_lock = true> 71 static void InvalidateRegion(Region *invalid_region); 72 73 /** 74 * Used in the barrier. Record the reference from the region of obj_addr to the region of value_addr. 75 * @param obj_addr - address of the object 76 * @param value_addr - address of the reference in the field 77 */ 78 template <bool need_lock = true> 79 static void AddRefWithAddr(const ObjectHeader *obj_addr, const ObjectHeader *value_addr); 80 81 /** 82 * Used in the barrier. Record the reference from the region of addr to the region of the reference in it's fields. 83 * @param addr - address of the object 84 */ 85 static void TraverseObjectToAddRef(const void *addr); 86 87 void Dump(std::ostream &out); 88 89 private: 90 friend class test::RemSetTest; 91 92 // used for testing only SetCardTable(CardTable * card_table)93 void SetCardTable(CardTable *card_table) 94 { 95 card_table_ = card_table; 96 } 97 98 CardPtr GetCardPtr(const void *addr); 99 MemRange GetMemoryRange(CardPtr card); 100 template <bool need_lock> 101 PandaUnorderedSet<Region *> *GetRefRegions(); 102 template <bool need_lock> 103 void AddRefRegion(Region *region); 104 template <bool need_lock> 105 void RemoveFromRegion(Region *region); 106 template <bool need_lock> 107 void RemoveRefRegion(Region *region); 108 109 Region *region_; 110 LockConfigT rem_set_lock_; 111 // TODO(alovkov): make value a Set? 112 PandaUnorderedMap<Region *, CardList *> regions_; 113 PandaUnorderedSet<Region *> ref_regions_; 114 InternalAllocatorPtr allocator_; 115 116 CardTable *card_table_; 117 }; 118 } // namespace panda::mem 119 #endif // PANDA_MEM_GC_G1_REM_SET_H 120