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