1 /* 2 * Copyright (c) 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 ECMASCRIPT_MEM_LINEAR_SPACE_H 17 #define ECMASCRIPT_MEM_LINEAR_SPACE_H 18 19 #include "ecmascript/mem/space-inl.h" 20 21 namespace panda::ecmascript { 22 class LinearSpace : public Space { 23 public: 24 explicit LinearSpace(Heap *heap, MemSpaceType type, size_t initialCapacity, size_t maximumCapacity); 25 NO_COPY_SEMANTIC(LinearSpace); 26 NO_MOVE_SEMANTIC(LinearSpace); 27 uintptr_t Allocate(size_t size, bool isPromoted = false); 28 bool Expand(bool isPromoted); 29 void Stop(); 30 void ResetAllocator(); 31 void IterateOverObjects(const std::function<void(TaggedObject *object)> &objectVisitor) const; DecreaseSurvivalObjectSize(size_t objSize)32 void DecreaseSurvivalObjectSize(size_t objSize) 33 { 34 survivalObjectSize_ -= objSize; 35 } GetAllocationTopAddress()36 const uintptr_t *GetAllocationTopAddress() 37 { 38 return allocator_.GetTopAddress(); 39 } GetAllocationEndAddress()40 const uintptr_t *GetAllocationEndAddress() 41 { 42 return allocator_.GetEndAddress(); 43 } 44 protected: 45 Heap *heap_ {nullptr}; 46 47 BumpPointerAllocator allocator_; 48 size_t overShootSize_ {0}; 49 size_t allocateAfterLastGC_ {0}; 50 size_t survivalObjectSize_ {0}; 51 uintptr_t waterLine_ {0}; 52 }; 53 54 class SemiSpace : public LinearSpace { 55 public: 56 explicit SemiSpace(Heap *heap, size_t initialCapacity, size_t maximumCapacity); 57 ~SemiSpace() override = default; 58 NO_COPY_SEMANTIC(SemiSpace); 59 NO_MOVE_SEMANTIC(SemiSpace); 60 61 void Initialize() override; 62 void Restart(); 63 64 uintptr_t AllocateSync(size_t size); 65 66 void SetOverShootSize(size_t size); 67 bool AdjustCapacity(size_t allocatedSizeSinceGC); 68 void AdjustNativeLimit(size_t previousNativeSize); 69 void SetWaterLine(); 70 GetWaterLine()71 uintptr_t GetWaterLine() const 72 { 73 return waterLine_; 74 } GetTop()75 uintptr_t GetTop() const 76 { 77 return allocator_.GetTop(); 78 } 79 size_t GetHeapObjectSize() const; 80 size_t GetSurvivalObjectSize() const; 81 size_t GetAllocatedSizeSinceGC(uintptr_t top = 0) const; 82 83 bool SwapRegion(Region *region, SemiSpace *fromSpace); 84 ResetNativeBindingSize()85 void ResetNativeBindingSize() 86 { 87 newSpaceNativeBindingSize_ = 0; 88 } IncreaseNativeBindingSize(size_t size)89 void IncreaseNativeBindingSize(size_t size) 90 { 91 newSpaceNativeBindingSize_ += size; 92 } GetNativeBindingSize()93 size_t GetNativeBindingSize() 94 { 95 return newSpaceNativeBindingSize_; 96 } 97 NativeBindingSizeLargerThanLimit()98 bool NativeBindingSizeLargerThanLimit() 99 { 100 return newSpaceNativeBindingSize_ > newSpaceNativeLimit_; 101 } 102 private: 103 static constexpr int GROWING_FACTOR = 2; 104 os::memory::Mutex lock_; 105 size_t minimumCapacity_; 106 size_t newSpaceNativeLimit_; 107 size_t newSpaceNativeBindingSize_ {0}; 108 }; 109 110 class SnapshotSpace : public LinearSpace { 111 public: 112 explicit SnapshotSpace(Heap *heap, size_t initialCapacity, size_t maximumCapacity); 113 ~SnapshotSpace() override = default; 114 NO_COPY_SEMANTIC(SnapshotSpace); 115 NO_MOVE_SEMANTIC(SnapshotSpace); 116 GetHeapObjectSize()117 size_t GetHeapObjectSize() const 118 { 119 return liveObjectSize_; 120 } 121 IncreaseLiveObjectSize(size_t size)122 void IncreaseLiveObjectSize(size_t size) 123 { 124 liveObjectSize_ += size; 125 } 126 127 private: 128 size_t liveObjectSize_ {0}; 129 }; 130 131 class ReadOnlySpace : public LinearSpace { 132 public: 133 explicit ReadOnlySpace(Heap *heap, size_t initialCapacity, size_t maximumCapacity); 134 ~ReadOnlySpace() override = default; SetReadOnly()135 void SetReadOnly() 136 { 137 auto cb = [](Region *region) { 138 region->SetReadOnlyAndMarked(); 139 }; 140 EnumerateRegions(cb); 141 } 142 ClearReadOnly()143 void ClearReadOnly() 144 { 145 auto cb = [](Region *region) { 146 region->ClearReadOnly(); 147 }; 148 EnumerateRegions(cb); 149 } 150 151 NO_COPY_SEMANTIC(ReadOnlySpace); 152 NO_MOVE_SEMANTIC(ReadOnlySpace); 153 }; 154 } // namespace panda::ecmascript 155 #endif // ECMASCRIPT_MEM_LINEAR_SPACE_H 156