/* * Copyright (c) 2021 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "ecmascript/mem/native_area_allocator.h" #include "ecmascript/platform/os.h" #include "libpandabase/os/mem.h" namespace panda::ecmascript { Area *NativeAreaAllocator::AllocateArea(size_t capacity) { size_t headerSize = sizeof(Area); if (capacity < headerSize) { LOG_ECMA_MEM(FATAL) << "capacity must have a size not less than sizeof Area."; UNREACHABLE(); } if (cachedArea_ != nullptr && capacity <= cachedArea_->GetSize()) { auto result = cachedArea_; cachedArea_ = nullptr; return result; } // NOLINTNEXTLINE(cppcoreguidelines-no-malloc) void *mem = malloc(capacity); if (mem == nullptr) { LOG_ECMA_MEM(FATAL) << "malloc failed, current alloc size = " << capacity << ", total allocated size = " << nativeMemoryUsage_.load(std::memory_order_relaxed); UNREACHABLE(); } #if ECMASCRIPT_ENABLE_ZAP_MEM if (memset_s(mem, capacity, 0, capacity) != EOK) { LOG_FULL(FATAL) << "memset_s failed"; UNREACHABLE(); } #endif IncreaseNativeMemoryUsage(capacity); // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) uintptr_t begin = reinterpret_cast(mem) + headerSize; capacity -= headerSize; return new (mem) Area(begin, capacity); } void NativeAreaAllocator::FreeArea(Area *area) { if (area == nullptr) { return; } if (cachedArea_ == nullptr && area->GetSize() <= MAX_CACHED_CHUNK_AREA_SIZE) { cachedArea_ = area; return; } auto size = area->GetSize() + sizeof(Area); DecreaseNativeMemoryUsage(size); #if ECMASCRIPT_ENABLE_ZAP_MEM if (memset_s(area, size, INVALID_VALUE, size) != EOK) { LOG_FULL(FATAL) << "memset_s failed"; UNREACHABLE(); } #endif // NOLINTNEXTLINE(cppcoreguidelines-no-malloc) free(reinterpret_cast(area)); } void NativeAreaAllocator::Free(void *mem, size_t size) { if (mem == nullptr) { return; } DecreaseNativeMemoryUsage(size); #if ECMASCRIPT_ENABLE_ZAP_MEM if (memset_s(mem, size, INVALID_VALUE, size) != EOK) { LOG_FULL(FATAL) << "memset_s failed"; UNREACHABLE(); } #endif // NOLINTNEXTLINE(cppcoreguidelines-no-malloc) free(mem); } void *NativeAreaAllocator::AllocateBuffer(size_t size) { if (size == 0) { LOG_ECMA_MEM(FATAL) << "size must have a size bigger than 0"; UNREACHABLE(); } // NOLINTNEXTLINE(cppcoreguidelines-no-malloc) void *ptr = malloc(size); if (ptr == nullptr) { LOG_ECMA_MEM(FATAL) << "malloc failed, current alloc size = " << size << ", total allocated size = " << nativeMemoryUsage_.load(std::memory_order_relaxed); UNREACHABLE(); } #if ECMASCRIPT_ENABLE_ZAP_MEM if (memset_s(ptr, size, INVALID_VALUE, size) != EOK) { LOG_FULL(FATAL) << "memset_s failed"; UNREACHABLE(); } #endif IncreaseNativeMemoryUsage(MallocUsableSize(ptr)); return ptr; } void NativeAreaAllocator::FreeBuffer(void *mem) { if (mem == nullptr) { return; } DecreaseNativeMemoryUsage(MallocUsableSize(mem)); #if ECMASCRIPT_ENABLE_ZAP_MEM if (memset_s(mem, size, INVALID_VALUE, size) != EOK) { LOG_FULL(FATAL) << "memset_s failed"; UNREACHABLE(); } #endif // NOLINTNEXTLINE(cppcoreguidelines-no-malloc) free(mem); } void NativeAreaAllocator::FreeBufferFunc(void *buffer, void *data) { if (buffer == nullptr || data == nullptr) { return; } NativeAreaAllocator* allocator = reinterpret_cast(data); allocator->FreeBuffer(buffer); } } // namespace panda::ecmascript