• 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             LOG_ECMA(FATAL) << "this branch is unreachable";
61             UNREACHABLE();
62     }
63     return result;
64 }
65 
AllocateInYoungSpace(size_t size)66 uintptr_t TlabAllocator::AllocateInYoungSpace(size_t size)
67 {
68     ASSERT(AlignUp(size, static_cast<size_t>(MemAlignment::MEM_ALIGN_OBJECT)) == size);
69     if (UNLIKELY(size > SMALL_OBJECT_SIZE)) {
70         uintptr_t address = heap_->AllocateYoungSync(size);
71         return address;
72     }
73     uintptr_t result = youngAllocator_.Allocate(size);
74     if (result != 0) {
75         return result;
76     }
77     if (!enableExpandYoung_ || !ExpandYoung()) {
78         enableExpandYoung_ = false;
79         return 0;
80     }
81     return youngAllocator_.Allocate(size);
82 }
83 
AllocateInCompressSpace(size_t size)84 uintptr_t TlabAllocator::AllocateInCompressSpace(size_t size)
85 {
86     ASSERT(AlignUp(size, static_cast<size_t>(MemAlignment::MEM_ALIGN_OBJECT)) == size);
87     size = AlignUp(size, static_cast<size_t>(MemAlignment::MEM_ALIGN_OBJECT));
88     uintptr_t result = localSpace_->Allocate(size, true);
89     ASSERT(result != 0);
90     return result;
91 }
92 
AllocateInOldSpace(size_t size)93 uintptr_t TlabAllocator::AllocateInOldSpace(size_t size)
94 {
95     ASSERT(AlignUp(size, static_cast<size_t>(MemAlignment::MEM_ALIGN_OBJECT)) == size);
96     size = AlignUp(size, static_cast<size_t>(MemAlignment::MEM_ALIGN_OBJECT));
97     // 1. Allocate from freelist in compress allocator
98     uintptr_t result = localSpace_->Allocate(size, false);
99     if (result == 0) {
100         // 2. Expand region from old space
101         ExpandCompressFromOld(size);
102         result = localSpace_->Allocate(size, true);
103     }
104     ASSERT(result != 0);
105     return result;
106 }
107 
ExpandYoung()108 bool TlabAllocator::ExpandYoung()
109 {
110     uintptr_t buffer = heap_->AllocateYoungSync(MIN_BUFFER_SIZE);
111     if (buffer == 0) {
112         if (youngAllocator_.Available() != 0) {
113             FreeObject::FillFreeObject(heap_->GetEcmaVM(), youngAllocator_.GetTop(), youngAllocator_.Available());
114         }
115         return false;
116     }
117     uintptr_t end = buffer + MIN_BUFFER_SIZE;
118 
119     if (buffer == youngAllocator_.GetEnd()) {
120         buffer = youngAllocator_.GetTop();
121     } else {
122         if (youngAllocator_.Available() != 0) {
123             FreeObject::FillFreeObject(heap_->GetEcmaVM(), youngAllocator_.GetTop(), youngAllocator_.Available());
124         }
125     }
126     youngAllocator_.Reset(buffer, end);
127     return true;
128 }
129 
ExpandCompressFromOld(size_t size)130 bool TlabAllocator::ExpandCompressFromOld(size_t size)
131 {
132     auto region = heap_->GetOldSpace()->TryToGetExclusiveRegion(size);
133     if (region != nullptr) {
134         localSpace_->AddRegionToList(region);
135         return true;
136     }
137     return false;
138 }
139 }  // namespace panda::ecmascript
140 #endif  // ECMASCRIPT_MEM_TLAB_ALLOCATOR_INL_H
141