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 ECMASCRIPT_MEM_ALLOCATOR_INL_H
17 #define ECMASCRIPT_MEM_ALLOCATOR_INL_H
18
19 #include <cstdlib>
20
21 #include "ecmascript/free_object.h"
22 #include "ecmascript/mem/allocator.h"
23 #include "ecmascript/mem/heap.h"
24
25 namespace panda::ecmascript {
BumpPointerAllocator(uintptr_t begin,uintptr_t end)26 BumpPointerAllocator::BumpPointerAllocator(uintptr_t begin, uintptr_t end) : begin_(begin), top_(begin), end_(end) {}
27
Reset()28 void BumpPointerAllocator::Reset()
29 {
30 begin_ = 0;
31 top_ = 0;
32 end_ = 0;
33 }
34
Reset(uintptr_t begin,uintptr_t end)35 void BumpPointerAllocator::Reset(uintptr_t begin, uintptr_t end)
36 {
37 begin_ = begin;
38 top_ = begin;
39 end_ = end;
40 ASAN_POISON_MEMORY_REGION(reinterpret_cast<void *>(top_), (end_ - top_));
41 }
42
Reset(uintptr_t begin,uintptr_t end,uintptr_t top)43 void BumpPointerAllocator::Reset(uintptr_t begin, uintptr_t end, uintptr_t top)
44 {
45 begin_ = begin;
46 top_ = top;
47 end_ = end;
48 ASAN_POISON_MEMORY_REGION(reinterpret_cast<void *>(top_), (end_ - top_));
49 }
50
ResetTopPointer(uintptr_t top)51 void BumpPointerAllocator::ResetTopPointer(uintptr_t top)
52 {
53 top_ = top;
54 }
55
Allocate(size_t size)56 uintptr_t BumpPointerAllocator::Allocate(size_t size)
57 {
58 ASSERT(size != 0);
59 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
60 if (UNLIKELY(top_ + size > end_)) {
61 return 0;
62 }
63 uintptr_t result = top_;
64 // It need to mark unpoison when object being allocated.
65 ASAN_UNPOISON_MEMORY_REGION(reinterpret_cast<void *>(result), size);
66 top_ += size;
67 return result;
68 }
69
FreeListAllocator(Heap * heap)70 FreeListAllocator::FreeListAllocator(Heap *heap) : heap_(heap)
71 {
72 freeList_ = std::make_unique<FreeObjectList>();
73 }
74
Initialize(Region * region)75 void FreeListAllocator::Initialize(Region *region)
76 {
77 bpAllocator_.Reset(region->GetBegin(), region->GetEnd());
78 }
79
Reset(Heap * heap)80 void FreeListAllocator::Reset(Heap *heap)
81 {
82 heap_ = heap;
83 freeList_ = std::make_unique<FreeObjectList>();
84 FreeBumpPoint();
85 }
86
AddFree(Region * region)87 void FreeListAllocator::AddFree(Region *region)
88 {
89 auto begin = region->GetBegin();
90 auto end = region->GetEnd();
91 FreeBumpPoint();
92 bpAllocator_.Reset(begin, end);
93 }
94
Allocate(size_t size)95 uintptr_t FreeListAllocator::Allocate(size_t size)
96 {
97 auto ret = bpAllocator_.Allocate(size);
98 if (LIKELY(ret != 0)) {
99 allocationSizeAccumulator_ += size;
100 return ret;
101 }
102 FreeObject *object = freeList_->Allocate(size);
103 if (object != nullptr) {
104 ret = Allocate(object, size);
105 }
106 return ret;
107 }
108
Allocate(FreeObject * object,size_t size)109 uintptr_t FreeListAllocator::Allocate(FreeObject *object, size_t size)
110 {
111 uintptr_t begin = object->GetBegin();
112 uintptr_t end = object->GetEnd();
113 uintptr_t remainSize = end - begin - size;
114 ASSERT(remainSize >= 0);
115 // Keep a longest freeObject between bump-pointer and free object that just allocated
116 allocationSizeAccumulator_ += size;
117 if (remainSize <= bpAllocator_.Available()) {
118 Free(begin + size, remainSize);
119 return begin;
120 } else {
121 FreeBumpPoint();
122 bpAllocator_.Reset(begin, end);
123 auto ret = bpAllocator_.Allocate(size);
124 return ret;
125 }
126 }
127
FreeBumpPoint()128 void FreeListAllocator::FreeBumpPoint()
129 {
130 auto begin = bpAllocator_.GetTop();
131 auto size = bpAllocator_.Available();
132 bpAllocator_.Reset();
133 Free(begin, size);
134 }
135
FillBumpPointer()136 void FreeListAllocator::FillBumpPointer()
137 {
138 size_t size = bpAllocator_.Available();
139 if (size != 0) {
140 FreeObject::FillFreeObject(heap_->GetEcmaVM(), bpAllocator_.GetTop(), size);
141 }
142 }
143
ResetBumpPointer(uintptr_t begin,uintptr_t end,uintptr_t top)144 void FreeListAllocator::ResetBumpPointer(uintptr_t begin, uintptr_t end, uintptr_t top)
145 {
146 bpAllocator_.Reset(begin, end, top);
147 }
148
ResetTopPointer(uintptr_t top)149 void FreeListAllocator::ResetTopPointer(uintptr_t top)
150 {
151 bpAllocator_.ResetTopPointer(top);
152 }
153
154 // The object will be marked with poison after being put into the freelist when is_asan is true.
Free(uintptr_t begin,size_t size,bool isAdd)155 void FreeListAllocator::Free(uintptr_t begin, size_t size, bool isAdd)
156 {
157 ASSERT(heap_ != nullptr);
158 ASSERT(size >= 0);
159 if (size != 0) {
160 FreeObject::FillFreeObject(heap_->GetEcmaVM(), begin, size);
161 ASAN_UNPOISON_MEMORY_REGION(reinterpret_cast<void *>(begin), size);
162 freeList_->Free(begin, size, isAdd);
163 ASAN_POISON_MEMORY_REGION(reinterpret_cast<void *>(begin), size);
164 }
165 }
166
LookupSuitableFreeObject(size_t size)167 uintptr_t FreeListAllocator::LookupSuitableFreeObject(size_t size)
168 {
169 auto freeObject = freeList_->LookupSuitableFreeObject(size);
170 if (freeObject != nullptr) {
171 return freeObject->GetBegin();
172 }
173 return 0;
174 }
175
RebuildFreeList()176 void FreeListAllocator::RebuildFreeList()
177 {
178 bpAllocator_.Reset();
179 freeList_->Rebuild();
180 }
181
CollectFreeObjectSet(Region * region)182 inline void FreeListAllocator::CollectFreeObjectSet(Region *region)
183 {
184 region->EnumerateFreeObjectSets([&](FreeObjectSet *set) {
185 if (set == nullptr || set->Empty()) {
186 return;
187 }
188 freeList_->AddSet(set);
189 });
190 freeList_->IncreaseWastedSize(region->GetWastedSize());
191 }
192
MatchFreeObjectSet(Region * region,size_t size)193 inline bool FreeListAllocator::MatchFreeObjectSet(Region *region, size_t size)
194 {
195 bool ret = false;
196 region->REnumerateFreeObjectSets([&](FreeObjectSet *set) {
197 if (set == nullptr || set->Empty()) {
198 return true;
199 }
200 ret = freeList_->MatchFreeObjectInSet(set, size);
201 return false;
202 });
203 return ret;
204 }
205
DetachFreeObjectSet(Region * region)206 inline void FreeListAllocator::DetachFreeObjectSet(Region *region)
207 {
208 region->EnumerateFreeObjectSets([&](FreeObjectSet *set) {
209 if (set == nullptr || set->Empty()) {
210 return;
211 }
212 freeList_->RemoveSet(set);
213 });
214 freeList_->DecreaseWastedSize(region->GetWastedSize());
215 }
216
GetAvailableSize()217 size_t FreeListAllocator::GetAvailableSize() const
218 {
219 return freeList_->GetFreeObjectSize() + bpAllocator_.Available();
220 }
221
GetWastedSize()222 size_t FreeListAllocator::GetWastedSize() const
223 {
224 return freeList_->GetWastedSize();
225 }
226 } // namespace panda::ecmascript
227 #endif // ECMASCRIPT_MEM_ALLOCATOR_INL_H
228