• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_TLAB_ALLOCATOR_INL_H
17 #define ECMASCRIPT_MEM_TLAB_ALLOCATOR_INL_H
18 
19 #include "ecmascript/mem/tlab_allocator.h"
20 
21 #include "ecmascript/free_object.h"
22 #include "ecmascript/mem/full_gc.h"
23 
24 namespace panda::ecmascript {
25 static constexpr size_t MIN_BUFFER_SIZE = 31_KB;
26 static constexpr size_t SMALL_OBJECT_SIZE = 8_KB;
27 
TlabAllocator(Heap * heap)28 TlabAllocator::TlabAllocator(Heap *heap)
29     : heap_(heap), enableExpandYoung_(true)
30 {
31     size_t maxOldSpaceCapacity = heap->GetOldSpace()->GetMaximumCapacity();
32     localSpace_ = new LocalSpace(heap, maxOldSpaceCapacity, maxOldSpaceCapacity);
33     youngAllocator_.Reset();
34 }
35 
Finalize()36 inline void TlabAllocator::Finalize()
37 {
38     if (youngAllocator_.Available() != 0) {
39         FreeObject::FillFreeObject(heap_->GetEcmaVM(), youngAllocator_.GetTop(), youngAllocator_.Available());
40         youngAllocator_.Reset();
41     }
42 
43     heap_->MergeToOldSpaceSync(localSpace_);
44 }
45 
Allocate(size_t size,MemSpaceType space)46 uintptr_t TlabAllocator::Allocate(size_t size, MemSpaceType space)
47 {
48     uintptr_t result = 0;
49     switch (space) {
50         case SEMI_SPACE:
51             result = AllocateInYoungSpace(size);
52             break;
53         case OLD_SPACE:
54             result = AllocateInOldSpace(size);
55             break;
56         case COMPRESS_SPACE:
57             result = AllocateInCompressSpace(size);
58             break;
59         default:
60             UNREACHABLE();
61     }
62     return result;
63 }
64 
AllocateInYoungSpace(size_t size)65 uintptr_t TlabAllocator::AllocateInYoungSpace(size_t size)
66 {
67     ASSERT(AlignUp(size, static_cast<size_t>(MemAlignment::MEM_ALIGN_OBJECT)) == size);
68     if (UNLIKELY(size > SMALL_OBJECT_SIZE)) {
69         uintptr_t address = heap_->AllocateYoungSync(size);
70         return address;
71     }
72     uintptr_t result = youngAllocator_.Allocate(size);
73     if (result != 0) {
74         return result;
75     }
76     if (!enableExpandYoung_ || !ExpandYoung()) {
77         enableExpandYoung_ = false;
78         return 0;
79     }
80     return youngAllocator_.Allocate(size);
81 }
82 
AllocateInCompressSpace(size_t size)83 uintptr_t TlabAllocator::AllocateInCompressSpace(size_t size)
84 {
85     ASSERT(AlignUp(size, static_cast<size_t>(MemAlignment::MEM_ALIGN_OBJECT)) == size);
86     size = AlignUp(size, static_cast<size_t>(MemAlignment::MEM_ALIGN_OBJECT));
87     uintptr_t result = localSpace_->Allocate(size, true);
88     ASSERT(result != 0);
89     return result;
90 }
91 
AllocateInOldSpace(size_t size)92 uintptr_t TlabAllocator::AllocateInOldSpace(size_t size)
93 {
94     ASSERT(AlignUp(size, static_cast<size_t>(MemAlignment::MEM_ALIGN_OBJECT)) == size);
95     size = AlignUp(size, static_cast<size_t>(MemAlignment::MEM_ALIGN_OBJECT));
96     // 1. Allocate from freelist in compress allocator
97     uintptr_t result = localSpace_->Allocate(size, false);
98     if (result == 0) {
99         // 2. Expand region from old space
100         ExpandCompressFromOld(size);
101         result = localSpace_->Allocate(size, true);
102     }
103     ASSERT(result != 0);
104     return result;
105 }
106 
ExpandYoung()107 bool TlabAllocator::ExpandYoung()
108 {
109     uintptr_t buffer = heap_->AllocateYoungSync(MIN_BUFFER_SIZE);
110     if (buffer == 0) {
111         if (youngAllocator_.Available() != 0) {
112             FreeObject::FillFreeObject(heap_->GetEcmaVM(), youngAllocator_.GetTop(), youngAllocator_.Available());
113         }
114         return false;
115     }
116     uintptr_t end = buffer + MIN_BUFFER_SIZE;
117 
118     if (buffer == youngAllocator_.GetEnd()) {
119         buffer = youngAllocator_.GetTop();
120     } else {
121         if (youngAllocator_.Available() != 0) {
122             FreeObject::FillFreeObject(heap_->GetEcmaVM(), youngAllocator_.GetTop(), youngAllocator_.Available());
123         }
124     }
125     youngAllocator_.Reset(buffer, end);
126     return true;
127 }
128 
ExpandCompressFromOld(size_t size)129 bool TlabAllocator::ExpandCompressFromOld(size_t size)
130 {
131     auto region = heap_->GetOldSpace()->TryToGetExclusiveRegion(size);
132     if (region != nullptr) {
133         localSpace_->AddRegionToList(region);
134         return true;
135     }
136     return false;
137 }
138 }  // namespace panda::ecmascript
139 #endif  // ECMASCRIPT_MEM_TLAB_ALLOCATOR_INL_H
140