• 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 #include "ecmascript/mem/chunk.h"
17 
18 #include "ecmascript/mem/heap.h"
19 
20 namespace panda::ecmascript {
Chunk(NativeAreaAllocator * allocator)21 Chunk::Chunk(NativeAreaAllocator *allocator) : allocator_(allocator) {}
22 
NewArea(size_t size)23 Area *Chunk::NewArea(size_t size)
24 {
25     auto area = allocator_->AllocateArea(size);
26     if (area == nullptr) {
27         LOG_ECMA_MEM(FATAL) << "OOM Chunk : NewArea area is nullptr";
28         UNREACHABLE();
29     }
30     areaList_.AddNode(area);
31     currentArea_ = area;
32     return area;
33 }
34 
Expand(size_t size)35 uintptr_t Chunk::Expand(size_t size)
36 {
37     ASSERT(end_ - ptr_ < size);
38 
39     Area *head = currentArea_;
40     size_t newSize;
41     if (head != nullptr) {
42         // NOLINTNEXTLINE(hicpp-signed-bitwise)
43         newSize = size + (head->GetSize() << 1);
44     } else {
45         newSize = sizeof(Area) + MEM_ALIGN + size;
46     }
47 
48     if (newSize < MIN_CHUNK_AREA_SIZE) {
49         newSize = MIN_CHUNK_AREA_SIZE;
50     } else if (newSize > MAX_CHUNK_AREA_SIZE) {
51         size_t minNewSize = sizeof(Area) + MEM_ALIGN + size;
52         newSize = std::max(minNewSize, MAX_CHUNK_AREA_SIZE);
53     }
54 
55     if (newSize > static_cast<size_t>(std::numeric_limits<int>::max())) {
56         LOG_ECMA_MEM(FATAL) << "OOM chunk : newSize is "<< newSize << ", size is " << size;
57         UNREACHABLE();
58     }
59 
60     Area *area = NewArea(newSize);
61     if (area == nullptr) {
62         LOG_ECMA_MEM(FATAL) << "OOM chunk : NewArea area is nullptr";
63         UNREACHABLE();
64     }
65     uintptr_t result = AlignUp(area->GetBegin(), MEM_ALIGN);
66     ptr_ = result + size;
67     end_ = area->GetEnd();
68     return result;
69 }
70 
ReleaseMemory()71 void Chunk::ReleaseMemory()
72 {
73     while (!areaList_.IsEmpty()) {
74         Area *node = areaList_.PopBack();
75         allocator_->FreeArea(node);
76     }
77     ptr_ = 0;
78     end_ = 0;
79 }
80 }  // namespace panda::ecmascript
81