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_REF_CACHE_BUILDER_H 16 #define PANDA_RUNTIME_MEM_GC_G1_REF_CACHE_BUILDER_H 17 18 #include "runtime/mem/gc/g1/g1-gc.h" 19 20 namespace panda::mem { 21 /** 22 * Gets reference fields from an object and puts it to the ref collection. 23 * The ref collection has limited size. If there is no room in the ref collection 24 * the whole object is put to the object collection. 25 */ 26 template <class LanguageConfig> 27 class RefCacheBuilder { 28 using RefVector = typename G1GC<LanguageConfig>::RefVector; 29 30 public: RefCacheBuilder(G1GC<LanguageConfig> * gc,RefVector * refs,PandaVector<ObjectHeader * > * objects,os::memory::Mutex * objects_lock)31 RefCacheBuilder(G1GC<LanguageConfig> *gc, RefVector *refs, PandaVector<ObjectHeader *> *objects, 32 os::memory::Mutex *objects_lock) 33 : gc_(gc), refs_(refs), objects_(objects), objects_lock_(objects_lock) 34 { 35 } 36 operator()37 bool operator()(ObjectHeader *object, ObjectHeader *field, uint32_t offset, bool is_volatile) 38 { 39 if (!gc_->InGCSweepRange(field)) { 40 return true; 41 } 42 if (refs_->size() < refs_->capacity()) { 43 // There is room to store references 44 refs_->push_back(RefInfo(object, offset, is_volatile)); 45 ++refs_in_object_; 46 return true; 47 } 48 // There is no room to store references. 49 // Drop all references of the current object 50 refs_->resize(refs_->size() - refs_in_object_); 51 // Store the whole object 52 os::memory::LockHolder lock(*objects_lock_); 53 objects_->push_back(object); 54 // Stop object traversing. 55 return false; 56 } 57 58 private: 59 G1GC<LanguageConfig> *gc_; 60 size_t refs_in_object_ = 0; 61 RefVector *refs_; 62 PandaVector<ObjectHeader *> *objects_ GUARDED_BY(objects_lock_); 63 os::memory::Mutex *objects_lock_; 64 }; 65 } // namespace panda::mem 66 #endif // PANDA_RUNTIME_MEM_GC_G1_REF_CACHE_BUILDER_H 67