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 RUNTIME_MEM_PANDA_PYGOTE_SPACE_ALLOCATOR_H 16 #define RUNTIME_MEM_PANDA_PYGOTE_SPACE_ALLOCATOR_H 17 18 #include <functional> 19 #include <memory> 20 #include <vector> 21 22 #include "libpandabase/macros.h" 23 #include "libpandabase/mem/arena-inl.h" 24 #include "libpandabase/mem/mem.h" 25 #include "libpandabase/mem/mem_range.h" 26 #include "runtime/mem/runslots_allocator.h" 27 #include "runtime/mem/gc/bitmap.h" 28 #include "runtime/mem/heap_space.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 SetHeapSpace(HeapSpace * heap_space)95 void SetHeapSpace(HeapSpace *heap_space) 96 { 97 heap_space_ = heap_space; 98 } 99 100 private: 101 void CreateLiveBitmap(void *heap_begin, size_t heap_size); 102 RunSlotsAllocator<AllocConfigT> runslots_alloc_; 103 Arena *arena_ = nullptr; 104 SpaceType space_type_ = SpaceType::SPACE_TYPE_OBJECT; 105 PygoteSpaceState state_ = STATE_PYGOTE_INIT; 106 BitmapList live_bitmaps_; 107 MemStatsType *mem_stats_; 108 HeapSpace *heap_space_ {nullptr}; 109 }; 110 111 } // namespace panda::mem 112 113 #endif // RUNTIME_MEM_PANDA_PYGOTE_SPACE_ALLOCATOR_H 114