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 #include "ecmascript/mem/space-inl.h"
17
18 #include "ecmascript/ecma_vm.h"
19 #include "ecmascript/mem/heap_region_allocator.h"
20 #include "ecmascript/mem/mem_controller.h"
21 #include "ecmascript/mem/region-inl.h"
22 #include "ecmascript/mem/space.h"
23
24 namespace panda::ecmascript {
Space(HeapRegionAllocator * heapRegionAllocator,MemSpaceType spaceType,size_t initialCapacity,size_t maximumCapacity)25 Space::Space(HeapRegionAllocator *heapRegionAllocator,
26 MemSpaceType spaceType, size_t initialCapacity,
27 size_t maximumCapacity)
28 : heapRegionAllocator_(heapRegionAllocator),
29 spaceType_(spaceType),
30 initialCapacity_(initialCapacity),
31 maximumCapacity_(maximumCapacity),
32 committedSize_(0)
33 {
34 }
35
Destroy()36 void Space::Destroy()
37 {
38 ReclaimRegions();
39 }
40
ReclaimRegions()41 void Space::ReclaimRegions()
42 {
43 EnumerateRegions([this](Region *current) { ClearAndFreeRegion(current); });
44 regionList_.Clear();
45 committedSize_ = 0;
46 }
47
ClearAndFreeRegion(Region * region)48 void Space::ClearAndFreeRegion(Region *region)
49 {
50 LOG_ECMA_MEM(DEBUG) << "Clear region from:" << region << " to " << ToSpaceTypeName(spaceType_);
51 region->DeleteCrossRegionRSet();
52 region->DeleteOldToNewRSet();
53 region->DeleteSweepingRSet();
54 DecreaseCommitted(region->GetCapacity());
55 DecreaseObjectSize(region->GetSize());
56 if (spaceType_ == MemSpaceType::OLD_SPACE || spaceType_ == MemSpaceType::NON_MOVABLE ||
57 spaceType_ == MemSpaceType::MACHINE_CODE_SPACE || spaceType_ == MemSpaceType::LOCAL_SPACE ||
58 spaceType_ == MemSpaceType::APPSPAWN_SPACE) {
59 region->DestroyFreeObjectSets();
60 }
61 heapRegionAllocator_->FreeRegion(region);
62 }
63
HugeObjectSpace(Heap * heap,HeapRegionAllocator * heapRegionAllocator,size_t initialCapacity,size_t maximumCapacity)64 HugeObjectSpace::HugeObjectSpace(Heap *heap, HeapRegionAllocator *heapRegionAllocator,
65 size_t initialCapacity, size_t maximumCapacity)
66 : Space(heapRegionAllocator, MemSpaceType::HUGE_OBJECT_SPACE, initialCapacity, maximumCapacity),
67 heap_(heap)
68 {
69 }
70
Allocate(size_t objectSize,JSThread * thread)71 uintptr_t HugeObjectSpace::Allocate(size_t objectSize, JSThread *thread)
72 {
73 // In HugeObject allocation, we have a revervation of 8 bytes for markBitSet in objectSize.
74 // In case Region is not aligned by 16 bytes, HUGE_OBJECT_BITSET_SIZE is 8 bytes more.
75 size_t alignedSize = AlignUp(objectSize + sizeof(Region) + HUGE_OBJECT_BITSET_SIZE, PANDA_POOL_ALIGNMENT_IN_BYTES);
76 if (heap_->OldSpaceExceedCapacity(alignedSize)) {
77 LOG_ECMA_MEM(INFO) << "Committed size " << committedSize_ << " of huge object space is too big.";
78 return 0;
79 }
80 Region *region = heapRegionAllocator_->AllocateAlignedRegion(this, alignedSize, thread);
81 AddRegion(region);
82 // It need to mark unpoison when huge object being allocated.
83 ASAN_UNPOISON_MEMORY_REGION(reinterpret_cast<void *>(region->GetBegin()), objectSize);
84 return region->GetBegin();
85 }
86
Sweep()87 void HugeObjectSpace::Sweep()
88 {
89 Region *currentRegion = GetRegionList().GetFirst();
90 while (currentRegion != nullptr) {
91 Region *next = currentRegion->GetNext();
92 bool isMarked = false;
93 currentRegion->IterateAllMarkedBits([&isMarked]([[maybe_unused]] void *mem) { isMarked = true; });
94 if (!isMarked) {
95 GetRegionList().RemoveNode(currentRegion);
96 hugeNeedFreeList_.AddNode(currentRegion);
97 }
98 currentRegion = next;
99 }
100 }
101
GetHeapObjectSize() const102 size_t HugeObjectSpace::GetHeapObjectSize() const
103 {
104 return committedSize_;
105 }
106
IterateOverObjects(const std::function<void (TaggedObject * object)> & objectVisitor) const107 void HugeObjectSpace::IterateOverObjects(const std::function<void(TaggedObject *object)> &objectVisitor) const
108 {
109 EnumerateRegions([&](Region *region) {
110 uintptr_t curPtr = region->GetBegin();
111 objectVisitor(reinterpret_cast<TaggedObject *>(curPtr));
112 });
113 }
114
ReclaimHugeRegion()115 void HugeObjectSpace::ReclaimHugeRegion()
116 {
117 if (hugeNeedFreeList_.IsEmpty()) {
118 return;
119 }
120 do {
121 Region *last = hugeNeedFreeList_.PopBack();
122 ClearAndFreeRegion(last);
123 } while (!hugeNeedFreeList_.IsEmpty());
124 }
125 } // namespace panda::ecmascript
126