1 /* 2 * Copyright (c) 2021 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_RUNTIME_MEM_PYGOTE_SPACE_ALLOCATOR_H_ 17 #define PANDA_RUNTIME_MEM_PYGOTE_SPACE_ALLOCATOR_H_ 18 19 #include <functional> 20 #include <memory> 21 #include <vector> 22 23 #include "libpandabase/macros.h" 24 #include "libpandabase/mem/arena.h" 25 #include "libpandabase/mem/mem.h" 26 #include "libpandabase/mem/mem_range.h" 27 #include "runtime/mem/runslots_allocator.h" 28 #include "runtime/mem/gc/bitmap.h" 29 30 namespace panda { 31 class ObjectHeader; 32 } // namespace panda 33 34 namespace panda::mem { 35 36 enum PygoteSpaceState { 37 STATE_PYGOTE_INIT, // before pygote fork, used for small non-movable objects 38 STATE_PYGOTE_FORKING, // at first pygote fork, allocate for copied objects 39 STATE_PYGOTE_FORKED, // after fork, can't allocate/free objects in it 40 }; 41 42 using BitmapList = std::vector<MarkBitmap *>; 43 template <typename AllocConfigT> 44 class PygoteSpaceAllocator final { 45 public: 46 DEFAULT_NOEXCEPT_MOVE_SEMANTIC(PygoteSpaceAllocator); 47 NO_COPY_SEMANTIC(PygoteSpaceAllocator); 48 explicit PygoteSpaceAllocator(MemStatsType *mem_stats); 49 ~PygoteSpaceAllocator(); 50 void *Alloc(size_t size, Alignment alignment = panda::DEFAULT_ALIGNMENT); 51 52 void Free(void *mem); 53 54 void SetState(PygoteSpaceState new_state); 55 GetState()56 PygoteSpaceState GetState() const 57 { 58 return state_; 59 } 60 GetMaxSize()61 static constexpr size_t GetMaxSize() 62 { 63 return RunSlotsAllocator<AllocConfigT>::GetMaxSize(); 64 } 65 CanAllocNonMovable(size_t size,Alignment align)66 bool CanAllocNonMovable(size_t size, Alignment align) 67 { 68 return state_ == STATE_PYGOTE_INIT && AlignUp(size, GetAlignmentInBytes(align)) <= GetMaxSize(); 69 } 70 71 bool ContainObject(const ObjectHeader *object); 72 73 bool IsLive(const ObjectHeader *object); 74 75 void ClearLiveBitmaps(); 76 GetLiveBitmaps()77 BitmapList &GetLiveBitmaps() 78 { 79 // return live_bitmaps as mark bitmap for gc, 80 // gc will update it at end of gc process 81 return live_bitmaps_; 82 } 83 84 template <typename Visitor> 85 void IterateOverObjectsInRange(const Visitor &visitor, void *start, void *end); 86 87 void IterateOverObjects(const ObjectVisitor &object_visitor); 88 89 void VisitAndRemoveAllPools(const MemVisitor &mem_visitor); 90 91 void VisitAndRemoveFreePools(const MemVisitor &mem_visitor); 92 93 void Collect(const GCObjectVisitor &gc_visitor); 94 95 private: 96 void CreateLiveBitmap(void *heap_begin, size_t heap_size); 97 RunSlotsAllocator<AllocConfigT> runslots_alloc_; 98 Arena *arena_ = nullptr; 99 SpaceType space_type_ = SpaceType::SPACE_TYPE_OBJECT; 100 PygoteSpaceState state_ = STATE_PYGOTE_INIT; 101 BitmapList live_bitmaps_; 102 MemStatsType *mem_stats_; 103 }; 104 105 } // namespace panda::mem 106 107 #endif // PANDA_RUNTIME_MEM_PYGOTE_SPACE_ALLOCATOR_H_ 108