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/native_area_allocator.h"
17
18 #include "ecmascript/platform/os.h"
19
20 namespace panda::ecmascript {
AllocateArea(size_t capacity)21 Area *NativeAreaAllocator::AllocateArea(size_t capacity)
22 {
23 size_t headerSize = sizeof(Area);
24 if (capacity < headerSize) {
25 LOG_ECMA_MEM(FATAL) << "capacity must have a size not less than sizeof Area.";
26 UNREACHABLE();
27 }
28 if (cachedArea_ != nullptr && capacity <= cachedArea_->GetSize()) {
29 auto result = cachedArea_;
30 cachedArea_ = nullptr;
31 return result;
32 }
33 // NOLINTNEXTLINE(cppcoreguidelines-no-malloc)
34 void *mem = malloc(capacity);
35 if (mem == nullptr) {
36 LOG_ECMA_MEM(FATAL) << "malloc failed, current alloc size = " << capacity
37 << ", total allocated size = " << nativeMemoryUsage_.load(std::memory_order_relaxed);
38 UNREACHABLE();
39 }
40 #if ECMASCRIPT_ENABLE_ZAP_MEM
41 if (memset_s(mem, capacity, 0, capacity) != EOK) {
42 LOG_FULL(FATAL) << "memset_s failed";
43 UNREACHABLE();
44 }
45 #endif
46 IncreaseNativeMemoryUsage(capacity);
47 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
48 uintptr_t begin = reinterpret_cast<uintptr_t>(mem) + headerSize;
49 capacity -= headerSize;
50 return new (mem) Area(begin, capacity);
51 }
52
FreeArea(Area * area)53 void NativeAreaAllocator::FreeArea(Area *area)
54 {
55 if (area == nullptr) {
56 return;
57 }
58 if (cachedArea_ == nullptr && area->GetSize() <= MAX_CACHED_CHUNK_AREA_SIZE) {
59 cachedArea_ = area;
60 return;
61 }
62 auto size = area->GetSize() + sizeof(Area);
63 DecreaseNativeMemoryUsage(size);
64 #if ECMASCRIPT_ENABLE_ZAP_MEM
65 if (memset_s(area, size, INVALID_VALUE, size) != EOK) {
66 LOG_FULL(FATAL) << "memset_s failed";
67 UNREACHABLE();
68 }
69 #endif
70 // NOLINTNEXTLINE(cppcoreguidelines-no-malloc)
71 free(reinterpret_cast<std::byte *>(area));
72 }
73
Free(void * mem,size_t size)74 void NativeAreaAllocator::Free(void *mem, size_t size)
75 {
76 if (mem == nullptr) {
77 return;
78 }
79 DecreaseNativeMemoryUsage(size);
80 #if ECMASCRIPT_ENABLE_ZAP_MEM
81 if (memset_s(mem, size, INVALID_VALUE, size) != EOK) {
82 LOG_FULL(FATAL) << "memset_s failed";
83 UNREACHABLE();
84 }
85 #endif
86 // NOLINTNEXTLINE(cppcoreguidelines-no-malloc)
87 free(mem);
88 }
89
AllocateBuffer(size_t size)90 void *NativeAreaAllocator::AllocateBuffer(size_t size)
91 {
92 if (size == 0) {
93 LOG_ECMA_MEM(FATAL) << "size must have a size bigger than 0";
94 UNREACHABLE();
95 }
96 // NOLINTNEXTLINE(cppcoreguidelines-no-malloc)
97 void *ptr = malloc(size);
98 if (ptr == nullptr) {
99 LOG_ECMA_MEM(FATAL) << "malloc failed, current alloc size = " << size
100 << ", total allocated size = " << nativeMemoryUsage_.load(std::memory_order_relaxed);
101 UNREACHABLE();
102 }
103 #if ECMASCRIPT_ENABLE_ZAP_MEM
104 if (memset_s(ptr, size, INVALID_VALUE, size) != EOK) {
105 LOG_FULL(FATAL) << "memset_s failed";
106 UNREACHABLE();
107 }
108 #endif
109 IncreaseNativeMemoryUsage(MallocUsableSize(ptr));
110 return ptr;
111 }
112
FreeBuffer(void * mem)113 void NativeAreaAllocator::FreeBuffer(void *mem)
114 {
115 if (mem == nullptr) {
116 return;
117 }
118 size_t size = MallocUsableSize(mem);
119 DecreaseNativeMemoryUsage(size);
120
121 #if ECMASCRIPT_ENABLE_ZAP_MEM
122 if (memset_s(mem, size, INVALID_VALUE, size) != EOK) {
123 LOG_FULL(FATAL) << "memset_s failed";
124 UNREACHABLE();
125 }
126 #endif
127 // NOLINTNEXTLINE(cppcoreguidelines-no-malloc)
128 free(mem);
129 }
130
FreeBufferFunc(void * env,void * buffer,void * data)131 void NativeAreaAllocator::FreeBufferFunc([[maybe_unused]] void *env, void *buffer, void *data)
132 {
133 if (buffer == nullptr || data == nullptr) {
134 return;
135 }
136 NativeAreaAllocator* allocator = reinterpret_cast<NativeAreaAllocator*>(data);
137 allocator->FreeBuffer(buffer);
138 }
139 } // namespace panda::ecmascript
140